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.
26 * $FreeBSD: head/sys/net80211/ieee80211_node.c 206358 2010-04-07 15:29:13Z rpaulo $
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>
43 #include <net/route.h>
45 #include <netproto/802_11/ieee80211_var.h>
46 #include <netproto/802_11/ieee80211_input.h>
47 #ifdef IEEE80211_SUPPORT_SUPERG
48 #include <netproto/802_11/ieee80211_superg.h>
50 #ifdef IEEE80211_SUPPORT_TDMA
51 #include <netproto/802_11/ieee80211_tdma.h>
53 #include <netproto/802_11/ieee80211_wds.h>
54 #include <netproto/802_11/ieee80211_mesh.h>
55 #include <netproto/802_11/ieee80211_ratectl.h>
60 * IEEE80211_NODE_HASHSIZE must be a power of 2.
62 CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
65 * Association id's are managed with a bit vector.
67 #define IEEE80211_AID_SET(_vap, b) \
68 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
69 (1 << (IEEE80211_AID(b) % 32)))
70 #define IEEE80211_AID_CLR(_vap, b) \
71 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
72 ~(1 << (IEEE80211_AID(b) % 32)))
73 #define IEEE80211_AID_ISSET(_vap, b) \
74 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
76 #ifdef IEEE80211_DEBUG_REFCNT
77 #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
79 #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
82 static int ieee80211_sta_join1(struct ieee80211_node *);
84 static struct ieee80211_node *node_alloc(struct ieee80211vap *,
85 const uint8_t [IEEE80211_ADDR_LEN]);
86 static void node_cleanup(struct ieee80211_node *);
87 static void node_free(struct ieee80211_node *);
88 static void node_age(struct ieee80211_node *);
89 static int8_t node_getrssi(const struct ieee80211_node *);
90 static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
91 static void node_getmimoinfo(const struct ieee80211_node *,
92 struct ieee80211_mimo_info *);
93 static void ieee80211_node_timeout_callout(void *arg);
95 static void _ieee80211_free_node(struct ieee80211_node *);
97 static void ieee80211_node_table_init(struct ieee80211com *ic,
98 struct ieee80211_node_table *nt, const char *name,
99 int inact, int keymaxix);
100 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
101 struct ieee80211vap *);
102 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
103 static void ieee80211_erp_timeout(struct ieee80211com *);
105 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
106 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
109 ieee80211_node_attach(struct ieee80211com *ic)
111 /* XXX really want maxlen enforced per-sta */
112 ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
114 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
115 IEEE80211_INACT_INIT, ic->ic_max_keyix);
116 callout_init_mp(&ic->ic_inact);
117 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
118 ieee80211_node_timeout_callout, ic);
120 ic->ic_node_alloc = node_alloc;
121 ic->ic_node_free = node_free;
122 ic->ic_node_cleanup = node_cleanup;
123 ic->ic_node_age = node_age;
124 ic->ic_node_drain = node_age; /* NB: same as age */
125 ic->ic_node_getrssi = node_getrssi;
126 ic->ic_node_getsignal = node_getsignal;
127 ic->ic_node_getmimoinfo = node_getmimoinfo;
130 * Set flags to be propagated to all vap's;
131 * these define default behaviour/configuration.
133 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
137 ieee80211_node_detach(struct ieee80211com *ic)
140 callout_stop(&ic->ic_inact);
141 ieee80211_node_table_cleanup(&ic->ic_sta);
142 ieee80211_ageq_cleanup(&ic->ic_stageq);
146 ieee80211_node_vattach(struct ieee80211vap *vap)
148 /* NB: driver can override */
149 vap->iv_max_aid = IEEE80211_AID_DEF;
151 /* default station inactivity timer setings */
152 vap->iv_inact_init = IEEE80211_INACT_INIT;
153 vap->iv_inact_auth = IEEE80211_INACT_AUTH;
154 vap->iv_inact_run = IEEE80211_INACT_RUN;
155 vap->iv_inact_probe = IEEE80211_INACT_PROBE;
157 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
158 "%s: init %u auth %u run %u probe %u\n", __func__,
159 vap->iv_inact_init, vap->iv_inact_auth,
160 vap->iv_inact_run, vap->iv_inact_probe);
164 ieee80211_node_latevattach(struct ieee80211vap *vap)
166 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
167 /* XXX should we allow max aid to be zero? */
168 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
169 vap->iv_max_aid = IEEE80211_AID_MIN;
170 if_printf(vap->iv_ifp,
171 "WARNING: max aid too small, changed to %d\n",
174 vap->iv_aid_bitmap = (uint32_t *) kmalloc(
175 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
176 M_80211_NODE, M_INTWAIT | M_ZERO);
177 if (vap->iv_aid_bitmap == NULL) {
178 /* XXX no way to recover */
179 kprintf("%s: no memory for AID bitmap, max aid %d!\n",
180 __func__, vap->iv_max_aid);
185 ieee80211_reset_bss(vap);
187 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
191 ieee80211_node_vdetach(struct ieee80211vap *vap)
193 struct ieee80211com *ic = vap->iv_ic;
195 ieee80211_node_table_reset(&ic->ic_sta, vap);
196 if (vap->iv_bss != NULL) {
197 ieee80211_free_node(vap->iv_bss);
200 if (vap->iv_aid_bitmap != NULL) {
201 kfree(vap->iv_aid_bitmap, M_80211_NODE);
202 vap->iv_aid_bitmap = NULL;
207 * Port authorize/unauthorize interfaces for use by an authenticator.
211 ieee80211_node_authorize(struct ieee80211_node *ni)
213 struct ieee80211vap *vap = ni->ni_vap;
215 ni->ni_flags |= IEEE80211_NODE_AUTH;
216 ni->ni_inact_reload = vap->iv_inact_run;
217 ni->ni_inact = ni->ni_inact_reload;
219 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
220 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
224 ieee80211_node_unauthorize(struct ieee80211_node *ni)
226 struct ieee80211vap *vap = ni->ni_vap;
228 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
229 ni->ni_inact_reload = vap->iv_inact_auth;
230 if (ni->ni_inact > ni->ni_inact_reload)
231 ni->ni_inact = ni->ni_inact_reload;
233 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
234 "%s: inact_reload %u inact %u", __func__,
235 ni->ni_inact_reload, ni->ni_inact);
239 * Fix tx parameters for a node according to ``association state''.
242 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
244 struct ieee80211vap *vap = ni->ni_vap;
245 enum ieee80211_phymode mode;
247 if (ni->ni_flags & IEEE80211_NODE_HT) {
248 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
249 mode = IEEE80211_MODE_11NA;
251 mode = IEEE80211_MODE_11NG;
252 } else { /* legacy rate handling */
253 if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
254 mode = IEEE80211_MODE_STURBO_A;
255 else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
256 mode = IEEE80211_MODE_HALF;
257 else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
258 mode = IEEE80211_MODE_QUARTER;
259 /* NB: 108A should be handled as 11a */
260 else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
261 mode = IEEE80211_MODE_11A;
262 else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
263 (ni->ni_flags & IEEE80211_NODE_ERP))
264 mode = IEEE80211_MODE_11G;
266 mode = IEEE80211_MODE_11B;
268 ni->ni_txparms = &vap->iv_txparms[mode];
272 * Set/change the channel. The rate set is also updated as
273 * to insure a consistent view by drivers.
274 * XXX should be private but hostap needs it to deal with CSA
277 ieee80211_node_set_chan(struct ieee80211_node *ni,
278 struct ieee80211_channel *chan)
280 struct ieee80211com *ic = ni->ni_ic;
281 struct ieee80211vap *vap = ni->ni_vap;
282 enum ieee80211_phymode mode;
284 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
287 mode = ieee80211_chan2mode(chan);
288 if (IEEE80211_IS_CHAN_HT(chan)) {
290 * XXX Gotta be careful here; the rate set returned by
291 * ieee80211_get_suprates is actually any HT rate
292 * set so blindly copying it will be bad. We must
293 * install the legacy rate est in ni_rates and the
294 * HT rate set in ni_htrates.
296 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
298 * Setup bss tx parameters based on operating mode. We
299 * use legacy rates when operating in a mixed HT+non-HT bss
300 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
302 if (mode == IEEE80211_MODE_11NA &&
303 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
304 mode = IEEE80211_MODE_11A;
305 else if (mode == IEEE80211_MODE_11NG &&
306 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
307 mode = IEEE80211_MODE_11G;
308 if (mode == IEEE80211_MODE_11G &&
309 (vap->iv_flags & IEEE80211_F_PUREG) == 0)
310 mode = IEEE80211_MODE_11B;
312 ni->ni_txparms = &vap->iv_txparms[mode];
313 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
317 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
319 /* propagate useful state */
320 nbss->ni_authmode = obss->ni_authmode;
321 nbss->ni_txpower = obss->ni_txpower;
322 nbss->ni_vlan = obss->ni_vlan;
323 /* XXX statistics? */
324 /* XXX legacy WDS bssid? */
328 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
330 struct ieee80211com *ic = vap->iv_ic;
331 struct ieee80211_node *ni;
333 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
334 "%s: creating %s on channel %u\n", __func__,
335 ieee80211_opmode_name[vap->iv_opmode],
336 ieee80211_chan2ieee(ic, chan));
338 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
343 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
344 ni->ni_esslen = vap->iv_des_ssid[0].len;
345 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
346 if (vap->iv_bss != NULL)
347 copy_bss(ni, vap->iv_bss);
348 ni->ni_intval = ic->ic_bintval;
349 if (vap->iv_flags & IEEE80211_F_PRIVACY)
350 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
351 if (ic->ic_phytype == IEEE80211_T_FH) {
352 ni->ni_fhdwell = 200; /* XXX */
355 if (vap->iv_opmode == IEEE80211_M_IBSS) {
356 vap->iv_flags |= IEEE80211_F_SIBSS;
357 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
358 if (vap->iv_flags & IEEE80211_F_DESBSSID)
359 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
361 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
362 /* clear group bit, add local bit */
363 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
365 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
366 if (vap->iv_flags & IEEE80211_F_DESBSSID)
367 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
369 #ifdef IEEE80211_SUPPORT_TDMA
370 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
372 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
373 #ifdef IEEE80211_SUPPORT_MESH
374 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
375 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
376 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
380 * Fix the channel and related attributes.
382 /* clear DFS CAC state on previous channel */
383 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
384 ic->ic_bsschan->ic_freq != chan->ic_freq &&
385 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
386 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
387 ic->ic_bsschan = chan;
388 ieee80211_node_set_chan(ni, chan);
389 ic->ic_curmode = ieee80211_chan2mode(chan);
391 * Do mode-specific setup.
393 if (IEEE80211_IS_CHAN_FULL(chan)) {
394 if (IEEE80211_IS_CHAN_ANYG(chan)) {
396 * Use a mixed 11b/11g basic rate set.
398 ieee80211_setbasicrates(&ni->ni_rates,
400 if (vap->iv_flags & IEEE80211_F_PUREG) {
402 * Also mark OFDM rates basic so 11b
403 * stations do not join (WiFi compliance).
405 ieee80211_addbasicrates(&ni->ni_rates,
408 } else if (IEEE80211_IS_CHAN_B(chan)) {
410 * Force pure 11b rate set.
412 ieee80211_setbasicrates(&ni->ni_rates,
417 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
421 * Reset bss state on transition to the INIT state.
422 * Clear any stations from the table (they have been
423 * deauth'd) and reset the bss node (clears key, rate
427 ieee80211_reset_bss(struct ieee80211vap *vap)
429 struct ieee80211com *ic = vap->iv_ic;
430 struct ieee80211_node *ni, *obss;
432 ieee80211_node_table_reset(&ic->ic_sta, vap);
433 /* XXX multi-bss: wrong */
434 ieee80211_reset_erp(ic);
436 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
437 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
439 vap->iv_bss = ieee80211_ref_node(ni);
442 ni->ni_intval = ic->ic_bintval;
443 ieee80211_free_node(obss);
445 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
449 match_ssid(const struct ieee80211_node *ni,
450 int nssid, const struct ieee80211_scan_ssid ssids[])
454 for (i = 0; i < nssid; i++) {
455 if (ni->ni_esslen == ssids[i].len &&
456 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
463 * Test a node for suitability/compatibility.
466 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
468 struct ieee80211com *ic = ni->ni_ic;
471 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
473 if (vap->iv_opmode == IEEE80211_M_IBSS) {
474 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
477 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
480 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
481 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
484 /* XXX does this mean privacy is supported or required? */
485 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
488 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
489 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
490 if (rate & IEEE80211_RATE_BASIC)
492 if (vap->iv_des_nssid != 0 &&
493 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
495 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
496 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
501 #ifdef IEEE80211_DEBUG
503 * Display node suitability/compatibility.
506 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
508 struct ieee80211com *ic = ni->ni_ic;
513 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
515 if (vap->iv_opmode == IEEE80211_M_IBSS) {
516 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
519 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
522 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
523 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
526 /* XXX does this mean privacy is supported or required? */
527 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
530 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
531 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
532 if (rate & IEEE80211_RATE_BASIC)
534 if (vap->iv_des_nssid != 0 &&
535 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
537 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
538 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
541 kprintf(" %c %6D", fail ? '-' : '+', ni->ni_macaddr, ":");
542 kprintf(" %6D%c", ni->ni_bssid, ":", fail & 0x20 ? '!' : ' ');
544 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
545 kprintf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
546 fail & 0x08 ? '!' : ' ');
548 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
549 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
551 fail & 0x02 ? '!' : ' ');
553 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
554 fail & 0x04 ? '!' : ' ');
555 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
556 kprintf("%s\n", fail & 0x10 ? "!" : "");
558 #endif /* IEEE80211_DEBUG */
561 * Handle 802.11 ad hoc network merge. The
562 * convention, set by the Wireless Ethernet Compatibility Alliance
563 * (WECA), is that an 802.11 station will change its BSSID to match
564 * the "oldest" 802.11 ad hoc network, on the same channel, that
565 * has the station's desired SSID. The "oldest" 802.11 network
566 * sends beacons with the greatest TSF timestamp.
568 * The caller is assumed to validate TSF's before attempting a merge.
570 * Return !0 if the BSSID changed, 0 otherwise.
573 ieee80211_ibss_merge(struct ieee80211_node *ni)
575 struct ieee80211vap *vap = ni->ni_vap;
576 #ifdef IEEE80211_DEBUG
577 struct ieee80211com *ic = ni->ni_ic;
580 if (ni == vap->iv_bss ||
581 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
582 /* unchanged, nothing to do */
585 if (!check_bss(vap, ni)) {
586 /* capabilities mismatch */
587 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
588 "%s: merge failed, capabilities mismatch\n", __func__);
589 #ifdef IEEE80211_DEBUG
590 if (ieee80211_msg_assoc(vap))
591 check_bss_debug(vap, ni);
593 vap->iv_stats.is_ibss_capmismatch++;
596 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
597 "%s: new bssid %6D: %s preamble, %s slot time%s\n", __func__,
599 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
600 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
601 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
603 return ieee80211_sta_join1(ieee80211_ref_node(ni));
607 * Calculate HT channel promotion flags for all vaps.
608 * This assumes ni_chan have been setup for each vap.
611 gethtadjustflags(struct ieee80211com *ic)
613 struct ieee80211vap *vap;
618 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
619 if (vap->iv_state < IEEE80211_S_RUN)
621 switch (vap->iv_opmode) {
622 case IEEE80211_M_WDS:
623 case IEEE80211_M_STA:
624 case IEEE80211_M_AHDEMO:
625 case IEEE80211_M_HOSTAP:
626 case IEEE80211_M_IBSS:
627 case IEEE80211_M_MBSS:
628 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
638 * Check if the current channel needs to change based on whether
639 * any vap's are using HT20/HT40. This is used to sync the state
640 * of ic_curchan after a channel width change on a running vap.
643 ieee80211_sync_curchan(struct ieee80211com *ic)
645 struct ieee80211_channel *c;
647 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
648 if (c != ic->ic_curchan) {
650 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
651 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
652 ic->ic_set_channel(ic);
653 ieee80211_radiotap_chan_change(ic);
658 * Setup the current channel. The request channel may be
659 * promoted if other vap's are operating with HT20/HT40.
662 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
664 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
665 int flags = gethtadjustflags(ic);
667 * Check for channel promotion required to support the
668 * set of running vap's. This assumes we are called
669 * after ni_chan is setup for each vap.
671 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
672 if (flags > ieee80211_htchanflags(c))
673 c = ieee80211_ht_adjust_channel(ic, c, flags);
675 ic->ic_bsschan = ic->ic_curchan = c;
676 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
677 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
681 * Change the current channel. The channel change is guaranteed to have
682 * happened before the next state change.
685 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
687 ieee80211_setupcurchan(ic, c);
688 ieee80211_runtask(ic, &ic->ic_chan_task);
692 * Join the specified IBSS/BSS network. The node is assumed to
693 * be passed in with a held reference.
696 ieee80211_sta_join1(struct ieee80211_node *selbs)
698 struct ieee80211vap *vap = selbs->ni_vap;
699 struct ieee80211com *ic = selbs->ni_ic;
700 struct ieee80211_node *obss;
704 * Committed to selbs, setup state.
708 * Check if old+new node have the same address in which
709 * case we can reassociate when operating in sta mode.
711 canreassoc = (obss != NULL &&
712 vap->iv_state == IEEE80211_S_RUN &&
713 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
714 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */
716 copy_bss(selbs, obss);
717 ieee80211_node_decref(obss); /* iv_bss reference */
718 ieee80211_free_node(obss); /* station table reference */
719 obss = NULL; /* NB: guard against later use */
723 * Delete unusable rates; we've already checked
724 * that the negotiated rate set is acceptable.
726 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
727 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
729 ieee80211_setcurchan(ic, selbs->ni_chan);
731 * Set the erp state (mostly the slot time) to deal with
732 * the auto-select case; this should be redundant if the
735 ieee80211_reset_erp(ic);
736 ieee80211_wme_initparams(vap);
738 if (vap->iv_opmode == IEEE80211_M_STA) {
741 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
744 * Act as if we received a DEAUTH frame in case we
745 * are invoked from the RUN state. This will cause
746 * us to try to re-authenticate if we are operating
749 ieee80211_new_state(vap, IEEE80211_S_AUTH,
750 IEEE80211_FC0_SUBTYPE_DEAUTH);
753 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
758 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
759 const struct ieee80211_scan_entry *se)
761 struct ieee80211com *ic = vap->iv_ic;
762 struct ieee80211_node *ni;
764 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
770 * Expand scan state into node's format.
771 * XXX may not need all this stuff
773 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
774 ni->ni_esslen = se->se_ssid[1];
775 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
776 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
777 ni->ni_intval = se->se_intval;
778 ni->ni_capinfo = se->se_capinfo;
780 ni->ni_timoff = se->se_timoff;
781 ni->ni_fhdwell = se->se_fhdwell;
782 ni->ni_fhindex = se->se_fhindex;
783 ni->ni_erp = se->se_erp;
784 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
785 ni->ni_noise = se->se_noise;
786 if (vap->iv_opmode == IEEE80211_M_STA) {
787 /* NB: only infrastructure mode requires an associd */
788 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
791 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
792 ieee80211_ies_expand(&ni->ni_ies);
793 #ifdef IEEE80211_SUPPORT_SUPERG
794 if (ni->ni_ies.ath_ie != NULL)
795 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
797 if (ni->ni_ies.htcap_ie != NULL)
798 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
799 if (ni->ni_ies.htinfo_ie != NULL)
800 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
801 #ifdef IEEE80211_SUPPORT_MESH
802 if (ni->ni_ies.meshid_ie != NULL)
803 ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
805 #ifdef IEEE80211_SUPPORT_TDMA
806 if (ni->ni_ies.tdma_ie != NULL)
807 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
811 vap->iv_dtim_period = se->se_dtimperiod;
812 vap->iv_dtim_count = 0;
814 /* NB: must be after ni_chan is setup */
815 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
817 if (ieee80211_iserp_rateset(&ni->ni_rates))
818 ni->ni_flags |= IEEE80211_NODE_ERP;
819 ieee80211_node_setuptxparms(ni);
821 return ieee80211_sta_join1(ieee80211_ref_node(ni));
825 * Leave the specified IBSS/BSS network. The node is assumed to
826 * be passed in with a held reference.
829 ieee80211_sta_leave(struct ieee80211_node *ni)
831 struct ieee80211com *ic = ni->ni_ic;
833 ic->ic_node_cleanup(ni);
834 ieee80211_notify_node_leave(ni);
838 * Send a deauthenticate frame and drop the station.
841 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
843 /* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
844 ieee80211_ref_node(ni);
845 if (ni->ni_associd != 0)
846 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
847 ieee80211_node_leave(ni);
848 ieee80211_free_node(ni);
851 static struct ieee80211_node *
852 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
854 struct ieee80211_node *ni;
856 ni = (struct ieee80211_node *) kmalloc(sizeof(struct ieee80211_node),
857 M_80211_NODE, M_INTWAIT | M_ZERO);
862 * Initialize an ie blob with the specified data. If previous
863 * data exists re-use the data block. As a side effect we clear
864 * all references to specific ie's; the caller is required to
868 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
870 /* NB: assumes data+len are the last fields */
871 memset(ies, 0, offsetof(struct ieee80211_ies, data));
872 if (ies->data != NULL && ies->len != len) {
873 /* data size changed */
874 kfree(ies->data, M_80211_NODE_IE);
877 if (ies->data == NULL) {
878 ies->data = (uint8_t *) kmalloc(len, M_80211_NODE_IE, M_INTWAIT);
879 if (ies->data == NULL) {
881 /* NB: pointers have already been zero'd above */
885 memcpy(ies->data, data, len);
891 * Reclaim storage for an ie blob.
894 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
896 if (ies->data != NULL)
897 kfree(ies->data, M_80211_NODE_IE);
901 * Expand an ie blob data contents and to fillin individual
902 * ie pointers. The data blob is assumed to be well-formed;
903 * we don't do any validity checking of ie lengths.
906 ieee80211_ies_expand(struct ieee80211_ies *ies)
915 case IEEE80211_ELEMID_VENDOR:
918 else if (iswmeoui(ie))
920 #ifdef IEEE80211_SUPPORT_SUPERG
921 else if (isatherosoui(ie))
924 #ifdef IEEE80211_SUPPORT_TDMA
925 else if (istdmaoui(ie))
929 case IEEE80211_ELEMID_RSN:
932 case IEEE80211_ELEMID_HTCAP:
935 #ifdef IEEE80211_SUPPORT_MESH
936 case IEEE80211_ELEMID_MESHID:
947 * Reclaim any resources in a node and reset any critical
948 * state. Typically nodes are free'd immediately after,
949 * but in some cases the storage may be reused so we need
950 * to insure consistent state (should probably fix that).
953 node_cleanup(struct ieee80211_node *ni)
955 #define N(a) (sizeof(a)/sizeof(a[0]))
956 struct ieee80211vap *vap = ni->ni_vap;
957 struct ieee80211com *ic = ni->ni_ic;
960 /* NB: preserve ni_table */
961 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
962 if (vap->iv_opmode != IEEE80211_M_STA)
964 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
965 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
966 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
969 * Cleanup any HT-related state.
971 if (ni->ni_flags & IEEE80211_NODE_HT)
972 ieee80211_ht_node_cleanup(ni);
973 #ifdef IEEE80211_SUPPORT_SUPERG
974 else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
975 ieee80211_ff_node_cleanup(ni);
977 #ifdef IEEE80211_SUPPORT_MESH
979 * Cleanup any mesh-related state.
981 if (vap->iv_opmode == IEEE80211_M_MBSS)
982 ieee80211_mesh_node_cleanup(ni);
985 * Clear any staging queue entries.
987 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
990 * Clear AREF flag that marks the authorization refcnt bump
991 * has happened. This is probably not needed as the node
992 * should always be removed from the table so not found but
993 * do it just in case.
994 * Likewise clear the ASSOCID flag as these flags are intended
995 * to be managed in tandem.
997 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1000 * Drain power save queue and, if needed, clear TIM.
1002 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1003 vap->iv_set_tim(ni, 0);
1006 if (ni->ni_challenge != NULL) {
1007 kfree(ni->ni_challenge, M_80211_NODE);
1008 ni->ni_challenge = NULL;
1011 * Preserve SSID, WPA, and WME ie's so the bss node is
1012 * reusable during a re-auth/re-assoc state transition.
1013 * If we remove these data they will not be recreated
1014 * because they come from a probe-response or beacon frame
1015 * which cannot be expected prior to the association-response.
1016 * This should not be an issue when operating in other modes
1017 * as stations leaving always go through a full state transition
1018 * which will rebuild this state.
1020 * XXX does this leave us open to inheriting old state?
1022 for (i = 0; i < N(ni->ni_rxfrag); i++)
1023 if (ni->ni_rxfrag[i] != NULL) {
1024 m_freem(ni->ni_rxfrag[i]);
1025 ni->ni_rxfrag[i] = NULL;
1028 * Must be careful here to remove any key map entry w/o a LOR.
1030 ieee80211_node_delucastkey(ni);
1035 node_free(struct ieee80211_node *ni)
1037 struct ieee80211com *ic = ni->ni_ic;
1039 ieee80211_ratectl_node_deinit(ni);
1040 ic->ic_node_cleanup(ni);
1041 ieee80211_ies_cleanup(&ni->ni_ies);
1042 ieee80211_psq_cleanup(&ni->ni_psq);
1043 kfree(ni, M_80211_NODE);
1047 node_age(struct ieee80211_node *ni)
1049 struct ieee80211vap *vap = ni->ni_vap;
1052 * Age frames on the power save queue.
1054 if (ieee80211_node_psq_age(ni) != 0 &&
1055 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1056 vap->iv_set_tim(ni, 0);
1058 * Age out HT resources (e.g. frames on the
1059 * A-MPDU reorder queues).
1061 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1062 ieee80211_ht_node_age(ni);
1066 node_getrssi(const struct ieee80211_node *ni)
1068 uint32_t avgrssi = ni->ni_avgrssi;
1071 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1073 rssi = IEEE80211_RSSI_GET(avgrssi);
1074 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1078 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1080 *rssi = node_getrssi(ni);
1081 *noise = ni->ni_noise;
1085 node_getmimoinfo(const struct ieee80211_node *ni,
1086 struct ieee80211_mimo_info *info)
1088 /* XXX zero data? */
1091 struct ieee80211_node *
1092 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1093 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1095 struct ieee80211com *ic = nt->nt_ic;
1096 struct ieee80211_node *ni;
1099 ni = ic->ic_node_alloc(vap, macaddr);
1101 vap->iv_stats.is_rx_nodealloc++;
1105 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1106 "%s %p<%6D> in %s table\n", __func__, ni,
1107 macaddr, ":", nt->nt_name);
1109 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1110 hash = IEEE80211_NODE_HASH(ic, macaddr);
1111 ieee80211_node_initref(ni); /* mark referenced */
1112 ni->ni_chan = IEEE80211_CHAN_ANYC;
1113 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1114 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1115 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1116 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1117 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1118 ni->ni_inact_reload = nt->nt_inact_init;
1119 ni->ni_inact = ni->ni_inact_reload;
1120 ni->ni_ath_defkeyix = 0x7fff;
1121 ieee80211_psq_init(&ni->ni_psq, "unknown");
1122 #ifdef IEEE80211_SUPPORT_MESH
1123 if (vap->iv_opmode == IEEE80211_M_MBSS)
1124 ieee80211_mesh_node_init(vap, ni);
1126 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1127 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1132 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1133 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1139 * Craft a temporary node suitable for sending a management frame
1140 * to the specified station. We craft only as much state as we
1141 * need to do the work since the node will be immediately reclaimed
1142 * once the send completes.
1144 struct ieee80211_node *
1145 ieee80211_tmp_node(struct ieee80211vap *vap,
1146 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1148 struct ieee80211com *ic = vap->iv_ic;
1149 struct ieee80211_node *ni;
1151 ni = ic->ic_node_alloc(vap, macaddr);
1153 struct ieee80211_node *bss = vap->iv_bss;
1155 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1156 "%s %p<%6D>\n", __func__, ni, macaddr, ":");
1158 ni->ni_table = NULL; /* NB: pedantic */
1159 ni->ni_ic = ic; /* NB: needed to set channel */
1162 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1163 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1164 ieee80211_node_initref(ni); /* mark referenced */
1165 /* NB: required by ieee80211_fix_rate */
1166 ieee80211_node_set_chan(ni, bss->ni_chan);
1167 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1168 IEEE80211_KEYIX_NONE);
1169 ni->ni_txpower = bss->ni_txpower;
1170 /* XXX optimize away */
1171 ieee80211_psq_init(&ni->ni_psq, "unknown");
1174 vap->iv_stats.is_rx_nodealloc++;
1179 struct ieee80211_node *
1180 ieee80211_dup_bss(struct ieee80211vap *vap,
1181 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1183 struct ieee80211com *ic = vap->iv_ic;
1184 struct ieee80211_node *ni;
1186 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1188 struct ieee80211_node *bss = vap->iv_bss;
1190 * Inherit from iv_bss.
1193 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1194 ieee80211_node_set_chan(ni, bss->ni_chan);
1200 * Create a bss node for a legacy WDS vap. The far end does
1201 * not associate so we just create create a new node and
1202 * simulate an association. The caller is responsible for
1203 * installing the node as the bss node and handling any further
1204 * setup work like authorizing the port.
1206 struct ieee80211_node *
1207 ieee80211_node_create_wds(struct ieee80211vap *vap,
1208 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1210 struct ieee80211com *ic = vap->iv_ic;
1211 struct ieee80211_node *ni;
1213 /* XXX check if node already in sta table? */
1214 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1216 ni->ni_wdsvap = vap;
1217 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1219 * Inherit any manually configured settings.
1221 copy_bss(ni, vap->iv_bss);
1222 ieee80211_node_set_chan(ni, chan);
1223 /* NB: propagate ssid so available to WPA supplicant */
1224 ni->ni_esslen = vap->iv_des_ssid[0].len;
1225 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1226 /* NB: no associd for peer */
1228 * There are no management frames to use to
1229 * discover neighbor capabilities, so blindly
1230 * propagate the local configuration.
1232 if (vap->iv_flags & IEEE80211_F_WME)
1233 ni->ni_flags |= IEEE80211_NODE_QOS;
1234 #ifdef IEEE80211_SUPPORT_SUPERG
1235 if (vap->iv_flags & IEEE80211_F_FF)
1236 ni->ni_flags |= IEEE80211_NODE_FF;
1238 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1239 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1241 * Device is HT-capable and HT is enabled for
1242 * the vap; setup HT operation. On return
1243 * ni_chan will be adjusted to an HT channel.
1245 ieee80211_ht_wds_init(ni);
1247 struct ieee80211_channel *c = ni->ni_chan;
1249 * Force a legacy channel to be used.
1251 c = ieee80211_find_channel(ic,
1252 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1253 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1254 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1261 struct ieee80211_node *
1262 #ifdef IEEE80211_DEBUG_REFCNT
1263 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1264 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1266 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1267 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1270 struct ieee80211_node *ni;
1273 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1274 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1275 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1276 ieee80211_ref_node(ni); /* mark referenced */
1277 #ifdef IEEE80211_DEBUG_REFCNT
1278 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1279 "%s (%s:%u) %p<%6D> refcnt %d\n", __func__,
1281 ni, ni->ni_macaddr, ":",
1282 ieee80211_node_refcnt(ni));
1290 struct ieee80211_node *
1291 #ifdef IEEE80211_DEBUG_REFCNT
1292 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1293 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1295 ieee80211_find_node(struct ieee80211_node_table *nt,
1296 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1299 struct ieee80211_node *ni;
1301 ni = ieee80211_find_node_locked(nt, macaddr);
1305 struct ieee80211_node *
1306 #ifdef IEEE80211_DEBUG_REFCNT
1307 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1308 const struct ieee80211vap *vap,
1309 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1311 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1312 const struct ieee80211vap *vap,
1313 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1316 struct ieee80211_node *ni;
1319 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1320 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1321 if (ni->ni_vap == vap &&
1322 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1323 ieee80211_ref_node(ni); /* mark referenced */
1324 #ifdef IEEE80211_DEBUG_REFCNT
1325 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1326 "%s (%s:%u) %p<%6D> refcnt %d\n", __func__,
1328 ni, ni->ni_macaddr, ":",
1329 ieee80211_node_refcnt(ni));
1337 struct ieee80211_node *
1338 #ifdef IEEE80211_DEBUG_REFCNT
1339 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1340 const struct ieee80211vap *vap,
1341 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1343 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1344 const struct ieee80211vap *vap,
1345 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1348 struct ieee80211_node *ni;
1350 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1355 * Fake up a node; this handles node discovery in adhoc mode.
1356 * Note that for the driver's benefit we we treat this like
1357 * an association so the driver has an opportunity to setup
1358 * it's private state.
1360 struct ieee80211_node *
1361 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1362 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1364 struct ieee80211_node *ni;
1366 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1367 "%s: mac<%6D>\n", __func__, macaddr, ":");
1368 ni = ieee80211_dup_bss(vap, macaddr);
1370 struct ieee80211com *ic = vap->iv_ic;
1372 /* XXX no rate negotiation; just dup */
1373 ni->ni_rates = vap->iv_bss->ni_rates;
1374 if (ieee80211_iserp_rateset(&ni->ni_rates))
1375 ni->ni_flags |= IEEE80211_NODE_ERP;
1376 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1378 * In adhoc demo mode there are no management
1379 * frames to use to discover neighbor capabilities,
1380 * so blindly propagate the local configuration
1381 * so we can do interesting things (e.g. use
1382 * WME to disable ACK's).
1384 if (vap->iv_flags & IEEE80211_F_WME)
1385 ni->ni_flags |= IEEE80211_NODE_QOS;
1386 #ifdef IEEE80211_SUPPORT_SUPERG
1387 if (vap->iv_flags & IEEE80211_F_FF)
1388 ni->ni_flags |= IEEE80211_NODE_FF;
1391 ieee80211_node_setuptxparms(ni);
1392 if (ic->ic_newassoc != NULL)
1393 ic->ic_newassoc(ni, 1);
1394 /* XXX not right for 802.1x/WPA */
1395 ieee80211_node_authorize(ni);
1401 ieee80211_init_neighbor(struct ieee80211_node *ni,
1402 const struct ieee80211_frame *wh,
1403 const struct ieee80211_scanparams *sp)
1405 ni->ni_esslen = sp->ssid[1];
1406 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1407 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1408 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1409 ni->ni_intval = sp->bintval;
1410 ni->ni_capinfo = sp->capinfo;
1411 ni->ni_chan = ni->ni_ic->ic_curchan;
1412 ni->ni_fhdwell = sp->fhdwell;
1413 ni->ni_fhindex = sp->fhindex;
1414 ni->ni_erp = sp->erp;
1415 ni->ni_timoff = sp->timoff;
1416 #ifdef IEEE80211_SUPPORT_MESH
1417 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1418 ieee80211_mesh_init_neighbor(ni, wh, sp);
1420 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1421 ieee80211_ies_expand(&ni->ni_ies);
1422 if (ni->ni_ies.wme_ie != NULL)
1423 ni->ni_flags |= IEEE80211_NODE_QOS;
1425 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1426 #ifdef IEEE80211_SUPPORT_SUPERG
1427 if (ni->ni_ies.ath_ie != NULL)
1428 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1432 /* NB: must be after ni_chan is setup */
1433 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1434 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1435 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1439 * Do node discovery in adhoc mode on receipt of a beacon
1440 * or probe response frame. Note that for the driver's
1441 * benefit we we treat this like an association so the
1442 * driver has an opportunity to setup it's private state.
1444 struct ieee80211_node *
1445 ieee80211_add_neighbor(struct ieee80211vap *vap,
1446 const struct ieee80211_frame *wh,
1447 const struct ieee80211_scanparams *sp)
1449 struct ieee80211_node *ni;
1451 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1452 "%s: mac<%6D>\n", __func__, wh->i_addr2, ":");
1453 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1455 struct ieee80211com *ic = vap->iv_ic;
1457 ieee80211_init_neighbor(ni, wh, sp);
1458 if (ieee80211_iserp_rateset(&ni->ni_rates))
1459 ni->ni_flags |= IEEE80211_NODE_ERP;
1460 ieee80211_node_setuptxparms(ni);
1461 if (ic->ic_newassoc != NULL)
1462 ic->ic_newassoc(ni, 1);
1463 /* XXX not right for 802.1x/WPA */
1464 ieee80211_node_authorize(ni);
1469 #define IS_PROBEREQ(wh) \
1470 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1471 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1472 #define IS_BCAST_PROBEREQ(wh) \
1473 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1474 ((const struct ieee80211_frame *)(wh))->i_addr3))
1476 static __inline struct ieee80211_node *
1477 _find_rxnode(struct ieee80211_node_table *nt,
1478 const struct ieee80211_frame_min *wh)
1480 if (IS_BCAST_PROBEREQ(wh))
1481 return NULL; /* spam bcast probe req to all vap's */
1482 return ieee80211_find_node_locked(nt, wh->i_addr2);
1486 * Locate the node for sender, track state, and then pass the
1487 * (referenced) node up to the 802.11 layer for its use. Note
1488 * we can return NULL if the sender is not in the table.
1490 struct ieee80211_node *
1491 #ifdef IEEE80211_DEBUG_REFCNT
1492 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1493 const struct ieee80211_frame_min *wh, const char *func, int line)
1495 ieee80211_find_rxnode(struct ieee80211com *ic,
1496 const struct ieee80211_frame_min *wh)
1499 struct ieee80211_node_table *nt;
1500 struct ieee80211_node *ni;
1503 ni = _find_rxnode(nt, wh);
1509 * Like ieee80211_find_rxnode but use the supplied h/w
1510 * key index as a hint to locate the node in the key
1511 * mapping table. If an entry is present at the key
1512 * index we return it; otherwise do a normal lookup and
1513 * update the mapping table if the station has a unicast
1514 * key assigned to it.
1516 struct ieee80211_node *
1517 #ifdef IEEE80211_DEBUG_REFCNT
1518 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1519 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1520 const char *func, int line)
1522 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1523 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1526 struct ieee80211_node_table *nt;
1527 struct ieee80211_node *ni;
1530 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1531 ni = nt->nt_keyixmap[keyix];
1535 ni = _find_rxnode(nt, wh);
1536 if (ni != NULL && nt->nt_keyixmap != NULL) {
1538 * If the station has a unicast key cache slot
1539 * assigned update the key->node mapping table.
1541 keyix = ni->ni_ucastkey.wk_rxkeyix;
1542 /* XXX can keyixmap[keyix] != NULL? */
1543 if (keyix < nt->nt_keyixmax &&
1544 nt->nt_keyixmap[keyix] == NULL) {
1545 IEEE80211_DPRINTF(ni->ni_vap,
1547 "%s: add key map entry %p<%6D> refcnt %d\n",
1548 __func__, ni, ni->ni_macaddr, ":",
1549 ieee80211_node_refcnt(ni)+1);
1550 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1554 if (IS_BCAST_PROBEREQ(wh))
1555 ni = NULL; /* spam bcast probe req to all vap's */
1557 ieee80211_ref_node(ni);
1561 #undef IS_BCAST_PROBEREQ
1565 * Return a reference to the appropriate node for sending
1566 * a data frame. This handles node discovery in adhoc networks.
1568 struct ieee80211_node *
1569 #ifdef IEEE80211_DEBUG_REFCNT
1570 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1571 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1572 const char *func, int line)
1574 ieee80211_find_txnode(struct ieee80211vap *vap,
1575 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1578 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1579 struct ieee80211_node *ni;
1582 * The destination address should be in the node table
1583 * unless this is a multicast/broadcast frame. We can
1584 * also optimize station mode operation, all frames go
1587 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1588 if (vap->iv_opmode == IEEE80211_M_STA ||
1589 vap->iv_opmode == IEEE80211_M_WDS ||
1590 IEEE80211_IS_MULTICAST(macaddr))
1591 ni = ieee80211_ref_node(vap->iv_bss);
1593 ni = ieee80211_find_node_locked(nt, macaddr);
1596 if (vap->iv_opmode == IEEE80211_M_IBSS ||
1597 vap->iv_opmode == IEEE80211_M_AHDEMO) {
1599 * In adhoc mode cons up a node for the destination.
1600 * Note that we need an additional reference for the
1601 * caller to be consistent with
1602 * ieee80211_find_node_locked.
1604 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1606 (void) ieee80211_ref_node(ni);
1608 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1609 "no node, discard frame (%s)", __func__);
1610 vap->iv_stats.is_tx_nonode++;
1617 _ieee80211_free_node(struct ieee80211_node *ni)
1619 struct ieee80211_node_table *nt = ni->ni_table;
1622 * NB: careful about referencing the vap as it may be
1623 * gone if the last reference was held by a driver.
1624 * We know the com will always be present so it's safe
1625 * to use ni_ic below to reclaim resources.
1628 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1629 "%s %p<%6D> in %s table\n", __func__, ni,
1630 ni->ni_macaddr, ":",
1631 nt != NULL ? nt->nt_name : "<gone>");
1633 if (ni->ni_associd != 0) {
1634 struct ieee80211vap *vap = ni->ni_vap;
1635 if (vap->iv_aid_bitmap != NULL)
1636 IEEE80211_AID_CLR(vap, ni->ni_associd);
1639 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1640 LIST_REMOVE(ni, ni_hash);
1642 ni->ni_ic->ic_node_free(ni);
1646 #ifdef IEEE80211_DEBUG_REFCNT
1647 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1649 ieee80211_free_node(struct ieee80211_node *ni)
1652 struct ieee80211_node_table *nt = ni->ni_table;
1654 #ifdef IEEE80211_DEBUG_REFCNT
1655 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1656 "%s (%s:%u) %p<%6D> refcnt %d\n", __func__, func, line, ni,
1657 ni->ni_macaddr, ":", ieee80211_node_refcnt(ni)-1);
1660 if (ieee80211_node_dectestref(ni)) {
1662 * Last reference, reclaim state.
1664 _ieee80211_free_node(ni);
1665 } else if (ieee80211_node_refcnt(ni) == 1 &&
1666 nt->nt_keyixmap != NULL) {
1667 ieee80211_keyix keyix;
1669 * Check for a last reference in the key mapping table.
1671 keyix = ni->ni_ucastkey.wk_rxkeyix;
1672 if (keyix < nt->nt_keyixmax &&
1673 nt->nt_keyixmap[keyix] == ni) {
1674 IEEE80211_DPRINTF(ni->ni_vap,
1676 "%s: %p<%6D> clear key map entry", __func__,
1677 ni, ni->ni_macaddr, ":");
1678 nt->nt_keyixmap[keyix] = NULL;
1679 ieee80211_node_decref(ni); /* XXX needed? */
1680 _ieee80211_free_node(ni);
1684 if (ieee80211_node_dectestref(ni))
1685 _ieee80211_free_node(ni);
1690 * Reclaim a unicast key and clear any key cache state.
1693 ieee80211_node_delucastkey(struct ieee80211_node *ni)
1695 struct ieee80211com *ic = ni->ni_ic;
1696 struct ieee80211_node_table *nt = &ic->ic_sta;
1697 struct ieee80211_node *nikey;
1698 ieee80211_keyix keyix;
1702 * NB: We must beware of LOR here; deleting the key
1703 * can cause the crypto layer to block traffic updates
1704 * which can generate a LOR against the node table lock;
1705 * grab it here and stash the key index for our use below.
1707 * Must also beware of recursion on the node table lock.
1708 * When called from node_cleanup we may already have
1709 * the node table lock held. Unfortunately there's no
1710 * way to separate out this path so we must do this
1714 status = 1; /* NB: success */
1715 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1716 keyix = ni->ni_ucastkey.wk_rxkeyix;
1717 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1718 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1719 nikey = nt->nt_keyixmap[keyix];
1720 nt->nt_keyixmap[keyix] = NULL;
1724 if (nikey != NULL) {
1725 KASSERT(nikey == ni,
1726 ("key map out of sync, ni %p nikey %p", ni, nikey));
1727 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1728 "%s: delete key map entry %p<%6D> refcnt %d\n",
1729 __func__, ni, ni->ni_macaddr, ":",
1730 ieee80211_node_refcnt(ni)-1);
1731 ieee80211_free_node(ni);
1737 * Reclaim a node. If this is the last reference count then
1738 * do the normal free work. Otherwise remove it from the node
1739 * table and mark it gone by clearing the back-reference.
1742 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1744 ieee80211_keyix keyix;
1746 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1747 "%s: remove %p<%6D> from %s table, refcnt %d\n",
1748 __func__, ni, ni->ni_macaddr, ":",
1749 nt->nt_name, ieee80211_node_refcnt(ni)-1);
1751 * Clear any entry in the unicast key mapping table.
1752 * We need to do it here so rx lookups don't find it
1753 * in the mapping table even if it's not in the hash
1754 * table. We cannot depend on the mapping table entry
1755 * being cleared because the node may not be free'd.
1757 keyix = ni->ni_ucastkey.wk_rxkeyix;
1758 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1759 nt->nt_keyixmap[keyix] == ni) {
1760 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1761 "%s: %p<%6D> clear key map entry %u\n",
1762 __func__, ni, ni->ni_macaddr, ":", keyix);
1763 nt->nt_keyixmap[keyix] = NULL;
1764 ieee80211_node_decref(ni); /* NB: don't need free */
1766 if (!ieee80211_node_dectestref(ni)) {
1768 * Other references are present, just remove the
1769 * node from the table so it cannot be found. When
1770 * the references are dropped storage will be
1773 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1774 LIST_REMOVE(ni, ni_hash);
1775 ni->ni_table = NULL; /* clear reference */
1777 _ieee80211_free_node(ni);
1781 * Node table support.
1785 ieee80211_node_table_init(struct ieee80211com *ic,
1786 struct ieee80211_node_table *nt,
1787 const char *name, int inact, int keyixmax)
1790 TAILQ_INIT(&nt->nt_node);
1793 nt->nt_inact_init = inact;
1794 nt->nt_keyixmax = keyixmax;
1795 if (nt->nt_keyixmax > 0) {
1796 nt->nt_keyixmap = (struct ieee80211_node **) kmalloc(
1797 keyixmax * sizeof(struct ieee80211_node *),
1798 M_80211_NODE, M_INTWAIT | M_ZERO);
1799 if (nt->nt_keyixmap == NULL)
1800 if_printf(ic->ic_ifp,
1801 "Cannot allocate key index map with %u entries\n",
1804 nt->nt_keyixmap = NULL;
1808 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
1809 struct ieee80211vap *match)
1811 struct ieee80211_node *ni, *next;
1813 TAILQ_FOREACH_MUTABLE(ni, &nt->nt_node, ni_list, next) {
1814 if (match != NULL && ni->ni_vap != match)
1816 /* XXX can this happen? if so need's work */
1817 if (ni->ni_associd != 0) {
1818 struct ieee80211vap *vap = ni->ni_vap;
1820 if (vap->iv_auth->ia_node_leave != NULL)
1821 vap->iv_auth->ia_node_leave(ni);
1822 if (vap->iv_aid_bitmap != NULL)
1823 IEEE80211_AID_CLR(vap, ni->ni_associd);
1825 ni->ni_wdsvap = NULL; /* clear reference */
1826 node_reclaim(nt, ni);
1828 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1830 * Make a separate pass to clear references to this vap
1831 * held by DWDS entries. They will not be matched above
1832 * because ni_vap will point to the ap vap but we still
1833 * need to clear ni_wdsvap when the WDS vap is destroyed
1836 TAILQ_FOREACH_MUTABLE(ni, &nt->nt_node, ni_list, next)
1837 if (ni->ni_wdsvap == match)
1838 ni->ni_wdsvap = NULL;
1843 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1845 ieee80211_node_table_reset(nt, NULL);
1846 if (nt->nt_keyixmap != NULL) {
1848 /* XXX verify all entries are NULL */
1850 for (i = 0; i < nt->nt_keyixmax; i++)
1851 if (nt->nt_keyixmap[i] != NULL)
1852 kprintf("%s: %s[%u] still active\n", __func__,
1855 kfree(nt->nt_keyixmap, M_80211_NODE);
1856 nt->nt_keyixmap = NULL;
1861 * Timeout inactive stations and do related housekeeping.
1862 * Note that we cannot hold the node lock while sending a
1863 * frame as this would lead to a LOR. Instead we use a
1864 * generation number to mark nodes that we've scanned and
1865 * drop the lock and restart a scan if we have to time out
1866 * a node. Since we are single-threaded by virtue of
1867 * controlling the inactivity timer we can be sure this will
1868 * process each node only once.
1871 ieee80211_timeout_stations(struct ieee80211com *ic)
1873 struct ieee80211_node_table *nt = &ic->ic_sta;
1874 struct ieee80211vap *vap;
1875 struct ieee80211_node *ni;
1878 gen = ++nt->nt_scangen;
1880 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1881 if (ni->ni_scangen == gen) /* previously handled */
1883 ni->ni_scangen = gen;
1885 * Ignore entries for which have yet to receive an
1886 * authentication frame. These are transient and
1887 * will be reclaimed when the last reference to them
1888 * goes away (when frame xmits complete).
1892 * Only process stations when in RUN state. This
1893 * insures, for example, that we don't timeout an
1894 * inactive station during CAC. Note that CSA state
1895 * is actually handled in ieee80211_node_timeout as
1896 * it applies to more than timeout processing.
1898 if (vap->iv_state != IEEE80211_S_RUN)
1900 /* XXX can vap be NULL? */
1901 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
1902 vap->iv_opmode == IEEE80211_M_STA) &&
1903 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1906 * Free fragment if not needed anymore
1907 * (last fragment older than 1s).
1908 * XXX doesn't belong here, move to node_age
1910 if (ni->ni_rxfrag[0] != NULL &&
1911 ticks > ni->ni_rxfragstamp + hz) {
1912 m_freem(ni->ni_rxfrag[0]);
1913 ni->ni_rxfrag[0] = NULL;
1915 if (ni->ni_inact > 0) {
1917 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1918 "%s: inact %u inact_reload %u nrates %u",
1919 __func__, ni->ni_inact, ni->ni_inact_reload,
1920 ni->ni_rates.rs_nrates);
1923 * Special case ourself; we may be idle for extended periods
1924 * of time and regardless reclaiming our state is wrong.
1925 * XXX run ic_node_age
1927 if (ni == vap->iv_bss)
1929 if (ni->ni_associd != 0 ||
1930 (vap->iv_opmode == IEEE80211_M_IBSS ||
1931 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
1933 * Age/drain resources held by the station.
1935 ic->ic_node_age(ni);
1937 * Probe the station before time it out. We
1938 * send a null data frame which may not be
1939 * universally supported by drivers (need it
1940 * for ps-poll support so it should be...).
1942 * XXX don't probe the station unless we've
1943 * received a frame from them (and have
1944 * some idea of the rates they are capable
1945 * of); this will get fixed more properly
1946 * soon with better handling of the rate set.
1948 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1949 (0 < ni->ni_inact &&
1950 ni->ni_inact <= vap->iv_inact_probe) &&
1951 ni->ni_rates.rs_nrates != 0) {
1953 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1955 "probe station due to inactivity");
1957 * Grab a reference before unlocking the table
1958 * so the node cannot be reclaimed before we
1959 * send the frame. ieee80211_send_nulldata
1960 * understands we've done this and reclaims the
1961 * ref for us as needed.
1963 ieee80211_ref_node(ni);
1964 ieee80211_send_nulldata(ni);
1969 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1970 ni->ni_inact <= 0) {
1972 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1973 "station timed out due to inactivity "
1974 "(refcnt %u)", ieee80211_node_refcnt(ni));
1976 * Send a deauthenticate frame and drop the station.
1977 * This is somewhat complicated due to reference counts
1978 * and locking. At this point a station will typically
1979 * have a reference count of 1. ieee80211_node_leave
1980 * will do a "free" of the node which will drop the
1981 * reference count. But in the meantime a reference
1982 * wil be held by the deauth frame. The actual reclaim
1983 * of the node will happen either after the tx is
1984 * completed or by ieee80211_node_leave.
1986 * Separately we must drop the node lock before sending
1987 * in case the driver takes a lock, as this can result
1988 * in a LOR between the node lock and the driver lock.
1990 ieee80211_ref_node(ni);
1991 if (ni->ni_associd != 0) {
1992 IEEE80211_SEND_MGMT(ni,
1993 IEEE80211_FC0_SUBTYPE_DEAUTH,
1994 IEEE80211_REASON_AUTH_EXPIRE);
1996 ieee80211_node_leave(ni);
1997 ieee80211_free_node(ni);
1998 vap->iv_stats.is_node_timeout++;
2005 * Aggressively reclaim resources. This should be used
2006 * only in a critical situation to reclaim mbuf resources.
2009 ieee80211_drain(struct ieee80211com *ic)
2011 struct ieee80211_node_table *nt = &ic->ic_sta;
2012 struct ieee80211vap *vap;
2013 struct ieee80211_node *ni;
2015 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2017 * Ignore entries for which have yet to receive an
2018 * authentication frame. These are transient and
2019 * will be reclaimed when the last reference to them
2020 * goes away (when frame xmits complete).
2024 * Only process stations when in RUN state. This
2025 * insures, for example, that we don't timeout an
2026 * inactive station during CAC. Note that CSA state
2027 * is actually handled in ieee80211_node_timeout as
2028 * it applies to more than timeout processing.
2030 if (vap->iv_state != IEEE80211_S_RUN)
2032 /* XXX can vap be NULL? */
2033 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2034 vap->iv_opmode == IEEE80211_M_STA) &&
2035 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2039 * XXX doesn't belong here, move to node_drain
2041 if (ni->ni_rxfrag[0] != NULL) {
2042 m_freem(ni->ni_rxfrag[0]);
2043 ni->ni_rxfrag[0] = NULL;
2046 * Drain resources held by the station.
2048 ic->ic_node_drain(ni);
2053 * Per-ieee80211com inactivity timer callback.
2056 ieee80211_node_timeout_callout(void *arg)
2058 struct ieee80211com *ic = arg;
2061 * Defer timeout processing if a channel switch is pending.
2062 * We typically need to be mute so not doing things that
2063 * might generate frames is good to handle in one place.
2064 * Supressing the station timeout processing may extend the
2065 * lifetime of inactive stations (by not decrementing their
2066 * idle counters) but this should be ok unless the CSA is
2067 * active for an unusually long time.
2069 wlan_serialize_enter();
2070 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2071 ieee80211_scan_timeout(ic);
2072 ieee80211_timeout_stations(ic);
2073 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2075 ieee80211_erp_timeout(ic);
2076 ieee80211_ht_timeout(ic);
2078 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2079 ieee80211_node_timeout_callout, ic);
2080 wlan_serialize_exit();
2084 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2085 ieee80211_iter_func *f, void *arg)
2087 struct ieee80211_node *ni;
2090 gen = ++nt->nt_scangen;
2092 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2093 if (ni->ni_scangen != gen) {
2094 ni->ni_scangen = gen;
2095 (void) ieee80211_ref_node(ni);
2097 ieee80211_free_node(ni);
2104 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2106 kprintf("0x%p: mac %6D refcnt %d\n", ni, ni->ni_macaddr, ":",
2107 ieee80211_node_refcnt(ni));
2108 kprintf("\tscangen %u authmode %u flags 0x%x\n",
2109 ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2110 kprintf("\tassocid 0x%x txpower %u vlan %u\n",
2111 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2112 kprintf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2113 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2114 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2115 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2116 ni->ni_rxfragstamp);
2117 kprintf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2118 node_getrssi(ni), ni->ni_noise,
2119 ni->ni_intval, ni->ni_capinfo);
2120 kprintf("\tbssid %6D essid \"%.*s\" channel %u:0x%x\n",
2122 ni->ni_esslen, ni->ni_essid,
2123 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2124 kprintf("\tinact %u inact_reload %u txrate %u\n",
2125 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2126 kprintf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2127 ni->ni_htcap, ni->ni_htparam,
2128 ni->ni_htctlchan, ni->ni_ht2ndchan);
2129 kprintf("\thtopmode %x htstbc %x chw %u\n",
2130 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2134 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2136 ieee80211_iterate_nodes(nt,
2137 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2141 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2143 struct ieee80211vap *vap;
2145 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2146 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2147 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2151 ieee80211_notify_erp(struct ieee80211com *ic)
2153 ieee80211_notify_erp_locked(ic);
2157 * Handle a station joining an 11g network.
2160 ieee80211_node_join_11g(struct ieee80211_node *ni)
2162 struct ieee80211com *ic = ni->ni_ic;
2165 * Station isn't capable of short slot time. Bump
2166 * the count of long slot time stations and disable
2167 * use of short slot time. Note that the actual switch
2168 * over to long slot time use may not occur until the
2169 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2171 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2172 ic->ic_longslotsta++;
2173 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2174 "station needs long slot time, count %d",
2175 ic->ic_longslotsta);
2176 /* XXX vap's w/ conflicting needs won't work */
2177 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2179 * Don't force slot time when switched to turbo
2180 * mode as non-ERP stations won't be present; this
2181 * need only be done when on the normal G channel.
2183 ieee80211_set_shortslottime(ic, 0);
2187 * If the new station is not an ERP station
2188 * then bump the counter and enable protection
2191 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2193 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2194 "station is !ERP, %d non-ERP stations associated",
2197 * If station does not support short preamble
2198 * then we must enable use of Barker preamble.
2200 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2201 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2202 "%s", "station needs long preamble");
2203 ic->ic_flags |= IEEE80211_F_USEBARKER;
2204 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2207 * If protection is configured and this is the first
2208 * indication we should use protection, enable it.
2210 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2211 ic->ic_nonerpsta == 1 &&
2212 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2213 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2214 "%s: enable use of protection\n", __func__);
2215 ic->ic_flags |= IEEE80211_F_USEPROT;
2216 ieee80211_notify_erp_locked(ic);
2219 ni->ni_flags |= IEEE80211_NODE_ERP;
2223 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2225 struct ieee80211com *ic = ni->ni_ic;
2226 struct ieee80211vap *vap = ni->ni_vap;
2229 if (ni->ni_associd == 0) {
2232 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2234 * It would be good to search the bitmap
2235 * more efficiently, but this will do for now.
2237 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2238 if (!IEEE80211_AID_ISSET(vap, aid))
2241 if (aid >= vap->iv_max_aid) {
2242 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2243 ieee80211_node_leave(ni);
2246 ni->ni_associd = aid | 0xc000;
2247 ni->ni_jointime = time_second;
2248 IEEE80211_AID_SET(vap, ni->ni_associd);
2249 vap->iv_sta_assoc++;
2252 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2253 ieee80211_ht_node_join(ni);
2254 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2255 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2256 ieee80211_node_join_11g(ni);
2262 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2263 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2264 IEEE80211_NODE_AID(ni),
2265 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2266 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2267 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2268 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2269 ni->ni_flags & IEEE80211_NODE_HT ?
2270 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2271 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2272 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2273 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2274 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2275 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2276 ", fast-frames" : "",
2277 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2281 ieee80211_node_setuptxparms(ni);
2282 /* give driver a chance to setup state like ni_txrate */
2283 if (ic->ic_newassoc != NULL)
2284 ic->ic_newassoc(ni, newassoc);
2285 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2286 /* tell the authenticator about new station */
2287 if (vap->iv_auth->ia_node_join != NULL)
2288 vap->iv_auth->ia_node_join(ni);
2289 ieee80211_notify_node_join(ni,
2290 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2294 disable_protection(struct ieee80211com *ic)
2296 KASSERT(ic->ic_nonerpsta == 0 &&
2297 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2298 ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2301 ic->ic_flags &= ~IEEE80211_F_USEPROT;
2302 /* XXX verify mode? */
2303 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2304 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2305 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2307 ieee80211_notify_erp_locked(ic);
2311 * Handle a station leaving an 11g network.
2314 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2316 struct ieee80211com *ic = ni->ni_ic;
2318 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2319 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2320 ic->ic_bsschan->ic_flags));
2323 * If a long slot station do the slot time bookkeeping.
2325 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2326 KASSERT(ic->ic_longslotsta > 0,
2327 ("bogus long slot station count %d", ic->ic_longslotsta));
2328 ic->ic_longslotsta--;
2329 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2330 "long slot time station leaves, count now %d",
2331 ic->ic_longslotsta);
2332 if (ic->ic_longslotsta == 0) {
2334 * Re-enable use of short slot time if supported
2335 * and not operating in IBSS mode (per spec).
2337 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2338 ic->ic_opmode != IEEE80211_M_IBSS) {
2339 IEEE80211_DPRINTF(ni->ni_vap,
2340 IEEE80211_MSG_ASSOC,
2341 "%s: re-enable use of short slot time\n",
2343 ieee80211_set_shortslottime(ic, 1);
2348 * If a non-ERP station do the protection-related bookkeeping.
2350 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2351 KASSERT(ic->ic_nonerpsta > 0,
2352 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2354 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2355 "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2356 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2357 " (non-ERP sta present)" : "");
2358 if (ic->ic_nonerpsta == 0 &&
2359 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2360 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2361 "%s: disable use of protection\n", __func__);
2362 disable_protection(ic);
2368 * Time out presence of an overlapping bss with non-ERP
2369 * stations. When operating in hostap mode we listen for
2370 * beacons from other stations and if we identify a non-ERP
2371 * station is present we enable protection. To identify
2372 * when all non-ERP stations are gone we time out this
2376 ieee80211_erp_timeout(struct ieee80211com *ic)
2378 if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2379 time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2381 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2382 "%s", "age out non-ERP sta present on channel");
2384 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2385 if (ic->ic_nonerpsta == 0)
2386 disable_protection(ic);
2391 * Handle bookkeeping for station deauthentication/disassociation
2392 * when operating as an ap.
2395 ieee80211_node_leave(struct ieee80211_node *ni)
2397 struct ieee80211com *ic = ni->ni_ic;
2398 struct ieee80211vap *vap = ni->ni_vap;
2399 struct ieee80211_node_table *nt = ni->ni_table;
2401 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2402 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2404 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2405 ("unexpected operating mode %u", vap->iv_opmode));
2407 * If node wasn't previously associated all
2408 * we need to do is reclaim the reference.
2410 /* XXX ibss mode bypasses 11g and notification */
2411 if (ni->ni_associd == 0)
2414 * Tell the authenticator the station is leaving.
2415 * Note that we must do this before yanking the
2416 * association id as the authenticator uses the
2417 * associd to locate it's state block.
2419 if (vap->iv_auth->ia_node_leave != NULL)
2420 vap->iv_auth->ia_node_leave(ni);
2422 IEEE80211_AID_CLR(vap, ni->ni_associd);
2424 vap->iv_sta_assoc--;
2427 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2428 ieee80211_ht_node_leave(ni);
2429 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2430 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2431 ieee80211_node_leave_11g(ni);
2433 * Cleanup station state. In particular clear various
2434 * state that might otherwise be reused if the node
2435 * is reused before the reference count goes to zero
2436 * (and memory is reclaimed).
2438 ieee80211_sta_leave(ni);
2441 * Remove the node from any table it's recorded in and
2442 * drop the caller's reference. Removal from the table
2443 * is important to insure the node is not reprocessed
2447 node_reclaim(nt, ni);
2449 ieee80211_free_node(ni);
2453 struct ieee80211vap *vap;
2455 uint32_t rssi_total;
2459 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2461 struct rssiinfo *info = arg;
2462 struct ieee80211vap *vap = ni->ni_vap;
2465 if (info->vap != vap)
2467 /* only associated stations */
2468 if (ni->ni_associd == 0)
2470 rssi = vap->iv_ic->ic_node_getrssi(ni);
2472 info->rssi_samples++;
2473 info->rssi_total += rssi;
2478 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2480 struct rssiinfo *info = arg;
2481 struct ieee80211vap *vap = ni->ni_vap;
2484 if (info->vap != vap)
2486 /* only neighbors */
2487 /* XXX check bssid */
2488 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2490 rssi = vap->iv_ic->ic_node_getrssi(ni);
2492 info->rssi_samples++;
2493 info->rssi_total += rssi;
2497 #ifdef IEEE80211_SUPPORT_MESH
2499 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
2501 struct rssiinfo *info = arg;
2502 struct ieee80211vap *vap = ni->ni_vap;
2505 if (info->vap != vap)
2507 /* only neighbors that peered successfully */
2508 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
2510 rssi = vap->iv_ic->ic_node_getrssi(ni);
2512 info->rssi_samples++;
2513 info->rssi_total += rssi;
2516 #endif /* IEEE80211_SUPPORT_MESH */
2519 ieee80211_getrssi(struct ieee80211vap *vap)
2521 #define NZ(x) ((x) == 0 ? 1 : (x))
2522 struct ieee80211com *ic = vap->iv_ic;
2523 struct rssiinfo info;
2525 info.rssi_total = 0;
2526 info.rssi_samples = 0;
2528 switch (vap->iv_opmode) {
2529 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
2530 case IEEE80211_M_AHDEMO: /* average of all neighbors */
2531 ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2533 case IEEE80211_M_HOSTAP: /* average of all associated stations */
2534 ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2536 #ifdef IEEE80211_SUPPORT_MESH
2537 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
2538 ieee80211_iterate_nodes(&ic->ic_sta, get_mesh_rssi, &info);
2541 case IEEE80211_M_MONITOR: /* XXX */
2542 case IEEE80211_M_STA: /* use stats from associated ap */
2544 if (vap->iv_bss != NULL)
2545 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2546 info.rssi_samples = 1;
2549 return info.rssi_total / NZ(info.rssi_samples);
2554 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2557 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
2559 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2560 /* for non-station mode return avg'd rssi accounting */
2561 if (vap->iv_opmode != IEEE80211_M_STA)
2562 *rssi = ieee80211_getrssi(vap);