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