2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
38 #include <sys/socket.h>
41 #include <net/if_media.h>
42 #include <net/ethernet.h>
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_input.h>
46 #include <net80211/ieee80211_wds.h>
51 * Association id's are managed with a bit vector.
53 #define IEEE80211_AID_SET(_vap, b) \
54 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
55 (1 << (IEEE80211_AID(b) % 32)))
56 #define IEEE80211_AID_CLR(_vap, b) \
57 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
58 ~(1 << (IEEE80211_AID(b) % 32)))
59 #define IEEE80211_AID_ISSET(_vap, b) \
60 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
62 #ifdef IEEE80211_DEBUG_REFCNT
63 #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
65 #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
68 static int ieee80211_sta_join1(struct ieee80211_node *);
70 static struct ieee80211_node *node_alloc(struct ieee80211vap *,
71 const uint8_t [IEEE80211_ADDR_LEN]);
72 static void node_cleanup(struct ieee80211_node *);
73 static void node_free(struct ieee80211_node *);
74 static void node_age(struct ieee80211_node *);
75 static int8_t node_getrssi(const struct ieee80211_node *);
76 static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
77 static void node_getmimoinfo(const struct ieee80211_node *,
78 struct ieee80211_mimo_info *);
80 static void _ieee80211_free_node(struct ieee80211_node *);
82 static void ieee80211_node_table_init(struct ieee80211com *ic,
83 struct ieee80211_node_table *nt, const char *name,
84 int inact, int keymaxix);
85 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
86 struct ieee80211vap *);
87 static void ieee80211_node_reclaim(struct ieee80211_node *);
88 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
89 static void ieee80211_erp_timeout(struct ieee80211com *);
91 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
92 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
95 ieee80211_node_attach(struct ieee80211com *ic)
97 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
98 IEEE80211_INACT_INIT, ic->ic_max_keyix);
99 callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
100 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
101 ieee80211_node_timeout, ic);
103 ic->ic_node_alloc = node_alloc;
104 ic->ic_node_free = node_free;
105 ic->ic_node_cleanup = node_cleanup;
106 ic->ic_node_age = node_age;
107 ic->ic_node_drain = node_age; /* NB: same as age */
108 ic->ic_node_getrssi = node_getrssi;
109 ic->ic_node_getsignal = node_getsignal;
110 ic->ic_node_getmimoinfo = node_getmimoinfo;
113 * Set flags to be propagated to all vap's;
114 * these define default behaviour/configuration.
116 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
120 ieee80211_node_detach(struct ieee80211com *ic)
123 callout_drain(&ic->ic_inact);
124 ieee80211_node_table_cleanup(&ic->ic_sta);
128 ieee80211_node_vattach(struct ieee80211vap *vap)
130 /* NB: driver can override */
131 vap->iv_max_aid = IEEE80211_AID_DEF;
133 /* default station inactivity timer setings */
134 vap->iv_inact_init = IEEE80211_INACT_INIT;
135 vap->iv_inact_auth = IEEE80211_INACT_AUTH;
136 vap->iv_inact_run = IEEE80211_INACT_RUN;
137 vap->iv_inact_probe = IEEE80211_INACT_PROBE;
139 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
140 "%s: init %u auth %u run %u probe %u\n", __func__,
141 vap->iv_inact_init, vap->iv_inact_auth,
142 vap->iv_inact_run, vap->iv_inact_probe);
146 ieee80211_node_latevattach(struct ieee80211vap *vap)
148 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
149 /* XXX should we allow max aid to be zero? */
150 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
151 vap->iv_max_aid = IEEE80211_AID_MIN;
152 if_printf(vap->iv_ifp,
153 "WARNING: max aid too small, changed to %d\n",
156 MALLOC(vap->iv_aid_bitmap, uint32_t *,
157 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
158 M_80211_NODE, M_NOWAIT | M_ZERO);
159 if (vap->iv_aid_bitmap == NULL) {
160 /* XXX no way to recover */
161 printf("%s: no memory for AID bitmap, max aid %d!\n",
162 __func__, vap->iv_max_aid);
167 ieee80211_reset_bss(vap);
169 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
173 ieee80211_node_vdetach(struct ieee80211vap *vap)
175 struct ieee80211com *ic = vap->iv_ic;
177 ieee80211_node_table_reset(&ic->ic_sta, vap);
178 if (vap->iv_bss != NULL) {
179 ieee80211_free_node(vap->iv_bss);
182 if (vap->iv_aid_bitmap != NULL) {
183 FREE(vap->iv_aid_bitmap, M_80211_NODE);
184 vap->iv_aid_bitmap = NULL;
189 * Port authorize/unauthorize interfaces for use by an authenticator.
193 ieee80211_node_authorize(struct ieee80211_node *ni)
195 struct ieee80211vap *vap = ni->ni_vap;
197 ni->ni_flags |= IEEE80211_NODE_AUTH;
198 ni->ni_inact_reload = vap->iv_inact_run;
199 ni->ni_inact = ni->ni_inact_reload;
201 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
202 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
206 ieee80211_node_unauthorize(struct ieee80211_node *ni)
208 struct ieee80211vap *vap = ni->ni_vap;
210 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
211 ni->ni_inact_reload = vap->iv_inact_auth;
212 if (ni->ni_inact > ni->ni_inact_reload)
213 ni->ni_inact = ni->ni_inact_reload;
215 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
216 "%s: inact_reload %u inact %u", __func__,
217 ni->ni_inact_reload, ni->ni_inact);
221 * Fix tx parameters for a node according to ``association state''.
224 node_setuptxparms(struct ieee80211_node *ni)
226 struct ieee80211vap *vap = ni->ni_vap;
228 if (ni->ni_flags & IEEE80211_NODE_HT) {
229 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
230 ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NA];
232 ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NG];
233 } else { /* legacy rate handling */
234 if (IEEE80211_IS_CHAN_A(ni->ni_chan))
235 ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11A];
236 else if (ni->ni_flags & IEEE80211_NODE_ERP)
237 ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11G];
239 ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11B];
244 * Set/change the channel. The rate set is also updated as
245 * to insure a consistent view by drivers.
246 * XXX should be private but hostap needs it to deal with CSA
249 ieee80211_node_set_chan(struct ieee80211_node *ni,
250 struct ieee80211_channel *chan)
252 struct ieee80211com *ic = ni->ni_ic;
253 struct ieee80211vap *vap = ni->ni_vap;
254 enum ieee80211_phymode mode;
256 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
259 mode = ieee80211_chan2mode(chan);
260 if (IEEE80211_IS_CHAN_HT(chan)) {
262 * XXX Gotta be careful here; the rate set returned by
263 * ieee80211_get_suprates is actually any HT rate
264 * set so blindly copying it will be bad. We must
265 * install the legacy rate est in ni_rates and the
266 * HT rate set in ni_htrates.
268 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
270 * Setup bss tx parameters based on operating mode. We
271 * use legacy rates when operating in a mixed HT+non-HT bss
272 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
274 if (mode == IEEE80211_MODE_11NA &&
275 (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
276 mode = IEEE80211_MODE_11A;
277 else if (mode == IEEE80211_MODE_11NG &&
278 (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
279 mode = IEEE80211_MODE_11G;
280 if (mode == IEEE80211_MODE_11G &&
281 (vap->iv_flags & IEEE80211_F_PUREG) == 0)
282 mode = IEEE80211_MODE_11B;
284 ni->ni_txparms = &vap->iv_txparms[mode];
285 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
289 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
291 /* propagate useful state */
292 nbss->ni_authmode = obss->ni_authmode;
293 nbss->ni_txpower = obss->ni_txpower;
294 nbss->ni_vlan = obss->ni_vlan;
295 /* XXX statistics? */
296 /* XXX legacy WDS bssid? */
300 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
302 struct ieee80211com *ic = vap->iv_ic;
303 struct ieee80211_node *ni;
305 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
306 "%s: creating ibss on channel %u\n", __func__,
307 ieee80211_chan2ieee(ic, chan));
309 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
314 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
315 ni->ni_esslen = vap->iv_des_ssid[0].len;
316 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
317 if (vap->iv_bss != NULL)
318 copy_bss(ni, vap->iv_bss);
319 ni->ni_intval = ic->ic_bintval;
320 if (vap->iv_flags & IEEE80211_F_PRIVACY)
321 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
322 if (ic->ic_phytype == IEEE80211_T_FH) {
323 ni->ni_fhdwell = 200; /* XXX */
326 if (vap->iv_opmode == IEEE80211_M_IBSS) {
327 vap->iv_flags |= IEEE80211_F_SIBSS;
328 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
329 if (vap->iv_flags & IEEE80211_F_DESBSSID)
330 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
332 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
333 /* clear group bit, add local bit */
334 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
336 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
337 if (vap->iv_flags & IEEE80211_F_DESBSSID)
338 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
340 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
343 * Fix the channel and related attributes.
345 /* clear DFS CAC state on previous channel */
346 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
347 ic->ic_bsschan->ic_freq != chan->ic_freq &&
348 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
349 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
350 ic->ic_bsschan = chan;
351 ieee80211_node_set_chan(ni, chan);
352 ic->ic_curmode = ieee80211_chan2mode(chan);
354 * Do mode-specific setup.
356 if (IEEE80211_IS_CHAN_FULL(chan)) {
357 if (IEEE80211_IS_CHAN_ANYG(chan)) {
359 * Use a mixed 11b/11g basic rate set.
361 ieee80211_setbasicrates(&ni->ni_rates,
363 if (vap->iv_flags & IEEE80211_F_PUREG) {
365 * Also mark OFDM rates basic so 11b
366 * stations do not join (WiFi compliance).
368 ieee80211_addbasicrates(&ni->ni_rates,
371 } else if (IEEE80211_IS_CHAN_B(chan)) {
373 * Force pure 11b rate set.
375 ieee80211_setbasicrates(&ni->ni_rates,
380 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
384 * Reset bss state on transition to the INIT state.
385 * Clear any stations from the table (they have been
386 * deauth'd) and reset the bss node (clears key, rate
390 ieee80211_reset_bss(struct ieee80211vap *vap)
392 struct ieee80211com *ic = vap->iv_ic;
393 struct ieee80211_node *ni, *obss;
395 ieee80211_node_table_reset(&ic->ic_sta, vap);
396 /* XXX multi-bss: wrong */
397 ieee80211_reset_erp(ic);
399 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
400 KASSERT(ni != NULL, ("unable to setup inital BSS node"));
402 vap->iv_bss = ieee80211_ref_node(ni);
405 ni->ni_intval = ic->ic_bintval;
406 ieee80211_free_node(obss);
408 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
412 match_ssid(const struct ieee80211_node *ni,
413 int nssid, const struct ieee80211_scan_ssid ssids[])
417 for (i = 0; i < nssid; i++) {
418 if (ni->ni_esslen == ssids[i].len &&
419 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
426 * Test a node for suitability/compatibility.
429 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
431 struct ieee80211com *ic = ni->ni_ic;
434 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
436 if (vap->iv_opmode == IEEE80211_M_IBSS) {
437 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
440 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
443 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
444 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
447 /* XXX does this mean privacy is supported or required? */
448 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
451 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
452 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
453 if (rate & IEEE80211_RATE_BASIC)
455 if (vap->iv_des_nssid != 0 &&
456 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
458 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
459 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
464 #ifdef IEEE80211_DEBUG
466 * Display node suitability/compatibility.
469 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
471 struct ieee80211com *ic = ni->ni_ic;
476 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
478 if (vap->iv_opmode == IEEE80211_M_IBSS) {
479 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
482 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
485 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
486 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
489 /* XXX does this mean privacy is supported or required? */
490 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
493 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
494 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
495 if (rate & IEEE80211_RATE_BASIC)
497 if (vap->iv_des_nssid != 0 &&
498 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
500 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
501 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
504 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
505 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
507 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
508 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
509 fail & 0x08 ? '!' : ' ');
511 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
512 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
514 fail & 0x02 ? '!' : ' ');
516 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
517 fail & 0x04 ? '!' : ' ');
518 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
519 printf("%s\n", fail & 0x10 ? "!" : "");
521 #endif /* IEEE80211_DEBUG */
524 * Handle 802.11 ad hoc network merge. The
525 * convention, set by the Wireless Ethernet Compatibility Alliance
526 * (WECA), is that an 802.11 station will change its BSSID to match
527 * the "oldest" 802.11 ad hoc network, on the same channel, that
528 * has the station's desired SSID. The "oldest" 802.11 network
529 * sends beacons with the greatest TSF timestamp.
531 * The caller is assumed to validate TSF's before attempting a merge.
533 * Return !0 if the BSSID changed, 0 otherwise.
536 ieee80211_ibss_merge(struct ieee80211_node *ni)
538 struct ieee80211vap *vap = ni->ni_vap;
539 #ifdef IEEE80211_DEBUG
540 struct ieee80211com *ic = ni->ni_ic;
543 if (ni == vap->iv_bss ||
544 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
545 /* unchanged, nothing to do */
548 if (!check_bss(vap, ni)) {
549 /* capabilities mismatch */
550 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
551 "%s: merge failed, capabilities mismatch\n", __func__);
552 #ifdef IEEE80211_DEBUG
553 if (ieee80211_msg_assoc(vap))
554 check_bss_debug(vap, ni);
556 vap->iv_stats.is_ibss_capmismatch++;
559 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
560 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
561 ether_sprintf(ni->ni_bssid),
562 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
563 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
564 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
566 return ieee80211_sta_join1(ieee80211_ref_node(ni));
570 * Calculate HT channel promotion flags for all vaps.
571 * This assumes ni_chan have been setup for each vap.
574 gethtadjustflags(struct ieee80211com *ic)
576 struct ieee80211vap *vap;
581 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
582 if (vap->iv_state < IEEE80211_S_RUN)
584 switch (vap->iv_opmode) {
585 case IEEE80211_M_WDS:
586 case IEEE80211_M_STA:
587 case IEEE80211_M_AHDEMO:
588 case IEEE80211_M_HOSTAP:
589 case IEEE80211_M_IBSS:
590 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
600 * Check if the current channel needs to change based on whether
601 * any vap's are using HT20/HT40. This is used sync the state of
602 * ic_curchan after a channel width change on a running vap.
605 ieee80211_sync_curchan(struct ieee80211com *ic)
607 struct ieee80211_channel *c;
609 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
610 if (c != ic->ic_curchan) {
612 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
613 ic->ic_set_channel(ic);
618 * Change the current channel. The request channel may be
619 * promoted if other vap's are operating with HT20/HT40.
622 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
624 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
625 int flags = gethtadjustflags(ic);
627 * Check for channel promotion required to support the
628 * set of running vap's. This assumes we are called
629 * after ni_chan is setup for each vap.
631 /* NB: this assumes IEEE80211_FEXT_USEHT40 > IEEE80211_FEXT_HT */
632 if (flags > ieee80211_htchanflags(c))
633 c = ieee80211_ht_adjust_channel(ic, c, flags);
635 ic->ic_bsschan = ic->ic_curchan = c;
636 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
637 ic->ic_set_channel(ic);
641 * Join the specified IBSS/BSS network. The node is assumed to
642 * be passed in with a held reference.
645 ieee80211_sta_join1(struct ieee80211_node *selbs)
647 struct ieee80211vap *vap = selbs->ni_vap;
648 struct ieee80211com *ic = selbs->ni_ic;
649 struct ieee80211_node *obss;
653 * Committed to selbs, setup state.
657 * Check if old+new node have the same address in which
658 * case we can reassociate when operating in sta mode.
660 canreassoc = (obss != NULL &&
661 vap->iv_state == IEEE80211_S_RUN &&
662 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
663 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */
665 copy_bss(selbs, obss);
666 ieee80211_node_reclaim(obss);
667 obss = NULL; /* NB: guard against later use */
671 * Delete unusable rates; we've already checked
672 * that the negotiated rate set is acceptable.
674 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
675 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
677 ieee80211_setcurchan(ic, selbs->ni_chan);
679 * Set the erp state (mostly the slot time) to deal with
680 * the auto-select case; this should be redundant if the
683 ieee80211_reset_erp(ic);
684 ieee80211_wme_initparams(vap);
686 if (vap->iv_opmode == IEEE80211_M_STA) {
689 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
692 * Act as if we received a DEAUTH frame in case we
693 * are invoked from the RUN state. This will cause
694 * us to try to re-authenticate if we are operating
697 ieee80211_new_state(vap, IEEE80211_S_AUTH,
698 IEEE80211_FC0_SUBTYPE_DEAUTH);
701 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
706 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
707 const struct ieee80211_scan_entry *se)
709 struct ieee80211com *ic = vap->iv_ic;
710 struct ieee80211_node *ni;
712 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
718 * Expand scan state into node's format.
719 * XXX may not need all this stuff
721 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
722 ni->ni_esslen = se->se_ssid[1];
723 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
724 ni->ni_rstamp = se->se_rstamp;
725 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
726 ni->ni_intval = se->se_intval;
727 ni->ni_capinfo = se->se_capinfo;
729 ni->ni_timoff = se->se_timoff;
730 ni->ni_fhdwell = se->se_fhdwell;
731 ni->ni_fhindex = se->se_fhindex;
732 ni->ni_erp = se->se_erp;
733 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
734 ni->ni_noise = se->se_noise;
736 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
737 ieee80211_ies_expand(&ni->ni_ies);
738 if (ni->ni_ies.ath_ie != NULL)
739 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
740 if (ni->ni_ies.htcap_ie != NULL)
741 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
742 if (ni->ni_ies.htinfo_ie != NULL)
743 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
746 vap->iv_dtim_period = se->se_dtimperiod;
747 vap->iv_dtim_count = 0;
749 /* NB: must be after ni_chan is setup */
750 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
752 if (ieee80211_iserp_rateset(&ni->ni_rates))
753 ni->ni_flags |= IEEE80211_NODE_ERP;
754 node_setuptxparms(ni);
756 return ieee80211_sta_join1(ieee80211_ref_node(ni));
760 * Leave the specified IBSS/BSS network. The node is assumed to
761 * be passed in with a held reference.
764 ieee80211_sta_leave(struct ieee80211_node *ni)
766 struct ieee80211com *ic = ni->ni_ic;
768 ic->ic_node_cleanup(ni);
769 ieee80211_notify_node_leave(ni);
773 * Send a deauthenticate frame and drop the station.
776 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
778 /* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
779 ieee80211_ref_node(ni);
780 if (ni->ni_associd != 0)
781 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
782 ieee80211_node_leave(ni);
783 ieee80211_free_node(ni);
786 static struct ieee80211_node *
787 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
789 struct ieee80211_node *ni;
791 MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
792 M_80211_NODE, M_NOWAIT | M_ZERO);
797 * Initialize an ie blob with the specified data. If previous
798 * data exists re-use the data block. As a side effect we clear
799 * all references to specific ie's; the caller is required to
803 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
805 /* NB: assumes data+len are the last fields */
806 memset(ies, 0, offsetof(struct ieee80211_ies, data));
807 if (ies->data != NULL && ies->len != len) {
808 /* data size changed */
809 FREE(ies->data, M_80211_NODE_IE);
812 if (ies->data == NULL) {
813 MALLOC(ies->data, uint8_t *, len, M_80211_NODE_IE, M_NOWAIT);
814 if (ies->data == NULL) {
816 /* NB: pointers have already been zero'd above */
820 memcpy(ies->data, data, len);
826 * Reclaim storage for an ie blob.
829 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
831 if (ies->data != NULL)
832 FREE(ies->data, M_80211_NODE_IE);
836 * Expand an ie blob data contents and to fillin individual
837 * ie pointers. The data blob is assumed to be well-formed;
838 * we don't do any validity checking of ie lengths.
841 ieee80211_ies_expand(struct ieee80211_ies *ies)
850 case IEEE80211_ELEMID_VENDOR:
853 else if (iswmeoui(ie))
855 else if (isatherosoui(ie))
858 case IEEE80211_ELEMID_RSN:
861 case IEEE80211_ELEMID_HTCAP:
871 * Reclaim any resources in a node and reset any critical
872 * state. Typically nodes are free'd immediately after,
873 * but in some cases the storage may be reused so we need
874 * to insure consistent state (should probably fix that).
877 node_cleanup(struct ieee80211_node *ni)
879 #define N(a) (sizeof(a)/sizeof(a[0]))
880 struct ieee80211vap *vap = ni->ni_vap;
883 /* NB: preserve ni_table */
884 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
885 if (vap->iv_opmode != IEEE80211_M_STA)
887 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
888 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
889 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
892 * Cleanup any HT-related state.
894 if (ni->ni_flags & IEEE80211_NODE_HT)
895 ieee80211_ht_node_cleanup(ni);
897 * Clear AREF flag that marks the authorization refcnt bump
898 * has happened. This is probably not needed as the node
899 * should always be removed from the table so not found but
900 * do it just in case.
902 ni->ni_flags &= ~IEEE80211_NODE_AREF;
905 * Drain power save queue and, if needed, clear TIM.
907 if (ieee80211_node_saveq_drain(ni) != 0 && vap->iv_set_tim != NULL)
908 vap->iv_set_tim(ni, 0);
911 if (ni->ni_challenge != NULL) {
912 FREE(ni->ni_challenge, M_80211_NODE);
913 ni->ni_challenge = NULL;
916 * Preserve SSID, WPA, and WME ie's so the bss node is
917 * reusable during a re-auth/re-assoc state transition.
918 * If we remove these data they will not be recreated
919 * because they come from a probe-response or beacon frame
920 * which cannot be expected prior to the association-response.
921 * This should not be an issue when operating in other modes
922 * as stations leaving always go through a full state transition
923 * which will rebuild this state.
925 * XXX does this leave us open to inheriting old state?
927 for (i = 0; i < N(ni->ni_rxfrag); i++)
928 if (ni->ni_rxfrag[i] != NULL) {
929 m_freem(ni->ni_rxfrag[i]);
930 ni->ni_rxfrag[i] = NULL;
933 * Must be careful here to remove any key map entry w/o a LOR.
935 ieee80211_node_delucastkey(ni);
940 node_free(struct ieee80211_node *ni)
942 struct ieee80211com *ic = ni->ni_ic;
944 ic->ic_node_cleanup(ni);
945 ieee80211_ies_cleanup(&ni->ni_ies);
946 IEEE80211_NODE_SAVEQ_DESTROY(ni);
947 IEEE80211_NODE_WDSQ_DESTROY(ni);
948 FREE(ni, M_80211_NODE);
952 node_age(struct ieee80211_node *ni)
954 struct ieee80211vap *vap = ni->ni_vap;
956 IEEE80211_NODE_LOCK_ASSERT(&ic->ic_sta);
959 * Age frames on the power save queue.
961 if (ieee80211_node_saveq_age(ni) != 0 &&
962 IEEE80211_NODE_SAVEQ_QLEN(ni) == 0 &&
963 vap->iv_set_tim != NULL)
964 vap->iv_set_tim(ni, 0);
966 * Age frames on the wds pending queue.
968 if (IEEE80211_NODE_WDSQ_QLEN(ni) != 0)
969 ieee80211_node_wdsq_age(ni);
971 * Age out HT resources (e.g. frames on the
972 * A-MPDU reorder queues).
974 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
975 ieee80211_ht_node_age(ni);
979 node_getrssi(const struct ieee80211_node *ni)
981 uint32_t avgrssi = ni->ni_avgrssi;
984 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
986 rssi = IEEE80211_RSSI_GET(avgrssi);
987 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
991 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
993 *rssi = node_getrssi(ni);
994 *noise = ni->ni_noise;
998 node_getmimoinfo(const struct ieee80211_node *ni,
999 struct ieee80211_mimo_info *info)
1001 /* XXX zero data? */
1004 struct ieee80211_node *
1005 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1006 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1008 struct ieee80211com *ic = nt->nt_ic;
1009 struct ieee80211_node *ni;
1012 ni = ic->ic_node_alloc(vap, macaddr);
1014 vap->iv_stats.is_rx_nodealloc++;
1018 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1019 "%s %p<%s> in %s table\n", __func__, ni,
1020 ether_sprintf(macaddr), nt->nt_name);
1022 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1023 hash = IEEE80211_NODE_HASH(macaddr);
1024 ieee80211_node_initref(ni); /* mark referenced */
1025 ni->ni_chan = IEEE80211_CHAN_ANYC;
1026 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1027 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1028 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1029 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1030 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1031 ni->ni_inact_reload = nt->nt_inact_init;
1032 ni->ni_inact = ni->ni_inact_reload;
1033 ni->ni_ath_defkeyix = 0x7fff;
1034 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1035 IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1037 IEEE80211_NODE_LOCK(nt);
1038 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1039 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1043 IEEE80211_NODE_UNLOCK(nt);
1045 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1046 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1052 * Craft a temporary node suitable for sending a management frame
1053 * to the specified station. We craft only as much state as we
1054 * need to do the work since the node will be immediately reclaimed
1055 * once the send completes.
1057 struct ieee80211_node *
1058 ieee80211_tmp_node(struct ieee80211vap *vap,
1059 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1061 struct ieee80211com *ic = vap->iv_ic;
1062 struct ieee80211_node *ni;
1064 ni = ic->ic_node_alloc(vap, macaddr);
1066 struct ieee80211_node *bss = vap->iv_bss;
1068 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1069 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1071 ni->ni_table = NULL; /* NB: pedantic */
1072 ni->ni_ic = ic; /* NB: needed to set channel */
1075 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1076 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1077 ieee80211_node_initref(ni); /* mark referenced */
1078 /* NB: required by ieee80211_fix_rate */
1079 ieee80211_node_set_chan(ni, bss->ni_chan);
1080 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1081 IEEE80211_KEYIX_NONE);
1082 ni->ni_txpower = bss->ni_txpower;
1083 /* XXX optimize away */
1084 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1085 IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1088 vap->iv_stats.is_rx_nodealloc++;
1093 struct ieee80211_node *
1094 ieee80211_dup_bss(struct ieee80211vap *vap,
1095 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1097 struct ieee80211com *ic = vap->iv_ic;
1098 struct ieee80211_node *ni;
1100 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1102 struct ieee80211_node *bss = vap->iv_bss;
1104 * Inherit from iv_bss.
1107 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1108 ieee80211_node_set_chan(ni, bss->ni_chan);
1114 * Create a bss node for a legacy WDS vap. The far end does
1115 * not associate so we just create create a new node and
1116 * simulate an association. The caller is responsible for
1117 * installing the node as the bss node and handling any further
1118 * setup work like authorizing the port.
1120 struct ieee80211_node *
1121 ieee80211_node_create_wds(struct ieee80211vap *vap,
1122 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1124 struct ieee80211com *ic = vap->iv_ic;
1125 struct ieee80211_node *ni;
1127 /* XXX check if node already in sta table? */
1128 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1130 ni->ni_wdsvap = vap;
1131 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1133 * Inherit any manually configured settings.
1135 copy_bss(ni, vap->iv_bss);
1136 ieee80211_node_set_chan(ni, chan);
1137 /* NB: propagate ssid so available to WPA supplicant */
1138 ni->ni_esslen = vap->iv_des_ssid[0].len;
1139 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1140 /* NB: no associd for peer */
1142 * There are no management frames to use to
1143 * discover neighbor capabilities, so blindly
1144 * propagate the local configuration.
1146 if (vap->iv_flags & IEEE80211_F_WME)
1147 ni->ni_flags |= IEEE80211_NODE_QOS;
1148 if (vap->iv_flags & IEEE80211_F_FF)
1149 ni->ni_flags |= IEEE80211_NODE_FF;
1150 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1151 (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1153 * Device is HT-capable and HT is enabled for
1154 * the vap; setup HT operation. On return
1155 * ni_chan will be adjusted to an HT channel.
1157 ieee80211_ht_wds_init(ni);
1159 struct ieee80211_channel *c = ni->ni_chan;
1161 * Force a legacy channel to be used.
1163 c = ieee80211_find_channel(ic,
1164 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1165 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1166 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1173 struct ieee80211_node *
1174 #ifdef IEEE80211_DEBUG_REFCNT
1175 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1176 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1178 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1179 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1182 struct ieee80211_node *ni;
1185 IEEE80211_NODE_LOCK_ASSERT(nt);
1187 hash = IEEE80211_NODE_HASH(macaddr);
1188 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1189 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1190 ieee80211_ref_node(ni); /* mark referenced */
1191 #ifdef IEEE80211_DEBUG_REFCNT
1192 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1193 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1195 ni, ether_sprintf(ni->ni_macaddr),
1196 ieee80211_node_refcnt(ni));
1204 struct ieee80211_node *
1205 #ifdef IEEE80211_DEBUG_REFCNT
1206 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1207 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1209 ieee80211_find_node(struct ieee80211_node_table *nt,
1210 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1213 struct ieee80211_node *ni;
1215 IEEE80211_NODE_LOCK(nt);
1216 ni = ieee80211_find_node_locked(nt, macaddr);
1217 IEEE80211_NODE_UNLOCK(nt);
1221 struct ieee80211_node *
1222 #ifdef IEEE80211_DEBUG_REFCNT
1223 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1224 const struct ieee80211vap *vap,
1225 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1227 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1228 const struct ieee80211vap *vap,
1229 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1232 struct ieee80211_node *ni;
1235 IEEE80211_NODE_LOCK_ASSERT(nt);
1237 hash = IEEE80211_NODE_HASH(macaddr);
1238 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1239 if (ni->ni_vap == vap &&
1240 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1241 ieee80211_ref_node(ni); /* mark referenced */
1242 #ifdef IEEE80211_DEBUG_REFCNT
1243 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1244 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1246 ni, ether_sprintf(ni->ni_macaddr),
1247 ieee80211_node_refcnt(ni));
1255 struct ieee80211_node *
1256 #ifdef IEEE80211_DEBUG_REFCNT
1257 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1258 const struct ieee80211vap *vap,
1259 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1261 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1262 const struct ieee80211vap *vap,
1263 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1266 struct ieee80211_node *ni;
1268 IEEE80211_NODE_LOCK(nt);
1269 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1270 IEEE80211_NODE_UNLOCK(nt);
1275 * Fake up a node; this handles node discovery in adhoc mode.
1276 * Note that for the driver's benefit we we treat this like
1277 * an association so the driver has an opportunity to setup
1278 * it's private state.
1280 struct ieee80211_node *
1281 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1282 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1284 struct ieee80211_node *ni;
1286 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1287 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1288 ni = ieee80211_dup_bss(vap, macaddr);
1290 struct ieee80211com *ic = vap->iv_ic;
1292 /* XXX no rate negotiation; just dup */
1293 ni->ni_rates = vap->iv_bss->ni_rates;
1294 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1296 * In adhoc demo mode there are no management
1297 * frames to use to discover neighbor capabilities,
1298 * so blindly propagate the local configuration
1299 * so we can do interesting things (e.g. use
1300 * WME to disable ACK's).
1302 if (vap->iv_flags & IEEE80211_F_WME)
1303 ni->ni_flags |= IEEE80211_NODE_QOS;
1304 if (vap->iv_flags & IEEE80211_F_FF)
1305 ni->ni_flags |= IEEE80211_NODE_FF;
1307 node_setuptxparms(ni);
1308 if (ic->ic_newassoc != NULL)
1309 ic->ic_newassoc(ni, 1);
1310 /* XXX not right for 802.1x/WPA */
1311 ieee80211_node_authorize(ni);
1317 ieee80211_init_neighbor(struct ieee80211_node *ni,
1318 const struct ieee80211_frame *wh,
1319 const struct ieee80211_scanparams *sp)
1321 ni->ni_esslen = sp->ssid[1];
1322 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1323 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1324 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1325 ni->ni_intval = sp->bintval;
1326 ni->ni_capinfo = sp->capinfo;
1327 ni->ni_chan = ni->ni_ic->ic_curchan;
1328 ni->ni_fhdwell = sp->fhdwell;
1329 ni->ni_fhindex = sp->fhindex;
1330 ni->ni_erp = sp->erp;
1331 ni->ni_timoff = sp->timoff;
1333 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1334 ieee80211_ies_expand(&ni->ni_ies);
1335 if (ni->ni_ies.ath_ie != NULL)
1336 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1339 /* NB: must be after ni_chan is setup */
1340 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1341 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1342 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1346 * Do node discovery in adhoc mode on receipt of a beacon
1347 * or probe response frame. Note that for the driver's
1348 * benefit we we treat this like an association so the
1349 * driver has an opportunity to setup it's private state.
1351 struct ieee80211_node *
1352 ieee80211_add_neighbor(struct ieee80211vap *vap,
1353 const struct ieee80211_frame *wh,
1354 const struct ieee80211_scanparams *sp)
1356 struct ieee80211_node *ni;
1358 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1359 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1360 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1362 struct ieee80211com *ic = vap->iv_ic;
1364 ieee80211_init_neighbor(ni, wh, sp);
1365 node_setuptxparms(ni);
1366 if (ic->ic_newassoc != NULL)
1367 ic->ic_newassoc(ni, 1);
1368 /* XXX not right for 802.1x/WPA */
1369 ieee80211_node_authorize(ni);
1374 #define IS_CTL(wh) \
1375 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1376 #define IS_PSPOLL(wh) \
1377 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1378 #define IS_BAR(wh) \
1379 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
1380 #define IS_PROBEREQ(wh) \
1381 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1382 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1383 #define IS_BCAST_PROBEREQ(wh) \
1384 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1385 ((const struct ieee80211_frame *)(wh))->i_addr3))
1387 static __inline struct ieee80211_node *
1388 _find_rxnode(struct ieee80211_node_table *nt,
1389 const struct ieee80211_frame_min *wh)
1391 /* XXX 4-address frames? */
1392 if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1393 return ieee80211_find_node_locked(nt, wh->i_addr1);
1394 if (IS_BCAST_PROBEREQ(wh))
1395 return NULL; /* spam bcast probe req to all vap's */
1396 return ieee80211_find_node_locked(nt, wh->i_addr2);
1400 * Locate the node for sender, track state, and then pass the
1401 * (referenced) node up to the 802.11 layer for its use. Note
1402 * we can return NULL if the sender is not in the table.
1404 struct ieee80211_node *
1405 #ifdef IEEE80211_DEBUG_REFCNT
1406 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1407 const struct ieee80211_frame_min *wh, const char *func, int line)
1409 ieee80211_find_rxnode(struct ieee80211com *ic,
1410 const struct ieee80211_frame_min *wh)
1413 struct ieee80211_node_table *nt;
1414 struct ieee80211_node *ni;
1417 IEEE80211_NODE_LOCK(nt);
1418 ni = _find_rxnode(nt, wh);
1419 IEEE80211_NODE_UNLOCK(nt);
1425 * Like ieee80211_find_rxnode but use the supplied h/w
1426 * key index as a hint to locate the node in the key
1427 * mapping table. If an entry is present at the key
1428 * index we return it; otherwise do a normal lookup and
1429 * update the mapping table if the station has a unicast
1430 * key assigned to it.
1432 struct ieee80211_node *
1433 #ifdef IEEE80211_DEBUG_REFCNT
1434 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1435 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1436 const char *func, int line)
1438 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1439 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1442 struct ieee80211_node_table *nt;
1443 struct ieee80211_node *ni;
1446 IEEE80211_NODE_LOCK(nt);
1447 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1448 ni = nt->nt_keyixmap[keyix];
1452 ni = _find_rxnode(nt, wh);
1453 if (ni != NULL && nt->nt_keyixmap != NULL) {
1455 * If the station has a unicast key cache slot
1456 * assigned update the key->node mapping table.
1458 keyix = ni->ni_ucastkey.wk_rxkeyix;
1459 /* XXX can keyixmap[keyix] != NULL? */
1460 if (keyix < nt->nt_keyixmax &&
1461 nt->nt_keyixmap[keyix] == NULL) {
1462 IEEE80211_DPRINTF(ni->ni_vap,
1464 "%s: add key map entry %p<%s> refcnt %d\n",
1465 __func__, ni, ether_sprintf(ni->ni_macaddr),
1466 ieee80211_node_refcnt(ni)+1);
1467 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1471 if (IS_BCAST_PROBEREQ(wh))
1472 ni = NULL; /* spam bcast probe req to all vap's */
1474 ieee80211_ref_node(ni);
1476 IEEE80211_NODE_UNLOCK(nt);
1480 #undef IS_BCAST_PROBEREQ
1487 * Return a reference to the appropriate node for sending
1488 * a data frame. This handles node discovery in adhoc networks.
1490 struct ieee80211_node *
1491 #ifdef IEEE80211_DEBUG_REFCNT
1492 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1493 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1494 const char *func, int line)
1496 ieee80211_find_txnode(struct ieee80211vap *vap,
1497 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1500 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1501 struct ieee80211_node *ni;
1504 * The destination address should be in the node table
1505 * unless this is a multicast/broadcast frame. We can
1506 * also optimize station mode operation, all frames go
1509 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1510 IEEE80211_NODE_LOCK(nt);
1511 if (vap->iv_opmode == IEEE80211_M_STA ||
1512 vap->iv_opmode == IEEE80211_M_WDS ||
1513 IEEE80211_IS_MULTICAST(macaddr))
1514 ni = ieee80211_ref_node(vap->iv_bss);
1516 ni = ieee80211_find_node_locked(nt, macaddr);
1517 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1518 (ni != NULL && ni->ni_associd == 0)) {
1520 * Station is not associated; don't permit the
1521 * data frame to be sent by returning NULL. This
1522 * is kinda a kludge but the least intrusive way
1523 * to add this check into all drivers.
1525 ieee80211_unref_node(&ni); /* NB: null's ni */
1528 IEEE80211_NODE_UNLOCK(nt);
1531 if (vap->iv_opmode == IEEE80211_M_IBSS ||
1532 vap->iv_opmode == IEEE80211_M_AHDEMO) {
1534 * In adhoc mode cons up a node for the destination.
1535 * Note that we need an additional reference for the
1536 * caller to be consistent with
1537 * ieee80211_find_node_locked.
1539 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1541 (void) ieee80211_ref_node(ni);
1543 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1544 "no node, discard frame (%s)", __func__);
1545 vap->iv_stats.is_tx_nonode++;
1552 _ieee80211_free_node(struct ieee80211_node *ni)
1554 struct ieee80211_node_table *nt = ni->ni_table;
1557 * NB: careful about referencing the vap as it may be
1558 * gone if the last reference was held by a driver.
1559 * We know the com will always be present so it's safe
1560 * to use ni_ic below to reclaim resources.
1563 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1564 "%s %p<%s> in %s table\n", __func__, ni,
1565 ether_sprintf(ni->ni_macaddr),
1566 nt != NULL ? nt->nt_name : "<gone>");
1568 if (ni->ni_associd != 0) {
1569 struct ieee80211vap *vap = ni->ni_vap;
1570 if (vap->iv_aid_bitmap != NULL)
1571 IEEE80211_AID_CLR(vap, ni->ni_associd);
1574 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1575 LIST_REMOVE(ni, ni_hash);
1577 ni->ni_ic->ic_node_free(ni);
1581 #ifdef IEEE80211_DEBUG_REFCNT
1582 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1584 ieee80211_free_node(struct ieee80211_node *ni)
1587 struct ieee80211_node_table *nt = ni->ni_table;
1589 #ifdef IEEE80211_DEBUG_REFCNT
1590 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1591 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1592 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1595 IEEE80211_NODE_LOCK(nt);
1596 if (ieee80211_node_dectestref(ni)) {
1598 * Last reference, reclaim state.
1600 _ieee80211_free_node(ni);
1601 } else if (ieee80211_node_refcnt(ni) == 1 &&
1602 nt->nt_keyixmap != NULL) {
1603 ieee80211_keyix keyix;
1605 * Check for a last reference in the key mapping table.
1607 keyix = ni->ni_ucastkey.wk_rxkeyix;
1608 if (keyix < nt->nt_keyixmax &&
1609 nt->nt_keyixmap[keyix] == ni) {
1610 IEEE80211_DPRINTF(ni->ni_vap,
1612 "%s: %p<%s> clear key map entry", __func__,
1613 ni, ether_sprintf(ni->ni_macaddr));
1614 nt->nt_keyixmap[keyix] = NULL;
1615 ieee80211_node_decref(ni); /* XXX needed? */
1616 _ieee80211_free_node(ni);
1619 IEEE80211_NODE_UNLOCK(nt);
1621 if (ieee80211_node_dectestref(ni))
1622 _ieee80211_free_node(ni);
1627 * Reclaim a unicast key and clear any key cache state.
1630 ieee80211_node_delucastkey(struct ieee80211_node *ni)
1632 struct ieee80211com *ic = ni->ni_ic;
1633 struct ieee80211_node_table *nt = &ic->ic_sta;
1634 struct ieee80211_node *nikey;
1635 ieee80211_keyix keyix;
1636 int isowned, status;
1639 * NB: We must beware of LOR here; deleting the key
1640 * can cause the crypto layer to block traffic updates
1641 * which can generate a LOR against the node table lock;
1642 * grab it here and stash the key index for our use below.
1644 * Must also beware of recursion on the node table lock.
1645 * When called from node_cleanup we may already have
1646 * the node table lock held. Unfortunately there's no
1647 * way to separate out this path so we must do this
1650 isowned = IEEE80211_NODE_IS_LOCKED(nt);
1652 IEEE80211_NODE_LOCK(nt);
1654 status = 1; /* NB: success */
1655 if (!IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1656 keyix = ni->ni_ucastkey.wk_rxkeyix;
1657 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1658 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1659 nikey = nt->nt_keyixmap[keyix];
1660 nt->nt_keyixmap[keyix] = NULL;;
1664 IEEE80211_NODE_UNLOCK(nt);
1666 if (nikey != NULL) {
1667 KASSERT(nikey == ni,
1668 ("key map out of sync, ni %p nikey %p", ni, nikey));
1669 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1670 "%s: delete key map entry %p<%s> refcnt %d\n",
1671 __func__, ni, ether_sprintf(ni->ni_macaddr),
1672 ieee80211_node_refcnt(ni)-1);
1673 ieee80211_free_node(ni);
1679 * Reclaim a node. If this is the last reference count then
1680 * do the normal free work. Otherwise remove it from the node
1681 * table and mark it gone by clearing the back-reference.
1684 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1686 ieee80211_keyix keyix;
1688 IEEE80211_NODE_LOCK_ASSERT(nt);
1690 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1691 "%s: remove %p<%s> from %s table, refcnt %d\n",
1692 __func__, ni, ether_sprintf(ni->ni_macaddr),
1693 nt->nt_name, ieee80211_node_refcnt(ni)-1);
1695 * Clear any entry in the unicast key mapping table.
1696 * We need to do it here so rx lookups don't find it
1697 * in the mapping table even if it's not in the hash
1698 * table. We cannot depend on the mapping table entry
1699 * being cleared because the node may not be free'd.
1701 keyix = ni->ni_ucastkey.wk_rxkeyix;
1702 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1703 nt->nt_keyixmap[keyix] == ni) {
1704 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1705 "%s: %p<%s> clear key map entry\n",
1706 __func__, ni, ether_sprintf(ni->ni_macaddr));
1707 nt->nt_keyixmap[keyix] = NULL;
1708 ieee80211_node_decref(ni); /* NB: don't need free */
1710 if (!ieee80211_node_dectestref(ni)) {
1712 * Other references are present, just remove the
1713 * node from the table so it cannot be found. When
1714 * the references are dropped storage will be
1717 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1718 LIST_REMOVE(ni, ni_hash);
1719 ni->ni_table = NULL; /* clear reference */
1721 _ieee80211_free_node(ni);
1725 * Reclaim a (bss) node. Decrement the refcnt and reclaim
1726 * the node if the only other reference to it is in the sta
1727 * table. This is effectively ieee80211_free_node followed
1728 * by node_reclaim when the refcnt is 1 (after the free).
1731 ieee80211_node_reclaim(struct ieee80211_node *ni)
1733 struct ieee80211_node_table *nt = ni->ni_table;
1735 KASSERT(nt != NULL, ("reclaim node not in table"));
1737 #ifdef IEEE80211_DEBUG_REFCNT
1738 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1739 "%s %p<%s> refcnt %d\n", __func__, ni,
1740 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1742 IEEE80211_NODE_LOCK(nt);
1743 if (ieee80211_node_dectestref(ni)) {
1745 * Last reference, reclaim state.
1747 _ieee80211_free_node(ni);
1749 } else if (ieee80211_node_refcnt(ni) == 1 &&
1750 nt->nt_keyixmap != NULL) {
1751 ieee80211_keyix keyix;
1753 * Check for a last reference in the key mapping table.
1755 keyix = ni->ni_ucastkey.wk_rxkeyix;
1756 if (keyix < nt->nt_keyixmax &&
1757 nt->nt_keyixmap[keyix] == ni) {
1758 IEEE80211_DPRINTF(ni->ni_vap,
1760 "%s: %p<%s> clear key map entry", __func__,
1761 ni, ether_sprintf(ni->ni_macaddr));
1762 nt->nt_keyixmap[keyix] = NULL;
1763 ieee80211_node_decref(ni); /* XXX needed? */
1764 _ieee80211_free_node(ni);
1768 if (nt != NULL && ieee80211_node_refcnt(ni) == 1) {
1770 * Last reference is in the sta table; complete
1771 * the reclaim. This handles bss nodes being
1772 * recycled: the node has two references, one for
1773 * iv_bss and one for the table. After dropping
1774 * the iv_bss ref above we need to reclaim the sta
1777 ieee80211_node_decref(ni); /* NB: be pendantic */
1778 _ieee80211_free_node(ni);
1780 IEEE80211_NODE_UNLOCK(nt);
1784 * Node table support.
1788 ieee80211_node_table_init(struct ieee80211com *ic,
1789 struct ieee80211_node_table *nt,
1790 const char *name, int inact, int keyixmax)
1792 struct ifnet *ifp = ic->ic_ifp;
1795 IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1796 IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1797 TAILQ_INIT(&nt->nt_node);
1800 nt->nt_inact_init = inact;
1801 nt->nt_keyixmax = keyixmax;
1802 if (nt->nt_keyixmax > 0) {
1803 MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
1804 keyixmax * sizeof(struct ieee80211_node *),
1805 M_80211_NODE, M_NOWAIT | M_ZERO);
1806 if (nt->nt_keyixmap == NULL)
1807 if_printf(ic->ic_ifp,
1808 "Cannot allocate key index map with %u entries\n",
1811 nt->nt_keyixmap = NULL;
1815 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
1816 struct ieee80211vap *match)
1818 struct ieee80211_node *ni, *next;
1820 IEEE80211_NODE_LOCK(nt);
1821 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1822 if (match != NULL && ni->ni_vap != match)
1824 /* XXX can this happen? if so need's work */
1825 if (ni->ni_associd != 0) {
1826 struct ieee80211vap *vap = ni->ni_vap;
1828 if (vap->iv_auth->ia_node_leave != NULL)
1829 vap->iv_auth->ia_node_leave(ni);
1830 if (vap->iv_aid_bitmap != NULL)
1831 IEEE80211_AID_CLR(vap, ni->ni_associd);
1833 ni->ni_wdsvap = NULL; /* clear reference */
1834 node_reclaim(nt, ni);
1836 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1838 * Make a separate pass to clear references to this vap
1839 * held by DWDS entries. They will not be matched above
1840 * because ni_vap will point to the ap vap but we still
1841 * need to clear ni_wdsvap when the WDS vap is destroyed
1844 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1845 if (ni->ni_wdsvap == match)
1846 ni->ni_wdsvap = NULL;
1848 IEEE80211_NODE_UNLOCK(nt);
1852 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1854 ieee80211_node_table_reset(nt, NULL);
1855 if (nt->nt_keyixmap != NULL) {
1857 /* XXX verify all entries are NULL */
1859 for (i = 0; i < nt->nt_keyixmax; i++)
1860 if (nt->nt_keyixmap[i] != NULL)
1861 printf("%s: %s[%u] still active\n", __func__,
1864 FREE(nt->nt_keyixmap, M_80211_NODE);
1865 nt->nt_keyixmap = NULL;
1867 IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1868 IEEE80211_NODE_LOCK_DESTROY(nt);
1872 * Timeout inactive stations and do related housekeeping.
1873 * Note that we cannot hold the node lock while sending a
1874 * frame as this would lead to a LOR. Instead we use a
1875 * generation number to mark nodes that we've scanned and
1876 * drop the lock and restart a scan if we have to time out
1877 * a node. Since we are single-threaded by virtue of
1878 * controlling the inactivity timer we can be sure this will
1879 * process each node only once.
1882 ieee80211_timeout_stations(struct ieee80211com *ic)
1884 struct ieee80211_node_table *nt = &ic->ic_sta;
1885 struct ieee80211vap *vap;
1886 struct ieee80211_node *ni;
1889 IEEE80211_NODE_ITERATE_LOCK(nt);
1890 gen = ++nt->nt_scangen;
1892 IEEE80211_NODE_LOCK(nt);
1893 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1894 if (ni->ni_scangen == gen) /* previously handled */
1896 ni->ni_scangen = gen;
1898 * Ignore entries for which have yet to receive an
1899 * authentication frame. These are transient and
1900 * will be reclaimed when the last reference to them
1901 * goes away (when frame xmits complete).
1905 * Only process stations when in RUN state. This
1906 * insures, for example, that we don't timeout an
1907 * inactive station during CAC. Note that CSA state
1908 * is actually handled in ieee80211_node_timeout as
1909 * it applies to more than timeout processing.
1911 if (vap->iv_state != IEEE80211_S_RUN)
1913 /* XXX can vap be NULL? */
1914 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
1915 vap->iv_opmode == IEEE80211_M_STA) &&
1916 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1919 * Free fragment if not needed anymore
1920 * (last fragment older than 1s).
1921 * XXX doesn't belong here, move to node_age
1923 if (ni->ni_rxfrag[0] != NULL &&
1924 ticks > ni->ni_rxfragstamp + hz) {
1925 m_freem(ni->ni_rxfrag[0]);
1926 ni->ni_rxfrag[0] = NULL;
1928 if (ni->ni_inact > 0) {
1930 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1931 "%s: inact %u inact_reload %u nrates %u",
1932 __func__, ni->ni_inact, ni->ni_inact_reload,
1933 ni->ni_rates.rs_nrates);
1936 * Special case ourself; we may be idle for extended periods
1937 * of time and regardless reclaiming our state is wrong.
1938 * XXX run ic_node_age
1940 if (ni == vap->iv_bss)
1942 if (ni->ni_associd != 0 ||
1943 (vap->iv_opmode == IEEE80211_M_IBSS ||
1944 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
1946 * Age/drain resources held by the station.
1948 ic->ic_node_age(ni);
1950 * Probe the station before time it out. We
1951 * send a null data frame which may not be
1952 * universally supported by drivers (need it
1953 * for ps-poll support so it should be...).
1955 * XXX don't probe the station unless we've
1956 * received a frame from them (and have
1957 * some idea of the rates they are capable
1958 * of); this will get fixed more properly
1959 * soon with better handling of the rate set.
1961 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1962 (0 < ni->ni_inact &&
1963 ni->ni_inact <= vap->iv_inact_probe) &&
1964 ni->ni_rates.rs_nrates != 0) {
1966 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1968 "probe station due to inactivity");
1970 * Grab a reference before unlocking the table
1971 * so the node cannot be reclaimed before we
1972 * send the frame. ieee80211_send_nulldata
1973 * understands we've done this and reclaims the
1974 * ref for us as needed.
1976 ieee80211_ref_node(ni);
1977 IEEE80211_NODE_UNLOCK(nt);
1978 ieee80211_send_nulldata(ni);
1983 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1984 ni->ni_inact <= 0) {
1986 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1987 "station timed out due to inactivity "
1988 "(refcnt %u)", ieee80211_node_refcnt(ni));
1990 * Send a deauthenticate frame and drop the station.
1991 * This is somewhat complicated due to reference counts
1992 * and locking. At this point a station will typically
1993 * have a reference count of 1. ieee80211_node_leave
1994 * will do a "free" of the node which will drop the
1995 * reference count. But in the meantime a reference
1996 * wil be held by the deauth frame. The actual reclaim
1997 * of the node will happen either after the tx is
1998 * completed or by ieee80211_node_leave.
2000 * Separately we must drop the node lock before sending
2001 * in case the driver takes a lock, as this can result
2002 * in a LOR between the node lock and the driver lock.
2004 ieee80211_ref_node(ni);
2005 IEEE80211_NODE_UNLOCK(nt);
2006 if (ni->ni_associd != 0) {
2007 IEEE80211_SEND_MGMT(ni,
2008 IEEE80211_FC0_SUBTYPE_DEAUTH,
2009 IEEE80211_REASON_AUTH_EXPIRE);
2011 ieee80211_node_leave(ni);
2012 ieee80211_free_node(ni);
2013 vap->iv_stats.is_node_timeout++;
2017 IEEE80211_NODE_UNLOCK(nt);
2019 IEEE80211_NODE_ITERATE_UNLOCK(nt);
2023 * Aggressively reclaim resources. This should be used
2024 * only in a critical situation to reclaim mbuf resources.
2027 ieee80211_drain(struct ieee80211com *ic)
2029 struct ieee80211_node_table *nt = &ic->ic_sta;
2030 struct ieee80211vap *vap;
2031 struct ieee80211_node *ni;
2033 IEEE80211_NODE_LOCK(nt);
2034 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2036 * Ignore entries for which have yet to receive an
2037 * authentication frame. These are transient and
2038 * will be reclaimed when the last reference to them
2039 * goes away (when frame xmits complete).
2043 * Only process stations when in RUN state. This
2044 * insures, for example, that we don't timeout an
2045 * inactive station during CAC. Note that CSA state
2046 * is actually handled in ieee80211_node_timeout as
2047 * it applies to more than timeout processing.
2049 if (vap->iv_state != IEEE80211_S_RUN)
2051 /* XXX can vap be NULL? */
2052 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2053 vap->iv_opmode == IEEE80211_M_STA) &&
2054 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2058 * XXX doesn't belong here, move to node_drain
2060 if (ni->ni_rxfrag[0] != NULL) {
2061 m_freem(ni->ni_rxfrag[0]);
2062 ni->ni_rxfrag[0] = NULL;
2065 * Drain resources held by the station.
2067 ic->ic_node_drain(ni);
2069 IEEE80211_NODE_UNLOCK(nt);
2073 * Per-ieee80211com inactivity timer callback.
2076 ieee80211_node_timeout(void *arg)
2078 struct ieee80211com *ic = arg;
2081 * Defer timeout processing if a channel switch is pending.
2082 * We typically need to be mute so not doing things that
2083 * might generate frames is good to handle in one place.
2084 * Supressing the station timeout processing may extend the
2085 * lifetime of inactive stations (by not decrementing their
2086 * idle counters) but this should be ok unless the CSA is
2087 * active for an unusually long time.
2089 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2090 ieee80211_scan_timeout(ic);
2091 ieee80211_timeout_stations(ic);
2094 ieee80211_erp_timeout(ic);
2095 ieee80211_ht_timeout(ic);
2096 IEEE80211_UNLOCK(ic);
2098 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2099 ieee80211_node_timeout, ic);
2103 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2104 ieee80211_iter_func *f, void *arg)
2106 struct ieee80211_node *ni;
2109 IEEE80211_NODE_ITERATE_LOCK(nt);
2110 gen = ++nt->nt_scangen;
2112 IEEE80211_NODE_LOCK(nt);
2113 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2114 if (ni->ni_scangen != gen) {
2115 ni->ni_scangen = gen;
2116 (void) ieee80211_ref_node(ni);
2117 IEEE80211_NODE_UNLOCK(nt);
2119 ieee80211_free_node(ni);
2123 IEEE80211_NODE_UNLOCK(nt);
2125 IEEE80211_NODE_ITERATE_UNLOCK(nt);
2129 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2131 printf("0x%p: mac %s refcnt %d\n", ni,
2132 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2133 printf("\tscangen %u authmode %u flags 0x%x\n",
2134 ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2135 printf("\tassocid 0x%x txpower %u vlan %u\n",
2136 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2137 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2138 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2139 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2140 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2141 ni->ni_rxfragstamp);
2142 printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
2143 ni->ni_rstamp, node_getrssi(ni), ni->ni_noise,
2144 ni->ni_intval, ni->ni_capinfo);
2145 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2146 ether_sprintf(ni->ni_bssid),
2147 ni->ni_esslen, ni->ni_essid,
2148 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2149 printf("\tinact %u inact_reload %u txrate %u\n",
2150 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2151 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2152 ni->ni_htcap, ni->ni_htparam,
2153 ni->ni_htctlchan, ni->ni_ht2ndchan);
2154 printf("\thtopmode %x htstbc %x chw %u\n",
2155 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2159 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2161 ieee80211_iterate_nodes(nt,
2162 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2166 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2168 struct ieee80211vap *vap;
2170 IEEE80211_LOCK_ASSERT(ic);
2172 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2173 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2174 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2178 ieee80211_notify_erp(struct ieee80211com *ic)
2181 ieee80211_notify_erp_locked(ic);
2182 IEEE80211_UNLOCK(ic);
2186 * Handle a station joining an 11g network.
2189 ieee80211_node_join_11g(struct ieee80211_node *ni)
2191 struct ieee80211com *ic = ni->ni_ic;
2193 IEEE80211_LOCK_ASSERT(ic);
2196 * Station isn't capable of short slot time. Bump
2197 * the count of long slot time stations and disable
2198 * use of short slot time. Note that the actual switch
2199 * over to long slot time use may not occur until the
2200 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2202 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2203 ic->ic_longslotsta++;
2204 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2205 "station needs long slot time, count %d",
2206 ic->ic_longslotsta);
2207 /* XXX vap's w/ conflicting needs won't work */
2208 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2210 * Don't force slot time when switched to turbo
2211 * mode as non-ERP stations won't be present; this
2212 * need only be done when on the normal G channel.
2214 ieee80211_set_shortslottime(ic, 0);
2218 * If the new station is not an ERP station
2219 * then bump the counter and enable protection
2222 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2224 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2225 "station is !ERP, %d non-ERP stations associated",
2228 * If station does not support short preamble
2229 * then we must enable use of Barker preamble.
2231 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2232 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2233 "%s", "station needs long preamble");
2234 ic->ic_flags |= IEEE80211_F_USEBARKER;
2235 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2238 * If protection is configured and this is the first
2239 * indication we should use protection, enable it.
2241 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2242 ic->ic_nonerpsta == 1 &&
2243 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2244 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2245 "%s: enable use of protection\n", __func__);
2246 ic->ic_flags |= IEEE80211_F_USEPROT;
2247 ieee80211_notify_erp_locked(ic);
2250 ni->ni_flags |= IEEE80211_NODE_ERP;
2254 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2256 struct ieee80211com *ic = ni->ni_ic;
2257 struct ieee80211vap *vap = ni->ni_vap;
2260 if (ni->ni_associd == 0) {
2263 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2265 * It would be good to search the bitmap
2266 * more efficiently, but this will do for now.
2268 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2269 if (!IEEE80211_AID_ISSET(vap, aid))
2272 if (aid >= vap->iv_max_aid) {
2273 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2274 ieee80211_node_leave(ni);
2277 ni->ni_associd = aid | 0xc000;
2278 ni->ni_jointime = time_uptime;
2280 IEEE80211_AID_SET(vap, ni->ni_associd);
2281 vap->iv_sta_assoc++;
2284 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2285 ieee80211_ht_node_join(ni);
2286 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2287 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2288 ieee80211_node_join_11g(ni);
2289 IEEE80211_UNLOCK(ic);
2295 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2296 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2297 IEEE80211_NODE_AID(ni),
2298 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2299 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2300 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2301 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2302 ni->ni_flags & IEEE80211_NODE_HT ?
2303 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2304 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2305 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2306 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2307 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2308 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2309 ", fast-frames" : "",
2310 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2314 node_setuptxparms(ni);
2315 /* give driver a chance to setup state like ni_txrate */
2316 if (ic->ic_newassoc != NULL)
2317 ic->ic_newassoc(ni, newassoc);
2318 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2319 /* tell the authenticator about new station */
2320 if (vap->iv_auth->ia_node_join != NULL)
2321 vap->iv_auth->ia_node_join(ni);
2322 ieee80211_notify_node_join(ni,
2323 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2327 disable_protection(struct ieee80211com *ic)
2329 KASSERT(ic->ic_nonerpsta == 0 &&
2330 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2331 ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2334 ic->ic_flags &= ~IEEE80211_F_USEPROT;
2335 /* XXX verify mode? */
2336 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2337 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2338 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2340 ieee80211_notify_erp_locked(ic);
2344 * Handle a station leaving an 11g network.
2347 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2349 struct ieee80211com *ic = ni->ni_ic;
2351 IEEE80211_LOCK_ASSERT(ic);
2353 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2354 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2355 ic->ic_bsschan->ic_flags));
2358 * If a long slot station do the slot time bookkeeping.
2360 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2361 KASSERT(ic->ic_longslotsta > 0,
2362 ("bogus long slot station count %d", ic->ic_longslotsta));
2363 ic->ic_longslotsta--;
2364 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2365 "long slot time station leaves, count now %d",
2366 ic->ic_longslotsta);
2367 if (ic->ic_longslotsta == 0) {
2369 * Re-enable use of short slot time if supported
2370 * and not operating in IBSS mode (per spec).
2372 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2373 ic->ic_opmode != IEEE80211_M_IBSS) {
2374 IEEE80211_DPRINTF(ni->ni_vap,
2375 IEEE80211_MSG_ASSOC,
2376 "%s: re-enable use of short slot time\n",
2378 ieee80211_set_shortslottime(ic, 1);
2383 * If a non-ERP station do the protection-related bookkeeping.
2385 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2386 KASSERT(ic->ic_nonerpsta > 0,
2387 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2389 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2390 "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2391 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2392 " (non-ERP sta present)" : "");
2393 if (ic->ic_nonerpsta == 0 &&
2394 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2395 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2396 "%s: disable use of protection\n", __func__);
2397 disable_protection(ic);
2403 * Time out presence of an overlapping bss with non-ERP
2404 * stations. When operating in hostap mode we listen for
2405 * beacons from other stations and if we identify a non-ERP
2406 * station is present we enable protection. To identify
2407 * when all non-ERP stations are gone we time out this
2411 ieee80211_erp_timeout(struct ieee80211com *ic)
2414 IEEE80211_LOCK_ASSERT(ic);
2416 if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2417 time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2419 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2420 "%s", "age out non-ERP sta present on channel");
2422 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2423 if (ic->ic_nonerpsta == 0)
2424 disable_protection(ic);
2429 * Handle bookkeeping for station deauthentication/disassociation
2430 * when operating as an ap.
2433 ieee80211_node_leave(struct ieee80211_node *ni)
2435 struct ieee80211com *ic = ni->ni_ic;
2436 struct ieee80211vap *vap = ni->ni_vap;
2437 struct ieee80211_node_table *nt = ni->ni_table;
2439 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2440 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2442 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2443 ("unexpected operating mode %u", vap->iv_opmode));
2445 * If node wasn't previously associated all
2446 * we need to do is reclaim the reference.
2448 /* XXX ibss mode bypasses 11g and notification */
2449 if (ni->ni_associd == 0)
2452 * Tell the authenticator the station is leaving.
2453 * Note that we must do this before yanking the
2454 * association id as the authenticator uses the
2455 * associd to locate it's state block.
2457 if (vap->iv_auth->ia_node_leave != NULL)
2458 vap->iv_auth->ia_node_leave(ni);
2461 IEEE80211_AID_CLR(vap, ni->ni_associd);
2463 vap->iv_sta_assoc--;
2466 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2467 ieee80211_ht_node_leave(ni);
2468 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2469 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2470 ieee80211_node_leave_11g(ni);
2471 IEEE80211_UNLOCK(ic);
2473 * Cleanup station state. In particular clear various
2474 * state that might otherwise be reused if the node
2475 * is reused before the reference count goes to zero
2476 * (and memory is reclaimed).
2478 ieee80211_sta_leave(ni);
2481 * Remove the node from any table it's recorded in and
2482 * drop the caller's reference. Removal from the table
2483 * is important to insure the node is not reprocessed
2487 IEEE80211_NODE_LOCK(nt);
2488 node_reclaim(nt, ni);
2489 IEEE80211_NODE_UNLOCK(nt);
2491 ieee80211_free_node(ni);
2495 struct ieee80211vap *vap;
2497 uint32_t rssi_total;
2501 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2503 struct rssiinfo *info = arg;
2504 struct ieee80211vap *vap = ni->ni_vap;
2507 if (info->vap != vap)
2509 /* only associated stations */
2510 if (ni->ni_associd == 0)
2512 rssi = vap->iv_ic->ic_node_getrssi(ni);
2514 info->rssi_samples++;
2515 info->rssi_total += rssi;
2520 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2522 struct rssiinfo *info = arg;
2523 struct ieee80211vap *vap = ni->ni_vap;
2526 if (info->vap != vap)
2528 /* only neighbors */
2529 /* XXX check bssid */
2530 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2532 rssi = vap->iv_ic->ic_node_getrssi(ni);
2534 info->rssi_samples++;
2535 info->rssi_total += rssi;
2540 ieee80211_getrssi(struct ieee80211vap *vap)
2542 #define NZ(x) ((x) == 0 ? 1 : (x))
2543 struct ieee80211com *ic = vap->iv_ic;
2544 struct rssiinfo info;
2546 info.rssi_total = 0;
2547 info.rssi_samples = 0;
2549 switch (vap->iv_opmode) {
2550 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
2551 case IEEE80211_M_AHDEMO: /* average of all neighbors */
2552 ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2554 case IEEE80211_M_HOSTAP: /* average of all associated stations */
2555 ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2557 case IEEE80211_M_MONITOR: /* XXX */
2558 case IEEE80211_M_STA: /* use stats from associated ap */
2560 if (vap->iv_bss != NULL)
2561 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2562 info.rssi_samples = 1;
2565 return info.rssi_total / NZ(info.rssi_samples);
2570 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2573 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
2575 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2576 /* for non-station mode return avg'd rssi accounting */
2577 if (vap->iv_opmode != IEEE80211_M_STA)
2578 *rssi = ieee80211_getrssi(vap);