2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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>
60 * IEEE80211_NODE_HASHSIZE must be a power of 2.
62 CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
65 * Association id's are managed with a bit vector.
67 #define IEEE80211_AID_SET(_vap, b) \
68 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
69 (1 << (IEEE80211_AID(b) % 32)))
70 #define IEEE80211_AID_CLR(_vap, b) \
71 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
72 ~(1 << (IEEE80211_AID(b) % 32)))
73 #define IEEE80211_AID_ISSET(_vap, b) \
74 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
76 static int ieee80211_sta_join1(struct ieee80211_node *);
78 static struct ieee80211_node *node_alloc(struct ieee80211vap *,
79 const uint8_t [IEEE80211_ADDR_LEN]);
80 static void node_cleanup(struct ieee80211_node *);
81 static void node_free(struct ieee80211_node *);
82 static void node_age(struct ieee80211_node *);
83 static int8_t node_getrssi(const struct ieee80211_node *);
84 static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
85 static void node_getmimoinfo(const struct ieee80211_node *,
86 struct ieee80211_mimo_info *);
88 static void _ieee80211_free_node(struct ieee80211_node *);
90 static void node_reclaim(struct ieee80211_node_table *nt,
91 struct ieee80211_node *ni);
92 static void ieee80211_node_table_init(struct ieee80211com *ic,
93 struct ieee80211_node_table *nt, const char *name,
94 int inact, int keymaxix);
95 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
96 struct ieee80211vap *);
97 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
98 static void ieee80211_erp_timeout(struct ieee80211com *);
100 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
101 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
104 ieee80211_node_attach(struct ieee80211com *ic)
106 /* XXX really want maxlen enforced per-sta */
107 ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
109 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
110 IEEE80211_INACT_INIT, ic->ic_max_keyix);
111 callout_init(&ic->ic_inact, 1);
112 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
113 ieee80211_node_timeout, ic);
115 ic->ic_node_alloc = node_alloc;
116 ic->ic_node_free = node_free;
117 ic->ic_node_cleanup = node_cleanup;
118 ic->ic_node_age = node_age;
119 ic->ic_node_drain = node_age; /* NB: same as age */
120 ic->ic_node_getrssi = node_getrssi;
121 ic->ic_node_getsignal = node_getsignal;
122 ic->ic_node_getmimoinfo = node_getmimoinfo;
125 * Set flags to be propagated to all vap's;
126 * these define default behaviour/configuration.
128 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
132 ieee80211_node_detach(struct ieee80211com *ic)
135 callout_drain(&ic->ic_inact);
136 ieee80211_node_table_cleanup(&ic->ic_sta);
137 ieee80211_ageq_cleanup(&ic->ic_stageq);
141 ieee80211_node_vattach(struct ieee80211vap *vap)
143 /* NB: driver can override */
144 vap->iv_max_aid = IEEE80211_AID_DEF;
146 /* default station inactivity timer setings */
147 vap->iv_inact_init = IEEE80211_INACT_INIT;
148 vap->iv_inact_auth = IEEE80211_INACT_AUTH;
149 vap->iv_inact_run = IEEE80211_INACT_RUN;
150 vap->iv_inact_probe = IEEE80211_INACT_PROBE;
152 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
153 "%s: init %u auth %u run %u probe %u\n", __func__,
154 vap->iv_inact_init, vap->iv_inact_auth,
155 vap->iv_inact_run, vap->iv_inact_probe);
159 ieee80211_node_latevattach(struct ieee80211vap *vap)
161 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
162 /* XXX should we allow max aid to be zero? */
163 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
164 vap->iv_max_aid = IEEE80211_AID_MIN;
165 if_printf(vap->iv_ifp,
166 "WARNING: max aid too small, changed to %d\n",
169 vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC(
170 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
172 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
173 if (vap->iv_aid_bitmap == NULL) {
174 /* XXX no way to recover */
175 printf("%s: no memory for AID bitmap, max aid %d!\n",
176 __func__, vap->iv_max_aid);
181 ieee80211_reset_bss(vap);
183 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
187 ieee80211_node_vdetach(struct ieee80211vap *vap)
189 struct ieee80211com *ic = vap->iv_ic;
191 ieee80211_node_table_reset(&ic->ic_sta, vap);
192 if (vap->iv_bss != NULL) {
193 ieee80211_free_node(vap->iv_bss);
196 if (vap->iv_aid_bitmap != NULL) {
197 IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE);
198 vap->iv_aid_bitmap = NULL;
203 * Port authorize/unauthorize interfaces for use by an authenticator.
207 ieee80211_node_authorize(struct ieee80211_node *ni)
209 struct ieee80211vap *vap = ni->ni_vap;
211 ni->ni_flags |= IEEE80211_NODE_AUTH;
212 ni->ni_inact_reload = vap->iv_inact_run;
213 ni->ni_inact = ni->ni_inact_reload;
215 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
216 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
220 ieee80211_node_unauthorize(struct ieee80211_node *ni)
222 struct ieee80211vap *vap = ni->ni_vap;
224 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
225 ni->ni_inact_reload = vap->iv_inact_auth;
226 if (ni->ni_inact > ni->ni_inact_reload)
227 ni->ni_inact = ni->ni_inact_reload;
229 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
230 "%s: inact_reload %u inact %u", __func__,
231 ni->ni_inact_reload, ni->ni_inact);
235 * Fix tx parameters for a node according to ``association state''.
238 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
240 struct ieee80211vap *vap = ni->ni_vap;
241 enum ieee80211_phymode mode;
243 if (ni->ni_flags & IEEE80211_NODE_HT) {
244 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
245 mode = IEEE80211_MODE_11NA;
247 mode = IEEE80211_MODE_11NG;
248 } else { /* legacy rate handling */
249 if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
250 mode = IEEE80211_MODE_STURBO_A;
251 else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
252 mode = IEEE80211_MODE_HALF;
253 else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
254 mode = IEEE80211_MODE_QUARTER;
255 /* NB: 108A should be handled as 11a */
256 else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
257 mode = IEEE80211_MODE_11A;
258 else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
259 (ni->ni_flags & IEEE80211_NODE_ERP))
260 mode = IEEE80211_MODE_11G;
262 mode = IEEE80211_MODE_11B;
264 ni->ni_txparms = &vap->iv_txparms[mode];
268 * Set/change the channel. The rate set is also updated as
269 * to insure a consistent view by drivers.
270 * XXX should be private but hostap needs it to deal with CSA
273 ieee80211_node_set_chan(struct ieee80211_node *ni,
274 struct ieee80211_channel *chan)
276 struct ieee80211com *ic = ni->ni_ic;
277 struct ieee80211vap *vap = ni->ni_vap;
278 enum ieee80211_phymode mode;
280 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
283 mode = ieee80211_chan2mode(chan);
284 if (IEEE80211_IS_CHAN_HT(chan)) {
286 * We must install the legacy rate est in ni_rates and the
287 * HT rate set in ni_htrates.
289 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
291 * Setup bss tx parameters based on operating mode. We
292 * use legacy rates when operating in a mixed HT+non-HT bss
293 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
295 if (mode == IEEE80211_MODE_11NA &&
296 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
297 mode = IEEE80211_MODE_11A;
298 else if (mode == IEEE80211_MODE_11NG &&
299 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
300 mode = IEEE80211_MODE_11G;
301 if (mode == IEEE80211_MODE_11G &&
302 (vap->iv_flags & IEEE80211_F_PUREG) == 0)
303 mode = IEEE80211_MODE_11B;
305 ni->ni_txparms = &vap->iv_txparms[mode];
306 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
310 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
312 /* propagate useful state */
313 nbss->ni_authmode = obss->ni_authmode;
314 nbss->ni_txpower = obss->ni_txpower;
315 nbss->ni_vlan = obss->ni_vlan;
316 /* XXX statistics? */
317 /* XXX legacy WDS bssid? */
321 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
323 struct ieee80211com *ic = vap->iv_ic;
324 struct ieee80211_node *ni;
326 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
327 "%s: creating %s on channel %u%c flags 0x%08x\n", __func__,
328 ieee80211_opmode_name[vap->iv_opmode],
329 ieee80211_chan2ieee(ic, chan),
330 ieee80211_channel_type_char(chan),
333 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
338 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
339 ni->ni_esslen = vap->iv_des_ssid[0].len;
340 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
341 if (vap->iv_bss != NULL)
342 copy_bss(ni, vap->iv_bss);
343 ni->ni_intval = ic->ic_bintval;
344 if (vap->iv_flags & IEEE80211_F_PRIVACY)
345 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
346 if (ic->ic_phytype == IEEE80211_T_FH) {
347 ni->ni_fhdwell = 200; /* XXX */
350 if (vap->iv_opmode == IEEE80211_M_IBSS) {
351 vap->iv_flags |= IEEE80211_F_SIBSS;
352 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
353 if (vap->iv_flags & IEEE80211_F_DESBSSID)
354 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
356 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
357 /* clear group bit, add local bit */
358 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
360 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
361 if (vap->iv_flags & IEEE80211_F_DESBSSID)
362 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
364 #ifdef IEEE80211_SUPPORT_TDMA
365 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
367 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
368 #ifdef IEEE80211_SUPPORT_MESH
369 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
370 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
371 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
375 * Fix the channel and related attributes.
377 /* clear DFS CAC state on previous channel */
378 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
379 ic->ic_bsschan->ic_freq != chan->ic_freq &&
380 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
381 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
382 ic->ic_bsschan = chan;
383 ieee80211_node_set_chan(ni, chan);
384 ic->ic_curmode = ieee80211_chan2mode(chan);
386 * Do mode-specific setup.
388 if (IEEE80211_IS_CHAN_FULL(chan)) {
389 if (IEEE80211_IS_CHAN_ANYG(chan)) {
391 * Use a mixed 11b/11g basic rate set.
393 ieee80211_setbasicrates(&ni->ni_rates,
395 if (vap->iv_flags & IEEE80211_F_PUREG) {
397 * Also mark OFDM rates basic so 11b
398 * stations do not join (WiFi compliance).
400 ieee80211_addbasicrates(&ni->ni_rates,
403 } else if (IEEE80211_IS_CHAN_B(chan)) {
405 * Force pure 11b rate set.
407 ieee80211_setbasicrates(&ni->ni_rates,
412 /* XXX TODO: other bits and pieces - eg fast-frames? */
414 /* If we're an 11n channel then initialise the 11n bits */
415 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
417 ieee80211_ht_node_init(ni);
420 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
424 * Reset bss state on transition to the INIT state.
425 * Clear any stations from the table (they have been
426 * deauth'd) and reset the bss node (clears key, rate
430 ieee80211_reset_bss(struct ieee80211vap *vap)
432 struct ieee80211com *ic = vap->iv_ic;
433 struct ieee80211_node *ni, *obss;
435 ieee80211_node_table_reset(&ic->ic_sta, vap);
436 /* XXX multi-bss: wrong */
437 ieee80211_reset_erp(ic);
439 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
440 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
442 vap->iv_bss = ieee80211_ref_node(ni);
445 ni->ni_intval = ic->ic_bintval;
446 ieee80211_free_node(obss);
448 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
452 match_ssid(const struct ieee80211_node *ni,
453 int nssid, const struct ieee80211_scan_ssid ssids[])
457 for (i = 0; i < nssid; i++) {
458 if (ni->ni_esslen == ssids[i].len &&
459 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
466 * Test a node for suitability/compatibility.
469 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
471 struct ieee80211com *ic = ni->ni_ic;
474 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
476 if (vap->iv_opmode == IEEE80211_M_IBSS) {
477 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
480 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
483 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
484 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
487 /* XXX does this mean privacy is supported or required? */
488 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
491 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
492 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
493 if (rate & IEEE80211_RATE_BASIC)
495 if (vap->iv_des_nssid != 0 &&
496 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
498 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
499 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
504 #ifdef IEEE80211_DEBUG
506 * Display node suitability/compatibility.
509 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
511 struct ieee80211com *ic = ni->ni_ic;
516 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
518 if (vap->iv_opmode == IEEE80211_M_IBSS) {
519 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
522 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
525 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
526 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
529 /* XXX does this mean privacy is supported or required? */
530 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
533 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
534 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
535 if (rate & IEEE80211_RATE_BASIC)
537 if (vap->iv_des_nssid != 0 &&
538 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
540 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
541 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
544 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
545 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
547 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
548 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
549 fail & 0x08 ? '!' : ' ');
551 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
552 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
554 fail & 0x02 ? '!' : ' ');
556 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
557 fail & 0x04 ? '!' : ' ');
558 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
559 printf("%s\n", fail & 0x10 ? "!" : "");
561 #endif /* IEEE80211_DEBUG */
565 ieee80211_ibss_merge_check(struct ieee80211_node *ni)
567 struct ieee80211vap *vap = ni->ni_vap;
569 if (ni == vap->iv_bss ||
570 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
571 /* unchanged, nothing to do */
575 if (!check_bss(vap, ni)) {
576 /* capabilities mismatch */
577 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
578 "%s: merge failed, capabilities mismatch\n", __func__);
579 #ifdef IEEE80211_DEBUG
580 if (ieee80211_msg_assoc(vap))
581 check_bss_debug(vap, ni);
583 vap->iv_stats.is_ibss_capmismatch++;
591 * Check if the given node should populate the node table.
593 * We need to be in "see all beacons for all ssids" mode in order
594 * to do IBSS merges, however this means we will populate nodes for
595 * /all/ IBSS SSIDs, versus just the one we care about.
597 * So this check ensures the node can actually belong to our IBSS
598 * configuration. For now it simply checks the SSID.
601 ieee80211_ibss_node_check_new(struct ieee80211_node *ni,
602 const struct ieee80211_scanparams *scan)
604 struct ieee80211vap *vap = ni->ni_vap;
608 * If we have no SSID and no scan SSID, return OK.
610 if (vap->iv_des_nssid == 0 && scan->ssid == NULL)
614 * If we have one of (SSID, scan SSID) then return error.
616 if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL))
620 * Double-check - we need scan SSID.
622 if (scan->ssid == NULL)
626 * Check if the scan SSID matches the SSID list for the VAP.
628 for (i = 0; i < vap->iv_des_nssid; i++) {
630 /* Sanity length check */
631 if (vap->iv_des_ssid[i].len != scan->ssid[1])
634 /* Note: SSID in the scan entry is the IE format */
635 if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2,
636 vap->iv_des_ssid[i].len) == 0)
647 * Handle 802.11 ad hoc network merge. The
648 * convention, set by the Wireless Ethernet Compatibility Alliance
649 * (WECA), is that an 802.11 station will change its BSSID to match
650 * the "oldest" 802.11 ad hoc network, on the same channel, that
651 * has the station's desired SSID. The "oldest" 802.11 network
652 * sends beacons with the greatest TSF timestamp.
654 * The caller is assumed to validate TSF's before attempting a merge.
656 * Return !0 if the BSSID changed, 0 otherwise.
659 ieee80211_ibss_merge(struct ieee80211_node *ni)
661 #ifdef IEEE80211_DEBUG
662 struct ieee80211vap *vap = ni->ni_vap;
663 struct ieee80211com *ic = ni->ni_ic;
666 if (! ieee80211_ibss_merge_check(ni))
669 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
670 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
671 ether_sprintf(ni->ni_bssid),
672 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
673 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
674 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
676 return ieee80211_sta_join1(ieee80211_ref_node(ni));
680 * Calculate HT channel promotion flags for all vaps.
681 * This assumes ni_chan have been setup for each vap.
684 gethtadjustflags(struct ieee80211com *ic)
686 struct ieee80211vap *vap;
691 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
692 if (vap->iv_state < IEEE80211_S_RUN)
694 switch (vap->iv_opmode) {
695 case IEEE80211_M_WDS:
696 case IEEE80211_M_STA:
697 case IEEE80211_M_AHDEMO:
698 case IEEE80211_M_HOSTAP:
699 case IEEE80211_M_IBSS:
700 case IEEE80211_M_MBSS:
701 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
711 * Check if the current channel needs to change based on whether
712 * any vap's are using HT20/HT40. This is used to sync the state
713 * of ic_curchan after a channel width change on a running vap.
716 ieee80211_sync_curchan(struct ieee80211com *ic)
718 struct ieee80211_channel *c;
720 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
721 if (c != ic->ic_curchan) {
723 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
724 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
725 IEEE80211_UNLOCK(ic);
726 ic->ic_set_channel(ic);
727 ieee80211_radiotap_chan_change(ic);
733 * Setup the current channel. The request channel may be
734 * promoted if other vap's are operating with HT20/HT40.
737 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
739 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
740 int flags = gethtadjustflags(ic);
742 * Check for channel promotion required to support the
743 * set of running vap's. This assumes we are called
744 * after ni_chan is setup for each vap.
746 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
747 if (flags > ieee80211_htchanflags(c))
748 c = ieee80211_ht_adjust_channel(ic, c, flags);
750 ic->ic_bsschan = ic->ic_curchan = c;
751 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
752 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
756 * Change the current channel. The channel change is guaranteed to have
757 * happened before the next state change.
760 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
762 ieee80211_setupcurchan(ic, c);
763 ieee80211_runtask(ic, &ic->ic_chan_task);
767 ieee80211_update_chw(struct ieee80211com *ic)
770 ieee80211_setupcurchan(ic, ic->ic_curchan);
771 ieee80211_runtask(ic, &ic->ic_chw_task);
775 * Join the specified IBSS/BSS network. The node is assumed to
776 * be passed in with a held reference.
779 ieee80211_sta_join1(struct ieee80211_node *selbs)
781 struct ieee80211vap *vap = selbs->ni_vap;
782 struct ieee80211com *ic = selbs->ni_ic;
783 struct ieee80211_node *obss;
787 * Committed to selbs, setup state.
791 * Check if old+new node have the same address in which
792 * case we can reassociate when operating in sta mode.
794 canreassoc = (obss != NULL &&
795 vap->iv_state == IEEE80211_S_RUN &&
796 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
797 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */
799 struct ieee80211_node_table *nt = obss->ni_table;
801 copy_bss(selbs, obss);
802 ieee80211_node_decref(obss); /* iv_bss reference */
804 IEEE80211_NODE_LOCK(nt);
805 node_reclaim(nt, obss); /* station table reference */
806 IEEE80211_NODE_UNLOCK(nt);
808 obss = NULL; /* NB: guard against later use */
812 * Delete unusable rates; we've already checked
813 * that the negotiated rate set is acceptable.
815 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
816 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
818 ieee80211_setcurchan(ic, selbs->ni_chan);
820 * Set the erp state (mostly the slot time) to deal with
821 * the auto-select case; this should be redundant if the
824 ieee80211_reset_erp(ic);
825 ieee80211_wme_initparams(vap);
827 if (vap->iv_opmode == IEEE80211_M_STA) {
830 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
833 * Act as if we received a DEAUTH frame in case we
834 * are invoked from the RUN state. This will cause
835 * us to try to re-authenticate if we are operating
838 ieee80211_new_state(vap, IEEE80211_S_AUTH,
839 IEEE80211_FC0_SUBTYPE_DEAUTH);
842 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
847 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
848 const struct ieee80211_scan_entry *se)
850 struct ieee80211com *ic = vap->iv_ic;
851 struct ieee80211_node *ni;
853 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
860 * Expand scan state into node's format.
861 * XXX may not need all this stuff
863 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
864 ni->ni_esslen = se->se_ssid[1];
865 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
866 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
867 ni->ni_intval = se->se_intval;
868 ni->ni_capinfo = se->se_capinfo;
870 ni->ni_timoff = se->se_timoff;
871 ni->ni_fhdwell = se->se_fhdwell;
872 ni->ni_fhindex = se->se_fhindex;
873 ni->ni_erp = se->se_erp;
874 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
875 ni->ni_noise = se->se_noise;
876 if (vap->iv_opmode == IEEE80211_M_STA) {
877 /* NB: only infrastructure mode requires an associd */
878 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
881 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
882 ieee80211_ies_expand(&ni->ni_ies);
883 #ifdef IEEE80211_SUPPORT_SUPERG
884 if (ni->ni_ies.ath_ie != NULL)
885 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
887 if (ni->ni_ies.htcap_ie != NULL)
888 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
889 if (ni->ni_ies.htinfo_ie != NULL)
890 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
891 #ifdef IEEE80211_SUPPORT_MESH
892 if (ni->ni_ies.meshid_ie != NULL)
893 ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
895 #ifdef IEEE80211_SUPPORT_TDMA
896 if (ni->ni_ies.tdma_ie != NULL)
897 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
900 /* XXX parse VHT IEs */
901 /* XXX parse BSSLOAD IE */
902 /* XXX parse APCHANREP IE */
905 vap->iv_dtim_period = se->se_dtimperiod;
906 vap->iv_dtim_count = 0;
908 /* NB: must be after ni_chan is setup */
909 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
911 if (ieee80211_iserp_rateset(&ni->ni_rates))
912 ni->ni_flags |= IEEE80211_NODE_ERP;
915 * Setup HT state for this node if it's available, otherwise
916 * non-STA modes won't pick this state up.
918 * For IBSS and related modes that don't go through an
919 * association request/response, the only appropriate place
920 * to setup the HT state is here.
922 if (ni->ni_ies.htinfo_ie != NULL &&
923 ni->ni_ies.htcap_ie != NULL &&
924 vap->iv_flags_ht & IEEE80211_FHT_HT) {
925 ieee80211_ht_node_init(ni);
926 ieee80211_ht_updateparams(ni,
928 ni->ni_ies.htinfo_ie);
929 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
930 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
931 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
933 /* XXX else check for ath FF? */
934 /* XXX QoS? Difficult given that WME config is specific to a master */
936 ieee80211_node_setuptxparms(ni);
937 ieee80211_ratectl_node_init(ni);
939 return ieee80211_sta_join1(ieee80211_ref_node(ni));
943 * Leave the specified IBSS/BSS network. The node is assumed to
944 * be passed in with a held reference.
947 ieee80211_sta_leave(struct ieee80211_node *ni)
949 struct ieee80211com *ic = ni->ni_ic;
951 ic->ic_node_cleanup(ni);
952 ieee80211_notify_node_leave(ni);
956 * Send a deauthenticate frame and drop the station.
959 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
961 /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
962 ieee80211_ref_node(ni);
963 if (ni->ni_associd != 0)
964 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
965 ieee80211_node_leave(ni);
966 ieee80211_free_node(ni);
969 static struct ieee80211_node *
970 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
972 struct ieee80211_node *ni;
974 ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
975 M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
980 * Initialize an ie blob with the specified data. If previous
981 * data exists re-use the data block. As a side effect we clear
982 * all references to specific ie's; the caller is required to
986 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
988 /* NB: assumes data+len are the last fields */
989 memset(ies, 0, offsetof(struct ieee80211_ies, data));
990 if (ies->data != NULL && ies->len != len) {
991 /* data size changed */
992 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
995 if (ies->data == NULL) {
996 ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
997 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
998 if (ies->data == NULL) {
1000 /* NB: pointers have already been zero'd above */
1004 memcpy(ies->data, data, len);
1010 * Reclaim storage for an ie blob.
1013 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
1015 if (ies->data != NULL)
1016 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1020 * Expand an ie blob data contents and to fillin individual
1021 * ie pointers. The data blob is assumed to be well-formed;
1022 * we don't do any validity checking of ie lengths.
1025 ieee80211_ies_expand(struct ieee80211_ies *ies)
1034 case IEEE80211_ELEMID_VENDOR:
1037 else if (iswmeoui(ie))
1039 #ifdef IEEE80211_SUPPORT_SUPERG
1040 else if (isatherosoui(ie))
1043 #ifdef IEEE80211_SUPPORT_TDMA
1044 else if (istdmaoui(ie))
1048 case IEEE80211_ELEMID_RSN:
1051 case IEEE80211_ELEMID_HTCAP:
1054 case IEEE80211_ELEMID_HTINFO:
1055 ies->htinfo_ie = ie;
1057 #ifdef IEEE80211_SUPPORT_MESH
1058 case IEEE80211_ELEMID_MESHID:
1059 ies->meshid_ie = ie;
1062 case IEEE80211_ELEMID_VHT_CAP:
1063 ies->vhtcap_ie = ie;
1065 case IEEE80211_ELEMID_VHT_OPMODE:
1066 ies->vhtopmode_ie = ie;
1068 case IEEE80211_ELEMID_VHT_PWR_ENV:
1069 ies->vhtpwrenv_ie = ie;
1071 case IEEE80211_ELEMID_BSSLOAD:
1072 ies->bssload_ie = ie;
1074 case IEEE80211_ELEMID_APCHANREP:
1075 ies->apchanrep_ie = ie;
1084 * Reclaim any resources in a node and reset any critical
1085 * state. Typically nodes are free'd immediately after,
1086 * but in some cases the storage may be reused so we need
1087 * to insure consistent state (should probably fix that).
1090 node_cleanup(struct ieee80211_node *ni)
1092 struct ieee80211vap *vap = ni->ni_vap;
1093 struct ieee80211com *ic = ni->ni_ic;
1096 /* NB: preserve ni_table */
1097 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1098 if (vap->iv_opmode != IEEE80211_M_STA)
1100 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
1101 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
1102 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
1105 * Cleanup any HT-related state.
1107 if (ni->ni_flags & IEEE80211_NODE_HT)
1108 ieee80211_ht_node_cleanup(ni);
1109 #ifdef IEEE80211_SUPPORT_SUPERG
1110 /* Always do FF node cleanup; for A-MSDU */
1111 ieee80211_ff_node_cleanup(ni);
1113 #ifdef IEEE80211_SUPPORT_MESH
1115 * Cleanup any mesh-related state.
1117 if (vap->iv_opmode == IEEE80211_M_MBSS)
1118 ieee80211_mesh_node_cleanup(ni);
1121 * Clear any staging queue entries.
1123 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1126 * Clear AREF flag that marks the authorization refcnt bump
1127 * has happened. This is probably not needed as the node
1128 * should always be removed from the table so not found but
1129 * do it just in case.
1130 * Likewise clear the ASSOCID flag as these flags are intended
1131 * to be managed in tandem.
1133 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1136 * Drain power save queue and, if needed, clear TIM.
1138 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1139 vap->iv_set_tim(ni, 0);
1142 if (ni->ni_challenge != NULL) {
1143 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1144 ni->ni_challenge = NULL;
1147 * Preserve SSID, WPA, and WME ie's so the bss node is
1148 * reusable during a re-auth/re-assoc state transition.
1149 * If we remove these data they will not be recreated
1150 * because they come from a probe-response or beacon frame
1151 * which cannot be expected prior to the association-response.
1152 * This should not be an issue when operating in other modes
1153 * as stations leaving always go through a full state transition
1154 * which will rebuild this state.
1156 * XXX does this leave us open to inheriting old state?
1158 for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1159 if (ni->ni_rxfrag[i] != NULL) {
1160 m_freem(ni->ni_rxfrag[i]);
1161 ni->ni_rxfrag[i] = NULL;
1164 * Must be careful here to remove any key map entry w/o a LOR.
1166 ieee80211_node_delucastkey(ni);
1170 node_free(struct ieee80211_node *ni)
1172 struct ieee80211com *ic = ni->ni_ic;
1174 ieee80211_ratectl_node_deinit(ni);
1175 ic->ic_node_cleanup(ni);
1176 ieee80211_ies_cleanup(&ni->ni_ies);
1177 ieee80211_psq_cleanup(&ni->ni_psq);
1178 IEEE80211_FREE(ni, M_80211_NODE);
1182 node_age(struct ieee80211_node *ni)
1184 struct ieee80211vap *vap = ni->ni_vap;
1187 * Age frames on the power save queue.
1189 if (ieee80211_node_psq_age(ni) != 0 &&
1190 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1191 vap->iv_set_tim(ni, 0);
1193 * Age out HT resources (e.g. frames on the
1194 * A-MPDU reorder queues).
1196 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1197 ieee80211_ht_node_age(ni);
1201 node_getrssi(const struct ieee80211_node *ni)
1203 uint32_t avgrssi = ni->ni_avgrssi;
1206 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1208 rssi = IEEE80211_RSSI_GET(avgrssi);
1209 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1213 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1215 *rssi = node_getrssi(ni);
1216 *noise = ni->ni_noise;
1220 node_getmimoinfo(const struct ieee80211_node *ni,
1221 struct ieee80211_mimo_info *info)
1227 bzero(info, sizeof(*info));
1229 for (i = 0; i < ni->ni_mimo_chains; i++) {
1230 avgrssi = ni->ni_mimo_rssi_ctl[i];
1231 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1234 rssi = IEEE80211_RSSI_GET(avgrssi);
1235 info->rssi[i] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1237 info->noise[i] = ni->ni_mimo_noise_ctl[i];
1240 /* XXX ext radios? */
1246 ieee80211_add_node_nt(struct ieee80211_node_table *nt,
1247 struct ieee80211_node *ni)
1249 struct ieee80211com *ic = nt->nt_ic;
1252 IEEE80211_NODE_LOCK_ASSERT(nt);
1254 hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr);
1255 (void) ic; /* XXX IEEE80211_NODE_HASH */
1256 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1257 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1263 ieee80211_del_node_nt(struct ieee80211_node_table *nt,
1264 struct ieee80211_node *ni)
1267 IEEE80211_NODE_LOCK_ASSERT(nt);
1269 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1270 LIST_REMOVE(ni, ni_hash);
1272 KASSERT(nt->nt_count >= 0,
1273 ("nt_count is negative (%d)!\n", nt->nt_count));
1274 ni->ni_table = NULL;
1277 struct ieee80211_node *
1278 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1279 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1281 struct ieee80211com *ic = nt->nt_ic;
1282 struct ieee80211_node *ni;
1284 ni = ic->ic_node_alloc(vap, macaddr);
1286 vap->iv_stats.is_rx_nodealloc++;
1290 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1291 "%s %p<%s> in %s table\n", __func__, ni,
1292 ether_sprintf(macaddr), nt->nt_name);
1294 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1295 ieee80211_node_initref(ni); /* mark referenced */
1296 ni->ni_chan = IEEE80211_CHAN_ANYC;
1297 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1298 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1299 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1300 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1301 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1302 ni->ni_inact_reload = nt->nt_inact_init;
1303 ni->ni_inact = ni->ni_inact_reload;
1304 ni->ni_ath_defkeyix = 0x7fff;
1305 ieee80211_psq_init(&ni->ni_psq, "unknown");
1306 #ifdef IEEE80211_SUPPORT_MESH
1307 if (vap->iv_opmode == IEEE80211_M_MBSS)
1308 ieee80211_mesh_node_init(vap, ni);
1310 IEEE80211_NODE_LOCK(nt);
1311 ieee80211_add_node_nt(nt, ni);
1314 IEEE80211_NODE_UNLOCK(nt);
1316 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1317 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1319 ieee80211_ratectl_node_init(ni);
1325 * Craft a temporary node suitable for sending a management frame
1326 * to the specified station. We craft only as much state as we
1327 * need to do the work since the node will be immediately reclaimed
1328 * once the send completes.
1330 struct ieee80211_node *
1331 ieee80211_tmp_node(struct ieee80211vap *vap,
1332 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1334 struct ieee80211com *ic = vap->iv_ic;
1335 struct ieee80211_node *ni;
1337 ni = ic->ic_node_alloc(vap, macaddr);
1339 struct ieee80211_node *bss = vap->iv_bss;
1341 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1342 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1344 ni->ni_table = NULL; /* NB: pedantic */
1345 ni->ni_ic = ic; /* NB: needed to set channel */
1348 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1349 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1350 ieee80211_node_initref(ni); /* mark referenced */
1351 /* NB: required by ieee80211_fix_rate */
1352 ieee80211_node_set_chan(ni, bss->ni_chan);
1353 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1354 IEEE80211_KEYIX_NONE);
1355 ni->ni_txpower = bss->ni_txpower;
1356 /* XXX optimize away */
1357 ieee80211_psq_init(&ni->ni_psq, "unknown");
1359 ieee80211_ratectl_node_init(ni);
1362 vap->iv_stats.is_rx_nodealloc++;
1367 struct ieee80211_node *
1368 ieee80211_dup_bss(struct ieee80211vap *vap,
1369 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1371 struct ieee80211com *ic = vap->iv_ic;
1372 struct ieee80211_node *ni;
1374 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1376 struct ieee80211_node *bss = vap->iv_bss;
1378 * Inherit from iv_bss.
1381 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1382 ieee80211_node_set_chan(ni, bss->ni_chan);
1388 * Create a bss node for a legacy WDS vap. The far end does
1389 * not associate so we just create create a new node and
1390 * simulate an association. The caller is responsible for
1391 * installing the node as the bss node and handling any further
1392 * setup work like authorizing the port.
1394 struct ieee80211_node *
1395 ieee80211_node_create_wds(struct ieee80211vap *vap,
1396 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1398 struct ieee80211com *ic = vap->iv_ic;
1399 struct ieee80211_node *ni;
1401 /* XXX check if node already in sta table? */
1402 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1404 ni->ni_wdsvap = vap;
1405 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1407 * Inherit any manually configured settings.
1409 copy_bss(ni, vap->iv_bss);
1410 ieee80211_node_set_chan(ni, chan);
1411 /* NB: propagate ssid so available to WPA supplicant */
1412 ni->ni_esslen = vap->iv_des_ssid[0].len;
1413 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1414 /* NB: no associd for peer */
1416 * There are no management frames to use to
1417 * discover neighbor capabilities, so blindly
1418 * propagate the local configuration.
1420 if (vap->iv_flags & IEEE80211_F_WME)
1421 ni->ni_flags |= IEEE80211_NODE_QOS;
1422 #ifdef IEEE80211_SUPPORT_SUPERG
1423 if (vap->iv_flags & IEEE80211_F_FF)
1424 ni->ni_flags |= IEEE80211_NODE_FF;
1426 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1427 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1429 * Device is HT-capable and HT is enabled for
1430 * the vap; setup HT operation. On return
1431 * ni_chan will be adjusted to an HT channel.
1433 ieee80211_ht_wds_init(ni);
1435 struct ieee80211_channel *c = ni->ni_chan;
1437 * Force a legacy channel to be used.
1439 c = ieee80211_find_channel(ic,
1440 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1441 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1442 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1449 struct ieee80211_node *
1450 #ifdef IEEE80211_DEBUG_REFCNT
1451 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1452 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1454 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1455 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1458 struct ieee80211_node *ni;
1461 IEEE80211_NODE_LOCK_ASSERT(nt);
1463 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1464 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1465 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1466 ieee80211_ref_node(ni); /* mark referenced */
1467 #ifdef IEEE80211_DEBUG_REFCNT
1468 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1469 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1471 ni, ether_sprintf(ni->ni_macaddr),
1472 ieee80211_node_refcnt(ni));
1480 struct ieee80211_node *
1481 #ifdef IEEE80211_DEBUG_REFCNT
1482 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1483 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1485 ieee80211_find_node(struct ieee80211_node_table *nt,
1486 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1489 struct ieee80211_node *ni;
1491 IEEE80211_NODE_LOCK(nt);
1492 ni = ieee80211_find_node_locked(nt, macaddr);
1493 IEEE80211_NODE_UNLOCK(nt);
1497 struct ieee80211_node *
1498 #ifdef IEEE80211_DEBUG_REFCNT
1499 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1500 const struct ieee80211vap *vap,
1501 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1503 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1504 const struct ieee80211vap *vap,
1505 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1508 struct ieee80211_node *ni;
1511 IEEE80211_NODE_LOCK_ASSERT(nt);
1513 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1514 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1515 if (ni->ni_vap == vap &&
1516 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1517 ieee80211_ref_node(ni); /* mark referenced */
1518 #ifdef IEEE80211_DEBUG_REFCNT
1519 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1520 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1522 ni, ether_sprintf(ni->ni_macaddr),
1523 ieee80211_node_refcnt(ni));
1531 struct ieee80211_node *
1532 #ifdef IEEE80211_DEBUG_REFCNT
1533 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1534 const struct ieee80211vap *vap,
1535 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1537 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1538 const struct ieee80211vap *vap,
1539 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1542 struct ieee80211_node *ni;
1544 IEEE80211_NODE_LOCK(nt);
1545 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1546 IEEE80211_NODE_UNLOCK(nt);
1551 * Fake up a node; this handles node discovery in adhoc mode.
1552 * Note that for the driver's benefit we we treat this like
1553 * an association so the driver has an opportunity to setup
1554 * it's private state.
1556 struct ieee80211_node *
1557 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1558 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1560 struct ieee80211_node *ni;
1562 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1563 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1564 ni = ieee80211_dup_bss(vap, macaddr);
1566 struct ieee80211com *ic = vap->iv_ic;
1568 /* XXX no rate negotiation; just dup */
1569 ni->ni_rates = vap->iv_bss->ni_rates;
1570 if (ieee80211_iserp_rateset(&ni->ni_rates))
1571 ni->ni_flags |= IEEE80211_NODE_ERP;
1572 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1574 * In adhoc demo mode there are no management
1575 * frames to use to discover neighbor capabilities,
1576 * so blindly propagate the local configuration
1577 * so we can do interesting things (e.g. use
1578 * WME to disable ACK's).
1583 if (vap->iv_flags & IEEE80211_F_WME)
1584 ni->ni_flags |= IEEE80211_NODE_QOS;
1585 #ifdef IEEE80211_SUPPORT_SUPERG
1586 if (vap->iv_flags & IEEE80211_F_FF)
1587 ni->ni_flags |= IEEE80211_NODE_FF;
1590 ieee80211_node_setuptxparms(ni);
1591 ieee80211_ratectl_node_init(ni);
1594 * XXX TODO: 11n? At least 20MHz, at least A-MPDU RX,
1595 * not A-MPDU TX; not 11n rates, etc. We'll cycle
1596 * that after we hear that we can indeed do 11n
1597 * (either by a beacon frame or by a probe response.)
1601 * This is the first time we see the node.
1603 if (ic->ic_newassoc != NULL)
1604 ic->ic_newassoc(ni, 1);
1607 * Kick off a probe request to the given node;
1608 * we will then use the probe response to update
1609 * 11n/etc configuration state.
1611 * XXX TODO: this isn't guaranteed, and until we get
1612 * a probe response, we won't be able to actually
1613 * do anything 802.11n related to the node.
1614 * So if this does indeed work, maybe we should hold
1615 * off on sending responses until we get the probe
1616 * response, or just default to some sensible subset
1617 * of 802.11n behaviour (eg always allow aggregation
1618 * negotiation TO us, but not FROM us, etc) so we
1619 * aren't entirely busted.
1621 if (vap->iv_opmode == IEEE80211_M_IBSS) {
1622 ieee80211_send_probereq(ni, /* node */
1623 vap->iv_myaddr, /* SA */
1624 ni->ni_macaddr, /* DA */
1625 vap->iv_bss->ni_bssid, /* BSSID */
1626 vap->iv_bss->ni_essid,
1627 vap->iv_bss->ni_esslen); /* SSID */
1630 /* XXX not right for 802.1x/WPA */
1631 ieee80211_node_authorize(ni);
1637 ieee80211_init_neighbor(struct ieee80211_node *ni,
1638 const struct ieee80211_frame *wh,
1639 const struct ieee80211_scanparams *sp)
1641 int do_ht_setup = 0;
1643 ni->ni_esslen = sp->ssid[1];
1644 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1645 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1646 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1647 ni->ni_intval = sp->bintval;
1648 ni->ni_capinfo = sp->capinfo;
1649 ni->ni_chan = ni->ni_ic->ic_curchan;
1650 ni->ni_fhdwell = sp->fhdwell;
1651 ni->ni_fhindex = sp->fhindex;
1652 ni->ni_erp = sp->erp;
1653 ni->ni_timoff = sp->timoff;
1654 #ifdef IEEE80211_SUPPORT_MESH
1655 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1656 ieee80211_mesh_init_neighbor(ni, wh, sp);
1658 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1659 ieee80211_ies_expand(&ni->ni_ies);
1660 if (ni->ni_ies.wme_ie != NULL)
1661 ni->ni_flags |= IEEE80211_NODE_QOS;
1663 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1664 #ifdef IEEE80211_SUPPORT_SUPERG
1665 if (ni->ni_ies.ath_ie != NULL)
1666 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1668 if (ni->ni_ies.htcap_ie != NULL)
1669 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1670 if (ni->ni_ies.htinfo_ie != NULL)
1671 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1673 if ((ni->ni_ies.htcap_ie != NULL) &&
1674 (ni->ni_ies.htinfo_ie != NULL) &&
1675 (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1680 /* NB: must be after ni_chan is setup */
1681 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1682 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1683 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1686 * If the neighbor is HT compatible, flip that on.
1689 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1690 "%s: doing HT setup\n", __func__);
1691 ieee80211_ht_node_init(ni);
1692 ieee80211_ht_updateparams(ni,
1693 ni->ni_ies.htcap_ie,
1694 ni->ni_ies.htinfo_ie);
1695 ieee80211_setup_htrates(ni,
1696 ni->ni_ies.htcap_ie,
1697 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1698 ieee80211_setup_basic_htrates(ni,
1699 ni->ni_ies.htinfo_ie);
1700 ieee80211_node_setuptxparms(ni);
1701 ieee80211_ratectl_node_init(ni);
1703 /* Reassociate; we're now 11n */
1705 * XXX TODO: this is the wrong thing to do -
1706 * we're calling it with isnew=1 so the ath(4)
1707 * driver reinitialises the rate tables.
1708 * This "mostly" works for ath(4), but it won't
1709 * be right for firmware devices which allocate
1712 * So, do we just create a new node and delete
1715 if (ni->ni_ic->ic_newassoc)
1716 ni->ni_ic->ic_newassoc(ni, 1);
1721 * Do node discovery in adhoc mode on receipt of a beacon
1722 * or probe response frame. Note that for the driver's
1723 * benefit we we treat this like an association so the
1724 * driver has an opportunity to setup it's private state.
1726 struct ieee80211_node *
1727 ieee80211_add_neighbor(struct ieee80211vap *vap,
1728 const struct ieee80211_frame *wh,
1729 const struct ieee80211_scanparams *sp)
1731 struct ieee80211_node *ni;
1733 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1734 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1735 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1737 struct ieee80211com *ic = vap->iv_ic;
1739 ieee80211_init_neighbor(ni, wh, sp);
1740 if (ieee80211_iserp_rateset(&ni->ni_rates))
1741 ni->ni_flags |= IEEE80211_NODE_ERP;
1742 ieee80211_node_setuptxparms(ni);
1743 ieee80211_ratectl_node_init(ni);
1744 if (ic->ic_newassoc != NULL)
1745 ic->ic_newassoc(ni, 1);
1746 /* XXX not right for 802.1x/WPA */
1747 ieee80211_node_authorize(ni);
1752 #define IS_PROBEREQ(wh) \
1753 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1754 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1755 #define IS_BCAST_PROBEREQ(wh) \
1756 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1757 ((const struct ieee80211_frame *)(wh))->i_addr3))
1759 static __inline struct ieee80211_node *
1760 _find_rxnode(struct ieee80211_node_table *nt,
1761 const struct ieee80211_frame_min *wh)
1763 if (IS_BCAST_PROBEREQ(wh))
1764 return NULL; /* spam bcast probe req to all vap's */
1765 return ieee80211_find_node_locked(nt, wh->i_addr2);
1769 * Locate the node for sender, track state, and then pass the
1770 * (referenced) node up to the 802.11 layer for its use. Note
1771 * we can return NULL if the sender is not in the table.
1773 struct ieee80211_node *
1774 #ifdef IEEE80211_DEBUG_REFCNT
1775 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1776 const struct ieee80211_frame_min *wh, const char *func, int line)
1778 ieee80211_find_rxnode(struct ieee80211com *ic,
1779 const struct ieee80211_frame_min *wh)
1782 struct ieee80211_node_table *nt;
1783 struct ieee80211_node *ni;
1786 IEEE80211_NODE_LOCK(nt);
1787 ni = _find_rxnode(nt, wh);
1788 IEEE80211_NODE_UNLOCK(nt);
1794 * Like ieee80211_find_rxnode but use the supplied h/w
1795 * key index as a hint to locate the node in the key
1796 * mapping table. If an entry is present at the key
1797 * index we return it; otherwise do a normal lookup and
1798 * update the mapping table if the station has a unicast
1799 * key assigned to it.
1801 struct ieee80211_node *
1802 #ifdef IEEE80211_DEBUG_REFCNT
1803 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1804 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1805 const char *func, int line)
1807 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1808 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1811 struct ieee80211_node_table *nt;
1812 struct ieee80211_node *ni;
1815 IEEE80211_NODE_LOCK(nt);
1816 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1817 ni = nt->nt_keyixmap[keyix];
1821 ni = _find_rxnode(nt, wh);
1822 if (ni != NULL && nt->nt_keyixmap != NULL) {
1824 * If the station has a unicast key cache slot
1825 * assigned update the key->node mapping table.
1827 keyix = ni->ni_ucastkey.wk_rxkeyix;
1828 /* XXX can keyixmap[keyix] != NULL? */
1829 if (keyix < nt->nt_keyixmax &&
1830 nt->nt_keyixmap[keyix] == NULL) {
1831 IEEE80211_DPRINTF(ni->ni_vap,
1833 "%s: add key map entry %p<%s> refcnt %d\n",
1834 __func__, ni, ether_sprintf(ni->ni_macaddr),
1835 ieee80211_node_refcnt(ni)+1);
1836 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1840 if (IS_BCAST_PROBEREQ(wh))
1841 ni = NULL; /* spam bcast probe req to all vap's */
1843 ieee80211_ref_node(ni);
1845 IEEE80211_NODE_UNLOCK(nt);
1849 #undef IS_BCAST_PROBEREQ
1853 * Return a reference to the appropriate node for sending
1854 * a data frame. This handles node discovery in adhoc networks.
1856 struct ieee80211_node *
1857 #ifdef IEEE80211_DEBUG_REFCNT
1858 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1859 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1860 const char *func, int line)
1862 ieee80211_find_txnode(struct ieee80211vap *vap,
1863 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1866 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1867 struct ieee80211_node *ni;
1870 * The destination address should be in the node table
1871 * unless this is a multicast/broadcast frame. We can
1872 * also optimize station mode operation, all frames go
1875 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1876 IEEE80211_NODE_LOCK(nt);
1877 if (vap->iv_opmode == IEEE80211_M_STA ||
1878 vap->iv_opmode == IEEE80211_M_WDS ||
1879 IEEE80211_IS_MULTICAST(macaddr))
1880 ni = ieee80211_ref_node(vap->iv_bss);
1882 ni = ieee80211_find_node_locked(nt, macaddr);
1883 IEEE80211_NODE_UNLOCK(nt);
1886 if (vap->iv_opmode == IEEE80211_M_IBSS ||
1887 vap->iv_opmode == IEEE80211_M_AHDEMO) {
1889 * In adhoc mode cons up a node for the destination.
1890 * Note that we need an additional reference for the
1891 * caller to be consistent with
1892 * ieee80211_find_node_locked.
1895 * XXX TODO: this doesn't fake up 11n state; we need
1896 * to find another way to get it upgraded.
1898 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1900 (void) ieee80211_ref_node(ni);
1902 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1903 "no node, discard frame (%s)", __func__);
1904 vap->iv_stats.is_tx_nonode++;
1911 _ieee80211_free_node(struct ieee80211_node *ni)
1913 struct ieee80211_node_table *nt = ni->ni_table;
1916 * NB: careful about referencing the vap as it may be
1917 * gone if the last reference was held by a driver.
1918 * We know the com will always be present so it's safe
1919 * to use ni_ic below to reclaim resources.
1922 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1923 "%s %p<%s> in %s table\n", __func__, ni,
1924 ether_sprintf(ni->ni_macaddr),
1925 nt != NULL ? nt->nt_name : "<gone>");
1927 if (ni->ni_associd != 0) {
1928 struct ieee80211vap *vap = ni->ni_vap;
1929 if (vap->iv_aid_bitmap != NULL)
1930 IEEE80211_AID_CLR(vap, ni->ni_associd);
1933 ieee80211_del_node_nt(nt, ni);
1934 ni->ni_ic->ic_node_free(ni);
1938 * Clear any entry in the unicast key mapping table.
1941 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1943 ieee80211_keyix keyix;
1945 keyix = ni->ni_ucastkey.wk_rxkeyix;
1946 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1947 nt->nt_keyixmap[keyix] == ni) {
1948 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1949 "%s: %p<%s> clear key map entry %u\n",
1950 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1951 nt->nt_keyixmap[keyix] = NULL;
1952 ieee80211_node_decref(ni);
1960 #ifdef IEEE80211_DEBUG_REFCNT
1961 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1963 ieee80211_free_node(struct ieee80211_node *ni)
1966 struct ieee80211_node_table *nt = ni->ni_table;
1968 #ifdef IEEE80211_DEBUG_REFCNT
1969 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1970 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1971 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1974 IEEE80211_NODE_LOCK(nt);
1975 if (ieee80211_node_dectestref(ni)) {
1977 * Last reference, reclaim state.
1979 _ieee80211_free_node(ni);
1980 } else if (ieee80211_node_refcnt(ni) == 1)
1981 if (node_clear_keyixmap(nt, ni))
1982 _ieee80211_free_node(ni);
1983 IEEE80211_NODE_UNLOCK(nt);
1985 if (ieee80211_node_dectestref(ni))
1986 _ieee80211_free_node(ni);
1991 * Reclaim a unicast key and clear any key cache state.
1994 ieee80211_node_delucastkey(struct ieee80211_node *ni)
1996 struct ieee80211com *ic = ni->ni_ic;
1997 struct ieee80211_node_table *nt = &ic->ic_sta;
1998 struct ieee80211_node *nikey;
1999 ieee80211_keyix keyix;
2000 int isowned, status;
2003 * NB: We must beware of LOR here; deleting the key
2004 * can cause the crypto layer to block traffic updates
2005 * which can generate a LOR against the node table lock;
2006 * grab it here and stash the key index for our use below.
2008 * Must also beware of recursion on the node table lock.
2009 * When called from node_cleanup we may already have
2010 * the node table lock held. Unfortunately there's no
2011 * way to separate out this path so we must do this
2014 isowned = IEEE80211_NODE_IS_LOCKED(nt);
2016 IEEE80211_NODE_LOCK(nt);
2018 status = 1; /* NB: success */
2019 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
2020 keyix = ni->ni_ucastkey.wk_rxkeyix;
2021 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
2022 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
2023 nikey = nt->nt_keyixmap[keyix];
2024 nt->nt_keyixmap[keyix] = NULL;
2028 IEEE80211_NODE_UNLOCK(nt);
2030 if (nikey != NULL) {
2031 KASSERT(nikey == ni,
2032 ("key map out of sync, ni %p nikey %p", ni, nikey));
2033 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2034 "%s: delete key map entry %p<%s> refcnt %d\n",
2035 __func__, ni, ether_sprintf(ni->ni_macaddr),
2036 ieee80211_node_refcnt(ni)-1);
2037 ieee80211_free_node(ni);
2043 * Reclaim a node. If this is the last reference count then
2044 * do the normal free work. Otherwise remove it from the node
2045 * table and mark it gone by clearing the back-reference.
2048 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2051 IEEE80211_NODE_LOCK_ASSERT(nt);
2053 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2054 "%s: remove %p<%s> from %s table, refcnt %d\n",
2055 __func__, ni, ether_sprintf(ni->ni_macaddr),
2056 nt->nt_name, ieee80211_node_refcnt(ni)-1);
2058 * Clear any entry in the unicast key mapping table.
2059 * We need to do it here so rx lookups don't find it
2060 * in the mapping table even if it's not in the hash
2061 * table. We cannot depend on the mapping table entry
2062 * being cleared because the node may not be free'd.
2064 (void)node_clear_keyixmap(nt, ni);
2065 if (!ieee80211_node_dectestref(ni)) {
2067 * Other references are present, just remove the
2068 * node from the table so it cannot be found. When
2069 * the references are dropped storage will be
2072 ieee80211_del_node_nt(nt, ni);
2074 _ieee80211_free_node(ni);
2078 * Node table support.
2082 ieee80211_node_table_init(struct ieee80211com *ic,
2083 struct ieee80211_node_table *nt,
2084 const char *name, int inact, int keyixmax)
2088 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
2089 TAILQ_INIT(&nt->nt_node);
2092 nt->nt_inact_init = inact;
2093 nt->nt_keyixmax = keyixmax;
2094 if (nt->nt_keyixmax > 0) {
2095 nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
2096 keyixmax * sizeof(struct ieee80211_node *),
2098 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2099 if (nt->nt_keyixmap == NULL)
2101 "Cannot allocate key index map with %u entries\n",
2104 nt->nt_keyixmap = NULL;
2108 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
2109 struct ieee80211vap *match)
2111 struct ieee80211_node *ni, *next;
2113 IEEE80211_NODE_LOCK(nt);
2114 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
2115 if (match != NULL && ni->ni_vap != match)
2117 /* XXX can this happen? if so need's work */
2118 if (ni->ni_associd != 0) {
2119 struct ieee80211vap *vap = ni->ni_vap;
2121 if (vap->iv_auth->ia_node_leave != NULL)
2122 vap->iv_auth->ia_node_leave(ni);
2123 if (vap->iv_aid_bitmap != NULL)
2124 IEEE80211_AID_CLR(vap, ni->ni_associd);
2126 ni->ni_wdsvap = NULL; /* clear reference */
2127 node_reclaim(nt, ni);
2129 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
2131 * Make a separate pass to clear references to this vap
2132 * held by DWDS entries. They will not be matched above
2133 * because ni_vap will point to the ap vap but we still
2134 * need to clear ni_wdsvap when the WDS vap is destroyed
2137 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
2138 if (ni->ni_wdsvap == match)
2139 ni->ni_wdsvap = NULL;
2141 IEEE80211_NODE_UNLOCK(nt);
2145 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2147 ieee80211_node_table_reset(nt, NULL);
2148 if (nt->nt_keyixmap != NULL) {
2150 /* XXX verify all entries are NULL */
2152 for (i = 0; i < nt->nt_keyixmax; i++)
2153 if (nt->nt_keyixmap[i] != NULL)
2154 printf("%s: %s[%u] still active\n", __func__,
2157 IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
2158 nt->nt_keyixmap = NULL;
2160 IEEE80211_NODE_LOCK_DESTROY(nt);
2164 timeout_stations(void *arg __unused, struct ieee80211_node *ni)
2166 struct ieee80211com *ic = ni->ni_ic;
2167 struct ieee80211vap *vap = ni->ni_vap;
2170 * Only process stations when in RUN state. This
2171 * insures, for example, that we don't timeout an
2172 * inactive station during CAC. Note that CSA state
2173 * is actually handled in ieee80211_node_timeout as
2174 * it applies to more than timeout processing.
2176 if (vap->iv_state != IEEE80211_S_RUN)
2179 * Ignore entries for which have yet to receive an
2180 * authentication frame. These are transient and
2181 * will be reclaimed when the last reference to them
2182 * goes away (when frame xmits complete).
2184 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2185 vap->iv_opmode == IEEE80211_M_STA) &&
2186 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2189 * Free fragment if not needed anymore
2190 * (last fragment older than 1s).
2191 * XXX doesn't belong here, move to node_age
2193 if (ni->ni_rxfrag[0] != NULL &&
2194 ticks > ni->ni_rxfragstamp + hz) {
2195 m_freem(ni->ni_rxfrag[0]);
2196 ni->ni_rxfrag[0] = NULL;
2198 if (ni->ni_inact > 0) {
2200 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2201 "%s: inact %u inact_reload %u nrates %u",
2202 __func__, ni->ni_inact, ni->ni_inact_reload,
2203 ni->ni_rates.rs_nrates);
2206 * Special case ourself; we may be idle for extended periods
2207 * of time and regardless reclaiming our state is wrong.
2208 * XXX run ic_node_age
2210 /* XXX before inact decrement? */
2211 if (ni == vap->iv_bss)
2213 if (ni->ni_associd != 0 ||
2214 (vap->iv_opmode == IEEE80211_M_IBSS ||
2215 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2217 * Age/drain resources held by the station.
2219 ic->ic_node_age(ni);
2221 * Probe the station before time it out. We
2222 * send a null data frame which may not be
2223 * universally supported by drivers (need it
2224 * for ps-poll support so it should be...).
2226 * XXX don't probe the station unless we've
2227 * received a frame from them (and have
2228 * some idea of the rates they are capable
2229 * of); this will get fixed more properly
2230 * soon with better handling of the rate set.
2232 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2233 (0 < ni->ni_inact &&
2234 ni->ni_inact <= vap->iv_inact_probe) &&
2235 ni->ni_rates.rs_nrates != 0) {
2237 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2239 "probe station due to inactivity");
2241 * Grab a reference so the node cannot
2242 * be reclaimed before we send the frame.
2243 * ieee80211_send_nulldata understands
2244 * we've done this and reclaims the
2245 * ref for us as needed.
2247 /* XXX fix this (not required anymore). */
2248 ieee80211_ref_node(ni);
2250 ieee80211_send_nulldata(ni);
2255 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2256 ni->ni_inact <= 0) {
2258 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2259 "station timed out due to inactivity "
2260 "(refcnt %u)", ieee80211_node_refcnt(ni));
2262 * Send a deauthenticate frame and drop the station.
2263 * This is somewhat complicated due to reference counts
2264 * and locking. At this point a station will typically
2265 * have a reference count of 2. ieee80211_node_leave
2266 * will do a "free" of the node which will drop the
2267 * reference count. But in the meantime a reference
2268 * wil be held by the deauth frame. The actual reclaim
2269 * of the node will happen either after the tx is
2270 * completed or by ieee80211_node_leave.
2272 if (ni->ni_associd != 0) {
2273 IEEE80211_SEND_MGMT(ni,
2274 IEEE80211_FC0_SUBTYPE_DEAUTH,
2275 IEEE80211_REASON_AUTH_EXPIRE);
2277 ieee80211_node_leave(ni);
2278 vap->iv_stats.is_node_timeout++;
2283 * Timeout inactive stations and do related housekeeping.
2286 ieee80211_timeout_stations(struct ieee80211com *ic)
2288 struct ieee80211_node_table *nt = &ic->ic_sta;
2290 ieee80211_iterate_nodes(nt, timeout_stations, NULL);
2294 * Aggressively reclaim resources. This should be used
2295 * only in a critical situation to reclaim mbuf resources.
2298 ieee80211_drain(struct ieee80211com *ic)
2300 struct ieee80211_node_table *nt = &ic->ic_sta;
2301 struct ieee80211vap *vap;
2302 struct ieee80211_node *ni;
2304 IEEE80211_NODE_LOCK(nt);
2305 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2307 * Ignore entries for which have yet to receive an
2308 * authentication frame. These are transient and
2309 * will be reclaimed when the last reference to them
2310 * goes away (when frame xmits complete).
2314 * Only process stations when in RUN state. This
2315 * insures, for example, that we don't timeout an
2316 * inactive station during CAC. Note that CSA state
2317 * is actually handled in ieee80211_node_timeout as
2318 * it applies to more than timeout processing.
2320 if (vap->iv_state != IEEE80211_S_RUN)
2322 /* XXX can vap be NULL? */
2323 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2324 vap->iv_opmode == IEEE80211_M_STA) &&
2325 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2329 * XXX doesn't belong here, move to node_drain
2331 if (ni->ni_rxfrag[0] != NULL) {
2332 m_freem(ni->ni_rxfrag[0]);
2333 ni->ni_rxfrag[0] = NULL;
2336 * Drain resources held by the station.
2338 ic->ic_node_drain(ni);
2340 IEEE80211_NODE_UNLOCK(nt);
2344 * Per-ieee80211com inactivity timer callback.
2347 ieee80211_node_timeout(void *arg)
2349 struct ieee80211com *ic = arg;
2352 * Defer timeout processing if a channel switch is pending.
2353 * We typically need to be mute so not doing things that
2354 * might generate frames is good to handle in one place.
2355 * Suppressing the station timeout processing may extend the
2356 * lifetime of inactive stations (by not decrementing their
2357 * idle counters) but this should be ok unless the CSA is
2358 * active for an unusually long time.
2360 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2361 ieee80211_scan_timeout(ic);
2362 ieee80211_timeout_stations(ic);
2363 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2366 ieee80211_erp_timeout(ic);
2367 ieee80211_ht_timeout(ic);
2368 IEEE80211_UNLOCK(ic);
2370 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2371 ieee80211_node_timeout, ic);
2375 * The same as ieee80211_iterate_nodes(), but for one vap only.
2378 ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
2379 struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
2381 struct ieee80211_node **ni_arr;
2382 struct ieee80211_node *ni;
2387 * Iterate over the node table and save an array of ref'ed nodes.
2389 * This is separated out from calling the actual node function so that
2390 * no LORs will occur.
2392 IEEE80211_NODE_LOCK(nt);
2393 count = nt->nt_count;
2394 size = count * sizeof(struct ieee80211_node *);
2395 ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
2396 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2397 if (ni_arr == NULL) {
2398 IEEE80211_NODE_UNLOCK(nt);
2403 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2404 if (vap != NULL && ni->ni_vap != vap)
2407 ("node array overflow (vap %p, i %d, count %d)\n",
2409 ni_arr[i] = ieee80211_ref_node(ni);
2412 IEEE80211_NODE_UNLOCK(nt);
2414 for (i = 0; i < count; i++) {
2415 if (ni_arr[i] == NULL) /* end of the list */
2417 (*f)(arg, ni_arr[i]);
2418 /* ieee80211_free_node() locks by itself */
2419 ieee80211_free_node(ni_arr[i]);
2422 IEEE80211_FREE(ni_arr, M_80211_NODE);
2428 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2429 * reference in the source.
2432 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2433 ieee80211_iter_func *f, void *arg)
2435 /* XXX no way to pass error to the caller. */
2436 (void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
2440 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2442 printf("0x%p: mac %s refcnt %d\n", ni,
2443 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2444 printf("\tauthmode %u flags 0x%x\n",
2445 ni->ni_authmode, ni->ni_flags);
2446 printf("\tassocid 0x%x txpower %u vlan %u\n",
2447 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2448 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2449 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2450 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2451 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2452 ni->ni_rxfragstamp);
2453 printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2454 node_getrssi(ni), ni->ni_noise,
2455 ni->ni_intval, ni->ni_capinfo);
2456 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2457 ether_sprintf(ni->ni_bssid),
2458 ni->ni_esslen, ni->ni_essid,
2459 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2460 printf("\tinact %u inact_reload %u txrate %u\n",
2461 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2462 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2463 ni->ni_htcap, ni->ni_htparam,
2464 ni->ni_htctlchan, ni->ni_ht2ndchan);
2465 printf("\thtopmode %x htstbc %x chw %u\n",
2466 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2470 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2472 ieee80211_iterate_nodes(nt,
2473 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2477 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2479 struct ieee80211vap *vap;
2481 IEEE80211_LOCK_ASSERT(ic);
2483 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2484 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2485 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2489 ieee80211_notify_erp(struct ieee80211com *ic)
2492 ieee80211_notify_erp_locked(ic);
2493 IEEE80211_UNLOCK(ic);
2497 * Handle a station joining an 11g network.
2500 ieee80211_node_join_11g(struct ieee80211_node *ni)
2502 struct ieee80211com *ic = ni->ni_ic;
2504 IEEE80211_LOCK_ASSERT(ic);
2507 * Station isn't capable of short slot time. Bump
2508 * the count of long slot time stations and disable
2509 * use of short slot time. Note that the actual switch
2510 * over to long slot time use may not occur until the
2511 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2513 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2514 ic->ic_longslotsta++;
2515 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2516 "station needs long slot time, count %d",
2517 ic->ic_longslotsta);
2518 /* XXX vap's w/ conflicting needs won't work */
2519 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2521 * Don't force slot time when switched to turbo
2522 * mode as non-ERP stations won't be present; this
2523 * need only be done when on the normal G channel.
2525 ieee80211_set_shortslottime(ic, 0);
2529 * If the new station is not an ERP station
2530 * then bump the counter and enable protection
2533 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2535 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2536 "station is !ERP, %d non-ERP stations associated",
2539 * If station does not support short preamble
2540 * then we must enable use of Barker preamble.
2542 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2543 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2544 "%s", "station needs long preamble");
2545 ic->ic_flags |= IEEE80211_F_USEBARKER;
2546 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2549 * If protection is configured and this is the first
2550 * indication we should use protection, enable it.
2552 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2553 ic->ic_nonerpsta == 1 &&
2554 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2555 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2556 "%s: enable use of protection\n", __func__);
2557 ic->ic_flags |= IEEE80211_F_USEPROT;
2558 ieee80211_notify_erp_locked(ic);
2561 ni->ni_flags |= IEEE80211_NODE_ERP;
2565 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2567 struct ieee80211com *ic = ni->ni_ic;
2568 struct ieee80211vap *vap = ni->ni_vap;
2571 if (ni->ni_associd == 0) {
2574 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2576 * It would be good to search the bitmap
2577 * more efficiently, but this will do for now.
2579 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2580 if (!IEEE80211_AID_ISSET(vap, aid))
2583 if (aid >= vap->iv_max_aid) {
2584 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2585 ieee80211_node_leave(ni);
2588 ni->ni_associd = aid | 0xc000;
2589 ni->ni_jointime = time_uptime;
2591 IEEE80211_AID_SET(vap, ni->ni_associd);
2592 vap->iv_sta_assoc++;
2595 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2596 ieee80211_ht_node_join(ni);
2597 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2598 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2599 ieee80211_node_join_11g(ni);
2600 IEEE80211_UNLOCK(ic);
2606 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2607 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2608 IEEE80211_NODE_AID(ni),
2609 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2610 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2611 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2612 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2613 ni->ni_flags & IEEE80211_NODE_HT ?
2614 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2615 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2616 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2617 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2618 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2619 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2620 ", fast-frames" : "",
2621 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2625 ieee80211_node_setuptxparms(ni);
2626 ieee80211_ratectl_node_init(ni);
2627 /* give driver a chance to setup state like ni_txrate */
2628 if (ic->ic_newassoc != NULL)
2629 ic->ic_newassoc(ni, newassoc);
2630 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2631 /* tell the authenticator about new station */
2632 if (vap->iv_auth->ia_node_join != NULL)
2633 vap->iv_auth->ia_node_join(ni);
2634 ieee80211_notify_node_join(ni,
2635 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2639 disable_protection(struct ieee80211com *ic)
2641 KASSERT(ic->ic_nonerpsta == 0 &&
2642 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2643 ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2646 ic->ic_flags &= ~IEEE80211_F_USEPROT;
2647 /* XXX verify mode? */
2648 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2649 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2650 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2652 ieee80211_notify_erp_locked(ic);
2656 * Handle a station leaving an 11g network.
2659 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2661 struct ieee80211com *ic = ni->ni_ic;
2663 IEEE80211_LOCK_ASSERT(ic);
2665 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2666 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2667 ic->ic_bsschan->ic_flags));
2670 * If a long slot station do the slot time bookkeeping.
2672 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2673 KASSERT(ic->ic_longslotsta > 0,
2674 ("bogus long slot station count %d", ic->ic_longslotsta));
2675 ic->ic_longslotsta--;
2676 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2677 "long slot time station leaves, count now %d",
2678 ic->ic_longslotsta);
2679 if (ic->ic_longslotsta == 0) {
2681 * Re-enable use of short slot time if supported
2682 * and not operating in IBSS mode (per spec).
2684 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2685 ic->ic_opmode != IEEE80211_M_IBSS) {
2686 IEEE80211_DPRINTF(ni->ni_vap,
2687 IEEE80211_MSG_ASSOC,
2688 "%s: re-enable use of short slot time\n",
2690 ieee80211_set_shortslottime(ic, 1);
2695 * If a non-ERP station do the protection-related bookkeeping.
2697 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2698 KASSERT(ic->ic_nonerpsta > 0,
2699 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2701 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2702 "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2703 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2704 " (non-ERP sta present)" : "");
2705 if (ic->ic_nonerpsta == 0 &&
2706 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2707 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2708 "%s: disable use of protection\n", __func__);
2709 disable_protection(ic);
2715 * Time out presence of an overlapping bss with non-ERP
2716 * stations. When operating in hostap mode we listen for
2717 * beacons from other stations and if we identify a non-ERP
2718 * station is present we enable protection. To identify
2719 * when all non-ERP stations are gone we time out this
2723 ieee80211_erp_timeout(struct ieee80211com *ic)
2726 IEEE80211_LOCK_ASSERT(ic);
2728 if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2729 ieee80211_time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2731 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2732 "%s", "age out non-ERP sta present on channel");
2734 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2735 if (ic->ic_nonerpsta == 0)
2736 disable_protection(ic);
2741 * Handle bookkeeping for station deauthentication/disassociation
2742 * when operating as an ap.
2745 ieee80211_node_leave(struct ieee80211_node *ni)
2747 struct ieee80211com *ic = ni->ni_ic;
2748 struct ieee80211vap *vap = ni->ni_vap;
2749 struct ieee80211_node_table *nt = ni->ni_table;
2751 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2752 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2754 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2755 ("unexpected operating mode %u", vap->iv_opmode));
2757 * If node wasn't previously associated all
2758 * we need to do is reclaim the reference.
2760 /* XXX ibss mode bypasses 11g and notification */
2761 if (ni->ni_associd == 0)
2764 * Tell the authenticator the station is leaving.
2765 * Note that we must do this before yanking the
2766 * association id as the authenticator uses the
2767 * associd to locate it's state block.
2769 if (vap->iv_auth->ia_node_leave != NULL)
2770 vap->iv_auth->ia_node_leave(ni);
2773 IEEE80211_AID_CLR(vap, ni->ni_associd);
2774 vap->iv_sta_assoc--;
2777 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2778 ieee80211_ht_node_leave(ni);
2779 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2780 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2781 ieee80211_node_leave_11g(ni);
2782 IEEE80211_UNLOCK(ic);
2784 * Cleanup station state. In particular clear various
2785 * state that might otherwise be reused if the node
2786 * is reused before the reference count goes to zero
2787 * (and memory is reclaimed).
2789 ieee80211_sta_leave(ni);
2792 * Remove the node from any table it's recorded in and
2793 * drop the caller's reference. Removal from the table
2794 * is important to insure the node is not reprocessed
2798 IEEE80211_NODE_LOCK(nt);
2799 node_reclaim(nt, ni);
2800 IEEE80211_NODE_UNLOCK(nt);
2802 ieee80211_free_node(ni);
2807 uint32_t rssi_total;
2811 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2813 struct rssiinfo *info = arg;
2814 struct ieee80211vap *vap = ni->ni_vap;
2817 /* only associated stations */
2818 if (ni->ni_associd == 0)
2820 rssi = vap->iv_ic->ic_node_getrssi(ni);
2822 info->rssi_samples++;
2823 info->rssi_total += rssi;
2828 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2830 struct rssiinfo *info = arg;
2831 struct ieee80211vap *vap = ni->ni_vap;
2834 /* only neighbors */
2835 /* XXX check bssid */
2836 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2838 rssi = vap->iv_ic->ic_node_getrssi(ni);
2840 info->rssi_samples++;
2841 info->rssi_total += rssi;
2845 #ifdef IEEE80211_SUPPORT_MESH
2847 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
2849 struct rssiinfo *info = arg;
2850 struct ieee80211vap *vap = ni->ni_vap;
2853 /* only neighbors that peered successfully */
2854 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
2856 rssi = vap->iv_ic->ic_node_getrssi(ni);
2858 info->rssi_samples++;
2859 info->rssi_total += rssi;
2862 #endif /* IEEE80211_SUPPORT_MESH */
2865 ieee80211_getrssi(struct ieee80211vap *vap)
2867 #define NZ(x) ((x) == 0 ? 1 : (x))
2868 struct ieee80211com *ic = vap->iv_ic;
2869 struct rssiinfo info;
2871 info.rssi_total = 0;
2872 info.rssi_samples = 0;
2873 switch (vap->iv_opmode) {
2874 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
2875 case IEEE80211_M_AHDEMO: /* average of all neighbors */
2876 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
2879 case IEEE80211_M_HOSTAP: /* average of all associated stations */
2880 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
2883 #ifdef IEEE80211_SUPPORT_MESH
2884 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
2885 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
2889 case IEEE80211_M_MONITOR: /* XXX */
2890 case IEEE80211_M_STA: /* use stats from associated ap */
2892 if (vap->iv_bss != NULL)
2893 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2894 info.rssi_samples = 1;
2897 return info.rssi_total / NZ(info.rssi_samples);
2902 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2905 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
2907 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2908 /* for non-station mode return avg'd rssi accounting */
2909 if (vap->iv_opmode != IEEE80211_M_STA)
2910 *rssi = ieee80211_getrssi(vap);