ieee80211_node.c revision 148302
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 148302 2005-07-22 17:36:12Z 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 52147221Ssam/* 53147221Ssam * Association id's are managed with a bit vector. 54147221Ssam */ 55147221Ssam#define IEEE80211_AID_SET(b, w) \ 56147221Ssam ((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32))) 57147221Ssam#define IEEE80211_AID_CLR(b, w) \ 58147221Ssam ((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32))) 59147221Ssam#define IEEE80211_AID_ISSET(b, w) \ 60147221Ssam ((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32))) 61147221Ssam 62138568Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211_node_table *); 63138568Ssamstatic void node_cleanup(struct ieee80211_node *); 64138568Ssamstatic void node_free(struct ieee80211_node *); 65138568Ssamstatic u_int8_t node_getrssi(const struct ieee80211_node *); 66116742Ssam 67138568Ssamstatic void ieee80211_setup_node(struct ieee80211_node_table *, 68138568Ssam struct ieee80211_node *, const u_int8_t *); 69138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *); 70138568Ssamstatic void ieee80211_free_allnodes(struct ieee80211_node_table *); 71120104Ssam 72138568Ssamstatic void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *); 73138568Ssamstatic void ieee80211_timeout_stations(struct ieee80211_node_table *); 74116742Ssam 75138568Ssamstatic void ieee80211_set_tim(struct ieee80211com *, 76138568Ssam struct ieee80211_node *, int set); 77138568Ssam 78138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic, 79138568Ssam struct ieee80211_node_table *nt, const char *name, int inact, 80138568Ssam void (*timeout)(struct ieee80211_node_table *)); 81138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt); 82138568Ssam 83127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state"); 84120481Ssam 85116742Ssamvoid 86138568Ssamieee80211_node_attach(struct ieee80211com *ic) 87116742Ssam{ 88116742Ssam 89140753Ssam ieee80211_node_table_init(ic, &ic->ic_sta, "station", 90140753Ssam IEEE80211_INACT_INIT, ieee80211_timeout_stations); 91138568Ssam ieee80211_node_table_init(ic, &ic->ic_scan, "scan", 92138568Ssam IEEE80211_INACT_SCAN, ieee80211_timeout_scan_candidates); 93138568Ssam 94138568Ssam ic->ic_node_alloc = node_alloc; 95138568Ssam ic->ic_node_free = node_free; 96138568Ssam ic->ic_node_cleanup = node_cleanup; 97138568Ssam ic->ic_node_getrssi = node_getrssi; 98138568Ssam 99138568Ssam /* default station inactivity timer setings */ 100138568Ssam ic->ic_inact_init = IEEE80211_INACT_INIT; 101138568Ssam ic->ic_inact_auth = IEEE80211_INACT_AUTH; 102138568Ssam ic->ic_inact_run = IEEE80211_INACT_RUN; 103138568Ssam ic->ic_inact_probe = IEEE80211_INACT_PROBE; 104138568Ssam 105138568Ssam /* XXX defer */ 106138568Ssam if (ic->ic_max_aid == 0) 107138568Ssam ic->ic_max_aid = IEEE80211_AID_DEF; 108138568Ssam else if (ic->ic_max_aid > IEEE80211_AID_MAX) 109138568Ssam ic->ic_max_aid = IEEE80211_AID_MAX; 110138568Ssam MALLOC(ic->ic_aid_bitmap, u_int32_t *, 111138568Ssam howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t), 112138568Ssam M_DEVBUF, M_NOWAIT | M_ZERO); 113138568Ssam if (ic->ic_aid_bitmap == NULL) { 114138568Ssam /* XXX no way to recover */ 115138568Ssam printf("%s: no memory for AID bitmap!\n", __func__); 116138568Ssam ic->ic_max_aid = 0; 117138568Ssam } 118138568Ssam 119138568Ssam /* XXX defer until using hostap/ibss mode */ 120138568Ssam ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t); 121138568Ssam MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len, 122138568Ssam M_DEVBUF, M_NOWAIT | M_ZERO); 123138568Ssam if (ic->ic_tim_bitmap == NULL) { 124138568Ssam /* XXX no way to recover */ 125138568Ssam printf("%s: no memory for TIM bitmap!\n", __func__); 126138568Ssam } 127138568Ssam ic->ic_set_tim = ieee80211_set_tim; /* NB: driver should override */ 128118887Ssam} 129118887Ssam 130118887Ssamvoid 131138568Ssamieee80211_node_lateattach(struct ieee80211com *ic) 132118887Ssam{ 133127766Ssam struct ieee80211_node *ni; 134138568Ssam struct ieee80211_rsnparms *rsn; 135118887Ssam 136138568Ssam ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr); 137127766Ssam KASSERT(ni != NULL, ("unable to setup inital BSS node")); 138138568Ssam /* 139138568Ssam * Setup "global settings" in the bss node so that 140138568Ssam * each new station automatically inherits them. 141138568Ssam */ 142138568Ssam rsn = &ni->ni_rsn; 143138568Ssam /* WEP, TKIP, and AES-CCM are always supported */ 144138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP; 145138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP; 146138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM; 147138568Ssam if (ic->ic_caps & IEEE80211_C_AES) 148138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB; 149138568Ssam if (ic->ic_caps & IEEE80211_C_CKIP) 150138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP; 151138568Ssam /* 152138568Ssam * Default unicast cipher to WEP for 802.1x use. If 153138568Ssam * WPA is enabled the management code will set these 154138568Ssam * values to reflect. 155138568Ssam */ 156138568Ssam rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP; 157138568Ssam rsn->rsn_ucastkeylen = 104 / NBBY; 158138568Ssam /* 159138568Ssam * WPA says the multicast cipher is the lowest unicast 160138568Ssam * cipher supported. But we skip WEP which would 161138568Ssam * otherwise be used based on this criteria. 162138568Ssam */ 163138568Ssam rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP; 164138568Ssam rsn->rsn_mcastkeylen = 128 / NBBY; 165138568Ssam 166138568Ssam /* 167138568Ssam * We support both WPA-PSK and 802.1x; the one used 168138568Ssam * is determined by the authentication mode and the 169138568Ssam * setting of the PSK state. 170138568Ssam */ 171138568Ssam rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK; 172138568Ssam rsn->rsn_keymgmt = WPA_ASE_8021X_PSK; 173138568Ssam 174138568Ssam ic->ic_bss = ieee80211_ref_node(ni); /* hold reference */ 175138568Ssam ic->ic_auth = ieee80211_authenticator_get(ni->ni_authmode); 176116742Ssam} 177116742Ssam 178116742Ssamvoid 179138568Ssamieee80211_node_detach(struct ieee80211com *ic) 180116742Ssam{ 181116742Ssam 182138568Ssam if (ic->ic_bss != NULL) { 183138568Ssam ieee80211_free_node(ic->ic_bss); 184138568Ssam ic->ic_bss = NULL; 185138568Ssam } 186138568Ssam ieee80211_node_table_cleanup(&ic->ic_scan); 187140753Ssam ieee80211_node_table_cleanup(&ic->ic_sta); 188138568Ssam if (ic->ic_aid_bitmap != NULL) { 189138568Ssam FREE(ic->ic_aid_bitmap, M_DEVBUF); 190138568Ssam ic->ic_aid_bitmap = NULL; 191138568Ssam } 192138568Ssam if (ic->ic_tim_bitmap != NULL) { 193138568Ssam FREE(ic->ic_tim_bitmap, M_DEVBUF); 194138568Ssam ic->ic_tim_bitmap = NULL; 195138568Ssam } 196116742Ssam} 197116742Ssam 198138568Ssam/* 199138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator. 200138568Ssam */ 201138568Ssam 202138568Ssamvoid 203148302Ssamieee80211_node_authorize(struct ieee80211_node *ni) 204138568Ssam{ 205148302Ssam struct ieee80211com *ic = ni->ni_ic; 206148302Ssam 207138568Ssam ni->ni_flags |= IEEE80211_NODE_AUTH; 208139528Ssam ni->ni_inact_reload = ic->ic_inact_run; 209138568Ssam} 210138568Ssam 211138568Ssamvoid 212148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni) 213138568Ssam{ 214138568Ssam ni->ni_flags &= ~IEEE80211_NODE_AUTH; 215138568Ssam} 216138568Ssam 217116742Ssam/* 218138568Ssam * Set/change the channel. The rate set is also updated as 219138568Ssam * to insure a consistent view by drivers. 220138568Ssam */ 221138568Ssamstatic __inline void 222138568Ssamieee80211_set_chan(struct ieee80211com *ic, 223138568Ssam struct ieee80211_node *ni, struct ieee80211_channel *chan) 224138568Ssam{ 225138568Ssam ni->ni_chan = chan; 226138568Ssam ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)]; 227138568Ssam} 228138568Ssam 229138568Ssam/* 230116742Ssam * AP scanning support. 231116742Ssam */ 232116742Ssam 233138568Ssam#ifdef IEEE80211_DEBUG 234138568Ssamstatic void 235138568Ssamdump_chanlist(const u_char chans[]) 236138568Ssam{ 237138568Ssam const char *sep; 238138568Ssam int i; 239138568Ssam 240138568Ssam sep = " "; 241138568Ssam for (i = 0; i < IEEE80211_CHAN_MAX; i++) 242138568Ssam if (isset(chans, i)) { 243138568Ssam printf("%s%u", sep, i); 244138568Ssam sep = ", "; 245138568Ssam } 246138568Ssam} 247138568Ssam#endif /* IEEE80211_DEBUG */ 248138568Ssam 249116742Ssam/* 250138568Ssam * Initialize the channel set to scan based on the 251116742Ssam * of available channels and the current PHY mode. 252116742Ssam */ 253117811Ssamstatic void 254138568Ssamieee80211_reset_scan(struct ieee80211com *ic) 255116742Ssam{ 256116742Ssam 257138568Ssam /* XXX ic_des_chan should be handled with ic_chan_active */ 258138568Ssam if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) { 259138568Ssam memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan)); 260138568Ssam setbit(ic->ic_chan_scan, 261138568Ssam ieee80211_chan2ieee(ic, ic->ic_des_chan)); 262138568Ssam } else 263138568Ssam memcpy(ic->ic_chan_scan, ic->ic_chan_active, 264138568Ssam sizeof(ic->ic_chan_active)); 265117811Ssam /* NB: hack, setup so next_scan starts with the first channel */ 266117811Ssam if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC) 267138568Ssam ieee80211_set_chan(ic, ic->ic_bss, 268138568Ssam &ic->ic_channels[IEEE80211_CHAN_MAX]); 269138568Ssam#ifdef IEEE80211_DEBUG 270138568Ssam if (ieee80211_msg_scan(ic)) { 271138568Ssam printf("%s: scan set:", __func__); 272138568Ssam dump_chanlist(ic->ic_chan_scan); 273138568Ssam printf(" start chan %u\n", 274138568Ssam ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)); 275138568Ssam } 276138568Ssam#endif /* IEEE80211_DEBUG */ 277116742Ssam} 278116742Ssam 279116742Ssam/* 280116742Ssam * Begin an active scan. 281116742Ssam */ 282116742Ssamvoid 283138568Ssamieee80211_begin_scan(struct ieee80211com *ic, int reset) 284116742Ssam{ 285116742Ssam 286138568Ssam ic->ic_scan.nt_scangen++; 287117811Ssam /* 288117811Ssam * In all but hostap mode scanning starts off in 289117811Ssam * an active mode before switching to passive. 290117811Ssam */ 291121180Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP) { 292117811Ssam ic->ic_flags |= IEEE80211_F_ASCAN; 293121180Ssam ic->ic_stats.is_scan_active++; 294121180Ssam } else 295121180Ssam ic->ic_stats.is_scan_passive++; 296139520Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 297139520Ssam "begin %s scan in %s mode, scangen %u\n", 298138568Ssam (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive", 299139520Ssam ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen); 300116742Ssam /* 301138568Ssam * Clear scan state and flush any previously seen AP's. 302116742Ssam */ 303138568Ssam ieee80211_reset_scan(ic); 304138568Ssam if (reset) 305138568Ssam ieee80211_free_allnodes(&ic->ic_scan); 306116742Ssam 307138568Ssam ic->ic_flags |= IEEE80211_F_SCAN; 308138568Ssam 309117811Ssam /* Scan the next channel. */ 310138568Ssam ieee80211_next_scan(ic); 311116742Ssam} 312116742Ssam 313116742Ssam/* 314116742Ssam * Switch to the next channel marked for scanning. 315116742Ssam */ 316138568Ssamint 317138568Ssamieee80211_next_scan(struct ieee80211com *ic) 318116742Ssam{ 319116742Ssam struct ieee80211_channel *chan; 320116742Ssam 321138568Ssam /* 322138568Ssam * Insure any previous mgt frame timeouts don't fire. 323138568Ssam * This assumes the driver does the right thing in 324138568Ssam * flushing anything queued in the driver and below. 325138568Ssam */ 326138568Ssam ic->ic_mgt_timer = 0; 327138568Ssam 328116742Ssam chan = ic->ic_bss->ni_chan; 329138568Ssam do { 330116742Ssam if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX]) 331116742Ssam chan = &ic->ic_channels[0]; 332116742Ssam if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) { 333138568Ssam clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan)); 334138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 335138568Ssam "%s: chan %d->%d\n", __func__, 336138568Ssam ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan), 337138568Ssam ieee80211_chan2ieee(ic, chan)); 338138568Ssam ieee80211_set_chan(ic, ic->ic_bss, chan); 339138568Ssam#ifdef notyet 340138568Ssam /* XXX driver state change */ 341116742Ssam /* 342138568Ssam * Scan next channel. If doing an active scan 343138568Ssam * and the channel is not marked passive-only 344138568Ssam * then send a probe request. Otherwise just 345138568Ssam * listen for beacons on the channel. 346116742Ssam */ 347138568Ssam if ((ic->ic_flags & IEEE80211_F_ASCAN) && 348138568Ssam (ni->ni_chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) { 349138568Ssam IEEE80211_SEND_MGMT(ic, ni, 350138568Ssam IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0); 351138568Ssam } 352138568Ssam#else 353138568Ssam ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 354138568Ssam#endif 355138568Ssam return 1; 356116742Ssam } 357138568Ssam } while (chan != ic->ic_bss->ni_chan); 358138568Ssam ieee80211_end_scan(ic); 359138568Ssam return 0; 360116742Ssam} 361116742Ssam 362141658Ssamstatic __inline void 363141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss) 364141658Ssam{ 365141658Ssam /* propagate useful state */ 366141658Ssam nbss->ni_authmode = obss->ni_authmode; 367141658Ssam nbss->ni_txpower = obss->ni_txpower; 368141658Ssam nbss->ni_vlan = obss->ni_vlan; 369141658Ssam nbss->ni_rsn = obss->ni_rsn; 370141658Ssam /* XXX statistics? */ 371141658Ssam} 372141658Ssam 373116742Ssamvoid 374116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 375116742Ssam{ 376140753Ssam struct ieee80211_node_table *nt; 377116742Ssam struct ieee80211_node *ni; 378116742Ssam 379138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 380138568Ssam "%s: creating ibss\n", __func__); 381138568Ssam 382138568Ssam /* 383138568Ssam * Create the station/neighbor table. Note that for adhoc 384138568Ssam * mode we make the initial inactivity timer longer since 385138568Ssam * we create nodes only through discovery and they typically 386138568Ssam * are long-lived associations. 387138568Ssam */ 388140753Ssam nt = &ic->ic_sta; 389140753Ssam IEEE80211_NODE_LOCK(nt); 390140753Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 391140753Ssam nt->nt_name = "station"; 392140753Ssam nt->nt_inact_init = ic->ic_inact_init; 393140753Ssam } else { 394140753Ssam nt->nt_name = "neighbor"; 395140753Ssam nt->nt_inact_init = ic->ic_inact_run; 396140753Ssam } 397140753Ssam IEEE80211_NODE_UNLOCK(nt); 398140753Ssam 399140753Ssam ni = ieee80211_alloc_node(nt, ic->ic_myaddr); 400140753Ssam if (ni == NULL) { 401140753Ssam /* XXX recovery? */ 402138568Ssam return; 403138568Ssam } 404116742Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 405116742Ssam ni->ni_esslen = ic->ic_des_esslen; 406116742Ssam memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 407141658Ssam copy_bss(ni, ic->ic_bss); 408116742Ssam ni->ni_intval = ic->ic_lintval; 409138568Ssam if (ic->ic_flags & IEEE80211_F_PRIVACY) 410116742Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 411116742Ssam if (ic->ic_phytype == IEEE80211_T_FH) { 412116742Ssam ni->ni_fhdwell = 200; /* XXX */ 413116742Ssam ni->ni_fhindex = 1; 414116742Ssam } 415138568Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 416138568Ssam ic->ic_flags |= IEEE80211_F_SIBSS; 417138568Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */ 418143300Ssam if (ic->ic_flags & IEEE80211_F_DESBSSID) 419143300Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid); 420143300Ssam else 421143300Ssam ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */ 422138568Ssam } 423138568Ssam /* 424138568Ssam * Fix the channel and related attributes. 425138568Ssam */ 426138568Ssam ieee80211_set_chan(ic, ni, chan); 427138568Ssam ic->ic_curmode = ieee80211_chan2mode(ic, chan); 428138568Ssam /* 429138568Ssam * Do mode-specific rate setup. 430138568Ssam */ 431138568Ssam if (ic->ic_curmode == IEEE80211_MODE_11G) { 432138568Ssam /* 433138568Ssam * Use a mixed 11b/11g rate set. 434138568Ssam */ 435138568Ssam ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G); 436138568Ssam } else if (ic->ic_curmode == IEEE80211_MODE_11B) { 437138568Ssam /* 438138568Ssam * Force pure 11b rate set. 439138568Ssam */ 440138568Ssam ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B); 441138568Ssam } 442138568Ssam 443140753Ssam (void) ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 444116742Ssam} 445116742Ssam 446138568Ssamvoid 447138568Ssamieee80211_reset_bss(struct ieee80211com *ic) 448138568Ssam{ 449138568Ssam struct ieee80211_node *ni, *obss; 450138568Ssam 451138568Ssam ieee80211_node_table_reset(&ic->ic_scan); 452140753Ssam ieee80211_node_table_reset(&ic->ic_sta); 453140753Ssam 454138568Ssam ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr); 455138568Ssam KASSERT(ni != NULL, ("unable to setup inital BSS node")); 456138568Ssam obss = ic->ic_bss; 457138568Ssam ic->ic_bss = ieee80211_ref_node(ni); 458141658Ssam if (obss != NULL) { 459141658Ssam copy_bss(ni, obss); 460141658Ssam ni->ni_intval = ic->ic_lintval; 461138568Ssam ieee80211_free_node(obss); 462141658Ssam } 463138568Ssam} 464138568Ssam 465127767Ssamstatic int 466138568Ssamieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni) 467127767Ssam{ 468127767Ssam u_int8_t rate; 469127767Ssam int fail; 470127767Ssam 471127767Ssam fail = 0; 472127767Ssam if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 473127767Ssam fail |= 0x01; 474127767Ssam if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 475127767Ssam ni->ni_chan != ic->ic_des_chan) 476127767Ssam fail |= 0x01; 477127767Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 478127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 479127767Ssam fail |= 0x02; 480127767Ssam } else { 481127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 482127767Ssam fail |= 0x02; 483127767Ssam } 484138568Ssam if (ic->ic_flags & IEEE80211_F_PRIVACY) { 485127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 486127767Ssam fail |= 0x04; 487127767Ssam } else { 488127767Ssam /* XXX does this mean privacy is supported or required? */ 489127767Ssam if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 490127767Ssam fail |= 0x04; 491127767Ssam } 492148299Ssam rate = ieee80211_fix_rate(ni, IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 493127767Ssam if (rate & IEEE80211_RATE_BASIC) 494127767Ssam fail |= 0x08; 495127767Ssam if (ic->ic_des_esslen != 0 && 496127767Ssam (ni->ni_esslen != ic->ic_des_esslen || 497127767Ssam memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) 498127767Ssam fail |= 0x10; 499127767Ssam if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 500127767Ssam !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 501127767Ssam fail |= 0x20; 502127767Ssam#ifdef IEEE80211_DEBUG 503138568Ssam if (ieee80211_msg_scan(ic)) { 504127767Ssam printf(" %c %s", fail ? '-' : '+', 505127767Ssam ether_sprintf(ni->ni_macaddr)); 506127767Ssam printf(" %s%c", ether_sprintf(ni->ni_bssid), 507127767Ssam fail & 0x20 ? '!' : ' '); 508127767Ssam printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan), 509127767Ssam fail & 0x01 ? '!' : ' '); 510127767Ssam printf(" %+4d", ni->ni_rssi); 511127767Ssam printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 512127767Ssam fail & 0x08 ? '!' : ' '); 513127767Ssam printf(" %4s%c", 514127767Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 515127767Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 516127767Ssam "????", 517127767Ssam fail & 0x02 ? '!' : ' '); 518127767Ssam printf(" %3s%c ", 519127767Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 520127767Ssam "wep" : "no", 521127767Ssam fail & 0x04 ? '!' : ' '); 522127767Ssam ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 523127767Ssam printf("%s\n", fail & 0x10 ? "!" : ""); 524127767Ssam } 525127767Ssam#endif 526127767Ssam return fail; 527127767Ssam} 528127767Ssam 529138568Ssamstatic __inline u_int8_t 530138568Ssammaxrate(const struct ieee80211_node *ni) 531138568Ssam{ 532138568Ssam const struct ieee80211_rateset *rs = &ni->ni_rates; 533138568Ssam /* NB: assumes rate set is sorted (happens on frame receive) */ 534138568Ssam return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL; 535138568Ssam} 536138568Ssam 537116742Ssam/* 538138568Ssam * Compare the capabilities of two nodes and decide which is 539138568Ssam * more desirable (return >0 if a is considered better). Note 540138568Ssam * that we assume compatibility/usability has already been checked 541138568Ssam * so we don't need to (e.g. validate whether privacy is supported). 542138568Ssam * Used to select the best scan candidate for association in a BSS. 543138568Ssam */ 544138568Ssamstatic int 545138568Ssamieee80211_node_compare(struct ieee80211com *ic, 546138568Ssam const struct ieee80211_node *a, 547138568Ssam const struct ieee80211_node *b) 548138568Ssam{ 549138568Ssam u_int8_t maxa, maxb; 550138568Ssam u_int8_t rssia, rssib; 551138568Ssam 552138568Ssam /* privacy support preferred */ 553138568Ssam if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) && 554138568Ssam (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 555138568Ssam return 1; 556138568Ssam if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 && 557138568Ssam (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)) 558138568Ssam return -1; 559138568Ssam 560138568Ssam rssia = ic->ic_node_getrssi(a); 561138568Ssam rssib = ic->ic_node_getrssi(b); 562139543Ssam if (abs(rssib - rssia) < 5) { 563139543Ssam /* best/max rate preferred if signal level close enough XXX */ 564139543Ssam maxa = maxrate(a); 565139543Ssam maxb = maxrate(b); 566139543Ssam if (maxa != maxb) 567139543Ssam return maxa - maxb; 568139543Ssam /* XXX use freq for channel preference */ 569139543Ssam /* for now just prefer 5Ghz band to all other bands */ 570139543Ssam if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) && 571139543Ssam !IEEE80211_IS_CHAN_5GHZ(b->ni_chan)) 572139543Ssam return 1; 573139543Ssam if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) && 574139543Ssam IEEE80211_IS_CHAN_5GHZ(b->ni_chan)) 575139543Ssam return -1; 576139543Ssam } 577138568Ssam /* all things being equal, use signal level */ 578138568Ssam return rssia - rssib; 579138568Ssam} 580138568Ssam 581138568Ssam/* 582140441Ssam * Mark an ongoing scan stopped. 583140441Ssam */ 584140441Ssamvoid 585140441Ssamieee80211_cancel_scan(struct ieee80211com *ic) 586140441Ssam{ 587140441Ssam 588140441Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n", 589140441Ssam __func__, 590140441Ssam (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive"); 591140441Ssam 592140441Ssam ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN); 593140441Ssam} 594140441Ssam 595140441Ssam/* 596116742Ssam * Complete a scan of potential channels. 597116742Ssam */ 598116742Ssamvoid 599138568Ssamieee80211_end_scan(struct ieee80211com *ic) 600116742Ssam{ 601140448Ssam struct ieee80211_node_table *nt = &ic->ic_scan; 602140448Ssam struct ieee80211_node *ni, *selbs; 603116742Ssam 604140441Ssam ieee80211_cancel_scan(ic); 605140441Ssam ieee80211_notify_scan_done(ic); 606116742Ssam 607116742Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 608138568Ssam u_int8_t maxrssi[IEEE80211_CHAN_MAX]; /* XXX off stack? */ 609138568Ssam int i, bestchan; 610138568Ssam u_int8_t rssi; 611138568Ssam 612116742Ssam /* 613116742Ssam * The passive scan to look for existing AP's completed, 614116742Ssam * select a channel to camp on. Identify the channels 615116742Ssam * that already have one or more AP's and try to locate 616140753Ssam * an unoccupied one. If that fails, pick a channel that 617138568Ssam * looks to be quietest. 618116742Ssam */ 619138568Ssam memset(maxrssi, 0, sizeof(maxrssi)); 620140448Ssam IEEE80211_NODE_LOCK(nt); 621140448Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 622138568Ssam rssi = ic->ic_node_getrssi(ni); 623138568Ssam i = ieee80211_chan2ieee(ic, ni->ni_chan); 624138568Ssam if (rssi > maxrssi[i]) 625138568Ssam maxrssi[i] = rssi; 626116742Ssam } 627140448Ssam IEEE80211_NODE_UNLOCK(nt); 628138568Ssam /* XXX select channel more intelligently */ 629138568Ssam bestchan = -1; 630116742Ssam for (i = 0; i < IEEE80211_CHAN_MAX; i++) 631138568Ssam if (isset(ic->ic_chan_active, i)) { 632138568Ssam /* 633138568Ssam * If the channel is unoccupied the max rssi 634138568Ssam * should be zero; just take it. Otherwise 635138568Ssam * track the channel with the lowest rssi and 636138568Ssam * use that when all channels appear occupied. 637138568Ssam */ 638138568Ssam if (maxrssi[i] == 0) { 639138568Ssam bestchan = i; 640116742Ssam break; 641138568Ssam } 642143715Ssam if (bestchan == -1 || 643143715Ssam maxrssi[i] < maxrssi[bestchan]) 644138568Ssam bestchan = i; 645138568Ssam } 646138568Ssam if (bestchan != -1) { 647138568Ssam ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]); 648138568Ssam return; 649116742Ssam } 650138568Ssam /* no suitable channel, should not happen */ 651138568Ssam } 652138568Ssam 653138568Ssam /* 654138568Ssam * When manually sequencing the state machine; scan just once 655138568Ssam * regardless of whether we have a candidate or not. The 656138568Ssam * controlling application is expected to setup state and 657138568Ssam * initiate an association. 658138568Ssam */ 659138568Ssam if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL) 660116742Ssam return; 661138568Ssam /* 662138568Ssam * Automatic sequencing; look for a candidate and 663138568Ssam * if found join the network. 664138568Ssam */ 665140448Ssam /* NB: unlocked read should be ok */ 666140448Ssam if (TAILQ_FIRST(&nt->nt_node) == NULL) { 667138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 668138568Ssam "%s: no scan candidate\n", __func__); 669116742Ssam notfound: 670116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS && 671116742Ssam (ic->ic_flags & IEEE80211_F_IBSSON) && 672116742Ssam ic->ic_des_esslen != 0) { 673116742Ssam ieee80211_create_ibss(ic, ic->ic_ibss_chan); 674116742Ssam return; 675116742Ssam } 676116742Ssam /* 677116742Ssam * Reset the list of channels to scan and start again. 678116742Ssam */ 679138568Ssam ieee80211_reset_scan(ic); 680138568Ssam ic->ic_flags |= IEEE80211_F_SCAN; 681138568Ssam ieee80211_next_scan(ic); 682116742Ssam return; 683116742Ssam } 684116742Ssam selbs = NULL; 685138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n", 686138568Ssam "macaddr bssid chan rssi rate flag wep essid"); 687140448Ssam IEEE80211_NODE_LOCK(nt); 688140448Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 689116742Ssam if (ni->ni_fails) { 690116742Ssam /* 691116742Ssam * The configuration of the access points may change 692116742Ssam * during my scan. So delete the entry for the AP 693116742Ssam * and retry to associate if there is another beacon. 694116742Ssam */ 695138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 696138568Ssam "%s: skip scan candidate %s, fails %u\n", 697138568Ssam __func__, ether_sprintf(ni->ni_macaddr), 698138568Ssam ni->ni_fails); 699140448Ssam ni->ni_fails++; 700140448Ssam#if 0 701116742Ssam if (ni->ni_fails++ > 2) 702138568Ssam ieee80211_free_node(ni); 703140448Ssam#endif 704116742Ssam continue; 705116742Ssam } 706138568Ssam if (ieee80211_match_bss(ic, ni) == 0) { 707116742Ssam if (selbs == NULL) 708116742Ssam selbs = ni; 709140448Ssam else if (ieee80211_node_compare(ic, ni, selbs) > 0) 710116742Ssam selbs = ni; 711116742Ssam } 712116742Ssam } 713140448Ssam if (selbs != NULL) /* NB: grab ref while dropping lock */ 714140448Ssam (void) ieee80211_ref_node(selbs); 715140448Ssam IEEE80211_NODE_UNLOCK(nt); 716116742Ssam if (selbs == NULL) 717116742Ssam goto notfound; 718138568Ssam if (!ieee80211_sta_join(ic, selbs)) { 719140448Ssam ieee80211_free_node(selbs); 720138568Ssam goto notfound; 721138568Ssam } 722138568Ssam} 723138568Ssam 724138568Ssam/* 725138568Ssam * Handle 802.11 ad hoc network merge. The 726138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance 727138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match 728138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that 729138568Ssam * has the station's desired SSID. The "oldest" 802.11 network 730138568Ssam * sends beacons with the greatest TSF timestamp. 731138568Ssam * 732138568Ssam * The caller is assumed to validate TSF's before attempting a merge. 733138568Ssam * 734138568Ssam * Return !0 if the BSSID changed, 0 otherwise. 735138568Ssam */ 736138568Ssamint 737138568Ssamieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni) 738138568Ssam{ 739138568Ssam 740140453Ssam if (ni == ic->ic_bss || 741140453Ssam IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) { 742138568Ssam /* unchanged, nothing to do */ 743138568Ssam return 0; 744138568Ssam } 745138568Ssam if (ieee80211_match_bss(ic, ni) != 0) { /* capabilities mismatch */ 746138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 747138568Ssam "%s: merge failed, capabilities mismatch\n", __func__); 748138568Ssam ic->ic_stats.is_ibss_capmismatch++; 749138568Ssam return 0; 750138568Ssam } 751138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 752138568Ssam "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__, 753138568Ssam ether_sprintf(ni->ni_bssid), 754138568Ssam ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 755138568Ssam ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long", 756138568Ssam ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "" 757138568Ssam ); 758140753Ssam return ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 759138568Ssam} 760138568Ssam 761138568Ssam/* 762138568Ssam * Join the specified IBSS/BSS network. The node is assumed to 763138568Ssam * be passed in with a held reference. 764138568Ssam */ 765138568Ssamint 766138568Ssamieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs) 767138568Ssam{ 768138568Ssam struct ieee80211_node *obss; 769138568Ssam 770116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 771140753Ssam struct ieee80211_node_table *nt; 772138568Ssam /* 773140440Ssam * Delete unusable rates; we've already checked 774140440Ssam * that the negotiated rate set is acceptable. 775138568Ssam */ 776148299Ssam ieee80211_fix_rate(selbs, IEEE80211_F_DODEL); 777127772Ssam /* 778140753Ssam * Fillin the neighbor table; it will already 779139521Ssam * exist if we are simply switching mastership. 780140753Ssam * XXX ic_sta always setup so this is unnecessary? 781127772Ssam */ 782140753Ssam nt = &ic->ic_sta; 783140753Ssam IEEE80211_NODE_LOCK(nt); 784140753Ssam nt->nt_name = "neighbor"; 785140753Ssam nt->nt_inact_init = ic->ic_inact_run; 786140753Ssam IEEE80211_NODE_UNLOCK(nt); 787138568Ssam } 788138568Ssam 789138568Ssam /* 790138568Ssam * Committed to selbs, setup state. 791138568Ssam */ 792138568Ssam obss = ic->ic_bss; 793140753Ssam ic->ic_bss = selbs; /* NB: caller assumed to bump refcnt */ 794138568Ssam if (obss != NULL) 795138568Ssam ieee80211_free_node(obss); 796138568Ssam /* 797138568Ssam * Set the erp state (mostly the slot time) to deal with 798138568Ssam * the auto-select case; this should be redundant if the 799138568Ssam * mode is locked. 800138568Ssam */ 801138568Ssam ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan); 802138568Ssam ieee80211_reset_erp(ic); 803138568Ssam ieee80211_wme_initparams(ic); 804140753Ssam 805140753Ssam if (ic->ic_opmode == IEEE80211_M_STA) 806140753Ssam ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 807140753Ssam else 808117811Ssam ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 809138568Ssam return 1; 810116742Ssam} 811116742Ssam 812138568Ssam/* 813138568Ssam * Leave the specified IBSS/BSS network. The node is assumed to 814138568Ssam * be passed in with a held reference. 815138568Ssam */ 816138568Ssamvoid 817138568Ssamieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 818138568Ssam{ 819138568Ssam ic->ic_node_cleanup(ni); 820138568Ssam ieee80211_notify_node_leave(ic, ni); 821138568Ssam} 822138568Ssam 823116742Ssamstatic struct ieee80211_node * 824138568Ssamnode_alloc(struct ieee80211_node_table *nt) 825116742Ssam{ 826127768Ssam struct ieee80211_node *ni; 827138568Ssam 828127768Ssam MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node), 829127768Ssam M_80211_NODE, M_NOWAIT | M_ZERO); 830127768Ssam return ni; 831116742Ssam} 832116742Ssam 833138568Ssam/* 834138568Ssam * Reclaim any resources in a node and reset any critical 835138568Ssam * state. Typically nodes are free'd immediately after, 836138568Ssam * but in some cases the storage may be reused so we need 837138568Ssam * to insure consistent state (should probably fix that). 838138568Ssam */ 839116742Ssamstatic void 840138568Ssamnode_cleanup(struct ieee80211_node *ni) 841116742Ssam{ 842138568Ssam#define N(a) (sizeof(a)/sizeof(a[0])) 843138568Ssam struct ieee80211com *ic = ni->ni_ic; 844138568Ssam int i, qlen; 845138568Ssam 846138568Ssam /* NB: preserve ni_table */ 847138568Ssam if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 848138568Ssam ic->ic_ps_sta--; 849138568Ssam ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 850138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 851138568Ssam "[%s] power save mode off, %u sta's in ps mode\n", 852138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta); 853138568Ssam } 854147788Ssam /* 855147788Ssam * Clear AREF flag that marks the authorization refcnt bump 856147788Ssam * has happened. This is probably not needed as the node 857147788Ssam * should always be removed from the table so not found but 858147788Ssam * do it just in case. 859147788Ssam */ 860147788Ssam ni->ni_flags &= ~IEEE80211_NODE_AREF; 861138568Ssam 862138568Ssam /* 863138568Ssam * Drain power save queue and, if needed, clear TIM. 864138568Ssam */ 865138568Ssam IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen); 866138568Ssam if (qlen != 0 && ic->ic_set_tim != NULL) 867138568Ssam ic->ic_set_tim(ic, ni, 0); 868138568Ssam 869138568Ssam ni->ni_associd = 0; 870138568Ssam if (ni->ni_challenge != NULL) { 871138568Ssam FREE(ni->ni_challenge, M_DEVBUF); 872138568Ssam ni->ni_challenge = NULL; 873138568Ssam } 874138568Ssam /* 875138568Ssam * Preserve SSID, WPA, and WME ie's so the bss node is 876138568Ssam * reusable during a re-auth/re-assoc state transition. 877138568Ssam * If we remove these data they will not be recreated 878138568Ssam * because they come from a probe-response or beacon frame 879138568Ssam * which cannot be expected prior to the association-response. 880138568Ssam * This should not be an issue when operating in other modes 881138568Ssam * as stations leaving always go through a full state transition 882138568Ssam * which will rebuild this state. 883138568Ssam * 884138568Ssam * XXX does this leave us open to inheriting old state? 885138568Ssam */ 886138568Ssam for (i = 0; i < N(ni->ni_rxfrag); i++) 887138568Ssam if (ni->ni_rxfrag[i] != NULL) { 888138568Ssam m_freem(ni->ni_rxfrag[i]); 889138568Ssam ni->ni_rxfrag[i] = NULL; 890138568Ssam } 891138568Ssam ieee80211_crypto_delkey(ic, &ni->ni_ucastkey); 892138568Ssam#undef N 893116742Ssam} 894116742Ssam 895116742Ssamstatic void 896138568Ssamnode_free(struct ieee80211_node *ni) 897116742Ssam{ 898138568Ssam struct ieee80211com *ic = ni->ni_ic; 899138568Ssam 900138568Ssam ic->ic_node_cleanup(ni); 901138568Ssam if (ni->ni_wpa_ie != NULL) 902138568Ssam FREE(ni->ni_wpa_ie, M_DEVBUF); 903138568Ssam if (ni->ni_wme_ie != NULL) 904138568Ssam FREE(ni->ni_wme_ie, M_DEVBUF); 905138568Ssam IEEE80211_NODE_SAVEQ_DESTROY(ni); 906138568Ssam FREE(ni, M_80211_NODE); 907116742Ssam} 908116742Ssam 909120104Ssamstatic u_int8_t 910138568Ssamnode_getrssi(const struct ieee80211_node *ni) 911120104Ssam{ 912120104Ssam return ni->ni_rssi; 913120104Ssam} 914120104Ssam 915116742Ssamstatic void 916138568Ssamieee80211_setup_node(struct ieee80211_node_table *nt, 917138568Ssam struct ieee80211_node *ni, const u_int8_t *macaddr) 918116742Ssam{ 919138568Ssam struct ieee80211com *ic = nt->nt_ic; 920116742Ssam int hash; 921116742Ssam 922138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 923140766Ssam "%s %p<%s> in %s table\n", __func__, ni, 924138568Ssam ether_sprintf(macaddr), nt->nt_name); 925138568Ssam 926116742Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 927116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 928138568Ssam ieee80211_node_initref(ni); /* mark referenced */ 929138568Ssam ni->ni_chan = IEEE80211_CHAN_ANYC; 930138568Ssam ni->ni_authmode = IEEE80211_AUTH_OPEN; 931138568Ssam ni->ni_txpower = ic->ic_txpowlimit; /* max power */ 932138568Ssam ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE); 933139528Ssam ni->ni_inact_reload = nt->nt_inact_init; 934139528Ssam ni->ni_inact = ni->ni_inact_reload; 935138568Ssam IEEE80211_NODE_SAVEQ_INIT(ni, "unknown"); 936138568Ssam 937138568Ssam IEEE80211_NODE_LOCK(nt); 938138568Ssam TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list); 939138568Ssam LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash); 940138568Ssam ni->ni_table = nt; 941138568Ssam ni->ni_ic = ic; 942138568Ssam IEEE80211_NODE_UNLOCK(nt); 943116742Ssam} 944116742Ssam 945116742Ssamstruct ieee80211_node * 946138568Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 947116742Ssam{ 948138568Ssam struct ieee80211com *ic = nt->nt_ic; 949138568Ssam struct ieee80211_node *ni; 950138568Ssam 951138568Ssam ni = ic->ic_node_alloc(nt); 952116742Ssam if (ni != NULL) 953138568Ssam ieee80211_setup_node(nt, ni, macaddr); 954127769Ssam else 955127769Ssam ic->ic_stats.is_rx_nodealloc++; 956116742Ssam return ni; 957116742Ssam} 958116742Ssam 959116742Ssamstruct ieee80211_node * 960138568Ssamieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 961116742Ssam{ 962138568Ssam struct ieee80211com *ic = nt->nt_ic; 963138568Ssam struct ieee80211_node *ni; 964138568Ssam 965138568Ssam ni = ic->ic_node_alloc(nt); 966116742Ssam if (ni != NULL) { 967138568Ssam ieee80211_setup_node(nt, ni, macaddr); 968127770Ssam /* 969127770Ssam * Inherit from ic_bss. 970127770Ssam */ 971138568Ssam ni->ni_authmode = ic->ic_bss->ni_authmode; 972138568Ssam ni->ni_txpower = ic->ic_bss->ni_txpower; 973138568Ssam ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */ 974127770Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 975138568Ssam ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan); 976138568Ssam ni->ni_rsn = ic->ic_bss->ni_rsn; 977127770Ssam } else 978127770Ssam ic->ic_stats.is_rx_nodealloc++; 979116742Ssam return ni; 980116742Ssam} 981116742Ssam 982127772Ssamstatic struct ieee80211_node * 983138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 984138568Ssam_ieee80211_find_node_debug(struct ieee80211_node_table *nt, 985138568Ssam const u_int8_t *macaddr, const char *func, int line) 986138568Ssam#else 987138568Ssam_ieee80211_find_node(struct ieee80211_node_table *nt, 988138568Ssam const u_int8_t *macaddr) 989138568Ssam#endif 990116742Ssam{ 991116742Ssam struct ieee80211_node *ni; 992116742Ssam int hash; 993116742Ssam 994138568Ssam IEEE80211_NODE_LOCK_ASSERT(nt); 995127772Ssam 996116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 997138568Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 998116742Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 999138568Ssam ieee80211_ref_node(ni); /* mark referenced */ 1000138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1001138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1002140766Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1003140766Ssam func, line, 1004140766Ssam ni, ether_sprintf(ni->ni_macaddr), 1005140766Ssam ieee80211_node_refcnt(ni)); 1006138568Ssam#endif 1007127772Ssam return ni; 1008116742Ssam } 1009116742Ssam } 1010127772Ssam return NULL; 1011127772Ssam} 1012138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1013138568Ssam#define _ieee80211_find_node(nt, mac) \ 1014138568Ssam _ieee80211_find_node_debug(nt, mac, func, line) 1015138568Ssam#endif 1016127772Ssam 1017127772Ssamstruct ieee80211_node * 1018138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1019138568Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt, 1020138568Ssam const u_int8_t *macaddr, const char *func, int line) 1021138568Ssam#else 1022138568Ssamieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 1023138568Ssam#endif 1024127772Ssam{ 1025127772Ssam struct ieee80211_node *ni; 1026127772Ssam 1027138568Ssam IEEE80211_NODE_LOCK(nt); 1028138568Ssam ni = _ieee80211_find_node(nt, macaddr); 1029138568Ssam IEEE80211_NODE_UNLOCK(nt); 1030116742Ssam return ni; 1031116742Ssam} 1032116742Ssam 1033116742Ssam/* 1034138568Ssam * Fake up a node; this handles node discovery in adhoc mode. 1035138568Ssam * Note that for the driver's benefit we we treat this like 1036138568Ssam * an association so the driver has an opportunity to setup 1037138568Ssam * it's private state. 1038138568Ssam */ 1039138568Ssamstruct ieee80211_node * 1040138568Ssamieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt, 1041138568Ssam const u_int8_t macaddr[IEEE80211_ADDR_LEN]) 1042138568Ssam{ 1043138568Ssam struct ieee80211com *ic = nt->nt_ic; 1044138568Ssam struct ieee80211_node *ni; 1045138568Ssam 1046138568Ssam ni = ieee80211_dup_bss(nt, macaddr); 1047138568Ssam if (ni != NULL) { 1048138568Ssam /* XXX no rate negotiation; just dup */ 1049138568Ssam ni->ni_rates = ic->ic_bss->ni_rates; 1050139524Ssam if (ic->ic_newassoc != NULL) 1051138568Ssam ic->ic_newassoc(ic, ni, 1); 1052138568Ssam /* XXX not right for 802.1x/WPA */ 1053148302Ssam ieee80211_node_authorize(ni); 1054138568Ssam } 1055138568Ssam return ni; 1056138568Ssam} 1057138568Ssam 1058138568Ssam/* 1059138568Ssam * Locate the node for sender, track state, and then pass the 1060138568Ssam * (referenced) node up to the 802.11 layer for its use. We 1061138568Ssam * are required to pass some node so we fall back to ic_bss 1062138568Ssam * when this frame is from an unknown sender. The 802.11 layer 1063138568Ssam * knows this means the sender wasn't in the node table and 1064138568Ssam * acts accordingly. 1065138568Ssam */ 1066138568Ssamstruct ieee80211_node * 1067138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1068138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic, 1069138568Ssam const struct ieee80211_frame_min *wh, const char *func, int line) 1070138568Ssam#else 1071138568Ssamieee80211_find_rxnode(struct ieee80211com *ic, 1072138568Ssam const struct ieee80211_frame_min *wh) 1073138568Ssam#endif 1074138568Ssam{ 1075138568Ssam#define IS_CTL(wh) \ 1076138568Ssam ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) 1077138568Ssam#define IS_PSPOLL(wh) \ 1078138568Ssam ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL) 1079138568Ssam struct ieee80211_node_table *nt; 1080138568Ssam struct ieee80211_node *ni; 1081138568Ssam 1082138568Ssam /* XXX may want scanned nodes in the neighbor table for adhoc */ 1083138568Ssam if (ic->ic_opmode == IEEE80211_M_STA || 1084138568Ssam ic->ic_opmode == IEEE80211_M_MONITOR || 1085138568Ssam (ic->ic_flags & IEEE80211_F_SCAN)) 1086138568Ssam nt = &ic->ic_scan; 1087138568Ssam else 1088140753Ssam nt = &ic->ic_sta; 1089138568Ssam /* XXX check ic_bss first in station mode */ 1090138568Ssam /* XXX 4-address frames? */ 1091138568Ssam IEEE80211_NODE_LOCK(nt); 1092138568Ssam if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/) 1093138568Ssam ni = _ieee80211_find_node(nt, wh->i_addr1); 1094138568Ssam else 1095138568Ssam ni = _ieee80211_find_node(nt, wh->i_addr2); 1096138568Ssam IEEE80211_NODE_UNLOCK(nt); 1097138568Ssam 1098138568Ssam return (ni != NULL ? ni : ieee80211_ref_node(ic->ic_bss)); 1099138568Ssam#undef IS_PSPOLL 1100138568Ssam#undef IS_CTL 1101138568Ssam} 1102138568Ssam 1103138568Ssam/* 1104127772Ssam * Return a reference to the appropriate node for sending 1105127772Ssam * a data frame. This handles node discovery in adhoc networks. 1106127772Ssam */ 1107127772Ssamstruct ieee80211_node * 1108138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1109138568Ssamieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr, 1110138568Ssam const char *func, int line) 1111138568Ssam#else 1112138568Ssamieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr) 1113138568Ssam#endif 1114127772Ssam{ 1115140753Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 1116127772Ssam struct ieee80211_node *ni; 1117127772Ssam 1118127772Ssam /* 1119127772Ssam * The destination address should be in the node table 1120127772Ssam * unless we are operating in station mode or this is a 1121127772Ssam * multicast/broadcast frame. 1122127772Ssam */ 1123140753Ssam if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr)) 1124138568Ssam return ieee80211_ref_node(ic->ic_bss); 1125127772Ssam 1126127772Ssam /* XXX can't hold lock across dup_bss 'cuz of recursive locking */ 1127138568Ssam IEEE80211_NODE_LOCK(nt); 1128138568Ssam ni = _ieee80211_find_node(nt, macaddr); 1129138568Ssam IEEE80211_NODE_UNLOCK(nt); 1130138568Ssam 1131138568Ssam if (ni == NULL) { 1132138568Ssam if (ic->ic_opmode == IEEE80211_M_IBSS || 1133140497Ssam ic->ic_opmode == IEEE80211_M_AHDEMO) { 1134140497Ssam /* 1135140497Ssam * In adhoc mode cons up a node for the destination. 1136140497Ssam * Note that we need an additional reference for the 1137140497Ssam * caller to be consistent with _ieee80211_find_node. 1138140497Ssam */ 1139138568Ssam ni = ieee80211_fakeup_adhoc_node(nt, macaddr); 1140140497Ssam if (ni != NULL) 1141140497Ssam (void) ieee80211_ref_node(ni); 1142140497Ssam } else { 1143138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 1144138568Ssam "[%s] no node, discard frame (%s)\n", 1145138568Ssam ether_sprintf(macaddr), __func__); 1146138568Ssam ic->ic_stats.is_tx_nonode++; 1147127772Ssam } 1148127772Ssam } 1149127772Ssam return ni; 1150127772Ssam} 1151127772Ssam 1152127772Ssam/* 1153116742Ssam * Like find but search based on the channel too. 1154116742Ssam */ 1155116742Ssamstruct ieee80211_node * 1156138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1157138568Ssamieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt, 1158138568Ssam const u_int8_t *macaddr, struct ieee80211_channel *chan, 1159138568Ssam const char *func, int line) 1160138568Ssam#else 1161138568Ssamieee80211_find_node_with_channel(struct ieee80211_node_table *nt, 1162138568Ssam const u_int8_t *macaddr, struct ieee80211_channel *chan) 1163138568Ssam#endif 1164116742Ssam{ 1165116742Ssam struct ieee80211_node *ni; 1166116742Ssam int hash; 1167116742Ssam 1168116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 1169138568Ssam IEEE80211_NODE_LOCK(nt); 1170138568Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1171127772Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1172127772Ssam ni->ni_chan == chan) { 1173138568Ssam ieee80211_ref_node(ni); /* mark referenced */ 1174138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1175138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1176140766Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1177140766Ssam func, line, 1178138568Ssam#else 1179140766Ssam "%s %p<%s> refcnt %d\n", __func__, 1180138568Ssam#endif 1181140766Ssam ni, ether_sprintf(ni->ni_macaddr), 1182140766Ssam ieee80211_node_refcnt(ni)); 1183116742Ssam break; 1184116742Ssam } 1185116742Ssam } 1186138568Ssam IEEE80211_NODE_UNLOCK(nt); 1187116742Ssam return ni; 1188116742Ssam} 1189116742Ssam 1190138568Ssam/* 1191138568Ssam * Like find but search based on the ssid too. 1192138568Ssam */ 1193138568Ssamstruct ieee80211_node * 1194138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1195138568Ssamieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt, 1196138568Ssam const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid, 1197138568Ssam const char *func, int line) 1198138568Ssam#else 1199138568Ssamieee80211_find_node_with_ssid(struct ieee80211_node_table *nt, 1200138568Ssam const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid) 1201138568Ssam#endif 1202138568Ssam{ 1203147118Ssam#define MATCH_SSID(ni, ssid, ssidlen) \ 1204147118Ssam (ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0) 1205147118Ssam static const u_int8_t zeromac[IEEE80211_ADDR_LEN]; 1206138568Ssam struct ieee80211com *ic = nt->nt_ic; 1207138568Ssam struct ieee80211_node *ni; 1208138568Ssam int hash; 1209138568Ssam 1210138568Ssam IEEE80211_NODE_LOCK(nt); 1211147118Ssam /* 1212147118Ssam * A mac address that is all zero means match only the ssid; 1213147118Ssam * otherwise we must match both. 1214147118Ssam */ 1215147118Ssam if (IEEE80211_ADDR_EQ(macaddr, zeromac)) { 1216147118Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1217147118Ssam if (MATCH_SSID(ni, ssid, ssidlen)) 1218147118Ssam break; 1219147118Ssam } 1220147118Ssam } else { 1221147118Ssam hash = IEEE80211_NODE_HASH(macaddr); 1222147118Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1223147118Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1224147118Ssam MATCH_SSID(ni, ssid, ssidlen)) 1225147118Ssam break; 1226147118Ssam } 1227147118Ssam } 1228147118Ssam if (ni != NULL) { 1229147118Ssam ieee80211_ref_node(ni); /* mark referenced */ 1230147118Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1231138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1232147118Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1233147118Ssam func, line, 1234138568Ssam#else 1235147118Ssam "%s %p<%s> refcnt %d\n", __func__, 1236138568Ssam#endif 1237147118Ssam ni, ether_sprintf(ni->ni_macaddr), 1238147118Ssam ieee80211_node_refcnt(ni)); 1239138568Ssam } 1240138568Ssam IEEE80211_NODE_UNLOCK(nt); 1241138568Ssam return ni; 1242147118Ssam#undef MATCH_SSID 1243138568Ssam} 1244138568Ssam 1245116742Ssamstatic void 1246138568Ssam_ieee80211_free_node(struct ieee80211_node *ni) 1247116742Ssam{ 1248138568Ssam struct ieee80211com *ic = ni->ni_ic; 1249138568Ssam struct ieee80211_node_table *nt = ni->ni_table; 1250119150Ssam 1251138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1252140766Ssam "%s %p<%s> in %s table\n", __func__, ni, 1253140766Ssam ether_sprintf(ni->ni_macaddr), 1254138568Ssam nt != NULL ? nt->nt_name : "<gone>"); 1255138568Ssam 1256138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1257138568Ssam if (nt != NULL) { 1258138568Ssam TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1259138568Ssam LIST_REMOVE(ni, ni_hash); 1260138568Ssam } 1261138568Ssam ic->ic_node_free(ni); 1262116742Ssam} 1263116742Ssam 1264116742Ssamvoid 1265138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1266138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line) 1267138568Ssam#else 1268138568Ssamieee80211_free_node(struct ieee80211_node *ni) 1269138568Ssam#endif 1270116742Ssam{ 1271138568Ssam struct ieee80211_node_table *nt = ni->ni_table; 1272119150Ssam 1273138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1274140454Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1275140766Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 1276138568Ssam ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1277138568Ssam#endif 1278138568Ssam if (ieee80211_node_dectestref(ni)) { 1279138568Ssam /* 1280138568Ssam * Beware; if the node is marked gone then it's already 1281138568Ssam * been removed from the table and we cannot assume the 1282138568Ssam * table still exists. Regardless, there's no need to lock 1283138568Ssam * the table. 1284138568Ssam */ 1285138568Ssam if (ni->ni_table != NULL) { 1286138568Ssam IEEE80211_NODE_LOCK(nt); 1287138568Ssam _ieee80211_free_node(ni); 1288138568Ssam IEEE80211_NODE_UNLOCK(nt); 1289138568Ssam } else 1290138568Ssam _ieee80211_free_node(ni); 1291116742Ssam } 1292116742Ssam} 1293116742Ssam 1294138568Ssam/* 1295138568Ssam * Reclaim a node. If this is the last reference count then 1296138568Ssam * do the normal free work. Otherwise remove it from the node 1297138568Ssam * table and mark it gone by clearing the back-reference. 1298138568Ssam */ 1299138568Ssamstatic void 1300138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1301116742Ssam{ 1302138568Ssam 1303140766Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1304140766Ssam "%s: remove %p<%s> from %s table, refcnt %d\n", 1305140766Ssam __func__, ni, ether_sprintf(ni->ni_macaddr), 1306140766Ssam nt->nt_name, ieee80211_node_refcnt(ni)-1); 1307138568Ssam if (!ieee80211_node_dectestref(ni)) { 1308138568Ssam /* 1309138568Ssam * Other references are present, just remove the 1310138568Ssam * node from the table so it cannot be found. When 1311138568Ssam * the references are dropped storage will be 1312140753Ssam * reclaimed. 1313138568Ssam */ 1314138568Ssam TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1315138568Ssam LIST_REMOVE(ni, ni_hash); 1316138568Ssam ni->ni_table = NULL; /* clear reference */ 1317138568Ssam } else 1318138568Ssam _ieee80211_free_node(ni); 1319138568Ssam} 1320138568Ssam 1321138568Ssamstatic void 1322138568Ssamieee80211_free_allnodes_locked(struct ieee80211_node_table *nt) 1323138568Ssam{ 1324138568Ssam struct ieee80211com *ic = nt->nt_ic; 1325116742Ssam struct ieee80211_node *ni; 1326116742Ssam 1327138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1328138568Ssam "%s: free all nodes in %s table\n", __func__, nt->nt_name); 1329138568Ssam 1330138568Ssam while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) { 1331138568Ssam if (ni->ni_associd != 0) { 1332138568Ssam if (ic->ic_auth->ia_node_leave != NULL) 1333138568Ssam ic->ic_auth->ia_node_leave(ic, ni); 1334138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1335138568Ssam } 1336138568Ssam node_reclaim(nt, ni); 1337138568Ssam } 1338138568Ssam ieee80211_reset_erp(ic); 1339116742Ssam} 1340116742Ssam 1341138568Ssamstatic void 1342138568Ssamieee80211_free_allnodes(struct ieee80211_node_table *nt) 1343138568Ssam{ 1344138568Ssam 1345138568Ssam IEEE80211_NODE_LOCK(nt); 1346138568Ssam ieee80211_free_allnodes_locked(nt); 1347138568Ssam IEEE80211_NODE_UNLOCK(nt); 1348138568Ssam} 1349138568Ssam 1350120483Ssam/* 1351138568Ssam * Timeout entries in the scan cache. 1352138568Ssam */ 1353138568Ssamstatic void 1354138568Ssamieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt) 1355138568Ssam{ 1356138568Ssam struct ieee80211com *ic = nt->nt_ic; 1357138568Ssam struct ieee80211_node *ni, *tni; 1358138568Ssam 1359138568Ssam IEEE80211_NODE_LOCK(nt); 1360138568Ssam ni = ic->ic_bss; 1361138568Ssam /* XXX belongs elsewhere */ 1362138568Ssam if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) { 1363138568Ssam m_freem(ni->ni_rxfrag[0]); 1364138568Ssam ni->ni_rxfrag[0] = NULL; 1365138568Ssam } 1366138568Ssam TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) { 1367138568Ssam if (ni->ni_inact && --ni->ni_inact == 0) { 1368138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1369138568Ssam "[%s] scan candidate purged from cache " 1370138568Ssam "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr), 1371140766Ssam ieee80211_node_refcnt(ni)); 1372138568Ssam node_reclaim(nt, ni); 1373138568Ssam } 1374138568Ssam } 1375138568Ssam IEEE80211_NODE_UNLOCK(nt); 1376138568Ssam 1377138568Ssam nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1378138568Ssam} 1379138568Ssam 1380138568Ssam/* 1381138568Ssam * Timeout inactive stations and do related housekeeping. 1382138568Ssam * Note that we cannot hold the node lock while sending a 1383138568Ssam * frame as this would lead to a LOR. Instead we use a 1384138568Ssam * generation number to mark nodes that we've scanned and 1385138568Ssam * drop the lock and restart a scan if we have to time out 1386138568Ssam * a node. Since we are single-threaded by virtue of 1387120483Ssam * controlling the inactivity timer we can be sure this will 1388120483Ssam * process each node only once. 1389120483Ssam */ 1390138568Ssamstatic void 1391138568Ssamieee80211_timeout_stations(struct ieee80211_node_table *nt) 1392116742Ssam{ 1393138568Ssam struct ieee80211com *ic = nt->nt_ic; 1394120483Ssam struct ieee80211_node *ni; 1395138568Ssam u_int gen; 1396116742Ssam 1397138568Ssam IEEE80211_SCAN_LOCK(nt); 1398138568Ssam gen = nt->nt_scangen++; 1399138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1400140766Ssam "%s: %s scangen %u\n", __func__, nt->nt_name, gen); 1401120483Ssamrestart: 1402138568Ssam IEEE80211_NODE_LOCK(nt); 1403138568Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1404120483Ssam if (ni->ni_scangen == gen) /* previously handled */ 1405120483Ssam continue; 1406120483Ssam ni->ni_scangen = gen; 1407138568Ssam /* 1408147788Ssam * Ignore entries for which have yet to receive an 1409147788Ssam * authentication frame. These are transient and 1410147788Ssam * will be reclaimed when the last reference to them 1411147788Ssam * goes away (when frame xmits complete). 1412147788Ssam */ 1413147788Ssam if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0) 1414147788Ssam continue; 1415147788Ssam /* 1416138568Ssam * Free fragment if not needed anymore 1417138568Ssam * (last fragment older than 1s). 1418138568Ssam * XXX doesn't belong here 1419138568Ssam */ 1420138568Ssam if (ni->ni_rxfrag[0] != NULL && 1421138568Ssam ticks > ni->ni_rxfragstamp + hz) { 1422138568Ssam m_freem(ni->ni_rxfrag[0]); 1423138568Ssam ni->ni_rxfrag[0] = NULL; 1424138568Ssam } 1425140498Ssam /* 1426140498Ssam * Special case ourself; we may be idle for extended periods 1427140498Ssam * of time and regardless reclaiming our state is wrong. 1428140498Ssam */ 1429140498Ssam if (ni == ic->ic_bss) 1430140498Ssam continue; 1431138568Ssam ni->ni_inact--; 1432138568Ssam if (ni->ni_associd != 0) { 1433119150Ssam /* 1434138568Ssam * Age frames on the power save queue. The 1435138568Ssam * aging interval is 4 times the listen 1436138568Ssam * interval specified by the station. This 1437138568Ssam * number is factored into the age calculations 1438138568Ssam * when the frame is placed on the queue. We 1439138568Ssam * store ages as time differences we can check 1440138568Ssam * and/or adjust only the head of the list. 1441138568Ssam */ 1442138568Ssam if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) { 1443138568Ssam struct mbuf *m; 1444138568Ssam int discard = 0; 1445138568Ssam 1446138568Ssam IEEE80211_NODE_SAVEQ_LOCK(ni); 1447138568Ssam while (IF_POLL(&ni->ni_savedq, m) != NULL && 1448138568Ssam M_AGE_GET(m) < IEEE80211_INACT_WAIT) { 1449138568SsamIEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/ 1450138568Ssam _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m); 1451138568Ssam m_freem(m); 1452138568Ssam discard++; 1453138568Ssam } 1454138568Ssam if (m != NULL) 1455138568Ssam M_AGE_SUB(m, IEEE80211_INACT_WAIT); 1456138568Ssam IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1457138568Ssam 1458138568Ssam if (discard != 0) { 1459138568Ssam IEEE80211_DPRINTF(ic, 1460138568Ssam IEEE80211_MSG_POWER, 1461138568Ssam "[%s] discard %u frames for age\n", 1462138568Ssam ether_sprintf(ni->ni_macaddr), 1463138568Ssam discard); 1464138568Ssam IEEE80211_NODE_STAT_ADD(ni, 1465138568Ssam ps_discard, discard); 1466138568Ssam if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) 1467138568Ssam ic->ic_set_tim(ic, ni, 0); 1468138568Ssam } 1469138568Ssam } 1470138568Ssam /* 1471138568Ssam * Probe the station before time it out. We 1472138568Ssam * send a null data frame which may not be 1473138568Ssam * universally supported by drivers (need it 1474138568Ssam * for ps-poll support so it should be...). 1475138568Ssam */ 1476139528Ssam if (0 < ni->ni_inact && 1477139528Ssam ni->ni_inact <= ic->ic_inact_probe) { 1478138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1479138568Ssam "[%s] probe station due to inactivity\n", 1480138568Ssam ether_sprintf(ni->ni_macaddr)); 1481138568Ssam IEEE80211_NODE_UNLOCK(nt); 1482148301Ssam ieee80211_send_nulldata(ni); 1483138568Ssam /* XXX stat? */ 1484138568Ssam goto restart; 1485138568Ssam } 1486138568Ssam } 1487138568Ssam if (ni->ni_inact <= 0) { 1488138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1489138568Ssam "[%s] station timed out due to inactivity " 1490138568Ssam "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr), 1491138568Ssam ieee80211_node_refcnt(ni)); 1492138568Ssam /* 1493138568Ssam * Send a deauthenticate frame and drop the station. 1494138568Ssam * This is somewhat complicated due to reference counts 1495138568Ssam * and locking. At this point a station will typically 1496138568Ssam * have a reference count of 1. ieee80211_node_leave 1497138568Ssam * will do a "free" of the node which will drop the 1498138568Ssam * reference count. But in the meantime a reference 1499138568Ssam * wil be held by the deauth frame. The actual reclaim 1500138568Ssam * of the node will happen either after the tx is 1501138568Ssam * completed or by ieee80211_node_leave. 1502120483Ssam * 1503138568Ssam * Separately we must drop the node lock before sending 1504138568Ssam * in case the driver takes a lock, as this will result 1505138568Ssam * in LOR between the node lock and the driver lock. 1506119150Ssam */ 1507138568Ssam IEEE80211_NODE_UNLOCK(nt); 1508138568Ssam if (ni->ni_associd != 0) { 1509138568Ssam IEEE80211_SEND_MGMT(ic, ni, 1510138568Ssam IEEE80211_FC0_SUBTYPE_DEAUTH, 1511138568Ssam IEEE80211_REASON_AUTH_EXPIRE); 1512138568Ssam } 1513138568Ssam ieee80211_node_leave(ic, ni); 1514121180Ssam ic->ic_stats.is_node_timeout++; 1515120483Ssam goto restart; 1516120483Ssam } 1517116742Ssam } 1518138568Ssam IEEE80211_NODE_UNLOCK(nt); 1519138568Ssam 1520138568Ssam IEEE80211_SCAN_UNLOCK(nt); 1521138568Ssam 1522138568Ssam nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1523116742Ssam} 1524116742Ssam 1525116742Ssamvoid 1526138568Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg) 1527116742Ssam{ 1528116742Ssam struct ieee80211_node *ni; 1529138568Ssam u_int gen; 1530116742Ssam 1531138568Ssam IEEE80211_SCAN_LOCK(nt); 1532138568Ssam gen = nt->nt_scangen++; 1533138568Ssamrestart: 1534138568Ssam IEEE80211_NODE_LOCK(nt); 1535138568Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1536138568Ssam if (ni->ni_scangen != gen) { 1537138568Ssam ni->ni_scangen = gen; 1538138568Ssam (void) ieee80211_ref_node(ni); 1539138568Ssam IEEE80211_NODE_UNLOCK(nt); 1540138568Ssam (*f)(arg, ni); 1541138568Ssam ieee80211_free_node(ni); 1542138568Ssam goto restart; 1543138568Ssam } 1544138568Ssam } 1545138568Ssam IEEE80211_NODE_UNLOCK(nt); 1546138568Ssam 1547138568Ssam IEEE80211_SCAN_UNLOCK(nt); 1548116742Ssam} 1549138568Ssam 1550138568Ssamvoid 1551138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1552138568Ssam{ 1553138568Ssam printf("0x%p: mac %s refcnt %d\n", ni, 1554138568Ssam ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 1555138568Ssam printf("\tscangen %u authmode %u flags 0x%x\n", 1556138568Ssam ni->ni_scangen, ni->ni_authmode, ni->ni_flags); 1557138568Ssam printf("\tassocid 0x%x txpower %u vlan %u\n", 1558138568Ssam ni->ni_associd, ni->ni_txpower, ni->ni_vlan); 1559138568Ssam printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n", 1560138568Ssam ni->ni_txseqs[0], 1561138568Ssam ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT, 1562138568Ssam ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK, 1563138568Ssam ni->ni_rxfragstamp); 1564138568Ssam printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n", 1565138568Ssam ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo); 1566138568Ssam printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n", 1567138568Ssam ether_sprintf(ni->ni_bssid), 1568138568Ssam ni->ni_esslen, ni->ni_essid, 1569138568Ssam ni->ni_chan->ic_freq, ni->ni_chan->ic_flags); 1570138568Ssam printf("\tfails %u inact %u txrate %u\n", 1571138568Ssam ni->ni_fails, ni->ni_inact, ni->ni_txrate); 1572138568Ssam} 1573138568Ssam 1574138568Ssamvoid 1575138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt) 1576138568Ssam{ 1577138568Ssam ieee80211_iterate_nodes(nt, 1578138568Ssam (ieee80211_iter_func *) ieee80211_dump_node, nt); 1579138568Ssam} 1580138568Ssam 1581138568Ssam/* 1582138568Ssam * Handle a station joining an 11g network. 1583138568Ssam */ 1584138568Ssamstatic void 1585138568Ssamieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 1586138568Ssam{ 1587138568Ssam 1588138568Ssam /* 1589138568Ssam * Station isn't capable of short slot time. Bump 1590138568Ssam * the count of long slot time stations and disable 1591138568Ssam * use of short slot time. Note that the actual switch 1592138568Ssam * over to long slot time use may not occur until the 1593138568Ssam * next beacon transmission (per sec. 7.3.1.4 of 11g). 1594138568Ssam */ 1595138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 1596138568Ssam ic->ic_longslotsta++; 1597138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1598138568Ssam "[%s] station needs long slot time, count %d\n", 1599138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 1600138568Ssam /* XXX vap's w/ conflicting needs won't work */ 1601138568Ssam ieee80211_set_shortslottime(ic, 0); 1602138568Ssam } 1603138568Ssam /* 1604138568Ssam * If the new station is not an ERP station 1605138568Ssam * then bump the counter and enable protection 1606138568Ssam * if configured. 1607138568Ssam */ 1608138568Ssam if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) { 1609138568Ssam ic->ic_nonerpsta++; 1610138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1611138568Ssam "[%s] station is !ERP, %d non-ERP stations associated\n", 1612138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 1613138568Ssam /* 1614138568Ssam * If protection is configured, enable it. 1615138568Ssam */ 1616138568Ssam if (ic->ic_protmode != IEEE80211_PROT_NONE) { 1617138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1618138568Ssam "%s: enable use of protection\n", __func__); 1619138568Ssam ic->ic_flags |= IEEE80211_F_USEPROT; 1620138568Ssam } 1621138568Ssam /* 1622138568Ssam * If station does not support short preamble 1623138568Ssam * then we must enable use of Barker preamble. 1624138568Ssam */ 1625138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) { 1626138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1627138568Ssam "[%s] station needs long preamble\n", 1628138568Ssam ether_sprintf(ni->ni_macaddr)); 1629138568Ssam ic->ic_flags |= IEEE80211_F_USEBARKER; 1630138568Ssam ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 1631138568Ssam } 1632138568Ssam } else 1633138568Ssam ni->ni_flags |= IEEE80211_NODE_ERP; 1634138568Ssam} 1635138568Ssam 1636138568Ssamvoid 1637138568Ssamieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp) 1638138568Ssam{ 1639138568Ssam int newassoc; 1640138568Ssam 1641138568Ssam if (ni->ni_associd == 0) { 1642138568Ssam u_int16_t aid; 1643138568Ssam 1644138568Ssam /* 1645138568Ssam * It would be good to search the bitmap 1646138568Ssam * more efficiently, but this will do for now. 1647138568Ssam */ 1648138568Ssam for (aid = 1; aid < ic->ic_max_aid; aid++) { 1649138568Ssam if (!IEEE80211_AID_ISSET(aid, 1650138568Ssam ic->ic_aid_bitmap)) 1651138568Ssam break; 1652138568Ssam } 1653138568Ssam if (aid >= ic->ic_max_aid) { 1654138568Ssam IEEE80211_SEND_MGMT(ic, ni, resp, 1655138568Ssam IEEE80211_REASON_ASSOC_TOOMANY); 1656138568Ssam ieee80211_node_leave(ic, ni); 1657138568Ssam return; 1658138568Ssam } 1659138568Ssam ni->ni_associd = aid | 0xc000; 1660138568Ssam IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap); 1661138568Ssam ic->ic_sta_assoc++; 1662138568Ssam newassoc = 1; 1663139522Ssam if (ic->ic_curmode == IEEE80211_MODE_11G || 1664139522Ssam ic->ic_curmode == IEEE80211_MODE_TURBO_G) 1665138568Ssam ieee80211_node_join_11g(ic, ni); 1666138568Ssam } else 1667138568Ssam newassoc = 0; 1668138568Ssam 1669138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 1670139523Ssam "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n", 1671139523Ssam ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re", 1672139523Ssam IEEE80211_NODE_AID(ni), 1673139523Ssam ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long", 1674139523Ssam ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", 1675139523Ssam ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "", 1676139523Ssam ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "" 1677139523Ssam ); 1678138568Ssam 1679138568Ssam /* give driver a chance to setup state like ni_txrate */ 1680139524Ssam if (ic->ic_newassoc != NULL) 1681138568Ssam ic->ic_newassoc(ic, ni, newassoc); 1682139528Ssam ni->ni_inact_reload = ic->ic_inact_auth; 1683139528Ssam ni->ni_inact = ni->ni_inact_reload; 1684138568Ssam IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS); 1685138568Ssam /* tell the authenticator about new station */ 1686138568Ssam if (ic->ic_auth->ia_node_join != NULL) 1687138568Ssam ic->ic_auth->ia_node_join(ic, ni); 1688138568Ssam ieee80211_notify_node_join(ic, ni, newassoc); 1689138568Ssam} 1690138568Ssam 1691138568Ssam/* 1692138568Ssam * Handle a station leaving an 11g network. 1693138568Ssam */ 1694138568Ssamstatic void 1695138568Ssamieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 1696138568Ssam{ 1697138568Ssam 1698139522Ssam KASSERT(ic->ic_curmode == IEEE80211_MODE_11G || 1699139522Ssam ic->ic_curmode == IEEE80211_MODE_TURBO_G, 1700138568Ssam ("not in 11g, curmode %x", ic->ic_curmode)); 1701138568Ssam 1702138568Ssam /* 1703138568Ssam * If a long slot station do the slot time bookkeeping. 1704138568Ssam */ 1705138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 1706138568Ssam KASSERT(ic->ic_longslotsta > 0, 1707138568Ssam ("bogus long slot station count %d", ic->ic_longslotsta)); 1708138568Ssam ic->ic_longslotsta--; 1709138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1710138568Ssam "[%s] long slot time station leaves, count now %d\n", 1711138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 1712138568Ssam if (ic->ic_longslotsta == 0) { 1713138568Ssam /* 1714138568Ssam * Re-enable use of short slot time if supported 1715138568Ssam * and not operating in IBSS mode (per spec). 1716138568Ssam */ 1717138568Ssam if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 1718138568Ssam ic->ic_opmode != IEEE80211_M_IBSS) { 1719138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1720138568Ssam "%s: re-enable use of short slot time\n", 1721138568Ssam __func__); 1722138568Ssam ieee80211_set_shortslottime(ic, 1); 1723138568Ssam } 1724138568Ssam } 1725138568Ssam } 1726138568Ssam /* 1727138568Ssam * If a non-ERP station do the protection-related bookkeeping. 1728138568Ssam */ 1729138568Ssam if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) { 1730138568Ssam KASSERT(ic->ic_nonerpsta > 0, 1731138568Ssam ("bogus non-ERP station count %d", ic->ic_nonerpsta)); 1732138568Ssam ic->ic_nonerpsta--; 1733138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1734138568Ssam "[%s] non-ERP station leaves, count now %d\n", 1735138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 1736138568Ssam if (ic->ic_nonerpsta == 0) { 1737138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1738138568Ssam "%s: disable use of protection\n", __func__); 1739138568Ssam ic->ic_flags &= ~IEEE80211_F_USEPROT; 1740138568Ssam /* XXX verify mode? */ 1741138568Ssam if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) { 1742138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1743138568Ssam "%s: re-enable use of short preamble\n", 1744138568Ssam __func__); 1745138568Ssam ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 1746138568Ssam ic->ic_flags &= ~IEEE80211_F_USEBARKER; 1747138568Ssam } 1748138568Ssam } 1749138568Ssam } 1750138568Ssam} 1751138568Ssam 1752138568Ssam/* 1753138568Ssam * Handle bookkeeping for station deauthentication/disassociation 1754138568Ssam * when operating as an ap. 1755138568Ssam */ 1756138568Ssamvoid 1757138568Ssamieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 1758138568Ssam{ 1759140499Ssam struct ieee80211_node_table *nt = ni->ni_table; 1760138568Ssam 1761138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 1762138568Ssam "[%s] station with aid %d leaves\n", 1763138568Ssam ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni)); 1764138568Ssam 1765138568Ssam KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 1766138568Ssam ic->ic_opmode == IEEE80211_M_IBSS || 1767138568Ssam ic->ic_opmode == IEEE80211_M_AHDEMO, 1768138568Ssam ("unexpected operating mode %u", ic->ic_opmode)); 1769138568Ssam /* 1770138568Ssam * If node wasn't previously associated all 1771138568Ssam * we need to do is reclaim the reference. 1772138568Ssam */ 1773138568Ssam /* XXX ibss mode bypasses 11g and notification */ 1774138568Ssam if (ni->ni_associd == 0) 1775138568Ssam goto done; 1776138568Ssam /* 1777138568Ssam * Tell the authenticator the station is leaving. 1778138568Ssam * Note that we must do this before yanking the 1779138568Ssam * association id as the authenticator uses the 1780138568Ssam * associd to locate it's state block. 1781138568Ssam */ 1782138568Ssam if (ic->ic_auth->ia_node_leave != NULL) 1783138568Ssam ic->ic_auth->ia_node_leave(ic, ni); 1784138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1785138568Ssam ni->ni_associd = 0; 1786138568Ssam ic->ic_sta_assoc--; 1787138568Ssam 1788139522Ssam if (ic->ic_curmode == IEEE80211_MODE_11G || 1789139522Ssam ic->ic_curmode == IEEE80211_MODE_TURBO_G) 1790138568Ssam ieee80211_node_leave_11g(ic, ni); 1791138568Ssam /* 1792138568Ssam * Cleanup station state. In particular clear various 1793138568Ssam * state that might otherwise be reused if the node 1794138568Ssam * is reused before the reference count goes to zero 1795138568Ssam * (and memory is reclaimed). 1796138568Ssam */ 1797138568Ssam ieee80211_sta_leave(ic, ni); 1798138568Ssamdone: 1799140499Ssam /* 1800140499Ssam * Remove the node from any table it's recorded in and 1801140499Ssam * drop the caller's reference. Removal from the table 1802140499Ssam * is important to insure the node is not reprocessed 1803140499Ssam * for inactivity. 1804140499Ssam */ 1805140499Ssam if (nt != NULL) { 1806140499Ssam IEEE80211_NODE_LOCK(nt); 1807140499Ssam node_reclaim(nt, ni); 1808140499Ssam IEEE80211_NODE_UNLOCK(nt); 1809140499Ssam } else 1810140499Ssam ieee80211_free_node(ni); 1811138568Ssam} 1812138568Ssam 1813138568Ssamu_int8_t 1814138568Ssamieee80211_getrssi(struct ieee80211com *ic) 1815138568Ssam{ 1816138568Ssam#define NZ(x) ((x) == 0 ? 1 : (x)) 1817140753Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 1818138568Ssam u_int32_t rssi_samples, rssi_total; 1819138568Ssam struct ieee80211_node *ni; 1820138568Ssam 1821138568Ssam rssi_total = 0; 1822138568Ssam rssi_samples = 0; 1823138568Ssam switch (ic->ic_opmode) { 1824138568Ssam case IEEE80211_M_IBSS: /* average of all ibss neighbors */ 1825138568Ssam /* XXX locking */ 1826140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 1827138568Ssam if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) { 1828138568Ssam rssi_samples++; 1829138568Ssam rssi_total += ic->ic_node_getrssi(ni); 1830138568Ssam } 1831138568Ssam break; 1832138568Ssam case IEEE80211_M_AHDEMO: /* average of all neighbors */ 1833138568Ssam /* XXX locking */ 1834140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1835138568Ssam rssi_samples++; 1836138568Ssam rssi_total += ic->ic_node_getrssi(ni); 1837138568Ssam } 1838138568Ssam break; 1839138568Ssam case IEEE80211_M_HOSTAP: /* average of all associated stations */ 1840138568Ssam /* XXX locking */ 1841140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 1842138568Ssam if (IEEE80211_AID(ni->ni_associd) != 0) { 1843138568Ssam rssi_samples++; 1844138568Ssam rssi_total += ic->ic_node_getrssi(ni); 1845138568Ssam } 1846138568Ssam break; 1847138568Ssam case IEEE80211_M_MONITOR: /* XXX */ 1848138568Ssam case IEEE80211_M_STA: /* use stats from associated ap */ 1849138568Ssam default: 1850138568Ssam if (ic->ic_bss != NULL) 1851138568Ssam rssi_total = ic->ic_node_getrssi(ic->ic_bss); 1852138568Ssam rssi_samples = 1; 1853138568Ssam break; 1854138568Ssam } 1855138568Ssam return rssi_total / NZ(rssi_samples); 1856138568Ssam#undef NZ 1857138568Ssam} 1858138568Ssam 1859138568Ssam/* 1860138568Ssam * Indicate whether there are frames queued for a station in power-save mode. 1861138568Ssam */ 1862138568Ssamstatic void 1863138568Ssamieee80211_set_tim(struct ieee80211com *ic, struct ieee80211_node *ni, int set) 1864138568Ssam{ 1865138568Ssam u_int16_t aid; 1866138568Ssam 1867138568Ssam KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 1868138568Ssam ic->ic_opmode == IEEE80211_M_IBSS, 1869138568Ssam ("operating mode %u", ic->ic_opmode)); 1870138568Ssam 1871138568Ssam aid = IEEE80211_AID(ni->ni_associd); 1872138568Ssam KASSERT(aid < ic->ic_max_aid, 1873138568Ssam ("bogus aid %u, max %u", aid, ic->ic_max_aid)); 1874138568Ssam 1875138568Ssam IEEE80211_BEACON_LOCK(ic); 1876138568Ssam if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) { 1877138568Ssam if (set) { 1878138568Ssam setbit(ic->ic_tim_bitmap, aid); 1879138568Ssam ic->ic_ps_pending++; 1880138568Ssam } else { 1881138568Ssam clrbit(ic->ic_tim_bitmap, aid); 1882138568Ssam ic->ic_ps_pending--; 1883138568Ssam } 1884138568Ssam ic->ic_flags |= IEEE80211_F_TIMUPDATE; 1885138568Ssam } 1886138568Ssam IEEE80211_BEACON_UNLOCK(ic); 1887138568Ssam} 1888138568Ssam 1889138568Ssam/* 1890138568Ssam * Node table support. 1891138568Ssam */ 1892138568Ssam 1893138568Ssamstatic void 1894138568Ssamieee80211_node_table_init(struct ieee80211com *ic, 1895138568Ssam struct ieee80211_node_table *nt, 1896138568Ssam const char *name, int inact, 1897138568Ssam void (*timeout)(struct ieee80211_node_table *)) 1898138568Ssam{ 1899138568Ssam 1900138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1901138568Ssam "%s %s table, inact %u\n", __func__, name, inact); 1902138568Ssam 1903138568Ssam nt->nt_ic = ic; 1904138568Ssam /* XXX need unit */ 1905138568Ssam IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname); 1906138568Ssam IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname); 1907138568Ssam TAILQ_INIT(&nt->nt_node); 1908138568Ssam nt->nt_name = name; 1909138568Ssam nt->nt_scangen = 1; 1910138568Ssam nt->nt_inact_init = inact; 1911138568Ssam nt->nt_timeout = timeout; 1912138568Ssam} 1913138568Ssam 1914138568Ssamvoid 1915138568Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt) 1916138568Ssam{ 1917138568Ssam 1918138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1919138568Ssam "%s %s table\n", __func__, nt->nt_name); 1920138568Ssam 1921138568Ssam IEEE80211_NODE_LOCK(nt); 1922138568Ssam nt->nt_inact_timer = 0; 1923138568Ssam ieee80211_free_allnodes_locked(nt); 1924138568Ssam IEEE80211_NODE_UNLOCK(nt); 1925138568Ssam} 1926138568Ssam 1927138568Ssamstatic void 1928138568Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt) 1929138568Ssam{ 1930138568Ssam 1931138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1932138568Ssam "%s %s table\n", __func__, nt->nt_name); 1933138568Ssam 1934138568Ssam ieee80211_free_allnodes_locked(nt); 1935138568Ssam IEEE80211_SCAN_LOCK_DESTROY(nt); 1936138568Ssam IEEE80211_NODE_LOCK_DESTROY(nt); 1937138568Ssam} 1938