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>
30 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
40 #include <sys/socket.h>
43 #include <net/if_var.h>
44 #include <net/if_media.h>
45 #include <net/ethernet.h>
47 #include <net80211/ieee80211_var.h>
48 #include <net80211/ieee80211_input.h>
49 #ifdef IEEE80211_SUPPORT_SUPERG
50 #include <net80211/ieee80211_superg.h>
52 #ifdef IEEE80211_SUPPORT_TDMA
53 #include <net80211/ieee80211_tdma.h>
55 #include <net80211/ieee80211_wds.h>
56 #include <net80211/ieee80211_mesh.h>
57 #include <net80211/ieee80211_ratectl.h>
58 #include <net80211/ieee80211_vht.h>
62 #ifdef IEEE80211_DEBUG_REFCNT
63 #define __debrefcnt_used
65 #define __debrefcnt_used __unused
69 * IEEE80211_NODE_HASHSIZE must be a power of 2.
71 CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
74 * Association id's are managed with a bit vector.
76 #define IEEE80211_AID_SET(_vap, b) \
77 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
78 (1 << (IEEE80211_AID(b) % 32)))
79 #define IEEE80211_AID_CLR(_vap, b) \
80 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
81 ~(1 << (IEEE80211_AID(b) % 32)))
82 #define IEEE80211_AID_ISSET(_vap, b) \
83 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
85 static int ieee80211_sta_join1(struct ieee80211_node *);
87 static struct ieee80211_node *node_alloc(struct ieee80211vap *,
88 const uint8_t [IEEE80211_ADDR_LEN]);
89 static int node_init(struct ieee80211_node *);
90 static void node_cleanup(struct ieee80211_node *);
91 static void node_free(struct ieee80211_node *);
92 static void node_age(struct ieee80211_node *);
93 static int8_t node_getrssi(const struct ieee80211_node *);
94 static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
95 static void node_getmimoinfo(const struct ieee80211_node *,
96 struct ieee80211_mimo_info *);
98 static void __ieee80211_free_node(struct ieee80211_node *);
100 static void node_reclaim(struct ieee80211_node_table *nt,
101 struct ieee80211_node *ni);
102 static void ieee80211_node_table_init(struct ieee80211com *ic,
103 struct ieee80211_node_table *nt, const char *name,
104 int inact, int keymaxix);
105 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
106 struct ieee80211vap *);
107 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
108 static void ieee80211_vap_erp_timeout(struct ieee80211vap *);
110 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
111 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
114 ieee80211_node_attach(struct ieee80211com *ic)
116 /* XXX really want maxlen enforced per-sta */
117 ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
119 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
120 IEEE80211_INACT_INIT, ic->ic_max_keyix);
121 callout_init(&ic->ic_inact, 1);
122 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
123 ieee80211_node_timeout, ic);
125 ic->ic_node_alloc = node_alloc;
126 ic->ic_node_init = node_init;
127 ic->ic_node_free = node_free;
128 ic->ic_node_cleanup = node_cleanup;
129 ic->ic_node_age = node_age;
130 ic->ic_node_drain = node_age; /* NB: same as age */
131 ic->ic_node_getrssi = node_getrssi;
132 ic->ic_node_getsignal = node_getsignal;
133 ic->ic_node_getmimoinfo = node_getmimoinfo;
136 * Set flags to be propagated to all vap's;
137 * these define default behaviour/configuration.
139 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
143 ieee80211_node_detach(struct ieee80211com *ic)
146 callout_drain(&ic->ic_inact);
147 ieee80211_node_table_cleanup(&ic->ic_sta);
148 ieee80211_ageq_drain(&ic->ic_stageq);
149 ieee80211_ageq_cleanup(&ic->ic_stageq);
153 ieee80211_node_vattach(struct ieee80211vap *vap)
155 /* NB: driver can override */
156 vap->iv_max_aid = IEEE80211_AID_DEF;
158 /* default station inactivity timer settings */
159 vap->iv_inact_init = IEEE80211_INACT_INIT;
160 vap->iv_inact_auth = IEEE80211_INACT_AUTH;
161 vap->iv_inact_run = IEEE80211_INACT_RUN;
162 vap->iv_inact_probe = IEEE80211_INACT_PROBE;
164 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
165 "%s: init %u auth %u run %u probe %u\n", __func__,
166 vap->iv_inact_init, vap->iv_inact_auth,
167 vap->iv_inact_run, vap->iv_inact_probe);
171 ieee80211_node_latevattach(struct ieee80211vap *vap)
173 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
174 /* XXX should we allow max aid to be zero? */
175 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
176 vap->iv_max_aid = IEEE80211_AID_MIN;
177 if_printf(vap->iv_ifp,
178 "WARNING: max aid too small, changed to %d\n",
181 vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC(
182 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
184 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
185 if (vap->iv_aid_bitmap == NULL) {
186 /* XXX no way to recover */
187 printf("%s: no memory for AID bitmap, max aid %d!\n",
188 __func__, vap->iv_max_aid);
193 ieee80211_reset_bss(vap);
195 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
199 ieee80211_node_vdetach(struct ieee80211vap *vap)
201 struct ieee80211com *ic = vap->iv_ic;
203 ieee80211_node_table_reset(&ic->ic_sta, vap);
204 if (vap->iv_bss != NULL) {
205 ieee80211_free_node(vap->iv_bss);
206 vap->iv_update_bss(vap, NULL);
208 if (vap->iv_aid_bitmap != NULL) {
209 IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE);
210 vap->iv_aid_bitmap = NULL;
215 * Port authorize/unauthorize interfaces for use by an authenticator.
219 ieee80211_node_authorize(struct ieee80211_node *ni)
221 struct ieee80211vap *vap = ni->ni_vap;
223 ni->ni_flags |= IEEE80211_NODE_AUTH;
224 ni->ni_inact_reload = vap->iv_inact_run;
225 ni->ni_inact = ni->ni_inact_reload;
227 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
228 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
232 ieee80211_node_unauthorize(struct ieee80211_node *ni)
234 struct ieee80211vap *vap = ni->ni_vap;
236 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
237 ni->ni_inact_reload = vap->iv_inact_auth;
238 if (ni->ni_inact > ni->ni_inact_reload)
239 ni->ni_inact = ni->ni_inact_reload;
241 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
242 "%s: inact_reload %u inact %u", __func__,
243 ni->ni_inact_reload, ni->ni_inact);
247 * Fix tx parameters for a node according to ``association state''.
250 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
252 struct ieee80211vap *vap = ni->ni_vap;
253 enum ieee80211_phymode mode;
255 if (ni->ni_flags & IEEE80211_NODE_VHT) {
256 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
257 mode = IEEE80211_MODE_VHT_5GHZ;
259 mode = IEEE80211_MODE_VHT_2GHZ;
260 } else if (ni->ni_flags & IEEE80211_NODE_HT) {
261 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
262 mode = IEEE80211_MODE_11NA;
264 mode = IEEE80211_MODE_11NG;
265 } else { /* legacy rate handling */
266 if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
267 mode = IEEE80211_MODE_STURBO_A;
268 else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
269 mode = IEEE80211_MODE_HALF;
270 else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
271 mode = IEEE80211_MODE_QUARTER;
272 /* NB: 108A should be handled as 11a */
273 else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
274 mode = IEEE80211_MODE_11A;
275 else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
276 (ni->ni_flags & IEEE80211_NODE_ERP))
277 mode = IEEE80211_MODE_11G;
279 mode = IEEE80211_MODE_11B;
281 ni->ni_txparms = &vap->iv_txparms[mode];
285 * Set/change the channel. The rate set is also updated as
286 * to insure a consistent view by drivers.
287 * XXX should be private but hostap needs it to deal with CSA
290 ieee80211_node_set_chan(struct ieee80211_node *ni,
291 struct ieee80211_channel *chan)
293 struct ieee80211com *ic = ni->ni_ic;
294 struct ieee80211vap *vap = ni->ni_vap;
295 enum ieee80211_phymode mode;
297 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
300 mode = ieee80211_chan2mode(chan);
301 if (IEEE80211_IS_CHAN_HT(chan)) {
303 * We must install the legacy rate est in ni_rates and the
304 * HT rate set in ni_htrates.
306 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
308 * Setup bss tx parameters based on operating mode. We
309 * use legacy rates when operating in a mixed HT+non-HT bss
310 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
312 if (mode == IEEE80211_MODE_11NA &&
313 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
314 mode = IEEE80211_MODE_11A;
315 else if (mode == IEEE80211_MODE_11NG &&
316 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
317 mode = IEEE80211_MODE_11G;
318 if (mode == IEEE80211_MODE_11G &&
319 (vap->iv_flags & IEEE80211_F_PUREG) == 0)
320 mode = IEEE80211_MODE_11B;
322 ni->ni_txparms = &vap->iv_txparms[mode];
323 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
327 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
329 /* propagate useful state */
330 nbss->ni_authmode = obss->ni_authmode;
331 nbss->ni_txpower = obss->ni_txpower;
332 nbss->ni_vlan = obss->ni_vlan;
333 /* XXX statistics? */
334 /* XXX legacy WDS bssid? */
338 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
340 struct ieee80211com *ic = vap->iv_ic;
341 struct ieee80211_node *ni;
343 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
344 "%s: creating %s on channel %u%c flags 0x%08x\n", __func__,
345 ieee80211_opmode_name[vap->iv_opmode],
346 ieee80211_chan2ieee(ic, chan),
347 ieee80211_channel_type_char(chan),
350 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
355 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
356 ni->ni_esslen = vap->iv_des_ssid[0].len;
357 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
358 if (vap->iv_bss != NULL)
359 copy_bss(ni, vap->iv_bss);
360 ni->ni_intval = ic->ic_bintval;
361 if (vap->iv_flags & IEEE80211_F_PRIVACY)
362 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
363 if (ic->ic_phytype == IEEE80211_T_FH) {
364 ni->ni_fhdwell = 200; /* XXX */
367 if (vap->iv_opmode == IEEE80211_M_IBSS) {
368 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
369 if (vap->iv_flags & IEEE80211_F_DESBSSID)
370 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
372 net80211_get_random_bytes(ni->ni_bssid,
374 /* clear group bit, add local bit */
375 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
377 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
378 if (vap->iv_flags & IEEE80211_F_DESBSSID)
379 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
381 #ifdef IEEE80211_SUPPORT_TDMA
382 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
384 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
385 #ifdef IEEE80211_SUPPORT_MESH
386 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
387 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
388 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
392 * Fix the channel and related attributes.
394 /* clear DFS CAC state on previous channel */
395 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
396 ic->ic_bsschan->ic_freq != chan->ic_freq &&
397 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
398 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
399 ic->ic_bsschan = chan;
400 ieee80211_node_set_chan(ni, chan);
401 ic->ic_curmode = ieee80211_chan2mode(chan);
403 * Do mode-specific setup.
405 if (IEEE80211_IS_CHAN_FULL(chan)) {
406 if (IEEE80211_IS_CHAN_ANYG(chan)) {
408 * Use a mixed 11b/11g basic rate set.
410 ieee80211_setbasicrates(&ni->ni_rates,
412 if (vap->iv_flags & IEEE80211_F_PUREG) {
414 * Also mark OFDM rates basic so 11b
415 * stations do not join (WiFi compliance).
417 ieee80211_addbasicrates(&ni->ni_rates,
420 } else if (IEEE80211_IS_CHAN_B(chan)) {
422 * Force pure 11b rate set.
424 ieee80211_setbasicrates(&ni->ni_rates,
429 /* XXX TODO: other bits and pieces - eg fast-frames? */
431 /* If we're an 11n channel then initialise the 11n bits */
432 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
434 ieee80211_ht_node_init(ni);
435 ieee80211_vht_node_init(ni);
436 } else if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
438 ieee80211_ht_node_init(ni);
441 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
445 * Reset bss state on transition to the INIT state.
446 * Clear any stations from the table (they have been
447 * deauth'd) and reset the bss node (clears key, rate
451 ieee80211_reset_bss(struct ieee80211vap *vap)
453 struct ieee80211com *ic = vap->iv_ic;
454 struct ieee80211_node *ni, *obss;
456 ieee80211_node_table_reset(&ic->ic_sta, vap);
457 /* XXX multi-bss: wrong */
458 ieee80211_vap_reset_erp(vap);
460 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
461 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
462 obss = vap->iv_update_bss(vap, ieee80211_ref_node(ni));
465 ni->ni_intval = ic->ic_bintval;
466 ieee80211_free_node(obss);
468 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
472 match_ssid(const struct ieee80211_node *ni,
473 int nssid, const struct ieee80211_scan_ssid ssids[])
477 for (i = 0; i < nssid; i++) {
478 if (ni->ni_esslen == ssids[i].len &&
479 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
486 * Test a node for suitability/compatibility.
489 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
491 struct ieee80211com *ic = ni->ni_ic;
494 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
496 if (vap->iv_opmode == IEEE80211_M_IBSS) {
497 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
500 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
503 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
504 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
507 /* XXX does this mean privacy is supported or required? */
508 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
511 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
512 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
513 if (rate & IEEE80211_RATE_BASIC)
515 if (vap->iv_des_nssid != 0 &&
516 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
518 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
519 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
524 #ifdef IEEE80211_DEBUG
526 * Display node suitability/compatibility.
529 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
531 struct ieee80211com *ic = ni->ni_ic;
536 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
538 if (vap->iv_opmode == IEEE80211_M_IBSS) {
539 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
542 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
545 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
546 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
549 /* XXX does this mean privacy is supported or required? */
550 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
553 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
554 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
555 if (rate & IEEE80211_RATE_BASIC)
557 if (vap->iv_des_nssid != 0 &&
558 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
560 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
561 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
564 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
565 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
567 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
568 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
569 fail & 0x08 ? '!' : ' ');
571 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
572 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
574 fail & 0x02 ? '!' : ' ');
576 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
577 fail & 0x04 ? '!' : ' ');
578 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
579 printf("%s\n", fail & 0x10 ? "!" : "");
581 #endif /* IEEE80211_DEBUG */
584 ieee80211_ibss_merge_check(struct ieee80211_node *ni)
586 struct ieee80211vap *vap = ni->ni_vap;
588 if (ni == vap->iv_bss ||
589 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
590 /* unchanged, nothing to do */
594 if (!check_bss(vap, ni)) {
595 /* capabilities mismatch */
596 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
597 "%s: merge failed, capabilities mismatch\n", __func__);
598 #ifdef IEEE80211_DEBUG
599 if (ieee80211_msg_assoc(vap))
600 check_bss_debug(vap, ni);
602 vap->iv_stats.is_ibss_capmismatch++;
610 * Check if the given node should populate the node table.
612 * We need to be in "see all beacons for all ssids" mode in order
613 * to do IBSS merges, however this means we will populate nodes for
614 * /all/ IBSS SSIDs, versus just the one we care about.
616 * So this check ensures the node can actually belong to our IBSS
617 * configuration. For now it simply checks the SSID.
620 ieee80211_ibss_node_check_new(struct ieee80211_node *ni,
621 const struct ieee80211_scanparams *scan)
623 struct ieee80211vap *vap = ni->ni_vap;
627 * If we have no SSID and no scan SSID, return OK.
629 if (vap->iv_des_nssid == 0 && scan->ssid == NULL)
633 * If we have one of (SSID, scan SSID) then return error.
635 if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL))
639 * Double-check - we need scan SSID.
641 if (scan->ssid == NULL)
645 * Check if the scan SSID matches the SSID list for the VAP.
647 for (i = 0; i < vap->iv_des_nssid; i++) {
648 /* Sanity length check */
649 if (vap->iv_des_ssid[i].len != scan->ssid[1])
652 /* Note: SSID in the scan entry is the IE format */
653 if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2,
654 vap->iv_des_ssid[i].len) == 0)
665 * Handle 802.11 ad hoc network merge. The
666 * convention, set by the Wireless Ethernet Compatibility Alliance
667 * (WECA), is that an 802.11 station will change its BSSID to match
668 * the "oldest" 802.11 ad hoc network, on the same channel, that
669 * has the station's desired SSID. The "oldest" 802.11 network
670 * sends beacons with the greatest TSF timestamp.
672 * The caller is assumed to validate TSF's before attempting a merge.
674 * Return !0 if the BSSID changed, 0 otherwise.
677 ieee80211_ibss_merge(struct ieee80211_node *ni)
679 #ifdef IEEE80211_DEBUG
680 struct ieee80211vap *vap = ni->ni_vap;
683 if (! ieee80211_ibss_merge_check(ni))
686 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
687 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
688 ether_sprintf(ni->ni_bssid),
689 vap->iv_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
690 vap->iv_flags&IEEE80211_F_SHSLOT ? "short" : "long",
691 vap->iv_flags&IEEE80211_F_USEPROT ? ", protection" : ""
693 return ieee80211_sta_join1(ieee80211_ref_node(ni));
697 * Calculate HT channel promotion flags for all vaps.
698 * This assumes ni_chan have been setup for each vap.
701 gethtadjustflags(struct ieee80211com *ic)
703 struct ieee80211vap *vap;
708 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
709 if (vap->iv_state < IEEE80211_S_RUN)
711 switch (vap->iv_opmode) {
712 case IEEE80211_M_WDS:
713 case IEEE80211_M_STA:
714 case IEEE80211_M_AHDEMO:
715 case IEEE80211_M_HOSTAP:
716 case IEEE80211_M_IBSS:
717 case IEEE80211_M_MBSS:
718 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
728 * Calculate VHT channel promotion flags for all vaps.
729 * This assumes ni_chan have been setup for each vap.
732 getvhtadjustflags(struct ieee80211com *ic)
734 struct ieee80211vap *vap;
739 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
740 if (vap->iv_state < IEEE80211_S_RUN)
742 switch (vap->iv_opmode) {
743 case IEEE80211_M_WDS:
744 case IEEE80211_M_STA:
745 case IEEE80211_M_AHDEMO:
746 case IEEE80211_M_HOSTAP:
747 case IEEE80211_M_IBSS:
748 case IEEE80211_M_MBSS:
749 flags |= ieee80211_vhtchanflags(vap->iv_bss->ni_chan);
759 * Check if the current channel needs to change based on whether
760 * any vap's are using HT20/HT40. This is used to sync the state
761 * of ic_curchan after a channel width change on a running vap.
763 * Same applies for VHT.
766 ieee80211_sync_curchan(struct ieee80211com *ic)
768 struct ieee80211_channel *c;
770 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
771 c = ieee80211_vht_adjust_channel(ic, c, getvhtadjustflags(ic));
773 if (c != ic->ic_curchan) {
775 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
776 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
777 IEEE80211_UNLOCK(ic);
778 ic->ic_set_channel(ic);
779 ieee80211_radiotap_chan_change(ic);
785 * Setup the current channel. The request channel may be
786 * promoted if other vap's are operating with HT20/HT40.
789 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
791 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
792 int flags = gethtadjustflags(ic);
794 * Check for channel promotion required to support the
795 * set of running vap's. This assumes we are called
796 * after ni_chan is setup for each vap.
799 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
800 if (flags > ieee80211_htchanflags(c))
801 c = ieee80211_ht_adjust_channel(ic, c, flags);
805 * VHT promotion - this will at least promote to VHT20/40
806 * based on what HT has done; it may further promote the
807 * channel to VHT80 or above.
809 if (ic->ic_vhtcaps != 0) {
810 int flags = getvhtadjustflags(ic);
811 if (flags > ieee80211_vhtchanflags(c))
812 c = ieee80211_vht_adjust_channel(ic, c, flags);
815 ic->ic_bsschan = ic->ic_curchan = c;
816 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
817 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
821 * Change the current channel. The channel change is guaranteed to have
822 * happened before the next state change.
825 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
827 ieee80211_setupcurchan(ic, c);
828 ieee80211_runtask(ic, &ic->ic_chan_task);
832 ieee80211_update_chw(struct ieee80211com *ic)
835 ieee80211_setupcurchan(ic, ic->ic_curchan);
836 ieee80211_runtask(ic, &ic->ic_chw_task);
840 * Join the specified IBSS/BSS network. The node is assumed to
841 * be passed in with a held reference.
844 ieee80211_sta_join1(struct ieee80211_node *selbs)
846 struct ieee80211vap *vap = selbs->ni_vap;
847 struct ieee80211com *ic = selbs->ni_ic;
848 struct ieee80211_node *obss;
852 * Committed to selbs, setup state.
854 obss = vap->iv_update_bss(vap, selbs); /* NB: caller assumed to bump refcnt */
856 * Check if old+new node have the same address in which
857 * case we can reassociate when operating in sta mode.
859 /* XXX We'll not be in RUN anymore as iv_state got updated already? */
860 canreassoc = (obss != NULL &&
861 vap->iv_state == IEEE80211_S_RUN &&
862 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
864 struct ieee80211_node_table *nt = obss->ni_table;
866 copy_bss(selbs, obss);
867 ieee80211_node_decref(obss); /* iv_bss reference */
869 IEEE80211_NODE_LOCK(nt);
870 node_reclaim(nt, obss); /* station table reference */
871 IEEE80211_NODE_UNLOCK(nt);
873 obss = NULL; /* NB: guard against later use */
877 * Delete unusable rates; we've already checked
878 * that the negotiated rate set is acceptable.
880 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
881 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
883 ieee80211_setcurchan(ic, selbs->ni_chan);
885 * Set the erp state (mostly the slot time) to deal with
886 * the auto-select case; this should be redundant if the
889 ieee80211_vap_reset_erp(vap);
890 ieee80211_wme_initparams(vap);
892 if (vap->iv_opmode == IEEE80211_M_STA) {
895 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
898 * Act as if we received a DEAUTH frame in case we
899 * are invoked from the RUN state. This will cause
900 * us to try to re-authenticate if we are operating
903 IEEE80211_DPRINTF(vap, IEEE80211_MSG_AUTH,
904 "%s %p<%s> %s -> AUTH, FC0_SUBTYPE_DEAUTH\n",
905 __func__, selbs, ether_sprintf(selbs->ni_macaddr),
906 ieee80211_state_name[vap->iv_state]);
907 ieee80211_new_state(vap, IEEE80211_S_AUTH,
908 IEEE80211_FC0_SUBTYPE_DEAUTH);
911 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
916 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
917 const struct ieee80211_scan_entry *se)
919 struct ieee80211com *ic = vap->iv_ic;
920 struct ieee80211_node *ni;
923 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
930 * Expand scan state into node's format.
931 * XXX may not need all this stuff
933 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
934 ni->ni_esslen = se->se_ssid[1];
935 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
936 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
937 ni->ni_intval = se->se_intval;
938 ni->ni_capinfo = se->se_capinfo;
940 ni->ni_timoff = se->se_timoff;
941 ni->ni_fhdwell = se->se_fhdwell;
942 ni->ni_fhindex = se->se_fhindex;
943 ni->ni_erp = se->se_erp;
944 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
945 ni->ni_noise = se->se_noise;
946 if (vap->iv_opmode == IEEE80211_M_STA) {
947 /* NB: only infrastructure mode requires an associd */
948 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
951 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
952 ieee80211_ies_expand(&ni->ni_ies);
953 #ifdef IEEE80211_SUPPORT_SUPERG
954 if (ni->ni_ies.ath_ie != NULL)
955 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
957 if (ni->ni_ies.htcap_ie != NULL)
958 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
959 if (ni->ni_ies.htinfo_ie != NULL)
960 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
961 #ifdef IEEE80211_SUPPORT_MESH
962 if (ni->ni_ies.meshid_ie != NULL)
963 ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
965 #ifdef IEEE80211_SUPPORT_TDMA
966 if (ni->ni_ies.tdma_ie != NULL)
967 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
969 if (ni->ni_ies.vhtcap_ie != NULL)
970 ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
971 if (ni->ni_ies.vhtopmode_ie != NULL)
972 ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
974 /* XXX parse BSSLOAD IE */
975 /* XXX parse TXPWRENV IE */
976 /* XXX parse APCHANREP IE */
979 vap->iv_dtim_period = se->se_dtimperiod;
980 vap->iv_dtim_count = 0;
982 /* NB: must be after ni_chan is setup */
983 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
985 if (ieee80211_iserp_rateset(&ni->ni_rates))
986 ni->ni_flags |= IEEE80211_NODE_ERP;
989 * Setup HT state for this node if it's available, otherwise
990 * non-STA modes won't pick this state up.
992 * For IBSS and related modes that don't go through an
993 * association request/response, the only appropriate place
994 * to setup the HT state is here.
996 if (ni->ni_ies.htinfo_ie != NULL &&
997 ni->ni_ies.htcap_ie != NULL &&
998 vap->iv_flags_ht & IEEE80211_FHT_HT) {
999 ieee80211_ht_node_init(ni);
1000 ieee80211_ht_updateparams(ni,
1001 ni->ni_ies.htcap_ie,
1002 ni->ni_ies.htinfo_ie);
1007 * Setup VHT state for this node if it's available.
1008 * Same as the above.
1010 * For now, don't allow 2GHz VHT operation.
1012 if (ni->ni_ies.vhtopmode_ie != NULL &&
1013 ni->ni_ies.vhtcap_ie != NULL &&
1014 vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1015 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1016 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
1021 ieee80211_vht_node_init(ni);
1022 ieee80211_vht_updateparams(ni,
1023 ni->ni_ies.vhtcap_ie,
1024 ni->ni_ies.vhtopmode_ie);
1025 ieee80211_setup_vht_rates(ni, ni->ni_ies.vhtcap_ie,
1026 ni->ni_ies.vhtopmode_ie);
1031 /* Finally do the node channel change */
1033 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1034 ni->ni_ies.htinfo_ie);
1035 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
1036 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1037 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
1040 /* XXX else check for ath FF? */
1041 /* XXX QoS? Difficult given that WME config is specific to a master */
1043 ieee80211_node_setuptxparms(ni);
1044 ieee80211_ratectl_node_init(ni);
1046 return ieee80211_sta_join1(ieee80211_ref_node(ni));
1050 * Leave the specified IBSS/BSS network. The node is assumed to
1051 * be passed in with a held reference.
1054 ieee80211_sta_leave(struct ieee80211_node *ni)
1056 struct ieee80211com *ic = ni->ni_ic;
1058 ic->ic_node_cleanup(ni);
1059 ieee80211_notify_node_leave(ni);
1063 * Send a deauthenticate frame and drop the station.
1066 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
1068 /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
1069 ieee80211_ref_node(ni);
1070 if (ni->ni_associd != 0)
1071 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1072 ieee80211_node_leave(ni);
1073 ieee80211_free_node(ni);
1076 static struct ieee80211_node *
1077 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1079 struct ieee80211_node *ni;
1081 ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
1082 M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1087 node_init(struct ieee80211_node *ni)
1093 * Initialize an ie blob with the specified data. If previous
1094 * data exists re-use the data block. As a side effect we clear
1095 * all references to specific ie's; the caller is required to
1099 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
1101 /* NB: assumes data+len are the last fields */
1102 memset(ies, 0, offsetof(struct ieee80211_ies, data));
1103 if (ies->data != NULL && ies->len != len) {
1104 /* data size changed */
1105 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1108 if (ies->data == NULL) {
1109 ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
1110 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1111 if (ies->data == NULL) {
1113 /* NB: pointers have already been zero'd above */
1117 memcpy(ies->data, data, len);
1123 * Reclaim storage for an ie blob.
1126 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
1128 if (ies->data != NULL)
1129 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1133 * Expand an ie blob data contents and to fillin individual
1134 * ie pointers. The data blob is assumed to be well-formed;
1135 * we don't do any validity checking of ie lengths.
1138 ieee80211_ies_expand(struct ieee80211_ies *ies)
1146 /* Make sure the given IE length fits into the total length. */
1147 if ((2 + ie[1]) > ielen) {
1148 printf("%s: malformed IEs! ies %p { data %p len %d }: "
1149 "ie %u len 2+%u > total len left %d\n",
1150 __func__, ies, ies->data, ies->len,
1151 ie[0], ie[1], ielen);
1155 case IEEE80211_ELEMID_VENDOR:
1158 else if (iswmeoui(ie))
1160 #ifdef IEEE80211_SUPPORT_SUPERG
1161 else if (isatherosoui(ie))
1164 #ifdef IEEE80211_SUPPORT_TDMA
1165 else if (istdmaoui(ie))
1169 case IEEE80211_ELEMID_RSN:
1172 case IEEE80211_ELEMID_HTCAP:
1175 case IEEE80211_ELEMID_HTINFO:
1176 ies->htinfo_ie = ie;
1178 #ifdef IEEE80211_SUPPORT_MESH
1179 case IEEE80211_ELEMID_MESHID:
1180 ies->meshid_ie = ie;
1183 case IEEE80211_ELEMID_VHT_CAP:
1184 ies->vhtcap_ie = ie;
1186 case IEEE80211_ELEMID_VHT_OPMODE:
1187 ies->vhtopmode_ie = ie;
1189 case IEEE80211_ELEMID_VHT_PWR_ENV:
1190 ies->vhtpwrenv_ie = ie;
1192 case IEEE80211_ELEMID_BSSLOAD:
1193 ies->bssload_ie = ie;
1195 case IEEE80211_ELEMID_APCHANREP:
1196 ies->apchanrep_ie = ie;
1205 * Reclaim any resources in a node and reset any critical
1206 * state. Typically nodes are free'd immediately after,
1207 * but in some cases the storage may be reused so we need
1208 * to insure consistent state (should probably fix that).
1211 node_cleanup(struct ieee80211_node *ni)
1213 struct ieee80211vap *vap = ni->ni_vap;
1214 struct ieee80211com *ic = ni->ni_ic;
1217 /* NB: preserve ni_table */
1218 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1219 if (vap->iv_opmode != IEEE80211_M_STA)
1221 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
1222 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
1223 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
1226 * Cleanup any VHT and HT-related state.
1228 if (ni->ni_flags & IEEE80211_NODE_VHT)
1229 ieee80211_vht_node_cleanup(ni);
1230 if (ni->ni_flags & IEEE80211_NODE_HT)
1231 ieee80211_ht_node_cleanup(ni);
1232 #ifdef IEEE80211_SUPPORT_SUPERG
1233 /* Always do FF node cleanup; for A-MSDU */
1234 ieee80211_ff_node_cleanup(ni);
1236 #ifdef IEEE80211_SUPPORT_MESH
1238 * Cleanup any mesh-related state.
1240 if (vap->iv_opmode == IEEE80211_M_MBSS)
1241 ieee80211_mesh_node_cleanup(ni);
1244 * Clear any staging queue entries.
1246 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1249 * Clear AREF flag that marks the authorization refcnt bump
1250 * has happened. This is probably not needed as the node
1251 * should always be removed from the table so not found but
1252 * do it just in case.
1253 * Likewise clear the ASSOCID flag as these flags are intended
1254 * to be managed in tandem.
1256 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1259 * Drain power save queue and, if needed, clear TIM.
1261 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1262 vap->iv_set_tim(ni, 0);
1265 if (ni->ni_challenge != NULL) {
1266 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1267 ni->ni_challenge = NULL;
1270 * Preserve SSID, WPA, and WME ie's so the bss node is
1271 * reusable during a re-auth/re-assoc state transition.
1272 * If we remove these data they will not be recreated
1273 * because they come from a probe-response or beacon frame
1274 * which cannot be expected prior to the association-response.
1275 * This should not be an issue when operating in other modes
1276 * as stations leaving always go through a full state transition
1277 * which will rebuild this state.
1279 * XXX does this leave us open to inheriting old state?
1281 for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1282 if (ni->ni_rxfrag[i] != NULL) {
1283 m_freem(ni->ni_rxfrag[i]);
1284 ni->ni_rxfrag[i] = NULL;
1287 * Must be careful here to remove any key map entry w/o a LOR.
1289 ieee80211_node_delucastkey(ni);
1293 node_free(struct ieee80211_node *ni)
1295 struct ieee80211com *ic = ni->ni_ic;
1297 ieee80211_ratectl_node_deinit(ni);
1298 ic->ic_node_cleanup(ni);
1299 ieee80211_ies_cleanup(&ni->ni_ies);
1300 ieee80211_psq_cleanup(&ni->ni_psq);
1301 IEEE80211_FREE(ni, M_80211_NODE);
1305 node_age(struct ieee80211_node *ni)
1307 struct ieee80211vap *vap = ni->ni_vap;
1310 * Age frames on the power save queue.
1312 if (ieee80211_node_psq_age(ni) != 0 &&
1313 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1314 vap->iv_set_tim(ni, 0);
1316 * Age out HT resources (e.g. frames on the
1317 * A-MPDU reorder queues).
1319 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1320 ieee80211_ht_node_age(ni);
1324 node_getrssi(const struct ieee80211_node *ni)
1326 uint32_t avgrssi = ni->ni_avgrssi;
1329 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1331 rssi = IEEE80211_RSSI_GET(avgrssi);
1332 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1336 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1338 *rssi = node_getrssi(ni);
1339 *noise = ni->ni_noise;
1343 node_getmimoinfo(const struct ieee80211_node *ni,
1344 struct ieee80211_mimo_info *info)
1350 bzero(info, sizeof(*info));
1352 for (i = 0; i < MIN(IEEE80211_MAX_CHAINS, ni->ni_mimo_chains); i++) {
1353 /* Note: for now, just pri20 channel info */
1354 avgrssi = ni->ni_mimo_rssi_ctl[i];
1355 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1356 info->ch[i].rssi[0] = 0;
1358 rssi = IEEE80211_RSSI_GET(avgrssi);
1359 info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1361 info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i];
1364 /* XXX ext radios? */
1370 ieee80211_add_node_nt(struct ieee80211_node_table *nt,
1371 struct ieee80211_node *ni)
1373 struct ieee80211com *ic = nt->nt_ic;
1376 IEEE80211_NODE_LOCK_ASSERT(nt);
1378 hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr);
1379 (void) ic; /* XXX IEEE80211_NODE_HASH */
1380 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1381 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1387 ieee80211_del_node_nt(struct ieee80211_node_table *nt,
1388 struct ieee80211_node *ni)
1391 IEEE80211_NODE_LOCK_ASSERT(nt);
1393 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1394 LIST_REMOVE(ni, ni_hash);
1396 KASSERT(nt->nt_count >= 0,
1397 ("nt_count is negative (%d)!\n", nt->nt_count));
1398 ni->ni_table = NULL;
1401 struct ieee80211_node *
1402 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1403 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1405 struct ieee80211com *ic = nt->nt_ic;
1406 struct ieee80211_node *ni;
1408 ni = ic->ic_node_alloc(vap, macaddr);
1410 vap->iv_stats.is_rx_nodealloc++;
1414 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1415 "%s %p<%s> in %s table\n", __func__, ni,
1416 ether_sprintf(macaddr), nt->nt_name);
1418 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1419 ieee80211_node_initref(ni); /* mark referenced */
1420 ni->ni_chan = IEEE80211_CHAN_ANYC;
1421 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1422 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1423 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1424 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1425 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1426 ni->ni_inact_reload = nt->nt_inact_init;
1427 ni->ni_inact = ni->ni_inact_reload;
1428 ni->ni_ath_defkeyix = 0x7fff;
1429 ieee80211_psq_init(&ni->ni_psq, "unknown");
1430 #ifdef IEEE80211_SUPPORT_MESH
1431 if (vap->iv_opmode == IEEE80211_M_MBSS)
1432 ieee80211_mesh_node_init(vap, ni);
1434 IEEE80211_NODE_LOCK(nt);
1435 ieee80211_add_node_nt(nt, ni);
1438 IEEE80211_NODE_UNLOCK(nt);
1440 /* handle failure; free node state */
1441 if (ic->ic_node_init(ni) != 0) {
1442 vap->iv_stats.is_rx_nodealloc++;
1443 ieee80211_psq_cleanup(&ni->ni_psq);
1444 ieee80211_ratectl_node_deinit(ni);
1445 __ieee80211_free_node(ni);
1449 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1450 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1456 * Craft a temporary node suitable for sending a management frame
1457 * to the specified station. We craft only as much state as we
1458 * need to do the work since the node will be immediately reclaimed
1459 * once the send completes.
1461 struct ieee80211_node *
1462 ieee80211_tmp_node(struct ieee80211vap *vap,
1463 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1465 struct ieee80211com *ic = vap->iv_ic;
1466 struct ieee80211_node *ni;
1468 ni = ic->ic_node_alloc(vap, macaddr);
1470 struct ieee80211_node *bss = vap->iv_bss;
1472 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1473 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1475 ni->ni_table = NULL; /* NB: pedantic */
1476 ni->ni_ic = ic; /* NB: needed to set channel */
1479 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1480 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1481 ieee80211_node_initref(ni); /* mark referenced */
1482 /* NB: required by ieee80211_fix_rate */
1483 ieee80211_node_set_chan(ni, bss->ni_chan);
1484 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1485 IEEE80211_KEYIX_NONE);
1486 ni->ni_txpower = bss->ni_txpower;
1487 /* XXX optimize away */
1488 ieee80211_psq_init(&ni->ni_psq, "unknown");
1490 ieee80211_ratectl_node_init(ni);
1492 /* handle failure; free node state */
1493 if (ic->ic_node_init(ni) != 0) {
1494 vap->iv_stats.is_rx_nodealloc++;
1495 ieee80211_psq_cleanup(&ni->ni_psq);
1496 ieee80211_ratectl_node_deinit(ni);
1497 __ieee80211_free_node(ni);
1503 vap->iv_stats.is_rx_nodealloc++;
1508 struct ieee80211_node *
1509 ieee80211_dup_bss(struct ieee80211vap *vap,
1510 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1512 struct ieee80211com *ic = vap->iv_ic;
1513 struct ieee80211_node *ni;
1515 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1517 struct ieee80211_node *bss = vap->iv_bss;
1519 * Inherit from iv_bss.
1522 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1523 ieee80211_node_set_chan(ni, bss->ni_chan);
1529 * Create a bss node for a legacy WDS vap. The far end does
1530 * not associate so we just create create a new node and
1531 * simulate an association. The caller is responsible for
1532 * installing the node as the bss node and handling any further
1533 * setup work like authorizing the port.
1535 struct ieee80211_node *
1536 ieee80211_node_create_wds(struct ieee80211vap *vap,
1537 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1539 struct ieee80211com *ic = vap->iv_ic;
1540 struct ieee80211_node *ni;
1542 /* XXX check if node already in sta table? */
1543 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1545 ni->ni_wdsvap = vap;
1546 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1548 * Inherit any manually configured settings.
1550 copy_bss(ni, vap->iv_bss);
1551 ieee80211_node_set_chan(ni, chan);
1552 /* NB: propagate ssid so available to WPA supplicant */
1553 ni->ni_esslen = vap->iv_des_ssid[0].len;
1554 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1555 /* NB: no associd for peer */
1557 * There are no management frames to use to
1558 * discover neighbor capabilities, so blindly
1559 * propagate the local configuration.
1561 if (vap->iv_flags & IEEE80211_F_WME)
1562 ni->ni_flags |= IEEE80211_NODE_QOS;
1563 #ifdef IEEE80211_SUPPORT_SUPERG
1564 if (vap->iv_flags & IEEE80211_F_FF)
1565 ni->ni_flags |= IEEE80211_NODE_FF;
1568 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1569 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1571 * Device is HT-capable and HT is enabled for
1572 * the vap; setup HT operation. On return
1573 * ni_chan will be adjusted to an HT channel.
1575 ieee80211_ht_wds_init(ni);
1576 if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1577 printf("%s: TODO: vht_wds_init\n", __func__);
1580 struct ieee80211_channel *c = ni->ni_chan;
1582 * Force a legacy channel to be used.
1584 c = ieee80211_find_channel(ic,
1585 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1586 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1587 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1594 struct ieee80211_node *
1595 _ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1596 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1597 const char *func __debrefcnt_used, int line __debrefcnt_used)
1599 struct ieee80211_node *ni;
1602 IEEE80211_NODE_LOCK_ASSERT(nt);
1604 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1605 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1606 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1607 ieee80211_ref_node(ni); /* mark referenced */
1608 #ifdef IEEE80211_DEBUG_REFCNT
1609 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1610 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1612 ni, ether_sprintf(ni->ni_macaddr),
1613 ieee80211_node_refcnt(ni));
1621 struct ieee80211_node *
1622 _ieee80211_find_node(struct ieee80211_node_table *nt,
1623 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1624 const char *func __debrefcnt_used, int line __debrefcnt_used)
1626 struct ieee80211_node *ni;
1628 IEEE80211_NODE_LOCK(nt);
1629 ni = _ieee80211_find_node_locked(nt, macaddr, func, line);
1630 IEEE80211_NODE_UNLOCK(nt);
1634 struct ieee80211_node *
1635 _ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1636 const struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN],
1637 const char *func __debrefcnt_used, int line __debrefcnt_used)
1639 struct ieee80211_node *ni;
1642 IEEE80211_NODE_LOCK_ASSERT(nt);
1644 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1645 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1646 if (ni->ni_vap == vap &&
1647 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1648 ieee80211_ref_node(ni); /* mark referenced */
1649 #ifdef IEEE80211_DEBUG_REFCNT
1650 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1651 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1653 ni, ether_sprintf(ni->ni_macaddr),
1654 ieee80211_node_refcnt(ni));
1662 struct ieee80211_node *
1663 _ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1664 const struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN],
1665 const char *func __debrefcnt_used, int line __debrefcnt_used)
1667 struct ieee80211_node *ni;
1669 IEEE80211_NODE_LOCK(nt);
1670 ni = _ieee80211_find_vap_node_locked(nt, vap, macaddr, func, line);
1671 IEEE80211_NODE_UNLOCK(nt);
1676 * Fake up a node; this handles node discovery in adhoc mode.
1677 * Note that for the driver's benefit we treat this like
1678 * an association so the driver has an opportunity to setup
1679 * it's private state.
1681 struct ieee80211_node *
1682 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1683 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1685 struct ieee80211_node *ni;
1687 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1688 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1689 ni = ieee80211_dup_bss(vap, macaddr);
1691 struct ieee80211com *ic = vap->iv_ic;
1693 /* XXX no rate negotiation; just dup */
1694 ni->ni_rates = vap->iv_bss->ni_rates;
1695 if (ieee80211_iserp_rateset(&ni->ni_rates))
1696 ni->ni_flags |= IEEE80211_NODE_ERP;
1697 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1699 * In adhoc demo mode there are no management
1700 * frames to use to discover neighbor capabilities,
1701 * so blindly propagate the local configuration
1702 * so we can do interesting things (e.g. use
1703 * WME to disable ACK's).
1708 if (vap->iv_flags & IEEE80211_F_WME)
1709 ni->ni_flags |= IEEE80211_NODE_QOS;
1710 #ifdef IEEE80211_SUPPORT_SUPERG
1711 if (vap->iv_flags & IEEE80211_F_FF)
1712 ni->ni_flags |= IEEE80211_NODE_FF;
1715 ieee80211_node_setuptxparms(ni);
1716 ieee80211_ratectl_node_init(ni);
1719 * XXX TODO: 11n? At least 20MHz, at least A-MPDU RX,
1720 * not A-MPDU TX; not 11n rates, etc. We'll cycle
1721 * that after we hear that we can indeed do 11n
1722 * (either by a beacon frame or by a probe response.)
1726 * This is the first time we see the node.
1728 if (ic->ic_newassoc != NULL)
1729 ic->ic_newassoc(ni, 1);
1732 * Kick off a probe request to the given node;
1733 * we will then use the probe response to update
1734 * 11n/etc configuration state.
1736 * XXX TODO: this isn't guaranteed, and until we get
1737 * a probe response, we won't be able to actually
1738 * do anything 802.11n related to the node.
1739 * So if this does indeed work, maybe we should hold
1740 * off on sending responses until we get the probe
1741 * response, or just default to some sensible subset
1742 * of 802.11n behaviour (eg always allow aggregation
1743 * negotiation TO us, but not FROM us, etc) so we
1744 * aren't entirely busted.
1746 if (vap->iv_opmode == IEEE80211_M_IBSS) {
1747 ieee80211_send_probereq(ni, /* node */
1748 vap->iv_myaddr, /* SA */
1749 ni->ni_macaddr, /* DA */
1750 vap->iv_bss->ni_bssid, /* BSSID */
1751 vap->iv_bss->ni_essid,
1752 vap->iv_bss->ni_esslen); /* SSID */
1755 /* XXX not right for 802.1x/WPA */
1756 ieee80211_node_authorize(ni);
1762 ieee80211_init_neighbor(struct ieee80211_node *ni,
1763 const struct ieee80211_frame *wh,
1764 const struct ieee80211_scanparams *sp)
1766 int do_ht_setup = 0, do_vht_setup = 0;
1768 ni->ni_esslen = sp->ssid[1];
1769 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1770 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1771 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1772 ni->ni_intval = sp->bintval;
1773 ni->ni_capinfo = sp->capinfo;
1774 ni->ni_chan = ni->ni_ic->ic_curchan;
1775 ni->ni_fhdwell = sp->fhdwell;
1776 ni->ni_fhindex = sp->fhindex;
1777 ni->ni_erp = sp->erp;
1778 ni->ni_timoff = sp->timoff;
1779 #ifdef IEEE80211_SUPPORT_MESH
1780 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1781 ieee80211_mesh_init_neighbor(ni, wh, sp);
1783 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1784 ieee80211_ies_expand(&ni->ni_ies);
1785 if (ni->ni_ies.wme_ie != NULL)
1786 ni->ni_flags |= IEEE80211_NODE_QOS;
1788 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1789 #ifdef IEEE80211_SUPPORT_SUPERG
1790 if (ni->ni_ies.ath_ie != NULL)
1791 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1793 if (ni->ni_ies.htcap_ie != NULL)
1794 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1795 if (ni->ni_ies.htinfo_ie != NULL)
1796 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1798 if (ni->ni_ies.vhtcap_ie != NULL)
1799 ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
1800 if (ni->ni_ies.vhtopmode_ie != NULL)
1801 ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
1803 if ((ni->ni_ies.htcap_ie != NULL) &&
1804 (ni->ni_ies.htinfo_ie != NULL) &&
1805 (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1809 if ((ni->ni_ies.vhtcap_ie != NULL) &&
1810 (ni->ni_ies.vhtopmode_ie != NULL) &&
1811 (ni->ni_vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
1816 /* NB: must be after ni_chan is setup */
1817 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1818 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1819 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1822 * If the neighbor is HT compatible, flip that on.
1825 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1826 "%s: doing HT setup\n", __func__);
1827 ieee80211_ht_node_init(ni);
1828 ieee80211_ht_updateparams(ni,
1829 ni->ni_ies.htcap_ie,
1830 ni->ni_ies.htinfo_ie);
1833 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1834 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
1839 ieee80211_vht_node_init(ni);
1840 ieee80211_vht_updateparams(ni,
1841 ni->ni_ies.vhtcap_ie,
1842 ni->ni_ies.vhtopmode_ie);
1843 ieee80211_setup_vht_rates(ni,
1844 ni->ni_ies.vhtcap_ie,
1845 ni->ni_ies.vhtopmode_ie);
1850 * Finally do the channel upgrade/change based
1851 * on the HT/VHT configuration.
1853 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1854 ni->ni_ies.htinfo_ie);
1855 ieee80211_setup_htrates(ni,
1856 ni->ni_ies.htcap_ie,
1857 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1858 ieee80211_setup_basic_htrates(ni,
1859 ni->ni_ies.htinfo_ie);
1861 ieee80211_node_setuptxparms(ni);
1862 ieee80211_ratectl_node_init(ni);
1864 /* Reassociate; we're now 11n/11ac */
1866 * XXX TODO: this is the wrong thing to do -
1867 * we're calling it with isnew=1 so the ath(4)
1868 * driver reinitialises the rate tables.
1869 * This "mostly" works for ath(4), but it won't
1870 * be right for firmware devices which allocate
1873 * So, do we just create a new node and delete
1876 if (ni->ni_ic->ic_newassoc)
1877 ni->ni_ic->ic_newassoc(ni, 1);
1882 * Do node discovery in adhoc mode on receipt of a beacon
1883 * or probe response frame. Note that for the driver's
1884 * benefit we treat this like an association so the
1885 * driver has an opportunity to setup it's private state.
1887 struct ieee80211_node *
1888 ieee80211_add_neighbor(struct ieee80211vap *vap,
1889 const struct ieee80211_frame *wh,
1890 const struct ieee80211_scanparams *sp)
1892 struct ieee80211_node *ni;
1894 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1895 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1896 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1898 struct ieee80211com *ic = vap->iv_ic;
1900 ieee80211_init_neighbor(ni, wh, sp);
1901 if (ieee80211_iserp_rateset(&ni->ni_rates))
1902 ni->ni_flags |= IEEE80211_NODE_ERP;
1903 ieee80211_node_setuptxparms(ni);
1904 ieee80211_ratectl_node_init(ni);
1905 if (ic->ic_newassoc != NULL)
1906 ic->ic_newassoc(ni, 1);
1907 /* XXX not right for 802.1x/WPA */
1908 ieee80211_node_authorize(ni);
1913 #define IS_PROBEREQ(wh) \
1914 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1915 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1916 #define IS_BCAST_PROBEREQ(wh) \
1917 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1918 ((const struct ieee80211_frame *)(wh))->i_addr3))
1920 static __inline struct ieee80211_node *
1921 _find_rxnode(struct ieee80211_node_table *nt,
1922 const struct ieee80211_frame_min *wh,
1923 const char *func __debrefcnt_used, int line __debrefcnt_used)
1925 if (IS_BCAST_PROBEREQ(wh))
1926 return NULL; /* spam bcast probe req to all vap's */
1927 return _ieee80211_find_node_locked(nt, wh->i_addr2, func, line);
1931 * Locate the node for sender, track state, and then pass the
1932 * (referenced) node up to the 802.11 layer for its use. Note
1933 * we can return NULL if the sender is not in the table.
1935 struct ieee80211_node *
1936 _ieee80211_find_rxnode(struct ieee80211com *ic,
1937 const struct ieee80211_frame_min *wh,
1938 const char *func __debrefcnt_used, int line __debrefcnt_used)
1940 struct ieee80211_node_table *nt;
1941 struct ieee80211_node *ni;
1944 IEEE80211_NODE_LOCK(nt);
1945 ni = _find_rxnode(nt, wh, func, line);
1946 IEEE80211_NODE_UNLOCK(nt);
1952 * Like ieee80211_find_rxnode but use the supplied h/w
1953 * key index as a hint to locate the node in the key
1954 * mapping table. If an entry is present at the key
1955 * index we return it; otherwise do a normal lookup and
1956 * update the mapping table if the station has a unicast
1957 * key assigned to it.
1959 struct ieee80211_node *
1960 _ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1961 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1962 const char *func __debrefcnt_used, int line __debrefcnt_used)
1964 struct ieee80211_node_table *nt;
1965 struct ieee80211_node *ni;
1968 IEEE80211_NODE_LOCK(nt);
1969 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1970 ni = nt->nt_keyixmap[keyix];
1974 ni = _find_rxnode(nt, wh, func, line);
1975 if (ni != NULL && nt->nt_keyixmap != NULL) {
1977 * If the station has a unicast key cache slot
1978 * assigned update the key->node mapping table.
1980 keyix = ni->ni_ucastkey.wk_rxkeyix;
1981 /* XXX can keyixmap[keyix] != NULL? */
1982 if (keyix < nt->nt_keyixmax &&
1983 nt->nt_keyixmap[keyix] == NULL) {
1984 IEEE80211_DPRINTF(ni->ni_vap,
1986 "%s: add key map entry %p<%s> refcnt %d\n",
1987 __func__, ni, ether_sprintf(ni->ni_macaddr),
1988 ieee80211_node_refcnt(ni)+1);
1989 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1993 if (IS_BCAST_PROBEREQ(wh))
1994 ni = NULL; /* spam bcast probe req to all vap's */
1996 ieee80211_ref_node(ni);
1998 IEEE80211_NODE_UNLOCK(nt);
2002 #undef IS_BCAST_PROBEREQ
2006 * Return a reference to the appropriate node for sending
2007 * a data frame. This handles node discovery in adhoc networks.
2009 struct ieee80211_node *
2010 _ieee80211_find_txnode(struct ieee80211vap *vap,
2011 const uint8_t macaddr[IEEE80211_ADDR_LEN],
2012 const char *func __debrefcnt_used, int line __debrefcnt_used)
2014 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
2015 struct ieee80211_node *ni;
2018 * The destination address should be in the node table
2019 * unless this is a multicast/broadcast frame. We can
2020 * also optimize station mode operation, all frames go
2023 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
2024 IEEE80211_NODE_LOCK(nt);
2025 if (vap->iv_opmode == IEEE80211_M_STA ||
2026 vap->iv_opmode == IEEE80211_M_WDS ||
2027 IEEE80211_IS_MULTICAST(macaddr))
2028 ni = ieee80211_ref_node(vap->iv_bss);
2030 ni = _ieee80211_find_node_locked(nt, macaddr, func, line);
2031 IEEE80211_NODE_UNLOCK(nt);
2034 if (vap->iv_opmode == IEEE80211_M_IBSS ||
2035 vap->iv_opmode == IEEE80211_M_AHDEMO) {
2037 * In adhoc mode cons up a node for the destination.
2038 * Note that we need an additional reference for the
2039 * caller to be consistent with
2040 * ieee80211_find_node_locked.
2043 * XXX TODO: this doesn't fake up 11n state; we need
2044 * to find another way to get it upgraded.
2046 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
2048 (void) ieee80211_ref_node(ni);
2050 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
2051 "no node, discard frame (%s)", __func__);
2052 vap->iv_stats.is_tx_nonode++;
2059 __ieee80211_free_node(struct ieee80211_node *ni)
2061 struct ieee80211_node_table *nt = ni->ni_table;
2064 * NB: careful about referencing the vap as it may be
2065 * gone if the last reference was held by a driver.
2066 * We know the com will always be present so it's safe
2067 * to use ni_ic below to reclaim resources.
2070 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2071 "%s %p<%s> in %s table\n", __func__, ni,
2072 ether_sprintf(ni->ni_macaddr),
2073 nt != NULL ? nt->nt_name : "<gone>");
2075 if (ni->ni_associd != 0) {
2076 struct ieee80211vap *vap = ni->ni_vap;
2077 if (vap->iv_aid_bitmap != NULL)
2078 IEEE80211_AID_CLR(vap, ni->ni_associd);
2081 ieee80211_del_node_nt(nt, ni);
2082 ni->ni_ic->ic_node_free(ni);
2086 * Clear any entry in the unicast key mapping table.
2089 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2091 ieee80211_keyix keyix;
2093 keyix = ni->ni_ucastkey.wk_rxkeyix;
2094 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
2095 nt->nt_keyixmap[keyix] == ni) {
2096 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2097 "%s: %p<%s> clear key map entry %u\n",
2098 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
2099 nt->nt_keyixmap[keyix] = NULL;
2100 ieee80211_node_decref(ni);
2108 _ieee80211_free_node(struct ieee80211_node *ni,
2109 const char *func __debrefcnt_used, int line __debrefcnt_used)
2111 struct ieee80211_node_table *nt = ni->ni_table;
2113 #ifdef IEEE80211_DEBUG_REFCNT
2114 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2115 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
2116 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
2119 IEEE80211_NODE_LOCK(nt);
2120 if (ieee80211_node_dectestref(ni)) {
2122 * Last reference, reclaim state.
2124 __ieee80211_free_node(ni);
2125 } else if (ieee80211_node_refcnt(ni) == 1)
2126 if (node_clear_keyixmap(nt, ni))
2127 __ieee80211_free_node(ni);
2128 IEEE80211_NODE_UNLOCK(nt);
2130 if (ieee80211_node_dectestref(ni))
2131 __ieee80211_free_node(ni);
2136 * Reclaim a unicast key and clear any key cache state.
2139 ieee80211_node_delucastkey(struct ieee80211_node *ni)
2141 struct ieee80211com *ic = ni->ni_ic;
2142 struct ieee80211_node_table *nt = &ic->ic_sta;
2143 struct ieee80211_node *nikey;
2144 ieee80211_keyix keyix;
2145 int isowned, status;
2148 * NB: We must beware of LOR here; deleting the key
2149 * can cause the crypto layer to block traffic updates
2150 * which can generate a LOR against the node table lock;
2151 * grab it here and stash the key index for our use below.
2153 * Must also beware of recursion on the node table lock.
2154 * When called from node_cleanup we may already have
2155 * the node table lock held. Unfortunately there's no
2156 * way to separate out this path so we must do this
2159 isowned = IEEE80211_NODE_IS_LOCKED(nt);
2161 IEEE80211_NODE_LOCK(nt);
2163 status = 1; /* NB: success */
2164 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
2165 keyix = ni->ni_ucastkey.wk_rxkeyix;
2166 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
2167 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
2168 nikey = nt->nt_keyixmap[keyix];
2169 nt->nt_keyixmap[keyix] = NULL;
2173 IEEE80211_NODE_UNLOCK(nt);
2175 if (nikey != NULL) {
2176 KASSERT(nikey == ni,
2177 ("key map out of sync, ni %p nikey %p", ni, nikey));
2178 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2179 "%s: delete key map entry %p<%s> refcnt %d\n",
2180 __func__, ni, ether_sprintf(ni->ni_macaddr),
2181 ieee80211_node_refcnt(ni)-1);
2182 ieee80211_free_node(ni);
2188 * Reclaim a node. If this is the last reference count then
2189 * do the normal free work. Otherwise remove it from the node
2190 * table and mark it gone by clearing the back-reference.
2193 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2196 IEEE80211_NODE_LOCK_ASSERT(nt);
2198 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2199 "%s: remove %p<%s> from %s table, refcnt %d\n",
2200 __func__, ni, ether_sprintf(ni->ni_macaddr),
2201 nt->nt_name, ieee80211_node_refcnt(ni)-1);
2203 * Clear any entry in the unicast key mapping table.
2204 * We need to do it here so rx lookups don't find it
2205 * in the mapping table even if it's not in the hash
2206 * table. We cannot depend on the mapping table entry
2207 * being cleared because the node may not be free'd.
2209 (void)node_clear_keyixmap(nt, ni);
2210 if (!ieee80211_node_dectestref(ni)) {
2212 * Other references are present, just remove the
2213 * node from the table so it cannot be found. When
2214 * the references are dropped storage will be
2217 ieee80211_del_node_nt(nt, ni);
2219 __ieee80211_free_node(ni);
2223 * Node table support.
2227 ieee80211_node_table_init(struct ieee80211com *ic,
2228 struct ieee80211_node_table *nt,
2229 const char *name, int inact, int keyixmax)
2233 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
2234 TAILQ_INIT(&nt->nt_node);
2237 nt->nt_inact_init = inact;
2238 nt->nt_keyixmax = keyixmax;
2239 if (nt->nt_keyixmax > 0) {
2240 nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
2241 keyixmax * sizeof(struct ieee80211_node *),
2243 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2244 if (nt->nt_keyixmap == NULL)
2246 "Cannot allocate key index map with %u entries\n",
2249 nt->nt_keyixmap = NULL;
2253 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
2254 struct ieee80211vap *match)
2256 struct ieee80211_node *ni, *next;
2258 IEEE80211_NODE_LOCK(nt);
2259 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
2260 if (match != NULL && ni->ni_vap != match)
2262 /* XXX can this happen? if so need's work */
2263 if (ni->ni_associd != 0) {
2264 struct ieee80211vap *vap = ni->ni_vap;
2266 if (vap->iv_auth->ia_node_leave != NULL)
2267 vap->iv_auth->ia_node_leave(ni);
2268 if (vap->iv_aid_bitmap != NULL)
2269 IEEE80211_AID_CLR(vap, ni->ni_associd);
2271 ni->ni_wdsvap = NULL; /* clear reference */
2272 node_reclaim(nt, ni);
2274 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
2276 * Make a separate pass to clear references to this vap
2277 * held by DWDS entries. They will not be matched above
2278 * because ni_vap will point to the ap vap but we still
2279 * need to clear ni_wdsvap when the WDS vap is destroyed
2282 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
2283 if (ni->ni_wdsvap == match)
2284 ni->ni_wdsvap = NULL;
2286 IEEE80211_NODE_UNLOCK(nt);
2290 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2292 ieee80211_node_table_reset(nt, NULL);
2293 if (nt->nt_keyixmap != NULL) {
2295 /* XXX verify all entries are NULL */
2297 for (i = 0; i < nt->nt_keyixmax; i++)
2298 if (nt->nt_keyixmap[i] != NULL)
2299 printf("%s: %s[%u] still active\n", __func__,
2302 IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
2303 nt->nt_keyixmap = NULL;
2305 IEEE80211_NODE_LOCK_DESTROY(nt);
2309 timeout_stations(void *arg __unused, struct ieee80211_node *ni)
2311 struct ieee80211com *ic = ni->ni_ic;
2312 struct ieee80211vap *vap = ni->ni_vap;
2315 * Only process stations when in RUN state. This
2316 * insures, for example, that we don't timeout an
2317 * inactive station during CAC. Note that CSA state
2318 * is actually handled in ieee80211_node_timeout as
2319 * it applies to more than timeout processing.
2321 if (vap->iv_state != IEEE80211_S_RUN)
2324 * Ignore entries for which have yet to receive an
2325 * authentication frame. These are transient and
2326 * will be reclaimed when the last reference to them
2327 * goes away (when frame xmits complete).
2329 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2330 vap->iv_opmode == IEEE80211_M_STA) &&
2331 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2334 * Free fragment if not needed anymore
2335 * (last fragment older than 1s).
2336 * XXX doesn't belong here, move to node_age
2338 if (ni->ni_rxfrag[0] != NULL &&
2339 ticks > ni->ni_rxfragstamp + hz) {
2340 m_freem(ni->ni_rxfrag[0]);
2341 ni->ni_rxfrag[0] = NULL;
2343 if (ni->ni_inact > 0) {
2345 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2346 "%s: inact %u inact_reload %u nrates %u",
2347 __func__, ni->ni_inact, ni->ni_inact_reload,
2348 ni->ni_rates.rs_nrates);
2351 * Special case ourself; we may be idle for extended periods
2352 * of time and regardless reclaiming our state is wrong.
2353 * XXX run ic_node_age
2355 /* XXX before inact decrement? */
2356 if (ni == vap->iv_bss)
2358 if (ni->ni_associd != 0 ||
2359 (vap->iv_opmode == IEEE80211_M_IBSS ||
2360 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2362 * Age/drain resources held by the station.
2364 ic->ic_node_age(ni);
2366 * Probe the station before time it out. We
2367 * send a null data frame which may not be
2368 * universally supported by drivers (need it
2369 * for ps-poll support so it should be...).
2371 * XXX don't probe the station unless we've
2372 * received a frame from them (and have
2373 * some idea of the rates they are capable
2374 * of); this will get fixed more properly
2375 * soon with better handling of the rate set.
2377 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2378 (0 < ni->ni_inact &&
2379 ni->ni_inact <= vap->iv_inact_probe) &&
2380 ni->ni_rates.rs_nrates != 0) {
2382 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2384 "probe station due to inactivity");
2386 * Grab a reference so the node cannot
2387 * be reclaimed before we send the frame.
2388 * ieee80211_send_nulldata understands
2389 * we've done this and reclaims the
2390 * ref for us as needed.
2392 /* XXX fix this (not required anymore). */
2393 ieee80211_ref_node(ni);
2395 ieee80211_send_nulldata(ni);
2400 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2401 ni->ni_inact <= 0) {
2403 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2404 "station timed out due to inactivity "
2405 "(refcnt %u)", ieee80211_node_refcnt(ni));
2407 * Send a deauthenticate frame and drop the station.
2408 * This is somewhat complicated due to reference counts
2409 * and locking. At this point a station will typically
2410 * have a reference count of 2. ieee80211_node_leave
2411 * will do a "free" of the node which will drop the
2412 * reference count. But in the meantime a reference
2413 * wil be held by the deauth frame. The actual reclaim
2414 * of the node will happen either after the tx is
2415 * completed or by ieee80211_node_leave.
2417 if (ni->ni_associd != 0) {
2418 IEEE80211_SEND_MGMT(ni,
2419 IEEE80211_FC0_SUBTYPE_DEAUTH,
2420 IEEE80211_REASON_AUTH_EXPIRE);
2422 ieee80211_node_leave(ni);
2423 vap->iv_stats.is_node_timeout++;
2428 * Timeout inactive stations and do related housekeeping.
2431 ieee80211_timeout_stations(struct ieee80211com *ic)
2433 struct ieee80211_node_table *nt = &ic->ic_sta;
2435 ieee80211_iterate_nodes(nt, timeout_stations, NULL);
2439 * Aggressively reclaim resources. This should be used
2440 * only in a critical situation to reclaim mbuf resources.
2443 ieee80211_drain(struct ieee80211com *ic)
2445 struct ieee80211_node_table *nt = &ic->ic_sta;
2446 struct ieee80211vap *vap;
2447 struct ieee80211_node *ni;
2449 IEEE80211_NODE_LOCK(nt);
2450 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2452 * Ignore entries for which have yet to receive an
2453 * authentication frame. These are transient and
2454 * will be reclaimed when the last reference to them
2455 * goes away (when frame xmits complete).
2459 * Only process stations when in RUN state. This
2460 * insures, for example, that we don't timeout an
2461 * inactive station during CAC. Note that CSA state
2462 * is actually handled in ieee80211_node_timeout as
2463 * it applies to more than timeout processing.
2465 if (vap->iv_state != IEEE80211_S_RUN)
2467 /* XXX can vap be NULL? */
2468 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2469 vap->iv_opmode == IEEE80211_M_STA) &&
2470 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2474 * XXX doesn't belong here, move to node_drain
2476 if (ni->ni_rxfrag[0] != NULL) {
2477 m_freem(ni->ni_rxfrag[0]);
2478 ni->ni_rxfrag[0] = NULL;
2481 * Drain resources held by the station.
2483 ic->ic_node_drain(ni);
2485 IEEE80211_NODE_UNLOCK(nt);
2489 * Per-ieee80211vap inactivity timer callback.
2492 ieee80211_vap_timeout(struct ieee80211vap *vap)
2495 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2497 ieee80211_vap_erp_timeout(vap);
2498 ieee80211_ht_timeout(vap);
2499 ieee80211_vht_timeout(vap);
2503 * Per-ieee80211com inactivity timer callback.
2506 ieee80211_node_timeout(void *arg)
2508 struct ieee80211com *ic = arg;
2509 struct ieee80211vap *vap;
2512 * Defer timeout processing if a channel switch is pending.
2513 * We typically need to be mute so not doing things that
2514 * might generate frames is good to handle in one place.
2515 * Suppressing the station timeout processing may extend the
2516 * lifetime of inactive stations (by not decrementing their
2517 * idle counters) but this should be ok unless the CSA is
2518 * active for an unusually long time.
2520 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2521 ieee80211_scan_timeout(ic);
2522 ieee80211_timeout_stations(ic);
2523 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2526 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2527 ieee80211_vap_timeout(vap);
2528 IEEE80211_UNLOCK(ic);
2530 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2531 ieee80211_node_timeout, ic);
2535 * The same as ieee80211_iterate_nodes(), but for one vap only.
2538 ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
2539 struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
2541 struct ieee80211_node **ni_arr;
2542 struct ieee80211_node *ni;
2547 * Iterate over the node table and save an array of ref'ed nodes.
2549 * This is separated out from calling the actual node function so that
2550 * no LORs will occur.
2552 IEEE80211_NODE_LOCK(nt);
2553 count = nt->nt_count;
2554 size = count * sizeof(struct ieee80211_node *);
2555 ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
2556 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2557 if (ni_arr == NULL) {
2558 IEEE80211_NODE_UNLOCK(nt);
2563 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2564 if (vap != NULL && ni->ni_vap != vap)
2567 ("node array overflow (vap %p, i %d, count %d)\n",
2569 ni_arr[i] = ieee80211_ref_node(ni);
2572 IEEE80211_NODE_UNLOCK(nt);
2574 for (i = 0; i < count; i++) {
2575 if (ni_arr[i] == NULL) /* end of the list */
2577 (*f)(arg, ni_arr[i]);
2578 /* ieee80211_free_node() locks by itself */
2579 ieee80211_free_node(ni_arr[i]);
2582 IEEE80211_FREE(ni_arr, M_80211_NODE);
2588 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2589 * reference in the source.
2592 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2593 ieee80211_iter_func *f, void *arg)
2595 /* XXX no way to pass error to the caller. */
2596 (void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
2600 ieee80211_dump_node(struct ieee80211_node_table *nt __unused,
2601 struct ieee80211_node *ni)
2603 printf("%p: mac %s refcnt %d\n", ni,
2604 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2605 printf("\tauthmode %u flags 0x%x\n",
2606 ni->ni_authmode, ni->ni_flags);
2607 printf("\tassocid 0x%x txpower %u vlan %u\n",
2608 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2609 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2610 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2611 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2612 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2613 ni->ni_rxfragstamp);
2614 printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2615 node_getrssi(ni), ni->ni_noise,
2616 ni->ni_intval, ni->ni_capinfo);
2617 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2618 ether_sprintf(ni->ni_bssid),
2619 ni->ni_esslen, ni->ni_essid,
2620 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2621 printf("\tinact %u inact_reload %u txrate %u\n",
2622 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2623 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2624 ni->ni_htcap, ni->ni_htparam,
2625 ni->ni_htctlchan, ni->ni_ht2ndchan);
2626 printf("\thtopmode %x htstbc %x htchw %u\n",
2627 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2628 printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n",
2629 ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2,
2630 (int) ni->ni_vht_basicmcs);
2635 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2637 ieee80211_iterate_nodes(nt,
2638 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2642 * Iterate over the VAPs and update their ERP beacon IEs.
2644 * Note this must be called from the deferred ERP update task paths.
2647 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2649 struct ieee80211vap *vap;
2651 IEEE80211_LOCK_ASSERT(ic);
2653 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2654 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2655 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2659 * Handle a station joining an 11g network.
2662 ieee80211_node_join_11g(struct ieee80211_node *ni)
2664 struct ieee80211com *ic = ni->ni_ic;
2665 struct ieee80211vap *vap = ni->ni_vap;
2667 IEEE80211_LOCK_ASSERT(ic);
2670 * Station isn't capable of short slot time. Bump
2671 * the count of long slot time stations and disable
2672 * use of short slot time. Note that the actual switch
2673 * over to long slot time use may not occur until the
2674 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2676 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2677 vap->iv_longslotsta++;
2678 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2679 "station needs long slot time, count %d",
2680 vap->iv_longslotsta);
2682 * XXX TODO: this may need all VAPs checked!
2684 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2686 * Don't force slot time when switched to turbo
2687 * mode as non-ERP stations won't be present; this
2688 * need only be done when on the normal G channel.
2690 ieee80211_vap_set_shortslottime(vap, 0);
2694 * If the new station is not an ERP station
2695 * then bump the counter and enable protection
2698 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2699 vap->iv_nonerpsta++;
2700 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2701 "station is !ERP, %d non-ERP stations associated",
2704 * If station does not support short preamble
2705 * then we must enable use of Barker preamble.
2707 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2708 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2709 "%s", "station needs long preamble");
2710 vap->iv_flags |= IEEE80211_F_USEBARKER;
2711 vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE;
2712 ieee80211_vap_update_preamble(vap);
2715 * If protection is configured and this is the first
2716 * indication we should use protection, enable it.
2718 if (vap->iv_protmode != IEEE80211_PROT_NONE &&
2719 vap->iv_nonerpsta == 1 &&
2720 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2721 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2722 "%s: enable use of protection\n", __func__);
2723 vap->iv_flags |= IEEE80211_F_USEPROT;
2724 ieee80211_vap_update_erp_protmode(vap);
2727 ni->ni_flags |= IEEE80211_NODE_ERP;
2731 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2733 struct ieee80211com *ic = ni->ni_ic;
2734 struct ieee80211vap *vap = ni->ni_vap;
2737 if (ni->ni_associd == 0) {
2740 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2742 * It would be good to search the bitmap
2743 * more efficiently, but this will do for now.
2745 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2746 if (!IEEE80211_AID_ISSET(vap, aid))
2749 if (aid >= vap->iv_max_aid) {
2750 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2751 ieee80211_node_leave(ni);
2754 ni->ni_associd = aid | 0xc000;
2755 ni->ni_jointime = time_uptime;
2757 IEEE80211_AID_SET(vap, ni->ni_associd);
2758 vap->iv_sta_assoc++;
2760 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2761 ieee80211_ht_node_join(ni);
2762 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2763 ieee80211_vht_node_join(ni);
2764 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2765 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2766 ieee80211_node_join_11g(ni);
2767 IEEE80211_UNLOCK(ic);
2774 * XXX VHT - should log VHT channel width, etc
2776 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2777 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s%s",
2778 IEEE80211_NODE_AID(ni),
2779 vap->iv_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2780 vap->iv_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2781 vap->iv_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2782 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2783 /* XXX update for VHT string */
2784 ni->ni_flags & IEEE80211_NODE_HT ?
2785 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2786 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2787 ni->ni_flags & IEEE80211_NODE_AMSDU ? " (+AMSDU)" : "",
2788 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2789 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2790 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2791 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2792 ", fast-frames" : "",
2793 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2797 ieee80211_node_setuptxparms(ni);
2798 ieee80211_ratectl_node_init(ni);
2799 /* give driver a chance to setup state like ni_txrate */
2800 if (ic->ic_newassoc != NULL)
2801 ic->ic_newassoc(ni, newassoc);
2802 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2803 /* tell the authenticator about new station */
2804 if (vap->iv_auth->ia_node_join != NULL)
2805 vap->iv_auth->ia_node_join(ni);
2806 ieee80211_notify_node_join(ni,
2807 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2811 disable_protection(struct ieee80211vap *vap)
2813 struct ieee80211com *ic = vap->iv_ic;
2815 KASSERT(vap->iv_nonerpsta == 0 &&
2816 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2817 ("%d non ERP stations, flags 0x%x", vap->iv_nonerpsta,
2818 vap->iv_flags_ext));
2820 vap->iv_flags &= ~IEEE80211_F_USEPROT;
2821 /* XXX verify mode? */
2822 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2823 vap->iv_flags |= IEEE80211_F_SHPREAMBLE;
2824 vap->iv_flags &= ~IEEE80211_F_USEBARKER;
2826 ieee80211_vap_update_erp_protmode(vap);
2827 ieee80211_vap_update_preamble(vap);
2831 * Handle a station leaving an 11g network.
2834 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2836 struct ieee80211com *ic = ni->ni_ic;
2837 struct ieee80211vap *vap = ni->ni_vap;
2839 IEEE80211_LOCK_ASSERT(ic);
2841 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2842 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2843 ic->ic_bsschan->ic_flags));
2846 * If a long slot station do the slot time bookkeeping.
2848 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2849 KASSERT(vap->iv_longslotsta > 0,
2850 ("bogus long slot station count %d", vap->iv_longslotsta));
2851 vap->iv_longslotsta--;
2852 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2853 "long slot time station leaves, count now %d",
2854 vap->iv_longslotsta);
2856 * XXX TODO: this may need all VAPs checked!
2858 if (vap->iv_longslotsta == 0) {
2860 * Re-enable use of short slot time if supported
2861 * and not operating in IBSS mode (per spec).
2863 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2864 ic->ic_opmode != IEEE80211_M_IBSS) {
2865 IEEE80211_DPRINTF(ni->ni_vap,
2866 IEEE80211_MSG_ASSOC,
2867 "%s: re-enable use of short slot time\n",
2869 ieee80211_vap_set_shortslottime(vap, 1);
2874 * If a non-ERP station do the protection-related bookkeeping.
2876 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2877 KASSERT(vap->iv_nonerpsta > 0,
2878 ("bogus non-ERP station count %d", vap->iv_nonerpsta));
2879 vap->iv_nonerpsta--;
2880 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2881 "non-ERP station leaves, count now %d%s", vap->iv_nonerpsta,
2882 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2883 " (non-ERP sta present)" : "");
2884 if (vap->iv_nonerpsta == 0 &&
2885 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2886 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2887 "%s: disable use of protection\n", __func__);
2888 disable_protection(vap);
2894 * Time out presence of an overlapping bss with non-ERP
2895 * stations. When operating in hostap mode we listen for
2896 * beacons from other stations and if we identify a non-ERP
2897 * station is present we enable protection. To identify
2898 * when all non-ERP stations are gone we time out this
2902 ieee80211_vap_erp_timeout(struct ieee80211vap *vap)
2905 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2907 if ((vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2908 ieee80211_time_after(ticks, vap->iv_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2909 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
2910 "%s", "age out non-ERP sta present on channel");
2911 vap->iv_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2912 if (vap->iv_nonerpsta == 0)
2913 disable_protection(vap);
2918 * Handle bookkeeping for station deauthentication/disassociation
2919 * when operating as an ap.
2922 ieee80211_node_leave(struct ieee80211_node *ni)
2924 struct ieee80211com *ic = ni->ni_ic;
2925 struct ieee80211vap *vap = ni->ni_vap;
2926 struct ieee80211_node_table *nt = ni->ni_table;
2928 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2929 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2931 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2932 ("unexpected operating mode %u", vap->iv_opmode));
2934 * If node wasn't previously associated all
2935 * we need to do is reclaim the reference.
2937 /* XXX ibss mode bypasses 11g and notification */
2938 if (ni->ni_associd == 0)
2941 * Tell the authenticator the station is leaving.
2942 * Note that we must do this before yanking the
2943 * association id as the authenticator uses the
2944 * associd to locate it's state block.
2946 if (vap->iv_auth->ia_node_leave != NULL)
2947 vap->iv_auth->ia_node_leave(ni);
2950 IEEE80211_AID_CLR(vap, ni->ni_associd);
2951 vap->iv_sta_assoc--;
2953 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2954 ieee80211_vht_node_leave(ni);
2955 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2956 ieee80211_ht_node_leave(ni);
2957 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2958 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2959 ieee80211_node_leave_11g(ni);
2960 IEEE80211_UNLOCK(ic);
2962 * Cleanup station state. In particular clear various
2963 * state that might otherwise be reused if the node
2964 * is reused before the reference count goes to zero
2965 * (and memory is reclaimed).
2967 ieee80211_sta_leave(ni);
2970 * Remove the node from any table it's recorded in and
2971 * drop the caller's reference. Removal from the table
2972 * is important to insure the node is not reprocessed
2976 IEEE80211_NODE_LOCK(nt);
2977 node_reclaim(nt, ni);
2978 IEEE80211_NODE_UNLOCK(nt);
2980 ieee80211_free_node(ni);
2985 uint32_t rssi_total;
2989 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2991 struct rssiinfo *info = arg;
2992 struct ieee80211vap *vap = ni->ni_vap;
2995 /* only associated stations */
2996 if (ni->ni_associd == 0)
2998 rssi = vap->iv_ic->ic_node_getrssi(ni);
3000 info->rssi_samples++;
3001 info->rssi_total += rssi;
3006 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
3008 struct rssiinfo *info = arg;
3009 struct ieee80211vap *vap = ni->ni_vap;
3012 /* only neighbors */
3013 /* XXX check bssid */
3014 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
3016 rssi = vap->iv_ic->ic_node_getrssi(ni);
3018 info->rssi_samples++;
3019 info->rssi_total += rssi;
3023 #ifdef IEEE80211_SUPPORT_MESH
3025 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
3027 struct rssiinfo *info = arg;
3028 struct ieee80211vap *vap = ni->ni_vap;
3031 /* only neighbors that peered successfully */
3032 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
3034 rssi = vap->iv_ic->ic_node_getrssi(ni);
3036 info->rssi_samples++;
3037 info->rssi_total += rssi;
3040 #endif /* IEEE80211_SUPPORT_MESH */
3043 ieee80211_getrssi(struct ieee80211vap *vap)
3045 #define NZ(x) ((x) == 0 ? 1 : (x))
3046 struct ieee80211com *ic = vap->iv_ic;
3047 struct rssiinfo info;
3049 info.rssi_total = 0;
3050 info.rssi_samples = 0;
3051 switch (vap->iv_opmode) {
3052 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
3053 case IEEE80211_M_AHDEMO: /* average of all neighbors */
3054 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
3057 case IEEE80211_M_HOSTAP: /* average of all associated stations */
3058 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
3061 #ifdef IEEE80211_SUPPORT_MESH
3062 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
3063 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
3067 case IEEE80211_M_MONITOR: /* XXX */
3068 case IEEE80211_M_STA: /* use stats from associated ap */
3070 if (vap->iv_bss != NULL)
3071 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
3072 info.rssi_samples = 1;
3075 return info.rssi_total / NZ(info.rssi_samples);
3080 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
3083 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
3085 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
3086 /* for non-station mode return avg'd rssi accounting */
3087 if (vap->iv_opmode != IEEE80211_M_STA)
3088 *rssi = ieee80211_getrssi(vap);