2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2005 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.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2 as published by the Free
19 * Software Foundation.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * $FreeBSD: src/sys/net80211/ieee80211_node.c,v 1.48.2.12 2006/07/10 00:46:27 sam Exp $
33 * $DragonFly: src/sys/netproto/802_11/wlan/ieee80211_node.c,v 1.18 2008/04/20 13:44:26 swildner Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
42 #include <sys/socket.h>
45 #include <net/if_arp.h>
46 #include <net/if_media.h>
47 #include <net/ethernet.h>
49 #include <netproto/802_11/ieee80211_var.h>
54 * Association id's are managed with a bit vector.
56 #define IEEE80211_AID_SET(b, w) \
57 ((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
58 #define IEEE80211_AID_CLR(b, w) \
59 ((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
60 #define IEEE80211_AID_ISSET(b, w) \
61 ((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
63 #ifdef IEEE80211_DEBUG_REFCNT
64 #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
66 #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
69 static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
70 static void node_cleanup(struct ieee80211_node *);
71 static void node_free(struct ieee80211_node *);
72 static uint8_t node_getrssi(const struct ieee80211_node *);
74 static void ieee80211_setup_node(struct ieee80211_node_table *,
75 struct ieee80211_node *, const uint8_t *);
76 static void _ieee80211_free_node(struct ieee80211_node *);
77 static void ieee80211_free_allnodes(struct ieee80211_node_table *);
79 static void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
80 static void ieee80211_timeout_stations(struct ieee80211_node_table *);
82 static void ieee80211_set_tim(struct ieee80211_node *, int set);
84 static void ieee80211_node_table_init(struct ieee80211com *ic,
85 struct ieee80211_node_table *nt, const char *name,
86 int inact, int keyixmax,
87 void (*timeout)(struct ieee80211_node_table *));
88 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
90 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
93 ieee80211_node_attach(struct ieee80211com *ic)
95 ic->ic_node_alloc = node_alloc;
96 ic->ic_node_free = node_free;
97 ic->ic_node_cleanup = node_cleanup;
98 ic->ic_node_getrssi = node_getrssi;
100 /* default station inactivity timer setings */
101 ic->ic_inact_init = IEEE80211_INACT_INIT;
102 ic->ic_inact_auth = IEEE80211_INACT_AUTH;
103 ic->ic_inact_run = IEEE80211_INACT_RUN;
104 ic->ic_inact_probe = IEEE80211_INACT_PROBE;
106 /* NB: driver should override */
107 ic->ic_max_aid = IEEE80211_AID_DEF;
108 ic->ic_set_tim = ieee80211_set_tim;
112 ieee80211_node_lateattach(struct ieee80211com *ic)
114 struct ieee80211_rsnparms *rsn;
116 if (ic->ic_max_aid > IEEE80211_AID_MAX)
117 ic->ic_max_aid = IEEE80211_AID_MAX;
120 kmalloc(howmany(ic->ic_max_aid, 32) * sizeof(uint32_t),
121 M_DEVBUF, M_WAITOK | M_ZERO);
123 /* XXX defer until using hostap/ibss mode */
124 ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(uint8_t);
125 ic->ic_tim_bitmap = kmalloc(ic->ic_tim_len, M_DEVBUF,
128 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
129 IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix,
130 ieee80211_timeout_stations);
131 ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
132 IEEE80211_INACT_SCAN, 0,
133 ieee80211_timeout_scan_candidates);
135 ieee80211_reset_bss(ic);
137 * Setup "global settings" in the bss node so that
138 * each new station automatically inherits them.
140 rsn = &ic->ic_bss->ni_rsn;
141 /* WEP, TKIP, and AES-CCM are always supported */
142 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
143 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
144 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
145 if (ic->ic_caps & IEEE80211_C_AES)
146 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
147 if (ic->ic_caps & IEEE80211_C_CKIP)
148 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
150 * Default unicast cipher to WEP for 802.1x use. If
151 * WPA is enabled the management code will set these
154 rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
155 rsn->rsn_ucastkeylen = 104 / NBBY;
157 * WPA says the multicast cipher is the lowest unicast
158 * cipher supported. But we skip WEP which would
159 * otherwise be used based on this criteria.
161 rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
162 rsn->rsn_mcastkeylen = 128 / NBBY;
165 * We support both WPA-PSK and 802.1x; the one used
166 * is determined by the authentication mode and the
167 * setting of the PSK state.
169 rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
170 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
172 ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
176 ieee80211_node_detach(struct ieee80211com *ic)
178 if (ic->ic_bss != NULL) {
179 ieee80211_free_node(ic->ic_bss);
182 ieee80211_node_table_cleanup(&ic->ic_scan);
183 ieee80211_node_table_cleanup(&ic->ic_sta);
184 if (ic->ic_aid_bitmap != NULL) {
185 kfree(ic->ic_aid_bitmap, M_DEVBUF);
186 ic->ic_aid_bitmap = NULL;
188 if (ic->ic_tim_bitmap != NULL) {
189 kfree(ic->ic_tim_bitmap, M_DEVBUF);
190 ic->ic_tim_bitmap = NULL;
195 * Port authorize/unauthorize interfaces for use by an authenticator.
199 ieee80211_node_authorize(struct ieee80211_node *ni)
201 struct ieee80211com *ic = ni->ni_ic;
203 ni->ni_flags |= IEEE80211_NODE_AUTH;
204 ni->ni_inact_reload = ic->ic_inact_run;
208 ieee80211_node_unauthorize(struct ieee80211_node *ni)
210 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
214 * Set/change the channel. The rate set is also updated as
215 * to insure a consistent view by drivers.
218 ieee80211_set_chan(struct ieee80211com *ic,
219 struct ieee80211_node *ni, struct ieee80211_channel *chan)
221 if (chan == IEEE80211_CHAN_ANYC) /* XXX while scanning */
222 chan = ic->ic_curchan;
224 ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
228 * AP scanning support.
231 #ifdef IEEE80211_DEBUG
233 dump_chanlist(const u_char chans[])
239 for (i = 0; i < IEEE80211_CHAN_MAX; i++)
240 if (isset(chans, i)) {
241 kprintf("%s%u", sep, i);
245 #endif /* IEEE80211_DEBUG */
248 * Initialize the channel set to scan based on the
249 * of available channels and the current PHY mode.
252 ieee80211_reset_scan(struct ieee80211com *ic)
255 /* XXX ic_des_chan should be handled with ic_chan_active */
256 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
257 memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
258 setbit(ic->ic_chan_scan,
259 ieee80211_chan2ieee(ic, ic->ic_des_chan));
261 memcpy(ic->ic_chan_scan, ic->ic_chan_active,
262 sizeof(ic->ic_chan_active));
263 #ifdef IEEE80211_DEBUG
264 if (ieee80211_msg_scan(ic)) {
265 kprintf("%s: scan set:", __func__);
266 dump_chanlist(ic->ic_chan_scan);
267 kprintf(" start chan %u\n",
268 ieee80211_chan2ieee(ic, ic->ic_curchan));
270 #endif /* IEEE80211_DEBUG */
274 * Begin an active scan.
277 ieee80211_begin_scan(struct ieee80211com *ic, int reset)
279 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
282 * In all but hostap mode scanning starts off in
283 * an active mode before switching to passive.
285 if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
286 ic->ic_flags |= IEEE80211_F_ASCAN;
287 ic->ic_stats.is_scan_active++;
289 ic->ic_stats.is_scan_passive++;
290 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
291 "begin %s scan in %s mode\n",
292 (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive",
293 ieee80211_phymode_name[ic->ic_curmode]);
295 * Clear scan state and flush any previously seen AP's.
297 ieee80211_reset_scan(ic);
299 ieee80211_free_allnodes(&ic->ic_scan);
301 ic->ic_flags |= IEEE80211_F_SCAN;
303 /* Scan the next channel. */
304 ieee80211_next_scan(ic);
308 * Switch to the next channel marked for scanning.
311 ieee80211_next_scan(struct ieee80211com *ic)
313 struct ieee80211_channel *chan;
316 * Insure any previous mgt frame timeouts don't fire.
317 * This assumes the driver does the right thing in
318 * flushing anything queued in the driver and below.
320 ic->ic_mgt_timer = 0;
321 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
323 chan = ic->ic_curchan;
325 if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
326 chan = &ic->ic_channels[0];
327 if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
328 clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
329 ieee80211_set_scanchan(ic, chan);
330 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
333 } while (chan != ic->ic_curchan);
334 ieee80211_end_scan(ic);
339 * Probe the curent channel, if allowed, while scanning.
340 * If the channel is not marked passive-only then send
341 * a probe request immediately. Otherwise mark state and
342 * listen for beacons on the channel; if we receive something
343 * then we'll transmit a probe request.
346 ieee80211_probe_curchan(struct ieee80211com *ic, int force)
348 struct ifnet *ifp = ic->ic_ifp;
350 if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) {
352 * XXX send both broadcast+directed probe request
354 ieee80211_send_probereq(ic->ic_bss,
355 ic->ic_myaddr, ifp->if_broadcastaddr,
356 ifp->if_broadcastaddr,
357 ic->ic_des_essid, ic->ic_des_esslen,
358 ic->ic_opt_ie, ic->ic_opt_ie_len);
360 ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
364 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
366 /* propagate useful state */
367 nbss->ni_authmode = obss->ni_authmode;
368 nbss->ni_txpower = obss->ni_txpower;
369 nbss->ni_vlan = obss->ni_vlan;
370 nbss->ni_rsn = obss->ni_rsn;
371 ieee80211_ratectl_data_dup(obss, nbss);
372 /* XXX statistics? */
376 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
378 struct ieee80211_node_table *nt;
379 struct ieee80211_node *ni;
381 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
383 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
384 "%s: creating ibss\n", __func__);
387 * Create the station/neighbor table. Note that for adhoc
388 * mode we make the initial inactivity timer longer since
389 * we create nodes only through discovery and they typically
390 * are long-lived associations.
393 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
394 nt->nt_name = "station";
395 nt->nt_inact_init = ic->ic_inact_init;
397 nt->nt_name = "neighbor";
398 nt->nt_inact_init = ic->ic_inact_run;
401 ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
406 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
407 ni->ni_esslen = ic->ic_des_esslen;
408 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
409 copy_bss(ni, ic->ic_bss);
410 ni->ni_intval = ic->ic_bintval;
411 if (ic->ic_flags & IEEE80211_F_PRIVACY)
412 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
413 if (ic->ic_phytype == IEEE80211_T_FH) {
414 ni->ni_fhdwell = 200; /* XXX */
417 if (ic->ic_opmode == IEEE80211_M_IBSS) {
418 ic->ic_flags |= IEEE80211_F_SIBSS;
419 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
420 if (ic->ic_flags & IEEE80211_F_DESBSSID) {
421 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
423 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
424 /* Clear group bit, add local bit */
425 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
427 } else if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
428 if (ic->ic_flags & IEEE80211_F_DESBSSID)
429 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
431 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
434 * Fix the channel and related attributes.
436 ieee80211_set_chan(ic, ni, chan);
437 ic->ic_curchan = chan;
438 ic->ic_curmode = ieee80211_chan2mode(ic, chan);
440 * Do mode-specific rate setup.
442 ieee80211_set_basicrates(&ni->ni_rates, ic->ic_curmode,
443 ic->ic_flags & IEEE80211_F_PUREG);
445 ieee80211_sta_join(ic, ieee80211_ref_node(ni));
449 ieee80211_reset_bss(struct ieee80211com *ic)
451 struct ieee80211_node *ni, *obss;
453 ieee80211_node_table_reset(&ic->ic_scan);
454 ieee80211_node_table_reset(&ic->ic_sta);
456 ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
457 KASSERT(ni != NULL, ("unable to setup initial BSS node"));
459 ic->ic_bss = ieee80211_ref_node(ni);
462 ni->ni_intval = ic->ic_bintval;
463 ieee80211_free_node(obss);
465 ic->ic_nbasicrates = 0;
469 #define STA_FAILS_MAX 2 /* assoc failures before ignored */
472 ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
477 IEEE80211_PRINT_NODERATES(ic, ni, 0);
480 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
482 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
483 ni->ni_chan != ic->ic_des_chan)
485 if (ic->ic_opmode == IEEE80211_M_IBSS) {
486 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
489 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
492 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
493 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
496 /* XXX does this mean privacy is supported or required? */
497 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
500 rate = ieee80211_fix_rate(ni,
501 IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE, 1);
502 if (rate & IEEE80211_RATE_BASIC)
504 if (ic->ic_des_esslen != 0 &&
505 (ni->ni_esslen != ic->ic_des_esslen ||
506 memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
508 if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
509 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
511 if (ni->ni_fails >= STA_FAILS_MAX)
513 #ifdef IEEE80211_DEBUG
514 if (ieee80211_msg_scan(ic)) {
516 fail & 0x40 ? '=' : fail & 0x80 ? '^' : fail ? '-' : '+',
517 ni->ni_macaddr, ":");
518 kprintf(" %6D%c", ni->ni_bssid, ":",
519 fail & 0x20 ? '!' : ' ');
520 kprintf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
521 fail & 0x01 ? '!' : ' ');
522 kprintf(" %+4d", ni->ni_rssi);
523 kprintf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
524 fail & 0x08 ? '!' : ' ');
526 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
527 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
529 fail & 0x02 ? '!' : ' ');
531 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
533 fail & 0x04 ? '!' : ' ');
534 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
535 kprintf("%s\n", fail & 0x10 ? "!" : "");
541 static __inline uint8_t
542 maxrate(const struct ieee80211_node *ni)
544 const struct ieee80211_rateset *rs = &ni->ni_rates;
545 /* NB: assumes rate set is sorted (happens on frame receive) */
546 return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
550 * Compare the capabilities of two nodes and decide which is
551 * more desirable (return >0 if a is considered better). Note
552 * that we assume compatibility/usability has already been checked
553 * so we don't need to (e.g. validate whether privacy is supported).
554 * Used to select the best scan candidate for association in a BSS.
557 ieee80211_node_compare(struct ieee80211com *ic,
558 const struct ieee80211_node *a,
559 const struct ieee80211_node *b)
561 #define ABS(a) ((a) < 0 ? -(a) : (a))
563 uint8_t rssia, rssib;
566 /* privacy support preferred */
567 if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
568 (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
570 if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
571 (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
574 /* compare count of previous failures */
575 weight = b->ni_fails - a->ni_fails;
579 rssia = ic->ic_node_getrssi(a);
580 rssib = ic->ic_node_getrssi(b);
581 if (ABS(rssib - rssia) < 5) {
582 /* best/max rate preferred if signal level close enough XXX */
587 /* XXX use freq for channel preference */
588 /* for now just prefer 5Ghz band to all other bands */
589 if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
590 !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
592 if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
593 IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
596 /* all things being equal, use signal level */
597 return rssia - rssib;
602 * Mark an ongoing scan stopped.
605 ieee80211_cancel_scan(struct ieee80211com *ic)
608 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
610 (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive");
612 ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
613 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
617 * Complete a scan of potential channels.
620 ieee80211_end_scan(struct ieee80211com *ic)
622 struct ieee80211_node_table *nt = &ic->ic_scan;
623 struct ieee80211_node *ni, *selbs;
625 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
627 ieee80211_cancel_scan(ic);
628 ieee80211_notify_scan_done(ic);
630 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
631 uint8_t maxrssi[IEEE80211_CHAN_MAX]; /* XXX off stack? */
636 * The passive scan to look for existing AP's completed,
637 * select a channel to camp on. Identify the channels
638 * that already have one or more AP's and try to locate
639 * an unoccupied one. If that fails, pick a channel that
640 * looks to be quietest.
642 memset(maxrssi, 0, sizeof(maxrssi));
643 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
644 rssi = ic->ic_node_getrssi(ni);
645 i = ieee80211_chan2ieee(ic, ni->ni_chan);
646 if (rssi > maxrssi[i])
649 /* XXX select channel more intelligently */
651 for (i = 0; i < IEEE80211_CHAN_MAX; i++)
652 if (isset(ic->ic_chan_active, i)) {
654 * If the channel is unoccupied the max rssi
655 * should be zero; just take it. Otherwise
656 * track the channel with the lowest rssi and
657 * use that when all channels appear occupied.
659 if (maxrssi[i] == 0) {
663 if (bestchan == -1 ||
664 maxrssi[i] < maxrssi[bestchan])
667 if (bestchan != -1) {
668 ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
671 /* no suitable channel, should not happen */
675 * When manually sequencing the state machine; scan just once
676 * regardless of whether we have a candidate or not. The
677 * controlling application is expected to setup state and
678 * initiate an association.
680 if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
683 * Automatic sequencing; look for a candidate and
684 * if found join the network.
686 /* NB: unlocked read should be ok */
687 if (TAILQ_FIRST(&nt->nt_node) == NULL) {
688 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
689 "%s: no scan candidate\n", __func__);
691 if (ic->ic_opmode == IEEE80211_M_IBSS &&
692 (ic->ic_flags & IEEE80211_F_IBSSON) &&
693 ic->ic_des_esslen != 0) {
694 ieee80211_create_ibss(ic, ic->ic_ibss_chan);
698 * Decrement the failure counts so entries will be
699 * reconsidered the next time around. We really want
700 * to do this only for sta's where we've previously
703 TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
707 * Reset the list of channels to scan and start again.
709 ieee80211_reset_scan(ic);
710 ic->ic_flags |= IEEE80211_F_SCAN;
711 ieee80211_next_scan(ic);
715 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
716 "macaddr bssid chan rssi rate flag wep essid");
717 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
718 if (ieee80211_match_bss(ic, ni) == 0) {
721 else if (ieee80211_node_compare(ic, ni, selbs) > 0)
725 if (selbs != NULL) /* NB: grab ref while dropping lock */
726 ieee80211_ref_node(selbs);
729 if (!ieee80211_sta_join(ic, selbs)) {
730 ieee80211_free_node(selbs);
736 * Handle 802.11 ad hoc network merge. The
737 * convention, set by the Wireless Ethernet Compatibility Alliance
738 * (WECA), is that an 802.11 station will change its BSSID to match
739 * the "oldest" 802.11 ad hoc network, on the same channel, that
740 * has the station's desired SSID. The "oldest" 802.11 network
741 * sends beacons with the greatest TSF timestamp.
743 * The caller is assumed to validate TSF's before attempting a merge.
745 * Return !0 if the BSSID changed, 0 otherwise.
748 ieee80211_ibss_merge(struct ieee80211_node *ni)
750 struct ieee80211com *ic = ni->ni_ic;
752 if (ni == ic->ic_bss ||
753 IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
754 /* unchanged, nothing to do */
757 if (ieee80211_match_bss(ic, ni) != 0) { /* capabilities mismatch */
758 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
759 "%s: merge failed, capabilities mismatch\n", __func__);
760 ic->ic_stats.is_ibss_capmismatch++;
763 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
764 "%6D: new bssid %s: %s preamble, %s slot time%s\n", __func__,
766 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
767 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
768 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
770 return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
774 * Join the specified IBSS/BSS network. The node is assumed to
775 * be passed in with a held reference.
778 ieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
780 struct ieee80211_node *obss;
782 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
784 if (ic->ic_opmode == IEEE80211_M_IBSS ||
785 ic->ic_opmode == IEEE80211_M_STA) {
787 * Delete unusable rates; we've already checked
788 * that the negotiated rate set is acceptable.
790 ieee80211_fix_rate(selbs, IEEE80211_F_DODEL, 1);
791 IEEE80211_PRINT_NODERATES(ic, selbs, 0);
793 if (ic->ic_opmode == IEEE80211_M_IBSS) {
794 struct ieee80211_node_table *nt;
797 * Fillin the neighbor table; it will already
798 * exist if we are simply switching mastership.
799 * XXX ic_sta always setup so this is unnecessary?
802 nt->nt_name = "neighbor";
803 nt->nt_inact_init = ic->ic_inact_run;
808 * Committed to selbs, setup state.
811 ic->ic_bss = selbs; /* NB: caller assumed to bump refcnt */
813 copy_bss(selbs, obss);
814 ieee80211_free_node(obss);
817 * Set the erp state (mostly the slot time) to deal with
818 * the auto-select case; this should be redundant if the
821 ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
822 ic->ic_curchan = selbs->ni_chan;
823 ieee80211_reset_erp(ic);
824 ieee80211_wme_initparams(ic);
827 * Copy BSS basic rate set.
830 ieee80211_copy_basicrates(&ic->ic_sup_rates[ic->ic_curmode],
832 #ifdef IEEE80211_DEBUG
833 if (ieee80211_msg(ic, IEEE80211_MSG_XRATE)) {
834 ieee80211_note(ic, "number basic rates %d, "
835 "supported rates (mode %d): ",
836 ic->ic_nbasicrates, ic->ic_curmode);
837 ieee80211_print_rateset(&ic->ic_sup_rates[ic->ic_curmode]);
842 if (ic->ic_opmode == IEEE80211_M_STA)
843 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
845 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
850 * Leave the specified IBSS/BSS network. The node is assumed to
851 * be passed in with a held reference.
854 ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
856 ic->ic_node_cleanup(ni);
857 ieee80211_notify_node_leave(ic, ni);
860 static struct ieee80211_node *
861 node_alloc(struct ieee80211_node_table *nt)
863 struct ieee80211_node *ni;
865 ni = kmalloc(sizeof(struct ieee80211_node), M_80211_NODE,
871 * Reclaim any resources in a node and reset any critical
872 * state. Typically nodes are free'd immediately after,
873 * but in some cases the storage may be reused so we need
874 * to insure consistent state (should probably fix that).
877 node_cleanup(struct ieee80211_node *ni)
879 #define N(a) (sizeof(a)/sizeof(a[0]))
880 struct ieee80211com *ic = ni->ni_ic;
883 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
885 /* NB: preserve ni_table */
886 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
888 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
889 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
890 "[%6D] power save mode off, %u sta's in ps mode\n",
891 ni->ni_macaddr, ":", ic->ic_ps_sta);
894 * Clear AREF flag that marks the authorization refcnt bump
895 * has happened. This is probably not needed as the node
896 * should always be removed from the table so not found but
897 * do it just in case.
899 ni->ni_flags &= ~IEEE80211_NODE_AREF;
902 * Drain power save queue and, if needed, clear TIM.
904 IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
905 if (qlen != 0 && ic->ic_set_tim != NULL)
906 ic->ic_set_tim(ni, 0);
909 if (ni->ni_challenge != NULL) {
910 kfree(ni->ni_challenge, M_DEVBUF);
911 ni->ni_challenge = NULL;
914 * Preserve SSID, WPA, and WME ie's so the bss node is
915 * reusable during a re-auth/re-assoc state transition.
916 * If we remove these data they will not be recreated
917 * because they come from a probe-response or beacon frame
918 * which cannot be expected prior to the association-response.
919 * This should not be an issue when operating in other modes
920 * as stations leaving always go through a full state transition
921 * which will rebuild this state.
923 * XXX does this leave us open to inheriting old state?
925 for (i = 0; i < N(ni->ni_rxfrag); i++)
926 if (ni->ni_rxfrag[i] != NULL) {
927 m_freem(ni->ni_rxfrag[i]);
928 ni->ni_rxfrag[i] = NULL;
931 * Must be careful here to remove any key map entry w/o a LOR.
933 ieee80211_node_delucastkey(ni);
938 node_free(struct ieee80211_node *ni)
940 struct ieee80211com *ic = ni->ni_ic;
942 ic->ic_node_cleanup(ni);
943 if (ni->ni_wpa_ie != NULL)
944 kfree(ni->ni_wpa_ie, M_DEVBUF);
945 if (ni->ni_wme_ie != NULL)
946 kfree(ni->ni_wme_ie, M_DEVBUF);
947 IEEE80211_NODE_SAVEQ_DESTROY(ni);
948 kfree(ni, M_80211_NODE);
952 node_getrssi(const struct ieee80211_node *ni)
958 ieee80211_setup_node(struct ieee80211_node_table *nt,
959 struct ieee80211_node *ni, const uint8_t *macaddr)
961 struct ieee80211com *ic = nt->nt_ic;
964 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
966 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
967 "%s %p<%6D> in %s table\n", __func__, ni,
968 macaddr, ":", nt->nt_name);
970 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
971 hash = IEEE80211_NODE_HASH(macaddr);
972 ieee80211_node_initref(ni); /* mark referenced */
973 ni->ni_chan = IEEE80211_CHAN_ANYC;
974 ni->ni_authmode = IEEE80211_AUTH_OPEN;
975 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
976 ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
977 ni->ni_inact_reload = nt->nt_inact_init;
978 ni->ni_inact = ni->ni_inact_reload;
979 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
981 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
982 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
986 ieee80211_ratectl_data_alloc(ni);
989 struct ieee80211_node *
990 ieee80211_alloc_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
992 struct ieee80211com *ic = nt->nt_ic;
993 struct ieee80211_node *ni;
995 ni = ic->ic_node_alloc(nt);
997 ieee80211_setup_node(nt, ni, macaddr);
999 ic->ic_stats.is_rx_nodealloc++;
1004 * Craft a temporary node suitable for sending a management frame
1005 * to the specified station. We craft only as much state as we
1006 * need to do the work since the node will be immediately reclaimed
1007 * once the send completes.
1009 struct ieee80211_node *
1010 ieee80211_tmp_node(struct ieee80211com *ic, const uint8_t *macaddr)
1012 struct ieee80211_node *ni;
1014 ni = ic->ic_node_alloc(&ic->ic_sta);
1016 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1017 "%s %p<%6D>\n", __func__, ni, macaddr, ":");
1019 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1020 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
1021 ieee80211_node_initref(ni); /* mark referenced */
1022 ni->ni_txpower = ic->ic_bss->ni_txpower;
1023 /* NB: required by ieee80211_fix_rate */
1024 ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
1025 ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
1026 IEEE80211_KEYIX_NONE);
1027 /* XXX optimize away */
1028 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1030 ni->ni_table = NULL; /* NB: pedantic */
1034 ic->ic_stats.is_rx_nodealloc++;
1039 struct ieee80211_node *
1040 ieee80211_dup_bss(struct ieee80211_node_table *nt, const uint8_t *macaddr)
1042 struct ieee80211com *ic = nt->nt_ic;
1043 struct ieee80211_node *ni;
1045 ni = ic->ic_node_alloc(nt);
1047 ieee80211_setup_node(nt, ni, macaddr);
1049 * Inherit from ic_bss.
1051 ni->ni_authmode = ic->ic_bss->ni_authmode;
1052 ni->ni_txpower = ic->ic_bss->ni_txpower;
1053 ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */
1054 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
1055 ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
1056 ni->ni_rsn = ic->ic_bss->ni_rsn;
1058 ic->ic_stats.is_rx_nodealloc++;
1062 static struct ieee80211_node *
1063 #ifdef IEEE80211_DEBUG_REFCNT
1064 _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1065 const uint8_t *macaddr, const char *func, int line)
1067 _ieee80211_find_node(struct ieee80211_node_table *nt,
1068 const uint8_t *macaddr)
1071 struct ieee80211_node *ni;
1074 hash = IEEE80211_NODE_HASH(macaddr);
1075 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1076 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1077 ieee80211_ref_node(ni); /* mark referenced */
1078 #ifdef IEEE80211_DEBUG_REFCNT
1079 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1080 "%s (%s:%u) %p<%6D> refcnt %d\n", __func__,
1082 ni, ni->ni_macaddr, ":",
1083 ieee80211_node_refcnt(ni));
1090 #ifdef IEEE80211_DEBUG_REFCNT
1091 #define _ieee80211_find_node(nt, mac) \
1092 _ieee80211_find_node_debug(nt, mac, func, line)
1095 struct ieee80211_node *
1096 #ifdef IEEE80211_DEBUG_REFCNT
1097 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1098 const uint8_t *macaddr, const char *func, int line)
1100 ieee80211_find_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
1103 struct ieee80211_node *ni;
1105 ASSERT_SERIALIZED(nt->nt_ic->ic_ifp->if_serializer);
1107 ni = _ieee80211_find_node(nt, macaddr);
1112 * Fake up a node; this handles node discovery in adhoc mode.
1113 * Note that for the driver's benefit we we treat this like
1114 * an association so the driver has an opportunity to setup
1115 * it's private state.
1117 struct ieee80211_node *
1118 ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
1119 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1121 struct ieee80211com *ic = nt->nt_ic;
1122 struct ieee80211_node *ni;
1124 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1125 "%s: mac<%6D>\n", __func__, macaddr, ":");
1126 ni = ieee80211_dup_bss(nt, macaddr);
1128 /* XXX no rate negotiation; just dup */
1129 ni->ni_rates = ic->ic_bss->ni_rates;
1131 ieee80211_ratectl_newassoc(ni, 1);
1133 if (ic->ic_newassoc != NULL)
1134 ic->ic_newassoc(ni, 1);
1136 /* XXX not right for 802.1x/WPA */
1137 ieee80211_node_authorize(ni);
1138 if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
1140 * Blindly propagate capabilities based on the
1141 * local configuration. In particular this permits
1142 * us to use QoS to disable ACK's.
1144 if (ic->ic_flags & IEEE80211_F_WME)
1145 ni->ni_flags |= IEEE80211_NODE_QOS;
1151 #ifdef IEEE80211_DEBUG
1153 dump_probe_beacon(uint8_t subtype, int isnew,
1154 const uint8_t mac[IEEE80211_ADDR_LEN],
1155 const struct ieee80211_scanparams *sp)
1158 kprintf("[%6D] %s%s on chan %u (bss chan %u) ",
1159 mac, ":", isnew ? "new " : "",
1160 ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
1161 sp->chan, sp->bchan);
1162 ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
1166 kprintf("[%6D] caps 0x%x bintval %u erp 0x%x",
1167 mac, ":", sp->capinfo, sp->bintval, sp->erp);
1168 if (sp->country != NULL) {
1169 #if defined(__FreeBSD__) || defined(__DragonFly__)
1170 kprintf(" country info %*D",
1171 sp->country[1], sp->country+2, " ");
1174 kprintf(" country info");
1175 for (i = 0; i < sp->country[1]; i++)
1176 kprintf(" %02x", sp->country[i+2]);
1182 #endif /* IEEE80211_DEBUG */
1185 saveie(uint8_t **iep, const uint8_t *ie)
1191 ieee80211_saveie(iep, ie);
1195 * Process a beacon or probe response frame.
1198 ieee80211_add_scan(struct ieee80211com *ic,
1199 const struct ieee80211_scanparams *sp,
1200 const struct ieee80211_frame *wh,
1201 int subtype, int rssi, int rstamp)
1203 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1204 struct ieee80211_node_table *nt = &ic->ic_scan;
1205 struct ieee80211_node *ni;
1208 ni = ieee80211_find_node(nt, wh->i_addr2);
1211 * Create a new entry.
1213 ni = ic->ic_node_alloc(nt);
1215 ic->ic_stats.is_rx_nodealloc++;
1218 ieee80211_setup_node(nt, ni, wh->i_addr2);
1220 * XXX inherit from ic_bss.
1222 ni->ni_authmode = ic->ic_bss->ni_authmode;
1223 ni->ni_txpower = ic->ic_bss->ni_txpower;
1224 ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */
1225 ieee80211_set_chan(ic, ni, ic->ic_curchan);
1226 ni->ni_rsn = ic->ic_bss->ni_rsn;
1229 #ifdef IEEE80211_DEBUG
1230 if (ieee80211_msg_scan(ic) && (ic->ic_flags & IEEE80211_F_SCAN))
1231 dump_probe_beacon(subtype, newnode, wh->i_addr2, sp);
1233 /* XXX ap beaconing multiple ssid w/ same bssid */
1234 if (sp->ssid[1] != 0 &&
1235 (ISPROBE(subtype) || ni->ni_esslen == 0)) {
1236 ni->ni_esslen = sp->ssid[1];
1237 memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
1238 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1240 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1242 ni->ni_rstamp = rstamp;
1243 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1244 ni->ni_intval = sp->bintval;
1245 ni->ni_capinfo = sp->capinfo;
1246 ni->ni_chan = &ic->ic_channels[sp->chan];
1247 ni->ni_fhdwell = sp->fhdwell;
1248 ni->ni_fhindex = sp->fhindex;
1249 ni->ni_erp = sp->erp;
1250 if (sp->tim != NULL) {
1251 struct ieee80211_tim_ie *ie =
1252 (struct ieee80211_tim_ie *) sp->tim;
1254 ni->ni_dtim_count = ie->tim_count;
1255 ni->ni_dtim_period = ie->tim_period;
1258 * Record the byte offset from the mac header to
1259 * the start of the TIM information element for
1260 * use by hardware and/or to speedup software
1261 * processing of beacon frames.
1263 ni->ni_timoff = sp->timoff;
1265 * Record optional information elements that might be
1266 * used by applications or drivers.
1268 saveie(&ni->ni_wme_ie, sp->wme);
1269 saveie(&ni->ni_wpa_ie, sp->wpa);
1271 /* NB: must be after ni_chan is setup */
1272 ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT, 0);
1275 ieee80211_free_node(ni);
1280 ieee80211_init_neighbor(struct ieee80211_node *ni,
1281 const struct ieee80211_frame *wh,
1282 const struct ieee80211_scanparams *sp)
1284 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1285 "%s: %p<%6D>\n", __func__, ni, ni->ni_macaddr, ":");
1286 ni->ni_esslen = sp->ssid[1];
1287 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1288 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1289 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1290 ni->ni_intval = sp->bintval;
1291 ni->ni_capinfo = sp->capinfo;
1292 ni->ni_chan = ni->ni_ic->ic_curchan;
1293 ni->ni_fhdwell = sp->fhdwell;
1294 ni->ni_fhindex = sp->fhindex;
1295 ni->ni_erp = sp->erp;
1296 ni->ni_timoff = sp->timoff;
1297 if (sp->wme != NULL)
1298 ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
1299 if (sp->wpa != NULL)
1300 ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
1302 /* NB: must be after ni_chan is setup */
1303 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1304 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1305 IEEE80211_F_DONEGO | IEEE80211_F_DODEL, 0);
1306 IEEE80211_PRINT_NODERATES(ni->ni_ic, ni, IEEE80211_MSG_NODE);
1310 * Do node discovery in adhoc mode on receipt of a beacon
1311 * or probe response frame. Note that for the driver's
1312 * benefit we we treat this like an association so the
1313 * driver has an opportunity to setup it's private state.
1315 struct ieee80211_node *
1316 ieee80211_add_neighbor(struct ieee80211com *ic,
1317 const struct ieee80211_frame *wh,
1318 const struct ieee80211_scanparams *sp)
1320 struct ieee80211_node *ni;
1322 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1323 "%s: mac<%s>\n", __func__, wh->i_addr2, ":");
1324 ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1326 ieee80211_init_neighbor(ni, wh, sp);
1328 ieee80211_ratectl_newassoc(ni, 1);
1330 if (ic->ic_newassoc != NULL)
1331 ic->ic_newassoc(ni, 1);
1333 /* XXX not right for 802.1x/WPA */
1334 ieee80211_node_authorize(ni);
1339 #define IS_CTL(wh) \
1340 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1341 #define IS_PSPOLL(wh) \
1342 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1344 * Locate the node for sender, track state, and then pass the
1345 * (referenced) node up to the 802.11 layer for its use. We
1346 * are required to pass some node so we fall back to ic_bss
1347 * when this frame is from an unknown sender. The 802.11 layer
1348 * knows this means the sender wasn't in the node table and
1351 struct ieee80211_node *
1352 #ifdef IEEE80211_DEBUG_REFCNT
1353 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1354 const struct ieee80211_frame_min *wh, const char *func, int line)
1356 ieee80211_find_rxnode(struct ieee80211com *ic,
1357 const struct ieee80211_frame_min *wh)
1360 struct ieee80211_node_table *nt;
1361 struct ieee80211_node *ni;
1363 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
1365 /* XXX may want scanned nodes in the neighbor table for adhoc */
1366 if (ic->ic_opmode == IEEE80211_M_STA ||
1367 ic->ic_opmode == IEEE80211_M_MONITOR ||
1368 (ic->ic_flags & IEEE80211_F_SCAN))
1372 /* XXX check ic_bss first in station mode */
1373 /* XXX 4-address frames? */
1374 if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1375 ni = _ieee80211_find_node(nt, wh->i_addr1);
1377 ni = _ieee80211_find_node(nt, wh->i_addr2);
1379 ni = ieee80211_ref_node(ic->ic_bss);
1385 * Like ieee80211_find_rxnode but use the supplied h/w
1386 * key index as a hint to locate the node in the key
1387 * mapping table. If an entry is present at the key
1388 * index we return it; otherwise do a normal lookup and
1389 * update the mapping table if the station has a unicast
1390 * key assigned to it.
1392 struct ieee80211_node *
1393 #ifdef IEEE80211_DEBUG_REFCNT
1394 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1395 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1396 const char *func, int line)
1398 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1399 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1402 struct ieee80211_node_table *nt;
1403 struct ieee80211_node *ni;
1405 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
1407 if (ic->ic_opmode == IEEE80211_M_STA ||
1408 ic->ic_opmode == IEEE80211_M_MONITOR ||
1409 (ic->ic_flags & IEEE80211_F_SCAN))
1413 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1414 ni = nt->nt_keyixmap[keyix];
1418 if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1419 ni = _ieee80211_find_node(nt, wh->i_addr1);
1421 ni = _ieee80211_find_node(nt, wh->i_addr2);
1423 ni = ieee80211_ref_node(ic->ic_bss);
1424 if (nt->nt_keyixmap != NULL) {
1426 * If the station has a unicast key cache slot
1427 * assigned update the key->node mapping table.
1429 keyix = ni->ni_ucastkey.wk_rxkeyix;
1430 /* XXX can keyixmap[keyix] != NULL? */
1431 if (keyix < nt->nt_keyixmax &&
1432 nt->nt_keyixmap[keyix] == NULL) {
1433 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1434 "%s: add key map entry %p<%6D> refcnt %d\n",
1435 __func__, ni, ni->ni_macaddr, ":",
1436 ieee80211_node_refcnt(ni)+1);
1437 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1441 ieee80211_ref_node(ni);
1450 * Return a reference to the appropriate node for sending
1451 * a data frame. This handles node discovery in adhoc networks.
1453 struct ieee80211_node *
1454 #ifdef IEEE80211_DEBUG_REFCNT
1455 ieee80211_find_txnode_debug(struct ieee80211com *ic, const uint8_t *macaddr,
1456 const char *func, int line)
1458 ieee80211_find_txnode(struct ieee80211com *ic, const uint8_t *macaddr)
1461 struct ieee80211_node_table *nt = &ic->ic_sta;
1462 struct ieee80211_node *ni;
1464 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
1467 * The destination address should be in the node table
1468 * unless this is a multicast/broadcast frame. We can
1469 * also optimize station mode operation, all frames go
1472 if (ic->ic_opmode == IEEE80211_M_STA ||
1473 IEEE80211_IS_MULTICAST(macaddr)) {
1474 ni = ieee80211_ref_node(ic->ic_bss);
1476 ni = _ieee80211_find_node(nt, macaddr);
1477 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1478 (ni != NULL && ni->ni_associd == 0)) {
1480 * Station is not associated; don't permit the
1481 * data frame to be sent by returning NULL. This
1482 * is kinda a kludge but the least intrusive way
1483 * to add this check into all drivers.
1485 ieee80211_unref_node(&ni); /* NB: null's ni */
1490 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1491 ic->ic_opmode == IEEE80211_M_AHDEMO) {
1493 * In adhoc mode cons up a node for the destination.
1494 * Note that we need an additional reference for the
1495 * caller to be consistent with _ieee80211_find_node.
1497 ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1499 ieee80211_ref_node(ni);
1501 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1502 "[%6D] no node, discard frame (%s)\n",
1503 macaddr, ":", __func__);
1504 ic->ic_stats.is_tx_nonode++;
1511 * Like find but search based on the channel too.
1513 struct ieee80211_node *
1514 #ifdef IEEE80211_DEBUG_REFCNT
1515 ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
1516 const uint8_t *macaddr, struct ieee80211_channel *chan,
1517 const char *func, int line)
1519 ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
1520 const uint8_t *macaddr, struct ieee80211_channel *chan)
1523 struct ieee80211_node *ni;
1526 ASSERT_SERIALIZED(nt->nt_ic->ic_ifp->if_serializer);
1528 hash = IEEE80211_NODE_HASH(macaddr);
1529 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1530 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1531 ni->ni_chan == chan) {
1532 ieee80211_ref_node(ni); /* mark referenced */
1533 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1534 REFCNT_LOC, ni, ni->ni_macaddr, ":",
1535 ieee80211_node_refcnt(ni));
1543 * Like find but search based on the ssid too.
1545 struct ieee80211_node *
1546 #ifdef IEEE80211_DEBUG_REFCNT
1547 ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1548 const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid,
1549 const char *func, int line)
1551 ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1552 const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid)
1555 #define MATCH_SSID(ni, ssid, ssidlen) \
1556 (ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1557 static const uint8_t zeromac[IEEE80211_ADDR_LEN];
1558 struct ieee80211com *ic = nt->nt_ic;
1559 struct ieee80211_node *ni;
1562 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
1565 * A mac address that is all zero means match only the ssid;
1566 * otherwise we must match both.
1568 if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1569 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1570 if (MATCH_SSID(ni, ssid, ssidlen))
1574 hash = IEEE80211_NODE_HASH(macaddr);
1575 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1576 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1577 MATCH_SSID(ni, ssid, ssidlen))
1582 ieee80211_ref_node(ni); /* mark referenced */
1583 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1584 REFCNT_LOC, ni, ni->ni_macaddr, ":",
1585 ieee80211_node_refcnt(ni));
1592 _ieee80211_free_node(struct ieee80211_node *ni)
1594 struct ieee80211com *ic = ni->ni_ic;
1595 struct ieee80211_node_table *nt = ni->ni_table;
1597 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1598 "%s %p<%6D> in %s table\n", __func__, ni,
1599 ni->ni_macaddr, ":",
1600 nt != NULL ? nt->nt_name : "<gone>");
1602 ieee80211_ratectl_data_free(ni);
1604 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1606 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1607 LIST_REMOVE(ni, ni_hash);
1609 ic->ic_node_free(ni);
1613 #ifdef IEEE80211_DEBUG_REFCNT
1614 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1616 ieee80211_free_node(struct ieee80211_node *ni)
1619 struct ieee80211_node_table *nt = ni->ni_table;
1621 ASSERT_SERIALIZED(ni->ni_ic->ic_ifp->if_serializer);
1623 #ifdef IEEE80211_DEBUG_REFCNT
1624 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1625 "%s (%s:%u) %p<%6D> refcnt %d\n", __func__, func, line, ni,
1626 ni->ni_macaddr, ":", ieee80211_node_refcnt(ni) - 1);
1629 if (ieee80211_node_dectestref(ni)) {
1631 * Last reference, reclaim state.
1633 _ieee80211_free_node(ni);
1634 } else if (ieee80211_node_refcnt(ni) == 1 &&
1635 nt->nt_keyixmap != NULL) {
1636 ieee80211_keyix keyix;
1638 * Check for a last reference in the key mapping table.
1640 keyix = ni->ni_ucastkey.wk_rxkeyix;
1641 if (keyix < nt->nt_keyixmax &&
1642 nt->nt_keyixmap[keyix] == ni) {
1643 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1644 "%s: %p<%6D> clear key map entry", __func__,
1645 ni, ni->ni_macaddr, ":");
1646 nt->nt_keyixmap[keyix] = NULL;
1647 ieee80211_node_decref(ni); /* XXX needed? */
1648 _ieee80211_free_node(ni);
1652 if (ieee80211_node_dectestref(ni))
1653 _ieee80211_free_node(ni);
1658 * Reclaim a unicast key and clear any key cache state.
1661 ieee80211_node_delucastkey(struct ieee80211_node *ni)
1663 struct ieee80211com *ic = ni->ni_ic;
1664 struct ieee80211_node_table *nt = &ic->ic_sta;
1665 struct ieee80211_node *nikey;
1666 ieee80211_keyix keyix;
1669 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
1671 keyix = ni->ni_ucastkey.wk_rxkeyix;
1672 status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1673 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1674 nikey = nt->nt_keyixmap[keyix];
1675 nt->nt_keyixmap[keyix] = NULL;
1679 if (nikey != NULL) {
1680 KASSERT(nikey == ni,
1681 ("key map out of sync, ni %p nikey %p", ni, nikey));
1682 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1683 "%s: delete key map entry %p<%6D> refcnt %d\n",
1684 __func__, ni, ni->ni_macaddr, ":",
1685 ieee80211_node_refcnt(ni)-1);
1686 ieee80211_free_node(ni);
1692 * Reclaim a node. If this is the last reference count then
1693 * do the normal free work. Otherwise remove it from the node
1694 * table and mark it gone by clearing the back-reference.
1697 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1699 ieee80211_keyix keyix;
1701 ASSERT_SERIALIZED(nt->nt_ic->ic_ifp->if_serializer);
1703 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1704 "%s: remove %p<%6D> from %s table, refcnt %d\n",
1705 __func__, ni, ni->ni_macaddr, ":",
1706 nt->nt_name, ieee80211_node_refcnt(ni)-1);
1709 * Clear any entry in the unicast key mapping table.
1710 * We need to do it here so rx lookups don't find it
1711 * in the mapping table even if it's not in the hash
1712 * table. We cannot depend on the mapping table entry
1713 * being cleared because the node may not be free'd.
1715 keyix = ni->ni_ucastkey.wk_rxkeyix;
1716 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1717 nt->nt_keyixmap[keyix] == ni) {
1718 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1719 "%s: %p<%6D> clear key map entry\n",
1720 __func__, ni, ni->ni_macaddr, ":");
1721 nt->nt_keyixmap[keyix] = NULL;
1722 ieee80211_node_decref(ni); /* NB: don't need free */
1724 if (!ieee80211_node_dectestref(ni)) {
1726 * Other references are present, just remove the
1727 * node from the table so it cannot be found. When
1728 * the references are dropped storage will be
1731 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1732 LIST_REMOVE(ni, ni_hash);
1733 ni->ni_table = NULL; /* clear reference */
1737 * We may want to put reclaimed node on <gone> table
1738 * so that ratectl modules can find them and free
1739 * the resources in their detach routines instead of
1740 * freeing the resources here.
1742 ieee80211_ratectl_data_free(ni);
1744 _ieee80211_free_node(ni);
1748 ieee80211_free_allnodes(struct ieee80211_node_table *nt)
1750 struct ieee80211com *ic = nt->nt_ic;
1751 struct ieee80211_node *ni;
1753 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1754 "%s: free all nodes in %s table\n", __func__, nt->nt_name);
1756 while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1757 if (ni->ni_associd != 0) {
1758 if (ic->ic_auth->ia_node_leave != NULL)
1759 ic->ic_auth->ia_node_leave(ic, ni);
1760 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1762 node_reclaim(nt, ni);
1764 ieee80211_reset_erp(ic);
1768 * Timeout entries in the scan cache.
1771 ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
1773 struct ieee80211com *ic = nt->nt_ic;
1774 struct ieee80211_node *ni, *tni;
1776 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
1779 /* XXX belongs elsewhere */
1780 if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
1781 m_freem(ni->ni_rxfrag[0]);
1782 ni->ni_rxfrag[0] = NULL;
1784 TAILQ_FOREACH_MUTABLE(ni, &nt->nt_node, ni_list, tni) {
1785 if (ni->ni_inact && --ni->ni_inact == 0) {
1786 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1787 "[%6D] scan candidate purged from cache "
1788 "(refcnt %u)\n", ni->ni_macaddr, ":",
1789 ieee80211_node_refcnt(ni));
1790 node_reclaim(nt, ni);
1794 nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1798 * Timeout inactive stations and do related housekeeping.
1799 * Note that we cannot hold the node lock while sending a
1800 * frame as this would lead to a LOR. Instead we use a
1801 * generation number to mark nodes that we've scanned and
1802 * drop the lock and restart a scan if we have to time out
1803 * a node. Since we are single-threaded by virtue of
1804 * controlling the inactivity timer we can be sure this will
1805 * process each node only once.
1808 ieee80211_timeout_stations(struct ieee80211_node_table *nt)
1810 struct ieee80211com *ic = nt->nt_ic;
1811 struct ieee80211_node *ni, *next;
1814 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
1816 isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1817 ic->ic_opmode == IEEE80211_M_AHDEMO);
1819 TAILQ_FOREACH_MUTABLE(ni, &nt->nt_node, ni_list, next) {
1821 * Ignore entries for which have yet to receive an
1822 * authentication frame. These are transient and
1823 * will be reclaimed when the last reference to them
1824 * goes away (when frame xmits complete).
1826 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1827 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1830 * Free fragment if not needed anymore
1831 * (last fragment older than 1s).
1832 * XXX doesn't belong here
1834 if (ni->ni_rxfrag[0] != NULL &&
1835 ticks > ni->ni_rxfragstamp + hz) {
1836 m_freem(ni->ni_rxfrag[0]);
1837 ni->ni_rxfrag[0] = NULL;
1840 * Special case ourself; we may be idle for extended periods
1841 * of time and regardless reclaiming our state is wrong.
1843 if (ni == ic->ic_bss)
1846 if (ni->ni_associd != 0 || isadhoc) {
1848 * Age frames on the power save queue. The
1849 * aging interval is 4 times the listen
1850 * interval specified by the station. This
1851 * number is factored into the age calculations
1852 * when the frame is placed on the queue. We
1853 * store ages as time differences we can check
1854 * and/or adjust only the head of the list.
1856 if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
1860 while (IF_POLL(&ni->ni_savedq, m) != NULL &&
1861 M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
1862 IEEE80211_DPRINTF(ic,
1863 IEEE80211_MSG_POWER,
1864 "[%6D] discard frame, age %u\n",
1865 ni->ni_macaddr, ":",
1866 M_AGE_GET(m));/*XXX*/
1867 _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
1872 M_AGE_SUB(m, IEEE80211_INACT_WAIT);
1875 IEEE80211_DPRINTF(ic,
1876 IEEE80211_MSG_POWER,
1877 "[%6D] discard %u frames for age\n",
1878 ni->ni_macaddr, ":",
1880 IEEE80211_NODE_STAT_ADD(ni,
1881 ps_discard, discard);
1882 if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1883 ic->ic_set_tim(ni, 0);
1887 * Probe the station before time it out. We
1888 * send a null data frame which may not be
1889 * universally supported by drivers (need it
1890 * for ps-poll support so it should be...).
1892 if (0 < ni->ni_inact &&
1893 ni->ni_inact <= ic->ic_inact_probe) {
1895 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1897 "probe station due to inactivity");
1899 * Grab a reference before unlocking the table
1900 * so the node cannot be reclaimed before we
1901 * send the frame. ieee80211_send_nulldata
1902 * understands we've done this and reclaims the
1903 * ref for us as needed.
1905 ieee80211_ref_node(ni);
1906 ieee80211_send_nulldata(ni);
1911 if (ni->ni_inact <= 0) {
1913 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1914 "station timed out due to inactivity "
1915 "(refcnt %u)", ieee80211_node_refcnt(ni));
1917 * Send a deauthenticate frame and drop the station.
1918 * This is somewhat complicated due to reference counts
1919 * and locking. At this point a station will typically
1920 * have a reference count of 1. ieee80211_node_leave
1921 * will do a "free" of the node which will drop the
1922 * reference count. But in the meantime a reference
1923 * wil be held by the deauth frame. The actual reclaim
1924 * of the node will happen either after the tx is
1925 * completed or by ieee80211_node_leave.
1927 if (ni->ni_associd != 0) {
1928 IEEE80211_SEND_MGMT(ic, ni,
1929 IEEE80211_FC0_SUBTYPE_DEAUTH,
1930 IEEE80211_REASON_AUTH_EXPIRE);
1932 ieee80211_node_leave(ic, ni);
1933 ic->ic_stats.is_node_timeout++;
1938 nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1942 ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1944 struct ieee80211_node *ni, *next;
1946 ASSERT_SERIALIZED(nt->nt_ic->ic_ifp->if_serializer);
1948 TAILQ_FOREACH_MUTABLE(ni, &nt->nt_node, ni_list, next)
1953 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1955 kprintf("0x%p: mac %6D refcnt %d\n", ni,
1956 ni->ni_macaddr, ":", ieee80211_node_refcnt(ni));
1957 kprintf("\tauthmode %u flags 0x%x\n",
1958 ni->ni_authmode, ni->ni_flags);
1959 kprintf("\tassocid 0x%x txpower %u vlan %u\n",
1960 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
1961 kprintf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1963 ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
1964 ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
1965 ni->ni_rxfragstamp);
1966 kprintf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
1967 ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
1968 kprintf("\tbssid %6D essid \"%.*s\" channel %u:0x%x\n",
1970 ni->ni_esslen, ni->ni_essid,
1971 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
1972 kprintf("\tfails %u inact %u txrate %u\n",
1973 ni->ni_fails, ni->ni_inact, ni->ni_txrate);
1977 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
1979 ieee80211_iterate_nodes(nt,
1980 (ieee80211_iter_func *) ieee80211_dump_node, nt);
1984 * Handle a station joining an 11g network.
1987 ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1991 * Station isn't capable of short slot time. Bump
1992 * the count of long slot time stations and disable
1993 * use of short slot time. Note that the actual switch
1994 * over to long slot time use may not occur until the
1995 * next beacon transmission (per sec. 7.3.1.4 of 11g).
1997 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1998 ic->ic_longslotsta++;
1999 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2000 "[%6D] station needs long slot time, count %d\n",
2001 ni->ni_macaddr, ":", ic->ic_longslotsta);
2002 /* XXX vap's w/ conflicting needs won't work */
2003 ieee80211_set_shortslottime(ic, 0);
2006 * If the new station is not an ERP station
2007 * then bump the counter and enable protection
2010 if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
2012 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2013 "[%6D] station is !ERP, %d non-ERP stations associated\n",
2014 ni->ni_macaddr, ":", ic->ic_nonerpsta);
2016 * If protection is configured, enable it.
2018 if (ic->ic_protmode != IEEE80211_PROT_NONE) {
2019 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2020 "%s: enable use of protection\n", __func__);
2021 ic->ic_flags |= IEEE80211_F_USEPROT;
2024 * If station does not support short preamble
2025 * then we must enable use of Barker preamble.
2027 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2028 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2029 "[%6D] station needs long preamble\n",
2030 ni->ni_macaddr, ":");
2031 ieee80211_set_shortpreamble(ic, 0);
2033 if (ic->ic_nonerpsta == 1)
2034 ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
2036 ni->ni_flags |= IEEE80211_NODE_ERP;
2040 ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
2044 if (ni->ni_associd == 0) {
2048 * It would be good to search the bitmap
2049 * more efficiently, but this will do for now.
2051 for (aid = 1; aid < ic->ic_max_aid; aid++) {
2052 if (!IEEE80211_AID_ISSET(aid,
2056 if (aid >= ic->ic_max_aid) {
2057 IEEE80211_SEND_MGMT(ic, ni, resp,
2058 IEEE80211_REASON_ASSOC_TOOMANY);
2059 ieee80211_node_leave(ic, ni);
2062 ni->ni_associd = aid | 0xc000;
2063 IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
2066 if (ic->ic_curmode == IEEE80211_MODE_11G)
2067 ieee80211_node_join_11g(ic, ni);
2071 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
2072 "[%6D] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
2073 ni->ni_macaddr, ":", newassoc ? "" : "re",
2074 IEEE80211_NODE_AID(ni),
2075 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2076 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2077 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2078 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
2081 IEEE80211_PRINT_NODERATES(ic, ni,
2082 IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG);
2084 ieee80211_ratectl_newassoc(ni, newassoc);
2086 /* give driver a chance to setup state like ni_txrate */
2087 if (ic->ic_newassoc != NULL)
2088 ic->ic_newassoc(ni, newassoc);
2090 ni->ni_inact_reload = ic->ic_inact_auth;
2091 ni->ni_inact = ni->ni_inact_reload;
2092 IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
2093 /* tell the authenticator about new station */
2094 if (ic->ic_auth->ia_node_join != NULL)
2095 ic->ic_auth->ia_node_join(ic, ni);
2096 ieee80211_notify_node_join(ic, ni, newassoc);
2100 * Handle a station leaving an 11g network.
2103 ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
2106 KASSERT(ic->ic_curmode == IEEE80211_MODE_11G,
2107 ("not in 11g, bss %u:0x%x, curmode %u", ni->ni_chan->ic_freq,
2108 ni->ni_chan->ic_flags, ic->ic_curmode));
2111 * If a long slot station do the slot time bookkeeping.
2113 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2114 KASSERT(ic->ic_longslotsta > 0,
2115 ("bogus long slot station count %d", ic->ic_longslotsta));
2116 ic->ic_longslotsta--;
2117 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2118 "[%6D] long slot time station leaves, count now %d\n",
2119 ni->ni_macaddr, ":", ic->ic_longslotsta);
2120 if (ic->ic_longslotsta == 0) {
2122 * Re-enable use of short slot time if supported
2123 * and not operating in IBSS mode (per spec).
2125 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2126 ic->ic_opmode != IEEE80211_M_IBSS) {
2127 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2128 "%s: re-enable use of short slot time\n",
2130 ieee80211_set_shortslottime(ic, 1);
2135 * If a non-ERP station do the protection-related bookkeeping.
2137 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2138 KASSERT(ic->ic_nonerpsta > 0,
2139 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2141 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2142 "[%6D] non-ERP station leaves, count now %d\n",
2143 ni->ni_macaddr, ":", ic->ic_nonerpsta);
2144 if (ic->ic_nonerpsta == 0) {
2145 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2146 "%s: disable use of protection\n", __func__);
2147 ic->ic_flags &= ~IEEE80211_F_USEPROT;
2148 /* XXX verify mode? */
2149 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2150 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2151 "%s: re-enable use of short preamble\n",
2153 ieee80211_set_shortpreamble(ic, 1);
2155 ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
2161 * Handle bookkeeping for station deauthentication/disassociation
2162 * when operating as an ap.
2165 ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
2167 struct ieee80211_node_table *nt = ni->ni_table;
2169 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
2171 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
2172 "[%6D] station with aid %d leaves\n",
2173 ni->ni_macaddr, ":", IEEE80211_NODE_AID(ni));
2175 KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
2176 ic->ic_opmode == IEEE80211_M_IBSS ||
2177 ic->ic_opmode == IEEE80211_M_AHDEMO,
2178 ("unexpected operating mode %u", ic->ic_opmode));
2180 * If node wasn't previously associated all
2181 * we need to do is reclaim the reference.
2183 /* XXX ibss mode bypasses 11g and notification */
2184 if (ni->ni_associd == 0)
2187 * Tell the authenticator the station is leaving.
2188 * Note that we must do this before yanking the
2189 * association id as the authenticator uses the
2190 * associd to locate it's state block.
2192 if (ic->ic_auth->ia_node_leave != NULL)
2193 ic->ic_auth->ia_node_leave(ic, ni);
2194 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
2198 if (ic->ic_curmode == IEEE80211_MODE_11G)
2199 ieee80211_node_leave_11g(ic, ni);
2201 * Cleanup station state. In particular clear various
2202 * state that might otherwise be reused if the node
2203 * is reused before the reference count goes to zero
2204 * (and memory is reclaimed).
2206 ieee80211_sta_leave(ic, ni);
2209 * Remove the node from any table it's recorded in and
2210 * drop the caller's reference. Removal from the table
2211 * is important to insure the node is not reprocessed
2215 node_reclaim(nt, ni);
2217 ieee80211_free_node(ni);
2221 ieee80211_getrssi(struct ieee80211com *ic)
2223 #define NZ(x) ((x) == 0 ? 1 : (x))
2224 struct ieee80211_node_table *nt = &ic->ic_sta;
2225 uint32_t rssi_samples, rssi_total;
2226 struct ieee80211_node *ni;
2230 switch (ic->ic_opmode) {
2231 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
2233 TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
2234 if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
2236 rssi_total += ic->ic_node_getrssi(ni);
2239 case IEEE80211_M_AHDEMO: /* average of all neighbors */
2241 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2243 rssi_total += ic->ic_node_getrssi(ni);
2246 case IEEE80211_M_HOSTAP: /* average of all associated stations */
2248 TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
2249 if (IEEE80211_AID(ni->ni_associd) != 0) {
2251 rssi_total += ic->ic_node_getrssi(ni);
2254 case IEEE80211_M_MONITOR: /* XXX */
2255 case IEEE80211_M_STA: /* use stats from associated ap */
2257 if (ic->ic_bss != NULL)
2258 rssi_total = ic->ic_node_getrssi(ic->ic_bss);
2262 return rssi_total / NZ(rssi_samples);
2267 * Indicate whether there are frames queued for a station in power-save mode.
2270 ieee80211_set_tim(struct ieee80211_node *ni, int set)
2272 struct ieee80211com *ic = ni->ni_ic;
2275 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
2277 KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
2278 ic->ic_opmode == IEEE80211_M_IBSS,
2279 ("operating mode %u", ic->ic_opmode));
2281 aid = IEEE80211_AID(ni->ni_associd);
2282 KASSERT(aid < ic->ic_max_aid,
2283 ("bogus aid %u, max %u", aid, ic->ic_max_aid));
2285 if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
2287 setbit(ic->ic_tim_bitmap, aid);
2288 ic->ic_ps_pending++;
2290 clrbit(ic->ic_tim_bitmap, aid);
2291 ic->ic_ps_pending--;
2293 ic->ic_flags |= IEEE80211_F_TIMUPDATE;
2298 * Node table support.
2302 ieee80211_node_table_init(struct ieee80211com *ic,
2303 struct ieee80211_node_table *nt,
2304 const char *name, int inact, int keyixmax,
2305 void (*timeout)(struct ieee80211_node_table *))
2307 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
2308 "%s %s table, inact %u\n", __func__, name, inact);
2311 TAILQ_INIT(&nt->nt_node);
2313 nt->nt_inact_init = inact;
2314 nt->nt_timeout = timeout;
2315 nt->nt_keyixmax = keyixmax;
2316 if (nt->nt_keyixmax > 0) {
2318 kmalloc(keyixmax * sizeof(struct ieee80211_node *),
2319 M_80211_NODE, M_WAITOK | M_ZERO);
2321 nt->nt_keyixmap = NULL;
2326 ieee80211_node_table_reset(struct ieee80211_node_table *nt)
2328 ASSERT_SERIALIZED(nt->nt_ic->ic_ifp->if_serializer);
2330 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2331 "%s %s table\n", __func__, nt->nt_name);
2333 nt->nt_inact_timer = 0;
2334 ieee80211_free_allnodes(nt);
2338 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2340 ASSERT_SERIALIZED(nt->nt_ic->ic_ifp->if_serializer);
2342 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2343 "%s %s table\n", __func__, nt->nt_name);
2345 ieee80211_free_allnodes(nt);
2346 if (nt->nt_keyixmap != NULL) {
2347 /* XXX verify all entries are NULL */
2349 for (i = 0; i < nt->nt_keyixmax; i++)
2350 if (nt->nt_keyixmap[i] != NULL) {
2351 kprintf("%s: %s[%u] still active\n", __func__,
2354 kfree(nt->nt_keyixmap, M_80211_NODE);
2355 nt->nt_keyixmap = NULL;
2360 * Update short preamble state
2363 ieee80211_update_shpreamble(struct ieee80211com *ic,
2364 const struct ieee80211_node *ni)
2368 switch (ic->ic_curmode) {
2369 case IEEE80211_MODE_11A:
2372 case IEEE80211_MODE_11G:
2373 if (ni->ni_erp & IEEE80211_ERP_LONG_PREAMBLE) {
2375 * According to IEEE Std 802.11g-2003 subclause
2376 * 7.3.2.13, page 10:
2377 * Short preamble should not be used, if barker
2378 * preamble mode bit is 1 in ERP informarion,
2379 * _regardless_ of the short preamble bit in
2380 * capability information.
2386 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) &&
2387 (ic->ic_caps & IEEE80211_C_SHPREAMBLE))
2391 ieee80211_set_shortpreamble(ic, shpreamble);
2395 ieee80211_set_scanchan(struct ieee80211com *ic, struct ieee80211_channel *chan)
2397 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: chan %d->%d\n", __func__,
2398 ieee80211_chan2ieee(ic, ic->ic_curchan),
2399 ieee80211_chan2ieee(ic, chan));
2400 ic->ic_curchan = chan;
2402 * XXX drivers should do this as needed,
2403 * XXX for now maintain compatibility
2405 ic->ic_bss->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];