ieee80211_node.c revision 179642
1/*- 2 * Copyright (c) 2001 Atsushi Onoe 3 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 179642 2008-06-07 17:51:41Z sam $"); 29 30#include "opt_wlan.h" 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/mbuf.h> 35#include <sys/malloc.h> 36#include <sys/kernel.h> 37 38#include <sys/socket.h> 39 40#include <net/if.h> 41#include <net/if_media.h> 42#include <net/ethernet.h> 43 44#include <net80211/ieee80211_var.h> 45#include <net80211/ieee80211_input.h> 46#include <net80211/ieee80211_wds.h> 47 48#include <net/bpf.h> 49 50/* 51 * Association id's are managed with a bit vector. 52 */ 53#define IEEE80211_AID_SET(_vap, b) \ 54 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \ 55 (1 << (IEEE80211_AID(b) % 32))) 56#define IEEE80211_AID_CLR(_vap, b) \ 57 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \ 58 ~(1 << (IEEE80211_AID(b) % 32))) 59#define IEEE80211_AID_ISSET(_vap, b) \ 60 ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32))) 61 62#ifdef IEEE80211_DEBUG_REFCNT 63#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line 64#else 65#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__ 66#endif 67 68static int ieee80211_sta_join1(struct ieee80211_node *); 69 70static struct ieee80211_node *node_alloc(struct ieee80211_node_table *); 71static void node_cleanup(struct ieee80211_node *); 72static void node_free(struct ieee80211_node *); 73static void node_age(struct ieee80211_node *); 74static int8_t node_getrssi(const struct ieee80211_node *); 75static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *); 76static void node_getmimoinfo(const struct ieee80211_node *, 77 struct ieee80211_mimo_info *); 78 79static void _ieee80211_free_node(struct ieee80211_node *); 80 81static void ieee80211_node_table_init(struct ieee80211com *ic, 82 struct ieee80211_node_table *nt, const char *name, 83 int inact, int keymaxix); 84static void ieee80211_node_table_reset(struct ieee80211_node_table *, 85 struct ieee80211vap *); 86static void ieee80211_node_reclaim(struct ieee80211_node *); 87static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt); 88static void ieee80211_erp_timeout(struct ieee80211com *); 89 90MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state"); 91MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie"); 92 93void 94ieee80211_node_attach(struct ieee80211com *ic) 95{ 96 ieee80211_node_table_init(ic, &ic->ic_sta, "station", 97 IEEE80211_INACT_INIT, ic->ic_max_keyix); 98 callout_init(&ic->ic_inact, CALLOUT_MPSAFE); 99 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz, 100 ieee80211_node_timeout, ic); 101 102 ic->ic_node_alloc = node_alloc; 103 ic->ic_node_free = node_free; 104 ic->ic_node_cleanup = node_cleanup; 105 ic->ic_node_age = node_age; 106 ic->ic_node_drain = node_age; /* NB: same as age */ 107 ic->ic_node_getrssi = node_getrssi; 108 ic->ic_node_getsignal = node_getsignal; 109 ic->ic_node_getmimoinfo = node_getmimoinfo; 110 111 /* 112 * Set flags to be propagated to all vap's; 113 * these define default behaviour/configuration. 114 */ 115 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */ 116} 117 118void 119ieee80211_node_detach(struct ieee80211com *ic) 120{ 121 122 callout_drain(&ic->ic_inact); 123 ieee80211_node_table_cleanup(&ic->ic_sta); 124} 125 126void 127ieee80211_node_vattach(struct ieee80211vap *vap) 128{ 129 /* NB: driver can override */ 130 vap->iv_max_aid = IEEE80211_AID_DEF; 131 132 /* default station inactivity timer setings */ 133 vap->iv_inact_init = IEEE80211_INACT_INIT; 134 vap->iv_inact_auth = IEEE80211_INACT_AUTH; 135 vap->iv_inact_run = IEEE80211_INACT_RUN; 136 vap->iv_inact_probe = IEEE80211_INACT_PROBE; 137} 138 139void 140ieee80211_node_latevattach(struct ieee80211vap *vap) 141{ 142 if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 143 /* XXX should we allow max aid to be zero? */ 144 if (vap->iv_max_aid < IEEE80211_AID_MIN) { 145 vap->iv_max_aid = IEEE80211_AID_MIN; 146 if_printf(vap->iv_ifp, 147 "WARNING: max aid too small, changed to %d\n", 148 vap->iv_max_aid); 149 } 150 MALLOC(vap->iv_aid_bitmap, uint32_t *, 151 howmany(vap->iv_max_aid, 32) * sizeof(uint32_t), 152 M_80211_NODE, M_NOWAIT | M_ZERO); 153 if (vap->iv_aid_bitmap == NULL) { 154 /* XXX no way to recover */ 155 printf("%s: no memory for AID bitmap, max aid %d!\n", 156 __func__, vap->iv_max_aid); 157 vap->iv_max_aid = 0; 158 } 159 } 160 161 ieee80211_reset_bss(vap); 162 163 vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode); 164} 165 166void 167ieee80211_node_vdetach(struct ieee80211vap *vap) 168{ 169 struct ieee80211com *ic = vap->iv_ic; 170 171 ieee80211_node_table_reset(&ic->ic_sta, vap); 172 if (vap->iv_bss != NULL) { 173 ieee80211_free_node(vap->iv_bss); 174 vap->iv_bss = NULL; 175 } 176 if (vap->iv_aid_bitmap != NULL) { 177 FREE(vap->iv_aid_bitmap, M_80211_NODE); 178 vap->iv_aid_bitmap = NULL; 179 } 180} 181 182/* 183 * Port authorize/unauthorize interfaces for use by an authenticator. 184 */ 185 186void 187ieee80211_node_authorize(struct ieee80211_node *ni) 188{ 189 ni->ni_flags |= IEEE80211_NODE_AUTH; 190 ni->ni_inact_reload = ni->ni_vap->iv_inact_run; 191 ni->ni_inact = ni->ni_inact_reload; 192} 193 194void 195ieee80211_node_unauthorize(struct ieee80211_node *ni) 196{ 197 ni->ni_flags &= ~IEEE80211_NODE_AUTH; 198 ni->ni_inact_reload = ni->ni_vap->iv_inact_auth; 199 if (ni->ni_inact > ni->ni_inact_reload) 200 ni->ni_inact = ni->ni_inact_reload; 201} 202 203/* 204 * Set/change the channel. The rate set is also updated as 205 * to insure a consistent view by drivers. 206 * XXX should be private but hostap needs it to deal with CSA 207 */ 208void 209ieee80211_node_set_chan(struct ieee80211_node *ni, 210 struct ieee80211_channel *chan) 211{ 212 struct ieee80211com *ic = ni->ni_ic; 213 214 KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel")); 215 216 ni->ni_chan = chan; 217 if (IEEE80211_IS_CHAN_HT(chan)) { 218 /* 219 * XXX Gotta be careful here; the rate set returned by 220 * ieee80211_get_suprates is actually any HT rate 221 * set so blindly copying it will be bad. We must 222 * install the legacy rate est in ni_rates and the 223 * HT rate set in ni_htrates. 224 */ 225 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan); 226 } 227 ni->ni_rates = *ieee80211_get_suprates(ic, chan); 228} 229 230static __inline void 231copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss) 232{ 233 /* propagate useful state */ 234 nbss->ni_authmode = obss->ni_authmode; 235 nbss->ni_txpower = obss->ni_txpower; 236 nbss->ni_vlan = obss->ni_vlan; 237 /* XXX statistics? */ 238 /* XXX legacy WDS bssid? */ 239} 240 241void 242ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan) 243{ 244 struct ieee80211com *ic = vap->iv_ic; 245 struct ieee80211_node *ni; 246 247 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 248 "%s: creating ibss on channel %u\n", __func__, 249 ieee80211_chan2ieee(ic, chan)); 250 251 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr); 252 if (ni == NULL) { 253 /* XXX recovery? */ 254 return; 255 } 256 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr); 257 ni->ni_esslen = vap->iv_des_ssid[0].len; 258 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen); 259 if (vap->iv_bss != NULL) 260 copy_bss(ni, vap->iv_bss); 261 ni->ni_intval = ic->ic_bintval; 262 if (vap->iv_flags & IEEE80211_F_PRIVACY) 263 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 264 if (ic->ic_phytype == IEEE80211_T_FH) { 265 ni->ni_fhdwell = 200; /* XXX */ 266 ni->ni_fhindex = 1; 267 } 268 if (vap->iv_opmode == IEEE80211_M_IBSS) { 269 vap->iv_flags |= IEEE80211_F_SIBSS; 270 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */ 271 if (vap->iv_flags & IEEE80211_F_DESBSSID) 272 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid); 273 else { 274 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN); 275 /* clear group bit, add local bit */ 276 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02; 277 } 278 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) { 279 if (vap->iv_flags & IEEE80211_F_DESBSSID) 280 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid); 281 else 282 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN); 283 } 284 /* 285 * Fix the channel and related attributes. 286 */ 287 /* clear DFS CAC state on previous channel */ 288 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC && 289 ic->ic_bsschan->ic_freq != chan->ic_freq && 290 IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) 291 ieee80211_dfs_cac_clear(ic, ic->ic_bsschan); 292 ic->ic_bsschan = chan; 293 ieee80211_node_set_chan(ni, chan); 294 ic->ic_curmode = ieee80211_chan2mode(chan); 295 /* 296 * Do mode-specific setup. 297 */ 298 if (IEEE80211_IS_CHAN_FULL(chan)) { 299 if (IEEE80211_IS_CHAN_ANYG(chan)) { 300 /* 301 * Use a mixed 11b/11g basic rate set. 302 */ 303 ieee80211_setbasicrates(&ni->ni_rates, 304 IEEE80211_MODE_11G); 305 if (vap->iv_flags & IEEE80211_F_PUREG) { 306 /* 307 * Also mark OFDM rates basic so 11b 308 * stations do not join (WiFi compliance). 309 */ 310 ieee80211_addbasicrates(&ni->ni_rates, 311 IEEE80211_MODE_11A); 312 } 313 } else if (IEEE80211_IS_CHAN_B(chan)) { 314 /* 315 * Force pure 11b rate set. 316 */ 317 ieee80211_setbasicrates(&ni->ni_rates, 318 IEEE80211_MODE_11B); 319 } 320 } 321 322 (void) ieee80211_sta_join1(ieee80211_ref_node(ni)); 323} 324 325/* 326 * Reset bss state on transition to the INIT state. 327 * Clear any stations from the table (they have been 328 * deauth'd) and reset the bss node (clears key, rate 329 * etc. state). 330 */ 331void 332ieee80211_reset_bss(struct ieee80211vap *vap) 333{ 334 struct ieee80211com *ic = vap->iv_ic; 335 struct ieee80211_node *ni, *obss; 336 337 ieee80211_node_table_reset(&ic->ic_sta, vap); 338 /* XXX multi-bss: wrong */ 339 ieee80211_reset_erp(ic); 340 341 ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr); 342 KASSERT(ni != NULL, ("unable to setup inital BSS node")); 343 obss = vap->iv_bss; 344 vap->iv_bss = ieee80211_ref_node(ni); 345 if (obss != NULL) { 346 copy_bss(ni, obss); 347 ni->ni_intval = ic->ic_bintval; 348 ieee80211_free_node(obss); 349 } else 350 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr); 351} 352 353static int 354match_ssid(const struct ieee80211_node *ni, 355 int nssid, const struct ieee80211_scan_ssid ssids[]) 356{ 357 int i; 358 359 for (i = 0; i < nssid; i++) { 360 if (ni->ni_esslen == ssids[i].len && 361 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0) 362 return 1; 363 } 364 return 0; 365} 366 367/* 368 * Test a node for suitability/compatibility. 369 */ 370static int 371check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni) 372{ 373 struct ieee80211com *ic = ni->ni_ic; 374 uint8_t rate; 375 376 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 377 return 0; 378 if (vap->iv_opmode == IEEE80211_M_IBSS) { 379 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 380 return 0; 381 } else { 382 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 383 return 0; 384 } 385 if (vap->iv_flags & IEEE80211_F_PRIVACY) { 386 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 387 return 0; 388 } else { 389 /* XXX does this mean privacy is supported or required? */ 390 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 391 return 0; 392 } 393 rate = ieee80211_fix_rate(ni, &ni->ni_rates, 394 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 395 if (rate & IEEE80211_RATE_BASIC) 396 return 0; 397 if (vap->iv_des_nssid != 0 && 398 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid)) 399 return 0; 400 if ((vap->iv_flags & IEEE80211_F_DESBSSID) && 401 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid)) 402 return 0; 403 return 1; 404} 405 406#ifdef IEEE80211_DEBUG 407/* 408 * Display node suitability/compatibility. 409 */ 410static void 411check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni) 412{ 413 struct ieee80211com *ic = ni->ni_ic; 414 uint8_t rate; 415 int fail; 416 417 fail = 0; 418 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 419 fail |= 0x01; 420 if (vap->iv_opmode == IEEE80211_M_IBSS) { 421 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 422 fail |= 0x02; 423 } else { 424 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 425 fail |= 0x02; 426 } 427 if (vap->iv_flags & IEEE80211_F_PRIVACY) { 428 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 429 fail |= 0x04; 430 } else { 431 /* XXX does this mean privacy is supported or required? */ 432 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 433 fail |= 0x04; 434 } 435 rate = ieee80211_fix_rate(ni, &ni->ni_rates, 436 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 437 if (rate & IEEE80211_RATE_BASIC) 438 fail |= 0x08; 439 if (vap->iv_des_nssid != 0 && 440 !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid)) 441 fail |= 0x10; 442 if ((vap->iv_flags & IEEE80211_F_DESBSSID) && 443 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid)) 444 fail |= 0x20; 445 446 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr)); 447 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' '); 448 printf(" %3d%c", 449 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' '); 450 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 451 fail & 0x08 ? '!' : ' '); 452 printf(" %4s%c", 453 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 454 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 455 "????", 456 fail & 0x02 ? '!' : ' '); 457 printf(" %3s%c ", 458 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no", 459 fail & 0x04 ? '!' : ' '); 460 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 461 printf("%s\n", fail & 0x10 ? "!" : ""); 462} 463#endif /* IEEE80211_DEBUG */ 464 465/* 466 * Handle 802.11 ad hoc network merge. The 467 * convention, set by the Wireless Ethernet Compatibility Alliance 468 * (WECA), is that an 802.11 station will change its BSSID to match 469 * the "oldest" 802.11 ad hoc network, on the same channel, that 470 * has the station's desired SSID. The "oldest" 802.11 network 471 * sends beacons with the greatest TSF timestamp. 472 * 473 * The caller is assumed to validate TSF's before attempting a merge. 474 * 475 * Return !0 if the BSSID changed, 0 otherwise. 476 */ 477int 478ieee80211_ibss_merge(struct ieee80211_node *ni) 479{ 480 struct ieee80211vap *vap = ni->ni_vap; 481#ifdef IEEE80211_DEBUG 482 struct ieee80211com *ic = ni->ni_ic; 483#endif 484 485 if (ni == vap->iv_bss || 486 IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) { 487 /* unchanged, nothing to do */ 488 return 0; 489 } 490 if (!check_bss(vap, ni)) { 491 /* capabilities mismatch */ 492 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC, 493 "%s: merge failed, capabilities mismatch\n", __func__); 494#ifdef IEEE80211_DEBUG 495 if (ieee80211_msg_assoc(vap)) 496 check_bss_debug(vap, ni); 497#endif 498 vap->iv_stats.is_ibss_capmismatch++; 499 return 0; 500 } 501 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC, 502 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__, 503 ether_sprintf(ni->ni_bssid), 504 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 505 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long", 506 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "" 507 ); 508 return ieee80211_sta_join1(ieee80211_ref_node(ni)); 509} 510 511/* 512 * Calculate HT channel promotion flags for all vaps. 513 * This assumes ni_chan have been setup for each vap. 514 */ 515static int 516gethtadjustflags(struct ieee80211com *ic) 517{ 518 struct ieee80211vap *vap; 519 int flags; 520 521 flags = 0; 522 /* XXX locking */ 523 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 524 if (vap->iv_state < IEEE80211_S_RUN) 525 continue; 526 switch (vap->iv_opmode) { 527 case IEEE80211_M_WDS: 528 case IEEE80211_M_STA: 529 case IEEE80211_M_AHDEMO: 530 case IEEE80211_M_HOSTAP: 531 case IEEE80211_M_IBSS: 532 flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan); 533 break; 534 default: 535 break; 536 } 537 } 538 return flags; 539} 540 541/* 542 * Check if the current channel needs to change based on whether 543 * any vap's are using HT20/HT40. This is used sync the state of 544 * ic_curchan after a channel width change on a running vap. 545 */ 546void 547ieee80211_sync_curchan(struct ieee80211com *ic) 548{ 549 struct ieee80211_channel *c; 550 551 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic)); 552 if (c != ic->ic_curchan) { 553 ic->ic_curchan = c; 554 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan); 555 ic->ic_set_channel(ic); 556 } 557} 558 559/* 560 * Change the current channel. The request channel may be 561 * promoted if other vap's are operating with HT20/HT40. 562 */ 563void 564ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c) 565{ 566 if (ic->ic_htcaps & IEEE80211_HTC_HT) { 567 int flags = gethtadjustflags(ic); 568 /* 569 * Check for channel promotion required to support the 570 * set of running vap's. This assumes we are called 571 * after ni_chan is setup for each vap. 572 */ 573 /* NB: this assumes IEEE80211_FEXT_USEHT40 > IEEE80211_FEXT_HT */ 574 if (flags > ieee80211_htchanflags(c)) 575 c = ieee80211_ht_adjust_channel(ic, c, flags); 576 } 577 ic->ic_bsschan = ic->ic_curchan = c; 578 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan); 579 ic->ic_set_channel(ic); 580} 581 582/* 583 * Join the specified IBSS/BSS network. The node is assumed to 584 * be passed in with a held reference. 585 */ 586static int 587ieee80211_sta_join1(struct ieee80211_node *selbs) 588{ 589 struct ieee80211vap *vap = selbs->ni_vap; 590 struct ieee80211com *ic = selbs->ni_ic; 591 struct ieee80211_node *obss; 592 int canreassoc; 593 594 /* 595 * Committed to selbs, setup state. 596 */ 597 obss = vap->iv_bss; 598 /* 599 * Check if old+new node have the same address in which 600 * case we can reassociate when operating in sta mode. 601 */ 602 canreassoc = (obss != NULL && 603 vap->iv_state == IEEE80211_S_RUN && 604 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr)); 605 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */ 606 if (obss != NULL) { 607 copy_bss(selbs, obss); 608 ieee80211_node_reclaim(obss); 609 obss = NULL; /* NB: guard against later use */ 610 } 611 612 /* 613 * Delete unusable rates; we've already checked 614 * that the negotiated rate set is acceptable. 615 */ 616 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates, 617 IEEE80211_F_DODEL | IEEE80211_F_JOIN); 618 619 ieee80211_setcurchan(ic, selbs->ni_chan); 620 /* 621 * Set the erp state (mostly the slot time) to deal with 622 * the auto-select case; this should be redundant if the 623 * mode is locked. 624 */ 625 ieee80211_reset_erp(ic); 626 ieee80211_wme_initparams(vap); 627 628 if (vap->iv_opmode == IEEE80211_M_STA) { 629 if (canreassoc) { 630 /* Reassociate */ 631 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1); 632 } else { 633 /* 634 * Act as if we received a DEAUTH frame in case we 635 * are invoked from the RUN state. This will cause 636 * us to try to re-authenticate if we are operating 637 * as a station. 638 */ 639 ieee80211_new_state(vap, IEEE80211_S_AUTH, 640 IEEE80211_FC0_SUBTYPE_DEAUTH); 641 } 642 } else 643 ieee80211_new_state(vap, IEEE80211_S_RUN, -1); 644 return 1; 645} 646 647int 648ieee80211_sta_join(struct ieee80211vap *vap, 649 const struct ieee80211_scan_entry *se) 650{ 651 struct ieee80211com *ic = vap->iv_ic; 652 struct ieee80211_node *ni; 653 654 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr); 655 if (ni == NULL) { 656 /* XXX msg */ 657 return 0; 658 } 659 /* 660 * Expand scan state into node's format. 661 * XXX may not need all this stuff 662 */ 663 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid); 664 ni->ni_esslen = se->se_ssid[1]; 665 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen); 666 ni->ni_rstamp = se->se_rstamp; 667 ni->ni_tstamp.tsf = se->se_tstamp.tsf; 668 ni->ni_intval = se->se_intval; 669 ni->ni_capinfo = se->se_capinfo; 670 ni->ni_chan = se->se_chan; 671 ni->ni_timoff = se->se_timoff; 672 ni->ni_fhdwell = se->se_fhdwell; 673 ni->ni_fhindex = se->se_fhindex; 674 ni->ni_erp = se->se_erp; 675 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi); 676 ni->ni_noise = se->se_noise; 677 678 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) { 679 ieee80211_ies_expand(&ni->ni_ies); 680 if (ni->ni_ies.ath_ie != NULL) 681 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie); 682 if (ni->ni_ies.htcap_ie != NULL) 683 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie); 684 if (ni->ni_ies.htinfo_ie != NULL) 685 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie); 686 } 687 688 vap->iv_dtim_period = se->se_dtimperiod; 689 vap->iv_dtim_count = 0; 690 691 /* NB: must be after ni_chan is setup */ 692 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates, 693 IEEE80211_F_DOSORT); 694 695 return ieee80211_sta_join1(ieee80211_ref_node(ni)); 696} 697 698/* 699 * Leave the specified IBSS/BSS network. The node is assumed to 700 * be passed in with a held reference. 701 */ 702void 703ieee80211_sta_leave(struct ieee80211_node *ni) 704{ 705 struct ieee80211com *ic = ni->ni_ic; 706 707 ic->ic_node_cleanup(ni); 708 ieee80211_notify_node_leave(ni); 709} 710 711/* 712 * Send a deauthenticate frame and drop the station. 713 */ 714void 715ieee80211_node_deauth(struct ieee80211_node *ni, int reason) 716{ 717 /* NB: bump the refcnt to be sure temporay nodes are not reclaimed */ 718 ieee80211_ref_node(ni); 719 if (ni->ni_associd != 0) 720 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason); 721 ieee80211_node_leave(ni); 722 ieee80211_free_node(ni); 723} 724 725static struct ieee80211_node * 726node_alloc(struct ieee80211_node_table *nt) 727{ 728 struct ieee80211_node *ni; 729 730 MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node), 731 M_80211_NODE, M_NOWAIT | M_ZERO); 732 return ni; 733} 734 735/* 736 * Initialize an ie blob with the specified data. If previous 737 * data exists re-use the data block. As a side effect we clear 738 * all references to specific ie's; the caller is required to 739 * recalculate them. 740 */ 741int 742ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len) 743{ 744 /* NB: assumes data+len are the last fields */ 745 memset(ies, 0, offsetof(struct ieee80211_ies, data)); 746 if (ies->data != NULL && ies->len != len) { 747 /* data size changed */ 748 FREE(ies->data, M_80211_NODE_IE); 749 ies->data = NULL; 750 } 751 if (ies->data == NULL) { 752 MALLOC(ies->data, uint8_t *, len, M_80211_NODE_IE, M_NOWAIT); 753 if (ies->data == NULL) { 754 ies->len = 0; 755 /* NB: pointers have already been zero'd above */ 756 return 0; 757 } 758 } 759 memcpy(ies->data, data, len); 760 ies->len = len; 761 return 1; 762} 763 764/* 765 * Reclaim storage for an ie blob. 766 */ 767void 768ieee80211_ies_cleanup(struct ieee80211_ies *ies) 769{ 770 if (ies->data != NULL) 771 FREE(ies->data, M_80211_NODE_IE); 772} 773 774/* 775 * Expand an ie blob data contents and to fillin individual 776 * ie pointers. The data blob is assumed to be well-formed; 777 * we don't do any validity checking of ie lengths. 778 */ 779void 780ieee80211_ies_expand(struct ieee80211_ies *ies) 781{ 782 uint8_t *ie; 783 int ielen; 784 785 ie = ies->data; 786 ielen = ies->len; 787 while (ielen > 0) { 788 switch (ie[0]) { 789 case IEEE80211_ELEMID_VENDOR: 790 if (iswpaoui(ie)) 791 ies->wpa_ie = ie; 792 else if (iswmeoui(ie)) 793 ies->wme_ie = ie; 794 else if (isatherosoui(ie)) 795 ies->ath_ie = ie; 796 break; 797 case IEEE80211_ELEMID_RSN: 798 ies->rsn_ie = ie; 799 break; 800 case IEEE80211_ELEMID_HTCAP: 801 ies->htcap_ie = ie; 802 break; 803 } 804 ielen -= 2 + ie[1]; 805 ie += 2 + ie[1]; 806 } 807} 808 809/* 810 * Reclaim any resources in a node and reset any critical 811 * state. Typically nodes are free'd immediately after, 812 * but in some cases the storage may be reused so we need 813 * to insure consistent state (should probably fix that). 814 */ 815static void 816node_cleanup(struct ieee80211_node *ni) 817{ 818#define N(a) (sizeof(a)/sizeof(a[0])) 819 struct ieee80211vap *vap = ni->ni_vap; 820 int i; 821 822 /* NB: preserve ni_table */ 823 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 824 if (vap->iv_opmode != IEEE80211_M_STA) 825 vap->iv_ps_sta--; 826 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 827 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 828 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta); 829 } 830 /* 831 * Cleanup any HT-related state. 832 */ 833 if (ni->ni_flags & IEEE80211_NODE_HT) 834 ieee80211_ht_node_cleanup(ni); 835 /* 836 * Clear AREF flag that marks the authorization refcnt bump 837 * has happened. This is probably not needed as the node 838 * should always be removed from the table so not found but 839 * do it just in case. 840 */ 841 ni->ni_flags &= ~IEEE80211_NODE_AREF; 842 843 /* 844 * Drain power save queue and, if needed, clear TIM. 845 */ 846 if (ieee80211_node_saveq_drain(ni) != 0 && vap->iv_set_tim != NULL) 847 vap->iv_set_tim(ni, 0); 848 849 ni->ni_associd = 0; 850 if (ni->ni_challenge != NULL) { 851 FREE(ni->ni_challenge, M_80211_NODE); 852 ni->ni_challenge = NULL; 853 } 854 /* 855 * Preserve SSID, WPA, and WME ie's so the bss node is 856 * reusable during a re-auth/re-assoc state transition. 857 * If we remove these data they will not be recreated 858 * because they come from a probe-response or beacon frame 859 * which cannot be expected prior to the association-response. 860 * This should not be an issue when operating in other modes 861 * as stations leaving always go through a full state transition 862 * which will rebuild this state. 863 * 864 * XXX does this leave us open to inheriting old state? 865 */ 866 for (i = 0; i < N(ni->ni_rxfrag); i++) 867 if (ni->ni_rxfrag[i] != NULL) { 868 m_freem(ni->ni_rxfrag[i]); 869 ni->ni_rxfrag[i] = NULL; 870 } 871 /* 872 * Must be careful here to remove any key map entry w/o a LOR. 873 */ 874 ieee80211_node_delucastkey(ni); 875#undef N 876} 877 878static void 879node_free(struct ieee80211_node *ni) 880{ 881 struct ieee80211com *ic = ni->ni_ic; 882 883 ic->ic_node_cleanup(ni); 884 ieee80211_ies_cleanup(&ni->ni_ies); 885 IEEE80211_NODE_SAVEQ_DESTROY(ni); 886 IEEE80211_NODE_WDSQ_DESTROY(ni); 887 FREE(ni, M_80211_NODE); 888} 889 890static void 891node_age(struct ieee80211_node *ni) 892{ 893 struct ieee80211vap *vap = ni->ni_vap; 894#if 0 895 IEEE80211_NODE_LOCK_ASSERT(&ic->ic_sta); 896#endif 897 /* 898 * Age frames on the power save queue. 899 */ 900 if (ieee80211_node_saveq_age(ni) != 0 && 901 IEEE80211_NODE_SAVEQ_QLEN(ni) == 0 && 902 vap->iv_set_tim != NULL) 903 vap->iv_set_tim(ni, 0); 904 /* 905 * Age frames on the wds pending queue. 906 */ 907 if (IEEE80211_NODE_WDSQ_QLEN(ni) != 0) 908 ieee80211_node_wdsq_age(ni); 909 /* 910 * Age out HT resources (e.g. frames on the 911 * A-MPDU reorder queues). 912 */ 913 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT)) 914 ieee80211_ht_node_age(ni); 915} 916 917static int8_t 918node_getrssi(const struct ieee80211_node *ni) 919{ 920 uint32_t avgrssi = ni->ni_avgrssi; 921 int32_t rssi; 922 923 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) 924 return 0; 925 rssi = IEEE80211_RSSI_GET(avgrssi); 926 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi; 927} 928 929static void 930node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise) 931{ 932 *rssi = node_getrssi(ni); 933 *noise = ni->ni_noise; 934} 935 936static void 937node_getmimoinfo(const struct ieee80211_node *ni, 938 struct ieee80211_mimo_info *info) 939{ 940 /* XXX zero data? */ 941} 942 943struct ieee80211_node * 944ieee80211_alloc_node(struct ieee80211_node_table *nt, 945 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN]) 946{ 947 struct ieee80211com *ic = nt->nt_ic; 948 struct ieee80211_node *ni; 949 int hash; 950 951 ni = ic->ic_node_alloc(nt); 952 if (ni == NULL) { 953 vap->iv_stats.is_rx_nodealloc++; 954 return NULL; 955 } 956 957 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 958 "%s %p<%s> in %s table\n", __func__, ni, 959 ether_sprintf(macaddr), nt->nt_name); 960 961 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 962 hash = IEEE80211_NODE_HASH(macaddr); 963 ieee80211_node_initref(ni); /* mark referenced */ 964 ni->ni_chan = IEEE80211_CHAN_ANYC; 965 ni->ni_authmode = IEEE80211_AUTH_OPEN; 966 ni->ni_txpower = ic->ic_txpowlimit; /* max power */ 967 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE); 968 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER; 969 ni->ni_inact_reload = nt->nt_inact_init; 970 ni->ni_inact = ni->ni_inact_reload; 971 ni->ni_ath_defkeyix = 0x7fff; 972 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown"); 973 IEEE80211_NODE_WDSQ_INIT(ni, "unknown"); 974 975 IEEE80211_NODE_LOCK(nt); 976 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list); 977 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash); 978 ni->ni_table = nt; 979 ni->ni_vap = vap; 980 ni->ni_ic = ic; 981 IEEE80211_NODE_UNLOCK(nt); 982 983 return ni; 984} 985 986/* 987 * Craft a temporary node suitable for sending a management frame 988 * to the specified station. We craft only as much state as we 989 * need to do the work since the node will be immediately reclaimed 990 * once the send completes. 991 */ 992struct ieee80211_node * 993ieee80211_tmp_node(struct ieee80211vap *vap, 994 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 995{ 996 struct ieee80211com *ic = vap->iv_ic; 997 struct ieee80211_node *ni; 998 999 ni = ic->ic_node_alloc(&ic->ic_sta); 1000 if (ni != NULL) { 1001 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1002 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr)); 1003 1004 ni->ni_table = NULL; /* NB: pedantic */ 1005 ni->ni_ic = ic; /* NB: needed to set channel */ 1006 ni->ni_vap = vap; 1007 1008 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 1009 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_bss->ni_bssid); 1010 ieee80211_node_initref(ni); /* mark referenced */ 1011 /* NB: required by ieee80211_fix_rate */ 1012 ieee80211_node_set_chan(ni, vap->iv_bss->ni_chan); 1013 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, 1014 IEEE80211_KEYIX_NONE); 1015 /* XXX optimize away */ 1016 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown"); 1017 IEEE80211_NODE_WDSQ_INIT(ni, "unknown"); 1018 } else { 1019 /* XXX msg */ 1020 vap->iv_stats.is_rx_nodealloc++; 1021 } 1022 return ni; 1023} 1024 1025struct ieee80211_node * 1026ieee80211_dup_bss(struct ieee80211vap *vap, 1027 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1028{ 1029 struct ieee80211com *ic = vap->iv_ic; 1030 struct ieee80211_node *ni; 1031 1032 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr); 1033 if (ni != NULL) { 1034 /* 1035 * Inherit from iv_bss. 1036 */ 1037 ni->ni_authmode = vap->iv_bss->ni_authmode; 1038 ni->ni_vlan = vap->iv_bss->ni_vlan; /* XXX?? */ 1039 IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_bss->ni_bssid); 1040 ieee80211_node_set_chan(ni, vap->iv_bss->ni_chan); 1041 } 1042 return ni; 1043} 1044 1045/* 1046 * Create a bss node for a legacy WDS vap. The far end does 1047 * not associate so we just create create a new node and 1048 * simulate an association. The caller is responsible for 1049 * installing the node as the bss node and handling any further 1050 * setup work like authorizing the port. 1051 */ 1052struct ieee80211_node * 1053ieee80211_node_create_wds(struct ieee80211vap *vap, 1054 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan) 1055{ 1056 struct ieee80211com *ic = vap->iv_ic; 1057 struct ieee80211_node *ni; 1058 1059 /* XXX check if node already in sta table? */ 1060 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid); 1061 if (ni != NULL) { 1062 ni->ni_wdsvap = vap; 1063 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid); 1064 /* 1065 * Inherit any manually configured settings. 1066 */ 1067 ni->ni_authmode = vap->iv_bss->ni_authmode; 1068 ni->ni_vlan = vap->iv_bss->ni_vlan; 1069 ieee80211_node_set_chan(ni, chan); 1070 /* NB: propagate ssid so available to WPA supplicant */ 1071 ni->ni_esslen = vap->iv_des_ssid[0].len; 1072 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen); 1073 /* NB: no associd for peer */ 1074 /* 1075 * There are no management frames to use to 1076 * discover neighbor capabilities, so blindly 1077 * propagate the local configuration. 1078 */ 1079 if (vap->iv_flags & IEEE80211_F_WME) 1080 ni->ni_flags |= IEEE80211_NODE_QOS; 1081 if (vap->iv_flags & IEEE80211_F_FF) 1082 ni->ni_flags |= IEEE80211_NODE_FF; 1083 if ((ic->ic_htcaps & IEEE80211_HTC_HT) && 1084 (vap->iv_flags_ext & IEEE80211_FEXT_HT)) { 1085 /* 1086 * Device is HT-capable and HT is enabled for 1087 * the vap; setup HT operation. On return 1088 * ni_chan will be adjusted to an HT channel. 1089 */ 1090 ieee80211_ht_wds_init(ni); 1091 } else { 1092 struct ieee80211_channel *c = ni->ni_chan; 1093 /* 1094 * Force a legacy channel to be used. 1095 */ 1096 c = ieee80211_find_channel(ic, 1097 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT); 1098 KASSERT(c != NULL, ("no legacy channel, %u/%x", 1099 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags)); 1100 ni->ni_chan = c; 1101 } 1102 } 1103 return ni; 1104} 1105 1106struct ieee80211_node * 1107#ifdef IEEE80211_DEBUG_REFCNT 1108ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt, 1109 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1110#else 1111ieee80211_find_node_locked(struct ieee80211_node_table *nt, 1112 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1113#endif 1114{ 1115 struct ieee80211_node *ni; 1116 int hash; 1117 1118 IEEE80211_NODE_LOCK_ASSERT(nt); 1119 1120 hash = IEEE80211_NODE_HASH(macaddr); 1121 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1122 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 1123 ieee80211_ref_node(ni); /* mark referenced */ 1124#ifdef IEEE80211_DEBUG_REFCNT 1125 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1126 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1127 func, line, 1128 ni, ether_sprintf(ni->ni_macaddr), 1129 ieee80211_node_refcnt(ni)); 1130#endif 1131 return ni; 1132 } 1133 } 1134 return NULL; 1135} 1136 1137struct ieee80211_node * 1138#ifdef IEEE80211_DEBUG_REFCNT 1139ieee80211_find_node_debug(struct ieee80211_node_table *nt, 1140 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1141#else 1142ieee80211_find_node(struct ieee80211_node_table *nt, 1143 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1144#endif 1145{ 1146 struct ieee80211_node *ni; 1147 1148 IEEE80211_NODE_LOCK(nt); 1149 ni = ieee80211_find_node_locked(nt, macaddr); 1150 IEEE80211_NODE_UNLOCK(nt); 1151 return ni; 1152} 1153 1154struct ieee80211_node * 1155#ifdef IEEE80211_DEBUG_REFCNT 1156ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt, 1157 const struct ieee80211vap *vap, 1158 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1159#else 1160ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt, 1161 const struct ieee80211vap *vap, 1162 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1163#endif 1164{ 1165 struct ieee80211_node *ni; 1166 int hash; 1167 1168 IEEE80211_NODE_LOCK_ASSERT(nt); 1169 1170 hash = IEEE80211_NODE_HASH(macaddr); 1171 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1172 if (ni->ni_vap == vap && 1173 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 1174 ieee80211_ref_node(ni); /* mark referenced */ 1175#ifdef IEEE80211_DEBUG_REFCNT 1176 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1177 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1178 func, line, 1179 ni, ether_sprintf(ni->ni_macaddr), 1180 ieee80211_node_refcnt(ni)); 1181#endif 1182 return ni; 1183 } 1184 } 1185 return NULL; 1186} 1187 1188struct ieee80211_node * 1189#ifdef IEEE80211_DEBUG_REFCNT 1190ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt, 1191 const struct ieee80211vap *vap, 1192 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1193#else 1194ieee80211_find_vap_node(struct ieee80211_node_table *nt, 1195 const struct ieee80211vap *vap, 1196 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1197#endif 1198{ 1199 struct ieee80211_node *ni; 1200 1201 IEEE80211_NODE_LOCK(nt); 1202 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr); 1203 IEEE80211_NODE_UNLOCK(nt); 1204 return ni; 1205} 1206 1207/* 1208 * Fake up a node; this handles node discovery in adhoc mode. 1209 * Note that for the driver's benefit we we treat this like 1210 * an association so the driver has an opportunity to setup 1211 * it's private state. 1212 */ 1213struct ieee80211_node * 1214ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap, 1215 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1216{ 1217 struct ieee80211_node *ni; 1218 1219 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1220 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr)); 1221 ni = ieee80211_dup_bss(vap, macaddr); 1222 if (ni != NULL) { 1223 struct ieee80211com *ic = vap->iv_ic; 1224 1225 /* XXX no rate negotiation; just dup */ 1226 ni->ni_rates = vap->iv_bss->ni_rates; 1227 if (vap->iv_opmode == IEEE80211_M_AHDEMO) { 1228 /* 1229 * In adhoc demo mode there are no management 1230 * frames to use to discover neighbor capabilities, 1231 * so blindly propagate the local configuration 1232 * so we can do interesting things (e.g. use 1233 * WME to disable ACK's). 1234 */ 1235 if (vap->iv_flags & IEEE80211_F_WME) 1236 ni->ni_flags |= IEEE80211_NODE_QOS; 1237 if (vap->iv_flags & IEEE80211_F_FF) 1238 ni->ni_flags |= IEEE80211_NODE_FF; 1239 } 1240 if (ic->ic_newassoc != NULL) 1241 ic->ic_newassoc(ni, 1); 1242 /* XXX not right for 802.1x/WPA */ 1243 ieee80211_node_authorize(ni); 1244 } 1245 return ni; 1246} 1247 1248void 1249ieee80211_init_neighbor(struct ieee80211_node *ni, 1250 const struct ieee80211_frame *wh, 1251 const struct ieee80211_scanparams *sp) 1252{ 1253 ni->ni_esslen = sp->ssid[1]; 1254 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]); 1255 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3); 1256 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp)); 1257 ni->ni_intval = sp->bintval; 1258 ni->ni_capinfo = sp->capinfo; 1259 ni->ni_chan = ni->ni_ic->ic_curchan; 1260 ni->ni_fhdwell = sp->fhdwell; 1261 ni->ni_fhindex = sp->fhindex; 1262 ni->ni_erp = sp->erp; 1263 ni->ni_timoff = sp->timoff; 1264 1265 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) { 1266 ieee80211_ies_expand(&ni->ni_ies); 1267 if (ni->ni_ies.ath_ie != NULL) 1268 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie); 1269 } 1270 1271 /* NB: must be after ni_chan is setup */ 1272 ieee80211_setup_rates(ni, sp->rates, sp->xrates, 1273 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | 1274 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1275} 1276 1277/* 1278 * Do node discovery in adhoc mode on receipt of a beacon 1279 * or probe response frame. Note that for the driver's 1280 * benefit we we treat this like an association so the 1281 * driver has an opportunity to setup it's private state. 1282 */ 1283struct ieee80211_node * 1284ieee80211_add_neighbor(struct ieee80211vap *vap, 1285 const struct ieee80211_frame *wh, 1286 const struct ieee80211_scanparams *sp) 1287{ 1288 struct ieee80211_node *ni; 1289 1290 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1291 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2)); 1292 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */ 1293 if (ni != NULL) { 1294 struct ieee80211com *ic = vap->iv_ic; 1295 1296 ieee80211_init_neighbor(ni, wh, sp); 1297 if (ic->ic_newassoc != NULL) 1298 ic->ic_newassoc(ni, 1); 1299 /* XXX not right for 802.1x/WPA */ 1300 ieee80211_node_authorize(ni); 1301 } 1302 return ni; 1303} 1304 1305#define IS_CTL(wh) \ 1306 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) 1307#define IS_PSPOLL(wh) \ 1308 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL) 1309#define IS_BAR(wh) \ 1310 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR) 1311#define IS_PROBEREQ(wh) \ 1312 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \ 1313 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ)) 1314#define IS_BCAST_PROBEREQ(wh) \ 1315 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \ 1316 ((const struct ieee80211_frame *)(wh))->i_addr3)) 1317 1318static __inline struct ieee80211_node * 1319_find_rxnode(struct ieee80211_node_table *nt, 1320 const struct ieee80211_frame_min *wh) 1321{ 1322 /* XXX 4-address frames? */ 1323 if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/) 1324 return ieee80211_find_node_locked(nt, wh->i_addr1); 1325 if (IS_BCAST_PROBEREQ(wh)) 1326 return NULL; /* spam bcast probe req to all vap's */ 1327 return ieee80211_find_node_locked(nt, wh->i_addr2); 1328} 1329 1330/* 1331 * Locate the node for sender, track state, and then pass the 1332 * (referenced) node up to the 802.11 layer for its use. Note 1333 * we can return NULL if the sender is not in the table. 1334 */ 1335struct ieee80211_node * 1336#ifdef IEEE80211_DEBUG_REFCNT 1337ieee80211_find_rxnode_debug(struct ieee80211com *ic, 1338 const struct ieee80211_frame_min *wh, const char *func, int line) 1339#else 1340ieee80211_find_rxnode(struct ieee80211com *ic, 1341 const struct ieee80211_frame_min *wh) 1342#endif 1343{ 1344 struct ieee80211_node_table *nt; 1345 struct ieee80211_node *ni; 1346 1347 nt = &ic->ic_sta; 1348 IEEE80211_NODE_LOCK(nt); 1349 ni = _find_rxnode(nt, wh); 1350 IEEE80211_NODE_UNLOCK(nt); 1351 1352 return ni; 1353} 1354 1355/* 1356 * Like ieee80211_find_rxnode but use the supplied h/w 1357 * key index as a hint to locate the node in the key 1358 * mapping table. If an entry is present at the key 1359 * index we return it; otherwise do a normal lookup and 1360 * update the mapping table if the station has a unicast 1361 * key assigned to it. 1362 */ 1363struct ieee80211_node * 1364#ifdef IEEE80211_DEBUG_REFCNT 1365ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic, 1366 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix, 1367 const char *func, int line) 1368#else 1369ieee80211_find_rxnode_withkey(struct ieee80211com *ic, 1370 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix) 1371#endif 1372{ 1373 struct ieee80211_node_table *nt; 1374 struct ieee80211_node *ni; 1375 1376 nt = &ic->ic_sta; 1377 IEEE80211_NODE_LOCK(nt); 1378 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) 1379 ni = nt->nt_keyixmap[keyix]; 1380 else 1381 ni = NULL; 1382 if (ni == NULL) { 1383 ni = _find_rxnode(nt, wh); 1384 if (ni != NULL && nt->nt_keyixmap != NULL) { 1385 /* 1386 * If the station has a unicast key cache slot 1387 * assigned update the key->node mapping table. 1388 */ 1389 keyix = ni->ni_ucastkey.wk_rxkeyix; 1390 /* XXX can keyixmap[keyix] != NULL? */ 1391 if (keyix < nt->nt_keyixmax && 1392 nt->nt_keyixmap[keyix] == NULL) { 1393 IEEE80211_DPRINTF(ni->ni_vap, 1394 IEEE80211_MSG_NODE, 1395 "%s: add key map entry %p<%s> refcnt %d\n", 1396 __func__, ni, ether_sprintf(ni->ni_macaddr), 1397 ieee80211_node_refcnt(ni)+1); 1398 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni); 1399 } 1400 } 1401 } else { 1402 if (IS_BCAST_PROBEREQ(wh)) 1403 ni = NULL; /* spam bcast probe req to all vap's */ 1404 else 1405 ieee80211_ref_node(ni); 1406 } 1407 IEEE80211_NODE_UNLOCK(nt); 1408 1409 return ni; 1410} 1411#undef IS_BCAST_PROBEREQ 1412#undef IS_PROBEREQ 1413#undef IS_BAR 1414#undef IS_PSPOLL 1415#undef IS_CTL 1416 1417/* 1418 * Return a reference to the appropriate node for sending 1419 * a data frame. This handles node discovery in adhoc networks. 1420 */ 1421struct ieee80211_node * 1422#ifdef IEEE80211_DEBUG_REFCNT 1423ieee80211_find_txnode_debug(struct ieee80211vap *vap, 1424 const uint8_t macaddr[IEEE80211_ADDR_LEN], 1425 const char *func, int line) 1426#else 1427ieee80211_find_txnode(struct ieee80211vap *vap, 1428 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1429#endif 1430{ 1431 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta; 1432 struct ieee80211_node *ni; 1433 1434 /* 1435 * The destination address should be in the node table 1436 * unless this is a multicast/broadcast frame. We can 1437 * also optimize station mode operation, all frames go 1438 * to the bss node. 1439 */ 1440 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */ 1441 IEEE80211_NODE_LOCK(nt); 1442 if (vap->iv_opmode == IEEE80211_M_STA || 1443 vap->iv_opmode == IEEE80211_M_WDS || 1444 IEEE80211_IS_MULTICAST(macaddr)) 1445 ni = ieee80211_ref_node(vap->iv_bss); 1446 else { 1447 ni = ieee80211_find_node_locked(nt, macaddr); 1448 if (vap->iv_opmode == IEEE80211_M_HOSTAP && 1449 (ni != NULL && ni->ni_associd == 0)) { 1450 /* 1451 * Station is not associated; don't permit the 1452 * data frame to be sent by returning NULL. This 1453 * is kinda a kludge but the least intrusive way 1454 * to add this check into all drivers. 1455 */ 1456 ieee80211_unref_node(&ni); /* NB: null's ni */ 1457 } 1458 } 1459 IEEE80211_NODE_UNLOCK(nt); 1460 1461 if (ni == NULL) { 1462 if (vap->iv_opmode == IEEE80211_M_IBSS || 1463 vap->iv_opmode == IEEE80211_M_AHDEMO) { 1464 /* 1465 * In adhoc mode cons up a node for the destination. 1466 * Note that we need an additional reference for the 1467 * caller to be consistent with 1468 * ieee80211_find_node_locked. 1469 */ 1470 ni = ieee80211_fakeup_adhoc_node(vap, macaddr); 1471 if (ni != NULL) 1472 (void) ieee80211_ref_node(ni); 1473 } else { 1474 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr, 1475 "no node, discard frame (%s)", __func__); 1476 vap->iv_stats.is_tx_nonode++; 1477 } 1478 } 1479 return ni; 1480} 1481 1482static void 1483_ieee80211_free_node(struct ieee80211_node *ni) 1484{ 1485 struct ieee80211_node_table *nt = ni->ni_table; 1486 1487 /* 1488 * NB: careful about referencing the vap as it may be 1489 * gone if the last reference was held by a driver. 1490 * We know the com will always be present so it's safe 1491 * to use ni_ic below to reclaim resources. 1492 */ 1493#if 0 1494 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1495 "%s %p<%s> in %s table\n", __func__, ni, 1496 ether_sprintf(ni->ni_macaddr), 1497 nt != NULL ? nt->nt_name : "<gone>"); 1498#endif 1499 if (ni->ni_associd != 0) { 1500 struct ieee80211vap *vap = ni->ni_vap; 1501 if (vap->iv_aid_bitmap != NULL) 1502 IEEE80211_AID_CLR(vap, ni->ni_associd); 1503 } 1504 if (nt != NULL) { 1505 TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1506 LIST_REMOVE(ni, ni_hash); 1507 } 1508 ni->ni_ic->ic_node_free(ni); 1509} 1510 1511void 1512#ifdef IEEE80211_DEBUG_REFCNT 1513ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line) 1514#else 1515ieee80211_free_node(struct ieee80211_node *ni) 1516#endif 1517{ 1518 struct ieee80211_node_table *nt = ni->ni_table; 1519 1520#ifdef IEEE80211_DEBUG_REFCNT 1521 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1522 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 1523 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1524#endif 1525 if (nt != NULL) { 1526 IEEE80211_NODE_LOCK(nt); 1527 if (ieee80211_node_dectestref(ni)) { 1528 /* 1529 * Last reference, reclaim state. 1530 */ 1531 _ieee80211_free_node(ni); 1532 } else if (ieee80211_node_refcnt(ni) == 1 && 1533 nt->nt_keyixmap != NULL) { 1534 ieee80211_keyix keyix; 1535 /* 1536 * Check for a last reference in the key mapping table. 1537 */ 1538 keyix = ni->ni_ucastkey.wk_rxkeyix; 1539 if (keyix < nt->nt_keyixmax && 1540 nt->nt_keyixmap[keyix] == ni) { 1541 IEEE80211_DPRINTF(ni->ni_vap, 1542 IEEE80211_MSG_NODE, 1543 "%s: %p<%s> clear key map entry", __func__, 1544 ni, ether_sprintf(ni->ni_macaddr)); 1545 nt->nt_keyixmap[keyix] = NULL; 1546 ieee80211_node_decref(ni); /* XXX needed? */ 1547 _ieee80211_free_node(ni); 1548 } 1549 } 1550 IEEE80211_NODE_UNLOCK(nt); 1551 } else { 1552 if (ieee80211_node_dectestref(ni)) 1553 _ieee80211_free_node(ni); 1554 } 1555} 1556 1557/* 1558 * Reclaim a unicast key and clear any key cache state. 1559 */ 1560int 1561ieee80211_node_delucastkey(struct ieee80211_node *ni) 1562{ 1563 struct ieee80211com *ic = ni->ni_ic; 1564 struct ieee80211_node_table *nt = &ic->ic_sta; 1565 struct ieee80211_node *nikey; 1566 ieee80211_keyix keyix; 1567 int isowned, status; 1568 1569 /* 1570 * NB: We must beware of LOR here; deleting the key 1571 * can cause the crypto layer to block traffic updates 1572 * which can generate a LOR against the node table lock; 1573 * grab it here and stash the key index for our use below. 1574 * 1575 * Must also beware of recursion on the node table lock. 1576 * When called from node_cleanup we may already have 1577 * the node table lock held. Unfortunately there's no 1578 * way to separate out this path so we must do this 1579 * conditionally. 1580 */ 1581 isowned = IEEE80211_NODE_IS_LOCKED(nt); 1582 if (!isowned) 1583 IEEE80211_NODE_LOCK(nt); 1584 nikey = NULL; 1585 status = 1; /* NB: success */ 1586 if (!IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) { 1587 keyix = ni->ni_ucastkey.wk_rxkeyix; 1588 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey); 1589 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) { 1590 nikey = nt->nt_keyixmap[keyix]; 1591 nt->nt_keyixmap[keyix] = NULL;; 1592 } 1593 } 1594 if (!isowned) 1595 IEEE80211_NODE_UNLOCK(nt); 1596 1597 if (nikey != NULL) { 1598 KASSERT(nikey == ni, 1599 ("key map out of sync, ni %p nikey %p", ni, nikey)); 1600 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1601 "%s: delete key map entry %p<%s> refcnt %d\n", 1602 __func__, ni, ether_sprintf(ni->ni_macaddr), 1603 ieee80211_node_refcnt(ni)-1); 1604 ieee80211_free_node(ni); 1605 } 1606 return status; 1607} 1608 1609/* 1610 * Reclaim a node. If this is the last reference count then 1611 * do the normal free work. Otherwise remove it from the node 1612 * table and mark it gone by clearing the back-reference. 1613 */ 1614static void 1615node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1616{ 1617 ieee80211_keyix keyix; 1618 1619 IEEE80211_NODE_LOCK_ASSERT(nt); 1620 1621 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1622 "%s: remove %p<%s> from %s table, refcnt %d\n", 1623 __func__, ni, ether_sprintf(ni->ni_macaddr), 1624 nt->nt_name, ieee80211_node_refcnt(ni)-1); 1625 /* 1626 * Clear any entry in the unicast key mapping table. 1627 * We need to do it here so rx lookups don't find it 1628 * in the mapping table even if it's not in the hash 1629 * table. We cannot depend on the mapping table entry 1630 * being cleared because the node may not be free'd. 1631 */ 1632 keyix = ni->ni_ucastkey.wk_rxkeyix; 1633 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax && 1634 nt->nt_keyixmap[keyix] == ni) { 1635 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1636 "%s: %p<%s> clear key map entry\n", 1637 __func__, ni, ether_sprintf(ni->ni_macaddr)); 1638 nt->nt_keyixmap[keyix] = NULL; 1639 ieee80211_node_decref(ni); /* NB: don't need free */ 1640 } 1641 if (!ieee80211_node_dectestref(ni)) { 1642 /* 1643 * Other references are present, just remove the 1644 * node from the table so it cannot be found. When 1645 * the references are dropped storage will be 1646 * reclaimed. 1647 */ 1648 TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1649 LIST_REMOVE(ni, ni_hash); 1650 ni->ni_table = NULL; /* clear reference */ 1651 } else 1652 _ieee80211_free_node(ni); 1653} 1654 1655/* 1656 * Reclaim a (bss) node. Decrement the refcnt and reclaim 1657 * the node if the only other reference to it is in the sta 1658 * table. This is effectively ieee80211_free_node followed 1659 * by node_reclaim when the refcnt is 1 (after the free). 1660 */ 1661static void 1662ieee80211_node_reclaim(struct ieee80211_node *ni) 1663{ 1664 struct ieee80211_node_table *nt = ni->ni_table; 1665 1666 KASSERT(nt != NULL, ("reclaim node not in table")); 1667 1668#ifdef IEEE80211_DEBUG_REFCNT 1669 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1670 "%s %p<%s> refcnt %d\n", __func__, ni, 1671 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1672#endif 1673 IEEE80211_NODE_LOCK(nt); 1674 if (ieee80211_node_dectestref(ni)) { 1675 /* 1676 * Last reference, reclaim state. 1677 */ 1678 _ieee80211_free_node(ni); 1679 nt = NULL; 1680 } else if (ieee80211_node_refcnt(ni) == 1 && 1681 nt->nt_keyixmap != NULL) { 1682 ieee80211_keyix keyix; 1683 /* 1684 * Check for a last reference in the key mapping table. 1685 */ 1686 keyix = ni->ni_ucastkey.wk_rxkeyix; 1687 if (keyix < nt->nt_keyixmax && 1688 nt->nt_keyixmap[keyix] == ni) { 1689 IEEE80211_DPRINTF(ni->ni_vap, 1690 IEEE80211_MSG_NODE, 1691 "%s: %p<%s> clear key map entry", __func__, 1692 ni, ether_sprintf(ni->ni_macaddr)); 1693 nt->nt_keyixmap[keyix] = NULL; 1694 ieee80211_node_decref(ni); /* XXX needed? */ 1695 _ieee80211_free_node(ni); 1696 nt = NULL; 1697 } 1698 } 1699 if (nt != NULL && ieee80211_node_refcnt(ni) == 1) { 1700 /* 1701 * Last reference is in the sta table; complete 1702 * the reclaim. This handles bss nodes being 1703 * recycled: the node has two references, one for 1704 * iv_bss and one for the table. After dropping 1705 * the iv_bss ref above we need to reclaim the sta 1706 * table reference. 1707 */ 1708 ieee80211_node_decref(ni); /* NB: be pendantic */ 1709 _ieee80211_free_node(ni); 1710 } 1711 IEEE80211_NODE_UNLOCK(nt); 1712} 1713 1714/* 1715 * Node table support. 1716 */ 1717 1718static void 1719ieee80211_node_table_init(struct ieee80211com *ic, 1720 struct ieee80211_node_table *nt, 1721 const char *name, int inact, int keyixmax) 1722{ 1723 struct ifnet *ifp = ic->ic_ifp; 1724 1725 nt->nt_ic = ic; 1726 IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname); 1727 IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname); 1728 TAILQ_INIT(&nt->nt_node); 1729 nt->nt_name = name; 1730 nt->nt_scangen = 1; 1731 nt->nt_inact_init = inact; 1732 nt->nt_keyixmax = keyixmax; 1733 if (nt->nt_keyixmax > 0) { 1734 MALLOC(nt->nt_keyixmap, struct ieee80211_node **, 1735 keyixmax * sizeof(struct ieee80211_node *), 1736 M_80211_NODE, M_NOWAIT | M_ZERO); 1737 if (nt->nt_keyixmap == NULL) 1738 if_printf(ic->ic_ifp, 1739 "Cannot allocate key index map with %u entries\n", 1740 keyixmax); 1741 } else 1742 nt->nt_keyixmap = NULL; 1743} 1744 1745static void 1746ieee80211_node_table_reset(struct ieee80211_node_table *nt, 1747 struct ieee80211vap *match) 1748{ 1749 struct ieee80211_node *ni, *next; 1750 1751 IEEE80211_NODE_LOCK(nt); 1752 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) { 1753 if (match != NULL && ni->ni_vap != match) 1754 continue; 1755 /* XXX can this happen? if so need's work */ 1756 if (ni->ni_associd != 0) { 1757 struct ieee80211vap *vap = ni->ni_vap; 1758 1759 if (vap->iv_auth->ia_node_leave != NULL) 1760 vap->iv_auth->ia_node_leave(ni); 1761 if (vap->iv_aid_bitmap != NULL) 1762 IEEE80211_AID_CLR(vap, ni->ni_associd); 1763 } 1764 ni->ni_wdsvap = NULL; /* clear reference */ 1765 node_reclaim(nt, ni); 1766 } 1767 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) { 1768 /* 1769 * Make a separate pass to clear references to this vap 1770 * held by DWDS entries. They will not be matched above 1771 * because ni_vap will point to the ap vap but we still 1772 * need to clear ni_wdsvap when the WDS vap is destroyed 1773 * and/or reset. 1774 */ 1775 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) 1776 if (ni->ni_wdsvap == match) 1777 ni->ni_wdsvap = NULL; 1778 } 1779 IEEE80211_NODE_UNLOCK(nt); 1780} 1781 1782static void 1783ieee80211_node_table_cleanup(struct ieee80211_node_table *nt) 1784{ 1785 ieee80211_node_table_reset(nt, NULL); 1786 if (nt->nt_keyixmap != NULL) { 1787#ifdef DIAGNOSTIC 1788 /* XXX verify all entries are NULL */ 1789 int i; 1790 for (i = 0; i < nt->nt_keyixmax; i++) 1791 if (nt->nt_keyixmap[i] != NULL) 1792 printf("%s: %s[%u] still active\n", __func__, 1793 nt->nt_name, i); 1794#endif 1795 FREE(nt->nt_keyixmap, M_80211_NODE); 1796 nt->nt_keyixmap = NULL; 1797 } 1798 IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt); 1799 IEEE80211_NODE_LOCK_DESTROY(nt); 1800} 1801 1802/* 1803 * Timeout inactive stations and do related housekeeping. 1804 * Note that we cannot hold the node lock while sending a 1805 * frame as this would lead to a LOR. Instead we use a 1806 * generation number to mark nodes that we've scanned and 1807 * drop the lock and restart a scan if we have to time out 1808 * a node. Since we are single-threaded by virtue of 1809 * controlling the inactivity timer we can be sure this will 1810 * process each node only once. 1811 */ 1812static void 1813ieee80211_timeout_stations(struct ieee80211com *ic) 1814{ 1815 struct ieee80211_node_table *nt = &ic->ic_sta; 1816 struct ieee80211vap *vap; 1817 struct ieee80211_node *ni; 1818 int gen = 0; 1819 1820 IEEE80211_NODE_ITERATE_LOCK(nt); 1821 gen = ++nt->nt_scangen; 1822restart: 1823 IEEE80211_NODE_LOCK(nt); 1824 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1825 if (ni->ni_scangen == gen) /* previously handled */ 1826 continue; 1827 ni->ni_scangen = gen; 1828 /* 1829 * Ignore entries for which have yet to receive an 1830 * authentication frame. These are transient and 1831 * will be reclaimed when the last reference to them 1832 * goes away (when frame xmits complete). 1833 */ 1834 vap = ni->ni_vap; 1835 /* 1836 * Only process stations when in RUN state. This 1837 * insures, for example, that we don't timeout an 1838 * inactive station during CAC. Note that CSA state 1839 * is actually handled in ieee80211_node_timeout as 1840 * it applies to more than timeout processing. 1841 */ 1842 if (vap->iv_state != IEEE80211_S_RUN) 1843 continue; 1844 /* XXX can vap be NULL? */ 1845 if ((vap->iv_opmode == IEEE80211_M_HOSTAP || 1846 vap->iv_opmode == IEEE80211_M_STA) && 1847 (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 1848 continue; 1849 /* 1850 * Free fragment if not needed anymore 1851 * (last fragment older than 1s). 1852 * XXX doesn't belong here, move to node_age 1853 */ 1854 if (ni->ni_rxfrag[0] != NULL && 1855 ticks > ni->ni_rxfragstamp + hz) { 1856 m_freem(ni->ni_rxfrag[0]); 1857 ni->ni_rxfrag[0] = NULL; 1858 } 1859 if (ni->ni_inact > 0) 1860 ni->ni_inact--; 1861 /* 1862 * Special case ourself; we may be idle for extended periods 1863 * of time and regardless reclaiming our state is wrong. 1864 * XXX run ic_node_age 1865 */ 1866 if (ni == vap->iv_bss) 1867 continue; 1868 if (ni->ni_associd != 0 || 1869 (vap->iv_opmode == IEEE80211_M_IBSS || 1870 vap->iv_opmode == IEEE80211_M_AHDEMO)) { 1871 /* 1872 * Age/drain resources held by the station. 1873 */ 1874 ic->ic_node_age(ni); 1875 /* 1876 * Probe the station before time it out. We 1877 * send a null data frame which may not be 1878 * universally supported by drivers (need it 1879 * for ps-poll support so it should be...). 1880 * 1881 * XXX don't probe the station unless we've 1882 * received a frame from them (and have 1883 * some idea of the rates they are capable 1884 * of); this will get fixed more properly 1885 * soon with better handling of the rate set. 1886 */ 1887 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) && 1888 (0 < ni->ni_inact && 1889 ni->ni_inact <= vap->iv_inact_probe) && 1890 ni->ni_rates.rs_nrates != 0) { 1891 IEEE80211_NOTE(vap, 1892 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, 1893 ni, "%s", 1894 "probe station due to inactivity"); 1895 /* 1896 * Grab a reference before unlocking the table 1897 * so the node cannot be reclaimed before we 1898 * send the frame. ieee80211_send_nulldata 1899 * understands we've done this and reclaims the 1900 * ref for us as needed. 1901 */ 1902 ieee80211_ref_node(ni); 1903 IEEE80211_NODE_UNLOCK(nt); 1904 ieee80211_send_nulldata(ni); 1905 /* XXX stat? */ 1906 goto restart; 1907 } 1908 } 1909 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) && 1910 ni->ni_inact <= 0) { 1911 IEEE80211_NOTE(vap, 1912 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni, 1913 "station timed out due to inactivity " 1914 "(refcnt %u)", ieee80211_node_refcnt(ni)); 1915 /* 1916 * Send a deauthenticate frame and drop the station. 1917 * This is somewhat complicated due to reference counts 1918 * and locking. At this point a station will typically 1919 * have a reference count of 1. ieee80211_node_leave 1920 * will do a "free" of the node which will drop the 1921 * reference count. But in the meantime a reference 1922 * wil be held by the deauth frame. The actual reclaim 1923 * of the node will happen either after the tx is 1924 * completed or by ieee80211_node_leave. 1925 * 1926 * Separately we must drop the node lock before sending 1927 * in case the driver takes a lock, as this can result 1928 * in a LOR between the node lock and the driver lock. 1929 */ 1930 ieee80211_ref_node(ni); 1931 IEEE80211_NODE_UNLOCK(nt); 1932 if (ni->ni_associd != 0) { 1933 IEEE80211_SEND_MGMT(ni, 1934 IEEE80211_FC0_SUBTYPE_DEAUTH, 1935 IEEE80211_REASON_AUTH_EXPIRE); 1936 } 1937 ieee80211_node_leave(ni); 1938 ieee80211_free_node(ni); 1939 vap->iv_stats.is_node_timeout++; 1940 goto restart; 1941 } 1942 } 1943 IEEE80211_NODE_UNLOCK(nt); 1944 1945 IEEE80211_NODE_ITERATE_UNLOCK(nt); 1946} 1947 1948/* 1949 * Aggressively reclaim resources. This should be used 1950 * only in a critical situation to reclaim mbuf resources. 1951 */ 1952void 1953ieee80211_drain(struct ieee80211com *ic) 1954{ 1955 struct ieee80211_node_table *nt = &ic->ic_sta; 1956 struct ieee80211vap *vap; 1957 struct ieee80211_node *ni; 1958 1959 IEEE80211_NODE_LOCK(nt); 1960 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1961 /* 1962 * Ignore entries for which have yet to receive an 1963 * authentication frame. These are transient and 1964 * will be reclaimed when the last reference to them 1965 * goes away (when frame xmits complete). 1966 */ 1967 vap = ni->ni_vap; 1968 /* 1969 * Only process stations when in RUN state. This 1970 * insures, for example, that we don't timeout an 1971 * inactive station during CAC. Note that CSA state 1972 * is actually handled in ieee80211_node_timeout as 1973 * it applies to more than timeout processing. 1974 */ 1975 if (vap->iv_state != IEEE80211_S_RUN) 1976 continue; 1977 /* XXX can vap be NULL? */ 1978 if ((vap->iv_opmode == IEEE80211_M_HOSTAP || 1979 vap->iv_opmode == IEEE80211_M_STA) && 1980 (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 1981 continue; 1982 /* 1983 * Free fragments. 1984 * XXX doesn't belong here, move to node_drain 1985 */ 1986 if (ni->ni_rxfrag[0] != NULL) { 1987 m_freem(ni->ni_rxfrag[0]); 1988 ni->ni_rxfrag[0] = NULL; 1989 } 1990 /* 1991 * Drain resources held by the station. 1992 */ 1993 ic->ic_node_drain(ni); 1994 } 1995 IEEE80211_NODE_UNLOCK(nt); 1996} 1997 1998/* 1999 * Per-ieee80211com inactivity timer callback. 2000 */ 2001void 2002ieee80211_node_timeout(void *arg) 2003{ 2004 struct ieee80211com *ic = arg; 2005 2006 /* 2007 * Defer timeout processing if a channel switch is pending. 2008 * We typically need to be mute so not doing things that 2009 * might generate frames is good to handle in one place. 2010 * Supressing the station timeout processing may extend the 2011 * lifetime of inactive stations (by not decrementing their 2012 * idle counters) but this should be ok unless the CSA is 2013 * active for an unusually long time. 2014 */ 2015 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) { 2016 ieee80211_scan_timeout(ic); 2017 ieee80211_timeout_stations(ic); 2018 2019 IEEE80211_LOCK(ic); 2020 ieee80211_erp_timeout(ic); 2021 ieee80211_ht_timeout(ic); 2022 IEEE80211_UNLOCK(ic); 2023 } 2024 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz, 2025 ieee80211_node_timeout, ic); 2026} 2027 2028void 2029ieee80211_iterate_nodes(struct ieee80211_node_table *nt, 2030 ieee80211_iter_func *f, void *arg) 2031{ 2032 struct ieee80211_node *ni; 2033 u_int gen; 2034 2035 IEEE80211_NODE_ITERATE_LOCK(nt); 2036 gen = ++nt->nt_scangen; 2037restart: 2038 IEEE80211_NODE_LOCK(nt); 2039 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 2040 if (ni->ni_scangen != gen) { 2041 ni->ni_scangen = gen; 2042 (void) ieee80211_ref_node(ni); 2043 IEEE80211_NODE_UNLOCK(nt); 2044 (*f)(arg, ni); 2045 ieee80211_free_node(ni); 2046 goto restart; 2047 } 2048 } 2049 IEEE80211_NODE_UNLOCK(nt); 2050 2051 IEEE80211_NODE_ITERATE_UNLOCK(nt); 2052} 2053 2054void 2055ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 2056{ 2057 printf("0x%p: mac %s refcnt %d\n", ni, 2058 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 2059 printf("\tscangen %u authmode %u flags 0x%x\n", 2060 ni->ni_scangen, ni->ni_authmode, ni->ni_flags); 2061 printf("\tassocid 0x%x txpower %u vlan %u\n", 2062 ni->ni_associd, ni->ni_txpower, ni->ni_vlan); 2063 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n", 2064 ni->ni_txseqs[IEEE80211_NONQOS_TID], 2065 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT, 2066 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK, 2067 ni->ni_rxfragstamp); 2068 printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n", 2069 ni->ni_rstamp, node_getrssi(ni), ni->ni_noise, 2070 ni->ni_intval, ni->ni_capinfo); 2071 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n", 2072 ether_sprintf(ni->ni_bssid), 2073 ni->ni_esslen, ni->ni_essid, 2074 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags); 2075 printf("\tinact %u txrate %u\n", 2076 ni->ni_inact, ni->ni_txrate); 2077 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n", 2078 ni->ni_htcap, ni->ni_htparam, 2079 ni->ni_htctlchan, ni->ni_ht2ndchan); 2080 printf("\thtopmode %x htstbc %x chw %u\n", 2081 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw); 2082} 2083 2084void 2085ieee80211_dump_nodes(struct ieee80211_node_table *nt) 2086{ 2087 ieee80211_iterate_nodes(nt, 2088 (ieee80211_iter_func *) ieee80211_dump_node, nt); 2089} 2090 2091static void 2092ieee80211_notify_erp_locked(struct ieee80211com *ic) 2093{ 2094 struct ieee80211vap *vap; 2095 2096 IEEE80211_LOCK_ASSERT(ic); 2097 2098 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 2099 if (vap->iv_opmode == IEEE80211_M_HOSTAP) 2100 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP); 2101} 2102 2103void 2104ieee80211_notify_erp(struct ieee80211com *ic) 2105{ 2106 IEEE80211_LOCK(ic); 2107 ieee80211_notify_erp_locked(ic); 2108 IEEE80211_UNLOCK(ic); 2109} 2110 2111/* 2112 * Handle a station joining an 11g network. 2113 */ 2114static void 2115ieee80211_node_join_11g(struct ieee80211_node *ni) 2116{ 2117 struct ieee80211com *ic = ni->ni_ic; 2118 2119 IEEE80211_LOCK_ASSERT(ic); 2120 2121 /* 2122 * Station isn't capable of short slot time. Bump 2123 * the count of long slot time stations and disable 2124 * use of short slot time. Note that the actual switch 2125 * over to long slot time use may not occur until the 2126 * next beacon transmission (per sec. 7.3.1.4 of 11g). 2127 */ 2128 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2129 ic->ic_longslotsta++; 2130 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2131 "station needs long slot time, count %d", 2132 ic->ic_longslotsta); 2133 /* XXX vap's w/ conflicting needs won't work */ 2134 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) { 2135 /* 2136 * Don't force slot time when switched to turbo 2137 * mode as non-ERP stations won't be present; this 2138 * need only be done when on the normal G channel. 2139 */ 2140 ieee80211_set_shortslottime(ic, 0); 2141 } 2142 } 2143 /* 2144 * If the new station is not an ERP station 2145 * then bump the counter and enable protection 2146 * if configured. 2147 */ 2148 if (!ieee80211_iserp_rateset(&ni->ni_rates)) { 2149 ic->ic_nonerpsta++; 2150 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2151 "station is !ERP, %d non-ERP stations associated", 2152 ic->ic_nonerpsta); 2153 /* 2154 * If station does not support short preamble 2155 * then we must enable use of Barker preamble. 2156 */ 2157 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) { 2158 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2159 "%s", "station needs long preamble"); 2160 ic->ic_flags |= IEEE80211_F_USEBARKER; 2161 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 2162 } 2163 /* 2164 * If protection is configured and this is the first 2165 * indication we should use protection, enable it. 2166 */ 2167 if (ic->ic_protmode != IEEE80211_PROT_NONE && 2168 ic->ic_nonerpsta == 1 && 2169 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) { 2170 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC, 2171 "%s: enable use of protection\n", __func__); 2172 ic->ic_flags |= IEEE80211_F_USEPROT; 2173 ieee80211_notify_erp_locked(ic); 2174 } 2175 } else 2176 ni->ni_flags |= IEEE80211_NODE_ERP; 2177} 2178 2179void 2180ieee80211_node_join(struct ieee80211_node *ni, int resp) 2181{ 2182 struct ieee80211com *ic = ni->ni_ic; 2183 struct ieee80211vap *vap = ni->ni_vap; 2184 int newassoc; 2185 2186 if (ni->ni_associd == 0) { 2187 uint16_t aid; 2188 2189 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap")); 2190 /* 2191 * It would be good to search the bitmap 2192 * more efficiently, but this will do for now. 2193 */ 2194 for (aid = 1; aid < vap->iv_max_aid; aid++) { 2195 if (!IEEE80211_AID_ISSET(vap, aid)) 2196 break; 2197 } 2198 if (aid >= vap->iv_max_aid) { 2199 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY); 2200 ieee80211_node_leave(ni); 2201 return; 2202 } 2203 ni->ni_associd = aid | 0xc000; 2204 ni->ni_jointime = time_uptime; 2205 IEEE80211_LOCK(ic); 2206 IEEE80211_AID_SET(vap, ni->ni_associd); 2207 vap->iv_sta_assoc++; 2208 ic->ic_sta_assoc++; 2209 2210 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan)) 2211 ieee80211_ht_node_join(ni); 2212 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) && 2213 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan)) 2214 ieee80211_node_join_11g(ni); 2215 IEEE80211_UNLOCK(ic); 2216 2217 newassoc = 1; 2218 } else 2219 newassoc = 0; 2220 2221 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni, 2222 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s", 2223 IEEE80211_NODE_AID(ni), 2224 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long", 2225 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", 2226 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "", 2227 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "", 2228 ni->ni_flags & IEEE80211_NODE_HT ? 2229 (ni->ni_chw == 20 ? ", HT20" : ", HT40") : "", 2230 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "", 2231 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ? 2232 ", fast-frames" : "", 2233 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ? 2234 ", turbo" : "" 2235 ); 2236 2237 /* give driver a chance to setup state like ni_txrate */ 2238 if (ic->ic_newassoc != NULL) 2239 ic->ic_newassoc(ni, newassoc); 2240 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS); 2241 /* tell the authenticator about new station */ 2242 if (vap->iv_auth->ia_node_join != NULL) 2243 vap->iv_auth->ia_node_join(ni); 2244 ieee80211_notify_node_join(ni, 2245 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 2246} 2247 2248static void 2249disable_protection(struct ieee80211com *ic) 2250{ 2251 KASSERT(ic->ic_nonerpsta == 0 && 2252 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0, 2253 ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta, 2254 ic->ic_flags_ext)); 2255 2256 ic->ic_flags &= ~IEEE80211_F_USEPROT; 2257 /* XXX verify mode? */ 2258 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) { 2259 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 2260 ic->ic_flags &= ~IEEE80211_F_USEBARKER; 2261 } 2262 ieee80211_notify_erp_locked(ic); 2263} 2264 2265/* 2266 * Handle a station leaving an 11g network. 2267 */ 2268static void 2269ieee80211_node_leave_11g(struct ieee80211_node *ni) 2270{ 2271 struct ieee80211com *ic = ni->ni_ic; 2272 2273 IEEE80211_LOCK_ASSERT(ic); 2274 2275 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan), 2276 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq, 2277 ic->ic_bsschan->ic_flags)); 2278 2279 /* 2280 * If a long slot station do the slot time bookkeeping. 2281 */ 2282 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2283 KASSERT(ic->ic_longslotsta > 0, 2284 ("bogus long slot station count %d", ic->ic_longslotsta)); 2285 ic->ic_longslotsta--; 2286 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2287 "long slot time station leaves, count now %d", 2288 ic->ic_longslotsta); 2289 if (ic->ic_longslotsta == 0) { 2290 /* 2291 * Re-enable use of short slot time if supported 2292 * and not operating in IBSS mode (per spec). 2293 */ 2294 if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 2295 ic->ic_opmode != IEEE80211_M_IBSS) { 2296 IEEE80211_DPRINTF(ni->ni_vap, 2297 IEEE80211_MSG_ASSOC, 2298 "%s: re-enable use of short slot time\n", 2299 __func__); 2300 ieee80211_set_shortslottime(ic, 1); 2301 } 2302 } 2303 } 2304 /* 2305 * If a non-ERP station do the protection-related bookkeeping. 2306 */ 2307 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) { 2308 KASSERT(ic->ic_nonerpsta > 0, 2309 ("bogus non-ERP station count %d", ic->ic_nonerpsta)); 2310 ic->ic_nonerpsta--; 2311 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2312 "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta, 2313 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ? 2314 " (non-ERP sta present)" : ""); 2315 if (ic->ic_nonerpsta == 0 && 2316 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) { 2317 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC, 2318 "%s: disable use of protection\n", __func__); 2319 disable_protection(ic); 2320 } 2321 } 2322} 2323 2324/* 2325 * Time out presence of an overlapping bss with non-ERP 2326 * stations. When operating in hostap mode we listen for 2327 * beacons from other stations and if we identify a non-ERP 2328 * station is present we enable protection. To identify 2329 * when all non-ERP stations are gone we time out this 2330 * condition. 2331 */ 2332static void 2333ieee80211_erp_timeout(struct ieee80211com *ic) 2334{ 2335 2336 IEEE80211_LOCK_ASSERT(ic); 2337 2338 if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) && 2339 time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) { 2340#if 0 2341 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni, 2342 "%s", "age out non-ERP sta present on channel"); 2343#endif 2344 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR; 2345 if (ic->ic_nonerpsta == 0) 2346 disable_protection(ic); 2347 } 2348} 2349 2350/* 2351 * Handle bookkeeping for station deauthentication/disassociation 2352 * when operating as an ap. 2353 */ 2354void 2355ieee80211_node_leave(struct ieee80211_node *ni) 2356{ 2357 struct ieee80211com *ic = ni->ni_ic; 2358 struct ieee80211vap *vap = ni->ni_vap; 2359 struct ieee80211_node_table *nt = ni->ni_table; 2360 2361 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni, 2362 "station with aid %d leaves", IEEE80211_NODE_AID(ni)); 2363 2364 KASSERT(vap->iv_opmode != IEEE80211_M_STA, 2365 ("unexpected operating mode %u", vap->iv_opmode)); 2366 /* 2367 * If node wasn't previously associated all 2368 * we need to do is reclaim the reference. 2369 */ 2370 /* XXX ibss mode bypasses 11g and notification */ 2371 if (ni->ni_associd == 0) 2372 goto done; 2373 /* 2374 * Tell the authenticator the station is leaving. 2375 * Note that we must do this before yanking the 2376 * association id as the authenticator uses the 2377 * associd to locate it's state block. 2378 */ 2379 if (vap->iv_auth->ia_node_leave != NULL) 2380 vap->iv_auth->ia_node_leave(ni); 2381 2382 IEEE80211_LOCK(ic); 2383 IEEE80211_AID_CLR(vap, ni->ni_associd); 2384 ni->ni_associd = 0; 2385 vap->iv_sta_assoc--; 2386 ic->ic_sta_assoc--; 2387 2388 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan)) 2389 ieee80211_ht_node_leave(ni); 2390 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) && 2391 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan)) 2392 ieee80211_node_leave_11g(ni); 2393 IEEE80211_UNLOCK(ic); 2394 /* 2395 * Cleanup station state. In particular clear various 2396 * state that might otherwise be reused if the node 2397 * is reused before the reference count goes to zero 2398 * (and memory is reclaimed). 2399 */ 2400 ieee80211_sta_leave(ni); 2401done: 2402 /* 2403 * Remove the node from any table it's recorded in and 2404 * drop the caller's reference. Removal from the table 2405 * is important to insure the node is not reprocessed 2406 * for inactivity. 2407 */ 2408 if (nt != NULL) { 2409 IEEE80211_NODE_LOCK(nt); 2410 node_reclaim(nt, ni); 2411 IEEE80211_NODE_UNLOCK(nt); 2412 } else 2413 ieee80211_free_node(ni); 2414} 2415 2416struct rssiinfo { 2417 struct ieee80211vap *vap; 2418 int rssi_samples; 2419 uint32_t rssi_total; 2420}; 2421 2422static void 2423get_hostap_rssi(void *arg, struct ieee80211_node *ni) 2424{ 2425 struct rssiinfo *info = arg; 2426 struct ieee80211vap *vap = ni->ni_vap; 2427 int8_t rssi; 2428 2429 if (info->vap != vap) 2430 return; 2431 /* only associated stations */ 2432 if (ni->ni_associd == 0) 2433 return; 2434 rssi = vap->iv_ic->ic_node_getrssi(ni); 2435 if (rssi != 0) { 2436 info->rssi_samples++; 2437 info->rssi_total += rssi; 2438 } 2439} 2440 2441static void 2442get_adhoc_rssi(void *arg, struct ieee80211_node *ni) 2443{ 2444 struct rssiinfo *info = arg; 2445 struct ieee80211vap *vap = ni->ni_vap; 2446 int8_t rssi; 2447 2448 if (info->vap != vap) 2449 return; 2450 /* only neighbors */ 2451 /* XXX check bssid */ 2452 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 2453 return; 2454 rssi = vap->iv_ic->ic_node_getrssi(ni); 2455 if (rssi != 0) { 2456 info->rssi_samples++; 2457 info->rssi_total += rssi; 2458 } 2459} 2460 2461int8_t 2462ieee80211_getrssi(struct ieee80211vap *vap) 2463{ 2464#define NZ(x) ((x) == 0 ? 1 : (x)) 2465 struct ieee80211com *ic = vap->iv_ic; 2466 struct rssiinfo info; 2467 2468 info.rssi_total = 0; 2469 info.rssi_samples = 0; 2470 info.vap = vap; 2471 switch (vap->iv_opmode) { 2472 case IEEE80211_M_IBSS: /* average of all ibss neighbors */ 2473 case IEEE80211_M_AHDEMO: /* average of all neighbors */ 2474 ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info); 2475 break; 2476 case IEEE80211_M_HOSTAP: /* average of all associated stations */ 2477 ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info); 2478 break; 2479 case IEEE80211_M_MONITOR: /* XXX */ 2480 case IEEE80211_M_STA: /* use stats from associated ap */ 2481 default: 2482 if (vap->iv_bss != NULL) 2483 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss); 2484 info.rssi_samples = 1; 2485 break; 2486 } 2487 return info.rssi_total / NZ(info.rssi_samples); 2488#undef NZ 2489} 2490 2491void 2492ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise) 2493{ 2494 2495 if (vap->iv_bss == NULL) /* NB: shouldn't happen */ 2496 return; 2497 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise); 2498 /* for non-station mode return avg'd rssi accounting */ 2499 if (vap->iv_opmode != IEEE80211_M_STA) 2500 *rssi = ieee80211_getrssi(vap); 2501} 2502