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, ni->ni_ies.vhtcap_ie,
1048 ni->ni_ies.vhtopmode_ie);
1049 do_ht = 1;
1050 }
1051 }
1052
1053 /* Finally do the node channel change */
1054 if (do_ht) {
1055 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1056 ni->ni_ies.htinfo_ie);
1057 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
1058 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1059 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
1060 }
1061
1062 /* XXX else check for ath FF? */
1063 /* XXX QoS? Difficult given that WME config is specific to a master */
1064
1065 ieee80211_node_setuptxparms(ni);
1066 ieee80211_ratectl_node_init(ni);
1067
1068 return ieee80211_sta_join1(ieee80211_ref_node(ni));
1069 }
1070
1071 /*
1072 * Leave the specified IBSS/BSS network. The node is assumed to
1073 * be passed in with a held reference.
1074 */
1075 void
ieee80211_sta_leave(struct ieee80211_node * ni)1076 ieee80211_sta_leave(struct ieee80211_node *ni)
1077 {
1078 struct ieee80211com *ic = ni->ni_ic;
1079
1080 ic->ic_node_cleanup(ni);
1081 ieee80211_notify_node_leave(ni);
1082 }
1083
1084 /*
1085 * Send a deauthenticate frame and drop the station.
1086 */
1087 void
ieee80211_node_deauth(struct ieee80211_node * ni,int reason)1088 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
1089 {
1090 /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
1091 ieee80211_ref_node(ni);
1092 if (ni->ni_associd != 0)
1093 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1094 ieee80211_node_leave(ni);
1095 ieee80211_free_node(ni);
1096 }
1097
1098 static struct ieee80211_node *
node_alloc(struct ieee80211vap * vap,const uint8_t macaddr[IEEE80211_ADDR_LEN])1099 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1100 {
1101 struct ieee80211_node *ni;
1102
1103 ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
1104 M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1105 return ni;
1106 }
1107
1108 static int
node_init(struct ieee80211_node * ni)1109 node_init(struct ieee80211_node *ni)
1110 {
1111 return 0;
1112 }
1113
1114 /*
1115 * Initialize an ie blob with the specified data. If previous
1116 * data exists re-use the data block. As a side effect we clear
1117 * all references to specific ie's; the caller is required to
1118 * recalculate them.
1119 */
1120 int
ieee80211_ies_init(struct ieee80211_ies * ies,const uint8_t * data,int len)1121 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
1122 {
1123 /* NB: assumes data+len are the last fields */
1124 memset(ies, 0, offsetof(struct ieee80211_ies, data));
1125 if (ies->data != NULL && ies->len != len) {
1126 /* data size changed */
1127 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1128 ies->data = NULL;
1129 }
1130 if (ies->data == NULL) {
1131 ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
1132 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1133 if (ies->data == NULL) {
1134 ies->len = 0;
1135 /* NB: pointers have already been zero'd above */
1136 return 0;
1137 }
1138 }
1139 memcpy(ies->data, data, len);
1140 ies->len = len;
1141 return 1;
1142 }
1143
1144 /*
1145 * Reclaim storage for an ie blob.
1146 */
1147 void
ieee80211_ies_cleanup(struct ieee80211_ies * ies)1148 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
1149 {
1150 if (ies->data != NULL)
1151 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1152 }
1153
1154 /*
1155 * Expand an ie blob data contents and to fillin individual
1156 * ie pointers. The data blob is assumed to be well-formed;
1157 * we don't do any validity checking of ie lengths.
1158 */
1159 void
ieee80211_ies_expand(struct ieee80211_ies * ies)1160 ieee80211_ies_expand(struct ieee80211_ies *ies)
1161 {
1162 uint8_t *ie;
1163 int ielen;
1164
1165 ie = ies->data;
1166 ielen = ies->len;
1167 while (ielen > 1) {
1168 /* Make sure the given IE length fits into the total length. */
1169 if ((2 + ie[1]) > ielen) {
1170 printf("%s: malformed IEs! ies %p { data %p len %d }: "
1171 "ie %u len 2+%u > total len left %d\n",
1172 __func__, ies, ies->data, ies->len,
1173 ie[0], ie[1], ielen);
1174 return;
1175 }
1176 switch (ie[0]) {
1177 case IEEE80211_ELEMID_VENDOR:
1178 if (iswpaoui(ie))
1179 ies->wpa_ie = ie;
1180 else if (iswmeoui(ie))
1181 ies->wme_ie = ie;
1182 #ifdef IEEE80211_SUPPORT_SUPERG
1183 else if (isatherosoui(ie))
1184 ies->ath_ie = ie;
1185 #endif
1186 #ifdef IEEE80211_SUPPORT_TDMA
1187 else if (istdmaoui(ie))
1188 ies->tdma_ie = ie;
1189 #endif
1190 break;
1191 case IEEE80211_ELEMID_RSN:
1192 ies->rsn_ie = ie;
1193 break;
1194 case IEEE80211_ELEMID_HTCAP:
1195 ies->htcap_ie = ie;
1196 break;
1197 case IEEE80211_ELEMID_HTINFO:
1198 ies->htinfo_ie = ie;
1199 break;
1200 #ifdef IEEE80211_SUPPORT_MESH
1201 case IEEE80211_ELEMID_MESHID:
1202 ies->meshid_ie = ie;
1203 break;
1204 #endif
1205 case IEEE80211_ELEMID_VHT_CAP:
1206 ies->vhtcap_ie = ie;
1207 break;
1208 case IEEE80211_ELEMID_VHT_OPMODE:
1209 ies->vhtopmode_ie = ie;
1210 break;
1211 case IEEE80211_ELEMID_VHT_PWR_ENV:
1212 ies->vhtpwrenv_ie = ie;
1213 break;
1214 case IEEE80211_ELEMID_BSSLOAD:
1215 ies->bssload_ie = ie;
1216 break;
1217 case IEEE80211_ELEMID_APCHANREP:
1218 ies->apchanrep_ie = ie;
1219 break;
1220 }
1221 ielen -= 2 + ie[1];
1222 ie += 2 + ie[1];
1223 }
1224 }
1225
1226 /*
1227 * Reclaim any resources in a node and reset any critical
1228 * state. Typically nodes are free'd immediately after,
1229 * but in some cases the storage may be reused so we need
1230 * to insure consistent state (should probably fix that).
1231 */
1232 static void
node_cleanup(struct ieee80211_node * ni)1233 node_cleanup(struct ieee80211_node *ni)
1234 {
1235 struct ieee80211vap *vap = ni->ni_vap;
1236 struct ieee80211com *ic = ni->ni_ic;
1237 int i;
1238
1239 /* NB: preserve ni_table */
1240 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1241 if (vap->iv_opmode != IEEE80211_M_STA)
1242 vap->iv_ps_sta--;
1243 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
1244 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
1245 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
1246 }
1247 /*
1248 * Cleanup any VHT and HT-related state.
1249 */
1250 if (ni->ni_flags & IEEE80211_NODE_VHT)
1251 ieee80211_vht_node_cleanup(ni);
1252 if (ni->ni_flags & IEEE80211_NODE_HT)
1253 ieee80211_ht_node_cleanup(ni);
1254 #ifdef IEEE80211_SUPPORT_SUPERG
1255 /* Always do FF node cleanup; for A-MSDU */
1256 ieee80211_ff_node_cleanup(ni);
1257 #endif
1258 #ifdef IEEE80211_SUPPORT_MESH
1259 /*
1260 * Cleanup any mesh-related state.
1261 */
1262 if (vap->iv_opmode == IEEE80211_M_MBSS)
1263 ieee80211_mesh_node_cleanup(ni);
1264 #endif
1265 /*
1266 * Clear any staging queue entries.
1267 */
1268 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1269
1270 /*
1271 * Clear AREF flag that marks the authorization refcnt bump
1272 * has happened. This is probably not needed as the node
1273 * should always be removed from the table so not found but
1274 * do it just in case.
1275 * Likewise clear the ASSOCID flag as these flags are intended
1276 * to be managed in tandem.
1277 */
1278 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1279
1280 /*
1281 * Drain power save queue and, if needed, clear TIM.
1282 */
1283 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1284 vap->iv_set_tim(ni, 0);
1285
1286 ni->ni_associd = 0;
1287 if (ni->ni_challenge != NULL) {
1288 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1289 ni->ni_challenge = NULL;
1290 }
1291 /*
1292 * Preserve SSID, WPA, and WME ie's so the bss node is
1293 * reusable during a re-auth/re-assoc state transition.
1294 * If we remove these data they will not be recreated
1295 * because they come from a probe-response or beacon frame
1296 * which cannot be expected prior to the association-response.
1297 * This should not be an issue when operating in other modes
1298 * as stations leaving always go through a full state transition
1299 * which will rebuild this state.
1300 *
1301 * XXX does this leave us open to inheriting old state?
1302 */
1303 for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1304 if (ni->ni_rxfrag[i] != NULL) {
1305 m_freem(ni->ni_rxfrag[i]);
1306 ni->ni_rxfrag[i] = NULL;
1307 }
1308 /*
1309 * Must be careful here to remove any key map entry w/o a LOR.
1310 */
1311 ieee80211_node_delucastkey(ni);
1312 }
1313
1314 static void
node_free(struct ieee80211_node * ni)1315 node_free(struct ieee80211_node *ni)
1316 {
1317 struct ieee80211com *ic = ni->ni_ic;
1318
1319 ieee80211_ratectl_node_deinit(ni);
1320 ic->ic_node_cleanup(ni);
1321 ieee80211_ies_cleanup(&ni->ni_ies);
1322 ieee80211_psq_cleanup(&ni->ni_psq);
1323 IEEE80211_FREE(ni, M_80211_NODE);
1324 }
1325
1326 static void
node_age(struct ieee80211_node * ni)1327 node_age(struct ieee80211_node *ni)
1328 {
1329 struct ieee80211vap *vap = ni->ni_vap;
1330
1331 /*
1332 * Age frames on the power save queue.
1333 */
1334 if (ieee80211_node_psq_age(ni) != 0 &&
1335 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1336 vap->iv_set_tim(ni, 0);
1337 /*
1338 * Age out HT resources (e.g. frames on the
1339 * A-MPDU reorder queues).
1340 */
1341 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1342 ieee80211_ht_node_age(ni);
1343 }
1344
1345 static int8_t
node_getrssi(const struct ieee80211_node * ni)1346 node_getrssi(const struct ieee80211_node *ni)
1347 {
1348 uint32_t avgrssi = ni->ni_avgrssi;
1349 int32_t rssi;
1350
1351 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1352 return 0;
1353 rssi = IEEE80211_RSSI_GET(avgrssi);
1354 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1355 }
1356
1357 static void
node_getsignal(const struct ieee80211_node * ni,int8_t * rssi,int8_t * noise)1358 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1359 {
1360 *rssi = node_getrssi(ni);
1361 *noise = ni->ni_noise;
1362 }
1363
1364 static void
node_getmimoinfo(const struct ieee80211_node * ni,struct ieee80211_mimo_info * info)1365 node_getmimoinfo(const struct ieee80211_node *ni,
1366 struct ieee80211_mimo_info *info)
1367 {
1368 int i;
1369 uint32_t avgrssi;
1370 int32_t rssi;
1371
1372 bzero(info, sizeof(*info));
1373
1374 for (i = 0; i < MIN(IEEE80211_MAX_CHAINS, ni->ni_mimo_chains); i++) {
1375 /* Note: for now, just pri20 channel info */
1376 avgrssi = ni->ni_mimo_rssi_ctl[i];
1377 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1378 info->ch[i].rssi[0] = 0;
1379 } else {
1380 rssi = IEEE80211_RSSI_GET(avgrssi);
1381 info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1382 }
1383 info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i];
1384 }
1385
1386 /* XXX ext radios? */
1387
1388 /* XXX EVM? */
1389 }
1390
1391 static void
ieee80211_add_node_nt(struct ieee80211_node_table * nt,struct ieee80211_node * ni)1392 ieee80211_add_node_nt(struct ieee80211_node_table *nt,
1393 struct ieee80211_node *ni)
1394 {
1395 struct ieee80211com *ic = nt->nt_ic;
1396 int hash;
1397
1398 IEEE80211_NODE_LOCK_ASSERT(nt);
1399
1400 hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr);
1401 (void) ic; /* XXX IEEE80211_NODE_HASH */
1402 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1403 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1404 nt->nt_count++;
1405 ni->ni_table = nt;
1406 }
1407
1408 static void
ieee80211_del_node_nt(struct ieee80211_node_table * nt,struct ieee80211_node * ni)1409 ieee80211_del_node_nt(struct ieee80211_node_table *nt,
1410 struct ieee80211_node *ni)
1411 {
1412
1413 IEEE80211_NODE_LOCK_ASSERT(nt);
1414
1415 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1416 LIST_REMOVE(ni, ni_hash);
1417 nt->nt_count--;
1418 KASSERT(nt->nt_count >= 0,
1419 ("nt_count is negative (%d)!\n", nt->nt_count));
1420 ni->ni_table = NULL;
1421 }
1422
1423 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)1424 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1425 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN],
1426 const char *func __debrefcnt_used, int line __debrefcnt_used)
1427 {
1428 struct ieee80211com *ic = nt->nt_ic;
1429 struct ieee80211_node *ni;
1430
1431 ni = ic->ic_node_alloc(vap, macaddr);
1432 if (ni == NULL) {
1433 vap->iv_stats.is_rx_nodealloc++;
1434 return NULL;
1435 }
1436
1437 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1438 "%s %p<%s> in %s table\n", __func__, ni,
1439 ether_sprintf(macaddr), nt->nt_name);
1440
1441 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1442 ieee80211_node_initref(ni); /* mark referenced */
1443 #ifdef IEEE80211_DEBUG_REFCNT
1444 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1445 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1446 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1447 #endif
1448 ni->ni_chan = IEEE80211_CHAN_ANYC;
1449 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1450 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1451 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1452 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1453 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1454 ni->ni_inact_reload = nt->nt_inact_init;
1455 ni->ni_inact = ni->ni_inact_reload;
1456 ni->ni_ath_defkeyix = 0x7fff;
1457 ieee80211_psq_init(&ni->ni_psq, "unknown");
1458 #ifdef IEEE80211_SUPPORT_MESH
1459 if (vap->iv_opmode == IEEE80211_M_MBSS)
1460 ieee80211_mesh_node_init(vap, ni);
1461 #endif
1462 IEEE80211_NODE_LOCK(nt);
1463 ieee80211_add_node_nt(nt, ni);
1464 ni->ni_vap = vap;
1465 ni->ni_ic = ic;
1466 IEEE80211_NODE_UNLOCK(nt);
1467
1468 /* handle failure; free node state */
1469 if (ic->ic_node_init(ni) != 0) {
1470 vap->iv_stats.is_rx_nodealloc++;
1471 ieee80211_psq_cleanup(&ni->ni_psq);
1472 ieee80211_ratectl_node_deinit(ni);
1473 _ieee80211_free_node(ni);
1474 return NULL;
1475 }
1476
1477 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1478 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1479
1480 return ni;
1481 }
1482
1483 /*
1484 * Craft a temporary node suitable for sending a management frame
1485 * to the specified station. We craft only as much state as we
1486 * need to do the work since the node will be immediately reclaimed
1487 * once the send completes.
1488 */
1489 struct ieee80211_node *
ieee80211_tmp_node(struct ieee80211vap * vap,const uint8_t macaddr[IEEE80211_ADDR_LEN])1490 ieee80211_tmp_node(struct ieee80211vap *vap,
1491 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1492 {
1493 struct ieee80211com *ic = vap->iv_ic;
1494 struct ieee80211_node *ni;
1495
1496 ni = ic->ic_node_alloc(vap, macaddr);
1497 if (ni != NULL) {
1498 struct ieee80211_node *bss = vap->iv_bss;
1499
1500 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1501 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1502
1503 ni->ni_table = NULL; /* NB: pedantic */
1504 ni->ni_ic = ic; /* NB: needed to set channel */
1505 ni->ni_vap = vap;
1506
1507 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1508 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1509 ieee80211_node_initref(ni); /* mark referenced */
1510 #ifdef IEEE80211_DEBUG_REFCNT
1511 /* Only one caller so we skip func/line passing into the func. */
1512 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1513 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, "", -1, ni,
1514 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1515 #endif
1516 /* NB: required by ieee80211_fix_rate */
1517 ieee80211_node_set_chan(ni, bss->ni_chan);
1518 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1519 IEEE80211_KEYIX_NONE);
1520 ni->ni_txpower = bss->ni_txpower;
1521 /* XXX optimize away */
1522 ieee80211_psq_init(&ni->ni_psq, "unknown");
1523
1524 ieee80211_ratectl_node_init(ni);
1525
1526 /* handle failure; free node state */
1527 if (ic->ic_node_init(ni) != 0) {
1528 vap->iv_stats.is_rx_nodealloc++;
1529 ieee80211_psq_cleanup(&ni->ni_psq);
1530 ieee80211_ratectl_node_deinit(ni);
1531 _ieee80211_free_node(ni);
1532 return NULL;
1533 }
1534
1535 } else {
1536 /* XXX msg */
1537 vap->iv_stats.is_rx_nodealloc++;
1538 }
1539 return ni;
1540 }
1541
1542 struct ieee80211_node *
ieee80211_dup_bss(struct ieee80211vap * vap,const uint8_t macaddr[IEEE80211_ADDR_LEN])1543 ieee80211_dup_bss(struct ieee80211vap *vap,
1544 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1545 {
1546 struct ieee80211com *ic = vap->iv_ic;
1547 struct ieee80211_node *ni;
1548
1549 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr, __func__, __LINE__);
1550 if (ni != NULL) {
1551 struct ieee80211_node *bss = vap->iv_bss;
1552 /*
1553 * Inherit from iv_bss.
1554 */
1555 copy_bss(ni, bss);
1556 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1557 ieee80211_node_set_chan(ni, bss->ni_chan);
1558 }
1559 return ni;
1560 }
1561
1562 /*
1563 * Create a bss node for a legacy WDS vap. The far end does
1564 * not associate so we just create create a new node and
1565 * simulate an association. The caller is responsible for
1566 * installing the node as the bss node and handling any further
1567 * setup work like authorizing the port.
1568 */
1569 struct ieee80211_node *
ieee80211_node_create_wds(struct ieee80211vap * vap,const uint8_t bssid[IEEE80211_ADDR_LEN],struct ieee80211_channel * chan)1570 ieee80211_node_create_wds(struct ieee80211vap *vap,
1571 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1572 {
1573 struct ieee80211com *ic = vap->iv_ic;
1574 struct ieee80211_node *ni;
1575
1576 /* XXX check if node already in sta table? */
1577 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid, __func__, __LINE__);
1578 if (ni != NULL) {
1579 ni->ni_wdsvap = vap;
1580 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1581 /*
1582 * Inherit any manually configured settings.
1583 */
1584 copy_bss(ni, vap->iv_bss);
1585 ieee80211_node_set_chan(ni, chan);
1586 /* NB: propagate ssid so available to WPA supplicant */
1587 ni->ni_esslen = vap->iv_des_ssid[0].len;
1588 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1589 /* NB: no associd for peer */
1590 /*
1591 * There are no management frames to use to
1592 * discover neighbor capabilities, so blindly
1593 * propagate the local configuration.
1594 */
1595 if (vap->iv_flags & IEEE80211_F_WME)
1596 ni->ni_flags |= IEEE80211_NODE_QOS;
1597 #ifdef IEEE80211_SUPPORT_SUPERG
1598 if (vap->iv_flags & IEEE80211_F_FF)
1599 ni->ni_flags |= IEEE80211_NODE_FF;
1600 #endif
1601 /* XXX VHT */
1602 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1603 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1604 /*
1605 * Device is HT-capable and HT is enabled for
1606 * the vap; setup HT operation. On return
1607 * ni_chan will be adjusted to an HT channel.
1608 */
1609 ieee80211_ht_wds_init(ni);
1610 if (vap->iv_vht_flags & IEEE80211_FVHT_VHT) {
1611 printf("%s: TODO: vht_wds_init\n", __func__);
1612 }
1613 } else {
1614 struct ieee80211_channel *c = ni->ni_chan;
1615 /*
1616 * Force a legacy channel to be used.
1617 */
1618 c = ieee80211_find_channel(ic,
1619 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1620 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1621 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1622 ni->ni_chan = c;
1623 }
1624 }
1625 return ni;
1626 }
1627
1628 struct ieee80211_node *
1629 #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_find_node_locked_debug(struct ieee80211_node_table * nt,const uint8_t macaddr[IEEE80211_ADDR_LEN],const char * func,int line)1630 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1631 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1632 #else
1633 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1634 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1635 #endif
1636 {
1637 struct ieee80211_node *ni;
1638 int hash;
1639
1640 IEEE80211_NODE_LOCK_ASSERT(nt);
1641
1642 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1643 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1644 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1645 ieee80211_ref_node(ni); /* mark referenced */
1646 #ifdef IEEE80211_DEBUG_REFCNT
1647 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1648 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1649 func, line,
1650 ni, ether_sprintf(ni->ni_macaddr),
1651 ieee80211_node_refcnt(ni));
1652 #endif
1653 return ni;
1654 }
1655 }
1656 return NULL;
1657 }
1658
1659 struct ieee80211_node *
1660 #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_find_node_debug(struct ieee80211_node_table * nt,const uint8_t macaddr[IEEE80211_ADDR_LEN],const char * func,int line)1661 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1662 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1663 #else
1664 ieee80211_find_node(struct ieee80211_node_table *nt,
1665 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1666 #endif
1667 {
1668 struct ieee80211_node *ni;
1669
1670 IEEE80211_NODE_LOCK(nt);
1671 ni = ieee80211_find_node_locked(nt, macaddr);
1672 IEEE80211_NODE_UNLOCK(nt);
1673 return ni;
1674 }
1675
1676 struct ieee80211_node *
1677 #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table * nt,const struct ieee80211vap * vap,const uint8_t macaddr[IEEE80211_ADDR_LEN],const char * func,int line)1678 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1679 const struct ieee80211vap *vap,
1680 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1681 #else
1682 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1683 const struct ieee80211vap *vap,
1684 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1685 #endif
1686 {
1687 struct ieee80211_node *ni;
1688 int hash;
1689
1690 IEEE80211_NODE_LOCK_ASSERT(nt);
1691
1692 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1693 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1694 if (ni->ni_vap == vap &&
1695 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1696 ieee80211_ref_node(ni); /* mark referenced */
1697 #ifdef IEEE80211_DEBUG_REFCNT
1698 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1699 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1700 func, line,
1701 ni, ether_sprintf(ni->ni_macaddr),
1702 ieee80211_node_refcnt(ni));
1703 #endif
1704 return ni;
1705 }
1706 }
1707 return NULL;
1708 }
1709
1710 struct ieee80211_node *
1711 #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_find_vap_node_debug(struct ieee80211_node_table * nt,const struct ieee80211vap * vap,const uint8_t macaddr[IEEE80211_ADDR_LEN],const char * func,int line)1712 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1713 const struct ieee80211vap *vap,
1714 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1715 #else
1716 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1717 const struct ieee80211vap *vap,
1718 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1719 #endif
1720 {
1721 struct ieee80211_node *ni;
1722
1723 IEEE80211_NODE_LOCK(nt);
1724 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1725 IEEE80211_NODE_UNLOCK(nt);
1726 return ni;
1727 }
1728
1729 /*
1730 * Fake up a node; this handles node discovery in adhoc mode.
1731 * Note that for the driver's benefit we treat this like
1732 * an association so the driver has an opportunity to setup
1733 * it's private state.
1734 */
1735 struct ieee80211_node *
ieee80211_fakeup_adhoc_node(struct ieee80211vap * vap,const uint8_t macaddr[IEEE80211_ADDR_LEN])1736 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1737 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1738 {
1739 struct ieee80211_node *ni;
1740
1741 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1742 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1743 ni = ieee80211_dup_bss(vap, macaddr);
1744 if (ni != NULL) {
1745 struct ieee80211com *ic = vap->iv_ic;
1746
1747 /* XXX no rate negotiation; just dup */
1748 ni->ni_rates = vap->iv_bss->ni_rates;
1749 if (ieee80211_iserp_rateset(&ni->ni_rates))
1750 ni->ni_flags |= IEEE80211_NODE_ERP;
1751 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1752 /*
1753 * In adhoc demo mode there are no management
1754 * frames to use to discover neighbor capabilities,
1755 * so blindly propagate the local configuration
1756 * so we can do interesting things (e.g. use
1757 * WME to disable ACK's).
1758 */
1759 /*
1760 * XXX TODO: 11n?
1761 */
1762 if (vap->iv_flags & IEEE80211_F_WME)
1763 ni->ni_flags |= IEEE80211_NODE_QOS;
1764 #ifdef IEEE80211_SUPPORT_SUPERG
1765 if (vap->iv_flags & IEEE80211_F_FF)
1766 ni->ni_flags |= IEEE80211_NODE_FF;
1767 #endif
1768 }
1769 ieee80211_node_setuptxparms(ni);
1770 ieee80211_ratectl_node_init(ni);
1771
1772 /*
1773 * XXX TODO: 11n? At least 20MHz, at least A-MPDU RX,
1774 * not A-MPDU TX; not 11n rates, etc. We'll cycle
1775 * that after we hear that we can indeed do 11n
1776 * (either by a beacon frame or by a probe response.)
1777 */
1778
1779 /*
1780 * This is the first time we see the node.
1781 */
1782 if (ic->ic_newassoc != NULL)
1783 ic->ic_newassoc(ni, 1);
1784
1785 /*
1786 * Kick off a probe request to the given node;
1787 * we will then use the probe response to update
1788 * 11n/etc configuration state.
1789 *
1790 * XXX TODO: this isn't guaranteed, and until we get
1791 * a probe response, we won't be able to actually
1792 * do anything 802.11n related to the node.
1793 * So if this does indeed work, maybe we should hold
1794 * off on sending responses until we get the probe
1795 * response, or just default to some sensible subset
1796 * of 802.11n behaviour (eg always allow aggregation
1797 * negotiation TO us, but not FROM us, etc) so we
1798 * aren't entirely busted.
1799 */
1800 if (vap->iv_opmode == IEEE80211_M_IBSS) {
1801 ieee80211_send_probereq(ni, /* node */
1802 vap->iv_myaddr, /* SA */
1803 ni->ni_macaddr, /* DA */
1804 vap->iv_bss->ni_bssid, /* BSSID */
1805 vap->iv_bss->ni_essid,
1806 vap->iv_bss->ni_esslen); /* SSID */
1807 }
1808
1809 /* XXX not right for 802.1x/WPA */
1810 ieee80211_node_authorize(ni);
1811 }
1812 return ni;
1813 }
1814
1815 void
ieee80211_init_neighbor(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const struct ieee80211_scanparams * sp)1816 ieee80211_init_neighbor(struct ieee80211_node *ni,
1817 const struct ieee80211_frame *wh,
1818 const struct ieee80211_scanparams *sp)
1819 {
1820 int do_ht_setup = 0, do_vht_setup = 0;
1821
1822 ni->ni_esslen = sp->ssid[1];
1823 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1824 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1825 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1826 ni->ni_intval = sp->bintval;
1827 ni->ni_capinfo = sp->capinfo;
1828 ni->ni_chan = ni->ni_ic->ic_curchan;
1829 ni->ni_fhdwell = sp->fhdwell;
1830 ni->ni_fhindex = sp->fhindex;
1831 ni->ni_erp = sp->erp;
1832 ni->ni_timoff = sp->timoff;
1833 #ifdef IEEE80211_SUPPORT_MESH
1834 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1835 ieee80211_mesh_init_neighbor(ni, wh, sp);
1836 #endif
1837 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1838 ieee80211_ies_expand(&ni->ni_ies);
1839 if (ni->ni_ies.wme_ie != NULL)
1840 ni->ni_flags |= IEEE80211_NODE_QOS;
1841 else
1842 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1843 #ifdef IEEE80211_SUPPORT_SUPERG
1844 if (ni->ni_ies.ath_ie != NULL)
1845 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1846 #endif
1847 if (ni->ni_ies.htcap_ie != NULL)
1848 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1849 if (ni->ni_ies.htinfo_ie != NULL)
1850 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1851
1852 if (ni->ni_ies.vhtcap_ie != NULL)
1853 ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
1854 if (ni->ni_ies.vhtopmode_ie != NULL)
1855 ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
1856
1857 if ((ni->ni_ies.htcap_ie != NULL) &&
1858 (ni->ni_ies.htinfo_ie != NULL) &&
1859 (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1860 do_ht_setup = 1;
1861 }
1862
1863 if ((ni->ni_ies.vhtcap_ie != NULL) &&
1864 (ni->ni_ies.vhtopmode_ie != NULL) &&
1865 (ni->ni_vap->iv_vht_flags & IEEE80211_FVHT_VHT)) {
1866 do_vht_setup = 1;
1867 }
1868 }
1869
1870 /* NB: must be after ni_chan is setup */
1871 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1872 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1873 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1874
1875 /*
1876 * If the neighbor is HT compatible, flip that on.
1877 */
1878 if (do_ht_setup) {
1879 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1880 "%s: doing HT setup\n", __func__);
1881 ieee80211_ht_node_init(ni);
1882 ieee80211_ht_updateparams(ni,
1883 ni->ni_ies.htcap_ie,
1884 ni->ni_ies.htinfo_ie);
1885
1886 if (do_vht_setup) {
1887 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1888 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
1889 __func__,
1890 ni->ni_macaddr,
1891 ":");
1892 } else {
1893 ieee80211_vht_node_init(ni);
1894 ieee80211_vht_updateparams(ni,
1895 ni->ni_ies.vhtcap_ie,
1896 ni->ni_ies.vhtopmode_ie);
1897 ieee80211_setup_vht_rates(ni,
1898 ni->ni_ies.vhtcap_ie,
1899 ni->ni_ies.vhtopmode_ie);
1900 }
1901 }
1902
1903 /*
1904 * Finally do the channel upgrade/change based
1905 * on the HT/VHT configuration.
1906 */
1907 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1908 ni->ni_ies.htinfo_ie);
1909 ieee80211_setup_htrates(ni,
1910 ni->ni_ies.htcap_ie,
1911 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1912 ieee80211_setup_basic_htrates(ni,
1913 ni->ni_ies.htinfo_ie);
1914
1915 ieee80211_node_setuptxparms(ni);
1916 ieee80211_ratectl_node_init(ni);
1917
1918 /* Reassociate; we're now 11n/11ac */
1919 /*
1920 * XXX TODO: this is the wrong thing to do -
1921 * we're calling it with isnew=1 so the ath(4)
1922 * driver reinitialises the rate tables.
1923 * This "mostly" works for ath(4), but it won't
1924 * be right for firmware devices which allocate
1925 * node states.
1926 *
1927 * So, do we just create a new node and delete
1928 * the old one? Or?
1929 */
1930 if (ni->ni_ic->ic_newassoc)
1931 ni->ni_ic->ic_newassoc(ni, 1);
1932 }
1933 }
1934
1935 /*
1936 * Do node discovery in adhoc mode on receipt of a beacon
1937 * or probe response frame. Note that for the driver's
1938 * benefit we treat this like an association so the
1939 * driver has an opportunity to setup it's private state.
1940 */
1941 struct ieee80211_node *
ieee80211_add_neighbor(struct ieee80211vap * vap,const struct ieee80211_frame * wh,const struct ieee80211_scanparams * sp)1942 ieee80211_add_neighbor(struct ieee80211vap *vap,
1943 const struct ieee80211_frame *wh,
1944 const struct ieee80211_scanparams *sp)
1945 {
1946 struct ieee80211_node *ni;
1947
1948 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1949 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1950 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1951 if (ni != NULL) {
1952 struct ieee80211com *ic = vap->iv_ic;
1953
1954 ieee80211_init_neighbor(ni, wh, sp);
1955 if (ieee80211_iserp_rateset(&ni->ni_rates))
1956 ni->ni_flags |= IEEE80211_NODE_ERP;
1957 ieee80211_node_setuptxparms(ni);
1958 ieee80211_ratectl_node_init(ni);
1959 if (ic->ic_newassoc != NULL)
1960 ic->ic_newassoc(ni, 1);
1961 /* XXX not right for 802.1x/WPA */
1962 ieee80211_node_authorize(ni);
1963 }
1964 return ni;
1965 }
1966
1967 #define IS_PROBEREQ(wh) \
1968 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1969 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1970 #define IS_BCAST_PROBEREQ(wh) \
1971 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1972 ((const struct ieee80211_frame *)(wh))->i_addr3))
1973
1974 static __inline struct ieee80211_node *
_find_rxnode(struct ieee80211_node_table * nt,const struct ieee80211_frame_min * wh)1975 _find_rxnode(struct ieee80211_node_table *nt,
1976 const struct ieee80211_frame_min *wh)
1977 {
1978 if (IS_BCAST_PROBEREQ(wh))
1979 return NULL; /* spam bcast probe req to all vap's */
1980 return ieee80211_find_node_locked(nt, wh->i_addr2);
1981 }
1982
1983 /*
1984 * Locate the node for sender, track state, and then pass the
1985 * (referenced) node up to the 802.11 layer for its use. Note
1986 * we can return NULL if the sender is not in the table.
1987 */
1988 struct ieee80211_node *
1989 #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_find_rxnode_debug(struct ieee80211com * ic,const struct ieee80211_frame_min * wh,const char * func,int line)1990 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1991 const struct ieee80211_frame_min *wh, const char *func, int line)
1992 #else
1993 ieee80211_find_rxnode(struct ieee80211com *ic,
1994 const struct ieee80211_frame_min *wh)
1995 #endif
1996 {
1997 struct ieee80211_node_table *nt;
1998 struct ieee80211_node *ni;
1999
2000 nt = &ic->ic_sta;
2001 IEEE80211_NODE_LOCK(nt);
2002 ni = _find_rxnode(nt, wh);
2003 IEEE80211_NODE_UNLOCK(nt);
2004
2005 return ni;
2006 }
2007
2008 /*
2009 * Like ieee80211_find_rxnode but use the supplied h/w
2010 * key index as a hint to locate the node in the key
2011 * mapping table. If an entry is present at the key
2012 * index we return it; otherwise do a normal lookup and
2013 * update the mapping table if the station has a unicast
2014 * key assigned to it.
2015 */
2016 struct ieee80211_node *
2017 #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_find_rxnode_withkey_debug(struct ieee80211com * ic,const struct ieee80211_frame_min * wh,ieee80211_keyix keyix,const char * func,int line)2018 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
2019 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
2020 const char *func, int line)
2021 #else
2022 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
2023 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
2024 #endif
2025 {
2026 struct ieee80211_node_table *nt;
2027 struct ieee80211_node *ni;
2028
2029 nt = &ic->ic_sta;
2030 IEEE80211_NODE_LOCK(nt);
2031 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
2032 ni = nt->nt_keyixmap[keyix];
2033 else
2034 ni = NULL;
2035 if (ni == NULL) {
2036 ni = _find_rxnode(nt, wh);
2037 if (ni != NULL && nt->nt_keyixmap != NULL) {
2038 /*
2039 * If the station has a unicast key cache slot
2040 * assigned update the key->node mapping table.
2041 */
2042 keyix = ni->ni_ucastkey.wk_rxkeyix;
2043 /* XXX can keyixmap[keyix] != NULL? */
2044 if (keyix < nt->nt_keyixmax &&
2045 nt->nt_keyixmap[keyix] == NULL) {
2046 IEEE80211_DPRINTF(ni->ni_vap,
2047 IEEE80211_MSG_NODE,
2048 "%s: add key map entry %p<%s> refcnt %d\n",
2049 __func__, ni, ether_sprintf(ni->ni_macaddr),
2050 ieee80211_node_refcnt(ni)+1);
2051 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
2052 }
2053 }
2054 } else {
2055 if (IS_BCAST_PROBEREQ(wh))
2056 ni = NULL; /* spam bcast probe req to all vap's */
2057 else
2058 ieee80211_ref_node(ni);
2059 }
2060 IEEE80211_NODE_UNLOCK(nt);
2061
2062 return ni;
2063 }
2064 #undef IS_BCAST_PROBEREQ
2065 #undef IS_PROBEREQ
2066
2067 /*
2068 * Return a reference to the appropriate node for sending
2069 * a data frame. This handles node discovery in adhoc networks.
2070 */
2071 struct ieee80211_node *
2072 #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_find_txnode_debug(struct ieee80211vap * vap,const uint8_t macaddr[IEEE80211_ADDR_LEN],const char * func,int line)2073 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
2074 const uint8_t macaddr[IEEE80211_ADDR_LEN],
2075 const char *func, int line)
2076 #else
2077 ieee80211_find_txnode(struct ieee80211vap *vap,
2078 const uint8_t macaddr[IEEE80211_ADDR_LEN])
2079 #endif
2080 {
2081 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
2082 struct ieee80211_node *ni;
2083
2084 /*
2085 * The destination address should be in the node table
2086 * unless this is a multicast/broadcast frame. We can
2087 * also optimize station mode operation, all frames go
2088 * to the bss node.
2089 */
2090 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
2091 IEEE80211_NODE_LOCK(nt);
2092 if (vap->iv_opmode == IEEE80211_M_STA ||
2093 vap->iv_opmode == IEEE80211_M_WDS ||
2094 IEEE80211_IS_MULTICAST(macaddr))
2095 ni = ieee80211_ref_node(vap->iv_bss);
2096 else
2097 ni = ieee80211_find_node_locked(nt, macaddr);
2098 IEEE80211_NODE_UNLOCK(nt);
2099
2100 if (ni == NULL) {
2101 if (vap->iv_opmode == IEEE80211_M_IBSS ||
2102 vap->iv_opmode == IEEE80211_M_AHDEMO) {
2103 /*
2104 * In adhoc mode cons up a node for the destination.
2105 * Note that we need an additional reference for the
2106 * caller to be consistent with
2107 * ieee80211_find_node_locked.
2108 */
2109 /*
2110 * XXX TODO: this doesn't fake up 11n state; we need
2111 * to find another way to get it upgraded.
2112 */
2113 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
2114 if (ni != NULL)
2115 (void) ieee80211_ref_node(ni);
2116 } else {
2117 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
2118 "no node, discard frame (%s)", __func__);
2119 vap->iv_stats.is_tx_nonode++;
2120 }
2121 }
2122 return ni;
2123 }
2124
2125 static void
_ieee80211_free_node(struct ieee80211_node * ni)2126 _ieee80211_free_node(struct ieee80211_node *ni)
2127 {
2128 struct ieee80211_node_table *nt = ni->ni_table;
2129
2130 /*
2131 * NB: careful about referencing the vap as it may be
2132 * gone if the last reference was held by a driver.
2133 * We know the com will always be present so it's safe
2134 * to use ni_ic below to reclaim resources.
2135 */
2136 #if 0
2137 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2138 "%s %p<%s> in %s table\n", __func__, ni,
2139 ether_sprintf(ni->ni_macaddr),
2140 nt != NULL ? nt->nt_name : "<gone>");
2141 #endif
2142 if (ni->ni_associd != 0) {
2143 struct ieee80211vap *vap = ni->ni_vap;
2144 if (vap->iv_aid_bitmap != NULL)
2145 IEEE80211_AID_CLR(vap, ni->ni_associd);
2146 }
2147 if (nt != NULL)
2148 ieee80211_del_node_nt(nt, ni);
2149 ni->ni_ic->ic_node_free(ni);
2150 }
2151
2152 /*
2153 * Clear any entry in the unicast key mapping table.
2154 */
2155 static int
node_clear_keyixmap(struct ieee80211_node_table * nt,struct ieee80211_node * ni)2156 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2157 {
2158 ieee80211_keyix keyix;
2159
2160 keyix = ni->ni_ucastkey.wk_rxkeyix;
2161 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
2162 nt->nt_keyixmap[keyix] == ni) {
2163 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2164 "%s: %p<%s> clear key map entry %u\n",
2165 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
2166 nt->nt_keyixmap[keyix] = NULL;
2167 ieee80211_node_decref(ni);
2168 return 1;
2169 }
2170
2171 return 0;
2172 }
2173
2174 void
2175 #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_free_node_debug(struct ieee80211_node * ni,const char * func,int line)2176 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
2177 #else
2178 ieee80211_free_node(struct ieee80211_node *ni)
2179 #endif
2180 {
2181 struct ieee80211_node_table *nt = ni->ni_table;
2182
2183 #ifdef IEEE80211_DEBUG_REFCNT
2184 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2185 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
2186 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
2187 #endif
2188 if (nt != NULL) {
2189 IEEE80211_NODE_LOCK(nt);
2190 if (ieee80211_node_dectestref(ni)) {
2191 /*
2192 * Last reference, reclaim state.
2193 */
2194 _ieee80211_free_node(ni);
2195 } else if (ieee80211_node_refcnt(ni) == 1)
2196 if (node_clear_keyixmap(nt, ni))
2197 _ieee80211_free_node(ni);
2198 IEEE80211_NODE_UNLOCK(nt);
2199 } else {
2200 if (ieee80211_node_dectestref(ni))
2201 _ieee80211_free_node(ni);
2202 }
2203 }
2204
2205 /*
2206 * Reclaim a unicast key and clear any key cache state.
2207 */
2208 int
ieee80211_node_delucastkey(struct ieee80211_node * ni)2209 ieee80211_node_delucastkey(struct ieee80211_node *ni)
2210 {
2211 struct ieee80211com *ic = ni->ni_ic;
2212 struct ieee80211_node_table *nt = &ic->ic_sta;
2213 struct ieee80211_node *nikey;
2214 ieee80211_keyix keyix;
2215 int isowned, status;
2216
2217 /*
2218 * NB: We must beware of LOR here; deleting the key
2219 * can cause the crypto layer to block traffic updates
2220 * which can generate a LOR against the node table lock;
2221 * grab it here and stash the key index for our use below.
2222 *
2223 * Must also beware of recursion on the node table lock.
2224 * When called from node_cleanup we may already have
2225 * the node table lock held. Unfortunately there's no
2226 * way to separate out this path so we must do this
2227 * conditionally.
2228 */
2229 isowned = IEEE80211_NODE_IS_LOCKED(nt);
2230 if (!isowned)
2231 IEEE80211_NODE_LOCK(nt);
2232 nikey = NULL;
2233 status = 1; /* NB: success */
2234 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
2235 keyix = ni->ni_ucastkey.wk_rxkeyix;
2236 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
2237 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
2238 nikey = nt->nt_keyixmap[keyix];
2239 nt->nt_keyixmap[keyix] = NULL;
2240 }
2241 }
2242 if (!isowned)
2243 IEEE80211_NODE_UNLOCK(nt);
2244
2245 if (nikey != NULL) {
2246 KASSERT(nikey == ni,
2247 ("key map out of sync, ni %p nikey %p", ni, nikey));
2248 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2249 "%s: delete key map entry %p<%s> refcnt %d\n",
2250 __func__, ni, ether_sprintf(ni->ni_macaddr),
2251 ieee80211_node_refcnt(ni)-1);
2252 ieee80211_free_node(ni);
2253 }
2254 return status;
2255 }
2256
2257 /*
2258 * Reclaim a node. If this is the last reference count then
2259 * do the normal free work. Otherwise remove it from the node
2260 * table and mark it gone by clearing the back-reference.
2261 */
2262 static void
node_reclaim(struct ieee80211_node_table * nt,struct ieee80211_node * ni)2263 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2264 {
2265
2266 IEEE80211_NODE_LOCK_ASSERT(nt);
2267
2268 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2269 "%s: remove %p<%s> from %s table, refcnt %d\n",
2270 __func__, ni, ether_sprintf(ni->ni_macaddr),
2271 nt->nt_name, ieee80211_node_refcnt(ni)-1);
2272 /*
2273 * Clear any entry in the unicast key mapping table.
2274 * We need to do it here so rx lookups don't find it
2275 * in the mapping table even if it's not in the hash
2276 * table. We cannot depend on the mapping table entry
2277 * being cleared because the node may not be free'd.
2278 */
2279 (void)node_clear_keyixmap(nt, ni);
2280 if (!ieee80211_node_dectestref(ni)) {
2281 /*
2282 * Other references are present, just remove the
2283 * node from the table so it cannot be found. When
2284 * the references are dropped storage will be
2285 * reclaimed.
2286 */
2287 ieee80211_del_node_nt(nt, ni);
2288 } else
2289 _ieee80211_free_node(ni);
2290 }
2291
2292 /*
2293 * Node table support.
2294 */
2295
2296 static void
ieee80211_node_table_init(struct ieee80211com * ic,struct ieee80211_node_table * nt,const char * name,int inact,int keyixmax)2297 ieee80211_node_table_init(struct ieee80211com *ic,
2298 struct ieee80211_node_table *nt,
2299 const char *name, int inact, int keyixmax)
2300 {
2301
2302 nt->nt_ic = ic;
2303 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
2304 TAILQ_INIT(&nt->nt_node);
2305 nt->nt_count = 0;
2306 nt->nt_name = name;
2307 nt->nt_inact_init = inact;
2308 nt->nt_keyixmax = keyixmax;
2309 if (nt->nt_keyixmax > 0) {
2310 nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
2311 keyixmax * sizeof(struct ieee80211_node *),
2312 M_80211_NODE,
2313 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2314 if (nt->nt_keyixmap == NULL)
2315 ic_printf(ic,
2316 "Cannot allocate key index map with %u entries\n",
2317 keyixmax);
2318 } else
2319 nt->nt_keyixmap = NULL;
2320 }
2321
2322 static void
ieee80211_node_table_reset(struct ieee80211_node_table * nt,struct ieee80211vap * match)2323 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
2324 struct ieee80211vap *match)
2325 {
2326 struct ieee80211_node *ni, *next;
2327
2328 IEEE80211_NODE_LOCK(nt);
2329 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
2330 if (match != NULL && ni->ni_vap != match)
2331 continue;
2332 /* XXX can this happen? if so need's work */
2333 if (ni->ni_associd != 0) {
2334 struct ieee80211vap *vap = ni->ni_vap;
2335
2336 if (vap->iv_auth->ia_node_leave != NULL)
2337 vap->iv_auth->ia_node_leave(ni);
2338 if (vap->iv_aid_bitmap != NULL)
2339 IEEE80211_AID_CLR(vap, ni->ni_associd);
2340 }
2341 ni->ni_wdsvap = NULL; /* clear reference */
2342 node_reclaim(nt, ni);
2343 }
2344 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
2345 /*
2346 * Make a separate pass to clear references to this vap
2347 * held by DWDS entries. They will not be matched above
2348 * because ni_vap will point to the ap vap but we still
2349 * need to clear ni_wdsvap when the WDS vap is destroyed
2350 * and/or reset.
2351 */
2352 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
2353 if (ni->ni_wdsvap == match)
2354 ni->ni_wdsvap = NULL;
2355 }
2356 IEEE80211_NODE_UNLOCK(nt);
2357 }
2358
2359 static void
ieee80211_node_table_cleanup(struct ieee80211_node_table * nt)2360 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2361 {
2362 ieee80211_node_table_reset(nt, NULL);
2363 if (nt->nt_keyixmap != NULL) {
2364 #ifdef DIAGNOSTIC
2365 /* XXX verify all entries are NULL */
2366 int i;
2367 for (i = 0; i < nt->nt_keyixmax; i++)
2368 if (nt->nt_keyixmap[i] != NULL)
2369 printf("%s: %s[%u] still active\n", __func__,
2370 nt->nt_name, i);
2371 #endif
2372 IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
2373 nt->nt_keyixmap = NULL;
2374 }
2375 IEEE80211_NODE_LOCK_DESTROY(nt);
2376 }
2377
2378 static void
timeout_stations(void * arg __unused,struct ieee80211_node * ni)2379 timeout_stations(void *arg __unused, struct ieee80211_node *ni)
2380 {
2381 struct ieee80211com *ic = ni->ni_ic;
2382 struct ieee80211vap *vap = ni->ni_vap;
2383
2384 /*
2385 * Only process stations when in RUN state. This
2386 * insures, for example, that we don't timeout an
2387 * inactive station during CAC. Note that CSA state
2388 * is actually handled in ieee80211_node_timeout as
2389 * it applies to more than timeout processing.
2390 */
2391 if (vap->iv_state != IEEE80211_S_RUN)
2392 return;
2393 /*
2394 * Ignore entries for which have yet to receive an
2395 * authentication frame. These are transient and
2396 * will be reclaimed when the last reference to them
2397 * goes away (when frame xmits complete).
2398 */
2399 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2400 vap->iv_opmode == IEEE80211_M_STA) &&
2401 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2402 return;
2403 /*
2404 * Free fragment if not needed anymore
2405 * (last fragment older than 1s).
2406 * XXX doesn't belong here, move to node_age
2407 */
2408 if (ni->ni_rxfrag[0] != NULL &&
2409 ticks > ni->ni_rxfragstamp + hz) {
2410 m_freem(ni->ni_rxfrag[0]);
2411 ni->ni_rxfrag[0] = NULL;
2412 }
2413 if (ni->ni_inact > 0) {
2414 ni->ni_inact--;
2415 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2416 "%s: inact %u inact_reload %u nrates %u",
2417 __func__, ni->ni_inact, ni->ni_inact_reload,
2418 ni->ni_rates.rs_nrates);
2419 }
2420 /*
2421 * Special case ourself; we may be idle for extended periods
2422 * of time and regardless reclaiming our state is wrong.
2423 * XXX run ic_node_age
2424 */
2425 /* XXX before inact decrement? */
2426 if (ni == vap->iv_bss)
2427 return;
2428 if (ni->ni_associd != 0 ||
2429 (vap->iv_opmode == IEEE80211_M_IBSS ||
2430 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2431 /*
2432 * Age/drain resources held by the station.
2433 */
2434 ic->ic_node_age(ni);
2435 /*
2436 * Probe the station before time it out. We
2437 * send a null data frame which may not be
2438 * universally supported by drivers (need it
2439 * for ps-poll support so it should be...).
2440 *
2441 * XXX don't probe the station unless we've
2442 * received a frame from them (and have
2443 * some idea of the rates they are capable
2444 * of); this will get fixed more properly
2445 * soon with better handling of the rate set.
2446 */
2447 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2448 (0 < ni->ni_inact &&
2449 ni->ni_inact <= vap->iv_inact_probe) &&
2450 ni->ni_rates.rs_nrates != 0) {
2451 IEEE80211_NOTE(vap,
2452 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2453 ni, "%s",
2454 "probe station due to inactivity");
2455 /*
2456 * Grab a reference so the node cannot
2457 * be reclaimed before we send the frame.
2458 * ieee80211_send_nulldata understands
2459 * we've done this and reclaims the
2460 * ref for us as needed.
2461 */
2462 /* XXX fix this (not required anymore). */
2463 ieee80211_ref_node(ni);
2464 /* XXX useless */
2465 ieee80211_send_nulldata(ni);
2466 /* XXX stat? */
2467 return;
2468 }
2469 }
2470 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2471 ni->ni_inact <= 0) {
2472 IEEE80211_NOTE(vap,
2473 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2474 "station timed out due to inactivity "
2475 "(refcnt %u)", ieee80211_node_refcnt(ni));
2476 /*
2477 * Send a deauthenticate frame and drop the station.
2478 * This is somewhat complicated due to reference counts
2479 * and locking. At this point a station will typically
2480 * have a reference count of 2. ieee80211_node_leave
2481 * will do a "free" of the node which will drop the
2482 * reference count. But in the meantime a reference
2483 * wil be held by the deauth frame. The actual reclaim
2484 * of the node will happen either after the tx is
2485 * completed or by ieee80211_node_leave.
2486 */
2487 if (ni->ni_associd != 0) {
2488 IEEE80211_SEND_MGMT(ni,
2489 IEEE80211_FC0_SUBTYPE_DEAUTH,
2490 IEEE80211_REASON_AUTH_EXPIRE);
2491 }
2492 ieee80211_node_leave(ni);
2493 vap->iv_stats.is_node_timeout++;
2494 }
2495 }
2496
2497 /*
2498 * Timeout inactive stations and do related housekeeping.
2499 */
2500 static void
ieee80211_timeout_stations(struct ieee80211com * ic)2501 ieee80211_timeout_stations(struct ieee80211com *ic)
2502 {
2503 struct ieee80211_node_table *nt = &ic->ic_sta;
2504
2505 ieee80211_iterate_nodes(nt, timeout_stations, NULL);
2506 }
2507
2508 /*
2509 * Aggressively reclaim resources. This should be used
2510 * only in a critical situation to reclaim mbuf resources.
2511 */
2512 void
ieee80211_drain(struct ieee80211com * ic)2513 ieee80211_drain(struct ieee80211com *ic)
2514 {
2515 struct ieee80211_node_table *nt = &ic->ic_sta;
2516 struct ieee80211vap *vap;
2517 struct ieee80211_node *ni;
2518
2519 IEEE80211_NODE_LOCK(nt);
2520 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2521 /*
2522 * Ignore entries for which have yet to receive an
2523 * authentication frame. These are transient and
2524 * will be reclaimed when the last reference to them
2525 * goes away (when frame xmits complete).
2526 */
2527 vap = ni->ni_vap;
2528 /*
2529 * Only process stations when in RUN state. This
2530 * insures, for example, that we don't timeout an
2531 * inactive station during CAC. Note that CSA state
2532 * is actually handled in ieee80211_node_timeout as
2533 * it applies to more than timeout processing.
2534 */
2535 if (vap->iv_state != IEEE80211_S_RUN)
2536 continue;
2537 /* XXX can vap be NULL? */
2538 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2539 vap->iv_opmode == IEEE80211_M_STA) &&
2540 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2541 continue;
2542 /*
2543 * Free fragments.
2544 * XXX doesn't belong here, move to node_drain
2545 */
2546 if (ni->ni_rxfrag[0] != NULL) {
2547 m_freem(ni->ni_rxfrag[0]);
2548 ni->ni_rxfrag[0] = NULL;
2549 }
2550 /*
2551 * Drain resources held by the station.
2552 */
2553 ic->ic_node_drain(ni);
2554 }
2555 IEEE80211_NODE_UNLOCK(nt);
2556 }
2557
2558 /*
2559 * Per-ieee80211vap inactivity timer callback.
2560 */
2561 static void
ieee80211_vap_timeout(struct ieee80211vap * vap)2562 ieee80211_vap_timeout(struct ieee80211vap *vap)
2563 {
2564
2565 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2566
2567 ieee80211_vap_erp_timeout(vap);
2568 ieee80211_ht_timeout(vap);
2569 ieee80211_vht_timeout(vap);
2570 }
2571
2572 /*
2573 * Per-ieee80211com inactivity timer callback.
2574 */
2575 void
ieee80211_node_timeout(void * arg)2576 ieee80211_node_timeout(void *arg)
2577 {
2578 struct ieee80211com *ic = arg;
2579 struct ieee80211vap *vap;
2580
2581 /*
2582 * Defer timeout processing if a channel switch is pending.
2583 * We typically need to be mute so not doing things that
2584 * might generate frames is good to handle in one place.
2585 * Suppressing the station timeout processing may extend the
2586 * lifetime of inactive stations (by not decrementing their
2587 * idle counters) but this should be ok unless the CSA is
2588 * active for an unusually long time.
2589 */
2590 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2591 ieee80211_scan_timeout(ic);
2592 ieee80211_timeout_stations(ic);
2593 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2594
2595 IEEE80211_LOCK(ic);
2596 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2597 ieee80211_vap_timeout(vap);
2598 IEEE80211_UNLOCK(ic);
2599 }
2600 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2601 ieee80211_node_timeout, ic);
2602 }
2603
2604 /*
2605 * The same as ieee80211_iterate_nodes(), but for one vap only.
2606 */
2607 int
ieee80211_iterate_nodes_vap(struct ieee80211_node_table * nt,struct ieee80211vap * vap,ieee80211_iter_func * f,void * arg)2608 ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
2609 struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
2610 {
2611 struct ieee80211_node **ni_arr;
2612 struct ieee80211_node *ni;
2613 size_t size;
2614 int count, i;
2615
2616 /*
2617 * Iterate over the node table and save an array of ref'ed nodes.
2618 *
2619 * This is separated out from calling the actual node function so that
2620 * no LORs will occur.
2621 */
2622 IEEE80211_NODE_LOCK(nt);
2623 count = nt->nt_count;
2624 size = count * sizeof(struct ieee80211_node *);
2625 ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
2626 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2627 if (ni_arr == NULL) {
2628 IEEE80211_NODE_UNLOCK(nt);
2629 return (ENOMEM);
2630 }
2631
2632 i = 0;
2633 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2634 if (vap != NULL && ni->ni_vap != vap)
2635 continue;
2636 KASSERT(i < count,
2637 ("node array overflow (vap %p, i %d, count %d)\n",
2638 vap, i, count));
2639 ni_arr[i] = ieee80211_ref_node(ni);
2640 i++;
2641 }
2642 IEEE80211_NODE_UNLOCK(nt);
2643
2644 for (i = 0; i < count; i++) {
2645 if (ni_arr[i] == NULL) /* end of the list */
2646 break;
2647 (*f)(arg, ni_arr[i]);
2648 /* ieee80211_free_node() locks by itself */
2649 ieee80211_free_node(ni_arr[i]);
2650 }
2651
2652 IEEE80211_FREE(ni_arr, M_80211_NODE);
2653
2654 return (0);
2655 }
2656
2657 /*
2658 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2659 * reference in the source.
2660 */
2661 void
ieee80211_iterate_nodes(struct ieee80211_node_table * nt,ieee80211_iter_func * f,void * arg)2662 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2663 ieee80211_iter_func *f, void *arg)
2664 {
2665 /* XXX no way to pass error to the caller. */
2666 (void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
2667 }
2668
2669 void
ieee80211_dump_node(struct ieee80211_node_table * nt __unused,struct ieee80211_node * ni)2670 ieee80211_dump_node(struct ieee80211_node_table *nt __unused,
2671 struct ieee80211_node *ni)
2672 {
2673 printf("%p: mac %s refcnt %d\n", ni,
2674 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2675 printf("\tauthmode %u flags 0x%x\n",
2676 ni->ni_authmode, ni->ni_flags);
2677 printf("\tassocid 0x%x txpower %u vlan %u\n",
2678 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2679 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2680 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2681 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2682 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2683 ni->ni_rxfragstamp);
2684 printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2685 node_getrssi(ni), ni->ni_noise,
2686 ni->ni_intval, ni->ni_capinfo);
2687 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2688 ether_sprintf(ni->ni_bssid),
2689 ni->ni_esslen, ni->ni_essid,
2690 (ni->ni_chan != IEEE80211_CHAN_ANYC) ? ni->ni_chan->ic_freq : 0,
2691 (ni->ni_chan != IEEE80211_CHAN_ANYC) ? ni->ni_chan->ic_flags : 0);
2692 printf("\tinact %u inact_reload %u txrate %u\n",
2693 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2694 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2695 ni->ni_htcap, ni->ni_htparam,
2696 ni->ni_htctlchan, ni->ni_ht2ndchan);
2697 printf("\thtopmode %x htstbc %x htchw %u\n",
2698 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2699 printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n",
2700 ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2,
2701 (int) ni->ni_vht_basicmcs);
2702 /* XXX VHT state */
2703 }
2704
2705 void
ieee80211_dump_nodes(struct ieee80211_node_table * nt)2706 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2707 {
2708 ieee80211_iterate_nodes(nt,
2709 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2710 }
2711
2712 /*
2713 * Iterate over the VAPs and update their ERP beacon IEs.
2714 *
2715 * Note this must be called from the deferred ERP update task paths.
2716 */
2717 void
ieee80211_notify_erp_locked(struct ieee80211com * ic)2718 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2719 {
2720 struct ieee80211vap *vap;
2721
2722 IEEE80211_LOCK_ASSERT(ic);
2723
2724 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2725 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2726 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2727 }
2728
2729 /*
2730 * Handle a station joining an 11g network.
2731 */
2732 static void
ieee80211_node_join_11g(struct ieee80211_node * ni)2733 ieee80211_node_join_11g(struct ieee80211_node *ni)
2734 {
2735 struct ieee80211com *ic = ni->ni_ic;
2736 struct ieee80211vap *vap = ni->ni_vap;
2737
2738 IEEE80211_LOCK_ASSERT(ic);
2739
2740 /*
2741 * Station isn't capable of short slot time. Bump
2742 * the count of long slot time stations and disable
2743 * use of short slot time. Note that the actual switch
2744 * over to long slot time use may not occur until the
2745 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2746 */
2747 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2748 vap->iv_longslotsta++;
2749 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2750 "station needs long slot time, count %d",
2751 vap->iv_longslotsta);
2752 /*
2753 * XXX TODO: this may need all VAPs checked!
2754 */
2755 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2756 /*
2757 * Don't force slot time when switched to turbo
2758 * mode as non-ERP stations won't be present; this
2759 * need only be done when on the normal G channel.
2760 */
2761 ieee80211_vap_set_shortslottime(vap, 0);
2762 }
2763 }
2764 /*
2765 * If the new station is not an ERP station
2766 * then bump the counter and enable protection
2767 * if configured.
2768 */
2769 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2770 vap->iv_nonerpsta++;
2771 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2772 "station is !ERP, %d non-ERP stations associated",
2773 vap->iv_nonerpsta);
2774 /*
2775 * If station does not support short preamble
2776 * then we must enable use of Barker preamble.
2777 */
2778 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2779 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2780 "%s", "station needs long preamble");
2781 vap->iv_flags |= IEEE80211_F_USEBARKER;
2782 vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE;
2783 ieee80211_vap_update_preamble(vap);
2784 }
2785 /*
2786 * If protection is configured and this is the first
2787 * indication we should use protection, enable it.
2788 */
2789 if (vap->iv_protmode != IEEE80211_PROT_NONE &&
2790 vap->iv_nonerpsta == 1 &&
2791 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2792 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2793 "%s: enable use of protection\n", __func__);
2794 vap->iv_flags |= IEEE80211_F_USEPROT;
2795 ieee80211_vap_update_erp_protmode(vap);
2796 }
2797 } else
2798 ni->ni_flags |= IEEE80211_NODE_ERP;
2799 }
2800
2801 void
ieee80211_node_join(struct ieee80211_node * ni,int resp)2802 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2803 {
2804 struct ieee80211com *ic = ni->ni_ic;
2805 struct ieee80211vap *vap = ni->ni_vap;
2806 int newassoc;
2807
2808 if (ni->ni_associd == 0) {
2809 uint16_t aid;
2810
2811 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2812 /*
2813 * It would be good to search the bitmap
2814 * more efficiently, but this will do for now.
2815 */
2816 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2817 if (!IEEE80211_AID_ISSET(vap, aid))
2818 break;
2819 }
2820 if (aid >= vap->iv_max_aid) {
2821 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2822 ieee80211_node_leave(ni);
2823 return;
2824 }
2825 ni->ni_associd = aid | 0xc000;
2826 ni->ni_jointime = time_uptime;
2827 IEEE80211_LOCK(ic);
2828 IEEE80211_AID_SET(vap, ni->ni_associd);
2829 vap->iv_sta_assoc++;
2830
2831 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2832 ieee80211_ht_node_join(ni);
2833 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2834 ieee80211_vht_node_join(ni);
2835 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2836 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2837 ieee80211_node_join_11g(ni);
2838 IEEE80211_UNLOCK(ic);
2839
2840 newassoc = 1;
2841 } else
2842 newassoc = 0;
2843
2844 /*
2845 * XXX VHT - should log VHT channel width, etc
2846 */
2847 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2848 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s%s",
2849 IEEE80211_NODE_AID(ni),
2850 vap->iv_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2851 vap->iv_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2852 vap->iv_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2853 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2854 /* XXX update for VHT string */
2855 ni->ni_flags & IEEE80211_NODE_HT ?
2856 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2857 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2858 ni->ni_flags & IEEE80211_NODE_AMSDU ? " (+AMSDU)" : "",
2859 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2860 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2861 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2862 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2863 ", fast-frames" : "",
2864 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2865 ", turbo" : ""
2866 );
2867
2868 ieee80211_node_setuptxparms(ni);
2869 ieee80211_ratectl_node_init(ni);
2870 /* give driver a chance to setup state like ni_txrate */
2871 if (ic->ic_newassoc != NULL)
2872 ic->ic_newassoc(ni, newassoc);
2873 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2874 /* tell the authenticator about new station */
2875 if (vap->iv_auth->ia_node_join != NULL)
2876 vap->iv_auth->ia_node_join(ni);
2877 ieee80211_notify_node_join(ni,
2878 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2879 }
2880
2881 static void
disable_protection(struct ieee80211vap * vap)2882 disable_protection(struct ieee80211vap *vap)
2883 {
2884 struct ieee80211com *ic = vap->iv_ic;
2885
2886 KASSERT(vap->iv_nonerpsta == 0 &&
2887 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2888 ("%d non ERP stations, flags 0x%x", vap->iv_nonerpsta,
2889 vap->iv_flags_ext));
2890
2891 vap->iv_flags &= ~IEEE80211_F_USEPROT;
2892 /* XXX verify mode? */
2893 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2894 vap->iv_flags |= IEEE80211_F_SHPREAMBLE;
2895 vap->iv_flags &= ~IEEE80211_F_USEBARKER;
2896 }
2897 ieee80211_vap_update_erp_protmode(vap);
2898 ieee80211_vap_update_preamble(vap);
2899 }
2900
2901 /*
2902 * Handle a station leaving an 11g network.
2903 */
2904 static void
ieee80211_node_leave_11g(struct ieee80211_node * ni)2905 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2906 {
2907 struct ieee80211com *ic = ni->ni_ic;
2908 struct ieee80211vap *vap = ni->ni_vap;
2909
2910 IEEE80211_LOCK_ASSERT(ic);
2911
2912 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2913 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2914 ic->ic_bsschan->ic_flags));
2915
2916 /*
2917 * If a long slot station do the slot time bookkeeping.
2918 */
2919 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2920 KASSERT(vap->iv_longslotsta > 0,
2921 ("bogus long slot station count %d", vap->iv_longslotsta));
2922 vap->iv_longslotsta--;
2923 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2924 "long slot time station leaves, count now %d",
2925 vap->iv_longslotsta);
2926 /*
2927 * XXX TODO: this may need all VAPs checked!
2928 */
2929 if (vap->iv_longslotsta == 0) {
2930 /*
2931 * Re-enable use of short slot time if supported
2932 * and not operating in IBSS mode (per spec).
2933 */
2934 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2935 ic->ic_opmode != IEEE80211_M_IBSS) {
2936 IEEE80211_DPRINTF(ni->ni_vap,
2937 IEEE80211_MSG_ASSOC,
2938 "%s: re-enable use of short slot time\n",
2939 __func__);
2940 ieee80211_vap_set_shortslottime(vap, 1);
2941 }
2942 }
2943 }
2944 /*
2945 * If a non-ERP station do the protection-related bookkeeping.
2946 */
2947 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2948 KASSERT(vap->iv_nonerpsta > 0,
2949 ("bogus non-ERP station count %d", vap->iv_nonerpsta));
2950 vap->iv_nonerpsta--;
2951 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2952 "non-ERP station leaves, count now %d%s", vap->iv_nonerpsta,
2953 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2954 " (non-ERP sta present)" : "");
2955 if (vap->iv_nonerpsta == 0 &&
2956 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2957 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2958 "%s: disable use of protection\n", __func__);
2959 disable_protection(vap);
2960 }
2961 }
2962 }
2963
2964 /*
2965 * Time out presence of an overlapping bss with non-ERP
2966 * stations. When operating in hostap mode we listen for
2967 * beacons from other stations and if we identify a non-ERP
2968 * station is present we enable protection. To identify
2969 * when all non-ERP stations are gone we time out this
2970 * condition.
2971 */
2972 static void
ieee80211_vap_erp_timeout(struct ieee80211vap * vap)2973 ieee80211_vap_erp_timeout(struct ieee80211vap *vap)
2974 {
2975
2976 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2977
2978 if ((vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2979 ieee80211_time_after(ticks, vap->iv_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2980 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
2981 "%s", "age out non-ERP sta present on channel");
2982 vap->iv_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2983 if (vap->iv_nonerpsta == 0)
2984 disable_protection(vap);
2985 }
2986 }
2987
2988 /*
2989 * Handle bookkeeping for station deauthentication/disassociation
2990 * when operating as an ap.
2991 */
2992 void
ieee80211_node_leave(struct ieee80211_node * ni)2993 ieee80211_node_leave(struct ieee80211_node *ni)
2994 {
2995 struct ieee80211com *ic = ni->ni_ic;
2996 struct ieee80211vap *vap = ni->ni_vap;
2997 struct ieee80211_node_table *nt = ni->ni_table;
2998
2999 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
3000 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
3001
3002 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
3003 ("unexpected operating mode %u", vap->iv_opmode));
3004 /*
3005 * If node wasn't previously associated all
3006 * we need to do is reclaim the reference.
3007 */
3008 /* XXX ibss mode bypasses 11g and notification */
3009 if (ni->ni_associd == 0)
3010 goto done;
3011 /*
3012 * Tell the authenticator the station is leaving.
3013 * Note that we must do this before yanking the
3014 * association id as the authenticator uses the
3015 * associd to locate it's state block.
3016 */
3017 if (vap->iv_auth->ia_node_leave != NULL)
3018 vap->iv_auth->ia_node_leave(ni);
3019
3020 IEEE80211_LOCK(ic);
3021 IEEE80211_AID_CLR(vap, ni->ni_associd);
3022 vap->iv_sta_assoc--;
3023
3024 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
3025 ieee80211_vht_node_leave(ni);
3026 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
3027 ieee80211_ht_node_leave(ni);
3028 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
3029 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
3030 ieee80211_node_leave_11g(ni);
3031 IEEE80211_UNLOCK(ic);
3032 /*
3033 * Cleanup station state. In particular clear various
3034 * state that might otherwise be reused if the node
3035 * is reused before the reference count goes to zero
3036 * (and memory is reclaimed).
3037 */
3038 ieee80211_sta_leave(ni);
3039 done:
3040 /*
3041 * Remove the node from any table it's recorded in and
3042 * drop the caller's reference. Removal from the table
3043 * is important to insure the node is not reprocessed
3044 * for inactivity.
3045 */
3046 if (nt != NULL) {
3047 IEEE80211_NODE_LOCK(nt);
3048 node_reclaim(nt, ni);
3049 IEEE80211_NODE_UNLOCK(nt);
3050 } else
3051 ieee80211_free_node(ni);
3052 }
3053
3054 struct rssiinfo {
3055 int rssi_samples;
3056 uint32_t rssi_total;
3057 };
3058
3059 static void
get_hostap_rssi(void * arg,struct ieee80211_node * ni)3060 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
3061 {
3062 struct rssiinfo *info = arg;
3063 struct ieee80211vap *vap = ni->ni_vap;
3064 int8_t rssi;
3065
3066 /* only associated stations */
3067 if (ni->ni_associd == 0)
3068 return;
3069 rssi = vap->iv_ic->ic_node_getrssi(ni);
3070 if (rssi != 0) {
3071 info->rssi_samples++;
3072 info->rssi_total += rssi;
3073 }
3074 }
3075
3076 static void
get_adhoc_rssi(void * arg,struct ieee80211_node * ni)3077 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
3078 {
3079 struct rssiinfo *info = arg;
3080 struct ieee80211vap *vap = ni->ni_vap;
3081 int8_t rssi;
3082
3083 /* only neighbors */
3084 /* XXX check bssid */
3085 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
3086 return;
3087 rssi = vap->iv_ic->ic_node_getrssi(ni);
3088 if (rssi != 0) {
3089 info->rssi_samples++;
3090 info->rssi_total += rssi;
3091 }
3092 }
3093
3094 #ifdef IEEE80211_SUPPORT_MESH
3095 static void
get_mesh_rssi(void * arg,struct ieee80211_node * ni)3096 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
3097 {
3098 struct rssiinfo *info = arg;
3099 struct ieee80211vap *vap = ni->ni_vap;
3100 int8_t rssi;
3101
3102 /* only neighbors that peered successfully */
3103 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
3104 return;
3105 rssi = vap->iv_ic->ic_node_getrssi(ni);
3106 if (rssi != 0) {
3107 info->rssi_samples++;
3108 info->rssi_total += rssi;
3109 }
3110 }
3111 #endif /* IEEE80211_SUPPORT_MESH */
3112
3113 int8_t
ieee80211_getrssi(struct ieee80211vap * vap)3114 ieee80211_getrssi(struct ieee80211vap *vap)
3115 {
3116 #define NZ(x) ((x) == 0 ? 1 : (x))
3117 struct ieee80211com *ic = vap->iv_ic;
3118 struct rssiinfo info;
3119
3120 info.rssi_total = 0;
3121 info.rssi_samples = 0;
3122 switch (vap->iv_opmode) {
3123 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
3124 case IEEE80211_M_AHDEMO: /* average of all neighbors */
3125 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
3126 &info);
3127 break;
3128 case IEEE80211_M_HOSTAP: /* average of all associated stations */
3129 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
3130 &info);
3131 break;
3132 #ifdef IEEE80211_SUPPORT_MESH
3133 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
3134 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
3135 &info);
3136 break;
3137 #endif
3138 case IEEE80211_M_MONITOR: /* XXX */
3139 case IEEE80211_M_STA: /* use stats from associated ap */
3140 default:
3141 if (vap->iv_bss != NULL)
3142 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
3143 info.rssi_samples = 1;
3144 break;
3145 }
3146 return info.rssi_total / NZ(info.rssi_samples);
3147 #undef NZ
3148 }
3149
3150 void
ieee80211_getsignal(struct ieee80211vap * vap,int8_t * rssi,int8_t * noise)3151 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
3152 {
3153
3154 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
3155 return;
3156 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
3157 /* for non-station mode return avg'd rssi accounting */
3158 if (vap->iv_opmode != IEEE80211_M_STA)
3159 *rssi = ieee80211_getrssi(vap);
3160 }
3161