ieee80211_node.c revision 117811
172445Sassar/*- 2233294Sstas * Copyright (c) 2001 Atsushi Onoe 372445Sassar * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting 472445Sassar * All rights reserved. 5233294Sstas * 672445Sassar * Redistribution and use in source and binary forms, with or without 772445Sassar * modification, are permitted provided that the following conditions 872445Sassar * are met: 9233294Sstas * 1. Redistributions of source code must retain the above copyright 1072445Sassar * notice, this list of conditions and the following disclaimer. 1172445Sassar * 2. Redistributions in binary form must reproduce the above copyright 12233294Sstas * notice, this list of conditions and the following disclaimer in the 1372445Sassar * documentation and/or other materials provided with the distribution. 1472445Sassar * 3. The name of the author may not be used to endorse or promote products 1572445Sassar * derived from this software without specific prior written permission. 16233294Sstas * 1772445Sassar * Alternatively, this software may be distributed under the terms of the 1872445Sassar * GNU General Public License ("GPL") version 2 as published by the Free 1972445Sassar * Software Foundation. 20233294Sstas * 2172445Sassar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2272445Sassar * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2372445Sassar * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2472445Sassar * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2572445Sassar * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2672445Sassar * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2772445Sassar * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2872445Sassar * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2972445Sassar * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3072445Sassar * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3172445Sassar */ 3272445Sassar 3372445Sassar#include <sys/cdefs.h> 3472445Sassar__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 117811 2003-07-20 21:36:08Z sam $"); 3572445Sassar 3672445Sassar#include "opt_inet.h" 3772445Sassar 3872445Sassar#include <sys/param.h> 3972445Sassar#include <sys/systm.h> 4072445Sassar#include <sys/mbuf.h> 4172445Sassar#include <sys/malloc.h> 4272445Sassar#include <sys/kernel.h> 4372445Sassar#include <sys/socket.h> 4472445Sassar#include <sys/sockio.h> 4572445Sassar#include <sys/endian.h> 4672445Sassar#include <sys/errno.h> 47178825Sdfr#include <sys/bus.h> 4872445Sassar#include <sys/proc.h> 49178825Sdfr#include <sys/sysctl.h> 50178825Sdfr 51178825Sdfr#include <machine/atomic.h> 52178825Sdfr 53178825Sdfr#include <net/if.h> 54178825Sdfr#include <net/if_dl.h> 55178825Sdfr#include <net/if_media.h> 56178825Sdfr#include <net/if_arp.h> 57178825Sdfr#include <net/ethernet.h> 58178825Sdfr#include <net/if_llc.h> 59178825Sdfr 60178825Sdfr#include <net80211/ieee80211_var.h> 61178825Sdfr 6272445Sassar#include <net/bpf.h> 63178825Sdfr 64178825Sdfr#ifdef INET 6572445Sassar#include <netinet/in.h> 6672445Sassar#include <netinet/if_ether.h> 6772445Sassar#endif 6872445Sassar 6972445Sassarstatic struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *); 7072445Sassarstatic void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *); 7172445Sassarstatic void ieee80211_node_copy(struct ieee80211com *, 7272445Sassar struct ieee80211_node *, const struct ieee80211_node *); 7372445Sassarstatic void ieee80211_setup_node(struct ieee80211com *ic, 7472445Sassar struct ieee80211_node *ni, u_int8_t *macaddr); 7572445Sassarstatic void _ieee80211_free_node(struct ieee80211com *, 7672445Sassar struct ieee80211_node *); 7772445Sassar 7872445Sassarvoid 7972445Sassarieee80211_node_attach(struct ifnet *ifp) 8072445Sassar{ 8172445Sassar struct ieee80211com *ic = (void *)ifp; 8272445Sassar 8372445Sassar /* XXX need unit */ 84233294Sstas mtx_init(&ic->ic_nodelock, ifp->if_name, "802.11 node table", MTX_DEF); 85233294Sstas TAILQ_INIT(&ic->ic_node); 86178825Sdfr ic->ic_node_alloc = ieee80211_node_alloc; 8772445Sassar ic->ic_node_free = ieee80211_node_free; 8872445Sassar ic->ic_node_copy = ieee80211_node_copy; 8972445Sassar ic->ic_bss = (*ic->ic_node_alloc)(ic); 9072445Sassar KASSERT(ic->ic_bss != NULL, ("unable to setup inital BSS node")); 9172445Sassar ic->ic_bss->ni_chan = IEEE80211_CHAN_ANYC; 9272445Sassar} 9372445Sassar 9472445Sassarvoid 9572445Sassarieee80211_node_detach(struct ifnet *ifp) 9672445Sassar{ 9772445Sassar struct ieee80211com *ic = (void *)ifp; 98178825Sdfr 99178825Sdfr if (ic->ic_bss != NULL) 100233294Sstas (*ic->ic_node_free)(ic, ic->ic_bss); 101233294Sstas ieee80211_free_allnodes(ic); 102233294Sstas mtx_destroy(&ic->ic_nodelock); 103178825Sdfr} 104178825Sdfr 105178825Sdfr/* 10672445Sassar * AP scanning support. 10772445Sassar */ 10872445Sassar 10972445Sassar/* 11072445Sassar * Initialize the active channel set based on the set 11172445Sassar * of available channels and the current PHY mode. 11272445Sassar */ 11372445Sassarstatic void 11472445Sassarieee80211_reset_scan(struct ifnet *ifp) 11572445Sassar{ 11672445Sassar struct ieee80211com *ic = (void *)ifp; 11772445Sassar 11872445Sassar memcpy(ic->ic_chan_scan, ic->ic_chan_active, 11972445Sassar sizeof(ic->ic_chan_active)); 12072445Sassar /* NB: hack, setup so next_scan starts with the first channel */ 12172445Sassar if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC) 12272445Sassar ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX]; 12372445Sassar} 124178825Sdfr 12572445Sassar/* 126178825Sdfr * Begin an active scan. 12772445Sassar */ 12872445Sassarvoid 12972445Sassarieee80211_begin_scan(struct ifnet *ifp) 13072445Sassar{ 13172445Sassar struct ieee80211com *ic = (void *)ifp; 13272445Sassar 13372445Sassar /* 13472445Sassar * In all but hostap mode scanning starts off in 13572445Sassar * an active mode before switching to passive. 13672445Sassar */ 13772445Sassar if (ic->ic_opmode != IEEE80211_M_HOSTAP) 13872445Sassar ic->ic_flags |= IEEE80211_F_ASCAN; 13972445Sassar if (ifp->if_flags & IFF_DEBUG) 140178825Sdfr if_printf(ifp, "begin %s scan\n", 14172445Sassar (ic->ic_flags & IEEE80211_F_ASCAN) ? 14272445Sassar "active" : "passive"); 143178825Sdfr /* 144178825Sdfr * Clear scan state and flush any previously seen 14572445Sassar * AP's. Note that the latter assumes we don't act 14672445Sassar * as both an AP and a station, otherwise we'll 14772445Sassar * potentially flush state of stations associated 148178825Sdfr * with us. 14972445Sassar */ 150178825Sdfr ieee80211_reset_scan(ifp); 151178825Sdfr ieee80211_free_allnodes(ic); 15272445Sassar 15372445Sassar /* Scan the next channel. */ 15472445Sassar ieee80211_next_scan(ifp); 155178825Sdfr} 156178825Sdfr 157178825Sdfr/* 158178825Sdfr * Switch to the next channel marked for scanning. 159178825Sdfr */ 160178825Sdfrvoid 161178825Sdfrieee80211_next_scan(struct ifnet *ifp) 162178825Sdfr{ 163178825Sdfr struct ieee80211com *ic = (void *)ifp; 164178825Sdfr struct ieee80211_channel *chan; 165178825Sdfr 166178825Sdfr chan = ic->ic_bss->ni_chan; 167178825Sdfr for (;;) { 168178825Sdfr if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX]) 169178825Sdfr chan = &ic->ic_channels[0]; 170178825Sdfr if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) { 171178825Sdfr /* 172178825Sdfr * Honor channels marked passive-only 173178825Sdfr * during an active scan. 174178825Sdfr */ 175178825Sdfr if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 || 176178825Sdfr (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) 177178825Sdfr break; 178178825Sdfr } 179178825Sdfr if (chan == ic->ic_bss->ni_chan) { 180178825Sdfr ieee80211_end_scan(ifp); 181178825Sdfr return; 182178825Sdfr } 183233294Sstas } 184178825Sdfr clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan)); 185178825Sdfr IEEE80211_DPRINTF(("ieee80211_next_scan: chan %d->%d\n", 186178825Sdfr ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan), 187178825Sdfr ieee80211_chan2ieee(ic, chan))); 188178825Sdfr ic->ic_bss->ni_chan = chan; 189178825Sdfr ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 190178825Sdfr} 191178825Sdfr 192178825Sdfrvoid 193178825Sdfrieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 194178825Sdfr{ 195178825Sdfr struct ieee80211_node *ni; 196178825Sdfr struct ifnet *ifp = &ic->ic_if; 197178825Sdfr 198178825Sdfr ni = ic->ic_bss; 199178825Sdfr if (ifp->if_flags & IFF_DEBUG) 20072445Sassar if_printf(ifp, "creating ibss\n"); 201233294Sstas ic->ic_flags |= IEEE80211_F_SIBSS; 20272445Sassar ni->ni_chan = chan; 203102644Snectar ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)]; 20472445Sassar IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr); 20572445Sassar IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 20672445Sassar if (ic->ic_opmode == IEEE80211_M_IBSS) 20772445Sassar ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */ 20878527Sassar ni->ni_esslen = ic->ic_des_esslen; 20972445Sassar memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 21072445Sassar ni->ni_rssi = 0; 21172445Sassar ni->ni_rstamp = 0; 21272445Sassar ni->ni_rantenna = 0; 21372445Sassar memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp)); 21472445Sassar ni->ni_intval = ic->ic_lintval; 21572445Sassar ni->ni_capinfo = IEEE80211_CAPINFO_IBSS; 21672445Sassar if (ic->ic_flags & IEEE80211_F_WEPON) 21772445Sassar ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 218102644Snectar if (ic->ic_phytype == IEEE80211_T_FH) { 219178825Sdfr ni->ni_fhdwell = 200; /* XXX */ 22078527Sassar ni->ni_fhindex = 1; 22178527Sassar } 22278527Sassar ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 223233294Sstas} 22478527Sassar 22578527Sassar/* 22672445Sassar * Complete a scan of potential channels. 227233294Sstas */ 228178825Sdfrvoid 229178825Sdfrieee80211_end_scan(struct ifnet *ifp) 230178825Sdfr{ 231178825Sdfr struct ieee80211com *ic = (void *)ifp; 232178825Sdfr struct ieee80211_node *ni, *nextbs, *selbs; 233178825Sdfr u_int8_t rate; 234178825Sdfr int i, fail; 235178825Sdfr 236178825Sdfr ic->ic_flags &= ~IEEE80211_F_ASCAN; 237178825Sdfr ni = TAILQ_FIRST(&ic->ic_node); 238178825Sdfr 239178825Sdfr if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 240178825Sdfr /* XXX off stack? */ 241178825Sdfr u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)]; 242178825Sdfr /* 243178825Sdfr * The passive scan to look for existing AP's completed, 244233294Sstas * select a channel to camp on. Identify the channels 24572445Sassar * that already have one or more AP's and try to locate 24672445Sassar * an unnoccupied one. If that fails, pick a random 24772445Sassar * channel from the active set. 24872445Sassar */ 24972445Sassar for (; ni != NULL; ni = nextbs) { 25072445Sassar ieee80211_ref_node(ni); 25172445Sassar nextbs = TAILQ_NEXT(ni, ni_list); 25272445Sassar setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan)); 25372445Sassar ieee80211_free_node(ic, ni); 25472445Sassar } 25578527Sassar for (i = 0; i < IEEE80211_CHAN_MAX; i++) 25672445Sassar if (isset(ic->ic_chan_active, i) && isclr(occupied, i)) 25772445Sassar break; 25878527Sassar if (i == IEEE80211_CHAN_MAX) { 25978527Sassar fail = arc4random() & 3; /* random 0-3 */ 260233294Sstas for (i = 0; i < IEEE80211_CHAN_MAX; i++) 261233294Sstas if (isset(ic->ic_chan_active, i) && fail-- == 0) 262233294Sstas break; 263233294Sstas } 26478527Sassar ieee80211_create_ibss(ic, &ic->ic_channels[i]); 26578527Sassar return; 266233294Sstas } 26778527Sassar if (ni == NULL) { 26872445Sassar IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__)); 26972445Sassar notfound: 27072445Sassar if (ic->ic_opmode == IEEE80211_M_IBSS && 27172445Sassar (ic->ic_flags & IEEE80211_F_IBSSON) && 27272445Sassar ic->ic_des_esslen != 0) { 27372445Sassar ieee80211_create_ibss(ic, ic->ic_ibss_chan); 27472445Sassar return; 27572445Sassar } 27678527Sassar /* 27772445Sassar * Reset the list of channels to scan and start again. 27872445Sassar */ 27972445Sassar ieee80211_reset_scan(ifp); 28072445Sassar ieee80211_next_scan(ifp); 28178527Sassar return; 28278527Sassar } 28372445Sassar selbs = NULL; 284178825Sdfr if (ifp->if_flags & IFF_DEBUG) 28572445Sassar if_printf(ifp, "\tmacaddr bssid chan rssi rate ant flag wep essid\n"); 28672445Sassar for (; ni != NULL; ni = nextbs) { 28772445Sassar ieee80211_ref_node(ni); 288178825Sdfr nextbs = TAILQ_NEXT(ni, ni_list); 28978527Sassar if (ni->ni_fails) { 29078527Sassar /* 29178527Sassar * The configuration of the access points may change 292233294Sstas * during my scan. So delete the entry for the AP 29378527Sassar * and retry to associate if there is another beacon. 29478527Sassar */ 29572445Sassar if (ni->ni_fails++ > 2) 296 ieee80211_free_node(ic, ni); 297 continue; 298 } 299 fail = 0; 300 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 301 fail |= 0x01; 302 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 303 ni->ni_chan != ic->ic_des_chan) 304 fail |= 0x01; 305 if (ic->ic_opmode == IEEE80211_M_IBSS) { 306 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 307 fail |= 0x02; 308 } else { 309 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 310 fail |= 0x02; 311 } 312 if (ic->ic_flags & IEEE80211_F_WEPON) { 313 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 314 fail |= 0x04; 315 } else { 316 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 317 fail |= 0x04; 318 } 319 rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO); 320 if (rate & IEEE80211_RATE_BASIC) 321 fail |= 0x08; 322 if (ic->ic_des_esslen != 0 && 323 (ni->ni_esslen != ic->ic_des_esslen || 324 memcmp(ni->ni_essid, ic->ic_des_essid, 325 ic->ic_des_esslen != 0))) 326 fail |= 0x10; 327 if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 328 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 329 fail |= 0x20; 330 if (ifp->if_flags & IFF_DEBUG) { 331 printf(" %c %s", fail ? '-' : '+', 332 ether_sprintf(ni->ni_macaddr)); 333 printf(" %s%c", ether_sprintf(ni->ni_bssid), 334 fail & 0x20 ? '!' : ' '); 335 printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan), 336 fail & 0x01 ? '!' : ' '); 337 printf(" %+4d", ni->ni_rssi); 338 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 339 fail & 0x08 ? '!' : ' '); 340 printf(" %3d", ni->ni_rantenna); 341 printf(" %4s%c", 342 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 343 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 344 "????", 345 fail & 0x02 ? '!' : ' '); 346 printf(" %3s%c ", 347 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 348 "wep" : "no", 349 fail & 0x04 ? '!' : ' '); 350 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 351 printf("%s\n", fail & 0x10 ? "!" : ""); 352 } 353 if (!fail) { 354 if (selbs == NULL) 355 selbs = ni; 356 else if (ni->ni_rssi > selbs->ni_rssi) { 357 ieee80211_unref_node(&selbs); 358 selbs = ni; 359 } else 360 ieee80211_unref_node(&ni); 361 } else { 362 ieee80211_unref_node(&ni); 363 } 364 } 365 if (selbs == NULL) 366 goto notfound; 367 (*ic->ic_node_copy)(ic, ic->ic_bss, selbs); 368 if (ic->ic_opmode == IEEE80211_M_IBSS) { 369 ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE | 370 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 371 if (ic->ic_bss->ni_rates.rs_nrates == 0) { 372 selbs->ni_fails++; 373 ieee80211_unref_node(&selbs); 374 goto notfound; 375 } 376 ieee80211_unref_node(&selbs); 377 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 378 } else { 379 ieee80211_unref_node(&selbs); 380 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 381 } 382} 383 384static struct ieee80211_node * 385ieee80211_node_alloc(struct ieee80211com *ic) 386{ 387 return malloc(sizeof(struct ieee80211_node), M_DEVBUF, 388 M_NOWAIT | M_ZERO); 389} 390 391static void 392ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni) 393{ 394 free(ni, M_DEVBUF); 395} 396 397static void 398ieee80211_node_copy(struct ieee80211com *ic, 399 struct ieee80211_node *dst, const struct ieee80211_node *src) 400{ 401 *dst = *src; 402} 403 404static void 405ieee80211_setup_node(struct ieee80211com *ic, 406 struct ieee80211_node *ni, u_int8_t *macaddr) 407{ 408 int hash; 409 410 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 411 hash = IEEE80211_NODE_HASH(macaddr); 412 ni->ni_refcnt = 1; /* mark referenced */ 413 mtx_lock(&ic->ic_nodelock); 414 TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list); 415 LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash); 416 /* 417 * Note we don't enable the inactive timer when acting 418 * as a station. Nodes created in this mode represent 419 * AP's identified while scanning. If we time them out 420 * then several things happen: we can't return the data 421 * to users to show the list of AP's we encountered, and 422 * more importantly, we'll incorrectly deauthenticate 423 * ourself because the inactivity timer will kick us off. 424 */ 425 if (ic->ic_opmode != IEEE80211_M_STA) 426 ic->ic_inact_timer = IEEE80211_INACT_WAIT; 427 mtx_unlock(&ic->ic_nodelock); 428} 429 430struct ieee80211_node * 431ieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr) 432{ 433 struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic); 434 if (ni != NULL) 435 ieee80211_setup_node(ic, ni, macaddr); 436 return ni; 437} 438 439struct ieee80211_node * 440ieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr) 441{ 442 struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic); 443 if (ni != NULL) { 444 memcpy(ni, ic->ic_bss, sizeof(struct ieee80211_node)); 445 ieee80211_setup_node(ic, ni, macaddr); 446 } 447 return ni; 448} 449 450struct ieee80211_node * 451ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr) 452{ 453 struct ieee80211_node *ni; 454 int hash; 455 456 hash = IEEE80211_NODE_HASH(macaddr); 457 mtx_lock(&ic->ic_nodelock); 458 LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) { 459 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 460 atomic_add_int(&ni->ni_refcnt, 1); /* mark referenced */ 461 break; 462 } 463 } 464 mtx_unlock(&ic->ic_nodelock); 465 return ni; 466} 467 468/* 469 * Like find but search based on the channel too. 470 */ 471struct ieee80211_node * 472ieee80211_lookup_node(struct ieee80211com *ic, 473 u_int8_t *macaddr, struct ieee80211_channel *chan) 474{ 475 struct ieee80211_node *ni; 476 int hash; 477 478 hash = IEEE80211_NODE_HASH(macaddr); 479 mtx_lock(&ic->ic_nodelock); 480 LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) { 481 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && ni->ni_chan == chan) { 482 atomic_add_int(&ni->ni_refcnt, 1);/* mark referenced */ 483 break; 484 } 485 } 486 mtx_unlock(&ic->ic_nodelock); 487 return ni; 488} 489 490static void 491_ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni) 492{ 493 TAILQ_REMOVE(&ic->ic_node, ni, ni_list); 494 LIST_REMOVE(ni, ni_hash); 495 if (TAILQ_EMPTY(&ic->ic_node)) 496 ic->ic_inact_timer = 0; 497 (*ic->ic_node_free)(ic, ni); 498} 499 500void 501ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni) 502{ 503 /* XXX need equivalent of atomic_dec_and_test */ 504 atomic_subtract_int(&ni->ni_refcnt, 1); 505 if (atomic_cmpset_int(&ni->ni_refcnt, 0, 1)) { 506 mtx_lock(&ic->ic_nodelock); 507 _ieee80211_free_node(ic, ni); 508 mtx_unlock(&ic->ic_nodelock); 509 } 510} 511 512void 513ieee80211_free_allnodes(struct ieee80211com *ic) 514{ 515 struct ieee80211_node *ni; 516 517 mtx_lock(&ic->ic_nodelock); 518 while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL) 519 _ieee80211_free_node(ic, ni); 520 mtx_unlock(&ic->ic_nodelock); 521} 522 523void 524ieee80211_timeout_nodes(struct ieee80211com *ic) 525{ 526 struct ieee80211_node *ni, *nextbs; 527 528 mtx_lock(&ic->ic_nodelock); 529 for (ni = TAILQ_FIRST(&ic->ic_node); ni != NULL;) { 530 if (++ni->ni_inact <= IEEE80211_INACT_MAX) { 531 ni = TAILQ_NEXT(ni, ni_list); 532 continue; 533 } 534 /* NB: don't honor reference count */ 535 IEEE80211_DPRINTF(("station %s timed out " 536 "due to inactivity (%u secs)\n", 537 ether_sprintf(ni->ni_macaddr), 538 ni->ni_inact)); 539 nextbs = TAILQ_NEXT(ni, ni_list); 540 IEEE80211_SEND_MGMT(ic, ni, 541 IEEE80211_FC0_SUBTYPE_DEAUTH, 542 IEEE80211_REASON_AUTH_EXPIRE); 543 _ieee80211_free_node(ic, ni); 544 ni = nextbs; 545 } 546 if (!TAILQ_EMPTY(&ic->ic_node)) 547 ic->ic_inact_timer = IEEE80211_INACT_WAIT; 548 mtx_unlock(&ic->ic_nodelock); 549} 550 551void 552ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg) 553{ 554 struct ieee80211_node *ni; 555 556 mtx_lock(&ic->ic_nodelock); 557 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) 558 (*f)(arg, ni); 559 mtx_unlock(&ic->ic_nodelock); 560} 561