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 node_reclaim(struct ieee80211_node_table *nt,
97 struct ieee80211_node *ni);
98 static void ieee80211_node_table_init(struct ieee80211com *ic,
99 struct ieee80211_node_table *nt, const char *name,
100 int inact, int keymaxix);
101 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
102 struct ieee80211vap *);
103 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
104 static void ieee80211_erp_timeout(struct ieee80211com *);
106 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
107 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
110 ieee80211_node_attach(struct ieee80211com *ic)
112 /* XXX really want maxlen enforced per-sta */
113 ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
115 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
116 IEEE80211_INACT_INIT, ic->ic_max_keyix);
117 callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
118 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
119 ieee80211_node_timeout, ic);
121 ic->ic_node_alloc = node_alloc;
122 ic->ic_node_free = node_free;
123 ic->ic_node_cleanup = node_cleanup;
124 ic->ic_node_age = node_age;
125 ic->ic_node_drain = node_age; /* NB: same as age */
126 ic->ic_node_getrssi = node_getrssi;
127 ic->ic_node_getsignal = node_getsignal;
128 ic->ic_node_getmimoinfo = node_getmimoinfo;
131 * Set flags to be propagated to all vap's;
132 * these define default behaviour/configuration.
134 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
138 ieee80211_node_detach(struct ieee80211com *ic)
141 callout_drain(&ic->ic_inact);
142 ieee80211_node_table_cleanup(&ic->ic_sta);
143 ieee80211_ageq_cleanup(&ic->ic_stageq);
147 ieee80211_node_vattach(struct ieee80211vap *vap)
149 /* NB: driver can override */
150 vap->iv_max_aid = IEEE80211_AID_DEF;
152 /* default station inactivity timer setings */
153 vap->iv_inact_init = IEEE80211_INACT_INIT;
154 vap->iv_inact_auth = IEEE80211_INACT_AUTH;
155 vap->iv_inact_run = IEEE80211_INACT_RUN;
156 vap->iv_inact_probe = IEEE80211_INACT_PROBE;
158 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
159 "%s: init %u auth %u run %u probe %u\n", __func__,
160 vap->iv_inact_init, vap->iv_inact_auth,
161 vap->iv_inact_run, vap->iv_inact_probe);
165 ieee80211_node_latevattach(struct ieee80211vap *vap)
167 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
168 /* XXX should we allow max aid to be zero? */
169 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
170 vap->iv_max_aid = IEEE80211_AID_MIN;
171 if_printf(vap->iv_ifp,
172 "WARNING: max aid too small, changed to %d\n",
175 vap->iv_aid_bitmap = (uint32_t *) malloc(
176 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
177 M_80211_NODE, M_NOWAIT | M_ZERO);
178 if (vap->iv_aid_bitmap == NULL) {
179 /* XXX no way to recover */
180 printf("%s: no memory for AID bitmap, max aid %d!\n",
181 __func__, vap->iv_max_aid);
186 ieee80211_reset_bss(vap);
188 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
192 ieee80211_node_vdetach(struct ieee80211vap *vap)
194 struct ieee80211com *ic = vap->iv_ic;
196 ieee80211_node_table_reset(&ic->ic_sta, vap);
197 if (vap->iv_bss != NULL) {
198 ieee80211_free_node(vap->iv_bss);
201 if (vap->iv_aid_bitmap != NULL) {
202 free(vap->iv_aid_bitmap, M_80211_NODE);
203 vap->iv_aid_bitmap = NULL;
208 * Port authorize/unauthorize interfaces for use by an authenticator.
212 ieee80211_node_authorize(struct ieee80211_node *ni)
214 struct ieee80211vap *vap = ni->ni_vap;
216 ni->ni_flags |= IEEE80211_NODE_AUTH;
217 ni->ni_inact_reload = vap->iv_inact_run;
218 ni->ni_inact = ni->ni_inact_reload;
220 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
221 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
225 ieee80211_node_unauthorize(struct ieee80211_node *ni)
227 struct ieee80211vap *vap = ni->ni_vap;
229 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
230 ni->ni_inact_reload = vap->iv_inact_auth;
231 if (ni->ni_inact > ni->ni_inact_reload)
232 ni->ni_inact = ni->ni_inact_reload;
234 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
235 "%s: inact_reload %u inact %u", __func__,
236 ni->ni_inact_reload, ni->ni_inact);
240 * Fix tx parameters for a node according to ``association state''.
243 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
245 struct ieee80211vap *vap = ni->ni_vap;
246 enum ieee80211_phymode mode;
248 if (ni->ni_flags & IEEE80211_NODE_HT) {
249 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
250 mode = IEEE80211_MODE_11NA;
252 mode = IEEE80211_MODE_11NG;
253 } else { /* legacy rate handling */
254 if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
255 mode = IEEE80211_MODE_STURBO_A;
256 else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
257 mode = IEEE80211_MODE_HALF;
258 else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
259 mode = IEEE80211_MODE_QUARTER;
260 /* NB: 108A should be handled as 11a */
261 else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
262 mode = IEEE80211_MODE_11A;
263 else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
264 (ni->ni_flags & IEEE80211_NODE_ERP))
265 mode = IEEE80211_MODE_11G;
267 mode = IEEE80211_MODE_11B;
269 ni->ni_txparms = &vap->iv_txparms[mode];
273 * Set/change the channel. The rate set is also updated as
274 * to insure a consistent view by drivers.
275 * XXX should be private but hostap needs it to deal with CSA
278 ieee80211_node_set_chan(struct ieee80211_node *ni,
279 struct ieee80211_channel *chan)
281 struct ieee80211com *ic = ni->ni_ic;
282 struct ieee80211vap *vap = ni->ni_vap;
283 enum ieee80211_phymode mode;
285 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
288 mode = ieee80211_chan2mode(chan);
289 if (IEEE80211_IS_CHAN_HT(chan)) {
291 * We must install the legacy rate est in ni_rates and the
292 * HT rate set in ni_htrates.
294 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
296 * Setup bss tx parameters based on operating mode. We
297 * use legacy rates when operating in a mixed HT+non-HT bss
298 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
300 if (mode == IEEE80211_MODE_11NA &&
301 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
302 mode = IEEE80211_MODE_11A;
303 else if (mode == IEEE80211_MODE_11NG &&
304 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
305 mode = IEEE80211_MODE_11G;
306 if (mode == IEEE80211_MODE_11G &&
307 (vap->iv_flags & IEEE80211_F_PUREG) == 0)
308 mode = IEEE80211_MODE_11B;
310 ni->ni_txparms = &vap->iv_txparms[mode];
311 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
315 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
317 /* propagate useful state */
318 nbss->ni_authmode = obss->ni_authmode;
319 nbss->ni_txpower = obss->ni_txpower;
320 nbss->ni_vlan = obss->ni_vlan;
321 /* XXX statistics? */
322 /* XXX legacy WDS bssid? */
326 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
328 struct ieee80211com *ic = vap->iv_ic;
329 struct ieee80211_node *ni;
331 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
332 "%s: creating %s on channel %u\n", __func__,
333 ieee80211_opmode_name[vap->iv_opmode],
334 ieee80211_chan2ieee(ic, chan));
336 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
341 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
342 ni->ni_esslen = vap->iv_des_ssid[0].len;
343 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
344 if (vap->iv_bss != NULL)
345 copy_bss(ni, vap->iv_bss);
346 ni->ni_intval = ic->ic_bintval;
347 if (vap->iv_flags & IEEE80211_F_PRIVACY)
348 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
349 if (ic->ic_phytype == IEEE80211_T_FH) {
350 ni->ni_fhdwell = 200; /* XXX */
353 if (vap->iv_opmode == IEEE80211_M_IBSS) {
354 vap->iv_flags |= IEEE80211_F_SIBSS;
355 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
356 if (vap->iv_flags & IEEE80211_F_DESBSSID)
357 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
359 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
360 /* clear group bit, add local bit */
361 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
363 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
364 if (vap->iv_flags & IEEE80211_F_DESBSSID)
365 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
367 #ifdef IEEE80211_SUPPORT_TDMA
368 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
370 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
371 #ifdef IEEE80211_SUPPORT_MESH
372 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
373 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
374 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
378 * Fix the channel and related attributes.
380 /* clear DFS CAC state on previous channel */
381 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
382 ic->ic_bsschan->ic_freq != chan->ic_freq &&
383 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
384 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
385 ic->ic_bsschan = chan;
386 ieee80211_node_set_chan(ni, chan);
387 ic->ic_curmode = ieee80211_chan2mode(chan);
389 * Do mode-specific setup.
391 if (IEEE80211_IS_CHAN_FULL(chan)) {
392 if (IEEE80211_IS_CHAN_ANYG(chan)) {
394 * Use a mixed 11b/11g basic rate set.
396 ieee80211_setbasicrates(&ni->ni_rates,
398 if (vap->iv_flags & IEEE80211_F_PUREG) {
400 * Also mark OFDM rates basic so 11b
401 * stations do not join (WiFi compliance).
403 ieee80211_addbasicrates(&ni->ni_rates,
406 } else if (IEEE80211_IS_CHAN_B(chan)) {
408 * Force pure 11b rate set.
410 ieee80211_setbasicrates(&ni->ni_rates,
415 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
419 * Reset bss state on transition to the INIT state.
420 * Clear any stations from the table (they have been
421 * deauth'd) and reset the bss node (clears key, rate
425 ieee80211_reset_bss(struct ieee80211vap *vap)
427 struct ieee80211com *ic = vap->iv_ic;
428 struct ieee80211_node *ni, *obss;
430 ieee80211_node_table_reset(&ic->ic_sta, vap);
431 /* XXX multi-bss: wrong */
432 ieee80211_reset_erp(ic);
434 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
435 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
437 vap->iv_bss = ieee80211_ref_node(ni);
440 ni->ni_intval = ic->ic_bintval;
441 ieee80211_free_node(obss);
443 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
447 match_ssid(const struct ieee80211_node *ni,
448 int nssid, const struct ieee80211_scan_ssid ssids[])
452 for (i = 0; i < nssid; i++) {
453 if (ni->ni_esslen == ssids[i].len &&
454 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
461 * Test a node for suitability/compatibility.
464 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
466 struct ieee80211com *ic = ni->ni_ic;
469 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
471 if (vap->iv_opmode == IEEE80211_M_IBSS) {
472 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
475 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
478 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
479 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
482 /* XXX does this mean privacy is supported or required? */
483 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
486 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
487 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
488 if (rate & IEEE80211_RATE_BASIC)
490 if (vap->iv_des_nssid != 0 &&
491 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
493 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
494 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
499 #ifdef IEEE80211_DEBUG
501 * Display node suitability/compatibility.
504 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
506 struct ieee80211com *ic = ni->ni_ic;
511 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
513 if (vap->iv_opmode == IEEE80211_M_IBSS) {
514 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
517 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
520 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
521 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
524 /* XXX does this mean privacy is supported or required? */
525 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
528 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
529 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
530 if (rate & IEEE80211_RATE_BASIC)
532 if (vap->iv_des_nssid != 0 &&
533 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
535 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
536 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
539 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
540 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
542 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
543 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
544 fail & 0x08 ? '!' : ' ');
546 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
547 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
549 fail & 0x02 ? '!' : ' ');
551 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
552 fail & 0x04 ? '!' : ' ');
553 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
554 printf("%s\n", fail & 0x10 ? "!" : "");
556 #endif /* IEEE80211_DEBUG */
559 * Handle 802.11 ad hoc network merge. The
560 * convention, set by the Wireless Ethernet Compatibility Alliance
561 * (WECA), is that an 802.11 station will change its BSSID to match
562 * the "oldest" 802.11 ad hoc network, on the same channel, that
563 * has the station's desired SSID. The "oldest" 802.11 network
564 * sends beacons with the greatest TSF timestamp.
566 * The caller is assumed to validate TSF's before attempting a merge.
568 * Return !0 if the BSSID changed, 0 otherwise.
571 ieee80211_ibss_merge(struct ieee80211_node *ni)
573 struct ieee80211vap *vap = ni->ni_vap;
574 #ifdef IEEE80211_DEBUG
575 struct ieee80211com *ic = ni->ni_ic;
578 if (ni == vap->iv_bss ||
579 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
580 /* unchanged, nothing to do */
583 if (!check_bss(vap, ni)) {
584 /* capabilities mismatch */
585 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
586 "%s: merge failed, capabilities mismatch\n", __func__);
587 #ifdef IEEE80211_DEBUG
588 if (ieee80211_msg_assoc(vap))
589 check_bss_debug(vap, ni);
591 vap->iv_stats.is_ibss_capmismatch++;
594 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
595 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
596 ether_sprintf(ni->ni_bssid),
597 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
598 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
599 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
601 return ieee80211_sta_join1(ieee80211_ref_node(ni));
605 * Calculate HT channel promotion flags for all vaps.
606 * This assumes ni_chan have been setup for each vap.
609 gethtadjustflags(struct ieee80211com *ic)
611 struct ieee80211vap *vap;
616 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
617 if (vap->iv_state < IEEE80211_S_RUN)
619 switch (vap->iv_opmode) {
620 case IEEE80211_M_WDS:
621 case IEEE80211_M_STA:
622 case IEEE80211_M_AHDEMO:
623 case IEEE80211_M_HOSTAP:
624 case IEEE80211_M_IBSS:
625 case IEEE80211_M_MBSS:
626 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
636 * Check if the current channel needs to change based on whether
637 * any vap's are using HT20/HT40. This is used to sync the state
638 * of ic_curchan after a channel width change on a running vap.
641 ieee80211_sync_curchan(struct ieee80211com *ic)
643 struct ieee80211_channel *c;
645 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
646 if (c != ic->ic_curchan) {
648 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
649 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
650 IEEE80211_UNLOCK(ic);
651 ic->ic_set_channel(ic);
652 ieee80211_radiotap_chan_change(ic);
658 * Setup the current channel. The request channel may be
659 * promoted if other vap's are operating with HT20/HT40.
662 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
664 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
665 int flags = gethtadjustflags(ic);
667 * Check for channel promotion required to support the
668 * set of running vap's. This assumes we are called
669 * after ni_chan is setup for each vap.
671 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
672 if (flags > ieee80211_htchanflags(c))
673 c = ieee80211_ht_adjust_channel(ic, c, flags);
675 ic->ic_bsschan = ic->ic_curchan = c;
676 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
677 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
681 * Change the current channel. The channel change is guaranteed to have
682 * happened before the next state change.
685 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
687 ieee80211_setupcurchan(ic, c);
688 ieee80211_runtask(ic, &ic->ic_chan_task);
692 ieee80211_update_chw(struct ieee80211com *ic)
695 ieee80211_setupcurchan(ic, ic->ic_curchan);
696 ieee80211_runtask(ic, &ic->ic_chw_task);
700 * Join the specified IBSS/BSS network. The node is assumed to
701 * be passed in with a held reference.
704 ieee80211_sta_join1(struct ieee80211_node *selbs)
706 struct ieee80211vap *vap = selbs->ni_vap;
707 struct ieee80211com *ic = selbs->ni_ic;
708 struct ieee80211_node *obss;
712 * Committed to selbs, setup state.
716 * Check if old+new node have the same address in which
717 * case we can reassociate when operating in sta mode.
719 canreassoc = (obss != NULL &&
720 vap->iv_state == IEEE80211_S_RUN &&
721 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
722 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */
724 struct ieee80211_node_table *nt = obss->ni_table;
726 copy_bss(selbs, obss);
727 ieee80211_node_decref(obss); /* iv_bss reference */
729 IEEE80211_NODE_LOCK(nt);
730 node_reclaim(nt, obss); /* station table reference */
731 IEEE80211_NODE_UNLOCK(nt);
733 obss = NULL; /* NB: guard against later use */
737 * Delete unusable rates; we've already checked
738 * that the negotiated rate set is acceptable.
740 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
741 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
743 ieee80211_setcurchan(ic, selbs->ni_chan);
745 * Set the erp state (mostly the slot time) to deal with
746 * the auto-select case; this should be redundant if the
749 ieee80211_reset_erp(ic);
750 ieee80211_wme_initparams(vap);
752 if (vap->iv_opmode == IEEE80211_M_STA) {
755 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
758 * Act as if we received a DEAUTH frame in case we
759 * are invoked from the RUN state. This will cause
760 * us to try to re-authenticate if we are operating
763 ieee80211_new_state(vap, IEEE80211_S_AUTH,
764 IEEE80211_FC0_SUBTYPE_DEAUTH);
767 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
772 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
773 const struct ieee80211_scan_entry *se)
775 struct ieee80211com *ic = vap->iv_ic;
776 struct ieee80211_node *ni;
778 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
785 * Expand scan state into node's format.
786 * XXX may not need all this stuff
788 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
789 ni->ni_esslen = se->se_ssid[1];
790 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
791 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
792 ni->ni_intval = se->se_intval;
793 ni->ni_capinfo = se->se_capinfo;
795 ni->ni_timoff = se->se_timoff;
796 ni->ni_fhdwell = se->se_fhdwell;
797 ni->ni_fhindex = se->se_fhindex;
798 ni->ni_erp = se->se_erp;
799 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
800 ni->ni_noise = se->se_noise;
801 if (vap->iv_opmode == IEEE80211_M_STA) {
802 /* NB: only infrastructure mode requires an associd */
803 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
806 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
807 ieee80211_ies_expand(&ni->ni_ies);
808 #ifdef IEEE80211_SUPPORT_SUPERG
809 if (ni->ni_ies.ath_ie != NULL)
810 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
812 if (ni->ni_ies.htcap_ie != NULL)
813 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
814 if (ni->ni_ies.htinfo_ie != NULL)
815 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
816 #ifdef IEEE80211_SUPPORT_MESH
817 if (ni->ni_ies.meshid_ie != NULL)
818 ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
820 #ifdef IEEE80211_SUPPORT_TDMA
821 if (ni->ni_ies.tdma_ie != NULL)
822 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
826 vap->iv_dtim_period = se->se_dtimperiod;
827 vap->iv_dtim_count = 0;
829 /* NB: must be after ni_chan is setup */
830 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
832 if (ieee80211_iserp_rateset(&ni->ni_rates))
833 ni->ni_flags |= IEEE80211_NODE_ERP;
836 * Setup HT state for this node if it's available, otherwise
837 * non-STA modes won't pick this state up.
839 * For IBSS and related modes that don't go through an
840 * association request/response, the only appropriate place
841 * to setup the HT state is here.
843 if (ni->ni_ies.htinfo_ie != NULL &&
844 ni->ni_ies.htcap_ie != NULL &&
845 vap->iv_flags_ht & IEEE80211_FHT_HT) {
846 ieee80211_ht_node_init(ni);
847 ieee80211_ht_updateparams(ni,
849 ni->ni_ies.htinfo_ie);
850 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
851 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
852 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
854 /* XXX else check for ath FF? */
855 /* XXX QoS? Difficult given that WME config is specific to a master */
857 ieee80211_node_setuptxparms(ni);
858 ieee80211_ratectl_node_init(ni);
860 return ieee80211_sta_join1(ieee80211_ref_node(ni));
864 * Leave the specified IBSS/BSS network. The node is assumed to
865 * be passed in with a held reference.
868 ieee80211_sta_leave(struct ieee80211_node *ni)
870 struct ieee80211com *ic = ni->ni_ic;
872 ic->ic_node_cleanup(ni);
873 ieee80211_notify_node_leave(ni);
877 * Send a deauthenticate frame and drop the station.
880 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
882 /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
883 ieee80211_ref_node(ni);
884 if (ni->ni_associd != 0)
885 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
886 ieee80211_node_leave(ni);
887 ieee80211_free_node(ni);
890 static struct ieee80211_node *
891 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
893 struct ieee80211_node *ni;
895 ni = (struct ieee80211_node *) malloc(sizeof(struct ieee80211_node),
896 M_80211_NODE, M_NOWAIT | M_ZERO);
901 * Initialize an ie blob with the specified data. If previous
902 * data exists re-use the data block. As a side effect we clear
903 * all references to specific ie's; the caller is required to
907 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
909 /* NB: assumes data+len are the last fields */
910 memset(ies, 0, offsetof(struct ieee80211_ies, data));
911 if (ies->data != NULL && ies->len != len) {
912 /* data size changed */
913 free(ies->data, M_80211_NODE_IE);
916 if (ies->data == NULL) {
917 ies->data = (uint8_t *) malloc(len, M_80211_NODE_IE, M_NOWAIT);
918 if (ies->data == NULL) {
920 /* NB: pointers have already been zero'd above */
924 memcpy(ies->data, data, len);
930 * Reclaim storage for an ie blob.
933 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
935 if (ies->data != NULL)
936 free(ies->data, M_80211_NODE_IE);
940 * Expand an ie blob data contents and to fillin individual
941 * ie pointers. The data blob is assumed to be well-formed;
942 * we don't do any validity checking of ie lengths.
945 ieee80211_ies_expand(struct ieee80211_ies *ies)
954 case IEEE80211_ELEMID_VENDOR:
957 else if (iswmeoui(ie))
959 #ifdef IEEE80211_SUPPORT_SUPERG
960 else if (isatherosoui(ie))
963 #ifdef IEEE80211_SUPPORT_TDMA
964 else if (istdmaoui(ie))
968 case IEEE80211_ELEMID_RSN:
971 case IEEE80211_ELEMID_HTCAP:
974 case IEEE80211_ELEMID_HTINFO:
977 #ifdef IEEE80211_SUPPORT_MESH
978 case IEEE80211_ELEMID_MESHID:
989 * Reclaim any resources in a node and reset any critical
990 * state. Typically nodes are free'd immediately after,
991 * but in some cases the storage may be reused so we need
992 * to insure consistent state (should probably fix that).
995 node_cleanup(struct ieee80211_node *ni)
997 struct ieee80211vap *vap = ni->ni_vap;
998 struct ieee80211com *ic = ni->ni_ic;
1001 /* NB: preserve ni_table */
1002 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1003 if (vap->iv_opmode != IEEE80211_M_STA)
1005 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
1006 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
1007 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
1010 * Cleanup any HT-related state.
1012 if (ni->ni_flags & IEEE80211_NODE_HT)
1013 ieee80211_ht_node_cleanup(ni);
1014 #ifdef IEEE80211_SUPPORT_SUPERG
1015 else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
1016 ieee80211_ff_node_cleanup(ni);
1018 #ifdef IEEE80211_SUPPORT_MESH
1020 * Cleanup any mesh-related state.
1022 if (vap->iv_opmode == IEEE80211_M_MBSS)
1023 ieee80211_mesh_node_cleanup(ni);
1026 * Clear any staging queue entries.
1028 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1031 * Clear AREF flag that marks the authorization refcnt bump
1032 * has happened. This is probably not needed as the node
1033 * should always be removed from the table so not found but
1034 * do it just in case.
1035 * Likewise clear the ASSOCID flag as these flags are intended
1036 * to be managed in tandem.
1038 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1041 * Drain power save queue and, if needed, clear TIM.
1043 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1044 vap->iv_set_tim(ni, 0);
1047 if (ni->ni_challenge != NULL) {
1048 free(ni->ni_challenge, M_80211_NODE);
1049 ni->ni_challenge = NULL;
1052 * Preserve SSID, WPA, and WME ie's so the bss node is
1053 * reusable during a re-auth/re-assoc state transition.
1054 * If we remove these data they will not be recreated
1055 * because they come from a probe-response or beacon frame
1056 * which cannot be expected prior to the association-response.
1057 * This should not be an issue when operating in other modes
1058 * as stations leaving always go through a full state transition
1059 * which will rebuild this state.
1061 * XXX does this leave us open to inheriting old state?
1063 for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1064 if (ni->ni_rxfrag[i] != NULL) {
1065 m_freem(ni->ni_rxfrag[i]);
1066 ni->ni_rxfrag[i] = NULL;
1069 * Must be careful here to remove any key map entry w/o a LOR.
1071 ieee80211_node_delucastkey(ni);
1075 node_free(struct ieee80211_node *ni)
1077 struct ieee80211com *ic = ni->ni_ic;
1079 ieee80211_ratectl_node_deinit(ni);
1080 ic->ic_node_cleanup(ni);
1081 ieee80211_ies_cleanup(&ni->ni_ies);
1082 ieee80211_psq_cleanup(&ni->ni_psq);
1083 free(ni, M_80211_NODE);
1087 node_age(struct ieee80211_node *ni)
1089 struct ieee80211vap *vap = ni->ni_vap;
1091 IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
1094 * Age frames on the power save queue.
1096 if (ieee80211_node_psq_age(ni) != 0 &&
1097 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1098 vap->iv_set_tim(ni, 0);
1100 * Age out HT resources (e.g. frames on the
1101 * A-MPDU reorder queues).
1103 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1104 ieee80211_ht_node_age(ni);
1108 node_getrssi(const struct ieee80211_node *ni)
1110 uint32_t avgrssi = ni->ni_avgrssi;
1113 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1115 rssi = IEEE80211_RSSI_GET(avgrssi);
1116 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1120 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1122 *rssi = node_getrssi(ni);
1123 *noise = ni->ni_noise;
1127 node_getmimoinfo(const struct ieee80211_node *ni,
1128 struct ieee80211_mimo_info *info)
1134 bzero(info, sizeof(*info));
1136 for (i = 0; i < ni->ni_mimo_chains; i++) {
1137 avgrssi = ni->ni_mimo_rssi_ctl[i];
1138 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1141 rssi = IEEE80211_RSSI_GET(avgrssi);
1142 info->rssi[i] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1144 info->noise[i] = ni->ni_mimo_noise_ctl[i];
1147 /* XXX ext radios? */
1152 struct ieee80211_node *
1153 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1154 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1156 struct ieee80211com *ic = nt->nt_ic;
1157 struct ieee80211_node *ni;
1160 ni = ic->ic_node_alloc(vap, macaddr);
1162 vap->iv_stats.is_rx_nodealloc++;
1166 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1167 "%s %p<%s> in %s table\n", __func__, ni,
1168 ether_sprintf(macaddr), nt->nt_name);
1170 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1171 hash = IEEE80211_NODE_HASH(ic, macaddr);
1172 ieee80211_node_initref(ni); /* mark referenced */
1173 ni->ni_chan = IEEE80211_CHAN_ANYC;
1174 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1175 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1176 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1177 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1178 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1179 ni->ni_inact_reload = nt->nt_inact_init;
1180 ni->ni_inact = ni->ni_inact_reload;
1181 ni->ni_ath_defkeyix = 0x7fff;
1182 ieee80211_psq_init(&ni->ni_psq, "unknown");
1183 #ifdef IEEE80211_SUPPORT_MESH
1184 if (vap->iv_opmode == IEEE80211_M_MBSS)
1185 ieee80211_mesh_node_init(vap, ni);
1187 IEEE80211_NODE_LOCK(nt);
1188 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1189 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1193 IEEE80211_NODE_UNLOCK(nt);
1195 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1196 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1198 ieee80211_ratectl_node_init(ni);
1204 * Craft a temporary node suitable for sending a management frame
1205 * to the specified station. We craft only as much state as we
1206 * need to do the work since the node will be immediately reclaimed
1207 * once the send completes.
1209 struct ieee80211_node *
1210 ieee80211_tmp_node(struct ieee80211vap *vap,
1211 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1213 struct ieee80211com *ic = vap->iv_ic;
1214 struct ieee80211_node *ni;
1216 ni = ic->ic_node_alloc(vap, macaddr);
1218 struct ieee80211_node *bss = vap->iv_bss;
1220 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1221 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1223 ni->ni_table = NULL; /* NB: pedantic */
1224 ni->ni_ic = ic; /* NB: needed to set channel */
1227 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1228 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1229 ieee80211_node_initref(ni); /* mark referenced */
1230 /* NB: required by ieee80211_fix_rate */
1231 ieee80211_node_set_chan(ni, bss->ni_chan);
1232 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1233 IEEE80211_KEYIX_NONE);
1234 ni->ni_txpower = bss->ni_txpower;
1235 /* XXX optimize away */
1236 ieee80211_psq_init(&ni->ni_psq, "unknown");
1238 ieee80211_ratectl_node_init(ni);
1241 vap->iv_stats.is_rx_nodealloc++;
1246 struct ieee80211_node *
1247 ieee80211_dup_bss(struct ieee80211vap *vap,
1248 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1250 struct ieee80211com *ic = vap->iv_ic;
1251 struct ieee80211_node *ni;
1253 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1255 struct ieee80211_node *bss = vap->iv_bss;
1257 * Inherit from iv_bss.
1260 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1261 ieee80211_node_set_chan(ni, bss->ni_chan);
1267 * Create a bss node for a legacy WDS vap. The far end does
1268 * not associate so we just create create a new node and
1269 * simulate an association. The caller is responsible for
1270 * installing the node as the bss node and handling any further
1271 * setup work like authorizing the port.
1273 struct ieee80211_node *
1274 ieee80211_node_create_wds(struct ieee80211vap *vap,
1275 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1277 struct ieee80211com *ic = vap->iv_ic;
1278 struct ieee80211_node *ni;
1280 /* XXX check if node already in sta table? */
1281 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1283 ni->ni_wdsvap = vap;
1284 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1286 * Inherit any manually configured settings.
1288 copy_bss(ni, vap->iv_bss);
1289 ieee80211_node_set_chan(ni, chan);
1290 /* NB: propagate ssid so available to WPA supplicant */
1291 ni->ni_esslen = vap->iv_des_ssid[0].len;
1292 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1293 /* NB: no associd for peer */
1295 * There are no management frames to use to
1296 * discover neighbor capabilities, so blindly
1297 * propagate the local configuration.
1299 if (vap->iv_flags & IEEE80211_F_WME)
1300 ni->ni_flags |= IEEE80211_NODE_QOS;
1301 #ifdef IEEE80211_SUPPORT_SUPERG
1302 if (vap->iv_flags & IEEE80211_F_FF)
1303 ni->ni_flags |= IEEE80211_NODE_FF;
1305 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1306 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1308 * Device is HT-capable and HT is enabled for
1309 * the vap; setup HT operation. On return
1310 * ni_chan will be adjusted to an HT channel.
1312 ieee80211_ht_wds_init(ni);
1314 struct ieee80211_channel *c = ni->ni_chan;
1316 * Force a legacy channel to be used.
1318 c = ieee80211_find_channel(ic,
1319 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1320 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1321 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1328 struct ieee80211_node *
1329 #ifdef IEEE80211_DEBUG_REFCNT
1330 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1331 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1333 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1334 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1337 struct ieee80211_node *ni;
1340 IEEE80211_NODE_LOCK_ASSERT(nt);
1342 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1343 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1344 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1345 ieee80211_ref_node(ni); /* mark referenced */
1346 #ifdef IEEE80211_DEBUG_REFCNT
1347 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1348 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1350 ni, ether_sprintf(ni->ni_macaddr),
1351 ieee80211_node_refcnt(ni));
1359 struct ieee80211_node *
1360 #ifdef IEEE80211_DEBUG_REFCNT
1361 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1362 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1364 ieee80211_find_node(struct ieee80211_node_table *nt,
1365 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1368 struct ieee80211_node *ni;
1370 IEEE80211_NODE_LOCK(nt);
1371 ni = ieee80211_find_node_locked(nt, macaddr);
1372 IEEE80211_NODE_UNLOCK(nt);
1376 struct ieee80211_node *
1377 #ifdef IEEE80211_DEBUG_REFCNT
1378 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1379 const struct ieee80211vap *vap,
1380 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1382 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1383 const struct ieee80211vap *vap,
1384 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1387 struct ieee80211_node *ni;
1390 IEEE80211_NODE_LOCK_ASSERT(nt);
1392 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1393 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1394 if (ni->ni_vap == vap &&
1395 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1396 ieee80211_ref_node(ni); /* mark referenced */
1397 #ifdef IEEE80211_DEBUG_REFCNT
1398 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1399 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1401 ni, ether_sprintf(ni->ni_macaddr),
1402 ieee80211_node_refcnt(ni));
1410 struct ieee80211_node *
1411 #ifdef IEEE80211_DEBUG_REFCNT
1412 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1413 const struct ieee80211vap *vap,
1414 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1416 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1417 const struct ieee80211vap *vap,
1418 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1421 struct ieee80211_node *ni;
1423 IEEE80211_NODE_LOCK(nt);
1424 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1425 IEEE80211_NODE_UNLOCK(nt);
1430 * Fake up a node; this handles node discovery in adhoc mode.
1431 * Note that for the driver's benefit we we treat this like
1432 * an association so the driver has an opportunity to setup
1433 * it's private state.
1435 struct ieee80211_node *
1436 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1437 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1439 struct ieee80211_node *ni;
1441 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1442 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1443 ni = ieee80211_dup_bss(vap, macaddr);
1445 struct ieee80211com *ic = vap->iv_ic;
1447 /* XXX no rate negotiation; just dup */
1448 ni->ni_rates = vap->iv_bss->ni_rates;
1449 if (ieee80211_iserp_rateset(&ni->ni_rates))
1450 ni->ni_flags |= IEEE80211_NODE_ERP;
1451 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1453 * In adhoc demo mode there are no management
1454 * frames to use to discover neighbor capabilities,
1455 * so blindly propagate the local configuration
1456 * so we can do interesting things (e.g. use
1457 * WME to disable ACK's).
1459 if (vap->iv_flags & IEEE80211_F_WME)
1460 ni->ni_flags |= IEEE80211_NODE_QOS;
1461 #ifdef IEEE80211_SUPPORT_SUPERG
1462 if (vap->iv_flags & IEEE80211_F_FF)
1463 ni->ni_flags |= IEEE80211_NODE_FF;
1466 ieee80211_node_setuptxparms(ni);
1467 ieee80211_ratectl_node_init(ni);
1468 if (ic->ic_newassoc != NULL)
1469 ic->ic_newassoc(ni, 1);
1470 /* XXX not right for 802.1x/WPA */
1471 ieee80211_node_authorize(ni);
1477 ieee80211_init_neighbor(struct ieee80211_node *ni,
1478 const struct ieee80211_frame *wh,
1479 const struct ieee80211_scanparams *sp)
1481 int do_ht_setup = 0;
1483 ni->ni_esslen = sp->ssid[1];
1484 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1485 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1486 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1487 ni->ni_intval = sp->bintval;
1488 ni->ni_capinfo = sp->capinfo;
1489 ni->ni_chan = ni->ni_ic->ic_curchan;
1490 ni->ni_fhdwell = sp->fhdwell;
1491 ni->ni_fhindex = sp->fhindex;
1492 ni->ni_erp = sp->erp;
1493 ni->ni_timoff = sp->timoff;
1494 #ifdef IEEE80211_SUPPORT_MESH
1495 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1496 ieee80211_mesh_init_neighbor(ni, wh, sp);
1498 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1499 ieee80211_ies_expand(&ni->ni_ies);
1500 if (ni->ni_ies.wme_ie != NULL)
1501 ni->ni_flags |= IEEE80211_NODE_QOS;
1503 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1504 #ifdef IEEE80211_SUPPORT_SUPERG
1505 if (ni->ni_ies.ath_ie != NULL)
1506 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1508 if (ni->ni_ies.htcap_ie != NULL)
1509 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1510 if (ni->ni_ies.htinfo_ie != NULL)
1511 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1513 if ((ni->ni_ies.htcap_ie != NULL) &&
1514 (ni->ni_ies.htinfo_ie != NULL) &&
1515 (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1520 /* NB: must be after ni_chan is setup */
1521 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1522 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1523 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1526 * If the neighbor is HT compatible, flip that on.
1529 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1530 "%s: doing HT setup\n", __func__);
1531 ieee80211_ht_node_init(ni);
1532 ieee80211_ht_updateparams(ni,
1533 ni->ni_ies.htcap_ie,
1534 ni->ni_ies.htinfo_ie);
1535 ieee80211_setup_htrates(ni,
1536 ni->ni_ies.htcap_ie,
1537 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1538 ieee80211_setup_basic_htrates(ni,
1539 ni->ni_ies.htinfo_ie);
1540 ieee80211_node_setuptxparms(ni);
1541 ieee80211_ratectl_node_init(ni);
1546 * Do node discovery in adhoc mode on receipt of a beacon
1547 * or probe response frame. Note that for the driver's
1548 * benefit we we treat this like an association so the
1549 * driver has an opportunity to setup it's private state.
1551 struct ieee80211_node *
1552 ieee80211_add_neighbor(struct ieee80211vap *vap,
1553 const struct ieee80211_frame *wh,
1554 const struct ieee80211_scanparams *sp)
1556 struct ieee80211_node *ni;
1558 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1559 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1560 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1562 struct ieee80211com *ic = vap->iv_ic;
1564 ieee80211_init_neighbor(ni, wh, sp);
1565 if (ieee80211_iserp_rateset(&ni->ni_rates))
1566 ni->ni_flags |= IEEE80211_NODE_ERP;
1567 ieee80211_node_setuptxparms(ni);
1568 ieee80211_ratectl_node_init(ni);
1569 if (ic->ic_newassoc != NULL)
1570 ic->ic_newassoc(ni, 1);
1571 /* XXX not right for 802.1x/WPA */
1572 ieee80211_node_authorize(ni);
1577 #define IS_PROBEREQ(wh) \
1578 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1579 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1580 #define IS_BCAST_PROBEREQ(wh) \
1581 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1582 ((const struct ieee80211_frame *)(wh))->i_addr3))
1584 static __inline struct ieee80211_node *
1585 _find_rxnode(struct ieee80211_node_table *nt,
1586 const struct ieee80211_frame_min *wh)
1588 if (IS_BCAST_PROBEREQ(wh))
1589 return NULL; /* spam bcast probe req to all vap's */
1590 return ieee80211_find_node_locked(nt, wh->i_addr2);
1594 * Locate the node for sender, track state, and then pass the
1595 * (referenced) node up to the 802.11 layer for its use. Note
1596 * we can return NULL if the sender is not in the table.
1598 struct ieee80211_node *
1599 #ifdef IEEE80211_DEBUG_REFCNT
1600 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1601 const struct ieee80211_frame_min *wh, const char *func, int line)
1603 ieee80211_find_rxnode(struct ieee80211com *ic,
1604 const struct ieee80211_frame_min *wh)
1607 struct ieee80211_node_table *nt;
1608 struct ieee80211_node *ni;
1611 IEEE80211_NODE_LOCK(nt);
1612 ni = _find_rxnode(nt, wh);
1613 IEEE80211_NODE_UNLOCK(nt);
1619 * Like ieee80211_find_rxnode but use the supplied h/w
1620 * key index as a hint to locate the node in the key
1621 * mapping table. If an entry is present at the key
1622 * index we return it; otherwise do a normal lookup and
1623 * update the mapping table if the station has a unicast
1624 * key assigned to it.
1626 struct ieee80211_node *
1627 #ifdef IEEE80211_DEBUG_REFCNT
1628 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1629 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1630 const char *func, int line)
1632 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1633 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1636 struct ieee80211_node_table *nt;
1637 struct ieee80211_node *ni;
1640 IEEE80211_NODE_LOCK(nt);
1641 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1642 ni = nt->nt_keyixmap[keyix];
1646 ni = _find_rxnode(nt, wh);
1647 if (ni != NULL && nt->nt_keyixmap != NULL) {
1649 * If the station has a unicast key cache slot
1650 * assigned update the key->node mapping table.
1652 keyix = ni->ni_ucastkey.wk_rxkeyix;
1653 /* XXX can keyixmap[keyix] != NULL? */
1654 if (keyix < nt->nt_keyixmax &&
1655 nt->nt_keyixmap[keyix] == NULL) {
1656 IEEE80211_DPRINTF(ni->ni_vap,
1658 "%s: add key map entry %p<%s> refcnt %d\n",
1659 __func__, ni, ether_sprintf(ni->ni_macaddr),
1660 ieee80211_node_refcnt(ni)+1);
1661 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1665 if (IS_BCAST_PROBEREQ(wh))
1666 ni = NULL; /* spam bcast probe req to all vap's */
1668 ieee80211_ref_node(ni);
1670 IEEE80211_NODE_UNLOCK(nt);
1674 #undef IS_BCAST_PROBEREQ
1678 * Return a reference to the appropriate node for sending
1679 * a data frame. This handles node discovery in adhoc networks.
1681 struct ieee80211_node *
1682 #ifdef IEEE80211_DEBUG_REFCNT
1683 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1684 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1685 const char *func, int line)
1687 ieee80211_find_txnode(struct ieee80211vap *vap,
1688 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1691 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1692 struct ieee80211_node *ni;
1695 * The destination address should be in the node table
1696 * unless this is a multicast/broadcast frame. We can
1697 * also optimize station mode operation, all frames go
1700 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1701 IEEE80211_NODE_LOCK(nt);
1702 if (vap->iv_opmode == IEEE80211_M_STA ||
1703 vap->iv_opmode == IEEE80211_M_WDS ||
1704 IEEE80211_IS_MULTICAST(macaddr))
1705 ni = ieee80211_ref_node(vap->iv_bss);
1707 ni = ieee80211_find_node_locked(nt, macaddr);
1708 IEEE80211_NODE_UNLOCK(nt);
1711 if (vap->iv_opmode == IEEE80211_M_IBSS ||
1712 vap->iv_opmode == IEEE80211_M_AHDEMO) {
1714 * In adhoc mode cons up a node for the destination.
1715 * Note that we need an additional reference for the
1716 * caller to be consistent with
1717 * ieee80211_find_node_locked.
1719 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1721 (void) ieee80211_ref_node(ni);
1723 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1724 "no node, discard frame (%s)", __func__);
1725 vap->iv_stats.is_tx_nonode++;
1732 _ieee80211_free_node(struct ieee80211_node *ni)
1734 struct ieee80211_node_table *nt = ni->ni_table;
1737 * NB: careful about referencing the vap as it may be
1738 * gone if the last reference was held by a driver.
1739 * We know the com will always be present so it's safe
1740 * to use ni_ic below to reclaim resources.
1743 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1744 "%s %p<%s> in %s table\n", __func__, ni,
1745 ether_sprintf(ni->ni_macaddr),
1746 nt != NULL ? nt->nt_name : "<gone>");
1748 if (ni->ni_associd != 0) {
1749 struct ieee80211vap *vap = ni->ni_vap;
1750 if (vap->iv_aid_bitmap != NULL)
1751 IEEE80211_AID_CLR(vap, ni->ni_associd);
1754 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1755 LIST_REMOVE(ni, ni_hash);
1757 ni->ni_ic->ic_node_free(ni);
1761 * Clear any entry in the unicast key mapping table.
1764 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1766 ieee80211_keyix keyix;
1768 keyix = ni->ni_ucastkey.wk_rxkeyix;
1769 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1770 nt->nt_keyixmap[keyix] == ni) {
1771 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1772 "%s: %p<%s> clear key map entry %u\n",
1773 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1774 nt->nt_keyixmap[keyix] = NULL;
1775 ieee80211_node_decref(ni);
1783 #ifdef IEEE80211_DEBUG_REFCNT
1784 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1786 ieee80211_free_node(struct ieee80211_node *ni)
1789 struct ieee80211_node_table *nt = ni->ni_table;
1791 #ifdef IEEE80211_DEBUG_REFCNT
1792 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1793 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1794 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1797 IEEE80211_NODE_LOCK(nt);
1798 if (ieee80211_node_dectestref(ni)) {
1800 * Last reference, reclaim state.
1802 _ieee80211_free_node(ni);
1803 } else if (ieee80211_node_refcnt(ni) == 1)
1804 if (node_clear_keyixmap(nt, ni))
1805 _ieee80211_free_node(ni);
1806 IEEE80211_NODE_UNLOCK(nt);
1808 if (ieee80211_node_dectestref(ni))
1809 _ieee80211_free_node(ni);
1814 * Reclaim a unicast key and clear any key cache state.
1817 ieee80211_node_delucastkey(struct ieee80211_node *ni)
1819 struct ieee80211com *ic = ni->ni_ic;
1820 struct ieee80211_node_table *nt = &ic->ic_sta;
1821 struct ieee80211_node *nikey;
1822 ieee80211_keyix keyix;
1823 int isowned, status;
1826 * NB: We must beware of LOR here; deleting the key
1827 * can cause the crypto layer to block traffic updates
1828 * which can generate a LOR against the node table lock;
1829 * grab it here and stash the key index for our use below.
1831 * Must also beware of recursion on the node table lock.
1832 * When called from node_cleanup we may already have
1833 * the node table lock held. Unfortunately there's no
1834 * way to separate out this path so we must do this
1837 isowned = IEEE80211_NODE_IS_LOCKED(nt);
1839 IEEE80211_NODE_LOCK(nt);
1841 status = 1; /* NB: success */
1842 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1843 keyix = ni->ni_ucastkey.wk_rxkeyix;
1844 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1845 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1846 nikey = nt->nt_keyixmap[keyix];
1847 nt->nt_keyixmap[keyix] = NULL;
1851 IEEE80211_NODE_UNLOCK(nt);
1853 if (nikey != NULL) {
1854 KASSERT(nikey == ni,
1855 ("key map out of sync, ni %p nikey %p", ni, nikey));
1856 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1857 "%s: delete key map entry %p<%s> refcnt %d\n",
1858 __func__, ni, ether_sprintf(ni->ni_macaddr),
1859 ieee80211_node_refcnt(ni)-1);
1860 ieee80211_free_node(ni);
1866 * Reclaim a node. If this is the last reference count then
1867 * do the normal free work. Otherwise remove it from the node
1868 * table and mark it gone by clearing the back-reference.
1871 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1874 IEEE80211_NODE_LOCK_ASSERT(nt);
1876 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1877 "%s: remove %p<%s> from %s table, refcnt %d\n",
1878 __func__, ni, ether_sprintf(ni->ni_macaddr),
1879 nt->nt_name, ieee80211_node_refcnt(ni)-1);
1881 * Clear any entry in the unicast key mapping table.
1882 * We need to do it here so rx lookups don't find it
1883 * in the mapping table even if it's not in the hash
1884 * table. We cannot depend on the mapping table entry
1885 * being cleared because the node may not be free'd.
1887 (void)node_clear_keyixmap(nt, ni);
1888 if (!ieee80211_node_dectestref(ni)) {
1890 * Other references are present, just remove the
1891 * node from the table so it cannot be found. When
1892 * the references are dropped storage will be
1895 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1896 LIST_REMOVE(ni, ni_hash);
1897 ni->ni_table = NULL; /* clear reference */
1899 _ieee80211_free_node(ni);
1903 * Node table support.
1907 ieee80211_node_table_init(struct ieee80211com *ic,
1908 struct ieee80211_node_table *nt,
1909 const char *name, int inact, int keyixmax)
1911 struct ifnet *ifp = ic->ic_ifp;
1914 IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1915 IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1916 TAILQ_INIT(&nt->nt_node);
1919 nt->nt_inact_init = inact;
1920 nt->nt_keyixmax = keyixmax;
1921 if (nt->nt_keyixmax > 0) {
1922 nt->nt_keyixmap = (struct ieee80211_node **) malloc(
1923 keyixmax * sizeof(struct ieee80211_node *),
1924 M_80211_NODE, M_NOWAIT | M_ZERO);
1925 if (nt->nt_keyixmap == NULL)
1926 if_printf(ic->ic_ifp,
1927 "Cannot allocate key index map with %u entries\n",
1930 nt->nt_keyixmap = NULL;
1934 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
1935 struct ieee80211vap *match)
1937 struct ieee80211_node *ni, *next;
1939 IEEE80211_NODE_LOCK(nt);
1940 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1941 if (match != NULL && ni->ni_vap != match)
1943 /* XXX can this happen? if so need's work */
1944 if (ni->ni_associd != 0) {
1945 struct ieee80211vap *vap = ni->ni_vap;
1947 if (vap->iv_auth->ia_node_leave != NULL)
1948 vap->iv_auth->ia_node_leave(ni);
1949 if (vap->iv_aid_bitmap != NULL)
1950 IEEE80211_AID_CLR(vap, ni->ni_associd);
1952 ni->ni_wdsvap = NULL; /* clear reference */
1953 node_reclaim(nt, ni);
1955 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1957 * Make a separate pass to clear references to this vap
1958 * held by DWDS entries. They will not be matched above
1959 * because ni_vap will point to the ap vap but we still
1960 * need to clear ni_wdsvap when the WDS vap is destroyed
1963 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1964 if (ni->ni_wdsvap == match)
1965 ni->ni_wdsvap = NULL;
1967 IEEE80211_NODE_UNLOCK(nt);
1971 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1973 ieee80211_node_table_reset(nt, NULL);
1974 if (nt->nt_keyixmap != NULL) {
1976 /* XXX verify all entries are NULL */
1978 for (i = 0; i < nt->nt_keyixmax; i++)
1979 if (nt->nt_keyixmap[i] != NULL)
1980 printf("%s: %s[%u] still active\n", __func__,
1983 free(nt->nt_keyixmap, M_80211_NODE);
1984 nt->nt_keyixmap = NULL;
1986 IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1987 IEEE80211_NODE_LOCK_DESTROY(nt);
1991 * Timeout inactive stations and do related housekeeping.
1992 * Note that we cannot hold the node lock while sending a
1993 * frame as this would lead to a LOR. Instead we use a
1994 * generation number to mark nodes that we've scanned and
1995 * drop the lock and restart a scan if we have to time out
1996 * a node. Since we are single-threaded by virtue of
1997 * controlling the inactivity timer we can be sure this will
1998 * process each node only once.
2001 ieee80211_timeout_stations(struct ieee80211com *ic)
2003 struct ieee80211_node_table *nt = &ic->ic_sta;
2004 struct ieee80211vap *vap;
2005 struct ieee80211_node *ni;
2008 IEEE80211_NODE_ITERATE_LOCK(nt);
2009 gen = ++nt->nt_scangen;
2011 IEEE80211_NODE_LOCK(nt);
2012 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2013 if (ni->ni_scangen == gen) /* previously handled */
2015 ni->ni_scangen = gen;
2017 * Ignore entries for which have yet to receive an
2018 * authentication frame. These are transient and
2019 * will be reclaimed when the last reference to them
2020 * goes away (when frame xmits complete).
2024 * Only process stations when in RUN state. This
2025 * insures, for example, that we don't timeout an
2026 * inactive station during CAC. Note that CSA state
2027 * is actually handled in ieee80211_node_timeout as
2028 * it applies to more than timeout processing.
2030 if (vap->iv_state != IEEE80211_S_RUN)
2032 /* XXX can vap be NULL? */
2033 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2034 vap->iv_opmode == IEEE80211_M_STA) &&
2035 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2038 * Free fragment if not needed anymore
2039 * (last fragment older than 1s).
2040 * XXX doesn't belong here, move to node_age
2042 if (ni->ni_rxfrag[0] != NULL &&
2043 ticks > ni->ni_rxfragstamp + hz) {
2044 m_freem(ni->ni_rxfrag[0]);
2045 ni->ni_rxfrag[0] = NULL;
2047 if (ni->ni_inact > 0) {
2049 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2050 "%s: inact %u inact_reload %u nrates %u",
2051 __func__, ni->ni_inact, ni->ni_inact_reload,
2052 ni->ni_rates.rs_nrates);
2055 * Special case ourself; we may be idle for extended periods
2056 * of time and regardless reclaiming our state is wrong.
2057 * XXX run ic_node_age
2059 if (ni == vap->iv_bss)
2061 if (ni->ni_associd != 0 ||
2062 (vap->iv_opmode == IEEE80211_M_IBSS ||
2063 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2065 * Age/drain resources held by the station.
2067 ic->ic_node_age(ni);
2069 * Probe the station before time it out. We
2070 * send a null data frame which may not be
2071 * universally supported by drivers (need it
2072 * for ps-poll support so it should be...).
2074 * XXX don't probe the station unless we've
2075 * received a frame from them (and have
2076 * some idea of the rates they are capable
2077 * of); this will get fixed more properly
2078 * soon with better handling of the rate set.
2080 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2081 (0 < ni->ni_inact &&
2082 ni->ni_inact <= vap->iv_inact_probe) &&
2083 ni->ni_rates.rs_nrates != 0) {
2085 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2087 "probe station due to inactivity");
2089 * Grab a reference before unlocking the table
2090 * so the node cannot be reclaimed before we
2091 * send the frame. ieee80211_send_nulldata
2092 * understands we've done this and reclaims the
2093 * ref for us as needed.
2095 ieee80211_ref_node(ni);
2096 IEEE80211_NODE_UNLOCK(nt);
2097 ieee80211_send_nulldata(ni);
2102 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2103 ni->ni_inact <= 0) {
2105 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2106 "station timed out due to inactivity "
2107 "(refcnt %u)", ieee80211_node_refcnt(ni));
2109 * Send a deauthenticate frame and drop the station.
2110 * This is somewhat complicated due to reference counts
2111 * and locking. At this point a station will typically
2112 * have a reference count of 1. ieee80211_node_leave
2113 * will do a "free" of the node which will drop the
2114 * reference count. But in the meantime a reference
2115 * wil be held by the deauth frame. The actual reclaim
2116 * of the node will happen either after the tx is
2117 * completed or by ieee80211_node_leave.
2119 * Separately we must drop the node lock before sending
2120 * in case the driver takes a lock, as this can result
2121 * in a LOR between the node lock and the driver lock.
2123 ieee80211_ref_node(ni);
2124 IEEE80211_NODE_UNLOCK(nt);
2125 if (ni->ni_associd != 0) {
2126 IEEE80211_SEND_MGMT(ni,
2127 IEEE80211_FC0_SUBTYPE_DEAUTH,
2128 IEEE80211_REASON_AUTH_EXPIRE);
2130 ieee80211_node_leave(ni);
2131 ieee80211_free_node(ni);
2132 vap->iv_stats.is_node_timeout++;
2136 IEEE80211_NODE_UNLOCK(nt);
2138 IEEE80211_NODE_ITERATE_UNLOCK(nt);
2142 * Aggressively reclaim resources. This should be used
2143 * only in a critical situation to reclaim mbuf resources.
2146 ieee80211_drain(struct ieee80211com *ic)
2148 struct ieee80211_node_table *nt = &ic->ic_sta;
2149 struct ieee80211vap *vap;
2150 struct ieee80211_node *ni;
2152 IEEE80211_NODE_LOCK(nt);
2153 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2155 * Ignore entries for which have yet to receive an
2156 * authentication frame. These are transient and
2157 * will be reclaimed when the last reference to them
2158 * goes away (when frame xmits complete).
2162 * Only process stations when in RUN state. This
2163 * insures, for example, that we don't timeout an
2164 * inactive station during CAC. Note that CSA state
2165 * is actually handled in ieee80211_node_timeout as
2166 * it applies to more than timeout processing.
2168 if (vap->iv_state != IEEE80211_S_RUN)
2170 /* XXX can vap be NULL? */
2171 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2172 vap->iv_opmode == IEEE80211_M_STA) &&
2173 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2177 * XXX doesn't belong here, move to node_drain
2179 if (ni->ni_rxfrag[0] != NULL) {
2180 m_freem(ni->ni_rxfrag[0]);
2181 ni->ni_rxfrag[0] = NULL;
2184 * Drain resources held by the station.
2186 ic->ic_node_drain(ni);
2188 IEEE80211_NODE_UNLOCK(nt);
2192 * Per-ieee80211com inactivity timer callback.
2195 ieee80211_node_timeout(void *arg)
2197 struct ieee80211com *ic = arg;
2200 * Defer timeout processing if a channel switch is pending.
2201 * We typically need to be mute so not doing things that
2202 * might generate frames is good to handle in one place.
2203 * Supressing the station timeout processing may extend the
2204 * lifetime of inactive stations (by not decrementing their
2205 * idle counters) but this should be ok unless the CSA is
2206 * active for an unusually long time.
2208 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2209 ieee80211_scan_timeout(ic);
2210 ieee80211_timeout_stations(ic);
2211 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2214 ieee80211_erp_timeout(ic);
2215 ieee80211_ht_timeout(ic);
2216 IEEE80211_UNLOCK(ic);
2218 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2219 ieee80211_node_timeout, ic);
2223 * Iterate over the node table and return an array of ref'ed nodes.
2225 * This is separated out from calling the actual node function so that
2226 * no LORs will occur.
2228 * If there are too many nodes (ie, the number of nodes doesn't fit
2229 * within 'max_aid' entries) then the node references will be freed
2230 * and an error will be returned.
2232 * The responsibility of allocating and freeing "ni_arr" is up to
2236 ieee80211_iterate_nt(struct ieee80211_node_table *nt,
2237 struct ieee80211_node **ni_arr, uint16_t max_aid)
2241 struct ieee80211_node *ni;
2243 IEEE80211_NODE_ITERATE_LOCK(nt);
2244 IEEE80211_NODE_LOCK(nt);
2246 gen = ++nt->nt_scangen;
2250 * We simply assume here that since the node
2251 * scan generation doesn't change (as
2252 * we are holding both the node table and
2253 * node table iteration locks), we can simply
2254 * assign it to the node here.
2256 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2259 if_printf(nt->nt_ic->ic_ifp,
2260 "Node array overflow: max=%u", max_aid);
2263 ni_arr[i] = ieee80211_ref_node(ni);
2264 ni_arr[i]->ni_scangen = gen;
2269 * It's safe to unlock here.
2271 * If we're successful, the list is returned.
2272 * If we're unsuccessful, the list is ignored
2273 * and we remove our references.
2275 * This avoids any potential LOR with
2276 * ieee80211_free_node().
2278 IEEE80211_NODE_UNLOCK(nt);
2279 IEEE80211_NODE_ITERATE_UNLOCK(nt);
2282 * If ret is non-zero, we hit some kind of error.
2283 * Rather than walking some nodes, we'll walk none
2287 for (j = 0; j < i; j++) {
2288 /* ieee80211_free_node() locks by itself */
2289 ieee80211_free_node(ni_arr[j]);
2297 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2298 * reference in the source.
2300 * Note that this fetches 'max_aid' from the first VAP, rather than finding
2301 * the largest max_aid from all VAPs.
2304 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2305 ieee80211_iter_func *f, void *arg)
2307 struct ieee80211_node **ni_arr;
2311 struct ieee80211vap *vap;
2313 /* Overdoing it default */
2314 max_aid = IEEE80211_AID_MAX;
2316 /* Handle the case of there being no vaps just yet */
2317 vap = TAILQ_FIRST(&nt->nt_ic->ic_vaps);
2319 max_aid = vap->iv_max_aid;
2321 size = max_aid * sizeof(struct ieee80211_node *);
2322 ni_arr = (struct ieee80211_node **) malloc(size, M_80211_NODE,
2328 * If this fails, the node table won't have any
2329 * valid entries - ieee80211_iterate_nt() frees
2330 * the references to them. So don't try walking
2331 * the table; just skip to the end and free the
2334 if (ieee80211_iterate_nt(nt, ni_arr, max_aid) != 0)
2337 for (i = 0; i < max_aid; i++) {
2338 if (ni_arr[i] == NULL) /* end of the list */
2340 (*f)(arg, ni_arr[i]);
2341 /* ieee80211_free_node() locks by itself */
2342 ieee80211_free_node(ni_arr[i]);
2346 free(ni_arr, M_80211_NODE);
2350 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2352 printf("0x%p: mac %s refcnt %d\n", ni,
2353 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2354 printf("\tscangen %u authmode %u flags 0x%x\n",
2355 ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2356 printf("\tassocid 0x%x txpower %u vlan %u\n",
2357 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2358 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2359 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2360 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2361 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2362 ni->ni_rxfragstamp);
2363 printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2364 node_getrssi(ni), ni->ni_noise,
2365 ni->ni_intval, ni->ni_capinfo);
2366 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2367 ether_sprintf(ni->ni_bssid),
2368 ni->ni_esslen, ni->ni_essid,
2369 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2370 printf("\tinact %u inact_reload %u txrate %u\n",
2371 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2372 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2373 ni->ni_htcap, ni->ni_htparam,
2374 ni->ni_htctlchan, ni->ni_ht2ndchan);
2375 printf("\thtopmode %x htstbc %x chw %u\n",
2376 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2380 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2382 ieee80211_iterate_nodes(nt,
2383 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2387 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2389 struct ieee80211vap *vap;
2391 IEEE80211_LOCK_ASSERT(ic);
2393 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2394 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2395 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2399 ieee80211_notify_erp(struct ieee80211com *ic)
2402 ieee80211_notify_erp_locked(ic);
2403 IEEE80211_UNLOCK(ic);
2407 * Handle a station joining an 11g network.
2410 ieee80211_node_join_11g(struct ieee80211_node *ni)
2412 struct ieee80211com *ic = ni->ni_ic;
2414 IEEE80211_LOCK_ASSERT(ic);
2417 * Station isn't capable of short slot time. Bump
2418 * the count of long slot time stations and disable
2419 * use of short slot time. Note that the actual switch
2420 * over to long slot time use may not occur until the
2421 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2423 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2424 ic->ic_longslotsta++;
2425 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2426 "station needs long slot time, count %d",
2427 ic->ic_longslotsta);
2428 /* XXX vap's w/ conflicting needs won't work */
2429 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2431 * Don't force slot time when switched to turbo
2432 * mode as non-ERP stations won't be present; this
2433 * need only be done when on the normal G channel.
2435 ieee80211_set_shortslottime(ic, 0);
2439 * If the new station is not an ERP station
2440 * then bump the counter and enable protection
2443 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2445 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2446 "station is !ERP, %d non-ERP stations associated",
2449 * If station does not support short preamble
2450 * then we must enable use of Barker preamble.
2452 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2453 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2454 "%s", "station needs long preamble");
2455 ic->ic_flags |= IEEE80211_F_USEBARKER;
2456 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2459 * If protection is configured and this is the first
2460 * indication we should use protection, enable it.
2462 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2463 ic->ic_nonerpsta == 1 &&
2464 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2465 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2466 "%s: enable use of protection\n", __func__);
2467 ic->ic_flags |= IEEE80211_F_USEPROT;
2468 ieee80211_notify_erp_locked(ic);
2471 ni->ni_flags |= IEEE80211_NODE_ERP;
2475 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2477 struct ieee80211com *ic = ni->ni_ic;
2478 struct ieee80211vap *vap = ni->ni_vap;
2481 if (ni->ni_associd == 0) {
2484 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2486 * It would be good to search the bitmap
2487 * more efficiently, but this will do for now.
2489 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2490 if (!IEEE80211_AID_ISSET(vap, aid))
2493 if (aid >= vap->iv_max_aid) {
2494 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2495 ieee80211_node_leave(ni);
2498 ni->ni_associd = aid | 0xc000;
2499 ni->ni_jointime = time_uptime;
2501 IEEE80211_AID_SET(vap, ni->ni_associd);
2502 vap->iv_sta_assoc++;
2505 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2506 ieee80211_ht_node_join(ni);
2507 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2508 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2509 ieee80211_node_join_11g(ni);
2510 IEEE80211_UNLOCK(ic);
2516 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2517 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2518 IEEE80211_NODE_AID(ni),
2519 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2520 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2521 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2522 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2523 ni->ni_flags & IEEE80211_NODE_HT ?
2524 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2525 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2526 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2527 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2528 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2529 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2530 ", fast-frames" : "",
2531 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2535 ieee80211_node_setuptxparms(ni);
2536 ieee80211_ratectl_node_init(ni);
2537 /* give driver a chance to setup state like ni_txrate */
2538 if (ic->ic_newassoc != NULL)
2539 ic->ic_newassoc(ni, newassoc);
2540 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2541 /* tell the authenticator about new station */
2542 if (vap->iv_auth->ia_node_join != NULL)
2543 vap->iv_auth->ia_node_join(ni);
2544 ieee80211_notify_node_join(ni,
2545 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2549 disable_protection(struct ieee80211com *ic)
2551 KASSERT(ic->ic_nonerpsta == 0 &&
2552 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2553 ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2556 ic->ic_flags &= ~IEEE80211_F_USEPROT;
2557 /* XXX verify mode? */
2558 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2559 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2560 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2562 ieee80211_notify_erp_locked(ic);
2566 * Handle a station leaving an 11g network.
2569 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2571 struct ieee80211com *ic = ni->ni_ic;
2573 IEEE80211_LOCK_ASSERT(ic);
2575 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2576 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2577 ic->ic_bsschan->ic_flags));
2580 * If a long slot station do the slot time bookkeeping.
2582 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2583 KASSERT(ic->ic_longslotsta > 0,
2584 ("bogus long slot station count %d", ic->ic_longslotsta));
2585 ic->ic_longslotsta--;
2586 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2587 "long slot time station leaves, count now %d",
2588 ic->ic_longslotsta);
2589 if (ic->ic_longslotsta == 0) {
2591 * Re-enable use of short slot time if supported
2592 * and not operating in IBSS mode (per spec).
2594 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2595 ic->ic_opmode != IEEE80211_M_IBSS) {
2596 IEEE80211_DPRINTF(ni->ni_vap,
2597 IEEE80211_MSG_ASSOC,
2598 "%s: re-enable use of short slot time\n",
2600 ieee80211_set_shortslottime(ic, 1);
2605 * If a non-ERP station do the protection-related bookkeeping.
2607 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2608 KASSERT(ic->ic_nonerpsta > 0,
2609 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2611 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2612 "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2613 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2614 " (non-ERP sta present)" : "");
2615 if (ic->ic_nonerpsta == 0 &&
2616 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2617 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2618 "%s: disable use of protection\n", __func__);
2619 disable_protection(ic);
2625 * Time out presence of an overlapping bss with non-ERP
2626 * stations. When operating in hostap mode we listen for
2627 * beacons from other stations and if we identify a non-ERP
2628 * station is present we enable protection. To identify
2629 * when all non-ERP stations are gone we time out this
2633 ieee80211_erp_timeout(struct ieee80211com *ic)
2636 IEEE80211_LOCK_ASSERT(ic);
2638 if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2639 time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2641 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2642 "%s", "age out non-ERP sta present on channel");
2644 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2645 if (ic->ic_nonerpsta == 0)
2646 disable_protection(ic);
2651 * Handle bookkeeping for station deauthentication/disassociation
2652 * when operating as an ap.
2655 ieee80211_node_leave(struct ieee80211_node *ni)
2657 struct ieee80211com *ic = ni->ni_ic;
2658 struct ieee80211vap *vap = ni->ni_vap;
2659 struct ieee80211_node_table *nt = ni->ni_table;
2661 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2662 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2664 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2665 ("unexpected operating mode %u", vap->iv_opmode));
2667 * If node wasn't previously associated all
2668 * we need to do is reclaim the reference.
2670 /* XXX ibss mode bypasses 11g and notification */
2671 if (ni->ni_associd == 0)
2674 * Tell the authenticator the station is leaving.
2675 * Note that we must do this before yanking the
2676 * association id as the authenticator uses the
2677 * associd to locate it's state block.
2679 if (vap->iv_auth->ia_node_leave != NULL)
2680 vap->iv_auth->ia_node_leave(ni);
2683 IEEE80211_AID_CLR(vap, ni->ni_associd);
2685 vap->iv_sta_assoc--;
2688 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2689 ieee80211_ht_node_leave(ni);
2690 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2691 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2692 ieee80211_node_leave_11g(ni);
2693 IEEE80211_UNLOCK(ic);
2695 * Cleanup station state. In particular clear various
2696 * state that might otherwise be reused if the node
2697 * is reused before the reference count goes to zero
2698 * (and memory is reclaimed).
2700 ieee80211_sta_leave(ni);
2703 * Remove the node from any table it's recorded in and
2704 * drop the caller's reference. Removal from the table
2705 * is important to insure the node is not reprocessed
2709 IEEE80211_NODE_LOCK(nt);
2710 node_reclaim(nt, ni);
2711 IEEE80211_NODE_UNLOCK(nt);
2713 ieee80211_free_node(ni);
2717 struct ieee80211vap *vap;
2719 uint32_t rssi_total;
2723 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2725 struct rssiinfo *info = arg;
2726 struct ieee80211vap *vap = ni->ni_vap;
2729 if (info->vap != vap)
2731 /* only associated stations */
2732 if (ni->ni_associd == 0)
2734 rssi = vap->iv_ic->ic_node_getrssi(ni);
2736 info->rssi_samples++;
2737 info->rssi_total += rssi;
2742 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2744 struct rssiinfo *info = arg;
2745 struct ieee80211vap *vap = ni->ni_vap;
2748 if (info->vap != vap)
2750 /* only neighbors */
2751 /* XXX check bssid */
2752 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2754 rssi = vap->iv_ic->ic_node_getrssi(ni);
2756 info->rssi_samples++;
2757 info->rssi_total += rssi;
2761 #ifdef IEEE80211_SUPPORT_MESH
2763 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
2765 struct rssiinfo *info = arg;
2766 struct ieee80211vap *vap = ni->ni_vap;
2769 if (info->vap != vap)
2771 /* only neighbors that peered successfully */
2772 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
2774 rssi = vap->iv_ic->ic_node_getrssi(ni);
2776 info->rssi_samples++;
2777 info->rssi_total += rssi;
2780 #endif /* IEEE80211_SUPPORT_MESH */
2783 ieee80211_getrssi(struct ieee80211vap *vap)
2785 #define NZ(x) ((x) == 0 ? 1 : (x))
2786 struct ieee80211com *ic = vap->iv_ic;
2787 struct rssiinfo info;
2789 info.rssi_total = 0;
2790 info.rssi_samples = 0;
2792 switch (vap->iv_opmode) {
2793 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
2794 case IEEE80211_M_AHDEMO: /* average of all neighbors */
2795 ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2797 case IEEE80211_M_HOSTAP: /* average of all associated stations */
2798 ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2800 #ifdef IEEE80211_SUPPORT_MESH
2801 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
2802 ieee80211_iterate_nodes(&ic->ic_sta, get_mesh_rssi, &info);
2805 case IEEE80211_M_MONITOR: /* XXX */
2806 case IEEE80211_M_STA: /* use stats from associated ap */
2808 if (vap->iv_bss != NULL)
2809 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2810 info.rssi_samples = 1;
2813 return info.rssi_total / NZ(info.rssi_samples);
2818 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2821 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
2823 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2824 /* for non-station mode return avg'd rssi accounting */
2825 if (vap->iv_opmode != IEEE80211_M_STA)
2826 *rssi = ieee80211_getrssi(vap);