usr.sbin/makefs/ffs: Remove m_buf::b_is_hammer2
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_node.c
blob954bc5ad1247defed8ad8198ea18e43529ce5a09
1 /*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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$");
30 #include "opt_wlan.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/mbuf.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
38 #include <sys/socket.h>
40 #include <net/if.h>
41 #include <net/if_var.h>
42 #include <net/if_media.h>
43 #include <net/ethernet.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>
49 #endif
50 #ifdef IEEE80211_SUPPORT_TDMA
51 #include <netproto/802_11/ieee80211_tdma.h>
52 #endif
53 #include <netproto/802_11/ieee80211_wds.h>
54 #include <netproto/802_11/ieee80211_mesh.h>
55 #include <netproto/802_11/ieee80211_ratectl.h>
57 #include <net/bpf.h>
60 * IEEE80211_NODE_HASHSIZE must be a power of 2.
62 CTASSERT(powerof2(IEEE80211_NODE_HASHSIZE));
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
78 #else
79 #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
80 #endif
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 *);
94 static void _ieee80211_free_node(struct ieee80211_node *);
96 static void node_reclaim(struct ieee80211_node_table *nt,
97 struct ieee80211_node *ni);
98 static void ieee80211_node_table_init(struct ieee80211com *ic,
99 struct ieee80211_node_table *nt, const char *name,
100 int inact, int keymaxix);
101 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
102 struct ieee80211vap *);
103 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
104 static void ieee80211_erp_timeout(struct ieee80211com *);
106 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
107 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
109 void
110 ieee80211_node_attach(struct ieee80211com *ic)
112 /* XXX really want maxlen enforced per-sta */
113 ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
114 "802.11 staging q");
115 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
116 IEEE80211_INACT_INIT, ic->ic_max_keyix);
117 #if defined(__DragonFly__)
118 callout_init_mp(&ic->ic_inact);
119 #else
120 callout_init(&ic->ic_inact, 1);
121 #endif
122 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
123 ieee80211_node_timeout, ic);
125 ic->ic_node_alloc = node_alloc;
126 ic->ic_node_free = node_free;
127 ic->ic_node_cleanup = node_cleanup;
128 ic->ic_node_age = node_age;
129 ic->ic_node_drain = node_age; /* NB: same as age */
130 ic->ic_node_getrssi = node_getrssi;
131 ic->ic_node_getsignal = node_getsignal;
132 ic->ic_node_getmimoinfo = node_getmimoinfo;
135 * Set flags to be propagated to all vap's;
136 * these define default behaviour/configuration.
138 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
141 void
142 ieee80211_node_detach(struct ieee80211com *ic)
145 callout_drain(&ic->ic_inact);
146 ieee80211_node_table_cleanup(&ic->ic_sta);
147 ieee80211_ageq_cleanup(&ic->ic_stageq);
150 void
151 ieee80211_node_vattach(struct ieee80211vap *vap)
153 /* NB: driver can override */
154 vap->iv_max_aid = IEEE80211_AID_DEF;
156 /* default station inactivity timer setings */
157 vap->iv_inact_init = IEEE80211_INACT_INIT;
158 vap->iv_inact_auth = IEEE80211_INACT_AUTH;
159 vap->iv_inact_run = IEEE80211_INACT_RUN;
160 vap->iv_inact_probe = IEEE80211_INACT_PROBE;
162 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
163 "%s: init %u auth %u run %u probe %u\n", __func__,
164 vap->iv_inact_init, vap->iv_inact_auth,
165 vap->iv_inact_run, vap->iv_inact_probe);
168 void
169 ieee80211_node_latevattach(struct ieee80211vap *vap)
171 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
172 /* XXX should we allow max aid to be zero? */
173 if (vap->iv_max_aid < IEEE80211_AID_MIN) {
174 vap->iv_max_aid = IEEE80211_AID_MIN;
175 if_printf(vap->iv_ifp,
176 "WARNING: max aid too small, changed to %d\n",
177 vap->iv_max_aid);
179 #if defined(__DragonFly__)
180 vap->iv_aid_bitmap = (uint32_t *) kmalloc(
181 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
182 M_80211_NODE, M_INTWAIT | M_ZERO);
183 #else
184 vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC(
185 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
186 M_80211_NODE,
187 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
188 #endif
189 if (vap->iv_aid_bitmap == NULL) {
190 /* XXX no way to recover */
191 kprintf("%s: no memory for AID bitmap, max aid %d!\n",
192 __func__, vap->iv_max_aid);
193 vap->iv_max_aid = 0;
197 ieee80211_reset_bss(vap);
199 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
202 void
203 ieee80211_node_vdetach(struct ieee80211vap *vap)
205 struct ieee80211com *ic = vap->iv_ic;
207 ieee80211_node_table_reset(&ic->ic_sta, vap);
208 if (vap->iv_bss != NULL) {
209 ieee80211_free_node(vap->iv_bss);
210 vap->iv_bss = NULL;
212 if (vap->iv_aid_bitmap != NULL) {
213 IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE);
214 vap->iv_aid_bitmap = NULL;
219 * Port authorize/unauthorize interfaces for use by an authenticator.
222 void
223 ieee80211_node_authorize(struct ieee80211_node *ni)
225 struct ieee80211vap *vap = ni->ni_vap;
227 ni->ni_flags |= IEEE80211_NODE_AUTH;
228 ni->ni_inact_reload = vap->iv_inact_run;
229 ni->ni_inact = ni->ni_inact_reload;
231 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
232 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
235 void
236 ieee80211_node_unauthorize(struct ieee80211_node *ni)
238 struct ieee80211vap *vap = ni->ni_vap;
240 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
241 ni->ni_inact_reload = vap->iv_inact_auth;
242 if (ni->ni_inact > ni->ni_inact_reload)
243 ni->ni_inact = ni->ni_inact_reload;
245 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
246 "%s: inact_reload %u inact %u", __func__,
247 ni->ni_inact_reload, ni->ni_inact);
251 * Fix tx parameters for a node according to ``association state''.
253 void
254 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
256 struct ieee80211vap *vap = ni->ni_vap;
257 enum ieee80211_phymode mode;
259 if (ni->ni_flags & IEEE80211_NODE_HT) {
260 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
261 mode = IEEE80211_MODE_11NA;
262 else
263 mode = IEEE80211_MODE_11NG;
264 } else { /* legacy rate handling */
265 if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
266 mode = IEEE80211_MODE_STURBO_A;
267 else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
268 mode = IEEE80211_MODE_HALF;
269 else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
270 mode = IEEE80211_MODE_QUARTER;
271 /* NB: 108A should be handled as 11a */
272 else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
273 mode = IEEE80211_MODE_11A;
274 else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
275 (ni->ni_flags & IEEE80211_NODE_ERP))
276 mode = IEEE80211_MODE_11G;
277 else
278 mode = IEEE80211_MODE_11B;
280 ni->ni_txparms = &vap->iv_txparms[mode];
284 * Set/change the channel. The rate set is also updated as
285 * to insure a consistent view by drivers.
286 * XXX should be private but hostap needs it to deal with CSA
288 void
289 ieee80211_node_set_chan(struct ieee80211_node *ni,
290 struct ieee80211_channel *chan)
292 struct ieee80211com *ic = ni->ni_ic;
293 struct ieee80211vap *vap = ni->ni_vap;
294 enum ieee80211_phymode mode;
296 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
298 ni->ni_chan = chan;
299 mode = ieee80211_chan2mode(chan);
300 if (IEEE80211_IS_CHAN_HT(chan)) {
302 * We must install the legacy rate est in ni_rates and the
303 * HT rate set in ni_htrates.
305 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
307 * Setup bss tx parameters based on operating mode. We
308 * use legacy rates when operating in a mixed HT+non-HT bss
309 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
311 if (mode == IEEE80211_MODE_11NA &&
312 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
313 mode = IEEE80211_MODE_11A;
314 else if (mode == IEEE80211_MODE_11NG &&
315 (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
316 mode = IEEE80211_MODE_11G;
317 if (mode == IEEE80211_MODE_11G &&
318 (vap->iv_flags & IEEE80211_F_PUREG) == 0)
319 mode = IEEE80211_MODE_11B;
321 ni->ni_txparms = &vap->iv_txparms[mode];
322 ni->ni_rates = *ieee80211_get_suprates(ic, chan);
325 static __inline void
326 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
328 /* propagate useful state */
329 nbss->ni_authmode = obss->ni_authmode;
330 nbss->ni_txpower = obss->ni_txpower;
331 nbss->ni_vlan = obss->ni_vlan;
332 /* XXX statistics? */
333 /* XXX legacy WDS bssid? */
336 void
337 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
339 struct ieee80211com *ic = vap->iv_ic;
340 struct ieee80211_node *ni;
342 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
343 "%s: creating %s on channel %u%c\n", __func__,
344 ieee80211_opmode_name[vap->iv_opmode],
345 ieee80211_chan2ieee(ic, chan),
346 ieee80211_channel_type_char(chan));
348 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
349 if (ni == NULL) {
350 /* XXX recovery? */
351 return;
353 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
354 ni->ni_esslen = vap->iv_des_ssid[0].len;
355 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
356 if (vap->iv_bss != NULL)
357 copy_bss(ni, vap->iv_bss);
358 ni->ni_intval = ic->ic_bintval;
359 if (vap->iv_flags & IEEE80211_F_PRIVACY)
360 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
361 if (ic->ic_phytype == IEEE80211_T_FH) {
362 ni->ni_fhdwell = 200; /* XXX */
363 ni->ni_fhindex = 1;
365 if (vap->iv_opmode == IEEE80211_M_IBSS) {
366 vap->iv_flags |= IEEE80211_F_SIBSS;
367 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
368 if (vap->iv_flags & IEEE80211_F_DESBSSID)
369 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
370 else {
371 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
372 /* clear group bit, add local bit */
373 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
375 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
376 if (vap->iv_flags & IEEE80211_F_DESBSSID)
377 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
378 else
379 #ifdef IEEE80211_SUPPORT_TDMA
380 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
381 #endif
382 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
383 #ifdef IEEE80211_SUPPORT_MESH
384 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
385 ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
386 memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
387 #endif
390 * Fix the channel and related attributes.
392 /* clear DFS CAC state on previous channel */
393 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
394 ic->ic_bsschan->ic_freq != chan->ic_freq &&
395 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
396 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
397 ic->ic_bsschan = chan;
398 ieee80211_node_set_chan(ni, chan);
399 ic->ic_curmode = ieee80211_chan2mode(chan);
401 * Do mode-specific setup.
403 if (IEEE80211_IS_CHAN_FULL(chan)) {
404 if (IEEE80211_IS_CHAN_ANYG(chan)) {
406 * Use a mixed 11b/11g basic rate set.
408 ieee80211_setbasicrates(&ni->ni_rates,
409 IEEE80211_MODE_11G);
410 if (vap->iv_flags & IEEE80211_F_PUREG) {
412 * Also mark OFDM rates basic so 11b
413 * stations do not join (WiFi compliance).
415 ieee80211_addbasicrates(&ni->ni_rates,
416 IEEE80211_MODE_11A);
418 } else if (IEEE80211_IS_CHAN_B(chan)) {
420 * Force pure 11b rate set.
422 ieee80211_setbasicrates(&ni->ni_rates,
423 IEEE80211_MODE_11B);
427 (void) ieee80211_sta_join1(ieee80211_ref_node(ni));
431 * Reset bss state on transition to the INIT state.
432 * Clear any stations from the table (they have been
433 * deauth'd) and reset the bss node (clears key, rate
434 * etc. state).
436 void
437 ieee80211_reset_bss(struct ieee80211vap *vap)
439 struct ieee80211com *ic = vap->iv_ic;
440 struct ieee80211_node *ni, *obss;
442 ieee80211_node_table_reset(&ic->ic_sta, vap);
443 /* XXX multi-bss: wrong */
444 ieee80211_reset_erp(ic);
446 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
447 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
448 obss = vap->iv_bss;
449 vap->iv_bss = ieee80211_ref_node(ni);
450 if (obss != NULL) {
451 copy_bss(ni, obss);
452 ni->ni_intval = ic->ic_bintval;
453 ieee80211_free_node(obss);
454 } else
455 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
458 static int
459 match_ssid(const struct ieee80211_node *ni,
460 int nssid, const struct ieee80211_scan_ssid ssids[])
462 int i;
464 for (i = 0; i < nssid; i++) {
465 if (ni->ni_esslen == ssids[i].len &&
466 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
467 return 1;
469 return 0;
473 * Test a node for suitability/compatibility.
475 static int
476 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
478 struct ieee80211com *ic = ni->ni_ic;
479 uint8_t rate;
481 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
482 return 0;
483 if (vap->iv_opmode == IEEE80211_M_IBSS) {
484 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
485 return 0;
486 } else {
487 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
488 return 0;
490 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
491 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
492 return 0;
493 } else {
494 /* XXX does this mean privacy is supported or required? */
495 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
496 return 0;
498 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
499 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
500 if (rate & IEEE80211_RATE_BASIC)
501 return 0;
502 if (vap->iv_des_nssid != 0 &&
503 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
504 return 0;
505 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
506 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
507 return 0;
508 return 1;
511 #ifdef IEEE80211_DEBUG
513 * Display node suitability/compatibility.
515 static void
516 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
518 struct ieee80211com *ic = ni->ni_ic;
519 uint8_t rate;
520 int fail;
522 fail = 0;
523 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
524 fail |= 0x01;
525 if (vap->iv_opmode == IEEE80211_M_IBSS) {
526 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
527 fail |= 0x02;
528 } else {
529 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
530 fail |= 0x02;
532 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
533 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
534 fail |= 0x04;
535 } else {
536 /* XXX does this mean privacy is supported or required? */
537 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
538 fail |= 0x04;
540 rate = ieee80211_fix_rate(ni, &ni->ni_rates,
541 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
542 if (rate & IEEE80211_RATE_BASIC)
543 fail |= 0x08;
544 if (vap->iv_des_nssid != 0 &&
545 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
546 fail |= 0x10;
547 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
548 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
549 fail |= 0x20;
551 kprintf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
552 kprintf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
553 kprintf(" %3d%c",
554 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
555 kprintf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
556 fail & 0x08 ? '!' : ' ');
557 kprintf(" %4s%c",
558 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
559 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
560 "????",
561 fail & 0x02 ? '!' : ' ');
562 kprintf(" %3s%c ",
563 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
564 fail & 0x04 ? '!' : ' ');
565 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
566 kprintf("%s\n", fail & 0x10 ? "!" : "");
568 #endif /* IEEE80211_DEBUG */
572 ieee80211_ibss_merge_check(struct ieee80211_node *ni)
574 struct ieee80211vap *vap = ni->ni_vap;
576 if (ni == vap->iv_bss ||
577 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
578 /* unchanged, nothing to do */
579 return 0;
582 if (!check_bss(vap, ni)) {
583 /* capabilities mismatch */
584 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
585 "%s: merge failed, capabilities mismatch\n", __func__);
586 #ifdef IEEE80211_DEBUG
587 if (ieee80211_msg_assoc(vap))
588 check_bss_debug(vap, ni);
589 #endif
590 vap->iv_stats.is_ibss_capmismatch++;
591 return 0;
594 return 1;
598 * Handle 802.11 ad hoc network merge. The
599 * convention, set by the Wireless Ethernet Compatibility Alliance
600 * (WECA), is that an 802.11 station will change its BSSID to match
601 * the "oldest" 802.11 ad hoc network, on the same channel, that
602 * has the station's desired SSID. The "oldest" 802.11 network
603 * sends beacons with the greatest TSF timestamp.
605 * The caller is assumed to validate TSF's before attempting a merge.
607 * Return !0 if the BSSID changed, 0 otherwise.
610 ieee80211_ibss_merge(struct ieee80211_node *ni)
612 #ifdef IEEE80211_DEBUG
613 struct ieee80211vap *vap = ni->ni_vap;
614 struct ieee80211com *ic = ni->ni_ic;
615 #endif
617 if (! ieee80211_ibss_merge_check(ni))
618 return 0;
620 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
621 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
622 ether_sprintf(ni->ni_bssid),
623 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
624 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
625 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
627 return ieee80211_sta_join1(ieee80211_ref_node(ni));
631 * Calculate HT channel promotion flags for all vaps.
632 * This assumes ni_chan have been setup for each vap.
634 static int
635 gethtadjustflags(struct ieee80211com *ic)
637 struct ieee80211vap *vap;
638 int flags;
640 flags = 0;
641 /* XXX locking */
642 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
643 if (vap->iv_state < IEEE80211_S_RUN)
644 continue;
645 switch (vap->iv_opmode) {
646 case IEEE80211_M_WDS:
647 case IEEE80211_M_STA:
648 case IEEE80211_M_AHDEMO:
649 case IEEE80211_M_HOSTAP:
650 case IEEE80211_M_IBSS:
651 case IEEE80211_M_MBSS:
652 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
653 break;
654 default:
655 break;
658 return flags;
662 * Check if the current channel needs to change based on whether
663 * any vap's are using HT20/HT40. This is used to sync the state
664 * of ic_curchan after a channel width change on a running vap.
666 void
667 ieee80211_sync_curchan(struct ieee80211com *ic)
669 struct ieee80211_channel *c;
671 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
672 if (c != ic->ic_curchan) {
673 ic->ic_curchan = c;
674 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
675 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
676 IEEE80211_UNLOCK(ic);
677 ic->ic_set_channel(ic);
678 ieee80211_radiotap_chan_change(ic);
679 IEEE80211_LOCK(ic);
684 * Setup the current channel. The request channel may be
685 * promoted if other vap's are operating with HT20/HT40.
687 void
688 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
690 if (ic->ic_htcaps & IEEE80211_HTC_HT) {
691 int flags = gethtadjustflags(ic);
693 * Check for channel promotion required to support the
694 * set of running vap's. This assumes we are called
695 * after ni_chan is setup for each vap.
697 /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
698 if (flags > ieee80211_htchanflags(c))
699 c = ieee80211_ht_adjust_channel(ic, c, flags);
701 ic->ic_bsschan = ic->ic_curchan = c;
702 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
703 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
707 * Change the current channel. The channel change is guaranteed to have
708 * happened before the next state change.
710 void
711 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
713 ieee80211_setupcurchan(ic, c);
714 ieee80211_runtask(ic, &ic->ic_chan_task);
717 void
718 ieee80211_update_chw(struct ieee80211com *ic)
721 ieee80211_setupcurchan(ic, ic->ic_curchan);
722 ieee80211_runtask(ic, &ic->ic_chw_task);
726 * Join the specified IBSS/BSS network. The node is assumed to
727 * be passed in with a held reference.
729 static int
730 ieee80211_sta_join1(struct ieee80211_node *selbs)
732 struct ieee80211vap *vap = selbs->ni_vap;
733 struct ieee80211com *ic = selbs->ni_ic;
734 struct ieee80211_node *obss;
735 int canreassoc;
738 * Committed to selbs, setup state.
740 obss = vap->iv_bss;
742 * Check if old+new node have the same address in which
743 * case we can reassociate when operating in sta mode.
745 canreassoc = (obss != NULL &&
746 vap->iv_state == IEEE80211_S_RUN &&
747 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
748 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */
749 if (obss != NULL) {
750 struct ieee80211_node_table *nt = obss->ni_table;
752 copy_bss(selbs, obss);
753 ieee80211_node_decref(obss); /* iv_bss reference */
755 IEEE80211_NODE_LOCK(nt);
756 node_reclaim(nt, obss); /* station table reference */
757 IEEE80211_NODE_UNLOCK(nt);
759 obss = NULL; /* NB: guard against later use */
763 * Delete unusable rates; we've already checked
764 * that the negotiated rate set is acceptable.
766 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
767 IEEE80211_F_DODEL | IEEE80211_F_JOIN);
769 ieee80211_setcurchan(ic, selbs->ni_chan);
771 * Set the erp state (mostly the slot time) to deal with
772 * the auto-select case; this should be redundant if the
773 * mode is locked.
775 ieee80211_reset_erp(ic);
776 ieee80211_wme_initparams(vap);
778 if (vap->iv_opmode == IEEE80211_M_STA) {
779 if (canreassoc) {
780 /* Reassociate */
781 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
782 } else {
784 * Act as if we received a DEAUTH frame in case we
785 * are invoked from the RUN state. This will cause
786 * us to try to re-authenticate if we are operating
787 * as a station.
789 ieee80211_new_state(vap, IEEE80211_S_AUTH,
790 IEEE80211_FC0_SUBTYPE_DEAUTH);
792 } else
793 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
794 return 1;
798 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
799 const struct ieee80211_scan_entry *se)
801 struct ieee80211com *ic = vap->iv_ic;
802 struct ieee80211_node *ni;
804 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
805 if (ni == NULL) {
806 /* XXX msg */
807 return 0;
811 * Expand scan state into node's format.
812 * XXX may not need all this stuff
814 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
815 ni->ni_esslen = se->se_ssid[1];
816 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
817 ni->ni_tstamp.tsf = se->se_tstamp.tsf;
818 ni->ni_intval = se->se_intval;
819 ni->ni_capinfo = se->se_capinfo;
820 ni->ni_chan = chan;
821 ni->ni_timoff = se->se_timoff;
822 ni->ni_fhdwell = se->se_fhdwell;
823 ni->ni_fhindex = se->se_fhindex;
824 ni->ni_erp = se->se_erp;
825 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
826 ni->ni_noise = se->se_noise;
827 if (vap->iv_opmode == IEEE80211_M_STA) {
828 /* NB: only infrastructure mode requires an associd */
829 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
832 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
833 ieee80211_ies_expand(&ni->ni_ies);
834 #ifdef IEEE80211_SUPPORT_SUPERG
835 if (ni->ni_ies.ath_ie != NULL)
836 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
837 #endif
838 if (ni->ni_ies.htcap_ie != NULL)
839 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
840 if (ni->ni_ies.htinfo_ie != NULL)
841 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
842 #ifdef IEEE80211_SUPPORT_MESH
843 if (ni->ni_ies.meshid_ie != NULL)
844 ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
845 #endif
846 #ifdef IEEE80211_SUPPORT_TDMA
847 if (ni->ni_ies.tdma_ie != NULL)
848 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
849 #endif
852 vap->iv_dtim_period = se->se_dtimperiod;
853 vap->iv_dtim_count = 0;
855 /* NB: must be after ni_chan is setup */
856 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
857 IEEE80211_F_DOSORT);
858 if (ieee80211_iserp_rateset(&ni->ni_rates))
859 ni->ni_flags |= IEEE80211_NODE_ERP;
862 * Setup HT state for this node if it's available, otherwise
863 * non-STA modes won't pick this state up.
865 * For IBSS and related modes that don't go through an
866 * association request/response, the only appropriate place
867 * to setup the HT state is here.
869 if (ni->ni_ies.htinfo_ie != NULL &&
870 ni->ni_ies.htcap_ie != NULL &&
871 vap->iv_flags_ht & IEEE80211_FHT_HT) {
872 ieee80211_ht_node_init(ni);
873 ieee80211_ht_updateparams(ni,
874 ni->ni_ies.htcap_ie,
875 ni->ni_ies.htinfo_ie);
876 ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
877 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
878 ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
880 /* XXX else check for ath FF? */
881 /* XXX QoS? Difficult given that WME config is specific to a master */
883 ieee80211_node_setuptxparms(ni);
884 ieee80211_ratectl_node_init(ni);
886 return ieee80211_sta_join1(ieee80211_ref_node(ni));
890 * Leave the specified IBSS/BSS network. The node is assumed to
891 * be passed in with a held reference.
893 void
894 ieee80211_sta_leave(struct ieee80211_node *ni)
896 struct ieee80211com *ic = ni->ni_ic;
898 ic->ic_node_cleanup(ni);
899 ieee80211_notify_node_leave(ni);
903 * Send a deauthenticate frame and drop the station.
905 void
906 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
908 /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
909 ieee80211_ref_node(ni);
910 if (ni->ni_associd != 0)
911 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
912 ieee80211_node_leave(ni);
913 ieee80211_free_node(ni);
916 static struct ieee80211_node *
917 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
919 struct ieee80211_node *ni;
921 #if defined(__DragonFly__)
922 ni = (struct ieee80211_node *) kmalloc(sizeof(struct ieee80211_node),
923 M_80211_NODE, M_INTWAIT | M_ZERO);
924 #else
925 ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
926 M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
927 #endif
928 return ni;
932 * Initialize an ie blob with the specified data. If previous
933 * data exists re-use the data block. As a side effect we clear
934 * all references to specific ie's; the caller is required to
935 * recalculate them.
938 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
940 /* NB: assumes data+len are the last fields */
941 memset(ies, 0, offsetof(struct ieee80211_ies, data));
942 if (ies->data != NULL && ies->len != len) {
943 /* data size changed */
944 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
945 ies->data = NULL;
947 if (ies->data == NULL) {
948 #if defined(__DragonFly__)
949 ies->data = (uint8_t *) kmalloc(len,
950 M_80211_NODE_IE, M_INTWAIT | M_ZERO);
951 #else
952 ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
953 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
955 #endif
956 if (ies->data == NULL) {
957 ies->len = 0;
958 /* NB: pointers have already been zero'd above */
959 return 0;
962 memcpy(ies->data, data, len);
963 ies->len = len;
964 return 1;
968 * Reclaim storage for an ie blob.
970 void
971 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
973 if (ies->data != NULL)
974 IEEE80211_FREE(ies->data, M_80211_NODE_IE);
978 * Expand an ie blob data contents and to fillin individual
979 * ie pointers. The data blob is assumed to be well-formed;
980 * we don't do any validity checking of ie lengths.
982 void
983 ieee80211_ies_expand(struct ieee80211_ies *ies)
985 uint8_t *ie;
986 int ielen;
988 ie = ies->data;
989 ielen = ies->len;
990 while (ielen > 0) {
991 switch (ie[0]) {
992 case IEEE80211_ELEMID_VENDOR:
993 if (iswpaoui(ie))
994 ies->wpa_ie = ie;
995 else if (iswmeoui(ie))
996 ies->wme_ie = ie;
997 #ifdef IEEE80211_SUPPORT_SUPERG
998 else if (isatherosoui(ie))
999 ies->ath_ie = ie;
1000 #endif
1001 #ifdef IEEE80211_SUPPORT_TDMA
1002 else if (istdmaoui(ie))
1003 ies->tdma_ie = ie;
1004 #endif
1005 break;
1006 case IEEE80211_ELEMID_RSN:
1007 ies->rsn_ie = ie;
1008 break;
1009 case IEEE80211_ELEMID_HTCAP:
1010 ies->htcap_ie = ie;
1011 break;
1012 case IEEE80211_ELEMID_HTINFO:
1013 ies->htinfo_ie = ie;
1014 break;
1015 #ifdef IEEE80211_SUPPORT_MESH
1016 case IEEE80211_ELEMID_MESHID:
1017 ies->meshid_ie = ie;
1018 break;
1019 #endif
1021 ielen -= 2 + ie[1];
1022 ie += 2 + ie[1];
1027 * Reclaim any resources in a node and reset any critical
1028 * state. Typically nodes are free'd immediately after,
1029 * but in some cases the storage may be reused so we need
1030 * to insure consistent state (should probably fix that).
1032 static void
1033 node_cleanup(struct ieee80211_node *ni)
1035 struct ieee80211vap *vap = ni->ni_vap;
1036 struct ieee80211com *ic = ni->ni_ic;
1037 int i;
1039 /* NB: preserve ni_table */
1040 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1041 if (vap->iv_opmode != IEEE80211_M_STA)
1042 vap->iv_ps_sta--;
1043 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
1044 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
1045 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
1048 * Cleanup any HT-related state.
1050 if (ni->ni_flags & IEEE80211_NODE_HT)
1051 ieee80211_ht_node_cleanup(ni);
1052 #ifdef IEEE80211_SUPPORT_SUPERG
1053 /* Always do FF node cleanup; for A-MSDU */
1054 ieee80211_ff_node_cleanup(ni);
1055 #endif
1056 #ifdef IEEE80211_SUPPORT_MESH
1058 * Cleanup any mesh-related state.
1060 if (vap->iv_opmode == IEEE80211_M_MBSS)
1061 ieee80211_mesh_node_cleanup(ni);
1062 #endif
1064 * Clear any staging queue entries.
1066 ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1069 * Clear AREF flag that marks the authorization refcnt bump
1070 * has happened. This is probably not needed as the node
1071 * should always be removed from the table so not found but
1072 * do it just in case.
1073 * Likewise clear the ASSOCID flag as these flags are intended
1074 * to be managed in tandem.
1076 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1079 * Drain power save queue and, if needed, clear TIM.
1081 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1082 vap->iv_set_tim(ni, 0);
1084 ni->ni_associd = 0;
1085 if (ni->ni_challenge != NULL) {
1086 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1087 ni->ni_challenge = NULL;
1090 * Preserve SSID, WPA, and WME ie's so the bss node is
1091 * reusable during a re-auth/re-assoc state transition.
1092 * If we remove these data they will not be recreated
1093 * because they come from a probe-response or beacon frame
1094 * which cannot be expected prior to the association-response.
1095 * This should not be an issue when operating in other modes
1096 * as stations leaving always go through a full state transition
1097 * which will rebuild this state.
1099 * XXX does this leave us open to inheriting old state?
1101 for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1102 if (ni->ni_rxfrag[i] != NULL) {
1103 m_freem(ni->ni_rxfrag[i]);
1104 ni->ni_rxfrag[i] = NULL;
1107 * Must be careful here to remove any key map entry w/o a LOR.
1109 ieee80211_node_delucastkey(ni);
1112 static void
1113 node_free(struct ieee80211_node *ni)
1115 struct ieee80211com *ic = ni->ni_ic;
1117 ieee80211_ratectl_node_deinit(ni);
1118 ic->ic_node_cleanup(ni);
1119 ieee80211_ies_cleanup(&ni->ni_ies);
1120 ieee80211_psq_cleanup(&ni->ni_psq);
1121 IEEE80211_FREE(ni, M_80211_NODE);
1124 static void
1125 node_age(struct ieee80211_node *ni)
1127 struct ieee80211vap *vap = ni->ni_vap;
1129 IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
1132 * Age frames on the power save queue.
1134 if (ieee80211_node_psq_age(ni) != 0 &&
1135 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1136 vap->iv_set_tim(ni, 0);
1138 * Age out HT resources (e.g. frames on the
1139 * A-MPDU reorder queues).
1141 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1142 ieee80211_ht_node_age(ni);
1145 static int8_t
1146 node_getrssi(const struct ieee80211_node *ni)
1148 uint32_t avgrssi = ni->ni_avgrssi;
1149 int32_t rssi;
1151 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1152 return 0;
1153 rssi = IEEE80211_RSSI_GET(avgrssi);
1154 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1157 static void
1158 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1160 *rssi = node_getrssi(ni);
1161 *noise = ni->ni_noise;
1164 static void
1165 node_getmimoinfo(const struct ieee80211_node *ni,
1166 struct ieee80211_mimo_info *info)
1168 int i;
1169 uint32_t avgrssi;
1170 int32_t rssi;
1172 bzero(info, sizeof(*info));
1174 for (i = 0; i < ni->ni_mimo_chains; i++) {
1175 avgrssi = ni->ni_mimo_rssi_ctl[i];
1176 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1177 info->rssi[i] = 0;
1178 } else {
1179 rssi = IEEE80211_RSSI_GET(avgrssi);
1180 info->rssi[i] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1182 info->noise[i] = ni->ni_mimo_noise_ctl[i];
1185 /* XXX ext radios? */
1187 /* XXX EVM? */
1190 struct ieee80211_node *
1191 ieee80211_alloc_node(struct ieee80211_node_table *nt,
1192 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1194 struct ieee80211com *ic = nt->nt_ic;
1195 struct ieee80211_node *ni;
1196 int hash;
1198 ni = ic->ic_node_alloc(vap, macaddr);
1199 if (ni == NULL) {
1200 vap->iv_stats.is_rx_nodealloc++;
1201 return NULL;
1204 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1205 "%s %p<%s> in %s table\n", __func__, ni,
1206 ether_sprintf(macaddr), nt->nt_name);
1208 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1209 hash = IEEE80211_NODE_HASH(ic, macaddr);
1210 ieee80211_node_initref(ni); /* mark referenced */
1211 ni->ni_chan = IEEE80211_CHAN_ANYC;
1212 ni->ni_authmode = IEEE80211_AUTH_OPEN;
1213 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
1214 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1215 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1216 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1217 ni->ni_inact_reload = nt->nt_inact_init;
1218 ni->ni_inact = ni->ni_inact_reload;
1219 ni->ni_ath_defkeyix = 0x7fff;
1220 ieee80211_psq_init(&ni->ni_psq, "unknown");
1221 #ifdef IEEE80211_SUPPORT_MESH
1222 if (vap->iv_opmode == IEEE80211_M_MBSS)
1223 ieee80211_mesh_node_init(vap, ni);
1224 #endif
1225 IEEE80211_NODE_LOCK(nt);
1226 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1227 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1228 ni->ni_table = nt;
1229 ni->ni_vap = vap;
1230 ni->ni_ic = ic;
1231 IEEE80211_NODE_UNLOCK(nt);
1233 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1234 "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1236 ieee80211_ratectl_node_init(ni);
1238 return ni;
1242 * Craft a temporary node suitable for sending a management frame
1243 * to the specified station. We craft only as much state as we
1244 * need to do the work since the node will be immediately reclaimed
1245 * once the send completes.
1247 struct ieee80211_node *
1248 ieee80211_tmp_node(struct ieee80211vap *vap,
1249 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1251 struct ieee80211com *ic = vap->iv_ic;
1252 struct ieee80211_node *ni;
1254 ni = ic->ic_node_alloc(vap, macaddr);
1255 if (ni != NULL) {
1256 struct ieee80211_node *bss = vap->iv_bss;
1258 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1259 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1261 ni->ni_table = NULL; /* NB: pedantic */
1262 ni->ni_ic = ic; /* NB: needed to set channel */
1263 ni->ni_vap = vap;
1265 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1266 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1267 ieee80211_node_initref(ni); /* mark referenced */
1268 /* NB: required by ieee80211_fix_rate */
1269 ieee80211_node_set_chan(ni, bss->ni_chan);
1270 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1271 IEEE80211_KEYIX_NONE);
1272 ni->ni_txpower = bss->ni_txpower;
1273 /* XXX optimize away */
1274 ieee80211_psq_init(&ni->ni_psq, "unknown");
1276 ieee80211_ratectl_node_init(ni);
1277 } else {
1278 /* XXX msg */
1279 vap->iv_stats.is_rx_nodealloc++;
1281 return ni;
1284 struct ieee80211_node *
1285 ieee80211_dup_bss(struct ieee80211vap *vap,
1286 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1288 struct ieee80211com *ic = vap->iv_ic;
1289 struct ieee80211_node *ni;
1291 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1292 if (ni != NULL) {
1293 struct ieee80211_node *bss = vap->iv_bss;
1295 * Inherit from iv_bss.
1297 copy_bss(ni, bss);
1298 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1299 ieee80211_node_set_chan(ni, bss->ni_chan);
1301 return ni;
1305 * Create a bss node for a legacy WDS vap. The far end does
1306 * not associate so we just create create a new node and
1307 * simulate an association. The caller is responsible for
1308 * installing the node as the bss node and handling any further
1309 * setup work like authorizing the port.
1311 struct ieee80211_node *
1312 ieee80211_node_create_wds(struct ieee80211vap *vap,
1313 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1315 struct ieee80211com *ic = vap->iv_ic;
1316 struct ieee80211_node *ni;
1318 /* XXX check if node already in sta table? */
1319 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1320 if (ni != NULL) {
1321 ni->ni_wdsvap = vap;
1322 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1324 * Inherit any manually configured settings.
1326 copy_bss(ni, vap->iv_bss);
1327 ieee80211_node_set_chan(ni, chan);
1328 /* NB: propagate ssid so available to WPA supplicant */
1329 ni->ni_esslen = vap->iv_des_ssid[0].len;
1330 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1331 /* NB: no associd for peer */
1333 * There are no management frames to use to
1334 * discover neighbor capabilities, so blindly
1335 * propagate the local configuration.
1337 if (vap->iv_flags & IEEE80211_F_WME)
1338 ni->ni_flags |= IEEE80211_NODE_QOS;
1339 #ifdef IEEE80211_SUPPORT_SUPERG
1340 if (vap->iv_flags & IEEE80211_F_FF)
1341 ni->ni_flags |= IEEE80211_NODE_FF;
1342 #endif
1343 if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1344 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1346 * Device is HT-capable and HT is enabled for
1347 * the vap; setup HT operation. On return
1348 * ni_chan will be adjusted to an HT channel.
1350 ieee80211_ht_wds_init(ni);
1351 } else {
1352 struct ieee80211_channel *c = ni->ni_chan;
1354 * Force a legacy channel to be used.
1356 c = ieee80211_find_channel(ic,
1357 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1358 KASSERT(c != NULL, ("no legacy channel, %u/%x",
1359 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1360 ni->ni_chan = c;
1363 return ni;
1366 struct ieee80211_node *
1367 #ifdef IEEE80211_DEBUG_REFCNT
1368 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1369 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1370 #else
1371 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1372 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1373 #endif
1375 struct ieee80211_node *ni;
1376 int hash;
1378 IEEE80211_NODE_LOCK_ASSERT(nt);
1380 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1381 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1382 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1383 ieee80211_ref_node(ni); /* mark referenced */
1384 #ifdef IEEE80211_DEBUG_REFCNT
1385 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1386 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1387 func, line,
1388 ni, ether_sprintf(ni->ni_macaddr),
1389 ieee80211_node_refcnt(ni));
1390 #endif
1391 return ni;
1394 return NULL;
1397 struct ieee80211_node *
1398 #ifdef IEEE80211_DEBUG_REFCNT
1399 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1400 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1401 #else
1402 ieee80211_find_node(struct ieee80211_node_table *nt,
1403 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1404 #endif
1406 struct ieee80211_node *ni;
1408 IEEE80211_NODE_LOCK(nt);
1409 ni = ieee80211_find_node_locked(nt, macaddr);
1410 IEEE80211_NODE_UNLOCK(nt);
1411 return ni;
1414 struct ieee80211_node *
1415 #ifdef IEEE80211_DEBUG_REFCNT
1416 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1417 const struct ieee80211vap *vap,
1418 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1419 #else
1420 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1421 const struct ieee80211vap *vap,
1422 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1423 #endif
1425 struct ieee80211_node *ni;
1426 int hash;
1428 IEEE80211_NODE_LOCK_ASSERT(nt);
1430 hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1431 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1432 if (ni->ni_vap == vap &&
1433 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1434 ieee80211_ref_node(ni); /* mark referenced */
1435 #ifdef IEEE80211_DEBUG_REFCNT
1436 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1437 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1438 func, line,
1439 ni, ether_sprintf(ni->ni_macaddr),
1440 ieee80211_node_refcnt(ni));
1441 #endif
1442 return ni;
1445 return NULL;
1448 struct ieee80211_node *
1449 #ifdef IEEE80211_DEBUG_REFCNT
1450 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1451 const struct ieee80211vap *vap,
1452 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1453 #else
1454 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1455 const struct ieee80211vap *vap,
1456 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1457 #endif
1459 struct ieee80211_node *ni;
1461 IEEE80211_NODE_LOCK(nt);
1462 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1463 IEEE80211_NODE_UNLOCK(nt);
1464 return ni;
1468 * Fake up a node; this handles node discovery in adhoc mode.
1469 * Note that for the driver's benefit we we treat this like
1470 * an association so the driver has an opportunity to setup
1471 * it's private state.
1473 struct ieee80211_node *
1474 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1475 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1477 struct ieee80211_node *ni;
1479 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1480 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1481 ni = ieee80211_dup_bss(vap, macaddr);
1482 if (ni != NULL) {
1483 struct ieee80211com *ic = vap->iv_ic;
1485 /* XXX no rate negotiation; just dup */
1486 ni->ni_rates = vap->iv_bss->ni_rates;
1487 if (ieee80211_iserp_rateset(&ni->ni_rates))
1488 ni->ni_flags |= IEEE80211_NODE_ERP;
1489 if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1491 * In adhoc demo mode there are no management
1492 * frames to use to discover neighbor capabilities,
1493 * so blindly propagate the local configuration
1494 * so we can do interesting things (e.g. use
1495 * WME to disable ACK's).
1497 if (vap->iv_flags & IEEE80211_F_WME)
1498 ni->ni_flags |= IEEE80211_NODE_QOS;
1499 #ifdef IEEE80211_SUPPORT_SUPERG
1500 if (vap->iv_flags & IEEE80211_F_FF)
1501 ni->ni_flags |= IEEE80211_NODE_FF;
1502 #endif
1504 ieee80211_node_setuptxparms(ni);
1505 ieee80211_ratectl_node_init(ni);
1506 if (ic->ic_newassoc != NULL)
1507 ic->ic_newassoc(ni, 1);
1508 /* XXX not right for 802.1x/WPA */
1509 ieee80211_node_authorize(ni);
1511 return ni;
1514 void
1515 ieee80211_init_neighbor(struct ieee80211_node *ni,
1516 const struct ieee80211_frame *wh,
1517 const struct ieee80211_scanparams *sp)
1519 int do_ht_setup = 0;
1521 ni->ni_esslen = sp->ssid[1];
1522 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1523 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1524 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1525 ni->ni_intval = sp->bintval;
1526 ni->ni_capinfo = sp->capinfo;
1527 ni->ni_chan = ni->ni_ic->ic_curchan;
1528 ni->ni_fhdwell = sp->fhdwell;
1529 ni->ni_fhindex = sp->fhindex;
1530 ni->ni_erp = sp->erp;
1531 ni->ni_timoff = sp->timoff;
1532 #ifdef IEEE80211_SUPPORT_MESH
1533 if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1534 ieee80211_mesh_init_neighbor(ni, wh, sp);
1535 #endif
1536 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1537 ieee80211_ies_expand(&ni->ni_ies);
1538 if (ni->ni_ies.wme_ie != NULL)
1539 ni->ni_flags |= IEEE80211_NODE_QOS;
1540 else
1541 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1542 #ifdef IEEE80211_SUPPORT_SUPERG
1543 if (ni->ni_ies.ath_ie != NULL)
1544 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1545 #endif
1546 if (ni->ni_ies.htcap_ie != NULL)
1547 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1548 if (ni->ni_ies.htinfo_ie != NULL)
1549 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1551 if ((ni->ni_ies.htcap_ie != NULL) &&
1552 (ni->ni_ies.htinfo_ie != NULL) &&
1553 (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1554 do_ht_setup = 1;
1558 /* NB: must be after ni_chan is setup */
1559 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1560 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1561 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1564 * If the neighbor is HT compatible, flip that on.
1566 if (do_ht_setup) {
1567 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1568 "%s: doing HT setup\n", __func__);
1569 ieee80211_ht_node_init(ni);
1570 ieee80211_ht_updateparams(ni,
1571 ni->ni_ies.htcap_ie,
1572 ni->ni_ies.htinfo_ie);
1573 ieee80211_setup_htrates(ni,
1574 ni->ni_ies.htcap_ie,
1575 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1576 ieee80211_setup_basic_htrates(ni,
1577 ni->ni_ies.htinfo_ie);
1578 ieee80211_node_setuptxparms(ni);
1579 ieee80211_ratectl_node_init(ni);
1584 * Do node discovery in adhoc mode on receipt of a beacon
1585 * or probe response frame. Note that for the driver's
1586 * benefit we we treat this like an association so the
1587 * driver has an opportunity to setup it's private state.
1589 struct ieee80211_node *
1590 ieee80211_add_neighbor(struct ieee80211vap *vap,
1591 const struct ieee80211_frame *wh,
1592 const struct ieee80211_scanparams *sp)
1594 struct ieee80211_node *ni;
1596 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1597 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1598 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1599 if (ni != NULL) {
1600 struct ieee80211com *ic = vap->iv_ic;
1602 ieee80211_init_neighbor(ni, wh, sp);
1603 if (ieee80211_iserp_rateset(&ni->ni_rates))
1604 ni->ni_flags |= IEEE80211_NODE_ERP;
1605 ieee80211_node_setuptxparms(ni);
1606 ieee80211_ratectl_node_init(ni);
1607 if (ic->ic_newassoc != NULL)
1608 ic->ic_newassoc(ni, 1);
1609 /* XXX not right for 802.1x/WPA */
1610 ieee80211_node_authorize(ni);
1612 return ni;
1615 #define IS_PROBEREQ(wh) \
1616 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1617 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1618 #define IS_BCAST_PROBEREQ(wh) \
1619 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1620 ((const struct ieee80211_frame *)(wh))->i_addr3))
1622 static __inline struct ieee80211_node *
1623 _find_rxnode(struct ieee80211_node_table *nt,
1624 const struct ieee80211_frame_min *wh)
1626 if (IS_BCAST_PROBEREQ(wh))
1627 return NULL; /* spam bcast probe req to all vap's */
1628 return ieee80211_find_node_locked(nt, wh->i_addr2);
1632 * Locate the node for sender, track state, and then pass the
1633 * (referenced) node up to the 802.11 layer for its use. Note
1634 * we can return NULL if the sender is not in the table.
1636 struct ieee80211_node *
1637 #ifdef IEEE80211_DEBUG_REFCNT
1638 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1639 const struct ieee80211_frame_min *wh, const char *func, int line)
1640 #else
1641 ieee80211_find_rxnode(struct ieee80211com *ic,
1642 const struct ieee80211_frame_min *wh)
1643 #endif
1645 struct ieee80211_node_table *nt;
1646 struct ieee80211_node *ni;
1648 nt = &ic->ic_sta;
1649 IEEE80211_NODE_LOCK(nt);
1650 ni = _find_rxnode(nt, wh);
1651 IEEE80211_NODE_UNLOCK(nt);
1653 return ni;
1657 * Like ieee80211_find_rxnode but use the supplied h/w
1658 * key index as a hint to locate the node in the key
1659 * mapping table. If an entry is present at the key
1660 * index we return it; otherwise do a normal lookup and
1661 * update the mapping table if the station has a unicast
1662 * key assigned to it.
1664 struct ieee80211_node *
1665 #ifdef IEEE80211_DEBUG_REFCNT
1666 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1667 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1668 const char *func, int line)
1669 #else
1670 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1671 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1672 #endif
1674 struct ieee80211_node_table *nt;
1675 struct ieee80211_node *ni;
1677 nt = &ic->ic_sta;
1678 IEEE80211_NODE_LOCK(nt);
1679 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1680 ni = nt->nt_keyixmap[keyix];
1681 else
1682 ni = NULL;
1683 if (ni == NULL) {
1684 ni = _find_rxnode(nt, wh);
1685 if (ni != NULL && nt->nt_keyixmap != NULL) {
1687 * If the station has a unicast key cache slot
1688 * assigned update the key->node mapping table.
1690 keyix = ni->ni_ucastkey.wk_rxkeyix;
1691 /* XXX can keyixmap[keyix] != NULL? */
1692 if (keyix < nt->nt_keyixmax &&
1693 nt->nt_keyixmap[keyix] == NULL) {
1694 IEEE80211_DPRINTF(ni->ni_vap,
1695 IEEE80211_MSG_NODE,
1696 "%s: add key map entry %p<%s> refcnt %d\n",
1697 __func__, ni, ether_sprintf(ni->ni_macaddr),
1698 ieee80211_node_refcnt(ni)+1);
1699 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1702 } else {
1703 if (IS_BCAST_PROBEREQ(wh))
1704 ni = NULL; /* spam bcast probe req to all vap's */
1705 else
1706 ieee80211_ref_node(ni);
1708 IEEE80211_NODE_UNLOCK(nt);
1710 return ni;
1712 #undef IS_BCAST_PROBEREQ
1713 #undef IS_PROBEREQ
1716 * Return a reference to the appropriate node for sending
1717 * a data frame. This handles node discovery in adhoc networks.
1719 struct ieee80211_node *
1720 #ifdef IEEE80211_DEBUG_REFCNT
1721 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1722 const uint8_t macaddr[IEEE80211_ADDR_LEN],
1723 const char *func, int line)
1724 #else
1725 ieee80211_find_txnode(struct ieee80211vap *vap,
1726 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1727 #endif
1729 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1730 struct ieee80211_node *ni;
1733 * The destination address should be in the node table
1734 * unless this is a multicast/broadcast frame. We can
1735 * also optimize station mode operation, all frames go
1736 * to the bss node.
1738 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1739 IEEE80211_NODE_LOCK(nt);
1740 if (vap->iv_opmode == IEEE80211_M_STA ||
1741 vap->iv_opmode == IEEE80211_M_WDS ||
1742 IEEE80211_IS_MULTICAST(macaddr))
1743 ni = ieee80211_ref_node(vap->iv_bss);
1744 else
1745 ni = ieee80211_find_node_locked(nt, macaddr);
1746 IEEE80211_NODE_UNLOCK(nt);
1748 if (ni == NULL) {
1749 if (vap->iv_opmode == IEEE80211_M_IBSS ||
1750 vap->iv_opmode == IEEE80211_M_AHDEMO) {
1752 * In adhoc mode cons up a node for the destination.
1753 * Note that we need an additional reference for the
1754 * caller to be consistent with
1755 * ieee80211_find_node_locked.
1757 ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1758 if (ni != NULL)
1759 (void) ieee80211_ref_node(ni);
1760 } else {
1761 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1762 "no node, discard frame (%s)", __func__);
1763 vap->iv_stats.is_tx_nonode++;
1766 return ni;
1769 static void
1770 _ieee80211_free_node(struct ieee80211_node *ni)
1772 struct ieee80211_node_table *nt = ni->ni_table;
1775 * NB: careful about referencing the vap as it may be
1776 * gone if the last reference was held by a driver.
1777 * We know the com will always be present so it's safe
1778 * to use ni_ic below to reclaim resources.
1780 #if 0
1781 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1782 "%s %p<%s> in %s table\n", __func__, ni,
1783 ether_sprintf(ni->ni_macaddr),
1784 nt != NULL ? nt->nt_name : "<gone>");
1785 #endif
1786 if (ni->ni_associd != 0) {
1787 struct ieee80211vap *vap = ni->ni_vap;
1788 if (vap->iv_aid_bitmap != NULL)
1789 IEEE80211_AID_CLR(vap, ni->ni_associd);
1791 if (nt != NULL) {
1792 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1793 LIST_REMOVE(ni, ni_hash);
1795 ni->ni_ic->ic_node_free(ni);
1799 * Clear any entry in the unicast key mapping table.
1801 static int
1802 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1804 ieee80211_keyix keyix;
1806 keyix = ni->ni_ucastkey.wk_rxkeyix;
1807 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1808 nt->nt_keyixmap[keyix] == ni) {
1809 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1810 "%s: %p<%s> clear key map entry %u\n",
1811 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1812 nt->nt_keyixmap[keyix] = NULL;
1813 ieee80211_node_decref(ni);
1814 return 1;
1817 return 0;
1820 void
1821 #ifdef IEEE80211_DEBUG_REFCNT
1822 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1823 #else
1824 ieee80211_free_node(struct ieee80211_node *ni)
1825 #endif
1827 struct ieee80211_node_table *nt = ni->ni_table;
1829 #ifdef IEEE80211_DEBUG_REFCNT
1830 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1831 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1832 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1833 #endif
1834 if (nt != NULL) {
1835 IEEE80211_NODE_LOCK(nt);
1836 if (ieee80211_node_dectestref(ni)) {
1838 * Last reference, reclaim state.
1840 _ieee80211_free_node(ni);
1841 } else if (ieee80211_node_refcnt(ni) == 1)
1842 if (node_clear_keyixmap(nt, ni))
1843 _ieee80211_free_node(ni);
1844 IEEE80211_NODE_UNLOCK(nt);
1845 } else {
1846 if (ieee80211_node_dectestref(ni))
1847 _ieee80211_free_node(ni);
1852 * Reclaim a unicast key and clear any key cache state.
1855 ieee80211_node_delucastkey(struct ieee80211_node *ni)
1857 struct ieee80211com *ic = ni->ni_ic;
1858 struct ieee80211_node_table *nt = &ic->ic_sta;
1859 struct ieee80211_node *nikey;
1860 ieee80211_keyix keyix;
1861 int isowned, status;
1864 * NB: We must beware of LOR here; deleting the key
1865 * can cause the crypto layer to block traffic updates
1866 * which can generate a LOR against the node table lock;
1867 * grab it here and stash the key index for our use below.
1869 * Must also beware of recursion on the node table lock.
1870 * When called from node_cleanup we may already have
1871 * the node table lock held. Unfortunately there's no
1872 * way to separate out this path so we must do this
1873 * conditionally.
1875 isowned = IEEE80211_NODE_IS_LOCKED(nt);
1876 if (!isowned)
1877 IEEE80211_NODE_LOCK(nt);
1878 nikey = NULL;
1879 status = 1; /* NB: success */
1880 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1881 keyix = ni->ni_ucastkey.wk_rxkeyix;
1882 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1883 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1884 nikey = nt->nt_keyixmap[keyix];
1885 nt->nt_keyixmap[keyix] = NULL;
1888 if (!isowned)
1889 IEEE80211_NODE_UNLOCK(nt);
1891 if (nikey != NULL) {
1892 KASSERT(nikey == ni,
1893 ("key map out of sync, ni %p nikey %p", ni, nikey));
1894 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1895 "%s: delete key map entry %p<%s> refcnt %d\n",
1896 __func__, ni, ether_sprintf(ni->ni_macaddr),
1897 ieee80211_node_refcnt(ni)-1);
1898 ieee80211_free_node(ni);
1900 return status;
1904 * Reclaim a node. If this is the last reference count then
1905 * do the normal free work. Otherwise remove it from the node
1906 * table and mark it gone by clearing the back-reference.
1908 static void
1909 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1912 IEEE80211_NODE_LOCK_ASSERT(nt);
1914 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1915 "%s: remove %p<%s> from %s table, refcnt %d\n",
1916 __func__, ni, ether_sprintf(ni->ni_macaddr),
1917 nt->nt_name, ieee80211_node_refcnt(ni)-1);
1919 * Clear any entry in the unicast key mapping table.
1920 * We need to do it here so rx lookups don't find it
1921 * in the mapping table even if it's not in the hash
1922 * table. We cannot depend on the mapping table entry
1923 * being cleared because the node may not be free'd.
1925 (void)node_clear_keyixmap(nt, ni);
1926 if (!ieee80211_node_dectestref(ni)) {
1928 * Other references are present, just remove the
1929 * node from the table so it cannot be found. When
1930 * the references are dropped storage will be
1931 * reclaimed.
1933 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1934 LIST_REMOVE(ni, ni_hash);
1935 ni->ni_table = NULL; /* clear reference */
1936 } else
1937 _ieee80211_free_node(ni);
1941 * Node table support.
1944 static void
1945 ieee80211_node_table_init(struct ieee80211com *ic,
1946 struct ieee80211_node_table *nt,
1947 const char *name, int inact, int keyixmax)
1950 nt->nt_ic = ic;
1951 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
1952 IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ic->ic_name);
1953 TAILQ_INIT(&nt->nt_node);
1954 nt->nt_name = name;
1955 nt->nt_scangen = 1;
1956 nt->nt_inact_init = inact;
1957 nt->nt_keyixmax = keyixmax;
1958 if (nt->nt_keyixmax > 0) {
1959 #if defined(__DragonFly__)
1960 nt->nt_keyixmap = (struct ieee80211_node **) kmalloc(
1961 keyixmax * sizeof(struct ieee80211_node *),
1962 M_80211_NODE, M_INTWAIT | M_ZERO);
1963 #else
1964 nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
1965 keyixmax * sizeof(struct ieee80211_node *),
1966 M_80211_NODE,
1967 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1968 #endif
1969 if (nt->nt_keyixmap == NULL)
1970 ic_printf(ic,
1971 "Cannot allocate key index map with %u entries\n",
1972 keyixmax);
1973 } else
1974 nt->nt_keyixmap = NULL;
1977 static void
1978 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
1979 struct ieee80211vap *match)
1981 struct ieee80211_node *ni, *next;
1983 IEEE80211_NODE_LOCK(nt);
1984 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1985 if (match != NULL && ni->ni_vap != match)
1986 continue;
1987 /* XXX can this happen? if so need's work */
1988 if (ni->ni_associd != 0) {
1989 struct ieee80211vap *vap = ni->ni_vap;
1991 if (vap->iv_auth->ia_node_leave != NULL)
1992 vap->iv_auth->ia_node_leave(ni);
1993 if (vap->iv_aid_bitmap != NULL)
1994 IEEE80211_AID_CLR(vap, ni->ni_associd);
1996 ni->ni_wdsvap = NULL; /* clear reference */
1997 node_reclaim(nt, ni);
1999 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
2001 * Make a separate pass to clear references to this vap
2002 * held by DWDS entries. They will not be matched above
2003 * because ni_vap will point to the ap vap but we still
2004 * need to clear ni_wdsvap when the WDS vap is destroyed
2005 * and/or reset.
2007 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
2008 if (ni->ni_wdsvap == match)
2009 ni->ni_wdsvap = NULL;
2011 IEEE80211_NODE_UNLOCK(nt);
2014 static void
2015 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2017 ieee80211_node_table_reset(nt, NULL);
2018 if (nt->nt_keyixmap != NULL) {
2019 #ifdef DIAGNOSTIC
2020 /* XXX verify all entries are NULL */
2021 int i;
2022 for (i = 0; i < nt->nt_keyixmax; i++)
2023 if (nt->nt_keyixmap[i] != NULL)
2024 kprintf("%s: %s[%u] still active\n", __func__,
2025 nt->nt_name, i);
2026 #endif
2027 IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
2028 nt->nt_keyixmap = NULL;
2030 IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
2031 IEEE80211_NODE_LOCK_DESTROY(nt);
2035 * Timeout inactive stations and do related housekeeping.
2036 * Note that we cannot hold the node lock while sending a
2037 * frame as this would lead to a LOR. Instead we use a
2038 * generation number to mark nodes that we've scanned and
2039 * drop the lock and restart a scan if we have to time out
2040 * a node. Since we are single-threaded by virtue of
2041 * controlling the inactivity timer we can be sure this will
2042 * process each node only once.
2044 static void
2045 ieee80211_timeout_stations(struct ieee80211com *ic)
2047 struct ieee80211_node_table *nt = &ic->ic_sta;
2048 struct ieee80211vap *vap;
2049 struct ieee80211_node *ni;
2050 int gen = 0;
2052 IEEE80211_NODE_ITERATE_LOCK(nt);
2053 gen = ++nt->nt_scangen;
2054 restart:
2055 IEEE80211_NODE_LOCK(nt);
2056 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2057 if (ni->ni_scangen == gen) /* previously handled */
2058 continue;
2059 ni->ni_scangen = gen;
2061 * Ignore entries for which have yet to receive an
2062 * authentication frame. These are transient and
2063 * will be reclaimed when the last reference to them
2064 * goes away (when frame xmits complete).
2066 vap = ni->ni_vap;
2068 * Only process stations when in RUN state. This
2069 * insures, for example, that we don't timeout an
2070 * inactive station during CAC. Note that CSA state
2071 * is actually handled in ieee80211_node_timeout as
2072 * it applies to more than timeout processing.
2074 if (vap->iv_state != IEEE80211_S_RUN)
2075 continue;
2076 /* XXX can vap be NULL? */
2077 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2078 vap->iv_opmode == IEEE80211_M_STA) &&
2079 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2080 continue;
2082 * Free fragment if not needed anymore
2083 * (last fragment older than 1s).
2084 * XXX doesn't belong here, move to node_age
2086 if (ni->ni_rxfrag[0] != NULL &&
2087 ticks > ni->ni_rxfragstamp + hz) {
2088 m_freem(ni->ni_rxfrag[0]);
2089 ni->ni_rxfrag[0] = NULL;
2091 if (ni->ni_inact > 0) {
2092 ni->ni_inact--;
2093 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2094 "%s: inact %u inact_reload %u nrates %u",
2095 __func__, ni->ni_inact, ni->ni_inact_reload,
2096 ni->ni_rates.rs_nrates);
2099 * Special case ourself; we may be idle for extended periods
2100 * of time and regardless reclaiming our state is wrong.
2101 * XXX run ic_node_age
2103 if (ni == vap->iv_bss)
2104 continue;
2105 if (ni->ni_associd != 0 ||
2106 (vap->iv_opmode == IEEE80211_M_IBSS ||
2107 vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2109 * Age/drain resources held by the station.
2111 ic->ic_node_age(ni);
2113 * Probe the station before time it out. We
2114 * send a null data frame which may not be
2115 * universally supported by drivers (need it
2116 * for ps-poll support so it should be...).
2118 * XXX don't probe the station unless we've
2119 * received a frame from them (and have
2120 * some idea of the rates they are capable
2121 * of); this will get fixed more properly
2122 * soon with better handling of the rate set.
2124 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2125 (0 < ni->ni_inact &&
2126 ni->ni_inact <= vap->iv_inact_probe) &&
2127 ni->ni_rates.rs_nrates != 0) {
2128 IEEE80211_NOTE(vap,
2129 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2130 ni, "%s",
2131 "probe station due to inactivity");
2133 * Grab a reference before unlocking the table
2134 * so the node cannot be reclaimed before we
2135 * send the frame. ieee80211_send_nulldata
2136 * understands we've done this and reclaims the
2137 * ref for us as needed.
2139 ieee80211_ref_node(ni);
2140 IEEE80211_NODE_UNLOCK(nt);
2141 ieee80211_send_nulldata(ni);
2142 /* XXX stat? */
2143 goto restart;
2146 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2147 ni->ni_inact <= 0) {
2148 IEEE80211_NOTE(vap,
2149 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2150 "station timed out due to inactivity "
2151 "(refcnt %u)", ieee80211_node_refcnt(ni));
2153 * Send a deauthenticate frame and drop the station.
2154 * This is somewhat complicated due to reference counts
2155 * and locking. At this point a station will typically
2156 * have a reference count of 1. ieee80211_node_leave
2157 * will do a "free" of the node which will drop the
2158 * reference count. But in the meantime a reference
2159 * wil be held by the deauth frame. The actual reclaim
2160 * of the node will happen either after the tx is
2161 * completed or by ieee80211_node_leave.
2163 * Separately we must drop the node lock before sending
2164 * in case the driver takes a lock, as this can result
2165 * in a LOR between the node lock and the driver lock.
2167 ieee80211_ref_node(ni);
2168 IEEE80211_NODE_UNLOCK(nt);
2169 if (ni->ni_associd != 0) {
2170 IEEE80211_SEND_MGMT(ni,
2171 IEEE80211_FC0_SUBTYPE_DEAUTH,
2172 IEEE80211_REASON_AUTH_EXPIRE);
2174 ieee80211_node_leave(ni);
2175 ieee80211_free_node(ni);
2176 vap->iv_stats.is_node_timeout++;
2177 goto restart;
2180 IEEE80211_NODE_UNLOCK(nt);
2182 IEEE80211_NODE_ITERATE_UNLOCK(nt);
2186 * Aggressively reclaim resources. This should be used
2187 * only in a critical situation to reclaim mbuf resources.
2189 void
2190 ieee80211_drain(struct ieee80211com *ic)
2192 struct ieee80211_node_table *nt = &ic->ic_sta;
2193 struct ieee80211vap *vap;
2194 struct ieee80211_node *ni;
2196 IEEE80211_NODE_LOCK(nt);
2197 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2199 * Ignore entries for which have yet to receive an
2200 * authentication frame. These are transient and
2201 * will be reclaimed when the last reference to them
2202 * goes away (when frame xmits complete).
2204 vap = ni->ni_vap;
2206 * Only process stations when in RUN state. This
2207 * insures, for example, that we don't timeout an
2208 * inactive station during CAC. Note that CSA state
2209 * is actually handled in ieee80211_node_timeout as
2210 * it applies to more than timeout processing.
2212 if (vap->iv_state != IEEE80211_S_RUN)
2213 continue;
2214 /* XXX can vap be NULL? */
2215 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2216 vap->iv_opmode == IEEE80211_M_STA) &&
2217 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2218 continue;
2220 * Free fragments.
2221 * XXX doesn't belong here, move to node_drain
2223 if (ni->ni_rxfrag[0] != NULL) {
2224 m_freem(ni->ni_rxfrag[0]);
2225 ni->ni_rxfrag[0] = NULL;
2228 * Drain resources held by the station.
2230 ic->ic_node_drain(ni);
2232 IEEE80211_NODE_UNLOCK(nt);
2236 * Per-ieee80211com inactivity timer callback.
2238 void
2239 ieee80211_node_timeout(void *arg)
2241 struct ieee80211com *ic = arg;
2244 * Defer timeout processing if a channel switch is pending.
2245 * We typically need to be mute so not doing things that
2246 * might generate frames is good to handle in one place.
2247 * Suppressing the station timeout processing may extend the
2248 * lifetime of inactive stations (by not decrementing their
2249 * idle counters) but this should be ok unless the CSA is
2250 * active for an unusually long time.
2252 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2253 ieee80211_scan_timeout(ic);
2254 ieee80211_timeout_stations(ic);
2255 ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2257 IEEE80211_LOCK(ic);
2258 ieee80211_erp_timeout(ic);
2259 ieee80211_ht_timeout(ic);
2260 IEEE80211_UNLOCK(ic);
2262 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2263 ieee80211_node_timeout, ic);
2267 * Iterate over the node table and return an array of ref'ed nodes.
2269 * This is separated out from calling the actual node function so that
2270 * no LORs will occur.
2272 * If there are too many nodes (ie, the number of nodes doesn't fit
2273 * within 'max_aid' entries) then the node references will be freed
2274 * and an error will be returned.
2276 * The responsibility of allocating and freeing "ni_arr" is up to
2277 * the caller.
2280 ieee80211_iterate_nt(struct ieee80211_node_table *nt,
2281 struct ieee80211_node **ni_arr, uint16_t max_aid)
2283 u_int gen;
2284 int i, j, ret;
2285 struct ieee80211_node *ni;
2287 IEEE80211_NODE_ITERATE_LOCK(nt);
2288 IEEE80211_NODE_LOCK(nt);
2290 gen = ++nt->nt_scangen;
2291 i = ret = 0;
2294 * We simply assume here that since the node
2295 * scan generation doesn't change (as
2296 * we are holding both the node table and
2297 * node table iteration locks), we can simply
2298 * assign it to the node here.
2300 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2301 if (i >= max_aid) {
2302 ret = E2BIG;
2303 ic_printf(nt->nt_ic, "Node array overflow: max=%u",
2304 max_aid);
2305 break;
2307 ni_arr[i] = ieee80211_ref_node(ni);
2308 ni_arr[i]->ni_scangen = gen;
2309 i++;
2313 * It's safe to unlock here.
2315 * If we're successful, the list is returned.
2316 * If we're unsuccessful, the list is ignored
2317 * and we remove our references.
2319 * This avoids any potential LOR with
2320 * ieee80211_free_node().
2322 IEEE80211_NODE_UNLOCK(nt);
2323 IEEE80211_NODE_ITERATE_UNLOCK(nt);
2326 * If ret is non-zero, we hit some kind of error.
2327 * Rather than walking some nodes, we'll walk none
2328 * of them.
2330 if (ret) {
2331 for (j = 0; j < i; j++) {
2332 /* ieee80211_free_node() locks by itself */
2333 ieee80211_free_node(ni_arr[j]);
2337 return (ret);
2341 * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2342 * reference in the source.
2344 * Note that this fetches 'max_aid' from the first VAP, rather than finding
2345 * the largest max_aid from all VAPs.
2347 void
2348 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2349 ieee80211_iter_func *f, void *arg)
2351 struct ieee80211_node **ni_arr;
2352 size_t size;
2353 int i;
2354 uint16_t max_aid;
2355 struct ieee80211vap *vap;
2357 /* Overdoing it default */
2358 max_aid = IEEE80211_AID_MAX;
2360 /* Handle the case of there being no vaps just yet */
2361 vap = TAILQ_FIRST(&nt->nt_ic->ic_vaps);
2362 if (vap != NULL)
2363 max_aid = vap->iv_max_aid;
2365 size = max_aid * sizeof(struct ieee80211_node *);
2366 #if defined(__DragonFly__)
2367 ni_arr = (struct ieee80211_node **) kmalloc(size, M_80211_NODE,
2368 M_INTWAIT | M_ZERO);
2369 #else
2370 ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
2371 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2372 #endif
2373 if (ni_arr == NULL)
2374 return;
2377 * If this fails, the node table won't have any
2378 * valid entries - ieee80211_iterate_nt() frees
2379 * the references to them. So don't try walking
2380 * the table; just skip to the end and free the
2381 * temporary memory.
2383 if (ieee80211_iterate_nt(nt, ni_arr, max_aid) != 0)
2384 goto done;
2386 for (i = 0; i < max_aid; i++) {
2387 if (ni_arr[i] == NULL) /* end of the list */
2388 break;
2389 (*f)(arg, ni_arr[i]);
2390 /* ieee80211_free_node() locks by itself */
2391 ieee80211_free_node(ni_arr[i]);
2394 done:
2395 IEEE80211_FREE(ni_arr, M_80211_NODE);
2398 void
2399 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2401 kprintf("0x%p: mac %s refcnt %d\n", ni,
2402 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2403 kprintf("\tscangen %u authmode %u flags 0x%x\n",
2404 ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2405 kprintf("\tassocid 0x%x txpower %u vlan %u\n",
2406 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2407 kprintf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2408 ni->ni_txseqs[IEEE80211_NONQOS_TID],
2409 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2410 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2411 ni->ni_rxfragstamp);
2412 kprintf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2413 node_getrssi(ni), ni->ni_noise,
2414 ni->ni_intval, ni->ni_capinfo);
2415 kprintf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2416 ether_sprintf(ni->ni_bssid),
2417 ni->ni_esslen, ni->ni_essid,
2418 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2419 kprintf("\tinact %u inact_reload %u txrate %u\n",
2420 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2421 kprintf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2422 ni->ni_htcap, ni->ni_htparam,
2423 ni->ni_htctlchan, ni->ni_ht2ndchan);
2424 kprintf("\thtopmode %x htstbc %x chw %u\n",
2425 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2428 void
2429 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2431 ieee80211_iterate_nodes(nt,
2432 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2435 static void
2436 ieee80211_notify_erp_locked(struct ieee80211com *ic)
2438 struct ieee80211vap *vap;
2440 IEEE80211_LOCK_ASSERT(ic);
2442 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2443 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2444 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2447 void
2448 ieee80211_notify_erp(struct ieee80211com *ic)
2450 IEEE80211_LOCK(ic);
2451 ieee80211_notify_erp_locked(ic);
2452 IEEE80211_UNLOCK(ic);
2456 * Handle a station joining an 11g network.
2458 static void
2459 ieee80211_node_join_11g(struct ieee80211_node *ni)
2461 struct ieee80211com *ic = ni->ni_ic;
2463 IEEE80211_LOCK_ASSERT(ic);
2466 * Station isn't capable of short slot time. Bump
2467 * the count of long slot time stations and disable
2468 * use of short slot time. Note that the actual switch
2469 * over to long slot time use may not occur until the
2470 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2472 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2473 ic->ic_longslotsta++;
2474 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2475 "station needs long slot time, count %d",
2476 ic->ic_longslotsta);
2477 /* XXX vap's w/ conflicting needs won't work */
2478 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2480 * Don't force slot time when switched to turbo
2481 * mode as non-ERP stations won't be present; this
2482 * need only be done when on the normal G channel.
2484 ieee80211_set_shortslottime(ic, 0);
2488 * If the new station is not an ERP station
2489 * then bump the counter and enable protection
2490 * if configured.
2492 if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2493 ic->ic_nonerpsta++;
2494 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2495 "station is !ERP, %d non-ERP stations associated",
2496 ic->ic_nonerpsta);
2498 * If station does not support short preamble
2499 * then we must enable use of Barker preamble.
2501 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2502 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2503 "%s", "station needs long preamble");
2504 ic->ic_flags |= IEEE80211_F_USEBARKER;
2505 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2508 * If protection is configured and this is the first
2509 * indication we should use protection, enable it.
2511 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2512 ic->ic_nonerpsta == 1 &&
2513 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2514 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2515 "%s: enable use of protection\n", __func__);
2516 ic->ic_flags |= IEEE80211_F_USEPROT;
2517 ieee80211_notify_erp_locked(ic);
2519 } else
2520 ni->ni_flags |= IEEE80211_NODE_ERP;
2523 void
2524 ieee80211_node_join(struct ieee80211_node *ni, int resp)
2526 struct ieee80211com *ic = ni->ni_ic;
2527 struct ieee80211vap *vap = ni->ni_vap;
2528 int newassoc;
2530 if (ni->ni_associd == 0) {
2531 uint16_t aid;
2533 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2535 * It would be good to search the bitmap
2536 * more efficiently, but this will do for now.
2538 for (aid = 1; aid < vap->iv_max_aid; aid++) {
2539 if (!IEEE80211_AID_ISSET(vap, aid))
2540 break;
2542 if (aid >= vap->iv_max_aid) {
2543 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2544 ieee80211_node_leave(ni);
2545 return;
2547 ni->ni_associd = aid | 0xc000;
2548 ni->ni_jointime = time_uptime;
2549 IEEE80211_LOCK(ic);
2550 IEEE80211_AID_SET(vap, ni->ni_associd);
2551 vap->iv_sta_assoc++;
2552 ic->ic_sta_assoc++;
2554 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2555 ieee80211_ht_node_join(ni);
2556 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2557 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2558 ieee80211_node_join_11g(ni);
2559 IEEE80211_UNLOCK(ic);
2561 newassoc = 1;
2562 } else
2563 newassoc = 0;
2565 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2566 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2567 IEEE80211_NODE_AID(ni),
2568 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2569 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2570 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2571 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2572 ni->ni_flags & IEEE80211_NODE_HT ?
2573 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2574 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2575 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2576 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2577 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2578 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2579 ", fast-frames" : "",
2580 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2581 ", turbo" : ""
2584 ieee80211_node_setuptxparms(ni);
2585 ieee80211_ratectl_node_init(ni);
2586 /* give driver a chance to setup state like ni_txrate */
2587 if (ic->ic_newassoc != NULL)
2588 ic->ic_newassoc(ni, newassoc);
2589 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2590 /* tell the authenticator about new station */
2591 if (vap->iv_auth->ia_node_join != NULL)
2592 vap->iv_auth->ia_node_join(ni);
2593 ieee80211_notify_node_join(ni,
2594 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2597 static void
2598 disable_protection(struct ieee80211com *ic)
2600 KASSERT(ic->ic_nonerpsta == 0 &&
2601 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2602 ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2603 ic->ic_flags_ext));
2605 ic->ic_flags &= ~IEEE80211_F_USEPROT;
2606 /* XXX verify mode? */
2607 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2608 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2609 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2611 ieee80211_notify_erp_locked(ic);
2615 * Handle a station leaving an 11g network.
2617 static void
2618 ieee80211_node_leave_11g(struct ieee80211_node *ni)
2620 struct ieee80211com *ic = ni->ni_ic;
2622 IEEE80211_LOCK_ASSERT(ic);
2624 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2625 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2626 ic->ic_bsschan->ic_flags));
2629 * If a long slot station do the slot time bookkeeping.
2631 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2632 KASSERT(ic->ic_longslotsta > 0,
2633 ("bogus long slot station count %d", ic->ic_longslotsta));
2634 ic->ic_longslotsta--;
2635 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2636 "long slot time station leaves, count now %d",
2637 ic->ic_longslotsta);
2638 if (ic->ic_longslotsta == 0) {
2640 * Re-enable use of short slot time if supported
2641 * and not operating in IBSS mode (per spec).
2643 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2644 ic->ic_opmode != IEEE80211_M_IBSS) {
2645 IEEE80211_DPRINTF(ni->ni_vap,
2646 IEEE80211_MSG_ASSOC,
2647 "%s: re-enable use of short slot time\n",
2648 __func__);
2649 ieee80211_set_shortslottime(ic, 1);
2654 * If a non-ERP station do the protection-related bookkeeping.
2656 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2657 KASSERT(ic->ic_nonerpsta > 0,
2658 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2659 ic->ic_nonerpsta--;
2660 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2661 "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2662 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2663 " (non-ERP sta present)" : "");
2664 if (ic->ic_nonerpsta == 0 &&
2665 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2666 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2667 "%s: disable use of protection\n", __func__);
2668 disable_protection(ic);
2674 * Time out presence of an overlapping bss with non-ERP
2675 * stations. When operating in hostap mode we listen for
2676 * beacons from other stations and if we identify a non-ERP
2677 * station is present we enable protection. To identify
2678 * when all non-ERP stations are gone we time out this
2679 * condition.
2681 static void
2682 ieee80211_erp_timeout(struct ieee80211com *ic)
2685 IEEE80211_LOCK_ASSERT(ic);
2687 if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2688 ieee80211_time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2689 #if 0
2690 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2691 "%s", "age out non-ERP sta present on channel");
2692 #endif
2693 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2694 if (ic->ic_nonerpsta == 0)
2695 disable_protection(ic);
2700 * Handle bookkeeping for station deauthentication/disassociation
2701 * when operating as an ap.
2703 void
2704 ieee80211_node_leave(struct ieee80211_node *ni)
2706 struct ieee80211com *ic = ni->ni_ic;
2707 struct ieee80211vap *vap = ni->ni_vap;
2708 struct ieee80211_node_table *nt = ni->ni_table;
2710 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2711 "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2713 KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2714 ("unexpected operating mode %u", vap->iv_opmode));
2716 * If node wasn't previously associated all
2717 * we need to do is reclaim the reference.
2719 /* XXX ibss mode bypasses 11g and notification */
2720 if (ni->ni_associd == 0)
2721 goto done;
2723 * Tell the authenticator the station is leaving.
2724 * Note that we must do this before yanking the
2725 * association id as the authenticator uses the
2726 * associd to locate it's state block.
2728 if (vap->iv_auth->ia_node_leave != NULL)
2729 vap->iv_auth->ia_node_leave(ni);
2731 IEEE80211_LOCK(ic);
2732 IEEE80211_AID_CLR(vap, ni->ni_associd);
2733 vap->iv_sta_assoc--;
2734 ic->ic_sta_assoc--;
2736 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2737 ieee80211_ht_node_leave(ni);
2738 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2739 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2740 ieee80211_node_leave_11g(ni);
2741 IEEE80211_UNLOCK(ic);
2743 * Cleanup station state. In particular clear various
2744 * state that might otherwise be reused if the node
2745 * is reused before the reference count goes to zero
2746 * (and memory is reclaimed).
2748 ieee80211_sta_leave(ni);
2749 done:
2751 * Remove the node from any table it's recorded in and
2752 * drop the caller's reference. Removal from the table
2753 * is important to insure the node is not reprocessed
2754 * for inactivity.
2756 if (nt != NULL) {
2757 IEEE80211_NODE_LOCK(nt);
2758 node_reclaim(nt, ni);
2759 IEEE80211_NODE_UNLOCK(nt);
2760 } else
2761 ieee80211_free_node(ni);
2764 struct rssiinfo {
2765 struct ieee80211vap *vap;
2766 int rssi_samples;
2767 uint32_t rssi_total;
2770 static void
2771 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2773 struct rssiinfo *info = arg;
2774 struct ieee80211vap *vap = ni->ni_vap;
2775 int8_t rssi;
2777 if (info->vap != vap)
2778 return;
2779 /* only associated stations */
2780 if (ni->ni_associd == 0)
2781 return;
2782 rssi = vap->iv_ic->ic_node_getrssi(ni);
2783 if (rssi != 0) {
2784 info->rssi_samples++;
2785 info->rssi_total += rssi;
2789 static void
2790 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2792 struct rssiinfo *info = arg;
2793 struct ieee80211vap *vap = ni->ni_vap;
2794 int8_t rssi;
2796 if (info->vap != vap)
2797 return;
2798 /* only neighbors */
2799 /* XXX check bssid */
2800 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2801 return;
2802 rssi = vap->iv_ic->ic_node_getrssi(ni);
2803 if (rssi != 0) {
2804 info->rssi_samples++;
2805 info->rssi_total += rssi;
2809 #ifdef IEEE80211_SUPPORT_MESH
2810 static void
2811 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
2813 struct rssiinfo *info = arg;
2814 struct ieee80211vap *vap = ni->ni_vap;
2815 int8_t rssi;
2817 if (info->vap != vap)
2818 return;
2819 /* only neighbors that peered successfully */
2820 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
2821 return;
2822 rssi = vap->iv_ic->ic_node_getrssi(ni);
2823 if (rssi != 0) {
2824 info->rssi_samples++;
2825 info->rssi_total += rssi;
2828 #endif /* IEEE80211_SUPPORT_MESH */
2830 int8_t
2831 ieee80211_getrssi(struct ieee80211vap *vap)
2833 #define NZ(x) ((x) == 0 ? 1 : (x))
2834 struct ieee80211com *ic = vap->iv_ic;
2835 struct rssiinfo info;
2837 info.rssi_total = 0;
2838 info.rssi_samples = 0;
2839 info.vap = vap;
2840 switch (vap->iv_opmode) {
2841 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
2842 case IEEE80211_M_AHDEMO: /* average of all neighbors */
2843 ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2844 break;
2845 case IEEE80211_M_HOSTAP: /* average of all associated stations */
2846 ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2847 break;
2848 #ifdef IEEE80211_SUPPORT_MESH
2849 case IEEE80211_M_MBSS: /* average of all mesh neighbors */
2850 ieee80211_iterate_nodes(&ic->ic_sta, get_mesh_rssi, &info);
2851 break;
2852 #endif
2853 case IEEE80211_M_MONITOR: /* XXX */
2854 case IEEE80211_M_STA: /* use stats from associated ap */
2855 default:
2856 if (vap->iv_bss != NULL)
2857 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2858 info.rssi_samples = 1;
2859 break;
2861 return info.rssi_total / NZ(info.rssi_samples);
2862 #undef NZ
2865 void
2866 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2869 if (vap->iv_bss == NULL) /* NB: shouldn't happen */
2870 return;
2871 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2872 /* for non-station mode return avg'd rssi accounting */
2873 if (vap->iv_opmode != IEEE80211_M_STA)
2874 *rssi = ieee80211_getrssi(vap);