2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
38 #include <sys/socket.h>
41 #include <net/if_var.h>
42 #include <net/if_media.h>
43 #include <net/ethernet.h>
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_input.h>
47 #ifdef IEEE80211_SUPPORT_SUPERG
48 #include <net80211/ieee80211_superg.h>
50 #ifdef IEEE80211_SUPPORT_TDMA
51 #include <net80211/ieee80211_tdma.h>
53 #include <net80211/ieee80211_wds.h>
54 #include <net80211/ieee80211_mesh.h>
55 #include <net80211/ieee80211_ratectl.h>
60 * IEEE80211_NODE_HASHSIZE must be a power of 2.
62 CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
65 * Association id's are managed with a bit vector.
67 #define IEEE80211_AID_SET(_vap, b) \
68 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
69 (1 << (IEEE80211_AID(b) % 32)))
70 #define IEEE80211_AID_CLR(_vap, b) \
71 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
72 ~(1 << (IEEE80211_AID(b) % 32)))
73 #define IEEE80211_AID_ISSET(_vap, b) \
74 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
76 static int ieee80211_sta_join1(struct ieee80211_node *);
78 static struct ieee80211_node *node_alloc(struct ieee80211vap *,
79 const uint8_t [IEEE80211_ADDR_LEN]);
80 static void node_cleanup(struct ieee80211_node *);
81 static void node_free(struct ieee80211_node *);
82 static void node_age(struct ieee80211_node *);
83 static int8_t node_getrssi(const struct ieee80211_node *);
84 static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
85 static void node_getmimoinfo(const struct ieee80211_node *,
86 struct ieee80211_mimo_info *);
88 static void _ieee80211_free_node(struct ieee80211_node *);
90 static void node_reclaim(struct ieee80211_node_table *nt,
91 struct ieee80211_node *ni);
92 static void ieee80211_node_table_init(struct ieee80211com *ic,
93 struct ieee80211_node_table *nt, const char *name,
94 int inact, int keymaxix);
95 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
96 struct ieee80211vap *);
97 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
98 static void ieee80211_erp_timeout(struct ieee80211com *);
100 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
101 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
104 ieee80211_node_attach(struct ieee80211com *ic)
106 /* XXX really want maxlen enforced per-sta */
107 ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
109 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
110 IEEE80211_INACT_INIT, ic->ic_max_keyix);
111 callout_init(&ic->ic_inact, 1);
112 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
113 ieee80211_node_timeout, ic);
115 ic->ic_node_alloc = node_alloc;
116 ic->ic_node_free = node_free;
117 ic->ic_node_cleanup = node_cleanup;
118 ic->ic_node_age = node_age;
119 ic->ic_node_drain = node_age; /* NB: same as age */
120 ic->ic_node_getrssi = node_getrssi;
121 ic->ic_node_getsignal = node_getsignal;
122 ic->ic_node_getmimoinfo = node_getmimoinfo;
125 * Set flags to be propagated to all vap's;
126 * these define default behaviour/configuration.
128 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
132 ieee80211_node_detach(struct ieee80211com *ic)
135 callout_drain(&ic->ic_inact);
136 ieee80211_node_table_cleanup(&ic->ic_sta);
137 ieee80211_ageq_drain(&ic->ic_stageq);
138 ieee80211_ageq_cleanup(&ic->ic_stageq);
142 ieee80211_node_vattach(struct ieee80211vap *vap)
144 /* NB: driver can override */
145 vap->iv_max_aid = IEEE80211_AID_DEF;
147 /* default station inactivity timer setings */
148 vap->iv_inact_init = IEEE80211_INACT_INIT;
149 vap->iv_inact_auth = IEEE80211_INACT_AUTH;
150 vap->iv_inact_run = IEEE80211_INACT_RUN;
151 vap->iv_inact_probe = IEEE80211_INACT_PROBE;
153 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
154 "%s: init %u auth %u run %u probe %u\n", __func__,
155 vap->iv_inact_init, vap->iv_inact_auth,
156 vap->iv_inact_run, vap->iv_inact_probe);
160 ieee80211_node_latevattach(struct ieee80211vap *vap)
162 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
163 /* XXX should we allow max aid to be zero? */
164 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
165 vap->iv_max_aid = IEEE80211_AID_MIN;
166 if_printf(vap->iv_ifp,
167 "WARNING: max aid too small, changed to %d\n",
170 vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC(
171 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
173 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
174 if (vap->iv_aid_bitmap == NULL) {
175 /* XXX no way to recover */
176 printf("%s: no memory for AID bitmap, max aid %d!\n",
177 __func__, vap->iv_max_aid);
182 ieee80211_reset_bss(vap);
184 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
188 ieee80211_node_vdetach(struct ieee80211vap *vap)
190 struct ieee80211com *ic = vap->iv_ic;
192 ieee80211_node_table_reset(&ic->ic_sta, vap);
193 if (vap->iv_bss != NULL) {
194 ieee80211_free_node(vap->iv_bss);
197 if (vap->iv_aid_bitmap != NULL) {
198 IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE);
199 vap->iv_aid_bitmap = NULL;
204 * Port authorize/unauthorize interfaces for use by an authenticator.
208 ieee80211_node_authorize(struct ieee80211_node *ni)
210 struct ieee80211vap *vap = ni->ni_vap;
212 ni->ni_flags |= IEEE80211_NODE_AUTH;
213 ni->ni_inact_reload = vap->iv_inact_run;
214 ni->ni_inact = ni->ni_inact_reload;
216 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
217 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
221 ieee80211_node_unauthorize(struct ieee80211_node *ni)
223 struct ieee80211vap *vap = ni->ni_vap;
225 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
226 ni->ni_inact_reload = vap->iv_inact_auth;
227 if (ni->ni_inact > ni->ni_inact_reload)
228 ni->ni_inact = ni->ni_inact_reload;
230 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
231 "%s: inact_reload %u inact %u", __func__,
232 ni->ni_inact_reload, ni->ni_inact);
236 * Fix tx parameters for a node according to ``association state''.
239 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
241 struct ieee80211vap *vap = ni->ni_vap;
242 enum ieee80211_phymode mode;
244 if (ni->ni_flags & IEEE80211_NODE_HT) {
245 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
246 mode = IEEE80211_MODE_11NA;
248 mode = IEEE80211_MODE_11NG;
249 } else { /* legacy rate handling */
250 if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
251 mode = IEEE80211_MODE_STURBO_A;
252 else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
253 mode = IEEE80211_MODE_HALF;
254 else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
255 mode = IEEE80211_MODE_QUARTER;
256 /* NB: 108A should be handled as 11a */
257 else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
258 mode = IEEE80211_MODE_11A;
259 else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
260 (ni->ni_flags & IEEE80211_NODE_ERP))
261 mode = IEEE80211_MODE_11G;
263 mode = IEEE80211_MODE_11B;
265 ni->ni_txparms = &vap->iv_txparms[mode];
269 * Set/change the channel. The rate set is also updated as
270 * to insure a consistent view by drivers.
271 * XXX should be private but hostap needs it to deal with CSA
274 ieee80211_node_set_chan(struct ieee80211_node *ni,
275 struct ieee80211_channel *chan)
277 struct ieee80211com *ic = ni->ni_ic;
278 struct ieee80211vap *vap = ni->ni_vap;
279 enum ieee80211_phymode mode;
281 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
284 mode = ieee80211_chan2mode(chan);
285 if (IEEE80211_IS_CHAN_HT(chan)) {
287 * We must install the legacy rate est in ni_rates and the
288 * HT rate set in ni_htrates.
290 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
292 * Setup bss tx parameters based on operating mode. We
293 * use legacy rates when operating in a mixed HT+non-HT bss
294 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
296 if (mode == IEEE80211_MODE_11NA &&
297 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
298 mode = IEEE80211_MODE_11A;
299 else if (mode == IEEE80211_MODE_11NG &&
300 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
301 mode = IEEE80211_MODE_11G;
302 if (mode == IEEE80211_MODE_11G &&
303 (vap->iv_flags & IEEE80211_F_PUREG) == 0)
304 mode = IEEE80211_MODE_11B;
306 ni->ni_txparms = &vap->iv_txparms[mode];
307 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
311 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
313 /* propagate useful state */
314 nbss->ni_authmode = obss->ni_authmode;
315 nbss->ni_txpower = obss->ni_txpower;
316 nbss->ni_vlan = obss->ni_vlan;
317 /* XXX statistics? */
318 /* XXX legacy WDS bssid? */
322 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
324 struct ieee80211com *ic = vap->iv_ic;
325 struct ieee80211_node *ni;
327 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
328 "%s: creating %s on channel %u%c\n", __func__,
329 ieee80211_opmode_name[vap->iv_opmode],
330 ieee80211_chan2ieee(ic, chan),
331 ieee80211_channel_type_char(chan));
333 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
338 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
339 ni->ni_esslen = vap->iv_des_ssid[0].len;
340 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
341 if (vap->iv_bss != NULL)
342 copy_bss(ni, vap->iv_bss);
343 ni->ni_intval = ic->ic_bintval;
344 if (vap->iv_flags & IEEE80211_F_PRIVACY)
345 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
346 if (ic->ic_phytype == IEEE80211_T_FH) {
347 ni->ni_fhdwell = 200; /* XXX */
350 if (vap->iv_opmode == IEEE80211_M_IBSS) {
351 vap->iv_flags |= IEEE80211_F_SIBSS;
352 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
353 if (vap->iv_flags & IEEE80211_F_DESBSSID)
354 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
356 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
357 /* clear group bit, add local bit */
358 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
360 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
361 if (vap->iv_flags & IEEE80211_F_DESBSSID)
362 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
364 #ifdef IEEE80211_SUPPORT_TDMA
365 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
367 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
368 #ifdef IEEE80211_SUPPORT_MESH
369 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
370 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
371 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
375 * Fix the channel and related attributes.
377 /* clear DFS CAC state on previous channel */
378 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
379 ic->ic_bsschan->ic_freq != chan->ic_freq &&
380 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
381 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
382 ic->ic_bsschan = chan;
383 ieee80211_node_set_chan(ni, chan);
384 ic->ic_curmode = ieee80211_chan2mode(chan);
386 * Do mode-specific setup.
388 if (IEEE80211_IS_CHAN_FULL(chan)) {
389 if (IEEE80211_IS_CHAN_ANYG(chan)) {
391 * Use a mixed 11b/11g basic rate set.
393 ieee80211_setbasicrates(&ni->ni_rates,
395 if (vap->iv_flags & IEEE80211_F_PUREG) {
397 * Also mark OFDM rates basic so 11b
398 * stations do not join (WiFi compliance).
400 ieee80211_addbasicrates(&ni->ni_rates,
403 } else if (IEEE80211_IS_CHAN_B(chan)) {
405 * Force pure 11b rate set.
407 ieee80211_setbasicrates(&ni->ni_rates,
412 (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 */
557 ieee80211_ibss_merge_check(struct ieee80211_node *ni)
559 struct ieee80211vap *vap = ni->ni_vap;
561 if (ni == vap->iv_bss ||
562 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
563 /* unchanged, nothing to do */
567 if (!check_bss(vap, ni)) {
568 /* capabilities mismatch */
569 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
570 "%s: merge failed, capabilities mismatch\n", __func__);
571 #ifdef IEEE80211_DEBUG
572 if (ieee80211_msg_assoc(vap))
573 check_bss_debug(vap, ni);
575 vap->iv_stats.is_ibss_capmismatch++;
583 * Check if the given node should populate the node table.
585 * We need to be in "see all beacons for all ssids" mode in order
586 * to do IBSS merges, however this means we will populate nodes for
587 * /all/ IBSS SSIDs, versus just the one we care about.
589 * So this check ensures the node can actually belong to our IBSS
590 * configuration. For now it simply checks the SSID.
593 ieee80211_ibss_node_check_new(struct ieee80211_node *ni,
594 const struct ieee80211_scanparams *scan)
596 struct ieee80211vap *vap = ni->ni_vap;
600 * If we have no SSID and no scan SSID, return OK.
602 if (vap->iv_des_nssid == 0 && scan->ssid == NULL)
606 * If we have one of (SSID, scan SSID) then return error.
608 if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL))
612 * Double-check - we need scan SSID.
614 if (scan->ssid == NULL)
618 * Check if the scan SSID matches the SSID list for the VAP.
620 for (i = 0; i < vap->iv_des_nssid; i++) {
622 /* Sanity length check */
623 if (vap->iv_des_ssid[i].len != scan->ssid[1])
626 /* Note: SSID in the scan entry is the IE format */
627 if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2,
628 vap->iv_des_ssid[i].len) == 0)
639 * Handle 802.11 ad hoc network merge. The
640 * convention, set by the Wireless Ethernet Compatibility Alliance
641 * (WECA), is that an 802.11 station will change its BSSID to match
642 * the "oldest" 802.11 ad hoc network, on the same channel, that
643 * has the station's desired SSID. The "oldest" 802.11 network
644 * sends beacons with the greatest TSF timestamp.
646 * The caller is assumed to validate TSF's before attempting a merge.
648 * Return !0 if the BSSID changed, 0 otherwise.
651 ieee80211_ibss_merge(struct ieee80211_node *ni)
653 #ifdef IEEE80211_DEBUG
654 struct ieee80211vap *vap = ni->ni_vap;
655 struct ieee80211com *ic = ni->ni_ic;
658 if (! ieee80211_ibss_merge_check(ni))
661 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
662 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
663 ether_sprintf(ni->ni_bssid),
664 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
665 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
666 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
668 return ieee80211_sta_join1(ieee80211_ref_node(ni));
672 * Calculate HT channel promotion flags for all vaps.
673 * This assumes ni_chan have been setup for each vap.
676 gethtadjustflags(struct ieee80211com *ic)
678 struct ieee80211vap *vap;
683 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
684 if (vap->iv_state < IEEE80211_S_RUN)
686 switch (vap->iv_opmode) {
687 case IEEE80211_M_WDS:
688 case IEEE80211_M_STA:
689 case IEEE80211_M_AHDEMO:
690 case IEEE80211_M_HOSTAP:
691 case IEEE80211_M_IBSS:
692 case IEEE80211_M_MBSS:
693 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
703 * Check if the current channel needs to change based on whether
704 * any vap's are using HT20/HT40. This is used to sync the state
705 * of ic_curchan after a channel width change on a running vap.
708 ieee80211_sync_curchan(struct ieee80211com *ic)
710 struct ieee80211_channel *c;
712 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
713 if (c != ic->ic_curchan) {
715 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
716 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
717 IEEE80211_UNLOCK(ic);
718 ic->ic_set_channel(ic);
719 ieee80211_radiotap_chan_change(ic);
725 * Setup the current channel. The request channel may be
726 * promoted if other vap's are operating with HT20/HT40.
729 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
731 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
732 int flags = gethtadjustflags(ic);
734 * Check for channel promotion required to support the
735 * set of running vap's. This assumes we are called
736 * after ni_chan is setup for each vap.
738 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
739 if (flags > ieee80211_htchanflags(c))
740 c = ieee80211_ht_adjust_channel(ic, c, flags);
742 ic->ic_bsschan = ic->ic_curchan = c;
743 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
744 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
748 * Change the current channel. The channel change is guaranteed to have
749 * happened before the next state change.
752 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
754 ieee80211_setupcurchan(ic, c);
755 ieee80211_runtask(ic, &ic->ic_chan_task);
759 ieee80211_update_chw(struct ieee80211com *ic)
762 ieee80211_setupcurchan(ic, ic->ic_curchan);
763 ieee80211_runtask(ic, &ic->ic_chw_task);
767 * Join the specified IBSS/BSS network. The node is assumed to
768 * be passed in with a held reference.
771 ieee80211_sta_join1(struct ieee80211_node *selbs)
773 struct ieee80211vap *vap = selbs->ni_vap;
774 struct ieee80211com *ic = selbs->ni_ic;
775 struct ieee80211_node *obss;
779 * Committed to selbs, setup state.
783 * Check if old+new node have the same address in which
784 * case we can reassociate when operating in sta mode.
786 canreassoc = (obss != NULL &&
787 vap->iv_state == IEEE80211_S_RUN &&
788 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
789 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */
791 struct ieee80211_node_table *nt = obss->ni_table;
793 copy_bss(selbs, obss);
794 ieee80211_node_decref(obss); /* iv_bss reference */
796 IEEE80211_NODE_LOCK(nt);
797 node_reclaim(nt, obss); /* station table reference */
798 IEEE80211_NODE_UNLOCK(nt);
800 obss = NULL; /* NB: guard against later use */
804 * Delete unusable rates; we've already checked
805 * that the negotiated rate set is acceptable.
807 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
808 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
810 ieee80211_setcurchan(ic, selbs->ni_chan);
812 * Set the erp state (mostly the slot time) to deal with
813 * the auto-select case; this should be redundant if the
816 ieee80211_reset_erp(ic);
817 ieee80211_wme_initparams(vap);
819 if (vap->iv_opmode == IEEE80211_M_STA) {
822 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
825 * Act as if we received a DEAUTH frame in case we
826 * are invoked from the RUN state. This will cause
827 * us to try to re-authenticate if we are operating
830 ieee80211_new_state(vap, IEEE80211_S_AUTH,
831 IEEE80211_FC0_SUBTYPE_DEAUTH);
834 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
839 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
840 const struct ieee80211_scan_entry *se)
842 struct ieee80211com *ic = vap->iv_ic;
843 struct ieee80211_node *ni;
845 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
852 * Expand scan state into node's format.
853 * XXX may not need all this stuff
855 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
856 ni->ni_esslen = se->se_ssid[1];
857 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
858 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
859 ni->ni_intval = se->se_intval;
860 ni->ni_capinfo = se->se_capinfo;
862 ni->ni_timoff = se->se_timoff;
863 ni->ni_fhdwell = se->se_fhdwell;
864 ni->ni_fhindex = se->se_fhindex;
865 ni->ni_erp = se->se_erp;
866 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
867 ni->ni_noise = se->se_noise;
868 if (vap->iv_opmode == IEEE80211_M_STA) {
869 /* NB: only infrastructure mode requires an associd */
870 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
873 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
874 ieee80211_ies_expand(&ni->ni_ies);
875 #ifdef IEEE80211_SUPPORT_SUPERG
876 if (ni->ni_ies.ath_ie != NULL)
877 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
879 if (ni->ni_ies.htcap_ie != NULL)
880 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
881 if (ni->ni_ies.htinfo_ie != NULL)
882 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
883 #ifdef IEEE80211_SUPPORT_MESH
884 if (ni->ni_ies.meshid_ie != NULL)
885 ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
887 #ifdef IEEE80211_SUPPORT_TDMA
888 if (ni->ni_ies.tdma_ie != NULL)
889 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
893 vap->iv_dtim_period = se->se_dtimperiod;
894 vap->iv_dtim_count = 0;
896 /* NB: must be after ni_chan is setup */
897 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
899 if (ieee80211_iserp_rateset(&ni->ni_rates))
900 ni->ni_flags |= IEEE80211_NODE_ERP;
903 * Setup HT state for this node if it's available, otherwise
904 * non-STA modes won't pick this state up.
906 * For IBSS and related modes that don't go through an
907 * association request/response, the only appropriate place
908 * to setup the HT state is here.
910 if (ni->ni_ies.htinfo_ie != NULL &&
911 ni->ni_ies.htcap_ie != NULL &&
912 vap->iv_flags_ht & IEEE80211_FHT_HT) {
913 ieee80211_ht_node_init(ni);
914 ieee80211_ht_updateparams(ni,
916 ni->ni_ies.htinfo_ie);
917 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
918 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
919 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
921 /* XXX else check for ath FF? */
922 /* XXX QoS? Difficult given that WME config is specific to a master */
924 ieee80211_node_setuptxparms(ni);
925 ieee80211_ratectl_node_init(ni);
927 return ieee80211_sta_join1(ieee80211_ref_node(ni));
931 * Leave the specified IBSS/BSS network. The node is assumed to
932 * be passed in with a held reference.
935 ieee80211_sta_leave(struct ieee80211_node *ni)
937 struct ieee80211com *ic = ni->ni_ic;
939 ic->ic_node_cleanup(ni);
940 ieee80211_notify_node_leave(ni);
944 * Send a deauthenticate frame and drop the station.
947 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
949 /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
950 ieee80211_ref_node(ni);
951 if (ni->ni_associd != 0)
952 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
953 ieee80211_node_leave(ni);
954 ieee80211_free_node(ni);
957 static struct ieee80211_node *
958 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
960 struct ieee80211_node *ni;
962 ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
963 M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
968 * Initialize an ie blob with the specified data. If previous
969 * data exists re-use the data block. As a side effect we clear
970 * all references to specific ie's; the caller is required to
974 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
976 /* NB: assumes data+len are the last fields */
977 memset(ies, 0, offsetof(struct ieee80211_ies, data));
978 if (ies->data != NULL && ies->len != len) {
979 /* data size changed */
980 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
983 if (ies->data == NULL) {
984 ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
985 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
986 if (ies->data == NULL) {
988 /* NB: pointers have already been zero'd above */
992 memcpy(ies->data, data, len);
998 * Reclaim storage for an ie blob.
1001 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
1003 if (ies->data != NULL)
1004 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1008 * Expand an ie blob data contents and to fillin individual
1009 * ie pointers. The data blob is assumed to be well-formed;
1010 * we don't do any validity checking of ie lengths.
1013 ieee80211_ies_expand(struct ieee80211_ies *ies)
1022 case IEEE80211_ELEMID_VENDOR:
1025 else if (iswmeoui(ie))
1027 #ifdef IEEE80211_SUPPORT_SUPERG
1028 else if (isatherosoui(ie))
1031 #ifdef IEEE80211_SUPPORT_TDMA
1032 else if (istdmaoui(ie))
1036 case IEEE80211_ELEMID_RSN:
1039 case IEEE80211_ELEMID_HTCAP:
1042 case IEEE80211_ELEMID_HTINFO:
1043 ies->htinfo_ie = ie;
1045 #ifdef IEEE80211_SUPPORT_MESH
1046 case IEEE80211_ELEMID_MESHID:
1047 ies->meshid_ie = ie;
1057 * Reclaim any resources in a node and reset any critical
1058 * state. Typically nodes are free'd immediately after,
1059 * but in some cases the storage may be reused so we need
1060 * to insure consistent state (should probably fix that).
1063 node_cleanup(struct ieee80211_node *ni)
1065 struct ieee80211vap *vap = ni->ni_vap;
1066 struct ieee80211com *ic = ni->ni_ic;
1069 /* NB: preserve ni_table */
1070 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1071 if (vap->iv_opmode != IEEE80211_M_STA)
1073 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
1074 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
1075 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
1078 * Cleanup any HT-related state.
1080 if (ni->ni_flags & IEEE80211_NODE_HT)
1081 ieee80211_ht_node_cleanup(ni);
1082 #ifdef IEEE80211_SUPPORT_SUPERG
1083 /* Always do FF node cleanup; for A-MSDU */
1084 ieee80211_ff_node_cleanup(ni);
1086 #ifdef IEEE80211_SUPPORT_MESH
1088 * Cleanup any mesh-related state.
1090 if (vap->iv_opmode == IEEE80211_M_MBSS)
1091 ieee80211_mesh_node_cleanup(ni);
1094 * Clear any staging queue entries.
1096 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1099 * Clear AREF flag that marks the authorization refcnt bump
1100 * has happened. This is probably not needed as the node
1101 * should always be removed from the table so not found but
1102 * do it just in case.
1103 * Likewise clear the ASSOCID flag as these flags are intended
1104 * to be managed in tandem.
1106 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1109 * Drain power save queue and, if needed, clear TIM.
1111 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1112 vap->iv_set_tim(ni, 0);
1115 if (ni->ni_challenge != NULL) {
1116 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1117 ni->ni_challenge = NULL;
1120 * Preserve SSID, WPA, and WME ie's so the bss node is
1121 * reusable during a re-auth/re-assoc state transition.
1122 * If we remove these data they will not be recreated
1123 * because they come from a probe-response or beacon frame
1124 * which cannot be expected prior to the association-response.
1125 * This should not be an issue when operating in other modes
1126 * as stations leaving always go through a full state transition
1127 * which will rebuild this state.
1129 * XXX does this leave us open to inheriting old state?
1131 for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1132 if (ni->ni_rxfrag[i] != NULL) {
1133 m_freem(ni->ni_rxfrag[i]);
1134 ni->ni_rxfrag[i] = NULL;
1137 * Must be careful here to remove any key map entry w/o a LOR.
1139 ieee80211_node_delucastkey(ni);
1143 node_free(struct ieee80211_node *ni)
1145 struct ieee80211com *ic = ni->ni_ic;
1147 ieee80211_ratectl_node_deinit(ni);
1148 ic->ic_node_cleanup(ni);
1149 ieee80211_ies_cleanup(&ni->ni_ies);
1150 ieee80211_psq_cleanup(&ni->ni_psq);
1151 IEEE80211_FREE(ni, M_80211_NODE);
1155 node_age(struct ieee80211_node *ni)
1157 struct ieee80211vap *vap = ni->ni_vap;
1160 * Age frames on the power save queue.
1162 if (ieee80211_node_psq_age(ni) != 0 &&
1163 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1164 vap->iv_set_tim(ni, 0);
1166 * Age out HT resources (e.g. frames on the
1167 * A-MPDU reorder queues).
1169 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1170 ieee80211_ht_node_age(ni);
1174 node_getrssi(const struct ieee80211_node *ni)
1176 uint32_t avgrssi = ni->ni_avgrssi;
1179 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1181 rssi = IEEE80211_RSSI_GET(avgrssi);
1182 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1186 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1188 *rssi = node_getrssi(ni);
1189 *noise = ni->ni_noise;
1193 node_getmimoinfo(const struct ieee80211_node *ni,
1194 struct ieee80211_mimo_info *info)
1200 bzero(info, sizeof(*info));
1202 for (i = 0; i < ni->ni_mimo_chains; i++) {
1203 avgrssi = ni->ni_mimo_rssi_ctl[i];
1204 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1207 rssi = IEEE80211_RSSI_GET(avgrssi);
1208 info->rssi[i] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1210 info->noise[i] = ni->ni_mimo_noise_ctl[i];
1213 /* XXX ext radios? */
1218 struct ieee80211_node *
1219 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1220 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1222 struct ieee80211com *ic = nt->nt_ic;
1223 struct ieee80211_node *ni;
1226 ni = ic->ic_node_alloc(vap, macaddr);
1228 vap->iv_stats.is_rx_nodealloc++;
1232 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1233 "%s %p<%s> in %s table\n", __func__, ni,
1234 ether_sprintf(macaddr), nt->nt_name);
1236 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1237 hash = IEEE80211_NODE_HASH(ic, macaddr);
1238 ieee80211_node_initref(ni); /* mark referenced */
1239 ni->ni_chan = IEEE80211_CHAN_ANYC;
1240 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1241 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1242 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1243 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1244 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1245 ni->ni_inact_reload = nt->nt_inact_init;
1246 ni->ni_inact = ni->ni_inact_reload;
1247 ni->ni_ath_defkeyix = 0x7fff;
1248 ieee80211_psq_init(&ni->ni_psq, "unknown");
1249 #ifdef IEEE80211_SUPPORT_MESH
1250 if (vap->iv_opmode == IEEE80211_M_MBSS)
1251 ieee80211_mesh_node_init(vap, ni);
1253 IEEE80211_NODE_LOCK(nt);
1254 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1255 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1259 IEEE80211_NODE_UNLOCK(nt);
1261 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1262 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1268 * Craft a temporary node suitable for sending a management frame
1269 * to the specified station. We craft only as much state as we
1270 * need to do the work since the node will be immediately reclaimed
1271 * once the send completes.
1273 struct ieee80211_node *
1274 ieee80211_tmp_node(struct ieee80211vap *vap,
1275 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1277 struct ieee80211com *ic = vap->iv_ic;
1278 struct ieee80211_node *ni;
1280 ni = ic->ic_node_alloc(vap, macaddr);
1282 struct ieee80211_node *bss = vap->iv_bss;
1284 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1285 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1287 ni->ni_table = NULL; /* NB: pedantic */
1288 ni->ni_ic = ic; /* NB: needed to set channel */
1291 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1292 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1293 ieee80211_node_initref(ni); /* mark referenced */
1294 /* NB: required by ieee80211_fix_rate */
1295 ieee80211_node_set_chan(ni, bss->ni_chan);
1296 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1297 IEEE80211_KEYIX_NONE);
1298 ni->ni_txpower = bss->ni_txpower;
1299 /* XXX optimize away */
1300 ieee80211_psq_init(&ni->ni_psq, "unknown");
1302 ieee80211_ratectl_node_init(ni);
1305 vap->iv_stats.is_rx_nodealloc++;
1310 struct ieee80211_node *
1311 ieee80211_dup_bss(struct ieee80211vap *vap,
1312 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1314 struct ieee80211com *ic = vap->iv_ic;
1315 struct ieee80211_node *ni;
1317 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1319 struct ieee80211_node *bss = vap->iv_bss;
1321 * Inherit from iv_bss.
1324 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1325 ieee80211_node_set_chan(ni, bss->ni_chan);
1331 * Create a bss node for a legacy WDS vap. The far end does
1332 * not associate so we just create create a new node and
1333 * simulate an association. The caller is responsible for
1334 * installing the node as the bss node and handling any further
1335 * setup work like authorizing the port.
1337 struct ieee80211_node *
1338 ieee80211_node_create_wds(struct ieee80211vap *vap,
1339 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1341 struct ieee80211com *ic = vap->iv_ic;
1342 struct ieee80211_node *ni;
1344 /* XXX check if node already in sta table? */
1345 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1347 ni->ni_wdsvap = vap;
1348 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1350 * Inherit any manually configured settings.
1352 copy_bss(ni, vap->iv_bss);
1353 ieee80211_node_set_chan(ni, chan);
1354 /* NB: propagate ssid so available to WPA supplicant */
1355 ni->ni_esslen = vap->iv_des_ssid[0].len;
1356 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1357 /* NB: no associd for peer */
1359 * There are no management frames to use to
1360 * discover neighbor capabilities, so blindly
1361 * propagate the local configuration.
1363 if (vap->iv_flags & IEEE80211_F_WME)
1364 ni->ni_flags |= IEEE80211_NODE_QOS;
1365 #ifdef IEEE80211_SUPPORT_SUPERG
1366 if (vap->iv_flags & IEEE80211_F_FF)
1367 ni->ni_flags |= IEEE80211_NODE_FF;
1369 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1370 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1372 * Device is HT-capable and HT is enabled for
1373 * the vap; setup HT operation. On return
1374 * ni_chan will be adjusted to an HT channel.
1376 ieee80211_ht_wds_init(ni);
1378 struct ieee80211_channel *c = ni->ni_chan;
1380 * Force a legacy channel to be used.
1382 c = ieee80211_find_channel(ic,
1383 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1384 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1385 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1392 struct ieee80211_node *
1393 #ifdef IEEE80211_DEBUG_REFCNT
1394 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1395 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1397 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1398 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1401 struct ieee80211_node *ni;
1404 IEEE80211_NODE_LOCK_ASSERT(nt);
1406 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1407 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1408 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1409 ieee80211_ref_node(ni); /* mark referenced */
1410 #ifdef IEEE80211_DEBUG_REFCNT
1411 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1412 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1414 ni, ether_sprintf(ni->ni_macaddr),
1415 ieee80211_node_refcnt(ni));
1423 struct ieee80211_node *
1424 #ifdef IEEE80211_DEBUG_REFCNT
1425 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1426 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1428 ieee80211_find_node(struct ieee80211_node_table *nt,
1429 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1432 struct ieee80211_node *ni;
1434 IEEE80211_NODE_LOCK(nt);
1435 ni = ieee80211_find_node_locked(nt, macaddr);
1436 IEEE80211_NODE_UNLOCK(nt);
1440 struct ieee80211_node *
1441 #ifdef IEEE80211_DEBUG_REFCNT
1442 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1443 const struct ieee80211vap *vap,
1444 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1446 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1447 const struct ieee80211vap *vap,
1448 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1451 struct ieee80211_node *ni;
1454 IEEE80211_NODE_LOCK_ASSERT(nt);
1456 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1457 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1458 if (ni->ni_vap == vap &&
1459 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1460 ieee80211_ref_node(ni); /* mark referenced */
1461 #ifdef IEEE80211_DEBUG_REFCNT
1462 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1463 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1465 ni, ether_sprintf(ni->ni_macaddr),
1466 ieee80211_node_refcnt(ni));
1474 struct ieee80211_node *
1475 #ifdef IEEE80211_DEBUG_REFCNT
1476 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1477 const struct ieee80211vap *vap,
1478 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1480 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1481 const struct ieee80211vap *vap,
1482 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1485 struct ieee80211_node *ni;
1487 IEEE80211_NODE_LOCK(nt);
1488 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1489 IEEE80211_NODE_UNLOCK(nt);
1494 * Fake up a node; this handles node discovery in adhoc mode.
1495 * Note that for the driver's benefit we we treat this like
1496 * an association so the driver has an opportunity to setup
1497 * it's private state.
1499 struct ieee80211_node *
1500 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1501 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1503 struct ieee80211_node *ni;
1505 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1506 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1507 ni = ieee80211_dup_bss(vap, macaddr);
1509 struct ieee80211com *ic = vap->iv_ic;
1511 /* XXX no rate negotiation; just dup */
1512 ni->ni_rates = vap->iv_bss->ni_rates;
1513 if (ieee80211_iserp_rateset(&ni->ni_rates))
1514 ni->ni_flags |= IEEE80211_NODE_ERP;
1515 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1517 * In adhoc demo mode there are no management
1518 * frames to use to discover neighbor capabilities,
1519 * so blindly propagate the local configuration
1520 * so we can do interesting things (e.g. use
1521 * WME to disable ACK's).
1523 if (vap->iv_flags & IEEE80211_F_WME)
1524 ni->ni_flags |= IEEE80211_NODE_QOS;
1525 #ifdef IEEE80211_SUPPORT_SUPERG
1526 if (vap->iv_flags & IEEE80211_F_FF)
1527 ni->ni_flags |= IEEE80211_NODE_FF;
1530 ieee80211_node_setuptxparms(ni);
1531 ieee80211_ratectl_node_init(ni);
1532 if (ic->ic_newassoc != NULL)
1533 ic->ic_newassoc(ni, 1);
1534 /* XXX not right for 802.1x/WPA */
1535 ieee80211_node_authorize(ni);
1541 ieee80211_init_neighbor(struct ieee80211_node *ni,
1542 const struct ieee80211_frame *wh,
1543 const struct ieee80211_scanparams *sp)
1545 int do_ht_setup = 0;
1547 ni->ni_esslen = sp->ssid[1];
1548 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1549 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1550 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1551 ni->ni_intval = sp->bintval;
1552 ni->ni_capinfo = sp->capinfo;
1553 ni->ni_chan = ni->ni_ic->ic_curchan;
1554 ni->ni_fhdwell = sp->fhdwell;
1555 ni->ni_fhindex = sp->fhindex;
1556 ni->ni_erp = sp->erp;
1557 ni->ni_timoff = sp->timoff;
1558 #ifdef IEEE80211_SUPPORT_MESH
1559 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1560 ieee80211_mesh_init_neighbor(ni, wh, sp);
1562 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1563 ieee80211_ies_expand(&ni->ni_ies);
1564 if (ni->ni_ies.wme_ie != NULL)
1565 ni->ni_flags |= IEEE80211_NODE_QOS;
1567 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1568 #ifdef IEEE80211_SUPPORT_SUPERG
1569 if (ni->ni_ies.ath_ie != NULL)
1570 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1572 if (ni->ni_ies.htcap_ie != NULL)
1573 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1574 if (ni->ni_ies.htinfo_ie != NULL)
1575 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1577 if ((ni->ni_ies.htcap_ie != NULL) &&
1578 (ni->ni_ies.htinfo_ie != NULL) &&
1579 (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1584 /* NB: must be after ni_chan is setup */
1585 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1586 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1587 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1590 * If the neighbor is HT compatible, flip that on.
1593 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1594 "%s: doing HT setup\n", __func__);
1595 ieee80211_ht_node_init(ni);
1596 ieee80211_ht_updateparams(ni,
1597 ni->ni_ies.htcap_ie,
1598 ni->ni_ies.htinfo_ie);
1599 ieee80211_setup_htrates(ni,
1600 ni->ni_ies.htcap_ie,
1601 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1602 ieee80211_setup_basic_htrates(ni,
1603 ni->ni_ies.htinfo_ie);
1604 ieee80211_node_setuptxparms(ni);
1605 ieee80211_ratectl_node_init(ni);
1610 * Do node discovery in adhoc mode on receipt of a beacon
1611 * or probe response frame. Note that for the driver's
1612 * benefit we we treat this like an association so the
1613 * driver has an opportunity to setup it's private state.
1615 struct ieee80211_node *
1616 ieee80211_add_neighbor(struct ieee80211vap *vap,
1617 const struct ieee80211_frame *wh,
1618 const struct ieee80211_scanparams *sp)
1620 struct ieee80211_node *ni;
1622 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1623 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1624 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1626 struct ieee80211com *ic = vap->iv_ic;
1628 ieee80211_init_neighbor(ni, wh, sp);
1629 if (ieee80211_iserp_rateset(&ni->ni_rates))
1630 ni->ni_flags |= IEEE80211_NODE_ERP;
1631 ieee80211_node_setuptxparms(ni);
1632 ieee80211_ratectl_node_init(ni);
1633 if (ic->ic_newassoc != NULL)
1634 ic->ic_newassoc(ni, 1);
1635 /* XXX not right for 802.1x/WPA */
1636 ieee80211_node_authorize(ni);
1641 #define IS_PROBEREQ(wh) \
1642 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1643 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1644 #define IS_BCAST_PROBEREQ(wh) \
1645 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1646 ((const struct ieee80211_frame *)(wh))->i_addr3))
1648 static __inline struct ieee80211_node *
1649 _find_rxnode(struct ieee80211_node_table *nt,
1650 const struct ieee80211_frame_min *wh)
1652 if (IS_BCAST_PROBEREQ(wh))
1653 return NULL; /* spam bcast probe req to all vap's */
1654 return ieee80211_find_node_locked(nt, wh->i_addr2);
1658 * Locate the node for sender, track state, and then pass the
1659 * (referenced) node up to the 802.11 layer for its use. Note
1660 * we can return NULL if the sender is not in the table.
1662 struct ieee80211_node *
1663 #ifdef IEEE80211_DEBUG_REFCNT
1664 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1665 const struct ieee80211_frame_min *wh, const char *func, int line)
1667 ieee80211_find_rxnode(struct ieee80211com *ic,
1668 const struct ieee80211_frame_min *wh)
1671 struct ieee80211_node_table *nt;
1672 struct ieee80211_node *ni;
1675 IEEE80211_NODE_LOCK(nt);
1676 ni = _find_rxnode(nt, wh);
1677 IEEE80211_NODE_UNLOCK(nt);
1683 * Like ieee80211_find_rxnode but use the supplied h/w
1684 * key index as a hint to locate the node in the key
1685 * mapping table. If an entry is present at the key
1686 * index we return it; otherwise do a normal lookup and
1687 * update the mapping table if the station has a unicast
1688 * key assigned to it.
1690 struct ieee80211_node *
1691 #ifdef IEEE80211_DEBUG_REFCNT
1692 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1693 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1694 const char *func, int line)
1696 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1697 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1700 struct ieee80211_node_table *nt;
1701 struct ieee80211_node *ni;
1704 IEEE80211_NODE_LOCK(nt);
1705 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1706 ni = nt->nt_keyixmap[keyix];
1710 ni = _find_rxnode(nt, wh);
1711 if (ni != NULL && nt->nt_keyixmap != NULL) {
1713 * If the station has a unicast key cache slot
1714 * assigned update the key->node mapping table.
1716 keyix = ni->ni_ucastkey.wk_rxkeyix;
1717 /* XXX can keyixmap[keyix] != NULL? */
1718 if (keyix < nt->nt_keyixmax &&
1719 nt->nt_keyixmap[keyix] == NULL) {
1720 IEEE80211_DPRINTF(ni->ni_vap,
1722 "%s: add key map entry %p<%s> refcnt %d\n",
1723 __func__, ni, ether_sprintf(ni->ni_macaddr),
1724 ieee80211_node_refcnt(ni)+1);
1725 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1729 if (IS_BCAST_PROBEREQ(wh))
1730 ni = NULL; /* spam bcast probe req to all vap's */
1732 ieee80211_ref_node(ni);
1734 IEEE80211_NODE_UNLOCK(nt);
1738 #undef IS_BCAST_PROBEREQ
1742 * Return a reference to the appropriate node for sending
1743 * a data frame. This handles node discovery in adhoc networks.
1745 struct ieee80211_node *
1746 #ifdef IEEE80211_DEBUG_REFCNT
1747 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1748 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1749 const char *func, int line)
1751 ieee80211_find_txnode(struct ieee80211vap *vap,
1752 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1755 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1756 struct ieee80211_node *ni;
1759 * The destination address should be in the node table
1760 * unless this is a multicast/broadcast frame. We can
1761 * also optimize station mode operation, all frames go
1764 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1765 IEEE80211_NODE_LOCK(nt);
1766 if (vap->iv_opmode == IEEE80211_M_STA ||
1767 vap->iv_opmode == IEEE80211_M_WDS ||
1768 IEEE80211_IS_MULTICAST(macaddr))
1769 ni = ieee80211_ref_node(vap->iv_bss);
1771 ni = ieee80211_find_node_locked(nt, macaddr);
1772 IEEE80211_NODE_UNLOCK(nt);
1775 if (vap->iv_opmode == IEEE80211_M_IBSS ||
1776 vap->iv_opmode == IEEE80211_M_AHDEMO) {
1778 * In adhoc mode cons up a node for the destination.
1779 * Note that we need an additional reference for the
1780 * caller to be consistent with
1781 * ieee80211_find_node_locked.
1783 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1785 (void) ieee80211_ref_node(ni);
1787 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1788 "no node, discard frame (%s)", __func__);
1789 vap->iv_stats.is_tx_nonode++;
1796 _ieee80211_free_node(struct ieee80211_node *ni)
1798 struct ieee80211_node_table *nt = ni->ni_table;
1801 * NB: careful about referencing the vap as it may be
1802 * gone if the last reference was held by a driver.
1803 * We know the com will always be present so it's safe
1804 * to use ni_ic below to reclaim resources.
1807 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1808 "%s %p<%s> in %s table\n", __func__, ni,
1809 ether_sprintf(ni->ni_macaddr),
1810 nt != NULL ? nt->nt_name : "<gone>");
1812 if (ni->ni_associd != 0) {
1813 struct ieee80211vap *vap = ni->ni_vap;
1814 if (vap->iv_aid_bitmap != NULL)
1815 IEEE80211_AID_CLR(vap, ni->ni_associd);
1818 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1819 LIST_REMOVE(ni, ni_hash);
1821 ni->ni_ic->ic_node_free(ni);
1825 * Clear any entry in the unicast key mapping table.
1828 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1830 ieee80211_keyix keyix;
1832 keyix = ni->ni_ucastkey.wk_rxkeyix;
1833 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1834 nt->nt_keyixmap[keyix] == ni) {
1835 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1836 "%s: %p<%s> clear key map entry %u\n",
1837 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1838 nt->nt_keyixmap[keyix] = NULL;
1839 ieee80211_node_decref(ni);
1847 #ifdef IEEE80211_DEBUG_REFCNT
1848 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1850 ieee80211_free_node(struct ieee80211_node *ni)
1853 struct ieee80211_node_table *nt = ni->ni_table;
1855 #ifdef IEEE80211_DEBUG_REFCNT
1856 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1857 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1858 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1861 IEEE80211_NODE_LOCK(nt);
1862 if (ieee80211_node_dectestref(ni)) {
1864 * Last reference, reclaim state.
1866 _ieee80211_free_node(ni);
1867 } else if (ieee80211_node_refcnt(ni) == 1)
1868 if (node_clear_keyixmap(nt, ni))
1869 _ieee80211_free_node(ni);
1870 IEEE80211_NODE_UNLOCK(nt);
1872 if (ieee80211_node_dectestref(ni))
1873 _ieee80211_free_node(ni);
1878 * Reclaim a unicast key and clear any key cache state.
1881 ieee80211_node_delucastkey(struct ieee80211_node *ni)
1883 struct ieee80211com *ic = ni->ni_ic;
1884 struct ieee80211_node_table *nt = &ic->ic_sta;
1885 struct ieee80211_node *nikey;
1886 ieee80211_keyix keyix;
1887 int isowned, status;
1890 * NB: We must beware of LOR here; deleting the key
1891 * can cause the crypto layer to block traffic updates
1892 * which can generate a LOR against the node table lock;
1893 * grab it here and stash the key index for our use below.
1895 * Must also beware of recursion on the node table lock.
1896 * When called from node_cleanup we may already have
1897 * the node table lock held. Unfortunately there's no
1898 * way to separate out this path so we must do this
1901 isowned = IEEE80211_NODE_IS_LOCKED(nt);
1903 IEEE80211_NODE_LOCK(nt);
1905 status = 1; /* NB: success */
1906 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1907 keyix = ni->ni_ucastkey.wk_rxkeyix;
1908 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1909 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1910 nikey = nt->nt_keyixmap[keyix];
1911 nt->nt_keyixmap[keyix] = NULL;
1915 IEEE80211_NODE_UNLOCK(nt);
1917 if (nikey != NULL) {
1918 KASSERT(nikey == ni,
1919 ("key map out of sync, ni %p nikey %p", ni, nikey));
1920 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1921 "%s: delete key map entry %p<%s> refcnt %d\n",
1922 __func__, ni, ether_sprintf(ni->ni_macaddr),
1923 ieee80211_node_refcnt(ni)-1);
1924 ieee80211_free_node(ni);
1930 * Reclaim a node. If this is the last reference count then
1931 * do the normal free work. Otherwise remove it from the node
1932 * table and mark it gone by clearing the back-reference.
1935 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1938 IEEE80211_NODE_LOCK_ASSERT(nt);
1940 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1941 "%s: remove %p<%s> from %s table, refcnt %d\n",
1942 __func__, ni, ether_sprintf(ni->ni_macaddr),
1943 nt->nt_name, ieee80211_node_refcnt(ni)-1);
1945 * Clear any entry in the unicast key mapping table.
1946 * We need to do it here so rx lookups don't find it
1947 * in the mapping table even if it's not in the hash
1948 * table. We cannot depend on the mapping table entry
1949 * being cleared because the node may not be free'd.
1951 (void)node_clear_keyixmap(nt, ni);
1952 if (!ieee80211_node_dectestref(ni)) {
1954 * Other references are present, just remove the
1955 * node from the table so it cannot be found. When
1956 * the references are dropped storage will be
1959 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1960 LIST_REMOVE(ni, ni_hash);
1961 ni->ni_table = NULL; /* clear reference */
1963 _ieee80211_free_node(ni);
1967 * Node table support.
1971 ieee80211_node_table_init(struct ieee80211com *ic,
1972 struct ieee80211_node_table *nt,
1973 const char *name, int inact, int keyixmax)
1977 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
1978 TAILQ_INIT(&nt->nt_node);
1980 nt->nt_inact_init = inact;
1981 nt->nt_keyixmax = keyixmax;
1982 if (nt->nt_keyixmax > 0) {
1983 nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
1984 keyixmax * sizeof(struct ieee80211_node *),
1986 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1987 if (nt->nt_keyixmap == NULL)
1989 "Cannot allocate key index map with %u entries\n",
1992 nt->nt_keyixmap = NULL;
1996 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
1997 struct ieee80211vap *match)
1999 struct ieee80211_node *ni, *next;
2001 IEEE80211_NODE_LOCK(nt);
2002 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
2003 if (match != NULL && ni->ni_vap != match)
2005 /* XXX can this happen? if so need's work */
2006 if (ni->ni_associd != 0) {
2007 struct ieee80211vap *vap = ni->ni_vap;
2009 if (vap->iv_auth->ia_node_leave != NULL)
2010 vap->iv_auth->ia_node_leave(ni);
2011 if (vap->iv_aid_bitmap != NULL)
2012 IEEE80211_AID_CLR(vap, ni->ni_associd);
2014 ni->ni_wdsvap = NULL; /* clear reference */
2015 node_reclaim(nt, ni);
2017 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
2019 * Make a separate pass to clear references to this vap
2020 * held by DWDS entries. They will not be matched above
2021 * because ni_vap will point to the ap vap but we still
2022 * need to clear ni_wdsvap when the WDS vap is destroyed
2025 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
2026 if (ni->ni_wdsvap == match)
2027 ni->ni_wdsvap = NULL;
2029 IEEE80211_NODE_UNLOCK(nt);
2033 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2035 ieee80211_node_table_reset(nt, NULL);
2036 if (nt->nt_keyixmap != NULL) {
2038 /* XXX verify all entries are NULL */
2040 for (i = 0; i < nt->nt_keyixmax; i++)
2041 if (nt->nt_keyixmap[i] != NULL)
2042 printf("%s: %s[%u] still active\n", __func__,
2045 IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
2046 nt->nt_keyixmap = NULL;
2048 IEEE80211_NODE_LOCK_DESTROY(nt);
2052 timeout_stations(void *arg __unused, struct ieee80211_node *ni)
2054 struct ieee80211com *ic = ni->ni_ic;
2055 struct ieee80211vap *vap = ni->ni_vap;
2058 * Only process stations when in RUN state. This
2059 * insures, for example, that we don't timeout an
2060 * inactive station during CAC. Note that CSA state
2061 * is actually handled in ieee80211_node_timeout as
2062 * it applies to more than timeout processing.
2064 if (vap->iv_state != IEEE80211_S_RUN)
2067 * Ignore entries for which have yet to receive an
2068 * authentication frame. These are transient and
2069 * will be reclaimed when the last reference to them
2070 * goes away (when frame xmits complete).
2072 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2073 vap->iv_opmode == IEEE80211_M_STA) &&
2074 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2077 * Free fragment if not needed anymore
2078 * (last fragment older than 1s).
2079 * XXX doesn't belong here, move to node_age
2081 if (ni->ni_rxfrag[0] != NULL &&
2082 ticks > ni->ni_rxfragstamp + hz) {
2083 m_freem(ni->ni_rxfrag[0]);
2084 ni->ni_rxfrag[0] = NULL;
2086 if (ni->ni_inact > 0) {
2088 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2089 "%s: inact %u inact_reload %u nrates %u",
2090 __func__, ni->ni_inact, ni->ni_inact_reload,
2091 ni->ni_rates.rs_nrates);
2094 * Special case ourself; we may be idle for extended periods
2095 * of time and regardless reclaiming our state is wrong.
2096 * XXX run ic_node_age
2098 /* XXX before inact decrement? */
2099 if (ni == vap->iv_bss)
2101 if (ni->ni_associd != 0 ||
2102 (vap->iv_opmode == IEEE80211_M_IBSS ||
2103 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2105 * Age/drain resources held by the station.
2107 ic->ic_node_age(ni);
2109 * Probe the station before time it out. We
2110 * send a null data frame which may not be
2111 * universally supported by drivers (need it
2112 * for ps-poll support so it should be...).
2114 * XXX don't probe the station unless we've
2115 * received a frame from them (and have
2116 * some idea of the rates they are capable
2117 * of); this will get fixed more properly
2118 * soon with better handling of the rate set.
2120 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2121 (0 < ni->ni_inact &&
2122 ni->ni_inact <= vap->iv_inact_probe) &&
2123 ni->ni_rates.rs_nrates != 0) {
2125 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2127 "probe station due to inactivity");
2129 * Grab a reference so the node cannot
2130 * be reclaimed before we send the frame.
2131 * ieee80211_send_nulldata understands
2132 * we've done this and reclaims the
2133 * ref for us as needed.
2135 /* XXX fix this (not required anymore). */
2136 ieee80211_ref_node(ni);
2138 ieee80211_send_nulldata(ni);
2143 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2144 ni->ni_inact <= 0) {
2146 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2147 "station timed out due to inactivity "
2148 "(refcnt %u)", ieee80211_node_refcnt(ni));
2150 * Send a deauthenticate frame and drop the station.
2151 * This is somewhat complicated due to reference counts
2152 * and locking. At this point a station will typically
2153 * have a reference count of 2. ieee80211_node_leave
2154 * will do a "free" of the node which will drop the
2155 * reference count. But in the meantime a reference
2156 * wil be held by the deauth frame. The actual reclaim
2157 * of the node will happen either after the tx is
2158 * completed or by ieee80211_node_leave.
2160 if (ni->ni_associd != 0) {
2161 IEEE80211_SEND_MGMT(ni,
2162 IEEE80211_FC0_SUBTYPE_DEAUTH,
2163 IEEE80211_REASON_AUTH_EXPIRE);
2165 ieee80211_node_leave(ni);
2166 vap->iv_stats.is_node_timeout++;
2171 * Timeout inactive stations and do related housekeeping.
2174 ieee80211_timeout_stations(struct ieee80211com *ic)
2176 struct ieee80211_node_table *nt = &ic->ic_sta;
2178 ieee80211_iterate_nodes(nt, timeout_stations, NULL);
2182 * Aggressively reclaim resources. This should be used
2183 * only in a critical situation to reclaim mbuf resources.
2186 ieee80211_drain(struct ieee80211com *ic)
2188 struct ieee80211_node_table *nt = &ic->ic_sta;
2189 struct ieee80211vap *vap;
2190 struct ieee80211_node *ni;
2192 IEEE80211_NODE_LOCK(nt);
2193 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2195 * Ignore entries for which have yet to receive an
2196 * authentication frame. These are transient and
2197 * will be reclaimed when the last reference to them
2198 * goes away (when frame xmits complete).
2202 * Only process stations when in RUN state. This
2203 * insures, for example, that we don't timeout an
2204 * inactive station during CAC. Note that CSA state
2205 * is actually handled in ieee80211_node_timeout as
2206 * it applies to more than timeout processing.
2208 if (vap->iv_state != IEEE80211_S_RUN)
2210 /* XXX can vap be NULL? */
2211 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2212 vap->iv_opmode == IEEE80211_M_STA) &&
2213 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2217 * XXX doesn't belong here, move to node_drain
2219 if (ni->ni_rxfrag[0] != NULL) {
2220 m_freem(ni->ni_rxfrag[0]);
2221 ni->ni_rxfrag[0] = NULL;
2224 * Drain resources held by the station.
2226 ic->ic_node_drain(ni);
2228 IEEE80211_NODE_UNLOCK(nt);
2232 * Per-ieee80211com inactivity timer callback.
2235 ieee80211_node_timeout(void *arg)
2237 struct ieee80211com *ic = arg;
2240 * Defer timeout processing if a channel switch is pending.
2241 * We typically need to be mute so not doing things that
2242 * might generate frames is good to handle in one place.
2243 * Suppressing the station timeout processing may extend the
2244 * lifetime of inactive stations (by not decrementing their
2245 * idle counters) but this should be ok unless the CSA is
2246 * active for an unusually long time.
2248 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2249 ieee80211_scan_timeout(ic);
2250 ieee80211_timeout_stations(ic);
2251 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2254 ieee80211_erp_timeout(ic);
2255 ieee80211_ht_timeout(ic);
2256 IEEE80211_UNLOCK(ic);
2258 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2259 ieee80211_node_timeout, ic);
2263 * Iterate over the node table and return an array of ref'ed nodes.
2265 * This is separated out from calling the actual node function so that
2266 * no LORs will occur.
2268 * If there are too many nodes (ie, the number of nodes doesn't fit
2269 * within 'max_aid' entries) then the node references will be freed
2270 * and an error will be returned.
2272 * The responsibility of allocating and freeing "ni_arr" is up to
2276 ieee80211_iterate_nt(struct ieee80211_node_table *nt,
2277 struct ieee80211_node **ni_arr, uint16_t max_aid)
2280 struct ieee80211_node *ni;
2282 IEEE80211_NODE_LOCK(nt);
2285 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2288 ic_printf(nt->nt_ic, "Node array overflow: max=%u",
2292 ni_arr[i] = ieee80211_ref_node(ni);
2297 * It's safe to unlock here.
2299 * If we're successful, the list is returned.
2300 * If we're unsuccessful, the list is ignored
2301 * and we remove our references.
2303 * This avoids any potential LOR with
2304 * ieee80211_free_node().
2306 IEEE80211_NODE_UNLOCK(nt);
2309 * If ret is non-zero, we hit some kind of error.
2310 * Rather than walking some nodes, we'll walk none
2314 for (j = 0; j < i; j++) {
2315 /* ieee80211_free_node() locks by itself */
2316 ieee80211_free_node(ni_arr[j]);
2324 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2325 * reference in the source.
2327 * Note that this fetches 'max_aid' from the first VAP, rather than finding
2328 * the largest max_aid from all VAPs.
2331 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2332 ieee80211_iter_func *f, void *arg)
2334 struct ieee80211_node **ni_arr;
2338 struct ieee80211vap *vap;
2340 /* Overdoing it default */
2341 max_aid = IEEE80211_AID_MAX;
2343 /* Handle the case of there being no vaps just yet */
2344 vap = TAILQ_FIRST(&nt->nt_ic->ic_vaps);
2346 max_aid = vap->iv_max_aid;
2348 size = max_aid * sizeof(struct ieee80211_node *);
2349 ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
2350 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2355 * If this fails, the node table won't have any
2356 * valid entries - ieee80211_iterate_nt() frees
2357 * the references to them. So don't try walking
2358 * the table; just skip to the end and free the
2361 if (ieee80211_iterate_nt(nt, ni_arr, max_aid) != 0)
2364 for (i = 0; i < max_aid; i++) {
2365 if (ni_arr[i] == NULL) /* end of the list */
2367 (*f)(arg, ni_arr[i]);
2368 /* ieee80211_free_node() locks by itself */
2369 ieee80211_free_node(ni_arr[i]);
2373 IEEE80211_FREE(ni_arr, M_80211_NODE);
2377 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2379 printf("0x%p: mac %s refcnt %d\n", ni,
2380 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2381 printf("\tauthmode %u flags 0x%x\n",
2382 ni->ni_authmode, ni->ni_flags);
2383 printf("\tassocid 0x%x txpower %u vlan %u\n",
2384 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2385 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2386 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2387 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2388 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2389 ni->ni_rxfragstamp);
2390 printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2391 node_getrssi(ni), ni->ni_noise,
2392 ni->ni_intval, ni->ni_capinfo);
2393 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2394 ether_sprintf(ni->ni_bssid),
2395 ni->ni_esslen, ni->ni_essid,
2396 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2397 printf("\tinact %u inact_reload %u txrate %u\n",
2398 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2399 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2400 ni->ni_htcap, ni->ni_htparam,
2401 ni->ni_htctlchan, ni->ni_ht2ndchan);
2402 printf("\thtopmode %x htstbc %x chw %u\n",
2403 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2407 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2409 ieee80211_iterate_nodes(nt,
2410 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2414 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2416 struct ieee80211vap *vap;
2418 IEEE80211_LOCK_ASSERT(ic);
2420 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2421 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2422 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2426 ieee80211_notify_erp(struct ieee80211com *ic)
2429 ieee80211_notify_erp_locked(ic);
2430 IEEE80211_UNLOCK(ic);
2434 * Handle a station joining an 11g network.
2437 ieee80211_node_join_11g(struct ieee80211_node *ni)
2439 struct ieee80211com *ic = ni->ni_ic;
2441 IEEE80211_LOCK_ASSERT(ic);
2444 * Station isn't capable of short slot time. Bump
2445 * the count of long slot time stations and disable
2446 * use of short slot time. Note that the actual switch
2447 * over to long slot time use may not occur until the
2448 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2450 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2451 ic->ic_longslotsta++;
2452 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2453 "station needs long slot time, count %d",
2454 ic->ic_longslotsta);
2455 /* XXX vap's w/ conflicting needs won't work */
2456 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2458 * Don't force slot time when switched to turbo
2459 * mode as non-ERP stations won't be present; this
2460 * need only be done when on the normal G channel.
2462 ieee80211_set_shortslottime(ic, 0);
2466 * If the new station is not an ERP station
2467 * then bump the counter and enable protection
2470 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2472 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2473 "station is !ERP, %d non-ERP stations associated",
2476 * If station does not support short preamble
2477 * then we must enable use of Barker preamble.
2479 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2480 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2481 "%s", "station needs long preamble");
2482 ic->ic_flags |= IEEE80211_F_USEBARKER;
2483 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2486 * If protection is configured and this is the first
2487 * indication we should use protection, enable it.
2489 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2490 ic->ic_nonerpsta == 1 &&
2491 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2492 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2493 "%s: enable use of protection\n", __func__);
2494 ic->ic_flags |= IEEE80211_F_USEPROT;
2495 ieee80211_notify_erp_locked(ic);
2498 ni->ni_flags |= IEEE80211_NODE_ERP;
2502 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2504 struct ieee80211com *ic = ni->ni_ic;
2505 struct ieee80211vap *vap = ni->ni_vap;
2508 if (ni->ni_associd == 0) {
2511 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2513 * It would be good to search the bitmap
2514 * more efficiently, but this will do for now.
2516 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2517 if (!IEEE80211_AID_ISSET(vap, aid))
2520 if (aid >= vap->iv_max_aid) {
2521 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2522 ieee80211_node_leave(ni);
2525 ni->ni_associd = aid | 0xc000;
2526 ni->ni_jointime = time_uptime;
2528 IEEE80211_AID_SET(vap, ni->ni_associd);
2529 vap->iv_sta_assoc++;
2532 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2533 ieee80211_ht_node_join(ni);
2534 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2535 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2536 ieee80211_node_join_11g(ni);
2537 IEEE80211_UNLOCK(ic);
2543 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2544 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2545 IEEE80211_NODE_AID(ni),
2546 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2547 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2548 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2549 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2550 ni->ni_flags & IEEE80211_NODE_HT ?
2551 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2552 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2553 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2554 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2555 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2556 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2557 ", fast-frames" : "",
2558 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2562 ieee80211_node_setuptxparms(ni);
2563 ieee80211_ratectl_node_init(ni);
2564 /* give driver a chance to setup state like ni_txrate */
2565 if (ic->ic_newassoc != NULL)
2566 ic->ic_newassoc(ni, newassoc);
2567 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2568 /* tell the authenticator about new station */
2569 if (vap->iv_auth->ia_node_join != NULL)
2570 vap->iv_auth->ia_node_join(ni);
2571 ieee80211_notify_node_join(ni,
2572 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2576 disable_protection(struct ieee80211com *ic)
2578 KASSERT(ic->ic_nonerpsta == 0 &&
2579 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2580 ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2583 ic->ic_flags &= ~IEEE80211_F_USEPROT;
2584 /* XXX verify mode? */
2585 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2586 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2587 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2589 ieee80211_notify_erp_locked(ic);
2593 * Handle a station leaving an 11g network.
2596 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2598 struct ieee80211com *ic = ni->ni_ic;
2600 IEEE80211_LOCK_ASSERT(ic);
2602 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2603 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2604 ic->ic_bsschan->ic_flags));
2607 * If a long slot station do the slot time bookkeeping.
2609 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2610 KASSERT(ic->ic_longslotsta > 0,
2611 ("bogus long slot station count %d", ic->ic_longslotsta));
2612 ic->ic_longslotsta--;
2613 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2614 "long slot time station leaves, count now %d",
2615 ic->ic_longslotsta);
2616 if (ic->ic_longslotsta == 0) {
2618 * Re-enable use of short slot time if supported
2619 * and not operating in IBSS mode (per spec).
2621 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2622 ic->ic_opmode != IEEE80211_M_IBSS) {
2623 IEEE80211_DPRINTF(ni->ni_vap,
2624 IEEE80211_MSG_ASSOC,
2625 "%s: re-enable use of short slot time\n",
2627 ieee80211_set_shortslottime(ic, 1);
2632 * If a non-ERP station do the protection-related bookkeeping.
2634 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2635 KASSERT(ic->ic_nonerpsta > 0,
2636 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2638 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2639 "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2640 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2641 " (non-ERP sta present)" : "");
2642 if (ic->ic_nonerpsta == 0 &&
2643 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2644 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2645 "%s: disable use of protection\n", __func__);
2646 disable_protection(ic);
2652 * Time out presence of an overlapping bss with non-ERP
2653 * stations. When operating in hostap mode we listen for
2654 * beacons from other stations and if we identify a non-ERP
2655 * station is present we enable protection. To identify
2656 * when all non-ERP stations are gone we time out this
2660 ieee80211_erp_timeout(struct ieee80211com *ic)
2663 IEEE80211_LOCK_ASSERT(ic);
2665 if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2666 ieee80211_time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2668 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2669 "%s", "age out non-ERP sta present on channel");
2671 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2672 if (ic->ic_nonerpsta == 0)
2673 disable_protection(ic);
2678 * Handle bookkeeping for station deauthentication/disassociation
2679 * when operating as an ap.
2682 ieee80211_node_leave(struct ieee80211_node *ni)
2684 struct ieee80211com *ic = ni->ni_ic;
2685 struct ieee80211vap *vap = ni->ni_vap;
2686 struct ieee80211_node_table *nt = ni->ni_table;
2688 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2689 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2691 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2692 ("unexpected operating mode %u", vap->iv_opmode));
2694 * If node wasn't previously associated all
2695 * we need to do is reclaim the reference.
2697 /* XXX ibss mode bypasses 11g and notification */
2698 if (ni->ni_associd == 0)
2701 * Tell the authenticator the station is leaving.
2702 * Note that we must do this before yanking the
2703 * association id as the authenticator uses the
2704 * associd to locate it's state block.
2706 if (vap->iv_auth->ia_node_leave != NULL)
2707 vap->iv_auth->ia_node_leave(ni);
2710 IEEE80211_AID_CLR(vap, ni->ni_associd);
2711 vap->iv_sta_assoc--;
2714 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2715 ieee80211_ht_node_leave(ni);
2716 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2717 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2718 ieee80211_node_leave_11g(ni);
2719 IEEE80211_UNLOCK(ic);
2721 * Cleanup station state. In particular clear various
2722 * state that might otherwise be reused if the node
2723 * is reused before the reference count goes to zero
2724 * (and memory is reclaimed).
2726 ieee80211_sta_leave(ni);
2729 * Remove the node from any table it's recorded in and
2730 * drop the caller's reference. Removal from the table
2731 * is important to insure the node is not reprocessed
2735 IEEE80211_NODE_LOCK(nt);
2736 node_reclaim(nt, ni);
2737 IEEE80211_NODE_UNLOCK(nt);
2739 ieee80211_free_node(ni);
2743 struct ieee80211vap *vap;
2745 uint32_t rssi_total;
2749 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2751 struct rssiinfo *info = arg;
2752 struct ieee80211vap *vap = ni->ni_vap;
2755 if (info->vap != vap)
2757 /* only associated stations */
2758 if (ni->ni_associd == 0)
2760 rssi = vap->iv_ic->ic_node_getrssi(ni);
2762 info->rssi_samples++;
2763 info->rssi_total += rssi;
2768 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2770 struct rssiinfo *info = arg;
2771 struct ieee80211vap *vap = ni->ni_vap;
2774 if (info->vap != vap)
2776 /* only neighbors */
2777 /* XXX check bssid */
2778 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2780 rssi = vap->iv_ic->ic_node_getrssi(ni);
2782 info->rssi_samples++;
2783 info->rssi_total += rssi;
2787 #ifdef IEEE80211_SUPPORT_MESH
2789 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
2791 struct rssiinfo *info = arg;
2792 struct ieee80211vap *vap = ni->ni_vap;
2795 if (info->vap != vap)
2797 /* only neighbors that peered successfully */
2798 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
2800 rssi = vap->iv_ic->ic_node_getrssi(ni);
2802 info->rssi_samples++;
2803 info->rssi_total += rssi;
2806 #endif /* IEEE80211_SUPPORT_MESH */
2809 ieee80211_getrssi(struct ieee80211vap *vap)
2811 #define NZ(x) ((x) == 0 ? 1 : (x))
2812 struct ieee80211com *ic = vap->iv_ic;
2813 struct rssiinfo info;
2815 info.rssi_total = 0;
2816 info.rssi_samples = 0;
2818 switch (vap->iv_opmode) {
2819 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
2820 case IEEE80211_M_AHDEMO: /* average of all neighbors */
2821 ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2823 case IEEE80211_M_HOSTAP: /* average of all associated stations */
2824 ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2826 #ifdef IEEE80211_SUPPORT_MESH
2827 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
2828 ieee80211_iterate_nodes(&ic->ic_sta, get_mesh_rssi, &info);
2831 case IEEE80211_M_MONITOR: /* XXX */
2832 case IEEE80211_M_STA: /* use stats from associated ap */
2834 if (vap->iv_bss != NULL)
2835 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2836 info.rssi_samples = 1;
2839 return info.rssi_total / NZ(info.rssi_samples);
2844 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2847 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
2849 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2850 /* for non-station mode return avg'd rssi accounting */
2851 if (vap->iv_opmode != IEEE80211_M_STA)
2852 *rssi = ieee80211_getrssi(vap);