2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
38 #include <sys/socket.h>
41 #include <net/if_media.h>
42 #include <net/ethernet.h>
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_input.h>
46 #ifdef IEEE80211_SUPPORT_TDMA
47 #include <net80211/ieee80211_tdma.h>
49 #include <net80211/ieee80211_wds.h>
54 * Association id's are managed with a bit vector.
56 #define IEEE80211_AID_SET(_vap, b) \
57 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
58 (1 << (IEEE80211_AID(b) % 32)))
59 #define IEEE80211_AID_CLR(_vap, b) \
60 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
61 ~(1 << (IEEE80211_AID(b) % 32)))
62 #define IEEE80211_AID_ISSET(_vap, b) \
63 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
65 #ifdef IEEE80211_DEBUG_REFCNT
66 #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
68 #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
71 static int ieee80211_sta_join1(struct ieee80211_node *);
73 static struct ieee80211_node *node_alloc(struct ieee80211vap *,
74 const uint8_t [IEEE80211_ADDR_LEN]);
75 static void node_cleanup(struct ieee80211_node *);
76 static void node_free(struct ieee80211_node *);
77 static void node_age(struct ieee80211_node *);
78 static int8_t node_getrssi(const struct ieee80211_node *);
79 static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
80 static void node_getmimoinfo(const struct ieee80211_node *,
81 struct ieee80211_mimo_info *);
83 static void _ieee80211_free_node(struct ieee80211_node *);
85 static void ieee80211_node_table_init(struct ieee80211com *ic,
86 struct ieee80211_node_table *nt, const char *name,
87 int inact, int keymaxix);
88 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
89 struct ieee80211vap *);
90 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
91 static void ieee80211_erp_timeout(struct ieee80211com *);
93 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
94 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
97 ieee80211_node_attach(struct ieee80211com *ic)
99 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
100 IEEE80211_INACT_INIT, ic->ic_max_keyix);
101 callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
102 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
103 ieee80211_node_timeout, ic);
105 ic->ic_node_alloc = node_alloc;
106 ic->ic_node_free = node_free;
107 ic->ic_node_cleanup = node_cleanup;
108 ic->ic_node_age = node_age;
109 ic->ic_node_drain = node_age; /* NB: same as age */
110 ic->ic_node_getrssi = node_getrssi;
111 ic->ic_node_getsignal = node_getsignal;
112 ic->ic_node_getmimoinfo = node_getmimoinfo;
115 * Set flags to be propagated to all vap's;
116 * these define default behaviour/configuration.
118 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
122 ieee80211_node_detach(struct ieee80211com *ic)
125 callout_drain(&ic->ic_inact);
126 ieee80211_node_table_cleanup(&ic->ic_sta);
130 ieee80211_node_vattach(struct ieee80211vap *vap)
132 /* NB: driver can override */
133 vap->iv_max_aid = IEEE80211_AID_DEF;
135 /* default station inactivity timer setings */
136 vap->iv_inact_init = IEEE80211_INACT_INIT;
137 vap->iv_inact_auth = IEEE80211_INACT_AUTH;
138 vap->iv_inact_run = IEEE80211_INACT_RUN;
139 vap->iv_inact_probe = IEEE80211_INACT_PROBE;
141 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
142 "%s: init %u auth %u run %u probe %u\n", __func__,
143 vap->iv_inact_init, vap->iv_inact_auth,
144 vap->iv_inact_run, vap->iv_inact_probe);
148 ieee80211_node_latevattach(struct ieee80211vap *vap)
150 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
151 /* XXX should we allow max aid to be zero? */
152 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
153 vap->iv_max_aid = IEEE80211_AID_MIN;
154 if_printf(vap->iv_ifp,
155 "WARNING: max aid too small, changed to %d\n",
158 vap->iv_aid_bitmap = (uint32_t *) malloc(
159 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
160 M_80211_NODE, M_NOWAIT | M_ZERO);
161 if (vap->iv_aid_bitmap == NULL) {
162 /* XXX no way to recover */
163 printf("%s: no memory for AID bitmap, max aid %d!\n",
164 __func__, vap->iv_max_aid);
169 ieee80211_reset_bss(vap);
171 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
175 ieee80211_node_vdetach(struct ieee80211vap *vap)
177 struct ieee80211com *ic = vap->iv_ic;
179 ieee80211_node_table_reset(&ic->ic_sta, vap);
180 if (vap->iv_bss != NULL) {
181 ieee80211_free_node(vap->iv_bss);
184 if (vap->iv_aid_bitmap != NULL) {
185 free(vap->iv_aid_bitmap, M_80211_NODE);
186 vap->iv_aid_bitmap = NULL;
191 * Port authorize/unauthorize interfaces for use by an authenticator.
195 ieee80211_node_authorize(struct ieee80211_node *ni)
197 struct ieee80211vap *vap = ni->ni_vap;
199 ni->ni_flags |= IEEE80211_NODE_AUTH;
200 ni->ni_inact_reload = vap->iv_inact_run;
201 ni->ni_inact = ni->ni_inact_reload;
203 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
204 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
208 ieee80211_node_unauthorize(struct ieee80211_node *ni)
210 struct ieee80211vap *vap = ni->ni_vap;
212 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
213 ni->ni_inact_reload = vap->iv_inact_auth;
214 if (ni->ni_inact > ni->ni_inact_reload)
215 ni->ni_inact = ni->ni_inact_reload;
217 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
218 "%s: inact_reload %u inact %u", __func__,
219 ni->ni_inact_reload, ni->ni_inact);
223 * Fix tx parameters for a node according to ``association state''.
226 node_setuptxparms(struct ieee80211_node *ni)
228 struct ieee80211vap *vap = ni->ni_vap;
229 enum ieee80211_phymode mode;
231 if (ni->ni_flags & IEEE80211_NODE_HT) {
232 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
233 mode = IEEE80211_MODE_11NA;
235 mode = IEEE80211_MODE_11NG;
236 } else { /* legacy rate handling */
237 /* NB: 108A/108G should be handled as 11a/11g respectively */
238 if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
239 mode = IEEE80211_MODE_STURBO_A;
240 else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
241 mode = IEEE80211_MODE_HALF;
242 else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
243 mode = IEEE80211_MODE_QUARTER;
244 else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
245 mode = IEEE80211_MODE_11A;
246 else if (ni->ni_flags & IEEE80211_NODE_ERP)
247 mode = IEEE80211_MODE_11G;
249 mode = IEEE80211_MODE_11B;
251 ni->ni_txparms = &vap->iv_txparms[mode];
255 * Set/change the channel. The rate set is also updated as
256 * to insure a consistent view by drivers.
257 * XXX should be private but hostap needs it to deal with CSA
260 ieee80211_node_set_chan(struct ieee80211_node *ni,
261 struct ieee80211_channel *chan)
263 struct ieee80211com *ic = ni->ni_ic;
264 struct ieee80211vap *vap = ni->ni_vap;
265 enum ieee80211_phymode mode;
267 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
270 mode = ieee80211_chan2mode(chan);
271 if (IEEE80211_IS_CHAN_HT(chan)) {
273 * XXX Gotta be careful here; the rate set returned by
274 * ieee80211_get_suprates is actually any HT rate
275 * set so blindly copying it will be bad. We must
276 * install the legacy rate est in ni_rates and the
277 * HT rate set in ni_htrates.
279 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
281 * Setup bss tx parameters based on operating mode. We
282 * use legacy rates when operating in a mixed HT+non-HT bss
283 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
285 if (mode == IEEE80211_MODE_11NA &&
286 (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
287 mode = IEEE80211_MODE_11A;
288 else if (mode == IEEE80211_MODE_11NG &&
289 (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
290 mode = IEEE80211_MODE_11G;
291 if (mode == IEEE80211_MODE_11G &&
292 (vap->iv_flags & IEEE80211_F_PUREG) == 0)
293 mode = IEEE80211_MODE_11B;
295 ni->ni_txparms = &vap->iv_txparms[mode];
296 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
300 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
302 /* propagate useful state */
303 nbss->ni_authmode = obss->ni_authmode;
304 nbss->ni_txpower = obss->ni_txpower;
305 nbss->ni_vlan = obss->ni_vlan;
306 /* XXX statistics? */
307 /* XXX legacy WDS bssid? */
311 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
313 struct ieee80211com *ic = vap->iv_ic;
314 struct ieee80211_node *ni;
316 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
317 "%s: creating ibss on channel %u\n", __func__,
318 ieee80211_chan2ieee(ic, chan));
320 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
325 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
326 ni->ni_esslen = vap->iv_des_ssid[0].len;
327 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
328 if (vap->iv_bss != NULL)
329 copy_bss(ni, vap->iv_bss);
330 ni->ni_intval = ic->ic_bintval;
331 if (vap->iv_flags & IEEE80211_F_PRIVACY)
332 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
333 if (ic->ic_phytype == IEEE80211_T_FH) {
334 ni->ni_fhdwell = 200; /* XXX */
337 if (vap->iv_opmode == IEEE80211_M_IBSS) {
338 vap->iv_flags |= IEEE80211_F_SIBSS;
339 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
340 if (vap->iv_flags & IEEE80211_F_DESBSSID)
341 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
343 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
344 /* clear group bit, add local bit */
345 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
347 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
348 if (vap->iv_flags & IEEE80211_F_DESBSSID)
349 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
351 #ifdef IEEE80211_SUPPORT_TDMA
352 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
354 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
357 * Fix the channel and related attributes.
359 /* clear DFS CAC state on previous channel */
360 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
361 ic->ic_bsschan->ic_freq != chan->ic_freq &&
362 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
363 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
364 ic->ic_bsschan = chan;
365 ieee80211_node_set_chan(ni, chan);
366 ic->ic_curmode = ieee80211_chan2mode(chan);
368 * Do mode-specific setup.
370 if (IEEE80211_IS_CHAN_FULL(chan)) {
371 if (IEEE80211_IS_CHAN_ANYG(chan)) {
373 * Use a mixed 11b/11g basic rate set.
375 ieee80211_setbasicrates(&ni->ni_rates,
377 if (vap->iv_flags & IEEE80211_F_PUREG) {
379 * Also mark OFDM rates basic so 11b
380 * stations do not join (WiFi compliance).
382 ieee80211_addbasicrates(&ni->ni_rates,
385 } else if (IEEE80211_IS_CHAN_B(chan)) {
387 * Force pure 11b rate set.
389 ieee80211_setbasicrates(&ni->ni_rates,
394 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
398 * Reset bss state on transition to the INIT state.
399 * Clear any stations from the table (they have been
400 * deauth'd) and reset the bss node (clears key, rate
404 ieee80211_reset_bss(struct ieee80211vap *vap)
406 struct ieee80211com *ic = vap->iv_ic;
407 struct ieee80211_node *ni, *obss;
409 ieee80211_node_table_reset(&ic->ic_sta, vap);
410 /* XXX multi-bss: wrong */
411 ieee80211_reset_erp(ic);
413 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
414 KASSERT(ni != NULL, ("unable to setup inital BSS node"));
416 vap->iv_bss = ieee80211_ref_node(ni);
419 ni->ni_intval = ic->ic_bintval;
420 ieee80211_free_node(obss);
422 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
426 match_ssid(const struct ieee80211_node *ni,
427 int nssid, const struct ieee80211_scan_ssid ssids[])
431 for (i = 0; i < nssid; i++) {
432 if (ni->ni_esslen == ssids[i].len &&
433 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
440 * Test a node for suitability/compatibility.
443 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
445 struct ieee80211com *ic = ni->ni_ic;
448 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
450 if (vap->iv_opmode == IEEE80211_M_IBSS) {
451 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
454 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
457 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
458 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
461 /* XXX does this mean privacy is supported or required? */
462 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
465 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
466 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
467 if (rate & IEEE80211_RATE_BASIC)
469 if (vap->iv_des_nssid != 0 &&
470 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
472 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
473 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
478 #ifdef IEEE80211_DEBUG
480 * Display node suitability/compatibility.
483 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
485 struct ieee80211com *ic = ni->ni_ic;
490 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
492 if (vap->iv_opmode == IEEE80211_M_IBSS) {
493 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
496 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
499 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
500 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
503 /* XXX does this mean privacy is supported or required? */
504 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
507 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
508 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
509 if (rate & IEEE80211_RATE_BASIC)
511 if (vap->iv_des_nssid != 0 &&
512 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
514 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
515 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
518 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
519 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
521 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
522 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
523 fail & 0x08 ? '!' : ' ');
525 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
526 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
528 fail & 0x02 ? '!' : ' ');
530 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
531 fail & 0x04 ? '!' : ' ');
532 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
533 printf("%s\n", fail & 0x10 ? "!" : "");
535 #endif /* IEEE80211_DEBUG */
538 * Handle 802.11 ad hoc network merge. The
539 * convention, set by the Wireless Ethernet Compatibility Alliance
540 * (WECA), is that an 802.11 station will change its BSSID to match
541 * the "oldest" 802.11 ad hoc network, on the same channel, that
542 * has the station's desired SSID. The "oldest" 802.11 network
543 * sends beacons with the greatest TSF timestamp.
545 * The caller is assumed to validate TSF's before attempting a merge.
547 * Return !0 if the BSSID changed, 0 otherwise.
550 ieee80211_ibss_merge(struct ieee80211_node *ni)
552 struct ieee80211vap *vap = ni->ni_vap;
553 #ifdef IEEE80211_DEBUG
554 struct ieee80211com *ic = ni->ni_ic;
557 if (ni == vap->iv_bss ||
558 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
559 /* unchanged, nothing to do */
562 if (!check_bss(vap, ni)) {
563 /* capabilities mismatch */
564 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
565 "%s: merge failed, capabilities mismatch\n", __func__);
566 #ifdef IEEE80211_DEBUG
567 if (ieee80211_msg_assoc(vap))
568 check_bss_debug(vap, ni);
570 vap->iv_stats.is_ibss_capmismatch++;
573 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
574 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
575 ether_sprintf(ni->ni_bssid),
576 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
577 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
578 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
580 return ieee80211_sta_join1(ieee80211_ref_node(ni));
584 * Calculate HT channel promotion flags for all vaps.
585 * This assumes ni_chan have been setup for each vap.
588 gethtadjustflags(struct ieee80211com *ic)
590 struct ieee80211vap *vap;
595 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
596 if (vap->iv_state < IEEE80211_S_RUN)
598 switch (vap->iv_opmode) {
599 case IEEE80211_M_WDS:
600 case IEEE80211_M_STA:
601 case IEEE80211_M_AHDEMO:
602 case IEEE80211_M_HOSTAP:
603 case IEEE80211_M_IBSS:
604 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
614 * Check if the current channel needs to change based on whether
615 * any vap's are using HT20/HT40. This is used to sync the state
616 * of ic_curchan after a channel width change on a running vap.
619 ieee80211_sync_curchan(struct ieee80211com *ic)
621 struct ieee80211_channel *c;
623 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
624 if (c != ic->ic_curchan) {
626 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
627 ic->ic_set_channel(ic);
632 * Change the current channel. The request channel may be
633 * promoted if other vap's are operating with HT20/HT40.
636 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
638 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
639 int flags = gethtadjustflags(ic);
641 * Check for channel promotion required to support the
642 * set of running vap's. This assumes we are called
643 * after ni_chan is setup for each vap.
645 /* NB: this assumes IEEE80211_FEXT_USEHT40 > IEEE80211_FEXT_HT */
646 if (flags > ieee80211_htchanflags(c))
647 c = ieee80211_ht_adjust_channel(ic, c, flags);
649 ic->ic_bsschan = ic->ic_curchan = c;
650 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
651 ic->ic_set_channel(ic);
655 * Join the specified IBSS/BSS network. The node is assumed to
656 * be passed in with a held reference.
659 ieee80211_sta_join1(struct ieee80211_node *selbs)
661 struct ieee80211vap *vap = selbs->ni_vap;
662 struct ieee80211com *ic = selbs->ni_ic;
663 struct ieee80211_node *obss;
667 * Committed to selbs, setup state.
671 * Check if old+new node have the same address in which
672 * case we can reassociate when operating in sta mode.
674 canreassoc = (obss != NULL &&
675 vap->iv_state == IEEE80211_S_RUN &&
676 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
677 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */
679 copy_bss(selbs, obss);
680 ieee80211_node_decref(obss); /* iv_bss reference */
681 ieee80211_free_node(obss); /* station table reference */
682 obss = NULL; /* NB: guard against later use */
686 * Delete unusable rates; we've already checked
687 * that the negotiated rate set is acceptable.
689 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
690 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
692 ieee80211_setcurchan(ic, selbs->ni_chan);
694 * Set the erp state (mostly the slot time) to deal with
695 * the auto-select case; this should be redundant if the
698 ieee80211_reset_erp(ic);
699 ieee80211_wme_initparams(vap);
701 if (vap->iv_opmode == IEEE80211_M_STA) {
704 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
707 * Act as if we received a DEAUTH frame in case we
708 * are invoked from the RUN state. This will cause
709 * us to try to re-authenticate if we are operating
712 ieee80211_new_state(vap, IEEE80211_S_AUTH,
713 IEEE80211_FC0_SUBTYPE_DEAUTH);
716 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
721 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
722 const struct ieee80211_scan_entry *se)
724 struct ieee80211com *ic = vap->iv_ic;
725 struct ieee80211_node *ni;
727 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
733 * Expand scan state into node's format.
734 * XXX may not need all this stuff
736 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
737 ni->ni_esslen = se->se_ssid[1];
738 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
739 ni->ni_rstamp = se->se_rstamp;
740 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
741 ni->ni_intval = se->se_intval;
742 ni->ni_capinfo = se->se_capinfo;
744 ni->ni_timoff = se->se_timoff;
745 ni->ni_fhdwell = se->se_fhdwell;
746 ni->ni_fhindex = se->se_fhindex;
747 ni->ni_erp = se->se_erp;
748 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
749 ni->ni_noise = se->se_noise;
750 if (vap->iv_opmode == IEEE80211_M_STA) {
751 /* NB: only infrastructure mode requires an associd */
752 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
755 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
756 ieee80211_ies_expand(&ni->ni_ies);
757 if (ni->ni_ies.ath_ie != NULL)
758 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
759 if (ni->ni_ies.htcap_ie != NULL)
760 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
761 if (ni->ni_ies.htinfo_ie != NULL)
762 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
763 #ifdef IEEE80211_SUPPORT_TDMA
764 if (ni->ni_ies.tdma_ie != NULL)
765 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
769 vap->iv_dtim_period = se->se_dtimperiod;
770 vap->iv_dtim_count = 0;
772 /* NB: must be after ni_chan is setup */
773 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
775 if (ieee80211_iserp_rateset(&ni->ni_rates))
776 ni->ni_flags |= IEEE80211_NODE_ERP;
777 node_setuptxparms(ni);
779 return ieee80211_sta_join1(ieee80211_ref_node(ni));
783 * Leave the specified IBSS/BSS network. The node is assumed to
784 * be passed in with a held reference.
787 ieee80211_sta_leave(struct ieee80211_node *ni)
789 struct ieee80211com *ic = ni->ni_ic;
791 ic->ic_node_cleanup(ni);
792 ieee80211_notify_node_leave(ni);
796 * Send a deauthenticate frame and drop the station.
799 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
801 /* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
802 ieee80211_ref_node(ni);
803 if (ni->ni_associd != 0)
804 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
805 ieee80211_node_leave(ni);
806 ieee80211_free_node(ni);
809 static struct ieee80211_node *
810 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
812 struct ieee80211_node *ni;
814 ni = (struct ieee80211_node *) malloc(sizeof(struct ieee80211_node),
815 M_80211_NODE, M_NOWAIT | M_ZERO);
820 * Initialize an ie blob with the specified data. If previous
821 * data exists re-use the data block. As a side effect we clear
822 * all references to specific ie's; the caller is required to
826 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
828 /* NB: assumes data+len are the last fields */
829 memset(ies, 0, offsetof(struct ieee80211_ies, data));
830 if (ies->data != NULL && ies->len != len) {
831 /* data size changed */
832 free(ies->data, M_80211_NODE_IE);
835 if (ies->data == NULL) {
836 ies->data = (uint8_t *) malloc(len, M_80211_NODE_IE, M_NOWAIT);
837 if (ies->data == NULL) {
839 /* NB: pointers have already been zero'd above */
843 memcpy(ies->data, data, len);
849 * Reclaim storage for an ie blob.
852 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
854 if (ies->data != NULL)
855 free(ies->data, M_80211_NODE_IE);
859 * Expand an ie blob data contents and to fillin individual
860 * ie pointers. The data blob is assumed to be well-formed;
861 * we don't do any validity checking of ie lengths.
864 ieee80211_ies_expand(struct ieee80211_ies *ies)
873 case IEEE80211_ELEMID_VENDOR:
876 else if (iswmeoui(ie))
878 else if (isatherosoui(ie))
880 #ifdef IEEE80211_SUPPORT_TDMA
881 else if (istdmaoui(ie))
885 case IEEE80211_ELEMID_RSN:
888 case IEEE80211_ELEMID_HTCAP:
898 * Reclaim any resources in a node and reset any critical
899 * state. Typically nodes are free'd immediately after,
900 * but in some cases the storage may be reused so we need
901 * to insure consistent state (should probably fix that).
904 node_cleanup(struct ieee80211_node *ni)
906 #define N(a) (sizeof(a)/sizeof(a[0]))
907 struct ieee80211vap *vap = ni->ni_vap;
910 /* NB: preserve ni_table */
911 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
912 if (vap->iv_opmode != IEEE80211_M_STA)
914 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
915 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
916 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
919 * Cleanup any HT-related state.
921 if (ni->ni_flags & IEEE80211_NODE_HT)
922 ieee80211_ht_node_cleanup(ni);
924 * Clear AREF flag that marks the authorization refcnt bump
925 * has happened. This is probably not needed as the node
926 * should always be removed from the table so not found but
927 * do it just in case.
928 * Likewise clear the ASSOCID flag as these flags are intended
929 * to be managed in tandem.
931 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
934 * Drain power save queue and, if needed, clear TIM.
936 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
937 vap->iv_set_tim(ni, 0);
940 if (ni->ni_challenge != NULL) {
941 free(ni->ni_challenge, M_80211_NODE);
942 ni->ni_challenge = NULL;
945 * Preserve SSID, WPA, and WME ie's so the bss node is
946 * reusable during a re-auth/re-assoc state transition.
947 * If we remove these data they will not be recreated
948 * because they come from a probe-response or beacon frame
949 * which cannot be expected prior to the association-response.
950 * This should not be an issue when operating in other modes
951 * as stations leaving always go through a full state transition
952 * which will rebuild this state.
954 * XXX does this leave us open to inheriting old state?
956 for (i = 0; i < N(ni->ni_rxfrag); i++)
957 if (ni->ni_rxfrag[i] != NULL) {
958 m_freem(ni->ni_rxfrag[i]);
959 ni->ni_rxfrag[i] = NULL;
962 * Must be careful here to remove any key map entry w/o a LOR.
964 ieee80211_node_delucastkey(ni);
969 node_free(struct ieee80211_node *ni)
971 struct ieee80211com *ic = ni->ni_ic;
973 ic->ic_node_cleanup(ni);
974 ieee80211_ies_cleanup(&ni->ni_ies);
975 ieee80211_psq_cleanup(&ni->ni_psq);
976 IEEE80211_NODE_WDSQ_DESTROY(ni);
977 free(ni, M_80211_NODE);
981 node_age(struct ieee80211_node *ni)
983 struct ieee80211vap *vap = ni->ni_vap;
985 IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
988 * Age frames on the power save queue.
990 if (ieee80211_node_psq_age(ni) != 0 &&
991 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
992 vap->iv_set_tim(ni, 0);
994 * Age frames on the wds pending queue.
996 if (IEEE80211_NODE_WDSQ_QLEN(ni) != 0)
997 ieee80211_node_wdsq_age(ni);
999 * Age out HT resources (e.g. frames on the
1000 * A-MPDU reorder queues).
1002 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1003 ieee80211_ht_node_age(ni);
1007 node_getrssi(const struct ieee80211_node *ni)
1009 uint32_t avgrssi = ni->ni_avgrssi;
1012 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1014 rssi = IEEE80211_RSSI_GET(avgrssi);
1015 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1019 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1021 *rssi = node_getrssi(ni);
1022 *noise = ni->ni_noise;
1026 node_getmimoinfo(const struct ieee80211_node *ni,
1027 struct ieee80211_mimo_info *info)
1029 /* XXX zero data? */
1032 struct ieee80211_node *
1033 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1034 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1036 struct ieee80211com *ic = nt->nt_ic;
1037 struct ieee80211_node *ni;
1040 ni = ic->ic_node_alloc(vap, macaddr);
1042 vap->iv_stats.is_rx_nodealloc++;
1046 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1047 "%s %p<%s> in %s table\n", __func__, ni,
1048 ether_sprintf(macaddr), nt->nt_name);
1050 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1051 hash = IEEE80211_NODE_HASH(macaddr);
1052 ieee80211_node_initref(ni); /* mark referenced */
1053 ni->ni_chan = IEEE80211_CHAN_ANYC;
1054 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1055 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1056 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1057 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1058 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1059 ni->ni_inact_reload = nt->nt_inact_init;
1060 ni->ni_inact = ni->ni_inact_reload;
1061 ni->ni_ath_defkeyix = 0x7fff;
1062 ieee80211_psq_init(&ni->ni_psq, "unknown");
1063 IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1065 IEEE80211_NODE_LOCK(nt);
1066 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1067 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1071 IEEE80211_NODE_UNLOCK(nt);
1073 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1074 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1080 * Craft a temporary node suitable for sending a management frame
1081 * to the specified station. We craft only as much state as we
1082 * need to do the work since the node will be immediately reclaimed
1083 * once the send completes.
1085 struct ieee80211_node *
1086 ieee80211_tmp_node(struct ieee80211vap *vap,
1087 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1089 struct ieee80211com *ic = vap->iv_ic;
1090 struct ieee80211_node *ni;
1092 ni = ic->ic_node_alloc(vap, macaddr);
1094 struct ieee80211_node *bss = vap->iv_bss;
1096 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1097 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1099 ni->ni_table = NULL; /* NB: pedantic */
1100 ni->ni_ic = ic; /* NB: needed to set channel */
1103 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1104 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1105 ieee80211_node_initref(ni); /* mark referenced */
1106 /* NB: required by ieee80211_fix_rate */
1107 ieee80211_node_set_chan(ni, bss->ni_chan);
1108 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1109 IEEE80211_KEYIX_NONE);
1110 ni->ni_txpower = bss->ni_txpower;
1111 /* XXX optimize away */
1112 ieee80211_psq_init(&ni->ni_psq, "unknown");
1113 IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1116 vap->iv_stats.is_rx_nodealloc++;
1121 struct ieee80211_node *
1122 ieee80211_dup_bss(struct ieee80211vap *vap,
1123 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1125 struct ieee80211com *ic = vap->iv_ic;
1126 struct ieee80211_node *ni;
1128 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1130 struct ieee80211_node *bss = vap->iv_bss;
1132 * Inherit from iv_bss.
1135 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1136 ieee80211_node_set_chan(ni, bss->ni_chan);
1142 * Create a bss node for a legacy WDS vap. The far end does
1143 * not associate so we just create create a new node and
1144 * simulate an association. The caller is responsible for
1145 * installing the node as the bss node and handling any further
1146 * setup work like authorizing the port.
1148 struct ieee80211_node *
1149 ieee80211_node_create_wds(struct ieee80211vap *vap,
1150 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1152 struct ieee80211com *ic = vap->iv_ic;
1153 struct ieee80211_node *ni;
1155 /* XXX check if node already in sta table? */
1156 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1158 ni->ni_wdsvap = vap;
1159 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1161 * Inherit any manually configured settings.
1163 copy_bss(ni, vap->iv_bss);
1164 ieee80211_node_set_chan(ni, chan);
1165 /* NB: propagate ssid so available to WPA supplicant */
1166 ni->ni_esslen = vap->iv_des_ssid[0].len;
1167 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1168 /* NB: no associd for peer */
1170 * There are no management frames to use to
1171 * discover neighbor capabilities, so blindly
1172 * propagate the local configuration.
1174 if (vap->iv_flags & IEEE80211_F_WME)
1175 ni->ni_flags |= IEEE80211_NODE_QOS;
1176 if (vap->iv_flags & IEEE80211_F_FF)
1177 ni->ni_flags |= IEEE80211_NODE_FF;
1178 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1179 (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1181 * Device is HT-capable and HT is enabled for
1182 * the vap; setup HT operation. On return
1183 * ni_chan will be adjusted to an HT channel.
1185 ieee80211_ht_wds_init(ni);
1187 struct ieee80211_channel *c = ni->ni_chan;
1189 * Force a legacy channel to be used.
1191 c = ieee80211_find_channel(ic,
1192 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1193 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1194 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1201 struct ieee80211_node *
1202 #ifdef IEEE80211_DEBUG_REFCNT
1203 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1204 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1206 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1207 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1210 struct ieee80211_node *ni;
1213 IEEE80211_NODE_LOCK_ASSERT(nt);
1215 hash = IEEE80211_NODE_HASH(macaddr);
1216 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1217 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1218 ieee80211_ref_node(ni); /* mark referenced */
1219 #ifdef IEEE80211_DEBUG_REFCNT
1220 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1221 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1223 ni, ether_sprintf(ni->ni_macaddr),
1224 ieee80211_node_refcnt(ni));
1232 struct ieee80211_node *
1233 #ifdef IEEE80211_DEBUG_REFCNT
1234 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1235 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1237 ieee80211_find_node(struct ieee80211_node_table *nt,
1238 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1241 struct ieee80211_node *ni;
1243 IEEE80211_NODE_LOCK(nt);
1244 ni = ieee80211_find_node_locked(nt, macaddr);
1245 IEEE80211_NODE_UNLOCK(nt);
1249 struct ieee80211_node *
1250 #ifdef IEEE80211_DEBUG_REFCNT
1251 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1252 const struct ieee80211vap *vap,
1253 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1255 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1256 const struct ieee80211vap *vap,
1257 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1260 struct ieee80211_node *ni;
1263 IEEE80211_NODE_LOCK_ASSERT(nt);
1265 hash = IEEE80211_NODE_HASH(macaddr);
1266 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1267 if (ni->ni_vap == vap &&
1268 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1269 ieee80211_ref_node(ni); /* mark referenced */
1270 #ifdef IEEE80211_DEBUG_REFCNT
1271 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1272 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1274 ni, ether_sprintf(ni->ni_macaddr),
1275 ieee80211_node_refcnt(ni));
1283 struct ieee80211_node *
1284 #ifdef IEEE80211_DEBUG_REFCNT
1285 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1286 const struct ieee80211vap *vap,
1287 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1289 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1290 const struct ieee80211vap *vap,
1291 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1294 struct ieee80211_node *ni;
1296 IEEE80211_NODE_LOCK(nt);
1297 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1298 IEEE80211_NODE_UNLOCK(nt);
1303 * Fake up a node; this handles node discovery in adhoc mode.
1304 * Note that for the driver's benefit we we treat this like
1305 * an association so the driver has an opportunity to setup
1306 * it's private state.
1308 struct ieee80211_node *
1309 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1310 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1312 struct ieee80211_node *ni;
1314 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1315 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1316 ni = ieee80211_dup_bss(vap, macaddr);
1318 struct ieee80211com *ic = vap->iv_ic;
1320 /* XXX no rate negotiation; just dup */
1321 ni->ni_rates = vap->iv_bss->ni_rates;
1322 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1324 * In adhoc demo mode there are no management
1325 * frames to use to discover neighbor capabilities,
1326 * so blindly propagate the local configuration
1327 * so we can do interesting things (e.g. use
1328 * WME to disable ACK's).
1330 if (vap->iv_flags & IEEE80211_F_WME)
1331 ni->ni_flags |= IEEE80211_NODE_QOS;
1332 if (vap->iv_flags & IEEE80211_F_FF)
1333 ni->ni_flags |= IEEE80211_NODE_FF;
1335 node_setuptxparms(ni);
1336 if (ic->ic_newassoc != NULL)
1337 ic->ic_newassoc(ni, 1);
1338 /* XXX not right for 802.1x/WPA */
1339 ieee80211_node_authorize(ni);
1345 ieee80211_init_neighbor(struct ieee80211_node *ni,
1346 const struct ieee80211_frame *wh,
1347 const struct ieee80211_scanparams *sp)
1349 ni->ni_esslen = sp->ssid[1];
1350 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1351 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1352 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1353 ni->ni_intval = sp->bintval;
1354 ni->ni_capinfo = sp->capinfo;
1355 ni->ni_chan = ni->ni_ic->ic_curchan;
1356 ni->ni_fhdwell = sp->fhdwell;
1357 ni->ni_fhindex = sp->fhindex;
1358 ni->ni_erp = sp->erp;
1359 ni->ni_timoff = sp->timoff;
1361 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1362 ieee80211_ies_expand(&ni->ni_ies);
1363 if (ni->ni_ies.wme_ie != NULL)
1364 ni->ni_flags |= IEEE80211_NODE_QOS;
1366 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1367 if (ni->ni_ies.ath_ie != NULL)
1368 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1371 /* NB: must be after ni_chan is setup */
1372 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1373 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1374 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1378 * Do node discovery in adhoc mode on receipt of a beacon
1379 * or probe response frame. Note that for the driver's
1380 * benefit we we treat this like an association so the
1381 * driver has an opportunity to setup it's private state.
1383 struct ieee80211_node *
1384 ieee80211_add_neighbor(struct ieee80211vap *vap,
1385 const struct ieee80211_frame *wh,
1386 const struct ieee80211_scanparams *sp)
1388 struct ieee80211_node *ni;
1390 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1391 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1392 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1394 struct ieee80211com *ic = vap->iv_ic;
1396 ieee80211_init_neighbor(ni, wh, sp);
1397 node_setuptxparms(ni);
1398 if (ic->ic_newassoc != NULL)
1399 ic->ic_newassoc(ni, 1);
1400 /* XXX not right for 802.1x/WPA */
1401 ieee80211_node_authorize(ni);
1406 #define IS_CTL(wh) \
1407 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1408 #define IS_PSPOLL(wh) \
1409 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1410 #define IS_BAR(wh) \
1411 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
1412 #define IS_PROBEREQ(wh) \
1413 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1414 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1415 #define IS_BCAST_PROBEREQ(wh) \
1416 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1417 ((const struct ieee80211_frame *)(wh))->i_addr3))
1419 static __inline struct ieee80211_node *
1420 _find_rxnode(struct ieee80211_node_table *nt,
1421 const struct ieee80211_frame_min *wh)
1423 /* XXX 4-address frames? */
1424 if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1425 return ieee80211_find_node_locked(nt, wh->i_addr1);
1426 if (IS_BCAST_PROBEREQ(wh))
1427 return NULL; /* spam bcast probe req to all vap's */
1428 return ieee80211_find_node_locked(nt, wh->i_addr2);
1432 * Locate the node for sender, track state, and then pass the
1433 * (referenced) node up to the 802.11 layer for its use. Note
1434 * we can return NULL if the sender is not in the table.
1436 struct ieee80211_node *
1437 #ifdef IEEE80211_DEBUG_REFCNT
1438 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1439 const struct ieee80211_frame_min *wh, const char *func, int line)
1441 ieee80211_find_rxnode(struct ieee80211com *ic,
1442 const struct ieee80211_frame_min *wh)
1445 struct ieee80211_node_table *nt;
1446 struct ieee80211_node *ni;
1449 IEEE80211_NODE_LOCK(nt);
1450 ni = _find_rxnode(nt, wh);
1451 IEEE80211_NODE_UNLOCK(nt);
1457 * Like ieee80211_find_rxnode but use the supplied h/w
1458 * key index as a hint to locate the node in the key
1459 * mapping table. If an entry is present at the key
1460 * index we return it; otherwise do a normal lookup and
1461 * update the mapping table if the station has a unicast
1462 * key assigned to it.
1464 struct ieee80211_node *
1465 #ifdef IEEE80211_DEBUG_REFCNT
1466 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1467 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1468 const char *func, int line)
1470 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1471 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1474 struct ieee80211_node_table *nt;
1475 struct ieee80211_node *ni;
1478 IEEE80211_NODE_LOCK(nt);
1479 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1480 ni = nt->nt_keyixmap[keyix];
1484 ni = _find_rxnode(nt, wh);
1485 if (ni != NULL && nt->nt_keyixmap != NULL) {
1487 * If the station has a unicast key cache slot
1488 * assigned update the key->node mapping table.
1490 keyix = ni->ni_ucastkey.wk_rxkeyix;
1491 /* XXX can keyixmap[keyix] != NULL? */
1492 if (keyix < nt->nt_keyixmax &&
1493 nt->nt_keyixmap[keyix] == NULL) {
1494 IEEE80211_DPRINTF(ni->ni_vap,
1496 "%s: add key map entry %p<%s> refcnt %d\n",
1497 __func__, ni, ether_sprintf(ni->ni_macaddr),
1498 ieee80211_node_refcnt(ni)+1);
1499 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1503 if (IS_BCAST_PROBEREQ(wh))
1504 ni = NULL; /* spam bcast probe req to all vap's */
1506 ieee80211_ref_node(ni);
1508 IEEE80211_NODE_UNLOCK(nt);
1512 #undef IS_BCAST_PROBEREQ
1519 * Return a reference to the appropriate node for sending
1520 * a data frame. This handles node discovery in adhoc networks.
1522 struct ieee80211_node *
1523 #ifdef IEEE80211_DEBUG_REFCNT
1524 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1525 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1526 const char *func, int line)
1528 ieee80211_find_txnode(struct ieee80211vap *vap,
1529 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1532 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1533 struct ieee80211_node *ni;
1536 * The destination address should be in the node table
1537 * unless this is a multicast/broadcast frame. We can
1538 * also optimize station mode operation, all frames go
1541 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1542 IEEE80211_NODE_LOCK(nt);
1543 if (vap->iv_opmode == IEEE80211_M_STA ||
1544 vap->iv_opmode == IEEE80211_M_WDS ||
1545 IEEE80211_IS_MULTICAST(macaddr))
1546 ni = ieee80211_ref_node(vap->iv_bss);
1548 ni = ieee80211_find_node_locked(nt, macaddr);
1549 IEEE80211_NODE_UNLOCK(nt);
1552 if (vap->iv_opmode == IEEE80211_M_IBSS ||
1553 vap->iv_opmode == IEEE80211_M_AHDEMO) {
1555 * In adhoc mode cons up a node for the destination.
1556 * Note that we need an additional reference for the
1557 * caller to be consistent with
1558 * ieee80211_find_node_locked.
1560 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1562 (void) ieee80211_ref_node(ni);
1564 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1565 "no node, discard frame (%s)", __func__);
1566 vap->iv_stats.is_tx_nonode++;
1573 _ieee80211_free_node(struct ieee80211_node *ni)
1575 struct ieee80211_node_table *nt = ni->ni_table;
1578 * NB: careful about referencing the vap as it may be
1579 * gone if the last reference was held by a driver.
1580 * We know the com will always be present so it's safe
1581 * to use ni_ic below to reclaim resources.
1584 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1585 "%s %p<%s> in %s table\n", __func__, ni,
1586 ether_sprintf(ni->ni_macaddr),
1587 nt != NULL ? nt->nt_name : "<gone>");
1589 if (ni->ni_associd != 0) {
1590 struct ieee80211vap *vap = ni->ni_vap;
1591 if (vap->iv_aid_bitmap != NULL)
1592 IEEE80211_AID_CLR(vap, ni->ni_associd);
1595 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1596 LIST_REMOVE(ni, ni_hash);
1598 ni->ni_ic->ic_node_free(ni);
1602 #ifdef IEEE80211_DEBUG_REFCNT
1603 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1605 ieee80211_free_node(struct ieee80211_node *ni)
1608 struct ieee80211_node_table *nt = ni->ni_table;
1610 #ifdef IEEE80211_DEBUG_REFCNT
1611 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1612 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1613 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1616 IEEE80211_NODE_LOCK(nt);
1617 if (ieee80211_node_dectestref(ni)) {
1619 * Last reference, reclaim state.
1621 _ieee80211_free_node(ni);
1622 } else if (ieee80211_node_refcnt(ni) == 1 &&
1623 nt->nt_keyixmap != NULL) {
1624 ieee80211_keyix keyix;
1626 * Check for a last reference in the key mapping table.
1628 keyix = ni->ni_ucastkey.wk_rxkeyix;
1629 if (keyix < nt->nt_keyixmax &&
1630 nt->nt_keyixmap[keyix] == ni) {
1631 IEEE80211_DPRINTF(ni->ni_vap,
1633 "%s: %p<%s> clear key map entry", __func__,
1634 ni, ether_sprintf(ni->ni_macaddr));
1635 nt->nt_keyixmap[keyix] = NULL;
1636 ieee80211_node_decref(ni); /* XXX needed? */
1637 _ieee80211_free_node(ni);
1640 IEEE80211_NODE_UNLOCK(nt);
1642 if (ieee80211_node_dectestref(ni))
1643 _ieee80211_free_node(ni);
1648 * Reclaim a unicast key and clear any key cache state.
1651 ieee80211_node_delucastkey(struct ieee80211_node *ni)
1653 struct ieee80211com *ic = ni->ni_ic;
1654 struct ieee80211_node_table *nt = &ic->ic_sta;
1655 struct ieee80211_node *nikey;
1656 ieee80211_keyix keyix;
1657 int isowned, status;
1660 * NB: We must beware of LOR here; deleting the key
1661 * can cause the crypto layer to block traffic updates
1662 * which can generate a LOR against the node table lock;
1663 * grab it here and stash the key index for our use below.
1665 * Must also beware of recursion on the node table lock.
1666 * When called from node_cleanup we may already have
1667 * the node table lock held. Unfortunately there's no
1668 * way to separate out this path so we must do this
1671 isowned = IEEE80211_NODE_IS_LOCKED(nt);
1673 IEEE80211_NODE_LOCK(nt);
1675 status = 1; /* NB: success */
1676 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1677 keyix = ni->ni_ucastkey.wk_rxkeyix;
1678 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1679 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1680 nikey = nt->nt_keyixmap[keyix];
1681 nt->nt_keyixmap[keyix] = NULL;;
1685 IEEE80211_NODE_UNLOCK(nt);
1687 if (nikey != NULL) {
1688 KASSERT(nikey == ni,
1689 ("key map out of sync, ni %p nikey %p", ni, nikey));
1690 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1691 "%s: delete key map entry %p<%s> refcnt %d\n",
1692 __func__, ni, ether_sprintf(ni->ni_macaddr),
1693 ieee80211_node_refcnt(ni)-1);
1694 ieee80211_free_node(ni);
1700 * Reclaim a node. If this is the last reference count then
1701 * do the normal free work. Otherwise remove it from the node
1702 * table and mark it gone by clearing the back-reference.
1705 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1707 ieee80211_keyix keyix;
1709 IEEE80211_NODE_LOCK_ASSERT(nt);
1711 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1712 "%s: remove %p<%s> from %s table, refcnt %d\n",
1713 __func__, ni, ether_sprintf(ni->ni_macaddr),
1714 nt->nt_name, ieee80211_node_refcnt(ni)-1);
1716 * Clear any entry in the unicast key mapping table.
1717 * We need to do it here so rx lookups don't find it
1718 * in the mapping table even if it's not in the hash
1719 * table. We cannot depend on the mapping table entry
1720 * being cleared because the node may not be free'd.
1722 keyix = ni->ni_ucastkey.wk_rxkeyix;
1723 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1724 nt->nt_keyixmap[keyix] == ni) {
1725 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1726 "%s: %p<%s> clear key map entry %u\n",
1727 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1728 nt->nt_keyixmap[keyix] = NULL;
1729 ieee80211_node_decref(ni); /* NB: don't need free */
1731 if (!ieee80211_node_dectestref(ni)) {
1733 * Other references are present, just remove the
1734 * node from the table so it cannot be found. When
1735 * the references are dropped storage will be
1738 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1739 LIST_REMOVE(ni, ni_hash);
1740 ni->ni_table = NULL; /* clear reference */
1742 _ieee80211_free_node(ni);
1746 * Node table support.
1750 ieee80211_node_table_init(struct ieee80211com *ic,
1751 struct ieee80211_node_table *nt,
1752 const char *name, int inact, int keyixmax)
1754 struct ifnet *ifp = ic->ic_ifp;
1757 IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1758 IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1759 TAILQ_INIT(&nt->nt_node);
1762 nt->nt_inact_init = inact;
1763 nt->nt_keyixmax = keyixmax;
1764 if (nt->nt_keyixmax > 0) {
1765 nt->nt_keyixmap = (struct ieee80211_node **) malloc(
1766 keyixmax * sizeof(struct ieee80211_node *),
1767 M_80211_NODE, M_NOWAIT | M_ZERO);
1768 if (nt->nt_keyixmap == NULL)
1769 if_printf(ic->ic_ifp,
1770 "Cannot allocate key index map with %u entries\n",
1773 nt->nt_keyixmap = NULL;
1777 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
1778 struct ieee80211vap *match)
1780 struct ieee80211_node *ni, *next;
1782 IEEE80211_NODE_LOCK(nt);
1783 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1784 if (match != NULL && ni->ni_vap != match)
1786 /* XXX can this happen? if so need's work */
1787 if (ni->ni_associd != 0) {
1788 struct ieee80211vap *vap = ni->ni_vap;
1790 if (vap->iv_auth->ia_node_leave != NULL)
1791 vap->iv_auth->ia_node_leave(ni);
1792 if (vap->iv_aid_bitmap != NULL)
1793 IEEE80211_AID_CLR(vap, ni->ni_associd);
1795 ni->ni_wdsvap = NULL; /* clear reference */
1796 node_reclaim(nt, ni);
1798 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1800 * Make a separate pass to clear references to this vap
1801 * held by DWDS entries. They will not be matched above
1802 * because ni_vap will point to the ap vap but we still
1803 * need to clear ni_wdsvap when the WDS vap is destroyed
1806 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1807 if (ni->ni_wdsvap == match)
1808 ni->ni_wdsvap = NULL;
1810 IEEE80211_NODE_UNLOCK(nt);
1814 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1816 ieee80211_node_table_reset(nt, NULL);
1817 if (nt->nt_keyixmap != NULL) {
1819 /* XXX verify all entries are NULL */
1821 for (i = 0; i < nt->nt_keyixmax; i++)
1822 if (nt->nt_keyixmap[i] != NULL)
1823 printf("%s: %s[%u] still active\n", __func__,
1826 free(nt->nt_keyixmap, M_80211_NODE);
1827 nt->nt_keyixmap = NULL;
1829 IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1830 IEEE80211_NODE_LOCK_DESTROY(nt);
1834 * Timeout inactive stations and do related housekeeping.
1835 * Note that we cannot hold the node lock while sending a
1836 * frame as this would lead to a LOR. Instead we use a
1837 * generation number to mark nodes that we've scanned and
1838 * drop the lock and restart a scan if we have to time out
1839 * a node. Since we are single-threaded by virtue of
1840 * controlling the inactivity timer we can be sure this will
1841 * process each node only once.
1844 ieee80211_timeout_stations(struct ieee80211com *ic)
1846 struct ieee80211_node_table *nt = &ic->ic_sta;
1847 struct ieee80211vap *vap;
1848 struct ieee80211_node *ni;
1851 IEEE80211_NODE_ITERATE_LOCK(nt);
1852 gen = ++nt->nt_scangen;
1854 IEEE80211_NODE_LOCK(nt);
1855 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1856 if (ni->ni_scangen == gen) /* previously handled */
1858 ni->ni_scangen = gen;
1860 * Ignore entries for which have yet to receive an
1861 * authentication frame. These are transient and
1862 * will be reclaimed when the last reference to them
1863 * goes away (when frame xmits complete).
1867 * Only process stations when in RUN state. This
1868 * insures, for example, that we don't timeout an
1869 * inactive station during CAC. Note that CSA state
1870 * is actually handled in ieee80211_node_timeout as
1871 * it applies to more than timeout processing.
1873 if (vap->iv_state != IEEE80211_S_RUN)
1875 /* XXX can vap be NULL? */
1876 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
1877 vap->iv_opmode == IEEE80211_M_STA) &&
1878 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1881 * Free fragment if not needed anymore
1882 * (last fragment older than 1s).
1883 * XXX doesn't belong here, move to node_age
1885 if (ni->ni_rxfrag[0] != NULL &&
1886 ticks > ni->ni_rxfragstamp + hz) {
1887 m_freem(ni->ni_rxfrag[0]);
1888 ni->ni_rxfrag[0] = NULL;
1890 if (ni->ni_inact > 0) {
1892 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1893 "%s: inact %u inact_reload %u nrates %u",
1894 __func__, ni->ni_inact, ni->ni_inact_reload,
1895 ni->ni_rates.rs_nrates);
1898 * Special case ourself; we may be idle for extended periods
1899 * of time and regardless reclaiming our state is wrong.
1900 * XXX run ic_node_age
1902 if (ni == vap->iv_bss)
1904 if (ni->ni_associd != 0 ||
1905 (vap->iv_opmode == IEEE80211_M_IBSS ||
1906 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
1908 * Age/drain resources held by the station.
1910 ic->ic_node_age(ni);
1912 * Probe the station before time it out. We
1913 * send a null data frame which may not be
1914 * universally supported by drivers (need it
1915 * for ps-poll support so it should be...).
1917 * XXX don't probe the station unless we've
1918 * received a frame from them (and have
1919 * some idea of the rates they are capable
1920 * of); this will get fixed more properly
1921 * soon with better handling of the rate set.
1923 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1924 (0 < ni->ni_inact &&
1925 ni->ni_inact <= vap->iv_inact_probe) &&
1926 ni->ni_rates.rs_nrates != 0) {
1928 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1930 "probe station due to inactivity");
1932 * Grab a reference before unlocking the table
1933 * so the node cannot be reclaimed before we
1934 * send the frame. ieee80211_send_nulldata
1935 * understands we've done this and reclaims the
1936 * ref for us as needed.
1938 ieee80211_ref_node(ni);
1939 IEEE80211_NODE_UNLOCK(nt);
1940 ieee80211_send_nulldata(ni);
1945 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1946 ni->ni_inact <= 0) {
1948 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1949 "station timed out due to inactivity "
1950 "(refcnt %u)", ieee80211_node_refcnt(ni));
1952 * Send a deauthenticate frame and drop the station.
1953 * This is somewhat complicated due to reference counts
1954 * and locking. At this point a station will typically
1955 * have a reference count of 1. ieee80211_node_leave
1956 * will do a "free" of the node which will drop the
1957 * reference count. But in the meantime a reference
1958 * wil be held by the deauth frame. The actual reclaim
1959 * of the node will happen either after the tx is
1960 * completed or by ieee80211_node_leave.
1962 * Separately we must drop the node lock before sending
1963 * in case the driver takes a lock, as this can result
1964 * in a LOR between the node lock and the driver lock.
1966 ieee80211_ref_node(ni);
1967 IEEE80211_NODE_UNLOCK(nt);
1968 if (ni->ni_associd != 0) {
1969 IEEE80211_SEND_MGMT(ni,
1970 IEEE80211_FC0_SUBTYPE_DEAUTH,
1971 IEEE80211_REASON_AUTH_EXPIRE);
1973 ieee80211_node_leave(ni);
1974 ieee80211_free_node(ni);
1975 vap->iv_stats.is_node_timeout++;
1979 IEEE80211_NODE_UNLOCK(nt);
1981 IEEE80211_NODE_ITERATE_UNLOCK(nt);
1985 * Aggressively reclaim resources. This should be used
1986 * only in a critical situation to reclaim mbuf resources.
1989 ieee80211_drain(struct ieee80211com *ic)
1991 struct ieee80211_node_table *nt = &ic->ic_sta;
1992 struct ieee80211vap *vap;
1993 struct ieee80211_node *ni;
1995 IEEE80211_NODE_LOCK(nt);
1996 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1998 * Ignore entries for which have yet to receive an
1999 * authentication frame. These are transient and
2000 * will be reclaimed when the last reference to them
2001 * goes away (when frame xmits complete).
2005 * Only process stations when in RUN state. This
2006 * insures, for example, that we don't timeout an
2007 * inactive station during CAC. Note that CSA state
2008 * is actually handled in ieee80211_node_timeout as
2009 * it applies to more than timeout processing.
2011 if (vap->iv_state != IEEE80211_S_RUN)
2013 /* XXX can vap be NULL? */
2014 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2015 vap->iv_opmode == IEEE80211_M_STA) &&
2016 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2020 * XXX doesn't belong here, move to node_drain
2022 if (ni->ni_rxfrag[0] != NULL) {
2023 m_freem(ni->ni_rxfrag[0]);
2024 ni->ni_rxfrag[0] = NULL;
2027 * Drain resources held by the station.
2029 ic->ic_node_drain(ni);
2031 IEEE80211_NODE_UNLOCK(nt);
2035 * Per-ieee80211com inactivity timer callback.
2038 ieee80211_node_timeout(void *arg)
2040 struct ieee80211com *ic = arg;
2043 * Defer timeout processing if a channel switch is pending.
2044 * We typically need to be mute so not doing things that
2045 * might generate frames is good to handle in one place.
2046 * Supressing the station timeout processing may extend the
2047 * lifetime of inactive stations (by not decrementing their
2048 * idle counters) but this should be ok unless the CSA is
2049 * active for an unusually long time.
2051 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2052 ieee80211_scan_timeout(ic);
2053 ieee80211_timeout_stations(ic);
2056 ieee80211_erp_timeout(ic);
2057 ieee80211_ht_timeout(ic);
2058 IEEE80211_UNLOCK(ic);
2060 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2061 ieee80211_node_timeout, ic);
2065 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2066 ieee80211_iter_func *f, void *arg)
2068 struct ieee80211_node *ni;
2071 IEEE80211_NODE_ITERATE_LOCK(nt);
2072 gen = ++nt->nt_scangen;
2074 IEEE80211_NODE_LOCK(nt);
2075 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2076 if (ni->ni_scangen != gen) {
2077 ni->ni_scangen = gen;
2078 (void) ieee80211_ref_node(ni);
2079 IEEE80211_NODE_UNLOCK(nt);
2081 ieee80211_free_node(ni);
2085 IEEE80211_NODE_UNLOCK(nt);
2087 IEEE80211_NODE_ITERATE_UNLOCK(nt);
2091 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2093 printf("0x%p: mac %s refcnt %d\n", ni,
2094 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2095 printf("\tscangen %u authmode %u flags 0x%x\n",
2096 ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2097 printf("\tassocid 0x%x txpower %u vlan %u\n",
2098 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2099 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2100 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2101 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2102 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2103 ni->ni_rxfragstamp);
2104 printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
2105 ni->ni_rstamp, node_getrssi(ni), ni->ni_noise,
2106 ni->ni_intval, ni->ni_capinfo);
2107 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2108 ether_sprintf(ni->ni_bssid),
2109 ni->ni_esslen, ni->ni_essid,
2110 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2111 printf("\tinact %u inact_reload %u txrate %u\n",
2112 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2113 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2114 ni->ni_htcap, ni->ni_htparam,
2115 ni->ni_htctlchan, ni->ni_ht2ndchan);
2116 printf("\thtopmode %x htstbc %x chw %u\n",
2117 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2121 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2123 ieee80211_iterate_nodes(nt,
2124 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2128 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2130 struct ieee80211vap *vap;
2132 IEEE80211_LOCK_ASSERT(ic);
2134 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2135 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2136 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2140 ieee80211_notify_erp(struct ieee80211com *ic)
2143 ieee80211_notify_erp_locked(ic);
2144 IEEE80211_UNLOCK(ic);
2148 * Handle a station joining an 11g network.
2151 ieee80211_node_join_11g(struct ieee80211_node *ni)
2153 struct ieee80211com *ic = ni->ni_ic;
2155 IEEE80211_LOCK_ASSERT(ic);
2158 * Station isn't capable of short slot time. Bump
2159 * the count of long slot time stations and disable
2160 * use of short slot time. Note that the actual switch
2161 * over to long slot time use may not occur until the
2162 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2164 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2165 ic->ic_longslotsta++;
2166 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2167 "station needs long slot time, count %d",
2168 ic->ic_longslotsta);
2169 /* XXX vap's w/ conflicting needs won't work */
2170 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2172 * Don't force slot time when switched to turbo
2173 * mode as non-ERP stations won't be present; this
2174 * need only be done when on the normal G channel.
2176 ieee80211_set_shortslottime(ic, 0);
2180 * If the new station is not an ERP station
2181 * then bump the counter and enable protection
2184 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2186 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2187 "station is !ERP, %d non-ERP stations associated",
2190 * If station does not support short preamble
2191 * then we must enable use of Barker preamble.
2193 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2194 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2195 "%s", "station needs long preamble");
2196 ic->ic_flags |= IEEE80211_F_USEBARKER;
2197 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2200 * If protection is configured and this is the first
2201 * indication we should use protection, enable it.
2203 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2204 ic->ic_nonerpsta == 1 &&
2205 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2206 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2207 "%s: enable use of protection\n", __func__);
2208 ic->ic_flags |= IEEE80211_F_USEPROT;
2209 ieee80211_notify_erp_locked(ic);
2212 ni->ni_flags |= IEEE80211_NODE_ERP;
2216 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2218 struct ieee80211com *ic = ni->ni_ic;
2219 struct ieee80211vap *vap = ni->ni_vap;
2222 if (ni->ni_associd == 0) {
2225 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2227 * It would be good to search the bitmap
2228 * more efficiently, but this will do for now.
2230 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2231 if (!IEEE80211_AID_ISSET(vap, aid))
2234 if (aid >= vap->iv_max_aid) {
2235 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2236 ieee80211_node_leave(ni);
2239 ni->ni_associd = aid | 0xc000;
2240 ni->ni_jointime = time_uptime;
2242 IEEE80211_AID_SET(vap, ni->ni_associd);
2243 vap->iv_sta_assoc++;
2246 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2247 ieee80211_ht_node_join(ni);
2248 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2249 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2250 ieee80211_node_join_11g(ni);
2251 IEEE80211_UNLOCK(ic);
2257 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2258 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2259 IEEE80211_NODE_AID(ni),
2260 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2261 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2262 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2263 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2264 ni->ni_flags & IEEE80211_NODE_HT ?
2265 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2266 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2267 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2268 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2269 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2270 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2271 ", fast-frames" : "",
2272 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2276 node_setuptxparms(ni);
2277 /* give driver a chance to setup state like ni_txrate */
2278 if (ic->ic_newassoc != NULL)
2279 ic->ic_newassoc(ni, newassoc);
2280 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2281 /* tell the authenticator about new station */
2282 if (vap->iv_auth->ia_node_join != NULL)
2283 vap->iv_auth->ia_node_join(ni);
2284 ieee80211_notify_node_join(ni,
2285 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2289 disable_protection(struct ieee80211com *ic)
2291 KASSERT(ic->ic_nonerpsta == 0 &&
2292 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2293 ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2296 ic->ic_flags &= ~IEEE80211_F_USEPROT;
2297 /* XXX verify mode? */
2298 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2299 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2300 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2302 ieee80211_notify_erp_locked(ic);
2306 * Handle a station leaving an 11g network.
2309 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2311 struct ieee80211com *ic = ni->ni_ic;
2313 IEEE80211_LOCK_ASSERT(ic);
2315 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2316 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2317 ic->ic_bsschan->ic_flags));
2320 * If a long slot station do the slot time bookkeeping.
2322 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2323 KASSERT(ic->ic_longslotsta > 0,
2324 ("bogus long slot station count %d", ic->ic_longslotsta));
2325 ic->ic_longslotsta--;
2326 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2327 "long slot time station leaves, count now %d",
2328 ic->ic_longslotsta);
2329 if (ic->ic_longslotsta == 0) {
2331 * Re-enable use of short slot time if supported
2332 * and not operating in IBSS mode (per spec).
2334 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2335 ic->ic_opmode != IEEE80211_M_IBSS) {
2336 IEEE80211_DPRINTF(ni->ni_vap,
2337 IEEE80211_MSG_ASSOC,
2338 "%s: re-enable use of short slot time\n",
2340 ieee80211_set_shortslottime(ic, 1);
2345 * If a non-ERP station do the protection-related bookkeeping.
2347 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2348 KASSERT(ic->ic_nonerpsta > 0,
2349 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2351 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2352 "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2353 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2354 " (non-ERP sta present)" : "");
2355 if (ic->ic_nonerpsta == 0 &&
2356 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2357 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2358 "%s: disable use of protection\n", __func__);
2359 disable_protection(ic);
2365 * Time out presence of an overlapping bss with non-ERP
2366 * stations. When operating in hostap mode we listen for
2367 * beacons from other stations and if we identify a non-ERP
2368 * station is present we enable protection. To identify
2369 * when all non-ERP stations are gone we time out this
2373 ieee80211_erp_timeout(struct ieee80211com *ic)
2376 IEEE80211_LOCK_ASSERT(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);
2423 IEEE80211_AID_CLR(vap, ni->ni_associd);
2425 vap->iv_sta_assoc--;
2428 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2429 ieee80211_ht_node_leave(ni);
2430 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2431 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2432 ieee80211_node_leave_11g(ni);
2433 IEEE80211_UNLOCK(ic);
2435 * Cleanup station state. In particular clear various
2436 * state that might otherwise be reused if the node
2437 * is reused before the reference count goes to zero
2438 * (and memory is reclaimed).
2440 ieee80211_sta_leave(ni);
2443 * Remove the node from any table it's recorded in and
2444 * drop the caller's reference. Removal from the table
2445 * is important to insure the node is not reprocessed
2449 IEEE80211_NODE_LOCK(nt);
2450 node_reclaim(nt, ni);
2451 IEEE80211_NODE_UNLOCK(nt);
2453 ieee80211_free_node(ni);
2457 struct ieee80211vap *vap;
2459 uint32_t rssi_total;
2463 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2465 struct rssiinfo *info = arg;
2466 struct ieee80211vap *vap = ni->ni_vap;
2469 if (info->vap != vap)
2471 /* only associated stations */
2472 if (ni->ni_associd == 0)
2474 rssi = vap->iv_ic->ic_node_getrssi(ni);
2476 info->rssi_samples++;
2477 info->rssi_total += rssi;
2482 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2484 struct rssiinfo *info = arg;
2485 struct ieee80211vap *vap = ni->ni_vap;
2488 if (info->vap != vap)
2490 /* only neighbors */
2491 /* XXX check bssid */
2492 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2494 rssi = vap->iv_ic->ic_node_getrssi(ni);
2496 info->rssi_samples++;
2497 info->rssi_total += rssi;
2502 ieee80211_getrssi(struct ieee80211vap *vap)
2504 #define NZ(x) ((x) == 0 ? 1 : (x))
2505 struct ieee80211com *ic = vap->iv_ic;
2506 struct rssiinfo info;
2508 info.rssi_total = 0;
2509 info.rssi_samples = 0;
2511 switch (vap->iv_opmode) {
2512 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
2513 case IEEE80211_M_AHDEMO: /* average of all neighbors */
2514 ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2516 case IEEE80211_M_HOSTAP: /* average of all associated stations */
2517 ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2519 case IEEE80211_M_MONITOR: /* XXX */
2520 case IEEE80211_M_STA: /* use stats from associated ap */
2522 if (vap->iv_bss != NULL)
2523 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2524 info.rssi_samples = 1;
2527 return info.rssi_total / NZ(info.rssi_samples);
2532 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2535 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
2537 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2538 /* for non-station mode return avg'd rssi accounting */
2539 if (vap->iv_opmode != IEEE80211_M_STA)
2540 *rssi = ieee80211_getrssi(vap);