2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
63 * IEEE80211_NODE_HASHSIZE must be a power of 2.
65 CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
68 * Association id's are managed with a bit vector.
70 #define IEEE80211_AID_SET(_vap, b) \
71 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
72 (1 << (IEEE80211_AID(b) % 32)))
73 #define IEEE80211_AID_CLR(_vap, b) \
74 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
75 ~(1 << (IEEE80211_AID(b) % 32)))
76 #define IEEE80211_AID_ISSET(_vap, b) \
77 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
79 static int ieee80211_sta_join1(struct ieee80211_node *);
81 static struct ieee80211_node *node_alloc(struct ieee80211vap *,
82 const uint8_t [IEEE80211_ADDR_LEN]);
83 static int node_init(struct ieee80211_node *);
84 static void node_cleanup(struct ieee80211_node *);
85 static void node_free(struct ieee80211_node *);
86 static void node_age(struct ieee80211_node *);
87 static int8_t node_getrssi(const struct ieee80211_node *);
88 static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
89 static void node_getmimoinfo(const struct ieee80211_node *,
90 struct ieee80211_mimo_info *);
92 static void _ieee80211_free_node(struct ieee80211_node *);
94 static void node_reclaim(struct ieee80211_node_table *nt,
95 struct ieee80211_node *ni);
96 static void ieee80211_node_table_init(struct ieee80211com *ic,
97 struct ieee80211_node_table *nt, const char *name,
98 int inact, int keymaxix);
99 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
100 struct ieee80211vap *);
101 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
102 static void ieee80211_erp_timeout(struct ieee80211com *);
104 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
105 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
108 ieee80211_node_attach(struct ieee80211com *ic)
110 /* XXX really want maxlen enforced per-sta */
111 ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
113 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
114 IEEE80211_INACT_INIT, ic->ic_max_keyix);
115 callout_init(&ic->ic_inact, 1);
116 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
117 ieee80211_node_timeout, ic);
119 ic->ic_node_alloc = node_alloc;
120 ic->ic_node_init = node_init;
121 ic->ic_node_free = node_free;
122 ic->ic_node_cleanup = node_cleanup;
123 ic->ic_node_age = node_age;
124 ic->ic_node_drain = node_age; /* NB: same as age */
125 ic->ic_node_getrssi = node_getrssi;
126 ic->ic_node_getsignal = node_getsignal;
127 ic->ic_node_getmimoinfo = node_getmimoinfo;
130 * Set flags to be propagated to all vap's;
131 * these define default behaviour/configuration.
133 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
137 ieee80211_node_detach(struct ieee80211com *ic)
140 callout_drain(&ic->ic_inact);
141 ieee80211_node_table_cleanup(&ic->ic_sta);
142 ieee80211_ageq_drain(&ic->ic_stageq);
143 ieee80211_ageq_cleanup(&ic->ic_stageq);
147 ieee80211_node_vattach(struct ieee80211vap *vap)
149 /* NB: driver can override */
150 vap->iv_max_aid = IEEE80211_AID_DEF;
152 /* default station inactivity timer setings */
153 vap->iv_inact_init = IEEE80211_INACT_INIT;
154 vap->iv_inact_auth = IEEE80211_INACT_AUTH;
155 vap->iv_inact_run = IEEE80211_INACT_RUN;
156 vap->iv_inact_probe = IEEE80211_INACT_PROBE;
158 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
159 "%s: init %u auth %u run %u probe %u\n", __func__,
160 vap->iv_inact_init, vap->iv_inact_auth,
161 vap->iv_inact_run, vap->iv_inact_probe);
165 ieee80211_node_latevattach(struct ieee80211vap *vap)
167 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
168 /* XXX should we allow max aid to be zero? */
169 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
170 vap->iv_max_aid = IEEE80211_AID_MIN;
171 if_printf(vap->iv_ifp,
172 "WARNING: max aid too small, changed to %d\n",
175 vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC(
176 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
178 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
179 if (vap->iv_aid_bitmap == NULL) {
180 /* XXX no way to recover */
181 printf("%s: no memory for AID bitmap, max aid %d!\n",
182 __func__, vap->iv_max_aid);
187 ieee80211_reset_bss(vap);
189 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
193 ieee80211_node_vdetach(struct ieee80211vap *vap)
195 struct ieee80211com *ic = vap->iv_ic;
197 ieee80211_node_table_reset(&ic->ic_sta, vap);
198 if (vap->iv_bss != NULL) {
199 ieee80211_free_node(vap->iv_bss);
202 if (vap->iv_aid_bitmap != NULL) {
203 IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE);
204 vap->iv_aid_bitmap = NULL;
209 * Port authorize/unauthorize interfaces for use by an authenticator.
213 ieee80211_node_authorize(struct ieee80211_node *ni)
215 struct ieee80211vap *vap = ni->ni_vap;
217 ni->ni_flags |= IEEE80211_NODE_AUTH;
218 ni->ni_inact_reload = vap->iv_inact_run;
219 ni->ni_inact = ni->ni_inact_reload;
221 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
222 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
226 ieee80211_node_unauthorize(struct ieee80211_node *ni)
228 struct ieee80211vap *vap = ni->ni_vap;
230 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
231 ni->ni_inact_reload = vap->iv_inact_auth;
232 if (ni->ni_inact > ni->ni_inact_reload)
233 ni->ni_inact = ni->ni_inact_reload;
235 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
236 "%s: inact_reload %u inact %u", __func__,
237 ni->ni_inact_reload, ni->ni_inact);
241 * Fix tx parameters for a node according to ``association state''.
244 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
246 struct ieee80211vap *vap = ni->ni_vap;
247 enum ieee80211_phymode mode;
249 if (ni->ni_flags & IEEE80211_NODE_VHT) {
250 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
251 mode = IEEE80211_MODE_VHT_5GHZ;
253 mode = IEEE80211_MODE_VHT_2GHZ;
254 } else if (ni->ni_flags & IEEE80211_NODE_HT) {
255 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
256 mode = IEEE80211_MODE_11NA;
258 mode = IEEE80211_MODE_11NG;
259 } else { /* legacy rate handling */
260 if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
261 mode = IEEE80211_MODE_STURBO_A;
262 else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
263 mode = IEEE80211_MODE_HALF;
264 else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
265 mode = IEEE80211_MODE_QUARTER;
266 /* NB: 108A should be handled as 11a */
267 else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
268 mode = IEEE80211_MODE_11A;
269 else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
270 (ni->ni_flags & IEEE80211_NODE_ERP))
271 mode = IEEE80211_MODE_11G;
273 mode = IEEE80211_MODE_11B;
275 ni->ni_txparms = &vap->iv_txparms[mode];
279 * Set/change the channel. The rate set is also updated as
280 * to insure a consistent view by drivers.
281 * XXX should be private but hostap needs it to deal with CSA
284 ieee80211_node_set_chan(struct ieee80211_node *ni,
285 struct ieee80211_channel *chan)
287 struct ieee80211com *ic = ni->ni_ic;
288 struct ieee80211vap *vap = ni->ni_vap;
289 enum ieee80211_phymode mode;
291 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
294 mode = ieee80211_chan2mode(chan);
295 if (IEEE80211_IS_CHAN_HT(chan)) {
297 * We must install the legacy rate est in ni_rates and the
298 * HT rate set in ni_htrates.
300 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
302 * Setup bss tx parameters based on operating mode. We
303 * use legacy rates when operating in a mixed HT+non-HT bss
304 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
306 if (mode == IEEE80211_MODE_11NA &&
307 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
308 mode = IEEE80211_MODE_11A;
309 else if (mode == IEEE80211_MODE_11NG &&
310 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
311 mode = IEEE80211_MODE_11G;
312 if (mode == IEEE80211_MODE_11G &&
313 (vap->iv_flags & IEEE80211_F_PUREG) == 0)
314 mode = IEEE80211_MODE_11B;
316 ni->ni_txparms = &vap->iv_txparms[mode];
317 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
321 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
323 /* propagate useful state */
324 nbss->ni_authmode = obss->ni_authmode;
325 nbss->ni_txpower = obss->ni_txpower;
326 nbss->ni_vlan = obss->ni_vlan;
327 /* XXX statistics? */
328 /* XXX legacy WDS bssid? */
332 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
334 struct ieee80211com *ic = vap->iv_ic;
335 struct ieee80211_node *ni;
337 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
338 "%s: creating %s on channel %u%c flags 0x%08x\n", __func__,
339 ieee80211_opmode_name[vap->iv_opmode],
340 ieee80211_chan2ieee(ic, chan),
341 ieee80211_channel_type_char(chan),
344 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
349 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
350 ni->ni_esslen = vap->iv_des_ssid[0].len;
351 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
352 if (vap->iv_bss != NULL)
353 copy_bss(ni, vap->iv_bss);
354 ni->ni_intval = ic->ic_bintval;
355 if (vap->iv_flags & IEEE80211_F_PRIVACY)
356 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
357 if (ic->ic_phytype == IEEE80211_T_FH) {
358 ni->ni_fhdwell = 200; /* XXX */
361 if (vap->iv_opmode == IEEE80211_M_IBSS) {
362 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
363 if (vap->iv_flags & IEEE80211_F_DESBSSID)
364 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
366 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
367 /* clear group bit, add local bit */
368 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
370 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
371 if (vap->iv_flags & IEEE80211_F_DESBSSID)
372 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
374 #ifdef IEEE80211_SUPPORT_TDMA
375 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
377 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
378 #ifdef IEEE80211_SUPPORT_MESH
379 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
380 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
381 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
385 * Fix the channel and related attributes.
387 /* clear DFS CAC state on previous channel */
388 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
389 ic->ic_bsschan->ic_freq != chan->ic_freq &&
390 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
391 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
392 ic->ic_bsschan = chan;
393 ieee80211_node_set_chan(ni, chan);
394 ic->ic_curmode = ieee80211_chan2mode(chan);
396 * Do mode-specific setup.
398 if (IEEE80211_IS_CHAN_FULL(chan)) {
399 if (IEEE80211_IS_CHAN_ANYG(chan)) {
401 * Use a mixed 11b/11g basic rate set.
403 ieee80211_setbasicrates(&ni->ni_rates,
405 if (vap->iv_flags & IEEE80211_F_PUREG) {
407 * Also mark OFDM rates basic so 11b
408 * stations do not join (WiFi compliance).
410 ieee80211_addbasicrates(&ni->ni_rates,
413 } else if (IEEE80211_IS_CHAN_B(chan)) {
415 * Force pure 11b rate set.
417 ieee80211_setbasicrates(&ni->ni_rates,
422 /* XXX TODO: other bits and pieces - eg fast-frames? */
424 /* If we're an 11n channel then initialise the 11n bits */
425 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
427 ieee80211_ht_node_init(ni);
428 ieee80211_vht_node_init(ni);
429 } else if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
431 ieee80211_ht_node_init(ni);
434 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
438 * Reset bss state on transition to the INIT state.
439 * Clear any stations from the table (they have been
440 * deauth'd) and reset the bss node (clears key, rate
444 ieee80211_reset_bss(struct ieee80211vap *vap)
446 struct ieee80211com *ic = vap->iv_ic;
447 struct ieee80211_node *ni, *obss;
449 ieee80211_node_table_reset(&ic->ic_sta, vap);
450 /* XXX multi-bss: wrong */
451 ieee80211_vap_reset_erp(vap);
453 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
454 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
456 vap->iv_bss = ieee80211_ref_node(ni);
459 ni->ni_intval = ic->ic_bintval;
460 ieee80211_free_node(obss);
462 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
466 match_ssid(const struct ieee80211_node *ni,
467 int nssid, const struct ieee80211_scan_ssid ssids[])
471 for (i = 0; i < nssid; i++) {
472 if (ni->ni_esslen == ssids[i].len &&
473 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
480 * Test a node for suitability/compatibility.
483 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
485 struct ieee80211com *ic = ni->ni_ic;
488 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
490 if (vap->iv_opmode == IEEE80211_M_IBSS) {
491 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
494 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
497 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
498 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
501 /* XXX does this mean privacy is supported or required? */
502 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
505 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
506 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
507 if (rate & IEEE80211_RATE_BASIC)
509 if (vap->iv_des_nssid != 0 &&
510 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
512 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
513 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
518 #ifdef IEEE80211_DEBUG
520 * Display node suitability/compatibility.
523 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
525 struct ieee80211com *ic = ni->ni_ic;
530 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
532 if (vap->iv_opmode == IEEE80211_M_IBSS) {
533 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
536 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
539 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
540 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
543 /* XXX does this mean privacy is supported or required? */
544 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
547 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
548 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
549 if (rate & IEEE80211_RATE_BASIC)
551 if (vap->iv_des_nssid != 0 &&
552 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
554 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
555 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
558 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
559 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
561 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
562 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
563 fail & 0x08 ? '!' : ' ');
565 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
566 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
568 fail & 0x02 ? '!' : ' ');
570 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
571 fail & 0x04 ? '!' : ' ');
572 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
573 printf("%s\n", fail & 0x10 ? "!" : "");
575 #endif /* IEEE80211_DEBUG */
579 ieee80211_ibss_merge_check(struct ieee80211_node *ni)
581 struct ieee80211vap *vap = ni->ni_vap;
583 if (ni == vap->iv_bss ||
584 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
585 /* unchanged, nothing to do */
589 if (!check_bss(vap, ni)) {
590 /* capabilities mismatch */
591 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
592 "%s: merge failed, capabilities mismatch\n", __func__);
593 #ifdef IEEE80211_DEBUG
594 if (ieee80211_msg_assoc(vap))
595 check_bss_debug(vap, ni);
597 vap->iv_stats.is_ibss_capmismatch++;
605 * Check if the given node should populate the node table.
607 * We need to be in "see all beacons for all ssids" mode in order
608 * to do IBSS merges, however this means we will populate nodes for
609 * /all/ IBSS SSIDs, versus just the one we care about.
611 * So this check ensures the node can actually belong to our IBSS
612 * configuration. For now it simply checks the SSID.
615 ieee80211_ibss_node_check_new(struct ieee80211_node *ni,
616 const struct ieee80211_scanparams *scan)
618 struct ieee80211vap *vap = ni->ni_vap;
622 * If we have no SSID and no scan SSID, return OK.
624 if (vap->iv_des_nssid == 0 && scan->ssid == NULL)
628 * If we have one of (SSID, scan SSID) then return error.
630 if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL))
634 * Double-check - we need scan SSID.
636 if (scan->ssid == NULL)
640 * Check if the scan SSID matches the SSID list for the VAP.
642 for (i = 0; i < vap->iv_des_nssid; i++) {
644 /* Sanity length check */
645 if (vap->iv_des_ssid[i].len != scan->ssid[1])
648 /* Note: SSID in the scan entry is the IE format */
649 if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2,
650 vap->iv_des_ssid[i].len) == 0)
661 * Handle 802.11 ad hoc network merge. The
662 * convention, set by the Wireless Ethernet Compatibility Alliance
663 * (WECA), is that an 802.11 station will change its BSSID to match
664 * the "oldest" 802.11 ad hoc network, on the same channel, that
665 * has the station's desired SSID. The "oldest" 802.11 network
666 * sends beacons with the greatest TSF timestamp.
668 * The caller is assumed to validate TSF's before attempting a merge.
670 * Return !0 if the BSSID changed, 0 otherwise.
673 ieee80211_ibss_merge(struct ieee80211_node *ni)
675 #ifdef IEEE80211_DEBUG
676 struct ieee80211vap *vap = ni->ni_vap;
677 struct ieee80211com *ic = ni->ni_ic;
680 if (! ieee80211_ibss_merge_check(ni))
683 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
684 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
685 ether_sprintf(ni->ni_bssid),
686 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
687 vap->iv_flags&IEEE80211_F_SHSLOT ? "short" : "long",
688 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
690 return ieee80211_sta_join1(ieee80211_ref_node(ni));
694 * Calculate HT channel promotion flags for all vaps.
695 * This assumes ni_chan have been setup for each vap.
698 gethtadjustflags(struct ieee80211com *ic)
700 struct ieee80211vap *vap;
705 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
706 if (vap->iv_state < IEEE80211_S_RUN)
708 switch (vap->iv_opmode) {
709 case IEEE80211_M_WDS:
710 case IEEE80211_M_STA:
711 case IEEE80211_M_AHDEMO:
712 case IEEE80211_M_HOSTAP:
713 case IEEE80211_M_IBSS:
714 case IEEE80211_M_MBSS:
715 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
725 * Calculate VHT channel promotion flags for all vaps.
726 * This assumes ni_chan have been setup for each vap.
729 getvhtadjustflags(struct ieee80211com *ic)
731 struct ieee80211vap *vap;
736 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
737 if (vap->iv_state < IEEE80211_S_RUN)
739 switch (vap->iv_opmode) {
740 case IEEE80211_M_WDS:
741 case IEEE80211_M_STA:
742 case IEEE80211_M_AHDEMO:
743 case IEEE80211_M_HOSTAP:
744 case IEEE80211_M_IBSS:
745 case IEEE80211_M_MBSS:
746 flags |= ieee80211_vhtchanflags(vap->iv_bss->ni_chan);
756 * Check if the current channel needs to change based on whether
757 * any vap's are using HT20/HT40. This is used to sync the state
758 * of ic_curchan after a channel width change on a running vap.
760 * Same applies for VHT.
763 ieee80211_sync_curchan(struct ieee80211com *ic)
765 struct ieee80211_channel *c;
767 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
768 c = ieee80211_vht_adjust_channel(ic, c, getvhtadjustflags(ic));
770 if (c != ic->ic_curchan) {
772 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
773 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
774 IEEE80211_UNLOCK(ic);
775 ic->ic_set_channel(ic);
776 ieee80211_radiotap_chan_change(ic);
782 * Setup the current channel. The request channel may be
783 * promoted if other vap's are operating with HT20/HT40.
786 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
788 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
789 int flags = gethtadjustflags(ic);
791 * Check for channel promotion required to support the
792 * set of running vap's. This assumes we are called
793 * after ni_chan is setup for each vap.
796 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
797 if (flags > ieee80211_htchanflags(c))
798 c = ieee80211_ht_adjust_channel(ic, c, flags);
802 * VHT promotion - this will at least promote to VHT20/40
803 * based on what HT has done; it may further promote the
804 * channel to VHT80 or above.
806 if (ic->ic_vhtcaps != 0) {
807 int flags = getvhtadjustflags(ic);
808 if (flags > ieee80211_vhtchanflags(c))
809 c = ieee80211_vht_adjust_channel(ic, c, flags);
812 ic->ic_bsschan = ic->ic_curchan = c;
813 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
814 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
818 * Change the current channel. The channel change is guaranteed to have
819 * happened before the next state change.
822 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
824 ieee80211_setupcurchan(ic, c);
825 ieee80211_runtask(ic, &ic->ic_chan_task);
829 ieee80211_update_chw(struct ieee80211com *ic)
832 ieee80211_setupcurchan(ic, ic->ic_curchan);
833 ieee80211_runtask(ic, &ic->ic_chw_task);
837 * Join the specified IBSS/BSS network. The node is assumed to
838 * be passed in with a held reference.
841 ieee80211_sta_join1(struct ieee80211_node *selbs)
843 struct ieee80211vap *vap = selbs->ni_vap;
844 struct ieee80211com *ic = selbs->ni_ic;
845 struct ieee80211_node *obss;
849 * Committed to selbs, setup state.
853 * Check if old+new node have the same address in which
854 * case we can reassociate when operating in sta mode.
856 canreassoc = (obss != NULL &&
857 vap->iv_state == IEEE80211_S_RUN &&
858 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
859 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */
861 struct ieee80211_node_table *nt = obss->ni_table;
863 copy_bss(selbs, obss);
864 ieee80211_node_decref(obss); /* iv_bss reference */
866 IEEE80211_NODE_LOCK(nt);
867 node_reclaim(nt, obss); /* station table reference */
868 IEEE80211_NODE_UNLOCK(nt);
870 obss = NULL; /* NB: guard against later use */
874 * Delete unusable rates; we've already checked
875 * that the negotiated rate set is acceptable.
877 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
878 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
880 ieee80211_setcurchan(ic, selbs->ni_chan);
882 * Set the erp state (mostly the slot time) to deal with
883 * the auto-select case; this should be redundant if the
886 ieee80211_vap_reset_erp(vap);
887 ieee80211_wme_initparams(vap);
889 if (vap->iv_opmode == IEEE80211_M_STA) {
892 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
895 * Act as if we received a DEAUTH frame in case we
896 * are invoked from the RUN state. This will cause
897 * us to try to re-authenticate if we are operating
900 ieee80211_new_state(vap, IEEE80211_S_AUTH,
901 IEEE80211_FC0_SUBTYPE_DEAUTH);
904 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
909 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
910 const struct ieee80211_scan_entry *se)
912 struct ieee80211com *ic = vap->iv_ic;
913 struct ieee80211_node *ni;
916 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
923 * Expand scan state into node's format.
924 * XXX may not need all this stuff
926 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
927 ni->ni_esslen = se->se_ssid[1];
928 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
929 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
930 ni->ni_intval = se->se_intval;
931 ni->ni_capinfo = se->se_capinfo;
933 ni->ni_timoff = se->se_timoff;
934 ni->ni_fhdwell = se->se_fhdwell;
935 ni->ni_fhindex = se->se_fhindex;
936 ni->ni_erp = se->se_erp;
937 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
938 ni->ni_noise = se->se_noise;
939 if (vap->iv_opmode == IEEE80211_M_STA) {
940 /* NB: only infrastructure mode requires an associd */
941 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
944 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
945 ieee80211_ies_expand(&ni->ni_ies);
946 #ifdef IEEE80211_SUPPORT_SUPERG
947 if (ni->ni_ies.ath_ie != NULL)
948 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
950 if (ni->ni_ies.htcap_ie != NULL)
951 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
952 if (ni->ni_ies.htinfo_ie != NULL)
953 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
954 #ifdef IEEE80211_SUPPORT_MESH
955 if (ni->ni_ies.meshid_ie != NULL)
956 ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
958 #ifdef IEEE80211_SUPPORT_TDMA
959 if (ni->ni_ies.tdma_ie != NULL)
960 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
962 if (ni->ni_ies.vhtcap_ie != NULL)
963 ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
964 if (ni->ni_ies.vhtopmode_ie != NULL)
965 ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
967 /* XXX parse BSSLOAD IE */
968 /* XXX parse TXPWRENV IE */
969 /* XXX parse APCHANREP IE */
972 vap->iv_dtim_period = se->se_dtimperiod;
973 vap->iv_dtim_count = 0;
975 /* NB: must be after ni_chan is setup */
976 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
978 if (ieee80211_iserp_rateset(&ni->ni_rates))
979 ni->ni_flags |= IEEE80211_NODE_ERP;
982 * Setup HT state for this node if it's available, otherwise
983 * non-STA modes won't pick this state up.
985 * For IBSS and related modes that don't go through an
986 * association request/response, the only appropriate place
987 * to setup the HT state is here.
989 if (ni->ni_ies.htinfo_ie != NULL &&
990 ni->ni_ies.htcap_ie != NULL &&
991 vap->iv_flags_ht & IEEE80211_FHT_HT) {
992 ieee80211_ht_node_init(ni);
993 ieee80211_ht_updateparams(ni,
995 ni->ni_ies.htinfo_ie);
1000 * Setup VHT state for this node if it's available.
1001 * Same as the above.
1003 * For now, don't allow 2GHz VHT operation.
1005 if (ni->ni_ies.vhtopmode_ie != NULL &&
1006 ni->ni_ies.vhtcap_ie != NULL &&
1007 vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1008 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1009 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
1014 ieee80211_vht_node_init(ni);
1015 ieee80211_vht_updateparams(ni,
1016 ni->ni_ies.vhtcap_ie,
1017 ni->ni_ies.vhtopmode_ie);
1018 ieee80211_setup_vht_rates(ni, ni->ni_ies.vhtcap_ie,
1019 ni->ni_ies.vhtopmode_ie);
1024 /* Finally do the node channel change */
1026 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1027 ni->ni_ies.htinfo_ie);
1028 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
1029 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1030 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
1033 /* XXX else check for ath FF? */
1034 /* XXX QoS? Difficult given that WME config is specific to a master */
1036 ieee80211_node_setuptxparms(ni);
1037 ieee80211_ratectl_node_init(ni);
1039 return ieee80211_sta_join1(ieee80211_ref_node(ni));
1043 * Leave the specified IBSS/BSS network. The node is assumed to
1044 * be passed in with a held reference.
1047 ieee80211_sta_leave(struct ieee80211_node *ni)
1049 struct ieee80211com *ic = ni->ni_ic;
1051 ic->ic_node_cleanup(ni);
1052 ieee80211_notify_node_leave(ni);
1056 * Send a deauthenticate frame and drop the station.
1059 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
1061 /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
1062 ieee80211_ref_node(ni);
1063 if (ni->ni_associd != 0)
1064 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1065 ieee80211_node_leave(ni);
1066 ieee80211_free_node(ni);
1069 static struct ieee80211_node *
1070 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1072 struct ieee80211_node *ni;
1074 ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
1075 M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1080 node_init(struct ieee80211_node *ni)
1086 * Initialize an ie blob with the specified data. If previous
1087 * data exists re-use the data block. As a side effect we clear
1088 * all references to specific ie's; the caller is required to
1092 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
1094 /* NB: assumes data+len are the last fields */
1095 memset(ies, 0, offsetof(struct ieee80211_ies, data));
1096 if (ies->data != NULL && ies->len != len) {
1097 /* data size changed */
1098 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1101 if (ies->data == NULL) {
1102 ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
1103 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1104 if (ies->data == NULL) {
1106 /* NB: pointers have already been zero'd above */
1110 memcpy(ies->data, data, len);
1116 * Reclaim storage for an ie blob.
1119 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
1121 if (ies->data != NULL)
1122 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1126 * Expand an ie blob data contents and to fillin individual
1127 * ie pointers. The data blob is assumed to be well-formed;
1128 * we don't do any validity checking of ie lengths.
1131 ieee80211_ies_expand(struct ieee80211_ies *ies)
1140 case IEEE80211_ELEMID_VENDOR:
1143 else if (iswmeoui(ie))
1145 #ifdef IEEE80211_SUPPORT_SUPERG
1146 else if (isatherosoui(ie))
1149 #ifdef IEEE80211_SUPPORT_TDMA
1150 else if (istdmaoui(ie))
1154 case IEEE80211_ELEMID_RSN:
1157 case IEEE80211_ELEMID_HTCAP:
1160 case IEEE80211_ELEMID_HTINFO:
1161 ies->htinfo_ie = ie;
1163 #ifdef IEEE80211_SUPPORT_MESH
1164 case IEEE80211_ELEMID_MESHID:
1165 ies->meshid_ie = ie;
1168 case IEEE80211_ELEMID_VHT_CAP:
1169 ies->vhtcap_ie = ie;
1171 case IEEE80211_ELEMID_VHT_OPMODE:
1172 ies->vhtopmode_ie = ie;
1174 case IEEE80211_ELEMID_VHT_PWR_ENV:
1175 ies->vhtpwrenv_ie = ie;
1177 case IEEE80211_ELEMID_BSSLOAD:
1178 ies->bssload_ie = ie;
1180 case IEEE80211_ELEMID_APCHANREP:
1181 ies->apchanrep_ie = ie;
1190 * Reclaim any resources in a node and reset any critical
1191 * state. Typically nodes are free'd immediately after,
1192 * but in some cases the storage may be reused so we need
1193 * to insure consistent state (should probably fix that).
1196 node_cleanup(struct ieee80211_node *ni)
1198 struct ieee80211vap *vap = ni->ni_vap;
1199 struct ieee80211com *ic = ni->ni_ic;
1202 /* NB: preserve ni_table */
1203 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1204 if (vap->iv_opmode != IEEE80211_M_STA)
1206 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
1207 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
1208 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
1211 * Cleanup any VHT and HT-related state.
1213 if (ni->ni_flags & IEEE80211_NODE_VHT)
1214 ieee80211_vht_node_cleanup(ni);
1215 if (ni->ni_flags & IEEE80211_NODE_HT)
1216 ieee80211_ht_node_cleanup(ni);
1217 #ifdef IEEE80211_SUPPORT_SUPERG
1218 /* Always do FF node cleanup; for A-MSDU */
1219 ieee80211_ff_node_cleanup(ni);
1221 #ifdef IEEE80211_SUPPORT_MESH
1223 * Cleanup any mesh-related state.
1225 if (vap->iv_opmode == IEEE80211_M_MBSS)
1226 ieee80211_mesh_node_cleanup(ni);
1229 * Clear any staging queue entries.
1231 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1234 * Clear AREF flag that marks the authorization refcnt bump
1235 * has happened. This is probably not needed as the node
1236 * should always be removed from the table so not found but
1237 * do it just in case.
1238 * Likewise clear the ASSOCID flag as these flags are intended
1239 * to be managed in tandem.
1241 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1244 * Drain power save queue and, if needed, clear TIM.
1246 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1247 vap->iv_set_tim(ni, 0);
1250 if (ni->ni_challenge != NULL) {
1251 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1252 ni->ni_challenge = NULL;
1255 * Preserve SSID, WPA, and WME ie's so the bss node is
1256 * reusable during a re-auth/re-assoc state transition.
1257 * If we remove these data they will not be recreated
1258 * because they come from a probe-response or beacon frame
1259 * which cannot be expected prior to the association-response.
1260 * This should not be an issue when operating in other modes
1261 * as stations leaving always go through a full state transition
1262 * which will rebuild this state.
1264 * XXX does this leave us open to inheriting old state?
1266 for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1267 if (ni->ni_rxfrag[i] != NULL) {
1268 m_freem(ni->ni_rxfrag[i]);
1269 ni->ni_rxfrag[i] = NULL;
1272 * Must be careful here to remove any key map entry w/o a LOR.
1274 ieee80211_node_delucastkey(ni);
1278 node_free(struct ieee80211_node *ni)
1280 struct ieee80211com *ic = ni->ni_ic;
1282 ieee80211_ratectl_node_deinit(ni);
1283 ic->ic_node_cleanup(ni);
1284 ieee80211_ies_cleanup(&ni->ni_ies);
1285 ieee80211_psq_cleanup(&ni->ni_psq);
1286 IEEE80211_FREE(ni, M_80211_NODE);
1290 node_age(struct ieee80211_node *ni)
1292 struct ieee80211vap *vap = ni->ni_vap;
1295 * Age frames on the power save queue.
1297 if (ieee80211_node_psq_age(ni) != 0 &&
1298 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1299 vap->iv_set_tim(ni, 0);
1301 * Age out HT resources (e.g. frames on the
1302 * A-MPDU reorder queues).
1304 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1305 ieee80211_ht_node_age(ni);
1309 node_getrssi(const struct ieee80211_node *ni)
1311 uint32_t avgrssi = ni->ni_avgrssi;
1314 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1316 rssi = IEEE80211_RSSI_GET(avgrssi);
1317 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1321 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1323 *rssi = node_getrssi(ni);
1324 *noise = ni->ni_noise;
1328 node_getmimoinfo(const struct ieee80211_node *ni,
1329 struct ieee80211_mimo_info *info)
1335 bzero(info, sizeof(*info));
1337 for (i = 0; i < MIN(IEEE80211_MAX_CHAINS, ni->ni_mimo_chains); i++) {
1338 /* Note: for now, just pri20 channel info */
1339 avgrssi = ni->ni_mimo_rssi_ctl[i];
1340 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1341 info->ch[i].rssi[0] = 0;
1343 rssi = IEEE80211_RSSI_GET(avgrssi);
1344 info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1346 info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i];
1349 /* XXX ext radios? */
1355 ieee80211_add_node_nt(struct ieee80211_node_table *nt,
1356 struct ieee80211_node *ni)
1358 struct ieee80211com *ic = nt->nt_ic;
1361 IEEE80211_NODE_LOCK_ASSERT(nt);
1363 hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr);
1364 (void) ic; /* XXX IEEE80211_NODE_HASH */
1365 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1366 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1372 ieee80211_del_node_nt(struct ieee80211_node_table *nt,
1373 struct ieee80211_node *ni)
1376 IEEE80211_NODE_LOCK_ASSERT(nt);
1378 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1379 LIST_REMOVE(ni, ni_hash);
1381 KASSERT(nt->nt_count >= 0,
1382 ("nt_count is negative (%d)!\n", nt->nt_count));
1383 ni->ni_table = NULL;
1386 struct ieee80211_node *
1387 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1388 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1390 struct ieee80211com *ic = nt->nt_ic;
1391 struct ieee80211_node *ni;
1393 ni = ic->ic_node_alloc(vap, macaddr);
1395 vap->iv_stats.is_rx_nodealloc++;
1399 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1400 "%s %p<%s> in %s table\n", __func__, ni,
1401 ether_sprintf(macaddr), nt->nt_name);
1403 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1404 ieee80211_node_initref(ni); /* mark referenced */
1405 ni->ni_chan = IEEE80211_CHAN_ANYC;
1406 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1407 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1408 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1409 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1410 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1411 ni->ni_inact_reload = nt->nt_inact_init;
1412 ni->ni_inact = ni->ni_inact_reload;
1413 ni->ni_ath_defkeyix = 0x7fff;
1414 ieee80211_psq_init(&ni->ni_psq, "unknown");
1415 #ifdef IEEE80211_SUPPORT_MESH
1416 if (vap->iv_opmode == IEEE80211_M_MBSS)
1417 ieee80211_mesh_node_init(vap, ni);
1419 IEEE80211_NODE_LOCK(nt);
1420 ieee80211_add_node_nt(nt, ni);
1423 IEEE80211_NODE_UNLOCK(nt);
1425 /* handle failure; free node state */
1426 if (ic->ic_node_init(ni) != 0) {
1427 vap->iv_stats.is_rx_nodealloc++;
1428 ieee80211_psq_cleanup(&ni->ni_psq);
1429 ieee80211_ratectl_node_deinit(ni);
1430 _ieee80211_free_node(ni);
1434 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1435 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1441 * Craft a temporary node suitable for sending a management frame
1442 * to the specified station. We craft only as much state as we
1443 * need to do the work since the node will be immediately reclaimed
1444 * once the send completes.
1446 struct ieee80211_node *
1447 ieee80211_tmp_node(struct ieee80211vap *vap,
1448 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1450 struct ieee80211com *ic = vap->iv_ic;
1451 struct ieee80211_node *ni;
1453 ni = ic->ic_node_alloc(vap, macaddr);
1455 struct ieee80211_node *bss = vap->iv_bss;
1457 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1458 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1460 ni->ni_table = NULL; /* NB: pedantic */
1461 ni->ni_ic = ic; /* NB: needed to set channel */
1464 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1465 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1466 ieee80211_node_initref(ni); /* mark referenced */
1467 /* NB: required by ieee80211_fix_rate */
1468 ieee80211_node_set_chan(ni, bss->ni_chan);
1469 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1470 IEEE80211_KEYIX_NONE);
1471 ni->ni_txpower = bss->ni_txpower;
1472 /* XXX optimize away */
1473 ieee80211_psq_init(&ni->ni_psq, "unknown");
1475 ieee80211_ratectl_node_init(ni);
1477 /* handle failure; free node state */
1478 if (ic->ic_node_init(ni) != 0) {
1479 vap->iv_stats.is_rx_nodealloc++;
1480 ieee80211_psq_cleanup(&ni->ni_psq);
1481 ieee80211_ratectl_node_deinit(ni);
1482 _ieee80211_free_node(ni);
1488 vap->iv_stats.is_rx_nodealloc++;
1493 struct ieee80211_node *
1494 ieee80211_dup_bss(struct ieee80211vap *vap,
1495 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1497 struct ieee80211com *ic = vap->iv_ic;
1498 struct ieee80211_node *ni;
1500 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1502 struct ieee80211_node *bss = vap->iv_bss;
1504 * Inherit from iv_bss.
1507 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1508 ieee80211_node_set_chan(ni, bss->ni_chan);
1514 * Create a bss node for a legacy WDS vap. The far end does
1515 * not associate so we just create create a new node and
1516 * simulate an association. The caller is responsible for
1517 * installing the node as the bss node and handling any further
1518 * setup work like authorizing the port.
1520 struct ieee80211_node *
1521 ieee80211_node_create_wds(struct ieee80211vap *vap,
1522 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1524 struct ieee80211com *ic = vap->iv_ic;
1525 struct ieee80211_node *ni;
1527 /* XXX check if node already in sta table? */
1528 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1530 ni->ni_wdsvap = vap;
1531 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1533 * Inherit any manually configured settings.
1535 copy_bss(ni, vap->iv_bss);
1536 ieee80211_node_set_chan(ni, chan);
1537 /* NB: propagate ssid so available to WPA supplicant */
1538 ni->ni_esslen = vap->iv_des_ssid[0].len;
1539 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1540 /* NB: no associd for peer */
1542 * There are no management frames to use to
1543 * discover neighbor capabilities, so blindly
1544 * propagate the local configuration.
1546 if (vap->iv_flags & IEEE80211_F_WME)
1547 ni->ni_flags |= IEEE80211_NODE_QOS;
1548 #ifdef IEEE80211_SUPPORT_SUPERG
1549 if (vap->iv_flags & IEEE80211_F_FF)
1550 ni->ni_flags |= IEEE80211_NODE_FF;
1553 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1554 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1556 * Device is HT-capable and HT is enabled for
1557 * the vap; setup HT operation. On return
1558 * ni_chan will be adjusted to an HT channel.
1560 ieee80211_ht_wds_init(ni);
1561 if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1562 printf("%s: TODO: vht_wds_init\n", __func__);
1565 struct ieee80211_channel *c = ni->ni_chan;
1567 * Force a legacy channel to be used.
1569 c = ieee80211_find_channel(ic,
1570 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1571 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1572 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1579 struct ieee80211_node *
1580 #ifdef IEEE80211_DEBUG_REFCNT
1581 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1582 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1584 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1585 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1588 struct ieee80211_node *ni;
1591 IEEE80211_NODE_LOCK_ASSERT(nt);
1593 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1594 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1595 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1596 ieee80211_ref_node(ni); /* mark referenced */
1597 #ifdef IEEE80211_DEBUG_REFCNT
1598 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1599 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1601 ni, ether_sprintf(ni->ni_macaddr),
1602 ieee80211_node_refcnt(ni));
1610 struct ieee80211_node *
1611 #ifdef IEEE80211_DEBUG_REFCNT
1612 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1613 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1615 ieee80211_find_node(struct ieee80211_node_table *nt,
1616 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1619 struct ieee80211_node *ni;
1621 IEEE80211_NODE_LOCK(nt);
1622 ni = ieee80211_find_node_locked(nt, macaddr);
1623 IEEE80211_NODE_UNLOCK(nt);
1627 struct ieee80211_node *
1628 #ifdef IEEE80211_DEBUG_REFCNT
1629 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1630 const struct ieee80211vap *vap,
1631 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1633 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1634 const struct ieee80211vap *vap,
1635 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1638 struct ieee80211_node *ni;
1641 IEEE80211_NODE_LOCK_ASSERT(nt);
1643 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1644 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1645 if (ni->ni_vap == vap &&
1646 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1647 ieee80211_ref_node(ni); /* mark referenced */
1648 #ifdef IEEE80211_DEBUG_REFCNT
1649 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1650 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1652 ni, ether_sprintf(ni->ni_macaddr),
1653 ieee80211_node_refcnt(ni));
1661 struct ieee80211_node *
1662 #ifdef IEEE80211_DEBUG_REFCNT
1663 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1664 const struct ieee80211vap *vap,
1665 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1667 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1668 const struct ieee80211vap *vap,
1669 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1672 struct ieee80211_node *ni;
1674 IEEE80211_NODE_LOCK(nt);
1675 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1676 IEEE80211_NODE_UNLOCK(nt);
1681 * Fake up a node; this handles node discovery in adhoc mode.
1682 * Note that for the driver's benefit we we treat this like
1683 * an association so the driver has an opportunity to setup
1684 * it's private state.
1686 struct ieee80211_node *
1687 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1688 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1690 struct ieee80211_node *ni;
1692 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1693 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1694 ni = ieee80211_dup_bss(vap, macaddr);
1696 struct ieee80211com *ic = vap->iv_ic;
1698 /* XXX no rate negotiation; just dup */
1699 ni->ni_rates = vap->iv_bss->ni_rates;
1700 if (ieee80211_iserp_rateset(&ni->ni_rates))
1701 ni->ni_flags |= IEEE80211_NODE_ERP;
1702 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1704 * In adhoc demo mode there are no management
1705 * frames to use to discover neighbor capabilities,
1706 * so blindly propagate the local configuration
1707 * so we can do interesting things (e.g. use
1708 * WME to disable ACK's).
1713 if (vap->iv_flags & IEEE80211_F_WME)
1714 ni->ni_flags |= IEEE80211_NODE_QOS;
1715 #ifdef IEEE80211_SUPPORT_SUPERG
1716 if (vap->iv_flags & IEEE80211_F_FF)
1717 ni->ni_flags |= IEEE80211_NODE_FF;
1720 ieee80211_node_setuptxparms(ni);
1721 ieee80211_ratectl_node_init(ni);
1724 * XXX TODO: 11n? At least 20MHz, at least A-MPDU RX,
1725 * not A-MPDU TX; not 11n rates, etc. We'll cycle
1726 * that after we hear that we can indeed do 11n
1727 * (either by a beacon frame or by a probe response.)
1731 * This is the first time we see the node.
1733 if (ic->ic_newassoc != NULL)
1734 ic->ic_newassoc(ni, 1);
1737 * Kick off a probe request to the given node;
1738 * we will then use the probe response to update
1739 * 11n/etc configuration state.
1741 * XXX TODO: this isn't guaranteed, and until we get
1742 * a probe response, we won't be able to actually
1743 * do anything 802.11n related to the node.
1744 * So if this does indeed work, maybe we should hold
1745 * off on sending responses until we get the probe
1746 * response, or just default to some sensible subset
1747 * of 802.11n behaviour (eg always allow aggregation
1748 * negotiation TO us, but not FROM us, etc) so we
1749 * aren't entirely busted.
1751 if (vap->iv_opmode == IEEE80211_M_IBSS) {
1752 ieee80211_send_probereq(ni, /* node */
1753 vap->iv_myaddr, /* SA */
1754 ni->ni_macaddr, /* DA */
1755 vap->iv_bss->ni_bssid, /* BSSID */
1756 vap->iv_bss->ni_essid,
1757 vap->iv_bss->ni_esslen); /* SSID */
1760 /* XXX not right for 802.1x/WPA */
1761 ieee80211_node_authorize(ni);
1767 ieee80211_init_neighbor(struct ieee80211_node *ni,
1768 const struct ieee80211_frame *wh,
1769 const struct ieee80211_scanparams *sp)
1771 int do_ht_setup = 0, do_vht_setup = 0;
1773 ni->ni_esslen = sp->ssid[1];
1774 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1775 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1776 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1777 ni->ni_intval = sp->bintval;
1778 ni->ni_capinfo = sp->capinfo;
1779 ni->ni_chan = ni->ni_ic->ic_curchan;
1780 ni->ni_fhdwell = sp->fhdwell;
1781 ni->ni_fhindex = sp->fhindex;
1782 ni->ni_erp = sp->erp;
1783 ni->ni_timoff = sp->timoff;
1784 #ifdef IEEE80211_SUPPORT_MESH
1785 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1786 ieee80211_mesh_init_neighbor(ni, wh, sp);
1788 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1789 ieee80211_ies_expand(&ni->ni_ies);
1790 if (ni->ni_ies.wme_ie != NULL)
1791 ni->ni_flags |= IEEE80211_NODE_QOS;
1793 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1794 #ifdef IEEE80211_SUPPORT_SUPERG
1795 if (ni->ni_ies.ath_ie != NULL)
1796 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1798 if (ni->ni_ies.htcap_ie != NULL)
1799 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1800 if (ni->ni_ies.htinfo_ie != NULL)
1801 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1803 if (ni->ni_ies.vhtcap_ie != NULL)
1804 ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
1805 if (ni->ni_ies.vhtopmode_ie != NULL)
1806 ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
1808 if ((ni->ni_ies.htcap_ie != NULL) &&
1809 (ni->ni_ies.htinfo_ie != NULL) &&
1810 (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1814 if ((ni->ni_ies.vhtcap_ie != NULL) &&
1815 (ni->ni_ies.vhtopmode_ie != NULL) &&
1816 (ni->ni_vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
1822 /* NB: must be after ni_chan is setup */
1823 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1824 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1825 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1828 * If the neighbor is HT compatible, flip that on.
1831 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1832 "%s: doing HT setup\n", __func__);
1833 ieee80211_ht_node_init(ni);
1834 ieee80211_ht_updateparams(ni,
1835 ni->ni_ies.htcap_ie,
1836 ni->ni_ies.htinfo_ie);
1839 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1840 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
1845 ieee80211_vht_node_init(ni);
1846 ieee80211_vht_updateparams(ni,
1847 ni->ni_ies.vhtcap_ie,
1848 ni->ni_ies.vhtopmode_ie);
1849 ieee80211_setup_vht_rates(ni,
1850 ni->ni_ies.vhtcap_ie,
1851 ni->ni_ies.vhtopmode_ie);
1856 * Finally do the channel upgrade/change based
1857 * on the HT/VHT configuration.
1859 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1860 ni->ni_ies.htinfo_ie);
1861 ieee80211_setup_htrates(ni,
1862 ni->ni_ies.htcap_ie,
1863 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1864 ieee80211_setup_basic_htrates(ni,
1865 ni->ni_ies.htinfo_ie);
1867 ieee80211_node_setuptxparms(ni);
1868 ieee80211_ratectl_node_init(ni);
1870 /* Reassociate; we're now 11n/11ac */
1872 * XXX TODO: this is the wrong thing to do -
1873 * we're calling it with isnew=1 so the ath(4)
1874 * driver reinitialises the rate tables.
1875 * This "mostly" works for ath(4), but it won't
1876 * be right for firmware devices which allocate
1879 * So, do we just create a new node and delete
1882 if (ni->ni_ic->ic_newassoc)
1883 ni->ni_ic->ic_newassoc(ni, 1);
1888 * Do node discovery in adhoc mode on receipt of a beacon
1889 * or probe response frame. Note that for the driver's
1890 * benefit we we treat this like an association so the
1891 * driver has an opportunity to setup it's private state.
1893 struct ieee80211_node *
1894 ieee80211_add_neighbor(struct ieee80211vap *vap,
1895 const struct ieee80211_frame *wh,
1896 const struct ieee80211_scanparams *sp)
1898 struct ieee80211_node *ni;
1900 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1901 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1902 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1904 struct ieee80211com *ic = vap->iv_ic;
1906 ieee80211_init_neighbor(ni, wh, sp);
1907 if (ieee80211_iserp_rateset(&ni->ni_rates))
1908 ni->ni_flags |= IEEE80211_NODE_ERP;
1909 ieee80211_node_setuptxparms(ni);
1910 ieee80211_ratectl_node_init(ni);
1911 if (ic->ic_newassoc != NULL)
1912 ic->ic_newassoc(ni, 1);
1913 /* XXX not right for 802.1x/WPA */
1914 ieee80211_node_authorize(ni);
1919 #define IS_PROBEREQ(wh) \
1920 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1921 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1922 #define IS_BCAST_PROBEREQ(wh) \
1923 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1924 ((const struct ieee80211_frame *)(wh))->i_addr3))
1926 static __inline struct ieee80211_node *
1927 _find_rxnode(struct ieee80211_node_table *nt,
1928 const struct ieee80211_frame_min *wh)
1930 if (IS_BCAST_PROBEREQ(wh))
1931 return NULL; /* spam bcast probe req to all vap's */
1932 return ieee80211_find_node_locked(nt, wh->i_addr2);
1936 * Locate the node for sender, track state, and then pass the
1937 * (referenced) node up to the 802.11 layer for its use. Note
1938 * we can return NULL if the sender is not in the table.
1940 struct ieee80211_node *
1941 #ifdef IEEE80211_DEBUG_REFCNT
1942 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1943 const struct ieee80211_frame_min *wh, const char *func, int line)
1945 ieee80211_find_rxnode(struct ieee80211com *ic,
1946 const struct ieee80211_frame_min *wh)
1949 struct ieee80211_node_table *nt;
1950 struct ieee80211_node *ni;
1953 IEEE80211_NODE_LOCK(nt);
1954 ni = _find_rxnode(nt, wh);
1955 IEEE80211_NODE_UNLOCK(nt);
1961 * Like ieee80211_find_rxnode but use the supplied h/w
1962 * key index as a hint to locate the node in the key
1963 * mapping table. If an entry is present at the key
1964 * index we return it; otherwise do a normal lookup and
1965 * update the mapping table if the station has a unicast
1966 * key assigned to it.
1968 struct ieee80211_node *
1969 #ifdef IEEE80211_DEBUG_REFCNT
1970 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1971 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1972 const char *func, int line)
1974 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1975 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1978 struct ieee80211_node_table *nt;
1979 struct ieee80211_node *ni;
1982 IEEE80211_NODE_LOCK(nt);
1983 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1984 ni = nt->nt_keyixmap[keyix];
1988 ni = _find_rxnode(nt, wh);
1989 if (ni != NULL && nt->nt_keyixmap != NULL) {
1991 * If the station has a unicast key cache slot
1992 * assigned update the key->node mapping table.
1994 keyix = ni->ni_ucastkey.wk_rxkeyix;
1995 /* XXX can keyixmap[keyix] != NULL? */
1996 if (keyix < nt->nt_keyixmax &&
1997 nt->nt_keyixmap[keyix] == NULL) {
1998 IEEE80211_DPRINTF(ni->ni_vap,
2000 "%s: add key map entry %p<%s> refcnt %d\n",
2001 __func__, ni, ether_sprintf(ni->ni_macaddr),
2002 ieee80211_node_refcnt(ni)+1);
2003 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
2007 if (IS_BCAST_PROBEREQ(wh))
2008 ni = NULL; /* spam bcast probe req to all vap's */
2010 ieee80211_ref_node(ni);
2012 IEEE80211_NODE_UNLOCK(nt);
2016 #undef IS_BCAST_PROBEREQ
2020 * Return a reference to the appropriate node for sending
2021 * a data frame. This handles node discovery in adhoc networks.
2023 struct ieee80211_node *
2024 #ifdef IEEE80211_DEBUG_REFCNT
2025 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
2026 const uint8_t macaddr[IEEE80211_ADDR_LEN],
2027 const char *func, int line)
2029 ieee80211_find_txnode(struct ieee80211vap *vap,
2030 const uint8_t macaddr[IEEE80211_ADDR_LEN])
2033 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
2034 struct ieee80211_node *ni;
2037 * The destination address should be in the node table
2038 * unless this is a multicast/broadcast frame. We can
2039 * also optimize station mode operation, all frames go
2042 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
2043 IEEE80211_NODE_LOCK(nt);
2044 if (vap->iv_opmode == IEEE80211_M_STA ||
2045 vap->iv_opmode == IEEE80211_M_WDS ||
2046 IEEE80211_IS_MULTICAST(macaddr))
2047 ni = ieee80211_ref_node(vap->iv_bss);
2049 ni = ieee80211_find_node_locked(nt, macaddr);
2050 IEEE80211_NODE_UNLOCK(nt);
2053 if (vap->iv_opmode == IEEE80211_M_IBSS ||
2054 vap->iv_opmode == IEEE80211_M_AHDEMO) {
2056 * In adhoc mode cons up a node for the destination.
2057 * Note that we need an additional reference for the
2058 * caller to be consistent with
2059 * ieee80211_find_node_locked.
2062 * XXX TODO: this doesn't fake up 11n state; we need
2063 * to find another way to get it upgraded.
2065 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
2067 (void) ieee80211_ref_node(ni);
2069 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
2070 "no node, discard frame (%s)", __func__);
2071 vap->iv_stats.is_tx_nonode++;
2078 _ieee80211_free_node(struct ieee80211_node *ni)
2080 struct ieee80211_node_table *nt = ni->ni_table;
2083 * NB: careful about referencing the vap as it may be
2084 * gone if the last reference was held by a driver.
2085 * We know the com will always be present so it's safe
2086 * to use ni_ic below to reclaim resources.
2089 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2090 "%s %p<%s> in %s table\n", __func__, ni,
2091 ether_sprintf(ni->ni_macaddr),
2092 nt != NULL ? nt->nt_name : "<gone>");
2094 if (ni->ni_associd != 0) {
2095 struct ieee80211vap *vap = ni->ni_vap;
2096 if (vap->iv_aid_bitmap != NULL)
2097 IEEE80211_AID_CLR(vap, ni->ni_associd);
2100 ieee80211_del_node_nt(nt, ni);
2101 ni->ni_ic->ic_node_free(ni);
2105 * Clear any entry in the unicast key mapping table.
2108 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2110 ieee80211_keyix keyix;
2112 keyix = ni->ni_ucastkey.wk_rxkeyix;
2113 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
2114 nt->nt_keyixmap[keyix] == ni) {
2115 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2116 "%s: %p<%s> clear key map entry %u\n",
2117 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
2118 nt->nt_keyixmap[keyix] = NULL;
2119 ieee80211_node_decref(ni);
2127 #ifdef IEEE80211_DEBUG_REFCNT
2128 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
2130 ieee80211_free_node(struct ieee80211_node *ni)
2133 struct ieee80211_node_table *nt = ni->ni_table;
2135 #ifdef IEEE80211_DEBUG_REFCNT
2136 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2137 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
2138 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
2141 IEEE80211_NODE_LOCK(nt);
2142 if (ieee80211_node_dectestref(ni)) {
2144 * Last reference, reclaim state.
2146 _ieee80211_free_node(ni);
2147 } else if (ieee80211_node_refcnt(ni) == 1)
2148 if (node_clear_keyixmap(nt, ni))
2149 _ieee80211_free_node(ni);
2150 IEEE80211_NODE_UNLOCK(nt);
2152 if (ieee80211_node_dectestref(ni))
2153 _ieee80211_free_node(ni);
2158 * Reclaim a unicast key and clear any key cache state.
2161 ieee80211_node_delucastkey(struct ieee80211_node *ni)
2163 struct ieee80211com *ic = ni->ni_ic;
2164 struct ieee80211_node_table *nt = &ic->ic_sta;
2165 struct ieee80211_node *nikey;
2166 ieee80211_keyix keyix;
2167 int isowned, status;
2170 * NB: We must beware of LOR here; deleting the key
2171 * can cause the crypto layer to block traffic updates
2172 * which can generate a LOR against the node table lock;
2173 * grab it here and stash the key index for our use below.
2175 * Must also beware of recursion on the node table lock.
2176 * When called from node_cleanup we may already have
2177 * the node table lock held. Unfortunately there's no
2178 * way to separate out this path so we must do this
2181 isowned = IEEE80211_NODE_IS_LOCKED(nt);
2183 IEEE80211_NODE_LOCK(nt);
2185 status = 1; /* NB: success */
2186 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
2187 keyix = ni->ni_ucastkey.wk_rxkeyix;
2188 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
2189 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
2190 nikey = nt->nt_keyixmap[keyix];
2191 nt->nt_keyixmap[keyix] = NULL;
2195 IEEE80211_NODE_UNLOCK(nt);
2197 if (nikey != NULL) {
2198 KASSERT(nikey == ni,
2199 ("key map out of sync, ni %p nikey %p", ni, nikey));
2200 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2201 "%s: delete key map entry %p<%s> refcnt %d\n",
2202 __func__, ni, ether_sprintf(ni->ni_macaddr),
2203 ieee80211_node_refcnt(ni)-1);
2204 ieee80211_free_node(ni);
2210 * Reclaim a node. If this is the last reference count then
2211 * do the normal free work. Otherwise remove it from the node
2212 * table and mark it gone by clearing the back-reference.
2215 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2218 IEEE80211_NODE_LOCK_ASSERT(nt);
2220 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2221 "%s: remove %p<%s> from %s table, refcnt %d\n",
2222 __func__, ni, ether_sprintf(ni->ni_macaddr),
2223 nt->nt_name, ieee80211_node_refcnt(ni)-1);
2225 * Clear any entry in the unicast key mapping table.
2226 * We need to do it here so rx lookups don't find it
2227 * in the mapping table even if it's not in the hash
2228 * table. We cannot depend on the mapping table entry
2229 * being cleared because the node may not be free'd.
2231 (void)node_clear_keyixmap(nt, ni);
2232 if (!ieee80211_node_dectestref(ni)) {
2234 * Other references are present, just remove the
2235 * node from the table so it cannot be found. When
2236 * the references are dropped storage will be
2239 ieee80211_del_node_nt(nt, ni);
2241 _ieee80211_free_node(ni);
2245 * Node table support.
2249 ieee80211_node_table_init(struct ieee80211com *ic,
2250 struct ieee80211_node_table *nt,
2251 const char *name, int inact, int keyixmax)
2255 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
2256 TAILQ_INIT(&nt->nt_node);
2259 nt->nt_inact_init = inact;
2260 nt->nt_keyixmax = keyixmax;
2261 if (nt->nt_keyixmax > 0) {
2262 nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
2263 keyixmax * sizeof(struct ieee80211_node *),
2265 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2266 if (nt->nt_keyixmap == NULL)
2268 "Cannot allocate key index map with %u entries\n",
2271 nt->nt_keyixmap = NULL;
2275 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
2276 struct ieee80211vap *match)
2278 struct ieee80211_node *ni, *next;
2280 IEEE80211_NODE_LOCK(nt);
2281 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
2282 if (match != NULL && ni->ni_vap != match)
2284 /* XXX can this happen? if so need's work */
2285 if (ni->ni_associd != 0) {
2286 struct ieee80211vap *vap = ni->ni_vap;
2288 if (vap->iv_auth->ia_node_leave != NULL)
2289 vap->iv_auth->ia_node_leave(ni);
2290 if (vap->iv_aid_bitmap != NULL)
2291 IEEE80211_AID_CLR(vap, ni->ni_associd);
2293 ni->ni_wdsvap = NULL; /* clear reference */
2294 node_reclaim(nt, ni);
2296 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
2298 * Make a separate pass to clear references to this vap
2299 * held by DWDS entries. They will not be matched above
2300 * because ni_vap will point to the ap vap but we still
2301 * need to clear ni_wdsvap when the WDS vap is destroyed
2304 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
2305 if (ni->ni_wdsvap == match)
2306 ni->ni_wdsvap = NULL;
2308 IEEE80211_NODE_UNLOCK(nt);
2312 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2314 ieee80211_node_table_reset(nt, NULL);
2315 if (nt->nt_keyixmap != NULL) {
2317 /* XXX verify all entries are NULL */
2319 for (i = 0; i < nt->nt_keyixmax; i++)
2320 if (nt->nt_keyixmap[i] != NULL)
2321 printf("%s: %s[%u] still active\n", __func__,
2324 IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
2325 nt->nt_keyixmap = NULL;
2327 IEEE80211_NODE_LOCK_DESTROY(nt);
2331 timeout_stations(void *arg __unused, struct ieee80211_node *ni)
2333 struct ieee80211com *ic = ni->ni_ic;
2334 struct ieee80211vap *vap = ni->ni_vap;
2337 * Only process stations when in RUN state. This
2338 * insures, for example, that we don't timeout an
2339 * inactive station during CAC. Note that CSA state
2340 * is actually handled in ieee80211_node_timeout as
2341 * it applies to more than timeout processing.
2343 if (vap->iv_state != IEEE80211_S_RUN)
2346 * Ignore entries for which have yet to receive an
2347 * authentication frame. These are transient and
2348 * will be reclaimed when the last reference to them
2349 * goes away (when frame xmits complete).
2351 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2352 vap->iv_opmode == IEEE80211_M_STA) &&
2353 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2356 * Free fragment if not needed anymore
2357 * (last fragment older than 1s).
2358 * XXX doesn't belong here, move to node_age
2360 if (ni->ni_rxfrag[0] != NULL &&
2361 ticks > ni->ni_rxfragstamp + hz) {
2362 m_freem(ni->ni_rxfrag[0]);
2363 ni->ni_rxfrag[0] = NULL;
2365 if (ni->ni_inact > 0) {
2367 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2368 "%s: inact %u inact_reload %u nrates %u",
2369 __func__, ni->ni_inact, ni->ni_inact_reload,
2370 ni->ni_rates.rs_nrates);
2373 * Special case ourself; we may be idle for extended periods
2374 * of time and regardless reclaiming our state is wrong.
2375 * XXX run ic_node_age
2377 /* XXX before inact decrement? */
2378 if (ni == vap->iv_bss)
2380 if (ni->ni_associd != 0 ||
2381 (vap->iv_opmode == IEEE80211_M_IBSS ||
2382 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2384 * Age/drain resources held by the station.
2386 ic->ic_node_age(ni);
2388 * Probe the station before time it out. We
2389 * send a null data frame which may not be
2390 * universally supported by drivers (need it
2391 * for ps-poll support so it should be...).
2393 * XXX don't probe the station unless we've
2394 * received a frame from them (and have
2395 * some idea of the rates they are capable
2396 * of); this will get fixed more properly
2397 * soon with better handling of the rate set.
2399 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2400 (0 < ni->ni_inact &&
2401 ni->ni_inact <= vap->iv_inact_probe) &&
2402 ni->ni_rates.rs_nrates != 0) {
2404 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2406 "probe station due to inactivity");
2408 * Grab a reference so the node cannot
2409 * be reclaimed before we send the frame.
2410 * ieee80211_send_nulldata understands
2411 * we've done this and reclaims the
2412 * ref for us as needed.
2414 /* XXX fix this (not required anymore). */
2415 ieee80211_ref_node(ni);
2417 ieee80211_send_nulldata(ni);
2422 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2423 ni->ni_inact <= 0) {
2425 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2426 "station timed out due to inactivity "
2427 "(refcnt %u)", ieee80211_node_refcnt(ni));
2429 * Send a deauthenticate frame and drop the station.
2430 * This is somewhat complicated due to reference counts
2431 * and locking. At this point a station will typically
2432 * have a reference count of 2. ieee80211_node_leave
2433 * will do a "free" of the node which will drop the
2434 * reference count. But in the meantime a reference
2435 * wil be held by the deauth frame. The actual reclaim
2436 * of the node will happen either after the tx is
2437 * completed or by ieee80211_node_leave.
2439 if (ni->ni_associd != 0) {
2440 IEEE80211_SEND_MGMT(ni,
2441 IEEE80211_FC0_SUBTYPE_DEAUTH,
2442 IEEE80211_REASON_AUTH_EXPIRE);
2444 ieee80211_node_leave(ni);
2445 vap->iv_stats.is_node_timeout++;
2450 * Timeout inactive stations and do related housekeeping.
2453 ieee80211_timeout_stations(struct ieee80211com *ic)
2455 struct ieee80211_node_table *nt = &ic->ic_sta;
2457 ieee80211_iterate_nodes(nt, timeout_stations, NULL);
2461 * Aggressively reclaim resources. This should be used
2462 * only in a critical situation to reclaim mbuf resources.
2465 ieee80211_drain(struct ieee80211com *ic)
2467 struct ieee80211_node_table *nt = &ic->ic_sta;
2468 struct ieee80211vap *vap;
2469 struct ieee80211_node *ni;
2471 IEEE80211_NODE_LOCK(nt);
2472 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2474 * Ignore entries for which have yet to receive an
2475 * authentication frame. These are transient and
2476 * will be reclaimed when the last reference to them
2477 * goes away (when frame xmits complete).
2481 * Only process stations when in RUN state. This
2482 * insures, for example, that we don't timeout an
2483 * inactive station during CAC. Note that CSA state
2484 * is actually handled in ieee80211_node_timeout as
2485 * it applies to more than timeout processing.
2487 if (vap->iv_state != IEEE80211_S_RUN)
2489 /* XXX can vap be NULL? */
2490 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2491 vap->iv_opmode == IEEE80211_M_STA) &&
2492 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2496 * XXX doesn't belong here, move to node_drain
2498 if (ni->ni_rxfrag[0] != NULL) {
2499 m_freem(ni->ni_rxfrag[0]);
2500 ni->ni_rxfrag[0] = NULL;
2503 * Drain resources held by the station.
2505 ic->ic_node_drain(ni);
2507 IEEE80211_NODE_UNLOCK(nt);
2511 * Per-ieee80211com inactivity timer callback.
2514 ieee80211_node_timeout(void *arg)
2516 struct ieee80211com *ic = arg;
2519 * Defer timeout processing if a channel switch is pending.
2520 * We typically need to be mute so not doing things that
2521 * might generate frames is good to handle in one place.
2522 * Suppressing the station timeout processing may extend the
2523 * lifetime of inactive stations (by not decrementing their
2524 * idle counters) but this should be ok unless the CSA is
2525 * active for an unusually long time.
2527 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2528 ieee80211_scan_timeout(ic);
2529 ieee80211_timeout_stations(ic);
2530 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2533 ieee80211_erp_timeout(ic);
2534 ieee80211_ht_timeout(ic);
2535 ieee80211_vht_timeout(ic);
2536 IEEE80211_UNLOCK(ic);
2538 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2539 ieee80211_node_timeout, ic);
2543 * The same as ieee80211_iterate_nodes(), but for one vap only.
2546 ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
2547 struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
2549 struct ieee80211_node **ni_arr;
2550 struct ieee80211_node *ni;
2555 * Iterate over the node table and save an array of ref'ed nodes.
2557 * This is separated out from calling the actual node function so that
2558 * no LORs will occur.
2560 IEEE80211_NODE_LOCK(nt);
2561 count = nt->nt_count;
2562 size = count * sizeof(struct ieee80211_node *);
2563 ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
2564 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2565 if (ni_arr == NULL) {
2566 IEEE80211_NODE_UNLOCK(nt);
2571 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2572 if (vap != NULL && ni->ni_vap != vap)
2575 ("node array overflow (vap %p, i %d, count %d)\n",
2577 ni_arr[i] = ieee80211_ref_node(ni);
2580 IEEE80211_NODE_UNLOCK(nt);
2582 for (i = 0; i < count; i++) {
2583 if (ni_arr[i] == NULL) /* end of the list */
2585 (*f)(arg, ni_arr[i]);
2586 /* ieee80211_free_node() locks by itself */
2587 ieee80211_free_node(ni_arr[i]);
2590 IEEE80211_FREE(ni_arr, M_80211_NODE);
2596 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2597 * reference in the source.
2600 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2601 ieee80211_iter_func *f, void *arg)
2603 /* XXX no way to pass error to the caller. */
2604 (void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
2608 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2610 printf("0x%p: mac %s refcnt %d\n", ni,
2611 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2612 printf("\tauthmode %u flags 0x%x\n",
2613 ni->ni_authmode, ni->ni_flags);
2614 printf("\tassocid 0x%x txpower %u vlan %u\n",
2615 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2616 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2617 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2618 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2619 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2620 ni->ni_rxfragstamp);
2621 printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2622 node_getrssi(ni), ni->ni_noise,
2623 ni->ni_intval, ni->ni_capinfo);
2624 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2625 ether_sprintf(ni->ni_bssid),
2626 ni->ni_esslen, ni->ni_essid,
2627 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2628 printf("\tinact %u inact_reload %u txrate %u\n",
2629 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2630 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2631 ni->ni_htcap, ni->ni_htparam,
2632 ni->ni_htctlchan, ni->ni_ht2ndchan);
2633 printf("\thtopmode %x htstbc %x htchw %u\n",
2634 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2635 printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n",
2636 ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2,
2637 (int) ni->ni_vht_basicmcs);
2642 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2644 ieee80211_iterate_nodes(nt,
2645 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2649 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2651 struct ieee80211vap *vap;
2653 IEEE80211_LOCK_ASSERT(ic);
2655 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2656 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2657 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2661 ieee80211_notify_erp(struct ieee80211com *ic)
2664 ieee80211_notify_erp_locked(ic);
2665 IEEE80211_UNLOCK(ic);
2669 * Handle a station joining an 11g network.
2672 ieee80211_node_join_11g(struct ieee80211_node *ni)
2674 struct ieee80211com *ic = ni->ni_ic;
2675 struct ieee80211vap *vap = ni->ni_vap;
2677 IEEE80211_LOCK_ASSERT(ic);
2680 * Station isn't capable of short slot time. Bump
2681 * the count of long slot time stations and disable
2682 * use of short slot time. Note that the actual switch
2683 * over to long slot time use may not occur until the
2684 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2686 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2687 ic->ic_longslotsta++;
2688 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2689 "station needs long slot time, count %d",
2690 ic->ic_longslotsta);
2691 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2693 * Don't force slot time when switched to turbo
2694 * mode as non-ERP stations won't be present; this
2695 * need only be done when on the normal G channel.
2697 ieee80211_vap_set_shortslottime(vap, 0);
2701 * If the new station is not an ERP station
2702 * then bump the counter and enable protection
2705 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2707 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2708 "station is !ERP, %d non-ERP stations associated",
2711 * If station does not support short preamble
2712 * then we must enable use of Barker preamble.
2714 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2715 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2716 "%s", "station needs long preamble");
2717 ic->ic_flags |= IEEE80211_F_USEBARKER;
2718 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2721 * If protection is configured and this is the first
2722 * indication we should use protection, enable it.
2724 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2725 ic->ic_nonerpsta == 1 &&
2726 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2727 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2728 "%s: enable use of protection\n", __func__);
2729 ic->ic_flags |= IEEE80211_F_USEPROT;
2730 ieee80211_notify_erp_locked(ic);
2733 ni->ni_flags |= IEEE80211_NODE_ERP;
2737 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2739 struct ieee80211com *ic = ni->ni_ic;
2740 struct ieee80211vap *vap = ni->ni_vap;
2743 if (ni->ni_associd == 0) {
2746 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2748 * It would be good to search the bitmap
2749 * more efficiently, but this will do for now.
2751 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2752 if (!IEEE80211_AID_ISSET(vap, aid))
2755 if (aid >= vap->iv_max_aid) {
2756 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2757 ieee80211_node_leave(ni);
2760 ni->ni_associd = aid | 0xc000;
2761 ni->ni_jointime = time_uptime;
2763 IEEE80211_AID_SET(vap, ni->ni_associd);
2764 vap->iv_sta_assoc++;
2767 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2768 ieee80211_ht_node_join(ni);
2769 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2770 ieee80211_vht_node_join(ni);
2771 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2772 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2773 ieee80211_node_join_11g(ni);
2774 IEEE80211_UNLOCK(ic);
2781 * XXX VHT - should log VHT channel width, etc
2783 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2784 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s%s",
2785 IEEE80211_NODE_AID(ni),
2786 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2787 vap->iv_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2788 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2789 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2790 /* XXX update for VHT string */
2791 ni->ni_flags & IEEE80211_NODE_HT ?
2792 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2793 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2794 ni->ni_flags & IEEE80211_NODE_AMSDU ? " (+AMSDU)" : "",
2795 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2796 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2797 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2798 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2799 ", fast-frames" : "",
2800 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2804 ieee80211_node_setuptxparms(ni);
2805 ieee80211_ratectl_node_init(ni);
2806 /* give driver a chance to setup state like ni_txrate */
2807 if (ic->ic_newassoc != NULL)
2808 ic->ic_newassoc(ni, newassoc);
2809 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2810 /* tell the authenticator about new station */
2811 if (vap->iv_auth->ia_node_join != NULL)
2812 vap->iv_auth->ia_node_join(ni);
2813 ieee80211_notify_node_join(ni,
2814 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2818 disable_protection(struct ieee80211com *ic)
2820 KASSERT(ic->ic_nonerpsta == 0 &&
2821 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2822 ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2825 ic->ic_flags &= ~IEEE80211_F_USEPROT;
2826 /* XXX verify mode? */
2827 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2828 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2829 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2831 ieee80211_notify_erp_locked(ic);
2835 * Handle a station leaving an 11g network.
2838 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2840 struct ieee80211com *ic = ni->ni_ic;
2841 struct ieee80211vap *vap = ni->ni_vap;
2843 IEEE80211_LOCK_ASSERT(ic);
2845 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2846 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2847 ic->ic_bsschan->ic_flags));
2850 * If a long slot station do the slot time bookkeeping.
2852 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2853 KASSERT(ic->ic_longslotsta > 0,
2854 ("bogus long slot station count %d", ic->ic_longslotsta));
2855 ic->ic_longslotsta--;
2856 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2857 "long slot time station leaves, count now %d",
2858 ic->ic_longslotsta);
2859 if (ic->ic_longslotsta == 0) {
2861 * Re-enable use of short slot time if supported
2862 * and not operating in IBSS mode (per spec).
2864 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2865 ic->ic_opmode != IEEE80211_M_IBSS) {
2866 IEEE80211_DPRINTF(ni->ni_vap,
2867 IEEE80211_MSG_ASSOC,
2868 "%s: re-enable use of short slot time\n",
2870 ieee80211_vap_set_shortslottime(vap, 1);
2875 * If a non-ERP station do the protection-related bookkeeping.
2877 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2878 KASSERT(ic->ic_nonerpsta > 0,
2879 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2881 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2882 "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2883 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2884 " (non-ERP sta present)" : "");
2885 if (ic->ic_nonerpsta == 0 &&
2886 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2887 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2888 "%s: disable use of protection\n", __func__);
2889 disable_protection(ic);
2895 * Time out presence of an overlapping bss with non-ERP
2896 * stations. When operating in hostap mode we listen for
2897 * beacons from other stations and if we identify a non-ERP
2898 * station is present we enable protection. To identify
2899 * when all non-ERP stations are gone we time out this
2903 ieee80211_erp_timeout(struct ieee80211com *ic)
2906 IEEE80211_LOCK_ASSERT(ic);
2908 if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2909 ieee80211_time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2911 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2912 "%s", "age out non-ERP sta present on channel");
2914 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2915 if (ic->ic_nonerpsta == 0)
2916 disable_protection(ic);
2921 * Handle bookkeeping for station deauthentication/disassociation
2922 * when operating as an ap.
2925 ieee80211_node_leave(struct ieee80211_node *ni)
2927 struct ieee80211com *ic = ni->ni_ic;
2928 struct ieee80211vap *vap = ni->ni_vap;
2929 struct ieee80211_node_table *nt = ni->ni_table;
2931 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2932 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2934 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2935 ("unexpected operating mode %u", vap->iv_opmode));
2937 * If node wasn't previously associated all
2938 * we need to do is reclaim the reference.
2940 /* XXX ibss mode bypasses 11g and notification */
2941 if (ni->ni_associd == 0)
2944 * Tell the authenticator the station is leaving.
2945 * Note that we must do this before yanking the
2946 * association id as the authenticator uses the
2947 * associd to locate it's state block.
2949 if (vap->iv_auth->ia_node_leave != NULL)
2950 vap->iv_auth->ia_node_leave(ni);
2953 IEEE80211_AID_CLR(vap, ni->ni_associd);
2954 vap->iv_sta_assoc--;
2957 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2958 ieee80211_vht_node_leave(ni);
2959 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2960 ieee80211_ht_node_leave(ni);
2961 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2962 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2963 ieee80211_node_leave_11g(ni);
2964 IEEE80211_UNLOCK(ic);
2966 * Cleanup station state. In particular clear various
2967 * state that might otherwise be reused if the node
2968 * is reused before the reference count goes to zero
2969 * (and memory is reclaimed).
2971 ieee80211_sta_leave(ni);
2974 * Remove the node from any table it's recorded in and
2975 * drop the caller's reference. Removal from the table
2976 * is important to insure the node is not reprocessed
2980 IEEE80211_NODE_LOCK(nt);
2981 node_reclaim(nt, ni);
2982 IEEE80211_NODE_UNLOCK(nt);
2984 ieee80211_free_node(ni);
2989 uint32_t rssi_total;
2993 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2995 struct rssiinfo *info = arg;
2996 struct ieee80211vap *vap = ni->ni_vap;
2999 /* only associated stations */
3000 if (ni->ni_associd == 0)
3002 rssi = vap->iv_ic->ic_node_getrssi(ni);
3004 info->rssi_samples++;
3005 info->rssi_total += rssi;
3010 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
3012 struct rssiinfo *info = arg;
3013 struct ieee80211vap *vap = ni->ni_vap;
3016 /* only neighbors */
3017 /* XXX check bssid */
3018 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
3020 rssi = vap->iv_ic->ic_node_getrssi(ni);
3022 info->rssi_samples++;
3023 info->rssi_total += rssi;
3027 #ifdef IEEE80211_SUPPORT_MESH
3029 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
3031 struct rssiinfo *info = arg;
3032 struct ieee80211vap *vap = ni->ni_vap;
3035 /* only neighbors that peered successfully */
3036 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
3038 rssi = vap->iv_ic->ic_node_getrssi(ni);
3040 info->rssi_samples++;
3041 info->rssi_total += rssi;
3044 #endif /* IEEE80211_SUPPORT_MESH */
3047 ieee80211_getrssi(struct ieee80211vap *vap)
3049 #define NZ(x) ((x) == 0 ? 1 : (x))
3050 struct ieee80211com *ic = vap->iv_ic;
3051 struct rssiinfo info;
3053 info.rssi_total = 0;
3054 info.rssi_samples = 0;
3055 switch (vap->iv_opmode) {
3056 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
3057 case IEEE80211_M_AHDEMO: /* average of all neighbors */
3058 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
3061 case IEEE80211_M_HOSTAP: /* average of all associated stations */
3062 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
3065 #ifdef IEEE80211_SUPPORT_MESH
3066 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
3067 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
3071 case IEEE80211_M_MONITOR: /* XXX */
3072 case IEEE80211_M_STA: /* use stats from associated ap */
3074 if (vap->iv_bss != NULL)
3075 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
3076 info.rssi_samples = 1;
3079 return info.rssi_total / NZ(info.rssi_samples);
3084 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
3087 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
3089 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
3090 /* for non-station mode return avg'd rssi accounting */
3091 if (vap->iv_opmode != IEEE80211_M_STA)
3092 *rssi = ieee80211_getrssi(vap);