ieee80211_node.c revision 140753
1116742Ssam/*- 2116904Ssam * Copyright (c) 2001 Atsushi Onoe 3139530Ssam * Copyright (c) 2002-2005 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 140753 2005-01-24 19:32:10Z sam $"); 35116742Ssam 36116742Ssam#include <sys/param.h> 37116742Ssam#include <sys/systm.h> 38116742Ssam#include <sys/mbuf.h> 39116742Ssam#include <sys/malloc.h> 40116742Ssam#include <sys/kernel.h> 41138568Ssam 42116742Ssam#include <sys/socket.h> 43116742Ssam 44116742Ssam#include <net/if.h> 45116742Ssam#include <net/if_media.h> 46116742Ssam#include <net/ethernet.h> 47116742Ssam 48116742Ssam#include <net80211/ieee80211_var.h> 49116742Ssam 50116742Ssam#include <net/bpf.h> 51116742Ssam 52138568Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211_node_table *); 53138568Ssamstatic void node_cleanup(struct ieee80211_node *); 54138568Ssamstatic void node_free(struct ieee80211_node *); 55138568Ssamstatic u_int8_t node_getrssi(const struct ieee80211_node *); 56116742Ssam 57138568Ssamstatic void ieee80211_setup_node(struct ieee80211_node_table *, 58138568Ssam struct ieee80211_node *, const u_int8_t *); 59138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *); 60138568Ssamstatic void ieee80211_free_allnodes(struct ieee80211_node_table *); 61120104Ssam 62138568Ssamstatic void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *); 63138568Ssamstatic void ieee80211_timeout_stations(struct ieee80211_node_table *); 64116742Ssam 65138568Ssamstatic void ieee80211_set_tim(struct ieee80211com *, 66138568Ssam struct ieee80211_node *, int set); 67138568Ssam 68138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic, 69138568Ssam struct ieee80211_node_table *nt, const char *name, int inact, 70138568Ssam void (*timeout)(struct ieee80211_node_table *)); 71138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt); 72138568Ssam 73127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state"); 74120481Ssam 75116742Ssamvoid 76138568Ssamieee80211_node_attach(struct ieee80211com *ic) 77116742Ssam{ 78116742Ssam 79140753Ssam ieee80211_node_table_init(ic, &ic->ic_sta, "station", 80140753Ssam IEEE80211_INACT_INIT, ieee80211_timeout_stations); 81138568Ssam ieee80211_node_table_init(ic, &ic->ic_scan, "scan", 82138568Ssam IEEE80211_INACT_SCAN, ieee80211_timeout_scan_candidates); 83138568Ssam 84138568Ssam ic->ic_node_alloc = node_alloc; 85138568Ssam ic->ic_node_free = node_free; 86138568Ssam ic->ic_node_cleanup = node_cleanup; 87138568Ssam ic->ic_node_getrssi = node_getrssi; 88138568Ssam 89138568Ssam /* default station inactivity timer setings */ 90138568Ssam ic->ic_inact_init = IEEE80211_INACT_INIT; 91138568Ssam ic->ic_inact_auth = IEEE80211_INACT_AUTH; 92138568Ssam ic->ic_inact_run = IEEE80211_INACT_RUN; 93138568Ssam ic->ic_inact_probe = IEEE80211_INACT_PROBE; 94138568Ssam 95138568Ssam /* XXX defer */ 96138568Ssam if (ic->ic_max_aid == 0) 97138568Ssam ic->ic_max_aid = IEEE80211_AID_DEF; 98138568Ssam else if (ic->ic_max_aid > IEEE80211_AID_MAX) 99138568Ssam ic->ic_max_aid = IEEE80211_AID_MAX; 100138568Ssam MALLOC(ic->ic_aid_bitmap, u_int32_t *, 101138568Ssam howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t), 102138568Ssam M_DEVBUF, M_NOWAIT | M_ZERO); 103138568Ssam if (ic->ic_aid_bitmap == NULL) { 104138568Ssam /* XXX no way to recover */ 105138568Ssam printf("%s: no memory for AID bitmap!\n", __func__); 106138568Ssam ic->ic_max_aid = 0; 107138568Ssam } 108138568Ssam 109138568Ssam /* XXX defer until using hostap/ibss mode */ 110138568Ssam ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t); 111138568Ssam MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len, 112138568Ssam M_DEVBUF, M_NOWAIT | M_ZERO); 113138568Ssam if (ic->ic_tim_bitmap == NULL) { 114138568Ssam /* XXX no way to recover */ 115138568Ssam printf("%s: no memory for TIM bitmap!\n", __func__); 116138568Ssam } 117138568Ssam ic->ic_set_tim = ieee80211_set_tim; /* NB: driver should override */ 118118887Ssam} 119118887Ssam 120118887Ssamvoid 121138568Ssamieee80211_node_lateattach(struct ieee80211com *ic) 122118887Ssam{ 123127766Ssam struct ieee80211_node *ni; 124138568Ssam struct ieee80211_rsnparms *rsn; 125118887Ssam 126138568Ssam ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr); 127127766Ssam KASSERT(ni != NULL, ("unable to setup inital BSS node")); 128138568Ssam /* 129138568Ssam * Setup "global settings" in the bss node so that 130138568Ssam * each new station automatically inherits them. 131138568Ssam */ 132138568Ssam rsn = &ni->ni_rsn; 133138568Ssam /* WEP, TKIP, and AES-CCM are always supported */ 134138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP; 135138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP; 136138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM; 137138568Ssam if (ic->ic_caps & IEEE80211_C_AES) 138138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB; 139138568Ssam if (ic->ic_caps & IEEE80211_C_CKIP) 140138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP; 141138568Ssam /* 142138568Ssam * Default unicast cipher to WEP for 802.1x use. If 143138568Ssam * WPA is enabled the management code will set these 144138568Ssam * values to reflect. 145138568Ssam */ 146138568Ssam rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP; 147138568Ssam rsn->rsn_ucastkeylen = 104 / NBBY; 148138568Ssam /* 149138568Ssam * WPA says the multicast cipher is the lowest unicast 150138568Ssam * cipher supported. But we skip WEP which would 151138568Ssam * otherwise be used based on this criteria. 152138568Ssam */ 153138568Ssam rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP; 154138568Ssam rsn->rsn_mcastkeylen = 128 / NBBY; 155138568Ssam 156138568Ssam /* 157138568Ssam * We support both WPA-PSK and 802.1x; the one used 158138568Ssam * is determined by the authentication mode and the 159138568Ssam * setting of the PSK state. 160138568Ssam */ 161138568Ssam rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK; 162138568Ssam rsn->rsn_keymgmt = WPA_ASE_8021X_PSK; 163138568Ssam 164138568Ssam ic->ic_bss = ieee80211_ref_node(ni); /* hold reference */ 165138568Ssam ic->ic_auth = ieee80211_authenticator_get(ni->ni_authmode); 166116742Ssam} 167116742Ssam 168116742Ssamvoid 169138568Ssamieee80211_node_detach(struct ieee80211com *ic) 170116742Ssam{ 171116742Ssam 172138568Ssam if (ic->ic_bss != NULL) { 173138568Ssam ieee80211_free_node(ic->ic_bss); 174138568Ssam ic->ic_bss = NULL; 175138568Ssam } 176138568Ssam ieee80211_node_table_cleanup(&ic->ic_scan); 177140753Ssam ieee80211_node_table_cleanup(&ic->ic_sta); 178138568Ssam if (ic->ic_aid_bitmap != NULL) { 179138568Ssam FREE(ic->ic_aid_bitmap, M_DEVBUF); 180138568Ssam ic->ic_aid_bitmap = NULL; 181138568Ssam } 182138568Ssam if (ic->ic_tim_bitmap != NULL) { 183138568Ssam FREE(ic->ic_tim_bitmap, M_DEVBUF); 184138568Ssam ic->ic_tim_bitmap = NULL; 185138568Ssam } 186116742Ssam} 187116742Ssam 188138568Ssam/* 189138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator. 190138568Ssam */ 191138568Ssam 192138568Ssamvoid 193138568Ssamieee80211_node_authorize(struct ieee80211com *ic, struct ieee80211_node *ni) 194138568Ssam{ 195138568Ssam ni->ni_flags |= IEEE80211_NODE_AUTH; 196139528Ssam ni->ni_inact_reload = ic->ic_inact_run; 197138568Ssam} 198138568Ssam 199138568Ssamvoid 200138568Ssamieee80211_node_unauthorize(struct ieee80211com *ic, struct ieee80211_node *ni) 201138568Ssam{ 202138568Ssam ni->ni_flags &= ~IEEE80211_NODE_AUTH; 203138568Ssam} 204138568Ssam 205116742Ssam/* 206138568Ssam * Set/change the channel. The rate set is also updated as 207138568Ssam * to insure a consistent view by drivers. 208138568Ssam */ 209138568Ssamstatic __inline void 210138568Ssamieee80211_set_chan(struct ieee80211com *ic, 211138568Ssam struct ieee80211_node *ni, struct ieee80211_channel *chan) 212138568Ssam{ 213138568Ssam ni->ni_chan = chan; 214138568Ssam ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)]; 215138568Ssam} 216138568Ssam 217138568Ssam/* 218116742Ssam * AP scanning support. 219116742Ssam */ 220116742Ssam 221138568Ssam#ifdef IEEE80211_DEBUG 222138568Ssamstatic void 223138568Ssamdump_chanlist(const u_char chans[]) 224138568Ssam{ 225138568Ssam const char *sep; 226138568Ssam int i; 227138568Ssam 228138568Ssam sep = " "; 229138568Ssam for (i = 0; i < IEEE80211_CHAN_MAX; i++) 230138568Ssam if (isset(chans, i)) { 231138568Ssam printf("%s%u", sep, i); 232138568Ssam sep = ", "; 233138568Ssam } 234138568Ssam} 235138568Ssam#endif /* IEEE80211_DEBUG */ 236138568Ssam 237116742Ssam/* 238138568Ssam * Initialize the channel set to scan based on the 239116742Ssam * of available channels and the current PHY mode. 240116742Ssam */ 241117811Ssamstatic void 242138568Ssamieee80211_reset_scan(struct ieee80211com *ic) 243116742Ssam{ 244116742Ssam 245138568Ssam /* XXX ic_des_chan should be handled with ic_chan_active */ 246138568Ssam if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) { 247138568Ssam memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan)); 248138568Ssam setbit(ic->ic_chan_scan, 249138568Ssam ieee80211_chan2ieee(ic, ic->ic_des_chan)); 250138568Ssam } else 251138568Ssam memcpy(ic->ic_chan_scan, ic->ic_chan_active, 252138568Ssam sizeof(ic->ic_chan_active)); 253117811Ssam /* NB: hack, setup so next_scan starts with the first channel */ 254117811Ssam if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC) 255138568Ssam ieee80211_set_chan(ic, ic->ic_bss, 256138568Ssam &ic->ic_channels[IEEE80211_CHAN_MAX]); 257138568Ssam#ifdef IEEE80211_DEBUG 258138568Ssam if (ieee80211_msg_scan(ic)) { 259138568Ssam printf("%s: scan set:", __func__); 260138568Ssam dump_chanlist(ic->ic_chan_scan); 261138568Ssam printf(" start chan %u\n", 262138568Ssam ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)); 263138568Ssam } 264138568Ssam#endif /* IEEE80211_DEBUG */ 265116742Ssam} 266116742Ssam 267116742Ssam/* 268116742Ssam * Begin an active scan. 269116742Ssam */ 270116742Ssamvoid 271138568Ssamieee80211_begin_scan(struct ieee80211com *ic, int reset) 272116742Ssam{ 273116742Ssam 274138568Ssam ic->ic_scan.nt_scangen++; 275117811Ssam /* 276117811Ssam * In all but hostap mode scanning starts off in 277117811Ssam * an active mode before switching to passive. 278117811Ssam */ 279121180Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP) { 280117811Ssam ic->ic_flags |= IEEE80211_F_ASCAN; 281121180Ssam ic->ic_stats.is_scan_active++; 282121180Ssam } else 283121180Ssam ic->ic_stats.is_scan_passive++; 284139520Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 285139520Ssam "begin %s scan in %s mode, scangen %u\n", 286138568Ssam (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive", 287139520Ssam ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen); 288116742Ssam /* 289138568Ssam * Clear scan state and flush any previously seen AP's. 290116742Ssam */ 291138568Ssam ieee80211_reset_scan(ic); 292138568Ssam if (reset) 293138568Ssam ieee80211_free_allnodes(&ic->ic_scan); 294116742Ssam 295138568Ssam ic->ic_flags |= IEEE80211_F_SCAN; 296138568Ssam 297117811Ssam /* Scan the next channel. */ 298138568Ssam ieee80211_next_scan(ic); 299116742Ssam} 300116742Ssam 301116742Ssam/* 302116742Ssam * Switch to the next channel marked for scanning. 303116742Ssam */ 304138568Ssamint 305138568Ssamieee80211_next_scan(struct ieee80211com *ic) 306116742Ssam{ 307116742Ssam struct ieee80211_channel *chan; 308116742Ssam 309138568Ssam /* 310138568Ssam * Insure any previous mgt frame timeouts don't fire. 311138568Ssam * This assumes the driver does the right thing in 312138568Ssam * flushing anything queued in the driver and below. 313138568Ssam */ 314138568Ssam ic->ic_mgt_timer = 0; 315138568Ssam 316116742Ssam chan = ic->ic_bss->ni_chan; 317138568Ssam do { 318116742Ssam if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX]) 319116742Ssam chan = &ic->ic_channels[0]; 320116742Ssam if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) { 321138568Ssam clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan)); 322138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 323138568Ssam "%s: chan %d->%d\n", __func__, 324138568Ssam ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan), 325138568Ssam ieee80211_chan2ieee(ic, chan)); 326138568Ssam ieee80211_set_chan(ic, ic->ic_bss, chan); 327138568Ssam#ifdef notyet 328138568Ssam /* XXX driver state change */ 329116742Ssam /* 330138568Ssam * Scan next channel. If doing an active scan 331138568Ssam * and the channel is not marked passive-only 332138568Ssam * then send a probe request. Otherwise just 333138568Ssam * listen for beacons on the channel. 334116742Ssam */ 335138568Ssam if ((ic->ic_flags & IEEE80211_F_ASCAN) && 336138568Ssam (ni->ni_chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) { 337138568Ssam IEEE80211_SEND_MGMT(ic, ni, 338138568Ssam IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0); 339138568Ssam } 340138568Ssam#else 341138568Ssam ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 342138568Ssam#endif 343138568Ssam return 1; 344116742Ssam } 345138568Ssam } while (chan != ic->ic_bss->ni_chan); 346138568Ssam ieee80211_end_scan(ic); 347138568Ssam return 0; 348116742Ssam} 349116742Ssam 350116742Ssamvoid 351116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 352116742Ssam{ 353140753Ssam struct ieee80211_node_table *nt; 354116742Ssam struct ieee80211_node *ni; 355116742Ssam 356138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 357138568Ssam "%s: creating ibss\n", __func__); 358138568Ssam 359138568Ssam /* 360138568Ssam * Create the station/neighbor table. Note that for adhoc 361138568Ssam * mode we make the initial inactivity timer longer since 362138568Ssam * we create nodes only through discovery and they typically 363138568Ssam * are long-lived associations. 364138568Ssam */ 365140753Ssam nt = &ic->ic_sta; 366140753Ssam IEEE80211_NODE_LOCK(nt); 367140753Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 368140753Ssam nt->nt_name = "station"; 369140753Ssam nt->nt_inact_init = ic->ic_inact_init; 370140753Ssam } else { 371140753Ssam nt->nt_name = "neighbor"; 372140753Ssam nt->nt_inact_init = ic->ic_inact_run; 373140753Ssam } 374140753Ssam IEEE80211_NODE_UNLOCK(nt); 375140753Ssam 376140753Ssam ni = ieee80211_alloc_node(nt, ic->ic_myaddr); 377140753Ssam if (ni == NULL) { 378140753Ssam /* XXX recovery? */ 379138568Ssam return; 380138568Ssam } 381116742Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 382116742Ssam ni->ni_esslen = ic->ic_des_esslen; 383116742Ssam memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 384116742Ssam ni->ni_intval = ic->ic_lintval; 385138568Ssam if (ic->ic_flags & IEEE80211_F_PRIVACY) 386116742Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 387116742Ssam if (ic->ic_phytype == IEEE80211_T_FH) { 388116742Ssam ni->ni_fhdwell = 200; /* XXX */ 389116742Ssam ni->ni_fhindex = 1; 390116742Ssam } 391138568Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 392138568Ssam ic->ic_flags |= IEEE80211_F_SIBSS; 393138568Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */ 394138568Ssam ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */ 395138568Ssam } 396138568Ssam /* 397138568Ssam * Fix the channel and related attributes. 398138568Ssam */ 399138568Ssam ieee80211_set_chan(ic, ni, chan); 400138568Ssam ic->ic_curmode = ieee80211_chan2mode(ic, chan); 401138568Ssam /* 402138568Ssam * Do mode-specific rate setup. 403138568Ssam */ 404138568Ssam if (ic->ic_curmode == IEEE80211_MODE_11G) { 405138568Ssam /* 406138568Ssam * Use a mixed 11b/11g rate set. 407138568Ssam */ 408138568Ssam ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G); 409138568Ssam } else if (ic->ic_curmode == IEEE80211_MODE_11B) { 410138568Ssam /* 411138568Ssam * Force pure 11b rate set. 412138568Ssam */ 413138568Ssam ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B); 414138568Ssam } 415138568Ssam 416140753Ssam (void) ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 417116742Ssam} 418116742Ssam 419138568Ssamvoid 420138568Ssamieee80211_reset_bss(struct ieee80211com *ic) 421138568Ssam{ 422138568Ssam struct ieee80211_node *ni, *obss; 423138568Ssam 424138568Ssam ieee80211_node_table_reset(&ic->ic_scan); 425140753Ssam ieee80211_node_table_reset(&ic->ic_sta); 426140753Ssam 427138568Ssam ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr); 428138568Ssam KASSERT(ni != NULL, ("unable to setup inital BSS node")); 429138568Ssam obss = ic->ic_bss; 430138568Ssam ic->ic_bss = ieee80211_ref_node(ni); 431138568Ssam if (obss != NULL) 432138568Ssam ieee80211_free_node(obss); 433138568Ssam} 434138568Ssam 435127767Ssamstatic int 436138568Ssamieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni) 437127767Ssam{ 438127767Ssam u_int8_t rate; 439127767Ssam int fail; 440127767Ssam 441127767Ssam fail = 0; 442127767Ssam if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 443127767Ssam fail |= 0x01; 444127767Ssam if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 445127767Ssam ni->ni_chan != ic->ic_des_chan) 446127767Ssam fail |= 0x01; 447127767Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 448127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 449127767Ssam fail |= 0x02; 450127767Ssam } else { 451127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 452127767Ssam fail |= 0x02; 453127767Ssam } 454138568Ssam if (ic->ic_flags & IEEE80211_F_PRIVACY) { 455127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 456127767Ssam fail |= 0x04; 457127767Ssam } else { 458127767Ssam /* XXX does this mean privacy is supported or required? */ 459127767Ssam if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 460127767Ssam fail |= 0x04; 461127767Ssam } 462140440Ssam rate = ieee80211_fix_rate(ic, ni, 463140440Ssam IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 464127767Ssam if (rate & IEEE80211_RATE_BASIC) 465127767Ssam fail |= 0x08; 466127767Ssam if (ic->ic_des_esslen != 0 && 467127767Ssam (ni->ni_esslen != ic->ic_des_esslen || 468127767Ssam memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) 469127767Ssam fail |= 0x10; 470127767Ssam if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 471127767Ssam !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 472127767Ssam fail |= 0x20; 473127767Ssam#ifdef IEEE80211_DEBUG 474138568Ssam if (ieee80211_msg_scan(ic)) { 475127767Ssam printf(" %c %s", fail ? '-' : '+', 476127767Ssam ether_sprintf(ni->ni_macaddr)); 477127767Ssam printf(" %s%c", ether_sprintf(ni->ni_bssid), 478127767Ssam fail & 0x20 ? '!' : ' '); 479127767Ssam printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan), 480127767Ssam fail & 0x01 ? '!' : ' '); 481127767Ssam printf(" %+4d", ni->ni_rssi); 482127767Ssam printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 483127767Ssam fail & 0x08 ? '!' : ' '); 484127767Ssam printf(" %4s%c", 485127767Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 486127767Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 487127767Ssam "????", 488127767Ssam fail & 0x02 ? '!' : ' '); 489127767Ssam printf(" %3s%c ", 490127767Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 491127767Ssam "wep" : "no", 492127767Ssam fail & 0x04 ? '!' : ' '); 493127767Ssam ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 494127767Ssam printf("%s\n", fail & 0x10 ? "!" : ""); 495127767Ssam } 496127767Ssam#endif 497127767Ssam return fail; 498127767Ssam} 499127767Ssam 500138568Ssamstatic __inline u_int8_t 501138568Ssammaxrate(const struct ieee80211_node *ni) 502138568Ssam{ 503138568Ssam const struct ieee80211_rateset *rs = &ni->ni_rates; 504138568Ssam /* NB: assumes rate set is sorted (happens on frame receive) */ 505138568Ssam return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL; 506138568Ssam} 507138568Ssam 508116742Ssam/* 509138568Ssam * Compare the capabilities of two nodes and decide which is 510138568Ssam * more desirable (return >0 if a is considered better). Note 511138568Ssam * that we assume compatibility/usability has already been checked 512138568Ssam * so we don't need to (e.g. validate whether privacy is supported). 513138568Ssam * Used to select the best scan candidate for association in a BSS. 514138568Ssam */ 515138568Ssamstatic int 516138568Ssamieee80211_node_compare(struct ieee80211com *ic, 517138568Ssam const struct ieee80211_node *a, 518138568Ssam const struct ieee80211_node *b) 519138568Ssam{ 520138568Ssam u_int8_t maxa, maxb; 521138568Ssam u_int8_t rssia, rssib; 522138568Ssam 523138568Ssam /* privacy support preferred */ 524138568Ssam if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) && 525138568Ssam (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 526138568Ssam return 1; 527138568Ssam if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 && 528138568Ssam (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)) 529138568Ssam return -1; 530138568Ssam 531138568Ssam rssia = ic->ic_node_getrssi(a); 532138568Ssam rssib = ic->ic_node_getrssi(b); 533139543Ssam if (abs(rssib - rssia) < 5) { 534139543Ssam /* best/max rate preferred if signal level close enough XXX */ 535139543Ssam maxa = maxrate(a); 536139543Ssam maxb = maxrate(b); 537139543Ssam if (maxa != maxb) 538139543Ssam return maxa - maxb; 539139543Ssam /* XXX use freq for channel preference */ 540139543Ssam /* for now just prefer 5Ghz band to all other bands */ 541139543Ssam if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) && 542139543Ssam !IEEE80211_IS_CHAN_5GHZ(b->ni_chan)) 543139543Ssam return 1; 544139543Ssam if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) && 545139543Ssam IEEE80211_IS_CHAN_5GHZ(b->ni_chan)) 546139543Ssam return -1; 547139543Ssam } 548138568Ssam /* all things being equal, use signal level */ 549138568Ssam return rssia - rssib; 550138568Ssam} 551138568Ssam 552138568Ssam/* 553140441Ssam * Mark an ongoing scan stopped. 554140441Ssam */ 555140441Ssamvoid 556140441Ssamieee80211_cancel_scan(struct ieee80211com *ic) 557140441Ssam{ 558140441Ssam 559140441Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n", 560140441Ssam __func__, 561140441Ssam (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive"); 562140441Ssam 563140441Ssam ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN); 564140441Ssam} 565140441Ssam 566140441Ssam/* 567116742Ssam * Complete a scan of potential channels. 568116742Ssam */ 569116742Ssamvoid 570138568Ssamieee80211_end_scan(struct ieee80211com *ic) 571116742Ssam{ 572140448Ssam struct ieee80211_node_table *nt = &ic->ic_scan; 573140448Ssam struct ieee80211_node *ni, *selbs; 574116742Ssam 575140441Ssam ieee80211_cancel_scan(ic); 576140441Ssam ieee80211_notify_scan_done(ic); 577116742Ssam 578116742Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 579138568Ssam u_int8_t maxrssi[IEEE80211_CHAN_MAX]; /* XXX off stack? */ 580138568Ssam int i, bestchan; 581138568Ssam u_int8_t rssi; 582138568Ssam 583116742Ssam /* 584116742Ssam * The passive scan to look for existing AP's completed, 585116742Ssam * select a channel to camp on. Identify the channels 586116742Ssam * that already have one or more AP's and try to locate 587140753Ssam * an unoccupied one. If that fails, pick a channel that 588138568Ssam * looks to be quietest. 589116742Ssam */ 590138568Ssam memset(maxrssi, 0, sizeof(maxrssi)); 591140448Ssam IEEE80211_NODE_LOCK(nt); 592140448Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 593138568Ssam rssi = ic->ic_node_getrssi(ni); 594138568Ssam i = ieee80211_chan2ieee(ic, ni->ni_chan); 595138568Ssam if (rssi > maxrssi[i]) 596138568Ssam maxrssi[i] = rssi; 597116742Ssam } 598140448Ssam IEEE80211_NODE_UNLOCK(nt); 599138568Ssam /* XXX select channel more intelligently */ 600138568Ssam bestchan = -1; 601116742Ssam for (i = 0; i < IEEE80211_CHAN_MAX; i++) 602138568Ssam if (isset(ic->ic_chan_active, i)) { 603138568Ssam /* 604138568Ssam * If the channel is unoccupied the max rssi 605138568Ssam * should be zero; just take it. Otherwise 606138568Ssam * track the channel with the lowest rssi and 607138568Ssam * use that when all channels appear occupied. 608138568Ssam */ 609138568Ssam if (maxrssi[i] == 0) { 610138568Ssam bestchan = i; 611116742Ssam break; 612138568Ssam } 613138568Ssam if (maxrssi[i] < maxrssi[bestchan]) 614138568Ssam bestchan = i; 615138568Ssam } 616138568Ssam if (bestchan != -1) { 617138568Ssam ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]); 618138568Ssam return; 619116742Ssam } 620138568Ssam /* no suitable channel, should not happen */ 621138568Ssam } 622138568Ssam 623138568Ssam /* 624138568Ssam * When manually sequencing the state machine; scan just once 625138568Ssam * regardless of whether we have a candidate or not. The 626138568Ssam * controlling application is expected to setup state and 627138568Ssam * initiate an association. 628138568Ssam */ 629138568Ssam if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL) 630116742Ssam return; 631138568Ssam /* 632138568Ssam * Automatic sequencing; look for a candidate and 633138568Ssam * if found join the network. 634138568Ssam */ 635140448Ssam /* NB: unlocked read should be ok */ 636140448Ssam if (TAILQ_FIRST(&nt->nt_node) == NULL) { 637138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 638138568Ssam "%s: no scan candidate\n", __func__); 639116742Ssam notfound: 640116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS && 641116742Ssam (ic->ic_flags & IEEE80211_F_IBSSON) && 642116742Ssam ic->ic_des_esslen != 0) { 643116742Ssam ieee80211_create_ibss(ic, ic->ic_ibss_chan); 644116742Ssam return; 645116742Ssam } 646116742Ssam /* 647116742Ssam * Reset the list of channels to scan and start again. 648116742Ssam */ 649138568Ssam ieee80211_reset_scan(ic); 650138568Ssam ic->ic_flags |= IEEE80211_F_SCAN; 651138568Ssam ieee80211_next_scan(ic); 652116742Ssam return; 653116742Ssam } 654116742Ssam selbs = NULL; 655138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n", 656138568Ssam "macaddr bssid chan rssi rate flag wep essid"); 657140448Ssam IEEE80211_NODE_LOCK(nt); 658140448Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 659116742Ssam if (ni->ni_fails) { 660116742Ssam /* 661116742Ssam * The configuration of the access points may change 662116742Ssam * during my scan. So delete the entry for the AP 663116742Ssam * and retry to associate if there is another beacon. 664116742Ssam */ 665138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 666138568Ssam "%s: skip scan candidate %s, fails %u\n", 667138568Ssam __func__, ether_sprintf(ni->ni_macaddr), 668138568Ssam ni->ni_fails); 669140448Ssam ni->ni_fails++; 670140448Ssam#if 0 671116742Ssam if (ni->ni_fails++ > 2) 672138568Ssam ieee80211_free_node(ni); 673140448Ssam#endif 674116742Ssam continue; 675116742Ssam } 676138568Ssam if (ieee80211_match_bss(ic, ni) == 0) { 677116742Ssam if (selbs == NULL) 678116742Ssam selbs = ni; 679140448Ssam else if (ieee80211_node_compare(ic, ni, selbs) > 0) 680116742Ssam selbs = ni; 681116742Ssam } 682116742Ssam } 683140448Ssam if (selbs != NULL) /* NB: grab ref while dropping lock */ 684140448Ssam (void) ieee80211_ref_node(selbs); 685140448Ssam IEEE80211_NODE_UNLOCK(nt); 686116742Ssam if (selbs == NULL) 687116742Ssam goto notfound; 688138568Ssam if (!ieee80211_sta_join(ic, selbs)) { 689140448Ssam ieee80211_free_node(selbs); 690138568Ssam goto notfound; 691138568Ssam } 692138568Ssam} 693138568Ssam 694138568Ssam/* 695138568Ssam * Handle 802.11 ad hoc network merge. The 696138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance 697138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match 698138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that 699138568Ssam * has the station's desired SSID. The "oldest" 802.11 network 700138568Ssam * sends beacons with the greatest TSF timestamp. 701138568Ssam * 702138568Ssam * The caller is assumed to validate TSF's before attempting a merge. 703138568Ssam * 704138568Ssam * Return !0 if the BSSID changed, 0 otherwise. 705138568Ssam */ 706138568Ssamint 707138568Ssamieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni) 708138568Ssam{ 709138568Ssam 710140453Ssam if (ni == ic->ic_bss || 711140453Ssam IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) { 712138568Ssam /* unchanged, nothing to do */ 713138568Ssam return 0; 714138568Ssam } 715138568Ssam if (ieee80211_match_bss(ic, ni) != 0) { /* capabilities mismatch */ 716138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 717138568Ssam "%s: merge failed, capabilities mismatch\n", __func__); 718138568Ssam ic->ic_stats.is_ibss_capmismatch++; 719138568Ssam return 0; 720138568Ssam } 721138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 722138568Ssam "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__, 723138568Ssam ether_sprintf(ni->ni_bssid), 724138568Ssam ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 725138568Ssam ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long", 726138568Ssam ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "" 727138568Ssam ); 728140753Ssam return ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 729138568Ssam} 730138568Ssam 731138568Ssam/* 732138568Ssam * Join the specified IBSS/BSS network. The node is assumed to 733138568Ssam * be passed in with a held reference. 734138568Ssam */ 735138568Ssamint 736138568Ssamieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs) 737138568Ssam{ 738138568Ssam struct ieee80211_node *obss; 739138568Ssam 740116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 741140753Ssam struct ieee80211_node_table *nt; 742138568Ssam /* 743140440Ssam * Delete unusable rates; we've already checked 744140440Ssam * that the negotiated rate set is acceptable. 745138568Ssam */ 746140440Ssam ieee80211_fix_rate(ic, selbs, IEEE80211_F_DODEL); 747127772Ssam /* 748140753Ssam * Fillin the neighbor table; it will already 749139521Ssam * exist if we are simply switching mastership. 750140753Ssam * XXX ic_sta always setup so this is unnecessary? 751127772Ssam */ 752140753Ssam nt = &ic->ic_sta; 753140753Ssam IEEE80211_NODE_LOCK(nt); 754140753Ssam nt->nt_name = "neighbor"; 755140753Ssam nt->nt_inact_init = ic->ic_inact_run; 756140753Ssam IEEE80211_NODE_UNLOCK(nt); 757138568Ssam } 758138568Ssam 759138568Ssam /* 760138568Ssam * Committed to selbs, setup state. 761138568Ssam */ 762138568Ssam obss = ic->ic_bss; 763140753Ssam ic->ic_bss = selbs; /* NB: caller assumed to bump refcnt */ 764138568Ssam if (obss != NULL) 765138568Ssam ieee80211_free_node(obss); 766138568Ssam /* 767138568Ssam * Set the erp state (mostly the slot time) to deal with 768138568Ssam * the auto-select case; this should be redundant if the 769138568Ssam * mode is locked. 770138568Ssam */ 771138568Ssam ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan); 772138568Ssam ieee80211_reset_erp(ic); 773138568Ssam ieee80211_wme_initparams(ic); 774140753Ssam 775140753Ssam if (ic->ic_opmode == IEEE80211_M_STA) 776140753Ssam ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 777140753Ssam else 778117811Ssam ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 779138568Ssam return 1; 780116742Ssam} 781116742Ssam 782138568Ssam/* 783138568Ssam * Leave the specified IBSS/BSS network. The node is assumed to 784138568Ssam * be passed in with a held reference. 785138568Ssam */ 786138568Ssamvoid 787138568Ssamieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 788138568Ssam{ 789138568Ssam ic->ic_node_cleanup(ni); 790138568Ssam ieee80211_notify_node_leave(ic, ni); 791138568Ssam} 792138568Ssam 793116742Ssamstatic struct ieee80211_node * 794138568Ssamnode_alloc(struct ieee80211_node_table *nt) 795116742Ssam{ 796127768Ssam struct ieee80211_node *ni; 797138568Ssam 798127768Ssam MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node), 799127768Ssam M_80211_NODE, M_NOWAIT | M_ZERO); 800127768Ssam return ni; 801116742Ssam} 802116742Ssam 803138568Ssam/* 804138568Ssam * Reclaim any resources in a node and reset any critical 805138568Ssam * state. Typically nodes are free'd immediately after, 806138568Ssam * but in some cases the storage may be reused so we need 807138568Ssam * to insure consistent state (should probably fix that). 808138568Ssam */ 809116742Ssamstatic void 810138568Ssamnode_cleanup(struct ieee80211_node *ni) 811116742Ssam{ 812138568Ssam#define N(a) (sizeof(a)/sizeof(a[0])) 813138568Ssam struct ieee80211com *ic = ni->ni_ic; 814138568Ssam int i, qlen; 815138568Ssam 816138568Ssam /* NB: preserve ni_table */ 817138568Ssam if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 818138568Ssam ic->ic_ps_sta--; 819138568Ssam ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 820138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 821138568Ssam "[%s] power save mode off, %u sta's in ps mode\n", 822138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta); 823138568Ssam } 824138568Ssam 825138568Ssam /* 826138568Ssam * Drain power save queue and, if needed, clear TIM. 827138568Ssam */ 828138568Ssam IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen); 829138568Ssam if (qlen != 0 && ic->ic_set_tim != NULL) 830138568Ssam ic->ic_set_tim(ic, ni, 0); 831138568Ssam 832138568Ssam ni->ni_associd = 0; 833138568Ssam if (ni->ni_challenge != NULL) { 834138568Ssam FREE(ni->ni_challenge, M_DEVBUF); 835138568Ssam ni->ni_challenge = NULL; 836138568Ssam } 837138568Ssam /* 838138568Ssam * Preserve SSID, WPA, and WME ie's so the bss node is 839138568Ssam * reusable during a re-auth/re-assoc state transition. 840138568Ssam * If we remove these data they will not be recreated 841138568Ssam * because they come from a probe-response or beacon frame 842138568Ssam * which cannot be expected prior to the association-response. 843138568Ssam * This should not be an issue when operating in other modes 844138568Ssam * as stations leaving always go through a full state transition 845138568Ssam * which will rebuild this state. 846138568Ssam * 847138568Ssam * XXX does this leave us open to inheriting old state? 848138568Ssam */ 849138568Ssam for (i = 0; i < N(ni->ni_rxfrag); i++) 850138568Ssam if (ni->ni_rxfrag[i] != NULL) { 851138568Ssam m_freem(ni->ni_rxfrag[i]); 852138568Ssam ni->ni_rxfrag[i] = NULL; 853138568Ssam } 854138568Ssam ieee80211_crypto_delkey(ic, &ni->ni_ucastkey); 855138568Ssam#undef N 856116742Ssam} 857116742Ssam 858116742Ssamstatic void 859138568Ssamnode_free(struct ieee80211_node *ni) 860116742Ssam{ 861138568Ssam struct ieee80211com *ic = ni->ni_ic; 862138568Ssam 863138568Ssam ic->ic_node_cleanup(ni); 864138568Ssam if (ni->ni_wpa_ie != NULL) 865138568Ssam FREE(ni->ni_wpa_ie, M_DEVBUF); 866138568Ssam if (ni->ni_wme_ie != NULL) 867138568Ssam FREE(ni->ni_wme_ie, M_DEVBUF); 868138568Ssam IEEE80211_NODE_SAVEQ_DESTROY(ni); 869138568Ssam FREE(ni, M_80211_NODE); 870116742Ssam} 871116742Ssam 872120104Ssamstatic u_int8_t 873138568Ssamnode_getrssi(const struct ieee80211_node *ni) 874120104Ssam{ 875120104Ssam return ni->ni_rssi; 876120104Ssam} 877120104Ssam 878116742Ssamstatic void 879138568Ssamieee80211_setup_node(struct ieee80211_node_table *nt, 880138568Ssam struct ieee80211_node *ni, const u_int8_t *macaddr) 881116742Ssam{ 882138568Ssam struct ieee80211com *ic = nt->nt_ic; 883116742Ssam int hash; 884116742Ssam 885138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 886138568Ssam "%s %s in %s table\n", __func__, 887138568Ssam ether_sprintf(macaddr), nt->nt_name); 888138568Ssam 889116742Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 890116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 891138568Ssam ieee80211_node_initref(ni); /* mark referenced */ 892138568Ssam ni->ni_chan = IEEE80211_CHAN_ANYC; 893138568Ssam ni->ni_authmode = IEEE80211_AUTH_OPEN; 894138568Ssam ni->ni_txpower = ic->ic_txpowlimit; /* max power */ 895138568Ssam ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE); 896139528Ssam ni->ni_inact_reload = nt->nt_inact_init; 897139528Ssam ni->ni_inact = ni->ni_inact_reload; 898138568Ssam IEEE80211_NODE_SAVEQ_INIT(ni, "unknown"); 899138568Ssam 900138568Ssam IEEE80211_NODE_LOCK(nt); 901138568Ssam TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list); 902138568Ssam LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash); 903138568Ssam ni->ni_table = nt; 904138568Ssam ni->ni_ic = ic; 905138568Ssam IEEE80211_NODE_UNLOCK(nt); 906116742Ssam} 907116742Ssam 908116742Ssamstruct ieee80211_node * 909138568Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 910116742Ssam{ 911138568Ssam struct ieee80211com *ic = nt->nt_ic; 912138568Ssam struct ieee80211_node *ni; 913138568Ssam 914138568Ssam ni = ic->ic_node_alloc(nt); 915116742Ssam if (ni != NULL) 916138568Ssam ieee80211_setup_node(nt, ni, macaddr); 917127769Ssam else 918127769Ssam ic->ic_stats.is_rx_nodealloc++; 919116742Ssam return ni; 920116742Ssam} 921116742Ssam 922116742Ssamstruct ieee80211_node * 923138568Ssamieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 924116742Ssam{ 925138568Ssam struct ieee80211com *ic = nt->nt_ic; 926138568Ssam struct ieee80211_node *ni; 927138568Ssam 928138568Ssam ni = ic->ic_node_alloc(nt); 929116742Ssam if (ni != NULL) { 930138568Ssam ieee80211_setup_node(nt, ni, macaddr); 931127770Ssam /* 932127770Ssam * Inherit from ic_bss. 933127770Ssam */ 934138568Ssam ni->ni_authmode = ic->ic_bss->ni_authmode; 935138568Ssam ni->ni_txpower = ic->ic_bss->ni_txpower; 936138568Ssam ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */ 937127770Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 938138568Ssam ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan); 939138568Ssam ni->ni_rsn = ic->ic_bss->ni_rsn; 940127770Ssam } else 941127770Ssam ic->ic_stats.is_rx_nodealloc++; 942116742Ssam return ni; 943116742Ssam} 944116742Ssam 945127772Ssamstatic struct ieee80211_node * 946138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 947138568Ssam_ieee80211_find_node_debug(struct ieee80211_node_table *nt, 948138568Ssam const u_int8_t *macaddr, const char *func, int line) 949138568Ssam#else 950138568Ssam_ieee80211_find_node(struct ieee80211_node_table *nt, 951138568Ssam const u_int8_t *macaddr) 952138568Ssam#endif 953116742Ssam{ 954116742Ssam struct ieee80211_node *ni; 955116742Ssam int hash; 956116742Ssam 957138568Ssam IEEE80211_NODE_LOCK_ASSERT(nt); 958127772Ssam 959116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 960138568Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 961116742Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 962138568Ssam ieee80211_ref_node(ni); /* mark referenced */ 963138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 964138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 965138568Ssam "%s (%s:%u) %s refcnt %d\n", __func__, func, line, 966138568Ssam ether_sprintf(ni->ni_macaddr), 967138568Ssam ieee80211_node_refcnt(ni)); 968138568Ssam#endif 969127772Ssam return ni; 970116742Ssam } 971116742Ssam } 972127772Ssam return NULL; 973127772Ssam} 974138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 975138568Ssam#define _ieee80211_find_node(nt, mac) \ 976138568Ssam _ieee80211_find_node_debug(nt, mac, func, line) 977138568Ssam#endif 978127772Ssam 979127772Ssamstruct ieee80211_node * 980138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 981138568Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt, 982138568Ssam const u_int8_t *macaddr, const char *func, int line) 983138568Ssam#else 984138568Ssamieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 985138568Ssam#endif 986127772Ssam{ 987127772Ssam struct ieee80211_node *ni; 988127772Ssam 989138568Ssam IEEE80211_NODE_LOCK(nt); 990138568Ssam ni = _ieee80211_find_node(nt, macaddr); 991138568Ssam IEEE80211_NODE_UNLOCK(nt); 992116742Ssam return ni; 993116742Ssam} 994116742Ssam 995116742Ssam/* 996138568Ssam * Fake up a node; this handles node discovery in adhoc mode. 997138568Ssam * Note that for the driver's benefit we we treat this like 998138568Ssam * an association so the driver has an opportunity to setup 999138568Ssam * it's private state. 1000138568Ssam */ 1001138568Ssamstruct ieee80211_node * 1002138568Ssamieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt, 1003138568Ssam const u_int8_t macaddr[IEEE80211_ADDR_LEN]) 1004138568Ssam{ 1005138568Ssam struct ieee80211com *ic = nt->nt_ic; 1006138568Ssam struct ieee80211_node *ni; 1007138568Ssam 1008138568Ssam ni = ieee80211_dup_bss(nt, macaddr); 1009138568Ssam if (ni != NULL) { 1010138568Ssam /* XXX no rate negotiation; just dup */ 1011138568Ssam ni->ni_rates = ic->ic_bss->ni_rates; 1012139524Ssam if (ic->ic_newassoc != NULL) 1013138568Ssam ic->ic_newassoc(ic, ni, 1); 1014138568Ssam /* XXX not right for 802.1x/WPA */ 1015138568Ssam ieee80211_node_authorize(ic, ni); 1016138568Ssam } 1017138568Ssam return ni; 1018138568Ssam} 1019138568Ssam 1020138568Ssam/* 1021138568Ssam * Locate the node for sender, track state, and then pass the 1022138568Ssam * (referenced) node up to the 802.11 layer for its use. We 1023138568Ssam * are required to pass some node so we fall back to ic_bss 1024138568Ssam * when this frame is from an unknown sender. The 802.11 layer 1025138568Ssam * knows this means the sender wasn't in the node table and 1026138568Ssam * acts accordingly. 1027138568Ssam */ 1028138568Ssamstruct ieee80211_node * 1029138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1030138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic, 1031138568Ssam const struct ieee80211_frame_min *wh, const char *func, int line) 1032138568Ssam#else 1033138568Ssamieee80211_find_rxnode(struct ieee80211com *ic, 1034138568Ssam const struct ieee80211_frame_min *wh) 1035138568Ssam#endif 1036138568Ssam{ 1037138568Ssam#define IS_CTL(wh) \ 1038138568Ssam ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) 1039138568Ssam#define IS_PSPOLL(wh) \ 1040138568Ssam ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL) 1041138568Ssam struct ieee80211_node_table *nt; 1042138568Ssam struct ieee80211_node *ni; 1043138568Ssam 1044138568Ssam /* XXX may want scanned nodes in the neighbor table for adhoc */ 1045138568Ssam if (ic->ic_opmode == IEEE80211_M_STA || 1046138568Ssam ic->ic_opmode == IEEE80211_M_MONITOR || 1047138568Ssam (ic->ic_flags & IEEE80211_F_SCAN)) 1048138568Ssam nt = &ic->ic_scan; 1049138568Ssam else 1050140753Ssam nt = &ic->ic_sta; 1051138568Ssam /* XXX check ic_bss first in station mode */ 1052138568Ssam /* XXX 4-address frames? */ 1053138568Ssam IEEE80211_NODE_LOCK(nt); 1054138568Ssam if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/) 1055138568Ssam ni = _ieee80211_find_node(nt, wh->i_addr1); 1056138568Ssam else 1057138568Ssam ni = _ieee80211_find_node(nt, wh->i_addr2); 1058138568Ssam IEEE80211_NODE_UNLOCK(nt); 1059138568Ssam 1060138568Ssam return (ni != NULL ? ni : ieee80211_ref_node(ic->ic_bss)); 1061138568Ssam#undef IS_PSPOLL 1062138568Ssam#undef IS_CTL 1063138568Ssam} 1064138568Ssam 1065138568Ssam/* 1066127772Ssam * Return a reference to the appropriate node for sending 1067127772Ssam * a data frame. This handles node discovery in adhoc networks. 1068127772Ssam */ 1069127772Ssamstruct ieee80211_node * 1070138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1071138568Ssamieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr, 1072138568Ssam const char *func, int line) 1073138568Ssam#else 1074138568Ssamieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr) 1075138568Ssam#endif 1076127772Ssam{ 1077140753Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 1078127772Ssam struct ieee80211_node *ni; 1079127772Ssam 1080127772Ssam /* 1081127772Ssam * The destination address should be in the node table 1082127772Ssam * unless we are operating in station mode or this is a 1083127772Ssam * multicast/broadcast frame. 1084127772Ssam */ 1085140753Ssam if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr)) 1086138568Ssam return ieee80211_ref_node(ic->ic_bss); 1087127772Ssam 1088127772Ssam /* XXX can't hold lock across dup_bss 'cuz of recursive locking */ 1089138568Ssam IEEE80211_NODE_LOCK(nt); 1090138568Ssam ni = _ieee80211_find_node(nt, macaddr); 1091138568Ssam IEEE80211_NODE_UNLOCK(nt); 1092138568Ssam 1093138568Ssam if (ni == NULL) { 1094138568Ssam if (ic->ic_opmode == IEEE80211_M_IBSS || 1095140497Ssam ic->ic_opmode == IEEE80211_M_AHDEMO) { 1096140497Ssam /* 1097140497Ssam * In adhoc mode cons up a node for the destination. 1098140497Ssam * Note that we need an additional reference for the 1099140497Ssam * caller to be consistent with _ieee80211_find_node. 1100140497Ssam */ 1101138568Ssam ni = ieee80211_fakeup_adhoc_node(nt, macaddr); 1102140497Ssam if (ni != NULL) 1103140497Ssam (void) ieee80211_ref_node(ni); 1104140497Ssam } else { 1105138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 1106138568Ssam "[%s] no node, discard frame (%s)\n", 1107138568Ssam ether_sprintf(macaddr), __func__); 1108138568Ssam ic->ic_stats.is_tx_nonode++; 1109127772Ssam } 1110127772Ssam } 1111127772Ssam return ni; 1112127772Ssam} 1113127772Ssam 1114127772Ssam/* 1115116742Ssam * Like find but search based on the channel too. 1116116742Ssam */ 1117116742Ssamstruct ieee80211_node * 1118138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1119138568Ssamieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt, 1120138568Ssam const u_int8_t *macaddr, struct ieee80211_channel *chan, 1121138568Ssam const char *func, int line) 1122138568Ssam#else 1123138568Ssamieee80211_find_node_with_channel(struct ieee80211_node_table *nt, 1124138568Ssam const u_int8_t *macaddr, struct ieee80211_channel *chan) 1125138568Ssam#endif 1126116742Ssam{ 1127116742Ssam struct ieee80211_node *ni; 1128116742Ssam int hash; 1129116742Ssam 1130116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 1131138568Ssam IEEE80211_NODE_LOCK(nt); 1132138568Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1133127772Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1134127772Ssam ni->ni_chan == chan) { 1135138568Ssam ieee80211_ref_node(ni); /* mark referenced */ 1136138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1137138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1138138568Ssam "%s (%s:%u) %s refcnt %d\n", __func__, func, line, 1139138568Ssam#else 1140138568Ssam "%s %s refcnt %d\n", __func__, 1141138568Ssam#endif 1142138568Ssam ether_sprintf(ni->ni_macaddr), 1143138568Ssam ieee80211_node_refcnt(ni)); 1144116742Ssam break; 1145116742Ssam } 1146116742Ssam } 1147138568Ssam IEEE80211_NODE_UNLOCK(nt); 1148116742Ssam return ni; 1149116742Ssam} 1150116742Ssam 1151138568Ssam/* 1152138568Ssam * Like find but search based on the ssid too. 1153138568Ssam */ 1154138568Ssamstruct ieee80211_node * 1155138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1156138568Ssamieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt, 1157138568Ssam const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid, 1158138568Ssam const char *func, int line) 1159138568Ssam#else 1160138568Ssamieee80211_find_node_with_ssid(struct ieee80211_node_table *nt, 1161138568Ssam const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid) 1162138568Ssam#endif 1163138568Ssam{ 1164138568Ssam struct ieee80211com *ic = nt->nt_ic; 1165138568Ssam struct ieee80211_node *ni; 1166138568Ssam int hash; 1167138568Ssam 1168138568Ssam hash = IEEE80211_NODE_HASH(macaddr); 1169138568Ssam IEEE80211_NODE_LOCK(nt); 1170138568Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1171138568Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1172138568Ssam ni->ni_esslen == ic->ic_des_esslen && 1173138568Ssam memcmp(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen) == 0) { 1174138568Ssam ieee80211_ref_node(ni); /* mark referenced */ 1175138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1176138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1177138568Ssam "%s (%s:%u) %s refcnt %d\n", __func__, func, line, 1178138568Ssam#else 1179138568Ssam "%s %s refcnt %d\n", __func__, 1180138568Ssam#endif 1181138568Ssam ether_sprintf(ni->ni_macaddr), 1182138568Ssam ieee80211_node_refcnt(ni)); 1183138568Ssam break; 1184138568Ssam } 1185138568Ssam } 1186138568Ssam IEEE80211_NODE_UNLOCK(nt); 1187138568Ssam return ni; 1188138568Ssam} 1189138568Ssam 1190138568Ssam 1191116742Ssamstatic void 1192138568Ssam_ieee80211_free_node(struct ieee80211_node *ni) 1193116742Ssam{ 1194138568Ssam struct ieee80211com *ic = ni->ni_ic; 1195138568Ssam struct ieee80211_node_table *nt = ni->ni_table; 1196119150Ssam 1197138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1198138568Ssam "%s %s in %s table\n", __func__, ether_sprintf(ni->ni_macaddr), 1199138568Ssam nt != NULL ? nt->nt_name : "<gone>"); 1200138568Ssam 1201138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1202138568Ssam if (nt != NULL) { 1203138568Ssam TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1204138568Ssam LIST_REMOVE(ni, ni_hash); 1205138568Ssam } 1206138568Ssam ic->ic_node_free(ni); 1207116742Ssam} 1208116742Ssam 1209116742Ssamvoid 1210138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1211138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line) 1212138568Ssam#else 1213138568Ssamieee80211_free_node(struct ieee80211_node *ni) 1214138568Ssam#endif 1215116742Ssam{ 1216138568Ssam struct ieee80211_node_table *nt = ni->ni_table; 1217119150Ssam 1218138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1219140454Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1220138568Ssam "%s (%s:%u) %s refcnt %d\n", __func__, func, line, 1221138568Ssam ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1222138568Ssam#endif 1223138568Ssam if (ieee80211_node_dectestref(ni)) { 1224138568Ssam /* 1225138568Ssam * Beware; if the node is marked gone then it's already 1226138568Ssam * been removed from the table and we cannot assume the 1227138568Ssam * table still exists. Regardless, there's no need to lock 1228138568Ssam * the table. 1229138568Ssam */ 1230138568Ssam if (ni->ni_table != NULL) { 1231138568Ssam IEEE80211_NODE_LOCK(nt); 1232138568Ssam _ieee80211_free_node(ni); 1233138568Ssam IEEE80211_NODE_UNLOCK(nt); 1234138568Ssam } else 1235138568Ssam _ieee80211_free_node(ni); 1236116742Ssam } 1237116742Ssam} 1238116742Ssam 1239138568Ssam/* 1240138568Ssam * Reclaim a node. If this is the last reference count then 1241138568Ssam * do the normal free work. Otherwise remove it from the node 1242138568Ssam * table and mark it gone by clearing the back-reference. 1243138568Ssam */ 1244138568Ssamstatic void 1245138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1246116742Ssam{ 1247138568Ssam 1248138568Ssam if (!ieee80211_node_dectestref(ni)) { 1249138568Ssam /* 1250138568Ssam * Other references are present, just remove the 1251138568Ssam * node from the table so it cannot be found. When 1252138568Ssam * the references are dropped storage will be 1253140753Ssam * reclaimed. 1254138568Ssam */ 1255138568Ssam TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1256138568Ssam LIST_REMOVE(ni, ni_hash); 1257138568Ssam ni->ni_table = NULL; /* clear reference */ 1258138568Ssam } else 1259138568Ssam _ieee80211_free_node(ni); 1260138568Ssam} 1261138568Ssam 1262138568Ssamstatic void 1263138568Ssamieee80211_free_allnodes_locked(struct ieee80211_node_table *nt) 1264138568Ssam{ 1265138568Ssam struct ieee80211com *ic = nt->nt_ic; 1266116742Ssam struct ieee80211_node *ni; 1267116742Ssam 1268138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1269138568Ssam "%s: free all nodes in %s table\n", __func__, nt->nt_name); 1270138568Ssam 1271138568Ssam while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) { 1272138568Ssam if (ni->ni_associd != 0) { 1273138568Ssam if (ic->ic_auth->ia_node_leave != NULL) 1274138568Ssam ic->ic_auth->ia_node_leave(ic, ni); 1275138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1276138568Ssam } 1277138568Ssam node_reclaim(nt, ni); 1278138568Ssam } 1279138568Ssam ieee80211_reset_erp(ic); 1280116742Ssam} 1281116742Ssam 1282138568Ssamstatic void 1283138568Ssamieee80211_free_allnodes(struct ieee80211_node_table *nt) 1284138568Ssam{ 1285138568Ssam 1286138568Ssam IEEE80211_NODE_LOCK(nt); 1287138568Ssam ieee80211_free_allnodes_locked(nt); 1288138568Ssam IEEE80211_NODE_UNLOCK(nt); 1289138568Ssam} 1290138568Ssam 1291120483Ssam/* 1292138568Ssam * Timeout entries in the scan cache. 1293138568Ssam */ 1294138568Ssamstatic void 1295138568Ssamieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt) 1296138568Ssam{ 1297138568Ssam struct ieee80211com *ic = nt->nt_ic; 1298138568Ssam struct ieee80211_node *ni, *tni; 1299138568Ssam 1300138568Ssam IEEE80211_NODE_LOCK(nt); 1301138568Ssam ni = ic->ic_bss; 1302138568Ssam /* XXX belongs elsewhere */ 1303138568Ssam if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) { 1304138568Ssam m_freem(ni->ni_rxfrag[0]); 1305138568Ssam ni->ni_rxfrag[0] = NULL; 1306138568Ssam } 1307138568Ssam TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) { 1308138568Ssam if (ni->ni_inact && --ni->ni_inact == 0) { 1309138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1310138568Ssam "[%s] scan candidate purged from cache " 1311138568Ssam "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr), 1312138568Ssam ieee80211_node_refcnt(ni)-1); 1313138568Ssam node_reclaim(nt, ni); 1314138568Ssam } 1315138568Ssam } 1316138568Ssam IEEE80211_NODE_UNLOCK(nt); 1317138568Ssam 1318138568Ssam nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1319138568Ssam} 1320138568Ssam 1321138568Ssam/* 1322138568Ssam * Timeout inactive stations and do related housekeeping. 1323138568Ssam * Note that we cannot hold the node lock while sending a 1324138568Ssam * frame as this would lead to a LOR. Instead we use a 1325138568Ssam * generation number to mark nodes that we've scanned and 1326138568Ssam * drop the lock and restart a scan if we have to time out 1327138568Ssam * a node. Since we are single-threaded by virtue of 1328120483Ssam * controlling the inactivity timer we can be sure this will 1329120483Ssam * process each node only once. 1330120483Ssam */ 1331138568Ssamstatic void 1332138568Ssamieee80211_timeout_stations(struct ieee80211_node_table *nt) 1333116742Ssam{ 1334138568Ssam struct ieee80211com *ic = nt->nt_ic; 1335120483Ssam struct ieee80211_node *ni; 1336138568Ssam u_int gen; 1337116742Ssam 1338138568Ssam IEEE80211_SCAN_LOCK(nt); 1339138568Ssam gen = nt->nt_scangen++; 1340138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1341138568Ssam "%s: sta scangen %u\n", __func__, gen); 1342120483Ssamrestart: 1343138568Ssam IEEE80211_NODE_LOCK(nt); 1344138568Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1345120483Ssam if (ni->ni_scangen == gen) /* previously handled */ 1346120483Ssam continue; 1347120483Ssam ni->ni_scangen = gen; 1348138568Ssam /* 1349138568Ssam * Free fragment if not needed anymore 1350138568Ssam * (last fragment older than 1s). 1351138568Ssam * XXX doesn't belong here 1352138568Ssam */ 1353138568Ssam if (ni->ni_rxfrag[0] != NULL && 1354138568Ssam ticks > ni->ni_rxfragstamp + hz) { 1355138568Ssam m_freem(ni->ni_rxfrag[0]); 1356138568Ssam ni->ni_rxfrag[0] = NULL; 1357138568Ssam } 1358140498Ssam /* 1359140498Ssam * Special case ourself; we may be idle for extended periods 1360140498Ssam * of time and regardless reclaiming our state is wrong. 1361140498Ssam */ 1362140498Ssam if (ni == ic->ic_bss) 1363140498Ssam continue; 1364138568Ssam ni->ni_inact--; 1365138568Ssam if (ni->ni_associd != 0) { 1366119150Ssam /* 1367138568Ssam * Age frames on the power save queue. The 1368138568Ssam * aging interval is 4 times the listen 1369138568Ssam * interval specified by the station. This 1370138568Ssam * number is factored into the age calculations 1371138568Ssam * when the frame is placed on the queue. We 1372138568Ssam * store ages as time differences we can check 1373138568Ssam * and/or adjust only the head of the list. 1374138568Ssam */ 1375138568Ssam if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) { 1376138568Ssam struct mbuf *m; 1377138568Ssam int discard = 0; 1378138568Ssam 1379138568Ssam IEEE80211_NODE_SAVEQ_LOCK(ni); 1380138568Ssam while (IF_POLL(&ni->ni_savedq, m) != NULL && 1381138568Ssam M_AGE_GET(m) < IEEE80211_INACT_WAIT) { 1382138568SsamIEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/ 1383138568Ssam _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m); 1384138568Ssam m_freem(m); 1385138568Ssam discard++; 1386138568Ssam } 1387138568Ssam if (m != NULL) 1388138568Ssam M_AGE_SUB(m, IEEE80211_INACT_WAIT); 1389138568Ssam IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1390138568Ssam 1391138568Ssam if (discard != 0) { 1392138568Ssam IEEE80211_DPRINTF(ic, 1393138568Ssam IEEE80211_MSG_POWER, 1394138568Ssam "[%s] discard %u frames for age\n", 1395138568Ssam ether_sprintf(ni->ni_macaddr), 1396138568Ssam discard); 1397138568Ssam IEEE80211_NODE_STAT_ADD(ni, 1398138568Ssam ps_discard, discard); 1399138568Ssam if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) 1400138568Ssam ic->ic_set_tim(ic, ni, 0); 1401138568Ssam } 1402138568Ssam } 1403138568Ssam /* 1404138568Ssam * Probe the station before time it out. We 1405138568Ssam * send a null data frame which may not be 1406138568Ssam * universally supported by drivers (need it 1407138568Ssam * for ps-poll support so it should be...). 1408138568Ssam */ 1409139528Ssam if (0 < ni->ni_inact && 1410139528Ssam ni->ni_inact <= ic->ic_inact_probe) { 1411138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1412138568Ssam "[%s] probe station due to inactivity\n", 1413138568Ssam ether_sprintf(ni->ni_macaddr)); 1414138568Ssam IEEE80211_NODE_UNLOCK(nt); 1415138568Ssam ieee80211_send_nulldata(ic, ni); 1416138568Ssam /* XXX stat? */ 1417138568Ssam goto restart; 1418138568Ssam } 1419138568Ssam } 1420138568Ssam if (ni->ni_inact <= 0) { 1421138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1422138568Ssam "[%s] station timed out due to inactivity " 1423138568Ssam "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr), 1424138568Ssam ieee80211_node_refcnt(ni)); 1425138568Ssam /* 1426138568Ssam * Send a deauthenticate frame and drop the station. 1427138568Ssam * This is somewhat complicated due to reference counts 1428138568Ssam * and locking. At this point a station will typically 1429138568Ssam * have a reference count of 1. ieee80211_node_leave 1430138568Ssam * will do a "free" of the node which will drop the 1431138568Ssam * reference count. But in the meantime a reference 1432138568Ssam * wil be held by the deauth frame. The actual reclaim 1433138568Ssam * of the node will happen either after the tx is 1434138568Ssam * completed or by ieee80211_node_leave. 1435120483Ssam * 1436138568Ssam * Separately we must drop the node lock before sending 1437138568Ssam * in case the driver takes a lock, as this will result 1438138568Ssam * in LOR between the node lock and the driver lock. 1439119150Ssam */ 1440138568Ssam IEEE80211_NODE_UNLOCK(nt); 1441138568Ssam if (ni->ni_associd != 0) { 1442138568Ssam IEEE80211_SEND_MGMT(ic, ni, 1443138568Ssam IEEE80211_FC0_SUBTYPE_DEAUTH, 1444138568Ssam IEEE80211_REASON_AUTH_EXPIRE); 1445138568Ssam } 1446138568Ssam ieee80211_node_leave(ic, ni); 1447121180Ssam ic->ic_stats.is_node_timeout++; 1448120483Ssam goto restart; 1449120483Ssam } 1450116742Ssam } 1451138568Ssam IEEE80211_NODE_UNLOCK(nt); 1452138568Ssam 1453138568Ssam IEEE80211_SCAN_UNLOCK(nt); 1454138568Ssam 1455138568Ssam nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1456116742Ssam} 1457116742Ssam 1458116742Ssamvoid 1459138568Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg) 1460116742Ssam{ 1461116742Ssam struct ieee80211_node *ni; 1462138568Ssam u_int gen; 1463116742Ssam 1464138568Ssam IEEE80211_SCAN_LOCK(nt); 1465138568Ssam gen = nt->nt_scangen++; 1466138568Ssamrestart: 1467138568Ssam IEEE80211_NODE_LOCK(nt); 1468138568Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1469138568Ssam if (ni->ni_scangen != gen) { 1470138568Ssam ni->ni_scangen = gen; 1471138568Ssam (void) ieee80211_ref_node(ni); 1472138568Ssam IEEE80211_NODE_UNLOCK(nt); 1473138568Ssam (*f)(arg, ni); 1474138568Ssam ieee80211_free_node(ni); 1475138568Ssam goto restart; 1476138568Ssam } 1477138568Ssam } 1478138568Ssam IEEE80211_NODE_UNLOCK(nt); 1479138568Ssam 1480138568Ssam IEEE80211_SCAN_UNLOCK(nt); 1481116742Ssam} 1482138568Ssam 1483138568Ssamvoid 1484138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1485138568Ssam{ 1486138568Ssam printf("0x%p: mac %s refcnt %d\n", ni, 1487138568Ssam ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 1488138568Ssam printf("\tscangen %u authmode %u flags 0x%x\n", 1489138568Ssam ni->ni_scangen, ni->ni_authmode, ni->ni_flags); 1490138568Ssam printf("\tassocid 0x%x txpower %u vlan %u\n", 1491138568Ssam ni->ni_associd, ni->ni_txpower, ni->ni_vlan); 1492138568Ssam printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n", 1493138568Ssam ni->ni_txseqs[0], 1494138568Ssam ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT, 1495138568Ssam ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK, 1496138568Ssam ni->ni_rxfragstamp); 1497138568Ssam printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n", 1498138568Ssam ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo); 1499138568Ssam printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n", 1500138568Ssam ether_sprintf(ni->ni_bssid), 1501138568Ssam ni->ni_esslen, ni->ni_essid, 1502138568Ssam ni->ni_chan->ic_freq, ni->ni_chan->ic_flags); 1503138568Ssam printf("\tfails %u inact %u txrate %u\n", 1504138568Ssam ni->ni_fails, ni->ni_inact, ni->ni_txrate); 1505138568Ssam} 1506138568Ssam 1507138568Ssamvoid 1508138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt) 1509138568Ssam{ 1510138568Ssam ieee80211_iterate_nodes(nt, 1511138568Ssam (ieee80211_iter_func *) ieee80211_dump_node, nt); 1512138568Ssam} 1513138568Ssam 1514138568Ssam/* 1515138568Ssam * Handle a station joining an 11g network. 1516138568Ssam */ 1517138568Ssamstatic void 1518138568Ssamieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 1519138568Ssam{ 1520138568Ssam 1521138568Ssam /* 1522138568Ssam * Station isn't capable of short slot time. Bump 1523138568Ssam * the count of long slot time stations and disable 1524138568Ssam * use of short slot time. Note that the actual switch 1525138568Ssam * over to long slot time use may not occur until the 1526138568Ssam * next beacon transmission (per sec. 7.3.1.4 of 11g). 1527138568Ssam */ 1528138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 1529138568Ssam ic->ic_longslotsta++; 1530138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1531138568Ssam "[%s] station needs long slot time, count %d\n", 1532138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 1533138568Ssam /* XXX vap's w/ conflicting needs won't work */ 1534138568Ssam ieee80211_set_shortslottime(ic, 0); 1535138568Ssam } 1536138568Ssam /* 1537138568Ssam * If the new station is not an ERP station 1538138568Ssam * then bump the counter and enable protection 1539138568Ssam * if configured. 1540138568Ssam */ 1541138568Ssam if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) { 1542138568Ssam ic->ic_nonerpsta++; 1543138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1544138568Ssam "[%s] station is !ERP, %d non-ERP stations associated\n", 1545138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 1546138568Ssam /* 1547138568Ssam * If protection is configured, enable it. 1548138568Ssam */ 1549138568Ssam if (ic->ic_protmode != IEEE80211_PROT_NONE) { 1550138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1551138568Ssam "%s: enable use of protection\n", __func__); 1552138568Ssam ic->ic_flags |= IEEE80211_F_USEPROT; 1553138568Ssam } 1554138568Ssam /* 1555138568Ssam * If station does not support short preamble 1556138568Ssam * then we must enable use of Barker preamble. 1557138568Ssam */ 1558138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) { 1559138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1560138568Ssam "[%s] station needs long preamble\n", 1561138568Ssam ether_sprintf(ni->ni_macaddr)); 1562138568Ssam ic->ic_flags |= IEEE80211_F_USEBARKER; 1563138568Ssam ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 1564138568Ssam } 1565138568Ssam } else 1566138568Ssam ni->ni_flags |= IEEE80211_NODE_ERP; 1567138568Ssam} 1568138568Ssam 1569138568Ssamvoid 1570138568Ssamieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp) 1571138568Ssam{ 1572138568Ssam int newassoc; 1573138568Ssam 1574138568Ssam if (ni->ni_associd == 0) { 1575138568Ssam u_int16_t aid; 1576138568Ssam 1577138568Ssam /* 1578138568Ssam * It would be good to search the bitmap 1579138568Ssam * more efficiently, but this will do for now. 1580138568Ssam */ 1581138568Ssam for (aid = 1; aid < ic->ic_max_aid; aid++) { 1582138568Ssam if (!IEEE80211_AID_ISSET(aid, 1583138568Ssam ic->ic_aid_bitmap)) 1584138568Ssam break; 1585138568Ssam } 1586138568Ssam if (aid >= ic->ic_max_aid) { 1587138568Ssam IEEE80211_SEND_MGMT(ic, ni, resp, 1588138568Ssam IEEE80211_REASON_ASSOC_TOOMANY); 1589138568Ssam ieee80211_node_leave(ic, ni); 1590138568Ssam return; 1591138568Ssam } 1592138568Ssam ni->ni_associd = aid | 0xc000; 1593138568Ssam IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap); 1594138568Ssam ic->ic_sta_assoc++; 1595138568Ssam newassoc = 1; 1596139522Ssam if (ic->ic_curmode == IEEE80211_MODE_11G || 1597139522Ssam ic->ic_curmode == IEEE80211_MODE_TURBO_G) 1598138568Ssam ieee80211_node_join_11g(ic, ni); 1599138568Ssam } else 1600138568Ssam newassoc = 0; 1601138568Ssam 1602138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 1603139523Ssam "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n", 1604139523Ssam ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re", 1605139523Ssam IEEE80211_NODE_AID(ni), 1606139523Ssam ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long", 1607139523Ssam ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", 1608139523Ssam ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "", 1609139523Ssam ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "" 1610139523Ssam ); 1611138568Ssam 1612138568Ssam /* give driver a chance to setup state like ni_txrate */ 1613139524Ssam if (ic->ic_newassoc != NULL) 1614138568Ssam ic->ic_newassoc(ic, ni, newassoc); 1615139528Ssam ni->ni_inact_reload = ic->ic_inact_auth; 1616139528Ssam ni->ni_inact = ni->ni_inact_reload; 1617138568Ssam IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS); 1618138568Ssam /* tell the authenticator about new station */ 1619138568Ssam if (ic->ic_auth->ia_node_join != NULL) 1620138568Ssam ic->ic_auth->ia_node_join(ic, ni); 1621138568Ssam ieee80211_notify_node_join(ic, ni, newassoc); 1622138568Ssam} 1623138568Ssam 1624138568Ssam/* 1625138568Ssam * Handle a station leaving an 11g network. 1626138568Ssam */ 1627138568Ssamstatic void 1628138568Ssamieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 1629138568Ssam{ 1630138568Ssam 1631139522Ssam KASSERT(ic->ic_curmode == IEEE80211_MODE_11G || 1632139522Ssam ic->ic_curmode == IEEE80211_MODE_TURBO_G, 1633138568Ssam ("not in 11g, curmode %x", ic->ic_curmode)); 1634138568Ssam 1635138568Ssam /* 1636138568Ssam * If a long slot station do the slot time bookkeeping. 1637138568Ssam */ 1638138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 1639138568Ssam KASSERT(ic->ic_longslotsta > 0, 1640138568Ssam ("bogus long slot station count %d", ic->ic_longslotsta)); 1641138568Ssam ic->ic_longslotsta--; 1642138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1643138568Ssam "[%s] long slot time station leaves, count now %d\n", 1644138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 1645138568Ssam if (ic->ic_longslotsta == 0) { 1646138568Ssam /* 1647138568Ssam * Re-enable use of short slot time if supported 1648138568Ssam * and not operating in IBSS mode (per spec). 1649138568Ssam */ 1650138568Ssam if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 1651138568Ssam ic->ic_opmode != IEEE80211_M_IBSS) { 1652138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1653138568Ssam "%s: re-enable use of short slot time\n", 1654138568Ssam __func__); 1655138568Ssam ieee80211_set_shortslottime(ic, 1); 1656138568Ssam } 1657138568Ssam } 1658138568Ssam } 1659138568Ssam /* 1660138568Ssam * If a non-ERP station do the protection-related bookkeeping. 1661138568Ssam */ 1662138568Ssam if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) { 1663138568Ssam KASSERT(ic->ic_nonerpsta > 0, 1664138568Ssam ("bogus non-ERP station count %d", ic->ic_nonerpsta)); 1665138568Ssam ic->ic_nonerpsta--; 1666138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1667138568Ssam "[%s] non-ERP station leaves, count now %d\n", 1668138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 1669138568Ssam if (ic->ic_nonerpsta == 0) { 1670138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1671138568Ssam "%s: disable use of protection\n", __func__); 1672138568Ssam ic->ic_flags &= ~IEEE80211_F_USEPROT; 1673138568Ssam /* XXX verify mode? */ 1674138568Ssam if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) { 1675138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1676138568Ssam "%s: re-enable use of short preamble\n", 1677138568Ssam __func__); 1678138568Ssam ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 1679138568Ssam ic->ic_flags &= ~IEEE80211_F_USEBARKER; 1680138568Ssam } 1681138568Ssam } 1682138568Ssam } 1683138568Ssam} 1684138568Ssam 1685138568Ssam/* 1686138568Ssam * Handle bookkeeping for station deauthentication/disassociation 1687138568Ssam * when operating as an ap. 1688138568Ssam */ 1689138568Ssamvoid 1690138568Ssamieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 1691138568Ssam{ 1692140499Ssam struct ieee80211_node_table *nt = ni->ni_table; 1693138568Ssam 1694138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 1695138568Ssam "[%s] station with aid %d leaves\n", 1696138568Ssam ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni)); 1697138568Ssam 1698138568Ssam KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 1699138568Ssam ic->ic_opmode == IEEE80211_M_IBSS || 1700138568Ssam ic->ic_opmode == IEEE80211_M_AHDEMO, 1701138568Ssam ("unexpected operating mode %u", ic->ic_opmode)); 1702138568Ssam /* 1703138568Ssam * If node wasn't previously associated all 1704138568Ssam * we need to do is reclaim the reference. 1705138568Ssam */ 1706138568Ssam /* XXX ibss mode bypasses 11g and notification */ 1707138568Ssam if (ni->ni_associd == 0) 1708138568Ssam goto done; 1709138568Ssam /* 1710138568Ssam * Tell the authenticator the station is leaving. 1711138568Ssam * Note that we must do this before yanking the 1712138568Ssam * association id as the authenticator uses the 1713138568Ssam * associd to locate it's state block. 1714138568Ssam */ 1715138568Ssam if (ic->ic_auth->ia_node_leave != NULL) 1716138568Ssam ic->ic_auth->ia_node_leave(ic, ni); 1717138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1718138568Ssam ni->ni_associd = 0; 1719138568Ssam ic->ic_sta_assoc--; 1720138568Ssam 1721139522Ssam if (ic->ic_curmode == IEEE80211_MODE_11G || 1722139522Ssam ic->ic_curmode == IEEE80211_MODE_TURBO_G) 1723138568Ssam ieee80211_node_leave_11g(ic, ni); 1724138568Ssam /* 1725138568Ssam * Cleanup station state. In particular clear various 1726138568Ssam * state that might otherwise be reused if the node 1727138568Ssam * is reused before the reference count goes to zero 1728138568Ssam * (and memory is reclaimed). 1729138568Ssam */ 1730138568Ssam ieee80211_sta_leave(ic, ni); 1731138568Ssamdone: 1732140499Ssam /* 1733140499Ssam * Remove the node from any table it's recorded in and 1734140499Ssam * drop the caller's reference. Removal from the table 1735140499Ssam * is important to insure the node is not reprocessed 1736140499Ssam * for inactivity. 1737140499Ssam */ 1738140499Ssam if (nt != NULL) { 1739140499Ssam IEEE80211_NODE_LOCK(nt); 1740140499Ssam node_reclaim(nt, ni); 1741140499Ssam IEEE80211_NODE_UNLOCK(nt); 1742140499Ssam } else 1743140499Ssam ieee80211_free_node(ni); 1744138568Ssam} 1745138568Ssam 1746138568Ssamu_int8_t 1747138568Ssamieee80211_getrssi(struct ieee80211com *ic) 1748138568Ssam{ 1749138568Ssam#define NZ(x) ((x) == 0 ? 1 : (x)) 1750140753Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 1751138568Ssam u_int32_t rssi_samples, rssi_total; 1752138568Ssam struct ieee80211_node *ni; 1753138568Ssam 1754138568Ssam rssi_total = 0; 1755138568Ssam rssi_samples = 0; 1756138568Ssam switch (ic->ic_opmode) { 1757138568Ssam case IEEE80211_M_IBSS: /* average of all ibss neighbors */ 1758138568Ssam /* XXX locking */ 1759140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 1760138568Ssam if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) { 1761138568Ssam rssi_samples++; 1762138568Ssam rssi_total += ic->ic_node_getrssi(ni); 1763138568Ssam } 1764138568Ssam break; 1765138568Ssam case IEEE80211_M_AHDEMO: /* average of all neighbors */ 1766138568Ssam /* XXX locking */ 1767140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1768138568Ssam rssi_samples++; 1769138568Ssam rssi_total += ic->ic_node_getrssi(ni); 1770138568Ssam } 1771138568Ssam break; 1772138568Ssam case IEEE80211_M_HOSTAP: /* average of all associated stations */ 1773138568Ssam /* XXX locking */ 1774140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 1775138568Ssam if (IEEE80211_AID(ni->ni_associd) != 0) { 1776138568Ssam rssi_samples++; 1777138568Ssam rssi_total += ic->ic_node_getrssi(ni); 1778138568Ssam } 1779138568Ssam break; 1780138568Ssam case IEEE80211_M_MONITOR: /* XXX */ 1781138568Ssam case IEEE80211_M_STA: /* use stats from associated ap */ 1782138568Ssam default: 1783138568Ssam if (ic->ic_bss != NULL) 1784138568Ssam rssi_total = ic->ic_node_getrssi(ic->ic_bss); 1785138568Ssam rssi_samples = 1; 1786138568Ssam break; 1787138568Ssam } 1788138568Ssam return rssi_total / NZ(rssi_samples); 1789138568Ssam#undef NZ 1790138568Ssam} 1791138568Ssam 1792138568Ssam/* 1793138568Ssam * Indicate whether there are frames queued for a station in power-save mode. 1794138568Ssam */ 1795138568Ssamstatic void 1796138568Ssamieee80211_set_tim(struct ieee80211com *ic, struct ieee80211_node *ni, int set) 1797138568Ssam{ 1798138568Ssam u_int16_t aid; 1799138568Ssam 1800138568Ssam KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 1801138568Ssam ic->ic_opmode == IEEE80211_M_IBSS, 1802138568Ssam ("operating mode %u", ic->ic_opmode)); 1803138568Ssam 1804138568Ssam aid = IEEE80211_AID(ni->ni_associd); 1805138568Ssam KASSERT(aid < ic->ic_max_aid, 1806138568Ssam ("bogus aid %u, max %u", aid, ic->ic_max_aid)); 1807138568Ssam 1808138568Ssam IEEE80211_BEACON_LOCK(ic); 1809138568Ssam if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) { 1810138568Ssam if (set) { 1811138568Ssam setbit(ic->ic_tim_bitmap, aid); 1812138568Ssam ic->ic_ps_pending++; 1813138568Ssam } else { 1814138568Ssam clrbit(ic->ic_tim_bitmap, aid); 1815138568Ssam ic->ic_ps_pending--; 1816138568Ssam } 1817138568Ssam ic->ic_flags |= IEEE80211_F_TIMUPDATE; 1818138568Ssam } 1819138568Ssam IEEE80211_BEACON_UNLOCK(ic); 1820138568Ssam} 1821138568Ssam 1822138568Ssam/* 1823138568Ssam * Node table support. 1824138568Ssam */ 1825138568Ssam 1826138568Ssamstatic void 1827138568Ssamieee80211_node_table_init(struct ieee80211com *ic, 1828138568Ssam struct ieee80211_node_table *nt, 1829138568Ssam const char *name, int inact, 1830138568Ssam void (*timeout)(struct ieee80211_node_table *)) 1831138568Ssam{ 1832138568Ssam 1833138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1834138568Ssam "%s %s table, inact %u\n", __func__, name, inact); 1835138568Ssam 1836138568Ssam nt->nt_ic = ic; 1837138568Ssam /* XXX need unit */ 1838138568Ssam IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname); 1839138568Ssam IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname); 1840138568Ssam TAILQ_INIT(&nt->nt_node); 1841138568Ssam nt->nt_name = name; 1842138568Ssam nt->nt_scangen = 1; 1843138568Ssam nt->nt_inact_init = inact; 1844138568Ssam nt->nt_timeout = timeout; 1845138568Ssam} 1846138568Ssam 1847138568Ssamvoid 1848138568Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt) 1849138568Ssam{ 1850138568Ssam 1851138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1852138568Ssam "%s %s table\n", __func__, nt->nt_name); 1853138568Ssam 1854138568Ssam IEEE80211_NODE_LOCK(nt); 1855138568Ssam nt->nt_inact_timer = 0; 1856138568Ssam ieee80211_free_allnodes_locked(nt); 1857138568Ssam IEEE80211_NODE_UNLOCK(nt); 1858138568Ssam} 1859138568Ssam 1860138568Ssamstatic void 1861138568Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt) 1862138568Ssam{ 1863138568Ssam 1864138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1865138568Ssam "%s %s table\n", __func__, nt->nt_name); 1866138568Ssam 1867138568Ssam ieee80211_free_allnodes_locked(nt); 1868138568Ssam IEEE80211_SCAN_LOCK_DESTROY(nt); 1869138568Ssam IEEE80211_NODE_LOCK_DESTROY(nt); 1870138568Ssam} 1871