2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2001 Atsushi Onoe
5 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
38 #include <sys/socket.h>
41 #include <net/if_var.h>
42 #include <net/if_media.h>
43 #include <net/ethernet.h>
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_input.h>
47 #ifdef IEEE80211_SUPPORT_SUPERG
48 #include <net80211/ieee80211_superg.h>
50 #ifdef IEEE80211_SUPPORT_TDMA
51 #include <net80211/ieee80211_tdma.h>
53 #include <net80211/ieee80211_wds.h>
54 #include <net80211/ieee80211_mesh.h>
55 #include <net80211/ieee80211_ratectl.h>
56 #include <net80211/ieee80211_vht.h>
60 #ifdef IEEE80211_DEBUG_REFCNT
61 #define __debrefcnt_used
63 #define __debrefcnt_used __unused
67 * IEEE80211_NODE_HASHSIZE must be a power of 2.
69 CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
72 * Association id's are managed with a bit vector.
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)))
83 static int ieee80211_sta_join1(struct ieee80211_node *);
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 *);
99 static void _ieee80211_free_node(struct ieee80211_node *);
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 *);
111 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
112 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
115 ieee80211_node_attach(struct ieee80211com *ic)
117 /* XXX really want maxlen enforced per-sta */
118 ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
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);
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;
137 * Set flags to be propagated to all vap's;
138 * these define default behaviour/configuration.
140 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
144 ieee80211_node_detach(struct ieee80211com *ic)
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);
154 ieee80211_node_vattach(struct ieee80211vap *vap)
156 /* NB: driver can override */
157 vap->iv_max_aid = IEEE80211_AID_DEF;
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;
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);
172 ieee80211_node_latevattach(struct ieee80211vap *vap)
175 /* XXX should ieee80211_vap_attach(), our only caller hold the lock? */
176 IEEE80211_UNLOCK_ASSERT(vap->iv_ic);
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",
186 vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC(
187 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
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);
198 IEEE80211_LOCK(vap->iv_ic);
199 ieee80211_reset_bss(vap);
200 IEEE80211_UNLOCK(vap->iv_ic);
202 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
206 ieee80211_node_vdetach(struct ieee80211vap *vap)
208 struct ieee80211com *ic = vap->iv_ic;
210 /* XXX should ieee80211_vap_detach(), our only caller hold the lock? */
211 IEEE80211_UNLOCK_ASSERT(vap->iv_ic);
213 ieee80211_node_table_reset(&ic->ic_sta, vap);
215 if (vap->iv_bss != NULL) {
216 ieee80211_free_node(vap->iv_bss);
217 vap->iv_update_bss(vap, NULL);
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;
227 * Port authorize/unauthorize interfaces for use by an authenticator.
231 ieee80211_node_authorize(struct ieee80211_node *ni)
233 struct ieee80211vap *vap = ni->ni_vap;
235 ni->ni_flags |= IEEE80211_NODE_AUTH;
236 ni->ni_inact_reload = vap->iv_inact_run;
237 ni->ni_inact = ni->ni_inact_reload;
239 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
240 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
244 ieee80211_node_unauthorize(struct ieee80211_node *ni)
246 struct ieee80211vap *vap = ni->ni_vap;
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;
253 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
254 "%s: inact_reload %u inact %u", __func__,
255 ni->ni_inact_reload, ni->ni_inact);
259 * Fix tx parameters for a node according to ``association state''.
262 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
264 struct ieee80211vap *vap = ni->ni_vap;
265 enum ieee80211_phymode mode;
267 if (ni->ni_flags & IEEE80211_NODE_VHT) {
268 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
269 mode = IEEE80211_MODE_VHT_5GHZ;
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;
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;
291 mode = IEEE80211_MODE_11B;
293 ni->ni_txparms = &vap->iv_txparms[mode];
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
302 ieee80211_node_set_chan(struct ieee80211_node *ni,
303 struct ieee80211_channel *chan)
305 struct ieee80211com *ic = ni->ni_ic;
306 struct ieee80211vap *vap = ni->ni_vap;
307 enum ieee80211_phymode mode;
309 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
312 mode = ieee80211_chan2mode(chan);
313 if (IEEE80211_IS_CHAN_HT(chan)) {
315 * We must install the legacy rate est in ni_rates and the
316 * HT rate set in ni_htrates.
318 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
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.
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;
334 ni->ni_txparms = &vap->iv_txparms[mode];
335 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
339 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
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? */
350 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
352 struct ieee80211com *ic = vap->iv_ic;
353 struct ieee80211_node *ni;
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),
362 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr,
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 */
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);
385 net80211_get_random_bytes(ni->ni_bssid,
387 /* clear group bit, add local bit */
388 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
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);
394 #ifdef IEEE80211_SUPPORT_TDMA
395 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
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);
405 * Fix the channel and related attributes.
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);
416 * Do mode-specific setup.
418 if (IEEE80211_IS_CHAN_FULL(chan)) {
419 if (IEEE80211_IS_CHAN_ANYG(chan)) {
421 * Use a mixed 11b/11g basic rate set.
423 ieee80211_setbasicrates(&ni->ni_rates,
425 if (vap->iv_flags & IEEE80211_F_PUREG) {
427 * Also mark OFDM rates basic so 11b
428 * stations do not join (WiFi compliance).
430 ieee80211_addbasicrates(&ni->ni_rates,
433 } else if (IEEE80211_IS_CHAN_B(chan)) {
435 * Force pure 11b rate set.
437 ieee80211_setbasicrates(&ni->ni_rates,
442 /* XXX TODO: other bits and pieces - eg fast-frames? */
444 /* If we're an 11n channel then initialise the 11n bits */
445 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
447 ieee80211_ht_node_init(ni);
448 ieee80211_vht_node_init(ni);
449 } else if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
451 ieee80211_ht_node_init(ni);
454 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
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
464 ieee80211_reset_bss(struct ieee80211vap *vap)
466 struct ieee80211com *ic = vap->iv_ic;
467 struct ieee80211_node *ni, *obss;
469 IEEE80211_LOCK_ASSERT(ic);
471 ieee80211_node_table_reset(&ic->ic_sta, vap);
472 /* XXX multi-bss: wrong */
473 ieee80211_vap_reset_erp(vap);
475 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr,
477 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
478 obss = vap->iv_update_bss(vap, ieee80211_ref_node(ni));
481 ni->ni_intval = ic->ic_bintval;
482 ieee80211_free_node(obss);
484 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
488 match_ssid(const struct ieee80211_node *ni,
489 int nssid, const struct ieee80211_scan_ssid ssids[])
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)
502 * Test a node for suitability/compatibility.
505 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
507 struct ieee80211com *ic = ni->ni_ic;
510 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
512 if (vap->iv_opmode == IEEE80211_M_IBSS) {
513 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
516 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
519 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
520 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
523 /* XXX does this mean privacy is supported or required? */
524 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
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)
531 if (vap->iv_des_nssid != 0 &&
532 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
534 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
535 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
540 #ifdef IEEE80211_DEBUG
542 * Display node suitability/compatibility.
545 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
547 struct ieee80211com *ic = ni->ni_ic;
552 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
554 if (vap->iv_opmode == IEEE80211_M_IBSS) {
555 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
558 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
561 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
562 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
565 /* XXX does this mean privacy is supported or required? */
566 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
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)
573 if (vap->iv_des_nssid != 0 &&
574 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
576 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
577 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
580 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
581 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
583 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
584 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
585 fail & 0x08 ? '!' : ' ');
587 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
588 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
590 fail & 0x02 ? '!' : ' ');
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 ? "!" : "");
597 #endif /* IEEE80211_DEBUG */
600 ieee80211_ibss_merge_check(struct ieee80211_node *ni)
602 struct ieee80211vap *vap = ni->ni_vap;
604 if (ni == vap->iv_bss ||
605 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
606 /* unchanged, nothing to do */
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);
618 vap->iv_stats.is_ibss_capmismatch++;
626 * Check if the given node should populate the node table.
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.
632 * So this check ensures the node can actually belong to our IBSS
633 * configuration. For now it simply checks the SSID.
636 ieee80211_ibss_node_check_new(struct ieee80211_node *ni,
637 const struct ieee80211_scanparams *scan)
639 struct ieee80211vap *vap = ni->ni_vap;
643 * If we have no SSID and no scan SSID, return OK.
645 if (vap->iv_des_nssid == 0 && scan->ssid == NULL)
649 * If we have one of (SSID, scan SSID) then return error.
651 if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL))
655 * Double-check - we need scan SSID.
657 if (scan->ssid == NULL)
661 * Check if the scan SSID matches the SSID list for the VAP.
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])
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)
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.
688 * The caller is assumed to validate TSF's before attempting a merge.
690 * Return !0 if the BSSID changed, 0 otherwise.
693 ieee80211_ibss_merge(struct ieee80211_node *ni)
695 #ifdef IEEE80211_DEBUG
696 struct ieee80211vap *vap = ni->ni_vap;
699 if (! ieee80211_ibss_merge_check(ni))
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" : ""
709 return ieee80211_sta_join1(ieee80211_ref_node(ni));
713 * Calculate HT channel promotion flags for all vaps.
714 * This assumes ni_chan have been setup for each vap.
717 gethtadjustflags(struct ieee80211com *ic)
719 struct ieee80211vap *vap;
724 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
725 if (vap->iv_state < IEEE80211_S_RUN)
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);
744 * Calculate VHT channel promotion flags for all vaps.
745 * This assumes ni_chan have been setup for each vap.
748 getvhtadjustflags(struct ieee80211com *ic)
750 struct ieee80211vap *vap;
755 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
756 if (vap->iv_state < IEEE80211_S_RUN)
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);
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.
779 * Same applies for VHT.
782 ieee80211_sync_curchan(struct ieee80211com *ic)
784 struct ieee80211_channel *c;
786 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
787 c = ieee80211_vht_adjust_channel(ic, c, getvhtadjustflags(ic));
789 if (c != ic->ic_curchan) {
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);
801 * Setup the current channel. The request channel may be
802 * promoted if other vap's are operating with HT20/HT40.
805 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
807 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
808 int flags = gethtadjustflags(ic);
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.
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);
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.
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);
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);
837 * Change the current channel. The channel change is guaranteed to have
838 * happened before the next state change.
841 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
843 ieee80211_setupcurchan(ic, c);
844 ieee80211_runtask(ic, &ic->ic_chan_task);
848 ieee80211_update_chw(struct ieee80211com *ic)
851 ieee80211_setupcurchan(ic, ic->ic_curchan);
852 ieee80211_runtask(ic, &ic->ic_chw_task);
856 * Join the specified IBSS/BSS network. The node is assumed to
857 * be passed in with a held reference.
860 ieee80211_sta_join1(struct ieee80211_node *selbs)
862 struct ieee80211vap *vap = selbs->ni_vap;
863 struct ieee80211com *ic = selbs->ni_ic;
864 struct ieee80211_node *obss;
868 * Committed to selbs, setup state.
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);
874 * Check if old+new node have the same address in which
875 * case we can reassociate when operating in sta mode.
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));
882 struct ieee80211_node_table *nt = obss->ni_table;
884 copy_bss(selbs, obss);
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);
891 ieee80211_free_node(obss); /* iv_bss reference */
894 obss = NULL; /* NB: guard against later use */
898 * Delete unusable rates; we've already checked
899 * that the negotiated rate set is acceptable.
901 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
902 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
904 ieee80211_setcurchan(ic, selbs->ni_chan);
906 * Set the erp state (mostly the slot time) to deal with
907 * the auto-select case; this should be redundant if the
910 ieee80211_vap_reset_erp(vap);
911 ieee80211_wme_initparams(vap);
913 if (vap->iv_opmode == IEEE80211_M_STA) {
916 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
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
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);
932 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
937 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
938 const struct ieee80211_scan_entry *se)
940 struct ieee80211com *ic = vap->iv_ic;
941 struct ieee80211_node *ni;
944 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr,
952 * Expand scan state into node's format.
953 * XXX may not need all this stuff
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;
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;
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);
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);
987 #ifdef IEEE80211_SUPPORT_TDMA
988 if (ni->ni_ies.tdma_ie != NULL)
989 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
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);
996 /* XXX parse BSSLOAD IE */
997 /* XXX parse TXPWRENV IE */
998 /* XXX parse APCHANREP IE */
1001 vap->iv_dtim_period = se->se_dtimperiod;
1002 vap->iv_dtim_count = 0;
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;
1011 * Setup HT state for this node if it's available, otherwise
1012 * non-STA modes won't pick this state up.
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.
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);
1029 * Setup VHT state for this node if it's available.
1030 * Same as the above.
1032 * For now, don't allow 2GHz VHT operation.
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",
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);
1053 /* Finally do the node channel change */
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);
1062 /* XXX else check for ath FF? */
1063 /* XXX QoS? Difficult given that WME config is specific to a master */
1065 ieee80211_node_setuptxparms(ni);
1066 ieee80211_ratectl_node_init(ni);
1068 return ieee80211_sta_join1(ieee80211_ref_node(ni));
1072 * Leave the specified IBSS/BSS network. The node is assumed to
1073 * be passed in with a held reference.
1076 ieee80211_sta_leave(struct ieee80211_node *ni)
1078 struct ieee80211com *ic = ni->ni_ic;
1080 ic->ic_node_cleanup(ni);
1081 ieee80211_notify_node_leave(ni);
1085 * Send a deauthenticate frame and drop the station.
1088 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
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);
1098 static struct ieee80211_node *
1099 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1101 struct ieee80211_node *ni;
1103 ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
1104 M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1109 node_init(struct ieee80211_node *ni)
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
1121 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
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);
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) {
1135 /* NB: pointers have already been zero'd above */
1139 memcpy(ies->data, data, len);
1145 * Reclaim storage for an ie blob.
1148 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
1150 if (ies->data != NULL)
1151 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
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.
1160 ieee80211_ies_expand(struct ieee80211_ies *ies)
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);
1177 case IEEE80211_ELEMID_VENDOR:
1180 else if (iswmeoui(ie))
1182 #ifdef IEEE80211_SUPPORT_SUPERG
1183 else if (isatherosoui(ie))
1186 #ifdef IEEE80211_SUPPORT_TDMA
1187 else if (istdmaoui(ie))
1191 case IEEE80211_ELEMID_RSN:
1194 case IEEE80211_ELEMID_HTCAP:
1197 case IEEE80211_ELEMID_HTINFO:
1198 ies->htinfo_ie = ie;
1200 #ifdef IEEE80211_SUPPORT_MESH
1201 case IEEE80211_ELEMID_MESHID:
1202 ies->meshid_ie = ie;
1205 case IEEE80211_ELEMID_VHT_CAP:
1206 ies->vhtcap_ie = ie;
1208 case IEEE80211_ELEMID_VHT_OPMODE:
1209 ies->vhtopmode_ie = ie;
1211 case IEEE80211_ELEMID_VHT_PWR_ENV:
1212 ies->vhtpwrenv_ie = ie;
1214 case IEEE80211_ELEMID_BSSLOAD:
1215 ies->bssload_ie = ie;
1217 case IEEE80211_ELEMID_APCHANREP:
1218 ies->apchanrep_ie = ie;
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).
1233 node_cleanup(struct ieee80211_node *ni)
1235 struct ieee80211vap *vap = ni->ni_vap;
1236 struct ieee80211com *ic = ni->ni_ic;
1239 /* NB: preserve ni_table */
1240 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1241 if (vap->iv_opmode != IEEE80211_M_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);
1248 * Cleanup any VHT and HT-related state.
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);
1258 #ifdef IEEE80211_SUPPORT_MESH
1260 * Cleanup any mesh-related state.
1262 if (vap->iv_opmode == IEEE80211_M_MBSS)
1263 ieee80211_mesh_node_cleanup(ni);
1266 * Clear any staging queue entries.
1268 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
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.
1278 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1281 * Drain power save queue and, if needed, clear TIM.
1283 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1284 vap->iv_set_tim(ni, 0);
1287 if (ni->ni_challenge != NULL) {
1288 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1289 ni->ni_challenge = NULL;
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.
1301 * XXX does this leave us open to inheriting old state?
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;
1309 * Must be careful here to remove any key map entry w/o a LOR.
1311 ieee80211_node_delucastkey(ni);
1315 node_free(struct ieee80211_node *ni)
1317 struct ieee80211com *ic = ni->ni_ic;
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);
1327 node_age(struct ieee80211_node *ni)
1329 struct ieee80211vap *vap = ni->ni_vap;
1332 * Age frames on the power save queue.
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);
1338 * Age out HT resources (e.g. frames on the
1339 * A-MPDU reorder queues).
1341 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1342 ieee80211_ht_node_age(ni);
1346 node_getrssi(const struct ieee80211_node *ni)
1348 uint32_t avgrssi = ni->ni_avgrssi;
1351 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1353 rssi = IEEE80211_RSSI_GET(avgrssi);
1354 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1358 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1360 *rssi = node_getrssi(ni);
1361 *noise = ni->ni_noise;
1365 node_getmimoinfo(const struct ieee80211_node *ni,
1366 struct ieee80211_mimo_info *info)
1372 bzero(info, sizeof(*info));
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;
1380 rssi = IEEE80211_RSSI_GET(avgrssi);
1381 info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1383 info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i];
1386 /* XXX ext radios? */
1392 ieee80211_add_node_nt(struct ieee80211_node_table *nt,
1393 struct ieee80211_node *ni)
1395 struct ieee80211com *ic = nt->nt_ic;
1398 IEEE80211_NODE_LOCK_ASSERT(nt);
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);
1409 ieee80211_del_node_nt(struct ieee80211_node_table *nt,
1410 struct ieee80211_node *ni)
1413 IEEE80211_NODE_LOCK_ASSERT(nt);
1415 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1416 LIST_REMOVE(ni, ni_hash);
1418 KASSERT(nt->nt_count >= 0,
1419 ("nt_count is negative (%d)!\n", nt->nt_count));
1420 ni->ni_table = NULL;
1423 static struct ieee80211_node *
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)
1428 struct ieee80211com *ic = nt->nt_ic;
1429 struct ieee80211_node *ni;
1431 ni = ic->ic_node_alloc(vap, macaddr);
1433 vap->iv_stats.is_rx_nodealloc++;
1437 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1438 "%s %p<%s> in %s table\n", __func__, ni,
1439 ether_sprintf(macaddr), nt->nt_name);
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));
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);
1462 IEEE80211_NODE_LOCK(nt);
1463 ieee80211_add_node_nt(nt, ni);
1466 IEEE80211_NODE_UNLOCK(nt);
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);
1477 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1478 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
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.
1489 struct ieee80211_node *
1490 ieee80211_tmp_node(struct ieee80211vap *vap,
1491 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1493 struct ieee80211com *ic = vap->iv_ic;
1494 struct ieee80211_node *ni;
1496 ni = ic->ic_node_alloc(vap, macaddr);
1498 struct ieee80211_node *bss = vap->iv_bss;
1500 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1501 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1503 ni->ni_table = NULL; /* NB: pedantic */
1504 ni->ni_ic = ic; /* NB: needed to set channel */
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));
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");
1524 ieee80211_ratectl_node_init(ni);
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);
1537 vap->iv_stats.is_rx_nodealloc++;
1542 struct ieee80211_node *
1543 ieee80211_dup_bss(struct ieee80211vap *vap,
1544 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1546 struct ieee80211com *ic = vap->iv_ic;
1547 struct ieee80211_node *ni;
1549 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr, __func__, __LINE__);
1551 struct ieee80211_node *bss = vap->iv_bss;
1553 * Inherit from iv_bss.
1556 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1557 ieee80211_node_set_chan(ni, bss->ni_chan);
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.
1569 struct ieee80211_node *
1570 ieee80211_node_create_wds(struct ieee80211vap *vap,
1571 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1573 struct ieee80211com *ic = vap->iv_ic;
1574 struct ieee80211_node *ni;
1576 /* XXX check if node already in sta table? */
1577 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid, __func__, __LINE__);
1579 ni->ni_wdsvap = vap;
1580 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1582 * Inherit any manually configured settings.
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 */
1591 * There are no management frames to use to
1592 * discover neighbor capabilities, so blindly
1593 * propagate the local configuration.
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;
1602 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1603 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
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.
1609 ieee80211_ht_wds_init(ni);
1610 if (vap->iv_vht_flags & IEEE80211_FVHT_VHT) {
1611 printf("%s: TODO: vht_wds_init\n", __func__);
1614 struct ieee80211_channel *c = ni->ni_chan;
1616 * Force a legacy channel to be used.
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));
1628 struct ieee80211_node *
1629 #ifdef IEEE80211_DEBUG_REFCNT
1630 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1631 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1633 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1634 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1637 struct ieee80211_node *ni;
1640 IEEE80211_NODE_LOCK_ASSERT(nt);
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__,
1650 ni, ether_sprintf(ni->ni_macaddr),
1651 ieee80211_node_refcnt(ni));
1659 struct ieee80211_node *
1660 #ifdef IEEE80211_DEBUG_REFCNT
1661 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1662 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1664 ieee80211_find_node(struct ieee80211_node_table *nt,
1665 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1668 struct ieee80211_node *ni;
1670 IEEE80211_NODE_LOCK(nt);
1671 ni = ieee80211_find_node_locked(nt, macaddr);
1672 IEEE80211_NODE_UNLOCK(nt);
1676 struct ieee80211_node *
1677 #ifdef IEEE80211_DEBUG_REFCNT
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)
1682 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1683 const struct ieee80211vap *vap,
1684 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1687 struct ieee80211_node *ni;
1690 IEEE80211_NODE_LOCK_ASSERT(nt);
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__,
1701 ni, ether_sprintf(ni->ni_macaddr),
1702 ieee80211_node_refcnt(ni));
1710 struct ieee80211_node *
1711 #ifdef IEEE80211_DEBUG_REFCNT
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)
1716 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1717 const struct ieee80211vap *vap,
1718 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1721 struct ieee80211_node *ni;
1723 IEEE80211_NODE_LOCK(nt);
1724 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1725 IEEE80211_NODE_UNLOCK(nt);
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.
1735 struct ieee80211_node *
1736 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1737 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1739 struct ieee80211_node *ni;
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);
1745 struct ieee80211com *ic = vap->iv_ic;
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) {
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).
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;
1769 ieee80211_node_setuptxparms(ni);
1770 ieee80211_ratectl_node_init(ni);
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.)
1780 * This is the first time we see the node.
1782 if (ic->ic_newassoc != NULL)
1783 ic->ic_newassoc(ni, 1);
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.
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.
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 */
1809 /* XXX not right for 802.1x/WPA */
1810 ieee80211_node_authorize(ni);
1816 ieee80211_init_neighbor(struct ieee80211_node *ni,
1817 const struct ieee80211_frame *wh,
1818 const struct ieee80211_scanparams *sp)
1820 int do_ht_setup = 0, do_vht_setup = 0;
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);
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;
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);
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);
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);
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)) {
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)) {
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);
1876 * If the neighbor is HT compatible, flip that on.
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);
1887 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1888 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
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);
1904 * Finally do the channel upgrade/change based
1905 * on the HT/VHT configuration.
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);
1915 ieee80211_node_setuptxparms(ni);
1916 ieee80211_ratectl_node_init(ni);
1918 /* Reassociate; we're now 11n/11ac */
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
1927 * So, do we just create a new node and delete
1930 if (ni->ni_ic->ic_newassoc)
1931 ni->ni_ic->ic_newassoc(ni, 1);
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.
1941 struct ieee80211_node *
1942 ieee80211_add_neighbor(struct ieee80211vap *vap,
1943 const struct ieee80211_frame *wh,
1944 const struct ieee80211_scanparams *sp)
1946 struct ieee80211_node *ni;
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? */
1952 struct ieee80211com *ic = vap->iv_ic;
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);
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))
1974 static __inline struct ieee80211_node *
1975 _find_rxnode(struct ieee80211_node_table *nt,
1976 const struct ieee80211_frame_min *wh)
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);
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.
1988 struct ieee80211_node *
1989 #ifdef IEEE80211_DEBUG_REFCNT
1990 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1991 const struct ieee80211_frame_min *wh, const char *func, int line)
1993 ieee80211_find_rxnode(struct ieee80211com *ic,
1994 const struct ieee80211_frame_min *wh)
1997 struct ieee80211_node_table *nt;
1998 struct ieee80211_node *ni;
2001 IEEE80211_NODE_LOCK(nt);
2002 ni = _find_rxnode(nt, wh);
2003 IEEE80211_NODE_UNLOCK(nt);
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.
2016 struct ieee80211_node *
2017 #ifdef IEEE80211_DEBUG_REFCNT
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)
2022 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
2023 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
2026 struct ieee80211_node_table *nt;
2027 struct ieee80211_node *ni;
2030 IEEE80211_NODE_LOCK(nt);
2031 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
2032 ni = nt->nt_keyixmap[keyix];
2036 ni = _find_rxnode(nt, wh);
2037 if (ni != NULL && nt->nt_keyixmap != NULL) {
2039 * If the station has a unicast key cache slot
2040 * assigned update the key->node mapping table.
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,
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);
2055 if (IS_BCAST_PROBEREQ(wh))
2056 ni = NULL; /* spam bcast probe req to all vap's */
2058 ieee80211_ref_node(ni);
2060 IEEE80211_NODE_UNLOCK(nt);
2064 #undef IS_BCAST_PROBEREQ
2068 * Return a reference to the appropriate node for sending
2069 * a data frame. This handles node discovery in adhoc networks.
2071 struct ieee80211_node *
2072 #ifdef IEEE80211_DEBUG_REFCNT
2073 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
2074 const uint8_t macaddr[IEEE80211_ADDR_LEN],
2075 const char *func, int line)
2077 ieee80211_find_txnode(struct ieee80211vap *vap,
2078 const uint8_t macaddr[IEEE80211_ADDR_LEN])
2081 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
2082 struct ieee80211_node *ni;
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
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);
2097 ni = ieee80211_find_node_locked(nt, macaddr);
2098 IEEE80211_NODE_UNLOCK(nt);
2101 if (vap->iv_opmode == IEEE80211_M_IBSS ||
2102 vap->iv_opmode == IEEE80211_M_AHDEMO) {
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.
2110 * XXX TODO: this doesn't fake up 11n state; we need
2111 * to find another way to get it upgraded.
2113 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
2115 (void) ieee80211_ref_node(ni);
2117 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
2118 "no node, discard frame (%s)", __func__);
2119 vap->iv_stats.is_tx_nonode++;
2126 _ieee80211_free_node(struct ieee80211_node *ni)
2128 struct ieee80211_node_table *nt = ni->ni_table;
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.
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>");
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);
2148 ieee80211_del_node_nt(nt, ni);
2149 ni->ni_ic->ic_node_free(ni);
2153 * Clear any entry in the unicast key mapping table.
2156 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2158 ieee80211_keyix keyix;
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);
2175 #ifdef IEEE80211_DEBUG_REFCNT
2176 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
2178 ieee80211_free_node(struct ieee80211_node *ni)
2181 struct ieee80211_node_table *nt = ni->ni_table;
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);
2189 IEEE80211_NODE_LOCK(nt);
2190 if (ieee80211_node_dectestref(ni)) {
2192 * Last reference, reclaim state.
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);
2200 if (ieee80211_node_dectestref(ni))
2201 _ieee80211_free_node(ni);
2206 * Reclaim a unicast key and clear any key cache state.
2209 ieee80211_node_delucastkey(struct ieee80211_node *ni)
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;
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.
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
2229 isowned = IEEE80211_NODE_IS_LOCKED(nt);
2231 IEEE80211_NODE_LOCK(nt);
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;
2243 IEEE80211_NODE_UNLOCK(nt);
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);
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.
2263 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2266 IEEE80211_NODE_LOCK_ASSERT(nt);
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);
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.
2279 (void)node_clear_keyixmap(nt, ni);
2280 if (!ieee80211_node_dectestref(ni)) {
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
2287 ieee80211_del_node_nt(nt, ni);
2289 _ieee80211_free_node(ni);
2293 * Node table support.
2297 ieee80211_node_table_init(struct ieee80211com *ic,
2298 struct ieee80211_node_table *nt,
2299 const char *name, int inact, int keyixmax)
2303 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
2304 TAILQ_INIT(&nt->nt_node);
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 *),
2313 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2314 if (nt->nt_keyixmap == NULL)
2316 "Cannot allocate key index map with %u entries\n",
2319 nt->nt_keyixmap = NULL;
2323 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
2324 struct ieee80211vap *match)
2326 struct ieee80211_node *ni, *next;
2328 IEEE80211_NODE_LOCK(nt);
2329 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
2330 if (match != NULL && ni->ni_vap != match)
2332 /* XXX can this happen? if so need's work */
2333 if (ni->ni_associd != 0) {
2334 struct ieee80211vap *vap = ni->ni_vap;
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);
2341 ni->ni_wdsvap = NULL; /* clear reference */
2342 node_reclaim(nt, ni);
2344 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
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
2352 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
2353 if (ni->ni_wdsvap == match)
2354 ni->ni_wdsvap = NULL;
2356 IEEE80211_NODE_UNLOCK(nt);
2360 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2362 ieee80211_node_table_reset(nt, NULL);
2363 if (nt->nt_keyixmap != NULL) {
2365 /* XXX verify all entries are NULL */
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__,
2372 IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
2373 nt->nt_keyixmap = NULL;
2375 IEEE80211_NODE_LOCK_DESTROY(nt);
2379 timeout_stations(void *arg __unused, struct ieee80211_node *ni)
2381 struct ieee80211com *ic = ni->ni_ic;
2382 struct ieee80211vap *vap = ni->ni_vap;
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.
2391 if (vap->iv_state != IEEE80211_S_RUN)
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).
2399 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2400 vap->iv_opmode == IEEE80211_M_STA) &&
2401 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2404 * Free fragment if not needed anymore
2405 * (last fragment older than 1s).
2406 * XXX doesn't belong here, move to node_age
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;
2413 if (ni->ni_inact > 0) {
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);
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
2425 /* XXX before inact decrement? */
2426 if (ni == vap->iv_bss)
2428 if (ni->ni_associd != 0 ||
2429 (vap->iv_opmode == IEEE80211_M_IBSS ||
2430 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2432 * Age/drain resources held by the station.
2434 ic->ic_node_age(ni);
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...).
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.
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) {
2452 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2454 "probe station due to inactivity");
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.
2462 /* XXX fix this (not required anymore). */
2463 ieee80211_ref_node(ni);
2465 ieee80211_send_nulldata(ni);
2470 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2471 ni->ni_inact <= 0) {
2473 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2474 "station timed out due to inactivity "
2475 "(refcnt %u)", ieee80211_node_refcnt(ni));
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.
2487 if (ni->ni_associd != 0) {
2488 IEEE80211_SEND_MGMT(ni,
2489 IEEE80211_FC0_SUBTYPE_DEAUTH,
2490 IEEE80211_REASON_AUTH_EXPIRE);
2492 ieee80211_node_leave(ni);
2493 vap->iv_stats.is_node_timeout++;
2498 * Timeout inactive stations and do related housekeeping.
2501 ieee80211_timeout_stations(struct ieee80211com *ic)
2503 struct ieee80211_node_table *nt = &ic->ic_sta;
2505 ieee80211_iterate_nodes(nt, timeout_stations, NULL);
2509 * Aggressively reclaim resources. This should be used
2510 * only in a critical situation to reclaim mbuf resources.
2513 ieee80211_drain(struct ieee80211com *ic)
2515 struct ieee80211_node_table *nt = &ic->ic_sta;
2516 struct ieee80211vap *vap;
2517 struct ieee80211_node *ni;
2519 IEEE80211_NODE_LOCK(nt);
2520 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
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).
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.
2535 if (vap->iv_state != IEEE80211_S_RUN)
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)
2544 * XXX doesn't belong here, move to node_drain
2546 if (ni->ni_rxfrag[0] != NULL) {
2547 m_freem(ni->ni_rxfrag[0]);
2548 ni->ni_rxfrag[0] = NULL;
2551 * Drain resources held by the station.
2553 ic->ic_node_drain(ni);
2555 IEEE80211_NODE_UNLOCK(nt);
2559 * Per-ieee80211vap inactivity timer callback.
2562 ieee80211_vap_timeout(struct ieee80211vap *vap)
2565 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2567 ieee80211_vap_erp_timeout(vap);
2568 ieee80211_ht_timeout(vap);
2569 ieee80211_vht_timeout(vap);
2573 * Per-ieee80211com inactivity timer callback.
2576 ieee80211_node_timeout(void *arg)
2578 struct ieee80211com *ic = arg;
2579 struct ieee80211vap *vap;
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.
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);
2596 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2597 ieee80211_vap_timeout(vap);
2598 IEEE80211_UNLOCK(ic);
2600 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2601 ieee80211_node_timeout, ic);
2605 * The same as ieee80211_iterate_nodes(), but for one vap only.
2608 ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
2609 struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
2611 struct ieee80211_node **ni_arr;
2612 struct ieee80211_node *ni;
2617 * Iterate over the node table and save an array of ref'ed nodes.
2619 * This is separated out from calling the actual node function so that
2620 * no LORs will occur.
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);
2633 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2634 if (vap != NULL && ni->ni_vap != vap)
2637 ("node array overflow (vap %p, i %d, count %d)\n",
2639 ni_arr[i] = ieee80211_ref_node(ni);
2642 IEEE80211_NODE_UNLOCK(nt);
2644 for (i = 0; i < count; i++) {
2645 if (ni_arr[i] == NULL) /* end of the list */
2647 (*f)(arg, ni_arr[i]);
2648 /* ieee80211_free_node() locks by itself */
2649 ieee80211_free_node(ni_arr[i]);
2652 IEEE80211_FREE(ni_arr, M_80211_NODE);
2658 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2659 * reference in the source.
2662 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2663 ieee80211_iter_func *f, void *arg)
2665 /* XXX no way to pass error to the caller. */
2666 (void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
2670 ieee80211_dump_node(struct ieee80211_node_table *nt __unused,
2671 struct ieee80211_node *ni)
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->ic_freq, ni->ni_chan->ic_flags);
2691 printf("\tinact %u inact_reload %u txrate %u\n",
2692 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2693 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2694 ni->ni_htcap, ni->ni_htparam,
2695 ni->ni_htctlchan, ni->ni_ht2ndchan);
2696 printf("\thtopmode %x htstbc %x htchw %u\n",
2697 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2698 printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n",
2699 ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2,
2700 (int) ni->ni_vht_basicmcs);
2705 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2707 ieee80211_iterate_nodes(nt,
2708 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2712 * Iterate over the VAPs and update their ERP beacon IEs.
2714 * Note this must be called from the deferred ERP update task paths.
2717 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2719 struct ieee80211vap *vap;
2721 IEEE80211_LOCK_ASSERT(ic);
2723 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2724 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2725 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2729 * Handle a station joining an 11g network.
2732 ieee80211_node_join_11g(struct ieee80211_node *ni)
2734 struct ieee80211com *ic = ni->ni_ic;
2735 struct ieee80211vap *vap = ni->ni_vap;
2737 IEEE80211_LOCK_ASSERT(ic);
2740 * Station isn't capable of short slot time. Bump
2741 * the count of long slot time stations and disable
2742 * use of short slot time. Note that the actual switch
2743 * over to long slot time use may not occur until the
2744 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2746 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2747 vap->iv_longslotsta++;
2748 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2749 "station needs long slot time, count %d",
2750 vap->iv_longslotsta);
2752 * XXX TODO: this may need all VAPs checked!
2754 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2756 * Don't force slot time when switched to turbo
2757 * mode as non-ERP stations won't be present; this
2758 * need only be done when on the normal G channel.
2760 ieee80211_vap_set_shortslottime(vap, 0);
2764 * If the new station is not an ERP station
2765 * then bump the counter and enable protection
2768 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2769 vap->iv_nonerpsta++;
2770 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2771 "station is !ERP, %d non-ERP stations associated",
2774 * If station does not support short preamble
2775 * then we must enable use of Barker preamble.
2777 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2778 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2779 "%s", "station needs long preamble");
2780 vap->iv_flags |= IEEE80211_F_USEBARKER;
2781 vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE;
2782 ieee80211_vap_update_preamble(vap);
2785 * If protection is configured and this is the first
2786 * indication we should use protection, enable it.
2788 if (vap->iv_protmode != IEEE80211_PROT_NONE &&
2789 vap->iv_nonerpsta == 1 &&
2790 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2791 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2792 "%s: enable use of protection\n", __func__);
2793 vap->iv_flags |= IEEE80211_F_USEPROT;
2794 ieee80211_vap_update_erp_protmode(vap);
2797 ni->ni_flags |= IEEE80211_NODE_ERP;
2801 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2803 struct ieee80211com *ic = ni->ni_ic;
2804 struct ieee80211vap *vap = ni->ni_vap;
2807 if (ni->ni_associd == 0) {
2810 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2812 * It would be good to search the bitmap
2813 * more efficiently, but this will do for now.
2815 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2816 if (!IEEE80211_AID_ISSET(vap, aid))
2819 if (aid >= vap->iv_max_aid) {
2820 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2821 ieee80211_node_leave(ni);
2824 ni->ni_associd = aid | 0xc000;
2825 ni->ni_jointime = time_uptime;
2827 IEEE80211_AID_SET(vap, ni->ni_associd);
2828 vap->iv_sta_assoc++;
2830 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2831 ieee80211_ht_node_join(ni);
2832 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2833 ieee80211_vht_node_join(ni);
2834 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2835 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2836 ieee80211_node_join_11g(ni);
2837 IEEE80211_UNLOCK(ic);
2844 * XXX VHT - should log VHT channel width, etc
2846 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2847 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s%s",
2848 IEEE80211_NODE_AID(ni),
2849 vap->iv_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2850 vap->iv_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2851 vap->iv_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2852 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2853 /* XXX update for VHT string */
2854 ni->ni_flags & IEEE80211_NODE_HT ?
2855 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2856 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2857 ni->ni_flags & IEEE80211_NODE_AMSDU ? " (+AMSDU)" : "",
2858 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2859 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2860 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2861 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2862 ", fast-frames" : "",
2863 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2867 ieee80211_node_setuptxparms(ni);
2868 ieee80211_ratectl_node_init(ni);
2869 /* give driver a chance to setup state like ni_txrate */
2870 if (ic->ic_newassoc != NULL)
2871 ic->ic_newassoc(ni, newassoc);
2872 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2873 /* tell the authenticator about new station */
2874 if (vap->iv_auth->ia_node_join != NULL)
2875 vap->iv_auth->ia_node_join(ni);
2876 ieee80211_notify_node_join(ni,
2877 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2881 disable_protection(struct ieee80211vap *vap)
2883 struct ieee80211com *ic = vap->iv_ic;
2885 KASSERT(vap->iv_nonerpsta == 0 &&
2886 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2887 ("%d non ERP stations, flags 0x%x", vap->iv_nonerpsta,
2888 vap->iv_flags_ext));
2890 vap->iv_flags &= ~IEEE80211_F_USEPROT;
2891 /* XXX verify mode? */
2892 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2893 vap->iv_flags |= IEEE80211_F_SHPREAMBLE;
2894 vap->iv_flags &= ~IEEE80211_F_USEBARKER;
2896 ieee80211_vap_update_erp_protmode(vap);
2897 ieee80211_vap_update_preamble(vap);
2901 * Handle a station leaving an 11g network.
2904 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2906 struct ieee80211com *ic = ni->ni_ic;
2907 struct ieee80211vap *vap = ni->ni_vap;
2909 IEEE80211_LOCK_ASSERT(ic);
2911 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2912 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2913 ic->ic_bsschan->ic_flags));
2916 * If a long slot station do the slot time bookkeeping.
2918 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2919 KASSERT(vap->iv_longslotsta > 0,
2920 ("bogus long slot station count %d", vap->iv_longslotsta));
2921 vap->iv_longslotsta--;
2922 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2923 "long slot time station leaves, count now %d",
2924 vap->iv_longslotsta);
2926 * XXX TODO: this may need all VAPs checked!
2928 if (vap->iv_longslotsta == 0) {
2930 * Re-enable use of short slot time if supported
2931 * and not operating in IBSS mode (per spec).
2933 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2934 ic->ic_opmode != IEEE80211_M_IBSS) {
2935 IEEE80211_DPRINTF(ni->ni_vap,
2936 IEEE80211_MSG_ASSOC,
2937 "%s: re-enable use of short slot time\n",
2939 ieee80211_vap_set_shortslottime(vap, 1);
2944 * If a non-ERP station do the protection-related bookkeeping.
2946 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2947 KASSERT(vap->iv_nonerpsta > 0,
2948 ("bogus non-ERP station count %d", vap->iv_nonerpsta));
2949 vap->iv_nonerpsta--;
2950 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2951 "non-ERP station leaves, count now %d%s", vap->iv_nonerpsta,
2952 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2953 " (non-ERP sta present)" : "");
2954 if (vap->iv_nonerpsta == 0 &&
2955 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2956 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2957 "%s: disable use of protection\n", __func__);
2958 disable_protection(vap);
2964 * Time out presence of an overlapping bss with non-ERP
2965 * stations. When operating in hostap mode we listen for
2966 * beacons from other stations and if we identify a non-ERP
2967 * station is present we enable protection. To identify
2968 * when all non-ERP stations are gone we time out this
2972 ieee80211_vap_erp_timeout(struct ieee80211vap *vap)
2975 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2977 if ((vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2978 ieee80211_time_after(ticks, vap->iv_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2979 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
2980 "%s", "age out non-ERP sta present on channel");
2981 vap->iv_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2982 if (vap->iv_nonerpsta == 0)
2983 disable_protection(vap);
2988 * Handle bookkeeping for station deauthentication/disassociation
2989 * when operating as an ap.
2992 ieee80211_node_leave(struct ieee80211_node *ni)
2994 struct ieee80211com *ic = ni->ni_ic;
2995 struct ieee80211vap *vap = ni->ni_vap;
2996 struct ieee80211_node_table *nt = ni->ni_table;
2998 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2999 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
3001 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
3002 ("unexpected operating mode %u", vap->iv_opmode));
3004 * If node wasn't previously associated all
3005 * we need to do is reclaim the reference.
3007 /* XXX ibss mode bypasses 11g and notification */
3008 if (ni->ni_associd == 0)
3011 * Tell the authenticator the station is leaving.
3012 * Note that we must do this before yanking the
3013 * association id as the authenticator uses the
3014 * associd to locate it's state block.
3016 if (vap->iv_auth->ia_node_leave != NULL)
3017 vap->iv_auth->ia_node_leave(ni);
3020 IEEE80211_AID_CLR(vap, ni->ni_associd);
3021 vap->iv_sta_assoc--;
3023 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
3024 ieee80211_vht_node_leave(ni);
3025 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
3026 ieee80211_ht_node_leave(ni);
3027 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
3028 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
3029 ieee80211_node_leave_11g(ni);
3030 IEEE80211_UNLOCK(ic);
3032 * Cleanup station state. In particular clear various
3033 * state that might otherwise be reused if the node
3034 * is reused before the reference count goes to zero
3035 * (and memory is reclaimed).
3037 ieee80211_sta_leave(ni);
3040 * Remove the node from any table it's recorded in and
3041 * drop the caller's reference. Removal from the table
3042 * is important to insure the node is not reprocessed
3046 IEEE80211_NODE_LOCK(nt);
3047 node_reclaim(nt, ni);
3048 IEEE80211_NODE_UNLOCK(nt);
3050 ieee80211_free_node(ni);
3055 uint32_t rssi_total;
3059 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
3061 struct rssiinfo *info = arg;
3062 struct ieee80211vap *vap = ni->ni_vap;
3065 /* only associated stations */
3066 if (ni->ni_associd == 0)
3068 rssi = vap->iv_ic->ic_node_getrssi(ni);
3070 info->rssi_samples++;
3071 info->rssi_total += rssi;
3076 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
3078 struct rssiinfo *info = arg;
3079 struct ieee80211vap *vap = ni->ni_vap;
3082 /* only neighbors */
3083 /* XXX check bssid */
3084 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
3086 rssi = vap->iv_ic->ic_node_getrssi(ni);
3088 info->rssi_samples++;
3089 info->rssi_total += rssi;
3093 #ifdef IEEE80211_SUPPORT_MESH
3095 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
3097 struct rssiinfo *info = arg;
3098 struct ieee80211vap *vap = ni->ni_vap;
3101 /* only neighbors that peered successfully */
3102 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
3104 rssi = vap->iv_ic->ic_node_getrssi(ni);
3106 info->rssi_samples++;
3107 info->rssi_total += rssi;
3110 #endif /* IEEE80211_SUPPORT_MESH */
3113 ieee80211_getrssi(struct ieee80211vap *vap)
3115 #define NZ(x) ((x) == 0 ? 1 : (x))
3116 struct ieee80211com *ic = vap->iv_ic;
3117 struct rssiinfo info;
3119 info.rssi_total = 0;
3120 info.rssi_samples = 0;
3121 switch (vap->iv_opmode) {
3122 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
3123 case IEEE80211_M_AHDEMO: /* average of all neighbors */
3124 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
3127 case IEEE80211_M_HOSTAP: /* average of all associated stations */
3128 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
3131 #ifdef IEEE80211_SUPPORT_MESH
3132 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
3133 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
3137 case IEEE80211_M_MONITOR: /* XXX */
3138 case IEEE80211_M_STA: /* use stats from associated ap */
3140 if (vap->iv_bss != NULL)
3141 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
3142 info.rssi_samples = 1;
3145 return info.rssi_total / NZ(info.rssi_samples);
3150 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
3153 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
3155 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
3156 /* for non-station mode return avg'd rssi accounting */
3157 if (vap->iv_opmode != IEEE80211_M_STA)
3158 *rssi = ieee80211_getrssi(vap);