ieee80211_node.c revision 148299
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 148299 2005-07-22 17:29:03Z sam $"); 35116742Ssam 36116742Ssam#include <sys/param.h> 37116742Ssam#include <sys/systm.h> 38116742Ssam#include <sys/mbuf.h> 39116742Ssam#include <sys/malloc.h> 40116742Ssam#include <sys/kernel.h> 41138568Ssam 42116742Ssam#include <sys/socket.h> 43116742Ssam 44116742Ssam#include <net/if.h> 45116742Ssam#include <net/if_media.h> 46116742Ssam#include <net/ethernet.h> 47116742Ssam 48116742Ssam#include <net80211/ieee80211_var.h> 49116742Ssam 50116742Ssam#include <net/bpf.h> 51116742Ssam 52147221Ssam/* 53147221Ssam * Association id's are managed with a bit vector. 54147221Ssam */ 55147221Ssam#define IEEE80211_AID_SET(b, w) \ 56147221Ssam ((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32))) 57147221Ssam#define IEEE80211_AID_CLR(b, w) \ 58147221Ssam ((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32))) 59147221Ssam#define IEEE80211_AID_ISSET(b, w) \ 60147221Ssam ((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32))) 61147221Ssam 62138568Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211_node_table *); 63138568Ssamstatic void node_cleanup(struct ieee80211_node *); 64138568Ssamstatic void node_free(struct ieee80211_node *); 65138568Ssamstatic u_int8_t node_getrssi(const struct ieee80211_node *); 66116742Ssam 67138568Ssamstatic void ieee80211_setup_node(struct ieee80211_node_table *, 68138568Ssam struct ieee80211_node *, const u_int8_t *); 69138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *); 70138568Ssamstatic void ieee80211_free_allnodes(struct ieee80211_node_table *); 71120104Ssam 72138568Ssamstatic void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *); 73138568Ssamstatic void ieee80211_timeout_stations(struct ieee80211_node_table *); 74116742Ssam 75138568Ssamstatic void ieee80211_set_tim(struct ieee80211com *, 76138568Ssam struct ieee80211_node *, int set); 77138568Ssam 78138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic, 79138568Ssam struct ieee80211_node_table *nt, const char *name, int inact, 80138568Ssam void (*timeout)(struct ieee80211_node_table *)); 81138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt); 82138568Ssam 83127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state"); 84120481Ssam 85116742Ssamvoid 86138568Ssamieee80211_node_attach(struct ieee80211com *ic) 87116742Ssam{ 88116742Ssam 89140753Ssam ieee80211_node_table_init(ic, &ic->ic_sta, "station", 90140753Ssam IEEE80211_INACT_INIT, ieee80211_timeout_stations); 91138568Ssam ieee80211_node_table_init(ic, &ic->ic_scan, "scan", 92138568Ssam IEEE80211_INACT_SCAN, ieee80211_timeout_scan_candidates); 93138568Ssam 94138568Ssam ic->ic_node_alloc = node_alloc; 95138568Ssam ic->ic_node_free = node_free; 96138568Ssam ic->ic_node_cleanup = node_cleanup; 97138568Ssam ic->ic_node_getrssi = node_getrssi; 98138568Ssam 99138568Ssam /* default station inactivity timer setings */ 100138568Ssam ic->ic_inact_init = IEEE80211_INACT_INIT; 101138568Ssam ic->ic_inact_auth = IEEE80211_INACT_AUTH; 102138568Ssam ic->ic_inact_run = IEEE80211_INACT_RUN; 103138568Ssam ic->ic_inact_probe = IEEE80211_INACT_PROBE; 104138568Ssam 105138568Ssam /* XXX defer */ 106138568Ssam if (ic->ic_max_aid == 0) 107138568Ssam ic->ic_max_aid = IEEE80211_AID_DEF; 108138568Ssam else if (ic->ic_max_aid > IEEE80211_AID_MAX) 109138568Ssam ic->ic_max_aid = IEEE80211_AID_MAX; 110138568Ssam MALLOC(ic->ic_aid_bitmap, u_int32_t *, 111138568Ssam howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t), 112138568Ssam M_DEVBUF, M_NOWAIT | M_ZERO); 113138568Ssam if (ic->ic_aid_bitmap == NULL) { 114138568Ssam /* XXX no way to recover */ 115138568Ssam printf("%s: no memory for AID bitmap!\n", __func__); 116138568Ssam ic->ic_max_aid = 0; 117138568Ssam } 118138568Ssam 119138568Ssam /* XXX defer until using hostap/ibss mode */ 120138568Ssam ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t); 121138568Ssam MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len, 122138568Ssam M_DEVBUF, M_NOWAIT | M_ZERO); 123138568Ssam if (ic->ic_tim_bitmap == NULL) { 124138568Ssam /* XXX no way to recover */ 125138568Ssam printf("%s: no memory for TIM bitmap!\n", __func__); 126138568Ssam } 127138568Ssam ic->ic_set_tim = ieee80211_set_tim; /* NB: driver should override */ 128118887Ssam} 129118887Ssam 130118887Ssamvoid 131138568Ssamieee80211_node_lateattach(struct ieee80211com *ic) 132118887Ssam{ 133127766Ssam struct ieee80211_node *ni; 134138568Ssam struct ieee80211_rsnparms *rsn; 135118887Ssam 136138568Ssam ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr); 137127766Ssam KASSERT(ni != NULL, ("unable to setup inital BSS node")); 138138568Ssam /* 139138568Ssam * Setup "global settings" in the bss node so that 140138568Ssam * each new station automatically inherits them. 141138568Ssam */ 142138568Ssam rsn = &ni->ni_rsn; 143138568Ssam /* WEP, TKIP, and AES-CCM are always supported */ 144138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP; 145138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP; 146138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM; 147138568Ssam if (ic->ic_caps & IEEE80211_C_AES) 148138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB; 149138568Ssam if (ic->ic_caps & IEEE80211_C_CKIP) 150138568Ssam rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP; 151138568Ssam /* 152138568Ssam * Default unicast cipher to WEP for 802.1x use. If 153138568Ssam * WPA is enabled the management code will set these 154138568Ssam * values to reflect. 155138568Ssam */ 156138568Ssam rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP; 157138568Ssam rsn->rsn_ucastkeylen = 104 / NBBY; 158138568Ssam /* 159138568Ssam * WPA says the multicast cipher is the lowest unicast 160138568Ssam * cipher supported. But we skip WEP which would 161138568Ssam * otherwise be used based on this criteria. 162138568Ssam */ 163138568Ssam rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP; 164138568Ssam rsn->rsn_mcastkeylen = 128 / NBBY; 165138568Ssam 166138568Ssam /* 167138568Ssam * We support both WPA-PSK and 802.1x; the one used 168138568Ssam * is determined by the authentication mode and the 169138568Ssam * setting of the PSK state. 170138568Ssam */ 171138568Ssam rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK; 172138568Ssam rsn->rsn_keymgmt = WPA_ASE_8021X_PSK; 173138568Ssam 174138568Ssam ic->ic_bss = ieee80211_ref_node(ni); /* hold reference */ 175138568Ssam ic->ic_auth = ieee80211_authenticator_get(ni->ni_authmode); 176116742Ssam} 177116742Ssam 178116742Ssamvoid 179138568Ssamieee80211_node_detach(struct ieee80211com *ic) 180116742Ssam{ 181116742Ssam 182138568Ssam if (ic->ic_bss != NULL) { 183138568Ssam ieee80211_free_node(ic->ic_bss); 184138568Ssam ic->ic_bss = NULL; 185138568Ssam } 186138568Ssam ieee80211_node_table_cleanup(&ic->ic_scan); 187140753Ssam ieee80211_node_table_cleanup(&ic->ic_sta); 188138568Ssam if (ic->ic_aid_bitmap != NULL) { 189138568Ssam FREE(ic->ic_aid_bitmap, M_DEVBUF); 190138568Ssam ic->ic_aid_bitmap = NULL; 191138568Ssam } 192138568Ssam if (ic->ic_tim_bitmap != NULL) { 193138568Ssam FREE(ic->ic_tim_bitmap, M_DEVBUF); 194138568Ssam ic->ic_tim_bitmap = NULL; 195138568Ssam } 196116742Ssam} 197116742Ssam 198138568Ssam/* 199138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator. 200138568Ssam */ 201138568Ssam 202138568Ssamvoid 203138568Ssamieee80211_node_authorize(struct ieee80211com *ic, struct ieee80211_node *ni) 204138568Ssam{ 205138568Ssam ni->ni_flags |= IEEE80211_NODE_AUTH; 206139528Ssam ni->ni_inact_reload = ic->ic_inact_run; 207138568Ssam} 208138568Ssam 209138568Ssamvoid 210138568Ssamieee80211_node_unauthorize(struct ieee80211com *ic, struct ieee80211_node *ni) 211138568Ssam{ 212138568Ssam ni->ni_flags &= ~IEEE80211_NODE_AUTH; 213138568Ssam} 214138568Ssam 215116742Ssam/* 216138568Ssam * Set/change the channel. The rate set is also updated as 217138568Ssam * to insure a consistent view by drivers. 218138568Ssam */ 219138568Ssamstatic __inline void 220138568Ssamieee80211_set_chan(struct ieee80211com *ic, 221138568Ssam struct ieee80211_node *ni, struct ieee80211_channel *chan) 222138568Ssam{ 223138568Ssam ni->ni_chan = chan; 224138568Ssam ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)]; 225138568Ssam} 226138568Ssam 227138568Ssam/* 228116742Ssam * AP scanning support. 229116742Ssam */ 230116742Ssam 231138568Ssam#ifdef IEEE80211_DEBUG 232138568Ssamstatic void 233138568Ssamdump_chanlist(const u_char chans[]) 234138568Ssam{ 235138568Ssam const char *sep; 236138568Ssam int i; 237138568Ssam 238138568Ssam sep = " "; 239138568Ssam for (i = 0; i < IEEE80211_CHAN_MAX; i++) 240138568Ssam if (isset(chans, i)) { 241138568Ssam printf("%s%u", sep, i); 242138568Ssam sep = ", "; 243138568Ssam } 244138568Ssam} 245138568Ssam#endif /* IEEE80211_DEBUG */ 246138568Ssam 247116742Ssam/* 248138568Ssam * Initialize the channel set to scan based on the 249116742Ssam * of available channels and the current PHY mode. 250116742Ssam */ 251117811Ssamstatic void 252138568Ssamieee80211_reset_scan(struct ieee80211com *ic) 253116742Ssam{ 254116742Ssam 255138568Ssam /* XXX ic_des_chan should be handled with ic_chan_active */ 256138568Ssam if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) { 257138568Ssam memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan)); 258138568Ssam setbit(ic->ic_chan_scan, 259138568Ssam ieee80211_chan2ieee(ic, ic->ic_des_chan)); 260138568Ssam } else 261138568Ssam memcpy(ic->ic_chan_scan, ic->ic_chan_active, 262138568Ssam sizeof(ic->ic_chan_active)); 263117811Ssam /* NB: hack, setup so next_scan starts with the first channel */ 264117811Ssam if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC) 265138568Ssam ieee80211_set_chan(ic, ic->ic_bss, 266138568Ssam &ic->ic_channels[IEEE80211_CHAN_MAX]); 267138568Ssam#ifdef IEEE80211_DEBUG 268138568Ssam if (ieee80211_msg_scan(ic)) { 269138568Ssam printf("%s: scan set:", __func__); 270138568Ssam dump_chanlist(ic->ic_chan_scan); 271138568Ssam printf(" start chan %u\n", 272138568Ssam ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)); 273138568Ssam } 274138568Ssam#endif /* IEEE80211_DEBUG */ 275116742Ssam} 276116742Ssam 277116742Ssam/* 278116742Ssam * Begin an active scan. 279116742Ssam */ 280116742Ssamvoid 281138568Ssamieee80211_begin_scan(struct ieee80211com *ic, int reset) 282116742Ssam{ 283116742Ssam 284138568Ssam ic->ic_scan.nt_scangen++; 285117811Ssam /* 286117811Ssam * In all but hostap mode scanning starts off in 287117811Ssam * an active mode before switching to passive. 288117811Ssam */ 289121180Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP) { 290117811Ssam ic->ic_flags |= IEEE80211_F_ASCAN; 291121180Ssam ic->ic_stats.is_scan_active++; 292121180Ssam } else 293121180Ssam ic->ic_stats.is_scan_passive++; 294139520Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 295139520Ssam "begin %s scan in %s mode, scangen %u\n", 296138568Ssam (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive", 297139520Ssam ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen); 298116742Ssam /* 299138568Ssam * Clear scan state and flush any previously seen AP's. 300116742Ssam */ 301138568Ssam ieee80211_reset_scan(ic); 302138568Ssam if (reset) 303138568Ssam ieee80211_free_allnodes(&ic->ic_scan); 304116742Ssam 305138568Ssam ic->ic_flags |= IEEE80211_F_SCAN; 306138568Ssam 307117811Ssam /* Scan the next channel. */ 308138568Ssam ieee80211_next_scan(ic); 309116742Ssam} 310116742Ssam 311116742Ssam/* 312116742Ssam * Switch to the next channel marked for scanning. 313116742Ssam */ 314138568Ssamint 315138568Ssamieee80211_next_scan(struct ieee80211com *ic) 316116742Ssam{ 317116742Ssam struct ieee80211_channel *chan; 318116742Ssam 319138568Ssam /* 320138568Ssam * Insure any previous mgt frame timeouts don't fire. 321138568Ssam * This assumes the driver does the right thing in 322138568Ssam * flushing anything queued in the driver and below. 323138568Ssam */ 324138568Ssam ic->ic_mgt_timer = 0; 325138568Ssam 326116742Ssam chan = ic->ic_bss->ni_chan; 327138568Ssam do { 328116742Ssam if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX]) 329116742Ssam chan = &ic->ic_channels[0]; 330116742Ssam if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) { 331138568Ssam clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan)); 332138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 333138568Ssam "%s: chan %d->%d\n", __func__, 334138568Ssam ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan), 335138568Ssam ieee80211_chan2ieee(ic, chan)); 336138568Ssam ieee80211_set_chan(ic, ic->ic_bss, chan); 337138568Ssam#ifdef notyet 338138568Ssam /* XXX driver state change */ 339116742Ssam /* 340138568Ssam * Scan next channel. If doing an active scan 341138568Ssam * and the channel is not marked passive-only 342138568Ssam * then send a probe request. Otherwise just 343138568Ssam * listen for beacons on the channel. 344116742Ssam */ 345138568Ssam if ((ic->ic_flags & IEEE80211_F_ASCAN) && 346138568Ssam (ni->ni_chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) { 347138568Ssam IEEE80211_SEND_MGMT(ic, ni, 348138568Ssam IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0); 349138568Ssam } 350138568Ssam#else 351138568Ssam ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 352138568Ssam#endif 353138568Ssam return 1; 354116742Ssam } 355138568Ssam } while (chan != ic->ic_bss->ni_chan); 356138568Ssam ieee80211_end_scan(ic); 357138568Ssam return 0; 358116742Ssam} 359116742Ssam 360141658Ssamstatic __inline void 361141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss) 362141658Ssam{ 363141658Ssam /* propagate useful state */ 364141658Ssam nbss->ni_authmode = obss->ni_authmode; 365141658Ssam nbss->ni_txpower = obss->ni_txpower; 366141658Ssam nbss->ni_vlan = obss->ni_vlan; 367141658Ssam nbss->ni_rsn = obss->ni_rsn; 368141658Ssam /* XXX statistics? */ 369141658Ssam} 370141658Ssam 371116742Ssamvoid 372116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 373116742Ssam{ 374140753Ssam struct ieee80211_node_table *nt; 375116742Ssam struct ieee80211_node *ni; 376116742Ssam 377138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 378138568Ssam "%s: creating ibss\n", __func__); 379138568Ssam 380138568Ssam /* 381138568Ssam * Create the station/neighbor table. Note that for adhoc 382138568Ssam * mode we make the initial inactivity timer longer since 383138568Ssam * we create nodes only through discovery and they typically 384138568Ssam * are long-lived associations. 385138568Ssam */ 386140753Ssam nt = &ic->ic_sta; 387140753Ssam IEEE80211_NODE_LOCK(nt); 388140753Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 389140753Ssam nt->nt_name = "station"; 390140753Ssam nt->nt_inact_init = ic->ic_inact_init; 391140753Ssam } else { 392140753Ssam nt->nt_name = "neighbor"; 393140753Ssam nt->nt_inact_init = ic->ic_inact_run; 394140753Ssam } 395140753Ssam IEEE80211_NODE_UNLOCK(nt); 396140753Ssam 397140753Ssam ni = ieee80211_alloc_node(nt, ic->ic_myaddr); 398140753Ssam if (ni == NULL) { 399140753Ssam /* XXX recovery? */ 400138568Ssam return; 401138568Ssam } 402116742Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 403116742Ssam ni->ni_esslen = ic->ic_des_esslen; 404116742Ssam memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 405141658Ssam copy_bss(ni, ic->ic_bss); 406116742Ssam ni->ni_intval = ic->ic_lintval; 407138568Ssam if (ic->ic_flags & IEEE80211_F_PRIVACY) 408116742Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 409116742Ssam if (ic->ic_phytype == IEEE80211_T_FH) { 410116742Ssam ni->ni_fhdwell = 200; /* XXX */ 411116742Ssam ni->ni_fhindex = 1; 412116742Ssam } 413138568Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 414138568Ssam ic->ic_flags |= IEEE80211_F_SIBSS; 415138568Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */ 416143300Ssam if (ic->ic_flags & IEEE80211_F_DESBSSID) 417143300Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid); 418143300Ssam else 419143300Ssam ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */ 420138568Ssam } 421138568Ssam /* 422138568Ssam * Fix the channel and related attributes. 423138568Ssam */ 424138568Ssam ieee80211_set_chan(ic, ni, chan); 425138568Ssam ic->ic_curmode = ieee80211_chan2mode(ic, chan); 426138568Ssam /* 427138568Ssam * Do mode-specific rate setup. 428138568Ssam */ 429138568Ssam if (ic->ic_curmode == IEEE80211_MODE_11G) { 430138568Ssam /* 431138568Ssam * Use a mixed 11b/11g rate set. 432138568Ssam */ 433138568Ssam ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G); 434138568Ssam } else if (ic->ic_curmode == IEEE80211_MODE_11B) { 435138568Ssam /* 436138568Ssam * Force pure 11b rate set. 437138568Ssam */ 438138568Ssam ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B); 439138568Ssam } 440138568Ssam 441140753Ssam (void) ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 442116742Ssam} 443116742Ssam 444138568Ssamvoid 445138568Ssamieee80211_reset_bss(struct ieee80211com *ic) 446138568Ssam{ 447138568Ssam struct ieee80211_node *ni, *obss; 448138568Ssam 449138568Ssam ieee80211_node_table_reset(&ic->ic_scan); 450140753Ssam ieee80211_node_table_reset(&ic->ic_sta); 451140753Ssam 452138568Ssam ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr); 453138568Ssam KASSERT(ni != NULL, ("unable to setup inital BSS node")); 454138568Ssam obss = ic->ic_bss; 455138568Ssam ic->ic_bss = ieee80211_ref_node(ni); 456141658Ssam if (obss != NULL) { 457141658Ssam copy_bss(ni, obss); 458141658Ssam ni->ni_intval = ic->ic_lintval; 459138568Ssam ieee80211_free_node(obss); 460141658Ssam } 461138568Ssam} 462138568Ssam 463127767Ssamstatic int 464138568Ssamieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni) 465127767Ssam{ 466127767Ssam u_int8_t rate; 467127767Ssam int fail; 468127767Ssam 469127767Ssam fail = 0; 470127767Ssam if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 471127767Ssam fail |= 0x01; 472127767Ssam if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 473127767Ssam ni->ni_chan != ic->ic_des_chan) 474127767Ssam fail |= 0x01; 475127767Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 476127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 477127767Ssam fail |= 0x02; 478127767Ssam } else { 479127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 480127767Ssam fail |= 0x02; 481127767Ssam } 482138568Ssam if (ic->ic_flags & IEEE80211_F_PRIVACY) { 483127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 484127767Ssam fail |= 0x04; 485127767Ssam } else { 486127767Ssam /* XXX does this mean privacy is supported or required? */ 487127767Ssam if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 488127767Ssam fail |= 0x04; 489127767Ssam } 490148299Ssam rate = ieee80211_fix_rate(ni, IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 491127767Ssam if (rate & IEEE80211_RATE_BASIC) 492127767Ssam fail |= 0x08; 493127767Ssam if (ic->ic_des_esslen != 0 && 494127767Ssam (ni->ni_esslen != ic->ic_des_esslen || 495127767Ssam memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) 496127767Ssam fail |= 0x10; 497127767Ssam if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 498127767Ssam !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 499127767Ssam fail |= 0x20; 500127767Ssam#ifdef IEEE80211_DEBUG 501138568Ssam if (ieee80211_msg_scan(ic)) { 502127767Ssam printf(" %c %s", fail ? '-' : '+', 503127767Ssam ether_sprintf(ni->ni_macaddr)); 504127767Ssam printf(" %s%c", ether_sprintf(ni->ni_bssid), 505127767Ssam fail & 0x20 ? '!' : ' '); 506127767Ssam printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan), 507127767Ssam fail & 0x01 ? '!' : ' '); 508127767Ssam printf(" %+4d", ni->ni_rssi); 509127767Ssam printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 510127767Ssam fail & 0x08 ? '!' : ' '); 511127767Ssam printf(" %4s%c", 512127767Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 513127767Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 514127767Ssam "????", 515127767Ssam fail & 0x02 ? '!' : ' '); 516127767Ssam printf(" %3s%c ", 517127767Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 518127767Ssam "wep" : "no", 519127767Ssam fail & 0x04 ? '!' : ' '); 520127767Ssam ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 521127767Ssam printf("%s\n", fail & 0x10 ? "!" : ""); 522127767Ssam } 523127767Ssam#endif 524127767Ssam return fail; 525127767Ssam} 526127767Ssam 527138568Ssamstatic __inline u_int8_t 528138568Ssammaxrate(const struct ieee80211_node *ni) 529138568Ssam{ 530138568Ssam const struct ieee80211_rateset *rs = &ni->ni_rates; 531138568Ssam /* NB: assumes rate set is sorted (happens on frame receive) */ 532138568Ssam return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL; 533138568Ssam} 534138568Ssam 535116742Ssam/* 536138568Ssam * Compare the capabilities of two nodes and decide which is 537138568Ssam * more desirable (return >0 if a is considered better). Note 538138568Ssam * that we assume compatibility/usability has already been checked 539138568Ssam * so we don't need to (e.g. validate whether privacy is supported). 540138568Ssam * Used to select the best scan candidate for association in a BSS. 541138568Ssam */ 542138568Ssamstatic int 543138568Ssamieee80211_node_compare(struct ieee80211com *ic, 544138568Ssam const struct ieee80211_node *a, 545138568Ssam const struct ieee80211_node *b) 546138568Ssam{ 547138568Ssam u_int8_t maxa, maxb; 548138568Ssam u_int8_t rssia, rssib; 549138568Ssam 550138568Ssam /* privacy support preferred */ 551138568Ssam if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) && 552138568Ssam (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 553138568Ssam return 1; 554138568Ssam if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 && 555138568Ssam (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)) 556138568Ssam return -1; 557138568Ssam 558138568Ssam rssia = ic->ic_node_getrssi(a); 559138568Ssam rssib = ic->ic_node_getrssi(b); 560139543Ssam if (abs(rssib - rssia) < 5) { 561139543Ssam /* best/max rate preferred if signal level close enough XXX */ 562139543Ssam maxa = maxrate(a); 563139543Ssam maxb = maxrate(b); 564139543Ssam if (maxa != maxb) 565139543Ssam return maxa - maxb; 566139543Ssam /* XXX use freq for channel preference */ 567139543Ssam /* for now just prefer 5Ghz band to all other bands */ 568139543Ssam if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) && 569139543Ssam !IEEE80211_IS_CHAN_5GHZ(b->ni_chan)) 570139543Ssam return 1; 571139543Ssam if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) && 572139543Ssam IEEE80211_IS_CHAN_5GHZ(b->ni_chan)) 573139543Ssam return -1; 574139543Ssam } 575138568Ssam /* all things being equal, use signal level */ 576138568Ssam return rssia - rssib; 577138568Ssam} 578138568Ssam 579138568Ssam/* 580140441Ssam * Mark an ongoing scan stopped. 581140441Ssam */ 582140441Ssamvoid 583140441Ssamieee80211_cancel_scan(struct ieee80211com *ic) 584140441Ssam{ 585140441Ssam 586140441Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n", 587140441Ssam __func__, 588140441Ssam (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive"); 589140441Ssam 590140441Ssam ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN); 591140441Ssam} 592140441Ssam 593140441Ssam/* 594116742Ssam * Complete a scan of potential channels. 595116742Ssam */ 596116742Ssamvoid 597138568Ssamieee80211_end_scan(struct ieee80211com *ic) 598116742Ssam{ 599140448Ssam struct ieee80211_node_table *nt = &ic->ic_scan; 600140448Ssam struct ieee80211_node *ni, *selbs; 601116742Ssam 602140441Ssam ieee80211_cancel_scan(ic); 603140441Ssam ieee80211_notify_scan_done(ic); 604116742Ssam 605116742Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 606138568Ssam u_int8_t maxrssi[IEEE80211_CHAN_MAX]; /* XXX off stack? */ 607138568Ssam int i, bestchan; 608138568Ssam u_int8_t rssi; 609138568Ssam 610116742Ssam /* 611116742Ssam * The passive scan to look for existing AP's completed, 612116742Ssam * select a channel to camp on. Identify the channels 613116742Ssam * that already have one or more AP's and try to locate 614140753Ssam * an unoccupied one. If that fails, pick a channel that 615138568Ssam * looks to be quietest. 616116742Ssam */ 617138568Ssam memset(maxrssi, 0, sizeof(maxrssi)); 618140448Ssam IEEE80211_NODE_LOCK(nt); 619140448Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 620138568Ssam rssi = ic->ic_node_getrssi(ni); 621138568Ssam i = ieee80211_chan2ieee(ic, ni->ni_chan); 622138568Ssam if (rssi > maxrssi[i]) 623138568Ssam maxrssi[i] = rssi; 624116742Ssam } 625140448Ssam IEEE80211_NODE_UNLOCK(nt); 626138568Ssam /* XXX select channel more intelligently */ 627138568Ssam bestchan = -1; 628116742Ssam for (i = 0; i < IEEE80211_CHAN_MAX; i++) 629138568Ssam if (isset(ic->ic_chan_active, i)) { 630138568Ssam /* 631138568Ssam * If the channel is unoccupied the max rssi 632138568Ssam * should be zero; just take it. Otherwise 633138568Ssam * track the channel with the lowest rssi and 634138568Ssam * use that when all channels appear occupied. 635138568Ssam */ 636138568Ssam if (maxrssi[i] == 0) { 637138568Ssam bestchan = i; 638116742Ssam break; 639138568Ssam } 640143715Ssam if (bestchan == -1 || 641143715Ssam maxrssi[i] < maxrssi[bestchan]) 642138568Ssam bestchan = i; 643138568Ssam } 644138568Ssam if (bestchan != -1) { 645138568Ssam ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]); 646138568Ssam return; 647116742Ssam } 648138568Ssam /* no suitable channel, should not happen */ 649138568Ssam } 650138568Ssam 651138568Ssam /* 652138568Ssam * When manually sequencing the state machine; scan just once 653138568Ssam * regardless of whether we have a candidate or not. The 654138568Ssam * controlling application is expected to setup state and 655138568Ssam * initiate an association. 656138568Ssam */ 657138568Ssam if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL) 658116742Ssam return; 659138568Ssam /* 660138568Ssam * Automatic sequencing; look for a candidate and 661138568Ssam * if found join the network. 662138568Ssam */ 663140448Ssam /* NB: unlocked read should be ok */ 664140448Ssam if (TAILQ_FIRST(&nt->nt_node) == NULL) { 665138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 666138568Ssam "%s: no scan candidate\n", __func__); 667116742Ssam notfound: 668116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS && 669116742Ssam (ic->ic_flags & IEEE80211_F_IBSSON) && 670116742Ssam ic->ic_des_esslen != 0) { 671116742Ssam ieee80211_create_ibss(ic, ic->ic_ibss_chan); 672116742Ssam return; 673116742Ssam } 674116742Ssam /* 675116742Ssam * Reset the list of channels to scan and start again. 676116742Ssam */ 677138568Ssam ieee80211_reset_scan(ic); 678138568Ssam ic->ic_flags |= IEEE80211_F_SCAN; 679138568Ssam ieee80211_next_scan(ic); 680116742Ssam return; 681116742Ssam } 682116742Ssam selbs = NULL; 683138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n", 684138568Ssam "macaddr bssid chan rssi rate flag wep essid"); 685140448Ssam IEEE80211_NODE_LOCK(nt); 686140448Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 687116742Ssam if (ni->ni_fails) { 688116742Ssam /* 689116742Ssam * The configuration of the access points may change 690116742Ssam * during my scan. So delete the entry for the AP 691116742Ssam * and retry to associate if there is another beacon. 692116742Ssam */ 693138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 694138568Ssam "%s: skip scan candidate %s, fails %u\n", 695138568Ssam __func__, ether_sprintf(ni->ni_macaddr), 696138568Ssam ni->ni_fails); 697140448Ssam ni->ni_fails++; 698140448Ssam#if 0 699116742Ssam if (ni->ni_fails++ > 2) 700138568Ssam ieee80211_free_node(ni); 701140448Ssam#endif 702116742Ssam continue; 703116742Ssam } 704138568Ssam if (ieee80211_match_bss(ic, ni) == 0) { 705116742Ssam if (selbs == NULL) 706116742Ssam selbs = ni; 707140448Ssam else if (ieee80211_node_compare(ic, ni, selbs) > 0) 708116742Ssam selbs = ni; 709116742Ssam } 710116742Ssam } 711140448Ssam if (selbs != NULL) /* NB: grab ref while dropping lock */ 712140448Ssam (void) ieee80211_ref_node(selbs); 713140448Ssam IEEE80211_NODE_UNLOCK(nt); 714116742Ssam if (selbs == NULL) 715116742Ssam goto notfound; 716138568Ssam if (!ieee80211_sta_join(ic, selbs)) { 717140448Ssam ieee80211_free_node(selbs); 718138568Ssam goto notfound; 719138568Ssam } 720138568Ssam} 721138568Ssam 722138568Ssam/* 723138568Ssam * Handle 802.11 ad hoc network merge. The 724138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance 725138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match 726138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that 727138568Ssam * has the station's desired SSID. The "oldest" 802.11 network 728138568Ssam * sends beacons with the greatest TSF timestamp. 729138568Ssam * 730138568Ssam * The caller is assumed to validate TSF's before attempting a merge. 731138568Ssam * 732138568Ssam * Return !0 if the BSSID changed, 0 otherwise. 733138568Ssam */ 734138568Ssamint 735138568Ssamieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni) 736138568Ssam{ 737138568Ssam 738140453Ssam if (ni == ic->ic_bss || 739140453Ssam IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) { 740138568Ssam /* unchanged, nothing to do */ 741138568Ssam return 0; 742138568Ssam } 743138568Ssam if (ieee80211_match_bss(ic, ni) != 0) { /* capabilities mismatch */ 744138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 745138568Ssam "%s: merge failed, capabilities mismatch\n", __func__); 746138568Ssam ic->ic_stats.is_ibss_capmismatch++; 747138568Ssam return 0; 748138568Ssam } 749138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 750138568Ssam "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__, 751138568Ssam ether_sprintf(ni->ni_bssid), 752138568Ssam ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 753138568Ssam ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long", 754138568Ssam ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "" 755138568Ssam ); 756140753Ssam return ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 757138568Ssam} 758138568Ssam 759138568Ssam/* 760138568Ssam * Join the specified IBSS/BSS network. The node is assumed to 761138568Ssam * be passed in with a held reference. 762138568Ssam */ 763138568Ssamint 764138568Ssamieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs) 765138568Ssam{ 766138568Ssam struct ieee80211_node *obss; 767138568Ssam 768116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 769140753Ssam struct ieee80211_node_table *nt; 770138568Ssam /* 771140440Ssam * Delete unusable rates; we've already checked 772140440Ssam * that the negotiated rate set is acceptable. 773138568Ssam */ 774148299Ssam ieee80211_fix_rate(selbs, IEEE80211_F_DODEL); 775127772Ssam /* 776140753Ssam * Fillin the neighbor table; it will already 777139521Ssam * exist if we are simply switching mastership. 778140753Ssam * XXX ic_sta always setup so this is unnecessary? 779127772Ssam */ 780140753Ssam nt = &ic->ic_sta; 781140753Ssam IEEE80211_NODE_LOCK(nt); 782140753Ssam nt->nt_name = "neighbor"; 783140753Ssam nt->nt_inact_init = ic->ic_inact_run; 784140753Ssam IEEE80211_NODE_UNLOCK(nt); 785138568Ssam } 786138568Ssam 787138568Ssam /* 788138568Ssam * Committed to selbs, setup state. 789138568Ssam */ 790138568Ssam obss = ic->ic_bss; 791140753Ssam ic->ic_bss = selbs; /* NB: caller assumed to bump refcnt */ 792138568Ssam if (obss != NULL) 793138568Ssam ieee80211_free_node(obss); 794138568Ssam /* 795138568Ssam * Set the erp state (mostly the slot time) to deal with 796138568Ssam * the auto-select case; this should be redundant if the 797138568Ssam * mode is locked. 798138568Ssam */ 799138568Ssam ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan); 800138568Ssam ieee80211_reset_erp(ic); 801138568Ssam ieee80211_wme_initparams(ic); 802140753Ssam 803140753Ssam if (ic->ic_opmode == IEEE80211_M_STA) 804140753Ssam ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 805140753Ssam else 806117811Ssam ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 807138568Ssam return 1; 808116742Ssam} 809116742Ssam 810138568Ssam/* 811138568Ssam * Leave the specified IBSS/BSS network. The node is assumed to 812138568Ssam * be passed in with a held reference. 813138568Ssam */ 814138568Ssamvoid 815138568Ssamieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 816138568Ssam{ 817138568Ssam ic->ic_node_cleanup(ni); 818138568Ssam ieee80211_notify_node_leave(ic, ni); 819138568Ssam} 820138568Ssam 821116742Ssamstatic struct ieee80211_node * 822138568Ssamnode_alloc(struct ieee80211_node_table *nt) 823116742Ssam{ 824127768Ssam struct ieee80211_node *ni; 825138568Ssam 826127768Ssam MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node), 827127768Ssam M_80211_NODE, M_NOWAIT | M_ZERO); 828127768Ssam return ni; 829116742Ssam} 830116742Ssam 831138568Ssam/* 832138568Ssam * Reclaim any resources in a node and reset any critical 833138568Ssam * state. Typically nodes are free'd immediately after, 834138568Ssam * but in some cases the storage may be reused so we need 835138568Ssam * to insure consistent state (should probably fix that). 836138568Ssam */ 837116742Ssamstatic void 838138568Ssamnode_cleanup(struct ieee80211_node *ni) 839116742Ssam{ 840138568Ssam#define N(a) (sizeof(a)/sizeof(a[0])) 841138568Ssam struct ieee80211com *ic = ni->ni_ic; 842138568Ssam int i, qlen; 843138568Ssam 844138568Ssam /* NB: preserve ni_table */ 845138568Ssam if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 846138568Ssam ic->ic_ps_sta--; 847138568Ssam ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 848138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 849138568Ssam "[%s] power save mode off, %u sta's in ps mode\n", 850138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta); 851138568Ssam } 852147788Ssam /* 853147788Ssam * Clear AREF flag that marks the authorization refcnt bump 854147788Ssam * has happened. This is probably not needed as the node 855147788Ssam * should always be removed from the table so not found but 856147788Ssam * do it just in case. 857147788Ssam */ 858147788Ssam ni->ni_flags &= ~IEEE80211_NODE_AREF; 859138568Ssam 860138568Ssam /* 861138568Ssam * Drain power save queue and, if needed, clear TIM. 862138568Ssam */ 863138568Ssam IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen); 864138568Ssam if (qlen != 0 && ic->ic_set_tim != NULL) 865138568Ssam ic->ic_set_tim(ic, ni, 0); 866138568Ssam 867138568Ssam ni->ni_associd = 0; 868138568Ssam if (ni->ni_challenge != NULL) { 869138568Ssam FREE(ni->ni_challenge, M_DEVBUF); 870138568Ssam ni->ni_challenge = NULL; 871138568Ssam } 872138568Ssam /* 873138568Ssam * Preserve SSID, WPA, and WME ie's so the bss node is 874138568Ssam * reusable during a re-auth/re-assoc state transition. 875138568Ssam * If we remove these data they will not be recreated 876138568Ssam * because they come from a probe-response or beacon frame 877138568Ssam * which cannot be expected prior to the association-response. 878138568Ssam * This should not be an issue when operating in other modes 879138568Ssam * as stations leaving always go through a full state transition 880138568Ssam * which will rebuild this state. 881138568Ssam * 882138568Ssam * XXX does this leave us open to inheriting old state? 883138568Ssam */ 884138568Ssam for (i = 0; i < N(ni->ni_rxfrag); i++) 885138568Ssam if (ni->ni_rxfrag[i] != NULL) { 886138568Ssam m_freem(ni->ni_rxfrag[i]); 887138568Ssam ni->ni_rxfrag[i] = NULL; 888138568Ssam } 889138568Ssam ieee80211_crypto_delkey(ic, &ni->ni_ucastkey); 890138568Ssam#undef N 891116742Ssam} 892116742Ssam 893116742Ssamstatic void 894138568Ssamnode_free(struct ieee80211_node *ni) 895116742Ssam{ 896138568Ssam struct ieee80211com *ic = ni->ni_ic; 897138568Ssam 898138568Ssam ic->ic_node_cleanup(ni); 899138568Ssam if (ni->ni_wpa_ie != NULL) 900138568Ssam FREE(ni->ni_wpa_ie, M_DEVBUF); 901138568Ssam if (ni->ni_wme_ie != NULL) 902138568Ssam FREE(ni->ni_wme_ie, M_DEVBUF); 903138568Ssam IEEE80211_NODE_SAVEQ_DESTROY(ni); 904138568Ssam FREE(ni, M_80211_NODE); 905116742Ssam} 906116742Ssam 907120104Ssamstatic u_int8_t 908138568Ssamnode_getrssi(const struct ieee80211_node *ni) 909120104Ssam{ 910120104Ssam return ni->ni_rssi; 911120104Ssam} 912120104Ssam 913116742Ssamstatic void 914138568Ssamieee80211_setup_node(struct ieee80211_node_table *nt, 915138568Ssam struct ieee80211_node *ni, const u_int8_t *macaddr) 916116742Ssam{ 917138568Ssam struct ieee80211com *ic = nt->nt_ic; 918116742Ssam int hash; 919116742Ssam 920138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 921140766Ssam "%s %p<%s> in %s table\n", __func__, ni, 922138568Ssam ether_sprintf(macaddr), nt->nt_name); 923138568Ssam 924116742Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 925116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 926138568Ssam ieee80211_node_initref(ni); /* mark referenced */ 927138568Ssam ni->ni_chan = IEEE80211_CHAN_ANYC; 928138568Ssam ni->ni_authmode = IEEE80211_AUTH_OPEN; 929138568Ssam ni->ni_txpower = ic->ic_txpowlimit; /* max power */ 930138568Ssam ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE); 931139528Ssam ni->ni_inact_reload = nt->nt_inact_init; 932139528Ssam ni->ni_inact = ni->ni_inact_reload; 933138568Ssam IEEE80211_NODE_SAVEQ_INIT(ni, "unknown"); 934138568Ssam 935138568Ssam IEEE80211_NODE_LOCK(nt); 936138568Ssam TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list); 937138568Ssam LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash); 938138568Ssam ni->ni_table = nt; 939138568Ssam ni->ni_ic = ic; 940138568Ssam IEEE80211_NODE_UNLOCK(nt); 941116742Ssam} 942116742Ssam 943116742Ssamstruct ieee80211_node * 944138568Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 945116742Ssam{ 946138568Ssam struct ieee80211com *ic = nt->nt_ic; 947138568Ssam struct ieee80211_node *ni; 948138568Ssam 949138568Ssam ni = ic->ic_node_alloc(nt); 950116742Ssam if (ni != NULL) 951138568Ssam ieee80211_setup_node(nt, ni, macaddr); 952127769Ssam else 953127769Ssam ic->ic_stats.is_rx_nodealloc++; 954116742Ssam return ni; 955116742Ssam} 956116742Ssam 957116742Ssamstruct ieee80211_node * 958138568Ssamieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 959116742Ssam{ 960138568Ssam struct ieee80211com *ic = nt->nt_ic; 961138568Ssam struct ieee80211_node *ni; 962138568Ssam 963138568Ssam ni = ic->ic_node_alloc(nt); 964116742Ssam if (ni != NULL) { 965138568Ssam ieee80211_setup_node(nt, ni, macaddr); 966127770Ssam /* 967127770Ssam * Inherit from ic_bss. 968127770Ssam */ 969138568Ssam ni->ni_authmode = ic->ic_bss->ni_authmode; 970138568Ssam ni->ni_txpower = ic->ic_bss->ni_txpower; 971138568Ssam ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */ 972127770Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 973138568Ssam ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan); 974138568Ssam ni->ni_rsn = ic->ic_bss->ni_rsn; 975127770Ssam } else 976127770Ssam ic->ic_stats.is_rx_nodealloc++; 977116742Ssam return ni; 978116742Ssam} 979116742Ssam 980127772Ssamstatic struct ieee80211_node * 981138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 982138568Ssam_ieee80211_find_node_debug(struct ieee80211_node_table *nt, 983138568Ssam const u_int8_t *macaddr, const char *func, int line) 984138568Ssam#else 985138568Ssam_ieee80211_find_node(struct ieee80211_node_table *nt, 986138568Ssam const u_int8_t *macaddr) 987138568Ssam#endif 988116742Ssam{ 989116742Ssam struct ieee80211_node *ni; 990116742Ssam int hash; 991116742Ssam 992138568Ssam IEEE80211_NODE_LOCK_ASSERT(nt); 993127772Ssam 994116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 995138568Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 996116742Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 997138568Ssam ieee80211_ref_node(ni); /* mark referenced */ 998138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 999138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1000140766Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1001140766Ssam func, line, 1002140766Ssam ni, ether_sprintf(ni->ni_macaddr), 1003140766Ssam ieee80211_node_refcnt(ni)); 1004138568Ssam#endif 1005127772Ssam return ni; 1006116742Ssam } 1007116742Ssam } 1008127772Ssam return NULL; 1009127772Ssam} 1010138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1011138568Ssam#define _ieee80211_find_node(nt, mac) \ 1012138568Ssam _ieee80211_find_node_debug(nt, mac, func, line) 1013138568Ssam#endif 1014127772Ssam 1015127772Ssamstruct ieee80211_node * 1016138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1017138568Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt, 1018138568Ssam const u_int8_t *macaddr, const char *func, int line) 1019138568Ssam#else 1020138568Ssamieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 1021138568Ssam#endif 1022127772Ssam{ 1023127772Ssam struct ieee80211_node *ni; 1024127772Ssam 1025138568Ssam IEEE80211_NODE_LOCK(nt); 1026138568Ssam ni = _ieee80211_find_node(nt, macaddr); 1027138568Ssam IEEE80211_NODE_UNLOCK(nt); 1028116742Ssam return ni; 1029116742Ssam} 1030116742Ssam 1031116742Ssam/* 1032138568Ssam * Fake up a node; this handles node discovery in adhoc mode. 1033138568Ssam * Note that for the driver's benefit we we treat this like 1034138568Ssam * an association so the driver has an opportunity to setup 1035138568Ssam * it's private state. 1036138568Ssam */ 1037138568Ssamstruct ieee80211_node * 1038138568Ssamieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt, 1039138568Ssam const u_int8_t macaddr[IEEE80211_ADDR_LEN]) 1040138568Ssam{ 1041138568Ssam struct ieee80211com *ic = nt->nt_ic; 1042138568Ssam struct ieee80211_node *ni; 1043138568Ssam 1044138568Ssam ni = ieee80211_dup_bss(nt, macaddr); 1045138568Ssam if (ni != NULL) { 1046138568Ssam /* XXX no rate negotiation; just dup */ 1047138568Ssam ni->ni_rates = ic->ic_bss->ni_rates; 1048139524Ssam if (ic->ic_newassoc != NULL) 1049138568Ssam ic->ic_newassoc(ic, ni, 1); 1050138568Ssam /* XXX not right for 802.1x/WPA */ 1051138568Ssam ieee80211_node_authorize(ic, ni); 1052138568Ssam } 1053138568Ssam return ni; 1054138568Ssam} 1055138568Ssam 1056138568Ssam/* 1057138568Ssam * Locate the node for sender, track state, and then pass the 1058138568Ssam * (referenced) node up to the 802.11 layer for its use. We 1059138568Ssam * are required to pass some node so we fall back to ic_bss 1060138568Ssam * when this frame is from an unknown sender. The 802.11 layer 1061138568Ssam * knows this means the sender wasn't in the node table and 1062138568Ssam * acts accordingly. 1063138568Ssam */ 1064138568Ssamstruct ieee80211_node * 1065138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1066138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic, 1067138568Ssam const struct ieee80211_frame_min *wh, const char *func, int line) 1068138568Ssam#else 1069138568Ssamieee80211_find_rxnode(struct ieee80211com *ic, 1070138568Ssam const struct ieee80211_frame_min *wh) 1071138568Ssam#endif 1072138568Ssam{ 1073138568Ssam#define IS_CTL(wh) \ 1074138568Ssam ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) 1075138568Ssam#define IS_PSPOLL(wh) \ 1076138568Ssam ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL) 1077138568Ssam struct ieee80211_node_table *nt; 1078138568Ssam struct ieee80211_node *ni; 1079138568Ssam 1080138568Ssam /* XXX may want scanned nodes in the neighbor table for adhoc */ 1081138568Ssam if (ic->ic_opmode == IEEE80211_M_STA || 1082138568Ssam ic->ic_opmode == IEEE80211_M_MONITOR || 1083138568Ssam (ic->ic_flags & IEEE80211_F_SCAN)) 1084138568Ssam nt = &ic->ic_scan; 1085138568Ssam else 1086140753Ssam nt = &ic->ic_sta; 1087138568Ssam /* XXX check ic_bss first in station mode */ 1088138568Ssam /* XXX 4-address frames? */ 1089138568Ssam IEEE80211_NODE_LOCK(nt); 1090138568Ssam if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/) 1091138568Ssam ni = _ieee80211_find_node(nt, wh->i_addr1); 1092138568Ssam else 1093138568Ssam ni = _ieee80211_find_node(nt, wh->i_addr2); 1094138568Ssam IEEE80211_NODE_UNLOCK(nt); 1095138568Ssam 1096138568Ssam return (ni != NULL ? ni : ieee80211_ref_node(ic->ic_bss)); 1097138568Ssam#undef IS_PSPOLL 1098138568Ssam#undef IS_CTL 1099138568Ssam} 1100138568Ssam 1101138568Ssam/* 1102127772Ssam * Return a reference to the appropriate node for sending 1103127772Ssam * a data frame. This handles node discovery in adhoc networks. 1104127772Ssam */ 1105127772Ssamstruct ieee80211_node * 1106138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1107138568Ssamieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr, 1108138568Ssam const char *func, int line) 1109138568Ssam#else 1110138568Ssamieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr) 1111138568Ssam#endif 1112127772Ssam{ 1113140753Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 1114127772Ssam struct ieee80211_node *ni; 1115127772Ssam 1116127772Ssam /* 1117127772Ssam * The destination address should be in the node table 1118127772Ssam * unless we are operating in station mode or this is a 1119127772Ssam * multicast/broadcast frame. 1120127772Ssam */ 1121140753Ssam if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr)) 1122138568Ssam return ieee80211_ref_node(ic->ic_bss); 1123127772Ssam 1124127772Ssam /* XXX can't hold lock across dup_bss 'cuz of recursive locking */ 1125138568Ssam IEEE80211_NODE_LOCK(nt); 1126138568Ssam ni = _ieee80211_find_node(nt, macaddr); 1127138568Ssam IEEE80211_NODE_UNLOCK(nt); 1128138568Ssam 1129138568Ssam if (ni == NULL) { 1130138568Ssam if (ic->ic_opmode == IEEE80211_M_IBSS || 1131140497Ssam ic->ic_opmode == IEEE80211_M_AHDEMO) { 1132140497Ssam /* 1133140497Ssam * In adhoc mode cons up a node for the destination. 1134140497Ssam * Note that we need an additional reference for the 1135140497Ssam * caller to be consistent with _ieee80211_find_node. 1136140497Ssam */ 1137138568Ssam ni = ieee80211_fakeup_adhoc_node(nt, macaddr); 1138140497Ssam if (ni != NULL) 1139140497Ssam (void) ieee80211_ref_node(ni); 1140140497Ssam } else { 1141138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 1142138568Ssam "[%s] no node, discard frame (%s)\n", 1143138568Ssam ether_sprintf(macaddr), __func__); 1144138568Ssam ic->ic_stats.is_tx_nonode++; 1145127772Ssam } 1146127772Ssam } 1147127772Ssam return ni; 1148127772Ssam} 1149127772Ssam 1150127772Ssam/* 1151116742Ssam * Like find but search based on the channel too. 1152116742Ssam */ 1153116742Ssamstruct ieee80211_node * 1154138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1155138568Ssamieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt, 1156138568Ssam const u_int8_t *macaddr, struct ieee80211_channel *chan, 1157138568Ssam const char *func, int line) 1158138568Ssam#else 1159138568Ssamieee80211_find_node_with_channel(struct ieee80211_node_table *nt, 1160138568Ssam const u_int8_t *macaddr, struct ieee80211_channel *chan) 1161138568Ssam#endif 1162116742Ssam{ 1163116742Ssam struct ieee80211_node *ni; 1164116742Ssam int hash; 1165116742Ssam 1166116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 1167138568Ssam IEEE80211_NODE_LOCK(nt); 1168138568Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1169127772Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1170127772Ssam ni->ni_chan == chan) { 1171138568Ssam ieee80211_ref_node(ni); /* mark referenced */ 1172138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1173138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1174140766Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1175140766Ssam func, line, 1176138568Ssam#else 1177140766Ssam "%s %p<%s> refcnt %d\n", __func__, 1178138568Ssam#endif 1179140766Ssam ni, ether_sprintf(ni->ni_macaddr), 1180140766Ssam ieee80211_node_refcnt(ni)); 1181116742Ssam break; 1182116742Ssam } 1183116742Ssam } 1184138568Ssam IEEE80211_NODE_UNLOCK(nt); 1185116742Ssam return ni; 1186116742Ssam} 1187116742Ssam 1188138568Ssam/* 1189138568Ssam * Like find but search based on the ssid too. 1190138568Ssam */ 1191138568Ssamstruct ieee80211_node * 1192138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1193138568Ssamieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt, 1194138568Ssam const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid, 1195138568Ssam const char *func, int line) 1196138568Ssam#else 1197138568Ssamieee80211_find_node_with_ssid(struct ieee80211_node_table *nt, 1198138568Ssam const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid) 1199138568Ssam#endif 1200138568Ssam{ 1201147118Ssam#define MATCH_SSID(ni, ssid, ssidlen) \ 1202147118Ssam (ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0) 1203147118Ssam static const u_int8_t zeromac[IEEE80211_ADDR_LEN]; 1204138568Ssam struct ieee80211com *ic = nt->nt_ic; 1205138568Ssam struct ieee80211_node *ni; 1206138568Ssam int hash; 1207138568Ssam 1208138568Ssam IEEE80211_NODE_LOCK(nt); 1209147118Ssam /* 1210147118Ssam * A mac address that is all zero means match only the ssid; 1211147118Ssam * otherwise we must match both. 1212147118Ssam */ 1213147118Ssam if (IEEE80211_ADDR_EQ(macaddr, zeromac)) { 1214147118Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1215147118Ssam if (MATCH_SSID(ni, ssid, ssidlen)) 1216147118Ssam break; 1217147118Ssam } 1218147118Ssam } else { 1219147118Ssam hash = IEEE80211_NODE_HASH(macaddr); 1220147118Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1221147118Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1222147118Ssam MATCH_SSID(ni, ssid, ssidlen)) 1223147118Ssam break; 1224147118Ssam } 1225147118Ssam } 1226147118Ssam if (ni != NULL) { 1227147118Ssam ieee80211_ref_node(ni); /* mark referenced */ 1228147118Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1229138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1230147118Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1231147118Ssam func, line, 1232138568Ssam#else 1233147118Ssam "%s %p<%s> refcnt %d\n", __func__, 1234138568Ssam#endif 1235147118Ssam ni, ether_sprintf(ni->ni_macaddr), 1236147118Ssam ieee80211_node_refcnt(ni)); 1237138568Ssam } 1238138568Ssam IEEE80211_NODE_UNLOCK(nt); 1239138568Ssam return ni; 1240147118Ssam#undef MATCH_SSID 1241138568Ssam} 1242138568Ssam 1243116742Ssamstatic void 1244138568Ssam_ieee80211_free_node(struct ieee80211_node *ni) 1245116742Ssam{ 1246138568Ssam struct ieee80211com *ic = ni->ni_ic; 1247138568Ssam struct ieee80211_node_table *nt = ni->ni_table; 1248119150Ssam 1249138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1250140766Ssam "%s %p<%s> in %s table\n", __func__, ni, 1251140766Ssam ether_sprintf(ni->ni_macaddr), 1252138568Ssam nt != NULL ? nt->nt_name : "<gone>"); 1253138568Ssam 1254138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1255138568Ssam if (nt != NULL) { 1256138568Ssam TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1257138568Ssam LIST_REMOVE(ni, ni_hash); 1258138568Ssam } 1259138568Ssam ic->ic_node_free(ni); 1260116742Ssam} 1261116742Ssam 1262116742Ssamvoid 1263138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1264138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line) 1265138568Ssam#else 1266138568Ssamieee80211_free_node(struct ieee80211_node *ni) 1267138568Ssam#endif 1268116742Ssam{ 1269138568Ssam struct ieee80211_node_table *nt = ni->ni_table; 1270119150Ssam 1271138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1272140454Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1273140766Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 1274138568Ssam ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1275138568Ssam#endif 1276138568Ssam if (ieee80211_node_dectestref(ni)) { 1277138568Ssam /* 1278138568Ssam * Beware; if the node is marked gone then it's already 1279138568Ssam * been removed from the table and we cannot assume the 1280138568Ssam * table still exists. Regardless, there's no need to lock 1281138568Ssam * the table. 1282138568Ssam */ 1283138568Ssam if (ni->ni_table != NULL) { 1284138568Ssam IEEE80211_NODE_LOCK(nt); 1285138568Ssam _ieee80211_free_node(ni); 1286138568Ssam IEEE80211_NODE_UNLOCK(nt); 1287138568Ssam } else 1288138568Ssam _ieee80211_free_node(ni); 1289116742Ssam } 1290116742Ssam} 1291116742Ssam 1292138568Ssam/* 1293138568Ssam * Reclaim a node. If this is the last reference count then 1294138568Ssam * do the normal free work. Otherwise remove it from the node 1295138568Ssam * table and mark it gone by clearing the back-reference. 1296138568Ssam */ 1297138568Ssamstatic void 1298138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1299116742Ssam{ 1300138568Ssam 1301140766Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1302140766Ssam "%s: remove %p<%s> from %s table, refcnt %d\n", 1303140766Ssam __func__, ni, ether_sprintf(ni->ni_macaddr), 1304140766Ssam nt->nt_name, ieee80211_node_refcnt(ni)-1); 1305138568Ssam if (!ieee80211_node_dectestref(ni)) { 1306138568Ssam /* 1307138568Ssam * Other references are present, just remove the 1308138568Ssam * node from the table so it cannot be found. When 1309138568Ssam * the references are dropped storage will be 1310140753Ssam * reclaimed. 1311138568Ssam */ 1312138568Ssam TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1313138568Ssam LIST_REMOVE(ni, ni_hash); 1314138568Ssam ni->ni_table = NULL; /* clear reference */ 1315138568Ssam } else 1316138568Ssam _ieee80211_free_node(ni); 1317138568Ssam} 1318138568Ssam 1319138568Ssamstatic void 1320138568Ssamieee80211_free_allnodes_locked(struct ieee80211_node_table *nt) 1321138568Ssam{ 1322138568Ssam struct ieee80211com *ic = nt->nt_ic; 1323116742Ssam struct ieee80211_node *ni; 1324116742Ssam 1325138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1326138568Ssam "%s: free all nodes in %s table\n", __func__, nt->nt_name); 1327138568Ssam 1328138568Ssam while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) { 1329138568Ssam if (ni->ni_associd != 0) { 1330138568Ssam if (ic->ic_auth->ia_node_leave != NULL) 1331138568Ssam ic->ic_auth->ia_node_leave(ic, ni); 1332138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1333138568Ssam } 1334138568Ssam node_reclaim(nt, ni); 1335138568Ssam } 1336138568Ssam ieee80211_reset_erp(ic); 1337116742Ssam} 1338116742Ssam 1339138568Ssamstatic void 1340138568Ssamieee80211_free_allnodes(struct ieee80211_node_table *nt) 1341138568Ssam{ 1342138568Ssam 1343138568Ssam IEEE80211_NODE_LOCK(nt); 1344138568Ssam ieee80211_free_allnodes_locked(nt); 1345138568Ssam IEEE80211_NODE_UNLOCK(nt); 1346138568Ssam} 1347138568Ssam 1348120483Ssam/* 1349138568Ssam * Timeout entries in the scan cache. 1350138568Ssam */ 1351138568Ssamstatic void 1352138568Ssamieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt) 1353138568Ssam{ 1354138568Ssam struct ieee80211com *ic = nt->nt_ic; 1355138568Ssam struct ieee80211_node *ni, *tni; 1356138568Ssam 1357138568Ssam IEEE80211_NODE_LOCK(nt); 1358138568Ssam ni = ic->ic_bss; 1359138568Ssam /* XXX belongs elsewhere */ 1360138568Ssam if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) { 1361138568Ssam m_freem(ni->ni_rxfrag[0]); 1362138568Ssam ni->ni_rxfrag[0] = NULL; 1363138568Ssam } 1364138568Ssam TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) { 1365138568Ssam if (ni->ni_inact && --ni->ni_inact == 0) { 1366138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1367138568Ssam "[%s] scan candidate purged from cache " 1368138568Ssam "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr), 1369140766Ssam ieee80211_node_refcnt(ni)); 1370138568Ssam node_reclaim(nt, ni); 1371138568Ssam } 1372138568Ssam } 1373138568Ssam IEEE80211_NODE_UNLOCK(nt); 1374138568Ssam 1375138568Ssam nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1376138568Ssam} 1377138568Ssam 1378138568Ssam/* 1379138568Ssam * Timeout inactive stations and do related housekeeping. 1380138568Ssam * Note that we cannot hold the node lock while sending a 1381138568Ssam * frame as this would lead to a LOR. Instead we use a 1382138568Ssam * generation number to mark nodes that we've scanned and 1383138568Ssam * drop the lock and restart a scan if we have to time out 1384138568Ssam * a node. Since we are single-threaded by virtue of 1385120483Ssam * controlling the inactivity timer we can be sure this will 1386120483Ssam * process each node only once. 1387120483Ssam */ 1388138568Ssamstatic void 1389138568Ssamieee80211_timeout_stations(struct ieee80211_node_table *nt) 1390116742Ssam{ 1391138568Ssam struct ieee80211com *ic = nt->nt_ic; 1392120483Ssam struct ieee80211_node *ni; 1393138568Ssam u_int gen; 1394116742Ssam 1395138568Ssam IEEE80211_SCAN_LOCK(nt); 1396138568Ssam gen = nt->nt_scangen++; 1397138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1398140766Ssam "%s: %s scangen %u\n", __func__, nt->nt_name, gen); 1399120483Ssamrestart: 1400138568Ssam IEEE80211_NODE_LOCK(nt); 1401138568Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1402120483Ssam if (ni->ni_scangen == gen) /* previously handled */ 1403120483Ssam continue; 1404120483Ssam ni->ni_scangen = gen; 1405138568Ssam /* 1406147788Ssam * Ignore entries for which have yet to receive an 1407147788Ssam * authentication frame. These are transient and 1408147788Ssam * will be reclaimed when the last reference to them 1409147788Ssam * goes away (when frame xmits complete). 1410147788Ssam */ 1411147788Ssam if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0) 1412147788Ssam continue; 1413147788Ssam /* 1414138568Ssam * Free fragment if not needed anymore 1415138568Ssam * (last fragment older than 1s). 1416138568Ssam * XXX doesn't belong here 1417138568Ssam */ 1418138568Ssam if (ni->ni_rxfrag[0] != NULL && 1419138568Ssam ticks > ni->ni_rxfragstamp + hz) { 1420138568Ssam m_freem(ni->ni_rxfrag[0]); 1421138568Ssam ni->ni_rxfrag[0] = NULL; 1422138568Ssam } 1423140498Ssam /* 1424140498Ssam * Special case ourself; we may be idle for extended periods 1425140498Ssam * of time and regardless reclaiming our state is wrong. 1426140498Ssam */ 1427140498Ssam if (ni == ic->ic_bss) 1428140498Ssam continue; 1429138568Ssam ni->ni_inact--; 1430138568Ssam if (ni->ni_associd != 0) { 1431119150Ssam /* 1432138568Ssam * Age frames on the power save queue. The 1433138568Ssam * aging interval is 4 times the listen 1434138568Ssam * interval specified by the station. This 1435138568Ssam * number is factored into the age calculations 1436138568Ssam * when the frame is placed on the queue. We 1437138568Ssam * store ages as time differences we can check 1438138568Ssam * and/or adjust only the head of the list. 1439138568Ssam */ 1440138568Ssam if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) { 1441138568Ssam struct mbuf *m; 1442138568Ssam int discard = 0; 1443138568Ssam 1444138568Ssam IEEE80211_NODE_SAVEQ_LOCK(ni); 1445138568Ssam while (IF_POLL(&ni->ni_savedq, m) != NULL && 1446138568Ssam M_AGE_GET(m) < IEEE80211_INACT_WAIT) { 1447138568SsamIEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/ 1448138568Ssam _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m); 1449138568Ssam m_freem(m); 1450138568Ssam discard++; 1451138568Ssam } 1452138568Ssam if (m != NULL) 1453138568Ssam M_AGE_SUB(m, IEEE80211_INACT_WAIT); 1454138568Ssam IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1455138568Ssam 1456138568Ssam if (discard != 0) { 1457138568Ssam IEEE80211_DPRINTF(ic, 1458138568Ssam IEEE80211_MSG_POWER, 1459138568Ssam "[%s] discard %u frames for age\n", 1460138568Ssam ether_sprintf(ni->ni_macaddr), 1461138568Ssam discard); 1462138568Ssam IEEE80211_NODE_STAT_ADD(ni, 1463138568Ssam ps_discard, discard); 1464138568Ssam if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) 1465138568Ssam ic->ic_set_tim(ic, ni, 0); 1466138568Ssam } 1467138568Ssam } 1468138568Ssam /* 1469138568Ssam * Probe the station before time it out. We 1470138568Ssam * send a null data frame which may not be 1471138568Ssam * universally supported by drivers (need it 1472138568Ssam * for ps-poll support so it should be...). 1473138568Ssam */ 1474139528Ssam if (0 < ni->ni_inact && 1475139528Ssam ni->ni_inact <= ic->ic_inact_probe) { 1476138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1477138568Ssam "[%s] probe station due to inactivity\n", 1478138568Ssam ether_sprintf(ni->ni_macaddr)); 1479138568Ssam IEEE80211_NODE_UNLOCK(nt); 1480138568Ssam ieee80211_send_nulldata(ic, ni); 1481138568Ssam /* XXX stat? */ 1482138568Ssam goto restart; 1483138568Ssam } 1484138568Ssam } 1485138568Ssam if (ni->ni_inact <= 0) { 1486138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1487138568Ssam "[%s] station timed out due to inactivity " 1488138568Ssam "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr), 1489138568Ssam ieee80211_node_refcnt(ni)); 1490138568Ssam /* 1491138568Ssam * Send a deauthenticate frame and drop the station. 1492138568Ssam * This is somewhat complicated due to reference counts 1493138568Ssam * and locking. At this point a station will typically 1494138568Ssam * have a reference count of 1. ieee80211_node_leave 1495138568Ssam * will do a "free" of the node which will drop the 1496138568Ssam * reference count. But in the meantime a reference 1497138568Ssam * wil be held by the deauth frame. The actual reclaim 1498138568Ssam * of the node will happen either after the tx is 1499138568Ssam * completed or by ieee80211_node_leave. 1500120483Ssam * 1501138568Ssam * Separately we must drop the node lock before sending 1502138568Ssam * in case the driver takes a lock, as this will result 1503138568Ssam * in LOR between the node lock and the driver lock. 1504119150Ssam */ 1505138568Ssam IEEE80211_NODE_UNLOCK(nt); 1506138568Ssam if (ni->ni_associd != 0) { 1507138568Ssam IEEE80211_SEND_MGMT(ic, ni, 1508138568Ssam IEEE80211_FC0_SUBTYPE_DEAUTH, 1509138568Ssam IEEE80211_REASON_AUTH_EXPIRE); 1510138568Ssam } 1511138568Ssam ieee80211_node_leave(ic, ni); 1512121180Ssam ic->ic_stats.is_node_timeout++; 1513120483Ssam goto restart; 1514120483Ssam } 1515116742Ssam } 1516138568Ssam IEEE80211_NODE_UNLOCK(nt); 1517138568Ssam 1518138568Ssam IEEE80211_SCAN_UNLOCK(nt); 1519138568Ssam 1520138568Ssam nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1521116742Ssam} 1522116742Ssam 1523116742Ssamvoid 1524138568Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg) 1525116742Ssam{ 1526116742Ssam struct ieee80211_node *ni; 1527138568Ssam u_int gen; 1528116742Ssam 1529138568Ssam IEEE80211_SCAN_LOCK(nt); 1530138568Ssam gen = nt->nt_scangen++; 1531138568Ssamrestart: 1532138568Ssam IEEE80211_NODE_LOCK(nt); 1533138568Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1534138568Ssam if (ni->ni_scangen != gen) { 1535138568Ssam ni->ni_scangen = gen; 1536138568Ssam (void) ieee80211_ref_node(ni); 1537138568Ssam IEEE80211_NODE_UNLOCK(nt); 1538138568Ssam (*f)(arg, ni); 1539138568Ssam ieee80211_free_node(ni); 1540138568Ssam goto restart; 1541138568Ssam } 1542138568Ssam } 1543138568Ssam IEEE80211_NODE_UNLOCK(nt); 1544138568Ssam 1545138568Ssam IEEE80211_SCAN_UNLOCK(nt); 1546116742Ssam} 1547138568Ssam 1548138568Ssamvoid 1549138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1550138568Ssam{ 1551138568Ssam printf("0x%p: mac %s refcnt %d\n", ni, 1552138568Ssam ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 1553138568Ssam printf("\tscangen %u authmode %u flags 0x%x\n", 1554138568Ssam ni->ni_scangen, ni->ni_authmode, ni->ni_flags); 1555138568Ssam printf("\tassocid 0x%x txpower %u vlan %u\n", 1556138568Ssam ni->ni_associd, ni->ni_txpower, ni->ni_vlan); 1557138568Ssam printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n", 1558138568Ssam ni->ni_txseqs[0], 1559138568Ssam ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT, 1560138568Ssam ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK, 1561138568Ssam ni->ni_rxfragstamp); 1562138568Ssam printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n", 1563138568Ssam ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo); 1564138568Ssam printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n", 1565138568Ssam ether_sprintf(ni->ni_bssid), 1566138568Ssam ni->ni_esslen, ni->ni_essid, 1567138568Ssam ni->ni_chan->ic_freq, ni->ni_chan->ic_flags); 1568138568Ssam printf("\tfails %u inact %u txrate %u\n", 1569138568Ssam ni->ni_fails, ni->ni_inact, ni->ni_txrate); 1570138568Ssam} 1571138568Ssam 1572138568Ssamvoid 1573138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt) 1574138568Ssam{ 1575138568Ssam ieee80211_iterate_nodes(nt, 1576138568Ssam (ieee80211_iter_func *) ieee80211_dump_node, nt); 1577138568Ssam} 1578138568Ssam 1579138568Ssam/* 1580138568Ssam * Handle a station joining an 11g network. 1581138568Ssam */ 1582138568Ssamstatic void 1583138568Ssamieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 1584138568Ssam{ 1585138568Ssam 1586138568Ssam /* 1587138568Ssam * Station isn't capable of short slot time. Bump 1588138568Ssam * the count of long slot time stations and disable 1589138568Ssam * use of short slot time. Note that the actual switch 1590138568Ssam * over to long slot time use may not occur until the 1591138568Ssam * next beacon transmission (per sec. 7.3.1.4 of 11g). 1592138568Ssam */ 1593138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 1594138568Ssam ic->ic_longslotsta++; 1595138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1596138568Ssam "[%s] station needs long slot time, count %d\n", 1597138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 1598138568Ssam /* XXX vap's w/ conflicting needs won't work */ 1599138568Ssam ieee80211_set_shortslottime(ic, 0); 1600138568Ssam } 1601138568Ssam /* 1602138568Ssam * If the new station is not an ERP station 1603138568Ssam * then bump the counter and enable protection 1604138568Ssam * if configured. 1605138568Ssam */ 1606138568Ssam if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) { 1607138568Ssam ic->ic_nonerpsta++; 1608138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1609138568Ssam "[%s] station is !ERP, %d non-ERP stations associated\n", 1610138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 1611138568Ssam /* 1612138568Ssam * If protection is configured, enable it. 1613138568Ssam */ 1614138568Ssam if (ic->ic_protmode != IEEE80211_PROT_NONE) { 1615138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1616138568Ssam "%s: enable use of protection\n", __func__); 1617138568Ssam ic->ic_flags |= IEEE80211_F_USEPROT; 1618138568Ssam } 1619138568Ssam /* 1620138568Ssam * If station does not support short preamble 1621138568Ssam * then we must enable use of Barker preamble. 1622138568Ssam */ 1623138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) { 1624138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1625138568Ssam "[%s] station needs long preamble\n", 1626138568Ssam ether_sprintf(ni->ni_macaddr)); 1627138568Ssam ic->ic_flags |= IEEE80211_F_USEBARKER; 1628138568Ssam ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 1629138568Ssam } 1630138568Ssam } else 1631138568Ssam ni->ni_flags |= IEEE80211_NODE_ERP; 1632138568Ssam} 1633138568Ssam 1634138568Ssamvoid 1635138568Ssamieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp) 1636138568Ssam{ 1637138568Ssam int newassoc; 1638138568Ssam 1639138568Ssam if (ni->ni_associd == 0) { 1640138568Ssam u_int16_t aid; 1641138568Ssam 1642138568Ssam /* 1643138568Ssam * It would be good to search the bitmap 1644138568Ssam * more efficiently, but this will do for now. 1645138568Ssam */ 1646138568Ssam for (aid = 1; aid < ic->ic_max_aid; aid++) { 1647138568Ssam if (!IEEE80211_AID_ISSET(aid, 1648138568Ssam ic->ic_aid_bitmap)) 1649138568Ssam break; 1650138568Ssam } 1651138568Ssam if (aid >= ic->ic_max_aid) { 1652138568Ssam IEEE80211_SEND_MGMT(ic, ni, resp, 1653138568Ssam IEEE80211_REASON_ASSOC_TOOMANY); 1654138568Ssam ieee80211_node_leave(ic, ni); 1655138568Ssam return; 1656138568Ssam } 1657138568Ssam ni->ni_associd = aid | 0xc000; 1658138568Ssam IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap); 1659138568Ssam ic->ic_sta_assoc++; 1660138568Ssam newassoc = 1; 1661139522Ssam if (ic->ic_curmode == IEEE80211_MODE_11G || 1662139522Ssam ic->ic_curmode == IEEE80211_MODE_TURBO_G) 1663138568Ssam ieee80211_node_join_11g(ic, ni); 1664138568Ssam } else 1665138568Ssam newassoc = 0; 1666138568Ssam 1667138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 1668139523Ssam "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n", 1669139523Ssam ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re", 1670139523Ssam IEEE80211_NODE_AID(ni), 1671139523Ssam ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long", 1672139523Ssam ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", 1673139523Ssam ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "", 1674139523Ssam ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "" 1675139523Ssam ); 1676138568Ssam 1677138568Ssam /* give driver a chance to setup state like ni_txrate */ 1678139524Ssam if (ic->ic_newassoc != NULL) 1679138568Ssam ic->ic_newassoc(ic, ni, newassoc); 1680139528Ssam ni->ni_inact_reload = ic->ic_inact_auth; 1681139528Ssam ni->ni_inact = ni->ni_inact_reload; 1682138568Ssam IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS); 1683138568Ssam /* tell the authenticator about new station */ 1684138568Ssam if (ic->ic_auth->ia_node_join != NULL) 1685138568Ssam ic->ic_auth->ia_node_join(ic, ni); 1686138568Ssam ieee80211_notify_node_join(ic, ni, newassoc); 1687138568Ssam} 1688138568Ssam 1689138568Ssam/* 1690138568Ssam * Handle a station leaving an 11g network. 1691138568Ssam */ 1692138568Ssamstatic void 1693138568Ssamieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 1694138568Ssam{ 1695138568Ssam 1696139522Ssam KASSERT(ic->ic_curmode == IEEE80211_MODE_11G || 1697139522Ssam ic->ic_curmode == IEEE80211_MODE_TURBO_G, 1698138568Ssam ("not in 11g, curmode %x", ic->ic_curmode)); 1699138568Ssam 1700138568Ssam /* 1701138568Ssam * If a long slot station do the slot time bookkeeping. 1702138568Ssam */ 1703138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 1704138568Ssam KASSERT(ic->ic_longslotsta > 0, 1705138568Ssam ("bogus long slot station count %d", ic->ic_longslotsta)); 1706138568Ssam ic->ic_longslotsta--; 1707138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1708138568Ssam "[%s] long slot time station leaves, count now %d\n", 1709138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 1710138568Ssam if (ic->ic_longslotsta == 0) { 1711138568Ssam /* 1712138568Ssam * Re-enable use of short slot time if supported 1713138568Ssam * and not operating in IBSS mode (per spec). 1714138568Ssam */ 1715138568Ssam if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 1716138568Ssam ic->ic_opmode != IEEE80211_M_IBSS) { 1717138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1718138568Ssam "%s: re-enable use of short slot time\n", 1719138568Ssam __func__); 1720138568Ssam ieee80211_set_shortslottime(ic, 1); 1721138568Ssam } 1722138568Ssam } 1723138568Ssam } 1724138568Ssam /* 1725138568Ssam * If a non-ERP station do the protection-related bookkeeping. 1726138568Ssam */ 1727138568Ssam if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) { 1728138568Ssam KASSERT(ic->ic_nonerpsta > 0, 1729138568Ssam ("bogus non-ERP station count %d", ic->ic_nonerpsta)); 1730138568Ssam ic->ic_nonerpsta--; 1731138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1732138568Ssam "[%s] non-ERP station leaves, count now %d\n", 1733138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 1734138568Ssam if (ic->ic_nonerpsta == 0) { 1735138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1736138568Ssam "%s: disable use of protection\n", __func__); 1737138568Ssam ic->ic_flags &= ~IEEE80211_F_USEPROT; 1738138568Ssam /* XXX verify mode? */ 1739138568Ssam if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) { 1740138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1741138568Ssam "%s: re-enable use of short preamble\n", 1742138568Ssam __func__); 1743138568Ssam ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 1744138568Ssam ic->ic_flags &= ~IEEE80211_F_USEBARKER; 1745138568Ssam } 1746138568Ssam } 1747138568Ssam } 1748138568Ssam} 1749138568Ssam 1750138568Ssam/* 1751138568Ssam * Handle bookkeeping for station deauthentication/disassociation 1752138568Ssam * when operating as an ap. 1753138568Ssam */ 1754138568Ssamvoid 1755138568Ssamieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 1756138568Ssam{ 1757140499Ssam struct ieee80211_node_table *nt = ni->ni_table; 1758138568Ssam 1759138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 1760138568Ssam "[%s] station with aid %d leaves\n", 1761138568Ssam ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni)); 1762138568Ssam 1763138568Ssam KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 1764138568Ssam ic->ic_opmode == IEEE80211_M_IBSS || 1765138568Ssam ic->ic_opmode == IEEE80211_M_AHDEMO, 1766138568Ssam ("unexpected operating mode %u", ic->ic_opmode)); 1767138568Ssam /* 1768138568Ssam * If node wasn't previously associated all 1769138568Ssam * we need to do is reclaim the reference. 1770138568Ssam */ 1771138568Ssam /* XXX ibss mode bypasses 11g and notification */ 1772138568Ssam if (ni->ni_associd == 0) 1773138568Ssam goto done; 1774138568Ssam /* 1775138568Ssam * Tell the authenticator the station is leaving. 1776138568Ssam * Note that we must do this before yanking the 1777138568Ssam * association id as the authenticator uses the 1778138568Ssam * associd to locate it's state block. 1779138568Ssam */ 1780138568Ssam if (ic->ic_auth->ia_node_leave != NULL) 1781138568Ssam ic->ic_auth->ia_node_leave(ic, ni); 1782138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1783138568Ssam ni->ni_associd = 0; 1784138568Ssam ic->ic_sta_assoc--; 1785138568Ssam 1786139522Ssam if (ic->ic_curmode == IEEE80211_MODE_11G || 1787139522Ssam ic->ic_curmode == IEEE80211_MODE_TURBO_G) 1788138568Ssam ieee80211_node_leave_11g(ic, ni); 1789138568Ssam /* 1790138568Ssam * Cleanup station state. In particular clear various 1791138568Ssam * state that might otherwise be reused if the node 1792138568Ssam * is reused before the reference count goes to zero 1793138568Ssam * (and memory is reclaimed). 1794138568Ssam */ 1795138568Ssam ieee80211_sta_leave(ic, ni); 1796138568Ssamdone: 1797140499Ssam /* 1798140499Ssam * Remove the node from any table it's recorded in and 1799140499Ssam * drop the caller's reference. Removal from the table 1800140499Ssam * is important to insure the node is not reprocessed 1801140499Ssam * for inactivity. 1802140499Ssam */ 1803140499Ssam if (nt != NULL) { 1804140499Ssam IEEE80211_NODE_LOCK(nt); 1805140499Ssam node_reclaim(nt, ni); 1806140499Ssam IEEE80211_NODE_UNLOCK(nt); 1807140499Ssam } else 1808140499Ssam ieee80211_free_node(ni); 1809138568Ssam} 1810138568Ssam 1811138568Ssamu_int8_t 1812138568Ssamieee80211_getrssi(struct ieee80211com *ic) 1813138568Ssam{ 1814138568Ssam#define NZ(x) ((x) == 0 ? 1 : (x)) 1815140753Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 1816138568Ssam u_int32_t rssi_samples, rssi_total; 1817138568Ssam struct ieee80211_node *ni; 1818138568Ssam 1819138568Ssam rssi_total = 0; 1820138568Ssam rssi_samples = 0; 1821138568Ssam switch (ic->ic_opmode) { 1822138568Ssam case IEEE80211_M_IBSS: /* average of all ibss neighbors */ 1823138568Ssam /* XXX locking */ 1824140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 1825138568Ssam if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) { 1826138568Ssam rssi_samples++; 1827138568Ssam rssi_total += ic->ic_node_getrssi(ni); 1828138568Ssam } 1829138568Ssam break; 1830138568Ssam case IEEE80211_M_AHDEMO: /* average of all neighbors */ 1831138568Ssam /* XXX locking */ 1832140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1833138568Ssam rssi_samples++; 1834138568Ssam rssi_total += ic->ic_node_getrssi(ni); 1835138568Ssam } 1836138568Ssam break; 1837138568Ssam case IEEE80211_M_HOSTAP: /* average of all associated stations */ 1838138568Ssam /* XXX locking */ 1839140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 1840138568Ssam if (IEEE80211_AID(ni->ni_associd) != 0) { 1841138568Ssam rssi_samples++; 1842138568Ssam rssi_total += ic->ic_node_getrssi(ni); 1843138568Ssam } 1844138568Ssam break; 1845138568Ssam case IEEE80211_M_MONITOR: /* XXX */ 1846138568Ssam case IEEE80211_M_STA: /* use stats from associated ap */ 1847138568Ssam default: 1848138568Ssam if (ic->ic_bss != NULL) 1849138568Ssam rssi_total = ic->ic_node_getrssi(ic->ic_bss); 1850138568Ssam rssi_samples = 1; 1851138568Ssam break; 1852138568Ssam } 1853138568Ssam return rssi_total / NZ(rssi_samples); 1854138568Ssam#undef NZ 1855138568Ssam} 1856138568Ssam 1857138568Ssam/* 1858138568Ssam * Indicate whether there are frames queued for a station in power-save mode. 1859138568Ssam */ 1860138568Ssamstatic void 1861138568Ssamieee80211_set_tim(struct ieee80211com *ic, struct ieee80211_node *ni, int set) 1862138568Ssam{ 1863138568Ssam u_int16_t aid; 1864138568Ssam 1865138568Ssam KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 1866138568Ssam ic->ic_opmode == IEEE80211_M_IBSS, 1867138568Ssam ("operating mode %u", ic->ic_opmode)); 1868138568Ssam 1869138568Ssam aid = IEEE80211_AID(ni->ni_associd); 1870138568Ssam KASSERT(aid < ic->ic_max_aid, 1871138568Ssam ("bogus aid %u, max %u", aid, ic->ic_max_aid)); 1872138568Ssam 1873138568Ssam IEEE80211_BEACON_LOCK(ic); 1874138568Ssam if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) { 1875138568Ssam if (set) { 1876138568Ssam setbit(ic->ic_tim_bitmap, aid); 1877138568Ssam ic->ic_ps_pending++; 1878138568Ssam } else { 1879138568Ssam clrbit(ic->ic_tim_bitmap, aid); 1880138568Ssam ic->ic_ps_pending--; 1881138568Ssam } 1882138568Ssam ic->ic_flags |= IEEE80211_F_TIMUPDATE; 1883138568Ssam } 1884138568Ssam IEEE80211_BEACON_UNLOCK(ic); 1885138568Ssam} 1886138568Ssam 1887138568Ssam/* 1888138568Ssam * Node table support. 1889138568Ssam */ 1890138568Ssam 1891138568Ssamstatic void 1892138568Ssamieee80211_node_table_init(struct ieee80211com *ic, 1893138568Ssam struct ieee80211_node_table *nt, 1894138568Ssam const char *name, int inact, 1895138568Ssam void (*timeout)(struct ieee80211_node_table *)) 1896138568Ssam{ 1897138568Ssam 1898138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1899138568Ssam "%s %s table, inact %u\n", __func__, name, inact); 1900138568Ssam 1901138568Ssam nt->nt_ic = ic; 1902138568Ssam /* XXX need unit */ 1903138568Ssam IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname); 1904138568Ssam IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname); 1905138568Ssam TAILQ_INIT(&nt->nt_node); 1906138568Ssam nt->nt_name = name; 1907138568Ssam nt->nt_scangen = 1; 1908138568Ssam nt->nt_inact_init = inact; 1909138568Ssam nt->nt_timeout = timeout; 1910138568Ssam} 1911138568Ssam 1912138568Ssamvoid 1913138568Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt) 1914138568Ssam{ 1915138568Ssam 1916138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1917138568Ssam "%s %s table\n", __func__, nt->nt_name); 1918138568Ssam 1919138568Ssam IEEE80211_NODE_LOCK(nt); 1920138568Ssam nt->nt_inact_timer = 0; 1921138568Ssam ieee80211_free_allnodes_locked(nt); 1922138568Ssam IEEE80211_NODE_UNLOCK(nt); 1923138568Ssam} 1924138568Ssam 1925138568Ssamstatic void 1926138568Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt) 1927138568Ssam{ 1928138568Ssam 1929138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1930138568Ssam "%s %s table\n", __func__, nt->nt_name); 1931138568Ssam 1932138568Ssam ieee80211_free_allnodes_locked(nt); 1933138568Ssam IEEE80211_SCAN_LOCK_DESTROY(nt); 1934138568Ssam IEEE80211_NODE_LOCK_DESTROY(nt); 1935138568Ssam} 1936