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>
56 #include <net80211/ieee80211_vht.h>
61 * IEEE80211_NODE_HASHSIZE must be a power of 2.
63 CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
66 * Association id's are managed with a bit vector.
68 #define IEEE80211_AID_SET(_vap, b) \
69 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
70 (1 << (IEEE80211_AID(b) % 32)))
71 #define IEEE80211_AID_CLR(_vap, b) \
72 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
73 ~(1 << (IEEE80211_AID(b) % 32)))
74 #define IEEE80211_AID_ISSET(_vap, b) \
75 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
77 static int ieee80211_sta_join1(struct ieee80211_node *);
79 static struct ieee80211_node *node_alloc(struct ieee80211vap *,
80 const uint8_t [IEEE80211_ADDR_LEN]);
81 static void node_cleanup(struct ieee80211_node *);
82 static void node_free(struct ieee80211_node *);
83 static void node_age(struct ieee80211_node *);
84 static int8_t node_getrssi(const struct ieee80211_node *);
85 static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
86 static void node_getmimoinfo(const struct ieee80211_node *,
87 struct ieee80211_mimo_info *);
89 static void _ieee80211_free_node(struct ieee80211_node *);
91 static void node_reclaim(struct ieee80211_node_table *nt,
92 struct ieee80211_node *ni);
93 static void ieee80211_node_table_init(struct ieee80211com *ic,
94 struct ieee80211_node_table *nt, const char *name,
95 int inact, int keymaxix);
96 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
97 struct ieee80211vap *);
98 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
99 static void ieee80211_erp_timeout(struct ieee80211com *);
101 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
102 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
105 ieee80211_node_attach(struct ieee80211com *ic)
107 /* XXX really want maxlen enforced per-sta */
108 ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
110 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
111 IEEE80211_INACT_INIT, ic->ic_max_keyix);
112 callout_init(&ic->ic_inact, 1);
113 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
114 ieee80211_node_timeout, ic);
116 ic->ic_node_alloc = node_alloc;
117 ic->ic_node_free = node_free;
118 ic->ic_node_cleanup = node_cleanup;
119 ic->ic_node_age = node_age;
120 ic->ic_node_drain = node_age; /* NB: same as age */
121 ic->ic_node_getrssi = node_getrssi;
122 ic->ic_node_getsignal = node_getsignal;
123 ic->ic_node_getmimoinfo = node_getmimoinfo;
126 * Set flags to be propagated to all vap's;
127 * these define default behaviour/configuration.
129 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
133 ieee80211_node_detach(struct ieee80211com *ic)
136 callout_drain(&ic->ic_inact);
137 ieee80211_node_table_cleanup(&ic->ic_sta);
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 flags 0x%08x\n", __func__,
329 ieee80211_opmode_name[vap->iv_opmode],
330 ieee80211_chan2ieee(ic, chan),
331 ieee80211_channel_type_char(chan),
334 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
339 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
340 ni->ni_esslen = vap->iv_des_ssid[0].len;
341 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
342 if (vap->iv_bss != NULL)
343 copy_bss(ni, vap->iv_bss);
344 ni->ni_intval = ic->ic_bintval;
345 if (vap->iv_flags & IEEE80211_F_PRIVACY)
346 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
347 if (ic->ic_phytype == IEEE80211_T_FH) {
348 ni->ni_fhdwell = 200; /* XXX */
351 if (vap->iv_opmode == IEEE80211_M_IBSS) {
352 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
353 if (vap->iv_flags & IEEE80211_F_DESBSSID)
354 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
356 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
357 /* clear group bit, add local bit */
358 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
360 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
361 if (vap->iv_flags & IEEE80211_F_DESBSSID)
362 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
364 #ifdef IEEE80211_SUPPORT_TDMA
365 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
367 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
368 #ifdef IEEE80211_SUPPORT_MESH
369 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
370 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
371 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
375 * Fix the channel and related attributes.
377 /* clear DFS CAC state on previous channel */
378 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
379 ic->ic_bsschan->ic_freq != chan->ic_freq &&
380 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
381 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
382 ic->ic_bsschan = chan;
383 ieee80211_node_set_chan(ni, chan);
384 ic->ic_curmode = ieee80211_chan2mode(chan);
386 * Do mode-specific setup.
388 if (IEEE80211_IS_CHAN_FULL(chan)) {
389 if (IEEE80211_IS_CHAN_ANYG(chan)) {
391 * Use a mixed 11b/11g basic rate set.
393 ieee80211_setbasicrates(&ni->ni_rates,
395 if (vap->iv_flags & IEEE80211_F_PUREG) {
397 * Also mark OFDM rates basic so 11b
398 * stations do not join (WiFi compliance).
400 ieee80211_addbasicrates(&ni->ni_rates,
403 } else if (IEEE80211_IS_CHAN_B(chan)) {
405 * Force pure 11b rate set.
407 ieee80211_setbasicrates(&ni->ni_rates,
412 /* XXX TODO: other bits and pieces - eg fast-frames? */
414 /* If we're an 11n channel then initialise the 11n bits */
415 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
417 ieee80211_ht_node_init(ni);
418 ieee80211_vht_node_init(ni);
419 } else if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
421 ieee80211_ht_node_init(ni);
424 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
428 * Reset bss state on transition to the INIT state.
429 * Clear any stations from the table (they have been
430 * deauth'd) and reset the bss node (clears key, rate
434 ieee80211_reset_bss(struct ieee80211vap *vap)
436 struct ieee80211com *ic = vap->iv_ic;
437 struct ieee80211_node *ni, *obss;
439 ieee80211_node_table_reset(&ic->ic_sta, vap);
440 /* XXX multi-bss: wrong */
441 ieee80211_reset_erp(ic);
443 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
444 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
446 vap->iv_bss = ieee80211_ref_node(ni);
449 ni->ni_intval = ic->ic_bintval;
450 ieee80211_free_node(obss);
452 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
456 match_ssid(const struct ieee80211_node *ni,
457 int nssid, const struct ieee80211_scan_ssid ssids[])
461 for (i = 0; i < nssid; i++) {
462 if (ni->ni_esslen == ssids[i].len &&
463 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
470 * Test a node for suitability/compatibility.
473 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
475 struct ieee80211com *ic = ni->ni_ic;
478 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
480 if (vap->iv_opmode == IEEE80211_M_IBSS) {
481 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
484 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
487 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
488 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
491 /* XXX does this mean privacy is supported or required? */
492 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
495 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
496 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
497 if (rate & IEEE80211_RATE_BASIC)
499 if (vap->iv_des_nssid != 0 &&
500 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
502 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
503 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
508 #ifdef IEEE80211_DEBUG
510 * Display node suitability/compatibility.
513 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
515 struct ieee80211com *ic = ni->ni_ic;
520 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
522 if (vap->iv_opmode == IEEE80211_M_IBSS) {
523 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
526 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
529 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
530 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
533 /* XXX does this mean privacy is supported or required? */
534 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
537 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
538 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
539 if (rate & IEEE80211_RATE_BASIC)
541 if (vap->iv_des_nssid != 0 &&
542 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
544 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
545 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
548 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
549 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
551 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
552 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
553 fail & 0x08 ? '!' : ' ');
555 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
556 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
558 fail & 0x02 ? '!' : ' ');
560 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
561 fail & 0x04 ? '!' : ' ');
562 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
563 printf("%s\n", fail & 0x10 ? "!" : "");
565 #endif /* IEEE80211_DEBUG */
569 ieee80211_ibss_merge_check(struct ieee80211_node *ni)
571 struct ieee80211vap *vap = ni->ni_vap;
573 if (ni == vap->iv_bss ||
574 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
575 /* unchanged, nothing to do */
579 if (!check_bss(vap, ni)) {
580 /* capabilities mismatch */
581 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
582 "%s: merge failed, capabilities mismatch\n", __func__);
583 #ifdef IEEE80211_DEBUG
584 if (ieee80211_msg_assoc(vap))
585 check_bss_debug(vap, ni);
587 vap->iv_stats.is_ibss_capmismatch++;
595 * Check if the given node should populate the node table.
597 * We need to be in "see all beacons for all ssids" mode in order
598 * to do IBSS merges, however this means we will populate nodes for
599 * /all/ IBSS SSIDs, versus just the one we care about.
601 * So this check ensures the node can actually belong to our IBSS
602 * configuration. For now it simply checks the SSID.
605 ieee80211_ibss_node_check_new(struct ieee80211_node *ni,
606 const struct ieee80211_scanparams *scan)
608 struct ieee80211vap *vap = ni->ni_vap;
612 * If we have no SSID and no scan SSID, return OK.
614 if (vap->iv_des_nssid == 0 && scan->ssid == NULL)
618 * If we have one of (SSID, scan SSID) then return error.
620 if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL))
624 * Double-check - we need scan SSID.
626 if (scan->ssid == NULL)
630 * Check if the scan SSID matches the SSID list for the VAP.
632 for (i = 0; i < vap->iv_des_nssid; i++) {
634 /* Sanity length check */
635 if (vap->iv_des_ssid[i].len != scan->ssid[1])
638 /* Note: SSID in the scan entry is the IE format */
639 if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2,
640 vap->iv_des_ssid[i].len) == 0)
651 * Handle 802.11 ad hoc network merge. The
652 * convention, set by the Wireless Ethernet Compatibility Alliance
653 * (WECA), is that an 802.11 station will change its BSSID to match
654 * the "oldest" 802.11 ad hoc network, on the same channel, that
655 * has the station's desired SSID. The "oldest" 802.11 network
656 * sends beacons with the greatest TSF timestamp.
658 * The caller is assumed to validate TSF's before attempting a merge.
660 * Return !0 if the BSSID changed, 0 otherwise.
663 ieee80211_ibss_merge(struct ieee80211_node *ni)
665 #ifdef IEEE80211_DEBUG
666 struct ieee80211vap *vap = ni->ni_vap;
667 struct ieee80211com *ic = ni->ni_ic;
670 if (! ieee80211_ibss_merge_check(ni))
673 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
674 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
675 ether_sprintf(ni->ni_bssid),
676 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
677 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
678 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
680 return ieee80211_sta_join1(ieee80211_ref_node(ni));
684 * Calculate HT channel promotion flags for all vaps.
685 * This assumes ni_chan have been setup for each vap.
688 gethtadjustflags(struct ieee80211com *ic)
690 struct ieee80211vap *vap;
695 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
696 if (vap->iv_state < IEEE80211_S_RUN)
698 switch (vap->iv_opmode) {
699 case IEEE80211_M_WDS:
700 case IEEE80211_M_STA:
701 case IEEE80211_M_AHDEMO:
702 case IEEE80211_M_HOSTAP:
703 case IEEE80211_M_IBSS:
704 case IEEE80211_M_MBSS:
705 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
715 * Calculate VHT channel promotion flags for all vaps.
716 * This assumes ni_chan have been setup for each vap.
719 getvhtadjustflags(struct ieee80211com *ic)
721 struct ieee80211vap *vap;
726 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
727 if (vap->iv_state < IEEE80211_S_RUN)
729 switch (vap->iv_opmode) {
730 case IEEE80211_M_WDS:
731 case IEEE80211_M_STA:
732 case IEEE80211_M_AHDEMO:
733 case IEEE80211_M_HOSTAP:
734 case IEEE80211_M_IBSS:
735 case IEEE80211_M_MBSS:
736 flags |= ieee80211_vhtchanflags(vap->iv_bss->ni_chan);
746 * Check if the current channel needs to change based on whether
747 * any vap's are using HT20/HT40. This is used to sync the state
748 * of ic_curchan after a channel width change on a running vap.
750 * Same applies for VHT.
753 ieee80211_sync_curchan(struct ieee80211com *ic)
755 struct ieee80211_channel *c;
757 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
758 c = ieee80211_vht_adjust_channel(ic, c, getvhtadjustflags(ic));
760 if (c != ic->ic_curchan) {
762 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
763 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
764 IEEE80211_UNLOCK(ic);
765 ic->ic_set_channel(ic);
766 ieee80211_radiotap_chan_change(ic);
772 * Setup the current channel. The request channel may be
773 * promoted if other vap's are operating with HT20/HT40.
776 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
778 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
779 int flags = gethtadjustflags(ic);
781 * Check for channel promotion required to support the
782 * set of running vap's. This assumes we are called
783 * after ni_chan is setup for each vap.
786 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
787 if (flags > ieee80211_htchanflags(c))
788 c = ieee80211_ht_adjust_channel(ic, c, flags);
792 * VHT promotion - this will at least promote to VHT20/40
793 * based on what HT has done; it may further promote the
794 * channel to VHT80 or above.
796 if (ic->ic_vhtcaps != 0) {
797 int flags = getvhtadjustflags(ic);
798 if (flags > ieee80211_vhtchanflags(c))
799 c = ieee80211_vht_adjust_channel(ic, c, flags);
802 ic->ic_bsschan = ic->ic_curchan = c;
803 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
804 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
808 * Change the current channel. The channel change is guaranteed to have
809 * happened before the next state change.
812 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
814 ieee80211_setupcurchan(ic, c);
815 ieee80211_runtask(ic, &ic->ic_chan_task);
819 ieee80211_update_chw(struct ieee80211com *ic)
822 ieee80211_setupcurchan(ic, ic->ic_curchan);
823 ieee80211_runtask(ic, &ic->ic_chw_task);
827 * Join the specified IBSS/BSS network. The node is assumed to
828 * be passed in with a held reference.
831 ieee80211_sta_join1(struct ieee80211_node *selbs)
833 struct ieee80211vap *vap = selbs->ni_vap;
834 struct ieee80211com *ic = selbs->ni_ic;
835 struct ieee80211_node *obss;
839 * Committed to selbs, setup state.
843 * Check if old+new node have the same address in which
844 * case we can reassociate when operating in sta mode.
846 canreassoc = (obss != NULL &&
847 vap->iv_state == IEEE80211_S_RUN &&
848 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
849 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */
851 struct ieee80211_node_table *nt = obss->ni_table;
853 copy_bss(selbs, obss);
854 ieee80211_node_decref(obss); /* iv_bss reference */
856 IEEE80211_NODE_LOCK(nt);
857 node_reclaim(nt, obss); /* station table reference */
858 IEEE80211_NODE_UNLOCK(nt);
860 obss = NULL; /* NB: guard against later use */
864 * Delete unusable rates; we've already checked
865 * that the negotiated rate set is acceptable.
867 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
868 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
870 ieee80211_setcurchan(ic, selbs->ni_chan);
872 * Set the erp state (mostly the slot time) to deal with
873 * the auto-select case; this should be redundant if the
876 ieee80211_reset_erp(ic);
877 ieee80211_wme_initparams(vap);
879 if (vap->iv_opmode == IEEE80211_M_STA) {
882 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
885 * Act as if we received a DEAUTH frame in case we
886 * are invoked from the RUN state. This will cause
887 * us to try to re-authenticate if we are operating
890 ieee80211_new_state(vap, IEEE80211_S_AUTH,
891 IEEE80211_FC0_SUBTYPE_DEAUTH);
894 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
899 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
900 const struct ieee80211_scan_entry *se)
902 struct ieee80211com *ic = vap->iv_ic;
903 struct ieee80211_node *ni;
906 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
913 * Expand scan state into node's format.
914 * XXX may not need all this stuff
916 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
917 ni->ni_esslen = se->se_ssid[1];
918 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
919 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
920 ni->ni_intval = se->se_intval;
921 ni->ni_capinfo = se->se_capinfo;
923 ni->ni_timoff = se->se_timoff;
924 ni->ni_fhdwell = se->se_fhdwell;
925 ni->ni_fhindex = se->se_fhindex;
926 ni->ni_erp = se->se_erp;
927 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
928 ni->ni_noise = se->se_noise;
929 if (vap->iv_opmode == IEEE80211_M_STA) {
930 /* NB: only infrastructure mode requires an associd */
931 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
934 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
935 ieee80211_ies_expand(&ni->ni_ies);
936 #ifdef IEEE80211_SUPPORT_SUPERG
937 if (ni->ni_ies.ath_ie != NULL)
938 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
940 if (ni->ni_ies.htcap_ie != NULL)
941 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
942 if (ni->ni_ies.htinfo_ie != NULL)
943 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
944 #ifdef IEEE80211_SUPPORT_MESH
945 if (ni->ni_ies.meshid_ie != NULL)
946 ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
948 #ifdef IEEE80211_SUPPORT_TDMA
949 if (ni->ni_ies.tdma_ie != NULL)
950 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
952 if (ni->ni_ies.vhtcap_ie != NULL)
953 ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
954 if (ni->ni_ies.vhtopmode_ie != NULL)
955 ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
957 /* XXX parse BSSLOAD IE */
958 /* XXX parse TXPWRENV IE */
959 /* XXX parse APCHANREP IE */
962 vap->iv_dtim_period = se->se_dtimperiod;
963 vap->iv_dtim_count = 0;
965 /* NB: must be after ni_chan is setup */
966 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
968 if (ieee80211_iserp_rateset(&ni->ni_rates))
969 ni->ni_flags |= IEEE80211_NODE_ERP;
972 * Setup HT state for this node if it's available, otherwise
973 * non-STA modes won't pick this state up.
975 * For IBSS and related modes that don't go through an
976 * association request/response, the only appropriate place
977 * to setup the HT state is here.
979 if (ni->ni_ies.htinfo_ie != NULL &&
980 ni->ni_ies.htcap_ie != NULL &&
981 vap->iv_flags_ht & IEEE80211_FHT_HT) {
982 ieee80211_ht_node_init(ni);
983 ieee80211_ht_updateparams(ni,
985 ni->ni_ies.htinfo_ie);
990 * Setup VHT state for this node if it's available.
993 * For now, don't allow 2GHz VHT operation.
995 if (ni->ni_ies.vhtopmode_ie != NULL &&
996 ni->ni_ies.vhtcap_ie != NULL &&
997 vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
998 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
999 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
1004 ieee80211_vht_node_init(ni);
1005 ieee80211_vht_updateparams(ni,
1006 ni->ni_ies.vhtcap_ie,
1007 ni->ni_ies.vhtopmode_ie);
1008 ieee80211_setup_vht_rates(ni, ni->ni_ies.vhtcap_ie,
1009 ni->ni_ies.vhtopmode_ie);
1014 /* Finally do the node channel change */
1016 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1017 ni->ni_ies.htinfo_ie);
1018 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
1019 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1020 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
1023 /* XXX else check for ath FF? */
1024 /* XXX QoS? Difficult given that WME config is specific to a master */
1026 ieee80211_node_setuptxparms(ni);
1027 ieee80211_ratectl_node_init(ni);
1029 return ieee80211_sta_join1(ieee80211_ref_node(ni));
1033 * Leave the specified IBSS/BSS network. The node is assumed to
1034 * be passed in with a held reference.
1037 ieee80211_sta_leave(struct ieee80211_node *ni)
1039 struct ieee80211com *ic = ni->ni_ic;
1041 ic->ic_node_cleanup(ni);
1042 ieee80211_notify_node_leave(ni);
1046 * Send a deauthenticate frame and drop the station.
1049 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
1051 /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
1052 ieee80211_ref_node(ni);
1053 if (ni->ni_associd != 0)
1054 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1055 ieee80211_node_leave(ni);
1056 ieee80211_free_node(ni);
1059 static struct ieee80211_node *
1060 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1062 struct ieee80211_node *ni;
1064 ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
1065 M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1070 * Initialize an ie blob with the specified data. If previous
1071 * data exists re-use the data block. As a side effect we clear
1072 * all references to specific ie's; the caller is required to
1076 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
1078 /* NB: assumes data+len are the last fields */
1079 memset(ies, 0, offsetof(struct ieee80211_ies, data));
1080 if (ies->data != NULL && ies->len != len) {
1081 /* data size changed */
1082 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1085 if (ies->data == NULL) {
1086 ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
1087 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1088 if (ies->data == NULL) {
1090 /* NB: pointers have already been zero'd above */
1094 memcpy(ies->data, data, len);
1100 * Reclaim storage for an ie blob.
1103 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
1105 if (ies->data != NULL)
1106 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1110 * Expand an ie blob data contents and to fillin individual
1111 * ie pointers. The data blob is assumed to be well-formed;
1112 * we don't do any validity checking of ie lengths.
1115 ieee80211_ies_expand(struct ieee80211_ies *ies)
1124 case IEEE80211_ELEMID_VENDOR:
1127 else if (iswmeoui(ie))
1129 #ifdef IEEE80211_SUPPORT_SUPERG
1130 else if (isatherosoui(ie))
1133 #ifdef IEEE80211_SUPPORT_TDMA
1134 else if (istdmaoui(ie))
1138 case IEEE80211_ELEMID_RSN:
1141 case IEEE80211_ELEMID_HTCAP:
1144 case IEEE80211_ELEMID_HTINFO:
1145 ies->htinfo_ie = ie;
1147 #ifdef IEEE80211_SUPPORT_MESH
1148 case IEEE80211_ELEMID_MESHID:
1149 ies->meshid_ie = ie;
1152 case IEEE80211_ELEMID_VHT_CAP:
1153 ies->vhtcap_ie = ie;
1155 case IEEE80211_ELEMID_VHT_OPMODE:
1156 ies->vhtopmode_ie = ie;
1158 case IEEE80211_ELEMID_VHT_PWR_ENV:
1159 ies->vhtpwrenv_ie = ie;
1161 case IEEE80211_ELEMID_BSSLOAD:
1162 ies->bssload_ie = ie;
1164 case IEEE80211_ELEMID_APCHANREP:
1165 ies->apchanrep_ie = ie;
1174 * Reclaim any resources in a node and reset any critical
1175 * state. Typically nodes are free'd immediately after,
1176 * but in some cases the storage may be reused so we need
1177 * to insure consistent state (should probably fix that).
1180 node_cleanup(struct ieee80211_node *ni)
1182 struct ieee80211vap *vap = ni->ni_vap;
1183 struct ieee80211com *ic = ni->ni_ic;
1186 /* NB: preserve ni_table */
1187 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1188 if (vap->iv_opmode != IEEE80211_M_STA)
1190 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
1191 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
1192 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
1195 * Cleanup any VHT and HT-related state.
1197 if (ni->ni_flags & IEEE80211_NODE_VHT)
1198 ieee80211_vht_node_cleanup(ni);
1199 if (ni->ni_flags & IEEE80211_NODE_HT)
1200 ieee80211_ht_node_cleanup(ni);
1201 #ifdef IEEE80211_SUPPORT_SUPERG
1202 /* Always do FF node cleanup; for A-MSDU */
1203 ieee80211_ff_node_cleanup(ni);
1205 #ifdef IEEE80211_SUPPORT_MESH
1207 * Cleanup any mesh-related state.
1209 if (vap->iv_opmode == IEEE80211_M_MBSS)
1210 ieee80211_mesh_node_cleanup(ni);
1213 * Clear any staging queue entries.
1215 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1218 * Clear AREF flag that marks the authorization refcnt bump
1219 * has happened. This is probably not needed as the node
1220 * should always be removed from the table so not found but
1221 * do it just in case.
1222 * Likewise clear the ASSOCID flag as these flags are intended
1223 * to be managed in tandem.
1225 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1228 * Drain power save queue and, if needed, clear TIM.
1230 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1231 vap->iv_set_tim(ni, 0);
1234 if (ni->ni_challenge != NULL) {
1235 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1236 ni->ni_challenge = NULL;
1239 * Preserve SSID, WPA, and WME ie's so the bss node is
1240 * reusable during a re-auth/re-assoc state transition.
1241 * If we remove these data they will not be recreated
1242 * because they come from a probe-response or beacon frame
1243 * which cannot be expected prior to the association-response.
1244 * This should not be an issue when operating in other modes
1245 * as stations leaving always go through a full state transition
1246 * which will rebuild this state.
1248 * XXX does this leave us open to inheriting old state?
1250 for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1251 if (ni->ni_rxfrag[i] != NULL) {
1252 m_freem(ni->ni_rxfrag[i]);
1253 ni->ni_rxfrag[i] = NULL;
1256 * Must be careful here to remove any key map entry w/o a LOR.
1258 ieee80211_node_delucastkey(ni);
1262 node_free(struct ieee80211_node *ni)
1264 struct ieee80211com *ic = ni->ni_ic;
1266 ieee80211_ratectl_node_deinit(ni);
1267 ic->ic_node_cleanup(ni);
1268 ieee80211_ies_cleanup(&ni->ni_ies);
1269 ieee80211_psq_cleanup(&ni->ni_psq);
1270 IEEE80211_FREE(ni, M_80211_NODE);
1274 node_age(struct ieee80211_node *ni)
1276 struct ieee80211vap *vap = ni->ni_vap;
1279 * Age frames on the power save queue.
1281 if (ieee80211_node_psq_age(ni) != 0 &&
1282 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1283 vap->iv_set_tim(ni, 0);
1285 * Age out HT resources (e.g. frames on the
1286 * A-MPDU reorder queues).
1288 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1289 ieee80211_ht_node_age(ni);
1293 node_getrssi(const struct ieee80211_node *ni)
1295 uint32_t avgrssi = ni->ni_avgrssi;
1298 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1300 rssi = IEEE80211_RSSI_GET(avgrssi);
1301 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1305 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1307 *rssi = node_getrssi(ni);
1308 *noise = ni->ni_noise;
1312 node_getmimoinfo(const struct ieee80211_node *ni,
1313 struct ieee80211_mimo_info *info)
1319 bzero(info, sizeof(*info));
1321 for (i = 0; i < MIN(IEEE80211_MAX_CHAINS, ni->ni_mimo_chains); i++) {
1322 /* Note: for now, just pri20 channel info */
1323 avgrssi = ni->ni_mimo_rssi_ctl[i];
1324 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1325 info->ch[i].rssi[0] = 0;
1327 rssi = IEEE80211_RSSI_GET(avgrssi);
1328 info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1330 info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i];
1333 /* XXX ext radios? */
1339 ieee80211_add_node_nt(struct ieee80211_node_table *nt,
1340 struct ieee80211_node *ni)
1342 struct ieee80211com *ic = nt->nt_ic;
1345 IEEE80211_NODE_LOCK_ASSERT(nt);
1347 hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr);
1348 (void) ic; /* XXX IEEE80211_NODE_HASH */
1349 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1350 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1356 ieee80211_del_node_nt(struct ieee80211_node_table *nt,
1357 struct ieee80211_node *ni)
1360 IEEE80211_NODE_LOCK_ASSERT(nt);
1362 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1363 LIST_REMOVE(ni, ni_hash);
1365 KASSERT(nt->nt_count >= 0,
1366 ("nt_count is negative (%d)!\n", nt->nt_count));
1367 ni->ni_table = NULL;
1370 struct ieee80211_node *
1371 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1372 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1374 struct ieee80211com *ic = nt->nt_ic;
1375 struct ieee80211_node *ni;
1377 ni = ic->ic_node_alloc(vap, macaddr);
1379 vap->iv_stats.is_rx_nodealloc++;
1383 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1384 "%s %p<%s> in %s table\n", __func__, ni,
1385 ether_sprintf(macaddr), nt->nt_name);
1387 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1388 ieee80211_node_initref(ni); /* mark referenced */
1389 ni->ni_chan = IEEE80211_CHAN_ANYC;
1390 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1391 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1392 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1393 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1394 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1395 ni->ni_inact_reload = nt->nt_inact_init;
1396 ni->ni_inact = ni->ni_inact_reload;
1397 ni->ni_ath_defkeyix = 0x7fff;
1398 ieee80211_psq_init(&ni->ni_psq, "unknown");
1399 #ifdef IEEE80211_SUPPORT_MESH
1400 if (vap->iv_opmode == IEEE80211_M_MBSS)
1401 ieee80211_mesh_node_init(vap, ni);
1403 IEEE80211_NODE_LOCK(nt);
1404 ieee80211_add_node_nt(nt, ni);
1407 IEEE80211_NODE_UNLOCK(nt);
1409 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1410 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1412 ieee80211_ratectl_node_init(ni);
1418 * Craft a temporary node suitable for sending a management frame
1419 * to the specified station. We craft only as much state as we
1420 * need to do the work since the node will be immediately reclaimed
1421 * once the send completes.
1423 struct ieee80211_node *
1424 ieee80211_tmp_node(struct ieee80211vap *vap,
1425 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1427 struct ieee80211com *ic = vap->iv_ic;
1428 struct ieee80211_node *ni;
1430 ni = ic->ic_node_alloc(vap, macaddr);
1432 struct ieee80211_node *bss = vap->iv_bss;
1434 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1435 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1437 ni->ni_table = NULL; /* NB: pedantic */
1438 ni->ni_ic = ic; /* NB: needed to set channel */
1441 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1442 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1443 ieee80211_node_initref(ni); /* mark referenced */
1444 /* NB: required by ieee80211_fix_rate */
1445 ieee80211_node_set_chan(ni, bss->ni_chan);
1446 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1447 IEEE80211_KEYIX_NONE);
1448 ni->ni_txpower = bss->ni_txpower;
1449 /* XXX optimize away */
1450 ieee80211_psq_init(&ni->ni_psq, "unknown");
1452 ieee80211_ratectl_node_init(ni);
1455 vap->iv_stats.is_rx_nodealloc++;
1460 struct ieee80211_node *
1461 ieee80211_dup_bss(struct ieee80211vap *vap,
1462 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1464 struct ieee80211com *ic = vap->iv_ic;
1465 struct ieee80211_node *ni;
1467 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1469 struct ieee80211_node *bss = vap->iv_bss;
1471 * Inherit from iv_bss.
1474 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1475 ieee80211_node_set_chan(ni, bss->ni_chan);
1481 * Create a bss node for a legacy WDS vap. The far end does
1482 * not associate so we just create create a new node and
1483 * simulate an association. The caller is responsible for
1484 * installing the node as the bss node and handling any further
1485 * setup work like authorizing the port.
1487 struct ieee80211_node *
1488 ieee80211_node_create_wds(struct ieee80211vap *vap,
1489 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1491 struct ieee80211com *ic = vap->iv_ic;
1492 struct ieee80211_node *ni;
1494 /* XXX check if node already in sta table? */
1495 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1497 ni->ni_wdsvap = vap;
1498 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1500 * Inherit any manually configured settings.
1502 copy_bss(ni, vap->iv_bss);
1503 ieee80211_node_set_chan(ni, chan);
1504 /* NB: propagate ssid so available to WPA supplicant */
1505 ni->ni_esslen = vap->iv_des_ssid[0].len;
1506 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1507 /* NB: no associd for peer */
1509 * There are no management frames to use to
1510 * discover neighbor capabilities, so blindly
1511 * propagate the local configuration.
1513 if (vap->iv_flags & IEEE80211_F_WME)
1514 ni->ni_flags |= IEEE80211_NODE_QOS;
1515 #ifdef IEEE80211_SUPPORT_SUPERG
1516 if (vap->iv_flags & IEEE80211_F_FF)
1517 ni->ni_flags |= IEEE80211_NODE_FF;
1520 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1521 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1523 * Device is HT-capable and HT is enabled for
1524 * the vap; setup HT operation. On return
1525 * ni_chan will be adjusted to an HT channel.
1527 ieee80211_ht_wds_init(ni);
1528 if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1529 printf("%s: TODO: vht_wds_init\n", __func__);
1532 struct ieee80211_channel *c = ni->ni_chan;
1534 * Force a legacy channel to be used.
1536 c = ieee80211_find_channel(ic,
1537 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1538 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1539 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1546 struct ieee80211_node *
1547 #ifdef IEEE80211_DEBUG_REFCNT
1548 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1549 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1551 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1552 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1555 struct ieee80211_node *ni;
1558 IEEE80211_NODE_LOCK_ASSERT(nt);
1560 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1561 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1562 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1563 ieee80211_ref_node(ni); /* mark referenced */
1564 #ifdef IEEE80211_DEBUG_REFCNT
1565 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1566 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1568 ni, ether_sprintf(ni->ni_macaddr),
1569 ieee80211_node_refcnt(ni));
1577 struct ieee80211_node *
1578 #ifdef IEEE80211_DEBUG_REFCNT
1579 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1580 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1582 ieee80211_find_node(struct ieee80211_node_table *nt,
1583 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1586 struct ieee80211_node *ni;
1588 IEEE80211_NODE_LOCK(nt);
1589 ni = ieee80211_find_node_locked(nt, macaddr);
1590 IEEE80211_NODE_UNLOCK(nt);
1594 struct ieee80211_node *
1595 #ifdef IEEE80211_DEBUG_REFCNT
1596 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1597 const struct ieee80211vap *vap,
1598 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1600 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1601 const struct ieee80211vap *vap,
1602 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1605 struct ieee80211_node *ni;
1608 IEEE80211_NODE_LOCK_ASSERT(nt);
1610 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1611 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1612 if (ni->ni_vap == vap &&
1613 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1614 ieee80211_ref_node(ni); /* mark referenced */
1615 #ifdef IEEE80211_DEBUG_REFCNT
1616 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1617 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1619 ni, ether_sprintf(ni->ni_macaddr),
1620 ieee80211_node_refcnt(ni));
1628 struct ieee80211_node *
1629 #ifdef IEEE80211_DEBUG_REFCNT
1630 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1631 const struct ieee80211vap *vap,
1632 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1634 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1635 const struct ieee80211vap *vap,
1636 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1639 struct ieee80211_node *ni;
1641 IEEE80211_NODE_LOCK(nt);
1642 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1643 IEEE80211_NODE_UNLOCK(nt);
1648 * Fake up a node; this handles node discovery in adhoc mode.
1649 * Note that for the driver's benefit we we treat this like
1650 * an association so the driver has an opportunity to setup
1651 * it's private state.
1653 struct ieee80211_node *
1654 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1655 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1657 struct ieee80211_node *ni;
1659 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1660 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1661 ni = ieee80211_dup_bss(vap, macaddr);
1663 struct ieee80211com *ic = vap->iv_ic;
1665 /* XXX no rate negotiation; just dup */
1666 ni->ni_rates = vap->iv_bss->ni_rates;
1667 if (ieee80211_iserp_rateset(&ni->ni_rates))
1668 ni->ni_flags |= IEEE80211_NODE_ERP;
1669 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1671 * In adhoc demo mode there are no management
1672 * frames to use to discover neighbor capabilities,
1673 * so blindly propagate the local configuration
1674 * so we can do interesting things (e.g. use
1675 * WME to disable ACK's).
1680 if (vap->iv_flags & IEEE80211_F_WME)
1681 ni->ni_flags |= IEEE80211_NODE_QOS;
1682 #ifdef IEEE80211_SUPPORT_SUPERG
1683 if (vap->iv_flags & IEEE80211_F_FF)
1684 ni->ni_flags |= IEEE80211_NODE_FF;
1687 ieee80211_node_setuptxparms(ni);
1688 ieee80211_ratectl_node_init(ni);
1691 * XXX TODO: 11n? At least 20MHz, at least A-MPDU RX,
1692 * not A-MPDU TX; not 11n rates, etc. We'll cycle
1693 * that after we hear that we can indeed do 11n
1694 * (either by a beacon frame or by a probe response.)
1698 * This is the first time we see the node.
1700 if (ic->ic_newassoc != NULL)
1701 ic->ic_newassoc(ni, 1);
1704 * Kick off a probe request to the given node;
1705 * we will then use the probe response to update
1706 * 11n/etc configuration state.
1708 * XXX TODO: this isn't guaranteed, and until we get
1709 * a probe response, we won't be able to actually
1710 * do anything 802.11n related to the node.
1711 * So if this does indeed work, maybe we should hold
1712 * off on sending responses until we get the probe
1713 * response, or just default to some sensible subset
1714 * of 802.11n behaviour (eg always allow aggregation
1715 * negotiation TO us, but not FROM us, etc) so we
1716 * aren't entirely busted.
1718 if (vap->iv_opmode == IEEE80211_M_IBSS) {
1719 ieee80211_send_probereq(ni, /* node */
1720 vap->iv_myaddr, /* SA */
1721 ni->ni_macaddr, /* DA */
1722 vap->iv_bss->ni_bssid, /* BSSID */
1723 vap->iv_bss->ni_essid,
1724 vap->iv_bss->ni_esslen); /* SSID */
1727 /* XXX not right for 802.1x/WPA */
1728 ieee80211_node_authorize(ni);
1734 ieee80211_init_neighbor(struct ieee80211_node *ni,
1735 const struct ieee80211_frame *wh,
1736 const struct ieee80211_scanparams *sp)
1738 int do_ht_setup = 0, do_vht_setup = 0;
1740 ni->ni_esslen = sp->ssid[1];
1741 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1742 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1743 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1744 ni->ni_intval = sp->bintval;
1745 ni->ni_capinfo = sp->capinfo;
1746 ni->ni_chan = ni->ni_ic->ic_curchan;
1747 ni->ni_fhdwell = sp->fhdwell;
1748 ni->ni_fhindex = sp->fhindex;
1749 ni->ni_erp = sp->erp;
1750 ni->ni_timoff = sp->timoff;
1751 #ifdef IEEE80211_SUPPORT_MESH
1752 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1753 ieee80211_mesh_init_neighbor(ni, wh, sp);
1755 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1756 ieee80211_ies_expand(&ni->ni_ies);
1757 if (ni->ni_ies.wme_ie != NULL)
1758 ni->ni_flags |= IEEE80211_NODE_QOS;
1760 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1761 #ifdef IEEE80211_SUPPORT_SUPERG
1762 if (ni->ni_ies.ath_ie != NULL)
1763 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1765 if (ni->ni_ies.htcap_ie != NULL)
1766 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1767 if (ni->ni_ies.htinfo_ie != NULL)
1768 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1770 if (ni->ni_ies.vhtcap_ie != NULL)
1771 ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
1772 if (ni->ni_ies.vhtopmode_ie != NULL)
1773 ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
1775 if ((ni->ni_ies.htcap_ie != NULL) &&
1776 (ni->ni_ies.htinfo_ie != NULL) &&
1777 (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1781 if ((ni->ni_ies.vhtcap_ie != NULL) &&
1782 (ni->ni_ies.vhtopmode_ie != NULL) &&
1783 (ni->ni_vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
1789 /* NB: must be after ni_chan is setup */
1790 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1791 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1792 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1795 * If the neighbor is HT compatible, flip that on.
1798 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1799 "%s: doing HT setup\n", __func__);
1800 ieee80211_ht_node_init(ni);
1801 ieee80211_ht_updateparams(ni,
1802 ni->ni_ies.htcap_ie,
1803 ni->ni_ies.htinfo_ie);
1806 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1807 printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
1812 ieee80211_vht_node_init(ni);
1813 ieee80211_vht_updateparams(ni,
1814 ni->ni_ies.vhtcap_ie,
1815 ni->ni_ies.vhtopmode_ie);
1816 ieee80211_setup_vht_rates(ni,
1817 ni->ni_ies.vhtcap_ie,
1818 ni->ni_ies.vhtopmode_ie);
1823 * Finally do the channel upgrade/change based
1824 * on the HT/VHT configuration.
1826 ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
1827 ni->ni_ies.htinfo_ie);
1828 ieee80211_setup_htrates(ni,
1829 ni->ni_ies.htcap_ie,
1830 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1831 ieee80211_setup_basic_htrates(ni,
1832 ni->ni_ies.htinfo_ie);
1834 ieee80211_node_setuptxparms(ni);
1835 ieee80211_ratectl_node_init(ni);
1837 /* Reassociate; we're now 11n/11ac */
1839 * XXX TODO: this is the wrong thing to do -
1840 * we're calling it with isnew=1 so the ath(4)
1841 * driver reinitialises the rate tables.
1842 * This "mostly" works for ath(4), but it won't
1843 * be right for firmware devices which allocate
1846 * So, do we just create a new node and delete
1849 if (ni->ni_ic->ic_newassoc)
1850 ni->ni_ic->ic_newassoc(ni, 1);
1855 * Do node discovery in adhoc mode on receipt of a beacon
1856 * or probe response frame. Note that for the driver's
1857 * benefit we we treat this like an association so the
1858 * driver has an opportunity to setup it's private state.
1860 struct ieee80211_node *
1861 ieee80211_add_neighbor(struct ieee80211vap *vap,
1862 const struct ieee80211_frame *wh,
1863 const struct ieee80211_scanparams *sp)
1865 struct ieee80211_node *ni;
1867 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1868 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1869 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1871 struct ieee80211com *ic = vap->iv_ic;
1873 ieee80211_init_neighbor(ni, wh, sp);
1874 if (ieee80211_iserp_rateset(&ni->ni_rates))
1875 ni->ni_flags |= IEEE80211_NODE_ERP;
1876 ieee80211_node_setuptxparms(ni);
1877 ieee80211_ratectl_node_init(ni);
1878 if (ic->ic_newassoc != NULL)
1879 ic->ic_newassoc(ni, 1);
1880 /* XXX not right for 802.1x/WPA */
1881 ieee80211_node_authorize(ni);
1886 #define IS_PROBEREQ(wh) \
1887 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1888 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1889 #define IS_BCAST_PROBEREQ(wh) \
1890 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1891 ((const struct ieee80211_frame *)(wh))->i_addr3))
1893 static __inline struct ieee80211_node *
1894 _find_rxnode(struct ieee80211_node_table *nt,
1895 const struct ieee80211_frame_min *wh)
1897 if (IS_BCAST_PROBEREQ(wh))
1898 return NULL; /* spam bcast probe req to all vap's */
1899 return ieee80211_find_node_locked(nt, wh->i_addr2);
1903 * Locate the node for sender, track state, and then pass the
1904 * (referenced) node up to the 802.11 layer for its use. Note
1905 * we can return NULL if the sender is not in the table.
1907 struct ieee80211_node *
1908 #ifdef IEEE80211_DEBUG_REFCNT
1909 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1910 const struct ieee80211_frame_min *wh, const char *func, int line)
1912 ieee80211_find_rxnode(struct ieee80211com *ic,
1913 const struct ieee80211_frame_min *wh)
1916 struct ieee80211_node_table *nt;
1917 struct ieee80211_node *ni;
1920 IEEE80211_NODE_LOCK(nt);
1921 ni = _find_rxnode(nt, wh);
1922 IEEE80211_NODE_UNLOCK(nt);
1928 * Like ieee80211_find_rxnode but use the supplied h/w
1929 * key index as a hint to locate the node in the key
1930 * mapping table. If an entry is present at the key
1931 * index we return it; otherwise do a normal lookup and
1932 * update the mapping table if the station has a unicast
1933 * key assigned to it.
1935 struct ieee80211_node *
1936 #ifdef IEEE80211_DEBUG_REFCNT
1937 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1938 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1939 const char *func, int line)
1941 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1942 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1945 struct ieee80211_node_table *nt;
1946 struct ieee80211_node *ni;
1949 IEEE80211_NODE_LOCK(nt);
1950 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1951 ni = nt->nt_keyixmap[keyix];
1955 ni = _find_rxnode(nt, wh);
1956 if (ni != NULL && nt->nt_keyixmap != NULL) {
1958 * If the station has a unicast key cache slot
1959 * assigned update the key->node mapping table.
1961 keyix = ni->ni_ucastkey.wk_rxkeyix;
1962 /* XXX can keyixmap[keyix] != NULL? */
1963 if (keyix < nt->nt_keyixmax &&
1964 nt->nt_keyixmap[keyix] == NULL) {
1965 IEEE80211_DPRINTF(ni->ni_vap,
1967 "%s: add key map entry %p<%s> refcnt %d\n",
1968 __func__, ni, ether_sprintf(ni->ni_macaddr),
1969 ieee80211_node_refcnt(ni)+1);
1970 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1974 if (IS_BCAST_PROBEREQ(wh))
1975 ni = NULL; /* spam bcast probe req to all vap's */
1977 ieee80211_ref_node(ni);
1979 IEEE80211_NODE_UNLOCK(nt);
1983 #undef IS_BCAST_PROBEREQ
1987 * Return a reference to the appropriate node for sending
1988 * a data frame. This handles node discovery in adhoc networks.
1990 struct ieee80211_node *
1991 #ifdef IEEE80211_DEBUG_REFCNT
1992 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1993 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1994 const char *func, int line)
1996 ieee80211_find_txnode(struct ieee80211vap *vap,
1997 const uint8_t macaddr[IEEE80211_ADDR_LEN])
2000 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
2001 struct ieee80211_node *ni;
2004 * The destination address should be in the node table
2005 * unless this is a multicast/broadcast frame. We can
2006 * also optimize station mode operation, all frames go
2009 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
2010 IEEE80211_NODE_LOCK(nt);
2011 if (vap->iv_opmode == IEEE80211_M_STA ||
2012 vap->iv_opmode == IEEE80211_M_WDS ||
2013 IEEE80211_IS_MULTICAST(macaddr))
2014 ni = ieee80211_ref_node(vap->iv_bss);
2016 ni = ieee80211_find_node_locked(nt, macaddr);
2017 IEEE80211_NODE_UNLOCK(nt);
2020 if (vap->iv_opmode == IEEE80211_M_IBSS ||
2021 vap->iv_opmode == IEEE80211_M_AHDEMO) {
2023 * In adhoc mode cons up a node for the destination.
2024 * Note that we need an additional reference for the
2025 * caller to be consistent with
2026 * ieee80211_find_node_locked.
2029 * XXX TODO: this doesn't fake up 11n state; we need
2030 * to find another way to get it upgraded.
2032 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
2034 (void) ieee80211_ref_node(ni);
2036 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
2037 "no node, discard frame (%s)", __func__);
2038 vap->iv_stats.is_tx_nonode++;
2045 _ieee80211_free_node(struct ieee80211_node *ni)
2047 struct ieee80211_node_table *nt = ni->ni_table;
2050 * NB: careful about referencing the vap as it may be
2051 * gone if the last reference was held by a driver.
2052 * We know the com will always be present so it's safe
2053 * to use ni_ic below to reclaim resources.
2056 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2057 "%s %p<%s> in %s table\n", __func__, ni,
2058 ether_sprintf(ni->ni_macaddr),
2059 nt != NULL ? nt->nt_name : "<gone>");
2061 if (ni->ni_associd != 0) {
2062 struct ieee80211vap *vap = ni->ni_vap;
2063 if (vap->iv_aid_bitmap != NULL)
2064 IEEE80211_AID_CLR(vap, ni->ni_associd);
2067 ieee80211_del_node_nt(nt, ni);
2068 ni->ni_ic->ic_node_free(ni);
2072 * Clear any entry in the unicast key mapping table.
2075 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2077 ieee80211_keyix keyix;
2079 keyix = ni->ni_ucastkey.wk_rxkeyix;
2080 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
2081 nt->nt_keyixmap[keyix] == ni) {
2082 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2083 "%s: %p<%s> clear key map entry %u\n",
2084 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
2085 nt->nt_keyixmap[keyix] = NULL;
2086 ieee80211_node_decref(ni);
2094 #ifdef IEEE80211_DEBUG_REFCNT
2095 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
2097 ieee80211_free_node(struct ieee80211_node *ni)
2100 struct ieee80211_node_table *nt = ni->ni_table;
2102 #ifdef IEEE80211_DEBUG_REFCNT
2103 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2104 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
2105 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
2108 IEEE80211_NODE_LOCK(nt);
2109 if (ieee80211_node_dectestref(ni)) {
2111 * Last reference, reclaim state.
2113 _ieee80211_free_node(ni);
2114 } else if (ieee80211_node_refcnt(ni) == 1)
2115 if (node_clear_keyixmap(nt, ni))
2116 _ieee80211_free_node(ni);
2117 IEEE80211_NODE_UNLOCK(nt);
2119 if (ieee80211_node_dectestref(ni))
2120 _ieee80211_free_node(ni);
2125 * Reclaim a unicast key and clear any key cache state.
2128 ieee80211_node_delucastkey(struct ieee80211_node *ni)
2130 struct ieee80211com *ic = ni->ni_ic;
2131 struct ieee80211_node_table *nt = &ic->ic_sta;
2132 struct ieee80211_node *nikey;
2133 ieee80211_keyix keyix;
2134 int isowned, status;
2137 * NB: We must beware of LOR here; deleting the key
2138 * can cause the crypto layer to block traffic updates
2139 * which can generate a LOR against the node table lock;
2140 * grab it here and stash the key index for our use below.
2142 * Must also beware of recursion on the node table lock.
2143 * When called from node_cleanup we may already have
2144 * the node table lock held. Unfortunately there's no
2145 * way to separate out this path so we must do this
2148 isowned = IEEE80211_NODE_IS_LOCKED(nt);
2150 IEEE80211_NODE_LOCK(nt);
2152 status = 1; /* NB: success */
2153 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
2154 keyix = ni->ni_ucastkey.wk_rxkeyix;
2155 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
2156 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
2157 nikey = nt->nt_keyixmap[keyix];
2158 nt->nt_keyixmap[keyix] = NULL;
2162 IEEE80211_NODE_UNLOCK(nt);
2164 if (nikey != NULL) {
2165 KASSERT(nikey == ni,
2166 ("key map out of sync, ni %p nikey %p", ni, nikey));
2167 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2168 "%s: delete key map entry %p<%s> refcnt %d\n",
2169 __func__, ni, ether_sprintf(ni->ni_macaddr),
2170 ieee80211_node_refcnt(ni)-1);
2171 ieee80211_free_node(ni);
2177 * Reclaim a node. If this is the last reference count then
2178 * do the normal free work. Otherwise remove it from the node
2179 * table and mark it gone by clearing the back-reference.
2182 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2185 IEEE80211_NODE_LOCK_ASSERT(nt);
2187 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2188 "%s: remove %p<%s> from %s table, refcnt %d\n",
2189 __func__, ni, ether_sprintf(ni->ni_macaddr),
2190 nt->nt_name, ieee80211_node_refcnt(ni)-1);
2192 * Clear any entry in the unicast key mapping table.
2193 * We need to do it here so rx lookups don't find it
2194 * in the mapping table even if it's not in the hash
2195 * table. We cannot depend on the mapping table entry
2196 * being cleared because the node may not be free'd.
2198 (void)node_clear_keyixmap(nt, ni);
2199 if (!ieee80211_node_dectestref(ni)) {
2201 * Other references are present, just remove the
2202 * node from the table so it cannot be found. When
2203 * the references are dropped storage will be
2206 ieee80211_del_node_nt(nt, ni);
2208 _ieee80211_free_node(ni);
2212 * Node table support.
2216 ieee80211_node_table_init(struct ieee80211com *ic,
2217 struct ieee80211_node_table *nt,
2218 const char *name, int inact, int keyixmax)
2222 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
2223 TAILQ_INIT(&nt->nt_node);
2226 nt->nt_inact_init = inact;
2227 nt->nt_keyixmax = keyixmax;
2228 if (nt->nt_keyixmax > 0) {
2229 nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
2230 keyixmax * sizeof(struct ieee80211_node *),
2232 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2233 if (nt->nt_keyixmap == NULL)
2235 "Cannot allocate key index map with %u entries\n",
2238 nt->nt_keyixmap = NULL;
2242 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
2243 struct ieee80211vap *match)
2245 struct ieee80211_node *ni, *next;
2247 IEEE80211_NODE_LOCK(nt);
2248 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
2249 if (match != NULL && ni->ni_vap != match)
2251 /* XXX can this happen? if so need's work */
2252 if (ni->ni_associd != 0) {
2253 struct ieee80211vap *vap = ni->ni_vap;
2255 if (vap->iv_auth->ia_node_leave != NULL)
2256 vap->iv_auth->ia_node_leave(ni);
2257 if (vap->iv_aid_bitmap != NULL)
2258 IEEE80211_AID_CLR(vap, ni->ni_associd);
2260 ni->ni_wdsvap = NULL; /* clear reference */
2261 node_reclaim(nt, ni);
2263 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
2265 * Make a separate pass to clear references to this vap
2266 * held by DWDS entries. They will not be matched above
2267 * because ni_vap will point to the ap vap but we still
2268 * need to clear ni_wdsvap when the WDS vap is destroyed
2271 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
2272 if (ni->ni_wdsvap == match)
2273 ni->ni_wdsvap = NULL;
2275 IEEE80211_NODE_UNLOCK(nt);
2279 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2281 ieee80211_node_table_reset(nt, NULL);
2282 if (nt->nt_keyixmap != NULL) {
2284 /* XXX verify all entries are NULL */
2286 for (i = 0; i < nt->nt_keyixmax; i++)
2287 if (nt->nt_keyixmap[i] != NULL)
2288 printf("%s: %s[%u] still active\n", __func__,
2291 IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
2292 nt->nt_keyixmap = NULL;
2294 IEEE80211_NODE_LOCK_DESTROY(nt);
2298 timeout_stations(void *arg __unused, struct ieee80211_node *ni)
2300 struct ieee80211com *ic = ni->ni_ic;
2301 struct ieee80211vap *vap = ni->ni_vap;
2304 * Only process stations when in RUN state. This
2305 * insures, for example, that we don't timeout an
2306 * inactive station during CAC. Note that CSA state
2307 * is actually handled in ieee80211_node_timeout as
2308 * it applies to more than timeout processing.
2310 if (vap->iv_state != IEEE80211_S_RUN)
2313 * Ignore entries for which have yet to receive an
2314 * authentication frame. These are transient and
2315 * will be reclaimed when the last reference to them
2316 * goes away (when frame xmits complete).
2318 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2319 vap->iv_opmode == IEEE80211_M_STA) &&
2320 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2323 * Free fragment if not needed anymore
2324 * (last fragment older than 1s).
2325 * XXX doesn't belong here, move to node_age
2327 if (ni->ni_rxfrag[0] != NULL &&
2328 ticks > ni->ni_rxfragstamp + hz) {
2329 m_freem(ni->ni_rxfrag[0]);
2330 ni->ni_rxfrag[0] = NULL;
2332 if (ni->ni_inact > 0) {
2334 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2335 "%s: inact %u inact_reload %u nrates %u",
2336 __func__, ni->ni_inact, ni->ni_inact_reload,
2337 ni->ni_rates.rs_nrates);
2340 * Special case ourself; we may be idle for extended periods
2341 * of time and regardless reclaiming our state is wrong.
2342 * XXX run ic_node_age
2344 /* XXX before inact decrement? */
2345 if (ni == vap->iv_bss)
2347 if (ni->ni_associd != 0 ||
2348 (vap->iv_opmode == IEEE80211_M_IBSS ||
2349 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2351 * Age/drain resources held by the station.
2353 ic->ic_node_age(ni);
2355 * Probe the station before time it out. We
2356 * send a null data frame which may not be
2357 * universally supported by drivers (need it
2358 * for ps-poll support so it should be...).
2360 * XXX don't probe the station unless we've
2361 * received a frame from them (and have
2362 * some idea of the rates they are capable
2363 * of); this will get fixed more properly
2364 * soon with better handling of the rate set.
2366 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2367 (0 < ni->ni_inact &&
2368 ni->ni_inact <= vap->iv_inact_probe) &&
2369 ni->ni_rates.rs_nrates != 0) {
2371 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2373 "probe station due to inactivity");
2375 * Grab a reference so the node cannot
2376 * be reclaimed before we send the frame.
2377 * ieee80211_send_nulldata understands
2378 * we've done this and reclaims the
2379 * ref for us as needed.
2381 /* XXX fix this (not required anymore). */
2382 ieee80211_ref_node(ni);
2384 ieee80211_send_nulldata(ni);
2389 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2390 ni->ni_inact <= 0) {
2392 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2393 "station timed out due to inactivity "
2394 "(refcnt %u)", ieee80211_node_refcnt(ni));
2396 * Send a deauthenticate frame and drop the station.
2397 * This is somewhat complicated due to reference counts
2398 * and locking. At this point a station will typically
2399 * have a reference count of 2. ieee80211_node_leave
2400 * will do a "free" of the node which will drop the
2401 * reference count. But in the meantime a reference
2402 * wil be held by the deauth frame. The actual reclaim
2403 * of the node will happen either after the tx is
2404 * completed or by ieee80211_node_leave.
2406 if (ni->ni_associd != 0) {
2407 IEEE80211_SEND_MGMT(ni,
2408 IEEE80211_FC0_SUBTYPE_DEAUTH,
2409 IEEE80211_REASON_AUTH_EXPIRE);
2411 ieee80211_node_leave(ni);
2412 vap->iv_stats.is_node_timeout++;
2417 * Timeout inactive stations and do related housekeeping.
2420 ieee80211_timeout_stations(struct ieee80211com *ic)
2422 struct ieee80211_node_table *nt = &ic->ic_sta;
2424 ieee80211_iterate_nodes(nt, timeout_stations, NULL);
2428 * Aggressively reclaim resources. This should be used
2429 * only in a critical situation to reclaim mbuf resources.
2432 ieee80211_drain(struct ieee80211com *ic)
2434 struct ieee80211_node_table *nt = &ic->ic_sta;
2435 struct ieee80211vap *vap;
2436 struct ieee80211_node *ni;
2438 IEEE80211_NODE_LOCK(nt);
2439 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2441 * Ignore entries for which have yet to receive an
2442 * authentication frame. These are transient and
2443 * will be reclaimed when the last reference to them
2444 * goes away (when frame xmits complete).
2448 * Only process stations when in RUN state. This
2449 * insures, for example, that we don't timeout an
2450 * inactive station during CAC. Note that CSA state
2451 * is actually handled in ieee80211_node_timeout as
2452 * it applies to more than timeout processing.
2454 if (vap->iv_state != IEEE80211_S_RUN)
2456 /* XXX can vap be NULL? */
2457 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2458 vap->iv_opmode == IEEE80211_M_STA) &&
2459 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2463 * XXX doesn't belong here, move to node_drain
2465 if (ni->ni_rxfrag[0] != NULL) {
2466 m_freem(ni->ni_rxfrag[0]);
2467 ni->ni_rxfrag[0] = NULL;
2470 * Drain resources held by the station.
2472 ic->ic_node_drain(ni);
2474 IEEE80211_NODE_UNLOCK(nt);
2478 * Per-ieee80211com inactivity timer callback.
2481 ieee80211_node_timeout(void *arg)
2483 struct ieee80211com *ic = arg;
2486 * Defer timeout processing if a channel switch is pending.
2487 * We typically need to be mute so not doing things that
2488 * might generate frames is good to handle in one place.
2489 * Suppressing the station timeout processing may extend the
2490 * lifetime of inactive stations (by not decrementing their
2491 * idle counters) but this should be ok unless the CSA is
2492 * active for an unusually long time.
2494 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2495 ieee80211_scan_timeout(ic);
2496 ieee80211_timeout_stations(ic);
2497 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2500 ieee80211_erp_timeout(ic);
2501 ieee80211_ht_timeout(ic);
2502 ieee80211_vht_timeout(ic);
2503 IEEE80211_UNLOCK(ic);
2505 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2506 ieee80211_node_timeout, ic);
2510 * The same as ieee80211_iterate_nodes(), but for one vap only.
2513 ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
2514 struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
2516 struct ieee80211_node **ni_arr;
2517 struct ieee80211_node *ni;
2522 * Iterate over the node table and save an array of ref'ed nodes.
2524 * This is separated out from calling the actual node function so that
2525 * no LORs will occur.
2527 IEEE80211_NODE_LOCK(nt);
2528 count = nt->nt_count;
2529 size = count * sizeof(struct ieee80211_node *);
2530 ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
2531 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2532 if (ni_arr == NULL) {
2533 IEEE80211_NODE_UNLOCK(nt);
2538 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2539 if (vap != NULL && ni->ni_vap != vap)
2542 ("node array overflow (vap %p, i %d, count %d)\n",
2544 ni_arr[i] = ieee80211_ref_node(ni);
2547 IEEE80211_NODE_UNLOCK(nt);
2549 for (i = 0; i < count; i++) {
2550 if (ni_arr[i] == NULL) /* end of the list */
2552 (*f)(arg, ni_arr[i]);
2553 /* ieee80211_free_node() locks by itself */
2554 ieee80211_free_node(ni_arr[i]);
2557 IEEE80211_FREE(ni_arr, M_80211_NODE);
2563 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2564 * reference in the source.
2567 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2568 ieee80211_iter_func *f, void *arg)
2570 /* XXX no way to pass error to the caller. */
2571 (void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
2575 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2577 printf("0x%p: mac %s refcnt %d\n", ni,
2578 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2579 printf("\tauthmode %u flags 0x%x\n",
2580 ni->ni_authmode, ni->ni_flags);
2581 printf("\tassocid 0x%x txpower %u vlan %u\n",
2582 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2583 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2584 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2585 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2586 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2587 ni->ni_rxfragstamp);
2588 printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2589 node_getrssi(ni), ni->ni_noise,
2590 ni->ni_intval, ni->ni_capinfo);
2591 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2592 ether_sprintf(ni->ni_bssid),
2593 ni->ni_esslen, ni->ni_essid,
2594 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2595 printf("\tinact %u inact_reload %u txrate %u\n",
2596 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2597 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2598 ni->ni_htcap, ni->ni_htparam,
2599 ni->ni_htctlchan, ni->ni_ht2ndchan);
2600 printf("\thtopmode %x htstbc %x htchw %u\n",
2601 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2602 printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n",
2603 ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2,
2604 (int) ni->ni_vht_basicmcs);
2609 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2611 ieee80211_iterate_nodes(nt,
2612 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2616 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2618 struct ieee80211vap *vap;
2620 IEEE80211_LOCK_ASSERT(ic);
2622 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2623 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2624 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2628 ieee80211_notify_erp(struct ieee80211com *ic)
2631 ieee80211_notify_erp_locked(ic);
2632 IEEE80211_UNLOCK(ic);
2636 * Handle a station joining an 11g network.
2639 ieee80211_node_join_11g(struct ieee80211_node *ni)
2641 struct ieee80211com *ic = ni->ni_ic;
2643 IEEE80211_LOCK_ASSERT(ic);
2646 * Station isn't capable of short slot time. Bump
2647 * the count of long slot time stations and disable
2648 * use of short slot time. Note that the actual switch
2649 * over to long slot time use may not occur until the
2650 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2652 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2653 ic->ic_longslotsta++;
2654 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2655 "station needs long slot time, count %d",
2656 ic->ic_longslotsta);
2657 /* XXX vap's w/ conflicting needs won't work */
2658 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2660 * Don't force slot time when switched to turbo
2661 * mode as non-ERP stations won't be present; this
2662 * need only be done when on the normal G channel.
2664 ieee80211_set_shortslottime(ic, 0);
2668 * If the new station is not an ERP station
2669 * then bump the counter and enable protection
2672 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2674 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2675 "station is !ERP, %d non-ERP stations associated",
2678 * If station does not support short preamble
2679 * then we must enable use of Barker preamble.
2681 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2682 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2683 "%s", "station needs long preamble");
2684 ic->ic_flags |= IEEE80211_F_USEBARKER;
2685 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2688 * If protection is configured and this is the first
2689 * indication we should use protection, enable it.
2691 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2692 ic->ic_nonerpsta == 1 &&
2693 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2694 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2695 "%s: enable use of protection\n", __func__);
2696 ic->ic_flags |= IEEE80211_F_USEPROT;
2697 ieee80211_notify_erp_locked(ic);
2700 ni->ni_flags |= IEEE80211_NODE_ERP;
2704 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2706 struct ieee80211com *ic = ni->ni_ic;
2707 struct ieee80211vap *vap = ni->ni_vap;
2710 if (ni->ni_associd == 0) {
2713 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2715 * It would be good to search the bitmap
2716 * more efficiently, but this will do for now.
2718 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2719 if (!IEEE80211_AID_ISSET(vap, aid))
2722 if (aid >= vap->iv_max_aid) {
2723 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2724 ieee80211_node_leave(ni);
2727 ni->ni_associd = aid | 0xc000;
2728 ni->ni_jointime = time_uptime;
2730 IEEE80211_AID_SET(vap, ni->ni_associd);
2731 vap->iv_sta_assoc++;
2734 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2735 ieee80211_ht_node_join(ni);
2736 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2737 ieee80211_vht_node_join(ni);
2738 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2739 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2740 ieee80211_node_join_11g(ni);
2741 IEEE80211_UNLOCK(ic);
2748 * XXX VHT - should log VHT channel width, etc
2750 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2751 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2752 IEEE80211_NODE_AID(ni),
2753 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2754 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2755 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2756 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2757 /* XXX update for VHT string */
2758 ni->ni_flags & IEEE80211_NODE_HT ?
2759 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2760 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2761 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2762 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2763 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2764 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2765 ", fast-frames" : "",
2766 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2770 ieee80211_node_setuptxparms(ni);
2771 ieee80211_ratectl_node_init(ni);
2772 /* give driver a chance to setup state like ni_txrate */
2773 if (ic->ic_newassoc != NULL)
2774 ic->ic_newassoc(ni, newassoc);
2775 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2776 /* tell the authenticator about new station */
2777 if (vap->iv_auth->ia_node_join != NULL)
2778 vap->iv_auth->ia_node_join(ni);
2779 ieee80211_notify_node_join(ni,
2780 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2784 disable_protection(struct ieee80211com *ic)
2786 KASSERT(ic->ic_nonerpsta == 0 &&
2787 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2788 ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2791 ic->ic_flags &= ~IEEE80211_F_USEPROT;
2792 /* XXX verify mode? */
2793 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2794 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2795 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2797 ieee80211_notify_erp_locked(ic);
2801 * Handle a station leaving an 11g network.
2804 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2806 struct ieee80211com *ic = ni->ni_ic;
2808 IEEE80211_LOCK_ASSERT(ic);
2810 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2811 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2812 ic->ic_bsschan->ic_flags));
2815 * If a long slot station do the slot time bookkeeping.
2817 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2818 KASSERT(ic->ic_longslotsta > 0,
2819 ("bogus long slot station count %d", ic->ic_longslotsta));
2820 ic->ic_longslotsta--;
2821 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2822 "long slot time station leaves, count now %d",
2823 ic->ic_longslotsta);
2824 if (ic->ic_longslotsta == 0) {
2826 * Re-enable use of short slot time if supported
2827 * and not operating in IBSS mode (per spec).
2829 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2830 ic->ic_opmode != IEEE80211_M_IBSS) {
2831 IEEE80211_DPRINTF(ni->ni_vap,
2832 IEEE80211_MSG_ASSOC,
2833 "%s: re-enable use of short slot time\n",
2835 ieee80211_set_shortslottime(ic, 1);
2840 * If a non-ERP station do the protection-related bookkeeping.
2842 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2843 KASSERT(ic->ic_nonerpsta > 0,
2844 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2846 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2847 "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2848 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2849 " (non-ERP sta present)" : "");
2850 if (ic->ic_nonerpsta == 0 &&
2851 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2852 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2853 "%s: disable use of protection\n", __func__);
2854 disable_protection(ic);
2860 * Time out presence of an overlapping bss with non-ERP
2861 * stations. When operating in hostap mode we listen for
2862 * beacons from other stations and if we identify a non-ERP
2863 * station is present we enable protection. To identify
2864 * when all non-ERP stations are gone we time out this
2868 ieee80211_erp_timeout(struct ieee80211com *ic)
2871 IEEE80211_LOCK_ASSERT(ic);
2873 if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2874 ieee80211_time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2876 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2877 "%s", "age out non-ERP sta present on channel");
2879 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2880 if (ic->ic_nonerpsta == 0)
2881 disable_protection(ic);
2886 * Handle bookkeeping for station deauthentication/disassociation
2887 * when operating as an ap.
2890 ieee80211_node_leave(struct ieee80211_node *ni)
2892 struct ieee80211com *ic = ni->ni_ic;
2893 struct ieee80211vap *vap = ni->ni_vap;
2894 struct ieee80211_node_table *nt = ni->ni_table;
2896 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2897 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2899 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2900 ("unexpected operating mode %u", vap->iv_opmode));
2902 * If node wasn't previously associated all
2903 * we need to do is reclaim the reference.
2905 /* XXX ibss mode bypasses 11g and notification */
2906 if (ni->ni_associd == 0)
2909 * Tell the authenticator the station is leaving.
2910 * Note that we must do this before yanking the
2911 * association id as the authenticator uses the
2912 * associd to locate it's state block.
2914 if (vap->iv_auth->ia_node_leave != NULL)
2915 vap->iv_auth->ia_node_leave(ni);
2918 IEEE80211_AID_CLR(vap, ni->ni_associd);
2919 vap->iv_sta_assoc--;
2922 if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
2923 ieee80211_vht_node_leave(ni);
2924 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2925 ieee80211_ht_node_leave(ni);
2926 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2927 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2928 ieee80211_node_leave_11g(ni);
2929 IEEE80211_UNLOCK(ic);
2931 * Cleanup station state. In particular clear various
2932 * state that might otherwise be reused if the node
2933 * is reused before the reference count goes to zero
2934 * (and memory is reclaimed).
2936 ieee80211_sta_leave(ni);
2939 * Remove the node from any table it's recorded in and
2940 * drop the caller's reference. Removal from the table
2941 * is important to insure the node is not reprocessed
2945 IEEE80211_NODE_LOCK(nt);
2946 node_reclaim(nt, ni);
2947 IEEE80211_NODE_UNLOCK(nt);
2949 ieee80211_free_node(ni);
2954 uint32_t rssi_total;
2958 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2960 struct rssiinfo *info = arg;
2961 struct ieee80211vap *vap = ni->ni_vap;
2964 /* only associated stations */
2965 if (ni->ni_associd == 0)
2967 rssi = vap->iv_ic->ic_node_getrssi(ni);
2969 info->rssi_samples++;
2970 info->rssi_total += rssi;
2975 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2977 struct rssiinfo *info = arg;
2978 struct ieee80211vap *vap = ni->ni_vap;
2981 /* only neighbors */
2982 /* XXX check bssid */
2983 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2985 rssi = vap->iv_ic->ic_node_getrssi(ni);
2987 info->rssi_samples++;
2988 info->rssi_total += rssi;
2992 #ifdef IEEE80211_SUPPORT_MESH
2994 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
2996 struct rssiinfo *info = arg;
2997 struct ieee80211vap *vap = ni->ni_vap;
3000 /* only neighbors that peered successfully */
3001 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
3003 rssi = vap->iv_ic->ic_node_getrssi(ni);
3005 info->rssi_samples++;
3006 info->rssi_total += rssi;
3009 #endif /* IEEE80211_SUPPORT_MESH */
3012 ieee80211_getrssi(struct ieee80211vap *vap)
3014 #define NZ(x) ((x) == 0 ? 1 : (x))
3015 struct ieee80211com *ic = vap->iv_ic;
3016 struct rssiinfo info;
3018 info.rssi_total = 0;
3019 info.rssi_samples = 0;
3020 switch (vap->iv_opmode) {
3021 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
3022 case IEEE80211_M_AHDEMO: /* average of all neighbors */
3023 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
3026 case IEEE80211_M_HOSTAP: /* average of all associated stations */
3027 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
3030 #ifdef IEEE80211_SUPPORT_MESH
3031 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
3032 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
3036 case IEEE80211_M_MONITOR: /* XXX */
3037 case IEEE80211_M_STA: /* use stats from associated ap */
3039 if (vap->iv_bss != NULL)
3040 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
3041 info.rssi_samples = 1;
3044 return info.rssi_total / NZ(info.rssi_samples);
3049 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
3052 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
3054 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
3055 /* for non-station mode return avg'd rssi accounting */
3056 if (vap->iv_opmode != IEEE80211_M_STA)
3057 *rssi = ieee80211_getrssi(vap);