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 #ifdef IEEE80211_DEBUG_REFCNT
77 #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
79 #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
82 static int ieee80211_sta_join1(struct ieee80211_node *);
84 static struct ieee80211_node *node_alloc(struct ieee80211vap *,
85 const uint8_t [IEEE80211_ADDR_LEN]);
86 static void node_cleanup(struct ieee80211_node *);
87 static void node_free(struct ieee80211_node *);
88 static void node_age(struct ieee80211_node *);
89 static int8_t node_getrssi(const struct ieee80211_node *);
90 static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
91 static void node_getmimoinfo(const struct ieee80211_node *,
92 struct ieee80211_mimo_info *);
94 static void _ieee80211_free_node(struct ieee80211_node *);
96 static void ieee80211_node_table_init(struct ieee80211com *ic,
97 struct ieee80211_node_table *nt, const char *name,
98 int inact, int keymaxix);
99 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
100 struct ieee80211vap *);
101 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
102 static void ieee80211_erp_timeout(struct ieee80211com *);
104 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
105 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
108 ieee80211_node_attach(struct ieee80211com *ic)
110 /* XXX really want maxlen enforced per-sta */
111 ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
113 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
114 IEEE80211_INACT_INIT, ic->ic_max_keyix);
115 callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
116 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
117 ieee80211_node_timeout, ic);
119 ic->ic_node_alloc = node_alloc;
120 ic->ic_node_free = node_free;
121 ic->ic_node_cleanup = node_cleanup;
122 ic->ic_node_age = node_age;
123 ic->ic_node_drain = node_age; /* NB: same as age */
124 ic->ic_node_getrssi = node_getrssi;
125 ic->ic_node_getsignal = node_getsignal;
126 ic->ic_node_getmimoinfo = node_getmimoinfo;
129 * Set flags to be propagated to all vap's;
130 * these define default behaviour/configuration.
132 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
136 ieee80211_node_detach(struct ieee80211com *ic)
139 callout_drain(&ic->ic_inact);
140 ieee80211_node_table_cleanup(&ic->ic_sta);
141 ieee80211_ageq_cleanup(&ic->ic_stageq);
145 ieee80211_node_vattach(struct ieee80211vap *vap)
147 /* NB: driver can override */
148 vap->iv_max_aid = IEEE80211_AID_DEF;
150 /* default station inactivity timer setings */
151 vap->iv_inact_init = IEEE80211_INACT_INIT;
152 vap->iv_inact_auth = IEEE80211_INACT_AUTH;
153 vap->iv_inact_run = IEEE80211_INACT_RUN;
154 vap->iv_inact_probe = IEEE80211_INACT_PROBE;
156 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
157 "%s: init %u auth %u run %u probe %u\n", __func__,
158 vap->iv_inact_init, vap->iv_inact_auth,
159 vap->iv_inact_run, vap->iv_inact_probe);
163 ieee80211_node_latevattach(struct ieee80211vap *vap)
165 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
166 /* XXX should we allow max aid to be zero? */
167 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
168 vap->iv_max_aid = IEEE80211_AID_MIN;
169 if_printf(vap->iv_ifp,
170 "WARNING: max aid too small, changed to %d\n",
173 vap->iv_aid_bitmap = (uint32_t *) malloc(
174 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
175 M_80211_NODE, M_NOWAIT | M_ZERO);
176 if (vap->iv_aid_bitmap == NULL) {
177 /* XXX no way to recover */
178 printf("%s: no memory for AID bitmap, max aid %d!\n",
179 __func__, vap->iv_max_aid);
184 ieee80211_reset_bss(vap);
186 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
190 ieee80211_node_vdetach(struct ieee80211vap *vap)
192 struct ieee80211com *ic = vap->iv_ic;
194 ieee80211_node_table_reset(&ic->ic_sta, vap);
195 if (vap->iv_bss != NULL) {
196 ieee80211_free_node(vap->iv_bss);
199 if (vap->iv_aid_bitmap != NULL) {
200 free(vap->iv_aid_bitmap, M_80211_NODE);
201 vap->iv_aid_bitmap = NULL;
206 * Port authorize/unauthorize interfaces for use by an authenticator.
210 ieee80211_node_authorize(struct ieee80211_node *ni)
212 struct ieee80211vap *vap = ni->ni_vap;
214 ni->ni_flags |= IEEE80211_NODE_AUTH;
215 ni->ni_inact_reload = vap->iv_inact_run;
216 ni->ni_inact = ni->ni_inact_reload;
218 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
219 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
223 ieee80211_node_unauthorize(struct ieee80211_node *ni)
225 struct ieee80211vap *vap = ni->ni_vap;
227 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
228 ni->ni_inact_reload = vap->iv_inact_auth;
229 if (ni->ni_inact > ni->ni_inact_reload)
230 ni->ni_inact = ni->ni_inact_reload;
232 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
233 "%s: inact_reload %u inact %u", __func__,
234 ni->ni_inact_reload, ni->ni_inact);
238 * Fix tx parameters for a node according to ``association state''.
241 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
243 struct ieee80211vap *vap = ni->ni_vap;
244 enum ieee80211_phymode mode;
246 if (ni->ni_flags & IEEE80211_NODE_HT) {
247 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
248 mode = IEEE80211_MODE_11NA;
250 mode = IEEE80211_MODE_11NG;
251 } else { /* legacy rate handling */
252 if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
253 mode = IEEE80211_MODE_STURBO_A;
254 else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
255 mode = IEEE80211_MODE_HALF;
256 else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
257 mode = IEEE80211_MODE_QUARTER;
258 /* NB: 108A should be handled as 11a */
259 else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
260 mode = IEEE80211_MODE_11A;
261 else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
262 (ni->ni_flags & IEEE80211_NODE_ERP))
263 mode = IEEE80211_MODE_11G;
265 mode = IEEE80211_MODE_11B;
267 ni->ni_txparms = &vap->iv_txparms[mode];
271 * Set/change the channel. The rate set is also updated as
272 * to insure a consistent view by drivers.
273 * XXX should be private but hostap needs it to deal with CSA
276 ieee80211_node_set_chan(struct ieee80211_node *ni,
277 struct ieee80211_channel *chan)
279 struct ieee80211com *ic = ni->ni_ic;
280 struct ieee80211vap *vap = ni->ni_vap;
281 enum ieee80211_phymode mode;
283 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
286 mode = ieee80211_chan2mode(chan);
287 if (IEEE80211_IS_CHAN_HT(chan)) {
289 * We must install the legacy rate est in ni_rates and the
290 * HT rate set in ni_htrates.
292 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
294 * Setup bss tx parameters based on operating mode. We
295 * use legacy rates when operating in a mixed HT+non-HT bss
296 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
298 if (mode == IEEE80211_MODE_11NA &&
299 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
300 mode = IEEE80211_MODE_11A;
301 else if (mode == IEEE80211_MODE_11NG &&
302 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
303 mode = IEEE80211_MODE_11G;
304 if (mode == IEEE80211_MODE_11G &&
305 (vap->iv_flags & IEEE80211_F_PUREG) == 0)
306 mode = IEEE80211_MODE_11B;
308 ni->ni_txparms = &vap->iv_txparms[mode];
309 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
313 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
315 /* propagate useful state */
316 nbss->ni_authmode = obss->ni_authmode;
317 nbss->ni_txpower = obss->ni_txpower;
318 nbss->ni_vlan = obss->ni_vlan;
319 /* XXX statistics? */
320 /* XXX legacy WDS bssid? */
324 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
326 struct ieee80211com *ic = vap->iv_ic;
327 struct ieee80211_node *ni;
329 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
330 "%s: creating %s on channel %u\n", __func__,
331 ieee80211_opmode_name[vap->iv_opmode],
332 ieee80211_chan2ieee(ic, chan));
334 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
339 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
340 ni->ni_esslen = vap->iv_des_ssid[0].len;
341 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
342 if (vap->iv_bss != NULL)
343 copy_bss(ni, vap->iv_bss);
344 ni->ni_intval = ic->ic_bintval;
345 if (vap->iv_flags & IEEE80211_F_PRIVACY)
346 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
347 if (ic->ic_phytype == IEEE80211_T_FH) {
348 ni->ni_fhdwell = 200; /* XXX */
351 if (vap->iv_opmode == IEEE80211_M_IBSS) {
352 vap->iv_flags |= IEEE80211_F_SIBSS;
353 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
354 if (vap->iv_flags & IEEE80211_F_DESBSSID)
355 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
357 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
358 /* clear group bit, add local bit */
359 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
361 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
362 if (vap->iv_flags & IEEE80211_F_DESBSSID)
363 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
365 #ifdef IEEE80211_SUPPORT_TDMA
366 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
368 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
369 #ifdef IEEE80211_SUPPORT_MESH
370 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
371 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
372 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
376 * Fix the channel and related attributes.
378 /* clear DFS CAC state on previous channel */
379 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
380 ic->ic_bsschan->ic_freq != chan->ic_freq &&
381 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
382 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
383 ic->ic_bsschan = chan;
384 ieee80211_node_set_chan(ni, chan);
385 ic->ic_curmode = ieee80211_chan2mode(chan);
387 * Do mode-specific setup.
389 if (IEEE80211_IS_CHAN_FULL(chan)) {
390 if (IEEE80211_IS_CHAN_ANYG(chan)) {
392 * Use a mixed 11b/11g basic rate set.
394 ieee80211_setbasicrates(&ni->ni_rates,
396 if (vap->iv_flags & IEEE80211_F_PUREG) {
398 * Also mark OFDM rates basic so 11b
399 * stations do not join (WiFi compliance).
401 ieee80211_addbasicrates(&ni->ni_rates,
404 } else if (IEEE80211_IS_CHAN_B(chan)) {
406 * Force pure 11b rate set.
408 ieee80211_setbasicrates(&ni->ni_rates,
413 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
417 * Reset bss state on transition to the INIT state.
418 * Clear any stations from the table (they have been
419 * deauth'd) and reset the bss node (clears key, rate
423 ieee80211_reset_bss(struct ieee80211vap *vap)
425 struct ieee80211com *ic = vap->iv_ic;
426 struct ieee80211_node *ni, *obss;
428 ieee80211_node_table_reset(&ic->ic_sta, vap);
429 /* XXX multi-bss: wrong */
430 ieee80211_reset_erp(ic);
432 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
433 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
435 vap->iv_bss = ieee80211_ref_node(ni);
438 ni->ni_intval = ic->ic_bintval;
439 ieee80211_free_node(obss);
441 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
445 match_ssid(const struct ieee80211_node *ni,
446 int nssid, const struct ieee80211_scan_ssid ssids[])
450 for (i = 0; i < nssid; i++) {
451 if (ni->ni_esslen == ssids[i].len &&
452 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
459 * Test a node for suitability/compatibility.
462 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
464 struct ieee80211com *ic = ni->ni_ic;
467 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
469 if (vap->iv_opmode == IEEE80211_M_IBSS) {
470 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
473 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
476 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
477 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
480 /* XXX does this mean privacy is supported or required? */
481 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
484 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
485 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
486 if (rate & IEEE80211_RATE_BASIC)
488 if (vap->iv_des_nssid != 0 &&
489 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
491 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
492 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
497 #ifdef IEEE80211_DEBUG
499 * Display node suitability/compatibility.
502 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
504 struct ieee80211com *ic = ni->ni_ic;
509 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
511 if (vap->iv_opmode == IEEE80211_M_IBSS) {
512 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
515 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
518 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
519 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
522 /* XXX does this mean privacy is supported or required? */
523 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
526 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
527 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
528 if (rate & IEEE80211_RATE_BASIC)
530 if (vap->iv_des_nssid != 0 &&
531 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
533 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
534 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
537 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
538 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
540 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
541 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
542 fail & 0x08 ? '!' : ' ');
544 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
545 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
547 fail & 0x02 ? '!' : ' ');
549 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
550 fail & 0x04 ? '!' : ' ');
551 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
552 printf("%s\n", fail & 0x10 ? "!" : "");
554 #endif /* IEEE80211_DEBUG */
557 * Handle 802.11 ad hoc network merge. The
558 * convention, set by the Wireless Ethernet Compatibility Alliance
559 * (WECA), is that an 802.11 station will change its BSSID to match
560 * the "oldest" 802.11 ad hoc network, on the same channel, that
561 * has the station's desired SSID. The "oldest" 802.11 network
562 * sends beacons with the greatest TSF timestamp.
564 * The caller is assumed to validate TSF's before attempting a merge.
566 * Return !0 if the BSSID changed, 0 otherwise.
569 ieee80211_ibss_merge(struct ieee80211_node *ni)
571 struct ieee80211vap *vap = ni->ni_vap;
572 #ifdef IEEE80211_DEBUG
573 struct ieee80211com *ic = ni->ni_ic;
576 if (ni == vap->iv_bss ||
577 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
578 /* unchanged, nothing to do */
581 if (!check_bss(vap, ni)) {
582 /* capabilities mismatch */
583 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
584 "%s: merge failed, capabilities mismatch\n", __func__);
585 #ifdef IEEE80211_DEBUG
586 if (ieee80211_msg_assoc(vap))
587 check_bss_debug(vap, ni);
589 vap->iv_stats.is_ibss_capmismatch++;
592 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
593 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
594 ether_sprintf(ni->ni_bssid),
595 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
596 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
597 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
599 return ieee80211_sta_join1(ieee80211_ref_node(ni));
603 * Calculate HT channel promotion flags for all vaps.
604 * This assumes ni_chan have been setup for each vap.
607 gethtadjustflags(struct ieee80211com *ic)
609 struct ieee80211vap *vap;
614 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
615 if (vap->iv_state < IEEE80211_S_RUN)
617 switch (vap->iv_opmode) {
618 case IEEE80211_M_WDS:
619 case IEEE80211_M_STA:
620 case IEEE80211_M_AHDEMO:
621 case IEEE80211_M_HOSTAP:
622 case IEEE80211_M_IBSS:
623 case IEEE80211_M_MBSS:
624 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
634 * Check if the current channel needs to change based on whether
635 * any vap's are using HT20/HT40. This is used to sync the state
636 * of ic_curchan after a channel width change on a running vap.
639 ieee80211_sync_curchan(struct ieee80211com *ic)
641 struct ieee80211_channel *c;
643 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
644 if (c != ic->ic_curchan) {
646 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
647 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
648 IEEE80211_UNLOCK(ic);
649 ic->ic_set_channel(ic);
650 ieee80211_radiotap_chan_change(ic);
656 * Setup the current channel. The request channel may be
657 * promoted if other vap's are operating with HT20/HT40.
660 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
662 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
663 int flags = gethtadjustflags(ic);
665 * Check for channel promotion required to support the
666 * set of running vap's. This assumes we are called
667 * after ni_chan is setup for each vap.
669 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
670 if (flags > ieee80211_htchanflags(c))
671 c = ieee80211_ht_adjust_channel(ic, c, flags);
673 ic->ic_bsschan = ic->ic_curchan = c;
674 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
675 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
679 * Change the current channel. The channel change is guaranteed to have
680 * happened before the next state change.
683 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
685 ieee80211_setupcurchan(ic, c);
686 ieee80211_runtask(ic, &ic->ic_chan_task);
690 ieee80211_update_chw(struct ieee80211com *ic)
693 ieee80211_setupcurchan(ic, ic->ic_curchan);
694 ieee80211_runtask(ic, &ic->ic_chw_task);
698 * Join the specified IBSS/BSS network. The node is assumed to
699 * be passed in with a held reference.
702 ieee80211_sta_join1(struct ieee80211_node *selbs)
704 struct ieee80211vap *vap = selbs->ni_vap;
705 struct ieee80211com *ic = selbs->ni_ic;
706 struct ieee80211_node *obss;
710 * Committed to selbs, setup state.
714 * Check if old+new node have the same address in which
715 * case we can reassociate when operating in sta mode.
717 canreassoc = (obss != NULL &&
718 vap->iv_state == IEEE80211_S_RUN &&
719 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
720 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */
722 copy_bss(selbs, obss);
723 ieee80211_node_decref(obss); /* iv_bss reference */
724 ieee80211_free_node(obss); /* station table reference */
725 obss = NULL; /* NB: guard against later use */
729 * Delete unusable rates; we've already checked
730 * that the negotiated rate set is acceptable.
732 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
733 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
735 ieee80211_setcurchan(ic, selbs->ni_chan);
737 * Set the erp state (mostly the slot time) to deal with
738 * the auto-select case; this should be redundant if the
741 ieee80211_reset_erp(ic);
742 ieee80211_wme_initparams(vap);
744 if (vap->iv_opmode == IEEE80211_M_STA) {
747 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
750 * Act as if we received a DEAUTH frame in case we
751 * are invoked from the RUN state. This will cause
752 * us to try to re-authenticate if we are operating
755 ieee80211_new_state(vap, IEEE80211_S_AUTH,
756 IEEE80211_FC0_SUBTYPE_DEAUTH);
759 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
764 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
765 const struct ieee80211_scan_entry *se)
767 struct ieee80211com *ic = vap->iv_ic;
768 struct ieee80211_node *ni;
770 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
777 * Expand scan state into node's format.
778 * XXX may not need all this stuff
780 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
781 ni->ni_esslen = se->se_ssid[1];
782 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
783 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
784 ni->ni_intval = se->se_intval;
785 ni->ni_capinfo = se->se_capinfo;
787 ni->ni_timoff = se->se_timoff;
788 ni->ni_fhdwell = se->se_fhdwell;
789 ni->ni_fhindex = se->se_fhindex;
790 ni->ni_erp = se->se_erp;
791 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
792 ni->ni_noise = se->se_noise;
793 if (vap->iv_opmode == IEEE80211_M_STA) {
794 /* NB: only infrastructure mode requires an associd */
795 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
798 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
799 ieee80211_ies_expand(&ni->ni_ies);
800 #ifdef IEEE80211_SUPPORT_SUPERG
801 if (ni->ni_ies.ath_ie != NULL)
802 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
804 if (ni->ni_ies.htcap_ie != NULL)
805 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
806 if (ni->ni_ies.htinfo_ie != NULL)
807 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
808 #ifdef IEEE80211_SUPPORT_MESH
809 if (ni->ni_ies.meshid_ie != NULL)
810 ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
812 #ifdef IEEE80211_SUPPORT_TDMA
813 if (ni->ni_ies.tdma_ie != NULL)
814 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
818 vap->iv_dtim_period = se->se_dtimperiod;
819 vap->iv_dtim_count = 0;
821 /* NB: must be after ni_chan is setup */
822 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
824 if (ieee80211_iserp_rateset(&ni->ni_rates))
825 ni->ni_flags |= IEEE80211_NODE_ERP;
828 * Setup HT state for this node if it's available, otherwise
829 * non-STA modes won't pick this state up.
831 * For IBSS and related modes that don't go through an
832 * association request/response, the only appropriate place
833 * to setup the HT state is here.
835 if (ni->ni_ies.htinfo_ie != NULL &&
836 ni->ni_ies.htcap_ie != NULL &&
837 vap->iv_flags_ht & IEEE80211_FHT_HT) {
838 ieee80211_ht_node_init(ni);
839 ieee80211_ht_updateparams(ni,
841 ni->ni_ies.htinfo_ie);
842 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
843 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
844 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
846 /* XXX else check for ath FF? */
847 /* XXX QoS? Difficult given that WME config is specific to a master */
849 ieee80211_node_setuptxparms(ni);
850 ieee80211_ratectl_node_init(ni);
852 return ieee80211_sta_join1(ieee80211_ref_node(ni));
856 * Leave the specified IBSS/BSS network. The node is assumed to
857 * be passed in with a held reference.
860 ieee80211_sta_leave(struct ieee80211_node *ni)
862 struct ieee80211com *ic = ni->ni_ic;
864 ic->ic_node_cleanup(ni);
865 ieee80211_notify_node_leave(ni);
869 * Send a deauthenticate frame and drop the station.
872 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
874 /* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
875 ieee80211_ref_node(ni);
876 if (ni->ni_associd != 0)
877 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
878 ieee80211_node_leave(ni);
879 ieee80211_free_node(ni);
882 static struct ieee80211_node *
883 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
885 struct ieee80211_node *ni;
887 ni = (struct ieee80211_node *) malloc(sizeof(struct ieee80211_node),
888 M_80211_NODE, M_NOWAIT | M_ZERO);
893 * Initialize an ie blob with the specified data. If previous
894 * data exists re-use the data block. As a side effect we clear
895 * all references to specific ie's; the caller is required to
899 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
901 /* NB: assumes data+len are the last fields */
902 memset(ies, 0, offsetof(struct ieee80211_ies, data));
903 if (ies->data != NULL && ies->len != len) {
904 /* data size changed */
905 free(ies->data, M_80211_NODE_IE);
908 if (ies->data == NULL) {
909 ies->data = (uint8_t *) malloc(len, M_80211_NODE_IE, M_NOWAIT);
910 if (ies->data == NULL) {
912 /* NB: pointers have already been zero'd above */
916 memcpy(ies->data, data, len);
922 * Reclaim storage for an ie blob.
925 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
927 if (ies->data != NULL)
928 free(ies->data, M_80211_NODE_IE);
932 * Expand an ie blob data contents and to fillin individual
933 * ie pointers. The data blob is assumed to be well-formed;
934 * we don't do any validity checking of ie lengths.
937 ieee80211_ies_expand(struct ieee80211_ies *ies)
946 case IEEE80211_ELEMID_VENDOR:
949 else if (iswmeoui(ie))
951 #ifdef IEEE80211_SUPPORT_SUPERG
952 else if (isatherosoui(ie))
955 #ifdef IEEE80211_SUPPORT_TDMA
956 else if (istdmaoui(ie))
960 case IEEE80211_ELEMID_RSN:
963 case IEEE80211_ELEMID_HTCAP:
966 case IEEE80211_ELEMID_HTINFO:
969 #ifdef IEEE80211_SUPPORT_MESH
970 case IEEE80211_ELEMID_MESHID:
981 * Reclaim any resources in a node and reset any critical
982 * state. Typically nodes are free'd immediately after,
983 * but in some cases the storage may be reused so we need
984 * to insure consistent state (should probably fix that).
987 node_cleanup(struct ieee80211_node *ni)
989 struct ieee80211vap *vap = ni->ni_vap;
990 struct ieee80211com *ic = ni->ni_ic;
993 /* NB: preserve ni_table */
994 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
995 if (vap->iv_opmode != IEEE80211_M_STA)
997 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
998 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
999 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
1002 * Cleanup any HT-related state.
1004 if (ni->ni_flags & IEEE80211_NODE_HT)
1005 ieee80211_ht_node_cleanup(ni);
1006 #ifdef IEEE80211_SUPPORT_SUPERG
1007 else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
1008 ieee80211_ff_node_cleanup(ni);
1010 #ifdef IEEE80211_SUPPORT_MESH
1012 * Cleanup any mesh-related state.
1014 if (vap->iv_opmode == IEEE80211_M_MBSS)
1015 ieee80211_mesh_node_cleanup(ni);
1018 * Clear any staging queue entries.
1020 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1023 * Clear AREF flag that marks the authorization refcnt bump
1024 * has happened. This is probably not needed as the node
1025 * should always be removed from the table so not found but
1026 * do it just in case.
1027 * Likewise clear the ASSOCID flag as these flags are intended
1028 * to be managed in tandem.
1030 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1033 * Drain power save queue and, if needed, clear TIM.
1035 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1036 vap->iv_set_tim(ni, 0);
1039 if (ni->ni_challenge != NULL) {
1040 free(ni->ni_challenge, M_80211_NODE);
1041 ni->ni_challenge = NULL;
1044 * Preserve SSID, WPA, and WME ie's so the bss node is
1045 * reusable during a re-auth/re-assoc state transition.
1046 * If we remove these data they will not be recreated
1047 * because they come from a probe-response or beacon frame
1048 * which cannot be expected prior to the association-response.
1049 * This should not be an issue when operating in other modes
1050 * as stations leaving always go through a full state transition
1051 * which will rebuild this state.
1053 * XXX does this leave us open to inheriting old state?
1055 for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1056 if (ni->ni_rxfrag[i] != NULL) {
1057 m_freem(ni->ni_rxfrag[i]);
1058 ni->ni_rxfrag[i] = NULL;
1061 * Must be careful here to remove any key map entry w/o a LOR.
1063 ieee80211_node_delucastkey(ni);
1067 node_free(struct ieee80211_node *ni)
1069 struct ieee80211com *ic = ni->ni_ic;
1071 ieee80211_ratectl_node_deinit(ni);
1072 ic->ic_node_cleanup(ni);
1073 ieee80211_ies_cleanup(&ni->ni_ies);
1074 ieee80211_psq_cleanup(&ni->ni_psq);
1075 free(ni, M_80211_NODE);
1079 node_age(struct ieee80211_node *ni)
1081 struct ieee80211vap *vap = ni->ni_vap;
1083 IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
1086 * Age frames on the power save queue.
1088 if (ieee80211_node_psq_age(ni) != 0 &&
1089 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1090 vap->iv_set_tim(ni, 0);
1092 * Age out HT resources (e.g. frames on the
1093 * A-MPDU reorder queues).
1095 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1096 ieee80211_ht_node_age(ni);
1100 node_getrssi(const struct ieee80211_node *ni)
1102 uint32_t avgrssi = ni->ni_avgrssi;
1105 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1107 rssi = IEEE80211_RSSI_GET(avgrssi);
1108 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1112 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1114 *rssi = node_getrssi(ni);
1115 *noise = ni->ni_noise;
1119 node_getmimoinfo(const struct ieee80211_node *ni,
1120 struct ieee80211_mimo_info *info)
1126 bzero(info, sizeof(*info));
1128 for (i = 0; i < ni->ni_mimo_chains; i++) {
1129 avgrssi = ni->ni_mimo_rssi_ctl[i];
1130 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1133 rssi = IEEE80211_RSSI_GET(avgrssi);
1134 info->rssi[i] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1136 info->noise[i] = ni->ni_mimo_noise_ctl[i];
1139 /* XXX ext radios? */
1144 struct ieee80211_node *
1145 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1146 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1148 struct ieee80211com *ic = nt->nt_ic;
1149 struct ieee80211_node *ni;
1152 ni = ic->ic_node_alloc(vap, macaddr);
1154 vap->iv_stats.is_rx_nodealloc++;
1158 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1159 "%s %p<%s> in %s table\n", __func__, ni,
1160 ether_sprintf(macaddr), nt->nt_name);
1162 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1163 hash = IEEE80211_NODE_HASH(ic, macaddr);
1164 ieee80211_node_initref(ni); /* mark referenced */
1165 ni->ni_chan = IEEE80211_CHAN_ANYC;
1166 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1167 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1168 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1169 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1170 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1171 ni->ni_inact_reload = nt->nt_inact_init;
1172 ni->ni_inact = ni->ni_inact_reload;
1173 ni->ni_ath_defkeyix = 0x7fff;
1174 ieee80211_psq_init(&ni->ni_psq, "unknown");
1175 #ifdef IEEE80211_SUPPORT_MESH
1176 if (vap->iv_opmode == IEEE80211_M_MBSS)
1177 ieee80211_mesh_node_init(vap, ni);
1179 IEEE80211_NODE_LOCK(nt);
1180 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1181 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1185 IEEE80211_NODE_UNLOCK(nt);
1187 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1188 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1190 ieee80211_ratectl_node_init(ni);
1196 * Craft a temporary node suitable for sending a management frame
1197 * to the specified station. We craft only as much state as we
1198 * need to do the work since the node will be immediately reclaimed
1199 * once the send completes.
1201 struct ieee80211_node *
1202 ieee80211_tmp_node(struct ieee80211vap *vap,
1203 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1205 struct ieee80211com *ic = vap->iv_ic;
1206 struct ieee80211_node *ni;
1208 ni = ic->ic_node_alloc(vap, macaddr);
1210 struct ieee80211_node *bss = vap->iv_bss;
1212 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1213 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1215 ni->ni_table = NULL; /* NB: pedantic */
1216 ni->ni_ic = ic; /* NB: needed to set channel */
1219 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1220 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1221 ieee80211_node_initref(ni); /* mark referenced */
1222 /* NB: required by ieee80211_fix_rate */
1223 ieee80211_node_set_chan(ni, bss->ni_chan);
1224 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1225 IEEE80211_KEYIX_NONE);
1226 ni->ni_txpower = bss->ni_txpower;
1227 /* XXX optimize away */
1228 ieee80211_psq_init(&ni->ni_psq, "unknown");
1230 ieee80211_ratectl_node_init(ni);
1233 vap->iv_stats.is_rx_nodealloc++;
1238 struct ieee80211_node *
1239 ieee80211_dup_bss(struct ieee80211vap *vap,
1240 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1242 struct ieee80211com *ic = vap->iv_ic;
1243 struct ieee80211_node *ni;
1245 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1247 struct ieee80211_node *bss = vap->iv_bss;
1249 * Inherit from iv_bss.
1252 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1253 ieee80211_node_set_chan(ni, bss->ni_chan);
1259 * Create a bss node for a legacy WDS vap. The far end does
1260 * not associate so we just create create a new node and
1261 * simulate an association. The caller is responsible for
1262 * installing the node as the bss node and handling any further
1263 * setup work like authorizing the port.
1265 struct ieee80211_node *
1266 ieee80211_node_create_wds(struct ieee80211vap *vap,
1267 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1269 struct ieee80211com *ic = vap->iv_ic;
1270 struct ieee80211_node *ni;
1272 /* XXX check if node already in sta table? */
1273 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1275 ni->ni_wdsvap = vap;
1276 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1278 * Inherit any manually configured settings.
1280 copy_bss(ni, vap->iv_bss);
1281 ieee80211_node_set_chan(ni, chan);
1282 /* NB: propagate ssid so available to WPA supplicant */
1283 ni->ni_esslen = vap->iv_des_ssid[0].len;
1284 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1285 /* NB: no associd for peer */
1287 * There are no management frames to use to
1288 * discover neighbor capabilities, so blindly
1289 * propagate the local configuration.
1291 if (vap->iv_flags & IEEE80211_F_WME)
1292 ni->ni_flags |= IEEE80211_NODE_QOS;
1293 #ifdef IEEE80211_SUPPORT_SUPERG
1294 if (vap->iv_flags & IEEE80211_F_FF)
1295 ni->ni_flags |= IEEE80211_NODE_FF;
1297 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1298 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1300 * Device is HT-capable and HT is enabled for
1301 * the vap; setup HT operation. On return
1302 * ni_chan will be adjusted to an HT channel.
1304 ieee80211_ht_wds_init(ni);
1306 struct ieee80211_channel *c = ni->ni_chan;
1308 * Force a legacy channel to be used.
1310 c = ieee80211_find_channel(ic,
1311 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1312 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1313 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1320 struct ieee80211_node *
1321 #ifdef IEEE80211_DEBUG_REFCNT
1322 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1323 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1325 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1326 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1329 struct ieee80211_node *ni;
1332 IEEE80211_NODE_LOCK_ASSERT(nt);
1334 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1335 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1336 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1337 ieee80211_ref_node(ni); /* mark referenced */
1338 #ifdef IEEE80211_DEBUG_REFCNT
1339 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1340 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1342 ni, ether_sprintf(ni->ni_macaddr),
1343 ieee80211_node_refcnt(ni));
1351 struct ieee80211_node *
1352 #ifdef IEEE80211_DEBUG_REFCNT
1353 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1354 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1356 ieee80211_find_node(struct ieee80211_node_table *nt,
1357 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1360 struct ieee80211_node *ni;
1362 IEEE80211_NODE_LOCK(nt);
1363 ni = ieee80211_find_node_locked(nt, macaddr);
1364 IEEE80211_NODE_UNLOCK(nt);
1368 struct ieee80211_node *
1369 #ifdef IEEE80211_DEBUG_REFCNT
1370 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1371 const struct ieee80211vap *vap,
1372 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1374 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1375 const struct ieee80211vap *vap,
1376 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1379 struct ieee80211_node *ni;
1382 IEEE80211_NODE_LOCK_ASSERT(nt);
1384 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1385 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1386 if (ni->ni_vap == vap &&
1387 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1388 ieee80211_ref_node(ni); /* mark referenced */
1389 #ifdef IEEE80211_DEBUG_REFCNT
1390 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1391 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1393 ni, ether_sprintf(ni->ni_macaddr),
1394 ieee80211_node_refcnt(ni));
1402 struct ieee80211_node *
1403 #ifdef IEEE80211_DEBUG_REFCNT
1404 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1405 const struct ieee80211vap *vap,
1406 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1408 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1409 const struct ieee80211vap *vap,
1410 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1413 struct ieee80211_node *ni;
1415 IEEE80211_NODE_LOCK(nt);
1416 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1417 IEEE80211_NODE_UNLOCK(nt);
1422 * Fake up a node; this handles node discovery in adhoc mode.
1423 * Note that for the driver's benefit we we treat this like
1424 * an association so the driver has an opportunity to setup
1425 * it's private state.
1427 struct ieee80211_node *
1428 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1429 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1431 struct ieee80211_node *ni;
1433 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1434 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1435 ni = ieee80211_dup_bss(vap, macaddr);
1437 struct ieee80211com *ic = vap->iv_ic;
1439 /* XXX no rate negotiation; just dup */
1440 ni->ni_rates = vap->iv_bss->ni_rates;
1441 if (ieee80211_iserp_rateset(&ni->ni_rates))
1442 ni->ni_flags |= IEEE80211_NODE_ERP;
1443 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1445 * In adhoc demo mode there are no management
1446 * frames to use to discover neighbor capabilities,
1447 * so blindly propagate the local configuration
1448 * so we can do interesting things (e.g. use
1449 * WME to disable ACK's).
1451 if (vap->iv_flags & IEEE80211_F_WME)
1452 ni->ni_flags |= IEEE80211_NODE_QOS;
1453 #ifdef IEEE80211_SUPPORT_SUPERG
1454 if (vap->iv_flags & IEEE80211_F_FF)
1455 ni->ni_flags |= IEEE80211_NODE_FF;
1458 ieee80211_node_setuptxparms(ni);
1459 ieee80211_ratectl_node_init(ni);
1460 if (ic->ic_newassoc != NULL)
1461 ic->ic_newassoc(ni, 1);
1462 /* XXX not right for 802.1x/WPA */
1463 ieee80211_node_authorize(ni);
1469 ieee80211_init_neighbor(struct ieee80211_node *ni,
1470 const struct ieee80211_frame *wh,
1471 const struct ieee80211_scanparams *sp)
1473 int do_ht_setup = 0;
1475 ni->ni_esslen = sp->ssid[1];
1476 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1477 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1478 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1479 ni->ni_intval = sp->bintval;
1480 ni->ni_capinfo = sp->capinfo;
1481 ni->ni_chan = ni->ni_ic->ic_curchan;
1482 ni->ni_fhdwell = sp->fhdwell;
1483 ni->ni_fhindex = sp->fhindex;
1484 ni->ni_erp = sp->erp;
1485 ni->ni_timoff = sp->timoff;
1486 #ifdef IEEE80211_SUPPORT_MESH
1487 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1488 ieee80211_mesh_init_neighbor(ni, wh, sp);
1490 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1491 ieee80211_ies_expand(&ni->ni_ies);
1492 if (ni->ni_ies.wme_ie != NULL)
1493 ni->ni_flags |= IEEE80211_NODE_QOS;
1495 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1496 #ifdef IEEE80211_SUPPORT_SUPERG
1497 if (ni->ni_ies.ath_ie != NULL)
1498 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1500 if (ni->ni_ies.htcap_ie != NULL)
1501 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1502 if (ni->ni_ies.htinfo_ie != NULL)
1503 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1505 if ((ni->ni_ies.htcap_ie != NULL) &&
1506 (ni->ni_ies.htinfo_ie != NULL) &&
1507 (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1512 /* NB: must be after ni_chan is setup */
1513 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1514 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1515 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1518 * If the neighbor is HT compatible, flip that on.
1521 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1522 "%s: doing HT setup\n", __func__);
1523 ieee80211_ht_node_init(ni);
1524 ieee80211_ht_updateparams(ni,
1525 ni->ni_ies.htcap_ie,
1526 ni->ni_ies.htinfo_ie);
1527 ieee80211_setup_htrates(ni,
1528 ni->ni_ies.htcap_ie,
1529 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1530 ieee80211_setup_basic_htrates(ni,
1531 ni->ni_ies.htinfo_ie);
1532 ieee80211_node_setuptxparms(ni);
1533 ieee80211_ratectl_node_init(ni);
1538 * Do node discovery in adhoc mode on receipt of a beacon
1539 * or probe response frame. Note that for the driver's
1540 * benefit we we treat this like an association so the
1541 * driver has an opportunity to setup it's private state.
1543 struct ieee80211_node *
1544 ieee80211_add_neighbor(struct ieee80211vap *vap,
1545 const struct ieee80211_frame *wh,
1546 const struct ieee80211_scanparams *sp)
1548 struct ieee80211_node *ni;
1550 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1551 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1552 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1554 struct ieee80211com *ic = vap->iv_ic;
1556 ieee80211_init_neighbor(ni, wh, sp);
1557 if (ieee80211_iserp_rateset(&ni->ni_rates))
1558 ni->ni_flags |= IEEE80211_NODE_ERP;
1559 ieee80211_node_setuptxparms(ni);
1560 ieee80211_ratectl_node_init(ni);
1561 if (ic->ic_newassoc != NULL)
1562 ic->ic_newassoc(ni, 1);
1563 /* XXX not right for 802.1x/WPA */
1564 ieee80211_node_authorize(ni);
1569 #define IS_PROBEREQ(wh) \
1570 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1571 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1572 #define IS_BCAST_PROBEREQ(wh) \
1573 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1574 ((const struct ieee80211_frame *)(wh))->i_addr3))
1576 static __inline struct ieee80211_node *
1577 _find_rxnode(struct ieee80211_node_table *nt,
1578 const struct ieee80211_frame_min *wh)
1580 if (IS_BCAST_PROBEREQ(wh))
1581 return NULL; /* spam bcast probe req to all vap's */
1582 return ieee80211_find_node_locked(nt, wh->i_addr2);
1586 * Locate the node for sender, track state, and then pass the
1587 * (referenced) node up to the 802.11 layer for its use. Note
1588 * we can return NULL if the sender is not in the table.
1590 struct ieee80211_node *
1591 #ifdef IEEE80211_DEBUG_REFCNT
1592 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1593 const struct ieee80211_frame_min *wh, const char *func, int line)
1595 ieee80211_find_rxnode(struct ieee80211com *ic,
1596 const struct ieee80211_frame_min *wh)
1599 struct ieee80211_node_table *nt;
1600 struct ieee80211_node *ni;
1603 IEEE80211_NODE_LOCK(nt);
1604 ni = _find_rxnode(nt, wh);
1605 IEEE80211_NODE_UNLOCK(nt);
1611 * Like ieee80211_find_rxnode but use the supplied h/w
1612 * key index as a hint to locate the node in the key
1613 * mapping table. If an entry is present at the key
1614 * index we return it; otherwise do a normal lookup and
1615 * update the mapping table if the station has a unicast
1616 * key assigned to it.
1618 struct ieee80211_node *
1619 #ifdef IEEE80211_DEBUG_REFCNT
1620 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1621 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1622 const char *func, int line)
1624 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1625 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1628 struct ieee80211_node_table *nt;
1629 struct ieee80211_node *ni;
1632 IEEE80211_NODE_LOCK(nt);
1633 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1634 ni = nt->nt_keyixmap[keyix];
1638 ni = _find_rxnode(nt, wh);
1639 if (ni != NULL && nt->nt_keyixmap != NULL) {
1641 * If the station has a unicast key cache slot
1642 * assigned update the key->node mapping table.
1644 keyix = ni->ni_ucastkey.wk_rxkeyix;
1645 /* XXX can keyixmap[keyix] != NULL? */
1646 if (keyix < nt->nt_keyixmax &&
1647 nt->nt_keyixmap[keyix] == NULL) {
1648 IEEE80211_DPRINTF(ni->ni_vap,
1650 "%s: add key map entry %p<%s> refcnt %d\n",
1651 __func__, ni, ether_sprintf(ni->ni_macaddr),
1652 ieee80211_node_refcnt(ni)+1);
1653 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1657 if (IS_BCAST_PROBEREQ(wh))
1658 ni = NULL; /* spam bcast probe req to all vap's */
1660 ieee80211_ref_node(ni);
1662 IEEE80211_NODE_UNLOCK(nt);
1666 #undef IS_BCAST_PROBEREQ
1670 * Return a reference to the appropriate node for sending
1671 * a data frame. This handles node discovery in adhoc networks.
1673 struct ieee80211_node *
1674 #ifdef IEEE80211_DEBUG_REFCNT
1675 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1676 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1677 const char *func, int line)
1679 ieee80211_find_txnode(struct ieee80211vap *vap,
1680 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1683 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1684 struct ieee80211_node *ni;
1687 * The destination address should be in the node table
1688 * unless this is a multicast/broadcast frame. We can
1689 * also optimize station mode operation, all frames go
1692 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1693 IEEE80211_NODE_LOCK(nt);
1694 if (vap->iv_opmode == IEEE80211_M_STA ||
1695 vap->iv_opmode == IEEE80211_M_WDS ||
1696 IEEE80211_IS_MULTICAST(macaddr))
1697 ni = ieee80211_ref_node(vap->iv_bss);
1699 ni = ieee80211_find_node_locked(nt, macaddr);
1700 IEEE80211_NODE_UNLOCK(nt);
1703 if (vap->iv_opmode == IEEE80211_M_IBSS ||
1704 vap->iv_opmode == IEEE80211_M_AHDEMO) {
1706 * In adhoc mode cons up a node for the destination.
1707 * Note that we need an additional reference for the
1708 * caller to be consistent with
1709 * ieee80211_find_node_locked.
1711 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1713 (void) ieee80211_ref_node(ni);
1715 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1716 "no node, discard frame (%s)", __func__);
1717 vap->iv_stats.is_tx_nonode++;
1724 _ieee80211_free_node(struct ieee80211_node *ni)
1726 struct ieee80211_node_table *nt = ni->ni_table;
1729 * NB: careful about referencing the vap as it may be
1730 * gone if the last reference was held by a driver.
1731 * We know the com will always be present so it's safe
1732 * to use ni_ic below to reclaim resources.
1735 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1736 "%s %p<%s> in %s table\n", __func__, ni,
1737 ether_sprintf(ni->ni_macaddr),
1738 nt != NULL ? nt->nt_name : "<gone>");
1740 if (ni->ni_associd != 0) {
1741 struct ieee80211vap *vap = ni->ni_vap;
1742 if (vap->iv_aid_bitmap != NULL)
1743 IEEE80211_AID_CLR(vap, ni->ni_associd);
1746 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1747 LIST_REMOVE(ni, ni_hash);
1749 ni->ni_ic->ic_node_free(ni);
1753 #ifdef IEEE80211_DEBUG_REFCNT
1754 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1756 ieee80211_free_node(struct ieee80211_node *ni)
1759 struct ieee80211_node_table *nt = ni->ni_table;
1761 #ifdef IEEE80211_DEBUG_REFCNT
1762 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1763 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1764 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1767 IEEE80211_NODE_LOCK(nt);
1768 if (ieee80211_node_dectestref(ni)) {
1770 * Last reference, reclaim state.
1772 _ieee80211_free_node(ni);
1773 } else if (ieee80211_node_refcnt(ni) == 1 &&
1774 nt->nt_keyixmap != NULL) {
1775 ieee80211_keyix keyix;
1777 * Check for a last reference in the key mapping table.
1779 keyix = ni->ni_ucastkey.wk_rxkeyix;
1780 if (keyix < nt->nt_keyixmax &&
1781 nt->nt_keyixmap[keyix] == ni) {
1782 IEEE80211_DPRINTF(ni->ni_vap,
1784 "%s: %p<%s> clear key map entry", __func__,
1785 ni, ether_sprintf(ni->ni_macaddr));
1786 nt->nt_keyixmap[keyix] = NULL;
1787 ieee80211_node_decref(ni); /* XXX needed? */
1788 _ieee80211_free_node(ni);
1791 IEEE80211_NODE_UNLOCK(nt);
1793 if (ieee80211_node_dectestref(ni))
1794 _ieee80211_free_node(ni);
1799 * Reclaim a unicast key and clear any key cache state.
1802 ieee80211_node_delucastkey(struct ieee80211_node *ni)
1804 struct ieee80211com *ic = ni->ni_ic;
1805 struct ieee80211_node_table *nt = &ic->ic_sta;
1806 struct ieee80211_node *nikey;
1807 ieee80211_keyix keyix;
1808 int isowned, status;
1811 * NB: We must beware of LOR here; deleting the key
1812 * can cause the crypto layer to block traffic updates
1813 * which can generate a LOR against the node table lock;
1814 * grab it here and stash the key index for our use below.
1816 * Must also beware of recursion on the node table lock.
1817 * When called from node_cleanup we may already have
1818 * the node table lock held. Unfortunately there's no
1819 * way to separate out this path so we must do this
1822 isowned = IEEE80211_NODE_IS_LOCKED(nt);
1824 IEEE80211_NODE_LOCK(nt);
1826 status = 1; /* NB: success */
1827 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1828 keyix = ni->ni_ucastkey.wk_rxkeyix;
1829 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1830 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1831 nikey = nt->nt_keyixmap[keyix];
1832 nt->nt_keyixmap[keyix] = NULL;
1836 IEEE80211_NODE_UNLOCK(nt);
1838 if (nikey != NULL) {
1839 KASSERT(nikey == ni,
1840 ("key map out of sync, ni %p nikey %p", ni, nikey));
1841 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1842 "%s: delete key map entry %p<%s> refcnt %d\n",
1843 __func__, ni, ether_sprintf(ni->ni_macaddr),
1844 ieee80211_node_refcnt(ni)-1);
1845 ieee80211_free_node(ni);
1851 * Reclaim a node. If this is the last reference count then
1852 * do the normal free work. Otherwise remove it from the node
1853 * table and mark it gone by clearing the back-reference.
1856 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1858 ieee80211_keyix keyix;
1860 IEEE80211_NODE_LOCK_ASSERT(nt);
1862 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1863 "%s: remove %p<%s> from %s table, refcnt %d\n",
1864 __func__, ni, ether_sprintf(ni->ni_macaddr),
1865 nt->nt_name, ieee80211_node_refcnt(ni)-1);
1867 * Clear any entry in the unicast key mapping table.
1868 * We need to do it here so rx lookups don't find it
1869 * in the mapping table even if it's not in the hash
1870 * table. We cannot depend on the mapping table entry
1871 * being cleared because the node may not be free'd.
1873 keyix = ni->ni_ucastkey.wk_rxkeyix;
1874 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1875 nt->nt_keyixmap[keyix] == ni) {
1876 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1877 "%s: %p<%s> clear key map entry %u\n",
1878 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1879 nt->nt_keyixmap[keyix] = NULL;
1880 ieee80211_node_decref(ni); /* NB: don't need free */
1882 if (!ieee80211_node_dectestref(ni)) {
1884 * Other references are present, just remove the
1885 * node from the table so it cannot be found. When
1886 * the references are dropped storage will be
1889 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1890 LIST_REMOVE(ni, ni_hash);
1891 ni->ni_table = NULL; /* clear reference */
1893 _ieee80211_free_node(ni);
1897 * Node table support.
1901 ieee80211_node_table_init(struct ieee80211com *ic,
1902 struct ieee80211_node_table *nt,
1903 const char *name, int inact, int keyixmax)
1905 struct ifnet *ifp = ic->ic_ifp;
1908 IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1909 IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1910 TAILQ_INIT(&nt->nt_node);
1913 nt->nt_inact_init = inact;
1914 nt->nt_keyixmax = keyixmax;
1915 if (nt->nt_keyixmax > 0) {
1916 nt->nt_keyixmap = (struct ieee80211_node **) malloc(
1917 keyixmax * sizeof(struct ieee80211_node *),
1918 M_80211_NODE, M_NOWAIT | M_ZERO);
1919 if (nt->nt_keyixmap == NULL)
1920 if_printf(ic->ic_ifp,
1921 "Cannot allocate key index map with %u entries\n",
1924 nt->nt_keyixmap = NULL;
1928 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
1929 struct ieee80211vap *match)
1931 struct ieee80211_node *ni, *next;
1933 IEEE80211_NODE_LOCK(nt);
1934 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1935 if (match != NULL && ni->ni_vap != match)
1937 /* XXX can this happen? if so need's work */
1938 if (ni->ni_associd != 0) {
1939 struct ieee80211vap *vap = ni->ni_vap;
1941 if (vap->iv_auth->ia_node_leave != NULL)
1942 vap->iv_auth->ia_node_leave(ni);
1943 if (vap->iv_aid_bitmap != NULL)
1944 IEEE80211_AID_CLR(vap, ni->ni_associd);
1946 ni->ni_wdsvap = NULL; /* clear reference */
1947 node_reclaim(nt, ni);
1949 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1951 * Make a separate pass to clear references to this vap
1952 * held by DWDS entries. They will not be matched above
1953 * because ni_vap will point to the ap vap but we still
1954 * need to clear ni_wdsvap when the WDS vap is destroyed
1957 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1958 if (ni->ni_wdsvap == match)
1959 ni->ni_wdsvap = NULL;
1961 IEEE80211_NODE_UNLOCK(nt);
1965 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1967 ieee80211_node_table_reset(nt, NULL);
1968 if (nt->nt_keyixmap != NULL) {
1970 /* XXX verify all entries are NULL */
1972 for (i = 0; i < nt->nt_keyixmax; i++)
1973 if (nt->nt_keyixmap[i] != NULL)
1974 printf("%s: %s[%u] still active\n", __func__,
1977 free(nt->nt_keyixmap, M_80211_NODE);
1978 nt->nt_keyixmap = NULL;
1980 IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1981 IEEE80211_NODE_LOCK_DESTROY(nt);
1985 * Timeout inactive stations and do related housekeeping.
1986 * Note that we cannot hold the node lock while sending a
1987 * frame as this would lead to a LOR. Instead we use a
1988 * generation number to mark nodes that we've scanned and
1989 * drop the lock and restart a scan if we have to time out
1990 * a node. Since we are single-threaded by virtue of
1991 * controlling the inactivity timer we can be sure this will
1992 * process each node only once.
1995 ieee80211_timeout_stations(struct ieee80211com *ic)
1997 struct ieee80211_node_table *nt = &ic->ic_sta;
1998 struct ieee80211vap *vap;
1999 struct ieee80211_node *ni;
2002 IEEE80211_NODE_ITERATE_LOCK(nt);
2003 gen = ++nt->nt_scangen;
2005 IEEE80211_NODE_LOCK(nt);
2006 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2007 if (ni->ni_scangen == gen) /* previously handled */
2009 ni->ni_scangen = gen;
2011 * Ignore entries for which have yet to receive an
2012 * authentication frame. These are transient and
2013 * will be reclaimed when the last reference to them
2014 * goes away (when frame xmits complete).
2018 * Only process stations when in RUN state. This
2019 * insures, for example, that we don't timeout an
2020 * inactive station during CAC. Note that CSA state
2021 * is actually handled in ieee80211_node_timeout as
2022 * it applies to more than timeout processing.
2024 if (vap->iv_state != IEEE80211_S_RUN)
2026 /* XXX can vap be NULL? */
2027 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2028 vap->iv_opmode == IEEE80211_M_STA) &&
2029 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2032 * Free fragment if not needed anymore
2033 * (last fragment older than 1s).
2034 * XXX doesn't belong here, move to node_age
2036 if (ni->ni_rxfrag[0] != NULL &&
2037 ticks > ni->ni_rxfragstamp + hz) {
2038 m_freem(ni->ni_rxfrag[0]);
2039 ni->ni_rxfrag[0] = NULL;
2041 if (ni->ni_inact > 0) {
2043 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2044 "%s: inact %u inact_reload %u nrates %u",
2045 __func__, ni->ni_inact, ni->ni_inact_reload,
2046 ni->ni_rates.rs_nrates);
2049 * Special case ourself; we may be idle for extended periods
2050 * of time and regardless reclaiming our state is wrong.
2051 * XXX run ic_node_age
2053 if (ni == vap->iv_bss)
2055 if (ni->ni_associd != 0 ||
2056 (vap->iv_opmode == IEEE80211_M_IBSS ||
2057 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2059 * Age/drain resources held by the station.
2061 ic->ic_node_age(ni);
2063 * Probe the station before time it out. We
2064 * send a null data frame which may not be
2065 * universally supported by drivers (need it
2066 * for ps-poll support so it should be...).
2068 * XXX don't probe the station unless we've
2069 * received a frame from them (and have
2070 * some idea of the rates they are capable
2071 * of); this will get fixed more properly
2072 * soon with better handling of the rate set.
2074 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2075 (0 < ni->ni_inact &&
2076 ni->ni_inact <= vap->iv_inact_probe) &&
2077 ni->ni_rates.rs_nrates != 0) {
2079 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2081 "probe station due to inactivity");
2083 * Grab a reference before unlocking the table
2084 * so the node cannot be reclaimed before we
2085 * send the frame. ieee80211_send_nulldata
2086 * understands we've done this and reclaims the
2087 * ref for us as needed.
2089 ieee80211_ref_node(ni);
2090 IEEE80211_NODE_UNLOCK(nt);
2091 ieee80211_send_nulldata(ni);
2096 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2097 ni->ni_inact <= 0) {
2099 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2100 "station timed out due to inactivity "
2101 "(refcnt %u)", ieee80211_node_refcnt(ni));
2103 * Send a deauthenticate frame and drop the station.
2104 * This is somewhat complicated due to reference counts
2105 * and locking. At this point a station will typically
2106 * have a reference count of 1. ieee80211_node_leave
2107 * will do a "free" of the node which will drop the
2108 * reference count. But in the meantime a reference
2109 * wil be held by the deauth frame. The actual reclaim
2110 * of the node will happen either after the tx is
2111 * completed or by ieee80211_node_leave.
2113 * Separately we must drop the node lock before sending
2114 * in case the driver takes a lock, as this can result
2115 * in a LOR between the node lock and the driver lock.
2117 ieee80211_ref_node(ni);
2118 IEEE80211_NODE_UNLOCK(nt);
2119 if (ni->ni_associd != 0) {
2120 IEEE80211_SEND_MGMT(ni,
2121 IEEE80211_FC0_SUBTYPE_DEAUTH,
2122 IEEE80211_REASON_AUTH_EXPIRE);
2124 ieee80211_node_leave(ni);
2125 ieee80211_free_node(ni);
2126 vap->iv_stats.is_node_timeout++;
2130 IEEE80211_NODE_UNLOCK(nt);
2132 IEEE80211_NODE_ITERATE_UNLOCK(nt);
2136 * Aggressively reclaim resources. This should be used
2137 * only in a critical situation to reclaim mbuf resources.
2140 ieee80211_drain(struct ieee80211com *ic)
2142 struct ieee80211_node_table *nt = &ic->ic_sta;
2143 struct ieee80211vap *vap;
2144 struct ieee80211_node *ni;
2146 IEEE80211_NODE_LOCK(nt);
2147 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2149 * Ignore entries for which have yet to receive an
2150 * authentication frame. These are transient and
2151 * will be reclaimed when the last reference to them
2152 * goes away (when frame xmits complete).
2156 * Only process stations when in RUN state. This
2157 * insures, for example, that we don't timeout an
2158 * inactive station during CAC. Note that CSA state
2159 * is actually handled in ieee80211_node_timeout as
2160 * it applies to more than timeout processing.
2162 if (vap->iv_state != IEEE80211_S_RUN)
2164 /* XXX can vap be NULL? */
2165 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2166 vap->iv_opmode == IEEE80211_M_STA) &&
2167 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2171 * XXX doesn't belong here, move to node_drain
2173 if (ni->ni_rxfrag[0] != NULL) {
2174 m_freem(ni->ni_rxfrag[0]);
2175 ni->ni_rxfrag[0] = NULL;
2178 * Drain resources held by the station.
2180 ic->ic_node_drain(ni);
2182 IEEE80211_NODE_UNLOCK(nt);
2186 * Per-ieee80211com inactivity timer callback.
2189 ieee80211_node_timeout(void *arg)
2191 struct ieee80211com *ic = arg;
2194 * Defer timeout processing if a channel switch is pending.
2195 * We typically need to be mute so not doing things that
2196 * might generate frames is good to handle in one place.
2197 * Supressing the station timeout processing may extend the
2198 * lifetime of inactive stations (by not decrementing their
2199 * idle counters) but this should be ok unless the CSA is
2200 * active for an unusually long time.
2202 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2203 ieee80211_scan_timeout(ic);
2204 ieee80211_timeout_stations(ic);
2205 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2208 ieee80211_erp_timeout(ic);
2209 ieee80211_ht_timeout(ic);
2210 IEEE80211_UNLOCK(ic);
2212 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2213 ieee80211_node_timeout, ic);
2217 * Iterate over the node table and return an array of ref'ed nodes.
2219 * This is separated out from calling the actual node function so that
2220 * no LORs will occur.
2222 * If there are too many nodes (ie, the number of nodes doesn't fit
2223 * within 'max_aid' entries) then the node references will be freed
2224 * and an error will be returned.
2226 * The responsibility of allocating and freeing "ni_arr" is up to
2230 ieee80211_iterate_nt(struct ieee80211_node_table *nt,
2231 struct ieee80211_node **ni_arr, uint16_t max_aid)
2235 struct ieee80211_node *ni;
2237 IEEE80211_NODE_ITERATE_LOCK(nt);
2238 IEEE80211_NODE_LOCK(nt);
2240 gen = ++nt->nt_scangen;
2244 * We simply assume here that since the node
2245 * scan generation doesn't change (as
2246 * we are holding both the node table and
2247 * node table iteration locks), we can simply
2248 * assign it to the node here.
2250 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2253 if_printf(nt->nt_ic->ic_ifp,
2254 "Node array overflow: max=%u", max_aid);
2257 ni_arr[i] = ieee80211_ref_node(ni);
2258 ni_arr[i]->ni_scangen = gen;
2263 * It's safe to unlock here.
2265 * If we're successful, the list is returned.
2266 * If we're unsuccessful, the list is ignored
2267 * and we remove our references.
2269 * This avoids any potential LOR with
2270 * ieee80211_free_node().
2272 IEEE80211_NODE_UNLOCK(nt);
2273 IEEE80211_NODE_ITERATE_UNLOCK(nt);
2276 * If ret is non-zero, we hit some kind of error.
2277 * Rather than walking some nodes, we'll walk none
2281 for (j = 0; j < i; j++) {
2282 /* ieee80211_free_node() locks by itself */
2283 ieee80211_free_node(ni_arr[j]);
2291 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2292 * reference in the source.
2294 * Note that this fetches 'max_aid' from the first VAP, rather than finding
2295 * the largest max_aid from all VAPs.
2298 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2299 ieee80211_iter_func *f, void *arg)
2301 struct ieee80211_node **ni_arr;
2305 struct ieee80211vap *vap;
2307 /* Overdoing it default */
2308 max_aid = IEEE80211_AID_MAX;
2310 /* Handle the case of there being no vaps just yet */
2311 vap = TAILQ_FIRST(&nt->nt_ic->ic_vaps);
2313 max_aid = vap->iv_max_aid;
2315 size = max_aid * sizeof(struct ieee80211_node *);
2316 ni_arr = (struct ieee80211_node **) malloc(size, M_80211_NODE,
2322 * If this fails, the node table won't have any
2323 * valid entries - ieee80211_iterate_nt() frees
2324 * the references to them. So don't try walking
2325 * the table; just skip to the end and free the
2328 if (ieee80211_iterate_nt(nt, ni_arr, max_aid) != 0)
2331 for (i = 0; i < max_aid; i++) {
2332 if (ni_arr[i] == NULL) /* end of the list */
2334 (*f)(arg, ni_arr[i]);
2335 /* ieee80211_free_node() locks by itself */
2336 ieee80211_free_node(ni_arr[i]);
2340 free(ni_arr, M_80211_NODE);
2344 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2346 printf("0x%p: mac %s refcnt %d\n", ni,
2347 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2348 printf("\tscangen %u authmode %u flags 0x%x\n",
2349 ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2350 printf("\tassocid 0x%x txpower %u vlan %u\n",
2351 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2352 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2353 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2354 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2355 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2356 ni->ni_rxfragstamp);
2357 printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2358 node_getrssi(ni), ni->ni_noise,
2359 ni->ni_intval, ni->ni_capinfo);
2360 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2361 ether_sprintf(ni->ni_bssid),
2362 ni->ni_esslen, ni->ni_essid,
2363 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2364 printf("\tinact %u inact_reload %u txrate %u\n",
2365 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2366 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2367 ni->ni_htcap, ni->ni_htparam,
2368 ni->ni_htctlchan, ni->ni_ht2ndchan);
2369 printf("\thtopmode %x htstbc %x chw %u\n",
2370 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2374 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2376 ieee80211_iterate_nodes(nt,
2377 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2381 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2383 struct ieee80211vap *vap;
2385 IEEE80211_LOCK_ASSERT(ic);
2387 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2388 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2389 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2393 ieee80211_notify_erp(struct ieee80211com *ic)
2396 ieee80211_notify_erp_locked(ic);
2397 IEEE80211_UNLOCK(ic);
2401 * Handle a station joining an 11g network.
2404 ieee80211_node_join_11g(struct ieee80211_node *ni)
2406 struct ieee80211com *ic = ni->ni_ic;
2408 IEEE80211_LOCK_ASSERT(ic);
2411 * Station isn't capable of short slot time. Bump
2412 * the count of long slot time stations and disable
2413 * use of short slot time. Note that the actual switch
2414 * over to long slot time use may not occur until the
2415 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2417 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2418 ic->ic_longslotsta++;
2419 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2420 "station needs long slot time, count %d",
2421 ic->ic_longslotsta);
2422 /* XXX vap's w/ conflicting needs won't work */
2423 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2425 * Don't force slot time when switched to turbo
2426 * mode as non-ERP stations won't be present; this
2427 * need only be done when on the normal G channel.
2429 ieee80211_set_shortslottime(ic, 0);
2433 * If the new station is not an ERP station
2434 * then bump the counter and enable protection
2437 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2439 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2440 "station is !ERP, %d non-ERP stations associated",
2443 * If station does not support short preamble
2444 * then we must enable use of Barker preamble.
2446 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2447 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2448 "%s", "station needs long preamble");
2449 ic->ic_flags |= IEEE80211_F_USEBARKER;
2450 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2453 * If protection is configured and this is the first
2454 * indication we should use protection, enable it.
2456 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2457 ic->ic_nonerpsta == 1 &&
2458 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2459 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2460 "%s: enable use of protection\n", __func__);
2461 ic->ic_flags |= IEEE80211_F_USEPROT;
2462 ieee80211_notify_erp_locked(ic);
2465 ni->ni_flags |= IEEE80211_NODE_ERP;
2469 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2471 struct ieee80211com *ic = ni->ni_ic;
2472 struct ieee80211vap *vap = ni->ni_vap;
2475 if (ni->ni_associd == 0) {
2478 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2480 * It would be good to search the bitmap
2481 * more efficiently, but this will do for now.
2483 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2484 if (!IEEE80211_AID_ISSET(vap, aid))
2487 if (aid >= vap->iv_max_aid) {
2488 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2489 ieee80211_node_leave(ni);
2492 ni->ni_associd = aid | 0xc000;
2493 ni->ni_jointime = time_uptime;
2495 IEEE80211_AID_SET(vap, ni->ni_associd);
2496 vap->iv_sta_assoc++;
2499 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2500 ieee80211_ht_node_join(ni);
2501 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2502 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2503 ieee80211_node_join_11g(ni);
2504 IEEE80211_UNLOCK(ic);
2510 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2511 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2512 IEEE80211_NODE_AID(ni),
2513 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2514 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2515 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2516 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2517 ni->ni_flags & IEEE80211_NODE_HT ?
2518 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2519 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2520 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2521 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2522 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2523 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2524 ", fast-frames" : "",
2525 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2529 ieee80211_node_setuptxparms(ni);
2530 ieee80211_ratectl_node_init(ni);
2531 /* give driver a chance to setup state like ni_txrate */
2532 if (ic->ic_newassoc != NULL)
2533 ic->ic_newassoc(ni, newassoc);
2534 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2535 /* tell the authenticator about new station */
2536 if (vap->iv_auth->ia_node_join != NULL)
2537 vap->iv_auth->ia_node_join(ni);
2538 ieee80211_notify_node_join(ni,
2539 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2543 disable_protection(struct ieee80211com *ic)
2545 KASSERT(ic->ic_nonerpsta == 0 &&
2546 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2547 ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2550 ic->ic_flags &= ~IEEE80211_F_USEPROT;
2551 /* XXX verify mode? */
2552 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2553 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2554 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2556 ieee80211_notify_erp_locked(ic);
2560 * Handle a station leaving an 11g network.
2563 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2565 struct ieee80211com *ic = ni->ni_ic;
2567 IEEE80211_LOCK_ASSERT(ic);
2569 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2570 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2571 ic->ic_bsschan->ic_flags));
2574 * If a long slot station do the slot time bookkeeping.
2576 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2577 KASSERT(ic->ic_longslotsta > 0,
2578 ("bogus long slot station count %d", ic->ic_longslotsta));
2579 ic->ic_longslotsta--;
2580 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2581 "long slot time station leaves, count now %d",
2582 ic->ic_longslotsta);
2583 if (ic->ic_longslotsta == 0) {
2585 * Re-enable use of short slot time if supported
2586 * and not operating in IBSS mode (per spec).
2588 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2589 ic->ic_opmode != IEEE80211_M_IBSS) {
2590 IEEE80211_DPRINTF(ni->ni_vap,
2591 IEEE80211_MSG_ASSOC,
2592 "%s: re-enable use of short slot time\n",
2594 ieee80211_set_shortslottime(ic, 1);
2599 * If a non-ERP station do the protection-related bookkeeping.
2601 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2602 KASSERT(ic->ic_nonerpsta > 0,
2603 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2605 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2606 "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2607 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2608 " (non-ERP sta present)" : "");
2609 if (ic->ic_nonerpsta == 0 &&
2610 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2611 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2612 "%s: disable use of protection\n", __func__);
2613 disable_protection(ic);
2619 * Time out presence of an overlapping bss with non-ERP
2620 * stations. When operating in hostap mode we listen for
2621 * beacons from other stations and if we identify a non-ERP
2622 * station is present we enable protection. To identify
2623 * when all non-ERP stations are gone we time out this
2627 ieee80211_erp_timeout(struct ieee80211com *ic)
2630 IEEE80211_LOCK_ASSERT(ic);
2632 if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2633 time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2635 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2636 "%s", "age out non-ERP sta present on channel");
2638 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2639 if (ic->ic_nonerpsta == 0)
2640 disable_protection(ic);
2645 * Handle bookkeeping for station deauthentication/disassociation
2646 * when operating as an ap.
2649 ieee80211_node_leave(struct ieee80211_node *ni)
2651 struct ieee80211com *ic = ni->ni_ic;
2652 struct ieee80211vap *vap = ni->ni_vap;
2653 struct ieee80211_node_table *nt = ni->ni_table;
2655 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2656 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2658 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2659 ("unexpected operating mode %u", vap->iv_opmode));
2661 * If node wasn't previously associated all
2662 * we need to do is reclaim the reference.
2664 /* XXX ibss mode bypasses 11g and notification */
2665 if (ni->ni_associd == 0)
2668 * Tell the authenticator the station is leaving.
2669 * Note that we must do this before yanking the
2670 * association id as the authenticator uses the
2671 * associd to locate it's state block.
2673 if (vap->iv_auth->ia_node_leave != NULL)
2674 vap->iv_auth->ia_node_leave(ni);
2677 IEEE80211_AID_CLR(vap, ni->ni_associd);
2679 vap->iv_sta_assoc--;
2682 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2683 ieee80211_ht_node_leave(ni);
2684 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2685 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2686 ieee80211_node_leave_11g(ni);
2687 IEEE80211_UNLOCK(ic);
2689 * Cleanup station state. In particular clear various
2690 * state that might otherwise be reused if the node
2691 * is reused before the reference count goes to zero
2692 * (and memory is reclaimed).
2694 ieee80211_sta_leave(ni);
2697 * Remove the node from any table it's recorded in and
2698 * drop the caller's reference. Removal from the table
2699 * is important to insure the node is not reprocessed
2703 IEEE80211_NODE_LOCK(nt);
2704 node_reclaim(nt, ni);
2705 IEEE80211_NODE_UNLOCK(nt);
2707 ieee80211_free_node(ni);
2711 struct ieee80211vap *vap;
2713 uint32_t rssi_total;
2717 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2719 struct rssiinfo *info = arg;
2720 struct ieee80211vap *vap = ni->ni_vap;
2723 if (info->vap != vap)
2725 /* only associated stations */
2726 if (ni->ni_associd == 0)
2728 rssi = vap->iv_ic->ic_node_getrssi(ni);
2730 info->rssi_samples++;
2731 info->rssi_total += rssi;
2736 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2738 struct rssiinfo *info = arg;
2739 struct ieee80211vap *vap = ni->ni_vap;
2742 if (info->vap != vap)
2744 /* only neighbors */
2745 /* XXX check bssid */
2746 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2748 rssi = vap->iv_ic->ic_node_getrssi(ni);
2750 info->rssi_samples++;
2751 info->rssi_total += rssi;
2755 #ifdef IEEE80211_SUPPORT_MESH
2757 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
2759 struct rssiinfo *info = arg;
2760 struct ieee80211vap *vap = ni->ni_vap;
2763 if (info->vap != vap)
2765 /* only neighbors that peered successfully */
2766 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
2768 rssi = vap->iv_ic->ic_node_getrssi(ni);
2770 info->rssi_samples++;
2771 info->rssi_total += rssi;
2774 #endif /* IEEE80211_SUPPORT_MESH */
2777 ieee80211_getrssi(struct ieee80211vap *vap)
2779 #define NZ(x) ((x) == 0 ? 1 : (x))
2780 struct ieee80211com *ic = vap->iv_ic;
2781 struct rssiinfo info;
2783 info.rssi_total = 0;
2784 info.rssi_samples = 0;
2786 switch (vap->iv_opmode) {
2787 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
2788 case IEEE80211_M_AHDEMO: /* average of all neighbors */
2789 ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2791 case IEEE80211_M_HOSTAP: /* average of all associated stations */
2792 ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2794 #ifdef IEEE80211_SUPPORT_MESH
2795 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
2796 ieee80211_iterate_nodes(&ic->ic_sta, get_mesh_rssi, &info);
2799 case IEEE80211_M_MONITOR: /* XXX */
2800 case IEEE80211_M_STA: /* use stats from associated ap */
2802 if (vap->iv_bss != NULL)
2803 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2804 info.rssi_samples = 1;
2807 return info.rssi_total / NZ(info.rssi_samples);
2812 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2815 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
2817 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2818 /* for non-station mode return avg'd rssi accounting */
2819 if (vap->iv_opmode != IEEE80211_M_STA)
2820 *rssi = ieee80211_getrssi(vap);