ieee80211_node.c revision 170360
1116742Ssam/*- 2116904Ssam * Copyright (c) 2001 Atsushi Onoe 3170360Ssam * Copyright (c) 2002-2007 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. 14116742Ssam * 15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25116742Ssam */ 26116742Ssam 27116742Ssam#include <sys/cdefs.h> 28116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 170360 2007-06-06 04:56:04Z sam $"); 29116742Ssam 30116742Ssam#include <sys/param.h> 31116742Ssam#include <sys/systm.h> 32116742Ssam#include <sys/mbuf.h> 33116742Ssam#include <sys/malloc.h> 34116742Ssam#include <sys/kernel.h> 35138568Ssam 36116742Ssam#include <sys/socket.h> 37116742Ssam 38116742Ssam#include <net/if.h> 39116742Ssam#include <net/if_media.h> 40116742Ssam#include <net/ethernet.h> 41116742Ssam 42116742Ssam#include <net80211/ieee80211_var.h> 43116742Ssam 44116742Ssam#include <net/bpf.h> 45116742Ssam 46147221Ssam/* 47147221Ssam * Association id's are managed with a bit vector. 48147221Ssam */ 49147221Ssam#define IEEE80211_AID_SET(b, w) \ 50147221Ssam ((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32))) 51147221Ssam#define IEEE80211_AID_CLR(b, w) \ 52147221Ssam ((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32))) 53147221Ssam#define IEEE80211_AID_ISSET(b, w) \ 54147221Ssam ((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32))) 55147221Ssam 56159139Sdds#ifdef IEEE80211_DEBUG_REFCNT 57159139Sdds#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line 58159139Sdds#else 59159139Sdds#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__ 60159139Sdds#endif 61159139Sdds 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; 227165569Ssam ni->ni_rates = *ieee80211_get_suprates(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; 324156358Ssam ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN; 325138568Ssam 326148936Ssam chan = ic->ic_curchan; 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__, 334148936Ssam ieee80211_chan2ieee(ic, ic->ic_curchan), 335138568Ssam ieee80211_chan2ieee(ic, chan)); 336148936Ssam ic->ic_curchan = chan; 337148936Ssam /* 338148936Ssam * XXX drivers should do this as needed, 339148936Ssam * XXX for now maintain compatibility 340148936Ssam */ 341165569Ssam ic->ic_bss->ni_rates = *ieee80211_get_suprates(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 350156358Ssam/* 351156358Ssam * Probe the curent channel, if allowed, while scanning. 352156358Ssam * If the channel is not marked passive-only then send 353156358Ssam * a probe request immediately. Otherwise mark state and 354156358Ssam * listen for beacons on the channel; if we receive something 355156358Ssam * then we'll transmit a probe request. 356156358Ssam */ 357156358Ssamvoid 358156358Ssamieee80211_probe_curchan(struct ieee80211com *ic, int force) 359156358Ssam{ 360156358Ssam struct ifnet *ifp = ic->ic_ifp; 361156358Ssam 362156358Ssam if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) { 363156358Ssam /* 364156358Ssam * XXX send both broadcast+directed probe request 365156358Ssam */ 366156358Ssam ieee80211_send_probereq(ic->ic_bss, 367156358Ssam ic->ic_myaddr, ifp->if_broadcastaddr, 368156358Ssam ifp->if_broadcastaddr, 369156358Ssam ic->ic_des_essid, ic->ic_des_esslen, 370156358Ssam ic->ic_opt_ie, ic->ic_opt_ie_len); 371156358Ssam } else 372156358Ssam ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN; 373156358Ssam} 374156358Ssam 375141658Ssamstatic __inline void 376141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss) 377141658Ssam{ 378141658Ssam /* propagate useful state */ 379141658Ssam nbss->ni_authmode = obss->ni_authmode; 380141658Ssam nbss->ni_txpower = obss->ni_txpower; 381141658Ssam nbss->ni_vlan = obss->ni_vlan; 382141658Ssam nbss->ni_rsn = obss->ni_rsn; 383141658Ssam /* XXX statistics? */ 384141658Ssam} 385141658Ssam 386116742Ssamvoid 387116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 388116742Ssam{ 389140753Ssam struct ieee80211_node_table *nt; 390116742Ssam struct ieee80211_node *ni; 391116742Ssam 392138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 393138568Ssam "%s: creating ibss\n", __func__); 394138568Ssam 395138568Ssam /* 396138568Ssam * Create the station/neighbor table. Note that for adhoc 397138568Ssam * mode we make the initial inactivity timer longer since 398138568Ssam * we create nodes only through discovery and they typically 399138568Ssam * are long-lived associations. 400138568Ssam */ 401140753Ssam nt = &ic->ic_sta; 402140753Ssam IEEE80211_NODE_LOCK(nt); 403140753Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 404140753Ssam nt->nt_name = "station"; 405140753Ssam nt->nt_inact_init = ic->ic_inact_init; 406140753Ssam } else { 407140753Ssam nt->nt_name = "neighbor"; 408140753Ssam nt->nt_inact_init = ic->ic_inact_run; 409140753Ssam } 410140753Ssam IEEE80211_NODE_UNLOCK(nt); 411140753Ssam 412148863Ssam ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr); 413140753Ssam if (ni == NULL) { 414140753Ssam /* XXX recovery? */ 415138568Ssam return; 416138568Ssam } 417116742Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 418116742Ssam ni->ni_esslen = ic->ic_des_esslen; 419116742Ssam memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 420141658Ssam copy_bss(ni, ic->ic_bss); 421148843Ssam ni->ni_intval = ic->ic_bintval; 422138568Ssam if (ic->ic_flags & IEEE80211_F_PRIVACY) 423116742Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 424116742Ssam if (ic->ic_phytype == IEEE80211_T_FH) { 425116742Ssam ni->ni_fhdwell = 200; /* XXX */ 426116742Ssam ni->ni_fhindex = 1; 427116742Ssam } 428138568Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 429138568Ssam ic->ic_flags |= IEEE80211_F_SIBSS; 430138568Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */ 431143300Ssam if (ic->ic_flags & IEEE80211_F_DESBSSID) 432143300Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid); 433167282Ssam else { 434167282Ssam get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN); 435167282Ssam /* clear group bit, add local bit */ 436167282Ssam ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02; 437167282Ssam } 438153403Ssam } else if (ic->ic_opmode == IEEE80211_M_AHDEMO) { 439153403Ssam if (ic->ic_flags & IEEE80211_F_DESBSSID) 440153403Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid); 441153403Ssam else 442153403Ssam memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN); 443138568Ssam } 444138568Ssam /* 445138568Ssam * Fix the channel and related attributes. 446138568Ssam */ 447138568Ssam ieee80211_set_chan(ic, ni, chan); 448148936Ssam ic->ic_curchan = chan; 449138568Ssam ic->ic_curmode = ieee80211_chan2mode(ic, chan); 450138568Ssam /* 451138568Ssam * Do mode-specific rate setup. 452138568Ssam */ 453166012Ssam if (IEEE80211_IS_CHAN_FULL(chan) && 454166012Ssam (ic->ic_curmode == IEEE80211_MODE_11G || 455166012Ssam ic->ic_curmode == IEEE80211_MODE_11B)) 456166012Ssam ieee80211_set11gbasicrates(&ni->ni_rates, ic->ic_curmode); 457138568Ssam 458140753Ssam (void) ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 459116742Ssam} 460116742Ssam 461138568Ssamvoid 462138568Ssamieee80211_reset_bss(struct ieee80211com *ic) 463138568Ssam{ 464138568Ssam struct ieee80211_node *ni, *obss; 465138568Ssam 466138568Ssam ieee80211_node_table_reset(&ic->ic_scan); 467140753Ssam ieee80211_node_table_reset(&ic->ic_sta); 468140753Ssam 469138568Ssam ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr); 470138568Ssam KASSERT(ni != NULL, ("unable to setup inital BSS node")); 471138568Ssam obss = ic->ic_bss; 472138568Ssam ic->ic_bss = ieee80211_ref_node(ni); 473141658Ssam if (obss != NULL) { 474141658Ssam copy_bss(ni, obss); 475148843Ssam ni->ni_intval = ic->ic_bintval; 476138568Ssam ieee80211_free_node(obss); 477141658Ssam } 478138568Ssam} 479138568Ssam 480148432Ssam/* XXX tunable */ 481148432Ssam#define STA_FAILS_MAX 2 /* assoc failures before ignored */ 482148432Ssam 483127767Ssamstatic int 484138568Ssamieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni) 485127767Ssam{ 486127767Ssam u_int8_t rate; 487127767Ssam int fail; 488127767Ssam 489127767Ssam fail = 0; 490127767Ssam if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 491127767Ssam fail |= 0x01; 492127767Ssam if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 493127767Ssam ni->ni_chan != ic->ic_des_chan) 494127767Ssam fail |= 0x01; 495127767Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 496127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 497127767Ssam fail |= 0x02; 498127767Ssam } else { 499127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 500127767Ssam fail |= 0x02; 501127767Ssam } 502138568Ssam if (ic->ic_flags & IEEE80211_F_PRIVACY) { 503127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 504127767Ssam fail |= 0x04; 505127767Ssam } else { 506127767Ssam /* XXX does this mean privacy is supported or required? */ 507127767Ssam if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 508127767Ssam fail |= 0x04; 509127767Ssam } 510167442Ssam rate = ieee80211_fix_rate(ni, &ni->ni_rates, 511165887Ssam IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 512127767Ssam if (rate & IEEE80211_RATE_BASIC) 513127767Ssam fail |= 0x08; 514127767Ssam if (ic->ic_des_esslen != 0 && 515127767Ssam (ni->ni_esslen != ic->ic_des_esslen || 516127767Ssam memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) 517127767Ssam fail |= 0x10; 518127767Ssam if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 519127767Ssam !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 520127767Ssam fail |= 0x20; 521148432Ssam if (ni->ni_fails >= STA_FAILS_MAX) 522148432Ssam fail |= 0x40; 523127767Ssam#ifdef IEEE80211_DEBUG 524138568Ssam if (ieee80211_msg_scan(ic)) { 525148432Ssam printf(" %c %s", 526148432Ssam fail & 0x40 ? '=' : fail & 0x80 ? '^' : fail ? '-' : '+', 527127767Ssam ether_sprintf(ni->ni_macaddr)); 528127767Ssam printf(" %s%c", ether_sprintf(ni->ni_bssid), 529127767Ssam fail & 0x20 ? '!' : ' '); 530127767Ssam printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan), 531127767Ssam fail & 0x01 ? '!' : ' '); 532127767Ssam printf(" %+4d", ni->ni_rssi); 533127767Ssam printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 534127767Ssam fail & 0x08 ? '!' : ' '); 535127767Ssam printf(" %4s%c", 536127767Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 537127767Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 538127767Ssam "????", 539127767Ssam fail & 0x02 ? '!' : ' '); 540127767Ssam printf(" %3s%c ", 541127767Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 542127767Ssam "wep" : "no", 543127767Ssam fail & 0x04 ? '!' : ' '); 544127767Ssam ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 545127767Ssam printf("%s\n", fail & 0x10 ? "!" : ""); 546127767Ssam } 547127767Ssam#endif 548127767Ssam return fail; 549127767Ssam} 550127767Ssam 551138568Ssamstatic __inline u_int8_t 552138568Ssammaxrate(const struct ieee80211_node *ni) 553138568Ssam{ 554138568Ssam const struct ieee80211_rateset *rs = &ni->ni_rates; 555138568Ssam /* NB: assumes rate set is sorted (happens on frame receive) */ 556138568Ssam return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL; 557138568Ssam} 558138568Ssam 559116742Ssam/* 560138568Ssam * Compare the capabilities of two nodes and decide which is 561138568Ssam * more desirable (return >0 if a is considered better). Note 562138568Ssam * that we assume compatibility/usability has already been checked 563138568Ssam * so we don't need to (e.g. validate whether privacy is supported). 564138568Ssam * Used to select the best scan candidate for association in a BSS. 565138568Ssam */ 566138568Ssamstatic int 567138568Ssamieee80211_node_compare(struct ieee80211com *ic, 568138568Ssam const struct ieee80211_node *a, 569138568Ssam const struct ieee80211_node *b) 570138568Ssam{ 571138568Ssam u_int8_t maxa, maxb; 572138568Ssam u_int8_t rssia, rssib; 573148432Ssam int weight; 574138568Ssam 575138568Ssam /* privacy support preferred */ 576138568Ssam if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) && 577138568Ssam (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 578138568Ssam return 1; 579138568Ssam if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 && 580138568Ssam (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)) 581138568Ssam return -1; 582138568Ssam 583148432Ssam /* compare count of previous failures */ 584148432Ssam weight = b->ni_fails - a->ni_fails; 585148432Ssam if (abs(weight) > 1) 586148432Ssam return weight; 587148432Ssam 588138568Ssam rssia = ic->ic_node_getrssi(a); 589138568Ssam rssib = ic->ic_node_getrssi(b); 590139543Ssam if (abs(rssib - rssia) < 5) { 591139543Ssam /* best/max rate preferred if signal level close enough XXX */ 592139543Ssam maxa = maxrate(a); 593139543Ssam maxb = maxrate(b); 594139543Ssam if (maxa != maxb) 595139543Ssam return maxa - maxb; 596139543Ssam /* XXX use freq for channel preference */ 597139543Ssam /* for now just prefer 5Ghz band to all other bands */ 598139543Ssam if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) && 599139543Ssam !IEEE80211_IS_CHAN_5GHZ(b->ni_chan)) 600139543Ssam return 1; 601139543Ssam if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) && 602139543Ssam IEEE80211_IS_CHAN_5GHZ(b->ni_chan)) 603139543Ssam return -1; 604139543Ssam } 605138568Ssam /* all things being equal, use signal level */ 606138568Ssam return rssia - rssib; 607138568Ssam} 608138568Ssam 609138568Ssam/* 610140441Ssam * Mark an ongoing scan stopped. 611140441Ssam */ 612140441Ssamvoid 613140441Ssamieee80211_cancel_scan(struct ieee80211com *ic) 614140441Ssam{ 615140441Ssam 616140441Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n", 617140441Ssam __func__, 618140441Ssam (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive"); 619140441Ssam 620140441Ssam ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN); 621156358Ssam ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN; 622140441Ssam} 623140441Ssam 624140441Ssam/* 625116742Ssam * Complete a scan of potential channels. 626116742Ssam */ 627116742Ssamvoid 628138568Ssamieee80211_end_scan(struct ieee80211com *ic) 629116742Ssam{ 630140448Ssam struct ieee80211_node_table *nt = &ic->ic_scan; 631140448Ssam struct ieee80211_node *ni, *selbs; 632116742Ssam 633140441Ssam ieee80211_cancel_scan(ic); 634140441Ssam ieee80211_notify_scan_done(ic); 635116742Ssam 636116742Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 637138568Ssam u_int8_t maxrssi[IEEE80211_CHAN_MAX]; /* XXX off stack? */ 638138568Ssam int i, bestchan; 639138568Ssam u_int8_t rssi; 640138568Ssam 641116742Ssam /* 642116742Ssam * The passive scan to look for existing AP's completed, 643116742Ssam * select a channel to camp on. Identify the channels 644116742Ssam * that already have one or more AP's and try to locate 645140753Ssam * an unoccupied one. If that fails, pick a channel that 646138568Ssam * looks to be quietest. 647116742Ssam */ 648138568Ssam memset(maxrssi, 0, sizeof(maxrssi)); 649140448Ssam IEEE80211_NODE_LOCK(nt); 650140448Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 651138568Ssam rssi = ic->ic_node_getrssi(ni); 652138568Ssam i = ieee80211_chan2ieee(ic, ni->ni_chan); 653138568Ssam if (rssi > maxrssi[i]) 654138568Ssam maxrssi[i] = rssi; 655116742Ssam } 656140448Ssam IEEE80211_NODE_UNLOCK(nt); 657138568Ssam /* XXX select channel more intelligently */ 658138568Ssam bestchan = -1; 659116742Ssam for (i = 0; i < IEEE80211_CHAN_MAX; i++) 660138568Ssam if (isset(ic->ic_chan_active, i)) { 661138568Ssam /* 662138568Ssam * If the channel is unoccupied the max rssi 663138568Ssam * should be zero; just take it. Otherwise 664138568Ssam * track the channel with the lowest rssi and 665138568Ssam * use that when all channels appear occupied. 666138568Ssam */ 667138568Ssam if (maxrssi[i] == 0) { 668138568Ssam bestchan = i; 669116742Ssam break; 670138568Ssam } 671143715Ssam if (bestchan == -1 || 672143715Ssam maxrssi[i] < maxrssi[bestchan]) 673138568Ssam bestchan = i; 674138568Ssam } 675138568Ssam if (bestchan != -1) { 676138568Ssam ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]); 677138568Ssam return; 678116742Ssam } 679138568Ssam /* no suitable channel, should not happen */ 680138568Ssam } 681138568Ssam 682138568Ssam /* 683138568Ssam * When manually sequencing the state machine; scan just once 684138568Ssam * regardless of whether we have a candidate or not. The 685138568Ssam * controlling application is expected to setup state and 686138568Ssam * initiate an association. 687138568Ssam */ 688138568Ssam if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL) 689116742Ssam return; 690138568Ssam /* 691138568Ssam * Automatic sequencing; look for a candidate and 692138568Ssam * if found join the network. 693138568Ssam */ 694140448Ssam /* NB: unlocked read should be ok */ 695140448Ssam if (TAILQ_FIRST(&nt->nt_node) == NULL) { 696138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 697138568Ssam "%s: no scan candidate\n", __func__); 698116742Ssam notfound: 699116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS && 700116742Ssam (ic->ic_flags & IEEE80211_F_IBSSON) && 701116742Ssam ic->ic_des_esslen != 0) { 702116742Ssam ieee80211_create_ibss(ic, ic->ic_ibss_chan); 703116742Ssam return; 704116742Ssam } 705116742Ssam /* 706148432Ssam * Decrement the failure counts so entries will be 707148432Ssam * reconsidered the next time around. We really want 708148432Ssam * to do this only for sta's where we've previously 709153352Ssam * had some success. 710148432Ssam */ 711148432Ssam IEEE80211_NODE_LOCK(nt); 712148432Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 713148432Ssam if (ni->ni_fails) 714148432Ssam ni->ni_fails--; 715148432Ssam IEEE80211_NODE_UNLOCK(nt); 716148432Ssam /* 717116742Ssam * Reset the list of channels to scan and start again. 718116742Ssam */ 719138568Ssam ieee80211_reset_scan(ic); 720138568Ssam ic->ic_flags |= IEEE80211_F_SCAN; 721138568Ssam ieee80211_next_scan(ic); 722116742Ssam return; 723116742Ssam } 724116742Ssam selbs = NULL; 725138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n", 726138568Ssam "macaddr bssid chan rssi rate flag wep essid"); 727140448Ssam IEEE80211_NODE_LOCK(nt); 728140448Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 729138568Ssam if (ieee80211_match_bss(ic, ni) == 0) { 730116742Ssam if (selbs == NULL) 731116742Ssam selbs = ni; 732140448Ssam else if (ieee80211_node_compare(ic, ni, selbs) > 0) 733116742Ssam selbs = ni; 734116742Ssam } 735116742Ssam } 736140448Ssam if (selbs != NULL) /* NB: grab ref while dropping lock */ 737140448Ssam (void) ieee80211_ref_node(selbs); 738140448Ssam IEEE80211_NODE_UNLOCK(nt); 739116742Ssam if (selbs == NULL) 740116742Ssam goto notfound; 741138568Ssam if (!ieee80211_sta_join(ic, selbs)) { 742140448Ssam ieee80211_free_node(selbs); 743138568Ssam goto notfound; 744138568Ssam } 745138568Ssam} 746138568Ssam 747138568Ssam/* 748138568Ssam * Handle 802.11 ad hoc network merge. The 749138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance 750138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match 751138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that 752138568Ssam * has the station's desired SSID. The "oldest" 802.11 network 753138568Ssam * sends beacons with the greatest TSF timestamp. 754138568Ssam * 755138568Ssam * The caller is assumed to validate TSF's before attempting a merge. 756138568Ssam * 757138568Ssam * Return !0 if the BSSID changed, 0 otherwise. 758138568Ssam */ 759138568Ssamint 760148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni) 761138568Ssam{ 762148306Ssam struct ieee80211com *ic = ni->ni_ic; 763138568Ssam 764140453Ssam if (ni == ic->ic_bss || 765140453Ssam IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) { 766138568Ssam /* unchanged, nothing to do */ 767138568Ssam return 0; 768138568Ssam } 769138568Ssam if (ieee80211_match_bss(ic, ni) != 0) { /* capabilities mismatch */ 770138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 771138568Ssam "%s: merge failed, capabilities mismatch\n", __func__); 772138568Ssam ic->ic_stats.is_ibss_capmismatch++; 773138568Ssam return 0; 774138568Ssam } 775138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 776138568Ssam "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__, 777138568Ssam ether_sprintf(ni->ni_bssid), 778138568Ssam ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 779138568Ssam ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long", 780138568Ssam ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "" 781138568Ssam ); 782140753Ssam return ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 783138568Ssam} 784138568Ssam 785138568Ssam/* 786138568Ssam * Join the specified IBSS/BSS network. The node is assumed to 787138568Ssam * be passed in with a held reference. 788138568Ssam */ 789138568Ssamint 790138568Ssamieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs) 791138568Ssam{ 792138568Ssam struct ieee80211_node *obss; 793138568Ssam 794116742Ssam if (ic->ic_opmode == IEEE80211_M_IBSS) { 795140753Ssam struct ieee80211_node_table *nt; 796138568Ssam /* 797140753Ssam * Fillin the neighbor table; it will already 798139521Ssam * exist if we are simply switching mastership. 799140753Ssam * XXX ic_sta always setup so this is unnecessary? 800127772Ssam */ 801140753Ssam nt = &ic->ic_sta; 802140753Ssam IEEE80211_NODE_LOCK(nt); 803140753Ssam nt->nt_name = "neighbor"; 804140753Ssam nt->nt_inact_init = ic->ic_inact_run; 805140753Ssam IEEE80211_NODE_UNLOCK(nt); 806138568Ssam } 807138568Ssam 808138568Ssam /* 809138568Ssam * Committed to selbs, setup state. 810138568Ssam */ 811138568Ssam obss = ic->ic_bss; 812140753Ssam ic->ic_bss = selbs; /* NB: caller assumed to bump refcnt */ 813153352Ssam if (obss != NULL) { 814153352Ssam copy_bss(selbs, obss); 815138568Ssam ieee80211_free_node(obss); 816153352Ssam } 817165887Ssam 818138568Ssam /* 819165887Ssam * Delete unusable rates; we've already checked 820165887Ssam * that the negotiated rate set is acceptable. 821165887Ssam */ 822167442Ssam ieee80211_fix_rate(ic->ic_bss, &ic->ic_bss->ni_rates, 823167442Ssam IEEE80211_F_DODEL | IEEE80211_F_JOIN); 824165887Ssam 825165887Ssam /* 826138568Ssam * Set the erp state (mostly the slot time) to deal with 827138568Ssam * the auto-select case; this should be redundant if the 828138568Ssam * mode is locked. 829138568Ssam */ 830138568Ssam ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan); 831148936Ssam ic->ic_curchan = selbs->ni_chan; 832138568Ssam ieee80211_reset_erp(ic); 833138568Ssam ieee80211_wme_initparams(ic); 834140753Ssam 835140753Ssam if (ic->ic_opmode == IEEE80211_M_STA) 836140753Ssam ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 837140753Ssam else 838117811Ssam ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 839138568Ssam return 1; 840116742Ssam} 841116742Ssam 842138568Ssam/* 843138568Ssam * Leave the specified IBSS/BSS network. The node is assumed to 844138568Ssam * be passed in with a held reference. 845138568Ssam */ 846138568Ssamvoid 847138568Ssamieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 848138568Ssam{ 849138568Ssam ic->ic_node_cleanup(ni); 850138568Ssam ieee80211_notify_node_leave(ic, ni); 851138568Ssam} 852138568Ssam 853116742Ssamstatic struct ieee80211_node * 854138568Ssamnode_alloc(struct ieee80211_node_table *nt) 855116742Ssam{ 856127768Ssam struct ieee80211_node *ni; 857138568Ssam 858127768Ssam MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node), 859127768Ssam M_80211_NODE, M_NOWAIT | M_ZERO); 860127768Ssam return ni; 861116742Ssam} 862116742Ssam 863138568Ssam/* 864138568Ssam * Reclaim any resources in a node and reset any critical 865138568Ssam * state. Typically nodes are free'd immediately after, 866138568Ssam * but in some cases the storage may be reused so we need 867138568Ssam * to insure consistent state (should probably fix that). 868138568Ssam */ 869116742Ssamstatic void 870138568Ssamnode_cleanup(struct ieee80211_node *ni) 871116742Ssam{ 872138568Ssam#define N(a) (sizeof(a)/sizeof(a[0])) 873138568Ssam struct ieee80211com *ic = ni->ni_ic; 874138568Ssam int i, qlen; 875138568Ssam 876138568Ssam /* NB: preserve ni_table */ 877138568Ssam if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 878138568Ssam ic->ic_ps_sta--; 879138568Ssam ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 880138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 881138568Ssam "[%s] power save mode off, %u sta's in ps mode\n", 882138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta); 883138568Ssam } 884147788Ssam /* 885147788Ssam * Clear AREF flag that marks the authorization refcnt bump 886147788Ssam * has happened. This is probably not needed as the node 887147788Ssam * should always be removed from the table so not found but 888147788Ssam * do it just in case. 889147788Ssam */ 890147788Ssam ni->ni_flags &= ~IEEE80211_NODE_AREF; 891138568Ssam 892138568Ssam /* 893138568Ssam * Drain power save queue and, if needed, clear TIM. 894138568Ssam */ 895138568Ssam IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen); 896138568Ssam if (qlen != 0 && ic->ic_set_tim != NULL) 897148304Ssam ic->ic_set_tim(ni, 0); 898138568Ssam 899138568Ssam ni->ni_associd = 0; 900138568Ssam if (ni->ni_challenge != NULL) { 901138568Ssam FREE(ni->ni_challenge, M_DEVBUF); 902138568Ssam ni->ni_challenge = NULL; 903138568Ssam } 904138568Ssam /* 905138568Ssam * Preserve SSID, WPA, and WME ie's so the bss node is 906138568Ssam * reusable during a re-auth/re-assoc state transition. 907138568Ssam * If we remove these data they will not be recreated 908138568Ssam * because they come from a probe-response or beacon frame 909138568Ssam * which cannot be expected prior to the association-response. 910138568Ssam * This should not be an issue when operating in other modes 911138568Ssam * as stations leaving always go through a full state transition 912138568Ssam * which will rebuild this state. 913138568Ssam * 914138568Ssam * XXX does this leave us open to inheriting old state? 915138568Ssam */ 916138568Ssam for (i = 0; i < N(ni->ni_rxfrag); i++) 917138568Ssam if (ni->ni_rxfrag[i] != NULL) { 918138568Ssam m_freem(ni->ni_rxfrag[i]); 919138568Ssam ni->ni_rxfrag[i] = NULL; 920138568Ssam } 921148863Ssam /* 922148863Ssam * Must be careful here to remove any key map entry w/o a LOR. 923148863Ssam */ 924148863Ssam ieee80211_node_delucastkey(ni); 925138568Ssam#undef N 926116742Ssam} 927116742Ssam 928116742Ssamstatic void 929138568Ssamnode_free(struct ieee80211_node *ni) 930116742Ssam{ 931138568Ssam struct ieee80211com *ic = ni->ni_ic; 932138568Ssam 933138568Ssam ic->ic_node_cleanup(ni); 934138568Ssam if (ni->ni_wpa_ie != NULL) 935138568Ssam FREE(ni->ni_wpa_ie, M_DEVBUF); 936138568Ssam if (ni->ni_wme_ie != NULL) 937138568Ssam FREE(ni->ni_wme_ie, M_DEVBUF); 938138568Ssam IEEE80211_NODE_SAVEQ_DESTROY(ni); 939138568Ssam FREE(ni, M_80211_NODE); 940116742Ssam} 941116742Ssam 942120104Ssamstatic u_int8_t 943138568Ssamnode_getrssi(const struct ieee80211_node *ni) 944120104Ssam{ 945120104Ssam return ni->ni_rssi; 946120104Ssam} 947120104Ssam 948116742Ssamstatic void 949138568Ssamieee80211_setup_node(struct ieee80211_node_table *nt, 950138568Ssam struct ieee80211_node *ni, const u_int8_t *macaddr) 951116742Ssam{ 952138568Ssam struct ieee80211com *ic = nt->nt_ic; 953116742Ssam int hash; 954116742Ssam 955138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 956140766Ssam "%s %p<%s> in %s table\n", __func__, ni, 957138568Ssam ether_sprintf(macaddr), nt->nt_name); 958138568Ssam 959116742Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 960116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 961138568Ssam ieee80211_node_initref(ni); /* mark referenced */ 962138568Ssam ni->ni_chan = IEEE80211_CHAN_ANYC; 963138568Ssam ni->ni_authmode = IEEE80211_AUTH_OPEN; 964138568Ssam ni->ni_txpower = ic->ic_txpowlimit; /* max power */ 965138568Ssam ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE); 966139528Ssam ni->ni_inact_reload = nt->nt_inact_init; 967139528Ssam ni->ni_inact = ni->ni_inact_reload; 968138568Ssam IEEE80211_NODE_SAVEQ_INIT(ni, "unknown"); 969138568Ssam 970138568Ssam IEEE80211_NODE_LOCK(nt); 971138568Ssam TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list); 972138568Ssam LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash); 973138568Ssam ni->ni_table = nt; 974138568Ssam ni->ni_ic = ic; 975138568Ssam IEEE80211_NODE_UNLOCK(nt); 976116742Ssam} 977116742Ssam 978116742Ssamstruct ieee80211_node * 979138568Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 980116742Ssam{ 981138568Ssam struct ieee80211com *ic = nt->nt_ic; 982138568Ssam struct ieee80211_node *ni; 983138568Ssam 984138568Ssam ni = ic->ic_node_alloc(nt); 985116742Ssam if (ni != NULL) 986138568Ssam ieee80211_setup_node(nt, ni, macaddr); 987127769Ssam else 988127769Ssam ic->ic_stats.is_rx_nodealloc++; 989116742Ssam return ni; 990116742Ssam} 991116742Ssam 992148777Ssam/* 993148777Ssam * Craft a temporary node suitable for sending a management frame 994148777Ssam * to the specified station. We craft only as much state as we 995148777Ssam * need to do the work since the node will be immediately reclaimed 996148777Ssam * once the send completes. 997148777Ssam */ 998116742Ssamstruct ieee80211_node * 999148777Ssamieee80211_tmp_node(struct ieee80211com *ic, const u_int8_t *macaddr) 1000148777Ssam{ 1001148777Ssam struct ieee80211_node *ni; 1002148777Ssam 1003148777Ssam ni = ic->ic_node_alloc(&ic->ic_sta); 1004148777Ssam if (ni != NULL) { 1005148777Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1006148777Ssam "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr)); 1007148777Ssam 1008148777Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 1009148777Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 1010148777Ssam ieee80211_node_initref(ni); /* mark referenced */ 1011148777Ssam ni->ni_txpower = ic->ic_bss->ni_txpower; 1012148777Ssam /* NB: required by ieee80211_fix_rate */ 1013148777Ssam ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan); 1014148777Ssam ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, 1015148777Ssam IEEE80211_KEYIX_NONE); 1016148777Ssam /* XXX optimize away */ 1017148777Ssam IEEE80211_NODE_SAVEQ_INIT(ni, "unknown"); 1018148777Ssam 1019148777Ssam ni->ni_table = NULL; /* NB: pedantic */ 1020148777Ssam ni->ni_ic = ic; 1021148777Ssam } else { 1022148777Ssam /* XXX msg */ 1023148777Ssam ic->ic_stats.is_rx_nodealloc++; 1024148777Ssam } 1025148777Ssam return ni; 1026148777Ssam} 1027148777Ssam 1028148777Ssamstruct ieee80211_node * 1029138568Ssamieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 1030116742Ssam{ 1031138568Ssam struct ieee80211com *ic = nt->nt_ic; 1032138568Ssam struct ieee80211_node *ni; 1033138568Ssam 1034138568Ssam ni = ic->ic_node_alloc(nt); 1035116742Ssam if (ni != NULL) { 1036138568Ssam ieee80211_setup_node(nt, ni, macaddr); 1037127770Ssam /* 1038127770Ssam * Inherit from ic_bss. 1039127770Ssam */ 1040138568Ssam ni->ni_authmode = ic->ic_bss->ni_authmode; 1041138568Ssam ni->ni_txpower = ic->ic_bss->ni_txpower; 1042138568Ssam ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */ 1043127770Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 1044138568Ssam ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan); 1045138568Ssam ni->ni_rsn = ic->ic_bss->ni_rsn; 1046127770Ssam } else 1047127770Ssam ic->ic_stats.is_rx_nodealloc++; 1048116742Ssam return ni; 1049116742Ssam} 1050116742Ssam 1051127772Ssamstatic struct ieee80211_node * 1052138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1053138568Ssam_ieee80211_find_node_debug(struct ieee80211_node_table *nt, 1054138568Ssam const u_int8_t *macaddr, const char *func, int line) 1055138568Ssam#else 1056138568Ssam_ieee80211_find_node(struct ieee80211_node_table *nt, 1057138568Ssam const u_int8_t *macaddr) 1058138568Ssam#endif 1059116742Ssam{ 1060116742Ssam struct ieee80211_node *ni; 1061116742Ssam int hash; 1062116742Ssam 1063138568Ssam IEEE80211_NODE_LOCK_ASSERT(nt); 1064127772Ssam 1065116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 1066138568Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1067116742Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 1068138568Ssam ieee80211_ref_node(ni); /* mark referenced */ 1069138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1070138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1071140766Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1072140766Ssam func, line, 1073140766Ssam ni, ether_sprintf(ni->ni_macaddr), 1074140766Ssam ieee80211_node_refcnt(ni)); 1075138568Ssam#endif 1076127772Ssam return ni; 1077116742Ssam } 1078116742Ssam } 1079127772Ssam return NULL; 1080127772Ssam} 1081138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1082138568Ssam#define _ieee80211_find_node(nt, mac) \ 1083138568Ssam _ieee80211_find_node_debug(nt, mac, func, line) 1084138568Ssam#endif 1085127772Ssam 1086127772Ssamstruct ieee80211_node * 1087138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1088138568Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt, 1089138568Ssam const u_int8_t *macaddr, const char *func, int line) 1090138568Ssam#else 1091138568Ssamieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 1092138568Ssam#endif 1093127772Ssam{ 1094127772Ssam struct ieee80211_node *ni; 1095127772Ssam 1096138568Ssam IEEE80211_NODE_LOCK(nt); 1097138568Ssam ni = _ieee80211_find_node(nt, macaddr); 1098138568Ssam IEEE80211_NODE_UNLOCK(nt); 1099116742Ssam return ni; 1100116742Ssam} 1101116742Ssam 1102116742Ssam/* 1103138568Ssam * Fake up a node; this handles node discovery in adhoc mode. 1104138568Ssam * Note that for the driver's benefit we we treat this like 1105138568Ssam * an association so the driver has an opportunity to setup 1106138568Ssam * it's private state. 1107138568Ssam */ 1108138568Ssamstruct ieee80211_node * 1109138568Ssamieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt, 1110138568Ssam const u_int8_t macaddr[IEEE80211_ADDR_LEN]) 1111138568Ssam{ 1112138568Ssam struct ieee80211com *ic = nt->nt_ic; 1113138568Ssam struct ieee80211_node *ni; 1114138568Ssam 1115153073Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1116153073Ssam "%s: mac<%s>\n", __func__, ether_sprintf(macaddr)); 1117138568Ssam ni = ieee80211_dup_bss(nt, macaddr); 1118138568Ssam if (ni != NULL) { 1119138568Ssam /* XXX no rate negotiation; just dup */ 1120138568Ssam ni->ni_rates = ic->ic_bss->ni_rates; 1121139524Ssam if (ic->ic_newassoc != NULL) 1122148307Ssam ic->ic_newassoc(ni, 1); 1123138568Ssam /* XXX not right for 802.1x/WPA */ 1124148302Ssam ieee80211_node_authorize(ni); 1125153404Ssam if (ic->ic_opmode == IEEE80211_M_AHDEMO) { 1126153404Ssam /* 1127153404Ssam * Blindly propagate capabilities based on the 1128153404Ssam * local configuration. In particular this permits 1129153404Ssam * us to use QoS to disable ACK's. 1130153404Ssam */ 1131153404Ssam if (ic->ic_flags & IEEE80211_F_WME) 1132153404Ssam ni->ni_flags |= IEEE80211_NODE_QOS; 1133153404Ssam } 1134138568Ssam } 1135138568Ssam return ni; 1136138568Ssam} 1137138568Ssam 1138148936Ssam#ifdef IEEE80211_DEBUG 1139148936Ssamstatic void 1140148936Ssamdump_probe_beacon(u_int8_t subtype, int isnew, 1141148936Ssam const u_int8_t mac[IEEE80211_ADDR_LEN], 1142148936Ssam const struct ieee80211_scanparams *sp) 1143148936Ssam{ 1144148936Ssam 1145148936Ssam printf("[%s] %s%s on chan %u (bss chan %u) ", 1146148936Ssam ether_sprintf(mac), isnew ? "new " : "", 1147148936Ssam ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT], 1148148936Ssam sp->chan, sp->bchan); 1149148936Ssam ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]); 1150148936Ssam printf("\n"); 1151148936Ssam 1152148936Ssam if (isnew) { 1153148936Ssam printf("[%s] caps 0x%x bintval %u erp 0x%x", 1154148936Ssam ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp); 1155148936Ssam if (sp->country != NULL) { 1156148936Ssam#ifdef __FreeBSD__ 1157148936Ssam printf(" country info %*D", 1158148936Ssam sp->country[1], sp->country+2, " "); 1159148936Ssam#else 1160148936Ssam int i; 1161148936Ssam printf(" country info"); 1162148936Ssam for (i = 0; i < sp->country[1]; i++) 1163148936Ssam printf(" %02x", sp->country[i+2]); 1164148936Ssam#endif 1165148936Ssam } 1166148936Ssam printf("\n"); 1167148936Ssam } 1168148936Ssam} 1169148936Ssam#endif /* IEEE80211_DEBUG */ 1170148936Ssam 1171148936Ssamstatic void 1172148936Ssamsaveie(u_int8_t **iep, const u_int8_t *ie) 1173148936Ssam{ 1174148936Ssam 1175148936Ssam if (ie == NULL) 1176148936Ssam *iep = NULL; 1177148936Ssam else 1178148936Ssam ieee80211_saveie(iep, ie); 1179148936Ssam} 1180148936Ssam 1181148936Ssam/* 1182148936Ssam * Process a beacon or probe response frame. 1183148936Ssam */ 1184148936Ssamvoid 1185148936Ssamieee80211_add_scan(struct ieee80211com *ic, 1186148936Ssam const struct ieee80211_scanparams *sp, 1187148936Ssam const struct ieee80211_frame *wh, 1188148936Ssam int subtype, int rssi, int rstamp) 1189148936Ssam{ 1190148936Ssam#define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1191148936Ssam struct ieee80211_node_table *nt = &ic->ic_scan; 1192148936Ssam struct ieee80211_node *ni; 1193148936Ssam int newnode = 0; 1194148936Ssam 1195148936Ssam ni = ieee80211_find_node(nt, wh->i_addr2); 1196148936Ssam if (ni == NULL) { 1197148936Ssam /* 1198148936Ssam * Create a new entry. 1199148936Ssam */ 1200148936Ssam ni = ic->ic_node_alloc(nt); 1201148936Ssam if (ni == NULL) { 1202148936Ssam ic->ic_stats.is_rx_nodealloc++; 1203148936Ssam return; 1204148936Ssam } 1205148936Ssam ieee80211_setup_node(nt, ni, wh->i_addr2); 1206148936Ssam /* 1207148936Ssam * XXX inherit from ic_bss. 1208148936Ssam */ 1209148936Ssam ni->ni_authmode = ic->ic_bss->ni_authmode; 1210148936Ssam ni->ni_txpower = ic->ic_bss->ni_txpower; 1211148936Ssam ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */ 1212148936Ssam ieee80211_set_chan(ic, ni, ic->ic_curchan); 1213148936Ssam ni->ni_rsn = ic->ic_bss->ni_rsn; 1214148936Ssam newnode = 1; 1215148936Ssam } 1216148936Ssam#ifdef IEEE80211_DEBUG 1217148936Ssam if (ieee80211_msg_scan(ic) && (ic->ic_flags & IEEE80211_F_SCAN)) 1218148936Ssam dump_probe_beacon(subtype, newnode, wh->i_addr2, sp); 1219148936Ssam#endif 1220148936Ssam /* XXX ap beaconing multiple ssid w/ same bssid */ 1221148936Ssam if (sp->ssid[1] != 0 && 1222148936Ssam (ISPROBE(subtype) || ni->ni_esslen == 0)) { 1223148936Ssam ni->ni_esslen = sp->ssid[1]; 1224148936Ssam memset(ni->ni_essid, 0, sizeof(ni->ni_essid)); 1225148936Ssam memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]); 1226148936Ssam } 1227148936Ssam ni->ni_scangen = ic->ic_scan.nt_scangen; 1228148936Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3); 1229148936Ssam ni->ni_rssi = rssi; 1230148936Ssam ni->ni_rstamp = rstamp; 1231148936Ssam memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp)); 1232148936Ssam ni->ni_intval = sp->bintval; 1233148936Ssam ni->ni_capinfo = sp->capinfo; 1234148936Ssam ni->ni_chan = &ic->ic_channels[sp->chan]; 1235148936Ssam ni->ni_fhdwell = sp->fhdwell; 1236148936Ssam ni->ni_fhindex = sp->fhindex; 1237148936Ssam ni->ni_erp = sp->erp; 1238148936Ssam if (sp->tim != NULL) { 1239148936Ssam struct ieee80211_tim_ie *ie = 1240148936Ssam (struct ieee80211_tim_ie *) sp->tim; 1241148936Ssam 1242148936Ssam ni->ni_dtim_count = ie->tim_count; 1243148936Ssam ni->ni_dtim_period = ie->tim_period; 1244148936Ssam } 1245148936Ssam /* 1246148936Ssam * Record the byte offset from the mac header to 1247148936Ssam * the start of the TIM information element for 1248148936Ssam * use by hardware and/or to speedup software 1249148936Ssam * processing of beacon frames. 1250148936Ssam */ 1251148936Ssam ni->ni_timoff = sp->timoff; 1252148936Ssam /* 1253148936Ssam * Record optional information elements that might be 1254148936Ssam * used by applications or drivers. 1255148936Ssam */ 1256148936Ssam saveie(&ni->ni_wme_ie, sp->wme); 1257148936Ssam saveie(&ni->ni_wpa_ie, sp->wpa); 1258148936Ssam 1259148936Ssam /* NB: must be after ni_chan is setup */ 1260148936Ssam ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT); 1261148936Ssam 1262148936Ssam if (!newnode) 1263148936Ssam ieee80211_free_node(ni); 1264148936Ssam#undef ISPROBE 1265148936Ssam} 1266148936Ssam 1267153073Ssamvoid 1268153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni, 1269153073Ssam const struct ieee80211_frame *wh, 1270153073Ssam const struct ieee80211_scanparams *sp) 1271153073Ssam{ 1272153073Ssam 1273153073Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1274153073Ssam "%s: %p<%s>\n", __func__, ni, ether_sprintf(ni->ni_macaddr)); 1275153073Ssam ni->ni_esslen = sp->ssid[1]; 1276153073Ssam memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]); 1277153073Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3); 1278153073Ssam memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp)); 1279153073Ssam ni->ni_intval = sp->bintval; 1280153073Ssam ni->ni_capinfo = sp->capinfo; 1281153073Ssam ni->ni_chan = ni->ni_ic->ic_curchan; 1282153073Ssam ni->ni_fhdwell = sp->fhdwell; 1283153073Ssam ni->ni_fhindex = sp->fhindex; 1284153073Ssam ni->ni_erp = sp->erp; 1285153073Ssam ni->ni_timoff = sp->timoff; 1286153073Ssam if (sp->wme != NULL) 1287153073Ssam ieee80211_saveie(&ni->ni_wme_ie, sp->wme); 1288153073Ssam if (sp->wpa != NULL) 1289153073Ssam ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa); 1290153073Ssam 1291153073Ssam /* NB: must be after ni_chan is setup */ 1292165887Ssam ieee80211_setup_rates(ni, sp->rates, sp->xrates, 1293165887Ssam IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | 1294165887Ssam IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1295153073Ssam} 1296153073Ssam 1297148936Ssam/* 1298148936Ssam * Do node discovery in adhoc mode on receipt of a beacon 1299148936Ssam * or probe response frame. Note that for the driver's 1300148936Ssam * benefit we we treat this like an association so the 1301148936Ssam * driver has an opportunity to setup it's private state. 1302148936Ssam */ 1303148936Ssamstruct ieee80211_node * 1304148936Ssamieee80211_add_neighbor(struct ieee80211com *ic, 1305148936Ssam const struct ieee80211_frame *wh, 1306148936Ssam const struct ieee80211_scanparams *sp) 1307148936Ssam{ 1308148936Ssam struct ieee80211_node *ni; 1309148936Ssam 1310153073Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1311153073Ssam "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2)); 1312148936Ssam ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */ 1313148936Ssam if (ni != NULL) { 1314153073Ssam ieee80211_init_neighbor(ni, wh, sp); 1315148936Ssam if (ic->ic_newassoc != NULL) 1316148936Ssam ic->ic_newassoc(ni, 1); 1317148936Ssam /* XXX not right for 802.1x/WPA */ 1318148936Ssam ieee80211_node_authorize(ni); 1319148936Ssam } 1320148936Ssam return ni; 1321148936Ssam} 1322148936Ssam 1323148863Ssam#define IS_CTL(wh) \ 1324148863Ssam ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) 1325148863Ssam#define IS_PSPOLL(wh) \ 1326148863Ssam ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL) 1327138568Ssam/* 1328138568Ssam * Locate the node for sender, track state, and then pass the 1329138568Ssam * (referenced) node up to the 802.11 layer for its use. We 1330138568Ssam * are required to pass some node so we fall back to ic_bss 1331138568Ssam * when this frame is from an unknown sender. The 802.11 layer 1332138568Ssam * knows this means the sender wasn't in the node table and 1333138568Ssam * acts accordingly. 1334138568Ssam */ 1335138568Ssamstruct ieee80211_node * 1336138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1337138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic, 1338138568Ssam const struct ieee80211_frame_min *wh, const char *func, int line) 1339138568Ssam#else 1340138568Ssamieee80211_find_rxnode(struct ieee80211com *ic, 1341138568Ssam const struct ieee80211_frame_min *wh) 1342138568Ssam#endif 1343138568Ssam{ 1344138568Ssam struct ieee80211_node_table *nt; 1345138568Ssam struct ieee80211_node *ni; 1346138568Ssam 1347138568Ssam /* XXX may want scanned nodes in the neighbor table for adhoc */ 1348138568Ssam if (ic->ic_opmode == IEEE80211_M_STA || 1349138568Ssam ic->ic_opmode == IEEE80211_M_MONITOR || 1350138568Ssam (ic->ic_flags & IEEE80211_F_SCAN)) 1351138568Ssam nt = &ic->ic_scan; 1352138568Ssam else 1353140753Ssam nt = &ic->ic_sta; 1354138568Ssam /* XXX check ic_bss first in station mode */ 1355138568Ssam /* XXX 4-address frames? */ 1356138568Ssam IEEE80211_NODE_LOCK(nt); 1357138568Ssam if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/) 1358138568Ssam ni = _ieee80211_find_node(nt, wh->i_addr1); 1359138568Ssam else 1360138568Ssam ni = _ieee80211_find_node(nt, wh->i_addr2); 1361148863Ssam if (ni == NULL) 1362148863Ssam ni = ieee80211_ref_node(ic->ic_bss); 1363138568Ssam IEEE80211_NODE_UNLOCK(nt); 1364138568Ssam 1365148863Ssam return ni; 1366148863Ssam} 1367148863Ssam 1368148863Ssam/* 1369148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w 1370148863Ssam * key index as a hint to locate the node in the key 1371148863Ssam * mapping table. If an entry is present at the key 1372148863Ssam * index we return it; otherwise do a normal lookup and 1373148863Ssam * update the mapping table if the station has a unicast 1374148863Ssam * key assigned to it. 1375148863Ssam */ 1376148863Ssamstruct ieee80211_node * 1377148863Ssam#ifdef IEEE80211_DEBUG_REFCNT 1378148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic, 1379148863Ssam const struct ieee80211_frame_min *wh, ieee80211_keyix keyix, 1380148863Ssam const char *func, int line) 1381148863Ssam#else 1382148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic, 1383148863Ssam const struct ieee80211_frame_min *wh, ieee80211_keyix keyix) 1384148863Ssam#endif 1385148863Ssam{ 1386148863Ssam struct ieee80211_node_table *nt; 1387148863Ssam struct ieee80211_node *ni; 1388148863Ssam 1389148863Ssam if (ic->ic_opmode == IEEE80211_M_STA || 1390148863Ssam ic->ic_opmode == IEEE80211_M_MONITOR || 1391148863Ssam (ic->ic_flags & IEEE80211_F_SCAN)) 1392148863Ssam nt = &ic->ic_scan; 1393148863Ssam else 1394148863Ssam nt = &ic->ic_sta; 1395148863Ssam IEEE80211_NODE_LOCK(nt); 1396148863Ssam if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) 1397148863Ssam ni = nt->nt_keyixmap[keyix]; 1398148863Ssam else 1399148863Ssam ni = NULL; 1400148863Ssam if (ni == NULL) { 1401148863Ssam if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/) 1402148863Ssam ni = _ieee80211_find_node(nt, wh->i_addr1); 1403148863Ssam else 1404148863Ssam ni = _ieee80211_find_node(nt, wh->i_addr2); 1405148863Ssam if (ni == NULL) 1406148863Ssam ni = ieee80211_ref_node(ic->ic_bss); 1407148863Ssam if (nt->nt_keyixmap != NULL) { 1408148863Ssam /* 1409148863Ssam * If the station has a unicast key cache slot 1410148863Ssam * assigned update the key->node mapping table. 1411148863Ssam */ 1412148863Ssam keyix = ni->ni_ucastkey.wk_rxkeyix; 1413148863Ssam /* XXX can keyixmap[keyix] != NULL? */ 1414148863Ssam if (keyix < nt->nt_keyixmax && 1415148863Ssam nt->nt_keyixmap[keyix] == NULL) { 1416148863Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1417148863Ssam "%s: add key map entry %p<%s> refcnt %d\n", 1418148863Ssam __func__, ni, ether_sprintf(ni->ni_macaddr), 1419148863Ssam ieee80211_node_refcnt(ni)+1); 1420148863Ssam nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni); 1421148863Ssam } 1422148863Ssam } 1423148863Ssam } else { 1424148863Ssam ieee80211_ref_node(ni); 1425148863Ssam } 1426148863Ssam IEEE80211_NODE_UNLOCK(nt); 1427148863Ssam 1428148863Ssam return ni; 1429148863Ssam} 1430138568Ssam#undef IS_PSPOLL 1431138568Ssam#undef IS_CTL 1432138568Ssam 1433138568Ssam/* 1434127772Ssam * Return a reference to the appropriate node for sending 1435127772Ssam * a data frame. This handles node discovery in adhoc networks. 1436127772Ssam */ 1437127772Ssamstruct ieee80211_node * 1438138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1439138568Ssamieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr, 1440138568Ssam const char *func, int line) 1441138568Ssam#else 1442138568Ssamieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr) 1443138568Ssam#endif 1444127772Ssam{ 1445140753Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 1446127772Ssam struct ieee80211_node *ni; 1447127772Ssam 1448127772Ssam /* 1449127772Ssam * The destination address should be in the node table 1450148863Ssam * unless this is a multicast/broadcast frame. We can 1451148863Ssam * also optimize station mode operation, all frames go 1452148863Ssam * to the bss node. 1453127772Ssam */ 1454127772Ssam /* XXX can't hold lock across dup_bss 'cuz of recursive locking */ 1455138568Ssam IEEE80211_NODE_LOCK(nt); 1456148863Ssam if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr)) 1457148863Ssam ni = ieee80211_ref_node(ic->ic_bss); 1458158121Ssam else { 1459148863Ssam ni = _ieee80211_find_node(nt, macaddr); 1460158121Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1461158121Ssam (ni != NULL && ni->ni_associd == 0)) { 1462158121Ssam /* 1463158121Ssam * Station is not associated; don't permit the 1464158121Ssam * data frame to be sent by returning NULL. This 1465158121Ssam * is kinda a kludge but the least intrusive way 1466158121Ssam * to add this check into all drivers. 1467158121Ssam */ 1468158121Ssam ieee80211_unref_node(&ni); /* NB: null's ni */ 1469158121Ssam } 1470158121Ssam } 1471138568Ssam IEEE80211_NODE_UNLOCK(nt); 1472138568Ssam 1473138568Ssam if (ni == NULL) { 1474138568Ssam if (ic->ic_opmode == IEEE80211_M_IBSS || 1475140497Ssam ic->ic_opmode == IEEE80211_M_AHDEMO) { 1476140497Ssam /* 1477140497Ssam * In adhoc mode cons up a node for the destination. 1478140497Ssam * Note that we need an additional reference for the 1479140497Ssam * caller to be consistent with _ieee80211_find_node. 1480140497Ssam */ 1481138568Ssam ni = ieee80211_fakeup_adhoc_node(nt, macaddr); 1482140497Ssam if (ni != NULL) 1483140497Ssam (void) ieee80211_ref_node(ni); 1484140497Ssam } else { 1485138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 1486138568Ssam "[%s] no node, discard frame (%s)\n", 1487138568Ssam ether_sprintf(macaddr), __func__); 1488138568Ssam ic->ic_stats.is_tx_nonode++; 1489127772Ssam } 1490127772Ssam } 1491127772Ssam return ni; 1492127772Ssam} 1493127772Ssam 1494127772Ssam/* 1495116742Ssam * Like find but search based on the channel too. 1496116742Ssam */ 1497116742Ssamstruct ieee80211_node * 1498138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1499138568Ssamieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt, 1500138568Ssam const u_int8_t *macaddr, struct ieee80211_channel *chan, 1501138568Ssam const char *func, int line) 1502138568Ssam#else 1503138568Ssamieee80211_find_node_with_channel(struct ieee80211_node_table *nt, 1504138568Ssam const u_int8_t *macaddr, struct ieee80211_channel *chan) 1505138568Ssam#endif 1506116742Ssam{ 1507116742Ssam struct ieee80211_node *ni; 1508116742Ssam int hash; 1509116742Ssam 1510116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 1511138568Ssam IEEE80211_NODE_LOCK(nt); 1512138568Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1513127772Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1514127772Ssam ni->ni_chan == chan) { 1515138568Ssam ieee80211_ref_node(ni); /* mark referenced */ 1516138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1517159139Sdds REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr), 1518140766Ssam ieee80211_node_refcnt(ni)); 1519116742Ssam break; 1520116742Ssam } 1521116742Ssam } 1522138568Ssam IEEE80211_NODE_UNLOCK(nt); 1523116742Ssam return ni; 1524116742Ssam} 1525116742Ssam 1526138568Ssam/* 1527138568Ssam * Like find but search based on the ssid too. 1528138568Ssam */ 1529138568Ssamstruct ieee80211_node * 1530138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1531138568Ssamieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt, 1532138568Ssam const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid, 1533138568Ssam const char *func, int line) 1534138568Ssam#else 1535138568Ssamieee80211_find_node_with_ssid(struct ieee80211_node_table *nt, 1536138568Ssam const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid) 1537138568Ssam#endif 1538138568Ssam{ 1539147118Ssam#define MATCH_SSID(ni, ssid, ssidlen) \ 1540147118Ssam (ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0) 1541147118Ssam static const u_int8_t zeromac[IEEE80211_ADDR_LEN]; 1542138568Ssam struct ieee80211com *ic = nt->nt_ic; 1543138568Ssam struct ieee80211_node *ni; 1544138568Ssam int hash; 1545138568Ssam 1546138568Ssam IEEE80211_NODE_LOCK(nt); 1547147118Ssam /* 1548147118Ssam * A mac address that is all zero means match only the ssid; 1549147118Ssam * otherwise we must match both. 1550147118Ssam */ 1551147118Ssam if (IEEE80211_ADDR_EQ(macaddr, zeromac)) { 1552147118Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1553147118Ssam if (MATCH_SSID(ni, ssid, ssidlen)) 1554147118Ssam break; 1555147118Ssam } 1556147118Ssam } else { 1557147118Ssam hash = IEEE80211_NODE_HASH(macaddr); 1558147118Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1559147118Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1560147118Ssam MATCH_SSID(ni, ssid, ssidlen)) 1561147118Ssam break; 1562147118Ssam } 1563147118Ssam } 1564147118Ssam if (ni != NULL) { 1565147118Ssam ieee80211_ref_node(ni); /* mark referenced */ 1566147118Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1567159139Sdds REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr), 1568147118Ssam ieee80211_node_refcnt(ni)); 1569138568Ssam } 1570138568Ssam IEEE80211_NODE_UNLOCK(nt); 1571138568Ssam return ni; 1572147118Ssam#undef MATCH_SSID 1573138568Ssam} 1574138568Ssam 1575116742Ssamstatic void 1576138568Ssam_ieee80211_free_node(struct ieee80211_node *ni) 1577116742Ssam{ 1578138568Ssam struct ieee80211com *ic = ni->ni_ic; 1579138568Ssam struct ieee80211_node_table *nt = ni->ni_table; 1580119150Ssam 1581138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1582140766Ssam "%s %p<%s> in %s table\n", __func__, ni, 1583140766Ssam ether_sprintf(ni->ni_macaddr), 1584138568Ssam nt != NULL ? nt->nt_name : "<gone>"); 1585138568Ssam 1586138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1587138568Ssam if (nt != NULL) { 1588138568Ssam TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1589138568Ssam LIST_REMOVE(ni, ni_hash); 1590138568Ssam } 1591138568Ssam ic->ic_node_free(ni); 1592116742Ssam} 1593116742Ssam 1594116742Ssamvoid 1595138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1596138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line) 1597138568Ssam#else 1598138568Ssamieee80211_free_node(struct ieee80211_node *ni) 1599138568Ssam#endif 1600116742Ssam{ 1601138568Ssam struct ieee80211_node_table *nt = ni->ni_table; 1602119150Ssam 1603138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1604140454Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1605140766Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 1606138568Ssam ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1607138568Ssam#endif 1608148863Ssam if (nt != NULL) { 1609148863Ssam IEEE80211_NODE_LOCK(nt); 1610148863Ssam if (ieee80211_node_dectestref(ni)) { 1611148863Ssam /* 1612148863Ssam * Last reference, reclaim state. 1613148863Ssam */ 1614138568Ssam _ieee80211_free_node(ni); 1615148863Ssam } else if (ieee80211_node_refcnt(ni) == 1 && 1616148863Ssam nt->nt_keyixmap != NULL) { 1617148863Ssam ieee80211_keyix keyix; 1618148863Ssam /* 1619148863Ssam * Check for a last reference in the key mapping table. 1620148863Ssam */ 1621148863Ssam keyix = ni->ni_ucastkey.wk_rxkeyix; 1622148863Ssam if (keyix < nt->nt_keyixmax && 1623148863Ssam nt->nt_keyixmap[keyix] == ni) { 1624148863Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1625148863Ssam "%s: %p<%s> clear key map entry", __func__, 1626148863Ssam ni, ether_sprintf(ni->ni_macaddr)); 1627148863Ssam nt->nt_keyixmap[keyix] = NULL; 1628148863Ssam ieee80211_node_decref(ni); /* XXX needed? */ 1629148863Ssam _ieee80211_free_node(ni); 1630148863Ssam } 1631148863Ssam } 1632148863Ssam IEEE80211_NODE_UNLOCK(nt); 1633148863Ssam } else { 1634148863Ssam if (ieee80211_node_dectestref(ni)) 1635138568Ssam _ieee80211_free_node(ni); 1636116742Ssam } 1637116742Ssam} 1638116742Ssam 1639138568Ssam/* 1640148863Ssam * Reclaim a unicast key and clear any key cache state. 1641148863Ssam */ 1642148863Ssamint 1643148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni) 1644148863Ssam{ 1645148863Ssam struct ieee80211com *ic = ni->ni_ic; 1646148863Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 1647148863Ssam struct ieee80211_node *nikey; 1648148863Ssam ieee80211_keyix keyix; 1649148863Ssam int isowned, status; 1650148863Ssam 1651148863Ssam /* 1652148863Ssam * NB: We must beware of LOR here; deleting the key 1653148863Ssam * can cause the crypto layer to block traffic updates 1654148863Ssam * which can generate a LOR against the node table lock; 1655148863Ssam * grab it here and stash the key index for our use below. 1656148863Ssam * 1657148863Ssam * Must also beware of recursion on the node table lock. 1658148863Ssam * When called from node_cleanup we may already have 1659148863Ssam * the node table lock held. Unfortunately there's no 1660148863Ssam * way to separate out this path so we must do this 1661148863Ssam * conditionally. 1662148863Ssam */ 1663148863Ssam isowned = IEEE80211_NODE_IS_LOCKED(nt); 1664148863Ssam if (!isowned) 1665148863Ssam IEEE80211_NODE_LOCK(nt); 1666148863Ssam keyix = ni->ni_ucastkey.wk_rxkeyix; 1667148863Ssam status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey); 1668148863Ssam if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) { 1669148863Ssam nikey = nt->nt_keyixmap[keyix]; 1670148863Ssam nt->nt_keyixmap[keyix] = NULL;; 1671148863Ssam } else 1672148863Ssam nikey = NULL; 1673148863Ssam if (!isowned) 1674148863Ssam IEEE80211_NODE_UNLOCK(&ic->ic_sta); 1675148863Ssam 1676148863Ssam if (nikey != NULL) { 1677148863Ssam KASSERT(nikey == ni, 1678148863Ssam ("key map out of sync, ni %p nikey %p", ni, nikey)); 1679148863Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1680148863Ssam "%s: delete key map entry %p<%s> refcnt %d\n", 1681148863Ssam __func__, ni, ether_sprintf(ni->ni_macaddr), 1682148863Ssam ieee80211_node_refcnt(ni)-1); 1683148863Ssam ieee80211_free_node(ni); 1684148863Ssam } 1685148863Ssam return status; 1686148863Ssam} 1687148863Ssam 1688148863Ssam/* 1689138568Ssam * Reclaim a node. If this is the last reference count then 1690138568Ssam * do the normal free work. Otherwise remove it from the node 1691138568Ssam * table and mark it gone by clearing the back-reference. 1692138568Ssam */ 1693138568Ssamstatic void 1694138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1695116742Ssam{ 1696148863Ssam ieee80211_keyix keyix; 1697138568Ssam 1698148863Ssam IEEE80211_NODE_LOCK_ASSERT(nt); 1699148863Ssam 1700140766Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1701140766Ssam "%s: remove %p<%s> from %s table, refcnt %d\n", 1702140766Ssam __func__, ni, ether_sprintf(ni->ni_macaddr), 1703140766Ssam nt->nt_name, ieee80211_node_refcnt(ni)-1); 1704148863Ssam /* 1705148863Ssam * Clear any entry in the unicast key mapping table. 1706148863Ssam * We need to do it here so rx lookups don't find it 1707148863Ssam * in the mapping table even if it's not in the hash 1708148863Ssam * table. We cannot depend on the mapping table entry 1709148863Ssam * being cleared because the node may not be free'd. 1710148863Ssam */ 1711148863Ssam keyix = ni->ni_ucastkey.wk_rxkeyix; 1712148863Ssam if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax && 1713148863Ssam nt->nt_keyixmap[keyix] == ni) { 1714148863Ssam IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1715148863Ssam "%s: %p<%s> clear key map entry\n", 1716148863Ssam __func__, ni, ether_sprintf(ni->ni_macaddr)); 1717148863Ssam nt->nt_keyixmap[keyix] = NULL; 1718148863Ssam ieee80211_node_decref(ni); /* NB: don't need free */ 1719148863Ssam } 1720138568Ssam if (!ieee80211_node_dectestref(ni)) { 1721138568Ssam /* 1722138568Ssam * Other references are present, just remove the 1723138568Ssam * node from the table so it cannot be found. When 1724138568Ssam * the references are dropped storage will be 1725140753Ssam * reclaimed. 1726138568Ssam */ 1727138568Ssam TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1728138568Ssam LIST_REMOVE(ni, ni_hash); 1729138568Ssam ni->ni_table = NULL; /* clear reference */ 1730138568Ssam } else 1731138568Ssam _ieee80211_free_node(ni); 1732138568Ssam} 1733138568Ssam 1734138568Ssamstatic void 1735138568Ssamieee80211_free_allnodes_locked(struct ieee80211_node_table *nt) 1736138568Ssam{ 1737138568Ssam struct ieee80211com *ic = nt->nt_ic; 1738116742Ssam struct ieee80211_node *ni; 1739116742Ssam 1740138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1741138568Ssam "%s: free all nodes in %s table\n", __func__, nt->nt_name); 1742138568Ssam 1743138568Ssam while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) { 1744138568Ssam if (ni->ni_associd != 0) { 1745138568Ssam if (ic->ic_auth->ia_node_leave != NULL) 1746138568Ssam ic->ic_auth->ia_node_leave(ic, ni); 1747138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1748138568Ssam } 1749138568Ssam node_reclaim(nt, ni); 1750138568Ssam } 1751138568Ssam ieee80211_reset_erp(ic); 1752116742Ssam} 1753116742Ssam 1754138568Ssamstatic void 1755138568Ssamieee80211_free_allnodes(struct ieee80211_node_table *nt) 1756138568Ssam{ 1757138568Ssam 1758138568Ssam IEEE80211_NODE_LOCK(nt); 1759138568Ssam ieee80211_free_allnodes_locked(nt); 1760138568Ssam IEEE80211_NODE_UNLOCK(nt); 1761138568Ssam} 1762138568Ssam 1763120483Ssam/* 1764138568Ssam * Timeout entries in the scan cache. 1765138568Ssam */ 1766138568Ssamstatic void 1767138568Ssamieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt) 1768138568Ssam{ 1769138568Ssam struct ieee80211com *ic = nt->nt_ic; 1770138568Ssam struct ieee80211_node *ni, *tni; 1771138568Ssam 1772138568Ssam IEEE80211_NODE_LOCK(nt); 1773138568Ssam ni = ic->ic_bss; 1774138568Ssam /* XXX belongs elsewhere */ 1775138568Ssam if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) { 1776138568Ssam m_freem(ni->ni_rxfrag[0]); 1777138568Ssam ni->ni_rxfrag[0] = NULL; 1778138568Ssam } 1779138568Ssam TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) { 1780138568Ssam if (ni->ni_inact && --ni->ni_inact == 0) { 1781138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1782138568Ssam "[%s] scan candidate purged from cache " 1783138568Ssam "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr), 1784140766Ssam ieee80211_node_refcnt(ni)); 1785138568Ssam node_reclaim(nt, ni); 1786138568Ssam } 1787138568Ssam } 1788138568Ssam IEEE80211_NODE_UNLOCK(nt); 1789138568Ssam 1790138568Ssam nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1791138568Ssam} 1792138568Ssam 1793138568Ssam/* 1794138568Ssam * Timeout inactive stations and do related housekeeping. 1795138568Ssam * Note that we cannot hold the node lock while sending a 1796138568Ssam * frame as this would lead to a LOR. Instead we use a 1797138568Ssam * generation number to mark nodes that we've scanned and 1798138568Ssam * drop the lock and restart a scan if we have to time out 1799138568Ssam * a node. Since we are single-threaded by virtue of 1800120483Ssam * controlling the inactivity timer we can be sure this will 1801120483Ssam * process each node only once. 1802120483Ssam */ 1803138568Ssamstatic void 1804138568Ssamieee80211_timeout_stations(struct ieee80211_node_table *nt) 1805116742Ssam{ 1806138568Ssam struct ieee80211com *ic = nt->nt_ic; 1807120483Ssam struct ieee80211_node *ni; 1808138568Ssam u_int gen; 1809148320Ssam int isadhoc; 1810116742Ssam 1811148320Ssam isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS || 1812148320Ssam ic->ic_opmode == IEEE80211_M_AHDEMO); 1813138568Ssam IEEE80211_SCAN_LOCK(nt); 1814154532Ssam gen = ++nt->nt_scangen; 1815138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1816140766Ssam "%s: %s scangen %u\n", __func__, nt->nt_name, gen); 1817120483Ssamrestart: 1818138568Ssam IEEE80211_NODE_LOCK(nt); 1819138568Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1820120483Ssam if (ni->ni_scangen == gen) /* previously handled */ 1821120483Ssam continue; 1822120483Ssam ni->ni_scangen = gen; 1823138568Ssam /* 1824147788Ssam * Ignore entries for which have yet to receive an 1825147788Ssam * authentication frame. These are transient and 1826147788Ssam * will be reclaimed when the last reference to them 1827147788Ssam * goes away (when frame xmits complete). 1828147788Ssam */ 1829148323Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1830148323Ssam (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 1831147788Ssam continue; 1832147788Ssam /* 1833138568Ssam * Free fragment if not needed anymore 1834138568Ssam * (last fragment older than 1s). 1835138568Ssam * XXX doesn't belong here 1836138568Ssam */ 1837138568Ssam if (ni->ni_rxfrag[0] != NULL && 1838138568Ssam ticks > ni->ni_rxfragstamp + hz) { 1839138568Ssam m_freem(ni->ni_rxfrag[0]); 1840138568Ssam ni->ni_rxfrag[0] = NULL; 1841138568Ssam } 1842140498Ssam /* 1843140498Ssam * Special case ourself; we may be idle for extended periods 1844140498Ssam * of time and regardless reclaiming our state is wrong. 1845140498Ssam */ 1846140498Ssam if (ni == ic->ic_bss) 1847140498Ssam continue; 1848138568Ssam ni->ni_inact--; 1849148320Ssam if (ni->ni_associd != 0 || isadhoc) { 1850119150Ssam /* 1851138568Ssam * Age frames on the power save queue. The 1852138568Ssam * aging interval is 4 times the listen 1853138568Ssam * interval specified by the station. This 1854138568Ssam * number is factored into the age calculations 1855138568Ssam * when the frame is placed on the queue. We 1856138568Ssam * store ages as time differences we can check 1857138568Ssam * and/or adjust only the head of the list. 1858138568Ssam */ 1859138568Ssam if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) { 1860138568Ssam struct mbuf *m; 1861138568Ssam int discard = 0; 1862138568Ssam 1863138568Ssam IEEE80211_NODE_SAVEQ_LOCK(ni); 1864138568Ssam while (IF_POLL(&ni->ni_savedq, m) != NULL && 1865138568Ssam M_AGE_GET(m) < IEEE80211_INACT_WAIT) { 1866138568SsamIEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/ 1867138568Ssam _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m); 1868138568Ssam m_freem(m); 1869138568Ssam discard++; 1870138568Ssam } 1871138568Ssam if (m != NULL) 1872138568Ssam M_AGE_SUB(m, IEEE80211_INACT_WAIT); 1873138568Ssam IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1874138568Ssam 1875138568Ssam if (discard != 0) { 1876138568Ssam IEEE80211_DPRINTF(ic, 1877138568Ssam IEEE80211_MSG_POWER, 1878138568Ssam "[%s] discard %u frames for age\n", 1879138568Ssam ether_sprintf(ni->ni_macaddr), 1880138568Ssam discard); 1881138568Ssam IEEE80211_NODE_STAT_ADD(ni, 1882138568Ssam ps_discard, discard); 1883138568Ssam if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) 1884148304Ssam ic->ic_set_tim(ni, 0); 1885138568Ssam } 1886138568Ssam } 1887138568Ssam /* 1888138568Ssam * Probe the station before time it out. We 1889138568Ssam * send a null data frame which may not be 1890138568Ssam * universally supported by drivers (need it 1891138568Ssam * for ps-poll support so it should be...). 1892138568Ssam */ 1893139528Ssam if (0 < ni->ni_inact && 1894139528Ssam ni->ni_inact <= ic->ic_inact_probe) { 1895148320Ssam IEEE80211_NOTE(ic, 1896148320Ssam IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, 1897148320Ssam ni, "%s", 1898148320Ssam "probe station due to inactivity"); 1899148582Ssam /* 1900148582Ssam * Grab a reference before unlocking the table 1901148582Ssam * so the node cannot be reclaimed before we 1902148582Ssam * send the frame. ieee80211_send_nulldata 1903148582Ssam * understands we've done this and reclaims the 1904148582Ssam * ref for us as needed. 1905148582Ssam */ 1906148582Ssam ieee80211_ref_node(ni); 1907138568Ssam IEEE80211_NODE_UNLOCK(nt); 1908148301Ssam ieee80211_send_nulldata(ni); 1909138568Ssam /* XXX stat? */ 1910138568Ssam goto restart; 1911138568Ssam } 1912138568Ssam } 1913138568Ssam if (ni->ni_inact <= 0) { 1914148320Ssam IEEE80211_NOTE(ic, 1915148320Ssam IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni, 1916148320Ssam "station timed out due to inactivity " 1917148320Ssam "(refcnt %u)", ieee80211_node_refcnt(ni)); 1918138568Ssam /* 1919138568Ssam * Send a deauthenticate frame and drop the station. 1920138568Ssam * This is somewhat complicated due to reference counts 1921138568Ssam * and locking. At this point a station will typically 1922138568Ssam * have a reference count of 1. ieee80211_node_leave 1923138568Ssam * will do a "free" of the node which will drop the 1924138568Ssam * reference count. But in the meantime a reference 1925138568Ssam * wil be held by the deauth frame. The actual reclaim 1926138568Ssam * of the node will happen either after the tx is 1927138568Ssam * completed or by ieee80211_node_leave. 1928120483Ssam * 1929138568Ssam * Separately we must drop the node lock before sending 1930138568Ssam * in case the driver takes a lock, as this will result 1931138568Ssam * in LOR between the node lock and the driver lock. 1932119150Ssam */ 1933138568Ssam IEEE80211_NODE_UNLOCK(nt); 1934138568Ssam if (ni->ni_associd != 0) { 1935138568Ssam IEEE80211_SEND_MGMT(ic, ni, 1936138568Ssam IEEE80211_FC0_SUBTYPE_DEAUTH, 1937138568Ssam IEEE80211_REASON_AUTH_EXPIRE); 1938138568Ssam } 1939138568Ssam ieee80211_node_leave(ic, ni); 1940121180Ssam ic->ic_stats.is_node_timeout++; 1941120483Ssam goto restart; 1942120483Ssam } 1943116742Ssam } 1944138568Ssam IEEE80211_NODE_UNLOCK(nt); 1945138568Ssam 1946138568Ssam IEEE80211_SCAN_UNLOCK(nt); 1947138568Ssam 1948138568Ssam nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1949116742Ssam} 1950116742Ssam 1951116742Ssamvoid 1952138568Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg) 1953116742Ssam{ 1954116742Ssam struct ieee80211_node *ni; 1955138568Ssam u_int gen; 1956116742Ssam 1957138568Ssam IEEE80211_SCAN_LOCK(nt); 1958154532Ssam gen = ++nt->nt_scangen; 1959138568Ssamrestart: 1960138568Ssam IEEE80211_NODE_LOCK(nt); 1961138568Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1962138568Ssam if (ni->ni_scangen != gen) { 1963138568Ssam ni->ni_scangen = gen; 1964138568Ssam (void) ieee80211_ref_node(ni); 1965138568Ssam IEEE80211_NODE_UNLOCK(nt); 1966138568Ssam (*f)(arg, ni); 1967138568Ssam ieee80211_free_node(ni); 1968138568Ssam goto restart; 1969138568Ssam } 1970138568Ssam } 1971138568Ssam IEEE80211_NODE_UNLOCK(nt); 1972138568Ssam 1973138568Ssam IEEE80211_SCAN_UNLOCK(nt); 1974116742Ssam} 1975138568Ssam 1976138568Ssamvoid 1977138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1978138568Ssam{ 1979138568Ssam printf("0x%p: mac %s refcnt %d\n", ni, 1980138568Ssam ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 1981138568Ssam printf("\tscangen %u authmode %u flags 0x%x\n", 1982138568Ssam ni->ni_scangen, ni->ni_authmode, ni->ni_flags); 1983138568Ssam printf("\tassocid 0x%x txpower %u vlan %u\n", 1984138568Ssam ni->ni_associd, ni->ni_txpower, ni->ni_vlan); 1985138568Ssam printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n", 1986167439Ssam ni->ni_txseqs[IEEE80211_NONQOS_TID], 1987167439Ssam ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT, 1988167439Ssam ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK, 1989138568Ssam ni->ni_rxfragstamp); 1990138568Ssam printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n", 1991138568Ssam ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo); 1992138568Ssam printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n", 1993138568Ssam ether_sprintf(ni->ni_bssid), 1994138568Ssam ni->ni_esslen, ni->ni_essid, 1995138568Ssam ni->ni_chan->ic_freq, ni->ni_chan->ic_flags); 1996138568Ssam printf("\tfails %u inact %u txrate %u\n", 1997138568Ssam ni->ni_fails, ni->ni_inact, ni->ni_txrate); 1998138568Ssam} 1999138568Ssam 2000138568Ssamvoid 2001138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt) 2002138568Ssam{ 2003138568Ssam ieee80211_iterate_nodes(nt, 2004138568Ssam (ieee80211_iter_func *) ieee80211_dump_node, nt); 2005138568Ssam} 2006138568Ssam 2007138568Ssam/* 2008138568Ssam * Handle a station joining an 11g network. 2009138568Ssam */ 2010138568Ssamstatic void 2011138568Ssamieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 2012138568Ssam{ 2013138568Ssam 2014138568Ssam /* 2015138568Ssam * Station isn't capable of short slot time. Bump 2016138568Ssam * the count of long slot time stations and disable 2017138568Ssam * use of short slot time. Note that the actual switch 2018138568Ssam * over to long slot time use may not occur until the 2019138568Ssam * next beacon transmission (per sec. 7.3.1.4 of 11g). 2020138568Ssam */ 2021138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2022138568Ssam ic->ic_longslotsta++; 2023138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2024138568Ssam "[%s] station needs long slot time, count %d\n", 2025138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 2026138568Ssam /* XXX vap's w/ conflicting needs won't work */ 2027138568Ssam ieee80211_set_shortslottime(ic, 0); 2028138568Ssam } 2029138568Ssam /* 2030138568Ssam * If the new station is not an ERP station 2031138568Ssam * then bump the counter and enable protection 2032138568Ssam * if configured. 2033138568Ssam */ 2034138568Ssam if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) { 2035138568Ssam ic->ic_nonerpsta++; 2036138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2037138568Ssam "[%s] station is !ERP, %d non-ERP stations associated\n", 2038138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 2039138568Ssam /* 2040138568Ssam * If protection is configured, enable it. 2041138568Ssam */ 2042138568Ssam if (ic->ic_protmode != IEEE80211_PROT_NONE) { 2043138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2044138568Ssam "%s: enable use of protection\n", __func__); 2045138568Ssam ic->ic_flags |= IEEE80211_F_USEPROT; 2046138568Ssam } 2047138568Ssam /* 2048138568Ssam * If station does not support short preamble 2049138568Ssam * then we must enable use of Barker preamble. 2050138568Ssam */ 2051138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) { 2052138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2053138568Ssam "[%s] station needs long preamble\n", 2054138568Ssam ether_sprintf(ni->ni_macaddr)); 2055138568Ssam ic->ic_flags |= IEEE80211_F_USEBARKER; 2056138568Ssam ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 2057138568Ssam } 2058153973Ssam if (ic->ic_nonerpsta == 1) 2059153973Ssam ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE; 2060138568Ssam } else 2061138568Ssam ni->ni_flags |= IEEE80211_NODE_ERP; 2062138568Ssam} 2063138568Ssam 2064138568Ssamvoid 2065138568Ssamieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp) 2066138568Ssam{ 2067138568Ssam int newassoc; 2068138568Ssam 2069138568Ssam if (ni->ni_associd == 0) { 2070138568Ssam u_int16_t aid; 2071138568Ssam 2072138568Ssam /* 2073138568Ssam * It would be good to search the bitmap 2074138568Ssam * more efficiently, but this will do for now. 2075138568Ssam */ 2076138568Ssam for (aid = 1; aid < ic->ic_max_aid; aid++) { 2077138568Ssam if (!IEEE80211_AID_ISSET(aid, 2078138568Ssam ic->ic_aid_bitmap)) 2079138568Ssam break; 2080138568Ssam } 2081138568Ssam if (aid >= ic->ic_max_aid) { 2082138568Ssam IEEE80211_SEND_MGMT(ic, ni, resp, 2083138568Ssam IEEE80211_REASON_ASSOC_TOOMANY); 2084138568Ssam ieee80211_node_leave(ic, ni); 2085138568Ssam return; 2086138568Ssam } 2087138568Ssam ni->ni_associd = aid | 0xc000; 2088138568Ssam IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap); 2089138568Ssam ic->ic_sta_assoc++; 2090138568Ssam newassoc = 1; 2091166012Ssam if (ic->ic_curmode == IEEE80211_MODE_11G && 2092166012Ssam IEEE80211_IS_CHAN_FULL(ni->ni_chan)) 2093138568Ssam ieee80211_node_join_11g(ic, ni); 2094138568Ssam } else 2095138568Ssam newassoc = 0; 2096138568Ssam 2097138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 2098139523Ssam "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n", 2099139523Ssam ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re", 2100139523Ssam IEEE80211_NODE_AID(ni), 2101139523Ssam ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long", 2102139523Ssam ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", 2103139523Ssam ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "", 2104139523Ssam ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "" 2105139523Ssam ); 2106138568Ssam 2107138568Ssam /* give driver a chance to setup state like ni_txrate */ 2108139524Ssam if (ic->ic_newassoc != NULL) 2109148307Ssam ic->ic_newassoc(ni, newassoc); 2110139528Ssam ni->ni_inact_reload = ic->ic_inact_auth; 2111139528Ssam ni->ni_inact = ni->ni_inact_reload; 2112138568Ssam IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS); 2113138568Ssam /* tell the authenticator about new station */ 2114138568Ssam if (ic->ic_auth->ia_node_join != NULL) 2115138568Ssam ic->ic_auth->ia_node_join(ic, ni); 2116138568Ssam ieee80211_notify_node_join(ic, ni, newassoc); 2117138568Ssam} 2118138568Ssam 2119138568Ssam/* 2120138568Ssam * Handle a station leaving an 11g network. 2121138568Ssam */ 2122138568Ssamstatic void 2123138568Ssamieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 2124138568Ssam{ 2125138568Ssam 2126149031Ssam KASSERT(ic->ic_curmode == IEEE80211_MODE_11G, 2127148941Ssam ("not in 11g, bss %u:0x%x, curmode %u", ni->ni_chan->ic_freq, 2128148941Ssam ni->ni_chan->ic_flags, ic->ic_curmode)); 2129138568Ssam 2130138568Ssam /* 2131138568Ssam * If a long slot station do the slot time bookkeeping. 2132138568Ssam */ 2133138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2134138568Ssam KASSERT(ic->ic_longslotsta > 0, 2135138568Ssam ("bogus long slot station count %d", ic->ic_longslotsta)); 2136138568Ssam ic->ic_longslotsta--; 2137138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2138138568Ssam "[%s] long slot time station leaves, count now %d\n", 2139138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 2140138568Ssam if (ic->ic_longslotsta == 0) { 2141138568Ssam /* 2142138568Ssam * Re-enable use of short slot time if supported 2143138568Ssam * and not operating in IBSS mode (per spec). 2144138568Ssam */ 2145138568Ssam if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 2146138568Ssam ic->ic_opmode != IEEE80211_M_IBSS) { 2147138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2148138568Ssam "%s: re-enable use of short slot time\n", 2149138568Ssam __func__); 2150138568Ssam ieee80211_set_shortslottime(ic, 1); 2151138568Ssam } 2152138568Ssam } 2153138568Ssam } 2154138568Ssam /* 2155138568Ssam * If a non-ERP station do the protection-related bookkeeping. 2156138568Ssam */ 2157138568Ssam if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) { 2158138568Ssam KASSERT(ic->ic_nonerpsta > 0, 2159138568Ssam ("bogus non-ERP station count %d", ic->ic_nonerpsta)); 2160138568Ssam ic->ic_nonerpsta--; 2161138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2162138568Ssam "[%s] non-ERP station leaves, count now %d\n", 2163138568Ssam ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 2164138568Ssam if (ic->ic_nonerpsta == 0) { 2165138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2166138568Ssam "%s: disable use of protection\n", __func__); 2167138568Ssam ic->ic_flags &= ~IEEE80211_F_USEPROT; 2168138568Ssam /* XXX verify mode? */ 2169138568Ssam if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) { 2170138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2171138568Ssam "%s: re-enable use of short preamble\n", 2172138568Ssam __func__); 2173138568Ssam ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 2174138568Ssam ic->ic_flags &= ~IEEE80211_F_USEBARKER; 2175138568Ssam } 2176153973Ssam ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE; 2177138568Ssam } 2178138568Ssam } 2179138568Ssam} 2180138568Ssam 2181138568Ssam/* 2182138568Ssam * Handle bookkeeping for station deauthentication/disassociation 2183138568Ssam * when operating as an ap. 2184138568Ssam */ 2185138568Ssamvoid 2186138568Ssamieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 2187138568Ssam{ 2188140499Ssam struct ieee80211_node_table *nt = ni->ni_table; 2189138568Ssam 2190138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 2191138568Ssam "[%s] station with aid %d leaves\n", 2192138568Ssam ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni)); 2193138568Ssam 2194138568Ssam KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 2195138568Ssam ic->ic_opmode == IEEE80211_M_IBSS || 2196138568Ssam ic->ic_opmode == IEEE80211_M_AHDEMO, 2197138568Ssam ("unexpected operating mode %u", ic->ic_opmode)); 2198138568Ssam /* 2199138568Ssam * If node wasn't previously associated all 2200138568Ssam * we need to do is reclaim the reference. 2201138568Ssam */ 2202138568Ssam /* XXX ibss mode bypasses 11g and notification */ 2203138568Ssam if (ni->ni_associd == 0) 2204138568Ssam goto done; 2205138568Ssam /* 2206138568Ssam * Tell the authenticator the station is leaving. 2207138568Ssam * Note that we must do this before yanking the 2208138568Ssam * association id as the authenticator uses the 2209138568Ssam * associd to locate it's state block. 2210138568Ssam */ 2211138568Ssam if (ic->ic_auth->ia_node_leave != NULL) 2212138568Ssam ic->ic_auth->ia_node_leave(ic, ni); 2213138568Ssam IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 2214138568Ssam ni->ni_associd = 0; 2215138568Ssam ic->ic_sta_assoc--; 2216138568Ssam 2217166012Ssam if (ic->ic_curmode == IEEE80211_MODE_11G && 2218166012Ssam IEEE80211_IS_CHAN_FULL(ni->ni_chan)) 2219138568Ssam ieee80211_node_leave_11g(ic, ni); 2220138568Ssam /* 2221138568Ssam * Cleanup station state. In particular clear various 2222138568Ssam * state that might otherwise be reused if the node 2223138568Ssam * is reused before the reference count goes to zero 2224138568Ssam * (and memory is reclaimed). 2225138568Ssam */ 2226138568Ssam ieee80211_sta_leave(ic, ni); 2227138568Ssamdone: 2228140499Ssam /* 2229140499Ssam * Remove the node from any table it's recorded in and 2230140499Ssam * drop the caller's reference. Removal from the table 2231140499Ssam * is important to insure the node is not reprocessed 2232140499Ssam * for inactivity. 2233140499Ssam */ 2234140499Ssam if (nt != NULL) { 2235140499Ssam IEEE80211_NODE_LOCK(nt); 2236140499Ssam node_reclaim(nt, ni); 2237140499Ssam IEEE80211_NODE_UNLOCK(nt); 2238140499Ssam } else 2239140499Ssam ieee80211_free_node(ni); 2240138568Ssam} 2241138568Ssam 2242138568Ssamu_int8_t 2243138568Ssamieee80211_getrssi(struct ieee80211com *ic) 2244138568Ssam{ 2245138568Ssam#define NZ(x) ((x) == 0 ? 1 : (x)) 2246140753Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 2247138568Ssam u_int32_t rssi_samples, rssi_total; 2248138568Ssam struct ieee80211_node *ni; 2249138568Ssam 2250138568Ssam rssi_total = 0; 2251138568Ssam rssi_samples = 0; 2252138568Ssam switch (ic->ic_opmode) { 2253138568Ssam case IEEE80211_M_IBSS: /* average of all ibss neighbors */ 2254138568Ssam /* XXX locking */ 2255140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 2256138568Ssam if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) { 2257138568Ssam rssi_samples++; 2258138568Ssam rssi_total += ic->ic_node_getrssi(ni); 2259138568Ssam } 2260138568Ssam break; 2261138568Ssam case IEEE80211_M_AHDEMO: /* average of all neighbors */ 2262138568Ssam /* XXX locking */ 2263140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 2264138568Ssam rssi_samples++; 2265138568Ssam rssi_total += ic->ic_node_getrssi(ni); 2266138568Ssam } 2267138568Ssam break; 2268138568Ssam case IEEE80211_M_HOSTAP: /* average of all associated stations */ 2269138568Ssam /* XXX locking */ 2270140753Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 2271138568Ssam if (IEEE80211_AID(ni->ni_associd) != 0) { 2272138568Ssam rssi_samples++; 2273138568Ssam rssi_total += ic->ic_node_getrssi(ni); 2274138568Ssam } 2275138568Ssam break; 2276138568Ssam case IEEE80211_M_MONITOR: /* XXX */ 2277138568Ssam case IEEE80211_M_STA: /* use stats from associated ap */ 2278138568Ssam default: 2279138568Ssam if (ic->ic_bss != NULL) 2280138568Ssam rssi_total = ic->ic_node_getrssi(ic->ic_bss); 2281138568Ssam rssi_samples = 1; 2282138568Ssam break; 2283138568Ssam } 2284138568Ssam return rssi_total / NZ(rssi_samples); 2285138568Ssam#undef NZ 2286138568Ssam} 2287138568Ssam 2288138568Ssam/* 2289138568Ssam * Indicate whether there are frames queued for a station in power-save mode. 2290138568Ssam */ 2291138568Ssamstatic void 2292148304Ssamieee80211_set_tim(struct ieee80211_node *ni, int set) 2293138568Ssam{ 2294148304Ssam struct ieee80211com *ic = ni->ni_ic; 2295138568Ssam u_int16_t aid; 2296138568Ssam 2297138568Ssam KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 2298138568Ssam ic->ic_opmode == IEEE80211_M_IBSS, 2299138568Ssam ("operating mode %u", ic->ic_opmode)); 2300138568Ssam 2301138568Ssam aid = IEEE80211_AID(ni->ni_associd); 2302138568Ssam KASSERT(aid < ic->ic_max_aid, 2303138568Ssam ("bogus aid %u, max %u", aid, ic->ic_max_aid)); 2304138568Ssam 2305138568Ssam IEEE80211_BEACON_LOCK(ic); 2306138568Ssam if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) { 2307138568Ssam if (set) { 2308138568Ssam setbit(ic->ic_tim_bitmap, aid); 2309138568Ssam ic->ic_ps_pending++; 2310138568Ssam } else { 2311138568Ssam clrbit(ic->ic_tim_bitmap, aid); 2312138568Ssam ic->ic_ps_pending--; 2313138568Ssam } 2314138568Ssam ic->ic_flags |= IEEE80211_F_TIMUPDATE; 2315138568Ssam } 2316138568Ssam IEEE80211_BEACON_UNLOCK(ic); 2317138568Ssam} 2318138568Ssam 2319138568Ssam/* 2320138568Ssam * Node table support. 2321138568Ssam */ 2322138568Ssam 2323138568Ssamstatic void 2324138568Ssamieee80211_node_table_init(struct ieee80211com *ic, 2325138568Ssam struct ieee80211_node_table *nt, 2326148863Ssam const char *name, int inact, int keyixmax, 2327138568Ssam void (*timeout)(struct ieee80211_node_table *)) 2328138568Ssam{ 2329138568Ssam 2330138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 2331138568Ssam "%s %s table, inact %u\n", __func__, name, inact); 2332138568Ssam 2333138568Ssam nt->nt_ic = ic; 2334138568Ssam /* XXX need unit */ 2335138568Ssam IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname); 2336138568Ssam IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname); 2337138568Ssam TAILQ_INIT(&nt->nt_node); 2338138568Ssam nt->nt_name = name; 2339138568Ssam nt->nt_scangen = 1; 2340138568Ssam nt->nt_inact_init = inact; 2341138568Ssam nt->nt_timeout = timeout; 2342148863Ssam nt->nt_keyixmax = keyixmax; 2343148863Ssam if (nt->nt_keyixmax > 0) { 2344148863Ssam MALLOC(nt->nt_keyixmap, struct ieee80211_node **, 2345148863Ssam keyixmax * sizeof(struct ieee80211_node *), 2346148863Ssam M_80211_NODE, M_NOWAIT | M_ZERO); 2347148863Ssam if (nt->nt_keyixmap == NULL) 2348148863Ssam if_printf(ic->ic_ifp, 2349148863Ssam "Cannot allocate key index map with %u entries\n", 2350148863Ssam keyixmax); 2351148863Ssam } else 2352148863Ssam nt->nt_keyixmap = NULL; 2353138568Ssam} 2354138568Ssam 2355138568Ssamvoid 2356138568Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt) 2357138568Ssam{ 2358138568Ssam 2359138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 2360138568Ssam "%s %s table\n", __func__, nt->nt_name); 2361138568Ssam 2362138568Ssam IEEE80211_NODE_LOCK(nt); 2363138568Ssam nt->nt_inact_timer = 0; 2364138568Ssam ieee80211_free_allnodes_locked(nt); 2365138568Ssam IEEE80211_NODE_UNLOCK(nt); 2366138568Ssam} 2367138568Ssam 2368138568Ssamstatic void 2369138568Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt) 2370138568Ssam{ 2371138568Ssam 2372138568Ssam IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 2373138568Ssam "%s %s table\n", __func__, nt->nt_name); 2374138568Ssam 2375148863Ssam IEEE80211_NODE_LOCK(nt); 2376138568Ssam ieee80211_free_allnodes_locked(nt); 2377148863Ssam if (nt->nt_keyixmap != NULL) { 2378148863Ssam /* XXX verify all entries are NULL */ 2379148863Ssam int i; 2380148863Ssam for (i = 0; i < nt->nt_keyixmax; i++) 2381148863Ssam if (nt->nt_keyixmap[i] != NULL) 2382148863Ssam printf("%s: %s[%u] still active\n", __func__, 2383148863Ssam nt->nt_name, i); 2384148863Ssam FREE(nt->nt_keyixmap, M_80211_NODE); 2385148863Ssam nt->nt_keyixmap = NULL; 2386148863Ssam } 2387138568Ssam IEEE80211_SCAN_LOCK_DESTROY(nt); 2388138568Ssam IEEE80211_NODE_LOCK_DESTROY(nt); 2389138568Ssam} 2390