ieee80211_node.c revision 184303
1116742Ssam/*- 2116904Ssam * Copyright (c) 2001 Atsushi Onoe 3178354Ssam * Copyright (c) 2002-2008 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 184303 2008-10-26 21:59:07Z sam $"); 29116742Ssam 30178354Ssam#include "opt_wlan.h" 31178354Ssam 32116742Ssam#include <sys/param.h> 33116742Ssam#include <sys/systm.h> 34116742Ssam#include <sys/mbuf.h> 35116742Ssam#include <sys/malloc.h> 36116742Ssam#include <sys/kernel.h> 37138568Ssam 38116742Ssam#include <sys/socket.h> 39116742Ssam 40116742Ssam#include <net/if.h> 41116742Ssam#include <net/if_media.h> 42116742Ssam#include <net/ethernet.h> 43116742Ssam 44116742Ssam#include <net80211/ieee80211_var.h> 45178354Ssam#include <net80211/ieee80211_input.h> 46178354Ssam#include <net80211/ieee80211_wds.h> 47116742Ssam 48116742Ssam#include <net/bpf.h> 49116742Ssam 50147221Ssam/* 51147221Ssam * Association id's are managed with a bit vector. 52147221Ssam */ 53178354Ssam#define IEEE80211_AID_SET(_vap, b) \ 54178354Ssam ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \ 55178354Ssam (1 << (IEEE80211_AID(b) % 32))) 56178354Ssam#define IEEE80211_AID_CLR(_vap, b) \ 57178354Ssam ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \ 58178354Ssam ~(1 << (IEEE80211_AID(b) % 32))) 59178354Ssam#define IEEE80211_AID_ISSET(_vap, b) \ 60178354Ssam ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32))) 61147221Ssam 62159139Sdds#ifdef IEEE80211_DEBUG_REFCNT 63159139Sdds#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line 64159139Sdds#else 65159139Sdds#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__ 66159139Sdds#endif 67159139Sdds 68170530Ssamstatic int ieee80211_sta_join1(struct ieee80211_node *); 69170530Ssam 70179643Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211vap *, 71179643Ssam const uint8_t [IEEE80211_ADDR_LEN]); 72138568Ssamstatic void node_cleanup(struct ieee80211_node *); 73138568Ssamstatic void node_free(struct ieee80211_node *); 74178354Ssamstatic void node_age(struct ieee80211_node *); 75170530Ssamstatic int8_t node_getrssi(const struct ieee80211_node *); 76170530Ssamstatic void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *); 77178354Ssamstatic void node_getmimoinfo(const struct ieee80211_node *, 78178354Ssam struct ieee80211_mimo_info *); 79116742Ssam 80138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *); 81120104Ssam 82138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic, 83148863Ssam struct ieee80211_node_table *nt, const char *name, 84170530Ssam int inact, int keymaxix); 85178354Ssamstatic void ieee80211_node_table_reset(struct ieee80211_node_table *, 86178354Ssam struct ieee80211vap *); 87178354Ssamstatic void ieee80211_node_reclaim(struct ieee80211_node *); 88138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt); 89172211Ssamstatic void ieee80211_erp_timeout(struct ieee80211com *); 90138568Ssam 91127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state"); 92178354SsamMALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie"); 93120481Ssam 94116742Ssamvoid 95138568Ssamieee80211_node_attach(struct ieee80211com *ic) 96116742Ssam{ 97178354Ssam ieee80211_node_table_init(ic, &ic->ic_sta, "station", 98178354Ssam IEEE80211_INACT_INIT, ic->ic_max_keyix); 99178354Ssam callout_init(&ic->ic_inact, CALLOUT_MPSAFE); 100178354Ssam callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz, 101178354Ssam ieee80211_node_timeout, ic); 102116742Ssam 103138568Ssam ic->ic_node_alloc = node_alloc; 104138568Ssam ic->ic_node_free = node_free; 105138568Ssam ic->ic_node_cleanup = node_cleanup; 106178354Ssam ic->ic_node_age = node_age; 107178354Ssam ic->ic_node_drain = node_age; /* NB: same as age */ 108138568Ssam ic->ic_node_getrssi = node_getrssi; 109170530Ssam ic->ic_node_getsignal = node_getsignal; 110178354Ssam ic->ic_node_getmimoinfo = node_getmimoinfo; 111138568Ssam 112178354Ssam /* 113178354Ssam * Set flags to be propagated to all vap's; 114178354Ssam * these define default behaviour/configuration. 115178354Ssam */ 116178354Ssam ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */ 117178354Ssam} 118138568Ssam 119178354Ssamvoid 120178354Ssamieee80211_node_detach(struct ieee80211com *ic) 121178354Ssam{ 122170530Ssam 123178354Ssam callout_drain(&ic->ic_inact); 124178354Ssam ieee80211_node_table_cleanup(&ic->ic_sta); 125178354Ssam} 126172062Ssam 127178354Ssamvoid 128178354Ssamieee80211_node_vattach(struct ieee80211vap *vap) 129178354Ssam{ 130178354Ssam /* NB: driver can override */ 131178354Ssam vap->iv_max_aid = IEEE80211_AID_DEF; 132178354Ssam 133178354Ssam /* default station inactivity timer setings */ 134178354Ssam vap->iv_inact_init = IEEE80211_INACT_INIT; 135178354Ssam vap->iv_inact_auth = IEEE80211_INACT_AUTH; 136178354Ssam vap->iv_inact_run = IEEE80211_INACT_RUN; 137178354Ssam vap->iv_inact_probe = IEEE80211_INACT_PROBE; 138184277Ssam 139184277Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT, 140184277Ssam "%s: init %u auth %u run %u probe %u\n", __func__, 141184277Ssam vap->iv_inact_init, vap->iv_inact_auth, 142184277Ssam vap->iv_inact_run, vap->iv_inact_probe); 143148863Ssam} 144148863Ssam 145148863Ssamvoid 146178354Ssamieee80211_node_latevattach(struct ieee80211vap *vap) 147148863Ssam{ 148178354Ssam if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 149178354Ssam /* XXX should we allow max aid to be zero? */ 150178354Ssam if (vap->iv_max_aid < IEEE80211_AID_MIN) { 151178354Ssam vap->iv_max_aid = IEEE80211_AID_MIN; 152178354Ssam if_printf(vap->iv_ifp, 153178354Ssam "WARNING: max aid too small, changed to %d\n", 154178354Ssam vap->iv_max_aid); 155178354Ssam } 156184210Sdes MALLOC(vap->iv_aid_bitmap, uint32_t *, 157184210Sdes howmany(vap->iv_max_aid, 32) * sizeof(uint32_t), 158178354Ssam M_80211_NODE, M_NOWAIT | M_ZERO); 159178354Ssam if (vap->iv_aid_bitmap == NULL) { 160178354Ssam /* XXX no way to recover */ 161178354Ssam printf("%s: no memory for AID bitmap, max aid %d!\n", 162178354Ssam __func__, vap->iv_max_aid); 163178354Ssam vap->iv_max_aid = 0; 164178354Ssam } 165138568Ssam } 166138568Ssam 167178354Ssam ieee80211_reset_bss(vap); 168118887Ssam 169178354Ssam vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode); 170116742Ssam} 171116742Ssam 172116742Ssamvoid 173178354Ssamieee80211_node_vdetach(struct ieee80211vap *vap) 174116742Ssam{ 175178354Ssam struct ieee80211com *ic = vap->iv_ic; 176116742Ssam 177178354Ssam ieee80211_node_table_reset(&ic->ic_sta, vap); 178178354Ssam if (vap->iv_bss != NULL) { 179178354Ssam ieee80211_free_node(vap->iv_bss); 180178354Ssam vap->iv_bss = NULL; 181138568Ssam } 182178354Ssam if (vap->iv_aid_bitmap != NULL) { 183184210Sdes FREE(vap->iv_aid_bitmap, M_80211_NODE); 184178354Ssam vap->iv_aid_bitmap = NULL; 185138568Ssam } 186116742Ssam} 187116742Ssam 188138568Ssam/* 189138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator. 190138568Ssam */ 191138568Ssam 192138568Ssamvoid 193148302Ssamieee80211_node_authorize(struct ieee80211_node *ni) 194138568Ssam{ 195184277Ssam struct ieee80211vap *vap = ni->ni_vap; 196184277Ssam 197138568Ssam ni->ni_flags |= IEEE80211_NODE_AUTH; 198184277Ssam ni->ni_inact_reload = vap->iv_inact_run; 199172062Ssam ni->ni_inact = ni->ni_inact_reload; 200184277Ssam 201184277Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 202184277Ssam "%s: inact_reload %u", __func__, ni->ni_inact_reload); 203138568Ssam} 204138568Ssam 205138568Ssamvoid 206148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni) 207138568Ssam{ 208184277Ssam struct ieee80211vap *vap = ni->ni_vap; 209184277Ssam 210138568Ssam ni->ni_flags &= ~IEEE80211_NODE_AUTH; 211184277Ssam ni->ni_inact_reload = vap->iv_inact_auth; 212172062Ssam if (ni->ni_inact > ni->ni_inact_reload) 213172062Ssam ni->ni_inact = ni->ni_inact_reload; 214184277Ssam 215184277Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 216184277Ssam "%s: inact_reload %u inact %u", __func__, 217184277Ssam ni->ni_inact_reload, ni->ni_inact); 218138568Ssam} 219138568Ssam 220116742Ssam/* 221183251Ssam * Fix tx parameters for a node according to ``association state''. 222183251Ssam */ 223183251Ssamstatic void 224183251Ssamnode_setuptxparms(struct ieee80211_node *ni) 225183251Ssam{ 226183251Ssam struct ieee80211vap *vap = ni->ni_vap; 227183251Ssam 228183251Ssam if (ni->ni_flags & IEEE80211_NODE_HT) { 229183251Ssam if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan)) 230183251Ssam ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NA]; 231183251Ssam else 232183251Ssam ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NG]; 233183251Ssam } else { /* legacy rate handling */ 234183251Ssam if (IEEE80211_IS_CHAN_A(ni->ni_chan)) 235183251Ssam ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11A]; 236183251Ssam else if (ni->ni_flags & IEEE80211_NODE_ERP) 237183251Ssam ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11G]; 238183251Ssam else 239183251Ssam ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11B]; 240183251Ssam } 241183251Ssam} 242183251Ssam 243183251Ssam/* 244138568Ssam * Set/change the channel. The rate set is also updated as 245138568Ssam * to insure a consistent view by drivers. 246178354Ssam * XXX should be private but hostap needs it to deal with CSA 247138568Ssam */ 248178354Ssamvoid 249178354Ssamieee80211_node_set_chan(struct ieee80211_node *ni, 250178354Ssam struct ieee80211_channel *chan) 251138568Ssam{ 252178354Ssam struct ieee80211com *ic = ni->ni_ic; 253183251Ssam struct ieee80211vap *vap = ni->ni_vap; 254183251Ssam enum ieee80211_phymode mode; 255170530Ssam 256178354Ssam KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel")); 257178354Ssam 258138568Ssam ni->ni_chan = chan; 259183251Ssam mode = ieee80211_chan2mode(chan); 260170530Ssam if (IEEE80211_IS_CHAN_HT(chan)) { 261170530Ssam /* 262170530Ssam * XXX Gotta be careful here; the rate set returned by 263170530Ssam * ieee80211_get_suprates is actually any HT rate 264170530Ssam * set so blindly copying it will be bad. We must 265170530Ssam * install the legacy rate est in ni_rates and the 266170530Ssam * HT rate set in ni_htrates. 267170530Ssam */ 268170530Ssam ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan); 269183251Ssam /* 270183251Ssam * Setup bss tx parameters based on operating mode. We 271183251Ssam * use legacy rates when operating in a mixed HT+non-HT bss 272183251Ssam * and non-ERP rates in 11g for mixed ERP+non-ERP bss. 273183251Ssam */ 274183251Ssam if (mode == IEEE80211_MODE_11NA && 275183251Ssam (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0) 276183251Ssam mode = IEEE80211_MODE_11A; 277183251Ssam else if (mode == IEEE80211_MODE_11NG && 278183251Ssam (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0) 279183251Ssam mode = IEEE80211_MODE_11G; 280183251Ssam if (mode == IEEE80211_MODE_11G && 281183251Ssam (vap->iv_flags & IEEE80211_F_PUREG) == 0) 282183251Ssam mode = IEEE80211_MODE_11B; 283170530Ssam } 284183251Ssam ni->ni_txparms = &vap->iv_txparms[mode]; 285165569Ssam ni->ni_rates = *ieee80211_get_suprates(ic, chan); 286138568Ssam} 287138568Ssam 288141658Ssamstatic __inline void 289141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss) 290141658Ssam{ 291141658Ssam /* propagate useful state */ 292141658Ssam nbss->ni_authmode = obss->ni_authmode; 293141658Ssam nbss->ni_txpower = obss->ni_txpower; 294141658Ssam nbss->ni_vlan = obss->ni_vlan; 295141658Ssam /* XXX statistics? */ 296178354Ssam /* XXX legacy WDS bssid? */ 297141658Ssam} 298141658Ssam 299116742Ssamvoid 300178354Ssamieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan) 301116742Ssam{ 302178354Ssam struct ieee80211com *ic = vap->iv_ic; 303116742Ssam struct ieee80211_node *ni; 304116742Ssam 305178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 306178354Ssam "%s: creating ibss on channel %u\n", __func__, 307178354Ssam ieee80211_chan2ieee(ic, chan)); 308138568Ssam 309178354Ssam ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr); 310140753Ssam if (ni == NULL) { 311140753Ssam /* XXX recovery? */ 312138568Ssam return; 313138568Ssam } 314178354Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr); 315178354Ssam ni->ni_esslen = vap->iv_des_ssid[0].len; 316178354Ssam memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen); 317178354Ssam if (vap->iv_bss != NULL) 318178354Ssam copy_bss(ni, vap->iv_bss); 319148843Ssam ni->ni_intval = ic->ic_bintval; 320178354Ssam if (vap->iv_flags & IEEE80211_F_PRIVACY) 321116742Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 322116742Ssam if (ic->ic_phytype == IEEE80211_T_FH) { 323116742Ssam ni->ni_fhdwell = 200; /* XXX */ 324116742Ssam ni->ni_fhindex = 1; 325116742Ssam } 326178354Ssam if (vap->iv_opmode == IEEE80211_M_IBSS) { 327178354Ssam vap->iv_flags |= IEEE80211_F_SIBSS; 328138568Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */ 329178354Ssam if (vap->iv_flags & IEEE80211_F_DESBSSID) 330178354Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid); 331167282Ssam else { 332167282Ssam get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN); 333167282Ssam /* clear group bit, add local bit */ 334167282Ssam ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02; 335167282Ssam } 336178354Ssam } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) { 337178354Ssam if (vap->iv_flags & IEEE80211_F_DESBSSID) 338178354Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid); 339153403Ssam else 340153403Ssam memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN); 341138568Ssam } 342138568Ssam /* 343138568Ssam * Fix the channel and related attributes. 344138568Ssam */ 345178354Ssam /* clear DFS CAC state on previous channel */ 346178354Ssam if (ic->ic_bsschan != IEEE80211_CHAN_ANYC && 347178354Ssam ic->ic_bsschan->ic_freq != chan->ic_freq && 348178354Ssam IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) 349178354Ssam ieee80211_dfs_cac_clear(ic, ic->ic_bsschan); 350170530Ssam ic->ic_bsschan = chan; 351178354Ssam ieee80211_node_set_chan(ni, chan); 352170530Ssam ic->ic_curmode = ieee80211_chan2mode(chan); 353138568Ssam /* 354178354Ssam * Do mode-specific setup. 355138568Ssam */ 356170530Ssam if (IEEE80211_IS_CHAN_FULL(chan)) { 357170530Ssam if (IEEE80211_IS_CHAN_ANYG(chan)) { 358170530Ssam /* 359178354Ssam * Use a mixed 11b/11g basic rate set. 360170530Ssam */ 361178354Ssam ieee80211_setbasicrates(&ni->ni_rates, 362178354Ssam IEEE80211_MODE_11G); 363178354Ssam if (vap->iv_flags & IEEE80211_F_PUREG) { 364178354Ssam /* 365178354Ssam * Also mark OFDM rates basic so 11b 366178354Ssam * stations do not join (WiFi compliance). 367178354Ssam */ 368178354Ssam ieee80211_addbasicrates(&ni->ni_rates, 369178354Ssam IEEE80211_MODE_11A); 370178354Ssam } 371170530Ssam } else if (IEEE80211_IS_CHAN_B(chan)) { 372170530Ssam /* 373170530Ssam * Force pure 11b rate set. 374170530Ssam */ 375178354Ssam ieee80211_setbasicrates(&ni->ni_rates, 376170530Ssam IEEE80211_MODE_11B); 377170530Ssam } 378170530Ssam } 379138568Ssam 380170530Ssam (void) ieee80211_sta_join1(ieee80211_ref_node(ni)); 381116742Ssam} 382116742Ssam 383170530Ssam/* 384170530Ssam * Reset bss state on transition to the INIT state. 385170530Ssam * Clear any stations from the table (they have been 386170530Ssam * deauth'd) and reset the bss node (clears key, rate 387170530Ssam * etc. state). 388170530Ssam */ 389138568Ssamvoid 390178354Ssamieee80211_reset_bss(struct ieee80211vap *vap) 391138568Ssam{ 392178354Ssam struct ieee80211com *ic = vap->iv_ic; 393138568Ssam struct ieee80211_node *ni, *obss; 394138568Ssam 395178354Ssam ieee80211_node_table_reset(&ic->ic_sta, vap); 396178354Ssam /* XXX multi-bss: wrong */ 397170530Ssam ieee80211_reset_erp(ic); 398140753Ssam 399178354Ssam ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr); 400138568Ssam KASSERT(ni != NULL, ("unable to setup inital BSS node")); 401178354Ssam obss = vap->iv_bss; 402178354Ssam vap->iv_bss = ieee80211_ref_node(ni); 403141658Ssam if (obss != NULL) { 404141658Ssam copy_bss(ni, obss); 405148843Ssam ni->ni_intval = ic->ic_bintval; 406138568Ssam ieee80211_free_node(obss); 407178354Ssam } else 408178354Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr); 409138568Ssam} 410138568Ssam 411170530Ssamstatic int 412170530Ssammatch_ssid(const struct ieee80211_node *ni, 413170530Ssam int nssid, const struct ieee80211_scan_ssid ssids[]) 414170530Ssam{ 415170530Ssam int i; 416148432Ssam 417170530Ssam for (i = 0; i < nssid; i++) { 418170530Ssam if (ni->ni_esslen == ssids[i].len && 419170530Ssam memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0) 420170530Ssam return 1; 421170530Ssam } 422170530Ssam return 0; 423170530Ssam} 424170530Ssam 425170530Ssam/* 426170530Ssam * Test a node for suitability/compatibility. 427170530Ssam */ 428127767Ssamstatic int 429178354Ssamcheck_bss(struct ieee80211vap *vap, struct ieee80211_node *ni) 430127767Ssam{ 431178354Ssam struct ieee80211com *ic = ni->ni_ic; 432170530Ssam uint8_t rate; 433170530Ssam 434170530Ssam if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 435170530Ssam return 0; 436178354Ssam if (vap->iv_opmode == IEEE80211_M_IBSS) { 437170530Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 438170530Ssam return 0; 439170530Ssam } else { 440170530Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 441170530Ssam return 0; 442170530Ssam } 443178354Ssam if (vap->iv_flags & IEEE80211_F_PRIVACY) { 444170530Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 445170530Ssam return 0; 446170530Ssam } else { 447170530Ssam /* XXX does this mean privacy is supported or required? */ 448170530Ssam if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 449170530Ssam return 0; 450170530Ssam } 451170530Ssam rate = ieee80211_fix_rate(ni, &ni->ni_rates, 452170530Ssam IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 453170530Ssam if (rate & IEEE80211_RATE_BASIC) 454170530Ssam return 0; 455178354Ssam if (vap->iv_des_nssid != 0 && 456178354Ssam !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid)) 457170530Ssam return 0; 458178354Ssam if ((vap->iv_flags & IEEE80211_F_DESBSSID) && 459178354Ssam !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid)) 460170530Ssam return 0; 461170530Ssam return 1; 462170530Ssam} 463170530Ssam 464170530Ssam#ifdef IEEE80211_DEBUG 465170530Ssam/* 466170530Ssam * Display node suitability/compatibility. 467170530Ssam */ 468170530Ssamstatic void 469178354Ssamcheck_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni) 470170530Ssam{ 471178354Ssam struct ieee80211com *ic = ni->ni_ic; 472170530Ssam uint8_t rate; 473127767Ssam int fail; 474127767Ssam 475127767Ssam fail = 0; 476127767Ssam if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 477127767Ssam fail |= 0x01; 478178354Ssam if (vap->iv_opmode == IEEE80211_M_IBSS) { 479127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 480127767Ssam fail |= 0x02; 481127767Ssam } else { 482127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 483127767Ssam fail |= 0x02; 484127767Ssam } 485178354Ssam if (vap->iv_flags & IEEE80211_F_PRIVACY) { 486127767Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 487127767Ssam fail |= 0x04; 488127767Ssam } else { 489127767Ssam /* XXX does this mean privacy is supported or required? */ 490127767Ssam if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 491127767Ssam fail |= 0x04; 492127767Ssam } 493167442Ssam rate = ieee80211_fix_rate(ni, &ni->ni_rates, 494165887Ssam IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 495127767Ssam if (rate & IEEE80211_RATE_BASIC) 496127767Ssam fail |= 0x08; 497178354Ssam if (vap->iv_des_nssid != 0 && 498178354Ssam !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid)) 499127767Ssam fail |= 0x10; 500178354Ssam if ((vap->iv_flags & IEEE80211_F_DESBSSID) && 501178354Ssam !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid)) 502127767Ssam fail |= 0x20; 503127767Ssam 504170530Ssam printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr)); 505170530Ssam printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' '); 506170530Ssam printf(" %3d%c", 507170530Ssam ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' '); 508170530Ssam printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 509170530Ssam fail & 0x08 ? '!' : ' '); 510170530Ssam printf(" %4s%c", 511170530Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 512170530Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 513170530Ssam "????", 514170530Ssam fail & 0x02 ? '!' : ' '); 515170530Ssam printf(" %3s%c ", 516170530Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no", 517170530Ssam fail & 0x04 ? '!' : ' '); 518170530Ssam ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 519170530Ssam printf("%s\n", fail & 0x10 ? "!" : ""); 520138568Ssam} 521170530Ssam#endif /* IEEE80211_DEBUG */ 522138568Ssam 523138568Ssam/* 524138568Ssam * Handle 802.11 ad hoc network merge. The 525138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance 526138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match 527138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that 528138568Ssam * has the station's desired SSID. The "oldest" 802.11 network 529138568Ssam * sends beacons with the greatest TSF timestamp. 530138568Ssam * 531138568Ssam * The caller is assumed to validate TSF's before attempting a merge. 532138568Ssam * 533138568Ssam * Return !0 if the BSSID changed, 0 otherwise. 534138568Ssam */ 535138568Ssamint 536148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni) 537138568Ssam{ 538178354Ssam struct ieee80211vap *vap = ni->ni_vap; 539178354Ssam#ifdef IEEE80211_DEBUG 540148306Ssam struct ieee80211com *ic = ni->ni_ic; 541178354Ssam#endif 542138568Ssam 543178354Ssam if (ni == vap->iv_bss || 544178354Ssam IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) { 545138568Ssam /* unchanged, nothing to do */ 546138568Ssam return 0; 547138568Ssam } 548178354Ssam if (!check_bss(vap, ni)) { 549170530Ssam /* capabilities mismatch */ 550178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC, 551138568Ssam "%s: merge failed, capabilities mismatch\n", __func__); 552170530Ssam#ifdef IEEE80211_DEBUG 553178354Ssam if (ieee80211_msg_assoc(vap)) 554178354Ssam check_bss_debug(vap, ni); 555170530Ssam#endif 556178354Ssam vap->iv_stats.is_ibss_capmismatch++; 557138568Ssam return 0; 558138568Ssam } 559178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC, 560138568Ssam "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__, 561138568Ssam ether_sprintf(ni->ni_bssid), 562138568Ssam ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 563138568Ssam ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long", 564138568Ssam ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "" 565138568Ssam ); 566170530Ssam return ieee80211_sta_join1(ieee80211_ref_node(ni)); 567138568Ssam} 568138568Ssam 569138568Ssam/* 570178354Ssam * Calculate HT channel promotion flags for all vaps. 571178354Ssam * This assumes ni_chan have been setup for each vap. 572173273Ssam */ 573178354Ssamstatic int 574178354Ssamgethtadjustflags(struct ieee80211com *ic) 575178354Ssam{ 576178354Ssam struct ieee80211vap *vap; 577178354Ssam int flags; 578178354Ssam 579178354Ssam flags = 0; 580178354Ssam /* XXX locking */ 581178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 582178354Ssam if (vap->iv_state < IEEE80211_S_RUN) 583178354Ssam continue; 584178354Ssam switch (vap->iv_opmode) { 585178354Ssam case IEEE80211_M_WDS: 586178354Ssam case IEEE80211_M_STA: 587178354Ssam case IEEE80211_M_AHDEMO: 588178354Ssam case IEEE80211_M_HOSTAP: 589178354Ssam case IEEE80211_M_IBSS: 590178354Ssam flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan); 591178354Ssam break; 592178354Ssam default: 593178354Ssam break; 594178354Ssam } 595178354Ssam } 596178354Ssam return flags; 597178354Ssam} 598178354Ssam 599178354Ssam/* 600178354Ssam * Check if the current channel needs to change based on whether 601184303Ssam * any vap's are using HT20/HT40. This is used to sync the state 602184303Ssam * of ic_curchan after a channel width change on a running vap. 603178354Ssam */ 604173273Ssamvoid 605178354Ssamieee80211_sync_curchan(struct ieee80211com *ic) 606173273Ssam{ 607178354Ssam struct ieee80211_channel *c; 608178354Ssam 609178354Ssam c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic)); 610178354Ssam if (c != ic->ic_curchan) { 611178354Ssam ic->ic_curchan = c; 612178354Ssam ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan); 613178354Ssam ic->ic_set_channel(ic); 614178354Ssam } 615178354Ssam} 616178354Ssam 617178354Ssam/* 618178354Ssam * Change the current channel. The request channel may be 619178354Ssam * promoted if other vap's are operating with HT20/HT40. 620178354Ssam */ 621178354Ssamvoid 622178354Ssamieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c) 623178354Ssam{ 624178354Ssam if (ic->ic_htcaps & IEEE80211_HTC_HT) { 625178354Ssam int flags = gethtadjustflags(ic); 626178354Ssam /* 627178354Ssam * Check for channel promotion required to support the 628178354Ssam * set of running vap's. This assumes we are called 629178354Ssam * after ni_chan is setup for each vap. 630178354Ssam */ 631178354Ssam /* NB: this assumes IEEE80211_FEXT_USEHT40 > IEEE80211_FEXT_HT */ 632178354Ssam if (flags > ieee80211_htchanflags(c)) 633178354Ssam c = ieee80211_ht_adjust_channel(ic, c, flags); 634178354Ssam } 635178354Ssam ic->ic_bsschan = ic->ic_curchan = c; 636173273Ssam ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan); 637173273Ssam ic->ic_set_channel(ic); 638173273Ssam} 639173273Ssam 640173273Ssam/* 641138568Ssam * Join the specified IBSS/BSS network. The node is assumed to 642138568Ssam * be passed in with a held reference. 643138568Ssam */ 644170530Ssamstatic int 645170530Ssamieee80211_sta_join1(struct ieee80211_node *selbs) 646138568Ssam{ 647178354Ssam struct ieee80211vap *vap = selbs->ni_vap; 648170530Ssam struct ieee80211com *ic = selbs->ni_ic; 649138568Ssam struct ieee80211_node *obss; 650170530Ssam int canreassoc; 651138568Ssam 652138568Ssam /* 653138568Ssam * Committed to selbs, setup state. 654138568Ssam */ 655178354Ssam obss = vap->iv_bss; 656170530Ssam /* 657170530Ssam * Check if old+new node have the same address in which 658170530Ssam * case we can reassociate when operating in sta mode. 659170530Ssam */ 660170530Ssam canreassoc = (obss != NULL && 661178354Ssam vap->iv_state == IEEE80211_S_RUN && 662170530Ssam IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr)); 663178354Ssam vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */ 664153352Ssam if (obss != NULL) { 665153352Ssam copy_bss(selbs, obss); 666178354Ssam ieee80211_node_reclaim(obss); 667178354Ssam obss = NULL; /* NB: guard against later use */ 668153352Ssam } 669165887Ssam 670138568Ssam /* 671165887Ssam * Delete unusable rates; we've already checked 672165887Ssam * that the negotiated rate set is acceptable. 673165887Ssam */ 674178354Ssam ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates, 675167442Ssam IEEE80211_F_DODEL | IEEE80211_F_JOIN); 676165887Ssam 677178354Ssam ieee80211_setcurchan(ic, selbs->ni_chan); 678165887Ssam /* 679138568Ssam * Set the erp state (mostly the slot time) to deal with 680138568Ssam * the auto-select case; this should be redundant if the 681138568Ssam * mode is locked. 682138568Ssam */ 683138568Ssam ieee80211_reset_erp(ic); 684178354Ssam ieee80211_wme_initparams(vap); 685140753Ssam 686178354Ssam if (vap->iv_opmode == IEEE80211_M_STA) { 687170530Ssam if (canreassoc) { 688170530Ssam /* Reassociate */ 689178354Ssam ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1); 690170530Ssam } else { 691170530Ssam /* 692170530Ssam * Act as if we received a DEAUTH frame in case we 693170530Ssam * are invoked from the RUN state. This will cause 694170530Ssam * us to try to re-authenticate if we are operating 695170530Ssam * as a station. 696170530Ssam */ 697178354Ssam ieee80211_new_state(vap, IEEE80211_S_AUTH, 698170530Ssam IEEE80211_FC0_SUBTYPE_DEAUTH); 699170530Ssam } 700170530Ssam } else 701178354Ssam ieee80211_new_state(vap, IEEE80211_S_RUN, -1); 702138568Ssam return 1; 703116742Ssam} 704116742Ssam 705170530Ssamint 706184274Ssamieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan, 707170530Ssam const struct ieee80211_scan_entry *se) 708170530Ssam{ 709178354Ssam struct ieee80211com *ic = vap->iv_ic; 710170530Ssam struct ieee80211_node *ni; 711170530Ssam 712178354Ssam ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr); 713170530Ssam if (ni == NULL) { 714170530Ssam /* XXX msg */ 715170530Ssam return 0; 716170530Ssam } 717170530Ssam /* 718170530Ssam * Expand scan state into node's format. 719170530Ssam * XXX may not need all this stuff 720170530Ssam */ 721170530Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid); 722170530Ssam ni->ni_esslen = se->se_ssid[1]; 723170530Ssam memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen); 724170530Ssam ni->ni_rstamp = se->se_rstamp; 725170530Ssam ni->ni_tstamp.tsf = se->se_tstamp.tsf; 726170530Ssam ni->ni_intval = se->se_intval; 727170530Ssam ni->ni_capinfo = se->se_capinfo; 728184274Ssam ni->ni_chan = chan; 729170530Ssam ni->ni_timoff = se->se_timoff; 730170530Ssam ni->ni_fhdwell = se->se_fhdwell; 731170530Ssam ni->ni_fhindex = se->se_fhindex; 732170530Ssam ni->ni_erp = se->se_erp; 733178354Ssam IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi); 734170530Ssam ni->ni_noise = se->se_noise; 735178354Ssam 736178354Ssam if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) { 737178354Ssam ieee80211_ies_expand(&ni->ni_ies); 738178354Ssam if (ni->ni_ies.ath_ie != NULL) 739178354Ssam ieee80211_parse_ath(ni, ni->ni_ies.ath_ie); 740178354Ssam if (ni->ni_ies.htcap_ie != NULL) 741178354Ssam ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie); 742178354Ssam if (ni->ni_ies.htinfo_ie != NULL) 743178354Ssam ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie); 744173864Ssam } 745170530Ssam 746178354Ssam vap->iv_dtim_period = se->se_dtimperiod; 747178354Ssam vap->iv_dtim_count = 0; 748170530Ssam 749170530Ssam /* NB: must be after ni_chan is setup */ 750170530Ssam ieee80211_setup_rates(ni, se->se_rates, se->se_xrates, 751170530Ssam IEEE80211_F_DOSORT); 752184279Ssam if (ieee80211_iserp_rateset(&ni->ni_rates)) 753184279Ssam ni->ni_flags |= IEEE80211_NODE_ERP; 754184279Ssam node_setuptxparms(ni); 755170530Ssam 756170530Ssam return ieee80211_sta_join1(ieee80211_ref_node(ni)); 757170530Ssam} 758170530Ssam 759138568Ssam/* 760138568Ssam * Leave the specified IBSS/BSS network. The node is assumed to 761138568Ssam * be passed in with a held reference. 762138568Ssam */ 763138568Ssamvoid 764178354Ssamieee80211_sta_leave(struct ieee80211_node *ni) 765138568Ssam{ 766178354Ssam struct ieee80211com *ic = ni->ni_ic; 767178354Ssam 768138568Ssam ic->ic_node_cleanup(ni); 769178354Ssam ieee80211_notify_node_leave(ni); 770138568Ssam} 771138568Ssam 772178354Ssam/* 773178354Ssam * Send a deauthenticate frame and drop the station. 774178354Ssam */ 775178354Ssamvoid 776178354Ssamieee80211_node_deauth(struct ieee80211_node *ni, int reason) 777178354Ssam{ 778178354Ssam /* NB: bump the refcnt to be sure temporay nodes are not reclaimed */ 779178354Ssam ieee80211_ref_node(ni); 780178354Ssam if (ni->ni_associd != 0) 781178354Ssam IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason); 782178354Ssam ieee80211_node_leave(ni); 783178354Ssam ieee80211_free_node(ni); 784178354Ssam} 785178354Ssam 786116742Ssamstatic struct ieee80211_node * 787179643Ssamnode_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN]) 788116742Ssam{ 789127768Ssam struct ieee80211_node *ni; 790138568Ssam 791184210Sdes MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node), 792127768Ssam M_80211_NODE, M_NOWAIT | M_ZERO); 793127768Ssam return ni; 794116742Ssam} 795116742Ssam 796138568Ssam/* 797178354Ssam * Initialize an ie blob with the specified data. If previous 798178354Ssam * data exists re-use the data block. As a side effect we clear 799178354Ssam * all references to specific ie's; the caller is required to 800178354Ssam * recalculate them. 801178354Ssam */ 802178354Ssamint 803178354Ssamieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len) 804178354Ssam{ 805178354Ssam /* NB: assumes data+len are the last fields */ 806178354Ssam memset(ies, 0, offsetof(struct ieee80211_ies, data)); 807178354Ssam if (ies->data != NULL && ies->len != len) { 808178354Ssam /* data size changed */ 809184210Sdes FREE(ies->data, M_80211_NODE_IE); 810178354Ssam ies->data = NULL; 811178354Ssam } 812178354Ssam if (ies->data == NULL) { 813184210Sdes MALLOC(ies->data, uint8_t *, len, M_80211_NODE_IE, M_NOWAIT); 814178354Ssam if (ies->data == NULL) { 815178354Ssam ies->len = 0; 816178354Ssam /* NB: pointers have already been zero'd above */ 817178354Ssam return 0; 818178354Ssam } 819178354Ssam } 820178354Ssam memcpy(ies->data, data, len); 821178354Ssam ies->len = len; 822178354Ssam return 1; 823178354Ssam} 824178354Ssam 825178354Ssam/* 826178354Ssam * Reclaim storage for an ie blob. 827178354Ssam */ 828178354Ssamvoid 829178354Ssamieee80211_ies_cleanup(struct ieee80211_ies *ies) 830178354Ssam{ 831178354Ssam if (ies->data != NULL) 832184210Sdes FREE(ies->data, M_80211_NODE_IE); 833178354Ssam} 834178354Ssam 835178354Ssam/* 836178354Ssam * Expand an ie blob data contents and to fillin individual 837178354Ssam * ie pointers. The data blob is assumed to be well-formed; 838178354Ssam * we don't do any validity checking of ie lengths. 839178354Ssam */ 840178354Ssamvoid 841178354Ssamieee80211_ies_expand(struct ieee80211_ies *ies) 842178354Ssam{ 843178354Ssam uint8_t *ie; 844178354Ssam int ielen; 845178354Ssam 846178354Ssam ie = ies->data; 847178354Ssam ielen = ies->len; 848178354Ssam while (ielen > 0) { 849178354Ssam switch (ie[0]) { 850178354Ssam case IEEE80211_ELEMID_VENDOR: 851178354Ssam if (iswpaoui(ie)) 852178354Ssam ies->wpa_ie = ie; 853178354Ssam else if (iswmeoui(ie)) 854178354Ssam ies->wme_ie = ie; 855178354Ssam else if (isatherosoui(ie)) 856178354Ssam ies->ath_ie = ie; 857178354Ssam break; 858178354Ssam case IEEE80211_ELEMID_RSN: 859178354Ssam ies->rsn_ie = ie; 860178354Ssam break; 861178354Ssam case IEEE80211_ELEMID_HTCAP: 862178354Ssam ies->htcap_ie = ie; 863178354Ssam break; 864178354Ssam } 865178354Ssam ielen -= 2 + ie[1]; 866178354Ssam ie += 2 + ie[1]; 867178354Ssam } 868178354Ssam} 869178354Ssam 870178354Ssam/* 871138568Ssam * Reclaim any resources in a node and reset any critical 872138568Ssam * state. Typically nodes are free'd immediately after, 873138568Ssam * but in some cases the storage may be reused so we need 874138568Ssam * to insure consistent state (should probably fix that). 875138568Ssam */ 876116742Ssamstatic void 877138568Ssamnode_cleanup(struct ieee80211_node *ni) 878116742Ssam{ 879138568Ssam#define N(a) (sizeof(a)/sizeof(a[0])) 880178354Ssam struct ieee80211vap *vap = ni->ni_vap; 881170530Ssam int i; 882138568Ssam 883138568Ssam /* NB: preserve ni_table */ 884138568Ssam if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 885178354Ssam if (vap->iv_opmode != IEEE80211_M_STA) 886178354Ssam vap->iv_ps_sta--; 887138568Ssam ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 888178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 889178354Ssam "power save mode off, %u sta's in ps mode", vap->iv_ps_sta); 890138568Ssam } 891147788Ssam /* 892173273Ssam * Cleanup any HT-related state. 893173273Ssam */ 894173273Ssam if (ni->ni_flags & IEEE80211_NODE_HT) 895173273Ssam ieee80211_ht_node_cleanup(ni); 896173273Ssam /* 897147788Ssam * Clear AREF flag that marks the authorization refcnt bump 898147788Ssam * has happened. This is probably not needed as the node 899147788Ssam * should always be removed from the table so not found but 900147788Ssam * do it just in case. 901147788Ssam */ 902147788Ssam ni->ni_flags &= ~IEEE80211_NODE_AREF; 903138568Ssam 904138568Ssam /* 905138568Ssam * Drain power save queue and, if needed, clear TIM. 906138568Ssam */ 907184288Ssam if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL) 908178354Ssam vap->iv_set_tim(ni, 0); 909138568Ssam 910138568Ssam ni->ni_associd = 0; 911138568Ssam if (ni->ni_challenge != NULL) { 912184210Sdes FREE(ni->ni_challenge, M_80211_NODE); 913138568Ssam ni->ni_challenge = NULL; 914138568Ssam } 915138568Ssam /* 916138568Ssam * Preserve SSID, WPA, and WME ie's so the bss node is 917138568Ssam * reusable during a re-auth/re-assoc state transition. 918138568Ssam * If we remove these data they will not be recreated 919138568Ssam * because they come from a probe-response or beacon frame 920138568Ssam * which cannot be expected prior to the association-response. 921138568Ssam * This should not be an issue when operating in other modes 922138568Ssam * as stations leaving always go through a full state transition 923138568Ssam * which will rebuild this state. 924138568Ssam * 925138568Ssam * XXX does this leave us open to inheriting old state? 926138568Ssam */ 927138568Ssam for (i = 0; i < N(ni->ni_rxfrag); i++) 928138568Ssam if (ni->ni_rxfrag[i] != NULL) { 929138568Ssam m_freem(ni->ni_rxfrag[i]); 930138568Ssam ni->ni_rxfrag[i] = NULL; 931138568Ssam } 932148863Ssam /* 933148863Ssam * Must be careful here to remove any key map entry w/o a LOR. 934148863Ssam */ 935148863Ssam ieee80211_node_delucastkey(ni); 936138568Ssam#undef N 937116742Ssam} 938116742Ssam 939116742Ssamstatic void 940138568Ssamnode_free(struct ieee80211_node *ni) 941116742Ssam{ 942138568Ssam struct ieee80211com *ic = ni->ni_ic; 943138568Ssam 944138568Ssam ic->ic_node_cleanup(ni); 945178354Ssam ieee80211_ies_cleanup(&ni->ni_ies); 946184288Ssam ieee80211_psq_cleanup(&ni->ni_psq); 947178354Ssam IEEE80211_NODE_WDSQ_DESTROY(ni); 948184210Sdes FREE(ni, M_80211_NODE); 949116742Ssam} 950116742Ssam 951178354Ssamstatic void 952178354Ssamnode_age(struct ieee80211_node *ni) 953178354Ssam{ 954178354Ssam struct ieee80211vap *vap = ni->ni_vap; 955184303Ssam 956184303Ssam IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta); 957184303Ssam 958178354Ssam /* 959178354Ssam * Age frames on the power save queue. 960178354Ssam */ 961184288Ssam if (ieee80211_node_psq_age(ni) != 0 && 962184288Ssam ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL) 963178354Ssam vap->iv_set_tim(ni, 0); 964178354Ssam /* 965178354Ssam * Age frames on the wds pending queue. 966178354Ssam */ 967178354Ssam if (IEEE80211_NODE_WDSQ_QLEN(ni) != 0) 968178354Ssam ieee80211_node_wdsq_age(ni); 969178354Ssam /* 970178354Ssam * Age out HT resources (e.g. frames on the 971178354Ssam * A-MPDU reorder queues). 972178354Ssam */ 973178354Ssam if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT)) 974178354Ssam ieee80211_ht_node_age(ni); 975178354Ssam} 976178354Ssam 977170530Ssamstatic int8_t 978138568Ssamnode_getrssi(const struct ieee80211_node *ni) 979120104Ssam{ 980178354Ssam uint32_t avgrssi = ni->ni_avgrssi; 981178354Ssam int32_t rssi; 982178354Ssam 983178354Ssam if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) 984178354Ssam return 0; 985178354Ssam rssi = IEEE80211_RSSI_GET(avgrssi); 986178354Ssam return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi; 987120104Ssam} 988120104Ssam 989116742Ssamstatic void 990170530Ssamnode_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise) 991170530Ssam{ 992178354Ssam *rssi = node_getrssi(ni); 993170530Ssam *noise = ni->ni_noise; 994170530Ssam} 995170530Ssam 996170530Ssamstatic void 997178354Ssamnode_getmimoinfo(const struct ieee80211_node *ni, 998178354Ssam struct ieee80211_mimo_info *info) 999116742Ssam{ 1000178354Ssam /* XXX zero data? */ 1001178354Ssam} 1002178354Ssam 1003178354Ssamstruct ieee80211_node * 1004178354Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, 1005178354Ssam struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1006178354Ssam{ 1007138568Ssam struct ieee80211com *ic = nt->nt_ic; 1008178354Ssam struct ieee80211_node *ni; 1009116742Ssam int hash; 1010116742Ssam 1011179643Ssam ni = ic->ic_node_alloc(vap, macaddr); 1012178354Ssam if (ni == NULL) { 1013178354Ssam vap->iv_stats.is_rx_nodealloc++; 1014178354Ssam return NULL; 1015178354Ssam } 1016178354Ssam 1017178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1018140766Ssam "%s %p<%s> in %s table\n", __func__, ni, 1019138568Ssam ether_sprintf(macaddr), nt->nt_name); 1020138568Ssam 1021116742Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 1022116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 1023138568Ssam ieee80211_node_initref(ni); /* mark referenced */ 1024138568Ssam ni->ni_chan = IEEE80211_CHAN_ANYC; 1025138568Ssam ni->ni_authmode = IEEE80211_AUTH_OPEN; 1026138568Ssam ni->ni_txpower = ic->ic_txpowlimit; /* max power */ 1027183251Ssam ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1028178354Ssam ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE); 1029178354Ssam ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER; 1030139528Ssam ni->ni_inact_reload = nt->nt_inact_init; 1031139528Ssam ni->ni_inact = ni->ni_inact_reload; 1032170530Ssam ni->ni_ath_defkeyix = 0x7fff; 1033184288Ssam ieee80211_psq_init(&ni->ni_psq, "unknown"); 1034178354Ssam IEEE80211_NODE_WDSQ_INIT(ni, "unknown"); 1035138568Ssam 1036138568Ssam IEEE80211_NODE_LOCK(nt); 1037138568Ssam TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list); 1038138568Ssam LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash); 1039138568Ssam ni->ni_table = nt; 1040178354Ssam ni->ni_vap = vap; 1041138568Ssam ni->ni_ic = ic; 1042138568Ssam IEEE80211_NODE_UNLOCK(nt); 1043116742Ssam 1044184277Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 1045184277Ssam "%s: inact_reload %u", __func__, ni->ni_inact_reload); 1046184277Ssam 1047116742Ssam return ni; 1048116742Ssam} 1049116742Ssam 1050148777Ssam/* 1051148777Ssam * Craft a temporary node suitable for sending a management frame 1052148777Ssam * to the specified station. We craft only as much state as we 1053148777Ssam * need to do the work since the node will be immediately reclaimed 1054148777Ssam * once the send completes. 1055148777Ssam */ 1056116742Ssamstruct ieee80211_node * 1057178354Ssamieee80211_tmp_node(struct ieee80211vap *vap, 1058178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1059148777Ssam{ 1060178354Ssam struct ieee80211com *ic = vap->iv_ic; 1061148777Ssam struct ieee80211_node *ni; 1062148777Ssam 1063179643Ssam ni = ic->ic_node_alloc(vap, macaddr); 1064148777Ssam if (ni != NULL) { 1065183259Ssam struct ieee80211_node *bss = vap->iv_bss; 1066183259Ssam 1067178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1068148777Ssam "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr)); 1069148777Ssam 1070178354Ssam ni->ni_table = NULL; /* NB: pedantic */ 1071178354Ssam ni->ni_ic = ic; /* NB: needed to set channel */ 1072178354Ssam ni->ni_vap = vap; 1073178354Ssam 1074148777Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 1075183259Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid); 1076148777Ssam ieee80211_node_initref(ni); /* mark referenced */ 1077148777Ssam /* NB: required by ieee80211_fix_rate */ 1078183259Ssam ieee80211_node_set_chan(ni, bss->ni_chan); 1079178354Ssam ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, 1080148777Ssam IEEE80211_KEYIX_NONE); 1081183259Ssam ni->ni_txpower = bss->ni_txpower; 1082148777Ssam /* XXX optimize away */ 1083184288Ssam ieee80211_psq_init(&ni->ni_psq, "unknown"); 1084178354Ssam IEEE80211_NODE_WDSQ_INIT(ni, "unknown"); 1085148777Ssam } else { 1086148777Ssam /* XXX msg */ 1087178354Ssam vap->iv_stats.is_rx_nodealloc++; 1088148777Ssam } 1089148777Ssam return ni; 1090148777Ssam} 1091148777Ssam 1092148777Ssamstruct ieee80211_node * 1093178354Ssamieee80211_dup_bss(struct ieee80211vap *vap, 1094178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1095116742Ssam{ 1096178354Ssam struct ieee80211com *ic = vap->iv_ic; 1097138568Ssam struct ieee80211_node *ni; 1098138568Ssam 1099178354Ssam ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr); 1100116742Ssam if (ni != NULL) { 1101183259Ssam struct ieee80211_node *bss = vap->iv_bss; 1102127770Ssam /* 1103178354Ssam * Inherit from iv_bss. 1104127770Ssam */ 1105183259Ssam copy_bss(ni, bss); 1106183259Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid); 1107183259Ssam ieee80211_node_set_chan(ni, bss->ni_chan); 1108178354Ssam } 1109116742Ssam return ni; 1110116742Ssam} 1111116742Ssam 1112178354Ssam/* 1113178354Ssam * Create a bss node for a legacy WDS vap. The far end does 1114178354Ssam * not associate so we just create create a new node and 1115178354Ssam * simulate an association. The caller is responsible for 1116178354Ssam * installing the node as the bss node and handling any further 1117178354Ssam * setup work like authorizing the port. 1118178354Ssam */ 1119178354Ssamstruct ieee80211_node * 1120178354Ssamieee80211_node_create_wds(struct ieee80211vap *vap, 1121178354Ssam const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan) 1122178354Ssam{ 1123178354Ssam struct ieee80211com *ic = vap->iv_ic; 1124178354Ssam struct ieee80211_node *ni; 1125178354Ssam 1126178354Ssam /* XXX check if node already in sta table? */ 1127178354Ssam ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid); 1128178354Ssam if (ni != NULL) { 1129178354Ssam ni->ni_wdsvap = vap; 1130178354Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, bssid); 1131178354Ssam /* 1132178354Ssam * Inherit any manually configured settings. 1133178354Ssam */ 1134183259Ssam copy_bss(ni, vap->iv_bss); 1135178354Ssam ieee80211_node_set_chan(ni, chan); 1136178354Ssam /* NB: propagate ssid so available to WPA supplicant */ 1137178354Ssam ni->ni_esslen = vap->iv_des_ssid[0].len; 1138178354Ssam memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen); 1139178354Ssam /* NB: no associd for peer */ 1140178354Ssam /* 1141178354Ssam * There are no management frames to use to 1142178354Ssam * discover neighbor capabilities, so blindly 1143178354Ssam * propagate the local configuration. 1144178354Ssam */ 1145178354Ssam if (vap->iv_flags & IEEE80211_F_WME) 1146178354Ssam ni->ni_flags |= IEEE80211_NODE_QOS; 1147178354Ssam if (vap->iv_flags & IEEE80211_F_FF) 1148178354Ssam ni->ni_flags |= IEEE80211_NODE_FF; 1149178354Ssam if ((ic->ic_htcaps & IEEE80211_HTC_HT) && 1150178354Ssam (vap->iv_flags_ext & IEEE80211_FEXT_HT)) { 1151178354Ssam /* 1152178354Ssam * Device is HT-capable and HT is enabled for 1153178354Ssam * the vap; setup HT operation. On return 1154178354Ssam * ni_chan will be adjusted to an HT channel. 1155178354Ssam */ 1156178354Ssam ieee80211_ht_wds_init(ni); 1157178354Ssam } else { 1158178354Ssam struct ieee80211_channel *c = ni->ni_chan; 1159178354Ssam /* 1160178354Ssam * Force a legacy channel to be used. 1161178354Ssam */ 1162178354Ssam c = ieee80211_find_channel(ic, 1163178354Ssam c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT); 1164178354Ssam KASSERT(c != NULL, ("no legacy channel, %u/%x", 1165178354Ssam ni->ni_chan->ic_freq, ni->ni_chan->ic_flags)); 1166178354Ssam ni->ni_chan = c; 1167178354Ssam } 1168178354Ssam } 1169178354Ssam return ni; 1170178354Ssam} 1171178354Ssam 1172178354Ssamstruct ieee80211_node * 1173138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1174178354Ssamieee80211_find_node_locked_debug(struct ieee80211_node_table *nt, 1175178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1176138568Ssam#else 1177178354Ssamieee80211_find_node_locked(struct ieee80211_node_table *nt, 1178178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1179138568Ssam#endif 1180116742Ssam{ 1181116742Ssam struct ieee80211_node *ni; 1182116742Ssam int hash; 1183116742Ssam 1184138568Ssam IEEE80211_NODE_LOCK_ASSERT(nt); 1185127772Ssam 1186116742Ssam hash = IEEE80211_NODE_HASH(macaddr); 1187138568Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1188116742Ssam if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 1189138568Ssam ieee80211_ref_node(ni); /* mark referenced */ 1190138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1191178354Ssam IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1192140766Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1193140766Ssam func, line, 1194140766Ssam ni, ether_sprintf(ni->ni_macaddr), 1195140766Ssam ieee80211_node_refcnt(ni)); 1196138568Ssam#endif 1197127772Ssam return ni; 1198116742Ssam } 1199116742Ssam } 1200127772Ssam return NULL; 1201127772Ssam} 1202178354Ssam 1203178354Ssamstruct ieee80211_node * 1204138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1205178354Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt, 1206178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1207178354Ssam#else 1208178354Ssamieee80211_find_node(struct ieee80211_node_table *nt, 1209178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1210138568Ssam#endif 1211178354Ssam{ 1212178354Ssam struct ieee80211_node *ni; 1213127772Ssam 1214178354Ssam IEEE80211_NODE_LOCK(nt); 1215178354Ssam ni = ieee80211_find_node_locked(nt, macaddr); 1216178354Ssam IEEE80211_NODE_UNLOCK(nt); 1217178354Ssam return ni; 1218178354Ssam} 1219178354Ssam 1220127772Ssamstruct ieee80211_node * 1221138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1222178354Ssamieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt, 1223178354Ssam const struct ieee80211vap *vap, 1224178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1225138568Ssam#else 1226178354Ssamieee80211_find_vap_node_locked(struct ieee80211_node_table *nt, 1227178354Ssam const struct ieee80211vap *vap, 1228178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1229138568Ssam#endif 1230127772Ssam{ 1231127772Ssam struct ieee80211_node *ni; 1232178354Ssam int hash; 1233127772Ssam 1234178354Ssam IEEE80211_NODE_LOCK_ASSERT(nt); 1235178354Ssam 1236178354Ssam hash = IEEE80211_NODE_HASH(macaddr); 1237178354Ssam LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1238178354Ssam if (ni->ni_vap == vap && 1239178354Ssam IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 1240178354Ssam ieee80211_ref_node(ni); /* mark referenced */ 1241178354Ssam#ifdef IEEE80211_DEBUG_REFCNT 1242178354Ssam IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1243178354Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1244178354Ssam func, line, 1245178354Ssam ni, ether_sprintf(ni->ni_macaddr), 1246178354Ssam ieee80211_node_refcnt(ni)); 1247178354Ssam#endif 1248178354Ssam return ni; 1249178354Ssam } 1250178354Ssam } 1251178354Ssam return NULL; 1252178354Ssam} 1253178354Ssam 1254178354Ssamstruct ieee80211_node * 1255178354Ssam#ifdef IEEE80211_DEBUG_REFCNT 1256178354Ssamieee80211_find_vap_node_debug(struct ieee80211_node_table *nt, 1257178354Ssam const struct ieee80211vap *vap, 1258178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1259178354Ssam#else 1260178354Ssamieee80211_find_vap_node(struct ieee80211_node_table *nt, 1261178354Ssam const struct ieee80211vap *vap, 1262178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1263178354Ssam#endif 1264178354Ssam{ 1265178354Ssam struct ieee80211_node *ni; 1266178354Ssam 1267138568Ssam IEEE80211_NODE_LOCK(nt); 1268178354Ssam ni = ieee80211_find_vap_node_locked(nt, vap, macaddr); 1269138568Ssam IEEE80211_NODE_UNLOCK(nt); 1270116742Ssam return ni; 1271116742Ssam} 1272116742Ssam 1273116742Ssam/* 1274138568Ssam * Fake up a node; this handles node discovery in adhoc mode. 1275138568Ssam * Note that for the driver's benefit we we treat this like 1276138568Ssam * an association so the driver has an opportunity to setup 1277138568Ssam * it's private state. 1278138568Ssam */ 1279138568Ssamstruct ieee80211_node * 1280178354Ssamieee80211_fakeup_adhoc_node(struct ieee80211vap *vap, 1281170530Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1282138568Ssam{ 1283138568Ssam struct ieee80211_node *ni; 1284138568Ssam 1285178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1286153073Ssam "%s: mac<%s>\n", __func__, ether_sprintf(macaddr)); 1287178354Ssam ni = ieee80211_dup_bss(vap, macaddr); 1288138568Ssam if (ni != NULL) { 1289178354Ssam struct ieee80211com *ic = vap->iv_ic; 1290178354Ssam 1291138568Ssam /* XXX no rate negotiation; just dup */ 1292178354Ssam ni->ni_rates = vap->iv_bss->ni_rates; 1293178354Ssam if (vap->iv_opmode == IEEE80211_M_AHDEMO) { 1294153404Ssam /* 1295170530Ssam * In adhoc demo mode there are no management 1296170530Ssam * frames to use to discover neighbor capabilities, 1297170530Ssam * so blindly propagate the local configuration 1298170530Ssam * so we can do interesting things (e.g. use 1299170530Ssam * WME to disable ACK's). 1300153404Ssam */ 1301178354Ssam if (vap->iv_flags & IEEE80211_F_WME) 1302153404Ssam ni->ni_flags |= IEEE80211_NODE_QOS; 1303178354Ssam if (vap->iv_flags & IEEE80211_F_FF) 1304170530Ssam ni->ni_flags |= IEEE80211_NODE_FF; 1305153404Ssam } 1306183251Ssam node_setuptxparms(ni); 1307178354Ssam if (ic->ic_newassoc != NULL) 1308178354Ssam ic->ic_newassoc(ni, 1); 1309170530Ssam /* XXX not right for 802.1x/WPA */ 1310170530Ssam ieee80211_node_authorize(ni); 1311138568Ssam } 1312138568Ssam return ni; 1313138568Ssam} 1314138568Ssam 1315148936Ssamvoid 1316153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni, 1317153073Ssam const struct ieee80211_frame *wh, 1318153073Ssam const struct ieee80211_scanparams *sp) 1319153073Ssam{ 1320153073Ssam ni->ni_esslen = sp->ssid[1]; 1321153073Ssam memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]); 1322153073Ssam IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3); 1323153073Ssam memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp)); 1324153073Ssam ni->ni_intval = sp->bintval; 1325153073Ssam ni->ni_capinfo = sp->capinfo; 1326153073Ssam ni->ni_chan = ni->ni_ic->ic_curchan; 1327153073Ssam ni->ni_fhdwell = sp->fhdwell; 1328153073Ssam ni->ni_fhindex = sp->fhindex; 1329153073Ssam ni->ni_erp = sp->erp; 1330153073Ssam ni->ni_timoff = sp->timoff; 1331153073Ssam 1332178354Ssam if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) { 1333178354Ssam ieee80211_ies_expand(&ni->ni_ies); 1334178354Ssam if (ni->ni_ies.ath_ie != NULL) 1335178354Ssam ieee80211_parse_ath(ni, ni->ni_ies.ath_ie); 1336178354Ssam } 1337178354Ssam 1338153073Ssam /* NB: must be after ni_chan is setup */ 1339165887Ssam ieee80211_setup_rates(ni, sp->rates, sp->xrates, 1340165887Ssam IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | 1341165887Ssam IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1342153073Ssam} 1343153073Ssam 1344148936Ssam/* 1345148936Ssam * Do node discovery in adhoc mode on receipt of a beacon 1346148936Ssam * or probe response frame. Note that for the driver's 1347148936Ssam * benefit we we treat this like an association so the 1348148936Ssam * driver has an opportunity to setup it's private state. 1349148936Ssam */ 1350148936Ssamstruct ieee80211_node * 1351178354Ssamieee80211_add_neighbor(struct ieee80211vap *vap, 1352148936Ssam const struct ieee80211_frame *wh, 1353148936Ssam const struct ieee80211_scanparams *sp) 1354148936Ssam{ 1355148936Ssam struct ieee80211_node *ni; 1356148936Ssam 1357178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1358153073Ssam "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2)); 1359178354Ssam ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */ 1360148936Ssam if (ni != NULL) { 1361178354Ssam struct ieee80211com *ic = vap->iv_ic; 1362178354Ssam 1363153073Ssam ieee80211_init_neighbor(ni, wh, sp); 1364183251Ssam node_setuptxparms(ni); 1365148936Ssam if (ic->ic_newassoc != NULL) 1366148936Ssam ic->ic_newassoc(ni, 1); 1367148936Ssam /* XXX not right for 802.1x/WPA */ 1368148936Ssam ieee80211_node_authorize(ni); 1369148936Ssam } 1370148936Ssam return ni; 1371148936Ssam} 1372148936Ssam 1373148863Ssam#define IS_CTL(wh) \ 1374148863Ssam ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) 1375148863Ssam#define IS_PSPOLL(wh) \ 1376148863Ssam ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL) 1377170530Ssam#define IS_BAR(wh) \ 1378170530Ssam ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR) 1379179220Ssam#define IS_PROBEREQ(wh) \ 1380179220Ssam ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \ 1381179220Ssam == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ)) 1382179220Ssam#define IS_BCAST_PROBEREQ(wh) \ 1383179220Ssam (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \ 1384179220Ssam ((const struct ieee80211_frame *)(wh))->i_addr3)) 1385170530Ssam 1386179220Ssamstatic __inline struct ieee80211_node * 1387179220Ssam_find_rxnode(struct ieee80211_node_table *nt, 1388179220Ssam const struct ieee80211_frame_min *wh) 1389179220Ssam{ 1390179220Ssam /* XXX 4-address frames? */ 1391179220Ssam if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/) 1392179220Ssam return ieee80211_find_node_locked(nt, wh->i_addr1); 1393179220Ssam if (IS_BCAST_PROBEREQ(wh)) 1394179220Ssam return NULL; /* spam bcast probe req to all vap's */ 1395179220Ssam return ieee80211_find_node_locked(nt, wh->i_addr2); 1396179220Ssam} 1397179220Ssam 1398138568Ssam/* 1399138568Ssam * Locate the node for sender, track state, and then pass the 1400179220Ssam * (referenced) node up to the 802.11 layer for its use. Note 1401179220Ssam * we can return NULL if the sender is not in the table. 1402138568Ssam */ 1403138568Ssamstruct ieee80211_node * 1404138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1405138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic, 1406138568Ssam const struct ieee80211_frame_min *wh, const char *func, int line) 1407138568Ssam#else 1408138568Ssamieee80211_find_rxnode(struct ieee80211com *ic, 1409138568Ssam const struct ieee80211_frame_min *wh) 1410138568Ssam#endif 1411138568Ssam{ 1412138568Ssam struct ieee80211_node_table *nt; 1413138568Ssam struct ieee80211_node *ni; 1414138568Ssam 1415170530Ssam nt = &ic->ic_sta; 1416138568Ssam IEEE80211_NODE_LOCK(nt); 1417179220Ssam ni = _find_rxnode(nt, wh); 1418138568Ssam IEEE80211_NODE_UNLOCK(nt); 1419138568Ssam 1420148863Ssam return ni; 1421148863Ssam} 1422148863Ssam 1423148863Ssam/* 1424148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w 1425148863Ssam * key index as a hint to locate the node in the key 1426148863Ssam * mapping table. If an entry is present at the key 1427148863Ssam * index we return it; otherwise do a normal lookup and 1428148863Ssam * update the mapping table if the station has a unicast 1429148863Ssam * key assigned to it. 1430148863Ssam */ 1431148863Ssamstruct ieee80211_node * 1432148863Ssam#ifdef IEEE80211_DEBUG_REFCNT 1433148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic, 1434148863Ssam const struct ieee80211_frame_min *wh, ieee80211_keyix keyix, 1435148863Ssam const char *func, int line) 1436148863Ssam#else 1437148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic, 1438148863Ssam const struct ieee80211_frame_min *wh, ieee80211_keyix keyix) 1439148863Ssam#endif 1440148863Ssam{ 1441148863Ssam struct ieee80211_node_table *nt; 1442148863Ssam struct ieee80211_node *ni; 1443148863Ssam 1444170530Ssam nt = &ic->ic_sta; 1445148863Ssam IEEE80211_NODE_LOCK(nt); 1446148863Ssam if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) 1447148863Ssam ni = nt->nt_keyixmap[keyix]; 1448148863Ssam else 1449148863Ssam ni = NULL; 1450148863Ssam if (ni == NULL) { 1451179220Ssam ni = _find_rxnode(nt, wh); 1452178354Ssam if (ni != NULL && nt->nt_keyixmap != NULL) { 1453148863Ssam /* 1454148863Ssam * If the station has a unicast key cache slot 1455148863Ssam * assigned update the key->node mapping table. 1456148863Ssam */ 1457148863Ssam keyix = ni->ni_ucastkey.wk_rxkeyix; 1458148863Ssam /* XXX can keyixmap[keyix] != NULL? */ 1459148863Ssam if (keyix < nt->nt_keyixmax && 1460148863Ssam nt->nt_keyixmap[keyix] == NULL) { 1461178354Ssam IEEE80211_DPRINTF(ni->ni_vap, 1462178354Ssam IEEE80211_MSG_NODE, 1463148863Ssam "%s: add key map entry %p<%s> refcnt %d\n", 1464148863Ssam __func__, ni, ether_sprintf(ni->ni_macaddr), 1465148863Ssam ieee80211_node_refcnt(ni)+1); 1466148863Ssam nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni); 1467148863Ssam } 1468148863Ssam } 1469179220Ssam } else { 1470179220Ssam if (IS_BCAST_PROBEREQ(wh)) 1471179220Ssam ni = NULL; /* spam bcast probe req to all vap's */ 1472179220Ssam else 1473179220Ssam ieee80211_ref_node(ni); 1474179220Ssam } 1475148863Ssam IEEE80211_NODE_UNLOCK(nt); 1476148863Ssam 1477148863Ssam return ni; 1478148863Ssam} 1479179220Ssam#undef IS_BCAST_PROBEREQ 1480179220Ssam#undef IS_PROBEREQ 1481170530Ssam#undef IS_BAR 1482138568Ssam#undef IS_PSPOLL 1483138568Ssam#undef IS_CTL 1484138568Ssam 1485138568Ssam/* 1486127772Ssam * Return a reference to the appropriate node for sending 1487127772Ssam * a data frame. This handles node discovery in adhoc networks. 1488127772Ssam */ 1489127772Ssamstruct ieee80211_node * 1490138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1491178354Ssamieee80211_find_txnode_debug(struct ieee80211vap *vap, 1492178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN], 1493138568Ssam const char *func, int line) 1494138568Ssam#else 1495178354Ssamieee80211_find_txnode(struct ieee80211vap *vap, 1496178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1497138568Ssam#endif 1498127772Ssam{ 1499178354Ssam struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta; 1500127772Ssam struct ieee80211_node *ni; 1501127772Ssam 1502127772Ssam /* 1503127772Ssam * The destination address should be in the node table 1504148863Ssam * unless this is a multicast/broadcast frame. We can 1505148863Ssam * also optimize station mode operation, all frames go 1506148863Ssam * to the bss node. 1507127772Ssam */ 1508127772Ssam /* XXX can't hold lock across dup_bss 'cuz of recursive locking */ 1509138568Ssam IEEE80211_NODE_LOCK(nt); 1510178354Ssam if (vap->iv_opmode == IEEE80211_M_STA || 1511178354Ssam vap->iv_opmode == IEEE80211_M_WDS || 1512178354Ssam IEEE80211_IS_MULTICAST(macaddr)) 1513178354Ssam ni = ieee80211_ref_node(vap->iv_bss); 1514158121Ssam else { 1515178354Ssam ni = ieee80211_find_node_locked(nt, macaddr); 1516178354Ssam if (vap->iv_opmode == IEEE80211_M_HOSTAP && 1517158121Ssam (ni != NULL && ni->ni_associd == 0)) { 1518158121Ssam /* 1519158121Ssam * Station is not associated; don't permit the 1520158121Ssam * data frame to be sent by returning NULL. This 1521158121Ssam * is kinda a kludge but the least intrusive way 1522158121Ssam * to add this check into all drivers. 1523158121Ssam */ 1524158121Ssam ieee80211_unref_node(&ni); /* NB: null's ni */ 1525158121Ssam } 1526158121Ssam } 1527138568Ssam IEEE80211_NODE_UNLOCK(nt); 1528138568Ssam 1529138568Ssam if (ni == NULL) { 1530178354Ssam if (vap->iv_opmode == IEEE80211_M_IBSS || 1531178354Ssam vap->iv_opmode == IEEE80211_M_AHDEMO) { 1532140497Ssam /* 1533140497Ssam * In adhoc mode cons up a node for the destination. 1534140497Ssam * Note that we need an additional reference for the 1535178354Ssam * caller to be consistent with 1536178354Ssam * ieee80211_find_node_locked. 1537140497Ssam */ 1538178354Ssam ni = ieee80211_fakeup_adhoc_node(vap, macaddr); 1539140497Ssam if (ni != NULL) 1540140497Ssam (void) ieee80211_ref_node(ni); 1541140497Ssam } else { 1542178354Ssam IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr, 1543178354Ssam "no node, discard frame (%s)", __func__); 1544178354Ssam vap->iv_stats.is_tx_nonode++; 1545127772Ssam } 1546127772Ssam } 1547127772Ssam return ni; 1548127772Ssam} 1549127772Ssam 1550116742Ssamstatic void 1551138568Ssam_ieee80211_free_node(struct ieee80211_node *ni) 1552116742Ssam{ 1553138568Ssam struct ieee80211_node_table *nt = ni->ni_table; 1554119150Ssam 1555179641Ssam /* 1556179641Ssam * NB: careful about referencing the vap as it may be 1557179641Ssam * gone if the last reference was held by a driver. 1558179641Ssam * We know the com will always be present so it's safe 1559179641Ssam * to use ni_ic below to reclaim resources. 1560179641Ssam */ 1561179641Ssam#if 0 1562178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1563140766Ssam "%s %p<%s> in %s table\n", __func__, ni, 1564140766Ssam ether_sprintf(ni->ni_macaddr), 1565138568Ssam nt != NULL ? nt->nt_name : "<gone>"); 1566179641Ssam#endif 1567179641Ssam if (ni->ni_associd != 0) { 1568179641Ssam struct ieee80211vap *vap = ni->ni_vap; 1569179641Ssam if (vap->iv_aid_bitmap != NULL) 1570179641Ssam IEEE80211_AID_CLR(vap, ni->ni_associd); 1571179641Ssam } 1572138568Ssam if (nt != NULL) { 1573138568Ssam TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1574138568Ssam LIST_REMOVE(ni, ni_hash); 1575138568Ssam } 1576179641Ssam ni->ni_ic->ic_node_free(ni); 1577116742Ssam} 1578116742Ssam 1579116742Ssamvoid 1580138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1581138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line) 1582138568Ssam#else 1583138568Ssamieee80211_free_node(struct ieee80211_node *ni) 1584138568Ssam#endif 1585116742Ssam{ 1586138568Ssam struct ieee80211_node_table *nt = ni->ni_table; 1587119150Ssam 1588138568Ssam#ifdef IEEE80211_DEBUG_REFCNT 1589178354Ssam IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1590140766Ssam "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 1591138568Ssam ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1592138568Ssam#endif 1593148863Ssam if (nt != NULL) { 1594148863Ssam IEEE80211_NODE_LOCK(nt); 1595148863Ssam if (ieee80211_node_dectestref(ni)) { 1596148863Ssam /* 1597148863Ssam * Last reference, reclaim state. 1598148863Ssam */ 1599138568Ssam _ieee80211_free_node(ni); 1600148863Ssam } else if (ieee80211_node_refcnt(ni) == 1 && 1601148863Ssam nt->nt_keyixmap != NULL) { 1602148863Ssam ieee80211_keyix keyix; 1603148863Ssam /* 1604148863Ssam * Check for a last reference in the key mapping table. 1605148863Ssam */ 1606148863Ssam keyix = ni->ni_ucastkey.wk_rxkeyix; 1607148863Ssam if (keyix < nt->nt_keyixmax && 1608148863Ssam nt->nt_keyixmap[keyix] == ni) { 1609178354Ssam IEEE80211_DPRINTF(ni->ni_vap, 1610178354Ssam IEEE80211_MSG_NODE, 1611148863Ssam "%s: %p<%s> clear key map entry", __func__, 1612148863Ssam ni, ether_sprintf(ni->ni_macaddr)); 1613148863Ssam nt->nt_keyixmap[keyix] = NULL; 1614148863Ssam ieee80211_node_decref(ni); /* XXX needed? */ 1615148863Ssam _ieee80211_free_node(ni); 1616148863Ssam } 1617148863Ssam } 1618148863Ssam IEEE80211_NODE_UNLOCK(nt); 1619148863Ssam } else { 1620148863Ssam if (ieee80211_node_dectestref(ni)) 1621138568Ssam _ieee80211_free_node(ni); 1622116742Ssam } 1623116742Ssam} 1624116742Ssam 1625138568Ssam/* 1626148863Ssam * Reclaim a unicast key and clear any key cache state. 1627148863Ssam */ 1628148863Ssamint 1629148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni) 1630148863Ssam{ 1631179641Ssam struct ieee80211com *ic = ni->ni_ic; 1632179641Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 1633148863Ssam struct ieee80211_node *nikey; 1634148863Ssam ieee80211_keyix keyix; 1635148863Ssam int isowned, status; 1636148863Ssam 1637148863Ssam /* 1638148863Ssam * NB: We must beware of LOR here; deleting the key 1639148863Ssam * can cause the crypto layer to block traffic updates 1640148863Ssam * which can generate a LOR against the node table lock; 1641148863Ssam * grab it here and stash the key index for our use below. 1642148863Ssam * 1643148863Ssam * Must also beware of recursion on the node table lock. 1644148863Ssam * When called from node_cleanup we may already have 1645148863Ssam * the node table lock held. Unfortunately there's no 1646148863Ssam * way to separate out this path so we must do this 1647148863Ssam * conditionally. 1648148863Ssam */ 1649148863Ssam isowned = IEEE80211_NODE_IS_LOCKED(nt); 1650148863Ssam if (!isowned) 1651148863Ssam IEEE80211_NODE_LOCK(nt); 1652179641Ssam nikey = NULL; 1653179641Ssam status = 1; /* NB: success */ 1654179641Ssam if (!IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) { 1655179641Ssam keyix = ni->ni_ucastkey.wk_rxkeyix; 1656179641Ssam status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey); 1657179641Ssam if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) { 1658179641Ssam nikey = nt->nt_keyixmap[keyix]; 1659179641Ssam nt->nt_keyixmap[keyix] = NULL;; 1660179641Ssam } 1661179641Ssam } 1662148863Ssam if (!isowned) 1663178354Ssam IEEE80211_NODE_UNLOCK(nt); 1664148863Ssam 1665148863Ssam if (nikey != NULL) { 1666148863Ssam KASSERT(nikey == ni, 1667148863Ssam ("key map out of sync, ni %p nikey %p", ni, nikey)); 1668179641Ssam IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1669148863Ssam "%s: delete key map entry %p<%s> refcnt %d\n", 1670148863Ssam __func__, ni, ether_sprintf(ni->ni_macaddr), 1671148863Ssam ieee80211_node_refcnt(ni)-1); 1672148863Ssam ieee80211_free_node(ni); 1673148863Ssam } 1674148863Ssam return status; 1675148863Ssam} 1676148863Ssam 1677148863Ssam/* 1678138568Ssam * Reclaim a node. If this is the last reference count then 1679138568Ssam * do the normal free work. Otherwise remove it from the node 1680138568Ssam * table and mark it gone by clearing the back-reference. 1681138568Ssam */ 1682138568Ssamstatic void 1683138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1684116742Ssam{ 1685148863Ssam ieee80211_keyix keyix; 1686138568Ssam 1687148863Ssam IEEE80211_NODE_LOCK_ASSERT(nt); 1688148863Ssam 1689178354Ssam IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1690140766Ssam "%s: remove %p<%s> from %s table, refcnt %d\n", 1691140766Ssam __func__, ni, ether_sprintf(ni->ni_macaddr), 1692140766Ssam nt->nt_name, ieee80211_node_refcnt(ni)-1); 1693148863Ssam /* 1694148863Ssam * Clear any entry in the unicast key mapping table. 1695148863Ssam * We need to do it here so rx lookups don't find it 1696148863Ssam * in the mapping table even if it's not in the hash 1697148863Ssam * table. We cannot depend on the mapping table entry 1698148863Ssam * being cleared because the node may not be free'd. 1699148863Ssam */ 1700148863Ssam keyix = ni->ni_ucastkey.wk_rxkeyix; 1701148863Ssam if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax && 1702148863Ssam nt->nt_keyixmap[keyix] == ni) { 1703178354Ssam IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1704148863Ssam "%s: %p<%s> clear key map entry\n", 1705148863Ssam __func__, ni, ether_sprintf(ni->ni_macaddr)); 1706148863Ssam nt->nt_keyixmap[keyix] = NULL; 1707148863Ssam ieee80211_node_decref(ni); /* NB: don't need free */ 1708148863Ssam } 1709138568Ssam if (!ieee80211_node_dectestref(ni)) { 1710138568Ssam /* 1711138568Ssam * Other references are present, just remove the 1712138568Ssam * node from the table so it cannot be found. When 1713138568Ssam * the references are dropped storage will be 1714140753Ssam * reclaimed. 1715138568Ssam */ 1716138568Ssam TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1717138568Ssam LIST_REMOVE(ni, ni_hash); 1718138568Ssam ni->ni_table = NULL; /* clear reference */ 1719138568Ssam } else 1720138568Ssam _ieee80211_free_node(ni); 1721138568Ssam} 1722138568Ssam 1723178354Ssam/* 1724178354Ssam * Reclaim a (bss) node. Decrement the refcnt and reclaim 1725178354Ssam * the node if the only other reference to it is in the sta 1726178354Ssam * table. This is effectively ieee80211_free_node followed 1727178354Ssam * by node_reclaim when the refcnt is 1 (after the free). 1728178354Ssam */ 1729138568Ssamstatic void 1730178354Ssamieee80211_node_reclaim(struct ieee80211_node *ni) 1731138568Ssam{ 1732178354Ssam struct ieee80211_node_table *nt = ni->ni_table; 1733116742Ssam 1734178354Ssam KASSERT(nt != NULL, ("reclaim node not in table")); 1735138568Ssam 1736178354Ssam#ifdef IEEE80211_DEBUG_REFCNT 1737178354Ssam IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1738178712Ssam "%s %p<%s> refcnt %d\n", __func__, ni, 1739178354Ssam ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1740178354Ssam#endif 1741178354Ssam IEEE80211_NODE_LOCK(nt); 1742178354Ssam if (ieee80211_node_dectestref(ni)) { 1743178354Ssam /* 1744178354Ssam * Last reference, reclaim state. 1745178354Ssam */ 1746178354Ssam _ieee80211_free_node(ni); 1747178354Ssam nt = NULL; 1748178354Ssam } else if (ieee80211_node_refcnt(ni) == 1 && 1749178354Ssam nt->nt_keyixmap != NULL) { 1750178354Ssam ieee80211_keyix keyix; 1751178354Ssam /* 1752178354Ssam * Check for a last reference in the key mapping table. 1753178354Ssam */ 1754178354Ssam keyix = ni->ni_ucastkey.wk_rxkeyix; 1755178354Ssam if (keyix < nt->nt_keyixmax && 1756178354Ssam nt->nt_keyixmap[keyix] == ni) { 1757178354Ssam IEEE80211_DPRINTF(ni->ni_vap, 1758178354Ssam IEEE80211_MSG_NODE, 1759178354Ssam "%s: %p<%s> clear key map entry", __func__, 1760178354Ssam ni, ether_sprintf(ni->ni_macaddr)); 1761178354Ssam nt->nt_keyixmap[keyix] = NULL; 1762178354Ssam ieee80211_node_decref(ni); /* XXX needed? */ 1763178354Ssam _ieee80211_free_node(ni); 1764178354Ssam nt = NULL; 1765178354Ssam } 1766178354Ssam } 1767178354Ssam if (nt != NULL && ieee80211_node_refcnt(ni) == 1) { 1768178354Ssam /* 1769178354Ssam * Last reference is in the sta table; complete 1770178354Ssam * the reclaim. This handles bss nodes being 1771178354Ssam * recycled: the node has two references, one for 1772178354Ssam * iv_bss and one for the table. After dropping 1773178354Ssam * the iv_bss ref above we need to reclaim the sta 1774178354Ssam * table reference. 1775178354Ssam */ 1776178354Ssam ieee80211_node_decref(ni); /* NB: be pendantic */ 1777178354Ssam _ieee80211_free_node(ni); 1778178354Ssam } 1779178354Ssam IEEE80211_NODE_UNLOCK(nt); 1780178354Ssam} 1781178354Ssam 1782178354Ssam/* 1783178354Ssam * Node table support. 1784178354Ssam */ 1785178354Ssam 1786178354Ssamstatic void 1787178354Ssamieee80211_node_table_init(struct ieee80211com *ic, 1788178354Ssam struct ieee80211_node_table *nt, 1789178354Ssam const char *name, int inact, int keyixmax) 1790178354Ssam{ 1791178354Ssam struct ifnet *ifp = ic->ic_ifp; 1792178354Ssam 1793178354Ssam nt->nt_ic = ic; 1794178354Ssam IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname); 1795178354Ssam IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname); 1796178354Ssam TAILQ_INIT(&nt->nt_node); 1797178354Ssam nt->nt_name = name; 1798178354Ssam nt->nt_scangen = 1; 1799178354Ssam nt->nt_inact_init = inact; 1800178354Ssam nt->nt_keyixmax = keyixmax; 1801178354Ssam if (nt->nt_keyixmax > 0) { 1802184210Sdes MALLOC(nt->nt_keyixmap, struct ieee80211_node **, 1803184210Sdes keyixmax * sizeof(struct ieee80211_node *), 1804178354Ssam M_80211_NODE, M_NOWAIT | M_ZERO); 1805178354Ssam if (nt->nt_keyixmap == NULL) 1806178354Ssam if_printf(ic->ic_ifp, 1807178354Ssam "Cannot allocate key index map with %u entries\n", 1808178354Ssam keyixmax); 1809178354Ssam } else 1810178354Ssam nt->nt_keyixmap = NULL; 1811178354Ssam} 1812178354Ssam 1813178354Ssamstatic void 1814178354Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt, 1815178354Ssam struct ieee80211vap *match) 1816178354Ssam{ 1817178354Ssam struct ieee80211_node *ni, *next; 1818178354Ssam 1819178354Ssam IEEE80211_NODE_LOCK(nt); 1820178354Ssam TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) { 1821178354Ssam if (match != NULL && ni->ni_vap != match) 1822178354Ssam continue; 1823178354Ssam /* XXX can this happen? if so need's work */ 1824138568Ssam if (ni->ni_associd != 0) { 1825178354Ssam struct ieee80211vap *vap = ni->ni_vap; 1826178354Ssam 1827178354Ssam if (vap->iv_auth->ia_node_leave != NULL) 1828178354Ssam vap->iv_auth->ia_node_leave(ni); 1829178354Ssam if (vap->iv_aid_bitmap != NULL) 1830178354Ssam IEEE80211_AID_CLR(vap, ni->ni_associd); 1831138568Ssam } 1832178354Ssam ni->ni_wdsvap = NULL; /* clear reference */ 1833138568Ssam node_reclaim(nt, ni); 1834138568Ssam } 1835178354Ssam if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) { 1836178354Ssam /* 1837178354Ssam * Make a separate pass to clear references to this vap 1838178354Ssam * held by DWDS entries. They will not be matched above 1839178354Ssam * because ni_vap will point to the ap vap but we still 1840178354Ssam * need to clear ni_wdsvap when the WDS vap is destroyed 1841178354Ssam * and/or reset. 1842178354Ssam */ 1843178354Ssam TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) 1844178354Ssam if (ni->ni_wdsvap == match) 1845178354Ssam ni->ni_wdsvap = NULL; 1846178354Ssam } 1847178354Ssam IEEE80211_NODE_UNLOCK(nt); 1848116742Ssam} 1849116742Ssam 1850178354Ssamstatic void 1851178354Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt) 1852178354Ssam{ 1853178354Ssam ieee80211_node_table_reset(nt, NULL); 1854178354Ssam if (nt->nt_keyixmap != NULL) { 1855178354Ssam#ifdef DIAGNOSTIC 1856178354Ssam /* XXX verify all entries are NULL */ 1857178354Ssam int i; 1858178354Ssam for (i = 0; i < nt->nt_keyixmax; i++) 1859178354Ssam if (nt->nt_keyixmap[i] != NULL) 1860178354Ssam printf("%s: %s[%u] still active\n", __func__, 1861178354Ssam nt->nt_name, i); 1862178354Ssam#endif 1863184210Sdes FREE(nt->nt_keyixmap, M_80211_NODE); 1864178354Ssam nt->nt_keyixmap = NULL; 1865178354Ssam } 1866178354Ssam IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt); 1867178354Ssam IEEE80211_NODE_LOCK_DESTROY(nt); 1868178354Ssam} 1869178354Ssam 1870120483Ssam/* 1871138568Ssam * Timeout inactive stations and do related housekeeping. 1872138568Ssam * Note that we cannot hold the node lock while sending a 1873138568Ssam * frame as this would lead to a LOR. Instead we use a 1874138568Ssam * generation number to mark nodes that we've scanned and 1875138568Ssam * drop the lock and restart a scan if we have to time out 1876138568Ssam * a node. Since we are single-threaded by virtue of 1877120483Ssam * controlling the inactivity timer we can be sure this will 1878120483Ssam * process each node only once. 1879120483Ssam */ 1880138568Ssamstatic void 1881178354Ssamieee80211_timeout_stations(struct ieee80211com *ic) 1882116742Ssam{ 1883178354Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 1884178354Ssam struct ieee80211vap *vap; 1885120483Ssam struct ieee80211_node *ni; 1886178354Ssam int gen = 0; 1887116742Ssam 1888178354Ssam IEEE80211_NODE_ITERATE_LOCK(nt); 1889154532Ssam gen = ++nt->nt_scangen; 1890120483Ssamrestart: 1891138568Ssam IEEE80211_NODE_LOCK(nt); 1892138568Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1893120483Ssam if (ni->ni_scangen == gen) /* previously handled */ 1894120483Ssam continue; 1895120483Ssam ni->ni_scangen = gen; 1896138568Ssam /* 1897147788Ssam * Ignore entries for which have yet to receive an 1898147788Ssam * authentication frame. These are transient and 1899147788Ssam * will be reclaimed when the last reference to them 1900147788Ssam * goes away (when frame xmits complete). 1901147788Ssam */ 1902178354Ssam vap = ni->ni_vap; 1903178354Ssam /* 1904178354Ssam * Only process stations when in RUN state. This 1905178354Ssam * insures, for example, that we don't timeout an 1906178354Ssam * inactive station during CAC. Note that CSA state 1907178354Ssam * is actually handled in ieee80211_node_timeout as 1908178354Ssam * it applies to more than timeout processing. 1909178354Ssam */ 1910178354Ssam if (vap->iv_state != IEEE80211_S_RUN) 1911178354Ssam continue; 1912178354Ssam /* XXX can vap be NULL? */ 1913178354Ssam if ((vap->iv_opmode == IEEE80211_M_HOSTAP || 1914178354Ssam vap->iv_opmode == IEEE80211_M_STA) && 1915148323Ssam (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 1916147788Ssam continue; 1917147788Ssam /* 1918138568Ssam * Free fragment if not needed anymore 1919138568Ssam * (last fragment older than 1s). 1920178354Ssam * XXX doesn't belong here, move to node_age 1921138568Ssam */ 1922138568Ssam if (ni->ni_rxfrag[0] != NULL && 1923138568Ssam ticks > ni->ni_rxfragstamp + hz) { 1924138568Ssam m_freem(ni->ni_rxfrag[0]); 1925138568Ssam ni->ni_rxfrag[0] = NULL; 1926138568Ssam } 1927184277Ssam if (ni->ni_inact > 0) { 1928172062Ssam ni->ni_inact--; 1929184277Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 1930184277Ssam "%s: inact %u inact_reload %u nrates %u", 1931184277Ssam __func__, ni->ni_inact, ni->ni_inact_reload, 1932184277Ssam ni->ni_rates.rs_nrates); 1933184277Ssam } 1934140498Ssam /* 1935140498Ssam * Special case ourself; we may be idle for extended periods 1936140498Ssam * of time and regardless reclaiming our state is wrong. 1937178354Ssam * XXX run ic_node_age 1938140498Ssam */ 1939178354Ssam if (ni == vap->iv_bss) 1940140498Ssam continue; 1941178354Ssam if (ni->ni_associd != 0 || 1942178354Ssam (vap->iv_opmode == IEEE80211_M_IBSS || 1943178354Ssam vap->iv_opmode == IEEE80211_M_AHDEMO)) { 1944119150Ssam /* 1945178354Ssam * Age/drain resources held by the station. 1946138568Ssam */ 1947178354Ssam ic->ic_node_age(ni); 1948138568Ssam /* 1949138568Ssam * Probe the station before time it out. We 1950138568Ssam * send a null data frame which may not be 1951138568Ssam * universally supported by drivers (need it 1952138568Ssam * for ps-poll support so it should be...). 1953170530Ssam * 1954170530Ssam * XXX don't probe the station unless we've 1955170530Ssam * received a frame from them (and have 1956170530Ssam * some idea of the rates they are capable 1957170530Ssam * of); this will get fixed more properly 1958170530Ssam * soon with better handling of the rate set. 1959138568Ssam */ 1960178354Ssam if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) && 1961172062Ssam (0 < ni->ni_inact && 1962178354Ssam ni->ni_inact <= vap->iv_inact_probe) && 1963170530Ssam ni->ni_rates.rs_nrates != 0) { 1964178354Ssam IEEE80211_NOTE(vap, 1965148320Ssam IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, 1966148320Ssam ni, "%s", 1967148320Ssam "probe station due to inactivity"); 1968148582Ssam /* 1969148582Ssam * Grab a reference before unlocking the table 1970148582Ssam * so the node cannot be reclaimed before we 1971148582Ssam * send the frame. ieee80211_send_nulldata 1972148582Ssam * understands we've done this and reclaims the 1973148582Ssam * ref for us as needed. 1974148582Ssam */ 1975148582Ssam ieee80211_ref_node(ni); 1976138568Ssam IEEE80211_NODE_UNLOCK(nt); 1977148301Ssam ieee80211_send_nulldata(ni); 1978138568Ssam /* XXX stat? */ 1979138568Ssam goto restart; 1980138568Ssam } 1981138568Ssam } 1982178354Ssam if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) && 1983172062Ssam ni->ni_inact <= 0) { 1984178354Ssam IEEE80211_NOTE(vap, 1985148320Ssam IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni, 1986148320Ssam "station timed out due to inactivity " 1987148320Ssam "(refcnt %u)", ieee80211_node_refcnt(ni)); 1988138568Ssam /* 1989138568Ssam * Send a deauthenticate frame and drop the station. 1990138568Ssam * This is somewhat complicated due to reference counts 1991138568Ssam * and locking. At this point a station will typically 1992138568Ssam * have a reference count of 1. ieee80211_node_leave 1993138568Ssam * will do a "free" of the node which will drop the 1994138568Ssam * reference count. But in the meantime a reference 1995138568Ssam * wil be held by the deauth frame. The actual reclaim 1996138568Ssam * of the node will happen either after the tx is 1997138568Ssam * completed or by ieee80211_node_leave. 1998120483Ssam * 1999138568Ssam * Separately we must drop the node lock before sending 2000170530Ssam * in case the driver takes a lock, as this can result 2001170530Ssam * in a LOR between the node lock and the driver lock. 2002119150Ssam */ 2003172229Ssam ieee80211_ref_node(ni); 2004138568Ssam IEEE80211_NODE_UNLOCK(nt); 2005138568Ssam if (ni->ni_associd != 0) { 2006178354Ssam IEEE80211_SEND_MGMT(ni, 2007138568Ssam IEEE80211_FC0_SUBTYPE_DEAUTH, 2008138568Ssam IEEE80211_REASON_AUTH_EXPIRE); 2009138568Ssam } 2010178354Ssam ieee80211_node_leave(ni); 2011172229Ssam ieee80211_free_node(ni); 2012178354Ssam vap->iv_stats.is_node_timeout++; 2013120483Ssam goto restart; 2014120483Ssam } 2015116742Ssam } 2016138568Ssam IEEE80211_NODE_UNLOCK(nt); 2017138568Ssam 2018178354Ssam IEEE80211_NODE_ITERATE_UNLOCK(nt); 2019170530Ssam} 2020138568Ssam 2021178354Ssam/* 2022178354Ssam * Aggressively reclaim resources. This should be used 2023178354Ssam * only in a critical situation to reclaim mbuf resources. 2024178354Ssam */ 2025170530Ssamvoid 2026178354Ssamieee80211_drain(struct ieee80211com *ic) 2027178354Ssam{ 2028178354Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 2029178354Ssam struct ieee80211vap *vap; 2030178354Ssam struct ieee80211_node *ni; 2031178354Ssam 2032178354Ssam IEEE80211_NODE_LOCK(nt); 2033178354Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 2034178354Ssam /* 2035178354Ssam * Ignore entries for which have yet to receive an 2036178354Ssam * authentication frame. These are transient and 2037178354Ssam * will be reclaimed when the last reference to them 2038178354Ssam * goes away (when frame xmits complete). 2039178354Ssam */ 2040178354Ssam vap = ni->ni_vap; 2041178354Ssam /* 2042178354Ssam * Only process stations when in RUN state. This 2043178354Ssam * insures, for example, that we don't timeout an 2044178354Ssam * inactive station during CAC. Note that CSA state 2045178354Ssam * is actually handled in ieee80211_node_timeout as 2046178354Ssam * it applies to more than timeout processing. 2047178354Ssam */ 2048178354Ssam if (vap->iv_state != IEEE80211_S_RUN) 2049178354Ssam continue; 2050178354Ssam /* XXX can vap be NULL? */ 2051178354Ssam if ((vap->iv_opmode == IEEE80211_M_HOSTAP || 2052178354Ssam vap->iv_opmode == IEEE80211_M_STA) && 2053178354Ssam (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 2054178354Ssam continue; 2055178354Ssam /* 2056178354Ssam * Free fragments. 2057178354Ssam * XXX doesn't belong here, move to node_drain 2058178354Ssam */ 2059178354Ssam if (ni->ni_rxfrag[0] != NULL) { 2060178354Ssam m_freem(ni->ni_rxfrag[0]); 2061178354Ssam ni->ni_rxfrag[0] = NULL; 2062178354Ssam } 2063178354Ssam /* 2064178354Ssam * Drain resources held by the station. 2065178354Ssam */ 2066178354Ssam ic->ic_node_drain(ni); 2067178354Ssam } 2068178354Ssam IEEE80211_NODE_UNLOCK(nt); 2069178354Ssam} 2070178354Ssam 2071178354Ssam/* 2072178354Ssam * Per-ieee80211com inactivity timer callback. 2073178354Ssam */ 2074178354Ssamvoid 2075170530Ssamieee80211_node_timeout(void *arg) 2076170530Ssam{ 2077170530Ssam struct ieee80211com *ic = arg; 2078170530Ssam 2079178354Ssam /* 2080178354Ssam * Defer timeout processing if a channel switch is pending. 2081178354Ssam * We typically need to be mute so not doing things that 2082178354Ssam * might generate frames is good to handle in one place. 2083178354Ssam * Supressing the station timeout processing may extend the 2084178354Ssam * lifetime of inactive stations (by not decrementing their 2085178354Ssam * idle counters) but this should be ok unless the CSA is 2086178354Ssam * active for an unusually long time. 2087178354Ssam */ 2088178354Ssam if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) { 2089178354Ssam ieee80211_scan_timeout(ic); 2090178354Ssam ieee80211_timeout_stations(ic); 2091170530Ssam 2092178354Ssam IEEE80211_LOCK(ic); 2093178354Ssam ieee80211_erp_timeout(ic); 2094178354Ssam ieee80211_ht_timeout(ic); 2095178354Ssam IEEE80211_UNLOCK(ic); 2096178354Ssam } 2097170530Ssam callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz, 2098170530Ssam ieee80211_node_timeout, ic); 2099116742Ssam} 2100116742Ssam 2101116742Ssamvoid 2102178354Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, 2103178354Ssam ieee80211_iter_func *f, void *arg) 2104116742Ssam{ 2105116742Ssam struct ieee80211_node *ni; 2106138568Ssam u_int gen; 2107116742Ssam 2108178354Ssam IEEE80211_NODE_ITERATE_LOCK(nt); 2109154532Ssam gen = ++nt->nt_scangen; 2110138568Ssamrestart: 2111138568Ssam IEEE80211_NODE_LOCK(nt); 2112138568Ssam TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 2113138568Ssam if (ni->ni_scangen != gen) { 2114138568Ssam ni->ni_scangen = gen; 2115138568Ssam (void) ieee80211_ref_node(ni); 2116138568Ssam IEEE80211_NODE_UNLOCK(nt); 2117138568Ssam (*f)(arg, ni); 2118138568Ssam ieee80211_free_node(ni); 2119138568Ssam goto restart; 2120138568Ssam } 2121138568Ssam } 2122138568Ssam IEEE80211_NODE_UNLOCK(nt); 2123138568Ssam 2124178354Ssam IEEE80211_NODE_ITERATE_UNLOCK(nt); 2125116742Ssam} 2126138568Ssam 2127138568Ssamvoid 2128138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 2129138568Ssam{ 2130138568Ssam printf("0x%p: mac %s refcnt %d\n", ni, 2131138568Ssam ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 2132138568Ssam printf("\tscangen %u authmode %u flags 0x%x\n", 2133138568Ssam ni->ni_scangen, ni->ni_authmode, ni->ni_flags); 2134138568Ssam printf("\tassocid 0x%x txpower %u vlan %u\n", 2135138568Ssam ni->ni_associd, ni->ni_txpower, ni->ni_vlan); 2136138568Ssam printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n", 2137167439Ssam ni->ni_txseqs[IEEE80211_NONQOS_TID], 2138167439Ssam ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT, 2139167439Ssam ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK, 2140138568Ssam ni->ni_rxfragstamp); 2141170530Ssam printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n", 2142178354Ssam ni->ni_rstamp, node_getrssi(ni), ni->ni_noise, 2143170530Ssam ni->ni_intval, ni->ni_capinfo); 2144138568Ssam printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n", 2145138568Ssam ether_sprintf(ni->ni_bssid), 2146138568Ssam ni->ni_esslen, ni->ni_essid, 2147138568Ssam ni->ni_chan->ic_freq, ni->ni_chan->ic_flags); 2148184277Ssam printf("\tinact %u inact_reload %u txrate %u\n", 2149184277Ssam ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate); 2150170530Ssam printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n", 2151170530Ssam ni->ni_htcap, ni->ni_htparam, 2152170530Ssam ni->ni_htctlchan, ni->ni_ht2ndchan); 2153170530Ssam printf("\thtopmode %x htstbc %x chw %u\n", 2154170530Ssam ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw); 2155138568Ssam} 2156138568Ssam 2157138568Ssamvoid 2158138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt) 2159138568Ssam{ 2160138568Ssam ieee80211_iterate_nodes(nt, 2161138568Ssam (ieee80211_iter_func *) ieee80211_dump_node, nt); 2162138568Ssam} 2163138568Ssam 2164179642Ssamstatic void 2165179642Ssamieee80211_notify_erp_locked(struct ieee80211com *ic) 2166172211Ssam{ 2167178354Ssam struct ieee80211vap *vap; 2168178354Ssam 2169178354Ssam IEEE80211_LOCK_ASSERT(ic); 2170178354Ssam 2171178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 2172178354Ssam if (vap->iv_opmode == IEEE80211_M_HOSTAP) 2173178354Ssam ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP); 2174172211Ssam} 2175172211Ssam 2176179642Ssamvoid 2177179642Ssamieee80211_notify_erp(struct ieee80211com *ic) 2178179642Ssam{ 2179179642Ssam IEEE80211_LOCK(ic); 2180179642Ssam ieee80211_notify_erp_locked(ic); 2181179642Ssam IEEE80211_UNLOCK(ic); 2182179642Ssam} 2183179642Ssam 2184138568Ssam/* 2185138568Ssam * Handle a station joining an 11g network. 2186138568Ssam */ 2187138568Ssamstatic void 2188178354Ssamieee80211_node_join_11g(struct ieee80211_node *ni) 2189138568Ssam{ 2190178354Ssam struct ieee80211com *ic = ni->ni_ic; 2191138568Ssam 2192173273Ssam IEEE80211_LOCK_ASSERT(ic); 2193173273Ssam 2194138568Ssam /* 2195138568Ssam * Station isn't capable of short slot time. Bump 2196138568Ssam * the count of long slot time stations and disable 2197138568Ssam * use of short slot time. Note that the actual switch 2198138568Ssam * over to long slot time use may not occur until the 2199138568Ssam * next beacon transmission (per sec. 7.3.1.4 of 11g). 2200138568Ssam */ 2201138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2202138568Ssam ic->ic_longslotsta++; 2203178354Ssam IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2204172211Ssam "station needs long slot time, count %d", 2205172211Ssam ic->ic_longslotsta); 2206138568Ssam /* XXX vap's w/ conflicting needs won't work */ 2207170530Ssam if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) { 2208170530Ssam /* 2209170530Ssam * Don't force slot time when switched to turbo 2210170530Ssam * mode as non-ERP stations won't be present; this 2211170530Ssam * need only be done when on the normal G channel. 2212170530Ssam */ 2213170530Ssam ieee80211_set_shortslottime(ic, 0); 2214170530Ssam } 2215138568Ssam } 2216138568Ssam /* 2217138568Ssam * If the new station is not an ERP station 2218138568Ssam * then bump the counter and enable protection 2219138568Ssam * if configured. 2220138568Ssam */ 2221178354Ssam if (!ieee80211_iserp_rateset(&ni->ni_rates)) { 2222138568Ssam ic->ic_nonerpsta++; 2223178354Ssam IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2224172211Ssam "station is !ERP, %d non-ERP stations associated", 2225172211Ssam ic->ic_nonerpsta); 2226138568Ssam /* 2227138568Ssam * If station does not support short preamble 2228138568Ssam * then we must enable use of Barker preamble. 2229138568Ssam */ 2230138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) { 2231178354Ssam IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2232172211Ssam "%s", "station needs long preamble"); 2233138568Ssam ic->ic_flags |= IEEE80211_F_USEBARKER; 2234138568Ssam ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 2235138568Ssam } 2236172211Ssam /* 2237178354Ssam * If protection is configured and this is the first 2238178354Ssam * indication we should use protection, enable it. 2239172211Ssam */ 2240172211Ssam if (ic->ic_protmode != IEEE80211_PROT_NONE && 2241172211Ssam ic->ic_nonerpsta == 1 && 2242172211Ssam (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) { 2243178354Ssam IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC, 2244172211Ssam "%s: enable use of protection\n", __func__); 2245172211Ssam ic->ic_flags |= IEEE80211_F_USEPROT; 2246179642Ssam ieee80211_notify_erp_locked(ic); 2247172211Ssam } 2248138568Ssam } else 2249138568Ssam ni->ni_flags |= IEEE80211_NODE_ERP; 2250138568Ssam} 2251138568Ssam 2252138568Ssamvoid 2253178354Ssamieee80211_node_join(struct ieee80211_node *ni, int resp) 2254138568Ssam{ 2255178354Ssam struct ieee80211com *ic = ni->ni_ic; 2256178354Ssam struct ieee80211vap *vap = ni->ni_vap; 2257138568Ssam int newassoc; 2258138568Ssam 2259138568Ssam if (ni->ni_associd == 0) { 2260170530Ssam uint16_t aid; 2261138568Ssam 2262178354Ssam KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap")); 2263138568Ssam /* 2264138568Ssam * It would be good to search the bitmap 2265138568Ssam * more efficiently, but this will do for now. 2266138568Ssam */ 2267178354Ssam for (aid = 1; aid < vap->iv_max_aid; aid++) { 2268178354Ssam if (!IEEE80211_AID_ISSET(vap, aid)) 2269138568Ssam break; 2270138568Ssam } 2271178354Ssam if (aid >= vap->iv_max_aid) { 2272179640Ssam IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY); 2273178354Ssam ieee80211_node_leave(ni); 2274138568Ssam return; 2275138568Ssam } 2276138568Ssam ni->ni_associd = aid | 0xc000; 2277173273Ssam ni->ni_jointime = time_uptime; 2278178354Ssam IEEE80211_LOCK(ic); 2279178354Ssam IEEE80211_AID_SET(vap, ni->ni_associd); 2280178354Ssam vap->iv_sta_assoc++; 2281138568Ssam ic->ic_sta_assoc++; 2282173273Ssam 2283173273Ssam if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan)) 2284173273Ssam ieee80211_ht_node_join(ni); 2285170530Ssam if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) && 2286170530Ssam IEEE80211_IS_CHAN_FULL(ic->ic_bsschan)) 2287178354Ssam ieee80211_node_join_11g(ni); 2288173273Ssam IEEE80211_UNLOCK(ic); 2289173273Ssam 2290173273Ssam newassoc = 1; 2291138568Ssam } else 2292138568Ssam newassoc = 0; 2293138568Ssam 2294178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni, 2295183256Ssam "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s", 2296139523Ssam IEEE80211_NODE_AID(ni), 2297139523Ssam ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long", 2298139523Ssam ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", 2299139523Ssam ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "", 2300170530Ssam ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "", 2301170530Ssam ni->ni_flags & IEEE80211_NODE_HT ? 2302182834Ssam (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "", 2303173273Ssam ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "", 2304183255Ssam ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" : 2305183255Ssam ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "", 2306183256Ssam ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "", 2307178354Ssam IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ? 2308170530Ssam ", fast-frames" : "", 2309178354Ssam IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ? 2310170530Ssam ", turbo" : "" 2311139523Ssam ); 2312138568Ssam 2313183251Ssam node_setuptxparms(ni); 2314138568Ssam /* give driver a chance to setup state like ni_txrate */ 2315139524Ssam if (ic->ic_newassoc != NULL) 2316148307Ssam ic->ic_newassoc(ni, newassoc); 2317178354Ssam IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS); 2318138568Ssam /* tell the authenticator about new station */ 2319178354Ssam if (vap->iv_auth->ia_node_join != NULL) 2320178354Ssam vap->iv_auth->ia_node_join(ni); 2321178354Ssam ieee80211_notify_node_join(ni, 2322173866Ssam resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 2323138568Ssam} 2324138568Ssam 2325172211Ssamstatic void 2326172211Ssamdisable_protection(struct ieee80211com *ic) 2327172211Ssam{ 2328172211Ssam KASSERT(ic->ic_nonerpsta == 0 && 2329172211Ssam (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0, 2330172211Ssam ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta, 2331172211Ssam ic->ic_flags_ext)); 2332172211Ssam 2333172211Ssam ic->ic_flags &= ~IEEE80211_F_USEPROT; 2334172211Ssam /* XXX verify mode? */ 2335172211Ssam if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) { 2336172211Ssam ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 2337172211Ssam ic->ic_flags &= ~IEEE80211_F_USEBARKER; 2338172211Ssam } 2339179642Ssam ieee80211_notify_erp_locked(ic); 2340172211Ssam} 2341172211Ssam 2342138568Ssam/* 2343138568Ssam * Handle a station leaving an 11g network. 2344138568Ssam */ 2345138568Ssamstatic void 2346178354Ssamieee80211_node_leave_11g(struct ieee80211_node *ni) 2347138568Ssam{ 2348178354Ssam struct ieee80211com *ic = ni->ni_ic; 2349138568Ssam 2350173273Ssam IEEE80211_LOCK_ASSERT(ic); 2351173273Ssam 2352170530Ssam KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan), 2353178354Ssam ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq, 2354178354Ssam ic->ic_bsschan->ic_flags)); 2355138568Ssam 2356138568Ssam /* 2357138568Ssam * If a long slot station do the slot time bookkeeping. 2358138568Ssam */ 2359138568Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2360138568Ssam KASSERT(ic->ic_longslotsta > 0, 2361138568Ssam ("bogus long slot station count %d", ic->ic_longslotsta)); 2362138568Ssam ic->ic_longslotsta--; 2363178354Ssam IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2364172211Ssam "long slot time station leaves, count now %d", 2365172211Ssam ic->ic_longslotsta); 2366138568Ssam if (ic->ic_longslotsta == 0) { 2367138568Ssam /* 2368138568Ssam * Re-enable use of short slot time if supported 2369138568Ssam * and not operating in IBSS mode (per spec). 2370138568Ssam */ 2371138568Ssam if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 2372138568Ssam ic->ic_opmode != IEEE80211_M_IBSS) { 2373178354Ssam IEEE80211_DPRINTF(ni->ni_vap, 2374178354Ssam IEEE80211_MSG_ASSOC, 2375138568Ssam "%s: re-enable use of short slot time\n", 2376138568Ssam __func__); 2377138568Ssam ieee80211_set_shortslottime(ic, 1); 2378138568Ssam } 2379138568Ssam } 2380138568Ssam } 2381138568Ssam /* 2382138568Ssam * If a non-ERP station do the protection-related bookkeeping. 2383138568Ssam */ 2384138568Ssam if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) { 2385138568Ssam KASSERT(ic->ic_nonerpsta > 0, 2386138568Ssam ("bogus non-ERP station count %d", ic->ic_nonerpsta)); 2387138568Ssam ic->ic_nonerpsta--; 2388178354Ssam IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2389172211Ssam "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta, 2390172211Ssam (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ? 2391172211Ssam " (non-ERP sta present)" : ""); 2392172211Ssam if (ic->ic_nonerpsta == 0 && 2393172211Ssam (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) { 2394178354Ssam IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC, 2395138568Ssam "%s: disable use of protection\n", __func__); 2396172211Ssam disable_protection(ic); 2397138568Ssam } 2398138568Ssam } 2399138568Ssam} 2400138568Ssam 2401138568Ssam/* 2402172211Ssam * Time out presence of an overlapping bss with non-ERP 2403172211Ssam * stations. When operating in hostap mode we listen for 2404172211Ssam * beacons from other stations and if we identify a non-ERP 2405172211Ssam * station is present we enable protection. To identify 2406172211Ssam * when all non-ERP stations are gone we time out this 2407172211Ssam * condition. 2408172211Ssam */ 2409172211Ssamstatic void 2410172211Ssamieee80211_erp_timeout(struct ieee80211com *ic) 2411172211Ssam{ 2412172211Ssam 2413172211Ssam IEEE80211_LOCK_ASSERT(ic); 2414172211Ssam 2415172211Ssam if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) && 2416172211Ssam time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) { 2417178354Ssam#if 0 2418178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni, 2419178354Ssam "%s", "age out non-ERP sta present on channel"); 2420178354Ssam#endif 2421172211Ssam ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR; 2422172211Ssam if (ic->ic_nonerpsta == 0) 2423172211Ssam disable_protection(ic); 2424172211Ssam } 2425172211Ssam} 2426172211Ssam 2427172211Ssam/* 2428138568Ssam * Handle bookkeeping for station deauthentication/disassociation 2429138568Ssam * when operating as an ap. 2430138568Ssam */ 2431138568Ssamvoid 2432178354Ssamieee80211_node_leave(struct ieee80211_node *ni) 2433138568Ssam{ 2434178354Ssam struct ieee80211com *ic = ni->ni_ic; 2435178354Ssam struct ieee80211vap *vap = ni->ni_vap; 2436140499Ssam struct ieee80211_node_table *nt = ni->ni_table; 2437138568Ssam 2438178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni, 2439172211Ssam "station with aid %d leaves", IEEE80211_NODE_AID(ni)); 2440138568Ssam 2441178354Ssam KASSERT(vap->iv_opmode != IEEE80211_M_STA, 2442178354Ssam ("unexpected operating mode %u", vap->iv_opmode)); 2443138568Ssam /* 2444138568Ssam * If node wasn't previously associated all 2445138568Ssam * we need to do is reclaim the reference. 2446138568Ssam */ 2447138568Ssam /* XXX ibss mode bypasses 11g and notification */ 2448138568Ssam if (ni->ni_associd == 0) 2449138568Ssam goto done; 2450138568Ssam /* 2451138568Ssam * Tell the authenticator the station is leaving. 2452138568Ssam * Note that we must do this before yanking the 2453138568Ssam * association id as the authenticator uses the 2454138568Ssam * associd to locate it's state block. 2455138568Ssam */ 2456178354Ssam if (vap->iv_auth->ia_node_leave != NULL) 2457178354Ssam vap->iv_auth->ia_node_leave(ni); 2458173273Ssam 2459173273Ssam IEEE80211_LOCK(ic); 2460178354Ssam IEEE80211_AID_CLR(vap, ni->ni_associd); 2461138568Ssam ni->ni_associd = 0; 2462178354Ssam vap->iv_sta_assoc--; 2463138568Ssam ic->ic_sta_assoc--; 2464138568Ssam 2465173273Ssam if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan)) 2466173273Ssam ieee80211_ht_node_leave(ni); 2467170530Ssam if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) && 2468170530Ssam IEEE80211_IS_CHAN_FULL(ic->ic_bsschan)) 2469178354Ssam ieee80211_node_leave_11g(ni); 2470173273Ssam IEEE80211_UNLOCK(ic); 2471138568Ssam /* 2472138568Ssam * Cleanup station state. In particular clear various 2473138568Ssam * state that might otherwise be reused if the node 2474138568Ssam * is reused before the reference count goes to zero 2475138568Ssam * (and memory is reclaimed). 2476138568Ssam */ 2477178354Ssam ieee80211_sta_leave(ni); 2478138568Ssamdone: 2479140499Ssam /* 2480140499Ssam * Remove the node from any table it's recorded in and 2481140499Ssam * drop the caller's reference. Removal from the table 2482140499Ssam * is important to insure the node is not reprocessed 2483140499Ssam * for inactivity. 2484140499Ssam */ 2485140499Ssam if (nt != NULL) { 2486140499Ssam IEEE80211_NODE_LOCK(nt); 2487140499Ssam node_reclaim(nt, ni); 2488140499Ssam IEEE80211_NODE_UNLOCK(nt); 2489140499Ssam } else 2490140499Ssam ieee80211_free_node(ni); 2491138568Ssam} 2492138568Ssam 2493178354Ssamstruct rssiinfo { 2494178354Ssam struct ieee80211vap *vap; 2495178354Ssam int rssi_samples; 2496178354Ssam uint32_t rssi_total; 2497178354Ssam}; 2498178354Ssam 2499178354Ssamstatic void 2500178354Ssamget_hostap_rssi(void *arg, struct ieee80211_node *ni) 2501178354Ssam{ 2502178354Ssam struct rssiinfo *info = arg; 2503178354Ssam struct ieee80211vap *vap = ni->ni_vap; 2504178354Ssam int8_t rssi; 2505178354Ssam 2506178354Ssam if (info->vap != vap) 2507178354Ssam return; 2508178354Ssam /* only associated stations */ 2509178354Ssam if (ni->ni_associd == 0) 2510178354Ssam return; 2511178354Ssam rssi = vap->iv_ic->ic_node_getrssi(ni); 2512178354Ssam if (rssi != 0) { 2513178354Ssam info->rssi_samples++; 2514178354Ssam info->rssi_total += rssi; 2515178354Ssam } 2516178354Ssam} 2517178354Ssam 2518178354Ssamstatic void 2519178354Ssamget_adhoc_rssi(void *arg, struct ieee80211_node *ni) 2520178354Ssam{ 2521178354Ssam struct rssiinfo *info = arg; 2522178354Ssam struct ieee80211vap *vap = ni->ni_vap; 2523178354Ssam int8_t rssi; 2524178354Ssam 2525178354Ssam if (info->vap != vap) 2526178354Ssam return; 2527178354Ssam /* only neighbors */ 2528178354Ssam /* XXX check bssid */ 2529178354Ssam if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 2530178354Ssam return; 2531178354Ssam rssi = vap->iv_ic->ic_node_getrssi(ni); 2532178354Ssam if (rssi != 0) { 2533178354Ssam info->rssi_samples++; 2534178354Ssam info->rssi_total += rssi; 2535178354Ssam } 2536178354Ssam} 2537178354Ssam 2538170530Ssamint8_t 2539178354Ssamieee80211_getrssi(struct ieee80211vap *vap) 2540138568Ssam{ 2541138568Ssam#define NZ(x) ((x) == 0 ? 1 : (x)) 2542178354Ssam struct ieee80211com *ic = vap->iv_ic; 2543178354Ssam struct rssiinfo info; 2544138568Ssam 2545178354Ssam info.rssi_total = 0; 2546178354Ssam info.rssi_samples = 0; 2547178354Ssam info.vap = vap; 2548178354Ssam switch (vap->iv_opmode) { 2549138568Ssam case IEEE80211_M_IBSS: /* average of all ibss neighbors */ 2550138568Ssam case IEEE80211_M_AHDEMO: /* average of all neighbors */ 2551178354Ssam ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info); 2552178354Ssam break; 2553138568Ssam case IEEE80211_M_HOSTAP: /* average of all associated stations */ 2554178354Ssam ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info); 2555138568Ssam break; 2556138568Ssam case IEEE80211_M_MONITOR: /* XXX */ 2557138568Ssam case IEEE80211_M_STA: /* use stats from associated ap */ 2558138568Ssam default: 2559178354Ssam if (vap->iv_bss != NULL) 2560178354Ssam info.rssi_total = ic->ic_node_getrssi(vap->iv_bss); 2561178354Ssam info.rssi_samples = 1; 2562138568Ssam break; 2563138568Ssam } 2564178354Ssam return info.rssi_total / NZ(info.rssi_samples); 2565138568Ssam#undef NZ 2566138568Ssam} 2567138568Ssam 2568170530Ssamvoid 2569178354Ssamieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise) 2570138568Ssam{ 2571138568Ssam 2572178354Ssam if (vap->iv_bss == NULL) /* NB: shouldn't happen */ 2573170530Ssam return; 2574178354Ssam vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise); 2575170530Ssam /* for non-station mode return avg'd rssi accounting */ 2576178354Ssam if (vap->iv_opmode != IEEE80211_M_STA) 2577178354Ssam *rssi = ieee80211_getrssi(vap); 2578138568Ssam} 2579