2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2001 Atsushi Onoe
5 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
38 #include <sys/socket.h>
41 #include <net/if_var.h>
42 #include <net/if_media.h>
43 #include <net/ethernet.h>
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_input.h>
47 #ifdef IEEE80211_SUPPORT_SUPERG
48 #include <net80211/ieee80211_superg.h>
50 #ifdef IEEE80211_SUPPORT_TDMA
51 #include <net80211/ieee80211_tdma.h>
53 #include <net80211/ieee80211_wds.h>
54 #include <net80211/ieee80211_mesh.h>
55 #include <net80211/ieee80211_ratectl.h>
56 #include <net80211/ieee80211_vht.h>
60 #ifdef IEEE80211_DEBUG_REFCNT
61 #define __debrefcnt_used
63 #define __debrefcnt_used __unused
67 * IEEE80211_NODE_HASHSIZE must be a power of 2.
69 CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
72 * Association id's are managed with a bit vector.
74 #define IEEE80211_AID_SET(_vap, b) \
75 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
76 (1 << (IEEE80211_AID(b) % 32)))
77 #define IEEE80211_AID_CLR(_vap, b) \
78 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
79 ~(1 << (IEEE80211_AID(b) % 32)))
80 #define IEEE80211_AID_ISSET(_vap, b) \
81 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
83 static int ieee80211_sta_join1(struct ieee80211_node *);
85 static struct ieee80211_node *node_alloc(struct ieee80211vap *,
86 const uint8_t [IEEE80211_ADDR_LEN]);
87 static int node_init(struct ieee80211_node *);
88 static void node_cleanup(struct ieee80211_node *);
89 static void node_free(struct ieee80211_node *);
90 static void node_age(struct ieee80211_node *);
91 static int8_t node_getrssi(const struct ieee80211_node *);
92 static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
93 static void node_getmimoinfo(const struct ieee80211_node *,
94 struct ieee80211_mimo_info *);
96 static void __ieee80211_free_node(struct ieee80211_node *);
98 static void node_reclaim(struct ieee80211_node_table *nt,
99 struct ieee80211_node *ni);
100 static void ieee80211_node_table_init(struct ieee80211com *ic,
101 struct ieee80211_node_table *nt, const char *name,
102 int inact, int keymaxix);
103 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
104 struct ieee80211vap *);
105 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
106 static void ieee80211_vap_erp_timeout(struct ieee80211vap *);
108 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
109 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
112 ieee80211_node_attach(struct ieee80211com *ic)
114 /* XXX really want maxlen enforced per-sta */
115 ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
117 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
118 IEEE80211_INACT_INIT, ic->ic_max_keyix);
119 callout_init(&ic->ic_inact, 1);
120 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
121 ieee80211_node_timeout, ic);
123 ic->ic_node_alloc = node_alloc;
124 ic->ic_node_init = node_init;
125 ic->ic_node_free = node_free;
126 ic->ic_node_cleanup = node_cleanup;
127 ic->ic_node_age = node_age;
128 ic->ic_node_drain = node_age; /* NB: same as age */
129 ic->ic_node_getrssi = node_getrssi;
130 ic->ic_node_getsignal = node_getsignal;
131 ic->ic_node_getmimoinfo = node_getmimoinfo;
134 * Set flags to be propagated to all vap's;
135 * these define default behaviour/configuration.
137 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
141 ieee80211_node_detach(struct ieee80211com *ic)
144 callout_drain(&ic->ic_inact);
145 ieee80211_node_table_cleanup(&ic->ic_sta);
146 ieee80211_ageq_drain(&ic->ic_stageq);
147 ieee80211_ageq_cleanup(&ic->ic_stageq);
151 ieee80211_node_vattach(struct ieee80211vap *vap)
153 /* NB: driver can override */
154 vap->iv_max_aid = IEEE80211_AID_DEF;
156 /* default station inactivity timer settings */
157 vap->iv_inact_init = IEEE80211_INACT_INIT;
158 vap->iv_inact_auth = IEEE80211_INACT_AUTH;
159 vap->iv_inact_run = IEEE80211_INACT_RUN;
160 vap->iv_inact_probe = IEEE80211_INACT_PROBE;
162 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
163 "%s: init %u auth %u run %u probe %u\n", __func__,
164 vap->iv_inact_init, vap->iv_inact_auth,
165 vap->iv_inact_run, vap->iv_inact_probe);
169 ieee80211_node_latevattach(struct ieee80211vap *vap)
171 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
172 /* XXX should we allow max aid to be zero? */
173 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
174 vap->iv_max_aid = IEEE80211_AID_MIN;
175 if_printf(vap->iv_ifp,
176 "WARNING: max aid too small, changed to %d\n",
179 vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC(
180 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
182 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
183 if (vap->iv_aid_bitmap == NULL) {
184 /* XXX no way to recover */
185 printf("%s: no memory for AID bitmap, max aid %d!\n",
186 __func__, vap->iv_max_aid);
191 ieee80211_reset_bss(vap);
193 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
197 ieee80211_node_vdetach(struct ieee80211vap *vap)
199 struct ieee80211com *ic = vap->iv_ic;
201 ieee80211_node_table_reset(&ic->ic_sta, vap);
202 if (vap->iv_bss != NULL) {
203 ieee80211_free_node(vap->iv_bss);
204 vap->iv_update_bss(vap, NULL);
206 if (vap->iv_aid_bitmap != NULL) {
207 IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE);
208 vap->iv_aid_bitmap = NULL;
213 * Port authorize/unauthorize interfaces for use by an authenticator.
217 ieee80211_node_authorize(struct ieee80211_node *ni)
219 struct ieee80211vap *vap = ni->ni_vap;
221 ni->ni_flags |= IEEE80211_NODE_AUTH;
222 ni->ni_inact_reload = vap->iv_inact_run;
223 ni->ni_inact = ni->ni_inact_reload;
225 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
226 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
230 ieee80211_node_unauthorize(struct ieee80211_node *ni)
232 struct ieee80211vap *vap = ni->ni_vap;
234 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
235 ni->ni_inact_reload = vap->iv_inact_auth;
236 if (ni->ni_inact > ni->ni_inact_reload)
237 ni->ni_inact = ni->ni_inact_reload;
239 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
240 "%s: inact_reload %u inact %u", __func__,
241 ni->ni_inact_reload, ni->ni_inact);
245 * Fix tx parameters for a node according to ``association state''.
248 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
250 struct ieee80211vap *vap = ni->ni_vap;
251 enum ieee80211_phymode mode;
253 if (ni->ni_flags & IEEE80211_NODE_VHT) {
254 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
255 mode = IEEE80211_MODE_VHT_5GHZ;
257 mode = IEEE80211_MODE_VHT_2GHZ;
258 } else if (ni->ni_flags & IEEE80211_NODE_HT) {
259 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
260 mode = IEEE80211_MODE_11NA;
262 mode = IEEE80211_MODE_11NG;
263 } else { /* legacy rate handling */
264 if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
265 mode = IEEE80211_MODE_STURBO_A;
266 else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
267 mode = IEEE80211_MODE_HALF;
268 else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
269 mode = IEEE80211_MODE_QUARTER;
270 /* NB: 108A should be handled as 11a */
271 else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
272 mode = IEEE80211_MODE_11A;
273 else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
274 (ni->ni_flags & IEEE80211_NODE_ERP))
275 mode = IEEE80211_MODE_11G;
277 mode = IEEE80211_MODE_11B;
279 ni->ni_txparms = &vap->iv_txparms[mode];
283 * Set/change the channel. The rate set is also updated as
284 * to insure a consistent view by drivers.
285 * XXX should be private but hostap needs it to deal with CSA
288 ieee80211_node_set_chan(struct ieee80211_node *ni,
289 struct ieee80211_channel *chan)
291 struct ieee80211com *ic = ni->ni_ic;
292 struct ieee80211vap *vap = ni->ni_vap;
293 enum ieee80211_phymode mode;
295 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
298 mode = ieee80211_chan2mode(chan);
299 if (IEEE80211_IS_CHAN_HT(chan)) {
301 * We must install the legacy rate est in ni_rates and the
302 * HT rate set in ni_htrates.
304 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
306 * Setup bss tx parameters based on operating mode. We
307 * use legacy rates when operating in a mixed HT+non-HT bss
308 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
310 if (mode == IEEE80211_MODE_11NA &&
311 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
312 mode = IEEE80211_MODE_11A;
313 else if (mode == IEEE80211_MODE_11NG &&
314 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
315 mode = IEEE80211_MODE_11G;
316 if (mode == IEEE80211_MODE_11G &&
317 (vap->iv_flags & IEEE80211_F_PUREG) == 0)
318 mode = IEEE80211_MODE_11B;
320 ni->ni_txparms = &vap->iv_txparms[mode];
321 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
325 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
327 /* propagate useful state */
328 nbss->ni_authmode = obss->ni_authmode;
329 nbss->ni_txpower = obss->ni_txpower;
330 nbss->ni_vlan = obss->ni_vlan;
331 /* XXX statistics? */
332 /* XXX legacy WDS bssid? */
336 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
338 struct ieee80211com *ic = vap->iv_ic;
339 struct ieee80211_node *ni;
341 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
342 "%s: creating %s on channel %u%c flags 0x%08x\n", __func__,
343 ieee80211_opmode_name[vap->iv_opmode],
344 ieee80211_chan2ieee(ic, chan),
345 ieee80211_channel_type_char(chan),
348 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
353 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
354 ni->ni_esslen = vap->iv_des_ssid[0].len;
355 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
356 if (vap->iv_bss != NULL)
357 copy_bss(ni, vap->iv_bss);
358 ni->ni_intval = ic->ic_bintval;
359 if (vap->iv_flags & IEEE80211_F_PRIVACY)
360 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
361 if (ic->ic_phytype == IEEE80211_T_FH) {
362 ni->ni_fhdwell = 200; /* XXX */
365 if (vap->iv_opmode == IEEE80211_M_IBSS) {
366 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
367 if (vap->iv_flags & IEEE80211_F_DESBSSID)
368 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
370 net80211_get_random_bytes(ni->ni_bssid,
372 /* clear group bit, add local bit */
373 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
375 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
376 if (vap->iv_flags & IEEE80211_F_DESBSSID)
377 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
379 #ifdef IEEE80211_SUPPORT_TDMA
380 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
382 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
383 #ifdef IEEE80211_SUPPORT_MESH
384 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
385 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
386 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
390 * Fix the channel and related attributes.
392 /* clear DFS CAC state on previous channel */
393 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
394 ic->ic_bsschan->ic_freq != chan->ic_freq &&
395 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
396 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
397 ic->ic_bsschan = chan;
398 ieee80211_node_set_chan(ni, chan);
399 ic->ic_curmode = ieee80211_chan2mode(chan);
401 * Do mode-specific setup.
403 if (IEEE80211_IS_CHAN_FULL(chan)) {
404 if (IEEE80211_IS_CHAN_ANYG(chan)) {
406 * Use a mixed 11b/11g basic rate set.
408 ieee80211_setbasicrates(&ni->ni_rates,
410 if (vap->iv_flags & IEEE80211_F_PUREG) {
412 * Also mark OFDM rates basic so 11b
413 * stations do not join (WiFi compliance).
415 ieee80211_addbasicrates(&ni->ni_rates,
418 } else if (IEEE80211_IS_CHAN_B(chan)) {
420 * Force pure 11b rate set.
422 ieee80211_setbasicrates(&ni->ni_rates,
427 /* XXX TODO: other bits and pieces - eg fast-frames? */
429 /* If we're an 11n channel then initialise the 11n bits */
430 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
432 ieee80211_ht_node_init(ni);
433 ieee80211_vht_node_init(ni);
434 } else if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
436 ieee80211_ht_node_init(ni);
439 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
443 * Reset bss state on transition to the INIT state.
444 * Clear any stations from the table (they have been
445 * deauth'd) and reset the bss node (clears key, rate
449 ieee80211_reset_bss(struct ieee80211vap *vap)
451 struct ieee80211com *ic = vap->iv_ic;
452 struct ieee80211_node *ni, *obss;
454 ieee80211_node_table_reset(&ic->ic_sta, vap);
455 /* XXX multi-bss: wrong */
456 ieee80211_vap_reset_erp(vap);
458 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
459 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
460 obss = vap->iv_update_bss(vap, ieee80211_ref_node(ni));
463 ni->ni_intval = ic->ic_bintval;
464 ieee80211_free_node(obss);
466 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
470 match_ssid(const struct ieee80211_node *ni,
471 int nssid, const struct ieee80211_scan_ssid ssids[])
475 for (i = 0; i < nssid; i++) {
476 if (ni->ni_esslen == ssids[i].len &&
477 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
484 * Test a node for suitability/compatibility.
487 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
489 struct ieee80211com *ic = ni->ni_ic;
492 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
494 if (vap->iv_opmode == IEEE80211_M_IBSS) {
495 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
498 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
501 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
502 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
505 /* XXX does this mean privacy is supported or required? */
506 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
509 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
510 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
511 if (rate & IEEE80211_RATE_BASIC)
513 if (vap->iv_des_nssid != 0 &&
514 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
516 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
517 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
522 #ifdef IEEE80211_DEBUG
524 * Display node suitability/compatibility.
527 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
529 struct ieee80211com *ic = ni->ni_ic;
534 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
536 if (vap->iv_opmode == IEEE80211_M_IBSS) {
537 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
540 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
543 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
544 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
547 /* XXX does this mean privacy is supported or required? */
548 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
551 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
552 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
553 if (rate & IEEE80211_RATE_BASIC)
555 if (vap->iv_des_nssid != 0 &&
556 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
558 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
559 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
562 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
563 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
565 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
566 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
567 fail & 0x08 ? '!' : ' ');
569 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
570 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
572 fail & 0x02 ? '!' : ' ');
574 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
575 fail & 0x04 ? '!' : ' ');
576 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
577 printf("%s\n", fail & 0x10 ? "!" : "");
579 #endif /* IEEE80211_DEBUG */
582 ieee80211_ibss_merge_check(struct ieee80211_node *ni)
584 struct ieee80211vap *vap = ni->ni_vap;
586 if (ni == vap->iv_bss ||
587 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
588 /* unchanged, nothing to do */
592 if (!check_bss(vap, ni)) {
593 /* capabilities mismatch */
594 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
595 "%s: merge failed, capabilities mismatch\n", __func__);
596 #ifdef IEEE80211_DEBUG
597 if (ieee80211_msg_assoc(vap))
598 check_bss_debug(vap, ni);
600 vap->iv_stats.is_ibss_capmismatch++;
608 * Check if the given node should populate the node table.
610 * We need to be in "see all beacons for all ssids" mode in order
611 * to do IBSS merges, however this means we will populate nodes for
612 * /all/ IBSS SSIDs, versus just the one we care about.
614 * So this check ensures the node can actually belong to our IBSS
615 * configuration. For now it simply checks the SSID.
618 ieee80211_ibss_node_check_new(struct ieee80211_node *ni,
619 const struct ieee80211_scanparams *scan)
621 struct ieee80211vap *vap = ni->ni_vap;
625 * If we have no SSID and no scan SSID, return OK.
627 if (vap->iv_des_nssid == 0 && scan->ssid == NULL)
631 * If we have one of (SSID, scan SSID) then return error.
633 if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL))
637 * Double-check - we need scan SSID.
639 if (scan->ssid == NULL)
643 * Check if the scan SSID matches the SSID list for the VAP.
645 for (i = 0; i < vap->iv_des_nssid; i++) {
646 /* Sanity length check */
647 if (vap->iv_des_ssid[i].len != scan->ssid[1])
650 /* Note: SSID in the scan entry is the IE format */
651 if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2,
652 vap->iv_des_ssid[i].len) == 0)
663 * Handle 802.11 ad hoc network merge. The
664 * convention, set by the Wireless Ethernet Compatibility Alliance
665 * (WECA), is that an 802.11 station will change its BSSID to match
666 * the "oldest" 802.11 ad hoc network, on the same channel, that
667 * has the station's desired SSID. The "oldest" 802.11 network
668 * sends beacons with the greatest TSF timestamp.
670 * The caller is assumed to validate TSF's before attempting a merge.
672 * Return !0 if the BSSID changed, 0 otherwise.
675 ieee80211_ibss_merge(struct ieee80211_node *ni)
677 #ifdef IEEE80211_DEBUG
678 struct ieee80211vap *vap = ni->ni_vap;
681 if (! ieee80211_ibss_merge_check(ni))
684 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
685 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
686 ether_sprintf(ni->ni_bssid),
687 vap->iv_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
688 vap->iv_flags&IEEE80211_F_SHSLOT ? "short" : "long",
689 vap->iv_flags&IEEE80211_F_USEPROT ? ", protection" : ""
691 return ieee80211_sta_join1(ieee80211_ref_node(ni));
695 * Calculate HT channel promotion flags for all vaps.
696 * This assumes ni_chan have been setup for each vap.
699 gethtadjustflags(struct ieee80211com *ic)
701 struct ieee80211vap *vap;
706 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
707 if (vap->iv_state < IEEE80211_S_RUN)
709 switch (vap->iv_opmode) {
710 case IEEE80211_M_WDS:
711 case IEEE80211_M_STA:
712 case IEEE80211_M_AHDEMO:
713 case IEEE80211_M_HOSTAP:
714 case IEEE80211_M_IBSS:
715 case IEEE80211_M_MBSS:
716 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
726 * Calculate VHT channel promotion flags for all vaps.
727 * This assumes ni_chan have been setup for each vap.
730 getvhtadjustflags(struct ieee80211com *ic)
732 struct ieee80211vap *vap;
737 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
738 if (vap->iv_state < IEEE80211_S_RUN)
740 switch (vap->iv_opmode) {
741 case IEEE80211_M_WDS:
742 case IEEE80211_M_STA:
743 case IEEE80211_M_AHDEMO:
744 case IEEE80211_M_HOSTAP:
745 case IEEE80211_M_IBSS:
746 case IEEE80211_M_MBSS:
747 flags |= ieee80211_vhtchanflags(vap->iv_bss->ni_chan);
757 * Check if the current channel needs to change based on whether
758 * any vap's are using HT20/HT40. This is used to sync the state
759 * of ic_curchan after a channel width change on a running vap.
761 * Same applies for VHT.
764 ieee80211_sync_curchan(struct ieee80211com *ic)
766 struct ieee80211_channel *c;
768 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
769 c = ieee80211_vht_adjust_channel(ic, c, getvhtadjustflags(ic));
771 if (c != ic->ic_curchan) {
773 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
774 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
775 IEEE80211_UNLOCK(ic);
776 ic->ic_set_channel(ic);
777 ieee80211_radiotap_chan_change(ic);
783 * Setup the current channel. The request channel may be
784 * promoted if other vap's are operating with HT20/HT40.
787 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
789 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
790 int flags = gethtadjustflags(ic);
792 * Check for channel promotion required to support the
793 * set of running vap's. This assumes we are called
794 * after ni_chan is setup for each vap.
797 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
798 if (flags > ieee80211_htchanflags(c))
799 c = ieee80211_ht_adjust_channel(ic, c, flags);
803 * VHT promotion - this will at least promote to VHT20/40
804 * based on what HT has done; it may further promote the
805 * channel to VHT80 or above.
807 if (ic->ic_vhtcaps != 0) {
808 int flags = getvhtadjustflags(ic);
809 if (flags > ieee80211_vhtchanflags(c))
810 c = ieee80211_vht_adjust_channel(ic, c, flags);
813 ic->ic_bsschan = ic->ic_curchan = c;
814 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
815 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
819 * Change the current channel. The channel change is guaranteed to have
820 * happened before the next state change.
823 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
825 ieee80211_setupcurchan(ic, c);
826 ieee80211_runtask(ic, &ic->ic_chan_task);
830 ieee80211_update_chw(struct ieee80211com *ic)
833 ieee80211_setupcurchan(ic, ic->ic_curchan);
834 ieee80211_runtask(ic, &ic->ic_chw_task);
838 * Join the specified IBSS/BSS network. The node is assumed to
839 * be passed in with a held reference.
842 ieee80211_sta_join1(struct ieee80211_node *selbs)
844 struct ieee80211vap *vap = selbs->ni_vap;
845 struct ieee80211com *ic = selbs->ni_ic;
846 struct ieee80211_node *obss;
850 * Committed to selbs, setup state.
852 obss = vap->iv_update_bss(vap, selbs); /* NB: caller assumed to bump refcnt */
854 * Check if old+new node have the same address in which
855 * case we can reassociate when operating in sta mode.
857 /* XXX We'll not be in RUN anymore as iv_state got updated already? */
858 canreassoc = (obss != NULL &&
859 vap->iv_state == IEEE80211_S_RUN &&
860 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
862 struct ieee80211_node_table *nt = obss->ni_table;
864 copy_bss(selbs, obss);
865 ieee80211_node_decref(obss); /* iv_bss reference */
867 IEEE80211_NODE_LOCK(nt);
868 node_reclaim(nt, obss); /* station table reference */
869 IEEE80211_NODE_UNLOCK(nt);
871 obss = NULL; /* NB: guard against later use */
875 * Delete unusable rates; we've already checked
876 * that the negotiated rate set is acceptable.
878 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
879 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
881 ieee80211_setcurchan(ic, selbs->ni_chan);
883 * Set the erp state (mostly the slot time) to deal with
884 * the auto-select case; this should be redundant if the
887 ieee80211_vap_reset_erp(vap);
888 ieee80211_wme_initparams(vap);
890 if (vap->iv_opmode == IEEE80211_M_STA) {
893 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
896 * Act as if we received a DEAUTH frame in case we
897 * are invoked from the RUN state. This will cause
898 * us to try to re-authenticate if we are operating
901 IEEE80211_DPRINTF(vap, IEEE80211_MSG_AUTH,
902 "%s %p<%s> %s -> AUTH, FC0_SUBTYPE_DEAUTH\n",
903 __func__, selbs, ether_sprintf(selbs->ni_macaddr),
904 ieee80211_state_name[vap->iv_state]);
905 ieee80211_new_state(vap, IEEE80211_S_AUTH,
906 IEEE80211_FC0_SUBTYPE_DEAUTH);
909 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
914 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
915 const struct ieee80211_scan_entry *se)
917 struct ieee80211com *ic = vap->iv_ic;
918 struct ieee80211_node *ni;
921 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
928 * Expand scan state into node's format.
929 * XXX may not need all this stuff
931 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
932 ni->ni_esslen = se->se_ssid[1];
933 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
934 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
935 ni->ni_intval = se->se_intval;
936 ni->ni_capinfo = se->se_capinfo;
938 ni->ni_timoff = se->se_timoff;
939 ni->ni_fhdwell = se->se_fhdwell;
940 ni->ni_fhindex = se->se_fhindex;
941 ni->ni_erp = se->se_erp;
942 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
943 ni->ni_noise = se->se_noise;
944 if (vap->iv_opmode == IEEE80211_M_STA) {
945 /* NB: only infrastructure mode requires an associd */
946 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
949 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
950 ieee80211_ies_expand(&ni->ni_ies);
951 #ifdef IEEE80211_SUPPORT_SUPERG
952 if (ni->ni_ies.ath_ie != NULL)
953 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
955 if (ni->ni_ies.htcap_ie != NULL)
956 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
957 if (ni->ni_ies.htinfo_ie != NULL)
958 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
959 #ifdef IEEE80211_SUPPORT_MESH
960 if (ni->ni_ies.meshid_ie != NULL)
961 ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
963 #ifdef IEEE80211_SUPPORT_TDMA
964 if (ni->ni_ies.tdma_ie != NULL)
965 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
967 if (ni->ni_ies.vhtcap_ie != NULL)
968 ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
969 if (ni->ni_ies.vhtopmode_ie != NULL)
970 ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
972 /* XXX parse BSSLOAD IE */
973 /* XXX parse TXPWRENV IE */
974 /* XXX parse APCHANREP IE */
977 vap->iv_dtim_period = se->se_dtimperiod;
978 vap->iv_dtim_count = 0;
980 /* NB: must be after ni_chan is setup */
981 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
983 if (ieee80211_iserp_rateset(&ni->ni_rates))
984 ni->ni_flags |= IEEE80211_NODE_ERP;
987 * Setup HT state for this node if it's available, otherwise
988 * non-STA modes won't pick this state up.
990 * For IBSS and related modes that don't go through an
991 * association request/response, the only appropriate place
992 * to setup the HT state is here.
994 if (ni->ni_ies.htinfo_ie != NULL &&
995 ni->ni_ies.htcap_ie != NULL &&
996 vap->iv_flags_ht & IEEE80211_FHT_HT) {
997 ieee80211_ht_node_init(ni);
998 ieee80211_ht_updateparams(ni,
1000 ni->ni_ies.htinfo_ie);
1005 * Setup VHT state for this node if it's available.
1006 * Same as the above.
1008 * For now, don't allow 2GHz VHT operation.
1010 if (ni->ni_ies.vhtopmode_ie != NULL &&
1011 ni->ni_ies.vhtcap_ie != NULL &&
1012 vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1013 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1014 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
1019 ieee80211_vht_node_init(ni);
1020 ieee80211_vht_updateparams(ni,
1021 ni->ni_ies.vhtcap_ie,
1022 ni->ni_ies.vhtopmode_ie);
1023 ieee80211_setup_vht_rates(ni, ni->ni_ies.vhtcap_ie,
1024 ni->ni_ies.vhtopmode_ie);
1029 /* Finally do the node channel change */
1031 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1032 ni->ni_ies.htinfo_ie);
1033 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
1034 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1035 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
1038 /* XXX else check for ath FF? */
1039 /* XXX QoS? Difficult given that WME config is specific to a master */
1041 ieee80211_node_setuptxparms(ni);
1042 ieee80211_ratectl_node_init(ni);
1044 return ieee80211_sta_join1(ieee80211_ref_node(ni));
1048 * Leave the specified IBSS/BSS network. The node is assumed to
1049 * be passed in with a held reference.
1052 ieee80211_sta_leave(struct ieee80211_node *ni)
1054 struct ieee80211com *ic = ni->ni_ic;
1056 ic->ic_node_cleanup(ni);
1057 ieee80211_notify_node_leave(ni);
1061 * Send a deauthenticate frame and drop the station.
1064 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
1066 /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
1067 ieee80211_ref_node(ni);
1068 if (ni->ni_associd != 0)
1069 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1070 ieee80211_node_leave(ni);
1071 ieee80211_free_node(ni);
1074 static struct ieee80211_node *
1075 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1077 struct ieee80211_node *ni;
1079 ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
1080 M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1085 node_init(struct ieee80211_node *ni)
1091 * Initialize an ie blob with the specified data. If previous
1092 * data exists re-use the data block. As a side effect we clear
1093 * all references to specific ie's; the caller is required to
1097 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
1099 /* NB: assumes data+len are the last fields */
1100 memset(ies, 0, offsetof(struct ieee80211_ies, data));
1101 if (ies->data != NULL && ies->len != len) {
1102 /* data size changed */
1103 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1106 if (ies->data == NULL) {
1107 ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
1108 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1109 if (ies->data == NULL) {
1111 /* NB: pointers have already been zero'd above */
1115 memcpy(ies->data, data, len);
1121 * Reclaim storage for an ie blob.
1124 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
1126 if (ies->data != NULL)
1127 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1131 * Expand an ie blob data contents and to fillin individual
1132 * ie pointers. The data blob is assumed to be well-formed;
1133 * we don't do any validity checking of ie lengths.
1136 ieee80211_ies_expand(struct ieee80211_ies *ies)
1144 /* Make sure the given IE length fits into the total length. */
1145 if ((2 + ie[1]) > ielen) {
1146 printf("%s: malformed IEs! ies %p { data %p len %d }: "
1147 "ie %u len 2+%u > total len left %d\n",
1148 __func__, ies, ies->data, ies->len,
1149 ie[0], ie[1], ielen);
1153 case IEEE80211_ELEMID_VENDOR:
1156 else if (iswmeoui(ie))
1158 #ifdef IEEE80211_SUPPORT_SUPERG
1159 else if (isatherosoui(ie))
1162 #ifdef IEEE80211_SUPPORT_TDMA
1163 else if (istdmaoui(ie))
1167 case IEEE80211_ELEMID_RSN:
1170 case IEEE80211_ELEMID_HTCAP:
1173 case IEEE80211_ELEMID_HTINFO:
1174 ies->htinfo_ie = ie;
1176 #ifdef IEEE80211_SUPPORT_MESH
1177 case IEEE80211_ELEMID_MESHID:
1178 ies->meshid_ie = ie;
1181 case IEEE80211_ELEMID_VHT_CAP:
1182 ies->vhtcap_ie = ie;
1184 case IEEE80211_ELEMID_VHT_OPMODE:
1185 ies->vhtopmode_ie = ie;
1187 case IEEE80211_ELEMID_VHT_PWR_ENV:
1188 ies->vhtpwrenv_ie = ie;
1190 case IEEE80211_ELEMID_BSSLOAD:
1191 ies->bssload_ie = ie;
1193 case IEEE80211_ELEMID_APCHANREP:
1194 ies->apchanrep_ie = ie;
1203 * Reclaim any resources in a node and reset any critical
1204 * state. Typically nodes are free'd immediately after,
1205 * but in some cases the storage may be reused so we need
1206 * to insure consistent state (should probably fix that).
1209 node_cleanup(struct ieee80211_node *ni)
1211 struct ieee80211vap *vap = ni->ni_vap;
1212 struct ieee80211com *ic = ni->ni_ic;
1215 /* NB: preserve ni_table */
1216 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1217 if (vap->iv_opmode != IEEE80211_M_STA)
1219 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
1220 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
1221 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
1224 * Cleanup any VHT and HT-related state.
1226 if (ni->ni_flags & IEEE80211_NODE_VHT)
1227 ieee80211_vht_node_cleanup(ni);
1228 if (ni->ni_flags & IEEE80211_NODE_HT)
1229 ieee80211_ht_node_cleanup(ni);
1230 #ifdef IEEE80211_SUPPORT_SUPERG
1231 /* Always do FF node cleanup; for A-MSDU */
1232 ieee80211_ff_node_cleanup(ni);
1234 #ifdef IEEE80211_SUPPORT_MESH
1236 * Cleanup any mesh-related state.
1238 if (vap->iv_opmode == IEEE80211_M_MBSS)
1239 ieee80211_mesh_node_cleanup(ni);
1242 * Clear any staging queue entries.
1244 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1247 * Clear AREF flag that marks the authorization refcnt bump
1248 * has happened. This is probably not needed as the node
1249 * should always be removed from the table so not found but
1250 * do it just in case.
1251 * Likewise clear the ASSOCID flag as these flags are intended
1252 * to be managed in tandem.
1254 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1257 * Drain power save queue and, if needed, clear TIM.
1259 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1260 vap->iv_set_tim(ni, 0);
1263 if (ni->ni_challenge != NULL) {
1264 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1265 ni->ni_challenge = NULL;
1268 * Preserve SSID, WPA, and WME ie's so the bss node is
1269 * reusable during a re-auth/re-assoc state transition.
1270 * If we remove these data they will not be recreated
1271 * because they come from a probe-response or beacon frame
1272 * which cannot be expected prior to the association-response.
1273 * This should not be an issue when operating in other modes
1274 * as stations leaving always go through a full state transition
1275 * which will rebuild this state.
1277 * XXX does this leave us open to inheriting old state?
1279 for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1280 if (ni->ni_rxfrag[i] != NULL) {
1281 m_freem(ni->ni_rxfrag[i]);
1282 ni->ni_rxfrag[i] = NULL;
1285 * Must be careful here to remove any key map entry w/o a LOR.
1287 ieee80211_node_delucastkey(ni);
1291 node_free(struct ieee80211_node *ni)
1293 struct ieee80211com *ic = ni->ni_ic;
1295 ieee80211_ratectl_node_deinit(ni);
1296 ic->ic_node_cleanup(ni);
1297 ieee80211_ies_cleanup(&ni->ni_ies);
1298 ieee80211_psq_cleanup(&ni->ni_psq);
1299 IEEE80211_FREE(ni, M_80211_NODE);
1303 node_age(struct ieee80211_node *ni)
1305 struct ieee80211vap *vap = ni->ni_vap;
1308 * Age frames on the power save queue.
1310 if (ieee80211_node_psq_age(ni) != 0 &&
1311 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1312 vap->iv_set_tim(ni, 0);
1314 * Age out HT resources (e.g. frames on the
1315 * A-MPDU reorder queues).
1317 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1318 ieee80211_ht_node_age(ni);
1322 node_getrssi(const struct ieee80211_node *ni)
1324 uint32_t avgrssi = ni->ni_avgrssi;
1327 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1329 rssi = IEEE80211_RSSI_GET(avgrssi);
1330 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1334 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1336 *rssi = node_getrssi(ni);
1337 *noise = ni->ni_noise;
1341 node_getmimoinfo(const struct ieee80211_node *ni,
1342 struct ieee80211_mimo_info *info)
1348 bzero(info, sizeof(*info));
1350 for (i = 0; i < MIN(IEEE80211_MAX_CHAINS, ni->ni_mimo_chains); i++) {
1351 /* Note: for now, just pri20 channel info */
1352 avgrssi = ni->ni_mimo_rssi_ctl[i];
1353 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1354 info->ch[i].rssi[0] = 0;
1356 rssi = IEEE80211_RSSI_GET(avgrssi);
1357 info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1359 info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i];
1362 /* XXX ext radios? */
1368 ieee80211_add_node_nt(struct ieee80211_node_table *nt,
1369 struct ieee80211_node *ni)
1371 struct ieee80211com *ic = nt->nt_ic;
1374 IEEE80211_NODE_LOCK_ASSERT(nt);
1376 hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr);
1377 (void) ic; /* XXX IEEE80211_NODE_HASH */
1378 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1379 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1385 ieee80211_del_node_nt(struct ieee80211_node_table *nt,
1386 struct ieee80211_node *ni)
1389 IEEE80211_NODE_LOCK_ASSERT(nt);
1391 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1392 LIST_REMOVE(ni, ni_hash);
1394 KASSERT(nt->nt_count >= 0,
1395 ("nt_count is negative (%d)!\n", nt->nt_count));
1396 ni->ni_table = NULL;
1399 struct ieee80211_node *
1400 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1401 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1403 struct ieee80211com *ic = nt->nt_ic;
1404 struct ieee80211_node *ni;
1406 ni = ic->ic_node_alloc(vap, macaddr);
1408 vap->iv_stats.is_rx_nodealloc++;
1412 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1413 "%s %p<%s> in %s table\n", __func__, ni,
1414 ether_sprintf(macaddr), nt->nt_name);
1416 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1417 ieee80211_node_initref(ni); /* mark referenced */
1418 ni->ni_chan = IEEE80211_CHAN_ANYC;
1419 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1420 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1421 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1422 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1423 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1424 ni->ni_inact_reload = nt->nt_inact_init;
1425 ni->ni_inact = ni->ni_inact_reload;
1426 ni->ni_ath_defkeyix = 0x7fff;
1427 ieee80211_psq_init(&ni->ni_psq, "unknown");
1428 #ifdef IEEE80211_SUPPORT_MESH
1429 if (vap->iv_opmode == IEEE80211_M_MBSS)
1430 ieee80211_mesh_node_init(vap, ni);
1432 IEEE80211_NODE_LOCK(nt);
1433 ieee80211_add_node_nt(nt, ni);
1436 IEEE80211_NODE_UNLOCK(nt);
1438 /* handle failure; free node state */
1439 if (ic->ic_node_init(ni) != 0) {
1440 vap->iv_stats.is_rx_nodealloc++;
1441 ieee80211_psq_cleanup(&ni->ni_psq);
1442 ieee80211_ratectl_node_deinit(ni);
1443 __ieee80211_free_node(ni);
1447 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1448 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1454 * Craft a temporary node suitable for sending a management frame
1455 * to the specified station. We craft only as much state as we
1456 * need to do the work since the node will be immediately reclaimed
1457 * once the send completes.
1459 struct ieee80211_node *
1460 ieee80211_tmp_node(struct ieee80211vap *vap,
1461 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1463 struct ieee80211com *ic = vap->iv_ic;
1464 struct ieee80211_node *ni;
1466 ni = ic->ic_node_alloc(vap, macaddr);
1468 struct ieee80211_node *bss = vap->iv_bss;
1470 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1471 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1473 ni->ni_table = NULL; /* NB: pedantic */
1474 ni->ni_ic = ic; /* NB: needed to set channel */
1477 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1478 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1479 ieee80211_node_initref(ni); /* mark referenced */
1480 /* NB: required by ieee80211_fix_rate */
1481 ieee80211_node_set_chan(ni, bss->ni_chan);
1482 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1483 IEEE80211_KEYIX_NONE);
1484 ni->ni_txpower = bss->ni_txpower;
1485 /* XXX optimize away */
1486 ieee80211_psq_init(&ni->ni_psq, "unknown");
1488 ieee80211_ratectl_node_init(ni);
1490 /* handle failure; free node state */
1491 if (ic->ic_node_init(ni) != 0) {
1492 vap->iv_stats.is_rx_nodealloc++;
1493 ieee80211_psq_cleanup(&ni->ni_psq);
1494 ieee80211_ratectl_node_deinit(ni);
1495 __ieee80211_free_node(ni);
1501 vap->iv_stats.is_rx_nodealloc++;
1506 struct ieee80211_node *
1507 ieee80211_dup_bss(struct ieee80211vap *vap,
1508 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1510 struct ieee80211com *ic = vap->iv_ic;
1511 struct ieee80211_node *ni;
1513 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1515 struct ieee80211_node *bss = vap->iv_bss;
1517 * Inherit from iv_bss.
1520 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1521 ieee80211_node_set_chan(ni, bss->ni_chan);
1527 * Create a bss node for a legacy WDS vap. The far end does
1528 * not associate so we just create create a new node and
1529 * simulate an association. The caller is responsible for
1530 * installing the node as the bss node and handling any further
1531 * setup work like authorizing the port.
1533 struct ieee80211_node *
1534 ieee80211_node_create_wds(struct ieee80211vap *vap,
1535 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1537 struct ieee80211com *ic = vap->iv_ic;
1538 struct ieee80211_node *ni;
1540 /* XXX check if node already in sta table? */
1541 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1543 ni->ni_wdsvap = vap;
1544 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1546 * Inherit any manually configured settings.
1548 copy_bss(ni, vap->iv_bss);
1549 ieee80211_node_set_chan(ni, chan);
1550 /* NB: propagate ssid so available to WPA supplicant */
1551 ni->ni_esslen = vap->iv_des_ssid[0].len;
1552 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1553 /* NB: no associd for peer */
1555 * There are no management frames to use to
1556 * discover neighbor capabilities, so blindly
1557 * propagate the local configuration.
1559 if (vap->iv_flags & IEEE80211_F_WME)
1560 ni->ni_flags |= IEEE80211_NODE_QOS;
1561 #ifdef IEEE80211_SUPPORT_SUPERG
1562 if (vap->iv_flags & IEEE80211_F_FF)
1563 ni->ni_flags |= IEEE80211_NODE_FF;
1566 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1567 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1569 * Device is HT-capable and HT is enabled for
1570 * the vap; setup HT operation. On return
1571 * ni_chan will be adjusted to an HT channel.
1573 ieee80211_ht_wds_init(ni);
1574 if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1575 printf("%s: TODO: vht_wds_init\n", __func__);
1578 struct ieee80211_channel *c = ni->ni_chan;
1580 * Force a legacy channel to be used.
1582 c = ieee80211_find_channel(ic,
1583 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1584 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1585 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1592 struct ieee80211_node *
1593 _ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1594 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1595 const char *func __debrefcnt_used, int line __debrefcnt_used)
1597 struct ieee80211_node *ni;
1600 IEEE80211_NODE_LOCK_ASSERT(nt);
1602 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1603 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1604 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1605 ieee80211_ref_node(ni); /* mark referenced */
1606 #ifdef IEEE80211_DEBUG_REFCNT
1607 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1608 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1610 ni, ether_sprintf(ni->ni_macaddr),
1611 ieee80211_node_refcnt(ni));
1619 struct ieee80211_node *
1620 _ieee80211_find_node(struct ieee80211_node_table *nt,
1621 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1622 const char *func __debrefcnt_used, int line __debrefcnt_used)
1624 struct ieee80211_node *ni;
1626 IEEE80211_NODE_LOCK(nt);
1627 ni = _ieee80211_find_node_locked(nt, macaddr, func, line);
1628 IEEE80211_NODE_UNLOCK(nt);
1632 struct ieee80211_node *
1633 _ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1634 const struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN],
1635 const char *func __debrefcnt_used, int line __debrefcnt_used)
1637 struct ieee80211_node *ni;
1640 IEEE80211_NODE_LOCK_ASSERT(nt);
1642 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1643 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1644 if (ni->ni_vap == vap &&
1645 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1646 ieee80211_ref_node(ni); /* mark referenced */
1647 #ifdef IEEE80211_DEBUG_REFCNT
1648 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1649 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1651 ni, ether_sprintf(ni->ni_macaddr),
1652 ieee80211_node_refcnt(ni));
1660 struct ieee80211_node *
1661 _ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1662 const struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN],
1663 const char *func __debrefcnt_used, int line __debrefcnt_used)
1665 struct ieee80211_node *ni;
1667 IEEE80211_NODE_LOCK(nt);
1668 ni = _ieee80211_find_vap_node_locked(nt, vap, macaddr, func, line);
1669 IEEE80211_NODE_UNLOCK(nt);
1674 * Fake up a node; this handles node discovery in adhoc mode.
1675 * Note that for the driver's benefit we treat this like
1676 * an association so the driver has an opportunity to setup
1677 * it's private state.
1679 struct ieee80211_node *
1680 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1681 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1683 struct ieee80211_node *ni;
1685 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1686 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1687 ni = ieee80211_dup_bss(vap, macaddr);
1689 struct ieee80211com *ic = vap->iv_ic;
1691 /* XXX no rate negotiation; just dup */
1692 ni->ni_rates = vap->iv_bss->ni_rates;
1693 if (ieee80211_iserp_rateset(&ni->ni_rates))
1694 ni->ni_flags |= IEEE80211_NODE_ERP;
1695 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1697 * In adhoc demo mode there are no management
1698 * frames to use to discover neighbor capabilities,
1699 * so blindly propagate the local configuration
1700 * so we can do interesting things (e.g. use
1701 * WME to disable ACK's).
1706 if (vap->iv_flags & IEEE80211_F_WME)
1707 ni->ni_flags |= IEEE80211_NODE_QOS;
1708 #ifdef IEEE80211_SUPPORT_SUPERG
1709 if (vap->iv_flags & IEEE80211_F_FF)
1710 ni->ni_flags |= IEEE80211_NODE_FF;
1713 ieee80211_node_setuptxparms(ni);
1714 ieee80211_ratectl_node_init(ni);
1717 * XXX TODO: 11n? At least 20MHz, at least A-MPDU RX,
1718 * not A-MPDU TX; not 11n rates, etc. We'll cycle
1719 * that after we hear that we can indeed do 11n
1720 * (either by a beacon frame or by a probe response.)
1724 * This is the first time we see the node.
1726 if (ic->ic_newassoc != NULL)
1727 ic->ic_newassoc(ni, 1);
1730 * Kick off a probe request to the given node;
1731 * we will then use the probe response to update
1732 * 11n/etc configuration state.
1734 * XXX TODO: this isn't guaranteed, and until we get
1735 * a probe response, we won't be able to actually
1736 * do anything 802.11n related to the node.
1737 * So if this does indeed work, maybe we should hold
1738 * off on sending responses until we get the probe
1739 * response, or just default to some sensible subset
1740 * of 802.11n behaviour (eg always allow aggregation
1741 * negotiation TO us, but not FROM us, etc) so we
1742 * aren't entirely busted.
1744 if (vap->iv_opmode == IEEE80211_M_IBSS) {
1745 ieee80211_send_probereq(ni, /* node */
1746 vap->iv_myaddr, /* SA */
1747 ni->ni_macaddr, /* DA */
1748 vap->iv_bss->ni_bssid, /* BSSID */
1749 vap->iv_bss->ni_essid,
1750 vap->iv_bss->ni_esslen); /* SSID */
1753 /* XXX not right for 802.1x/WPA */
1754 ieee80211_node_authorize(ni);
1760 ieee80211_init_neighbor(struct ieee80211_node *ni,
1761 const struct ieee80211_frame *wh,
1762 const struct ieee80211_scanparams *sp)
1764 int do_ht_setup = 0, do_vht_setup = 0;
1766 ni->ni_esslen = sp->ssid[1];
1767 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1768 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1769 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1770 ni->ni_intval = sp->bintval;
1771 ni->ni_capinfo = sp->capinfo;
1772 ni->ni_chan = ni->ni_ic->ic_curchan;
1773 ni->ni_fhdwell = sp->fhdwell;
1774 ni->ni_fhindex = sp->fhindex;
1775 ni->ni_erp = sp->erp;
1776 ni->ni_timoff = sp->timoff;
1777 #ifdef IEEE80211_SUPPORT_MESH
1778 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1779 ieee80211_mesh_init_neighbor(ni, wh, sp);
1781 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1782 ieee80211_ies_expand(&ni->ni_ies);
1783 if (ni->ni_ies.wme_ie != NULL)
1784 ni->ni_flags |= IEEE80211_NODE_QOS;
1786 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1787 #ifdef IEEE80211_SUPPORT_SUPERG
1788 if (ni->ni_ies.ath_ie != NULL)
1789 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1791 if (ni->ni_ies.htcap_ie != NULL)
1792 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1793 if (ni->ni_ies.htinfo_ie != NULL)
1794 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1796 if (ni->ni_ies.vhtcap_ie != NULL)
1797 ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
1798 if (ni->ni_ies.vhtopmode_ie != NULL)
1799 ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
1801 if ((ni->ni_ies.htcap_ie != NULL) &&
1802 (ni->ni_ies.htinfo_ie != NULL) &&
1803 (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1807 if ((ni->ni_ies.vhtcap_ie != NULL) &&
1808 (ni->ni_ies.vhtopmode_ie != NULL) &&
1809 (ni->ni_vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
1814 /* NB: must be after ni_chan is setup */
1815 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1816 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1817 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1820 * If the neighbor is HT compatible, flip that on.
1823 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1824 "%s: doing HT setup\n", __func__);
1825 ieee80211_ht_node_init(ni);
1826 ieee80211_ht_updateparams(ni,
1827 ni->ni_ies.htcap_ie,
1828 ni->ni_ies.htinfo_ie);
1831 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1832 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
1837 ieee80211_vht_node_init(ni);
1838 ieee80211_vht_updateparams(ni,
1839 ni->ni_ies.vhtcap_ie,
1840 ni->ni_ies.vhtopmode_ie);
1841 ieee80211_setup_vht_rates(ni,
1842 ni->ni_ies.vhtcap_ie,
1843 ni->ni_ies.vhtopmode_ie);
1848 * Finally do the channel upgrade/change based
1849 * on the HT/VHT configuration.
1851 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1852 ni->ni_ies.htinfo_ie);
1853 ieee80211_setup_htrates(ni,
1854 ni->ni_ies.htcap_ie,
1855 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1856 ieee80211_setup_basic_htrates(ni,
1857 ni->ni_ies.htinfo_ie);
1859 ieee80211_node_setuptxparms(ni);
1860 ieee80211_ratectl_node_init(ni);
1862 /* Reassociate; we're now 11n/11ac */
1864 * XXX TODO: this is the wrong thing to do -
1865 * we're calling it with isnew=1 so the ath(4)
1866 * driver reinitialises the rate tables.
1867 * This "mostly" works for ath(4), but it won't
1868 * be right for firmware devices which allocate
1871 * So, do we just create a new node and delete
1874 if (ni->ni_ic->ic_newassoc)
1875 ni->ni_ic->ic_newassoc(ni, 1);
1880 * Do node discovery in adhoc mode on receipt of a beacon
1881 * or probe response frame. Note that for the driver's
1882 * benefit we treat this like an association so the
1883 * driver has an opportunity to setup it's private state.
1885 struct ieee80211_node *
1886 ieee80211_add_neighbor(struct ieee80211vap *vap,
1887 const struct ieee80211_frame *wh,
1888 const struct ieee80211_scanparams *sp)
1890 struct ieee80211_node *ni;
1892 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1893 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1894 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1896 struct ieee80211com *ic = vap->iv_ic;
1898 ieee80211_init_neighbor(ni, wh, sp);
1899 if (ieee80211_iserp_rateset(&ni->ni_rates))
1900 ni->ni_flags |= IEEE80211_NODE_ERP;
1901 ieee80211_node_setuptxparms(ni);
1902 ieee80211_ratectl_node_init(ni);
1903 if (ic->ic_newassoc != NULL)
1904 ic->ic_newassoc(ni, 1);
1905 /* XXX not right for 802.1x/WPA */
1906 ieee80211_node_authorize(ni);
1911 #define IS_PROBEREQ(wh) \
1912 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1913 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1914 #define IS_BCAST_PROBEREQ(wh) \
1915 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1916 ((const struct ieee80211_frame *)(wh))->i_addr3))
1918 static __inline struct ieee80211_node *
1919 _find_rxnode(struct ieee80211_node_table *nt,
1920 const struct ieee80211_frame_min *wh,
1921 const char *func __debrefcnt_used, int line __debrefcnt_used)
1923 if (IS_BCAST_PROBEREQ(wh))
1924 return NULL; /* spam bcast probe req to all vap's */
1925 return _ieee80211_find_node_locked(nt, wh->i_addr2, func, line);
1929 * Locate the node for sender, track state, and then pass the
1930 * (referenced) node up to the 802.11 layer for its use. Note
1931 * we can return NULL if the sender is not in the table.
1933 struct ieee80211_node *
1934 _ieee80211_find_rxnode(struct ieee80211com *ic,
1935 const struct ieee80211_frame_min *wh,
1936 const char *func __debrefcnt_used, int line __debrefcnt_used)
1938 struct ieee80211_node_table *nt;
1939 struct ieee80211_node *ni;
1942 IEEE80211_NODE_LOCK(nt);
1943 ni = _find_rxnode(nt, wh, func, line);
1944 IEEE80211_NODE_UNLOCK(nt);
1950 * Like ieee80211_find_rxnode but use the supplied h/w
1951 * key index as a hint to locate the node in the key
1952 * mapping table. If an entry is present at the key
1953 * index we return it; otherwise do a normal lookup and
1954 * update the mapping table if the station has a unicast
1955 * key assigned to it.
1957 struct ieee80211_node *
1958 _ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1959 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1960 const char *func __debrefcnt_used, int line __debrefcnt_used)
1962 struct ieee80211_node_table *nt;
1963 struct ieee80211_node *ni;
1966 IEEE80211_NODE_LOCK(nt);
1967 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1968 ni = nt->nt_keyixmap[keyix];
1972 ni = _find_rxnode(nt, wh, func, line);
1973 if (ni != NULL && nt->nt_keyixmap != NULL) {
1975 * If the station has a unicast key cache slot
1976 * assigned update the key->node mapping table.
1978 keyix = ni->ni_ucastkey.wk_rxkeyix;
1979 /* XXX can keyixmap[keyix] != NULL? */
1980 if (keyix < nt->nt_keyixmax &&
1981 nt->nt_keyixmap[keyix] == NULL) {
1982 IEEE80211_DPRINTF(ni->ni_vap,
1984 "%s: add key map entry %p<%s> refcnt %d\n",
1985 __func__, ni, ether_sprintf(ni->ni_macaddr),
1986 ieee80211_node_refcnt(ni)+1);
1987 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1991 if (IS_BCAST_PROBEREQ(wh))
1992 ni = NULL; /* spam bcast probe req to all vap's */
1994 ieee80211_ref_node(ni);
1996 IEEE80211_NODE_UNLOCK(nt);
2000 #undef IS_BCAST_PROBEREQ
2004 * Return a reference to the appropriate node for sending
2005 * a data frame. This handles node discovery in adhoc networks.
2007 struct ieee80211_node *
2008 _ieee80211_find_txnode(struct ieee80211vap *vap,
2009 const uint8_t macaddr[IEEE80211_ADDR_LEN],
2010 const char *func __debrefcnt_used, int line __debrefcnt_used)
2012 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
2013 struct ieee80211_node *ni;
2016 * The destination address should be in the node table
2017 * unless this is a multicast/broadcast frame. We can
2018 * also optimize station mode operation, all frames go
2021 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
2022 IEEE80211_NODE_LOCK(nt);
2023 if (vap->iv_opmode == IEEE80211_M_STA ||
2024 vap->iv_opmode == IEEE80211_M_WDS ||
2025 IEEE80211_IS_MULTICAST(macaddr))
2026 ni = ieee80211_ref_node(vap->iv_bss);
2028 ni = _ieee80211_find_node_locked(nt, macaddr, func, line);
2029 IEEE80211_NODE_UNLOCK(nt);
2032 if (vap->iv_opmode == IEEE80211_M_IBSS ||
2033 vap->iv_opmode == IEEE80211_M_AHDEMO) {
2035 * In adhoc mode cons up a node for the destination.
2036 * Note that we need an additional reference for the
2037 * caller to be consistent with
2038 * ieee80211_find_node_locked.
2041 * XXX TODO: this doesn't fake up 11n state; we need
2042 * to find another way to get it upgraded.
2044 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
2046 (void) ieee80211_ref_node(ni);
2048 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
2049 "no node, discard frame (%s)", __func__);
2050 vap->iv_stats.is_tx_nonode++;
2057 __ieee80211_free_node(struct ieee80211_node *ni)
2059 struct ieee80211_node_table *nt = ni->ni_table;
2062 * NB: careful about referencing the vap as it may be
2063 * gone if the last reference was held by a driver.
2064 * We know the com will always be present so it's safe
2065 * to use ni_ic below to reclaim resources.
2068 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2069 "%s %p<%s> in %s table\n", __func__, ni,
2070 ether_sprintf(ni->ni_macaddr),
2071 nt != NULL ? nt->nt_name : "<gone>");
2073 if (ni->ni_associd != 0) {
2074 struct ieee80211vap *vap = ni->ni_vap;
2075 if (vap->iv_aid_bitmap != NULL)
2076 IEEE80211_AID_CLR(vap, ni->ni_associd);
2079 ieee80211_del_node_nt(nt, ni);
2080 ni->ni_ic->ic_node_free(ni);
2084 * Clear any entry in the unicast key mapping table.
2087 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2089 ieee80211_keyix keyix;
2091 keyix = ni->ni_ucastkey.wk_rxkeyix;
2092 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
2093 nt->nt_keyixmap[keyix] == ni) {
2094 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2095 "%s: %p<%s> clear key map entry %u\n",
2096 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
2097 nt->nt_keyixmap[keyix] = NULL;
2098 ieee80211_node_decref(ni);
2106 _ieee80211_free_node(struct ieee80211_node *ni,
2107 const char *func __debrefcnt_used, int line __debrefcnt_used)
2109 struct ieee80211_node_table *nt = ni->ni_table;
2111 #ifdef IEEE80211_DEBUG_REFCNT
2112 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2113 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
2114 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
2117 IEEE80211_NODE_LOCK(nt);
2118 if (ieee80211_node_dectestref(ni)) {
2120 * Last reference, reclaim state.
2122 __ieee80211_free_node(ni);
2123 } else if (ieee80211_node_refcnt(ni) == 1)
2124 if (node_clear_keyixmap(nt, ni))
2125 __ieee80211_free_node(ni);
2126 IEEE80211_NODE_UNLOCK(nt);
2128 if (ieee80211_node_dectestref(ni))
2129 __ieee80211_free_node(ni);
2134 * Reclaim a unicast key and clear any key cache state.
2137 ieee80211_node_delucastkey(struct ieee80211_node *ni)
2139 struct ieee80211com *ic = ni->ni_ic;
2140 struct ieee80211_node_table *nt = &ic->ic_sta;
2141 struct ieee80211_node *nikey;
2142 ieee80211_keyix keyix;
2143 int isowned, status;
2146 * NB: We must beware of LOR here; deleting the key
2147 * can cause the crypto layer to block traffic updates
2148 * which can generate a LOR against the node table lock;
2149 * grab it here and stash the key index for our use below.
2151 * Must also beware of recursion on the node table lock.
2152 * When called from node_cleanup we may already have
2153 * the node table lock held. Unfortunately there's no
2154 * way to separate out this path so we must do this
2157 isowned = IEEE80211_NODE_IS_LOCKED(nt);
2159 IEEE80211_NODE_LOCK(nt);
2161 status = 1; /* NB: success */
2162 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
2163 keyix = ni->ni_ucastkey.wk_rxkeyix;
2164 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
2165 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
2166 nikey = nt->nt_keyixmap[keyix];
2167 nt->nt_keyixmap[keyix] = NULL;
2171 IEEE80211_NODE_UNLOCK(nt);
2173 if (nikey != NULL) {
2174 KASSERT(nikey == ni,
2175 ("key map out of sync, ni %p nikey %p", ni, nikey));
2176 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2177 "%s: delete key map entry %p<%s> refcnt %d\n",
2178 __func__, ni, ether_sprintf(ni->ni_macaddr),
2179 ieee80211_node_refcnt(ni)-1);
2180 ieee80211_free_node(ni);
2186 * Reclaim a node. If this is the last reference count then
2187 * do the normal free work. Otherwise remove it from the node
2188 * table and mark it gone by clearing the back-reference.
2191 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2194 IEEE80211_NODE_LOCK_ASSERT(nt);
2196 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2197 "%s: remove %p<%s> from %s table, refcnt %d\n",
2198 __func__, ni, ether_sprintf(ni->ni_macaddr),
2199 nt->nt_name, ieee80211_node_refcnt(ni)-1);
2201 * Clear any entry in the unicast key mapping table.
2202 * We need to do it here so rx lookups don't find it
2203 * in the mapping table even if it's not in the hash
2204 * table. We cannot depend on the mapping table entry
2205 * being cleared because the node may not be free'd.
2207 (void)node_clear_keyixmap(nt, ni);
2208 if (!ieee80211_node_dectestref(ni)) {
2210 * Other references are present, just remove the
2211 * node from the table so it cannot be found. When
2212 * the references are dropped storage will be
2215 ieee80211_del_node_nt(nt, ni);
2217 __ieee80211_free_node(ni);
2221 * Node table support.
2225 ieee80211_node_table_init(struct ieee80211com *ic,
2226 struct ieee80211_node_table *nt,
2227 const char *name, int inact, int keyixmax)
2231 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
2232 TAILQ_INIT(&nt->nt_node);
2235 nt->nt_inact_init = inact;
2236 nt->nt_keyixmax = keyixmax;
2237 if (nt->nt_keyixmax > 0) {
2238 nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
2239 keyixmax * sizeof(struct ieee80211_node *),
2241 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2242 if (nt->nt_keyixmap == NULL)
2244 "Cannot allocate key index map with %u entries\n",
2247 nt->nt_keyixmap = NULL;
2251 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
2252 struct ieee80211vap *match)
2254 struct ieee80211_node *ni, *next;
2256 IEEE80211_NODE_LOCK(nt);
2257 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
2258 if (match != NULL && ni->ni_vap != match)
2260 /* XXX can this happen? if so need's work */
2261 if (ni->ni_associd != 0) {
2262 struct ieee80211vap *vap = ni->ni_vap;
2264 if (vap->iv_auth->ia_node_leave != NULL)
2265 vap->iv_auth->ia_node_leave(ni);
2266 if (vap->iv_aid_bitmap != NULL)
2267 IEEE80211_AID_CLR(vap, ni->ni_associd);
2269 ni->ni_wdsvap = NULL; /* clear reference */
2270 node_reclaim(nt, ni);
2272 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
2274 * Make a separate pass to clear references to this vap
2275 * held by DWDS entries. They will not be matched above
2276 * because ni_vap will point to the ap vap but we still
2277 * need to clear ni_wdsvap when the WDS vap is destroyed
2280 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
2281 if (ni->ni_wdsvap == match)
2282 ni->ni_wdsvap = NULL;
2284 IEEE80211_NODE_UNLOCK(nt);
2288 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2290 ieee80211_node_table_reset(nt, NULL);
2291 if (nt->nt_keyixmap != NULL) {
2293 /* XXX verify all entries are NULL */
2295 for (i = 0; i < nt->nt_keyixmax; i++)
2296 if (nt->nt_keyixmap[i] != NULL)
2297 printf("%s: %s[%u] still active\n", __func__,
2300 IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
2301 nt->nt_keyixmap = NULL;
2303 IEEE80211_NODE_LOCK_DESTROY(nt);
2307 timeout_stations(void *arg __unused, struct ieee80211_node *ni)
2309 struct ieee80211com *ic = ni->ni_ic;
2310 struct ieee80211vap *vap = ni->ni_vap;
2313 * Only process stations when in RUN state. This
2314 * insures, for example, that we don't timeout an
2315 * inactive station during CAC. Note that CSA state
2316 * is actually handled in ieee80211_node_timeout as
2317 * it applies to more than timeout processing.
2319 if (vap->iv_state != IEEE80211_S_RUN)
2322 * Ignore entries for which have yet to receive an
2323 * authentication frame. These are transient and
2324 * will be reclaimed when the last reference to them
2325 * goes away (when frame xmits complete).
2327 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2328 vap->iv_opmode == IEEE80211_M_STA) &&
2329 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2332 * Free fragment if not needed anymore
2333 * (last fragment older than 1s).
2334 * XXX doesn't belong here, move to node_age
2336 if (ni->ni_rxfrag[0] != NULL &&
2337 ticks > ni->ni_rxfragstamp + hz) {
2338 m_freem(ni->ni_rxfrag[0]);
2339 ni->ni_rxfrag[0] = NULL;
2341 if (ni->ni_inact > 0) {
2343 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2344 "%s: inact %u inact_reload %u nrates %u",
2345 __func__, ni->ni_inact, ni->ni_inact_reload,
2346 ni->ni_rates.rs_nrates);
2349 * Special case ourself; we may be idle for extended periods
2350 * of time and regardless reclaiming our state is wrong.
2351 * XXX run ic_node_age
2353 /* XXX before inact decrement? */
2354 if (ni == vap->iv_bss)
2356 if (ni->ni_associd != 0 ||
2357 (vap->iv_opmode == IEEE80211_M_IBSS ||
2358 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2360 * Age/drain resources held by the station.
2362 ic->ic_node_age(ni);
2364 * Probe the station before time it out. We
2365 * send a null data frame which may not be
2366 * universally supported by drivers (need it
2367 * for ps-poll support so it should be...).
2369 * XXX don't probe the station unless we've
2370 * received a frame from them (and have
2371 * some idea of the rates they are capable
2372 * of); this will get fixed more properly
2373 * soon with better handling of the rate set.
2375 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2376 (0 < ni->ni_inact &&
2377 ni->ni_inact <= vap->iv_inact_probe) &&
2378 ni->ni_rates.rs_nrates != 0) {
2380 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2382 "probe station due to inactivity");
2384 * Grab a reference so the node cannot
2385 * be reclaimed before we send the frame.
2386 * ieee80211_send_nulldata understands
2387 * we've done this and reclaims the
2388 * ref for us as needed.
2390 /* XXX fix this (not required anymore). */
2391 ieee80211_ref_node(ni);
2393 ieee80211_send_nulldata(ni);
2398 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2399 ni->ni_inact <= 0) {
2401 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2402 "station timed out due to inactivity "
2403 "(refcnt %u)", ieee80211_node_refcnt(ni));
2405 * Send a deauthenticate frame and drop the station.
2406 * This is somewhat complicated due to reference counts
2407 * and locking. At this point a station will typically
2408 * have a reference count of 2. ieee80211_node_leave
2409 * will do a "free" of the node which will drop the
2410 * reference count. But in the meantime a reference
2411 * wil be held by the deauth frame. The actual reclaim
2412 * of the node will happen either after the tx is
2413 * completed or by ieee80211_node_leave.
2415 if (ni->ni_associd != 0) {
2416 IEEE80211_SEND_MGMT(ni,
2417 IEEE80211_FC0_SUBTYPE_DEAUTH,
2418 IEEE80211_REASON_AUTH_EXPIRE);
2420 ieee80211_node_leave(ni);
2421 vap->iv_stats.is_node_timeout++;
2426 * Timeout inactive stations and do related housekeeping.
2429 ieee80211_timeout_stations(struct ieee80211com *ic)
2431 struct ieee80211_node_table *nt = &ic->ic_sta;
2433 ieee80211_iterate_nodes(nt, timeout_stations, NULL);
2437 * Aggressively reclaim resources. This should be used
2438 * only in a critical situation to reclaim mbuf resources.
2441 ieee80211_drain(struct ieee80211com *ic)
2443 struct ieee80211_node_table *nt = &ic->ic_sta;
2444 struct ieee80211vap *vap;
2445 struct ieee80211_node *ni;
2447 IEEE80211_NODE_LOCK(nt);
2448 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2450 * Ignore entries for which have yet to receive an
2451 * authentication frame. These are transient and
2452 * will be reclaimed when the last reference to them
2453 * goes away (when frame xmits complete).
2457 * Only process stations when in RUN state. This
2458 * insures, for example, that we don't timeout an
2459 * inactive station during CAC. Note that CSA state
2460 * is actually handled in ieee80211_node_timeout as
2461 * it applies to more than timeout processing.
2463 if (vap->iv_state != IEEE80211_S_RUN)
2465 /* XXX can vap be NULL? */
2466 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2467 vap->iv_opmode == IEEE80211_M_STA) &&
2468 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2472 * XXX doesn't belong here, move to node_drain
2474 if (ni->ni_rxfrag[0] != NULL) {
2475 m_freem(ni->ni_rxfrag[0]);
2476 ni->ni_rxfrag[0] = NULL;
2479 * Drain resources held by the station.
2481 ic->ic_node_drain(ni);
2483 IEEE80211_NODE_UNLOCK(nt);
2487 * Per-ieee80211vap inactivity timer callback.
2490 ieee80211_vap_timeout(struct ieee80211vap *vap)
2493 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2495 ieee80211_vap_erp_timeout(vap);
2496 ieee80211_ht_timeout(vap);
2497 ieee80211_vht_timeout(vap);
2501 * Per-ieee80211com inactivity timer callback.
2504 ieee80211_node_timeout(void *arg)
2506 struct ieee80211com *ic = arg;
2507 struct ieee80211vap *vap;
2510 * Defer timeout processing if a channel switch is pending.
2511 * We typically need to be mute so not doing things that
2512 * might generate frames is good to handle in one place.
2513 * Suppressing the station timeout processing may extend the
2514 * lifetime of inactive stations (by not decrementing their
2515 * idle counters) but this should be ok unless the CSA is
2516 * active for an unusually long time.
2518 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2519 ieee80211_scan_timeout(ic);
2520 ieee80211_timeout_stations(ic);
2521 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2524 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2525 ieee80211_vap_timeout(vap);
2526 IEEE80211_UNLOCK(ic);
2528 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2529 ieee80211_node_timeout, ic);
2533 * The same as ieee80211_iterate_nodes(), but for one vap only.
2536 ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
2537 struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
2539 struct ieee80211_node **ni_arr;
2540 struct ieee80211_node *ni;
2545 * Iterate over the node table and save an array of ref'ed nodes.
2547 * This is separated out from calling the actual node function so that
2548 * no LORs will occur.
2550 IEEE80211_NODE_LOCK(nt);
2551 count = nt->nt_count;
2552 size = count * sizeof(struct ieee80211_node *);
2553 ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
2554 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2555 if (ni_arr == NULL) {
2556 IEEE80211_NODE_UNLOCK(nt);
2561 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2562 if (vap != NULL && ni->ni_vap != vap)
2565 ("node array overflow (vap %p, i %d, count %d)\n",
2567 ni_arr[i] = ieee80211_ref_node(ni);
2570 IEEE80211_NODE_UNLOCK(nt);
2572 for (i = 0; i < count; i++) {
2573 if (ni_arr[i] == NULL) /* end of the list */
2575 (*f)(arg, ni_arr[i]);
2576 /* ieee80211_free_node() locks by itself */
2577 ieee80211_free_node(ni_arr[i]);
2580 IEEE80211_FREE(ni_arr, M_80211_NODE);
2586 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2587 * reference in the source.
2590 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2591 ieee80211_iter_func *f, void *arg)
2593 /* XXX no way to pass error to the caller. */
2594 (void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
2598 ieee80211_dump_node(struct ieee80211_node_table *nt __unused,
2599 struct ieee80211_node *ni)
2601 printf("%p: mac %s refcnt %d\n", ni,
2602 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2603 printf("\tauthmode %u flags 0x%x\n",
2604 ni->ni_authmode, ni->ni_flags);
2605 printf("\tassocid 0x%x txpower %u vlan %u\n",
2606 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2607 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2608 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2609 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2610 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2611 ni->ni_rxfragstamp);
2612 printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2613 node_getrssi(ni), ni->ni_noise,
2614 ni->ni_intval, ni->ni_capinfo);
2615 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2616 ether_sprintf(ni->ni_bssid),
2617 ni->ni_esslen, ni->ni_essid,
2618 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2619 printf("\tinact %u inact_reload %u txrate %u\n",
2620 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2621 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2622 ni->ni_htcap, ni->ni_htparam,
2623 ni->ni_htctlchan, ni->ni_ht2ndchan);
2624 printf("\thtopmode %x htstbc %x htchw %u\n",
2625 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2626 printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n",
2627 ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2,
2628 (int) ni->ni_vht_basicmcs);
2633 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2635 ieee80211_iterate_nodes(nt,
2636 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2640 * Iterate over the VAPs and update their ERP beacon IEs.
2642 * Note this must be called from the deferred ERP update task paths.
2645 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2647 struct ieee80211vap *vap;
2649 IEEE80211_LOCK_ASSERT(ic);
2651 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2652 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2653 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2657 * Handle a station joining an 11g network.
2660 ieee80211_node_join_11g(struct ieee80211_node *ni)
2662 struct ieee80211com *ic = ni->ni_ic;
2663 struct ieee80211vap *vap = ni->ni_vap;
2665 IEEE80211_LOCK_ASSERT(ic);
2668 * Station isn't capable of short slot time. Bump
2669 * the count of long slot time stations and disable
2670 * use of short slot time. Note that the actual switch
2671 * over to long slot time use may not occur until the
2672 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2674 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2675 vap->iv_longslotsta++;
2676 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2677 "station needs long slot time, count %d",
2678 vap->iv_longslotsta);
2680 * XXX TODO: this may need all VAPs checked!
2682 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2684 * Don't force slot time when switched to turbo
2685 * mode as non-ERP stations won't be present; this
2686 * need only be done when on the normal G channel.
2688 ieee80211_vap_set_shortslottime(vap, 0);
2692 * If the new station is not an ERP station
2693 * then bump the counter and enable protection
2696 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2697 vap->iv_nonerpsta++;
2698 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2699 "station is !ERP, %d non-ERP stations associated",
2702 * If station does not support short preamble
2703 * then we must enable use of Barker preamble.
2705 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2706 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2707 "%s", "station needs long preamble");
2708 vap->iv_flags |= IEEE80211_F_USEBARKER;
2709 vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE;
2710 ieee80211_vap_update_preamble(vap);
2713 * If protection is configured and this is the first
2714 * indication we should use protection, enable it.
2716 if (vap->iv_protmode != IEEE80211_PROT_NONE &&
2717 vap->iv_nonerpsta == 1 &&
2718 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2719 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2720 "%s: enable use of protection\n", __func__);
2721 vap->iv_flags |= IEEE80211_F_USEPROT;
2722 ieee80211_vap_update_erp_protmode(vap);
2725 ni->ni_flags |= IEEE80211_NODE_ERP;
2729 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2731 struct ieee80211com *ic = ni->ni_ic;
2732 struct ieee80211vap *vap = ni->ni_vap;
2735 if (ni->ni_associd == 0) {
2738 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2740 * It would be good to search the bitmap
2741 * more efficiently, but this will do for now.
2743 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2744 if (!IEEE80211_AID_ISSET(vap, aid))
2747 if (aid >= vap->iv_max_aid) {
2748 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2749 ieee80211_node_leave(ni);
2752 ni->ni_associd = aid | 0xc000;
2753 ni->ni_jointime = time_uptime;
2755 IEEE80211_AID_SET(vap, ni->ni_associd);
2756 vap->iv_sta_assoc++;
2758 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2759 ieee80211_ht_node_join(ni);
2760 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2761 ieee80211_vht_node_join(ni);
2762 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2763 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2764 ieee80211_node_join_11g(ni);
2765 IEEE80211_UNLOCK(ic);
2772 * XXX VHT - should log VHT channel width, etc
2774 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2775 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s%s",
2776 IEEE80211_NODE_AID(ni),
2777 vap->iv_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2778 vap->iv_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2779 vap->iv_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2780 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2781 /* XXX update for VHT string */
2782 ni->ni_flags & IEEE80211_NODE_HT ?
2783 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2784 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2785 ni->ni_flags & IEEE80211_NODE_AMSDU ? " (+AMSDU)" : "",
2786 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2787 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2788 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2789 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2790 ", fast-frames" : "",
2791 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2795 ieee80211_node_setuptxparms(ni);
2796 ieee80211_ratectl_node_init(ni);
2797 /* give driver a chance to setup state like ni_txrate */
2798 if (ic->ic_newassoc != NULL)
2799 ic->ic_newassoc(ni, newassoc);
2800 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2801 /* tell the authenticator about new station */
2802 if (vap->iv_auth->ia_node_join != NULL)
2803 vap->iv_auth->ia_node_join(ni);
2804 ieee80211_notify_node_join(ni,
2805 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2809 disable_protection(struct ieee80211vap *vap)
2811 struct ieee80211com *ic = vap->iv_ic;
2813 KASSERT(vap->iv_nonerpsta == 0 &&
2814 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2815 ("%d non ERP stations, flags 0x%x", vap->iv_nonerpsta,
2816 vap->iv_flags_ext));
2818 vap->iv_flags &= ~IEEE80211_F_USEPROT;
2819 /* XXX verify mode? */
2820 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2821 vap->iv_flags |= IEEE80211_F_SHPREAMBLE;
2822 vap->iv_flags &= ~IEEE80211_F_USEBARKER;
2824 ieee80211_vap_update_erp_protmode(vap);
2825 ieee80211_vap_update_preamble(vap);
2829 * Handle a station leaving an 11g network.
2832 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2834 struct ieee80211com *ic = ni->ni_ic;
2835 struct ieee80211vap *vap = ni->ni_vap;
2837 IEEE80211_LOCK_ASSERT(ic);
2839 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2840 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2841 ic->ic_bsschan->ic_flags));
2844 * If a long slot station do the slot time bookkeeping.
2846 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2847 KASSERT(vap->iv_longslotsta > 0,
2848 ("bogus long slot station count %d", vap->iv_longslotsta));
2849 vap->iv_longslotsta--;
2850 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2851 "long slot time station leaves, count now %d",
2852 vap->iv_longslotsta);
2854 * XXX TODO: this may need all VAPs checked!
2856 if (vap->iv_longslotsta == 0) {
2858 * Re-enable use of short slot time if supported
2859 * and not operating in IBSS mode (per spec).
2861 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2862 ic->ic_opmode != IEEE80211_M_IBSS) {
2863 IEEE80211_DPRINTF(ni->ni_vap,
2864 IEEE80211_MSG_ASSOC,
2865 "%s: re-enable use of short slot time\n",
2867 ieee80211_vap_set_shortslottime(vap, 1);
2872 * If a non-ERP station do the protection-related bookkeeping.
2874 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2875 KASSERT(vap->iv_nonerpsta > 0,
2876 ("bogus non-ERP station count %d", vap->iv_nonerpsta));
2877 vap->iv_nonerpsta--;
2878 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2879 "non-ERP station leaves, count now %d%s", vap->iv_nonerpsta,
2880 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2881 " (non-ERP sta present)" : "");
2882 if (vap->iv_nonerpsta == 0 &&
2883 (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2884 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2885 "%s: disable use of protection\n", __func__);
2886 disable_protection(vap);
2892 * Time out presence of an overlapping bss with non-ERP
2893 * stations. When operating in hostap mode we listen for
2894 * beacons from other stations and if we identify a non-ERP
2895 * station is present we enable protection. To identify
2896 * when all non-ERP stations are gone we time out this
2900 ieee80211_vap_erp_timeout(struct ieee80211vap *vap)
2903 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2905 if ((vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2906 ieee80211_time_after(ticks, vap->iv_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2907 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
2908 "%s", "age out non-ERP sta present on channel");
2909 vap->iv_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2910 if (vap->iv_nonerpsta == 0)
2911 disable_protection(vap);
2916 * Handle bookkeeping for station deauthentication/disassociation
2917 * when operating as an ap.
2920 ieee80211_node_leave(struct ieee80211_node *ni)
2922 struct ieee80211com *ic = ni->ni_ic;
2923 struct ieee80211vap *vap = ni->ni_vap;
2924 struct ieee80211_node_table *nt = ni->ni_table;
2926 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2927 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2929 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2930 ("unexpected operating mode %u", vap->iv_opmode));
2932 * If node wasn't previously associated all
2933 * we need to do is reclaim the reference.
2935 /* XXX ibss mode bypasses 11g and notification */
2936 if (ni->ni_associd == 0)
2939 * Tell the authenticator the station is leaving.
2940 * Note that we must do this before yanking the
2941 * association id as the authenticator uses the
2942 * associd to locate it's state block.
2944 if (vap->iv_auth->ia_node_leave != NULL)
2945 vap->iv_auth->ia_node_leave(ni);
2948 IEEE80211_AID_CLR(vap, ni->ni_associd);
2949 vap->iv_sta_assoc--;
2951 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2952 ieee80211_vht_node_leave(ni);
2953 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2954 ieee80211_ht_node_leave(ni);
2955 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2956 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2957 ieee80211_node_leave_11g(ni);
2958 IEEE80211_UNLOCK(ic);
2960 * Cleanup station state. In particular clear various
2961 * state that might otherwise be reused if the node
2962 * is reused before the reference count goes to zero
2963 * (and memory is reclaimed).
2965 ieee80211_sta_leave(ni);
2968 * Remove the node from any table it's recorded in and
2969 * drop the caller's reference. Removal from the table
2970 * is important to insure the node is not reprocessed
2974 IEEE80211_NODE_LOCK(nt);
2975 node_reclaim(nt, ni);
2976 IEEE80211_NODE_UNLOCK(nt);
2978 ieee80211_free_node(ni);
2983 uint32_t rssi_total;
2987 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2989 struct rssiinfo *info = arg;
2990 struct ieee80211vap *vap = ni->ni_vap;
2993 /* only associated stations */
2994 if (ni->ni_associd == 0)
2996 rssi = vap->iv_ic->ic_node_getrssi(ni);
2998 info->rssi_samples++;
2999 info->rssi_total += rssi;
3004 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
3006 struct rssiinfo *info = arg;
3007 struct ieee80211vap *vap = ni->ni_vap;
3010 /* only neighbors */
3011 /* XXX check bssid */
3012 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
3014 rssi = vap->iv_ic->ic_node_getrssi(ni);
3016 info->rssi_samples++;
3017 info->rssi_total += rssi;
3021 #ifdef IEEE80211_SUPPORT_MESH
3023 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
3025 struct rssiinfo *info = arg;
3026 struct ieee80211vap *vap = ni->ni_vap;
3029 /* only neighbors that peered successfully */
3030 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
3032 rssi = vap->iv_ic->ic_node_getrssi(ni);
3034 info->rssi_samples++;
3035 info->rssi_total += rssi;
3038 #endif /* IEEE80211_SUPPORT_MESH */
3041 ieee80211_getrssi(struct ieee80211vap *vap)
3043 #define NZ(x) ((x) == 0 ? 1 : (x))
3044 struct ieee80211com *ic = vap->iv_ic;
3045 struct rssiinfo info;
3047 info.rssi_total = 0;
3048 info.rssi_samples = 0;
3049 switch (vap->iv_opmode) {
3050 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
3051 case IEEE80211_M_AHDEMO: /* average of all neighbors */
3052 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
3055 case IEEE80211_M_HOSTAP: /* average of all associated stations */
3056 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
3059 #ifdef IEEE80211_SUPPORT_MESH
3060 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
3061 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
3065 case IEEE80211_M_MONITOR: /* XXX */
3066 case IEEE80211_M_STA: /* use stats from associated ap */
3068 if (vap->iv_bss != NULL)
3069 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
3070 info.rssi_samples = 1;
3073 return info.rssi_total / NZ(info.rssi_samples);
3078 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
3081 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
3083 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
3084 /* for non-station mode return avg'd rssi accounting */
3085 if (vap->iv_opmode != IEEE80211_M_STA)
3086 *rssi = ieee80211_getrssi(vap);