ieee80211_node.c revision 127648
1116742Ssam/*- 2116904Ssam * Copyright (c) 2001 Atsushi Onoe 3116742Ssam * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting 4116742Ssam * All rights reserved. 5116742Ssam * 6116742Ssam * Redistribution and use in source and binary forms, with or without 7116742Ssam * modification, are permitted provided that the following conditions 8116742Ssam * are met: 9116742Ssam * 1. Redistributions of source code must retain the above copyright 10116904Ssam * notice, this list of conditions and the following disclaimer. 11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright 12116904Ssam * notice, this list of conditions and the following disclaimer in the 13116904Ssam * documentation and/or other materials provided with the distribution. 14116904Ssam * 3. The name of the author may not be used to endorse or promote products 15116904Ssam * derived from this software without specific prior written permission. 16116742Ssam * 17116742Ssam * Alternatively, this software may be distributed under the terms of the 18116742Ssam * GNU General Public License ("GPL") version 2 as published by the Free 19116742Ssam * Software Foundation. 20116742Ssam * 21116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31116742Ssam */ 32116742Ssam 33116742Ssam#include <sys/cdefs.h> 34116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 127648 2004-03-30 22:57:57Z sam $"); 35116742Ssam 36116742Ssam#include "opt_inet.h" 37116742Ssam 38116742Ssam#include <sys/param.h> 39116742Ssam#include <sys/systm.h> 40116742Ssam#include <sys/mbuf.h> 41116742Ssam#include <sys/malloc.h> 42116742Ssam#include <sys/kernel.h> 43116742Ssam#include <sys/socket.h> 44116742Ssam#include <sys/sockio.h> 45116742Ssam#include <sys/endian.h> 46116742Ssam#include <sys/errno.h> 47116742Ssam#include <sys/bus.h> 48116742Ssam#include <sys/proc.h> 49116742Ssam#include <sys/sysctl.h> 50116742Ssam 51116742Ssam#include <machine/atomic.h> 52116742Ssam 53116742Ssam#include <net/if.h> 54116742Ssam#include <net/if_dl.h> 55116742Ssam#include <net/if_media.h> 56116742Ssam#include <net/if_arp.h> 57116742Ssam#include <net/ethernet.h> 58116742Ssam#include <net/if_llc.h> 59116742Ssam 60116742Ssam#include <net80211/ieee80211_var.h> 61116742Ssam 62116742Ssam#include <net/bpf.h> 63116742Ssam 64116742Ssam#ifdef INET 65116742Ssam#include <netinet/in.h> 66116742Ssam#include <netinet/if_ether.h> 67116742Ssam#endif 68116742Ssam 69116742Ssamstatic struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *); 70116742Ssamstatic void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *); 71116742Ssamstatic void ieee80211_node_copy(struct ieee80211com *, 72116742Ssam struct ieee80211_node *, const struct ieee80211_node *); 73120104Ssamstatic u_int8_t ieee80211_node_getrssi(struct ieee80211com *, 74120104Ssam struct ieee80211_node *); 75120104Ssam 76116742Ssamstatic void ieee80211_setup_node(struct ieee80211com *ic, 77116742Ssam struct ieee80211_node *ni, u_int8_t *macaddr); 78116742Ssamstatic void _ieee80211_free_node(struct ieee80211com *, 79116742Ssam struct ieee80211_node *); 80116742Ssam 81120481SsamMALLOC_DEFINE(M_80211_NODE, "node", "802.11 node state"); 82120481Ssam 83116742Ssamvoid 84116742Ssamieee80211_node_attach(struct ifnet *ifp) 85116742Ssam{ 86116742Ssam struct ieee80211com *ic = (void *)ifp; 87116742Ssam 88116742Ssam /* XXX need unit */ 89121816Sbrooks IEEE80211_NODE_LOCK_INIT(ic, ifp->if_xname); 90116742Ssam TAILQ_INIT(&ic->ic_node); 91116742Ssam ic->ic_node_alloc = ieee80211_node_alloc; 92116742Ssam ic->ic_node_free = ieee80211_node_free; 93116742Ssam ic->ic_node_copy = ieee80211_node_copy; 94120104Ssam ic->ic_node_getrssi = ieee80211_node_getrssi; 95120483Ssam ic->ic_scangen = 1; 96118887Ssam} 97118887Ssam 98118887Ssamvoid 99118887Ssamieee80211_node_lateattach(struct ifnet *ifp) 100118887Ssam{ 101118887Ssam struct ieee80211com *ic = (void *)ifp; 102118887Ssam 103116742Ssam ic->ic_bss = (*ic->ic_node_alloc)(ic); 104117041Ssam KASSERT(ic->ic_bss != NULL, ("unable to setup inital BSS node")); 105127648Ssam ic->ic_txpower = IEEE80211_TXPOWER_MAX; 106117041Ssam ic->ic_bss->ni_chan = IEEE80211_CHAN_ANYC; 107116742Ssam} 108116742Ssam 109116742Ssamvoid 110116742Ssamieee80211_node_detach(struct ifnet *ifp) 111116742Ssam{ 112116742Ssam struct ieee80211com *ic = (void *)ifp; 113116742Ssam 114116742Ssam if (ic->ic_bss != NULL) 115116742Ssam (*ic->ic_node_free)(ic, ic->ic_bss); 116116742Ssam ieee80211_free_allnodes(ic); 117121172Ssam IEEE80211_NODE_LOCK_DESTROY(ic); 118116742Ssam} 119116742Ssam 120116742Ssam/* 121116742Ssam * AP scanning support. 122116742Ssam */ 123116742Ssam 124116742Ssam/* 125116742Ssam * Initialize the active channel set based on the set 126116742Ssam * of available channels and the current PHY mode. 127116742Ssam */ 128117811Ssamstatic void 129116742Ssamieee80211_reset_scan(struct ifnet *ifp) 130116742Ssam{ 131116742Ssam struct ieee80211com *ic = (void *)ifp; 132116742Ssam 133116742Ssam memcpy(ic->ic_chan_scan, ic->ic_chan_active, 134116742Ssam sizeof(ic->ic_chan_active)); 135117811Ssam /* NB: hack, setup so next_scan starts with the first channel */ 136117811Ssam if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC) 137117811Ssam ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX]; 138116742Ssam} 139116742Ssam 140116742Ssam/* 141116742Ssam * Begin an active scan. 142116742Ssam */ 143116742Ssamvoid 144117811Ssamieee80211_begin_scan(struct ifnet *ifp) 145116742Ssam{ 146116742Ssam struct ieee80211com *ic = (void *)ifp; 147116742Ssam 148117811Ssam /* 149117811Ssam * In all but hostap mode scanning starts off in 150117811Ssam * an active mode before switching to passive. 151117811Ssam */ 152121180Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP) { 153117811Ssam ic->ic_flags |= IEEE80211_F_ASCAN; 154121180Ssam ic->ic_stats.is_scan_active++; 155121180Ssam } else 156121180Ssam ic->ic_stats.is_scan_passive++; 157116742Ssam if (ifp->if_flags & IFF_DEBUG) 158116742Ssam if_printf(ifp, "begin %s scan\n", 159117811Ssam (ic->ic_flags & IEEE80211_F_ASCAN) ? 160116742Ssam "active" : "passive"); 161116742Ssam /* 162117811Ssam * Clear scan state and flush any previously seen 163117811Ssam * AP's. Note that the latter assumes we don't act 164117811Ssam * as both an AP and a station, otherwise we'll 165117811Ssam * potentially flush state of stations associated 166117811Ssam * with us. 167116742Ssam */ 168117811Ssam ieee80211_reset_scan(ifp); 169116742Ssam ieee80211_free_allnodes(ic); 170116742Ssam 171117811Ssam /* Scan the next channel. */ 172117811Ssam ieee80211_next_scan(ifp); 173116742Ssam} 174116742Ssam 175116742Ssam/* 176116742Ssam * Switch to the next channel marked for scanning. 177116742Ssam */ 178116742Ssamvoid 179116742Ssamieee80211_next_scan(struct ifnet *ifp) 180116742Ssam{ 181116742Ssam struct ieee80211com *ic = (void *)ifp; 182116742Ssam struct ieee80211_channel *chan; 183116742Ssam 184116742Ssam chan = ic->ic_bss->ni_chan; 185116742Ssam for (;;) { 186116742Ssam if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX]) 187116742Ssam chan = &ic->ic_channels[0]; 188116742Ssam if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) { 189116742Ssam /* 190116742Ssam * Honor channels marked passive-only 191116742Ssam * during an active scan. 192116742Ssam */ 193116742Ssam if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 || 194116742Ssam (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) 195116742Ssam break; 196116742Ssam } 197116742Ssam if (chan == ic->ic_bss->ni_chan) { 198116742Ssam ieee80211_end_scan(ifp); 199116742Ssam return; 200116742Ssam } 201116742Ssam } 202116742Ssam clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan)); 203116742Ssam IEEE80211_DPRINTF(("ieee80211_next_scan: chan %d->%d\n", 204116742Ssam ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan), 205116742Ssam ieee80211_chan2ieee(ic, chan))); 206116742Ssam ic->ic_bss->ni_chan = chan; 207117811Ssam ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 208116742Ssam} 209116742Ssam 210116742Ssamvoid 211116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 212116742Ssam{ 213116742Ssam struct ieee80211_node *ni; 214116742Ssam struct ifnet *ifp = &ic->ic_if; 215116742Ssam 216116742Ssam ni = ic->ic_bss; 217116742Ssam if (ifp->if_flags & IFF_DEBUG) 218116742Ssam if_printf(ifp, "creating ibss\n"); 219116742Ssam ic->ic_flags |= IEEE80211_F_SIBSS; 220116742Ssam ni->ni_chan = chan; 221116742Ssam ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)]; 222116742Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr); 223116742Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 224116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) 225116742Ssam ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */ 226116742Ssam ni->ni_esslen = ic->ic_des_esslen; 227116742Ssam memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 228116742Ssam ni->ni_rssi = 0; 229116742Ssam ni->ni_rstamp = 0; 230116742Ssam memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp)); 231116742Ssam ni->ni_intval = ic->ic_lintval; 232116742Ssam ni->ni_capinfo = IEEE80211_CAPINFO_IBSS; 233116742Ssam if (ic->ic_flags & IEEE80211_F_WEPON) 234116742Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 235116742Ssam if (ic->ic_phytype == IEEE80211_T_FH) { 236116742Ssam ni->ni_fhdwell = 200; /* XXX */ 237116742Ssam ni->ni_fhindex = 1; 238116742Ssam } 239117811Ssam ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 240116742Ssam} 241116742Ssam 242116742Ssam/* 243116742Ssam * Complete a scan of potential channels. 244116742Ssam */ 245116742Ssamvoid 246116742Ssamieee80211_end_scan(struct ifnet *ifp) 247116742Ssam{ 248116742Ssam struct ieee80211com *ic = (void *)ifp; 249116742Ssam struct ieee80211_node *ni, *nextbs, *selbs; 250116742Ssam u_int8_t rate; 251116742Ssam int i, fail; 252116742Ssam 253116742Ssam ic->ic_flags &= ~IEEE80211_F_ASCAN; 254116742Ssam ni = TAILQ_FIRST(&ic->ic_node); 255116742Ssam 256116742Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 257116742Ssam /* XXX off stack? */ 258116742Ssam u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)]; 259116742Ssam /* 260116742Ssam * The passive scan to look for existing AP's completed, 261116742Ssam * select a channel to camp on. Identify the channels 262116742Ssam * that already have one or more AP's and try to locate 263116742Ssam * an unnoccupied one. If that fails, pick a random 264116742Ssam * channel from the active set. 265116742Ssam */ 266116742Ssam for (; ni != NULL; ni = nextbs) { 267116742Ssam ieee80211_ref_node(ni); 268116742Ssam nextbs = TAILQ_NEXT(ni, ni_list); 269116742Ssam setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan)); 270116742Ssam ieee80211_free_node(ic, ni); 271116742Ssam } 272116742Ssam for (i = 0; i < IEEE80211_CHAN_MAX; i++) 273116742Ssam if (isset(ic->ic_chan_active, i) && isclr(occupied, i)) 274116742Ssam break; 275116742Ssam if (i == IEEE80211_CHAN_MAX) { 276116742Ssam fail = arc4random() & 3; /* random 0-3 */ 277116742Ssam for (i = 0; i < IEEE80211_CHAN_MAX; i++) 278116742Ssam if (isset(ic->ic_chan_active, i) && fail-- == 0) 279116742Ssam break; 280116742Ssam } 281116742Ssam ieee80211_create_ibss(ic, &ic->ic_channels[i]); 282116742Ssam return; 283116742Ssam } 284116742Ssam if (ni == NULL) { 285116742Ssam IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__)); 286116742Ssam notfound: 287116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS && 288116742Ssam (ic->ic_flags & IEEE80211_F_IBSSON) && 289116742Ssam ic->ic_des_esslen != 0) { 290116742Ssam ieee80211_create_ibss(ic, ic->ic_ibss_chan); 291116742Ssam return; 292116742Ssam } 293116742Ssam /* 294116742Ssam * Reset the list of channels to scan and start again. 295116742Ssam */ 296116742Ssam ieee80211_reset_scan(ifp); 297116742Ssam ieee80211_next_scan(ifp); 298116742Ssam return; 299116742Ssam } 300116742Ssam selbs = NULL; 301116742Ssam if (ifp->if_flags & IFF_DEBUG) 302119150Ssam if_printf(ifp, "\tmacaddr bssid chan rssi rate flag wep essid\n"); 303116742Ssam for (; ni != NULL; ni = nextbs) { 304116742Ssam ieee80211_ref_node(ni); 305116742Ssam nextbs = TAILQ_NEXT(ni, ni_list); 306116742Ssam if (ni->ni_fails) { 307116742Ssam /* 308116742Ssam * The configuration of the access points may change 309116742Ssam * during my scan. So delete the entry for the AP 310116742Ssam * and retry to associate if there is another beacon. 311116742Ssam */ 312116742Ssam if (ni->ni_fails++ > 2) 313116742Ssam ieee80211_free_node(ic, ni); 314116742Ssam continue; 315116742Ssam } 316116742Ssam fail = 0; 317116742Ssam if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 318116742Ssam fail |= 0x01; 319116742Ssam if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 320116742Ssam ni->ni_chan != ic->ic_des_chan) 321116742Ssam fail |= 0x01; 322116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 323116742Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 324116742Ssam fail |= 0x02; 325116742Ssam } else { 326116742Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 327116742Ssam fail |= 0x02; 328116742Ssam } 329116742Ssam if (ic->ic_flags & IEEE80211_F_WEPON) { 330116742Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 331116742Ssam fail |= 0x04; 332116742Ssam } else { 333116742Ssam if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 334116742Ssam fail |= 0x04; 335116742Ssam } 336116742Ssam rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO); 337116742Ssam if (rate & IEEE80211_RATE_BASIC) 338116742Ssam fail |= 0x08; 339116742Ssam if (ic->ic_des_esslen != 0 && 340116742Ssam (ni->ni_esslen != ic->ic_des_esslen || 341122374Ssam memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) 342116742Ssam fail |= 0x10; 343116742Ssam if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 344116742Ssam !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 345116742Ssam fail |= 0x20; 346116742Ssam if (ifp->if_flags & IFF_DEBUG) { 347116742Ssam printf(" %c %s", fail ? '-' : '+', 348116742Ssam ether_sprintf(ni->ni_macaddr)); 349116742Ssam printf(" %s%c", ether_sprintf(ni->ni_bssid), 350116742Ssam fail & 0x20 ? '!' : ' '); 351116742Ssam printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan), 352116742Ssam fail & 0x01 ? '!' : ' '); 353116742Ssam printf(" %+4d", ni->ni_rssi); 354116742Ssam printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 355116742Ssam fail & 0x08 ? '!' : ' '); 356116742Ssam printf(" %4s%c", 357116742Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 358116742Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 359116742Ssam "????", 360116742Ssam fail & 0x02 ? '!' : ' '); 361116742Ssam printf(" %3s%c ", 362116742Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 363116742Ssam "wep" : "no", 364116742Ssam fail & 0x04 ? '!' : ' '); 365116742Ssam ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 366116742Ssam printf("%s\n", fail & 0x10 ? "!" : ""); 367116742Ssam } 368116742Ssam if (!fail) { 369116742Ssam if (selbs == NULL) 370116742Ssam selbs = ni; 371116742Ssam else if (ni->ni_rssi > selbs->ni_rssi) { 372116742Ssam ieee80211_unref_node(&selbs); 373116742Ssam selbs = ni; 374116742Ssam } else 375116742Ssam ieee80211_unref_node(&ni); 376116742Ssam } else { 377116742Ssam ieee80211_unref_node(&ni); 378116742Ssam } 379116742Ssam } 380116742Ssam if (selbs == NULL) 381116742Ssam goto notfound; 382116742Ssam (*ic->ic_node_copy)(ic, ic->ic_bss, selbs); 383116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 384116742Ssam ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE | 385116742Ssam IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 386116742Ssam if (ic->ic_bss->ni_rates.rs_nrates == 0) { 387116742Ssam selbs->ni_fails++; 388116742Ssam ieee80211_unref_node(&selbs); 389116742Ssam goto notfound; 390116742Ssam } 391116742Ssam ieee80211_unref_node(&selbs); 392117811Ssam ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 393116742Ssam } else { 394116742Ssam ieee80211_unref_node(&selbs); 395117811Ssam ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 396116742Ssam } 397116742Ssam} 398116742Ssam 399116742Ssamstatic struct ieee80211_node * 400116742Ssamieee80211_node_alloc(struct ieee80211com *ic) 401116742Ssam{ 402120481Ssam return malloc(sizeof(struct ieee80211_node), M_80211_NODE, 403116742Ssam M_NOWAIT | M_ZERO); 404116742Ssam} 405116742Ssam 406116742Ssamstatic void 407116742Ssamieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni) 408116742Ssam{ 409120481Ssam free(ni, M_80211_NODE); 410116742Ssam} 411116742Ssam 412116742Ssamstatic void 413116742Ssamieee80211_node_copy(struct ieee80211com *ic, 414116742Ssam struct ieee80211_node *dst, const struct ieee80211_node *src) 415116742Ssam{ 416116742Ssam *dst = *src; 417116742Ssam} 418116742Ssam 419120104Ssamstatic u_int8_t 420120104Ssamieee80211_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni) 421120104Ssam{ 422120104Ssam return ni->ni_rssi; 423120104Ssam} 424120104Ssam 425116742Ssamstatic void 426116742Ssamieee80211_setup_node(struct ieee80211com *ic, 427116742Ssam struct ieee80211_node *ni, u_int8_t *macaddr) 428116742Ssam{ 429116742Ssam int hash; 430116742Ssam 431116742Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 432116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 433116742Ssam ni->ni_refcnt = 1; /* mark referenced */ 434121172Ssam IEEE80211_NODE_LOCK(ic); 435116742Ssam TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list); 436116742Ssam LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash); 437116742Ssam /* 438116742Ssam * Note we don't enable the inactive timer when acting 439116742Ssam * as a station. Nodes created in this mode represent 440116742Ssam * AP's identified while scanning. If we time them out 441116742Ssam * then several things happen: we can't return the data 442116742Ssam * to users to show the list of AP's we encountered, and 443116742Ssam * more importantly, we'll incorrectly deauthenticate 444116742Ssam * ourself because the inactivity timer will kick us off. 445116742Ssam */ 446116742Ssam if (ic->ic_opmode != IEEE80211_M_STA) 447116742Ssam ic->ic_inact_timer = IEEE80211_INACT_WAIT; 448121172Ssam IEEE80211_NODE_UNLOCK(ic); 449116742Ssam} 450116742Ssam 451116742Ssamstruct ieee80211_node * 452116742Ssamieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr) 453116742Ssam{ 454116742Ssam struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic); 455116742Ssam if (ni != NULL) 456116742Ssam ieee80211_setup_node(ic, ni, macaddr); 457116742Ssam return ni; 458116742Ssam} 459116742Ssam 460116742Ssamstruct ieee80211_node * 461116742Ssamieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr) 462116742Ssam{ 463116742Ssam struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic); 464116742Ssam if (ni != NULL) { 465116742Ssam memcpy(ni, ic->ic_bss, sizeof(struct ieee80211_node)); 466116742Ssam ieee80211_setup_node(ic, ni, macaddr); 467116742Ssam } 468116742Ssam return ni; 469116742Ssam} 470116742Ssam 471116742Ssamstruct ieee80211_node * 472116742Ssamieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr) 473116742Ssam{ 474116742Ssam struct ieee80211_node *ni; 475116742Ssam int hash; 476116742Ssam 477116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 478121172Ssam IEEE80211_NODE_LOCK(ic); 479116742Ssam LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) { 480116742Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 481116742Ssam atomic_add_int(&ni->ni_refcnt, 1); /* mark referenced */ 482116742Ssam break; 483116742Ssam } 484116742Ssam } 485121172Ssam IEEE80211_NODE_UNLOCK(ic); 486116742Ssam return ni; 487116742Ssam} 488116742Ssam 489116742Ssam/* 490116742Ssam * Like find but search based on the channel too. 491116742Ssam */ 492116742Ssamstruct ieee80211_node * 493116742Ssamieee80211_lookup_node(struct ieee80211com *ic, 494116742Ssam u_int8_t *macaddr, struct ieee80211_channel *chan) 495116742Ssam{ 496116742Ssam struct ieee80211_node *ni; 497116742Ssam int hash; 498116742Ssam 499116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 500121172Ssam IEEE80211_NODE_LOCK(ic); 501116742Ssam LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) { 502116742Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && ni->ni_chan == chan) { 503116742Ssam atomic_add_int(&ni->ni_refcnt, 1);/* mark referenced */ 504116742Ssam break; 505116742Ssam } 506116742Ssam } 507121172Ssam IEEE80211_NODE_UNLOCK(ic); 508116742Ssam return ni; 509116742Ssam} 510116742Ssam 511116742Ssamstatic void 512116742Ssam_ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni) 513116742Ssam{ 514119150Ssam KASSERT(ni != ic->ic_bss, ("freeing bss node")); 515119150Ssam 516116742Ssam TAILQ_REMOVE(&ic->ic_node, ni, ni_list); 517116742Ssam LIST_REMOVE(ni, ni_hash); 518116742Ssam if (TAILQ_EMPTY(&ic->ic_node)) 519116742Ssam ic->ic_inact_timer = 0; 520116742Ssam (*ic->ic_node_free)(ic, ni); 521116742Ssam} 522116742Ssam 523116742Ssamvoid 524116742Ssamieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni) 525116742Ssam{ 526119150Ssam KASSERT(ni != ic->ic_bss, ("freeing ic_bss")); 527119150Ssam 528116742Ssam /* XXX need equivalent of atomic_dec_and_test */ 529116742Ssam atomic_subtract_int(&ni->ni_refcnt, 1); 530116742Ssam if (atomic_cmpset_int(&ni->ni_refcnt, 0, 1)) { 531121172Ssam IEEE80211_NODE_LOCK(ic); 532116742Ssam _ieee80211_free_node(ic, ni); 533121172Ssam IEEE80211_NODE_UNLOCK(ic); 534116742Ssam } 535116742Ssam} 536116742Ssam 537116742Ssamvoid 538116742Ssamieee80211_free_allnodes(struct ieee80211com *ic) 539116742Ssam{ 540116742Ssam struct ieee80211_node *ni; 541116742Ssam 542121172Ssam IEEE80211_NODE_LOCK(ic); 543116742Ssam while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL) 544116742Ssam _ieee80211_free_node(ic, ni); 545121172Ssam IEEE80211_NODE_UNLOCK(ic); 546116742Ssam} 547116742Ssam 548120483Ssam/* 549120483Ssam * Timeout inactive nodes. Note that we cannot hold the node 550120483Ssam * lock while sending a frame as this would lead to a LOR. 551120483Ssam * Instead we use a generation number to mark nodes that we've 552120483Ssam * scanned and drop the lock and restart a scan if we have to 553120483Ssam * time out a node. Since we are single-threaded by virtue of 554120483Ssam * controlling the inactivity timer we can be sure this will 555120483Ssam * process each node only once. 556120483Ssam */ 557116742Ssamvoid 558116742Ssamieee80211_timeout_nodes(struct ieee80211com *ic) 559116742Ssam{ 560120483Ssam struct ieee80211_node *ni; 561120483Ssam u_int gen = ic->ic_scangen++; /* NB: ok 'cuz single-threaded*/ 562116742Ssam 563120483Ssamrestart: 564121172Ssam IEEE80211_NODE_LOCK(ic); 565120483Ssam TAILQ_FOREACH(ni, &ic->ic_node, ni_list) { 566120483Ssam if (ni->ni_scangen == gen) /* previously handled */ 567120483Ssam continue; 568120483Ssam ni->ni_scangen = gen; 569119150Ssam if (++ni->ni_inact > IEEE80211_INACT_MAX) { 570119150Ssam IEEE80211_DPRINTF(("station %s timed out " 571116742Ssam "due to inactivity (%u secs)\n", 572116742Ssam ether_sprintf(ni->ni_macaddr), 573116742Ssam ni->ni_inact)); 574119150Ssam /* 575119150Ssam * Send a deauthenticate frame. 576120483Ssam * 577120483Ssam * Drop the node lock before sending the 578120483Ssam * deauthentication frame in case the driver takes 579120483Ssam * a lock, as this will result in a LOR between the 580120483Ssam * node lock and the driver lock. 581119150Ssam */ 582121172Ssam IEEE80211_NODE_UNLOCK(ic); 583119150Ssam IEEE80211_SEND_MGMT(ic, ni, 584119150Ssam IEEE80211_FC0_SUBTYPE_DEAUTH, 585119150Ssam IEEE80211_REASON_AUTH_EXPIRE); 586119150Ssam ieee80211_free_node(ic, ni); 587121180Ssam ic->ic_stats.is_node_timeout++; 588120483Ssam goto restart; 589120483Ssam } 590116742Ssam } 591116742Ssam if (!TAILQ_EMPTY(&ic->ic_node)) 592116742Ssam ic->ic_inact_timer = IEEE80211_INACT_WAIT; 593121172Ssam IEEE80211_NODE_UNLOCK(ic); 594116742Ssam} 595116742Ssam 596116742Ssamvoid 597116742Ssamieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg) 598116742Ssam{ 599116742Ssam struct ieee80211_node *ni; 600116742Ssam 601121172Ssam IEEE80211_NODE_LOCK(ic); 602116742Ssam TAILQ_FOREACH(ni, &ic->ic_node, ni_list) 603116742Ssam (*f)(arg, ni); 604121172Ssam IEEE80211_NODE_UNLOCK(ic); 605116742Ssam} 606