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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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_media.h>
46 #include <net/ethernet.h>
48 #include <net80211/ieee80211_var.h>
52 static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
53 static void node_cleanup(struct ieee80211_node *);
54 static void node_free(struct ieee80211_node *);
55 static u_int8_t node_getrssi(const struct ieee80211_node *);
57 static void ieee80211_setup_node(struct ieee80211_node_table *,
58 struct ieee80211_node *, const u_int8_t *);
59 static void _ieee80211_free_node(struct ieee80211_node *);
60 static void ieee80211_free_allnodes(struct ieee80211_node_table *);
62 static void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
63 static void ieee80211_timeout_stations(struct ieee80211_node_table *);
65 static void ieee80211_set_tim(struct ieee80211com *,
66 struct ieee80211_node *, int set);
68 static void ieee80211_node_table_init(struct ieee80211com *ic,
69 struct ieee80211_node_table *nt, const char *name, int inact,
70 void (*timeout)(struct ieee80211_node_table *));
71 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
73 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
76 ieee80211_node_attach(struct ieee80211com *ic)
79 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
80 IEEE80211_INACT_INIT, ieee80211_timeout_stations);
81 ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
82 IEEE80211_INACT_SCAN, ieee80211_timeout_scan_candidates);
84 ic->ic_node_alloc = node_alloc;
85 ic->ic_node_free = node_free;
86 ic->ic_node_cleanup = node_cleanup;
87 ic->ic_node_getrssi = node_getrssi;
89 /* default station inactivity timer setings */
90 ic->ic_inact_init = IEEE80211_INACT_INIT;
91 ic->ic_inact_auth = IEEE80211_INACT_AUTH;
92 ic->ic_inact_run = IEEE80211_INACT_RUN;
93 ic->ic_inact_probe = IEEE80211_INACT_PROBE;
96 if (ic->ic_max_aid == 0)
97 ic->ic_max_aid = IEEE80211_AID_DEF;
98 else if (ic->ic_max_aid > IEEE80211_AID_MAX)
99 ic->ic_max_aid = IEEE80211_AID_MAX;
100 MALLOC(ic->ic_aid_bitmap, u_int32_t *,
101 howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
102 M_DEVBUF, M_NOWAIT | M_ZERO);
103 if (ic->ic_aid_bitmap == NULL) {
104 /* XXX no way to recover */
105 printf("%s: no memory for AID bitmap!\n", __func__);
109 /* XXX defer until using hostap/ibss mode */
110 ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
111 MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
112 M_DEVBUF, M_NOWAIT | M_ZERO);
113 if (ic->ic_tim_bitmap == NULL) {
114 /* XXX no way to recover */
115 printf("%s: no memory for TIM bitmap!\n", __func__);
117 ic->ic_set_tim = ieee80211_set_tim; /* NB: driver should override */
121 ieee80211_node_lateattach(struct ieee80211com *ic)
123 struct ieee80211_node *ni;
124 struct ieee80211_rsnparms *rsn;
126 ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
127 KASSERT(ni != NULL, ("unable to setup inital BSS node"));
129 * Setup "global settings" in the bss node so that
130 * each new station automatically inherits them.
133 /* WEP, TKIP, and AES-CCM are always supported */
134 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
135 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
136 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
137 if (ic->ic_caps & IEEE80211_C_AES)
138 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
139 if (ic->ic_caps & IEEE80211_C_CKIP)
140 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
142 * Default unicast cipher to WEP for 802.1x use. If
143 * WPA is enabled the management code will set these
146 rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
147 rsn->rsn_ucastkeylen = 104 / NBBY;
149 * WPA says the multicast cipher is the lowest unicast
150 * cipher supported. But we skip WEP which would
151 * otherwise be used based on this criteria.
153 rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
154 rsn->rsn_mcastkeylen = 128 / NBBY;
157 * We support both WPA-PSK and 802.1x; the one used
158 * is determined by the authentication mode and the
159 * setting of the PSK state.
161 rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
162 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
164 ic->ic_bss = ieee80211_ref_node(ni); /* hold reference */
165 ic->ic_auth = ieee80211_authenticator_get(ni->ni_authmode);
169 ieee80211_node_detach(struct ieee80211com *ic)
172 if (ic->ic_bss != NULL) {
173 ieee80211_free_node(ic->ic_bss);
176 ieee80211_node_table_cleanup(&ic->ic_scan);
177 ieee80211_node_table_cleanup(&ic->ic_sta);
178 if (ic->ic_aid_bitmap != NULL) {
179 FREE(ic->ic_aid_bitmap, M_DEVBUF);
180 ic->ic_aid_bitmap = NULL;
182 if (ic->ic_tim_bitmap != NULL) {
183 FREE(ic->ic_tim_bitmap, M_DEVBUF);
184 ic->ic_tim_bitmap = NULL;
189 * Port authorize/unauthorize interfaces for use by an authenticator.
193 ieee80211_node_authorize(struct ieee80211com *ic, struct ieee80211_node *ni)
195 ni->ni_flags |= IEEE80211_NODE_AUTH;
196 ni->ni_inact_reload = ic->ic_inact_run;
200 ieee80211_node_unauthorize(struct ieee80211com *ic, struct ieee80211_node *ni)
202 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
206 * Set/change the channel. The rate set is also updated as
207 * to insure a consistent view by drivers.
210 ieee80211_set_chan(struct ieee80211com *ic,
211 struct ieee80211_node *ni, struct ieee80211_channel *chan)
214 ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
218 * AP scanning support.
221 #ifdef IEEE80211_DEBUG
223 dump_chanlist(const u_char chans[])
229 for (i = 0; i < IEEE80211_CHAN_MAX; i++)
230 if (isset(chans, i)) {
231 printf("%s%u", sep, i);
235 #endif /* IEEE80211_DEBUG */
238 * Initialize the channel set to scan based on the
239 * of available channels and the current PHY mode.
242 ieee80211_reset_scan(struct ieee80211com *ic)
245 /* XXX ic_des_chan should be handled with ic_chan_active */
246 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
247 memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
248 setbit(ic->ic_chan_scan,
249 ieee80211_chan2ieee(ic, ic->ic_des_chan));
251 memcpy(ic->ic_chan_scan, ic->ic_chan_active,
252 sizeof(ic->ic_chan_active));
253 /* NB: hack, setup so next_scan starts with the first channel */
254 if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
255 ieee80211_set_chan(ic, ic->ic_bss,
256 &ic->ic_channels[IEEE80211_CHAN_MAX]);
257 #ifdef IEEE80211_DEBUG
258 if (ieee80211_msg_scan(ic)) {
259 printf("%s: scan set:", __func__);
260 dump_chanlist(ic->ic_chan_scan);
261 printf(" start chan %u\n",
262 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
264 #endif /* IEEE80211_DEBUG */
268 * Begin an active scan.
271 ieee80211_begin_scan(struct ieee80211com *ic, int reset)
274 ic->ic_scan.nt_scangen++;
276 * In all but hostap mode scanning starts off in
277 * an active mode before switching to passive.
279 if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
280 ic->ic_flags |= IEEE80211_F_ASCAN;
281 ic->ic_stats.is_scan_active++;
283 ic->ic_stats.is_scan_passive++;
284 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
285 "begin %s scan in %s mode, scangen %u\n",
286 (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive",
287 ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
289 * Clear scan state and flush any previously seen AP's.
291 ieee80211_reset_scan(ic);
293 ieee80211_free_allnodes(&ic->ic_scan);
295 ic->ic_flags |= IEEE80211_F_SCAN;
297 /* Scan the next channel. */
298 ieee80211_next_scan(ic);
302 * Switch to the next channel marked for scanning.
305 ieee80211_next_scan(struct ieee80211com *ic)
307 struct ieee80211_channel *chan;
310 * Insure any previous mgt frame timeouts don't fire.
311 * This assumes the driver does the right thing in
312 * flushing anything queued in the driver and below.
314 ic->ic_mgt_timer = 0;
316 chan = ic->ic_bss->ni_chan;
318 if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
319 chan = &ic->ic_channels[0];
320 if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
321 clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
322 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
323 "%s: chan %d->%d\n", __func__,
324 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
325 ieee80211_chan2ieee(ic, chan));
326 ieee80211_set_chan(ic, ic->ic_bss, chan);
328 /* XXX driver state change */
330 * Scan next channel. If doing an active scan
331 * and the channel is not marked passive-only
332 * then send a probe request. Otherwise just
333 * listen for beacons on the channel.
335 if ((ic->ic_flags & IEEE80211_F_ASCAN) &&
336 (ni->ni_chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) {
337 IEEE80211_SEND_MGMT(ic, ni,
338 IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
341 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
345 } while (chan != ic->ic_bss->ni_chan);
346 ieee80211_end_scan(ic);
351 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
353 /* propagate useful state */
354 nbss->ni_authmode = obss->ni_authmode;
355 nbss->ni_txpower = obss->ni_txpower;
356 nbss->ni_vlan = obss->ni_vlan;
357 nbss->ni_rsn = obss->ni_rsn;
358 /* XXX statistics? */
362 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
364 struct ieee80211_node_table *nt;
365 struct ieee80211_node *ni;
367 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
368 "%s: creating ibss\n", __func__);
371 * Create the station/neighbor table. Note that for adhoc
372 * mode we make the initial inactivity timer longer since
373 * we create nodes only through discovery and they typically
374 * are long-lived associations.
377 IEEE80211_NODE_LOCK(nt);
378 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
379 nt->nt_name = "station";
380 nt->nt_inact_init = ic->ic_inact_init;
382 nt->nt_name = "neighbor";
383 nt->nt_inact_init = ic->ic_inact_run;
385 IEEE80211_NODE_UNLOCK(nt);
387 ni = ieee80211_alloc_node(nt, ic->ic_myaddr);
392 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
393 ni->ni_esslen = ic->ic_des_esslen;
394 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
395 copy_bss(ni, ic->ic_bss);
396 ni->ni_intval = ic->ic_lintval;
397 if (ic->ic_flags & IEEE80211_F_PRIVACY)
398 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
399 if (ic->ic_phytype == IEEE80211_T_FH) {
400 ni->ni_fhdwell = 200; /* XXX */
403 if (ic->ic_opmode == IEEE80211_M_IBSS) {
404 ic->ic_flags |= IEEE80211_F_SIBSS;
405 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */
406 if (ic->ic_flags & IEEE80211_F_DESBSSID)
407 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
409 ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */
412 * Fix the channel and related attributes.
414 ieee80211_set_chan(ic, ni, chan);
415 ic->ic_curmode = ieee80211_chan2mode(ic, chan);
417 * Do mode-specific rate setup.
419 if (ic->ic_curmode == IEEE80211_MODE_11G) {
421 * Use a mixed 11b/11g rate set.
423 ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
424 } else if (ic->ic_curmode == IEEE80211_MODE_11B) {
426 * Force pure 11b rate set.
428 ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
431 (void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
435 ieee80211_reset_bss(struct ieee80211com *ic)
437 struct ieee80211_node *ni, *obss;
439 ieee80211_node_table_reset(&ic->ic_scan);
440 ieee80211_node_table_reset(&ic->ic_sta);
442 ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
443 KASSERT(ni != NULL, ("unable to setup inital BSS node"));
445 ic->ic_bss = ieee80211_ref_node(ni);
448 ni->ni_intval = ic->ic_lintval;
449 ieee80211_free_node(obss);
454 ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
460 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
462 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
463 ni->ni_chan != ic->ic_des_chan)
465 if (ic->ic_opmode == IEEE80211_M_IBSS) {
466 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
469 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
472 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
473 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
476 /* XXX does this mean privacy is supported or required? */
477 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
480 rate = ieee80211_fix_rate(ic, ni,
481 IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
482 if (rate & IEEE80211_RATE_BASIC)
484 if (ic->ic_des_esslen != 0 &&
485 (ni->ni_esslen != ic->ic_des_esslen ||
486 memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
488 if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
489 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
491 #ifdef IEEE80211_DEBUG
492 if (ieee80211_msg_scan(ic)) {
493 printf(" %c %s", fail ? '-' : '+',
494 ether_sprintf(ni->ni_macaddr));
495 printf(" %s%c", ether_sprintf(ni->ni_bssid),
496 fail & 0x20 ? '!' : ' ');
497 printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
498 fail & 0x01 ? '!' : ' ');
499 printf(" %+4d", ni->ni_rssi);
500 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
501 fail & 0x08 ? '!' : ' ');
503 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
504 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
506 fail & 0x02 ? '!' : ' ');
508 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
510 fail & 0x04 ? '!' : ' ');
511 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
512 printf("%s\n", fail & 0x10 ? "!" : "");
518 static __inline u_int8_t
519 maxrate(const struct ieee80211_node *ni)
521 const struct ieee80211_rateset *rs = &ni->ni_rates;
522 /* NB: assumes rate set is sorted (happens on frame receive) */
523 return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
527 * Compare the capabilities of two nodes and decide which is
528 * more desirable (return >0 if a is considered better). Note
529 * that we assume compatibility/usability has already been checked
530 * so we don't need to (e.g. validate whether privacy is supported).
531 * Used to select the best scan candidate for association in a BSS.
534 ieee80211_node_compare(struct ieee80211com *ic,
535 const struct ieee80211_node *a,
536 const struct ieee80211_node *b)
539 u_int8_t rssia, rssib;
541 /* privacy support preferred */
542 if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
543 (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
545 if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
546 (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
549 rssia = ic->ic_node_getrssi(a);
550 rssib = ic->ic_node_getrssi(b);
551 if (abs(rssib - rssia) < 5) {
552 /* best/max rate preferred if signal level close enough XXX */
557 /* XXX use freq for channel preference */
558 /* for now just prefer 5Ghz band to all other bands */
559 if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
560 !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
562 if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
563 IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
566 /* all things being equal, use signal level */
567 return rssia - rssib;
571 * Mark an ongoing scan stopped.
574 ieee80211_cancel_scan(struct ieee80211com *ic)
577 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
579 (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive");
581 ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
585 * Complete a scan of potential channels.
588 ieee80211_end_scan(struct ieee80211com *ic)
590 struct ieee80211_node_table *nt = &ic->ic_scan;
591 struct ieee80211_node *ni, *selbs;
593 ieee80211_cancel_scan(ic);
594 ieee80211_notify_scan_done(ic);
596 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
597 u_int8_t maxrssi[IEEE80211_CHAN_MAX]; /* XXX off stack? */
602 * The passive scan to look for existing AP's completed,
603 * select a channel to camp on. Identify the channels
604 * that already have one or more AP's and try to locate
605 * an unoccupied one. If that fails, pick a channel that
606 * looks to be quietest.
608 memset(maxrssi, 0, sizeof(maxrssi));
609 IEEE80211_NODE_LOCK(nt);
610 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
611 rssi = ic->ic_node_getrssi(ni);
612 i = ieee80211_chan2ieee(ic, ni->ni_chan);
613 if (rssi > maxrssi[i])
616 IEEE80211_NODE_UNLOCK(nt);
617 /* XXX select channel more intelligently */
619 for (i = 0; i < IEEE80211_CHAN_MAX; i++)
620 if (isset(ic->ic_chan_active, i)) {
622 * If the channel is unoccupied the max rssi
623 * should be zero; just take it. Otherwise
624 * track the channel with the lowest rssi and
625 * use that when all channels appear occupied.
627 if (maxrssi[i] == 0) {
631 if (bestchan == -1 ||
632 maxrssi[i] < maxrssi[bestchan])
635 if (bestchan != -1) {
636 ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
639 /* no suitable channel, should not happen */
643 * When manually sequencing the state machine; scan just once
644 * regardless of whether we have a candidate or not. The
645 * controlling application is expected to setup state and
646 * initiate an association.
648 if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
651 * Automatic sequencing; look for a candidate and
652 * if found join the network.
654 /* NB: unlocked read should be ok */
655 if (TAILQ_FIRST(&nt->nt_node) == NULL) {
656 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
657 "%s: no scan candidate\n", __func__);
659 if (ic->ic_opmode == IEEE80211_M_IBSS &&
660 (ic->ic_flags & IEEE80211_F_IBSSON) &&
661 ic->ic_des_esslen != 0) {
662 ieee80211_create_ibss(ic, ic->ic_ibss_chan);
666 * Reset the list of channels to scan and start again.
668 ieee80211_reset_scan(ic);
669 ic->ic_flags |= IEEE80211_F_SCAN;
670 ieee80211_next_scan(ic);
674 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
675 "macaddr bssid chan rssi rate flag wep essid");
676 IEEE80211_NODE_LOCK(nt);
677 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
680 * The configuration of the access points may change
681 * during my scan. So delete the entry for the AP
682 * and retry to associate if there is another beacon.
684 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
685 "%s: skip scan candidate %s, fails %u\n",
686 __func__, ether_sprintf(ni->ni_macaddr),
690 if (ni->ni_fails++ > 2)
691 ieee80211_free_node(ni);
695 if (ieee80211_match_bss(ic, ni) == 0) {
698 else if (ieee80211_node_compare(ic, ni, selbs) > 0)
702 if (selbs != NULL) /* NB: grab ref while dropping lock */
703 (void) ieee80211_ref_node(selbs);
704 IEEE80211_NODE_UNLOCK(nt);
707 if (!ieee80211_sta_join(ic, selbs)) {
708 ieee80211_free_node(selbs);
714 * Handle 802.11 ad hoc network merge. The
715 * convention, set by the Wireless Ethernet Compatibility Alliance
716 * (WECA), is that an 802.11 station will change its BSSID to match
717 * the "oldest" 802.11 ad hoc network, on the same channel, that
718 * has the station's desired SSID. The "oldest" 802.11 network
719 * sends beacons with the greatest TSF timestamp.
721 * The caller is assumed to validate TSF's before attempting a merge.
723 * Return !0 if the BSSID changed, 0 otherwise.
726 ieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni)
729 if (ni == ic->ic_bss ||
730 IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
731 /* unchanged, nothing to do */
734 if (ieee80211_match_bss(ic, ni) != 0) { /* capabilities mismatch */
735 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
736 "%s: merge failed, capabilities mismatch\n", __func__);
737 ic->ic_stats.is_ibss_capmismatch++;
740 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
741 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
742 ether_sprintf(ni->ni_bssid),
743 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
744 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
745 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
747 return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
751 * Join the specified IBSS/BSS network. The node is assumed to
752 * be passed in with a held reference.
755 ieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
757 struct ieee80211_node *obss;
759 if (ic->ic_opmode == IEEE80211_M_IBSS) {
760 struct ieee80211_node_table *nt;
762 * Delete unusable rates; we've already checked
763 * that the negotiated rate set is acceptable.
765 ieee80211_fix_rate(ic, selbs, IEEE80211_F_DODEL);
767 * Fillin the neighbor table; it will already
768 * exist if we are simply switching mastership.
769 * XXX ic_sta always setup so this is unnecessary?
772 IEEE80211_NODE_LOCK(nt);
773 nt->nt_name = "neighbor";
774 nt->nt_inact_init = ic->ic_inact_run;
775 IEEE80211_NODE_UNLOCK(nt);
779 * Committed to selbs, setup state.
782 ic->ic_bss = selbs; /* NB: caller assumed to bump refcnt */
784 ieee80211_free_node(obss);
786 * Set the erp state (mostly the slot time) to deal with
787 * the auto-select case; this should be redundant if the
790 ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
791 ieee80211_reset_erp(ic);
792 ieee80211_wme_initparams(ic);
794 if (ic->ic_opmode == IEEE80211_M_STA)
795 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
797 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
802 * Leave the specified IBSS/BSS network. The node is assumed to
803 * be passed in with a held reference.
806 ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
808 ic->ic_node_cleanup(ni);
809 ieee80211_notify_node_leave(ic, ni);
812 static struct ieee80211_node *
813 node_alloc(struct ieee80211_node_table *nt)
815 struct ieee80211_node *ni;
817 MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
818 M_80211_NODE, M_NOWAIT | M_ZERO);
823 * Reclaim any resources in a node and reset any critical
824 * state. Typically nodes are free'd immediately after,
825 * but in some cases the storage may be reused so we need
826 * to insure consistent state (should probably fix that).
829 node_cleanup(struct ieee80211_node *ni)
831 #define N(a) (sizeof(a)/sizeof(a[0]))
832 struct ieee80211com *ic = ni->ni_ic;
835 /* NB: preserve ni_table */
836 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
838 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
839 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
840 "[%s] power save mode off, %u sta's in ps mode\n",
841 ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
845 * Drain power save queue and, if needed, clear TIM.
847 IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
848 if (qlen != 0 && ic->ic_set_tim != NULL)
849 ic->ic_set_tim(ic, ni, 0);
852 if (ni->ni_challenge != NULL) {
853 FREE(ni->ni_challenge, M_DEVBUF);
854 ni->ni_challenge = NULL;
857 * Preserve SSID, WPA, and WME ie's so the bss node is
858 * reusable during a re-auth/re-assoc state transition.
859 * If we remove these data they will not be recreated
860 * because they come from a probe-response or beacon frame
861 * which cannot be expected prior to the association-response.
862 * This should not be an issue when operating in other modes
863 * as stations leaving always go through a full state transition
864 * which will rebuild this state.
866 * XXX does this leave us open to inheriting old state?
868 for (i = 0; i < N(ni->ni_rxfrag); i++)
869 if (ni->ni_rxfrag[i] != NULL) {
870 m_freem(ni->ni_rxfrag[i]);
871 ni->ni_rxfrag[i] = NULL;
873 ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
878 node_free(struct ieee80211_node *ni)
880 struct ieee80211com *ic = ni->ni_ic;
882 ic->ic_node_cleanup(ni);
883 if (ni->ni_wpa_ie != NULL)
884 FREE(ni->ni_wpa_ie, M_DEVBUF);
885 if (ni->ni_wme_ie != NULL)
886 FREE(ni->ni_wme_ie, M_DEVBUF);
887 IEEE80211_NODE_SAVEQ_DESTROY(ni);
888 FREE(ni, M_80211_NODE);
892 node_getrssi(const struct ieee80211_node *ni)
898 ieee80211_setup_node(struct ieee80211_node_table *nt,
899 struct ieee80211_node *ni, const u_int8_t *macaddr)
901 struct ieee80211com *ic = nt->nt_ic;
904 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
905 "%s %p<%s> in %s table\n", __func__, ni,
906 ether_sprintf(macaddr), nt->nt_name);
908 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
909 hash = IEEE80211_NODE_HASH(macaddr);
910 ieee80211_node_initref(ni); /* mark referenced */
911 ni->ni_chan = IEEE80211_CHAN_ANYC;
912 ni->ni_authmode = IEEE80211_AUTH_OPEN;
913 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
914 ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
915 ni->ni_inact_reload = nt->nt_inact_init;
916 ni->ni_inact = ni->ni_inact_reload;
917 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
919 IEEE80211_NODE_LOCK(nt);
920 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
921 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
924 IEEE80211_NODE_UNLOCK(nt);
927 struct ieee80211_node *
928 ieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
930 struct ieee80211com *ic = nt->nt_ic;
931 struct ieee80211_node *ni;
933 ni = ic->ic_node_alloc(nt);
935 ieee80211_setup_node(nt, ni, macaddr);
937 ic->ic_stats.is_rx_nodealloc++;
941 struct ieee80211_node *
942 ieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
944 struct ieee80211com *ic = nt->nt_ic;
945 struct ieee80211_node *ni;
947 ni = ic->ic_node_alloc(nt);
949 ieee80211_setup_node(nt, ni, macaddr);
951 * Inherit from ic_bss.
953 ni->ni_authmode = ic->ic_bss->ni_authmode;
954 ni->ni_txpower = ic->ic_bss->ni_txpower;
955 ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */
956 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
957 ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
958 ni->ni_rsn = ic->ic_bss->ni_rsn;
960 ic->ic_stats.is_rx_nodealloc++;
964 static struct ieee80211_node *
965 #ifdef IEEE80211_DEBUG_REFCNT
966 _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
967 const u_int8_t *macaddr, const char *func, int line)
969 _ieee80211_find_node(struct ieee80211_node_table *nt,
970 const u_int8_t *macaddr)
973 struct ieee80211_node *ni;
976 IEEE80211_NODE_LOCK_ASSERT(nt);
978 hash = IEEE80211_NODE_HASH(macaddr);
979 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
980 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
981 ieee80211_ref_node(ni); /* mark referenced */
982 #ifdef IEEE80211_DEBUG_REFCNT
983 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
984 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
986 ni, ether_sprintf(ni->ni_macaddr),
987 ieee80211_node_refcnt(ni));
994 #ifdef IEEE80211_DEBUG_REFCNT
995 #define _ieee80211_find_node(nt, mac) \
996 _ieee80211_find_node_debug(nt, mac, func, line)
999 struct ieee80211_node *
1000 #ifdef IEEE80211_DEBUG_REFCNT
1001 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1002 const u_int8_t *macaddr, const char *func, int line)
1004 ieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1007 struct ieee80211_node *ni;
1009 IEEE80211_NODE_LOCK(nt);
1010 ni = _ieee80211_find_node(nt, macaddr);
1011 IEEE80211_NODE_UNLOCK(nt);
1016 * Fake up a node; this handles node discovery in adhoc mode.
1017 * Note that for the driver's benefit we we treat this like
1018 * an association so the driver has an opportunity to setup
1019 * it's private state.
1021 struct ieee80211_node *
1022 ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
1023 const u_int8_t macaddr[IEEE80211_ADDR_LEN])
1025 struct ieee80211com *ic = nt->nt_ic;
1026 struct ieee80211_node *ni;
1028 ni = ieee80211_dup_bss(nt, macaddr);
1030 /* XXX no rate negotiation; just dup */
1031 ni->ni_rates = ic->ic_bss->ni_rates;
1032 if (ic->ic_newassoc != NULL)
1033 ic->ic_newassoc(ic, ni, 1);
1034 /* XXX not right for 802.1x/WPA */
1035 ieee80211_node_authorize(ic, ni);
1041 * Locate the node for sender, track state, and then pass the
1042 * (referenced) node up to the 802.11 layer for its use. We
1043 * are required to pass some node so we fall back to ic_bss
1044 * when this frame is from an unknown sender. The 802.11 layer
1045 * knows this means the sender wasn't in the node table and
1048 struct ieee80211_node *
1049 #ifdef IEEE80211_DEBUG_REFCNT
1050 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1051 const struct ieee80211_frame_min *wh, const char *func, int line)
1053 ieee80211_find_rxnode(struct ieee80211com *ic,
1054 const struct ieee80211_frame_min *wh)
1057 #define IS_CTL(wh) \
1058 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1059 #define IS_PSPOLL(wh) \
1060 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1061 struct ieee80211_node_table *nt;
1062 struct ieee80211_node *ni;
1064 /* XXX may want scanned nodes in the neighbor table for adhoc */
1065 if (ic->ic_opmode == IEEE80211_M_STA ||
1066 ic->ic_opmode == IEEE80211_M_MONITOR ||
1067 (ic->ic_flags & IEEE80211_F_SCAN))
1071 /* XXX check ic_bss first in station mode */
1072 /* XXX 4-address frames? */
1073 IEEE80211_NODE_LOCK(nt);
1074 if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1075 ni = _ieee80211_find_node(nt, wh->i_addr1);
1077 ni = _ieee80211_find_node(nt, wh->i_addr2);
1078 IEEE80211_NODE_UNLOCK(nt);
1080 return (ni != NULL ? ni : ieee80211_ref_node(ic->ic_bss));
1086 * Return a reference to the appropriate node for sending
1087 * a data frame. This handles node discovery in adhoc networks.
1089 struct ieee80211_node *
1090 #ifdef IEEE80211_DEBUG_REFCNT
1091 ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
1092 const char *func, int line)
1094 ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
1097 struct ieee80211_node_table *nt = &ic->ic_sta;
1098 struct ieee80211_node *ni;
1101 * The destination address should be in the node table
1102 * unless we are operating in station mode or this is a
1103 * multicast/broadcast frame.
1105 if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1106 return ieee80211_ref_node(ic->ic_bss);
1108 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1109 IEEE80211_NODE_LOCK(nt);
1110 ni = _ieee80211_find_node(nt, macaddr);
1111 IEEE80211_NODE_UNLOCK(nt);
1114 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1115 ic->ic_opmode == IEEE80211_M_AHDEMO) {
1117 * In adhoc mode cons up a node for the destination.
1118 * Note that we need an additional reference for the
1119 * caller to be consistent with _ieee80211_find_node.
1121 ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1123 (void) ieee80211_ref_node(ni);
1125 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1126 "[%s] no node, discard frame (%s)\n",
1127 ether_sprintf(macaddr), __func__);
1128 ic->ic_stats.is_tx_nonode++;
1135 * Like find but search based on the channel too.
1137 struct ieee80211_node *
1138 #ifdef IEEE80211_DEBUG_REFCNT
1139 ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
1140 const u_int8_t *macaddr, struct ieee80211_channel *chan,
1141 const char *func, int line)
1143 ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
1144 const u_int8_t *macaddr, struct ieee80211_channel *chan)
1147 struct ieee80211_node *ni;
1150 hash = IEEE80211_NODE_HASH(macaddr);
1151 IEEE80211_NODE_LOCK(nt);
1152 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1153 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1154 ni->ni_chan == chan) {
1155 ieee80211_ref_node(ni); /* mark referenced */
1156 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1157 #ifdef IEEE80211_DEBUG_REFCNT
1158 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1161 "%s %p<%s> refcnt %d\n", __func__,
1163 ni, ether_sprintf(ni->ni_macaddr),
1164 ieee80211_node_refcnt(ni));
1168 IEEE80211_NODE_UNLOCK(nt);
1173 * Like find but search based on the ssid too.
1175 struct ieee80211_node *
1176 #ifdef IEEE80211_DEBUG_REFCNT
1177 ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1178 const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
1179 const char *func, int line)
1181 ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1182 const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
1185 struct ieee80211com *ic = nt->nt_ic;
1186 struct ieee80211_node *ni;
1189 hash = IEEE80211_NODE_HASH(macaddr);
1190 IEEE80211_NODE_LOCK(nt);
1191 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1192 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1193 ni->ni_esslen == ic->ic_des_esslen &&
1194 memcmp(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen) == 0) {
1195 ieee80211_ref_node(ni); /* mark referenced */
1196 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1197 #ifdef IEEE80211_DEBUG_REFCNT
1198 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1201 "%s %p<%s> refcnt %d\n", __func__,
1203 ni, ether_sprintf(ni->ni_macaddr),
1204 ieee80211_node_refcnt(ni));
1208 IEEE80211_NODE_UNLOCK(nt);
1213 _ieee80211_free_node(struct ieee80211_node *ni)
1215 struct ieee80211com *ic = ni->ni_ic;
1216 struct ieee80211_node_table *nt = ni->ni_table;
1218 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1219 "%s %p<%s> in %s table\n", __func__, ni,
1220 ether_sprintf(ni->ni_macaddr),
1221 nt != NULL ? nt->nt_name : "<gone>");
1223 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1225 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1226 LIST_REMOVE(ni, ni_hash);
1228 ic->ic_node_free(ni);
1232 #ifdef IEEE80211_DEBUG_REFCNT
1233 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1235 ieee80211_free_node(struct ieee80211_node *ni)
1238 struct ieee80211_node_table *nt = ni->ni_table;
1240 #ifdef IEEE80211_DEBUG_REFCNT
1241 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1242 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1243 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1245 if (ieee80211_node_dectestref(ni)) {
1247 * Beware; if the node is marked gone then it's already
1248 * been removed from the table and we cannot assume the
1249 * table still exists. Regardless, there's no need to lock
1252 if (ni->ni_table != NULL) {
1253 IEEE80211_NODE_LOCK(nt);
1254 _ieee80211_free_node(ni);
1255 IEEE80211_NODE_UNLOCK(nt);
1257 _ieee80211_free_node(ni);
1262 * Reclaim a node. If this is the last reference count then
1263 * do the normal free work. Otherwise remove it from the node
1264 * table and mark it gone by clearing the back-reference.
1267 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1270 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1271 "%s: remove %p<%s> from %s table, refcnt %d\n",
1272 __func__, ni, ether_sprintf(ni->ni_macaddr),
1273 nt->nt_name, ieee80211_node_refcnt(ni)-1);
1274 if (!ieee80211_node_dectestref(ni)) {
1276 * Other references are present, just remove the
1277 * node from the table so it cannot be found. When
1278 * the references are dropped storage will be
1281 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1282 LIST_REMOVE(ni, ni_hash);
1283 ni->ni_table = NULL; /* clear reference */
1285 _ieee80211_free_node(ni);
1289 ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
1291 struct ieee80211com *ic = nt->nt_ic;
1292 struct ieee80211_node *ni;
1294 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1295 "%s: free all nodes in %s table\n", __func__, nt->nt_name);
1297 while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1298 if (ni->ni_associd != 0) {
1299 if (ic->ic_auth->ia_node_leave != NULL)
1300 ic->ic_auth->ia_node_leave(ic, ni);
1301 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1303 node_reclaim(nt, ni);
1305 ieee80211_reset_erp(ic);
1309 ieee80211_free_allnodes(struct ieee80211_node_table *nt)
1312 IEEE80211_NODE_LOCK(nt);
1313 ieee80211_free_allnodes_locked(nt);
1314 IEEE80211_NODE_UNLOCK(nt);
1318 * Timeout entries in the scan cache.
1321 ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
1323 struct ieee80211com *ic = nt->nt_ic;
1324 struct ieee80211_node *ni, *tni;
1326 IEEE80211_NODE_LOCK(nt);
1328 /* XXX belongs elsewhere */
1329 if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
1330 m_freem(ni->ni_rxfrag[0]);
1331 ni->ni_rxfrag[0] = NULL;
1333 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
1334 if (ni->ni_inact && --ni->ni_inact == 0) {
1335 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1336 "[%s] scan candidate purged from cache "
1337 "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1338 ieee80211_node_refcnt(ni));
1339 node_reclaim(nt, ni);
1342 IEEE80211_NODE_UNLOCK(nt);
1344 nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1348 * Timeout inactive stations and do related housekeeping.
1349 * Note that we cannot hold the node lock while sending a
1350 * frame as this would lead to a LOR. Instead we use a
1351 * generation number to mark nodes that we've scanned and
1352 * drop the lock and restart a scan if we have to time out
1353 * a node. Since we are single-threaded by virtue of
1354 * controlling the inactivity timer we can be sure this will
1355 * process each node only once.
1358 ieee80211_timeout_stations(struct ieee80211_node_table *nt)
1360 struct ieee80211com *ic = nt->nt_ic;
1361 struct ieee80211_node *ni;
1364 IEEE80211_SCAN_LOCK(nt);
1365 gen = nt->nt_scangen++;
1366 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1367 "%s: %s scangen %u\n", __func__, nt->nt_name, gen);
1369 IEEE80211_NODE_LOCK(nt);
1370 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1371 if (ni->ni_scangen == gen) /* previously handled */
1373 ni->ni_scangen = gen;
1375 * Free fragment if not needed anymore
1376 * (last fragment older than 1s).
1377 * XXX doesn't belong here
1379 if (ni->ni_rxfrag[0] != NULL &&
1380 ticks > ni->ni_rxfragstamp + hz) {
1381 m_freem(ni->ni_rxfrag[0]);
1382 ni->ni_rxfrag[0] = NULL;
1385 * Special case ourself; we may be idle for extended periods
1386 * of time and regardless reclaiming our state is wrong.
1388 if (ni == ic->ic_bss)
1391 if (ni->ni_associd != 0) {
1393 * Age frames on the power save queue. The
1394 * aging interval is 4 times the listen
1395 * interval specified by the station. This
1396 * number is factored into the age calculations
1397 * when the frame is placed on the queue. We
1398 * store ages as time differences we can check
1399 * and/or adjust only the head of the list.
1401 if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
1405 IEEE80211_NODE_SAVEQ_LOCK(ni);
1406 while (IF_POLL(&ni->ni_savedq, m) != NULL &&
1407 M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
1408 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
1409 _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
1414 M_AGE_SUB(m, IEEE80211_INACT_WAIT);
1415 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1418 IEEE80211_DPRINTF(ic,
1419 IEEE80211_MSG_POWER,
1420 "[%s] discard %u frames for age\n",
1421 ether_sprintf(ni->ni_macaddr),
1423 IEEE80211_NODE_STAT_ADD(ni,
1424 ps_discard, discard);
1425 if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1426 ic->ic_set_tim(ic, ni, 0);
1430 * Probe the station before time it out. We
1431 * send a null data frame which may not be
1432 * universally supported by drivers (need it
1433 * for ps-poll support so it should be...).
1435 if (0 < ni->ni_inact &&
1436 ni->ni_inact <= ic->ic_inact_probe) {
1437 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1438 "[%s] probe station due to inactivity\n",
1439 ether_sprintf(ni->ni_macaddr));
1440 IEEE80211_NODE_UNLOCK(nt);
1441 ieee80211_send_nulldata(ic, ni);
1446 if (ni->ni_inact <= 0) {
1447 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1448 "[%s] station timed out due to inactivity "
1449 "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1450 ieee80211_node_refcnt(ni));
1452 * Send a deauthenticate frame and drop the station.
1453 * This is somewhat complicated due to reference counts
1454 * and locking. At this point a station will typically
1455 * have a reference count of 1. ieee80211_node_leave
1456 * will do a "free" of the node which will drop the
1457 * reference count. But in the meantime a reference
1458 * wil be held by the deauth frame. The actual reclaim
1459 * of the node will happen either after the tx is
1460 * completed or by ieee80211_node_leave.
1462 * Separately we must drop the node lock before sending
1463 * in case the driver takes a lock, as this will result
1464 * in LOR between the node lock and the driver lock.
1466 IEEE80211_NODE_UNLOCK(nt);
1467 if (ni->ni_associd != 0) {
1468 IEEE80211_SEND_MGMT(ic, ni,
1469 IEEE80211_FC0_SUBTYPE_DEAUTH,
1470 IEEE80211_REASON_AUTH_EXPIRE);
1472 ieee80211_node_leave(ic, ni);
1473 ic->ic_stats.is_node_timeout++;
1477 IEEE80211_NODE_UNLOCK(nt);
1479 IEEE80211_SCAN_UNLOCK(nt);
1481 nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1485 ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1487 struct ieee80211_node *ni;
1490 IEEE80211_SCAN_LOCK(nt);
1491 gen = nt->nt_scangen++;
1493 IEEE80211_NODE_LOCK(nt);
1494 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1495 if (ni->ni_scangen != gen) {
1496 ni->ni_scangen = gen;
1497 (void) ieee80211_ref_node(ni);
1498 IEEE80211_NODE_UNLOCK(nt);
1500 ieee80211_free_node(ni);
1504 IEEE80211_NODE_UNLOCK(nt);
1506 IEEE80211_SCAN_UNLOCK(nt);
1510 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1512 printf("0x%p: mac %s refcnt %d\n", ni,
1513 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1514 printf("\tscangen %u authmode %u flags 0x%x\n",
1515 ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
1516 printf("\tassocid 0x%x txpower %u vlan %u\n",
1517 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
1518 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1520 ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
1521 ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
1522 ni->ni_rxfragstamp);
1523 printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
1524 ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
1525 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
1526 ether_sprintf(ni->ni_bssid),
1527 ni->ni_esslen, ni->ni_essid,
1528 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
1529 printf("\tfails %u inact %u txrate %u\n",
1530 ni->ni_fails, ni->ni_inact, ni->ni_txrate);
1534 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
1536 ieee80211_iterate_nodes(nt,
1537 (ieee80211_iter_func *) ieee80211_dump_node, nt);
1541 * Handle a station joining an 11g network.
1544 ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1548 * Station isn't capable of short slot time. Bump
1549 * the count of long slot time stations and disable
1550 * use of short slot time. Note that the actual switch
1551 * over to long slot time use may not occur until the
1552 * next beacon transmission (per sec. 7.3.1.4 of 11g).
1554 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1555 ic->ic_longslotsta++;
1556 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1557 "[%s] station needs long slot time, count %d\n",
1558 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
1559 /* XXX vap's w/ conflicting needs won't work */
1560 ieee80211_set_shortslottime(ic, 0);
1563 * If the new station is not an ERP station
1564 * then bump the counter and enable protection
1567 if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
1569 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1570 "[%s] station is !ERP, %d non-ERP stations associated\n",
1571 ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
1573 * If protection is configured, enable it.
1575 if (ic->ic_protmode != IEEE80211_PROT_NONE) {
1576 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1577 "%s: enable use of protection\n", __func__);
1578 ic->ic_flags |= IEEE80211_F_USEPROT;
1581 * If station does not support short preamble
1582 * then we must enable use of Barker preamble.
1584 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
1585 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1586 "[%s] station needs long preamble\n",
1587 ether_sprintf(ni->ni_macaddr));
1588 ic->ic_flags |= IEEE80211_F_USEBARKER;
1589 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1592 ni->ni_flags |= IEEE80211_NODE_ERP;
1596 ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
1600 if (ni->ni_associd == 0) {
1604 * It would be good to search the bitmap
1605 * more efficiently, but this will do for now.
1607 for (aid = 1; aid < ic->ic_max_aid; aid++) {
1608 if (!IEEE80211_AID_ISSET(aid,
1612 if (aid >= ic->ic_max_aid) {
1613 IEEE80211_SEND_MGMT(ic, ni, resp,
1614 IEEE80211_REASON_ASSOC_TOOMANY);
1615 ieee80211_node_leave(ic, ni);
1618 ni->ni_associd = aid | 0xc000;
1619 IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
1622 if (ic->ic_curmode == IEEE80211_MODE_11G ||
1623 ic->ic_curmode == IEEE80211_MODE_TURBO_G)
1624 ieee80211_node_join_11g(ic, ni);
1628 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1629 "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
1630 ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
1631 IEEE80211_NODE_AID(ni),
1632 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
1633 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
1634 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
1635 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
1638 /* give driver a chance to setup state like ni_txrate */
1639 if (ic->ic_newassoc != NULL)
1640 ic->ic_newassoc(ic, ni, newassoc);
1641 ni->ni_inact_reload = ic->ic_inact_auth;
1642 ni->ni_inact = ni->ni_inact_reload;
1643 IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
1644 /* tell the authenticator about new station */
1645 if (ic->ic_auth->ia_node_join != NULL)
1646 ic->ic_auth->ia_node_join(ic, ni);
1647 ieee80211_notify_node_join(ic, ni, newassoc);
1651 * Handle a station leaving an 11g network.
1654 ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1657 KASSERT(ic->ic_curmode == IEEE80211_MODE_11G ||
1658 ic->ic_curmode == IEEE80211_MODE_TURBO_G,
1659 ("not in 11g, curmode %x", ic->ic_curmode));
1662 * If a long slot station do the slot time bookkeeping.
1664 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1665 KASSERT(ic->ic_longslotsta > 0,
1666 ("bogus long slot station count %d", ic->ic_longslotsta));
1667 ic->ic_longslotsta--;
1668 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1669 "[%s] long slot time station leaves, count now %d\n",
1670 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
1671 if (ic->ic_longslotsta == 0) {
1673 * Re-enable use of short slot time if supported
1674 * and not operating in IBSS mode (per spec).
1676 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
1677 ic->ic_opmode != IEEE80211_M_IBSS) {
1678 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1679 "%s: re-enable use of short slot time\n",
1681 ieee80211_set_shortslottime(ic, 1);
1686 * If a non-ERP station do the protection-related bookkeeping.
1688 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
1689 KASSERT(ic->ic_nonerpsta > 0,
1690 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
1692 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1693 "[%s] non-ERP station leaves, count now %d\n",
1694 ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
1695 if (ic->ic_nonerpsta == 0) {
1696 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1697 "%s: disable use of protection\n", __func__);
1698 ic->ic_flags &= ~IEEE80211_F_USEPROT;
1699 /* XXX verify mode? */
1700 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
1701 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1702 "%s: re-enable use of short preamble\n",
1704 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1705 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1712 * Handle bookkeeping for station deauthentication/disassociation
1713 * when operating as an ap.
1716 ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
1718 struct ieee80211_node_table *nt = ni->ni_table;
1720 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1721 "[%s] station with aid %d leaves\n",
1722 ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
1724 KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
1725 ic->ic_opmode == IEEE80211_M_IBSS ||
1726 ic->ic_opmode == IEEE80211_M_AHDEMO,
1727 ("unexpected operating mode %u", ic->ic_opmode));
1729 * If node wasn't previously associated all
1730 * we need to do is reclaim the reference.
1732 /* XXX ibss mode bypasses 11g and notification */
1733 if (ni->ni_associd == 0)
1736 * Tell the authenticator the station is leaving.
1737 * Note that we must do this before yanking the
1738 * association id as the authenticator uses the
1739 * associd to locate it's state block.
1741 if (ic->ic_auth->ia_node_leave != NULL)
1742 ic->ic_auth->ia_node_leave(ic, ni);
1743 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1747 if (ic->ic_curmode == IEEE80211_MODE_11G ||
1748 ic->ic_curmode == IEEE80211_MODE_TURBO_G)
1749 ieee80211_node_leave_11g(ic, ni);
1751 * Cleanup station state. In particular clear various
1752 * state that might otherwise be reused if the node
1753 * is reused before the reference count goes to zero
1754 * (and memory is reclaimed).
1756 ieee80211_sta_leave(ic, ni);
1759 * Remove the node from any table it's recorded in and
1760 * drop the caller's reference. Removal from the table
1761 * is important to insure the node is not reprocessed
1765 IEEE80211_NODE_LOCK(nt);
1766 node_reclaim(nt, ni);
1767 IEEE80211_NODE_UNLOCK(nt);
1769 ieee80211_free_node(ni);
1773 ieee80211_getrssi(struct ieee80211com *ic)
1775 #define NZ(x) ((x) == 0 ? 1 : (x))
1776 struct ieee80211_node_table *nt = &ic->ic_sta;
1777 u_int32_t rssi_samples, rssi_total;
1778 struct ieee80211_node *ni;
1782 switch (ic->ic_opmode) {
1783 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
1785 TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
1786 if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
1788 rssi_total += ic->ic_node_getrssi(ni);
1791 case IEEE80211_M_AHDEMO: /* average of all neighbors */
1793 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1795 rssi_total += ic->ic_node_getrssi(ni);
1798 case IEEE80211_M_HOSTAP: /* average of all associated stations */
1800 TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
1801 if (IEEE80211_AID(ni->ni_associd) != 0) {
1803 rssi_total += ic->ic_node_getrssi(ni);
1806 case IEEE80211_M_MONITOR: /* XXX */
1807 case IEEE80211_M_STA: /* use stats from associated ap */
1809 if (ic->ic_bss != NULL)
1810 rssi_total = ic->ic_node_getrssi(ic->ic_bss);
1814 return rssi_total / NZ(rssi_samples);
1819 * Indicate whether there are frames queued for a station in power-save mode.
1822 ieee80211_set_tim(struct ieee80211com *ic, struct ieee80211_node *ni, int set)
1826 KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
1827 ic->ic_opmode == IEEE80211_M_IBSS,
1828 ("operating mode %u", ic->ic_opmode));
1830 aid = IEEE80211_AID(ni->ni_associd);
1831 KASSERT(aid < ic->ic_max_aid,
1832 ("bogus aid %u, max %u", aid, ic->ic_max_aid));
1834 IEEE80211_BEACON_LOCK(ic);
1835 if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
1837 setbit(ic->ic_tim_bitmap, aid);
1838 ic->ic_ps_pending++;
1840 clrbit(ic->ic_tim_bitmap, aid);
1841 ic->ic_ps_pending--;
1843 ic->ic_flags |= IEEE80211_F_TIMUPDATE;
1845 IEEE80211_BEACON_UNLOCK(ic);
1849 * Node table support.
1853 ieee80211_node_table_init(struct ieee80211com *ic,
1854 struct ieee80211_node_table *nt,
1855 const char *name, int inact,
1856 void (*timeout)(struct ieee80211_node_table *))
1859 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1860 "%s %s table, inact %u\n", __func__, name, inact);
1864 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1865 IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1866 TAILQ_INIT(&nt->nt_node);
1869 nt->nt_inact_init = inact;
1870 nt->nt_timeout = timeout;
1874 ieee80211_node_table_reset(struct ieee80211_node_table *nt)
1877 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1878 "%s %s table\n", __func__, nt->nt_name);
1880 IEEE80211_NODE_LOCK(nt);
1881 nt->nt_inact_timer = 0;
1882 ieee80211_free_allnodes_locked(nt);
1883 IEEE80211_NODE_UNLOCK(nt);
1887 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1890 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1891 "%s %s table\n", __func__, nt->nt_name);
1893 ieee80211_free_allnodes_locked(nt);
1894 IEEE80211_SCAN_LOCK_DESTROY(nt);
1895 IEEE80211_NODE_LOCK_DESTROY(nt);