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_vap_erp_timeout(struct ieee80211vap *);
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 net80211_get_random_bytes(ni->ni_bssid,
368 /* clear group bit, add local bit */
369 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
371 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
372 if (vap->iv_flags & IEEE80211_F_DESBSSID)
373 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
375 #ifdef IEEE80211_SUPPORT_TDMA
376 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
378 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
379 #ifdef IEEE80211_SUPPORT_MESH
380 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
381 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
382 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
386 * Fix the channel and related attributes.
388 /* clear DFS CAC state on previous channel */
389 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
390 ic->ic_bsschan->ic_freq != chan->ic_freq &&
391 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
392 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
393 ic->ic_bsschan = chan;
394 ieee80211_node_set_chan(ni, chan);
395 ic->ic_curmode = ieee80211_chan2mode(chan);
397 * Do mode-specific setup.
399 if (IEEE80211_IS_CHAN_FULL(chan)) {
400 if (IEEE80211_IS_CHAN_ANYG(chan)) {
402 * Use a mixed 11b/11g basic rate set.
404 ieee80211_setbasicrates(&ni->ni_rates,
406 if (vap->iv_flags & IEEE80211_F_PUREG) {
408 * Also mark OFDM rates basic so 11b
409 * stations do not join (WiFi compliance).
411 ieee80211_addbasicrates(&ni->ni_rates,
414 } else if (IEEE80211_IS_CHAN_B(chan)) {
416 * Force pure 11b rate set.
418 ieee80211_setbasicrates(&ni->ni_rates,
423 /* XXX TODO: other bits and pieces - eg fast-frames? */
425 /* If we're an 11n channel then initialise the 11n bits */
426 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
428 ieee80211_ht_node_init(ni);
429 ieee80211_vht_node_init(ni);
430 } else if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
432 ieee80211_ht_node_init(ni);
435 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
439 * Reset bss state on transition to the INIT state.
440 * Clear any stations from the table (they have been
441 * deauth'd) and reset the bss node (clears key, rate
445 ieee80211_reset_bss(struct ieee80211vap *vap)
447 struct ieee80211com *ic = vap->iv_ic;
448 struct ieee80211_node *ni, *obss;
450 ieee80211_node_table_reset(&ic->ic_sta, vap);
451 /* XXX multi-bss: wrong */
452 ieee80211_vap_reset_erp(vap);
454 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
455 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
457 vap->iv_bss = ieee80211_ref_node(ni);
460 ni->ni_intval = ic->ic_bintval;
461 ieee80211_free_node(obss);
463 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
467 match_ssid(const struct ieee80211_node *ni,
468 int nssid, const struct ieee80211_scan_ssid ssids[])
472 for (i = 0; i < nssid; i++) {
473 if (ni->ni_esslen == ssids[i].len &&
474 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
481 * Test a node for suitability/compatibility.
484 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
486 struct ieee80211com *ic = ni->ni_ic;
489 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
491 if (vap->iv_opmode == IEEE80211_M_IBSS) {
492 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
495 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
498 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
499 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
502 /* XXX does this mean privacy is supported or required? */
503 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
506 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
507 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
508 if (rate & IEEE80211_RATE_BASIC)
510 if (vap->iv_des_nssid != 0 &&
511 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
513 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
514 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
519 #ifdef IEEE80211_DEBUG
521 * Display node suitability/compatibility.
524 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
526 struct ieee80211com *ic = ni->ni_ic;
531 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
533 if (vap->iv_opmode == IEEE80211_M_IBSS) {
534 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
537 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
540 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
541 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
544 /* XXX does this mean privacy is supported or required? */
545 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
548 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
549 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
550 if (rate & IEEE80211_RATE_BASIC)
552 if (vap->iv_des_nssid != 0 &&
553 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
555 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
556 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
559 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
560 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
562 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
563 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
564 fail & 0x08 ? '!' : ' ');
566 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
567 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
569 fail & 0x02 ? '!' : ' ');
571 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
572 fail & 0x04 ? '!' : ' ');
573 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
574 printf("%s\n", fail & 0x10 ? "!" : "");
576 #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++) {
643 /* Sanity length check */
644 if (vap->iv_des_ssid[i].len != scan->ssid[1])
647 /* Note: SSID in the scan entry is the IE format */
648 if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2,
649 vap->iv_des_ssid[i].len) == 0)
660 * Handle 802.11 ad hoc network merge. The
661 * convention, set by the Wireless Ethernet Compatibility Alliance
662 * (WECA), is that an 802.11 station will change its BSSID to match
663 * the "oldest" 802.11 ad hoc network, on the same channel, that
664 * has the station's desired SSID. The "oldest" 802.11 network
665 * sends beacons with the greatest TSF timestamp.
667 * The caller is assumed to validate TSF's before attempting a merge.
669 * Return !0 if the BSSID changed, 0 otherwise.
672 ieee80211_ibss_merge(struct ieee80211_node *ni)
674 #ifdef IEEE80211_DEBUG
675 struct ieee80211vap *vap = ni->ni_vap;
678 if (! ieee80211_ibss_merge_check(ni))
681 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
682 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
683 ether_sprintf(ni->ni_bssid),
684 vap->iv_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
685 vap->iv_flags&IEEE80211_F_SHSLOT ? "short" : "long",
686 vap->iv_flags&IEEE80211_F_USEPROT ? ", protection" : ""
688 return ieee80211_sta_join1(ieee80211_ref_node(ni));
692 * Calculate HT channel promotion flags for all vaps.
693 * This assumes ni_chan have been setup for each vap.
696 gethtadjustflags(struct ieee80211com *ic)
698 struct ieee80211vap *vap;
703 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
704 if (vap->iv_state < IEEE80211_S_RUN)
706 switch (vap->iv_opmode) {
707 case IEEE80211_M_WDS:
708 case IEEE80211_M_STA:
709 case IEEE80211_M_AHDEMO:
710 case IEEE80211_M_HOSTAP:
711 case IEEE80211_M_IBSS:
712 case IEEE80211_M_MBSS:
713 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
723 * Calculate VHT channel promotion flags for all vaps.
724 * This assumes ni_chan have been setup for each vap.
727 getvhtadjustflags(struct ieee80211com *ic)
729 struct ieee80211vap *vap;
734 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
735 if (vap->iv_state < IEEE80211_S_RUN)
737 switch (vap->iv_opmode) {
738 case IEEE80211_M_WDS:
739 case IEEE80211_M_STA:
740 case IEEE80211_M_AHDEMO:
741 case IEEE80211_M_HOSTAP:
742 case IEEE80211_M_IBSS:
743 case IEEE80211_M_MBSS:
744 flags |= ieee80211_vhtchanflags(vap->iv_bss->ni_chan);
754 * Check if the current channel needs to change based on whether
755 * any vap's are using HT20/HT40. This is used to sync the state
756 * of ic_curchan after a channel width change on a running vap.
758 * Same applies for VHT.
761 ieee80211_sync_curchan(struct ieee80211com *ic)
763 struct ieee80211_channel *c;
765 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
766 c = ieee80211_vht_adjust_channel(ic, c, getvhtadjustflags(ic));
768 if (c != ic->ic_curchan) {
770 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
771 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
772 IEEE80211_UNLOCK(ic);
773 ic->ic_set_channel(ic);
774 ieee80211_radiotap_chan_change(ic);
780 * Setup the current channel. The request channel may be
781 * promoted if other vap's are operating with HT20/HT40.
784 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
786 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
787 int flags = gethtadjustflags(ic);
789 * Check for channel promotion required to support the
790 * set of running vap's. This assumes we are called
791 * after ni_chan is setup for each vap.
794 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
795 if (flags > ieee80211_htchanflags(c))
796 c = ieee80211_ht_adjust_channel(ic, c, flags);
800 * VHT promotion - this will at least promote to VHT20/40
801 * based on what HT has done; it may further promote the
802 * channel to VHT80 or above.
804 if (ic->ic_vhtcaps != 0) {
805 int flags = getvhtadjustflags(ic);
806 if (flags > ieee80211_vhtchanflags(c))
807 c = ieee80211_vht_adjust_channel(ic, c, flags);
810 ic->ic_bsschan = ic->ic_curchan = c;
811 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
812 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
816 * Change the current channel. The channel change is guaranteed to have
817 * happened before the next state change.
820 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
822 ieee80211_setupcurchan(ic, c);
823 ieee80211_runtask(ic, &ic->ic_chan_task);
827 ieee80211_update_chw(struct ieee80211com *ic)
830 ieee80211_setupcurchan(ic, ic->ic_curchan);
831 ieee80211_runtask(ic, &ic->ic_chw_task);
835 * Join the specified IBSS/BSS network. The node is assumed to
836 * be passed in with a held reference.
839 ieee80211_sta_join1(struct ieee80211_node *selbs)
841 struct ieee80211vap *vap = selbs->ni_vap;
842 struct ieee80211com *ic = selbs->ni_ic;
843 struct ieee80211_node *obss;
847 * Committed to selbs, setup state.
851 * Check if old+new node have the same address in which
852 * case we can reassociate when operating in sta mode.
854 canreassoc = (obss != NULL &&
855 vap->iv_state == IEEE80211_S_RUN &&
856 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
857 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */
859 struct ieee80211_node_table *nt = obss->ni_table;
861 copy_bss(selbs, obss);
862 ieee80211_node_decref(obss); /* iv_bss reference */
864 IEEE80211_NODE_LOCK(nt);
865 node_reclaim(nt, obss); /* station table reference */
866 IEEE80211_NODE_UNLOCK(nt);
868 obss = NULL; /* NB: guard against later use */
872 * Delete unusable rates; we've already checked
873 * that the negotiated rate set is acceptable.
875 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
876 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
878 ieee80211_setcurchan(ic, selbs->ni_chan);
880 * Set the erp state (mostly the slot time) to deal with
881 * the auto-select case; this should be redundant if the
884 ieee80211_vap_reset_erp(vap);
885 ieee80211_wme_initparams(vap);
887 if (vap->iv_opmode == IEEE80211_M_STA) {
890 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
893 * Act as if we received a DEAUTH frame in case we
894 * are invoked from the RUN state. This will cause
895 * us to try to re-authenticate if we are operating
898 ieee80211_new_state(vap, IEEE80211_S_AUTH,
899 IEEE80211_FC0_SUBTYPE_DEAUTH);
902 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
907 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
908 const struct ieee80211_scan_entry *se)
910 struct ieee80211com *ic = vap->iv_ic;
911 struct ieee80211_node *ni;
914 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
921 * Expand scan state into node's format.
922 * XXX may not need all this stuff
924 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
925 ni->ni_esslen = se->se_ssid[1];
926 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
927 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
928 ni->ni_intval = se->se_intval;
929 ni->ni_capinfo = se->se_capinfo;
931 ni->ni_timoff = se->se_timoff;
932 ni->ni_fhdwell = se->se_fhdwell;
933 ni->ni_fhindex = se->se_fhindex;
934 ni->ni_erp = se->se_erp;
935 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
936 ni->ni_noise = se->se_noise;
937 if (vap->iv_opmode == IEEE80211_M_STA) {
938 /* NB: only infrastructure mode requires an associd */
939 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
942 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
943 ieee80211_ies_expand(&ni->ni_ies);
944 #ifdef IEEE80211_SUPPORT_SUPERG
945 if (ni->ni_ies.ath_ie != NULL)
946 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
948 if (ni->ni_ies.htcap_ie != NULL)
949 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
950 if (ni->ni_ies.htinfo_ie != NULL)
951 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
952 #ifdef IEEE80211_SUPPORT_MESH
953 if (ni->ni_ies.meshid_ie != NULL)
954 ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
956 #ifdef IEEE80211_SUPPORT_TDMA
957 if (ni->ni_ies.tdma_ie != NULL)
958 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
960 if (ni->ni_ies.vhtcap_ie != NULL)
961 ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
962 if (ni->ni_ies.vhtopmode_ie != NULL)
963 ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
965 /* XXX parse BSSLOAD IE */
966 /* XXX parse TXPWRENV IE */
967 /* XXX parse APCHANREP IE */
970 vap->iv_dtim_period = se->se_dtimperiod;
971 vap->iv_dtim_count = 0;
973 /* NB: must be after ni_chan is setup */
974 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
976 if (ieee80211_iserp_rateset(&ni->ni_rates))
977 ni->ni_flags |= IEEE80211_NODE_ERP;
980 * Setup HT state for this node if it's available, otherwise
981 * non-STA modes won't pick this state up.
983 * For IBSS and related modes that don't go through an
984 * association request/response, the only appropriate place
985 * to setup the HT state is here.
987 if (ni->ni_ies.htinfo_ie != NULL &&
988 ni->ni_ies.htcap_ie != NULL &&
989 vap->iv_flags_ht & IEEE80211_FHT_HT) {
990 ieee80211_ht_node_init(ni);
991 ieee80211_ht_updateparams(ni,
993 ni->ni_ies.htinfo_ie);
998 * Setup VHT state for this node if it's available.
1001 * For now, don't allow 2GHz VHT operation.
1003 if (ni->ni_ies.vhtopmode_ie != NULL &&
1004 ni->ni_ies.vhtcap_ie != NULL &&
1005 vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1006 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1007 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
1012 ieee80211_vht_node_init(ni);
1013 ieee80211_vht_updateparams(ni,
1014 ni->ni_ies.vhtcap_ie,
1015 ni->ni_ies.vhtopmode_ie);
1016 ieee80211_setup_vht_rates(ni, ni->ni_ies.vhtcap_ie,
1017 ni->ni_ies.vhtopmode_ie);
1022 /* Finally do the node channel change */
1024 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1025 ni->ni_ies.htinfo_ie);
1026 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
1027 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1028 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
1031 /* XXX else check for ath FF? */
1032 /* XXX QoS? Difficult given that WME config is specific to a master */
1034 ieee80211_node_setuptxparms(ni);
1035 ieee80211_ratectl_node_init(ni);
1037 return ieee80211_sta_join1(ieee80211_ref_node(ni));
1041 * Leave the specified IBSS/BSS network. The node is assumed to
1042 * be passed in with a held reference.
1045 ieee80211_sta_leave(struct ieee80211_node *ni)
1047 struct ieee80211com *ic = ni->ni_ic;
1049 ic->ic_node_cleanup(ni);
1050 ieee80211_notify_node_leave(ni);
1054 * Send a deauthenticate frame and drop the station.
1057 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
1059 /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
1060 ieee80211_ref_node(ni);
1061 if (ni->ni_associd != 0)
1062 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1063 ieee80211_node_leave(ni);
1064 ieee80211_free_node(ni);
1067 static struct ieee80211_node *
1068 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1070 struct ieee80211_node *ni;
1072 ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
1073 M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1078 node_init(struct ieee80211_node *ni)
1084 * Initialize an ie blob with the specified data. If previous
1085 * data exists re-use the data block. As a side effect we clear
1086 * all references to specific ie's; the caller is required to
1090 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
1092 /* NB: assumes data+len are the last fields */
1093 memset(ies, 0, offsetof(struct ieee80211_ies, data));
1094 if (ies->data != NULL && ies->len != len) {
1095 /* data size changed */
1096 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1099 if (ies->data == NULL) {
1100 ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
1101 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1102 if (ies->data == NULL) {
1104 /* NB: pointers have already been zero'd above */
1108 memcpy(ies->data, data, len);
1114 * Reclaim storage for an ie blob.
1117 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
1119 if (ies->data != NULL)
1120 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1124 * Expand an ie blob data contents and to fillin individual
1125 * ie pointers. The data blob is assumed to be well-formed;
1126 * we don't do any validity checking of ie lengths.
1129 ieee80211_ies_expand(struct ieee80211_ies *ies)
1138 case IEEE80211_ELEMID_VENDOR:
1141 else if (iswmeoui(ie))
1143 #ifdef IEEE80211_SUPPORT_SUPERG
1144 else if (isatherosoui(ie))
1147 #ifdef IEEE80211_SUPPORT_TDMA
1148 else if (istdmaoui(ie))
1152 case IEEE80211_ELEMID_RSN:
1155 case IEEE80211_ELEMID_HTCAP:
1158 case IEEE80211_ELEMID_HTINFO:
1159 ies->htinfo_ie = ie;
1161 #ifdef IEEE80211_SUPPORT_MESH
1162 case IEEE80211_ELEMID_MESHID:
1163 ies->meshid_ie = ie;
1166 case IEEE80211_ELEMID_VHT_CAP:
1167 ies->vhtcap_ie = ie;
1169 case IEEE80211_ELEMID_VHT_OPMODE:
1170 ies->vhtopmode_ie = ie;
1172 case IEEE80211_ELEMID_VHT_PWR_ENV:
1173 ies->vhtpwrenv_ie = ie;
1175 case IEEE80211_ELEMID_BSSLOAD:
1176 ies->bssload_ie = ie;
1178 case IEEE80211_ELEMID_APCHANREP:
1179 ies->apchanrep_ie = ie;
1188 * Reclaim any resources in a node and reset any critical
1189 * state. Typically nodes are free'd immediately after,
1190 * but in some cases the storage may be reused so we need
1191 * to insure consistent state (should probably fix that).
1194 node_cleanup(struct ieee80211_node *ni)
1196 struct ieee80211vap *vap = ni->ni_vap;
1197 struct ieee80211com *ic = ni->ni_ic;
1200 /* NB: preserve ni_table */
1201 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1202 if (vap->iv_opmode != IEEE80211_M_STA)
1204 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
1205 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
1206 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
1209 * Cleanup any VHT and HT-related state.
1211 if (ni->ni_flags & IEEE80211_NODE_VHT)
1212 ieee80211_vht_node_cleanup(ni);
1213 if (ni->ni_flags & IEEE80211_NODE_HT)
1214 ieee80211_ht_node_cleanup(ni);
1215 #ifdef IEEE80211_SUPPORT_SUPERG
1216 /* Always do FF node cleanup; for A-MSDU */
1217 ieee80211_ff_node_cleanup(ni);
1219 #ifdef IEEE80211_SUPPORT_MESH
1221 * Cleanup any mesh-related state.
1223 if (vap->iv_opmode == IEEE80211_M_MBSS)
1224 ieee80211_mesh_node_cleanup(ni);
1227 * Clear any staging queue entries.
1229 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1232 * Clear AREF flag that marks the authorization refcnt bump
1233 * has happened. This is probably not needed as the node
1234 * should always be removed from the table so not found but
1235 * do it just in case.
1236 * Likewise clear the ASSOCID flag as these flags are intended
1237 * to be managed in tandem.
1239 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1242 * Drain power save queue and, if needed, clear TIM.
1244 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1245 vap->iv_set_tim(ni, 0);
1248 if (ni->ni_challenge != NULL) {
1249 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1250 ni->ni_challenge = NULL;
1253 * Preserve SSID, WPA, and WME ie's so the bss node is
1254 * reusable during a re-auth/re-assoc state transition.
1255 * If we remove these data they will not be recreated
1256 * because they come from a probe-response or beacon frame
1257 * which cannot be expected prior to the association-response.
1258 * This should not be an issue when operating in other modes
1259 * as stations leaving always go through a full state transition
1260 * which will rebuild this state.
1262 * XXX does this leave us open to inheriting old state?
1264 for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1265 if (ni->ni_rxfrag[i] != NULL) {
1266 m_freem(ni->ni_rxfrag[i]);
1267 ni->ni_rxfrag[i] = NULL;
1270 * Must be careful here to remove any key map entry w/o a LOR.
1272 ieee80211_node_delucastkey(ni);
1276 node_free(struct ieee80211_node *ni)
1278 struct ieee80211com *ic = ni->ni_ic;
1280 ieee80211_ratectl_node_deinit(ni);
1281 ic->ic_node_cleanup(ni);
1282 ieee80211_ies_cleanup(&ni->ni_ies);
1283 ieee80211_psq_cleanup(&ni->ni_psq);
1284 IEEE80211_FREE(ni, M_80211_NODE);
1288 node_age(struct ieee80211_node *ni)
1290 struct ieee80211vap *vap = ni->ni_vap;
1293 * Age frames on the power save queue.
1295 if (ieee80211_node_psq_age(ni) != 0 &&
1296 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1297 vap->iv_set_tim(ni, 0);
1299 * Age out HT resources (e.g. frames on the
1300 * A-MPDU reorder queues).
1302 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1303 ieee80211_ht_node_age(ni);
1307 node_getrssi(const struct ieee80211_node *ni)
1309 uint32_t avgrssi = ni->ni_avgrssi;
1312 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1314 rssi = IEEE80211_RSSI_GET(avgrssi);
1315 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1319 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1321 *rssi = node_getrssi(ni);
1322 *noise = ni->ni_noise;
1326 node_getmimoinfo(const struct ieee80211_node *ni,
1327 struct ieee80211_mimo_info *info)
1333 bzero(info, sizeof(*info));
1335 for (i = 0; i < MIN(IEEE80211_MAX_CHAINS, ni->ni_mimo_chains); i++) {
1336 /* Note: for now, just pri20 channel info */
1337 avgrssi = ni->ni_mimo_rssi_ctl[i];
1338 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1339 info->ch[i].rssi[0] = 0;
1341 rssi = IEEE80211_RSSI_GET(avgrssi);
1342 info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1344 info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i];
1347 /* XXX ext radios? */
1353 ieee80211_add_node_nt(struct ieee80211_node_table *nt,
1354 struct ieee80211_node *ni)
1356 struct ieee80211com *ic = nt->nt_ic;
1359 IEEE80211_NODE_LOCK_ASSERT(nt);
1361 hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr);
1362 (void) ic; /* XXX IEEE80211_NODE_HASH */
1363 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1364 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1370 ieee80211_del_node_nt(struct ieee80211_node_table *nt,
1371 struct ieee80211_node *ni)
1374 IEEE80211_NODE_LOCK_ASSERT(nt);
1376 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1377 LIST_REMOVE(ni, ni_hash);
1379 KASSERT(nt->nt_count >= 0,
1380 ("nt_count is negative (%d)!\n", nt->nt_count));
1381 ni->ni_table = NULL;
1384 struct ieee80211_node *
1385 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1386 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1388 struct ieee80211com *ic = nt->nt_ic;
1389 struct ieee80211_node *ni;
1391 ni = ic->ic_node_alloc(vap, macaddr);
1393 vap->iv_stats.is_rx_nodealloc++;
1397 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1398 "%s %p<%s> in %s table\n", __func__, ni,
1399 ether_sprintf(macaddr), nt->nt_name);
1401 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1402 ieee80211_node_initref(ni); /* mark referenced */
1403 ni->ni_chan = IEEE80211_CHAN_ANYC;
1404 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1405 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1406 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1407 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1408 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1409 ni->ni_inact_reload = nt->nt_inact_init;
1410 ni->ni_inact = ni->ni_inact_reload;
1411 ni->ni_ath_defkeyix = 0x7fff;
1412 ieee80211_psq_init(&ni->ni_psq, "unknown");
1413 #ifdef IEEE80211_SUPPORT_MESH
1414 if (vap->iv_opmode == IEEE80211_M_MBSS)
1415 ieee80211_mesh_node_init(vap, ni);
1417 IEEE80211_NODE_LOCK(nt);
1418 ieee80211_add_node_nt(nt, ni);
1421 IEEE80211_NODE_UNLOCK(nt);
1423 /* handle failure; free node state */
1424 if (ic->ic_node_init(ni) != 0) {
1425 vap->iv_stats.is_rx_nodealloc++;
1426 ieee80211_psq_cleanup(&ni->ni_psq);
1427 ieee80211_ratectl_node_deinit(ni);
1428 _ieee80211_free_node(ni);
1432 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1433 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1439 * Craft a temporary node suitable for sending a management frame
1440 * to the specified station. We craft only as much state as we
1441 * need to do the work since the node will be immediately reclaimed
1442 * once the send completes.
1444 struct ieee80211_node *
1445 ieee80211_tmp_node(struct ieee80211vap *vap,
1446 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1448 struct ieee80211com *ic = vap->iv_ic;
1449 struct ieee80211_node *ni;
1451 ni = ic->ic_node_alloc(vap, macaddr);
1453 struct ieee80211_node *bss = vap->iv_bss;
1455 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1456 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1458 ni->ni_table = NULL; /* NB: pedantic */
1459 ni->ni_ic = ic; /* NB: needed to set channel */
1462 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1463 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1464 ieee80211_node_initref(ni); /* mark referenced */
1465 /* NB: required by ieee80211_fix_rate */
1466 ieee80211_node_set_chan(ni, bss->ni_chan);
1467 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1468 IEEE80211_KEYIX_NONE);
1469 ni->ni_txpower = bss->ni_txpower;
1470 /* XXX optimize away */
1471 ieee80211_psq_init(&ni->ni_psq, "unknown");
1473 ieee80211_ratectl_node_init(ni);
1475 /* handle failure; free node state */
1476 if (ic->ic_node_init(ni) != 0) {
1477 vap->iv_stats.is_rx_nodealloc++;
1478 ieee80211_psq_cleanup(&ni->ni_psq);
1479 ieee80211_ratectl_node_deinit(ni);
1480 _ieee80211_free_node(ni);
1486 vap->iv_stats.is_rx_nodealloc++;
1491 struct ieee80211_node *
1492 ieee80211_dup_bss(struct ieee80211vap *vap,
1493 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1495 struct ieee80211com *ic = vap->iv_ic;
1496 struct ieee80211_node *ni;
1498 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1500 struct ieee80211_node *bss = vap->iv_bss;
1502 * Inherit from iv_bss.
1505 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1506 ieee80211_node_set_chan(ni, bss->ni_chan);
1512 * Create a bss node for a legacy WDS vap. The far end does
1513 * not associate so we just create create a new node and
1514 * simulate an association. The caller is responsible for
1515 * installing the node as the bss node and handling any further
1516 * setup work like authorizing the port.
1518 struct ieee80211_node *
1519 ieee80211_node_create_wds(struct ieee80211vap *vap,
1520 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1522 struct ieee80211com *ic = vap->iv_ic;
1523 struct ieee80211_node *ni;
1525 /* XXX check if node already in sta table? */
1526 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1528 ni->ni_wdsvap = vap;
1529 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1531 * Inherit any manually configured settings.
1533 copy_bss(ni, vap->iv_bss);
1534 ieee80211_node_set_chan(ni, chan);
1535 /* NB: propagate ssid so available to WPA supplicant */
1536 ni->ni_esslen = vap->iv_des_ssid[0].len;
1537 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1538 /* NB: no associd for peer */
1540 * There are no management frames to use to
1541 * discover neighbor capabilities, so blindly
1542 * propagate the local configuration.
1544 if (vap->iv_flags & IEEE80211_F_WME)
1545 ni->ni_flags |= IEEE80211_NODE_QOS;
1546 #ifdef IEEE80211_SUPPORT_SUPERG
1547 if (vap->iv_flags & IEEE80211_F_FF)
1548 ni->ni_flags |= IEEE80211_NODE_FF;
1551 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1552 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1554 * Device is HT-capable and HT is enabled for
1555 * the vap; setup HT operation. On return
1556 * ni_chan will be adjusted to an HT channel.
1558 ieee80211_ht_wds_init(ni);
1559 if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1560 printf("%s: TODO: vht_wds_init\n", __func__);
1563 struct ieee80211_channel *c = ni->ni_chan;
1565 * Force a legacy channel to be used.
1567 c = ieee80211_find_channel(ic,
1568 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1569 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1570 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1577 struct ieee80211_node *
1578 #ifdef IEEE80211_DEBUG_REFCNT
1579 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1580 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1582 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1583 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1586 struct ieee80211_node *ni;
1589 IEEE80211_NODE_LOCK_ASSERT(nt);
1591 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1592 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1593 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1594 ieee80211_ref_node(ni); /* mark referenced */
1595 #ifdef IEEE80211_DEBUG_REFCNT
1596 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1597 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1599 ni, ether_sprintf(ni->ni_macaddr),
1600 ieee80211_node_refcnt(ni));
1608 struct ieee80211_node *
1609 #ifdef IEEE80211_DEBUG_REFCNT
1610 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1611 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1613 ieee80211_find_node(struct ieee80211_node_table *nt,
1614 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1617 struct ieee80211_node *ni;
1619 IEEE80211_NODE_LOCK(nt);
1620 ni = ieee80211_find_node_locked(nt, macaddr);
1621 IEEE80211_NODE_UNLOCK(nt);
1625 struct ieee80211_node *
1626 #ifdef IEEE80211_DEBUG_REFCNT
1627 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1628 const struct ieee80211vap *vap,
1629 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1631 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1632 const struct ieee80211vap *vap,
1633 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1636 struct ieee80211_node *ni;
1639 IEEE80211_NODE_LOCK_ASSERT(nt);
1641 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1642 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1643 if (ni->ni_vap == vap &&
1644 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1645 ieee80211_ref_node(ni); /* mark referenced */
1646 #ifdef IEEE80211_DEBUG_REFCNT
1647 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1648 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1650 ni, ether_sprintf(ni->ni_macaddr),
1651 ieee80211_node_refcnt(ni));
1659 struct ieee80211_node *
1660 #ifdef IEEE80211_DEBUG_REFCNT
1661 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1662 const struct ieee80211vap *vap,
1663 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1665 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1666 const struct ieee80211vap *vap,
1667 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1670 struct ieee80211_node *ni;
1672 IEEE80211_NODE_LOCK(nt);
1673 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1674 IEEE80211_NODE_UNLOCK(nt);
1679 * Fake up a node; this handles node discovery in adhoc mode.
1680 * Note that for the driver's benefit we we treat this like
1681 * an association so the driver has an opportunity to setup
1682 * it's private state.
1684 struct ieee80211_node *
1685 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1686 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1688 struct ieee80211_node *ni;
1690 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1691 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1692 ni = ieee80211_dup_bss(vap, macaddr);
1694 struct ieee80211com *ic = vap->iv_ic;
1696 /* XXX no rate negotiation; just dup */
1697 ni->ni_rates = vap->iv_bss->ni_rates;
1698 if (ieee80211_iserp_rateset(&ni->ni_rates))
1699 ni->ni_flags |= IEEE80211_NODE_ERP;
1700 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1702 * In adhoc demo mode there are no management
1703 * frames to use to discover neighbor capabilities,
1704 * so blindly propagate the local configuration
1705 * so we can do interesting things (e.g. use
1706 * WME to disable ACK's).
1711 if (vap->iv_flags & IEEE80211_F_WME)
1712 ni->ni_flags |= IEEE80211_NODE_QOS;
1713 #ifdef IEEE80211_SUPPORT_SUPERG
1714 if (vap->iv_flags & IEEE80211_F_FF)
1715 ni->ni_flags |= IEEE80211_NODE_FF;
1718 ieee80211_node_setuptxparms(ni);
1719 ieee80211_ratectl_node_init(ni);
1722 * XXX TODO: 11n? At least 20MHz, at least A-MPDU RX,
1723 * not A-MPDU TX; not 11n rates, etc. We'll cycle
1724 * that after we hear that we can indeed do 11n
1725 * (either by a beacon frame or by a probe response.)
1729 * This is the first time we see the node.
1731 if (ic->ic_newassoc != NULL)
1732 ic->ic_newassoc(ni, 1);
1735 * Kick off a probe request to the given node;
1736 * we will then use the probe response to update
1737 * 11n/etc configuration state.
1739 * XXX TODO: this isn't guaranteed, and until we get
1740 * a probe response, we won't be able to actually
1741 * do anything 802.11n related to the node.
1742 * So if this does indeed work, maybe we should hold
1743 * off on sending responses until we get the probe
1744 * response, or just default to some sensible subset
1745 * of 802.11n behaviour (eg always allow aggregation
1746 * negotiation TO us, but not FROM us, etc) so we
1747 * aren't entirely busted.
1749 if (vap->iv_opmode == IEEE80211_M_IBSS) {
1750 ieee80211_send_probereq(ni, /* node */
1751 vap->iv_myaddr, /* SA */
1752 ni->ni_macaddr, /* DA */
1753 vap->iv_bss->ni_bssid, /* BSSID */
1754 vap->iv_bss->ni_essid,
1755 vap->iv_bss->ni_esslen); /* SSID */
1758 /* XXX not right for 802.1x/WPA */
1759 ieee80211_node_authorize(ni);
1765 ieee80211_init_neighbor(struct ieee80211_node *ni,
1766 const struct ieee80211_frame *wh,
1767 const struct ieee80211_scanparams *sp)
1769 int do_ht_setup = 0, do_vht_setup = 0;
1771 ni->ni_esslen = sp->ssid[1];
1772 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1773 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1774 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1775 ni->ni_intval = sp->bintval;
1776 ni->ni_capinfo = sp->capinfo;
1777 ni->ni_chan = ni->ni_ic->ic_curchan;
1778 ni->ni_fhdwell = sp->fhdwell;
1779 ni->ni_fhindex = sp->fhindex;
1780 ni->ni_erp = sp->erp;
1781 ni->ni_timoff = sp->timoff;
1782 #ifdef IEEE80211_SUPPORT_MESH
1783 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1784 ieee80211_mesh_init_neighbor(ni, wh, sp);
1786 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1787 ieee80211_ies_expand(&ni->ni_ies);
1788 if (ni->ni_ies.wme_ie != NULL)
1789 ni->ni_flags |= IEEE80211_NODE_QOS;
1791 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1792 #ifdef IEEE80211_SUPPORT_SUPERG
1793 if (ni->ni_ies.ath_ie != NULL)
1794 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1796 if (ni->ni_ies.htcap_ie != NULL)
1797 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1798 if (ni->ni_ies.htinfo_ie != NULL)
1799 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1801 if (ni->ni_ies.vhtcap_ie != NULL)
1802 ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
1803 if (ni->ni_ies.vhtopmode_ie != NULL)
1804 ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
1806 if ((ni->ni_ies.htcap_ie != NULL) &&
1807 (ni->ni_ies.htinfo_ie != NULL) &&
1808 (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1812 if ((ni->ni_ies.vhtcap_ie != NULL) &&
1813 (ni->ni_ies.vhtopmode_ie != NULL) &&
1814 (ni->ni_vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
1819 /* NB: must be after ni_chan is setup */
1820 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1821 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1822 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1825 * If the neighbor is HT compatible, flip that on.
1828 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1829 "%s: doing HT setup\n", __func__);
1830 ieee80211_ht_node_init(ni);
1831 ieee80211_ht_updateparams(ni,
1832 ni->ni_ies.htcap_ie,
1833 ni->ni_ies.htinfo_ie);
1836 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1837 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
1842 ieee80211_vht_node_init(ni);
1843 ieee80211_vht_updateparams(ni,
1844 ni->ni_ies.vhtcap_ie,
1845 ni->ni_ies.vhtopmode_ie);
1846 ieee80211_setup_vht_rates(ni,
1847 ni->ni_ies.vhtcap_ie,
1848 ni->ni_ies.vhtopmode_ie);
1853 * Finally do the channel upgrade/change based
1854 * on the HT/VHT configuration.
1856 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1857 ni->ni_ies.htinfo_ie);
1858 ieee80211_setup_htrates(ni,
1859 ni->ni_ies.htcap_ie,
1860 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1861 ieee80211_setup_basic_htrates(ni,
1862 ni->ni_ies.htinfo_ie);
1864 ieee80211_node_setuptxparms(ni);
1865 ieee80211_ratectl_node_init(ni);
1867 /* Reassociate; we're now 11n/11ac */
1869 * XXX TODO: this is the wrong thing to do -
1870 * we're calling it with isnew=1 so the ath(4)
1871 * driver reinitialises the rate tables.
1872 * This "mostly" works for ath(4), but it won't
1873 * be right for firmware devices which allocate
1876 * So, do we just create a new node and delete
1879 if (ni->ni_ic->ic_newassoc)
1880 ni->ni_ic->ic_newassoc(ni, 1);
1885 * Do node discovery in adhoc mode on receipt of a beacon
1886 * or probe response frame. Note that for the driver's
1887 * benefit we we treat this like an association so the
1888 * driver has an opportunity to setup it's private state.
1890 struct ieee80211_node *
1891 ieee80211_add_neighbor(struct ieee80211vap *vap,
1892 const struct ieee80211_frame *wh,
1893 const struct ieee80211_scanparams *sp)
1895 struct ieee80211_node *ni;
1897 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1898 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1899 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1901 struct ieee80211com *ic = vap->iv_ic;
1903 ieee80211_init_neighbor(ni, wh, sp);
1904 if (ieee80211_iserp_rateset(&ni->ni_rates))
1905 ni->ni_flags |= IEEE80211_NODE_ERP;
1906 ieee80211_node_setuptxparms(ni);
1907 ieee80211_ratectl_node_init(ni);
1908 if (ic->ic_newassoc != NULL)
1909 ic->ic_newassoc(ni, 1);
1910 /* XXX not right for 802.1x/WPA */
1911 ieee80211_node_authorize(ni);
1916 #define IS_PROBEREQ(wh) \
1917 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1918 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1919 #define IS_BCAST_PROBEREQ(wh) \
1920 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1921 ((const struct ieee80211_frame *)(wh))->i_addr3))
1923 static __inline struct ieee80211_node *
1924 _find_rxnode(struct ieee80211_node_table *nt,
1925 const struct ieee80211_frame_min *wh)
1927 if (IS_BCAST_PROBEREQ(wh))
1928 return NULL; /* spam bcast probe req to all vap's */
1929 return ieee80211_find_node_locked(nt, wh->i_addr2);
1933 * Locate the node for sender, track state, and then pass the
1934 * (referenced) node up to the 802.11 layer for its use. Note
1935 * we can return NULL if the sender is not in the table.
1937 struct ieee80211_node *
1938 #ifdef IEEE80211_DEBUG_REFCNT
1939 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1940 const struct ieee80211_frame_min *wh, const char *func, int line)
1942 ieee80211_find_rxnode(struct ieee80211com *ic,
1943 const struct ieee80211_frame_min *wh)
1946 struct ieee80211_node_table *nt;
1947 struct ieee80211_node *ni;
1950 IEEE80211_NODE_LOCK(nt);
1951 ni = _find_rxnode(nt, wh);
1952 IEEE80211_NODE_UNLOCK(nt);
1958 * Like ieee80211_find_rxnode but use the supplied h/w
1959 * key index as a hint to locate the node in the key
1960 * mapping table. If an entry is present at the key
1961 * index we return it; otherwise do a normal lookup and
1962 * update the mapping table if the station has a unicast
1963 * key assigned to it.
1965 struct ieee80211_node *
1966 #ifdef IEEE80211_DEBUG_REFCNT
1967 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1968 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1969 const char *func, int line)
1971 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1972 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1975 struct ieee80211_node_table *nt;
1976 struct ieee80211_node *ni;
1979 IEEE80211_NODE_LOCK(nt);
1980 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1981 ni = nt->nt_keyixmap[keyix];
1985 ni = _find_rxnode(nt, wh);
1986 if (ni != NULL && nt->nt_keyixmap != NULL) {
1988 * If the station has a unicast key cache slot
1989 * assigned update the key->node mapping table.
1991 keyix = ni->ni_ucastkey.wk_rxkeyix;
1992 /* XXX can keyixmap[keyix] != NULL? */
1993 if (keyix < nt->nt_keyixmax &&
1994 nt->nt_keyixmap[keyix] == NULL) {
1995 IEEE80211_DPRINTF(ni->ni_vap,
1997 "%s: add key map entry %p<%s> refcnt %d\n",
1998 __func__, ni, ether_sprintf(ni->ni_macaddr),
1999 ieee80211_node_refcnt(ni)+1);
2000 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
2004 if (IS_BCAST_PROBEREQ(wh))
2005 ni = NULL; /* spam bcast probe req to all vap's */
2007 ieee80211_ref_node(ni);
2009 IEEE80211_NODE_UNLOCK(nt);
2013 #undef IS_BCAST_PROBEREQ
2017 * Return a reference to the appropriate node for sending
2018 * a data frame. This handles node discovery in adhoc networks.
2020 struct ieee80211_node *
2021 #ifdef IEEE80211_DEBUG_REFCNT
2022 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
2023 const uint8_t macaddr[IEEE80211_ADDR_LEN],
2024 const char *func, int line)
2026 ieee80211_find_txnode(struct ieee80211vap *vap,
2027 const uint8_t macaddr[IEEE80211_ADDR_LEN])
2030 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
2031 struct ieee80211_node *ni;
2034 * The destination address should be in the node table
2035 * unless this is a multicast/broadcast frame. We can
2036 * also optimize station mode operation, all frames go
2039 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
2040 IEEE80211_NODE_LOCK(nt);
2041 if (vap->iv_opmode == IEEE80211_M_STA ||
2042 vap->iv_opmode == IEEE80211_M_WDS ||
2043 IEEE80211_IS_MULTICAST(macaddr))
2044 ni = ieee80211_ref_node(vap->iv_bss);
2046 ni = ieee80211_find_node_locked(nt, macaddr);
2047 IEEE80211_NODE_UNLOCK(nt);
2050 if (vap->iv_opmode == IEEE80211_M_IBSS ||
2051 vap->iv_opmode == IEEE80211_M_AHDEMO) {
2053 * In adhoc mode cons up a node for the destination.
2054 * Note that we need an additional reference for the
2055 * caller to be consistent with
2056 * ieee80211_find_node_locked.
2059 * XXX TODO: this doesn't fake up 11n state; we need
2060 * to find another way to get it upgraded.
2062 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
2064 (void) ieee80211_ref_node(ni);
2066 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
2067 "no node, discard frame (%s)", __func__);
2068 vap->iv_stats.is_tx_nonode++;
2075 _ieee80211_free_node(struct ieee80211_node *ni)
2077 struct ieee80211_node_table *nt = ni->ni_table;
2080 * NB: careful about referencing the vap as it may be
2081 * gone if the last reference was held by a driver.
2082 * We know the com will always be present so it's safe
2083 * to use ni_ic below to reclaim resources.
2086 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2087 "%s %p<%s> in %s table\n", __func__, ni,
2088 ether_sprintf(ni->ni_macaddr),
2089 nt != NULL ? nt->nt_name : "<gone>");
2091 if (ni->ni_associd != 0) {
2092 struct ieee80211vap *vap = ni->ni_vap;
2093 if (vap->iv_aid_bitmap != NULL)
2094 IEEE80211_AID_CLR(vap, ni->ni_associd);
2097 ieee80211_del_node_nt(nt, ni);
2098 ni->ni_ic->ic_node_free(ni);
2102 * Clear any entry in the unicast key mapping table.
2105 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2107 ieee80211_keyix keyix;
2109 keyix = ni->ni_ucastkey.wk_rxkeyix;
2110 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
2111 nt->nt_keyixmap[keyix] == ni) {
2112 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2113 "%s: %p<%s> clear key map entry %u\n",
2114 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
2115 nt->nt_keyixmap[keyix] = NULL;
2116 ieee80211_node_decref(ni);
2124 #ifdef IEEE80211_DEBUG_REFCNT
2125 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
2127 ieee80211_free_node(struct ieee80211_node *ni)
2130 struct ieee80211_node_table *nt = ni->ni_table;
2132 #ifdef IEEE80211_DEBUG_REFCNT
2133 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2134 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
2135 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
2138 IEEE80211_NODE_LOCK(nt);
2139 if (ieee80211_node_dectestref(ni)) {
2141 * Last reference, reclaim state.
2143 _ieee80211_free_node(ni);
2144 } else if (ieee80211_node_refcnt(ni) == 1)
2145 if (node_clear_keyixmap(nt, ni))
2146 _ieee80211_free_node(ni);
2147 IEEE80211_NODE_UNLOCK(nt);
2149 if (ieee80211_node_dectestref(ni))
2150 _ieee80211_free_node(ni);
2155 * Reclaim a unicast key and clear any key cache state.
2158 ieee80211_node_delucastkey(struct ieee80211_node *ni)
2160 struct ieee80211com *ic = ni->ni_ic;
2161 struct ieee80211_node_table *nt = &ic->ic_sta;
2162 struct ieee80211_node *nikey;
2163 ieee80211_keyix keyix;
2164 int isowned, status;
2167 * NB: We must beware of LOR here; deleting the key
2168 * can cause the crypto layer to block traffic updates
2169 * which can generate a LOR against the node table lock;
2170 * grab it here and stash the key index for our use below.
2172 * Must also beware of recursion on the node table lock.
2173 * When called from node_cleanup we may already have
2174 * the node table lock held. Unfortunately there's no
2175 * way to separate out this path so we must do this
2178 isowned = IEEE80211_NODE_IS_LOCKED(nt);
2180 IEEE80211_NODE_LOCK(nt);
2182 status = 1; /* NB: success */
2183 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
2184 keyix = ni->ni_ucastkey.wk_rxkeyix;
2185 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
2186 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
2187 nikey = nt->nt_keyixmap[keyix];
2188 nt->nt_keyixmap[keyix] = NULL;
2192 IEEE80211_NODE_UNLOCK(nt);
2194 if (nikey != NULL) {
2195 KASSERT(nikey == ni,
2196 ("key map out of sync, ni %p nikey %p", ni, nikey));
2197 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2198 "%s: delete key map entry %p<%s> refcnt %d\n",
2199 __func__, ni, ether_sprintf(ni->ni_macaddr),
2200 ieee80211_node_refcnt(ni)-1);
2201 ieee80211_free_node(ni);
2207 * Reclaim a node. If this is the last reference count then
2208 * do the normal free work. Otherwise remove it from the node
2209 * table and mark it gone by clearing the back-reference.
2212 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2215 IEEE80211_NODE_LOCK_ASSERT(nt);
2217 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2218 "%s: remove %p<%s> from %s table, refcnt %d\n",
2219 __func__, ni, ether_sprintf(ni->ni_macaddr),
2220 nt->nt_name, ieee80211_node_refcnt(ni)-1);
2222 * Clear any entry in the unicast key mapping table.
2223 * We need to do it here so rx lookups don't find it
2224 * in the mapping table even if it's not in the hash
2225 * table. We cannot depend on the mapping table entry
2226 * being cleared because the node may not be free'd.
2228 (void)node_clear_keyixmap(nt, ni);
2229 if (!ieee80211_node_dectestref(ni)) {
2231 * Other references are present, just remove the
2232 * node from the table so it cannot be found. When
2233 * the references are dropped storage will be
2236 ieee80211_del_node_nt(nt, ni);
2238 _ieee80211_free_node(ni);
2242 * Node table support.
2246 ieee80211_node_table_init(struct ieee80211com *ic,
2247 struct ieee80211_node_table *nt,
2248 const char *name, int inact, int keyixmax)
2252 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
2253 TAILQ_INIT(&nt->nt_node);
2256 nt->nt_inact_init = inact;
2257 nt->nt_keyixmax = keyixmax;
2258 if (nt->nt_keyixmax > 0) {
2259 nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
2260 keyixmax * sizeof(struct ieee80211_node *),
2262 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2263 if (nt->nt_keyixmap == NULL)
2265 "Cannot allocate key index map with %u entries\n",
2268 nt->nt_keyixmap = NULL;
2272 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
2273 struct ieee80211vap *match)
2275 struct ieee80211_node *ni, *next;
2277 IEEE80211_NODE_LOCK(nt);
2278 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
2279 if (match != NULL && ni->ni_vap != match)
2281 /* XXX can this happen? if so need's work */
2282 if (ni->ni_associd != 0) {
2283 struct ieee80211vap *vap = ni->ni_vap;
2285 if (vap->iv_auth->ia_node_leave != NULL)
2286 vap->iv_auth->ia_node_leave(ni);
2287 if (vap->iv_aid_bitmap != NULL)
2288 IEEE80211_AID_CLR(vap, ni->ni_associd);
2290 ni->ni_wdsvap = NULL; /* clear reference */
2291 node_reclaim(nt, ni);
2293 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
2295 * Make a separate pass to clear references to this vap
2296 * held by DWDS entries. They will not be matched above
2297 * because ni_vap will point to the ap vap but we still
2298 * need to clear ni_wdsvap when the WDS vap is destroyed
2301 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
2302 if (ni->ni_wdsvap == match)
2303 ni->ni_wdsvap = NULL;
2305 IEEE80211_NODE_UNLOCK(nt);
2309 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2311 ieee80211_node_table_reset(nt, NULL);
2312 if (nt->nt_keyixmap != NULL) {
2314 /* XXX verify all entries are NULL */
2316 for (i = 0; i < nt->nt_keyixmax; i++)
2317 if (nt->nt_keyixmap[i] != NULL)
2318 printf("%s: %s[%u] still active\n", __func__,
2321 IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
2322 nt->nt_keyixmap = NULL;
2324 IEEE80211_NODE_LOCK_DESTROY(nt);
2328 timeout_stations(void *arg __unused, struct ieee80211_node *ni)
2330 struct ieee80211com *ic = ni->ni_ic;
2331 struct ieee80211vap *vap = ni->ni_vap;
2334 * Only process stations when in RUN state. This
2335 * insures, for example, that we don't timeout an
2336 * inactive station during CAC. Note that CSA state
2337 * is actually handled in ieee80211_node_timeout as
2338 * it applies to more than timeout processing.
2340 if (vap->iv_state != IEEE80211_S_RUN)
2343 * Ignore entries for which have yet to receive an
2344 * authentication frame. These are transient and
2345 * will be reclaimed when the last reference to them
2346 * goes away (when frame xmits complete).
2348 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2349 vap->iv_opmode == IEEE80211_M_STA) &&
2350 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2353 * Free fragment if not needed anymore
2354 * (last fragment older than 1s).
2355 * XXX doesn't belong here, move to node_age
2357 if (ni->ni_rxfrag[0] != NULL &&
2358 ticks > ni->ni_rxfragstamp + hz) {
2359 m_freem(ni->ni_rxfrag[0]);
2360 ni->ni_rxfrag[0] = NULL;
2362 if (ni->ni_inact > 0) {
2364 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2365 "%s: inact %u inact_reload %u nrates %u",
2366 __func__, ni->ni_inact, ni->ni_inact_reload,
2367 ni->ni_rates.rs_nrates);
2370 * Special case ourself; we may be idle for extended periods
2371 * of time and regardless reclaiming our state is wrong.
2372 * XXX run ic_node_age
2374 /* XXX before inact decrement? */
2375 if (ni == vap->iv_bss)
2377 if (ni->ni_associd != 0 ||
2378 (vap->iv_opmode == IEEE80211_M_IBSS ||
2379 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2381 * Age/drain resources held by the station.
2383 ic->ic_node_age(ni);
2385 * Probe the station before time it out. We
2386 * send a null data frame which may not be
2387 * universally supported by drivers (need it
2388 * for ps-poll support so it should be...).
2390 * XXX don't probe the station unless we've
2391 * received a frame from them (and have
2392 * some idea of the rates they are capable
2393 * of); this will get fixed more properly
2394 * soon with better handling of the rate set.
2396 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2397 (0 < ni->ni_inact &&
2398 ni->ni_inact <= vap->iv_inact_probe) &&
2399 ni->ni_rates.rs_nrates != 0) {
2401 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2403 "probe station due to inactivity");
2405 * Grab a reference so the node cannot
2406 * be reclaimed before we send the frame.
2407 * ieee80211_send_nulldata understands
2408 * we've done this and reclaims the
2409 * ref for us as needed.
2411 /* XXX fix this (not required anymore). */
2412 ieee80211_ref_node(ni);
2414 ieee80211_send_nulldata(ni);
2419 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2420 ni->ni_inact <= 0) {
2422 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2423 "station timed out due to inactivity "
2424 "(refcnt %u)", ieee80211_node_refcnt(ni));
2426 * Send a deauthenticate frame and drop the station.
2427 * This is somewhat complicated due to reference counts
2428 * and locking. At this point a station will typically
2429 * have a reference count of 2. ieee80211_node_leave
2430 * will do a "free" of the node which will drop the
2431 * reference count. But in the meantime a reference
2432 * wil be held by the deauth frame. The actual reclaim
2433 * of the node will happen either after the tx is
2434 * completed or by ieee80211_node_leave.
2436 if (ni->ni_associd != 0) {
2437 IEEE80211_SEND_MGMT(ni,
2438 IEEE80211_FC0_SUBTYPE_DEAUTH,
2439 IEEE80211_REASON_AUTH_EXPIRE);
2441 ieee80211_node_leave(ni);
2442 vap->iv_stats.is_node_timeout++;
2447 * Timeout inactive stations and do related housekeeping.
2450 ieee80211_timeout_stations(struct ieee80211com *ic)
2452 struct ieee80211_node_table *nt = &ic->ic_sta;
2454 ieee80211_iterate_nodes(nt, timeout_stations, NULL);
2458 * Aggressively reclaim resources. This should be used
2459 * only in a critical situation to reclaim mbuf resources.
2462 ieee80211_drain(struct ieee80211com *ic)
2464 struct ieee80211_node_table *nt = &ic->ic_sta;
2465 struct ieee80211vap *vap;
2466 struct ieee80211_node *ni;
2468 IEEE80211_NODE_LOCK(nt);
2469 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2471 * Ignore entries for which have yet to receive an
2472 * authentication frame. These are transient and
2473 * will be reclaimed when the last reference to them
2474 * goes away (when frame xmits complete).
2478 * Only process stations when in RUN state. This
2479 * insures, for example, that we don't timeout an
2480 * inactive station during CAC. Note that CSA state
2481 * is actually handled in ieee80211_node_timeout as
2482 * it applies to more than timeout processing.
2484 if (vap->iv_state != IEEE80211_S_RUN)
2486 /* XXX can vap be NULL? */
2487 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2488 vap->iv_opmode == IEEE80211_M_STA) &&
2489 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2493 * XXX doesn't belong here, move to node_drain
2495 if (ni->ni_rxfrag[0] != NULL) {
2496 m_freem(ni->ni_rxfrag[0]);
2497 ni->ni_rxfrag[0] = NULL;
2500 * Drain resources held by the station.
2502 ic->ic_node_drain(ni);
2504 IEEE80211_NODE_UNLOCK(nt);
2508 * Per-ieee80211vap inactivity timer callback.
2511 ieee80211_vap_timeout(struct ieee80211vap *vap)
2514 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2516 ieee80211_vap_erp_timeout(vap);
2517 ieee80211_ht_timeout(vap);
2518 ieee80211_vht_timeout(vap);
2522 * Per-ieee80211com inactivity timer callback.
2525 ieee80211_node_timeout(void *arg)
2527 struct ieee80211com *ic = arg;
2528 struct ieee80211vap *vap;
2531 * Defer timeout processing if a channel switch is pending.
2532 * We typically need to be mute so not doing things that
2533 * might generate frames is good to handle in one place.
2534 * Suppressing the station timeout processing may extend the
2535 * lifetime of inactive stations (by not decrementing their
2536 * idle counters) but this should be ok unless the CSA is
2537 * active for an unusually long time.
2539 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2540 ieee80211_scan_timeout(ic);
2541 ieee80211_timeout_stations(ic);
2542 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2545 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2546 ieee80211_vap_timeout(vap);
2547 IEEE80211_UNLOCK(ic);
2549 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2550 ieee80211_node_timeout, ic);
2554 * The same as ieee80211_iterate_nodes(), but for one vap only.
2557 ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
2558 struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
2560 struct ieee80211_node **ni_arr;
2561 struct ieee80211_node *ni;
2566 * Iterate over the node table and save an array of ref'ed nodes.
2568 * This is separated out from calling the actual node function so that
2569 * no LORs will occur.
2571 IEEE80211_NODE_LOCK(nt);
2572 count = nt->nt_count;
2573 size = count * sizeof(struct ieee80211_node *);
2574 ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
2575 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2576 if (ni_arr == NULL) {
2577 IEEE80211_NODE_UNLOCK(nt);
2582 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2583 if (vap != NULL && ni->ni_vap != vap)
2586 ("node array overflow (vap %p, i %d, count %d)\n",
2588 ni_arr[i] = ieee80211_ref_node(ni);
2591 IEEE80211_NODE_UNLOCK(nt);
2593 for (i = 0; i < count; i++) {
2594 if (ni_arr[i] == NULL) /* end of the list */
2596 (*f)(arg, ni_arr[i]);
2597 /* ieee80211_free_node() locks by itself */
2598 ieee80211_free_node(ni_arr[i]);
2601 IEEE80211_FREE(ni_arr, M_80211_NODE);
2607 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2608 * reference in the source.
2611 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2612 ieee80211_iter_func *f, void *arg)
2614 /* XXX no way to pass error to the caller. */
2615 (void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
2619 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2621 printf("0x%p: mac %s refcnt %d\n", ni,
2622 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2623 printf("\tauthmode %u flags 0x%x\n",
2624 ni->ni_authmode, ni->ni_flags);
2625 printf("\tassocid 0x%x txpower %u vlan %u\n",
2626 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2627 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2628 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2629 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2630 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2631 ni->ni_rxfragstamp);
2632 printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2633 node_getrssi(ni), ni->ni_noise,
2634 ni->ni_intval, ni->ni_capinfo);
2635 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2636 ether_sprintf(ni->ni_bssid),
2637 ni->ni_esslen, ni->ni_essid,
2638 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2639 printf("\tinact %u inact_reload %u txrate %u\n",
2640 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2641 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2642 ni->ni_htcap, ni->ni_htparam,
2643 ni->ni_htctlchan, ni->ni_ht2ndchan);
2644 printf("\thtopmode %x htstbc %x htchw %u\n",
2645 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2646 printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n",
2647 ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2,
2648 (int) ni->ni_vht_basicmcs);
2653 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2655 ieee80211_iterate_nodes(nt,
2656 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2660 * Iterate over the VAPs and update their ERP beacon IEs.
2662 * Note this must be called from the deferred ERP update task paths.
2665 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2667 struct ieee80211vap *vap;
2669 IEEE80211_LOCK_ASSERT(ic);
2671 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2672 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2673 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2677 * Handle a station joining an 11g network.
2680 ieee80211_node_join_11g(struct ieee80211_node *ni)
2682 struct ieee80211com *ic = ni->ni_ic;
2683 struct ieee80211vap *vap = ni->ni_vap;
2685 IEEE80211_LOCK_ASSERT(ic);
2688 * Station isn't capable of short slot time. Bump
2689 * the count of long slot time stations and disable
2690 * use of short slot time. Note that the actual switch
2691 * over to long slot time use may not occur until the
2692 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2694 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2695 vap->iv_longslotsta++;
2696 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2697 "station needs long slot time, count %d",
2698 vap->iv_longslotsta);
2700 * XXX TODO: this may need all VAPs checked!
2702 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2704 * Don't force slot time when switched to turbo
2705 * mode as non-ERP stations won't be present; this
2706 * need only be done when on the normal G channel.
2708 ieee80211_vap_set_shortslottime(vap, 0);
2712 * If the new station is not an ERP station
2713 * then bump the counter and enable protection
2716 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2717 vap->iv_nonerpsta++;
2718 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2719 "station is !ERP, %d non-ERP stations associated",
2722 * If station does not support short preamble
2723 * then we must enable use of Barker preamble.
2725 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2726 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2727 "%s", "station needs long preamble");
2728 vap->iv_flags |= IEEE80211_F_USEBARKER;
2729 vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE;
2730 ieee80211_vap_update_preamble(vap);
2733 * If protection is configured and this is the first
2734 * indication we should use protection, enable it.
2736 if (vap->iv_protmode != IEEE80211_PROT_NONE &&
2737 vap->iv_nonerpsta == 1 &&
2738 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2739 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2740 "%s: enable use of protection\n", __func__);
2741 vap->iv_flags |= IEEE80211_F_USEPROT;
2742 ieee80211_vap_update_erp_protmode(vap);
2745 ni->ni_flags |= IEEE80211_NODE_ERP;
2749 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2751 struct ieee80211com *ic = ni->ni_ic;
2752 struct ieee80211vap *vap = ni->ni_vap;
2755 if (ni->ni_associd == 0) {
2758 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2760 * It would be good to search the bitmap
2761 * more efficiently, but this will do for now.
2763 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2764 if (!IEEE80211_AID_ISSET(vap, aid))
2767 if (aid >= vap->iv_max_aid) {
2768 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2769 ieee80211_node_leave(ni);
2772 ni->ni_associd = aid | 0xc000;
2773 ni->ni_jointime = time_uptime;
2775 IEEE80211_AID_SET(vap, ni->ni_associd);
2776 vap->iv_sta_assoc++;
2778 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2779 ieee80211_ht_node_join(ni);
2780 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2781 ieee80211_vht_node_join(ni);
2782 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2783 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2784 ieee80211_node_join_11g(ni);
2785 IEEE80211_UNLOCK(ic);
2792 * XXX VHT - should log VHT channel width, etc
2794 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2795 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s%s",
2796 IEEE80211_NODE_AID(ni),
2797 vap->iv_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2798 vap->iv_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2799 vap->iv_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2800 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2801 /* XXX update for VHT string */
2802 ni->ni_flags & IEEE80211_NODE_HT ?
2803 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2804 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2805 ni->ni_flags & IEEE80211_NODE_AMSDU ? " (+AMSDU)" : "",
2806 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2807 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2808 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2809 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2810 ", fast-frames" : "",
2811 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2815 ieee80211_node_setuptxparms(ni);
2816 ieee80211_ratectl_node_init(ni);
2817 /* give driver a chance to setup state like ni_txrate */
2818 if (ic->ic_newassoc != NULL)
2819 ic->ic_newassoc(ni, newassoc);
2820 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2821 /* tell the authenticator about new station */
2822 if (vap->iv_auth->ia_node_join != NULL)
2823 vap->iv_auth->ia_node_join(ni);
2824 ieee80211_notify_node_join(ni,
2825 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2829 disable_protection(struct ieee80211vap *vap)
2831 struct ieee80211com *ic = vap->iv_ic;
2833 KASSERT(vap->iv_nonerpsta == 0 &&
2834 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2835 ("%d non ERP stations, flags 0x%x", vap->iv_nonerpsta,
2836 vap->iv_flags_ext));
2838 vap->iv_flags &= ~IEEE80211_F_USEPROT;
2839 /* XXX verify mode? */
2840 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2841 vap->iv_flags |= IEEE80211_F_SHPREAMBLE;
2842 vap->iv_flags &= ~IEEE80211_F_USEBARKER;
2844 ieee80211_vap_update_erp_protmode(vap);
2845 ieee80211_vap_update_preamble(vap);
2849 * Handle a station leaving an 11g network.
2852 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2854 struct ieee80211com *ic = ni->ni_ic;
2855 struct ieee80211vap *vap = ni->ni_vap;
2857 IEEE80211_LOCK_ASSERT(ic);
2859 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2860 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2861 ic->ic_bsschan->ic_flags));
2864 * If a long slot station do the slot time bookkeeping.
2866 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2867 KASSERT(vap->iv_longslotsta > 0,
2868 ("bogus long slot station count %d", vap->iv_longslotsta));
2869 vap->iv_longslotsta--;
2870 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2871 "long slot time station leaves, count now %d",
2872 vap->iv_longslotsta);
2874 * XXX TODO: this may need all VAPs checked!
2876 if (vap->iv_longslotsta == 0) {
2878 * Re-enable use of short slot time if supported
2879 * and not operating in IBSS mode (per spec).
2881 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2882 ic->ic_opmode != IEEE80211_M_IBSS) {
2883 IEEE80211_DPRINTF(ni->ni_vap,
2884 IEEE80211_MSG_ASSOC,
2885 "%s: re-enable use of short slot time\n",
2887 ieee80211_vap_set_shortslottime(vap, 1);
2892 * If a non-ERP station do the protection-related bookkeeping.
2894 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2895 KASSERT(vap->iv_nonerpsta > 0,
2896 ("bogus non-ERP station count %d", vap->iv_nonerpsta));
2897 vap->iv_nonerpsta--;
2898 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2899 "non-ERP station leaves, count now %d%s", vap->iv_nonerpsta,
2900 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2901 " (non-ERP sta present)" : "");
2902 if (vap->iv_nonerpsta == 0 &&
2903 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2904 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2905 "%s: disable use of protection\n", __func__);
2906 disable_protection(vap);
2912 * Time out presence of an overlapping bss with non-ERP
2913 * stations. When operating in hostap mode we listen for
2914 * beacons from other stations and if we identify a non-ERP
2915 * station is present we enable protection. To identify
2916 * when all non-ERP stations are gone we time out this
2920 ieee80211_vap_erp_timeout(struct ieee80211vap *vap)
2923 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2925 if ((vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2926 ieee80211_time_after(ticks, vap->iv_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2927 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
2928 "%s", "age out non-ERP sta present on channel");
2929 vap->iv_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2930 if (vap->iv_nonerpsta == 0)
2931 disable_protection(vap);
2936 * Handle bookkeeping for station deauthentication/disassociation
2937 * when operating as an ap.
2940 ieee80211_node_leave(struct ieee80211_node *ni)
2942 struct ieee80211com *ic = ni->ni_ic;
2943 struct ieee80211vap *vap = ni->ni_vap;
2944 struct ieee80211_node_table *nt = ni->ni_table;
2946 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2947 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2949 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2950 ("unexpected operating mode %u", vap->iv_opmode));
2952 * If node wasn't previously associated all
2953 * we need to do is reclaim the reference.
2955 /* XXX ibss mode bypasses 11g and notification */
2956 if (ni->ni_associd == 0)
2959 * Tell the authenticator the station is leaving.
2960 * Note that we must do this before yanking the
2961 * association id as the authenticator uses the
2962 * associd to locate it's state block.
2964 if (vap->iv_auth->ia_node_leave != NULL)
2965 vap->iv_auth->ia_node_leave(ni);
2968 IEEE80211_AID_CLR(vap, ni->ni_associd);
2969 vap->iv_sta_assoc--;
2971 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2972 ieee80211_vht_node_leave(ni);
2973 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2974 ieee80211_ht_node_leave(ni);
2975 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2976 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2977 ieee80211_node_leave_11g(ni);
2978 IEEE80211_UNLOCK(ic);
2980 * Cleanup station state. In particular clear various
2981 * state that might otherwise be reused if the node
2982 * is reused before the reference count goes to zero
2983 * (and memory is reclaimed).
2985 ieee80211_sta_leave(ni);
2988 * Remove the node from any table it's recorded in and
2989 * drop the caller's reference. Removal from the table
2990 * is important to insure the node is not reprocessed
2994 IEEE80211_NODE_LOCK(nt);
2995 node_reclaim(nt, ni);
2996 IEEE80211_NODE_UNLOCK(nt);
2998 ieee80211_free_node(ni);
3003 uint32_t rssi_total;
3007 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
3009 struct rssiinfo *info = arg;
3010 struct ieee80211vap *vap = ni->ni_vap;
3013 /* only associated stations */
3014 if (ni->ni_associd == 0)
3016 rssi = vap->iv_ic->ic_node_getrssi(ni);
3018 info->rssi_samples++;
3019 info->rssi_total += rssi;
3024 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
3026 struct rssiinfo *info = arg;
3027 struct ieee80211vap *vap = ni->ni_vap;
3030 /* only neighbors */
3031 /* XXX check bssid */
3032 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
3034 rssi = vap->iv_ic->ic_node_getrssi(ni);
3036 info->rssi_samples++;
3037 info->rssi_total += rssi;
3041 #ifdef IEEE80211_SUPPORT_MESH
3043 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
3045 struct rssiinfo *info = arg;
3046 struct ieee80211vap *vap = ni->ni_vap;
3049 /* only neighbors that peered successfully */
3050 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
3052 rssi = vap->iv_ic->ic_node_getrssi(ni);
3054 info->rssi_samples++;
3055 info->rssi_total += rssi;
3058 #endif /* IEEE80211_SUPPORT_MESH */
3061 ieee80211_getrssi(struct ieee80211vap *vap)
3063 #define NZ(x) ((x) == 0 ? 1 : (x))
3064 struct ieee80211com *ic = vap->iv_ic;
3065 struct rssiinfo info;
3067 info.rssi_total = 0;
3068 info.rssi_samples = 0;
3069 switch (vap->iv_opmode) {
3070 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
3071 case IEEE80211_M_AHDEMO: /* average of all neighbors */
3072 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
3075 case IEEE80211_M_HOSTAP: /* average of all associated stations */
3076 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
3079 #ifdef IEEE80211_SUPPORT_MESH
3080 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
3081 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
3085 case IEEE80211_M_MONITOR: /* XXX */
3086 case IEEE80211_M_STA: /* use stats from associated ap */
3088 if (vap->iv_bss != NULL)
3089 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
3090 info.rssi_samples = 1;
3093 return info.rssi_total / NZ(info.rssi_samples);
3098 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
3101 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
3103 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
3104 /* for non-station mode return avg'd rssi accounting */
3105 if (vap->iv_opmode != IEEE80211_M_STA)
3106 *rssi = ieee80211_getrssi(vap);