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_media.h>
42 #include <net/ethernet.h>
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_input.h>
46 #ifdef IEEE80211_SUPPORT_SUPERG
47 #include <net80211/ieee80211_superg.h>
49 #ifdef IEEE80211_SUPPORT_TDMA
50 #include <net80211/ieee80211_tdma.h>
52 #include <net80211/ieee80211_wds.h>
53 #include <net80211/ieee80211_mesh.h>
54 #include <net80211/ieee80211_ratectl.h>
59 * IEEE80211_NODE_HASHSIZE must be a power of 2.
61 CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
64 * Association id's are managed with a bit vector.
66 #define IEEE80211_AID_SET(_vap, b) \
67 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
68 (1 << (IEEE80211_AID(b) % 32)))
69 #define IEEE80211_AID_CLR(_vap, b) \
70 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
71 ~(1 << (IEEE80211_AID(b) % 32)))
72 #define IEEE80211_AID_ISSET(_vap, b) \
73 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
75 #ifdef IEEE80211_DEBUG_REFCNT
76 #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
78 #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
81 static int ieee80211_sta_join1(struct ieee80211_node *);
83 static struct ieee80211_node *node_alloc(struct ieee80211vap *,
84 const uint8_t [IEEE80211_ADDR_LEN]);
85 static void node_cleanup(struct ieee80211_node *);
86 static void node_free(struct ieee80211_node *);
87 static void node_age(struct ieee80211_node *);
88 static int8_t node_getrssi(const struct ieee80211_node *);
89 static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
90 static void node_getmimoinfo(const struct ieee80211_node *,
91 struct ieee80211_mimo_info *);
93 static void _ieee80211_free_node(struct ieee80211_node *);
95 static void ieee80211_node_table_init(struct ieee80211com *ic,
96 struct ieee80211_node_table *nt, const char *name,
97 int inact, int keymaxix);
98 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
99 struct ieee80211vap *);
100 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
101 static void ieee80211_erp_timeout(struct ieee80211com *);
103 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
104 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
107 ieee80211_node_attach(struct ieee80211com *ic)
109 /* XXX really want maxlen enforced per-sta */
110 ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
112 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
113 IEEE80211_INACT_INIT, ic->ic_max_keyix);
114 callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
115 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
116 ieee80211_node_timeout, ic);
118 ic->ic_node_alloc = node_alloc;
119 ic->ic_node_free = node_free;
120 ic->ic_node_cleanup = node_cleanup;
121 ic->ic_node_age = node_age;
122 ic->ic_node_drain = node_age; /* NB: same as age */
123 ic->ic_node_getrssi = node_getrssi;
124 ic->ic_node_getsignal = node_getsignal;
125 ic->ic_node_getmimoinfo = node_getmimoinfo;
128 * Set flags to be propagated to all vap's;
129 * these define default behaviour/configuration.
131 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
135 ieee80211_node_detach(struct ieee80211com *ic)
138 callout_drain(&ic->ic_inact);
139 ieee80211_node_table_cleanup(&ic->ic_sta);
140 ieee80211_ageq_cleanup(&ic->ic_stageq);
144 ieee80211_node_vattach(struct ieee80211vap *vap)
146 /* NB: driver can override */
147 vap->iv_max_aid = IEEE80211_AID_DEF;
149 /* default station inactivity timer setings */
150 vap->iv_inact_init = IEEE80211_INACT_INIT;
151 vap->iv_inact_auth = IEEE80211_INACT_AUTH;
152 vap->iv_inact_run = IEEE80211_INACT_RUN;
153 vap->iv_inact_probe = IEEE80211_INACT_PROBE;
155 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
156 "%s: init %u auth %u run %u probe %u\n", __func__,
157 vap->iv_inact_init, vap->iv_inact_auth,
158 vap->iv_inact_run, vap->iv_inact_probe);
162 ieee80211_node_latevattach(struct ieee80211vap *vap)
164 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
165 /* XXX should we allow max aid to be zero? */
166 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
167 vap->iv_max_aid = IEEE80211_AID_MIN;
168 if_printf(vap->iv_ifp,
169 "WARNING: max aid too small, changed to %d\n",
172 vap->iv_aid_bitmap = (uint32_t *) malloc(
173 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
174 M_80211_NODE, M_NOWAIT | M_ZERO);
175 if (vap->iv_aid_bitmap == NULL) {
176 /* XXX no way to recover */
177 printf("%s: no memory for AID bitmap, max aid %d!\n",
178 __func__, vap->iv_max_aid);
183 ieee80211_reset_bss(vap);
185 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
189 ieee80211_node_vdetach(struct ieee80211vap *vap)
191 struct ieee80211com *ic = vap->iv_ic;
193 ieee80211_node_table_reset(&ic->ic_sta, vap);
194 if (vap->iv_bss != NULL) {
195 ieee80211_free_node(vap->iv_bss);
198 if (vap->iv_aid_bitmap != NULL) {
199 free(vap->iv_aid_bitmap, M_80211_NODE);
200 vap->iv_aid_bitmap = NULL;
205 * Port authorize/unauthorize interfaces for use by an authenticator.
209 ieee80211_node_authorize(struct ieee80211_node *ni)
211 struct ieee80211vap *vap = ni->ni_vap;
213 ni->ni_flags |= IEEE80211_NODE_AUTH;
214 ni->ni_inact_reload = vap->iv_inact_run;
215 ni->ni_inact = ni->ni_inact_reload;
217 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
218 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
222 ieee80211_node_unauthorize(struct ieee80211_node *ni)
224 struct ieee80211vap *vap = ni->ni_vap;
226 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
227 ni->ni_inact_reload = vap->iv_inact_auth;
228 if (ni->ni_inact > ni->ni_inact_reload)
229 ni->ni_inact = ni->ni_inact_reload;
231 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
232 "%s: inact_reload %u inact %u", __func__,
233 ni->ni_inact_reload, ni->ni_inact);
237 * Fix tx parameters for a node according to ``association state''.
240 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
242 struct ieee80211vap *vap = ni->ni_vap;
243 enum ieee80211_phymode mode;
245 if (ni->ni_flags & IEEE80211_NODE_HT) {
246 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
247 mode = IEEE80211_MODE_11NA;
249 mode = IEEE80211_MODE_11NG;
250 } else { /* legacy rate handling */
251 if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
252 mode = IEEE80211_MODE_STURBO_A;
253 else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
254 mode = IEEE80211_MODE_HALF;
255 else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
256 mode = IEEE80211_MODE_QUARTER;
257 /* NB: 108A should be handled as 11a */
258 else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
259 mode = IEEE80211_MODE_11A;
260 else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
261 (ni->ni_flags & IEEE80211_NODE_ERP))
262 mode = IEEE80211_MODE_11G;
264 mode = IEEE80211_MODE_11B;
266 ni->ni_txparms = &vap->iv_txparms[mode];
270 * Set/change the channel. The rate set is also updated as
271 * to insure a consistent view by drivers.
272 * XXX should be private but hostap needs it to deal with CSA
275 ieee80211_node_set_chan(struct ieee80211_node *ni,
276 struct ieee80211_channel *chan)
278 struct ieee80211com *ic = ni->ni_ic;
279 struct ieee80211vap *vap = ni->ni_vap;
280 enum ieee80211_phymode mode;
282 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
285 mode = ieee80211_chan2mode(chan);
286 if (IEEE80211_IS_CHAN_HT(chan)) {
288 * We must install the legacy rate est in ni_rates and the
289 * HT rate set in ni_htrates.
291 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
293 * Setup bss tx parameters based on operating mode. We
294 * use legacy rates when operating in a mixed HT+non-HT bss
295 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
297 if (mode == IEEE80211_MODE_11NA &&
298 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
299 mode = IEEE80211_MODE_11A;
300 else if (mode == IEEE80211_MODE_11NG &&
301 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
302 mode = IEEE80211_MODE_11G;
303 if (mode == IEEE80211_MODE_11G &&
304 (vap->iv_flags & IEEE80211_F_PUREG) == 0)
305 mode = IEEE80211_MODE_11B;
307 ni->ni_txparms = &vap->iv_txparms[mode];
308 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
312 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
314 /* propagate useful state */
315 nbss->ni_authmode = obss->ni_authmode;
316 nbss->ni_txpower = obss->ni_txpower;
317 nbss->ni_vlan = obss->ni_vlan;
318 /* XXX statistics? */
319 /* XXX legacy WDS bssid? */
323 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
325 struct ieee80211com *ic = vap->iv_ic;
326 struct ieee80211_node *ni;
328 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
329 "%s: creating %s on channel %u\n", __func__,
330 ieee80211_opmode_name[vap->iv_opmode],
331 ieee80211_chan2ieee(ic, chan));
333 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
338 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
339 ni->ni_esslen = vap->iv_des_ssid[0].len;
340 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
341 if (vap->iv_bss != NULL)
342 copy_bss(ni, vap->iv_bss);
343 ni->ni_intval = ic->ic_bintval;
344 if (vap->iv_flags & IEEE80211_F_PRIVACY)
345 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
346 if (ic->ic_phytype == IEEE80211_T_FH) {
347 ni->ni_fhdwell = 200; /* XXX */
350 if (vap->iv_opmode == IEEE80211_M_IBSS) {
351 vap->iv_flags |= IEEE80211_F_SIBSS;
352 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
353 if (vap->iv_flags & IEEE80211_F_DESBSSID)
354 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
356 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
357 /* clear group bit, add local bit */
358 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
360 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
361 if (vap->iv_flags & IEEE80211_F_DESBSSID)
362 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
364 #ifdef IEEE80211_SUPPORT_TDMA
365 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
367 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
368 #ifdef IEEE80211_SUPPORT_MESH
369 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
370 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
371 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
375 * Fix the channel and related attributes.
377 /* clear DFS CAC state on previous channel */
378 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
379 ic->ic_bsschan->ic_freq != chan->ic_freq &&
380 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
381 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
382 ic->ic_bsschan = chan;
383 ieee80211_node_set_chan(ni, chan);
384 ic->ic_curmode = ieee80211_chan2mode(chan);
386 * Do mode-specific setup.
388 if (IEEE80211_IS_CHAN_FULL(chan)) {
389 if (IEEE80211_IS_CHAN_ANYG(chan)) {
391 * Use a mixed 11b/11g basic rate set.
393 ieee80211_setbasicrates(&ni->ni_rates,
395 if (vap->iv_flags & IEEE80211_F_PUREG) {
397 * Also mark OFDM rates basic so 11b
398 * stations do not join (WiFi compliance).
400 ieee80211_addbasicrates(&ni->ni_rates,
403 } else if (IEEE80211_IS_CHAN_B(chan)) {
405 * Force pure 11b rate set.
407 ieee80211_setbasicrates(&ni->ni_rates,
412 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
416 * Reset bss state on transition to the INIT state.
417 * Clear any stations from the table (they have been
418 * deauth'd) and reset the bss node (clears key, rate
422 ieee80211_reset_bss(struct ieee80211vap *vap)
424 struct ieee80211com *ic = vap->iv_ic;
425 struct ieee80211_node *ni, *obss;
427 ieee80211_node_table_reset(&ic->ic_sta, vap);
428 /* XXX multi-bss: wrong */
429 ieee80211_reset_erp(ic);
431 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
432 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
434 vap->iv_bss = ieee80211_ref_node(ni);
437 ni->ni_intval = ic->ic_bintval;
438 ieee80211_free_node(obss);
440 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
444 match_ssid(const struct ieee80211_node *ni,
445 int nssid, const struct ieee80211_scan_ssid ssids[])
449 for (i = 0; i < nssid; i++) {
450 if (ni->ni_esslen == ssids[i].len &&
451 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
458 * Test a node for suitability/compatibility.
461 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
463 struct ieee80211com *ic = ni->ni_ic;
466 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
468 if (vap->iv_opmode == IEEE80211_M_IBSS) {
469 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
472 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
475 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
476 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
479 /* XXX does this mean privacy is supported or required? */
480 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
483 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
484 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
485 if (rate & IEEE80211_RATE_BASIC)
487 if (vap->iv_des_nssid != 0 &&
488 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
490 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
491 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
496 #ifdef IEEE80211_DEBUG
498 * Display node suitability/compatibility.
501 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
503 struct ieee80211com *ic = ni->ni_ic;
508 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
510 if (vap->iv_opmode == IEEE80211_M_IBSS) {
511 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
514 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
517 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
518 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
521 /* XXX does this mean privacy is supported or required? */
522 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
525 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
526 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
527 if (rate & IEEE80211_RATE_BASIC)
529 if (vap->iv_des_nssid != 0 &&
530 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
532 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
533 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
536 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
537 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
539 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
540 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
541 fail & 0x08 ? '!' : ' ');
543 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
544 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
546 fail & 0x02 ? '!' : ' ');
548 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
549 fail & 0x04 ? '!' : ' ');
550 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
551 printf("%s\n", fail & 0x10 ? "!" : "");
553 #endif /* IEEE80211_DEBUG */
556 * Handle 802.11 ad hoc network merge. The
557 * convention, set by the Wireless Ethernet Compatibility Alliance
558 * (WECA), is that an 802.11 station will change its BSSID to match
559 * the "oldest" 802.11 ad hoc network, on the same channel, that
560 * has the station's desired SSID. The "oldest" 802.11 network
561 * sends beacons with the greatest TSF timestamp.
563 * The caller is assumed to validate TSF's before attempting a merge.
565 * Return !0 if the BSSID changed, 0 otherwise.
568 ieee80211_ibss_merge(struct ieee80211_node *ni)
570 struct ieee80211vap *vap = ni->ni_vap;
571 #ifdef IEEE80211_DEBUG
572 struct ieee80211com *ic = ni->ni_ic;
575 if (ni == vap->iv_bss ||
576 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
577 /* unchanged, nothing to do */
580 if (!check_bss(vap, ni)) {
581 /* capabilities mismatch */
582 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
583 "%s: merge failed, capabilities mismatch\n", __func__);
584 #ifdef IEEE80211_DEBUG
585 if (ieee80211_msg_assoc(vap))
586 check_bss_debug(vap, ni);
588 vap->iv_stats.is_ibss_capmismatch++;
591 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
592 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
593 ether_sprintf(ni->ni_bssid),
594 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
595 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
596 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
598 return ieee80211_sta_join1(ieee80211_ref_node(ni));
602 * Calculate HT channel promotion flags for all vaps.
603 * This assumes ni_chan have been setup for each vap.
606 gethtadjustflags(struct ieee80211com *ic)
608 struct ieee80211vap *vap;
613 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
614 if (vap->iv_state < IEEE80211_S_RUN)
616 switch (vap->iv_opmode) {
617 case IEEE80211_M_WDS:
618 case IEEE80211_M_STA:
619 case IEEE80211_M_AHDEMO:
620 case IEEE80211_M_HOSTAP:
621 case IEEE80211_M_IBSS:
622 case IEEE80211_M_MBSS:
623 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
633 * Check if the current channel needs to change based on whether
634 * any vap's are using HT20/HT40. This is used to sync the state
635 * of ic_curchan after a channel width change on a running vap.
638 ieee80211_sync_curchan(struct ieee80211com *ic)
640 struct ieee80211_channel *c;
642 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
643 if (c != ic->ic_curchan) {
645 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
646 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
647 IEEE80211_UNLOCK(ic);
648 ic->ic_set_channel(ic);
649 ieee80211_radiotap_chan_change(ic);
655 * Setup the current channel. The request channel may be
656 * promoted if other vap's are operating with HT20/HT40.
659 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
661 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
662 int flags = gethtadjustflags(ic);
664 * Check for channel promotion required to support the
665 * set of running vap's. This assumes we are called
666 * after ni_chan is setup for each vap.
668 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
669 if (flags > ieee80211_htchanflags(c))
670 c = ieee80211_ht_adjust_channel(ic, c, flags);
672 ic->ic_bsschan = ic->ic_curchan = c;
673 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
674 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
678 * Change the current channel. The channel change is guaranteed to have
679 * happened before the next state change.
682 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
684 ieee80211_setupcurchan(ic, c);
685 ieee80211_runtask(ic, &ic->ic_chan_task);
689 ieee80211_update_chw(struct ieee80211com *ic)
692 ieee80211_setupcurchan(ic, ic->ic_curchan);
693 ieee80211_runtask(ic, &ic->ic_chw_task);
697 * Join the specified IBSS/BSS network. The node is assumed to
698 * be passed in with a held reference.
701 ieee80211_sta_join1(struct ieee80211_node *selbs)
703 struct ieee80211vap *vap = selbs->ni_vap;
704 struct ieee80211com *ic = selbs->ni_ic;
705 struct ieee80211_node *obss;
709 * Committed to selbs, setup state.
713 * Check if old+new node have the same address in which
714 * case we can reassociate when operating in sta mode.
716 canreassoc = (obss != NULL &&
717 vap->iv_state == IEEE80211_S_RUN &&
718 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
719 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */
721 copy_bss(selbs, obss);
722 ieee80211_node_decref(obss); /* iv_bss reference */
723 ieee80211_free_node(obss); /* station table reference */
724 obss = NULL; /* NB: guard against later use */
728 * Delete unusable rates; we've already checked
729 * that the negotiated rate set is acceptable.
731 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
732 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
734 ieee80211_setcurchan(ic, selbs->ni_chan);
736 * Set the erp state (mostly the slot time) to deal with
737 * the auto-select case; this should be redundant if the
740 ieee80211_reset_erp(ic);
741 ieee80211_wme_initparams(vap);
743 if (vap->iv_opmode == IEEE80211_M_STA) {
746 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
749 * Act as if we received a DEAUTH frame in case we
750 * are invoked from the RUN state. This will cause
751 * us to try to re-authenticate if we are operating
754 ieee80211_new_state(vap, IEEE80211_S_AUTH,
755 IEEE80211_FC0_SUBTYPE_DEAUTH);
758 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
763 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
764 const struct ieee80211_scan_entry *se)
766 struct ieee80211com *ic = vap->iv_ic;
767 struct ieee80211_node *ni;
769 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
776 * Expand scan state into node's format.
777 * XXX may not need all this stuff
779 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
780 ni->ni_esslen = se->se_ssid[1];
781 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
782 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
783 ni->ni_intval = se->se_intval;
784 ni->ni_capinfo = se->se_capinfo;
786 ni->ni_timoff = se->se_timoff;
787 ni->ni_fhdwell = se->se_fhdwell;
788 ni->ni_fhindex = se->se_fhindex;
789 ni->ni_erp = se->se_erp;
790 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
791 ni->ni_noise = se->se_noise;
792 if (vap->iv_opmode == IEEE80211_M_STA) {
793 /* NB: only infrastructure mode requires an associd */
794 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
797 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
798 ieee80211_ies_expand(&ni->ni_ies);
799 #ifdef IEEE80211_SUPPORT_SUPERG
800 if (ni->ni_ies.ath_ie != NULL)
801 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
803 if (ni->ni_ies.htcap_ie != NULL)
804 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
805 if (ni->ni_ies.htinfo_ie != NULL)
806 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
807 #ifdef IEEE80211_SUPPORT_MESH
808 if (ni->ni_ies.meshid_ie != NULL)
809 ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
811 #ifdef IEEE80211_SUPPORT_TDMA
812 if (ni->ni_ies.tdma_ie != NULL)
813 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
817 vap->iv_dtim_period = se->se_dtimperiod;
818 vap->iv_dtim_count = 0;
820 /* NB: must be after ni_chan is setup */
821 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
823 if (ieee80211_iserp_rateset(&ni->ni_rates))
824 ni->ni_flags |= IEEE80211_NODE_ERP;
827 * Setup HT state for this node if it's available, otherwise
828 * non-STA modes won't pick this state up.
830 * For IBSS and related modes that don't go through an
831 * association request/response, the only appropriate place
832 * to setup the HT state is here.
834 if (ni->ni_ies.htinfo_ie != NULL &&
835 ni->ni_ies.htcap_ie != NULL &&
836 vap->iv_flags_ht & IEEE80211_FHT_HT) {
837 ieee80211_ht_node_init(ni);
838 ieee80211_ht_updateparams(ni,
840 ni->ni_ies.htinfo_ie);
841 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
842 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
843 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
845 /* XXX else check for ath FF? */
846 /* XXX QoS? Difficult given that WME config is specific to a master */
848 ieee80211_node_setuptxparms(ni);
849 ieee80211_ratectl_node_init(ni);
851 return ieee80211_sta_join1(ieee80211_ref_node(ni));
855 * Leave the specified IBSS/BSS network. The node is assumed to
856 * be passed in with a held reference.
859 ieee80211_sta_leave(struct ieee80211_node *ni)
861 struct ieee80211com *ic = ni->ni_ic;
863 ic->ic_node_cleanup(ni);
864 ieee80211_notify_node_leave(ni);
868 * Send a deauthenticate frame and drop the station.
871 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
873 /* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
874 ieee80211_ref_node(ni);
875 if (ni->ni_associd != 0)
876 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
877 ieee80211_node_leave(ni);
878 ieee80211_free_node(ni);
881 static struct ieee80211_node *
882 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
884 struct ieee80211_node *ni;
886 ni = (struct ieee80211_node *) malloc(sizeof(struct ieee80211_node),
887 M_80211_NODE, M_NOWAIT | M_ZERO);
892 * Initialize an ie blob with the specified data. If previous
893 * data exists re-use the data block. As a side effect we clear
894 * all references to specific ie's; the caller is required to
898 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
900 /* NB: assumes data+len are the last fields */
901 memset(ies, 0, offsetof(struct ieee80211_ies, data));
902 if (ies->data != NULL && ies->len != len) {
903 /* data size changed */
904 free(ies->data, M_80211_NODE_IE);
907 if (ies->data == NULL) {
908 ies->data = (uint8_t *) malloc(len, M_80211_NODE_IE, M_NOWAIT);
909 if (ies->data == NULL) {
911 /* NB: pointers have already been zero'd above */
915 memcpy(ies->data, data, len);
921 * Reclaim storage for an ie blob.
924 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
926 if (ies->data != NULL)
927 free(ies->data, M_80211_NODE_IE);
931 * Expand an ie blob data contents and to fillin individual
932 * ie pointers. The data blob is assumed to be well-formed;
933 * we don't do any validity checking of ie lengths.
936 ieee80211_ies_expand(struct ieee80211_ies *ies)
945 case IEEE80211_ELEMID_VENDOR:
948 else if (iswmeoui(ie))
950 #ifdef IEEE80211_SUPPORT_SUPERG
951 else if (isatherosoui(ie))
954 #ifdef IEEE80211_SUPPORT_TDMA
955 else if (istdmaoui(ie))
959 case IEEE80211_ELEMID_RSN:
962 case IEEE80211_ELEMID_HTCAP:
965 case IEEE80211_ELEMID_HTINFO:
968 #ifdef IEEE80211_SUPPORT_MESH
969 case IEEE80211_ELEMID_MESHID:
980 * Reclaim any resources in a node and reset any critical
981 * state. Typically nodes are free'd immediately after,
982 * but in some cases the storage may be reused so we need
983 * to insure consistent state (should probably fix that).
986 node_cleanup(struct ieee80211_node *ni)
988 struct ieee80211vap *vap = ni->ni_vap;
989 struct ieee80211com *ic = ni->ni_ic;
992 /* NB: preserve ni_table */
993 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
994 if (vap->iv_opmode != IEEE80211_M_STA)
996 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
997 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
998 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
1001 * Cleanup any HT-related state.
1003 if (ni->ni_flags & IEEE80211_NODE_HT)
1004 ieee80211_ht_node_cleanup(ni);
1005 #ifdef IEEE80211_SUPPORT_SUPERG
1006 else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
1007 ieee80211_ff_node_cleanup(ni);
1009 #ifdef IEEE80211_SUPPORT_MESH
1011 * Cleanup any mesh-related state.
1013 if (vap->iv_opmode == IEEE80211_M_MBSS)
1014 ieee80211_mesh_node_cleanup(ni);
1017 * Clear any staging queue entries.
1019 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1022 * Clear AREF flag that marks the authorization refcnt bump
1023 * has happened. This is probably not needed as the node
1024 * should always be removed from the table so not found but
1025 * do it just in case.
1026 * Likewise clear the ASSOCID flag as these flags are intended
1027 * to be managed in tandem.
1029 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1032 * Drain power save queue and, if needed, clear TIM.
1034 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1035 vap->iv_set_tim(ni, 0);
1038 if (ni->ni_challenge != NULL) {
1039 free(ni->ni_challenge, M_80211_NODE);
1040 ni->ni_challenge = NULL;
1043 * Preserve SSID, WPA, and WME ie's so the bss node is
1044 * reusable during a re-auth/re-assoc state transition.
1045 * If we remove these data they will not be recreated
1046 * because they come from a probe-response or beacon frame
1047 * which cannot be expected prior to the association-response.
1048 * This should not be an issue when operating in other modes
1049 * as stations leaving always go through a full state transition
1050 * which will rebuild this state.
1052 * XXX does this leave us open to inheriting old state?
1054 for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1055 if (ni->ni_rxfrag[i] != NULL) {
1056 m_freem(ni->ni_rxfrag[i]);
1057 ni->ni_rxfrag[i] = NULL;
1060 * Must be careful here to remove any key map entry w/o a LOR.
1062 ieee80211_node_delucastkey(ni);
1066 node_free(struct ieee80211_node *ni)
1068 struct ieee80211com *ic = ni->ni_ic;
1070 ieee80211_ratectl_node_deinit(ni);
1071 ic->ic_node_cleanup(ni);
1072 ieee80211_ies_cleanup(&ni->ni_ies);
1073 ieee80211_psq_cleanup(&ni->ni_psq);
1074 free(ni, M_80211_NODE);
1078 node_age(struct ieee80211_node *ni)
1080 struct ieee80211vap *vap = ni->ni_vap;
1082 IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
1085 * Age frames on the power save queue.
1087 if (ieee80211_node_psq_age(ni) != 0 &&
1088 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1089 vap->iv_set_tim(ni, 0);
1091 * Age out HT resources (e.g. frames on the
1092 * A-MPDU reorder queues).
1094 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1095 ieee80211_ht_node_age(ni);
1099 node_getrssi(const struct ieee80211_node *ni)
1101 uint32_t avgrssi = ni->ni_avgrssi;
1104 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1106 rssi = IEEE80211_RSSI_GET(avgrssi);
1107 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1111 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1113 *rssi = node_getrssi(ni);
1114 *noise = ni->ni_noise;
1118 node_getmimoinfo(const struct ieee80211_node *ni,
1119 struct ieee80211_mimo_info *info)
1125 bzero(info, sizeof(*info));
1127 for (i = 0; i < ni->ni_mimo_chains; i++) {
1128 avgrssi = ni->ni_mimo_rssi_ctl[i];
1129 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1132 rssi = IEEE80211_RSSI_GET(avgrssi);
1133 info->rssi[i] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1135 info->noise[i] = ni->ni_mimo_noise_ctl[i];
1138 /* XXX ext radios? */
1143 struct ieee80211_node *
1144 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1145 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1147 struct ieee80211com *ic = nt->nt_ic;
1148 struct ieee80211_node *ni;
1151 ni = ic->ic_node_alloc(vap, macaddr);
1153 vap->iv_stats.is_rx_nodealloc++;
1157 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1158 "%s %p<%s> in %s table\n", __func__, ni,
1159 ether_sprintf(macaddr), nt->nt_name);
1161 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1162 hash = IEEE80211_NODE_HASH(ic, macaddr);
1163 ieee80211_node_initref(ni); /* mark referenced */
1164 ni->ni_chan = IEEE80211_CHAN_ANYC;
1165 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1166 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1167 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1168 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1169 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1170 ni->ni_inact_reload = nt->nt_inact_init;
1171 ni->ni_inact = ni->ni_inact_reload;
1172 ni->ni_ath_defkeyix = 0x7fff;
1173 ieee80211_psq_init(&ni->ni_psq, "unknown");
1174 #ifdef IEEE80211_SUPPORT_MESH
1175 if (vap->iv_opmode == IEEE80211_M_MBSS)
1176 ieee80211_mesh_node_init(vap, ni);
1178 IEEE80211_NODE_LOCK(nt);
1179 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1180 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1184 IEEE80211_NODE_UNLOCK(nt);
1186 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1187 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1189 ieee80211_ratectl_node_init(ni);
1195 * Craft a temporary node suitable for sending a management frame
1196 * to the specified station. We craft only as much state as we
1197 * need to do the work since the node will be immediately reclaimed
1198 * once the send completes.
1200 struct ieee80211_node *
1201 ieee80211_tmp_node(struct ieee80211vap *vap,
1202 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1204 struct ieee80211com *ic = vap->iv_ic;
1205 struct ieee80211_node *ni;
1207 ni = ic->ic_node_alloc(vap, macaddr);
1209 struct ieee80211_node *bss = vap->iv_bss;
1211 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1212 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1214 ni->ni_table = NULL; /* NB: pedantic */
1215 ni->ni_ic = ic; /* NB: needed to set channel */
1218 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1219 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1220 ieee80211_node_initref(ni); /* mark referenced */
1221 /* NB: required by ieee80211_fix_rate */
1222 ieee80211_node_set_chan(ni, bss->ni_chan);
1223 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1224 IEEE80211_KEYIX_NONE);
1225 ni->ni_txpower = bss->ni_txpower;
1226 /* XXX optimize away */
1227 ieee80211_psq_init(&ni->ni_psq, "unknown");
1229 ieee80211_ratectl_node_init(ni);
1232 vap->iv_stats.is_rx_nodealloc++;
1237 struct ieee80211_node *
1238 ieee80211_dup_bss(struct ieee80211vap *vap,
1239 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1241 struct ieee80211com *ic = vap->iv_ic;
1242 struct ieee80211_node *ni;
1244 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1246 struct ieee80211_node *bss = vap->iv_bss;
1248 * Inherit from iv_bss.
1251 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1252 ieee80211_node_set_chan(ni, bss->ni_chan);
1258 * Create a bss node for a legacy WDS vap. The far end does
1259 * not associate so we just create create a new node and
1260 * simulate an association. The caller is responsible for
1261 * installing the node as the bss node and handling any further
1262 * setup work like authorizing the port.
1264 struct ieee80211_node *
1265 ieee80211_node_create_wds(struct ieee80211vap *vap,
1266 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1268 struct ieee80211com *ic = vap->iv_ic;
1269 struct ieee80211_node *ni;
1271 /* XXX check if node already in sta table? */
1272 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1274 ni->ni_wdsvap = vap;
1275 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1277 * Inherit any manually configured settings.
1279 copy_bss(ni, vap->iv_bss);
1280 ieee80211_node_set_chan(ni, chan);
1281 /* NB: propagate ssid so available to WPA supplicant */
1282 ni->ni_esslen = vap->iv_des_ssid[0].len;
1283 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1284 /* NB: no associd for peer */
1286 * There are no management frames to use to
1287 * discover neighbor capabilities, so blindly
1288 * propagate the local configuration.
1290 if (vap->iv_flags & IEEE80211_F_WME)
1291 ni->ni_flags |= IEEE80211_NODE_QOS;
1292 #ifdef IEEE80211_SUPPORT_SUPERG
1293 if (vap->iv_flags & IEEE80211_F_FF)
1294 ni->ni_flags |= IEEE80211_NODE_FF;
1296 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1297 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1299 * Device is HT-capable and HT is enabled for
1300 * the vap; setup HT operation. On return
1301 * ni_chan will be adjusted to an HT channel.
1303 ieee80211_ht_wds_init(ni);
1305 struct ieee80211_channel *c = ni->ni_chan;
1307 * Force a legacy channel to be used.
1309 c = ieee80211_find_channel(ic,
1310 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1311 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1312 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1319 struct ieee80211_node *
1320 #ifdef IEEE80211_DEBUG_REFCNT
1321 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1322 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1324 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1325 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1328 struct ieee80211_node *ni;
1331 IEEE80211_NODE_LOCK_ASSERT(nt);
1333 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1334 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1335 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1336 ieee80211_ref_node(ni); /* mark referenced */
1337 #ifdef IEEE80211_DEBUG_REFCNT
1338 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1339 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1341 ni, ether_sprintf(ni->ni_macaddr),
1342 ieee80211_node_refcnt(ni));
1350 struct ieee80211_node *
1351 #ifdef IEEE80211_DEBUG_REFCNT
1352 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1353 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1355 ieee80211_find_node(struct ieee80211_node_table *nt,
1356 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1359 struct ieee80211_node *ni;
1361 IEEE80211_NODE_LOCK(nt);
1362 ni = ieee80211_find_node_locked(nt, macaddr);
1363 IEEE80211_NODE_UNLOCK(nt);
1367 struct ieee80211_node *
1368 #ifdef IEEE80211_DEBUG_REFCNT
1369 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1370 const struct ieee80211vap *vap,
1371 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1373 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1374 const struct ieee80211vap *vap,
1375 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1378 struct ieee80211_node *ni;
1381 IEEE80211_NODE_LOCK_ASSERT(nt);
1383 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1384 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1385 if (ni->ni_vap == vap &&
1386 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1387 ieee80211_ref_node(ni); /* mark referenced */
1388 #ifdef IEEE80211_DEBUG_REFCNT
1389 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1390 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1392 ni, ether_sprintf(ni->ni_macaddr),
1393 ieee80211_node_refcnt(ni));
1401 struct ieee80211_node *
1402 #ifdef IEEE80211_DEBUG_REFCNT
1403 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1404 const struct ieee80211vap *vap,
1405 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1407 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1408 const struct ieee80211vap *vap,
1409 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1412 struct ieee80211_node *ni;
1414 IEEE80211_NODE_LOCK(nt);
1415 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1416 IEEE80211_NODE_UNLOCK(nt);
1421 * Fake up a node; this handles node discovery in adhoc mode.
1422 * Note that for the driver's benefit we we treat this like
1423 * an association so the driver has an opportunity to setup
1424 * it's private state.
1426 struct ieee80211_node *
1427 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1428 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1430 struct ieee80211_node *ni;
1432 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1433 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1434 ni = ieee80211_dup_bss(vap, macaddr);
1436 struct ieee80211com *ic = vap->iv_ic;
1438 /* XXX no rate negotiation; just dup */
1439 ni->ni_rates = vap->iv_bss->ni_rates;
1440 if (ieee80211_iserp_rateset(&ni->ni_rates))
1441 ni->ni_flags |= IEEE80211_NODE_ERP;
1442 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1444 * In adhoc demo mode there are no management
1445 * frames to use to discover neighbor capabilities,
1446 * so blindly propagate the local configuration
1447 * so we can do interesting things (e.g. use
1448 * WME to disable ACK's).
1450 if (vap->iv_flags & IEEE80211_F_WME)
1451 ni->ni_flags |= IEEE80211_NODE_QOS;
1452 #ifdef IEEE80211_SUPPORT_SUPERG
1453 if (vap->iv_flags & IEEE80211_F_FF)
1454 ni->ni_flags |= IEEE80211_NODE_FF;
1457 ieee80211_node_setuptxparms(ni);
1458 ieee80211_ratectl_node_init(ni);
1459 if (ic->ic_newassoc != NULL)
1460 ic->ic_newassoc(ni, 1);
1461 /* XXX not right for 802.1x/WPA */
1462 ieee80211_node_authorize(ni);
1468 ieee80211_init_neighbor(struct ieee80211_node *ni,
1469 const struct ieee80211_frame *wh,
1470 const struct ieee80211_scanparams *sp)
1472 int do_ht_setup = 0;
1474 ni->ni_esslen = sp->ssid[1];
1475 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1476 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1477 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1478 ni->ni_intval = sp->bintval;
1479 ni->ni_capinfo = sp->capinfo;
1480 ni->ni_chan = ni->ni_ic->ic_curchan;
1481 ni->ni_fhdwell = sp->fhdwell;
1482 ni->ni_fhindex = sp->fhindex;
1483 ni->ni_erp = sp->erp;
1484 ni->ni_timoff = sp->timoff;
1485 #ifdef IEEE80211_SUPPORT_MESH
1486 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1487 ieee80211_mesh_init_neighbor(ni, wh, sp);
1489 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1490 ieee80211_ies_expand(&ni->ni_ies);
1491 if (ni->ni_ies.wme_ie != NULL)
1492 ni->ni_flags |= IEEE80211_NODE_QOS;
1494 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1495 #ifdef IEEE80211_SUPPORT_SUPERG
1496 if (ni->ni_ies.ath_ie != NULL)
1497 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1499 if (ni->ni_ies.htcap_ie != NULL)
1500 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1501 if (ni->ni_ies.htinfo_ie != NULL)
1502 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1504 if ((ni->ni_ies.htcap_ie != NULL) &&
1505 (ni->ni_ies.htinfo_ie != NULL) &&
1506 (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1511 /* NB: must be after ni_chan is setup */
1512 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1513 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1514 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1517 * If the neighbor is HT compatible, flip that on.
1520 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1521 "%s: doing HT setup\n", __func__);
1522 ieee80211_ht_node_init(ni);
1523 ieee80211_ht_updateparams(ni,
1524 ni->ni_ies.htcap_ie,
1525 ni->ni_ies.htinfo_ie);
1526 ieee80211_setup_htrates(ni,
1527 ni->ni_ies.htcap_ie,
1528 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1529 ieee80211_setup_basic_htrates(ni,
1530 ni->ni_ies.htinfo_ie);
1531 ieee80211_node_setuptxparms(ni);
1532 ieee80211_ratectl_node_init(ni);
1537 * Do node discovery in adhoc mode on receipt of a beacon
1538 * or probe response frame. Note that for the driver's
1539 * benefit we we treat this like an association so the
1540 * driver has an opportunity to setup it's private state.
1542 struct ieee80211_node *
1543 ieee80211_add_neighbor(struct ieee80211vap *vap,
1544 const struct ieee80211_frame *wh,
1545 const struct ieee80211_scanparams *sp)
1547 struct ieee80211_node *ni;
1549 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1550 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1551 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1553 struct ieee80211com *ic = vap->iv_ic;
1555 ieee80211_init_neighbor(ni, wh, sp);
1556 if (ieee80211_iserp_rateset(&ni->ni_rates))
1557 ni->ni_flags |= IEEE80211_NODE_ERP;
1558 ieee80211_node_setuptxparms(ni);
1559 ieee80211_ratectl_node_init(ni);
1560 if (ic->ic_newassoc != NULL)
1561 ic->ic_newassoc(ni, 1);
1562 /* XXX not right for 802.1x/WPA */
1563 ieee80211_node_authorize(ni);
1568 #define IS_PROBEREQ(wh) \
1569 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1570 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1571 #define IS_BCAST_PROBEREQ(wh) \
1572 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1573 ((const struct ieee80211_frame *)(wh))->i_addr3))
1575 static __inline struct ieee80211_node *
1576 _find_rxnode(struct ieee80211_node_table *nt,
1577 const struct ieee80211_frame_min *wh)
1579 if (IS_BCAST_PROBEREQ(wh))
1580 return NULL; /* spam bcast probe req to all vap's */
1581 return ieee80211_find_node_locked(nt, wh->i_addr2);
1585 * Locate the node for sender, track state, and then pass the
1586 * (referenced) node up to the 802.11 layer for its use. Note
1587 * we can return NULL if the sender is not in the table.
1589 struct ieee80211_node *
1590 #ifdef IEEE80211_DEBUG_REFCNT
1591 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1592 const struct ieee80211_frame_min *wh, const char *func, int line)
1594 ieee80211_find_rxnode(struct ieee80211com *ic,
1595 const struct ieee80211_frame_min *wh)
1598 struct ieee80211_node_table *nt;
1599 struct ieee80211_node *ni;
1602 IEEE80211_NODE_LOCK(nt);
1603 ni = _find_rxnode(nt, wh);
1604 IEEE80211_NODE_UNLOCK(nt);
1610 * Like ieee80211_find_rxnode but use the supplied h/w
1611 * key index as a hint to locate the node in the key
1612 * mapping table. If an entry is present at the key
1613 * index we return it; otherwise do a normal lookup and
1614 * update the mapping table if the station has a unicast
1615 * key assigned to it.
1617 struct ieee80211_node *
1618 #ifdef IEEE80211_DEBUG_REFCNT
1619 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1620 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1621 const char *func, int line)
1623 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1624 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1627 struct ieee80211_node_table *nt;
1628 struct ieee80211_node *ni;
1631 IEEE80211_NODE_LOCK(nt);
1632 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1633 ni = nt->nt_keyixmap[keyix];
1637 ni = _find_rxnode(nt, wh);
1638 if (ni != NULL && nt->nt_keyixmap != NULL) {
1640 * If the station has a unicast key cache slot
1641 * assigned update the key->node mapping table.
1643 keyix = ni->ni_ucastkey.wk_rxkeyix;
1644 /* XXX can keyixmap[keyix] != NULL? */
1645 if (keyix < nt->nt_keyixmax &&
1646 nt->nt_keyixmap[keyix] == NULL) {
1647 IEEE80211_DPRINTF(ni->ni_vap,
1649 "%s: add key map entry %p<%s> refcnt %d\n",
1650 __func__, ni, ether_sprintf(ni->ni_macaddr),
1651 ieee80211_node_refcnt(ni)+1);
1652 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1656 if (IS_BCAST_PROBEREQ(wh))
1657 ni = NULL; /* spam bcast probe req to all vap's */
1659 ieee80211_ref_node(ni);
1661 IEEE80211_NODE_UNLOCK(nt);
1665 #undef IS_BCAST_PROBEREQ
1669 * Return a reference to the appropriate node for sending
1670 * a data frame. This handles node discovery in adhoc networks.
1672 struct ieee80211_node *
1673 #ifdef IEEE80211_DEBUG_REFCNT
1674 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1675 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1676 const char *func, int line)
1678 ieee80211_find_txnode(struct ieee80211vap *vap,
1679 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1682 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1683 struct ieee80211_node *ni;
1686 * The destination address should be in the node table
1687 * unless this is a multicast/broadcast frame. We can
1688 * also optimize station mode operation, all frames go
1691 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1692 IEEE80211_NODE_LOCK(nt);
1693 if (vap->iv_opmode == IEEE80211_M_STA ||
1694 vap->iv_opmode == IEEE80211_M_WDS ||
1695 IEEE80211_IS_MULTICAST(macaddr))
1696 ni = ieee80211_ref_node(vap->iv_bss);
1698 ni = ieee80211_find_node_locked(nt, macaddr);
1699 IEEE80211_NODE_UNLOCK(nt);
1702 if (vap->iv_opmode == IEEE80211_M_IBSS ||
1703 vap->iv_opmode == IEEE80211_M_AHDEMO) {
1705 * In adhoc mode cons up a node for the destination.
1706 * Note that we need an additional reference for the
1707 * caller to be consistent with
1708 * ieee80211_find_node_locked.
1710 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1712 (void) ieee80211_ref_node(ni);
1714 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1715 "no node, discard frame (%s)", __func__);
1716 vap->iv_stats.is_tx_nonode++;
1723 _ieee80211_free_node(struct ieee80211_node *ni)
1725 struct ieee80211_node_table *nt = ni->ni_table;
1728 * NB: careful about referencing the vap as it may be
1729 * gone if the last reference was held by a driver.
1730 * We know the com will always be present so it's safe
1731 * to use ni_ic below to reclaim resources.
1734 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1735 "%s %p<%s> in %s table\n", __func__, ni,
1736 ether_sprintf(ni->ni_macaddr),
1737 nt != NULL ? nt->nt_name : "<gone>");
1739 if (ni->ni_associd != 0) {
1740 struct ieee80211vap *vap = ni->ni_vap;
1741 if (vap->iv_aid_bitmap != NULL)
1742 IEEE80211_AID_CLR(vap, ni->ni_associd);
1745 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1746 LIST_REMOVE(ni, ni_hash);
1748 ni->ni_ic->ic_node_free(ni);
1752 #ifdef IEEE80211_DEBUG_REFCNT
1753 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1755 ieee80211_free_node(struct ieee80211_node *ni)
1758 struct ieee80211_node_table *nt = ni->ni_table;
1760 #ifdef IEEE80211_DEBUG_REFCNT
1761 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1762 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1763 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1766 IEEE80211_NODE_LOCK(nt);
1767 if (ieee80211_node_dectestref(ni)) {
1769 * Last reference, reclaim state.
1771 _ieee80211_free_node(ni);
1772 } else if (ieee80211_node_refcnt(ni) == 1 &&
1773 nt->nt_keyixmap != NULL) {
1774 ieee80211_keyix keyix;
1776 * Check for a last reference in the key mapping table.
1778 keyix = ni->ni_ucastkey.wk_rxkeyix;
1779 if (keyix < nt->nt_keyixmax &&
1780 nt->nt_keyixmap[keyix] == ni) {
1781 IEEE80211_DPRINTF(ni->ni_vap,
1783 "%s: %p<%s> clear key map entry", __func__,
1784 ni, ether_sprintf(ni->ni_macaddr));
1785 nt->nt_keyixmap[keyix] = NULL;
1786 ieee80211_node_decref(ni); /* XXX needed? */
1787 _ieee80211_free_node(ni);
1790 IEEE80211_NODE_UNLOCK(nt);
1792 if (ieee80211_node_dectestref(ni))
1793 _ieee80211_free_node(ni);
1798 * Reclaim a unicast key and clear any key cache state.
1801 ieee80211_node_delucastkey(struct ieee80211_node *ni)
1803 struct ieee80211com *ic = ni->ni_ic;
1804 struct ieee80211_node_table *nt = &ic->ic_sta;
1805 struct ieee80211_node *nikey;
1806 ieee80211_keyix keyix;
1807 int isowned, status;
1810 * NB: We must beware of LOR here; deleting the key
1811 * can cause the crypto layer to block traffic updates
1812 * which can generate a LOR against the node table lock;
1813 * grab it here and stash the key index for our use below.
1815 * Must also beware of recursion on the node table lock.
1816 * When called from node_cleanup we may already have
1817 * the node table lock held. Unfortunately there's no
1818 * way to separate out this path so we must do this
1821 isowned = IEEE80211_NODE_IS_LOCKED(nt);
1823 IEEE80211_NODE_LOCK(nt);
1825 status = 1; /* NB: success */
1826 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1827 keyix = ni->ni_ucastkey.wk_rxkeyix;
1828 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1829 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1830 nikey = nt->nt_keyixmap[keyix];
1831 nt->nt_keyixmap[keyix] = NULL;
1835 IEEE80211_NODE_UNLOCK(nt);
1837 if (nikey != NULL) {
1838 KASSERT(nikey == ni,
1839 ("key map out of sync, ni %p nikey %p", ni, nikey));
1840 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1841 "%s: delete key map entry %p<%s> refcnt %d\n",
1842 __func__, ni, ether_sprintf(ni->ni_macaddr),
1843 ieee80211_node_refcnt(ni)-1);
1844 ieee80211_free_node(ni);
1850 * Reclaim a node. If this is the last reference count then
1851 * do the normal free work. Otherwise remove it from the node
1852 * table and mark it gone by clearing the back-reference.
1855 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1857 ieee80211_keyix keyix;
1859 IEEE80211_NODE_LOCK_ASSERT(nt);
1861 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1862 "%s: remove %p<%s> from %s table, refcnt %d\n",
1863 __func__, ni, ether_sprintf(ni->ni_macaddr),
1864 nt->nt_name, ieee80211_node_refcnt(ni)-1);
1866 * Clear any entry in the unicast key mapping table.
1867 * We need to do it here so rx lookups don't find it
1868 * in the mapping table even if it's not in the hash
1869 * table. We cannot depend on the mapping table entry
1870 * being cleared because the node may not be free'd.
1872 keyix = ni->ni_ucastkey.wk_rxkeyix;
1873 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1874 nt->nt_keyixmap[keyix] == ni) {
1875 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1876 "%s: %p<%s> clear key map entry %u\n",
1877 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1878 nt->nt_keyixmap[keyix] = NULL;
1879 ieee80211_node_decref(ni); /* NB: don't need free */
1881 if (!ieee80211_node_dectestref(ni)) {
1883 * Other references are present, just remove the
1884 * node from the table so it cannot be found. When
1885 * the references are dropped storage will be
1888 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1889 LIST_REMOVE(ni, ni_hash);
1890 ni->ni_table = NULL; /* clear reference */
1892 _ieee80211_free_node(ni);
1896 * Node table support.
1900 ieee80211_node_table_init(struct ieee80211com *ic,
1901 struct ieee80211_node_table *nt,
1902 const char *name, int inact, int keyixmax)
1904 struct ifnet *ifp = ic->ic_ifp;
1907 IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1908 IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1909 TAILQ_INIT(&nt->nt_node);
1912 nt->nt_inact_init = inact;
1913 nt->nt_keyixmax = keyixmax;
1914 if (nt->nt_keyixmax > 0) {
1915 nt->nt_keyixmap = (struct ieee80211_node **) malloc(
1916 keyixmax * sizeof(struct ieee80211_node *),
1917 M_80211_NODE, M_NOWAIT | M_ZERO);
1918 if (nt->nt_keyixmap == NULL)
1919 if_printf(ic->ic_ifp,
1920 "Cannot allocate key index map with %u entries\n",
1923 nt->nt_keyixmap = NULL;
1927 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
1928 struct ieee80211vap *match)
1930 struct ieee80211_node *ni, *next;
1932 IEEE80211_NODE_LOCK(nt);
1933 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1934 if (match != NULL && ni->ni_vap != match)
1936 /* XXX can this happen? if so need's work */
1937 if (ni->ni_associd != 0) {
1938 struct ieee80211vap *vap = ni->ni_vap;
1940 if (vap->iv_auth->ia_node_leave != NULL)
1941 vap->iv_auth->ia_node_leave(ni);
1942 if (vap->iv_aid_bitmap != NULL)
1943 IEEE80211_AID_CLR(vap, ni->ni_associd);
1945 ni->ni_wdsvap = NULL; /* clear reference */
1946 node_reclaim(nt, ni);
1948 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1950 * Make a separate pass to clear references to this vap
1951 * held by DWDS entries. They will not be matched above
1952 * because ni_vap will point to the ap vap but we still
1953 * need to clear ni_wdsvap when the WDS vap is destroyed
1956 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1957 if (ni->ni_wdsvap == match)
1958 ni->ni_wdsvap = NULL;
1960 IEEE80211_NODE_UNLOCK(nt);
1964 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1966 ieee80211_node_table_reset(nt, NULL);
1967 if (nt->nt_keyixmap != NULL) {
1969 /* XXX verify all entries are NULL */
1971 for (i = 0; i < nt->nt_keyixmax; i++)
1972 if (nt->nt_keyixmap[i] != NULL)
1973 printf("%s: %s[%u] still active\n", __func__,
1976 free(nt->nt_keyixmap, M_80211_NODE);
1977 nt->nt_keyixmap = NULL;
1979 IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1980 IEEE80211_NODE_LOCK_DESTROY(nt);
1984 * Timeout inactive stations and do related housekeeping.
1985 * Note that we cannot hold the node lock while sending a
1986 * frame as this would lead to a LOR. Instead we use a
1987 * generation number to mark nodes that we've scanned and
1988 * drop the lock and restart a scan if we have to time out
1989 * a node. Since we are single-threaded by virtue of
1990 * controlling the inactivity timer we can be sure this will
1991 * process each node only once.
1994 ieee80211_timeout_stations(struct ieee80211com *ic)
1996 struct ieee80211_node_table *nt = &ic->ic_sta;
1997 struct ieee80211vap *vap;
1998 struct ieee80211_node *ni;
2001 IEEE80211_NODE_ITERATE_LOCK(nt);
2002 gen = ++nt->nt_scangen;
2004 IEEE80211_NODE_LOCK(nt);
2005 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2006 if (ni->ni_scangen == gen) /* previously handled */
2008 ni->ni_scangen = gen;
2010 * Ignore entries for which have yet to receive an
2011 * authentication frame. These are transient and
2012 * will be reclaimed when the last reference to them
2013 * goes away (when frame xmits complete).
2017 * Only process stations when in RUN state. This
2018 * insures, for example, that we don't timeout an
2019 * inactive station during CAC. Note that CSA state
2020 * is actually handled in ieee80211_node_timeout as
2021 * it applies to more than timeout processing.
2023 if (vap->iv_state != IEEE80211_S_RUN)
2025 /* XXX can vap be NULL? */
2026 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2027 vap->iv_opmode == IEEE80211_M_STA) &&
2028 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2031 * Free fragment if not needed anymore
2032 * (last fragment older than 1s).
2033 * XXX doesn't belong here, move to node_age
2035 if (ni->ni_rxfrag[0] != NULL &&
2036 ticks > ni->ni_rxfragstamp + hz) {
2037 m_freem(ni->ni_rxfrag[0]);
2038 ni->ni_rxfrag[0] = NULL;
2040 if (ni->ni_inact > 0) {
2042 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2043 "%s: inact %u inact_reload %u nrates %u",
2044 __func__, ni->ni_inact, ni->ni_inact_reload,
2045 ni->ni_rates.rs_nrates);
2048 * Special case ourself; we may be idle for extended periods
2049 * of time and regardless reclaiming our state is wrong.
2050 * XXX run ic_node_age
2052 if (ni == vap->iv_bss)
2054 if (ni->ni_associd != 0 ||
2055 (vap->iv_opmode == IEEE80211_M_IBSS ||
2056 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2058 * Age/drain resources held by the station.
2060 ic->ic_node_age(ni);
2062 * Probe the station before time it out. We
2063 * send a null data frame which may not be
2064 * universally supported by drivers (need it
2065 * for ps-poll support so it should be...).
2067 * XXX don't probe the station unless we've
2068 * received a frame from them (and have
2069 * some idea of the rates they are capable
2070 * of); this will get fixed more properly
2071 * soon with better handling of the rate set.
2073 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2074 (0 < ni->ni_inact &&
2075 ni->ni_inact <= vap->iv_inact_probe) &&
2076 ni->ni_rates.rs_nrates != 0) {
2078 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2080 "probe station due to inactivity");
2082 * Grab a reference before unlocking the table
2083 * so the node cannot be reclaimed before we
2084 * send the frame. ieee80211_send_nulldata
2085 * understands we've done this and reclaims the
2086 * ref for us as needed.
2088 ieee80211_ref_node(ni);
2089 IEEE80211_NODE_UNLOCK(nt);
2090 ieee80211_send_nulldata(ni);
2095 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2096 ni->ni_inact <= 0) {
2098 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2099 "station timed out due to inactivity "
2100 "(refcnt %u)", ieee80211_node_refcnt(ni));
2102 * Send a deauthenticate frame and drop the station.
2103 * This is somewhat complicated due to reference counts
2104 * and locking. At this point a station will typically
2105 * have a reference count of 1. ieee80211_node_leave
2106 * will do a "free" of the node which will drop the
2107 * reference count. But in the meantime a reference
2108 * wil be held by the deauth frame. The actual reclaim
2109 * of the node will happen either after the tx is
2110 * completed or by ieee80211_node_leave.
2112 * Separately we must drop the node lock before sending
2113 * in case the driver takes a lock, as this can result
2114 * in a LOR between the node lock and the driver lock.
2116 ieee80211_ref_node(ni);
2117 IEEE80211_NODE_UNLOCK(nt);
2118 if (ni->ni_associd != 0) {
2119 IEEE80211_SEND_MGMT(ni,
2120 IEEE80211_FC0_SUBTYPE_DEAUTH,
2121 IEEE80211_REASON_AUTH_EXPIRE);
2123 ieee80211_node_leave(ni);
2124 ieee80211_free_node(ni);
2125 vap->iv_stats.is_node_timeout++;
2129 IEEE80211_NODE_UNLOCK(nt);
2131 IEEE80211_NODE_ITERATE_UNLOCK(nt);
2135 * Aggressively reclaim resources. This should be used
2136 * only in a critical situation to reclaim mbuf resources.
2139 ieee80211_drain(struct ieee80211com *ic)
2141 struct ieee80211_node_table *nt = &ic->ic_sta;
2142 struct ieee80211vap *vap;
2143 struct ieee80211_node *ni;
2145 IEEE80211_NODE_LOCK(nt);
2146 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2148 * Ignore entries for which have yet to receive an
2149 * authentication frame. These are transient and
2150 * will be reclaimed when the last reference to them
2151 * goes away (when frame xmits complete).
2155 * Only process stations when in RUN state. This
2156 * insures, for example, that we don't timeout an
2157 * inactive station during CAC. Note that CSA state
2158 * is actually handled in ieee80211_node_timeout as
2159 * it applies to more than timeout processing.
2161 if (vap->iv_state != IEEE80211_S_RUN)
2163 /* XXX can vap be NULL? */
2164 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2165 vap->iv_opmode == IEEE80211_M_STA) &&
2166 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2170 * XXX doesn't belong here, move to node_drain
2172 if (ni->ni_rxfrag[0] != NULL) {
2173 m_freem(ni->ni_rxfrag[0]);
2174 ni->ni_rxfrag[0] = NULL;
2177 * Drain resources held by the station.
2179 ic->ic_node_drain(ni);
2181 IEEE80211_NODE_UNLOCK(nt);
2185 * Per-ieee80211com inactivity timer callback.
2188 ieee80211_node_timeout(void *arg)
2190 struct ieee80211com *ic = arg;
2193 * Defer timeout processing if a channel switch is pending.
2194 * We typically need to be mute so not doing things that
2195 * might generate frames is good to handle in one place.
2196 * Supressing the station timeout processing may extend the
2197 * lifetime of inactive stations (by not decrementing their
2198 * idle counters) but this should be ok unless the CSA is
2199 * active for an unusually long time.
2201 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2202 ieee80211_scan_timeout(ic);
2203 ieee80211_timeout_stations(ic);
2204 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2207 ieee80211_erp_timeout(ic);
2208 ieee80211_ht_timeout(ic);
2209 IEEE80211_UNLOCK(ic);
2211 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2212 ieee80211_node_timeout, ic);
2216 * Iterate over the node table and return an array of ref'ed nodes.
2218 * This is separated out from calling the actual node function so that
2219 * no LORs will occur.
2221 * If there are too many nodes (ie, the number of nodes doesn't fit
2222 * within 'max_aid' entries) then the node references will be freed
2223 * and an error will be returned.
2225 * The responsibility of allocating and freeing "ni_arr" is up to
2229 ieee80211_iterate_nt(struct ieee80211_node_table *nt,
2230 struct ieee80211_node **ni_arr, uint16_t max_aid)
2234 struct ieee80211_node *ni;
2236 IEEE80211_NODE_ITERATE_LOCK(nt);
2237 IEEE80211_NODE_LOCK(nt);
2239 gen = ++nt->nt_scangen;
2243 * We simply assume here that since the node
2244 * scan generation doesn't change (as
2245 * we are holding both the node table and
2246 * node table iteration locks), we can simply
2247 * assign it to the node here.
2249 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2252 if_printf(nt->nt_ic->ic_ifp,
2253 "Node array overflow: max=%u", max_aid);
2256 ni_arr[i] = ieee80211_ref_node(ni);
2257 ni_arr[i]->ni_scangen = gen;
2262 * It's safe to unlock here.
2264 * If we're successful, the list is returned.
2265 * If we're unsuccessful, the list is ignored
2266 * and we remove our references.
2268 * This avoids any potential LOR with
2269 * ieee80211_free_node().
2271 IEEE80211_NODE_UNLOCK(nt);
2272 IEEE80211_NODE_ITERATE_UNLOCK(nt);
2275 * If ret is non-zero, we hit some kind of error.
2276 * Rather than walking some nodes, we'll walk none
2280 for (j = 0; j < i; j++) {
2281 /* ieee80211_free_node() locks by itself */
2282 ieee80211_free_node(ni_arr[j]);
2290 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2291 * reference in the source.
2293 * Note that this fetches 'max_aid' from the first VAP, rather than finding
2294 * the largest max_aid from all VAPs.
2297 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2298 ieee80211_iter_func *f, void *arg)
2300 struct ieee80211_node **ni_arr;
2304 struct ieee80211vap *vap;
2306 /* Overdoing it default */
2307 max_aid = IEEE80211_AID_MAX;
2309 /* Handle the case of there being no vaps just yet */
2310 vap = TAILQ_FIRST(&nt->nt_ic->ic_vaps);
2312 max_aid = vap->iv_max_aid;
2314 size = max_aid * sizeof(struct ieee80211_node *);
2315 ni_arr = (struct ieee80211_node **) malloc(size, M_80211_NODE,
2321 * If this fails, the node table won't have any
2322 * valid entries - ieee80211_iterate_nt() frees
2323 * the references to them. So don't try walking
2324 * the table; just skip to the end and free the
2327 if (ieee80211_iterate_nt(nt, ni_arr, max_aid) != 0)
2330 for (i = 0; i < max_aid; i++) {
2331 if (ni_arr[i] == NULL) /* end of the list */
2333 (*f)(arg, ni_arr[i]);
2334 /* ieee80211_free_node() locks by itself */
2335 ieee80211_free_node(ni_arr[i]);
2339 free(ni_arr, M_80211_NODE);
2343 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2345 printf("0x%p: mac %s refcnt %d\n", ni,
2346 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2347 printf("\tscangen %u authmode %u flags 0x%x\n",
2348 ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2349 printf("\tassocid 0x%x txpower %u vlan %u\n",
2350 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2351 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2352 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2353 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2354 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2355 ni->ni_rxfragstamp);
2356 printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2357 node_getrssi(ni), ni->ni_noise,
2358 ni->ni_intval, ni->ni_capinfo);
2359 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2360 ether_sprintf(ni->ni_bssid),
2361 ni->ni_esslen, ni->ni_essid,
2362 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2363 printf("\tinact %u inact_reload %u txrate %u\n",
2364 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2365 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2366 ni->ni_htcap, ni->ni_htparam,
2367 ni->ni_htctlchan, ni->ni_ht2ndchan);
2368 printf("\thtopmode %x htstbc %x chw %u\n",
2369 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2373 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2375 ieee80211_iterate_nodes(nt,
2376 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2380 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2382 struct ieee80211vap *vap;
2384 IEEE80211_LOCK_ASSERT(ic);
2386 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2387 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2388 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2392 ieee80211_notify_erp(struct ieee80211com *ic)
2395 ieee80211_notify_erp_locked(ic);
2396 IEEE80211_UNLOCK(ic);
2400 * Handle a station joining an 11g network.
2403 ieee80211_node_join_11g(struct ieee80211_node *ni)
2405 struct ieee80211com *ic = ni->ni_ic;
2407 IEEE80211_LOCK_ASSERT(ic);
2410 * Station isn't capable of short slot time. Bump
2411 * the count of long slot time stations and disable
2412 * use of short slot time. Note that the actual switch
2413 * over to long slot time use may not occur until the
2414 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2416 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2417 ic->ic_longslotsta++;
2418 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2419 "station needs long slot time, count %d",
2420 ic->ic_longslotsta);
2421 /* XXX vap's w/ conflicting needs won't work */
2422 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2424 * Don't force slot time when switched to turbo
2425 * mode as non-ERP stations won't be present; this
2426 * need only be done when on the normal G channel.
2428 ieee80211_set_shortslottime(ic, 0);
2432 * If the new station is not an ERP station
2433 * then bump the counter and enable protection
2436 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2438 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2439 "station is !ERP, %d non-ERP stations associated",
2442 * If station does not support short preamble
2443 * then we must enable use of Barker preamble.
2445 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2446 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2447 "%s", "station needs long preamble");
2448 ic->ic_flags |= IEEE80211_F_USEBARKER;
2449 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2452 * If protection is configured and this is the first
2453 * indication we should use protection, enable it.
2455 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2456 ic->ic_nonerpsta == 1 &&
2457 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2458 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2459 "%s: enable use of protection\n", __func__);
2460 ic->ic_flags |= IEEE80211_F_USEPROT;
2461 ieee80211_notify_erp_locked(ic);
2464 ni->ni_flags |= IEEE80211_NODE_ERP;
2468 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2470 struct ieee80211com *ic = ni->ni_ic;
2471 struct ieee80211vap *vap = ni->ni_vap;
2474 if (ni->ni_associd == 0) {
2477 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2479 * It would be good to search the bitmap
2480 * more efficiently, but this will do for now.
2482 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2483 if (!IEEE80211_AID_ISSET(vap, aid))
2486 if (aid >= vap->iv_max_aid) {
2487 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2488 ieee80211_node_leave(ni);
2491 ni->ni_associd = aid | 0xc000;
2492 ni->ni_jointime = time_uptime;
2494 IEEE80211_AID_SET(vap, ni->ni_associd);
2495 vap->iv_sta_assoc++;
2498 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2499 ieee80211_ht_node_join(ni);
2500 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2501 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2502 ieee80211_node_join_11g(ni);
2503 IEEE80211_UNLOCK(ic);
2509 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2510 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2511 IEEE80211_NODE_AID(ni),
2512 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2513 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2514 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2515 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2516 ni->ni_flags & IEEE80211_NODE_HT ?
2517 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2518 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2519 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2520 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2521 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2522 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2523 ", fast-frames" : "",
2524 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2528 ieee80211_node_setuptxparms(ni);
2529 ieee80211_ratectl_node_init(ni);
2530 /* give driver a chance to setup state like ni_txrate */
2531 if (ic->ic_newassoc != NULL)
2532 ic->ic_newassoc(ni, newassoc);
2533 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2534 /* tell the authenticator about new station */
2535 if (vap->iv_auth->ia_node_join != NULL)
2536 vap->iv_auth->ia_node_join(ni);
2537 ieee80211_notify_node_join(ni,
2538 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2542 disable_protection(struct ieee80211com *ic)
2544 KASSERT(ic->ic_nonerpsta == 0 &&
2545 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2546 ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2549 ic->ic_flags &= ~IEEE80211_F_USEPROT;
2550 /* XXX verify mode? */
2551 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2552 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2553 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2555 ieee80211_notify_erp_locked(ic);
2559 * Handle a station leaving an 11g network.
2562 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2564 struct ieee80211com *ic = ni->ni_ic;
2566 IEEE80211_LOCK_ASSERT(ic);
2568 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2569 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2570 ic->ic_bsschan->ic_flags));
2573 * If a long slot station do the slot time bookkeeping.
2575 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2576 KASSERT(ic->ic_longslotsta > 0,
2577 ("bogus long slot station count %d", ic->ic_longslotsta));
2578 ic->ic_longslotsta--;
2579 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2580 "long slot time station leaves, count now %d",
2581 ic->ic_longslotsta);
2582 if (ic->ic_longslotsta == 0) {
2584 * Re-enable use of short slot time if supported
2585 * and not operating in IBSS mode (per spec).
2587 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2588 ic->ic_opmode != IEEE80211_M_IBSS) {
2589 IEEE80211_DPRINTF(ni->ni_vap,
2590 IEEE80211_MSG_ASSOC,
2591 "%s: re-enable use of short slot time\n",
2593 ieee80211_set_shortslottime(ic, 1);
2598 * If a non-ERP station do the protection-related bookkeeping.
2600 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2601 KASSERT(ic->ic_nonerpsta > 0,
2602 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2604 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2605 "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2606 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2607 " (non-ERP sta present)" : "");
2608 if (ic->ic_nonerpsta == 0 &&
2609 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2610 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2611 "%s: disable use of protection\n", __func__);
2612 disable_protection(ic);
2618 * Time out presence of an overlapping bss with non-ERP
2619 * stations. When operating in hostap mode we listen for
2620 * beacons from other stations and if we identify a non-ERP
2621 * station is present we enable protection. To identify
2622 * when all non-ERP stations are gone we time out this
2626 ieee80211_erp_timeout(struct ieee80211com *ic)
2629 IEEE80211_LOCK_ASSERT(ic);
2631 if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2632 time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2634 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2635 "%s", "age out non-ERP sta present on channel");
2637 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2638 if (ic->ic_nonerpsta == 0)
2639 disable_protection(ic);
2644 * Handle bookkeeping for station deauthentication/disassociation
2645 * when operating as an ap.
2648 ieee80211_node_leave(struct ieee80211_node *ni)
2650 struct ieee80211com *ic = ni->ni_ic;
2651 struct ieee80211vap *vap = ni->ni_vap;
2652 struct ieee80211_node_table *nt = ni->ni_table;
2654 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2655 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2657 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2658 ("unexpected operating mode %u", vap->iv_opmode));
2660 * If node wasn't previously associated all
2661 * we need to do is reclaim the reference.
2663 /* XXX ibss mode bypasses 11g and notification */
2664 if (ni->ni_associd == 0)
2667 * Tell the authenticator the station is leaving.
2668 * Note that we must do this before yanking the
2669 * association id as the authenticator uses the
2670 * associd to locate it's state block.
2672 if (vap->iv_auth->ia_node_leave != NULL)
2673 vap->iv_auth->ia_node_leave(ni);
2676 IEEE80211_AID_CLR(vap, ni->ni_associd);
2678 vap->iv_sta_assoc--;
2681 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2682 ieee80211_ht_node_leave(ni);
2683 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2684 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2685 ieee80211_node_leave_11g(ni);
2686 IEEE80211_UNLOCK(ic);
2688 * Cleanup station state. In particular clear various
2689 * state that might otherwise be reused if the node
2690 * is reused before the reference count goes to zero
2691 * (and memory is reclaimed).
2693 ieee80211_sta_leave(ni);
2696 * Remove the node from any table it's recorded in and
2697 * drop the caller's reference. Removal from the table
2698 * is important to insure the node is not reprocessed
2702 IEEE80211_NODE_LOCK(nt);
2703 node_reclaim(nt, ni);
2704 IEEE80211_NODE_UNLOCK(nt);
2706 ieee80211_free_node(ni);
2710 struct ieee80211vap *vap;
2712 uint32_t rssi_total;
2716 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2718 struct rssiinfo *info = arg;
2719 struct ieee80211vap *vap = ni->ni_vap;
2722 if (info->vap != vap)
2724 /* only associated stations */
2725 if (ni->ni_associd == 0)
2727 rssi = vap->iv_ic->ic_node_getrssi(ni);
2729 info->rssi_samples++;
2730 info->rssi_total += rssi;
2735 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2737 struct rssiinfo *info = arg;
2738 struct ieee80211vap *vap = ni->ni_vap;
2741 if (info->vap != vap)
2743 /* only neighbors */
2744 /* XXX check bssid */
2745 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2747 rssi = vap->iv_ic->ic_node_getrssi(ni);
2749 info->rssi_samples++;
2750 info->rssi_total += rssi;
2754 #ifdef IEEE80211_SUPPORT_MESH
2756 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
2758 struct rssiinfo *info = arg;
2759 struct ieee80211vap *vap = ni->ni_vap;
2762 if (info->vap != vap)
2764 /* only neighbors that peered successfully */
2765 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
2767 rssi = vap->iv_ic->ic_node_getrssi(ni);
2769 info->rssi_samples++;
2770 info->rssi_total += rssi;
2773 #endif /* IEEE80211_SUPPORT_MESH */
2776 ieee80211_getrssi(struct ieee80211vap *vap)
2778 #define NZ(x) ((x) == 0 ? 1 : (x))
2779 struct ieee80211com *ic = vap->iv_ic;
2780 struct rssiinfo info;
2782 info.rssi_total = 0;
2783 info.rssi_samples = 0;
2785 switch (vap->iv_opmode) {
2786 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
2787 case IEEE80211_M_AHDEMO: /* average of all neighbors */
2788 ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2790 case IEEE80211_M_HOSTAP: /* average of all associated stations */
2791 ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2793 #ifdef IEEE80211_SUPPORT_MESH
2794 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
2795 ieee80211_iterate_nodes(&ic->ic_sta, get_mesh_rssi, &info);
2798 case IEEE80211_M_MONITOR: /* XXX */
2799 case IEEE80211_M_STA: /* use stats from associated ap */
2801 if (vap->iv_bss != NULL)
2802 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2803 info.rssi_samples = 1;
2806 return info.rssi_total / NZ(info.rssi_samples);
2811 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2814 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
2816 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2817 /* for non-station mode return avg'd rssi accounting */
2818 if (vap->iv_opmode != IEEE80211_M_STA)
2819 *rssi = ieee80211_getrssi(vap);