ieee80211_node.c revision 154532
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 154532 2006-01-18 19:56:17Z 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 75148304Ssamstatic void ieee80211_set_tim(struct ieee80211_node *, int set); 76138568Ssam 77138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic, 78148863Ssam struct ieee80211_node_table *nt, const char *name, 79148863Ssam int inact, int keyixmax, 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 89138568Ssam ic->ic_node_alloc = node_alloc; 90138568Ssam ic->ic_node_free = node_free; 91138568Ssam ic->ic_node_cleanup = node_cleanup; 92138568Ssam ic->ic_node_getrssi = node_getrssi; 93138568Ssam 94138568Ssam /* default station inactivity timer setings */ 95138568Ssam ic->ic_inact_init = IEEE80211_INACT_INIT; 96138568Ssam ic->ic_inact_auth = IEEE80211_INACT_AUTH; 97138568Ssam ic->ic_inact_run = IEEE80211_INACT_RUN; 98138568Ssam ic->ic_inact_probe = IEEE80211_INACT_PROBE; 99138568Ssam 100148863Ssam /* NB: driver should override */ 101148863Ssam ic->ic_max_aid = IEEE80211_AID_DEF; 102148863Ssam ic->ic_set_tim = ieee80211_set_tim; 103148863Ssam} 104148863Ssam 105148863Ssamvoid 106148863Ssamieee80211_node_lateattach(struct ieee80211com *ic) 107148863Ssam{ 108148863Ssam struct ieee80211_rsnparms *rsn; 109148863Ssam 110148863Ssam if (ic->ic_max_aid > IEEE80211_AID_MAX) 111138568Ssam ic->ic_max_aid = IEEE80211_AID_MAX; 112138568Ssam MALLOC(ic->ic_aid_bitmap, u_int32_t *, 113138568Ssam howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t), 114138568Ssam M_DEVBUF, M_NOWAIT | M_ZERO); 115138568Ssam if (ic->ic_aid_bitmap == NULL) { 116138568Ssam /* XXX no way to recover */ 117138568Ssam printf("%s: no memory for AID bitmap!\n", __func__); 118138568Ssam ic->ic_max_aid = 0; 119138568Ssam } 120138568Ssam 121138568Ssam /* XXX defer until using hostap/ibss mode */ 122138568Ssam ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t); 123138568Ssam MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len, 124138568Ssam M_DEVBUF, M_NOWAIT | M_ZERO); 125138568Ssam if (ic->ic_tim_bitmap == NULL) { 126138568Ssam /* XXX no way to recover */ 127138568Ssam printf("%s: no memory for TIM bitmap!\n", __func__); 128138568Ssam } 129118887Ssam 130148863Ssam ieee80211_node_table_init(ic, &ic->ic_sta, "station", 131148863Ssam IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix, 132148863Ssam ieee80211_timeout_stations); 133148863Ssam ieee80211_node_table_init(ic, &ic->ic_scan, "scan", 134148863Ssam IEEE80211_INACT_SCAN, 0, 135148863Ssam ieee80211_timeout_scan_candidates); 136118887Ssam 137148863Ssam ieee80211_reset_bss(ic); 138138568Ssam /* 139138568Ssam * Setup "global settings" in the bss node so that 140138568Ssam * each new station automatically inherits them. 141138568Ssam */ 142148863Ssam rsn = &ic->ic_bss->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 174148863Ssam ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode); 175116742Ssam} 176116742Ssam 177116742Ssamvoid 178138568Ssamieee80211_node_detach(struct ieee80211com *ic) 179116742Ssam{ 180116742Ssam 181138568Ssam if (ic->ic_bss != NULL) { 182138568Ssam ieee80211_free_node(ic->ic_bss); 183138568Ssam ic->ic_bss = NULL; 184138568Ssam } 185138568Ssam ieee80211_node_table_cleanup(&ic->ic_scan); 186140753Ssam ieee80211_node_table_cleanup(&ic->ic_sta); 187138568Ssam if (ic->ic_aid_bitmap != NULL) { 188138568Ssam FREE(ic->ic_aid_bitmap, M_DEVBUF); 189138568Ssam ic->ic_aid_bitmap = NULL; 190138568Ssam } 191138568Ssam if (ic->ic_tim_bitmap != NULL) { 192138568Ssam FREE(ic->ic_tim_bitmap, M_DEVBUF); 193138568Ssam ic->ic_tim_bitmap = NULL; 194138568Ssam } 195116742Ssam} 196116742Ssam 197138568Ssam/* 198138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator. 199138568Ssam */ 200138568Ssam 201138568Ssamvoid 202148302Ssamieee80211_node_authorize(struct ieee80211_node *ni) 203138568Ssam{ 204148302Ssam struct ieee80211com *ic = ni->ni_ic; 205148302Ssam 206138568Ssam ni->ni_flags |= IEEE80211_NODE_AUTH; 207139528Ssam ni->ni_inact_reload = ic->ic_inact_run; 208138568Ssam} 209138568Ssam 210138568Ssamvoid 211148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni) 212138568Ssam{ 213138568Ssam ni->ni_flags &= ~IEEE80211_NODE_AUTH; 214138568Ssam} 215138568Ssam 216116742Ssam/* 217138568Ssam * Set/change the channel. The rate set is also updated as 218138568Ssam * to insure a consistent view by drivers. 219138568Ssam */ 220153351Ssamstatic void 221138568Ssamieee80211_set_chan(struct ieee80211com *ic, 222138568Ssam struct ieee80211_node *ni, struct ieee80211_channel *chan) 223138568Ssam{ 224153351Ssam if (chan == IEEE80211_CHAN_ANYC) /* XXX while scanning */ 225153351Ssam chan = ic->ic_curchan; 226138568Ssam ni->ni_chan = chan; 227138568Ssam ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)]; 228138568Ssam} 229138568Ssam 230138568Ssam/* 231116742Ssam * AP scanning support. 232116742Ssam */ 233116742Ssam 234138568Ssam#ifdef IEEE80211_DEBUG 235138568Ssamstatic void 236138568Ssamdump_chanlist(const u_char chans[]) 237138568Ssam{ 238138568Ssam const char *sep; 239138568Ssam int i; 240138568Ssam 241138568Ssam sep = " "; 242138568Ssam for (i = 0; i < IEEE80211_CHAN_MAX; i++) 243138568Ssam if (isset(chans, i)) { 244138568Ssam printf("%s%u", sep, i); 245138568Ssam sep = ", "; 246138568Ssam } 247138568Ssam} 248138568Ssam#endif /* IEEE80211_DEBUG */ 249138568Ssam 250116742Ssam/* 251138568Ssam * Initialize the channel set to scan based on the 252116742Ssam * of available channels and the current PHY mode. 253116742Ssam */ 254117811Ssamstatic void 255138568Ssamieee80211_reset_scan(struct ieee80211com *ic) 256116742Ssam{ 257116742Ssam 258138568Ssam /* XXX ic_des_chan should be handled with ic_chan_active */ 259138568Ssam if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) { 260138568Ssam memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan)); 261138568Ssam setbit(ic->ic_chan_scan, 262138568Ssam ieee80211_chan2ieee(ic, ic->ic_des_chan)); 263138568Ssam } else 264138568Ssam memcpy(ic->ic_chan_scan, ic->ic_chan_active, 265138568Ssam sizeof(ic->ic_chan_active)); 266138568Ssam#ifdef IEEE80211_DEBUG 267138568Ssam if (ieee80211_msg_scan(ic)) { 268138568Ssam printf("%s: scan set:", __func__); 269138568Ssam dump_chanlist(ic->ic_chan_scan); 270138568Ssam printf(" start chan %u\n", 271148936Ssam ieee80211_chan2ieee(ic, ic->ic_curchan)); 272138568Ssam } 273138568Ssam#endif /* IEEE80211_DEBUG */ 274116742Ssam} 275116742Ssam 276116742Ssam/* 277116742Ssam * Begin an active scan. 278116742Ssam */ 279116742Ssamvoid 280138568Ssamieee80211_begin_scan(struct ieee80211com *ic, int reset) 281116742Ssam{ 282116742Ssam 283138568Ssam ic->ic_scan.nt_scangen++; 284117811Ssam /* 285117811Ssam * In all but hostap mode scanning starts off in 286117811Ssam * an active mode before switching to passive. 287117811Ssam */ 288121180Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP) { 289117811Ssam ic->ic_flags |= IEEE80211_F_ASCAN; 290121180Ssam ic->ic_stats.is_scan_active++; 291121180Ssam } else 292121180Ssam ic->ic_stats.is_scan_passive++; 293139520Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 294139520Ssam "begin %s scan in %s mode, scangen %u\n", 295138568Ssam (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive", 296139520Ssam ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen); 297116742Ssam /* 298138568Ssam * Clear scan state and flush any previously seen AP's. 299116742Ssam */ 300138568Ssam ieee80211_reset_scan(ic); 301138568Ssam if (reset) 302138568Ssam ieee80211_free_allnodes(&ic->ic_scan); 303116742Ssam 304138568Ssam ic->ic_flags |= IEEE80211_F_SCAN; 305138568Ssam 306117811Ssam /* Scan the next channel. */ 307138568Ssam ieee80211_next_scan(ic); 308116742Ssam} 309116742Ssam 310116742Ssam/* 311116742Ssam * Switch to the next channel marked for scanning. 312116742Ssam */ 313138568Ssamint 314138568Ssamieee80211_next_scan(struct ieee80211com *ic) 315116742Ssam{ 316116742Ssam struct ieee80211_channel *chan; 317116742Ssam 318138568Ssam /* 319138568Ssam * Insure any previous mgt frame timeouts don't fire. 320138568Ssam * This assumes the driver does the right thing in 321138568Ssam * flushing anything queued in the driver and below. 322138568Ssam */ 323138568Ssam ic->ic_mgt_timer = 0; 324138568Ssam 325148936Ssam chan = ic->ic_curchan; 326138568Ssam do { 327116742Ssam if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX]) 328116742Ssam chan = &ic->ic_channels[0]; 329116742Ssam if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) { 330138568Ssam clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan)); 331138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 332138568Ssam "%s: chan %d->%d\n", __func__, 333148936Ssam ieee80211_chan2ieee(ic, ic->ic_curchan), 334138568Ssam ieee80211_chan2ieee(ic, chan)); 335148936Ssam ic->ic_curchan = chan; 336148936Ssam /* 337148936Ssam * XXX drivers should do this as needed, 338148936Ssam * XXX for now maintain compatibility 339148936Ssam */ 340148936Ssam ic->ic_bss->ni_rates = 341148936Ssam ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)]; 342138568Ssam ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 343138568Ssam return 1; 344116742Ssam } 345148936Ssam } while (chan != ic->ic_curchan); 346138568Ssam ieee80211_end_scan(ic); 347138568Ssam return 0; 348116742Ssam} 349116742Ssam 350141658Ssamstatic __inline void 351141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss) 352141658Ssam{ 353141658Ssam /* propagate useful state */ 354141658Ssam nbss->ni_authmode = obss->ni_authmode; 355141658Ssam nbss->ni_txpower = obss->ni_txpower; 356141658Ssam nbss->ni_vlan = obss->ni_vlan; 357141658Ssam nbss->ni_rsn = obss->ni_rsn; 358141658Ssam /* XXX statistics? */ 359141658Ssam} 360141658Ssam 361116742Ssamvoid 362116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 363116742Ssam{ 364140753Ssam struct ieee80211_node_table *nt; 365116742Ssam struct ieee80211_node *ni; 366116742Ssam 367138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 368138568Ssam "%s: creating ibss\n", __func__); 369138568Ssam 370138568Ssam /* 371138568Ssam * Create the station/neighbor table. Note that for adhoc 372138568Ssam * mode we make the initial inactivity timer longer since 373138568Ssam * we create nodes only through discovery and they typically 374138568Ssam * are long-lived associations. 375138568Ssam */ 376140753Ssam nt = &ic->ic_sta; 377140753Ssam IEEE80211_NODE_LOCK(nt); 378140753Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 379140753Ssam nt->nt_name = "station"; 380140753Ssam nt->nt_inact_init = ic->ic_inact_init; 381140753Ssam } else { 382140753Ssam nt->nt_name = "neighbor"; 383140753Ssam nt->nt_inact_init = ic->ic_inact_run; 384140753Ssam } 385140753Ssam IEEE80211_NODE_UNLOCK(nt); 386140753Ssam 387148863Ssam ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr); 388140753Ssam if (ni == NULL) { 389140753Ssam /* XXX recovery? */ 390138568Ssam return; 391138568Ssam } 392116742Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 393116742Ssam ni->ni_esslen = ic->ic_des_esslen; 394116742Ssam memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 395141658Ssam copy_bss(ni, ic->ic_bss); 396148843Ssam ni->ni_intval = ic->ic_bintval; 397138568Ssam if (ic->ic_flags & IEEE80211_F_PRIVACY) 398116742Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 399116742Ssam if (ic->ic_phytype == IEEE80211_T_FH) { 400116742Ssam ni->ni_fhdwell = 200; /* XXX */ 401116742Ssam ni->ni_fhindex = 1; 402116742Ssam } 403138568Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 404138568Ssam ic->ic_flags |= IEEE80211_F_SIBSS; 405138568Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */ 406143300Ssam if (ic->ic_flags & IEEE80211_F_DESBSSID) 407143300Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid); 408143300Ssam else 409143300Ssam ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */ 410153403Ssam } else if (ic->ic_opmode == IEEE80211_M_AHDEMO) { 411153403Ssam if (ic->ic_flags & IEEE80211_F_DESBSSID) 412153403Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid); 413153403Ssam else 414153403Ssam memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN); 415138568Ssam } 416138568Ssam /* 417138568Ssam * Fix the channel and related attributes. 418138568Ssam */ 419138568Ssam ieee80211_set_chan(ic, ni, chan); 420148936Ssam ic->ic_curchan = chan; 421138568Ssam ic->ic_curmode = ieee80211_chan2mode(ic, chan); 422138568Ssam /* 423138568Ssam * Do mode-specific rate setup. 424138568Ssam */ 425138568Ssam if (ic->ic_curmode == IEEE80211_MODE_11G) { 426138568Ssam /* 427138568Ssam * Use a mixed 11b/11g rate set. 428138568Ssam */ 429138568Ssam ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G); 430138568Ssam } else if (ic->ic_curmode == IEEE80211_MODE_11B) { 431138568Ssam /* 432138568Ssam * Force pure 11b rate set. 433138568Ssam */ 434138568Ssam ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B); 435138568Ssam } 436138568Ssam 437140753Ssam (void) ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 438116742Ssam} 439116742Ssam 440138568Ssamvoid 441138568Ssamieee80211_reset_bss(struct ieee80211com *ic) 442138568Ssam{ 443138568Ssam struct ieee80211_node *ni, *obss; 444138568Ssam 445138568Ssam ieee80211_node_table_reset(&ic->ic_scan); 446140753Ssam ieee80211_node_table_reset(&ic->ic_sta); 447140753Ssam 448138568Ssam ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr); 449138568Ssam KASSERT(ni != NULL, ("unable to setup inital BSS node")); 450138568Ssam obss = ic->ic_bss; 451138568Ssam ic->ic_bss = ieee80211_ref_node(ni); 452141658Ssam if (obss != NULL) { 453141658Ssam copy_bss(ni, obss); 454148843Ssam ni->ni_intval = ic->ic_bintval; 455138568Ssam ieee80211_free_node(obss); 456141658Ssam } 457138568Ssam} 458138568Ssam 459148432Ssam/* XXX tunable */ 460148432Ssam#define STA_FAILS_MAX 2 /* assoc failures before ignored */ 461148432Ssam 462127767Ssamstatic int 463138568Ssamieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni) 464127767Ssam{ 465127767Ssam u_int8_t rate; 466127767Ssam int fail; 467127767Ssam 468127767Ssam fail = 0; 469127767Ssam if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 470127767Ssam fail |= 0x01; 471127767Ssam if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 472127767Ssam ni->ni_chan != ic->ic_des_chan) 473127767Ssam fail |= 0x01; 474127767Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 475127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 476127767Ssam fail |= 0x02; 477127767Ssam } else { 478127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 479127767Ssam fail |= 0x02; 480127767Ssam } 481138568Ssam if (ic->ic_flags & IEEE80211_F_PRIVACY) { 482127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 483127767Ssam fail |= 0x04; 484127767Ssam } else { 485127767Ssam /* XXX does this mean privacy is supported or required? */ 486127767Ssam if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 487127767Ssam fail |= 0x04; 488127767Ssam } 489148299Ssam rate = ieee80211_fix_rate(ni, IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 490127767Ssam if (rate & IEEE80211_RATE_BASIC) 491127767Ssam fail |= 0x08; 492127767Ssam if (ic->ic_des_esslen != 0 && 493127767Ssam (ni->ni_esslen != ic->ic_des_esslen || 494127767Ssam memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) 495127767Ssam fail |= 0x10; 496127767Ssam if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 497127767Ssam !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 498127767Ssam fail |= 0x20; 499148432Ssam if (ni->ni_fails >= STA_FAILS_MAX) 500148432Ssam fail |= 0x40; 501127767Ssam#ifdef IEEE80211_DEBUG 502138568Ssam if (ieee80211_msg_scan(ic)) { 503148432Ssam printf(" %c %s", 504148432Ssam fail & 0x40 ? '=' : fail & 0x80 ? '^' : 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; 551148432Ssam int weight; 552138568Ssam 553138568Ssam /* privacy support preferred */ 554138568Ssam if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) && 555138568Ssam (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 556138568Ssam return 1; 557138568Ssam if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 && 558138568Ssam (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)) 559138568Ssam return -1; 560138568Ssam 561148432Ssam /* compare count of previous failures */ 562148432Ssam weight = b->ni_fails - a->ni_fails; 563148432Ssam if (abs(weight) > 1) 564148432Ssam return weight; 565148432Ssam 566138568Ssam rssia = ic->ic_node_getrssi(a); 567138568Ssam rssib = ic->ic_node_getrssi(b); 568139543Ssam if (abs(rssib - rssia) < 5) { 569139543Ssam /* best/max rate preferred if signal level close enough XXX */ 570139543Ssam maxa = maxrate(a); 571139543Ssam maxb = maxrate(b); 572139543Ssam if (maxa != maxb) 573139543Ssam return maxa - maxb; 574139543Ssam /* XXX use freq for channel preference */ 575139543Ssam /* for now just prefer 5Ghz band to all other bands */ 576139543Ssam if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) && 577139543Ssam !IEEE80211_IS_CHAN_5GHZ(b->ni_chan)) 578139543Ssam return 1; 579139543Ssam if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) && 580139543Ssam IEEE80211_IS_CHAN_5GHZ(b->ni_chan)) 581139543Ssam return -1; 582139543Ssam } 583138568Ssam /* all things being equal, use signal level */ 584138568Ssam return rssia - rssib; 585138568Ssam} 586138568Ssam 587138568Ssam/* 588140441Ssam * Mark an ongoing scan stopped. 589140441Ssam */ 590140441Ssamvoid 591140441Ssamieee80211_cancel_scan(struct ieee80211com *ic) 592140441Ssam{ 593140441Ssam 594140441Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n", 595140441Ssam __func__, 596140441Ssam (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive"); 597140441Ssam 598140441Ssam ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN); 599140441Ssam} 600140441Ssam 601140441Ssam/* 602116742Ssam * Complete a scan of potential channels. 603116742Ssam */ 604116742Ssamvoid 605138568Ssamieee80211_end_scan(struct ieee80211com *ic) 606116742Ssam{ 607140448Ssam struct ieee80211_node_table *nt = &ic->ic_scan; 608140448Ssam struct ieee80211_node *ni, *selbs; 609116742Ssam 610140441Ssam ieee80211_cancel_scan(ic); 611140441Ssam ieee80211_notify_scan_done(ic); 612116742Ssam 613116742Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 614138568Ssam u_int8_t maxrssi[IEEE80211_CHAN_MAX]; /* XXX off stack? */ 615138568Ssam int i, bestchan; 616138568Ssam u_int8_t rssi; 617138568Ssam 618116742Ssam /* 619116742Ssam * The passive scan to look for existing AP's completed, 620116742Ssam * select a channel to camp on. Identify the channels 621116742Ssam * that already have one or more AP's and try to locate 622140753Ssam * an unoccupied one. If that fails, pick a channel that 623138568Ssam * looks to be quietest. 624116742Ssam */ 625138568Ssam memset(maxrssi, 0, sizeof(maxrssi)); 626140448Ssam IEEE80211_NODE_LOCK(nt); 627140448Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 628138568Ssam rssi = ic->ic_node_getrssi(ni); 629138568Ssam i = ieee80211_chan2ieee(ic, ni->ni_chan); 630138568Ssam if (rssi > maxrssi[i]) 631138568Ssam maxrssi[i] = rssi; 632116742Ssam } 633140448Ssam IEEE80211_NODE_UNLOCK(nt); 634138568Ssam /* XXX select channel more intelligently */ 635138568Ssam bestchan = -1; 636116742Ssam for (i = 0; i < IEEE80211_CHAN_MAX; i++) 637138568Ssam if (isset(ic->ic_chan_active, i)) { 638138568Ssam /* 639138568Ssam * If the channel is unoccupied the max rssi 640138568Ssam * should be zero; just take it. Otherwise 641138568Ssam * track the channel with the lowest rssi and 642138568Ssam * use that when all channels appear occupied. 643138568Ssam */ 644138568Ssam if (maxrssi[i] == 0) { 645138568Ssam bestchan = i; 646116742Ssam break; 647138568Ssam } 648143715Ssam if (bestchan == -1 || 649143715Ssam maxrssi[i] < maxrssi[bestchan]) 650138568Ssam bestchan = i; 651138568Ssam } 652138568Ssam if (bestchan != -1) { 653138568Ssam ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]); 654138568Ssam return; 655116742Ssam } 656138568Ssam /* no suitable channel, should not happen */ 657138568Ssam } 658138568Ssam 659138568Ssam /* 660138568Ssam * When manually sequencing the state machine; scan just once 661138568Ssam * regardless of whether we have a candidate or not. The 662138568Ssam * controlling application is expected to setup state and 663138568Ssam * initiate an association. 664138568Ssam */ 665138568Ssam if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL) 666116742Ssam return; 667138568Ssam /* 668138568Ssam * Automatic sequencing; look for a candidate and 669138568Ssam * if found join the network. 670138568Ssam */ 671140448Ssam /* NB: unlocked read should be ok */ 672140448Ssam if (TAILQ_FIRST(&nt->nt_node) == NULL) { 673138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 674138568Ssam "%s: no scan candidate\n", __func__); 675116742Ssam notfound: 676116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS && 677116742Ssam (ic->ic_flags & IEEE80211_F_IBSSON) && 678116742Ssam ic->ic_des_esslen != 0) { 679116742Ssam ieee80211_create_ibss(ic, ic->ic_ibss_chan); 680116742Ssam return; 681116742Ssam } 682116742Ssam /* 683148432Ssam * Decrement the failure counts so entries will be 684148432Ssam * reconsidered the next time around. We really want 685148432Ssam * to do this only for sta's where we've previously 686153352Ssam * had some success. 687148432Ssam */ 688148432Ssam IEEE80211_NODE_LOCK(nt); 689148432Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 690148432Ssam if (ni->ni_fails) 691148432Ssam ni->ni_fails--; 692148432Ssam IEEE80211_NODE_UNLOCK(nt); 693148432Ssam /* 694116742Ssam * Reset the list of channels to scan and start again. 695116742Ssam */ 696138568Ssam ieee80211_reset_scan(ic); 697138568Ssam ic->ic_flags |= IEEE80211_F_SCAN; 698138568Ssam ieee80211_next_scan(ic); 699116742Ssam return; 700116742Ssam } 701116742Ssam selbs = NULL; 702138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n", 703138568Ssam "macaddr bssid chan rssi rate flag wep essid"); 704140448Ssam IEEE80211_NODE_LOCK(nt); 705140448Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 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 737148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni) 738138568Ssam{ 739148306Ssam struct ieee80211com *ic = ni->ni_ic; 740138568Ssam 741140453Ssam if (ni == ic->ic_bss || 742140453Ssam IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) { 743138568Ssam /* unchanged, nothing to do */ 744138568Ssam return 0; 745138568Ssam } 746138568Ssam if (ieee80211_match_bss(ic, ni) != 0) { /* capabilities mismatch */ 747138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 748138568Ssam "%s: merge failed, capabilities mismatch\n", __func__); 749138568Ssam ic->ic_stats.is_ibss_capmismatch++; 750138568Ssam return 0; 751138568Ssam } 752138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 753138568Ssam "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__, 754138568Ssam ether_sprintf(ni->ni_bssid), 755138568Ssam ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 756138568Ssam ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long", 757138568Ssam ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "" 758138568Ssam ); 759140753Ssam return ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 760138568Ssam} 761138568Ssam 762138568Ssam/* 763138568Ssam * Join the specified IBSS/BSS network. The node is assumed to 764138568Ssam * be passed in with a held reference. 765138568Ssam */ 766138568Ssamint 767138568Ssamieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs) 768138568Ssam{ 769138568Ssam struct ieee80211_node *obss; 770138568Ssam 771116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 772140753Ssam struct ieee80211_node_table *nt; 773138568Ssam /* 774140440Ssam * Delete unusable rates; we've already checked 775140440Ssam * that the negotiated rate set is acceptable. 776138568Ssam */ 777148299Ssam ieee80211_fix_rate(selbs, IEEE80211_F_DODEL); 778127772Ssam /* 779140753Ssam * Fillin the neighbor table; it will already 780139521Ssam * exist if we are simply switching mastership. 781140753Ssam * XXX ic_sta always setup so this is unnecessary? 782127772Ssam */ 783140753Ssam nt = &ic->ic_sta; 784140753Ssam IEEE80211_NODE_LOCK(nt); 785140753Ssam nt->nt_name = "neighbor"; 786140753Ssam nt->nt_inact_init = ic->ic_inact_run; 787140753Ssam IEEE80211_NODE_UNLOCK(nt); 788138568Ssam } 789138568Ssam 790138568Ssam /* 791138568Ssam * Committed to selbs, setup state. 792138568Ssam */ 793138568Ssam obss = ic->ic_bss; 794140753Ssam ic->ic_bss = selbs; /* NB: caller assumed to bump refcnt */ 795153352Ssam if (obss != NULL) { 796153352Ssam copy_bss(selbs, obss); 797138568Ssam ieee80211_free_node(obss); 798153352Ssam } 799138568Ssam /* 800138568Ssam * Set the erp state (mostly the slot time) to deal with 801138568Ssam * the auto-select case; this should be redundant if the 802138568Ssam * mode is locked. 803138568Ssam */ 804138568Ssam ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan); 805148936Ssam ic->ic_curchan = selbs->ni_chan; 806138568Ssam ieee80211_reset_erp(ic); 807138568Ssam ieee80211_wme_initparams(ic); 808140753Ssam 809140753Ssam if (ic->ic_opmode == IEEE80211_M_STA) 810140753Ssam ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 811140753Ssam else 812117811Ssam ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 813138568Ssam return 1; 814116742Ssam} 815116742Ssam 816138568Ssam/* 817138568Ssam * Leave the specified IBSS/BSS network. The node is assumed to 818138568Ssam * be passed in with a held reference. 819138568Ssam */ 820138568Ssamvoid 821138568Ssamieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 822138568Ssam{ 823138568Ssam ic->ic_node_cleanup(ni); 824138568Ssam ieee80211_notify_node_leave(ic, ni); 825138568Ssam} 826138568Ssam 827116742Ssamstatic struct ieee80211_node * 828138568Ssamnode_alloc(struct ieee80211_node_table *nt) 829116742Ssam{ 830127768Ssam struct ieee80211_node *ni; 831138568Ssam 832127768Ssam MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node), 833127768Ssam M_80211_NODE, M_NOWAIT | M_ZERO); 834127768Ssam return ni; 835116742Ssam} 836116742Ssam 837138568Ssam/* 838138568Ssam * Reclaim any resources in a node and reset any critical 839138568Ssam * state. Typically nodes are free'd immediately after, 840138568Ssam * but in some cases the storage may be reused so we need 841138568Ssam * to insure consistent state (should probably fix that). 842138568Ssam */ 843116742Ssamstatic void 844138568Ssamnode_cleanup(struct ieee80211_node *ni) 845116742Ssam{ 846138568Ssam#define N(a) (sizeof(a)/sizeof(a[0])) 847138568Ssam struct ieee80211com *ic = ni->ni_ic; 848138568Ssam int i, qlen; 849138568Ssam 850138568Ssam /* NB: preserve ni_table */ 851138568Ssam if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 852138568Ssam ic->ic_ps_sta--; 853138568Ssam ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 854138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 855138568Ssam "[%s] power save mode off, %u sta's in ps mode\n", 856138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta); 857138568Ssam } 858147788Ssam /* 859147788Ssam * Clear AREF flag that marks the authorization refcnt bump 860147788Ssam * has happened. This is probably not needed as the node 861147788Ssam * should always be removed from the table so not found but 862147788Ssam * do it just in case. 863147788Ssam */ 864147788Ssam ni->ni_flags &= ~IEEE80211_NODE_AREF; 865138568Ssam 866138568Ssam /* 867138568Ssam * Drain power save queue and, if needed, clear TIM. 868138568Ssam */ 869138568Ssam IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen); 870138568Ssam if (qlen != 0 && ic->ic_set_tim != NULL) 871148304Ssam ic->ic_set_tim(ni, 0); 872138568Ssam 873138568Ssam ni->ni_associd = 0; 874138568Ssam if (ni->ni_challenge != NULL) { 875138568Ssam FREE(ni->ni_challenge, M_DEVBUF); 876138568Ssam ni->ni_challenge = NULL; 877138568Ssam } 878138568Ssam /* 879138568Ssam * Preserve SSID, WPA, and WME ie's so the bss node is 880138568Ssam * reusable during a re-auth/re-assoc state transition. 881138568Ssam * If we remove these data they will not be recreated 882138568Ssam * because they come from a probe-response or beacon frame 883138568Ssam * which cannot be expected prior to the association-response. 884138568Ssam * This should not be an issue when operating in other modes 885138568Ssam * as stations leaving always go through a full state transition 886138568Ssam * which will rebuild this state. 887138568Ssam * 888138568Ssam * XXX does this leave us open to inheriting old state? 889138568Ssam */ 890138568Ssam for (i = 0; i < N(ni->ni_rxfrag); i++) 891138568Ssam if (ni->ni_rxfrag[i] != NULL) { 892138568Ssam m_freem(ni->ni_rxfrag[i]); 893138568Ssam ni->ni_rxfrag[i] = NULL; 894138568Ssam } 895148863Ssam /* 896148863Ssam * Must be careful here to remove any key map entry w/o a LOR. 897148863Ssam */ 898148863Ssam ieee80211_node_delucastkey(ni); 899138568Ssam#undef N 900116742Ssam} 901116742Ssam 902116742Ssamstatic void 903138568Ssamnode_free(struct ieee80211_node *ni) 904116742Ssam{ 905138568Ssam struct ieee80211com *ic = ni->ni_ic; 906138568Ssam 907138568Ssam ic->ic_node_cleanup(ni); 908138568Ssam if (ni->ni_wpa_ie != NULL) 909138568Ssam FREE(ni->ni_wpa_ie, M_DEVBUF); 910138568Ssam if (ni->ni_wme_ie != NULL) 911138568Ssam FREE(ni->ni_wme_ie, M_DEVBUF); 912138568Ssam IEEE80211_NODE_SAVEQ_DESTROY(ni); 913138568Ssam FREE(ni, M_80211_NODE); 914116742Ssam} 915116742Ssam 916120104Ssamstatic u_int8_t 917138568Ssamnode_getrssi(const struct ieee80211_node *ni) 918120104Ssam{ 919120104Ssam return ni->ni_rssi; 920120104Ssam} 921120104Ssam 922116742Ssamstatic void 923138568Ssamieee80211_setup_node(struct ieee80211_node_table *nt, 924138568Ssam struct ieee80211_node *ni, const u_int8_t *macaddr) 925116742Ssam{ 926138568Ssam struct ieee80211com *ic = nt->nt_ic; 927116742Ssam int hash; 928116742Ssam 929138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 930140766Ssam "%s %p<%s> in %s table\n", __func__, ni, 931138568Ssam ether_sprintf(macaddr), nt->nt_name); 932138568Ssam 933116742Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 934116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 935138568Ssam ieee80211_node_initref(ni); /* mark referenced */ 936138568Ssam ni->ni_chan = IEEE80211_CHAN_ANYC; 937138568Ssam ni->ni_authmode = IEEE80211_AUTH_OPEN; 938138568Ssam ni->ni_txpower = ic->ic_txpowlimit; /* max power */ 939138568Ssam ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE); 940139528Ssam ni->ni_inact_reload = nt->nt_inact_init; 941139528Ssam ni->ni_inact = ni->ni_inact_reload; 942138568Ssam IEEE80211_NODE_SAVEQ_INIT(ni, "unknown"); 943138568Ssam 944138568Ssam IEEE80211_NODE_LOCK(nt); 945138568Ssam TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list); 946138568Ssam LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash); 947138568Ssam ni->ni_table = nt; 948138568Ssam ni->ni_ic = ic; 949138568Ssam IEEE80211_NODE_UNLOCK(nt); 950116742Ssam} 951116742Ssam 952116742Ssamstruct ieee80211_node * 953138568Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 954116742Ssam{ 955138568Ssam struct ieee80211com *ic = nt->nt_ic; 956138568Ssam struct ieee80211_node *ni; 957138568Ssam 958138568Ssam ni = ic->ic_node_alloc(nt); 959116742Ssam if (ni != NULL) 960138568Ssam ieee80211_setup_node(nt, ni, macaddr); 961127769Ssam else 962127769Ssam ic->ic_stats.is_rx_nodealloc++; 963116742Ssam return ni; 964116742Ssam} 965116742Ssam 966148777Ssam/* 967148777Ssam * Craft a temporary node suitable for sending a management frame 968148777Ssam * to the specified station. We craft only as much state as we 969148777Ssam * need to do the work since the node will be immediately reclaimed 970148777Ssam * once the send completes. 971148777Ssam */ 972116742Ssamstruct ieee80211_node * 973148777Ssamieee80211_tmp_node(struct ieee80211com *ic, const u_int8_t *macaddr) 974148777Ssam{ 975148777Ssam struct ieee80211_node *ni; 976148777Ssam 977148777Ssam ni = ic->ic_node_alloc(&ic->ic_sta); 978148777Ssam if (ni != NULL) { 979148777Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 980148777Ssam "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr)); 981148777Ssam 982148777Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 983148777Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 984148777Ssam ieee80211_node_initref(ni); /* mark referenced */ 985148777Ssam ni->ni_txpower = ic->ic_bss->ni_txpower; 986148777Ssam /* NB: required by ieee80211_fix_rate */ 987148777Ssam ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan); 988148777Ssam ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, 989148777Ssam IEEE80211_KEYIX_NONE); 990148777Ssam /* XXX optimize away */ 991148777Ssam IEEE80211_NODE_SAVEQ_INIT(ni, "unknown"); 992148777Ssam 993148777Ssam ni->ni_table = NULL; /* NB: pedantic */ 994148777Ssam ni->ni_ic = ic; 995148777Ssam } else { 996148777Ssam /* XXX msg */ 997148777Ssam ic->ic_stats.is_rx_nodealloc++; 998148777Ssam } 999148777Ssam return ni; 1000148777Ssam} 1001148777Ssam 1002148777Ssamstruct ieee80211_node * 1003138568Ssamieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 1004116742Ssam{ 1005138568Ssam struct ieee80211com *ic = nt->nt_ic; 1006138568Ssam struct ieee80211_node *ni; 1007138568Ssam 1008138568Ssam ni = ic->ic_node_alloc(nt); 1009116742Ssam if (ni != NULL) { 1010138568Ssam ieee80211_setup_node(nt, ni, macaddr); 1011127770Ssam /* 1012127770Ssam * Inherit from ic_bss. 1013127770Ssam */ 1014138568Ssam ni->ni_authmode = ic->ic_bss->ni_authmode; 1015138568Ssam ni->ni_txpower = ic->ic_bss->ni_txpower; 1016138568Ssam ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */ 1017127770Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 1018138568Ssam ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan); 1019138568Ssam ni->ni_rsn = ic->ic_bss->ni_rsn; 1020127770Ssam } else 1021127770Ssam ic->ic_stats.is_rx_nodealloc++; 1022116742Ssam return ni; 1023116742Ssam} 1024116742Ssam 1025127772Ssamstatic struct ieee80211_node * 1026138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1027138568Ssam_ieee80211_find_node_debug(struct ieee80211_node_table *nt, 1028138568Ssam const u_int8_t *macaddr, const char *func, int line) 1029138568Ssam#else 1030138568Ssam_ieee80211_find_node(struct ieee80211_node_table *nt, 1031138568Ssam const u_int8_t *macaddr) 1032138568Ssam#endif 1033116742Ssam{ 1034116742Ssam struct ieee80211_node *ni; 1035116742Ssam int hash; 1036116742Ssam 1037138568Ssam IEEE80211_NODE_LOCK_ASSERT(nt); 1038127772Ssam 1039116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 1040138568Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1041116742Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 1042138568Ssam ieee80211_ref_node(ni); /* mark referenced */ 1043138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1044138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1045140766Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1046140766Ssam func, line, 1047140766Ssam ni, ether_sprintf(ni->ni_macaddr), 1048140766Ssam ieee80211_node_refcnt(ni)); 1049138568Ssam#endif 1050127772Ssam return ni; 1051116742Ssam } 1052116742Ssam } 1053127772Ssam return NULL; 1054127772Ssam} 1055138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1056138568Ssam#define _ieee80211_find_node(nt, mac) \ 1057138568Ssam _ieee80211_find_node_debug(nt, mac, func, line) 1058138568Ssam#endif 1059127772Ssam 1060127772Ssamstruct ieee80211_node * 1061138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1062138568Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt, 1063138568Ssam const u_int8_t *macaddr, const char *func, int line) 1064138568Ssam#else 1065138568Ssamieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 1066138568Ssam#endif 1067127772Ssam{ 1068127772Ssam struct ieee80211_node *ni; 1069127772Ssam 1070138568Ssam IEEE80211_NODE_LOCK(nt); 1071138568Ssam ni = _ieee80211_find_node(nt, macaddr); 1072138568Ssam IEEE80211_NODE_UNLOCK(nt); 1073116742Ssam return ni; 1074116742Ssam} 1075116742Ssam 1076116742Ssam/* 1077138568Ssam * Fake up a node; this handles node discovery in adhoc mode. 1078138568Ssam * Note that for the driver's benefit we we treat this like 1079138568Ssam * an association so the driver has an opportunity to setup 1080138568Ssam * it's private state. 1081138568Ssam */ 1082138568Ssamstruct ieee80211_node * 1083138568Ssamieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt, 1084138568Ssam const u_int8_t macaddr[IEEE80211_ADDR_LEN]) 1085138568Ssam{ 1086138568Ssam struct ieee80211com *ic = nt->nt_ic; 1087138568Ssam struct ieee80211_node *ni; 1088138568Ssam 1089153073Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1090153073Ssam "%s: mac<%s>\n", __func__, ether_sprintf(macaddr)); 1091138568Ssam ni = ieee80211_dup_bss(nt, macaddr); 1092138568Ssam if (ni != NULL) { 1093138568Ssam /* XXX no rate negotiation; just dup */ 1094138568Ssam ni->ni_rates = ic->ic_bss->ni_rates; 1095139524Ssam if (ic->ic_newassoc != NULL) 1096148307Ssam ic->ic_newassoc(ni, 1); 1097138568Ssam /* XXX not right for 802.1x/WPA */ 1098148302Ssam ieee80211_node_authorize(ni); 1099153404Ssam if (ic->ic_opmode == IEEE80211_M_AHDEMO) { 1100153404Ssam /* 1101153404Ssam * Blindly propagate capabilities based on the 1102153404Ssam * local configuration. In particular this permits 1103153404Ssam * us to use QoS to disable ACK's. 1104153404Ssam */ 1105153404Ssam if (ic->ic_flags & IEEE80211_F_WME) 1106153404Ssam ni->ni_flags |= IEEE80211_NODE_QOS; 1107153404Ssam } 1108138568Ssam } 1109138568Ssam return ni; 1110138568Ssam} 1111138568Ssam 1112148936Ssam#ifdef IEEE80211_DEBUG 1113148936Ssamstatic void 1114148936Ssamdump_probe_beacon(u_int8_t subtype, int isnew, 1115148936Ssam const u_int8_t mac[IEEE80211_ADDR_LEN], 1116148936Ssam const struct ieee80211_scanparams *sp) 1117148936Ssam{ 1118148936Ssam 1119148936Ssam printf("[%s] %s%s on chan %u (bss chan %u) ", 1120148936Ssam ether_sprintf(mac), isnew ? "new " : "", 1121148936Ssam ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT], 1122148936Ssam sp->chan, sp->bchan); 1123148936Ssam ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]); 1124148936Ssam printf("\n"); 1125148936Ssam 1126148936Ssam if (isnew) { 1127148936Ssam printf("[%s] caps 0x%x bintval %u erp 0x%x", 1128148936Ssam ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp); 1129148936Ssam if (sp->country != NULL) { 1130148936Ssam#ifdef __FreeBSD__ 1131148936Ssam printf(" country info %*D", 1132148936Ssam sp->country[1], sp->country+2, " "); 1133148936Ssam#else 1134148936Ssam int i; 1135148936Ssam printf(" country info"); 1136148936Ssam for (i = 0; i < sp->country[1]; i++) 1137148936Ssam printf(" %02x", sp->country[i+2]); 1138148936Ssam#endif 1139148936Ssam } 1140148936Ssam printf("\n"); 1141148936Ssam } 1142148936Ssam} 1143148936Ssam#endif /* IEEE80211_DEBUG */ 1144148936Ssam 1145148936Ssamstatic void 1146148936Ssamsaveie(u_int8_t **iep, const u_int8_t *ie) 1147148936Ssam{ 1148148936Ssam 1149148936Ssam if (ie == NULL) 1150148936Ssam *iep = NULL; 1151148936Ssam else 1152148936Ssam ieee80211_saveie(iep, ie); 1153148936Ssam} 1154148936Ssam 1155148936Ssam/* 1156148936Ssam * Process a beacon or probe response frame. 1157148936Ssam */ 1158148936Ssamvoid 1159148936Ssamieee80211_add_scan(struct ieee80211com *ic, 1160148936Ssam const struct ieee80211_scanparams *sp, 1161148936Ssam const struct ieee80211_frame *wh, 1162148936Ssam int subtype, int rssi, int rstamp) 1163148936Ssam{ 1164148936Ssam#define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1165148936Ssam struct ieee80211_node_table *nt = &ic->ic_scan; 1166148936Ssam struct ieee80211_node *ni; 1167148936Ssam int newnode = 0; 1168148936Ssam 1169148936Ssam ni = ieee80211_find_node(nt, wh->i_addr2); 1170148936Ssam if (ni == NULL) { 1171148936Ssam /* 1172148936Ssam * Create a new entry. 1173148936Ssam */ 1174148936Ssam ni = ic->ic_node_alloc(nt); 1175148936Ssam if (ni == NULL) { 1176148936Ssam ic->ic_stats.is_rx_nodealloc++; 1177148936Ssam return; 1178148936Ssam } 1179148936Ssam ieee80211_setup_node(nt, ni, wh->i_addr2); 1180148936Ssam /* 1181148936Ssam * XXX inherit from ic_bss. 1182148936Ssam */ 1183148936Ssam ni->ni_authmode = ic->ic_bss->ni_authmode; 1184148936Ssam ni->ni_txpower = ic->ic_bss->ni_txpower; 1185148936Ssam ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */ 1186148936Ssam ieee80211_set_chan(ic, ni, ic->ic_curchan); 1187148936Ssam ni->ni_rsn = ic->ic_bss->ni_rsn; 1188148936Ssam newnode = 1; 1189148936Ssam } 1190148936Ssam#ifdef IEEE80211_DEBUG 1191148936Ssam if (ieee80211_msg_scan(ic) && (ic->ic_flags & IEEE80211_F_SCAN)) 1192148936Ssam dump_probe_beacon(subtype, newnode, wh->i_addr2, sp); 1193148936Ssam#endif 1194148936Ssam /* XXX ap beaconing multiple ssid w/ same bssid */ 1195148936Ssam if (sp->ssid[1] != 0 && 1196148936Ssam (ISPROBE(subtype) || ni->ni_esslen == 0)) { 1197148936Ssam ni->ni_esslen = sp->ssid[1]; 1198148936Ssam memset(ni->ni_essid, 0, sizeof(ni->ni_essid)); 1199148936Ssam memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]); 1200148936Ssam } 1201148936Ssam ni->ni_scangen = ic->ic_scan.nt_scangen; 1202148936Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3); 1203148936Ssam ni->ni_rssi = rssi; 1204148936Ssam ni->ni_rstamp = rstamp; 1205148936Ssam memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp)); 1206148936Ssam ni->ni_intval = sp->bintval; 1207148936Ssam ni->ni_capinfo = sp->capinfo; 1208148936Ssam ni->ni_chan = &ic->ic_channels[sp->chan]; 1209148936Ssam ni->ni_fhdwell = sp->fhdwell; 1210148936Ssam ni->ni_fhindex = sp->fhindex; 1211148936Ssam ni->ni_erp = sp->erp; 1212148936Ssam if (sp->tim != NULL) { 1213148936Ssam struct ieee80211_tim_ie *ie = 1214148936Ssam (struct ieee80211_tim_ie *) sp->tim; 1215148936Ssam 1216148936Ssam ni->ni_dtim_count = ie->tim_count; 1217148936Ssam ni->ni_dtim_period = ie->tim_period; 1218148936Ssam } 1219148936Ssam /* 1220148936Ssam * Record the byte offset from the mac header to 1221148936Ssam * the start of the TIM information element for 1222148936Ssam * use by hardware and/or to speedup software 1223148936Ssam * processing of beacon frames. 1224148936Ssam */ 1225148936Ssam ni->ni_timoff = sp->timoff; 1226148936Ssam /* 1227148936Ssam * Record optional information elements that might be 1228148936Ssam * used by applications or drivers. 1229148936Ssam */ 1230148936Ssam saveie(&ni->ni_wme_ie, sp->wme); 1231148936Ssam saveie(&ni->ni_wpa_ie, sp->wpa); 1232148936Ssam 1233148936Ssam /* NB: must be after ni_chan is setup */ 1234148936Ssam ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT); 1235148936Ssam 1236148936Ssam if (!newnode) 1237148936Ssam ieee80211_free_node(ni); 1238148936Ssam#undef ISPROBE 1239148936Ssam} 1240148936Ssam 1241153073Ssamvoid 1242153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni, 1243153073Ssam const struct ieee80211_frame *wh, 1244153073Ssam const struct ieee80211_scanparams *sp) 1245153073Ssam{ 1246153073Ssam 1247153073Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1248153073Ssam "%s: %p<%s>\n", __func__, ni, ether_sprintf(ni->ni_macaddr)); 1249153073Ssam ni->ni_esslen = sp->ssid[1]; 1250153073Ssam memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]); 1251153073Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3); 1252153073Ssam memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp)); 1253153073Ssam ni->ni_intval = sp->bintval; 1254153073Ssam ni->ni_capinfo = sp->capinfo; 1255153073Ssam ni->ni_chan = ni->ni_ic->ic_curchan; 1256153073Ssam ni->ni_fhdwell = sp->fhdwell; 1257153073Ssam ni->ni_fhindex = sp->fhindex; 1258153073Ssam ni->ni_erp = sp->erp; 1259153073Ssam ni->ni_timoff = sp->timoff; 1260153073Ssam if (sp->wme != NULL) 1261153073Ssam ieee80211_saveie(&ni->ni_wme_ie, sp->wme); 1262153073Ssam if (sp->wpa != NULL) 1263153073Ssam ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa); 1264153073Ssam 1265153073Ssam /* NB: must be after ni_chan is setup */ 1266153073Ssam ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT); 1267153073Ssam} 1268153073Ssam 1269148936Ssam/* 1270148936Ssam * Do node discovery in adhoc mode on receipt of a beacon 1271148936Ssam * or probe response frame. Note that for the driver's 1272148936Ssam * benefit we we treat this like an association so the 1273148936Ssam * driver has an opportunity to setup it's private state. 1274148936Ssam */ 1275148936Ssamstruct ieee80211_node * 1276148936Ssamieee80211_add_neighbor(struct ieee80211com *ic, 1277148936Ssam const struct ieee80211_frame *wh, 1278148936Ssam const struct ieee80211_scanparams *sp) 1279148936Ssam{ 1280148936Ssam struct ieee80211_node *ni; 1281148936Ssam 1282153073Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1283153073Ssam "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2)); 1284148936Ssam ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */ 1285148936Ssam if (ni != NULL) { 1286153073Ssam ieee80211_init_neighbor(ni, wh, sp); 1287148936Ssam if (ic->ic_newassoc != NULL) 1288148936Ssam ic->ic_newassoc(ni, 1); 1289148936Ssam /* XXX not right for 802.1x/WPA */ 1290148936Ssam ieee80211_node_authorize(ni); 1291148936Ssam } 1292148936Ssam return ni; 1293148936Ssam} 1294148936Ssam 1295148863Ssam#define IS_CTL(wh) \ 1296148863Ssam ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) 1297148863Ssam#define IS_PSPOLL(wh) \ 1298148863Ssam ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL) 1299138568Ssam/* 1300138568Ssam * Locate the node for sender, track state, and then pass the 1301138568Ssam * (referenced) node up to the 802.11 layer for its use. We 1302138568Ssam * are required to pass some node so we fall back to ic_bss 1303138568Ssam * when this frame is from an unknown sender. The 802.11 layer 1304138568Ssam * knows this means the sender wasn't in the node table and 1305138568Ssam * acts accordingly. 1306138568Ssam */ 1307138568Ssamstruct ieee80211_node * 1308138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1309138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic, 1310138568Ssam const struct ieee80211_frame_min *wh, const char *func, int line) 1311138568Ssam#else 1312138568Ssamieee80211_find_rxnode(struct ieee80211com *ic, 1313138568Ssam const struct ieee80211_frame_min *wh) 1314138568Ssam#endif 1315138568Ssam{ 1316138568Ssam struct ieee80211_node_table *nt; 1317138568Ssam struct ieee80211_node *ni; 1318138568Ssam 1319138568Ssam /* XXX may want scanned nodes in the neighbor table for adhoc */ 1320138568Ssam if (ic->ic_opmode == IEEE80211_M_STA || 1321138568Ssam ic->ic_opmode == IEEE80211_M_MONITOR || 1322138568Ssam (ic->ic_flags & IEEE80211_F_SCAN)) 1323138568Ssam nt = &ic->ic_scan; 1324138568Ssam else 1325140753Ssam nt = &ic->ic_sta; 1326138568Ssam /* XXX check ic_bss first in station mode */ 1327138568Ssam /* XXX 4-address frames? */ 1328138568Ssam IEEE80211_NODE_LOCK(nt); 1329138568Ssam if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/) 1330138568Ssam ni = _ieee80211_find_node(nt, wh->i_addr1); 1331138568Ssam else 1332138568Ssam ni = _ieee80211_find_node(nt, wh->i_addr2); 1333148863Ssam if (ni == NULL) 1334148863Ssam ni = ieee80211_ref_node(ic->ic_bss); 1335138568Ssam IEEE80211_NODE_UNLOCK(nt); 1336138568Ssam 1337148863Ssam return ni; 1338148863Ssam} 1339148863Ssam 1340148863Ssam/* 1341148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w 1342148863Ssam * key index as a hint to locate the node in the key 1343148863Ssam * mapping table. If an entry is present at the key 1344148863Ssam * index we return it; otherwise do a normal lookup and 1345148863Ssam * update the mapping table if the station has a unicast 1346148863Ssam * key assigned to it. 1347148863Ssam */ 1348148863Ssamstruct ieee80211_node * 1349148863Ssam#ifdef IEEE80211_DEBUG_REFCNT 1350148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic, 1351148863Ssam const struct ieee80211_frame_min *wh, ieee80211_keyix keyix, 1352148863Ssam const char *func, int line) 1353148863Ssam#else 1354148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic, 1355148863Ssam const struct ieee80211_frame_min *wh, ieee80211_keyix keyix) 1356148863Ssam#endif 1357148863Ssam{ 1358148863Ssam struct ieee80211_node_table *nt; 1359148863Ssam struct ieee80211_node *ni; 1360148863Ssam 1361148863Ssam if (ic->ic_opmode == IEEE80211_M_STA || 1362148863Ssam ic->ic_opmode == IEEE80211_M_MONITOR || 1363148863Ssam (ic->ic_flags & IEEE80211_F_SCAN)) 1364148863Ssam nt = &ic->ic_scan; 1365148863Ssam else 1366148863Ssam nt = &ic->ic_sta; 1367148863Ssam IEEE80211_NODE_LOCK(nt); 1368148863Ssam if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) 1369148863Ssam ni = nt->nt_keyixmap[keyix]; 1370148863Ssam else 1371148863Ssam ni = NULL; 1372148863Ssam if (ni == NULL) { 1373148863Ssam if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/) 1374148863Ssam ni = _ieee80211_find_node(nt, wh->i_addr1); 1375148863Ssam else 1376148863Ssam ni = _ieee80211_find_node(nt, wh->i_addr2); 1377148863Ssam if (ni == NULL) 1378148863Ssam ni = ieee80211_ref_node(ic->ic_bss); 1379148863Ssam if (nt->nt_keyixmap != NULL) { 1380148863Ssam /* 1381148863Ssam * If the station has a unicast key cache slot 1382148863Ssam * assigned update the key->node mapping table. 1383148863Ssam */ 1384148863Ssam keyix = ni->ni_ucastkey.wk_rxkeyix; 1385148863Ssam /* XXX can keyixmap[keyix] != NULL? */ 1386148863Ssam if (keyix < nt->nt_keyixmax && 1387148863Ssam nt->nt_keyixmap[keyix] == NULL) { 1388148863Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1389148863Ssam "%s: add key map entry %p<%s> refcnt %d\n", 1390148863Ssam __func__, ni, ether_sprintf(ni->ni_macaddr), 1391148863Ssam ieee80211_node_refcnt(ni)+1); 1392148863Ssam nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni); 1393148863Ssam } 1394148863Ssam } 1395148863Ssam } else { 1396148863Ssam ieee80211_ref_node(ni); 1397148863Ssam } 1398148863Ssam IEEE80211_NODE_UNLOCK(nt); 1399148863Ssam 1400148863Ssam return ni; 1401148863Ssam} 1402138568Ssam#undef IS_PSPOLL 1403138568Ssam#undef IS_CTL 1404138568Ssam 1405138568Ssam/* 1406127772Ssam * Return a reference to the appropriate node for sending 1407127772Ssam * a data frame. This handles node discovery in adhoc networks. 1408127772Ssam */ 1409127772Ssamstruct ieee80211_node * 1410138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1411138568Ssamieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr, 1412138568Ssam const char *func, int line) 1413138568Ssam#else 1414138568Ssamieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr) 1415138568Ssam#endif 1416127772Ssam{ 1417140753Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 1418127772Ssam struct ieee80211_node *ni; 1419127772Ssam 1420127772Ssam /* 1421127772Ssam * The destination address should be in the node table 1422148863Ssam * unless this is a multicast/broadcast frame. We can 1423148863Ssam * also optimize station mode operation, all frames go 1424148863Ssam * to the bss node. 1425127772Ssam */ 1426127772Ssam /* XXX can't hold lock across dup_bss 'cuz of recursive locking */ 1427138568Ssam IEEE80211_NODE_LOCK(nt); 1428148863Ssam if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr)) 1429148863Ssam ni = ieee80211_ref_node(ic->ic_bss); 1430148863Ssam else 1431148863Ssam ni = _ieee80211_find_node(nt, macaddr); 1432138568Ssam IEEE80211_NODE_UNLOCK(nt); 1433138568Ssam 1434138568Ssam if (ni == NULL) { 1435138568Ssam if (ic->ic_opmode == IEEE80211_M_IBSS || 1436140497Ssam ic->ic_opmode == IEEE80211_M_AHDEMO) { 1437140497Ssam /* 1438140497Ssam * In adhoc mode cons up a node for the destination. 1439140497Ssam * Note that we need an additional reference for the 1440140497Ssam * caller to be consistent with _ieee80211_find_node. 1441140497Ssam */ 1442138568Ssam ni = ieee80211_fakeup_adhoc_node(nt, macaddr); 1443140497Ssam if (ni != NULL) 1444140497Ssam (void) ieee80211_ref_node(ni); 1445140497Ssam } else { 1446138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 1447138568Ssam "[%s] no node, discard frame (%s)\n", 1448138568Ssam ether_sprintf(macaddr), __func__); 1449138568Ssam ic->ic_stats.is_tx_nonode++; 1450127772Ssam } 1451127772Ssam } 1452127772Ssam return ni; 1453127772Ssam} 1454127772Ssam 1455127772Ssam/* 1456116742Ssam * Like find but search based on the channel too. 1457116742Ssam */ 1458116742Ssamstruct ieee80211_node * 1459138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1460138568Ssamieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt, 1461138568Ssam const u_int8_t *macaddr, struct ieee80211_channel *chan, 1462138568Ssam const char *func, int line) 1463138568Ssam#else 1464138568Ssamieee80211_find_node_with_channel(struct ieee80211_node_table *nt, 1465138568Ssam const u_int8_t *macaddr, struct ieee80211_channel *chan) 1466138568Ssam#endif 1467116742Ssam{ 1468116742Ssam struct ieee80211_node *ni; 1469116742Ssam int hash; 1470116742Ssam 1471116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 1472138568Ssam IEEE80211_NODE_LOCK(nt); 1473138568Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1474127772Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1475127772Ssam ni->ni_chan == chan) { 1476138568Ssam ieee80211_ref_node(ni); /* mark referenced */ 1477138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1478138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1479140766Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1480140766Ssam func, line, 1481138568Ssam#else 1482140766Ssam "%s %p<%s> refcnt %d\n", __func__, 1483138568Ssam#endif 1484140766Ssam ni, ether_sprintf(ni->ni_macaddr), 1485140766Ssam ieee80211_node_refcnt(ni)); 1486116742Ssam break; 1487116742Ssam } 1488116742Ssam } 1489138568Ssam IEEE80211_NODE_UNLOCK(nt); 1490116742Ssam return ni; 1491116742Ssam} 1492116742Ssam 1493138568Ssam/* 1494138568Ssam * Like find but search based on the ssid too. 1495138568Ssam */ 1496138568Ssamstruct ieee80211_node * 1497138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1498138568Ssamieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt, 1499138568Ssam const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid, 1500138568Ssam const char *func, int line) 1501138568Ssam#else 1502138568Ssamieee80211_find_node_with_ssid(struct ieee80211_node_table *nt, 1503138568Ssam const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid) 1504138568Ssam#endif 1505138568Ssam{ 1506147118Ssam#define MATCH_SSID(ni, ssid, ssidlen) \ 1507147118Ssam (ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0) 1508147118Ssam static const u_int8_t zeromac[IEEE80211_ADDR_LEN]; 1509138568Ssam struct ieee80211com *ic = nt->nt_ic; 1510138568Ssam struct ieee80211_node *ni; 1511138568Ssam int hash; 1512138568Ssam 1513138568Ssam IEEE80211_NODE_LOCK(nt); 1514147118Ssam /* 1515147118Ssam * A mac address that is all zero means match only the ssid; 1516147118Ssam * otherwise we must match both. 1517147118Ssam */ 1518147118Ssam if (IEEE80211_ADDR_EQ(macaddr, zeromac)) { 1519147118Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1520147118Ssam if (MATCH_SSID(ni, ssid, ssidlen)) 1521147118Ssam break; 1522147118Ssam } 1523147118Ssam } else { 1524147118Ssam hash = IEEE80211_NODE_HASH(macaddr); 1525147118Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1526147118Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1527147118Ssam MATCH_SSID(ni, ssid, ssidlen)) 1528147118Ssam break; 1529147118Ssam } 1530147118Ssam } 1531147118Ssam if (ni != NULL) { 1532147118Ssam ieee80211_ref_node(ni); /* mark referenced */ 1533147118Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1534138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1535147118Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1536147118Ssam func, line, 1537138568Ssam#else 1538147118Ssam "%s %p<%s> refcnt %d\n", __func__, 1539138568Ssam#endif 1540147118Ssam ni, ether_sprintf(ni->ni_macaddr), 1541147118Ssam ieee80211_node_refcnt(ni)); 1542138568Ssam } 1543138568Ssam IEEE80211_NODE_UNLOCK(nt); 1544138568Ssam return ni; 1545147118Ssam#undef MATCH_SSID 1546138568Ssam} 1547138568Ssam 1548116742Ssamstatic void 1549138568Ssam_ieee80211_free_node(struct ieee80211_node *ni) 1550116742Ssam{ 1551138568Ssam struct ieee80211com *ic = ni->ni_ic; 1552138568Ssam struct ieee80211_node_table *nt = ni->ni_table; 1553119150Ssam 1554138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1555140766Ssam "%s %p<%s> in %s table\n", __func__, ni, 1556140766Ssam ether_sprintf(ni->ni_macaddr), 1557138568Ssam nt != NULL ? nt->nt_name : "<gone>"); 1558138568Ssam 1559138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1560138568Ssam if (nt != NULL) { 1561138568Ssam TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1562138568Ssam LIST_REMOVE(ni, ni_hash); 1563138568Ssam } 1564138568Ssam ic->ic_node_free(ni); 1565116742Ssam} 1566116742Ssam 1567116742Ssamvoid 1568138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1569138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line) 1570138568Ssam#else 1571138568Ssamieee80211_free_node(struct ieee80211_node *ni) 1572138568Ssam#endif 1573116742Ssam{ 1574138568Ssam struct ieee80211_node_table *nt = ni->ni_table; 1575119150Ssam 1576138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1577140454Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1578140766Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 1579138568Ssam ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1580138568Ssam#endif 1581148863Ssam if (nt != NULL) { 1582148863Ssam IEEE80211_NODE_LOCK(nt); 1583148863Ssam if (ieee80211_node_dectestref(ni)) { 1584148863Ssam /* 1585148863Ssam * Last reference, reclaim state. 1586148863Ssam */ 1587138568Ssam _ieee80211_free_node(ni); 1588148863Ssam } else if (ieee80211_node_refcnt(ni) == 1 && 1589148863Ssam nt->nt_keyixmap != NULL) { 1590148863Ssam ieee80211_keyix keyix; 1591148863Ssam /* 1592148863Ssam * Check for a last reference in the key mapping table. 1593148863Ssam */ 1594148863Ssam keyix = ni->ni_ucastkey.wk_rxkeyix; 1595148863Ssam if (keyix < nt->nt_keyixmax && 1596148863Ssam nt->nt_keyixmap[keyix] == ni) { 1597148863Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1598148863Ssam "%s: %p<%s> clear key map entry", __func__, 1599148863Ssam ni, ether_sprintf(ni->ni_macaddr)); 1600148863Ssam nt->nt_keyixmap[keyix] = NULL; 1601148863Ssam ieee80211_node_decref(ni); /* XXX needed? */ 1602148863Ssam _ieee80211_free_node(ni); 1603148863Ssam } 1604148863Ssam } 1605148863Ssam IEEE80211_NODE_UNLOCK(nt); 1606148863Ssam } else { 1607148863Ssam if (ieee80211_node_dectestref(ni)) 1608138568Ssam _ieee80211_free_node(ni); 1609116742Ssam } 1610116742Ssam} 1611116742Ssam 1612138568Ssam/* 1613148863Ssam * Reclaim a unicast key and clear any key cache state. 1614148863Ssam */ 1615148863Ssamint 1616148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni) 1617148863Ssam{ 1618148863Ssam struct ieee80211com *ic = ni->ni_ic; 1619148863Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 1620148863Ssam struct ieee80211_node *nikey; 1621148863Ssam ieee80211_keyix keyix; 1622148863Ssam int isowned, status; 1623148863Ssam 1624148863Ssam /* 1625148863Ssam * NB: We must beware of LOR here; deleting the key 1626148863Ssam * can cause the crypto layer to block traffic updates 1627148863Ssam * which can generate a LOR against the node table lock; 1628148863Ssam * grab it here and stash the key index for our use below. 1629148863Ssam * 1630148863Ssam * Must also beware of recursion on the node table lock. 1631148863Ssam * When called from node_cleanup we may already have 1632148863Ssam * the node table lock held. Unfortunately there's no 1633148863Ssam * way to separate out this path so we must do this 1634148863Ssam * conditionally. 1635148863Ssam */ 1636148863Ssam isowned = IEEE80211_NODE_IS_LOCKED(nt); 1637148863Ssam if (!isowned) 1638148863Ssam IEEE80211_NODE_LOCK(nt); 1639148863Ssam keyix = ni->ni_ucastkey.wk_rxkeyix; 1640148863Ssam status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey); 1641148863Ssam if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) { 1642148863Ssam nikey = nt->nt_keyixmap[keyix]; 1643148863Ssam nt->nt_keyixmap[keyix] = NULL;; 1644148863Ssam } else 1645148863Ssam nikey = NULL; 1646148863Ssam if (!isowned) 1647148863Ssam IEEE80211_NODE_UNLOCK(&ic->ic_sta); 1648148863Ssam 1649148863Ssam if (nikey != NULL) { 1650148863Ssam KASSERT(nikey == ni, 1651148863Ssam ("key map out of sync, ni %p nikey %p", ni, nikey)); 1652148863Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1653148863Ssam "%s: delete key map entry %p<%s> refcnt %d\n", 1654148863Ssam __func__, ni, ether_sprintf(ni->ni_macaddr), 1655148863Ssam ieee80211_node_refcnt(ni)-1); 1656148863Ssam ieee80211_free_node(ni); 1657148863Ssam } 1658148863Ssam return status; 1659148863Ssam} 1660148863Ssam 1661148863Ssam/* 1662138568Ssam * Reclaim a node. If this is the last reference count then 1663138568Ssam * do the normal free work. Otherwise remove it from the node 1664138568Ssam * table and mark it gone by clearing the back-reference. 1665138568Ssam */ 1666138568Ssamstatic void 1667138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1668116742Ssam{ 1669148863Ssam ieee80211_keyix keyix; 1670138568Ssam 1671148863Ssam IEEE80211_NODE_LOCK_ASSERT(nt); 1672148863Ssam 1673140766Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1674140766Ssam "%s: remove %p<%s> from %s table, refcnt %d\n", 1675140766Ssam __func__, ni, ether_sprintf(ni->ni_macaddr), 1676140766Ssam nt->nt_name, ieee80211_node_refcnt(ni)-1); 1677148863Ssam /* 1678148863Ssam * Clear any entry in the unicast key mapping table. 1679148863Ssam * We need to do it here so rx lookups don't find it 1680148863Ssam * in the mapping table even if it's not in the hash 1681148863Ssam * table. We cannot depend on the mapping table entry 1682148863Ssam * being cleared because the node may not be free'd. 1683148863Ssam */ 1684148863Ssam keyix = ni->ni_ucastkey.wk_rxkeyix; 1685148863Ssam if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax && 1686148863Ssam nt->nt_keyixmap[keyix] == ni) { 1687148863Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1688148863Ssam "%s: %p<%s> clear key map entry\n", 1689148863Ssam __func__, ni, ether_sprintf(ni->ni_macaddr)); 1690148863Ssam nt->nt_keyixmap[keyix] = NULL; 1691148863Ssam ieee80211_node_decref(ni); /* NB: don't need free */ 1692148863Ssam } 1693138568Ssam if (!ieee80211_node_dectestref(ni)) { 1694138568Ssam /* 1695138568Ssam * Other references are present, just remove the 1696138568Ssam * node from the table so it cannot be found. When 1697138568Ssam * the references are dropped storage will be 1698140753Ssam * reclaimed. 1699138568Ssam */ 1700138568Ssam TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1701138568Ssam LIST_REMOVE(ni, ni_hash); 1702138568Ssam ni->ni_table = NULL; /* clear reference */ 1703138568Ssam } else 1704138568Ssam _ieee80211_free_node(ni); 1705138568Ssam} 1706138568Ssam 1707138568Ssamstatic void 1708138568Ssamieee80211_free_allnodes_locked(struct ieee80211_node_table *nt) 1709138568Ssam{ 1710138568Ssam struct ieee80211com *ic = nt->nt_ic; 1711116742Ssam struct ieee80211_node *ni; 1712116742Ssam 1713138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1714138568Ssam "%s: free all nodes in %s table\n", __func__, nt->nt_name); 1715138568Ssam 1716138568Ssam while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) { 1717138568Ssam if (ni->ni_associd != 0) { 1718138568Ssam if (ic->ic_auth->ia_node_leave != NULL) 1719138568Ssam ic->ic_auth->ia_node_leave(ic, ni); 1720138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1721138568Ssam } 1722138568Ssam node_reclaim(nt, ni); 1723138568Ssam } 1724138568Ssam ieee80211_reset_erp(ic); 1725116742Ssam} 1726116742Ssam 1727138568Ssamstatic void 1728138568Ssamieee80211_free_allnodes(struct ieee80211_node_table *nt) 1729138568Ssam{ 1730138568Ssam 1731138568Ssam IEEE80211_NODE_LOCK(nt); 1732138568Ssam ieee80211_free_allnodes_locked(nt); 1733138568Ssam IEEE80211_NODE_UNLOCK(nt); 1734138568Ssam} 1735138568Ssam 1736120483Ssam/* 1737138568Ssam * Timeout entries in the scan cache. 1738138568Ssam */ 1739138568Ssamstatic void 1740138568Ssamieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt) 1741138568Ssam{ 1742138568Ssam struct ieee80211com *ic = nt->nt_ic; 1743138568Ssam struct ieee80211_node *ni, *tni; 1744138568Ssam 1745138568Ssam IEEE80211_NODE_LOCK(nt); 1746138568Ssam ni = ic->ic_bss; 1747138568Ssam /* XXX belongs elsewhere */ 1748138568Ssam if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) { 1749138568Ssam m_freem(ni->ni_rxfrag[0]); 1750138568Ssam ni->ni_rxfrag[0] = NULL; 1751138568Ssam } 1752138568Ssam TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) { 1753138568Ssam if (ni->ni_inact && --ni->ni_inact == 0) { 1754138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1755138568Ssam "[%s] scan candidate purged from cache " 1756138568Ssam "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr), 1757140766Ssam ieee80211_node_refcnt(ni)); 1758138568Ssam node_reclaim(nt, ni); 1759138568Ssam } 1760138568Ssam } 1761138568Ssam IEEE80211_NODE_UNLOCK(nt); 1762138568Ssam 1763138568Ssam nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1764138568Ssam} 1765138568Ssam 1766138568Ssam/* 1767138568Ssam * Timeout inactive stations and do related housekeeping. 1768138568Ssam * Note that we cannot hold the node lock while sending a 1769138568Ssam * frame as this would lead to a LOR. Instead we use a 1770138568Ssam * generation number to mark nodes that we've scanned and 1771138568Ssam * drop the lock and restart a scan if we have to time out 1772138568Ssam * a node. Since we are single-threaded by virtue of 1773120483Ssam * controlling the inactivity timer we can be sure this will 1774120483Ssam * process each node only once. 1775120483Ssam */ 1776138568Ssamstatic void 1777138568Ssamieee80211_timeout_stations(struct ieee80211_node_table *nt) 1778116742Ssam{ 1779138568Ssam struct ieee80211com *ic = nt->nt_ic; 1780120483Ssam struct ieee80211_node *ni; 1781138568Ssam u_int gen; 1782148320Ssam int isadhoc; 1783116742Ssam 1784148320Ssam isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS || 1785148320Ssam ic->ic_opmode == IEEE80211_M_AHDEMO); 1786138568Ssam IEEE80211_SCAN_LOCK(nt); 1787154532Ssam gen = ++nt->nt_scangen; 1788138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1789140766Ssam "%s: %s scangen %u\n", __func__, nt->nt_name, gen); 1790120483Ssamrestart: 1791138568Ssam IEEE80211_NODE_LOCK(nt); 1792138568Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1793120483Ssam if (ni->ni_scangen == gen) /* previously handled */ 1794120483Ssam continue; 1795120483Ssam ni->ni_scangen = gen; 1796138568Ssam /* 1797147788Ssam * Ignore entries for which have yet to receive an 1798147788Ssam * authentication frame. These are transient and 1799147788Ssam * will be reclaimed when the last reference to them 1800147788Ssam * goes away (when frame xmits complete). 1801147788Ssam */ 1802148323Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1803148323Ssam (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 1804147788Ssam continue; 1805147788Ssam /* 1806138568Ssam * Free fragment if not needed anymore 1807138568Ssam * (last fragment older than 1s). 1808138568Ssam * XXX doesn't belong here 1809138568Ssam */ 1810138568Ssam if (ni->ni_rxfrag[0] != NULL && 1811138568Ssam ticks > ni->ni_rxfragstamp + hz) { 1812138568Ssam m_freem(ni->ni_rxfrag[0]); 1813138568Ssam ni->ni_rxfrag[0] = NULL; 1814138568Ssam } 1815140498Ssam /* 1816140498Ssam * Special case ourself; we may be idle for extended periods 1817140498Ssam * of time and regardless reclaiming our state is wrong. 1818140498Ssam */ 1819140498Ssam if (ni == ic->ic_bss) 1820140498Ssam continue; 1821138568Ssam ni->ni_inact--; 1822148320Ssam if (ni->ni_associd != 0 || isadhoc) { 1823119150Ssam /* 1824138568Ssam * Age frames on the power save queue. The 1825138568Ssam * aging interval is 4 times the listen 1826138568Ssam * interval specified by the station. This 1827138568Ssam * number is factored into the age calculations 1828138568Ssam * when the frame is placed on the queue. We 1829138568Ssam * store ages as time differences we can check 1830138568Ssam * and/or adjust only the head of the list. 1831138568Ssam */ 1832138568Ssam if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) { 1833138568Ssam struct mbuf *m; 1834138568Ssam int discard = 0; 1835138568Ssam 1836138568Ssam IEEE80211_NODE_SAVEQ_LOCK(ni); 1837138568Ssam while (IF_POLL(&ni->ni_savedq, m) != NULL && 1838138568Ssam M_AGE_GET(m) < IEEE80211_INACT_WAIT) { 1839138568SsamIEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/ 1840138568Ssam _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m); 1841138568Ssam m_freem(m); 1842138568Ssam discard++; 1843138568Ssam } 1844138568Ssam if (m != NULL) 1845138568Ssam M_AGE_SUB(m, IEEE80211_INACT_WAIT); 1846138568Ssam IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1847138568Ssam 1848138568Ssam if (discard != 0) { 1849138568Ssam IEEE80211_DPRINTF(ic, 1850138568Ssam IEEE80211_MSG_POWER, 1851138568Ssam "[%s] discard %u frames for age\n", 1852138568Ssam ether_sprintf(ni->ni_macaddr), 1853138568Ssam discard); 1854138568Ssam IEEE80211_NODE_STAT_ADD(ni, 1855138568Ssam ps_discard, discard); 1856138568Ssam if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) 1857148304Ssam ic->ic_set_tim(ni, 0); 1858138568Ssam } 1859138568Ssam } 1860138568Ssam /* 1861138568Ssam * Probe the station before time it out. We 1862138568Ssam * send a null data frame which may not be 1863138568Ssam * universally supported by drivers (need it 1864138568Ssam * for ps-poll support so it should be...). 1865138568Ssam */ 1866139528Ssam if (0 < ni->ni_inact && 1867139528Ssam ni->ni_inact <= ic->ic_inact_probe) { 1868148320Ssam IEEE80211_NOTE(ic, 1869148320Ssam IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, 1870148320Ssam ni, "%s", 1871148320Ssam "probe station due to inactivity"); 1872148582Ssam /* 1873148582Ssam * Grab a reference before unlocking the table 1874148582Ssam * so the node cannot be reclaimed before we 1875148582Ssam * send the frame. ieee80211_send_nulldata 1876148582Ssam * understands we've done this and reclaims the 1877148582Ssam * ref for us as needed. 1878148582Ssam */ 1879148582Ssam ieee80211_ref_node(ni); 1880138568Ssam IEEE80211_NODE_UNLOCK(nt); 1881148301Ssam ieee80211_send_nulldata(ni); 1882138568Ssam /* XXX stat? */ 1883138568Ssam goto restart; 1884138568Ssam } 1885138568Ssam } 1886138568Ssam if (ni->ni_inact <= 0) { 1887148320Ssam IEEE80211_NOTE(ic, 1888148320Ssam IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni, 1889148320Ssam "station timed out due to inactivity " 1890148320Ssam "(refcnt %u)", ieee80211_node_refcnt(ni)); 1891138568Ssam /* 1892138568Ssam * Send a deauthenticate frame and drop the station. 1893138568Ssam * This is somewhat complicated due to reference counts 1894138568Ssam * and locking. At this point a station will typically 1895138568Ssam * have a reference count of 1. ieee80211_node_leave 1896138568Ssam * will do a "free" of the node which will drop the 1897138568Ssam * reference count. But in the meantime a reference 1898138568Ssam * wil be held by the deauth frame. The actual reclaim 1899138568Ssam * of the node will happen either after the tx is 1900138568Ssam * completed or by ieee80211_node_leave. 1901120483Ssam * 1902138568Ssam * Separately we must drop the node lock before sending 1903138568Ssam * in case the driver takes a lock, as this will result 1904138568Ssam * in LOR between the node lock and the driver lock. 1905119150Ssam */ 1906138568Ssam IEEE80211_NODE_UNLOCK(nt); 1907138568Ssam if (ni->ni_associd != 0) { 1908138568Ssam IEEE80211_SEND_MGMT(ic, ni, 1909138568Ssam IEEE80211_FC0_SUBTYPE_DEAUTH, 1910138568Ssam IEEE80211_REASON_AUTH_EXPIRE); 1911138568Ssam } 1912138568Ssam ieee80211_node_leave(ic, ni); 1913121180Ssam ic->ic_stats.is_node_timeout++; 1914120483Ssam goto restart; 1915120483Ssam } 1916116742Ssam } 1917138568Ssam IEEE80211_NODE_UNLOCK(nt); 1918138568Ssam 1919138568Ssam IEEE80211_SCAN_UNLOCK(nt); 1920138568Ssam 1921138568Ssam nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1922116742Ssam} 1923116742Ssam 1924116742Ssamvoid 1925138568Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg) 1926116742Ssam{ 1927116742Ssam struct ieee80211_node *ni; 1928138568Ssam u_int gen; 1929116742Ssam 1930138568Ssam IEEE80211_SCAN_LOCK(nt); 1931154532Ssam gen = ++nt->nt_scangen; 1932138568Ssamrestart: 1933138568Ssam IEEE80211_NODE_LOCK(nt); 1934138568Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1935138568Ssam if (ni->ni_scangen != gen) { 1936138568Ssam ni->ni_scangen = gen; 1937138568Ssam (void) ieee80211_ref_node(ni); 1938138568Ssam IEEE80211_NODE_UNLOCK(nt); 1939138568Ssam (*f)(arg, ni); 1940138568Ssam ieee80211_free_node(ni); 1941138568Ssam goto restart; 1942138568Ssam } 1943138568Ssam } 1944138568Ssam IEEE80211_NODE_UNLOCK(nt); 1945138568Ssam 1946138568Ssam IEEE80211_SCAN_UNLOCK(nt); 1947116742Ssam} 1948138568Ssam 1949138568Ssamvoid 1950138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1951138568Ssam{ 1952138568Ssam printf("0x%p: mac %s refcnt %d\n", ni, 1953138568Ssam ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 1954138568Ssam printf("\tscangen %u authmode %u flags 0x%x\n", 1955138568Ssam ni->ni_scangen, ni->ni_authmode, ni->ni_flags); 1956138568Ssam printf("\tassocid 0x%x txpower %u vlan %u\n", 1957138568Ssam ni->ni_associd, ni->ni_txpower, ni->ni_vlan); 1958138568Ssam printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n", 1959138568Ssam ni->ni_txseqs[0], 1960138568Ssam ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT, 1961138568Ssam ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK, 1962138568Ssam ni->ni_rxfragstamp); 1963138568Ssam printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n", 1964138568Ssam ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo); 1965138568Ssam printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n", 1966138568Ssam ether_sprintf(ni->ni_bssid), 1967138568Ssam ni->ni_esslen, ni->ni_essid, 1968138568Ssam ni->ni_chan->ic_freq, ni->ni_chan->ic_flags); 1969138568Ssam printf("\tfails %u inact %u txrate %u\n", 1970138568Ssam ni->ni_fails, ni->ni_inact, ni->ni_txrate); 1971138568Ssam} 1972138568Ssam 1973138568Ssamvoid 1974138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt) 1975138568Ssam{ 1976138568Ssam ieee80211_iterate_nodes(nt, 1977138568Ssam (ieee80211_iter_func *) ieee80211_dump_node, nt); 1978138568Ssam} 1979138568Ssam 1980138568Ssam/* 1981138568Ssam * Handle a station joining an 11g network. 1982138568Ssam */ 1983138568Ssamstatic void 1984138568Ssamieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 1985138568Ssam{ 1986138568Ssam 1987138568Ssam /* 1988138568Ssam * Station isn't capable of short slot time. Bump 1989138568Ssam * the count of long slot time stations and disable 1990138568Ssam * use of short slot time. Note that the actual switch 1991138568Ssam * over to long slot time use may not occur until the 1992138568Ssam * next beacon transmission (per sec. 7.3.1.4 of 11g). 1993138568Ssam */ 1994138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 1995138568Ssam ic->ic_longslotsta++; 1996138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1997138568Ssam "[%s] station needs long slot time, count %d\n", 1998138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 1999138568Ssam /* XXX vap's w/ conflicting needs won't work */ 2000138568Ssam ieee80211_set_shortslottime(ic, 0); 2001138568Ssam } 2002138568Ssam /* 2003138568Ssam * If the new station is not an ERP station 2004138568Ssam * then bump the counter and enable protection 2005138568Ssam * if configured. 2006138568Ssam */ 2007138568Ssam if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) { 2008138568Ssam ic->ic_nonerpsta++; 2009138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2010138568Ssam "[%s] station is !ERP, %d non-ERP stations associated\n", 2011138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 2012138568Ssam /* 2013138568Ssam * If protection is configured, enable it. 2014138568Ssam */ 2015138568Ssam if (ic->ic_protmode != IEEE80211_PROT_NONE) { 2016138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2017138568Ssam "%s: enable use of protection\n", __func__); 2018138568Ssam ic->ic_flags |= IEEE80211_F_USEPROT; 2019138568Ssam } 2020138568Ssam /* 2021138568Ssam * If station does not support short preamble 2022138568Ssam * then we must enable use of Barker preamble. 2023138568Ssam */ 2024138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) { 2025138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2026138568Ssam "[%s] station needs long preamble\n", 2027138568Ssam ether_sprintf(ni->ni_macaddr)); 2028138568Ssam ic->ic_flags |= IEEE80211_F_USEBARKER; 2029138568Ssam ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 2030138568Ssam } 2031153973Ssam if (ic->ic_nonerpsta == 1) 2032153973Ssam ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE; 2033138568Ssam } else 2034138568Ssam ni->ni_flags |= IEEE80211_NODE_ERP; 2035138568Ssam} 2036138568Ssam 2037138568Ssamvoid 2038138568Ssamieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp) 2039138568Ssam{ 2040138568Ssam int newassoc; 2041138568Ssam 2042138568Ssam if (ni->ni_associd == 0) { 2043138568Ssam u_int16_t aid; 2044138568Ssam 2045138568Ssam /* 2046138568Ssam * It would be good to search the bitmap 2047138568Ssam * more efficiently, but this will do for now. 2048138568Ssam */ 2049138568Ssam for (aid = 1; aid < ic->ic_max_aid; aid++) { 2050138568Ssam if (!IEEE80211_AID_ISSET(aid, 2051138568Ssam ic->ic_aid_bitmap)) 2052138568Ssam break; 2053138568Ssam } 2054138568Ssam if (aid >= ic->ic_max_aid) { 2055138568Ssam IEEE80211_SEND_MGMT(ic, ni, resp, 2056138568Ssam IEEE80211_REASON_ASSOC_TOOMANY); 2057138568Ssam ieee80211_node_leave(ic, ni); 2058138568Ssam return; 2059138568Ssam } 2060138568Ssam ni->ni_associd = aid | 0xc000; 2061138568Ssam IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap); 2062138568Ssam ic->ic_sta_assoc++; 2063138568Ssam newassoc = 1; 2064149031Ssam if (ic->ic_curmode == IEEE80211_MODE_11G) 2065138568Ssam ieee80211_node_join_11g(ic, ni); 2066138568Ssam } else 2067138568Ssam newassoc = 0; 2068138568Ssam 2069138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 2070139523Ssam "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n", 2071139523Ssam ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re", 2072139523Ssam IEEE80211_NODE_AID(ni), 2073139523Ssam ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long", 2074139523Ssam ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", 2075139523Ssam ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "", 2076139523Ssam ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "" 2077139523Ssam ); 2078138568Ssam 2079138568Ssam /* give driver a chance to setup state like ni_txrate */ 2080139524Ssam if (ic->ic_newassoc != NULL) 2081148307Ssam ic->ic_newassoc(ni, newassoc); 2082139528Ssam ni->ni_inact_reload = ic->ic_inact_auth; 2083139528Ssam ni->ni_inact = ni->ni_inact_reload; 2084138568Ssam IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS); 2085138568Ssam /* tell the authenticator about new station */ 2086138568Ssam if (ic->ic_auth->ia_node_join != NULL) 2087138568Ssam ic->ic_auth->ia_node_join(ic, ni); 2088138568Ssam ieee80211_notify_node_join(ic, ni, newassoc); 2089138568Ssam} 2090138568Ssam 2091138568Ssam/* 2092138568Ssam * Handle a station leaving an 11g network. 2093138568Ssam */ 2094138568Ssamstatic void 2095138568Ssamieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 2096138568Ssam{ 2097138568Ssam 2098149031Ssam KASSERT(ic->ic_curmode == IEEE80211_MODE_11G, 2099148941Ssam ("not in 11g, bss %u:0x%x, curmode %u", ni->ni_chan->ic_freq, 2100148941Ssam ni->ni_chan->ic_flags, ic->ic_curmode)); 2101138568Ssam 2102138568Ssam /* 2103138568Ssam * If a long slot station do the slot time bookkeeping. 2104138568Ssam */ 2105138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2106138568Ssam KASSERT(ic->ic_longslotsta > 0, 2107138568Ssam ("bogus long slot station count %d", ic->ic_longslotsta)); 2108138568Ssam ic->ic_longslotsta--; 2109138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2110138568Ssam "[%s] long slot time station leaves, count now %d\n", 2111138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 2112138568Ssam if (ic->ic_longslotsta == 0) { 2113138568Ssam /* 2114138568Ssam * Re-enable use of short slot time if supported 2115138568Ssam * and not operating in IBSS mode (per spec). 2116138568Ssam */ 2117138568Ssam if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 2118138568Ssam ic->ic_opmode != IEEE80211_M_IBSS) { 2119138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2120138568Ssam "%s: re-enable use of short slot time\n", 2121138568Ssam __func__); 2122138568Ssam ieee80211_set_shortslottime(ic, 1); 2123138568Ssam } 2124138568Ssam } 2125138568Ssam } 2126138568Ssam /* 2127138568Ssam * If a non-ERP station do the protection-related bookkeeping. 2128138568Ssam */ 2129138568Ssam if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) { 2130138568Ssam KASSERT(ic->ic_nonerpsta > 0, 2131138568Ssam ("bogus non-ERP station count %d", ic->ic_nonerpsta)); 2132138568Ssam ic->ic_nonerpsta--; 2133138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2134138568Ssam "[%s] non-ERP station leaves, count now %d\n", 2135138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 2136138568Ssam if (ic->ic_nonerpsta == 0) { 2137138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2138138568Ssam "%s: disable use of protection\n", __func__); 2139138568Ssam ic->ic_flags &= ~IEEE80211_F_USEPROT; 2140138568Ssam /* XXX verify mode? */ 2141138568Ssam if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) { 2142138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2143138568Ssam "%s: re-enable use of short preamble\n", 2144138568Ssam __func__); 2145138568Ssam ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 2146138568Ssam ic->ic_flags &= ~IEEE80211_F_USEBARKER; 2147138568Ssam } 2148153973Ssam ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE; 2149138568Ssam } 2150138568Ssam } 2151138568Ssam} 2152138568Ssam 2153138568Ssam/* 2154138568Ssam * Handle bookkeeping for station deauthentication/disassociation 2155138568Ssam * when operating as an ap. 2156138568Ssam */ 2157138568Ssamvoid 2158138568Ssamieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 2159138568Ssam{ 2160140499Ssam struct ieee80211_node_table *nt = ni->ni_table; 2161138568Ssam 2162138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 2163138568Ssam "[%s] station with aid %d leaves\n", 2164138568Ssam ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni)); 2165138568Ssam 2166138568Ssam KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 2167138568Ssam ic->ic_opmode == IEEE80211_M_IBSS || 2168138568Ssam ic->ic_opmode == IEEE80211_M_AHDEMO, 2169138568Ssam ("unexpected operating mode %u", ic->ic_opmode)); 2170138568Ssam /* 2171138568Ssam * If node wasn't previously associated all 2172138568Ssam * we need to do is reclaim the reference. 2173138568Ssam */ 2174138568Ssam /* XXX ibss mode bypasses 11g and notification */ 2175138568Ssam if (ni->ni_associd == 0) 2176138568Ssam goto done; 2177138568Ssam /* 2178138568Ssam * Tell the authenticator the station is leaving. 2179138568Ssam * Note that we must do this before yanking the 2180138568Ssam * association id as the authenticator uses the 2181138568Ssam * associd to locate it's state block. 2182138568Ssam */ 2183138568Ssam if (ic->ic_auth->ia_node_leave != NULL) 2184138568Ssam ic->ic_auth->ia_node_leave(ic, ni); 2185138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 2186138568Ssam ni->ni_associd = 0; 2187138568Ssam ic->ic_sta_assoc--; 2188138568Ssam 2189149031Ssam if (ic->ic_curmode == IEEE80211_MODE_11G) 2190138568Ssam ieee80211_node_leave_11g(ic, ni); 2191138568Ssam /* 2192138568Ssam * Cleanup station state. In particular clear various 2193138568Ssam * state that might otherwise be reused if the node 2194138568Ssam * is reused before the reference count goes to zero 2195138568Ssam * (and memory is reclaimed). 2196138568Ssam */ 2197138568Ssam ieee80211_sta_leave(ic, ni); 2198138568Ssamdone: 2199140499Ssam /* 2200140499Ssam * Remove the node from any table it's recorded in and 2201140499Ssam * drop the caller's reference. Removal from the table 2202140499Ssam * is important to insure the node is not reprocessed 2203140499Ssam * for inactivity. 2204140499Ssam */ 2205140499Ssam if (nt != NULL) { 2206140499Ssam IEEE80211_NODE_LOCK(nt); 2207140499Ssam node_reclaim(nt, ni); 2208140499Ssam IEEE80211_NODE_UNLOCK(nt); 2209140499Ssam } else 2210140499Ssam ieee80211_free_node(ni); 2211138568Ssam} 2212138568Ssam 2213138568Ssamu_int8_t 2214138568Ssamieee80211_getrssi(struct ieee80211com *ic) 2215138568Ssam{ 2216138568Ssam#define NZ(x) ((x) == 0 ? 1 : (x)) 2217140753Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 2218138568Ssam u_int32_t rssi_samples, rssi_total; 2219138568Ssam struct ieee80211_node *ni; 2220138568Ssam 2221138568Ssam rssi_total = 0; 2222138568Ssam rssi_samples = 0; 2223138568Ssam switch (ic->ic_opmode) { 2224138568Ssam case IEEE80211_M_IBSS: /* average of all ibss neighbors */ 2225138568Ssam /* XXX locking */ 2226140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 2227138568Ssam if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) { 2228138568Ssam rssi_samples++; 2229138568Ssam rssi_total += ic->ic_node_getrssi(ni); 2230138568Ssam } 2231138568Ssam break; 2232138568Ssam case IEEE80211_M_AHDEMO: /* average of all neighbors */ 2233138568Ssam /* XXX locking */ 2234140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 2235138568Ssam rssi_samples++; 2236138568Ssam rssi_total += ic->ic_node_getrssi(ni); 2237138568Ssam } 2238138568Ssam break; 2239138568Ssam case IEEE80211_M_HOSTAP: /* average of all associated stations */ 2240138568Ssam /* XXX locking */ 2241140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 2242138568Ssam if (IEEE80211_AID(ni->ni_associd) != 0) { 2243138568Ssam rssi_samples++; 2244138568Ssam rssi_total += ic->ic_node_getrssi(ni); 2245138568Ssam } 2246138568Ssam break; 2247138568Ssam case IEEE80211_M_MONITOR: /* XXX */ 2248138568Ssam case IEEE80211_M_STA: /* use stats from associated ap */ 2249138568Ssam default: 2250138568Ssam if (ic->ic_bss != NULL) 2251138568Ssam rssi_total = ic->ic_node_getrssi(ic->ic_bss); 2252138568Ssam rssi_samples = 1; 2253138568Ssam break; 2254138568Ssam } 2255138568Ssam return rssi_total / NZ(rssi_samples); 2256138568Ssam#undef NZ 2257138568Ssam} 2258138568Ssam 2259138568Ssam/* 2260138568Ssam * Indicate whether there are frames queued for a station in power-save mode. 2261138568Ssam */ 2262138568Ssamstatic void 2263148304Ssamieee80211_set_tim(struct ieee80211_node *ni, int set) 2264138568Ssam{ 2265148304Ssam struct ieee80211com *ic = ni->ni_ic; 2266138568Ssam u_int16_t aid; 2267138568Ssam 2268138568Ssam KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 2269138568Ssam ic->ic_opmode == IEEE80211_M_IBSS, 2270138568Ssam ("operating mode %u", ic->ic_opmode)); 2271138568Ssam 2272138568Ssam aid = IEEE80211_AID(ni->ni_associd); 2273138568Ssam KASSERT(aid < ic->ic_max_aid, 2274138568Ssam ("bogus aid %u, max %u", aid, ic->ic_max_aid)); 2275138568Ssam 2276138568Ssam IEEE80211_BEACON_LOCK(ic); 2277138568Ssam if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) { 2278138568Ssam if (set) { 2279138568Ssam setbit(ic->ic_tim_bitmap, aid); 2280138568Ssam ic->ic_ps_pending++; 2281138568Ssam } else { 2282138568Ssam clrbit(ic->ic_tim_bitmap, aid); 2283138568Ssam ic->ic_ps_pending--; 2284138568Ssam } 2285138568Ssam ic->ic_flags |= IEEE80211_F_TIMUPDATE; 2286138568Ssam } 2287138568Ssam IEEE80211_BEACON_UNLOCK(ic); 2288138568Ssam} 2289138568Ssam 2290138568Ssam/* 2291138568Ssam * Node table support. 2292138568Ssam */ 2293138568Ssam 2294138568Ssamstatic void 2295138568Ssamieee80211_node_table_init(struct ieee80211com *ic, 2296138568Ssam struct ieee80211_node_table *nt, 2297148863Ssam const char *name, int inact, int keyixmax, 2298138568Ssam void (*timeout)(struct ieee80211_node_table *)) 2299138568Ssam{ 2300138568Ssam 2301138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 2302138568Ssam "%s %s table, inact %u\n", __func__, name, inact); 2303138568Ssam 2304138568Ssam nt->nt_ic = ic; 2305138568Ssam /* XXX need unit */ 2306138568Ssam IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname); 2307138568Ssam IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname); 2308138568Ssam TAILQ_INIT(&nt->nt_node); 2309138568Ssam nt->nt_name = name; 2310138568Ssam nt->nt_scangen = 1; 2311138568Ssam nt->nt_inact_init = inact; 2312138568Ssam nt->nt_timeout = timeout; 2313148863Ssam nt->nt_keyixmax = keyixmax; 2314148863Ssam if (nt->nt_keyixmax > 0) { 2315148863Ssam MALLOC(nt->nt_keyixmap, struct ieee80211_node **, 2316148863Ssam keyixmax * sizeof(struct ieee80211_node *), 2317148863Ssam M_80211_NODE, M_NOWAIT | M_ZERO); 2318148863Ssam if (nt->nt_keyixmap == NULL) 2319148863Ssam if_printf(ic->ic_ifp, 2320148863Ssam "Cannot allocate key index map with %u entries\n", 2321148863Ssam keyixmax); 2322148863Ssam } else 2323148863Ssam nt->nt_keyixmap = NULL; 2324138568Ssam} 2325138568Ssam 2326138568Ssamvoid 2327138568Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt) 2328138568Ssam{ 2329138568Ssam 2330138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 2331138568Ssam "%s %s table\n", __func__, nt->nt_name); 2332138568Ssam 2333138568Ssam IEEE80211_NODE_LOCK(nt); 2334138568Ssam nt->nt_inact_timer = 0; 2335138568Ssam ieee80211_free_allnodes_locked(nt); 2336138568Ssam IEEE80211_NODE_UNLOCK(nt); 2337138568Ssam} 2338138568Ssam 2339138568Ssamstatic void 2340138568Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt) 2341138568Ssam{ 2342138568Ssam 2343138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 2344138568Ssam "%s %s table\n", __func__, nt->nt_name); 2345138568Ssam 2346148863Ssam IEEE80211_NODE_LOCK(nt); 2347138568Ssam ieee80211_free_allnodes_locked(nt); 2348148863Ssam if (nt->nt_keyixmap != NULL) { 2349148863Ssam /* XXX verify all entries are NULL */ 2350148863Ssam int i; 2351148863Ssam for (i = 0; i < nt->nt_keyixmax; i++) 2352148863Ssam if (nt->nt_keyixmap[i] != NULL) 2353148863Ssam printf("%s: %s[%u] still active\n", __func__, 2354148863Ssam nt->nt_name, i); 2355148863Ssam FREE(nt->nt_keyixmap, M_80211_NODE); 2356148863Ssam nt->nt_keyixmap = NULL; 2357148863Ssam } 2358138568Ssam IEEE80211_SCAN_LOCK_DESTROY(nt); 2359138568Ssam IEEE80211_NODE_LOCK_DESTROY(nt); 2360138568Ssam} 2361