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]);
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)
174 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
175 /* XXX should we allow max aid to be zero? */
176 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
177 vap->iv_max_aid = IEEE80211_AID_MIN;
178 if_printf(vap->iv_ifp,
179 "WARNING: max aid too small, changed to %d\n",
182 vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC(
183 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
185 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
186 if (vap->iv_aid_bitmap == NULL) {
187 /* XXX no way to recover */
188 printf("%s: no memory for AID bitmap, max aid %d!\n",
189 __func__, vap->iv_max_aid);
194 ieee80211_reset_bss(vap);
196 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
200 ieee80211_node_vdetach(struct ieee80211vap *vap)
202 struct ieee80211com *ic = vap->iv_ic;
204 ieee80211_node_table_reset(&ic->ic_sta, vap);
205 if (vap->iv_bss != NULL) {
206 ieee80211_free_node(vap->iv_bss);
207 vap->iv_update_bss(vap, NULL);
209 if (vap->iv_aid_bitmap != NULL) {
210 IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE);
211 vap->iv_aid_bitmap = NULL;
216 * Port authorize/unauthorize interfaces for use by an authenticator.
220 ieee80211_node_authorize(struct ieee80211_node *ni)
222 struct ieee80211vap *vap = ni->ni_vap;
224 ni->ni_flags |= IEEE80211_NODE_AUTH;
225 ni->ni_inact_reload = vap->iv_inact_run;
226 ni->ni_inact = ni->ni_inact_reload;
228 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
229 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
233 ieee80211_node_unauthorize(struct ieee80211_node *ni)
235 struct ieee80211vap *vap = ni->ni_vap;
237 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
238 ni->ni_inact_reload = vap->iv_inact_auth;
239 if (ni->ni_inact > ni->ni_inact_reload)
240 ni->ni_inact = ni->ni_inact_reload;
242 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
243 "%s: inact_reload %u inact %u", __func__,
244 ni->ni_inact_reload, ni->ni_inact);
248 * Fix tx parameters for a node according to ``association state''.
251 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
253 struct ieee80211vap *vap = ni->ni_vap;
254 enum ieee80211_phymode mode;
256 if (ni->ni_flags & IEEE80211_NODE_VHT) {
257 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
258 mode = IEEE80211_MODE_VHT_5GHZ;
260 mode = IEEE80211_MODE_VHT_2GHZ;
261 } else if (ni->ni_flags & IEEE80211_NODE_HT) {
262 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
263 mode = IEEE80211_MODE_11NA;
265 mode = IEEE80211_MODE_11NG;
266 } else { /* legacy rate handling */
267 if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
268 mode = IEEE80211_MODE_STURBO_A;
269 else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
270 mode = IEEE80211_MODE_HALF;
271 else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
272 mode = IEEE80211_MODE_QUARTER;
273 /* NB: 108A should be handled as 11a */
274 else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
275 mode = IEEE80211_MODE_11A;
276 else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
277 (ni->ni_flags & IEEE80211_NODE_ERP))
278 mode = IEEE80211_MODE_11G;
280 mode = IEEE80211_MODE_11B;
282 ni->ni_txparms = &vap->iv_txparms[mode];
286 * Set/change the channel. The rate set is also updated as
287 * to insure a consistent view by drivers.
288 * XXX should be private but hostap needs it to deal with CSA
291 ieee80211_node_set_chan(struct ieee80211_node *ni,
292 struct ieee80211_channel *chan)
294 struct ieee80211com *ic = ni->ni_ic;
295 struct ieee80211vap *vap = ni->ni_vap;
296 enum ieee80211_phymode mode;
298 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
301 mode = ieee80211_chan2mode(chan);
302 if (IEEE80211_IS_CHAN_HT(chan)) {
304 * We must install the legacy rate est in ni_rates and the
305 * HT rate set in ni_htrates.
307 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
309 * Setup bss tx parameters based on operating mode. We
310 * use legacy rates when operating in a mixed HT+non-HT bss
311 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
313 if (mode == IEEE80211_MODE_11NA &&
314 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
315 mode = IEEE80211_MODE_11A;
316 else if (mode == IEEE80211_MODE_11NG &&
317 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
318 mode = IEEE80211_MODE_11G;
319 if (mode == IEEE80211_MODE_11G &&
320 (vap->iv_flags & IEEE80211_F_PUREG) == 0)
321 mode = IEEE80211_MODE_11B;
323 ni->ni_txparms = &vap->iv_txparms[mode];
324 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
328 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
330 /* propagate useful state */
331 nbss->ni_authmode = obss->ni_authmode;
332 nbss->ni_txpower = obss->ni_txpower;
333 nbss->ni_vlan = obss->ni_vlan;
334 /* XXX statistics? */
335 /* XXX legacy WDS bssid? */
339 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
341 struct ieee80211com *ic = vap->iv_ic;
342 struct ieee80211_node *ni;
344 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
345 "%s: creating %s on channel %u%c flags 0x%08x\n", __func__,
346 ieee80211_opmode_name[vap->iv_opmode],
347 ieee80211_chan2ieee(ic, chan),
348 ieee80211_channel_type_char(chan),
351 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
356 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
357 ni->ni_esslen = vap->iv_des_ssid[0].len;
358 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
359 if (vap->iv_bss != NULL)
360 copy_bss(ni, vap->iv_bss);
361 ni->ni_intval = ic->ic_bintval;
362 if (vap->iv_flags & IEEE80211_F_PRIVACY)
363 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
364 if (ic->ic_phytype == IEEE80211_T_FH) {
365 ni->ni_fhdwell = 200; /* XXX */
368 if (vap->iv_opmode == IEEE80211_M_IBSS) {
369 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
370 if (vap->iv_flags & IEEE80211_F_DESBSSID)
371 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
373 net80211_get_random_bytes(ni->ni_bssid,
375 /* clear group bit, add local bit */
376 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
378 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
379 if (vap->iv_flags & IEEE80211_F_DESBSSID)
380 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
382 #ifdef IEEE80211_SUPPORT_TDMA
383 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
385 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
386 #ifdef IEEE80211_SUPPORT_MESH
387 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
388 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
389 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
393 * Fix the channel and related attributes.
395 /* clear DFS CAC state on previous channel */
396 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
397 ic->ic_bsschan->ic_freq != chan->ic_freq &&
398 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
399 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
400 ic->ic_bsschan = chan;
401 ieee80211_node_set_chan(ni, chan);
402 ic->ic_curmode = ieee80211_chan2mode(chan);
404 * Do mode-specific setup.
406 if (IEEE80211_IS_CHAN_FULL(chan)) {
407 if (IEEE80211_IS_CHAN_ANYG(chan)) {
409 * Use a mixed 11b/11g basic rate set.
411 ieee80211_setbasicrates(&ni->ni_rates,
413 if (vap->iv_flags & IEEE80211_F_PUREG) {
415 * Also mark OFDM rates basic so 11b
416 * stations do not join (WiFi compliance).
418 ieee80211_addbasicrates(&ni->ni_rates,
421 } else if (IEEE80211_IS_CHAN_B(chan)) {
423 * Force pure 11b rate set.
425 ieee80211_setbasicrates(&ni->ni_rates,
430 /* XXX TODO: other bits and pieces - eg fast-frames? */
432 /* If we're an 11n channel then initialise the 11n bits */
433 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
435 ieee80211_ht_node_init(ni);
436 ieee80211_vht_node_init(ni);
437 } else if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
439 ieee80211_ht_node_init(ni);
442 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
446 * Reset bss state on transition to the INIT state.
447 * Clear any stations from the table (they have been
448 * deauth'd) and reset the bss node (clears key, rate
452 ieee80211_reset_bss(struct ieee80211vap *vap)
454 struct ieee80211com *ic = vap->iv_ic;
455 struct ieee80211_node *ni, *obss;
457 ieee80211_node_table_reset(&ic->ic_sta, vap);
458 /* XXX multi-bss: wrong */
459 ieee80211_vap_reset_erp(vap);
461 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
462 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
463 obss = vap->iv_update_bss(vap, ieee80211_ref_node(ni));
466 ni->ni_intval = ic->ic_bintval;
467 ieee80211_free_node(obss);
469 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
473 match_ssid(const struct ieee80211_node *ni,
474 int nssid, const struct ieee80211_scan_ssid ssids[])
478 for (i = 0; i < nssid; i++) {
479 if (ni->ni_esslen == ssids[i].len &&
480 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
487 * Test a node for suitability/compatibility.
490 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
492 struct ieee80211com *ic = ni->ni_ic;
495 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
497 if (vap->iv_opmode == IEEE80211_M_IBSS) {
498 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
501 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
504 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
505 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
508 /* XXX does this mean privacy is supported or required? */
509 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
512 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
513 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
514 if (rate & IEEE80211_RATE_BASIC)
516 if (vap->iv_des_nssid != 0 &&
517 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
519 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
520 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
525 #ifdef IEEE80211_DEBUG
527 * Display node suitability/compatibility.
530 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
532 struct ieee80211com *ic = ni->ni_ic;
537 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
539 if (vap->iv_opmode == IEEE80211_M_IBSS) {
540 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
543 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
546 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
547 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
550 /* XXX does this mean privacy is supported or required? */
551 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
554 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
555 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
556 if (rate & IEEE80211_RATE_BASIC)
558 if (vap->iv_des_nssid != 0 &&
559 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
561 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
562 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
565 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
566 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
568 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
569 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
570 fail & 0x08 ? '!' : ' ');
572 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
573 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
575 fail & 0x02 ? '!' : ' ');
577 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
578 fail & 0x04 ? '!' : ' ');
579 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
580 printf("%s\n", fail & 0x10 ? "!" : "");
582 #endif /* IEEE80211_DEBUG */
585 ieee80211_ibss_merge_check(struct ieee80211_node *ni)
587 struct ieee80211vap *vap = ni->ni_vap;
589 if (ni == vap->iv_bss ||
590 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
591 /* unchanged, nothing to do */
595 if (!check_bss(vap, ni)) {
596 /* capabilities mismatch */
597 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
598 "%s: merge failed, capabilities mismatch\n", __func__);
599 #ifdef IEEE80211_DEBUG
600 if (ieee80211_msg_assoc(vap))
601 check_bss_debug(vap, ni);
603 vap->iv_stats.is_ibss_capmismatch++;
611 * Check if the given node should populate the node table.
613 * We need to be in "see all beacons for all ssids" mode in order
614 * to do IBSS merges, however this means we will populate nodes for
615 * /all/ IBSS SSIDs, versus just the one we care about.
617 * So this check ensures the node can actually belong to our IBSS
618 * configuration. For now it simply checks the SSID.
621 ieee80211_ibss_node_check_new(struct ieee80211_node *ni,
622 const struct ieee80211_scanparams *scan)
624 struct ieee80211vap *vap = ni->ni_vap;
628 * If we have no SSID and no scan SSID, return OK.
630 if (vap->iv_des_nssid == 0 && scan->ssid == NULL)
634 * If we have one of (SSID, scan SSID) then return error.
636 if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL))
640 * Double-check - we need scan SSID.
642 if (scan->ssid == NULL)
646 * Check if the scan SSID matches the SSID list for the VAP.
648 for (i = 0; i < vap->iv_des_nssid; i++) {
649 /* Sanity length check */
650 if (vap->iv_des_ssid[i].len != scan->ssid[1])
653 /* Note: SSID in the scan entry is the IE format */
654 if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2,
655 vap->iv_des_ssid[i].len) == 0)
666 * Handle 802.11 ad hoc network merge. The
667 * convention, set by the Wireless Ethernet Compatibility Alliance
668 * (WECA), is that an 802.11 station will change its BSSID to match
669 * the "oldest" 802.11 ad hoc network, on the same channel, that
670 * has the station's desired SSID. The "oldest" 802.11 network
671 * sends beacons with the greatest TSF timestamp.
673 * The caller is assumed to validate TSF's before attempting a merge.
675 * Return !0 if the BSSID changed, 0 otherwise.
678 ieee80211_ibss_merge(struct ieee80211_node *ni)
680 #ifdef IEEE80211_DEBUG
681 struct ieee80211vap *vap = ni->ni_vap;
684 if (! ieee80211_ibss_merge_check(ni))
687 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
688 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
689 ether_sprintf(ni->ni_bssid),
690 vap->iv_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
691 vap->iv_flags&IEEE80211_F_SHSLOT ? "short" : "long",
692 vap->iv_flags&IEEE80211_F_USEPROT ? ", protection" : ""
694 return ieee80211_sta_join1(ieee80211_ref_node(ni));
698 * Calculate HT channel promotion flags for all vaps.
699 * This assumes ni_chan have been setup for each vap.
702 gethtadjustflags(struct ieee80211com *ic)
704 struct ieee80211vap *vap;
709 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
710 if (vap->iv_state < IEEE80211_S_RUN)
712 switch (vap->iv_opmode) {
713 case IEEE80211_M_WDS:
714 case IEEE80211_M_STA:
715 case IEEE80211_M_AHDEMO:
716 case IEEE80211_M_HOSTAP:
717 case IEEE80211_M_IBSS:
718 case IEEE80211_M_MBSS:
719 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
729 * Calculate VHT channel promotion flags for all vaps.
730 * This assumes ni_chan have been setup for each vap.
733 getvhtadjustflags(struct ieee80211com *ic)
735 struct ieee80211vap *vap;
740 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
741 if (vap->iv_state < IEEE80211_S_RUN)
743 switch (vap->iv_opmode) {
744 case IEEE80211_M_WDS:
745 case IEEE80211_M_STA:
746 case IEEE80211_M_AHDEMO:
747 case IEEE80211_M_HOSTAP:
748 case IEEE80211_M_IBSS:
749 case IEEE80211_M_MBSS:
750 flags |= ieee80211_vhtchanflags(vap->iv_bss->ni_chan);
760 * Check if the current channel needs to change based on whether
761 * any vap's are using HT20/HT40. This is used to sync the state
762 * of ic_curchan after a channel width change on a running vap.
764 * Same applies for VHT.
767 ieee80211_sync_curchan(struct ieee80211com *ic)
769 struct ieee80211_channel *c;
771 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
772 c = ieee80211_vht_adjust_channel(ic, c, getvhtadjustflags(ic));
774 if (c != ic->ic_curchan) {
776 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
777 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
778 IEEE80211_UNLOCK(ic);
779 ic->ic_set_channel(ic);
780 ieee80211_radiotap_chan_change(ic);
786 * Setup the current channel. The request channel may be
787 * promoted if other vap's are operating with HT20/HT40.
790 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
792 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
793 int flags = gethtadjustflags(ic);
795 * Check for channel promotion required to support the
796 * set of running vap's. This assumes we are called
797 * after ni_chan is setup for each vap.
800 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
801 if (flags > ieee80211_htchanflags(c))
802 c = ieee80211_ht_adjust_channel(ic, c, flags);
806 * VHT promotion - this will at least promote to VHT20/40
807 * based on what HT has done; it may further promote the
808 * channel to VHT80 or above.
810 if (ic->ic_vhtcaps != 0) {
811 int flags = getvhtadjustflags(ic);
812 if (flags > ieee80211_vhtchanflags(c))
813 c = ieee80211_vht_adjust_channel(ic, c, flags);
816 ic->ic_bsschan = ic->ic_curchan = c;
817 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
818 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
822 * Change the current channel. The channel change is guaranteed to have
823 * happened before the next state change.
826 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
828 ieee80211_setupcurchan(ic, c);
829 ieee80211_runtask(ic, &ic->ic_chan_task);
833 ieee80211_update_chw(struct ieee80211com *ic)
836 ieee80211_setupcurchan(ic, ic->ic_curchan);
837 ieee80211_runtask(ic, &ic->ic_chw_task);
841 * Join the specified IBSS/BSS network. The node is assumed to
842 * be passed in with a held reference.
845 ieee80211_sta_join1(struct ieee80211_node *selbs)
847 struct ieee80211vap *vap = selbs->ni_vap;
848 struct ieee80211com *ic = selbs->ni_ic;
849 struct ieee80211_node *obss;
853 * Committed to selbs, setup state.
855 obss = vap->iv_update_bss(vap, selbs); /* NB: caller assumed to bump refcnt */
857 * Check if old+new node have the same address in which
858 * case we can reassociate when operating in sta mode.
860 /* XXX We'll not be in RUN anymore as iv_state got updated already? */
861 canreassoc = (obss != NULL &&
862 vap->iv_state == IEEE80211_S_RUN &&
863 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
865 struct ieee80211_node_table *nt = obss->ni_table;
867 copy_bss(selbs, obss);
868 ieee80211_node_decref(obss); /* iv_bss reference */
870 IEEE80211_NODE_LOCK(nt);
871 node_reclaim(nt, obss); /* station table reference */
872 IEEE80211_NODE_UNLOCK(nt);
874 obss = NULL; /* NB: guard against later use */
878 * Delete unusable rates; we've already checked
879 * that the negotiated rate set is acceptable.
881 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
882 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
884 ieee80211_setcurchan(ic, selbs->ni_chan);
886 * Set the erp state (mostly the slot time) to deal with
887 * the auto-select case; this should be redundant if the
890 ieee80211_vap_reset_erp(vap);
891 ieee80211_wme_initparams(vap);
893 if (vap->iv_opmode == IEEE80211_M_STA) {
896 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
899 * Act as if we received a DEAUTH frame in case we
900 * are invoked from the RUN state. This will cause
901 * us to try to re-authenticate if we are operating
904 IEEE80211_DPRINTF(vap, IEEE80211_MSG_AUTH,
905 "%s %p<%s> %s -> AUTH, FC0_SUBTYPE_DEAUTH\n",
906 __func__, selbs, ether_sprintf(selbs->ni_macaddr),
907 ieee80211_state_name[vap->iv_state]);
908 ieee80211_new_state(vap, IEEE80211_S_AUTH,
909 IEEE80211_FC0_SUBTYPE_DEAUTH);
912 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
917 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
918 const struct ieee80211_scan_entry *se)
920 struct ieee80211com *ic = vap->iv_ic;
921 struct ieee80211_node *ni;
924 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
931 * Expand scan state into node's format.
932 * XXX may not need all this stuff
934 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
935 ni->ni_esslen = se->se_ssid[1];
936 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
937 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
938 ni->ni_intval = se->se_intval;
939 ni->ni_capinfo = se->se_capinfo;
941 ni->ni_timoff = se->se_timoff;
942 ni->ni_fhdwell = se->se_fhdwell;
943 ni->ni_fhindex = se->se_fhindex;
944 ni->ni_erp = se->se_erp;
945 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
946 ni->ni_noise = se->se_noise;
947 if (vap->iv_opmode == IEEE80211_M_STA) {
948 /* NB: only infrastructure mode requires an associd */
949 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
952 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
953 ieee80211_ies_expand(&ni->ni_ies);
954 #ifdef IEEE80211_SUPPORT_SUPERG
955 if (ni->ni_ies.ath_ie != NULL)
956 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
958 if (ni->ni_ies.htcap_ie != NULL)
959 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
960 if (ni->ni_ies.htinfo_ie != NULL)
961 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
962 #ifdef IEEE80211_SUPPORT_MESH
963 if (ni->ni_ies.meshid_ie != NULL)
964 ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
966 #ifdef IEEE80211_SUPPORT_TDMA
967 if (ni->ni_ies.tdma_ie != NULL)
968 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
970 if (ni->ni_ies.vhtcap_ie != NULL)
971 ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
972 if (ni->ni_ies.vhtopmode_ie != NULL)
973 ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
975 /* XXX parse BSSLOAD IE */
976 /* XXX parse TXPWRENV IE */
977 /* XXX parse APCHANREP IE */
980 vap->iv_dtim_period = se->se_dtimperiod;
981 vap->iv_dtim_count = 0;
983 /* NB: must be after ni_chan is setup */
984 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
986 if (ieee80211_iserp_rateset(&ni->ni_rates))
987 ni->ni_flags |= IEEE80211_NODE_ERP;
990 * Setup HT state for this node if it's available, otherwise
991 * non-STA modes won't pick this state up.
993 * For IBSS and related modes that don't go through an
994 * association request/response, the only appropriate place
995 * to setup the HT state is here.
997 if (ni->ni_ies.htinfo_ie != NULL &&
998 ni->ni_ies.htcap_ie != NULL &&
999 vap->iv_flags_ht & IEEE80211_FHT_HT) {
1000 ieee80211_ht_node_init(ni);
1001 ieee80211_ht_updateparams(ni,
1002 ni->ni_ies.htcap_ie,
1003 ni->ni_ies.htinfo_ie);
1008 * Setup VHT state for this node if it's available.
1009 * Same as the above.
1011 * For now, don't allow 2GHz VHT operation.
1013 if (ni->ni_ies.vhtopmode_ie != NULL &&
1014 ni->ni_ies.vhtcap_ie != NULL &&
1015 vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1016 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1017 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
1022 ieee80211_vht_node_init(ni);
1023 ieee80211_vht_updateparams(ni,
1024 ni->ni_ies.vhtcap_ie,
1025 ni->ni_ies.vhtopmode_ie);
1026 ieee80211_setup_vht_rates(ni, ni->ni_ies.vhtcap_ie,
1027 ni->ni_ies.vhtopmode_ie);
1032 /* Finally do the node channel change */
1034 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1035 ni->ni_ies.htinfo_ie);
1036 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
1037 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1038 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
1041 /* XXX else check for ath FF? */
1042 /* XXX QoS? Difficult given that WME config is specific to a master */
1044 ieee80211_node_setuptxparms(ni);
1045 ieee80211_ratectl_node_init(ni);
1047 return ieee80211_sta_join1(ieee80211_ref_node(ni));
1051 * Leave the specified IBSS/BSS network. The node is assumed to
1052 * be passed in with a held reference.
1055 ieee80211_sta_leave(struct ieee80211_node *ni)
1057 struct ieee80211com *ic = ni->ni_ic;
1059 ic->ic_node_cleanup(ni);
1060 ieee80211_notify_node_leave(ni);
1064 * Send a deauthenticate frame and drop the station.
1067 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
1069 /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
1070 ieee80211_ref_node(ni);
1071 if (ni->ni_associd != 0)
1072 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1073 ieee80211_node_leave(ni);
1074 ieee80211_free_node(ni);
1077 static struct ieee80211_node *
1078 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1080 struct ieee80211_node *ni;
1082 ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
1083 M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1088 node_init(struct ieee80211_node *ni)
1094 * Initialize an ie blob with the specified data. If previous
1095 * data exists re-use the data block. As a side effect we clear
1096 * all references to specific ie's; the caller is required to
1100 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
1102 /* NB: assumes data+len are the last fields */
1103 memset(ies, 0, offsetof(struct ieee80211_ies, data));
1104 if (ies->data != NULL && ies->len != len) {
1105 /* data size changed */
1106 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1109 if (ies->data == NULL) {
1110 ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
1111 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1112 if (ies->data == NULL) {
1114 /* NB: pointers have already been zero'd above */
1118 memcpy(ies->data, data, len);
1124 * Reclaim storage for an ie blob.
1127 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
1129 if (ies->data != NULL)
1130 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1134 * Expand an ie blob data contents and to fillin individual
1135 * ie pointers. The data blob is assumed to be well-formed;
1136 * we don't do any validity checking of ie lengths.
1139 ieee80211_ies_expand(struct ieee80211_ies *ies)
1147 /* Make sure the given IE length fits into the total length. */
1148 if ((2 + ie[1]) > ielen) {
1149 printf("%s: malformed IEs! ies %p { data %p len %d }: "
1150 "ie %u len 2+%u > total len left %d\n",
1151 __func__, ies, ies->data, ies->len,
1152 ie[0], ie[1], ielen);
1156 case IEEE80211_ELEMID_VENDOR:
1159 else if (iswmeoui(ie))
1161 #ifdef IEEE80211_SUPPORT_SUPERG
1162 else if (isatherosoui(ie))
1165 #ifdef IEEE80211_SUPPORT_TDMA
1166 else if (istdmaoui(ie))
1170 case IEEE80211_ELEMID_RSN:
1173 case IEEE80211_ELEMID_HTCAP:
1176 case IEEE80211_ELEMID_HTINFO:
1177 ies->htinfo_ie = ie;
1179 #ifdef IEEE80211_SUPPORT_MESH
1180 case IEEE80211_ELEMID_MESHID:
1181 ies->meshid_ie = ie;
1184 case IEEE80211_ELEMID_VHT_CAP:
1185 ies->vhtcap_ie = ie;
1187 case IEEE80211_ELEMID_VHT_OPMODE:
1188 ies->vhtopmode_ie = ie;
1190 case IEEE80211_ELEMID_VHT_PWR_ENV:
1191 ies->vhtpwrenv_ie = ie;
1193 case IEEE80211_ELEMID_BSSLOAD:
1194 ies->bssload_ie = ie;
1196 case IEEE80211_ELEMID_APCHANREP:
1197 ies->apchanrep_ie = ie;
1206 * Reclaim any resources in a node and reset any critical
1207 * state. Typically nodes are free'd immediately after,
1208 * but in some cases the storage may be reused so we need
1209 * to insure consistent state (should probably fix that).
1212 node_cleanup(struct ieee80211_node *ni)
1214 struct ieee80211vap *vap = ni->ni_vap;
1215 struct ieee80211com *ic = ni->ni_ic;
1218 /* NB: preserve ni_table */
1219 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1220 if (vap->iv_opmode != IEEE80211_M_STA)
1222 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
1223 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
1224 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
1227 * Cleanup any VHT and HT-related state.
1229 if (ni->ni_flags & IEEE80211_NODE_VHT)
1230 ieee80211_vht_node_cleanup(ni);
1231 if (ni->ni_flags & IEEE80211_NODE_HT)
1232 ieee80211_ht_node_cleanup(ni);
1233 #ifdef IEEE80211_SUPPORT_SUPERG
1234 /* Always do FF node cleanup; for A-MSDU */
1235 ieee80211_ff_node_cleanup(ni);
1237 #ifdef IEEE80211_SUPPORT_MESH
1239 * Cleanup any mesh-related state.
1241 if (vap->iv_opmode == IEEE80211_M_MBSS)
1242 ieee80211_mesh_node_cleanup(ni);
1245 * Clear any staging queue entries.
1247 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1250 * Clear AREF flag that marks the authorization refcnt bump
1251 * has happened. This is probably not needed as the node
1252 * should always be removed from the table so not found but
1253 * do it just in case.
1254 * Likewise clear the ASSOCID flag as these flags are intended
1255 * to be managed in tandem.
1257 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1260 * Drain power save queue and, if needed, clear TIM.
1262 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1263 vap->iv_set_tim(ni, 0);
1266 if (ni->ni_challenge != NULL) {
1267 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1268 ni->ni_challenge = NULL;
1271 * Preserve SSID, WPA, and WME ie's so the bss node is
1272 * reusable during a re-auth/re-assoc state transition.
1273 * If we remove these data they will not be recreated
1274 * because they come from a probe-response or beacon frame
1275 * which cannot be expected prior to the association-response.
1276 * This should not be an issue when operating in other modes
1277 * as stations leaving always go through a full state transition
1278 * which will rebuild this state.
1280 * XXX does this leave us open to inheriting old state?
1282 for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1283 if (ni->ni_rxfrag[i] != NULL) {
1284 m_freem(ni->ni_rxfrag[i]);
1285 ni->ni_rxfrag[i] = NULL;
1288 * Must be careful here to remove any key map entry w/o a LOR.
1290 ieee80211_node_delucastkey(ni);
1294 node_free(struct ieee80211_node *ni)
1296 struct ieee80211com *ic = ni->ni_ic;
1298 ieee80211_ratectl_node_deinit(ni);
1299 ic->ic_node_cleanup(ni);
1300 ieee80211_ies_cleanup(&ni->ni_ies);
1301 ieee80211_psq_cleanup(&ni->ni_psq);
1302 IEEE80211_FREE(ni, M_80211_NODE);
1306 node_age(struct ieee80211_node *ni)
1308 struct ieee80211vap *vap = ni->ni_vap;
1311 * Age frames on the power save queue.
1313 if (ieee80211_node_psq_age(ni) != 0 &&
1314 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1315 vap->iv_set_tim(ni, 0);
1317 * Age out HT resources (e.g. frames on the
1318 * A-MPDU reorder queues).
1320 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1321 ieee80211_ht_node_age(ni);
1325 node_getrssi(const struct ieee80211_node *ni)
1327 uint32_t avgrssi = ni->ni_avgrssi;
1330 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1332 rssi = IEEE80211_RSSI_GET(avgrssi);
1333 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1337 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1339 *rssi = node_getrssi(ni);
1340 *noise = ni->ni_noise;
1344 node_getmimoinfo(const struct ieee80211_node *ni,
1345 struct ieee80211_mimo_info *info)
1351 bzero(info, sizeof(*info));
1353 for (i = 0; i < MIN(IEEE80211_MAX_CHAINS, ni->ni_mimo_chains); i++) {
1354 /* Note: for now, just pri20 channel info */
1355 avgrssi = ni->ni_mimo_rssi_ctl[i];
1356 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1357 info->ch[i].rssi[0] = 0;
1359 rssi = IEEE80211_RSSI_GET(avgrssi);
1360 info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1362 info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i];
1365 /* XXX ext radios? */
1371 ieee80211_add_node_nt(struct ieee80211_node_table *nt,
1372 struct ieee80211_node *ni)
1374 struct ieee80211com *ic = nt->nt_ic;
1377 IEEE80211_NODE_LOCK_ASSERT(nt);
1379 hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr);
1380 (void) ic; /* XXX IEEE80211_NODE_HASH */
1381 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1382 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1388 ieee80211_del_node_nt(struct ieee80211_node_table *nt,
1389 struct ieee80211_node *ni)
1392 IEEE80211_NODE_LOCK_ASSERT(nt);
1394 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1395 LIST_REMOVE(ni, ni_hash);
1397 KASSERT(nt->nt_count >= 0,
1398 ("nt_count is negative (%d)!\n", nt->nt_count));
1399 ni->ni_table = NULL;
1402 static struct ieee80211_node *
1403 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1404 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1406 struct ieee80211com *ic = nt->nt_ic;
1407 struct ieee80211_node *ni;
1409 ni = ic->ic_node_alloc(vap, macaddr);
1411 vap->iv_stats.is_rx_nodealloc++;
1415 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1416 "%s %p<%s> in %s table\n", __func__, ni,
1417 ether_sprintf(macaddr), nt->nt_name);
1419 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1420 ieee80211_node_initref(ni); /* mark referenced */
1421 ni->ni_chan = IEEE80211_CHAN_ANYC;
1422 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1423 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1424 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1425 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1426 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1427 ni->ni_inact_reload = nt->nt_inact_init;
1428 ni->ni_inact = ni->ni_inact_reload;
1429 ni->ni_ath_defkeyix = 0x7fff;
1430 ieee80211_psq_init(&ni->ni_psq, "unknown");
1431 #ifdef IEEE80211_SUPPORT_MESH
1432 if (vap->iv_opmode == IEEE80211_M_MBSS)
1433 ieee80211_mesh_node_init(vap, ni);
1435 IEEE80211_NODE_LOCK(nt);
1436 ieee80211_add_node_nt(nt, ni);
1439 IEEE80211_NODE_UNLOCK(nt);
1441 /* handle failure; free node state */
1442 if (ic->ic_node_init(ni) != 0) {
1443 vap->iv_stats.is_rx_nodealloc++;
1444 ieee80211_psq_cleanup(&ni->ni_psq);
1445 ieee80211_ratectl_node_deinit(ni);
1446 __ieee80211_free_node(ni);
1450 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1451 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1457 * Craft a temporary node suitable for sending a management frame
1458 * to the specified station. We craft only as much state as we
1459 * need to do the work since the node will be immediately reclaimed
1460 * once the send completes.
1462 struct ieee80211_node *
1463 ieee80211_tmp_node(struct ieee80211vap *vap,
1464 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1466 struct ieee80211com *ic = vap->iv_ic;
1467 struct ieee80211_node *ni;
1469 ni = ic->ic_node_alloc(vap, macaddr);
1471 struct ieee80211_node *bss = vap->iv_bss;
1473 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1474 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1476 ni->ni_table = NULL; /* NB: pedantic */
1477 ni->ni_ic = ic; /* NB: needed to set channel */
1480 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1481 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1482 ieee80211_node_initref(ni); /* mark referenced */
1483 /* NB: required by ieee80211_fix_rate */
1484 ieee80211_node_set_chan(ni, bss->ni_chan);
1485 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1486 IEEE80211_KEYIX_NONE);
1487 ni->ni_txpower = bss->ni_txpower;
1488 /* XXX optimize away */
1489 ieee80211_psq_init(&ni->ni_psq, "unknown");
1491 ieee80211_ratectl_node_init(ni);
1493 /* handle failure; free node state */
1494 if (ic->ic_node_init(ni) != 0) {
1495 vap->iv_stats.is_rx_nodealloc++;
1496 ieee80211_psq_cleanup(&ni->ni_psq);
1497 ieee80211_ratectl_node_deinit(ni);
1498 __ieee80211_free_node(ni);
1504 vap->iv_stats.is_rx_nodealloc++;
1509 struct ieee80211_node *
1510 ieee80211_dup_bss(struct ieee80211vap *vap,
1511 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1513 struct ieee80211com *ic = vap->iv_ic;
1514 struct ieee80211_node *ni;
1516 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1518 struct ieee80211_node *bss = vap->iv_bss;
1520 * Inherit from iv_bss.
1523 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1524 ieee80211_node_set_chan(ni, bss->ni_chan);
1530 * Create a bss node for a legacy WDS vap. The far end does
1531 * not associate so we just create create a new node and
1532 * simulate an association. The caller is responsible for
1533 * installing the node as the bss node and handling any further
1534 * setup work like authorizing the port.
1536 struct ieee80211_node *
1537 ieee80211_node_create_wds(struct ieee80211vap *vap,
1538 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1540 struct ieee80211com *ic = vap->iv_ic;
1541 struct ieee80211_node *ni;
1543 /* XXX check if node already in sta table? */
1544 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1546 ni->ni_wdsvap = vap;
1547 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1549 * Inherit any manually configured settings.
1551 copy_bss(ni, vap->iv_bss);
1552 ieee80211_node_set_chan(ni, chan);
1553 /* NB: propagate ssid so available to WPA supplicant */
1554 ni->ni_esslen = vap->iv_des_ssid[0].len;
1555 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1556 /* NB: no associd for peer */
1558 * There are no management frames to use to
1559 * discover neighbor capabilities, so blindly
1560 * propagate the local configuration.
1562 if (vap->iv_flags & IEEE80211_F_WME)
1563 ni->ni_flags |= IEEE80211_NODE_QOS;
1564 #ifdef IEEE80211_SUPPORT_SUPERG
1565 if (vap->iv_flags & IEEE80211_F_FF)
1566 ni->ni_flags |= IEEE80211_NODE_FF;
1569 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1570 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1572 * Device is HT-capable and HT is enabled for
1573 * the vap; setup HT operation. On return
1574 * ni_chan will be adjusted to an HT channel.
1576 ieee80211_ht_wds_init(ni);
1577 if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1578 printf("%s: TODO: vht_wds_init\n", __func__);
1581 struct ieee80211_channel *c = ni->ni_chan;
1583 * Force a legacy channel to be used.
1585 c = ieee80211_find_channel(ic,
1586 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1587 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1588 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1595 struct ieee80211_node *
1596 _ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1597 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1598 const char *func __debrefcnt_used, int line __debrefcnt_used)
1600 struct ieee80211_node *ni;
1603 IEEE80211_NODE_LOCK_ASSERT(nt);
1605 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1606 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1607 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1608 ieee80211_ref_node(ni); /* mark referenced */
1609 #ifdef IEEE80211_DEBUG_REFCNT
1610 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1611 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1613 ni, ether_sprintf(ni->ni_macaddr),
1614 ieee80211_node_refcnt(ni));
1622 struct ieee80211_node *
1623 _ieee80211_find_node(struct ieee80211_node_table *nt,
1624 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1625 const char *func __debrefcnt_used, int line __debrefcnt_used)
1627 struct ieee80211_node *ni;
1629 IEEE80211_NODE_LOCK(nt);
1630 ni = _ieee80211_find_node_locked(nt, macaddr, func, line);
1631 IEEE80211_NODE_UNLOCK(nt);
1635 struct ieee80211_node *
1636 _ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1637 const struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN],
1638 const char *func __debrefcnt_used, int line __debrefcnt_used)
1640 struct ieee80211_node *ni;
1643 IEEE80211_NODE_LOCK_ASSERT(nt);
1645 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1646 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1647 if (ni->ni_vap == vap &&
1648 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1649 ieee80211_ref_node(ni); /* mark referenced */
1650 #ifdef IEEE80211_DEBUG_REFCNT
1651 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1652 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1654 ni, ether_sprintf(ni->ni_macaddr),
1655 ieee80211_node_refcnt(ni));
1663 struct ieee80211_node *
1664 _ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1665 const struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN],
1666 const char *func __debrefcnt_used, int line __debrefcnt_used)
1668 struct ieee80211_node *ni;
1670 IEEE80211_NODE_LOCK(nt);
1671 ni = _ieee80211_find_vap_node_locked(nt, vap, macaddr, func, line);
1672 IEEE80211_NODE_UNLOCK(nt);
1677 * Fake up a node; this handles node discovery in adhoc mode.
1678 * Note that for the driver's benefit we treat this like
1679 * an association so the driver has an opportunity to setup
1680 * it's private state.
1682 struct ieee80211_node *
1683 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1684 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1686 struct ieee80211_node *ni;
1688 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1689 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1690 ni = ieee80211_dup_bss(vap, macaddr);
1692 struct ieee80211com *ic = vap->iv_ic;
1694 /* XXX no rate negotiation; just dup */
1695 ni->ni_rates = vap->iv_bss->ni_rates;
1696 if (ieee80211_iserp_rateset(&ni->ni_rates))
1697 ni->ni_flags |= IEEE80211_NODE_ERP;
1698 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1700 * In adhoc demo mode there are no management
1701 * frames to use to discover neighbor capabilities,
1702 * so blindly propagate the local configuration
1703 * so we can do interesting things (e.g. use
1704 * WME to disable ACK's).
1709 if (vap->iv_flags & IEEE80211_F_WME)
1710 ni->ni_flags |= IEEE80211_NODE_QOS;
1711 #ifdef IEEE80211_SUPPORT_SUPERG
1712 if (vap->iv_flags & IEEE80211_F_FF)
1713 ni->ni_flags |= IEEE80211_NODE_FF;
1716 ieee80211_node_setuptxparms(ni);
1717 ieee80211_ratectl_node_init(ni);
1720 * XXX TODO: 11n? At least 20MHz, at least A-MPDU RX,
1721 * not A-MPDU TX; not 11n rates, etc. We'll cycle
1722 * that after we hear that we can indeed do 11n
1723 * (either by a beacon frame or by a probe response.)
1727 * This is the first time we see the node.
1729 if (ic->ic_newassoc != NULL)
1730 ic->ic_newassoc(ni, 1);
1733 * Kick off a probe request to the given node;
1734 * we will then use the probe response to update
1735 * 11n/etc configuration state.
1737 * XXX TODO: this isn't guaranteed, and until we get
1738 * a probe response, we won't be able to actually
1739 * do anything 802.11n related to the node.
1740 * So if this does indeed work, maybe we should hold
1741 * off on sending responses until we get the probe
1742 * response, or just default to some sensible subset
1743 * of 802.11n behaviour (eg always allow aggregation
1744 * negotiation TO us, but not FROM us, etc) so we
1745 * aren't entirely busted.
1747 if (vap->iv_opmode == IEEE80211_M_IBSS) {
1748 ieee80211_send_probereq(ni, /* node */
1749 vap->iv_myaddr, /* SA */
1750 ni->ni_macaddr, /* DA */
1751 vap->iv_bss->ni_bssid, /* BSSID */
1752 vap->iv_bss->ni_essid,
1753 vap->iv_bss->ni_esslen); /* SSID */
1756 /* XXX not right for 802.1x/WPA */
1757 ieee80211_node_authorize(ni);
1763 ieee80211_init_neighbor(struct ieee80211_node *ni,
1764 const struct ieee80211_frame *wh,
1765 const struct ieee80211_scanparams *sp)
1767 int do_ht_setup = 0, do_vht_setup = 0;
1769 ni->ni_esslen = sp->ssid[1];
1770 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1771 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1772 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1773 ni->ni_intval = sp->bintval;
1774 ni->ni_capinfo = sp->capinfo;
1775 ni->ni_chan = ni->ni_ic->ic_curchan;
1776 ni->ni_fhdwell = sp->fhdwell;
1777 ni->ni_fhindex = sp->fhindex;
1778 ni->ni_erp = sp->erp;
1779 ni->ni_timoff = sp->timoff;
1780 #ifdef IEEE80211_SUPPORT_MESH
1781 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1782 ieee80211_mesh_init_neighbor(ni, wh, sp);
1784 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1785 ieee80211_ies_expand(&ni->ni_ies);
1786 if (ni->ni_ies.wme_ie != NULL)
1787 ni->ni_flags |= IEEE80211_NODE_QOS;
1789 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1790 #ifdef IEEE80211_SUPPORT_SUPERG
1791 if (ni->ni_ies.ath_ie != NULL)
1792 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1794 if (ni->ni_ies.htcap_ie != NULL)
1795 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1796 if (ni->ni_ies.htinfo_ie != NULL)
1797 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1799 if (ni->ni_ies.vhtcap_ie != NULL)
1800 ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
1801 if (ni->ni_ies.vhtopmode_ie != NULL)
1802 ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
1804 if ((ni->ni_ies.htcap_ie != NULL) &&
1805 (ni->ni_ies.htinfo_ie != NULL) &&
1806 (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1810 if ((ni->ni_ies.vhtcap_ie != NULL) &&
1811 (ni->ni_ies.vhtopmode_ie != NULL) &&
1812 (ni->ni_vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
1817 /* NB: must be after ni_chan is setup */
1818 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1819 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1820 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1823 * If the neighbor is HT compatible, flip that on.
1826 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1827 "%s: doing HT setup\n", __func__);
1828 ieee80211_ht_node_init(ni);
1829 ieee80211_ht_updateparams(ni,
1830 ni->ni_ies.htcap_ie,
1831 ni->ni_ies.htinfo_ie);
1834 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1835 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
1840 ieee80211_vht_node_init(ni);
1841 ieee80211_vht_updateparams(ni,
1842 ni->ni_ies.vhtcap_ie,
1843 ni->ni_ies.vhtopmode_ie);
1844 ieee80211_setup_vht_rates(ni,
1845 ni->ni_ies.vhtcap_ie,
1846 ni->ni_ies.vhtopmode_ie);
1851 * Finally do the channel upgrade/change based
1852 * on the HT/VHT configuration.
1854 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1855 ni->ni_ies.htinfo_ie);
1856 ieee80211_setup_htrates(ni,
1857 ni->ni_ies.htcap_ie,
1858 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1859 ieee80211_setup_basic_htrates(ni,
1860 ni->ni_ies.htinfo_ie);
1862 ieee80211_node_setuptxparms(ni);
1863 ieee80211_ratectl_node_init(ni);
1865 /* Reassociate; we're now 11n/11ac */
1867 * XXX TODO: this is the wrong thing to do -
1868 * we're calling it with isnew=1 so the ath(4)
1869 * driver reinitialises the rate tables.
1870 * This "mostly" works for ath(4), but it won't
1871 * be right for firmware devices which allocate
1874 * So, do we just create a new node and delete
1877 if (ni->ni_ic->ic_newassoc)
1878 ni->ni_ic->ic_newassoc(ni, 1);
1883 * Do node discovery in adhoc mode on receipt of a beacon
1884 * or probe response frame. Note that for the driver's
1885 * benefit we treat this like an association so the
1886 * driver has an opportunity to setup it's private state.
1888 struct ieee80211_node *
1889 ieee80211_add_neighbor(struct ieee80211vap *vap,
1890 const struct ieee80211_frame *wh,
1891 const struct ieee80211_scanparams *sp)
1893 struct ieee80211_node *ni;
1895 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1896 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1897 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1899 struct ieee80211com *ic = vap->iv_ic;
1901 ieee80211_init_neighbor(ni, wh, sp);
1902 if (ieee80211_iserp_rateset(&ni->ni_rates))
1903 ni->ni_flags |= IEEE80211_NODE_ERP;
1904 ieee80211_node_setuptxparms(ni);
1905 ieee80211_ratectl_node_init(ni);
1906 if (ic->ic_newassoc != NULL)
1907 ic->ic_newassoc(ni, 1);
1908 /* XXX not right for 802.1x/WPA */
1909 ieee80211_node_authorize(ni);
1914 #define IS_PROBEREQ(wh) \
1915 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1916 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1917 #define IS_BCAST_PROBEREQ(wh) \
1918 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1919 ((const struct ieee80211_frame *)(wh))->i_addr3))
1921 static __inline struct ieee80211_node *
1922 _find_rxnode(struct ieee80211_node_table *nt,
1923 const struct ieee80211_frame_min *wh,
1924 const char *func __debrefcnt_used, int line __debrefcnt_used)
1926 if (IS_BCAST_PROBEREQ(wh))
1927 return NULL; /* spam bcast probe req to all vap's */
1928 return _ieee80211_find_node_locked(nt, wh->i_addr2, func, line);
1932 * Locate the node for sender, track state, and then pass the
1933 * (referenced) node up to the 802.11 layer for its use. Note
1934 * we can return NULL if the sender is not in the table.
1936 struct ieee80211_node *
1937 _ieee80211_find_rxnode(struct ieee80211com *ic,
1938 const struct ieee80211_frame_min *wh,
1939 const char *func __debrefcnt_used, int line __debrefcnt_used)
1941 struct ieee80211_node_table *nt;
1942 struct ieee80211_node *ni;
1945 IEEE80211_NODE_LOCK(nt);
1946 ni = _find_rxnode(nt, wh, func, line);
1947 IEEE80211_NODE_UNLOCK(nt);
1953 * Like ieee80211_find_rxnode but use the supplied h/w
1954 * key index as a hint to locate the node in the key
1955 * mapping table. If an entry is present at the key
1956 * index we return it; otherwise do a normal lookup and
1957 * update the mapping table if the station has a unicast
1958 * key assigned to it.
1960 struct ieee80211_node *
1961 _ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1962 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1963 const char *func __debrefcnt_used, int line __debrefcnt_used)
1965 struct ieee80211_node_table *nt;
1966 struct ieee80211_node *ni;
1969 IEEE80211_NODE_LOCK(nt);
1970 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1971 ni = nt->nt_keyixmap[keyix];
1975 ni = _find_rxnode(nt, wh, func, line);
1976 if (ni != NULL && nt->nt_keyixmap != NULL) {
1978 * If the station has a unicast key cache slot
1979 * assigned update the key->node mapping table.
1981 keyix = ni->ni_ucastkey.wk_rxkeyix;
1982 /* XXX can keyixmap[keyix] != NULL? */
1983 if (keyix < nt->nt_keyixmax &&
1984 nt->nt_keyixmap[keyix] == NULL) {
1985 IEEE80211_DPRINTF(ni->ni_vap,
1987 "%s: add key map entry %p<%s> refcnt %d\n",
1988 __func__, ni, ether_sprintf(ni->ni_macaddr),
1989 ieee80211_node_refcnt(ni)+1);
1990 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1994 if (IS_BCAST_PROBEREQ(wh))
1995 ni = NULL; /* spam bcast probe req to all vap's */
1997 ieee80211_ref_node(ni);
1999 IEEE80211_NODE_UNLOCK(nt);
2003 #undef IS_BCAST_PROBEREQ
2007 * Return a reference to the appropriate node for sending
2008 * a data frame. This handles node discovery in adhoc networks.
2010 struct ieee80211_node *
2011 _ieee80211_find_txnode(struct ieee80211vap *vap,
2012 const uint8_t macaddr[IEEE80211_ADDR_LEN],
2013 const char *func __debrefcnt_used, int line __debrefcnt_used)
2015 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
2016 struct ieee80211_node *ni;
2019 * The destination address should be in the node table
2020 * unless this is a multicast/broadcast frame. We can
2021 * also optimize station mode operation, all frames go
2024 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
2025 IEEE80211_NODE_LOCK(nt);
2026 if (vap->iv_opmode == IEEE80211_M_STA ||
2027 vap->iv_opmode == IEEE80211_M_WDS ||
2028 IEEE80211_IS_MULTICAST(macaddr))
2029 ni = ieee80211_ref_node(vap->iv_bss);
2031 ni = _ieee80211_find_node_locked(nt, macaddr, func, line);
2032 IEEE80211_NODE_UNLOCK(nt);
2035 if (vap->iv_opmode == IEEE80211_M_IBSS ||
2036 vap->iv_opmode == IEEE80211_M_AHDEMO) {
2038 * In adhoc mode cons up a node for the destination.
2039 * Note that we need an additional reference for the
2040 * caller to be consistent with
2041 * ieee80211_find_node_locked.
2044 * XXX TODO: this doesn't fake up 11n state; we need
2045 * to find another way to get it upgraded.
2047 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
2049 (void) ieee80211_ref_node(ni);
2051 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
2052 "no node, discard frame (%s)", __func__);
2053 vap->iv_stats.is_tx_nonode++;
2059 struct ieee80211_node *
2060 _ieee80211_ref_node(struct ieee80211_node *ni,
2061 const char *func __debrefcnt_used, int line __debrefcnt_used)
2064 #ifdef IEEE80211_DEBUG_REFCNT
2065 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2066 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
2067 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2069 ieee80211_node_incref(ni);
2074 __ieee80211_free_node(struct ieee80211_node *ni)
2076 struct ieee80211_node_table *nt = ni->ni_table;
2079 * NB: careful about referencing the vap as it may be
2080 * gone if the last reference was held by a driver.
2081 * We know the com will always be present so it's safe
2082 * to use ni_ic below to reclaim resources.
2085 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2086 "%s %p<%s> in %s table\n", __func__, ni,
2087 ether_sprintf(ni->ni_macaddr),
2088 nt != NULL ? nt->nt_name : "<gone>");
2090 if (ni->ni_associd != 0) {
2091 struct ieee80211vap *vap = ni->ni_vap;
2092 if (vap->iv_aid_bitmap != NULL)
2093 IEEE80211_AID_CLR(vap, ni->ni_associd);
2096 ieee80211_del_node_nt(nt, ni);
2097 ni->ni_ic->ic_node_free(ni);
2101 * Clear any entry in the unicast key mapping table.
2104 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2106 ieee80211_keyix keyix;
2108 keyix = ni->ni_ucastkey.wk_rxkeyix;
2109 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
2110 nt->nt_keyixmap[keyix] == ni) {
2111 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2112 "%s: %p<%s> clear key map entry %u\n",
2113 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
2114 nt->nt_keyixmap[keyix] = NULL;
2115 ieee80211_node_decref(ni);
2123 _ieee80211_free_node(struct ieee80211_node *ni,
2124 const char *func __debrefcnt_used, int line __debrefcnt_used)
2126 struct ieee80211_node_table *nt = ni->ni_table;
2128 #ifdef IEEE80211_DEBUG_REFCNT
2129 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2130 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
2131 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
2134 IEEE80211_NODE_LOCK(nt);
2135 if (ieee80211_node_dectestref(ni)) {
2137 * Last reference, reclaim state.
2139 __ieee80211_free_node(ni);
2140 } else if (ieee80211_node_refcnt(ni) == 1)
2141 if (node_clear_keyixmap(nt, ni))
2142 __ieee80211_free_node(ni);
2143 IEEE80211_NODE_UNLOCK(nt);
2145 if (ieee80211_node_dectestref(ni))
2146 __ieee80211_free_node(ni);
2151 * Reclaim a unicast key and clear any key cache state.
2154 ieee80211_node_delucastkey(struct ieee80211_node *ni)
2156 struct ieee80211com *ic = ni->ni_ic;
2157 struct ieee80211_node_table *nt = &ic->ic_sta;
2158 struct ieee80211_node *nikey;
2159 ieee80211_keyix keyix;
2160 int isowned, status;
2163 * NB: We must beware of LOR here; deleting the key
2164 * can cause the crypto layer to block traffic updates
2165 * which can generate a LOR against the node table lock;
2166 * grab it here and stash the key index for our use below.
2168 * Must also beware of recursion on the node table lock.
2169 * When called from node_cleanup we may already have
2170 * the node table lock held. Unfortunately there's no
2171 * way to separate out this path so we must do this
2174 isowned = IEEE80211_NODE_IS_LOCKED(nt);
2176 IEEE80211_NODE_LOCK(nt);
2178 status = 1; /* NB: success */
2179 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
2180 keyix = ni->ni_ucastkey.wk_rxkeyix;
2181 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
2182 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
2183 nikey = nt->nt_keyixmap[keyix];
2184 nt->nt_keyixmap[keyix] = NULL;
2188 IEEE80211_NODE_UNLOCK(nt);
2190 if (nikey != NULL) {
2191 KASSERT(nikey == ni,
2192 ("key map out of sync, ni %p nikey %p", ni, nikey));
2193 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2194 "%s: delete key map entry %p<%s> refcnt %d\n",
2195 __func__, ni, ether_sprintf(ni->ni_macaddr),
2196 ieee80211_node_refcnt(ni)-1);
2197 ieee80211_free_node(ni);
2203 * Reclaim a node. If this is the last reference count then
2204 * do the normal free work. Otherwise remove it from the node
2205 * table and mark it gone by clearing the back-reference.
2208 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2211 IEEE80211_NODE_LOCK_ASSERT(nt);
2213 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2214 "%s: remove %p<%s> from %s table, refcnt %d\n",
2215 __func__, ni, ether_sprintf(ni->ni_macaddr),
2216 nt->nt_name, ieee80211_node_refcnt(ni)-1);
2218 * Clear any entry in the unicast key mapping table.
2219 * We need to do it here so rx lookups don't find it
2220 * in the mapping table even if it's not in the hash
2221 * table. We cannot depend on the mapping table entry
2222 * being cleared because the node may not be free'd.
2224 (void)node_clear_keyixmap(nt, ni);
2225 if (!ieee80211_node_dectestref(ni)) {
2227 * Other references are present, just remove the
2228 * node from the table so it cannot be found. When
2229 * the references are dropped storage will be
2232 ieee80211_del_node_nt(nt, ni);
2234 __ieee80211_free_node(ni);
2238 * Node table support.
2242 ieee80211_node_table_init(struct ieee80211com *ic,
2243 struct ieee80211_node_table *nt,
2244 const char *name, int inact, int keyixmax)
2248 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
2249 TAILQ_INIT(&nt->nt_node);
2252 nt->nt_inact_init = inact;
2253 nt->nt_keyixmax = keyixmax;
2254 if (nt->nt_keyixmax > 0) {
2255 nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
2256 keyixmax * sizeof(struct ieee80211_node *),
2258 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2259 if (nt->nt_keyixmap == NULL)
2261 "Cannot allocate key index map with %u entries\n",
2264 nt->nt_keyixmap = NULL;
2268 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
2269 struct ieee80211vap *match)
2271 struct ieee80211_node *ni, *next;
2273 IEEE80211_NODE_LOCK(nt);
2274 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
2275 if (match != NULL && ni->ni_vap != match)
2277 /* XXX can this happen? if so need's work */
2278 if (ni->ni_associd != 0) {
2279 struct ieee80211vap *vap = ni->ni_vap;
2281 if (vap->iv_auth->ia_node_leave != NULL)
2282 vap->iv_auth->ia_node_leave(ni);
2283 if (vap->iv_aid_bitmap != NULL)
2284 IEEE80211_AID_CLR(vap, ni->ni_associd);
2286 ni->ni_wdsvap = NULL; /* clear reference */
2287 node_reclaim(nt, ni);
2289 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
2291 * Make a separate pass to clear references to this vap
2292 * held by DWDS entries. They will not be matched above
2293 * because ni_vap will point to the ap vap but we still
2294 * need to clear ni_wdsvap when the WDS vap is destroyed
2297 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
2298 if (ni->ni_wdsvap == match)
2299 ni->ni_wdsvap = NULL;
2301 IEEE80211_NODE_UNLOCK(nt);
2305 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2307 ieee80211_node_table_reset(nt, NULL);
2308 if (nt->nt_keyixmap != NULL) {
2310 /* XXX verify all entries are NULL */
2312 for (i = 0; i < nt->nt_keyixmax; i++)
2313 if (nt->nt_keyixmap[i] != NULL)
2314 printf("%s: %s[%u] still active\n", __func__,
2317 IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
2318 nt->nt_keyixmap = NULL;
2320 IEEE80211_NODE_LOCK_DESTROY(nt);
2324 timeout_stations(void *arg __unused, struct ieee80211_node *ni)
2326 struct ieee80211com *ic = ni->ni_ic;
2327 struct ieee80211vap *vap = ni->ni_vap;
2330 * Only process stations when in RUN state. This
2331 * insures, for example, that we don't timeout an
2332 * inactive station during CAC. Note that CSA state
2333 * is actually handled in ieee80211_node_timeout as
2334 * it applies to more than timeout processing.
2336 if (vap->iv_state != IEEE80211_S_RUN)
2339 * Ignore entries for which have yet to receive an
2340 * authentication frame. These are transient and
2341 * will be reclaimed when the last reference to them
2342 * goes away (when frame xmits complete).
2344 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2345 vap->iv_opmode == IEEE80211_M_STA) &&
2346 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2349 * Free fragment if not needed anymore
2350 * (last fragment older than 1s).
2351 * XXX doesn't belong here, move to node_age
2353 if (ni->ni_rxfrag[0] != NULL &&
2354 ticks > ni->ni_rxfragstamp + hz) {
2355 m_freem(ni->ni_rxfrag[0]);
2356 ni->ni_rxfrag[0] = NULL;
2358 if (ni->ni_inact > 0) {
2360 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2361 "%s: inact %u inact_reload %u nrates %u",
2362 __func__, ni->ni_inact, ni->ni_inact_reload,
2363 ni->ni_rates.rs_nrates);
2366 * Special case ourself; we may be idle for extended periods
2367 * of time and regardless reclaiming our state is wrong.
2368 * XXX run ic_node_age
2370 /* XXX before inact decrement? */
2371 if (ni == vap->iv_bss)
2373 if (ni->ni_associd != 0 ||
2374 (vap->iv_opmode == IEEE80211_M_IBSS ||
2375 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2377 * Age/drain resources held by the station.
2379 ic->ic_node_age(ni);
2381 * Probe the station before time it out. We
2382 * send a null data frame which may not be
2383 * universally supported by drivers (need it
2384 * for ps-poll support so it should be...).
2386 * XXX don't probe the station unless we've
2387 * received a frame from them (and have
2388 * some idea of the rates they are capable
2389 * of); this will get fixed more properly
2390 * soon with better handling of the rate set.
2392 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2393 (0 < ni->ni_inact &&
2394 ni->ni_inact <= vap->iv_inact_probe) &&
2395 ni->ni_rates.rs_nrates != 0) {
2397 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2399 "probe station due to inactivity");
2401 * Grab a reference so the node cannot
2402 * be reclaimed before we send the frame.
2403 * ieee80211_send_nulldata understands
2404 * we've done this and reclaims the
2405 * ref for us as needed.
2407 /* XXX fix this (not required anymore). */
2408 ieee80211_ref_node(ni);
2410 ieee80211_send_nulldata(ni);
2415 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2416 ni->ni_inact <= 0) {
2418 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2419 "station timed out due to inactivity "
2420 "(refcnt %u)", ieee80211_node_refcnt(ni));
2422 * Send a deauthenticate frame and drop the station.
2423 * This is somewhat complicated due to reference counts
2424 * and locking. At this point a station will typically
2425 * have a reference count of 2. ieee80211_node_leave
2426 * will do a "free" of the node which will drop the
2427 * reference count. But in the meantime a reference
2428 * wil be held by the deauth frame. The actual reclaim
2429 * of the node will happen either after the tx is
2430 * completed or by ieee80211_node_leave.
2432 if (ni->ni_associd != 0) {
2433 IEEE80211_SEND_MGMT(ni,
2434 IEEE80211_FC0_SUBTYPE_DEAUTH,
2435 IEEE80211_REASON_AUTH_EXPIRE);
2437 ieee80211_node_leave(ni);
2438 vap->iv_stats.is_node_timeout++;
2443 * Timeout inactive stations and do related housekeeping.
2446 ieee80211_timeout_stations(struct ieee80211com *ic)
2448 struct ieee80211_node_table *nt = &ic->ic_sta;
2450 ieee80211_iterate_nodes(nt, timeout_stations, NULL);
2454 * Aggressively reclaim resources. This should be used
2455 * only in a critical situation to reclaim mbuf resources.
2458 ieee80211_drain(struct ieee80211com *ic)
2460 struct ieee80211_node_table *nt = &ic->ic_sta;
2461 struct ieee80211vap *vap;
2462 struct ieee80211_node *ni;
2464 IEEE80211_NODE_LOCK(nt);
2465 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2467 * Ignore entries for which have yet to receive an
2468 * authentication frame. These are transient and
2469 * will be reclaimed when the last reference to them
2470 * goes away (when frame xmits complete).
2474 * Only process stations when in RUN state. This
2475 * insures, for example, that we don't timeout an
2476 * inactive station during CAC. Note that CSA state
2477 * is actually handled in ieee80211_node_timeout as
2478 * it applies to more than timeout processing.
2480 if (vap->iv_state != IEEE80211_S_RUN)
2482 /* XXX can vap be NULL? */
2483 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2484 vap->iv_opmode == IEEE80211_M_STA) &&
2485 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2489 * XXX doesn't belong here, move to node_drain
2491 if (ni->ni_rxfrag[0] != NULL) {
2492 m_freem(ni->ni_rxfrag[0]);
2493 ni->ni_rxfrag[0] = NULL;
2496 * Drain resources held by the station.
2498 ic->ic_node_drain(ni);
2500 IEEE80211_NODE_UNLOCK(nt);
2504 * Per-ieee80211vap inactivity timer callback.
2507 ieee80211_vap_timeout(struct ieee80211vap *vap)
2510 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2512 ieee80211_vap_erp_timeout(vap);
2513 ieee80211_ht_timeout(vap);
2514 ieee80211_vht_timeout(vap);
2518 * Per-ieee80211com inactivity timer callback.
2521 ieee80211_node_timeout(void *arg)
2523 struct ieee80211com *ic = arg;
2524 struct ieee80211vap *vap;
2527 * Defer timeout processing if a channel switch is pending.
2528 * We typically need to be mute so not doing things that
2529 * might generate frames is good to handle in one place.
2530 * Suppressing the station timeout processing may extend the
2531 * lifetime of inactive stations (by not decrementing their
2532 * idle counters) but this should be ok unless the CSA is
2533 * active for an unusually long time.
2535 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2536 ieee80211_scan_timeout(ic);
2537 ieee80211_timeout_stations(ic);
2538 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2541 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2542 ieee80211_vap_timeout(vap);
2543 IEEE80211_UNLOCK(ic);
2545 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2546 ieee80211_node_timeout, ic);
2550 * The same as ieee80211_iterate_nodes(), but for one vap only.
2553 ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
2554 struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
2556 struct ieee80211_node **ni_arr;
2557 struct ieee80211_node *ni;
2562 * Iterate over the node table and save an array of ref'ed nodes.
2564 * This is separated out from calling the actual node function so that
2565 * no LORs will occur.
2567 IEEE80211_NODE_LOCK(nt);
2568 count = nt->nt_count;
2569 size = count * sizeof(struct ieee80211_node *);
2570 ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
2571 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2572 if (ni_arr == NULL) {
2573 IEEE80211_NODE_UNLOCK(nt);
2578 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2579 if (vap != NULL && ni->ni_vap != vap)
2582 ("node array overflow (vap %p, i %d, count %d)\n",
2584 ni_arr[i] = ieee80211_ref_node(ni);
2587 IEEE80211_NODE_UNLOCK(nt);
2589 for (i = 0; i < count; i++) {
2590 if (ni_arr[i] == NULL) /* end of the list */
2592 (*f)(arg, ni_arr[i]);
2593 /* ieee80211_free_node() locks by itself */
2594 ieee80211_free_node(ni_arr[i]);
2597 IEEE80211_FREE(ni_arr, M_80211_NODE);
2603 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2604 * reference in the source.
2607 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2608 ieee80211_iter_func *f, void *arg)
2610 /* XXX no way to pass error to the caller. */
2611 (void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
2615 ieee80211_dump_node(struct ieee80211_node_table *nt __unused,
2616 struct ieee80211_node *ni)
2618 printf("%p: mac %s refcnt %d\n", ni,
2619 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2620 printf("\tauthmode %u flags 0x%x\n",
2621 ni->ni_authmode, ni->ni_flags);
2622 printf("\tassocid 0x%x txpower %u vlan %u\n",
2623 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2624 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2625 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2626 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2627 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2628 ni->ni_rxfragstamp);
2629 printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2630 node_getrssi(ni), ni->ni_noise,
2631 ni->ni_intval, ni->ni_capinfo);
2632 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2633 ether_sprintf(ni->ni_bssid),
2634 ni->ni_esslen, ni->ni_essid,
2635 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2636 printf("\tinact %u inact_reload %u txrate %u\n",
2637 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2638 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2639 ni->ni_htcap, ni->ni_htparam,
2640 ni->ni_htctlchan, ni->ni_ht2ndchan);
2641 printf("\thtopmode %x htstbc %x htchw %u\n",
2642 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2643 printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n",
2644 ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2,
2645 (int) ni->ni_vht_basicmcs);
2650 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2652 ieee80211_iterate_nodes(nt,
2653 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2657 * Iterate over the VAPs and update their ERP beacon IEs.
2659 * Note this must be called from the deferred ERP update task paths.
2662 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2664 struct ieee80211vap *vap;
2666 IEEE80211_LOCK_ASSERT(ic);
2668 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2669 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2670 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2674 * Handle a station joining an 11g network.
2677 ieee80211_node_join_11g(struct ieee80211_node *ni)
2679 struct ieee80211com *ic = ni->ni_ic;
2680 struct ieee80211vap *vap = ni->ni_vap;
2682 IEEE80211_LOCK_ASSERT(ic);
2685 * Station isn't capable of short slot time. Bump
2686 * the count of long slot time stations and disable
2687 * use of short slot time. Note that the actual switch
2688 * over to long slot time use may not occur until the
2689 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2691 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2692 vap->iv_longslotsta++;
2693 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2694 "station needs long slot time, count %d",
2695 vap->iv_longslotsta);
2697 * XXX TODO: this may need all VAPs checked!
2699 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2701 * Don't force slot time when switched to turbo
2702 * mode as non-ERP stations won't be present; this
2703 * need only be done when on the normal G channel.
2705 ieee80211_vap_set_shortslottime(vap, 0);
2709 * If the new station is not an ERP station
2710 * then bump the counter and enable protection
2713 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2714 vap->iv_nonerpsta++;
2715 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2716 "station is !ERP, %d non-ERP stations associated",
2719 * If station does not support short preamble
2720 * then we must enable use of Barker preamble.
2722 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2723 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2724 "%s", "station needs long preamble");
2725 vap->iv_flags |= IEEE80211_F_USEBARKER;
2726 vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE;
2727 ieee80211_vap_update_preamble(vap);
2730 * If protection is configured and this is the first
2731 * indication we should use protection, enable it.
2733 if (vap->iv_protmode != IEEE80211_PROT_NONE &&
2734 vap->iv_nonerpsta == 1 &&
2735 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2736 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2737 "%s: enable use of protection\n", __func__);
2738 vap->iv_flags |= IEEE80211_F_USEPROT;
2739 ieee80211_vap_update_erp_protmode(vap);
2742 ni->ni_flags |= IEEE80211_NODE_ERP;
2746 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2748 struct ieee80211com *ic = ni->ni_ic;
2749 struct ieee80211vap *vap = ni->ni_vap;
2752 if (ni->ni_associd == 0) {
2755 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2757 * It would be good to search the bitmap
2758 * more efficiently, but this will do for now.
2760 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2761 if (!IEEE80211_AID_ISSET(vap, aid))
2764 if (aid >= vap->iv_max_aid) {
2765 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2766 ieee80211_node_leave(ni);
2769 ni->ni_associd = aid | 0xc000;
2770 ni->ni_jointime = time_uptime;
2772 IEEE80211_AID_SET(vap, ni->ni_associd);
2773 vap->iv_sta_assoc++;
2775 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2776 ieee80211_ht_node_join(ni);
2777 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2778 ieee80211_vht_node_join(ni);
2779 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2780 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2781 ieee80211_node_join_11g(ni);
2782 IEEE80211_UNLOCK(ic);
2789 * XXX VHT - should log VHT channel width, etc
2791 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2792 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s%s",
2793 IEEE80211_NODE_AID(ni),
2794 vap->iv_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2795 vap->iv_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2796 vap->iv_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2797 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2798 /* XXX update for VHT string */
2799 ni->ni_flags & IEEE80211_NODE_HT ?
2800 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2801 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2802 ni->ni_flags & IEEE80211_NODE_AMSDU ? " (+AMSDU)" : "",
2803 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2804 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2805 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2806 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2807 ", fast-frames" : "",
2808 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2812 ieee80211_node_setuptxparms(ni);
2813 ieee80211_ratectl_node_init(ni);
2814 /* give driver a chance to setup state like ni_txrate */
2815 if (ic->ic_newassoc != NULL)
2816 ic->ic_newassoc(ni, newassoc);
2817 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2818 /* tell the authenticator about new station */
2819 if (vap->iv_auth->ia_node_join != NULL)
2820 vap->iv_auth->ia_node_join(ni);
2821 ieee80211_notify_node_join(ni,
2822 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2826 disable_protection(struct ieee80211vap *vap)
2828 struct ieee80211com *ic = vap->iv_ic;
2830 KASSERT(vap->iv_nonerpsta == 0 &&
2831 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2832 ("%d non ERP stations, flags 0x%x", vap->iv_nonerpsta,
2833 vap->iv_flags_ext));
2835 vap->iv_flags &= ~IEEE80211_F_USEPROT;
2836 /* XXX verify mode? */
2837 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2838 vap->iv_flags |= IEEE80211_F_SHPREAMBLE;
2839 vap->iv_flags &= ~IEEE80211_F_USEBARKER;
2841 ieee80211_vap_update_erp_protmode(vap);
2842 ieee80211_vap_update_preamble(vap);
2846 * Handle a station leaving an 11g network.
2849 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2851 struct ieee80211com *ic = ni->ni_ic;
2852 struct ieee80211vap *vap = ni->ni_vap;
2854 IEEE80211_LOCK_ASSERT(ic);
2856 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2857 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2858 ic->ic_bsschan->ic_flags));
2861 * If a long slot station do the slot time bookkeeping.
2863 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2864 KASSERT(vap->iv_longslotsta > 0,
2865 ("bogus long slot station count %d", vap->iv_longslotsta));
2866 vap->iv_longslotsta--;
2867 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2868 "long slot time station leaves, count now %d",
2869 vap->iv_longslotsta);
2871 * XXX TODO: this may need all VAPs checked!
2873 if (vap->iv_longslotsta == 0) {
2875 * Re-enable use of short slot time if supported
2876 * and not operating in IBSS mode (per spec).
2878 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2879 ic->ic_opmode != IEEE80211_M_IBSS) {
2880 IEEE80211_DPRINTF(ni->ni_vap,
2881 IEEE80211_MSG_ASSOC,
2882 "%s: re-enable use of short slot time\n",
2884 ieee80211_vap_set_shortslottime(vap, 1);
2889 * If a non-ERP station do the protection-related bookkeeping.
2891 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2892 KASSERT(vap->iv_nonerpsta > 0,
2893 ("bogus non-ERP station count %d", vap->iv_nonerpsta));
2894 vap->iv_nonerpsta--;
2895 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2896 "non-ERP station leaves, count now %d%s", vap->iv_nonerpsta,
2897 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2898 " (non-ERP sta present)" : "");
2899 if (vap->iv_nonerpsta == 0 &&
2900 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2901 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2902 "%s: disable use of protection\n", __func__);
2903 disable_protection(vap);
2909 * Time out presence of an overlapping bss with non-ERP
2910 * stations. When operating in hostap mode we listen for
2911 * beacons from other stations and if we identify a non-ERP
2912 * station is present we enable protection. To identify
2913 * when all non-ERP stations are gone we time out this
2917 ieee80211_vap_erp_timeout(struct ieee80211vap *vap)
2920 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2922 if ((vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2923 ieee80211_time_after(ticks, vap->iv_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2924 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
2925 "%s", "age out non-ERP sta present on channel");
2926 vap->iv_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2927 if (vap->iv_nonerpsta == 0)
2928 disable_protection(vap);
2933 * Handle bookkeeping for station deauthentication/disassociation
2934 * when operating as an ap.
2937 ieee80211_node_leave(struct ieee80211_node *ni)
2939 struct ieee80211com *ic = ni->ni_ic;
2940 struct ieee80211vap *vap = ni->ni_vap;
2941 struct ieee80211_node_table *nt = ni->ni_table;
2943 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2944 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2946 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2947 ("unexpected operating mode %u", vap->iv_opmode));
2949 * If node wasn't previously associated all
2950 * we need to do is reclaim the reference.
2952 /* XXX ibss mode bypasses 11g and notification */
2953 if (ni->ni_associd == 0)
2956 * Tell the authenticator the station is leaving.
2957 * Note that we must do this before yanking the
2958 * association id as the authenticator uses the
2959 * associd to locate it's state block.
2961 if (vap->iv_auth->ia_node_leave != NULL)
2962 vap->iv_auth->ia_node_leave(ni);
2965 IEEE80211_AID_CLR(vap, ni->ni_associd);
2966 vap->iv_sta_assoc--;
2968 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2969 ieee80211_vht_node_leave(ni);
2970 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2971 ieee80211_ht_node_leave(ni);
2972 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2973 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2974 ieee80211_node_leave_11g(ni);
2975 IEEE80211_UNLOCK(ic);
2977 * Cleanup station state. In particular clear various
2978 * state that might otherwise be reused if the node
2979 * is reused before the reference count goes to zero
2980 * (and memory is reclaimed).
2982 ieee80211_sta_leave(ni);
2985 * Remove the node from any table it's recorded in and
2986 * drop the caller's reference. Removal from the table
2987 * is important to insure the node is not reprocessed
2991 IEEE80211_NODE_LOCK(nt);
2992 node_reclaim(nt, ni);
2993 IEEE80211_NODE_UNLOCK(nt);
2995 ieee80211_free_node(ni);
3000 uint32_t rssi_total;
3004 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
3006 struct rssiinfo *info = arg;
3007 struct ieee80211vap *vap = ni->ni_vap;
3010 /* only associated stations */
3011 if (ni->ni_associd == 0)
3013 rssi = vap->iv_ic->ic_node_getrssi(ni);
3015 info->rssi_samples++;
3016 info->rssi_total += rssi;
3021 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
3023 struct rssiinfo *info = arg;
3024 struct ieee80211vap *vap = ni->ni_vap;
3027 /* only neighbors */
3028 /* XXX check bssid */
3029 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
3031 rssi = vap->iv_ic->ic_node_getrssi(ni);
3033 info->rssi_samples++;
3034 info->rssi_total += rssi;
3038 #ifdef IEEE80211_SUPPORT_MESH
3040 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
3042 struct rssiinfo *info = arg;
3043 struct ieee80211vap *vap = ni->ni_vap;
3046 /* only neighbors that peered successfully */
3047 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
3049 rssi = vap->iv_ic->ic_node_getrssi(ni);
3051 info->rssi_samples++;
3052 info->rssi_total += rssi;
3055 #endif /* IEEE80211_SUPPORT_MESH */
3058 ieee80211_getrssi(struct ieee80211vap *vap)
3060 #define NZ(x) ((x) == 0 ? 1 : (x))
3061 struct ieee80211com *ic = vap->iv_ic;
3062 struct rssiinfo info;
3064 info.rssi_total = 0;
3065 info.rssi_samples = 0;
3066 switch (vap->iv_opmode) {
3067 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
3068 case IEEE80211_M_AHDEMO: /* average of all neighbors */
3069 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
3072 case IEEE80211_M_HOSTAP: /* average of all associated stations */
3073 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
3076 #ifdef IEEE80211_SUPPORT_MESH
3077 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
3078 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
3082 case IEEE80211_M_MONITOR: /* XXX */
3083 case IEEE80211_M_STA: /* use stats from associated ap */
3085 if (vap->iv_bss != NULL)
3086 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
3087 info.rssi_samples = 1;
3090 return info.rssi_total / NZ(info.rssi_samples);
3095 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
3098 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
3100 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
3101 /* for non-station mode return avg'd rssi accounting */
3102 if (vap->iv_opmode != IEEE80211_M_STA)
3103 *rssi = ieee80211_getrssi(vap);