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