ieee80211_node.c revision 190579
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 190579 2009-03-30 21:53:27Z 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_rt = ieee80211_get_ratetable(ic->ic_curchan); 631 ic->ic_set_channel(ic); 632 } 633} 634 635/* 636 * Change the current channel. The request channel may be 637 * promoted if other vap's are operating with HT20/HT40. 638 */ 639void 640ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c) 641{ 642 if (ic->ic_htcaps & IEEE80211_HTC_HT) { 643 int flags = gethtadjustflags(ic); 644 /* 645 * Check for channel promotion required to support the 646 * set of running vap's. This assumes we are called 647 * after ni_chan is setup for each vap. 648 */ 649 /* NB: this assumes IEEE80211_FEXT_USEHT40 > IEEE80211_FEXT_HT */ 650 if (flags > ieee80211_htchanflags(c)) 651 c = ieee80211_ht_adjust_channel(ic, c, flags); 652 } 653 ic->ic_bsschan = ic->ic_curchan = c; 654 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan); 655 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan); 656 ic->ic_set_channel(ic); 657} 658 659/* 660 * Join the specified IBSS/BSS network. The node is assumed to 661 * be passed in with a held reference. 662 */ 663static int 664ieee80211_sta_join1(struct ieee80211_node *selbs) 665{ 666 struct ieee80211vap *vap = selbs->ni_vap; 667 struct ieee80211com *ic = selbs->ni_ic; 668 struct ieee80211_node *obss; 669 int canreassoc; 670 671 /* 672 * Committed to selbs, setup state. 673 */ 674 obss = vap->iv_bss; 675 /* 676 * Check if old+new node have the same address in which 677 * case we can reassociate when operating in sta mode. 678 */ 679 canreassoc = (obss != NULL && 680 vap->iv_state == IEEE80211_S_RUN && 681 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr)); 682 vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */ 683 if (obss != NULL) { 684 copy_bss(selbs, obss); 685 ieee80211_node_decref(obss); /* iv_bss reference */ 686 ieee80211_free_node(obss); /* station table reference */ 687 obss = NULL; /* NB: guard against later use */ 688 } 689 690 /* 691 * Delete unusable rates; we've already checked 692 * that the negotiated rate set is acceptable. 693 */ 694 ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates, 695 IEEE80211_F_DODEL | IEEE80211_F_JOIN); 696 697 ieee80211_setcurchan(ic, selbs->ni_chan); 698 /* 699 * Set the erp state (mostly the slot time) to deal with 700 * the auto-select case; this should be redundant if the 701 * mode is locked. 702 */ 703 ieee80211_reset_erp(ic); 704 ieee80211_wme_initparams(vap); 705 706 if (vap->iv_opmode == IEEE80211_M_STA) { 707 if (canreassoc) { 708 /* Reassociate */ 709 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1); 710 } else { 711 /* 712 * Act as if we received a DEAUTH frame in case we 713 * are invoked from the RUN state. This will cause 714 * us to try to re-authenticate if we are operating 715 * as a station. 716 */ 717 ieee80211_new_state(vap, IEEE80211_S_AUTH, 718 IEEE80211_FC0_SUBTYPE_DEAUTH); 719 } 720 } else 721 ieee80211_new_state(vap, IEEE80211_S_RUN, -1); 722 return 1; 723} 724 725int 726ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan, 727 const struct ieee80211_scan_entry *se) 728{ 729 struct ieee80211com *ic = vap->iv_ic; 730 struct ieee80211_node *ni; 731 732 ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr); 733 if (ni == NULL) { 734 /* XXX msg */ 735 return 0; 736 } 737 /* 738 * Expand scan state into node's format. 739 * XXX may not need all this stuff 740 */ 741 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid); 742 ni->ni_esslen = se->se_ssid[1]; 743 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen); 744 ni->ni_rstamp = se->se_rstamp; 745 ni->ni_tstamp.tsf = se->se_tstamp.tsf; 746 ni->ni_intval = se->se_intval; 747 ni->ni_capinfo = se->se_capinfo; 748 ni->ni_chan = chan; 749 ni->ni_timoff = se->se_timoff; 750 ni->ni_fhdwell = se->se_fhdwell; 751 ni->ni_fhindex = se->se_fhindex; 752 ni->ni_erp = se->se_erp; 753 IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi); 754 ni->ni_noise = se->se_noise; 755 if (vap->iv_opmode == IEEE80211_M_STA) { 756 /* NB: only infrastructure mode requires an associd */ 757 ni->ni_flags |= IEEE80211_NODE_ASSOCID; 758 } 759 760 if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) { 761 ieee80211_ies_expand(&ni->ni_ies); 762#ifdef IEEE80211_SUPPORT_SUPERG 763 if (ni->ni_ies.ath_ie != NULL) 764 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie); 765#endif 766 if (ni->ni_ies.htcap_ie != NULL) 767 ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie); 768 if (ni->ni_ies.htinfo_ie != NULL) 769 ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie); 770#ifdef IEEE80211_SUPPORT_TDMA 771 if (ni->ni_ies.tdma_ie != NULL) 772 ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie); 773#endif 774 } 775 776 vap->iv_dtim_period = se->se_dtimperiod; 777 vap->iv_dtim_count = 0; 778 779 /* NB: must be after ni_chan is setup */ 780 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates, 781 IEEE80211_F_DOSORT); 782 if (ieee80211_iserp_rateset(&ni->ni_rates)) 783 ni->ni_flags |= IEEE80211_NODE_ERP; 784 node_setuptxparms(ni); 785 786 return ieee80211_sta_join1(ieee80211_ref_node(ni)); 787} 788 789/* 790 * Leave the specified IBSS/BSS network. The node is assumed to 791 * be passed in with a held reference. 792 */ 793void 794ieee80211_sta_leave(struct ieee80211_node *ni) 795{ 796 struct ieee80211com *ic = ni->ni_ic; 797 798 ic->ic_node_cleanup(ni); 799 ieee80211_notify_node_leave(ni); 800} 801 802/* 803 * Send a deauthenticate frame and drop the station. 804 */ 805void 806ieee80211_node_deauth(struct ieee80211_node *ni, int reason) 807{ 808 /* NB: bump the refcnt to be sure temporay nodes are not reclaimed */ 809 ieee80211_ref_node(ni); 810 if (ni->ni_associd != 0) 811 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason); 812 ieee80211_node_leave(ni); 813 ieee80211_free_node(ni); 814} 815 816static struct ieee80211_node * 817node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN]) 818{ 819 struct ieee80211_node *ni; 820 821 ni = (struct ieee80211_node *) malloc(sizeof(struct ieee80211_node), 822 M_80211_NODE, M_NOWAIT | M_ZERO); 823 return ni; 824} 825 826/* 827 * Initialize an ie blob with the specified data. If previous 828 * data exists re-use the data block. As a side effect we clear 829 * all references to specific ie's; the caller is required to 830 * recalculate them. 831 */ 832int 833ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len) 834{ 835 /* NB: assumes data+len are the last fields */ 836 memset(ies, 0, offsetof(struct ieee80211_ies, data)); 837 if (ies->data != NULL && ies->len != len) { 838 /* data size changed */ 839 free(ies->data, M_80211_NODE_IE); 840 ies->data = NULL; 841 } 842 if (ies->data == NULL) { 843 ies->data = (uint8_t *) malloc(len, M_80211_NODE_IE, M_NOWAIT); 844 if (ies->data == NULL) { 845 ies->len = 0; 846 /* NB: pointers have already been zero'd above */ 847 return 0; 848 } 849 } 850 memcpy(ies->data, data, len); 851 ies->len = len; 852 return 1; 853} 854 855/* 856 * Reclaim storage for an ie blob. 857 */ 858void 859ieee80211_ies_cleanup(struct ieee80211_ies *ies) 860{ 861 if (ies->data != NULL) 862 free(ies->data, M_80211_NODE_IE); 863} 864 865/* 866 * Expand an ie blob data contents and to fillin individual 867 * ie pointers. The data blob is assumed to be well-formed; 868 * we don't do any validity checking of ie lengths. 869 */ 870void 871ieee80211_ies_expand(struct ieee80211_ies *ies) 872{ 873 uint8_t *ie; 874 int ielen; 875 876 ie = ies->data; 877 ielen = ies->len; 878 while (ielen > 0) { 879 switch (ie[0]) { 880 case IEEE80211_ELEMID_VENDOR: 881 if (iswpaoui(ie)) 882 ies->wpa_ie = ie; 883 else if (iswmeoui(ie)) 884 ies->wme_ie = ie; 885#ifdef IEEE80211_SUPPORT_SUPERG 886 else if (isatherosoui(ie)) 887 ies->ath_ie = ie; 888#endif 889#ifdef IEEE80211_SUPPORT_TDMA 890 else if (istdmaoui(ie)) 891 ies->tdma_ie = ie; 892#endif 893 break; 894 case IEEE80211_ELEMID_RSN: 895 ies->rsn_ie = ie; 896 break; 897 case IEEE80211_ELEMID_HTCAP: 898 ies->htcap_ie = ie; 899 break; 900 } 901 ielen -= 2 + ie[1]; 902 ie += 2 + ie[1]; 903 } 904} 905 906/* 907 * Reclaim any resources in a node and reset any critical 908 * state. Typically nodes are free'd immediately after, 909 * but in some cases the storage may be reused so we need 910 * to insure consistent state (should probably fix that). 911 */ 912static void 913node_cleanup(struct ieee80211_node *ni) 914{ 915#define N(a) (sizeof(a)/sizeof(a[0])) 916 struct ieee80211vap *vap = ni->ni_vap; 917 int i; 918 919 /* NB: preserve ni_table */ 920 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 921 if (vap->iv_opmode != IEEE80211_M_STA) 922 vap->iv_ps_sta--; 923 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 924 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 925 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta); 926 } 927 /* 928 * Cleanup any HT-related state. 929 */ 930 if (ni->ni_flags & IEEE80211_NODE_HT) 931 ieee80211_ht_node_cleanup(ni); 932#ifdef IEEE80211_SUPPORT_SUPERG 933 else if (ni->ni_ath_flags & IEEE80211_NODE_ATH) 934 ieee80211_ff_node_cleanup(ni); 935#endif 936 /* 937 * Clear AREF flag that marks the authorization refcnt bump 938 * has happened. This is probably not needed as the node 939 * should always be removed from the table so not found but 940 * do it just in case. 941 * Likewise clear the ASSOCID flag as these flags are intended 942 * to be managed in tandem. 943 */ 944 ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID); 945 946 /* 947 * Drain power save queue and, if needed, clear TIM. 948 */ 949 if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL) 950 vap->iv_set_tim(ni, 0); 951 952 ni->ni_associd = 0; 953 if (ni->ni_challenge != NULL) { 954 free(ni->ni_challenge, M_80211_NODE); 955 ni->ni_challenge = NULL; 956 } 957 /* 958 * Preserve SSID, WPA, and WME ie's so the bss node is 959 * reusable during a re-auth/re-assoc state transition. 960 * If we remove these data they will not be recreated 961 * because they come from a probe-response or beacon frame 962 * which cannot be expected prior to the association-response. 963 * This should not be an issue when operating in other modes 964 * as stations leaving always go through a full state transition 965 * which will rebuild this state. 966 * 967 * XXX does this leave us open to inheriting old state? 968 */ 969 for (i = 0; i < N(ni->ni_rxfrag); i++) 970 if (ni->ni_rxfrag[i] != NULL) { 971 m_freem(ni->ni_rxfrag[i]); 972 ni->ni_rxfrag[i] = NULL; 973 } 974 /* 975 * Must be careful here to remove any key map entry w/o a LOR. 976 */ 977 ieee80211_node_delucastkey(ni); 978#undef N 979} 980 981static void 982node_free(struct ieee80211_node *ni) 983{ 984 struct ieee80211com *ic = ni->ni_ic; 985 986 ic->ic_node_cleanup(ni); 987 ieee80211_ies_cleanup(&ni->ni_ies); 988 ieee80211_psq_cleanup(&ni->ni_psq); 989 IEEE80211_NODE_WDSQ_DESTROY(ni); 990 free(ni, M_80211_NODE); 991} 992 993static void 994node_age(struct ieee80211_node *ni) 995{ 996 struct ieee80211vap *vap = ni->ni_vap; 997 998 IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta); 999 1000 /* 1001 * Age frames on the power save queue. 1002 */ 1003 if (ieee80211_node_psq_age(ni) != 0 && 1004 ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL) 1005 vap->iv_set_tim(ni, 0); 1006 /* 1007 * Age frames on the wds pending queue. 1008 */ 1009 if (IEEE80211_NODE_WDSQ_QLEN(ni) != 0) 1010 ieee80211_node_wdsq_age(ni); 1011 /* 1012 * Age out HT resources (e.g. frames on the 1013 * A-MPDU reorder queues). 1014 */ 1015 if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT)) 1016 ieee80211_ht_node_age(ni); 1017} 1018 1019static int8_t 1020node_getrssi(const struct ieee80211_node *ni) 1021{ 1022 uint32_t avgrssi = ni->ni_avgrssi; 1023 int32_t rssi; 1024 1025 if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) 1026 return 0; 1027 rssi = IEEE80211_RSSI_GET(avgrssi); 1028 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi; 1029} 1030 1031static void 1032node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise) 1033{ 1034 *rssi = node_getrssi(ni); 1035 *noise = ni->ni_noise; 1036} 1037 1038static void 1039node_getmimoinfo(const struct ieee80211_node *ni, 1040 struct ieee80211_mimo_info *info) 1041{ 1042 /* XXX zero data? */ 1043} 1044 1045struct ieee80211_node * 1046ieee80211_alloc_node(struct ieee80211_node_table *nt, 1047 struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1048{ 1049 struct ieee80211com *ic = nt->nt_ic; 1050 struct ieee80211_node *ni; 1051 int hash; 1052 1053 ni = ic->ic_node_alloc(vap, macaddr); 1054 if (ni == NULL) { 1055 vap->iv_stats.is_rx_nodealloc++; 1056 return NULL; 1057 } 1058 1059 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1060 "%s %p<%s> in %s table\n", __func__, ni, 1061 ether_sprintf(macaddr), nt->nt_name); 1062 1063 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 1064 hash = IEEE80211_NODE_HASH(macaddr); 1065 ieee80211_node_initref(ni); /* mark referenced */ 1066 ni->ni_chan = IEEE80211_CHAN_ANYC; 1067 ni->ni_authmode = IEEE80211_AUTH_OPEN; 1068 ni->ni_txpower = ic->ic_txpowlimit; /* max power */ 1069 ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1070 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE); 1071 ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER; 1072 ni->ni_inact_reload = nt->nt_inact_init; 1073 ni->ni_inact = ni->ni_inact_reload; 1074 ni->ni_ath_defkeyix = 0x7fff; 1075 ieee80211_psq_init(&ni->ni_psq, "unknown"); 1076 IEEE80211_NODE_WDSQ_INIT(ni, "unknown"); 1077 1078 IEEE80211_NODE_LOCK(nt); 1079 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list); 1080 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash); 1081 ni->ni_table = nt; 1082 ni->ni_vap = vap; 1083 ni->ni_ic = ic; 1084 IEEE80211_NODE_UNLOCK(nt); 1085 1086 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 1087 "%s: inact_reload %u", __func__, ni->ni_inact_reload); 1088 1089 return ni; 1090} 1091 1092/* 1093 * Craft a temporary node suitable for sending a management frame 1094 * to the specified station. We craft only as much state as we 1095 * need to do the work since the node will be immediately reclaimed 1096 * once the send completes. 1097 */ 1098struct ieee80211_node * 1099ieee80211_tmp_node(struct ieee80211vap *vap, 1100 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1101{ 1102 struct ieee80211com *ic = vap->iv_ic; 1103 struct ieee80211_node *ni; 1104 1105 ni = ic->ic_node_alloc(vap, macaddr); 1106 if (ni != NULL) { 1107 struct ieee80211_node *bss = vap->iv_bss; 1108 1109 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1110 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr)); 1111 1112 ni->ni_table = NULL; /* NB: pedantic */ 1113 ni->ni_ic = ic; /* NB: needed to set channel */ 1114 ni->ni_vap = vap; 1115 1116 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 1117 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid); 1118 ieee80211_node_initref(ni); /* mark referenced */ 1119 /* NB: required by ieee80211_fix_rate */ 1120 ieee80211_node_set_chan(ni, bss->ni_chan); 1121 ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, 1122 IEEE80211_KEYIX_NONE); 1123 ni->ni_txpower = bss->ni_txpower; 1124 /* XXX optimize away */ 1125 ieee80211_psq_init(&ni->ni_psq, "unknown"); 1126 IEEE80211_NODE_WDSQ_INIT(ni, "unknown"); 1127 } else { 1128 /* XXX msg */ 1129 vap->iv_stats.is_rx_nodealloc++; 1130 } 1131 return ni; 1132} 1133 1134struct ieee80211_node * 1135ieee80211_dup_bss(struct ieee80211vap *vap, 1136 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1137{ 1138 struct ieee80211com *ic = vap->iv_ic; 1139 struct ieee80211_node *ni; 1140 1141 ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr); 1142 if (ni != NULL) { 1143 struct ieee80211_node *bss = vap->iv_bss; 1144 /* 1145 * Inherit from iv_bss. 1146 */ 1147 copy_bss(ni, bss); 1148 IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid); 1149 ieee80211_node_set_chan(ni, bss->ni_chan); 1150 } 1151 return ni; 1152} 1153 1154/* 1155 * Create a bss node for a legacy WDS vap. The far end does 1156 * not associate so we just create create a new node and 1157 * simulate an association. The caller is responsible for 1158 * installing the node as the bss node and handling any further 1159 * setup work like authorizing the port. 1160 */ 1161struct ieee80211_node * 1162ieee80211_node_create_wds(struct ieee80211vap *vap, 1163 const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan) 1164{ 1165 struct ieee80211com *ic = vap->iv_ic; 1166 struct ieee80211_node *ni; 1167 1168 /* XXX check if node already in sta table? */ 1169 ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid); 1170 if (ni != NULL) { 1171 ni->ni_wdsvap = vap; 1172 IEEE80211_ADDR_COPY(ni->ni_bssid, bssid); 1173 /* 1174 * Inherit any manually configured settings. 1175 */ 1176 copy_bss(ni, vap->iv_bss); 1177 ieee80211_node_set_chan(ni, chan); 1178 /* NB: propagate ssid so available to WPA supplicant */ 1179 ni->ni_esslen = vap->iv_des_ssid[0].len; 1180 memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen); 1181 /* NB: no associd for peer */ 1182 /* 1183 * There are no management frames to use to 1184 * discover neighbor capabilities, so blindly 1185 * propagate the local configuration. 1186 */ 1187 if (vap->iv_flags & IEEE80211_F_WME) 1188 ni->ni_flags |= IEEE80211_NODE_QOS; 1189#ifdef IEEE80211_SUPPORT_SUPERG 1190 if (vap->iv_flags & IEEE80211_F_FF) 1191 ni->ni_flags |= IEEE80211_NODE_FF; 1192#endif 1193 if ((ic->ic_htcaps & IEEE80211_HTC_HT) && 1194 (vap->iv_flags_ext & IEEE80211_FEXT_HT)) { 1195 /* 1196 * Device is HT-capable and HT is enabled for 1197 * the vap; setup HT operation. On return 1198 * ni_chan will be adjusted to an HT channel. 1199 */ 1200 ieee80211_ht_wds_init(ni); 1201 } else { 1202 struct ieee80211_channel *c = ni->ni_chan; 1203 /* 1204 * Force a legacy channel to be used. 1205 */ 1206 c = ieee80211_find_channel(ic, 1207 c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT); 1208 KASSERT(c != NULL, ("no legacy channel, %u/%x", 1209 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags)); 1210 ni->ni_chan = c; 1211 } 1212 } 1213 return ni; 1214} 1215 1216struct ieee80211_node * 1217#ifdef IEEE80211_DEBUG_REFCNT 1218ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt, 1219 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1220#else 1221ieee80211_find_node_locked(struct ieee80211_node_table *nt, 1222 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1223#endif 1224{ 1225 struct ieee80211_node *ni; 1226 int hash; 1227 1228 IEEE80211_NODE_LOCK_ASSERT(nt); 1229 1230 hash = IEEE80211_NODE_HASH(macaddr); 1231 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1232 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 1233 ieee80211_ref_node(ni); /* mark referenced */ 1234#ifdef IEEE80211_DEBUG_REFCNT 1235 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1236 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1237 func, line, 1238 ni, ether_sprintf(ni->ni_macaddr), 1239 ieee80211_node_refcnt(ni)); 1240#endif 1241 return ni; 1242 } 1243 } 1244 return NULL; 1245} 1246 1247struct ieee80211_node * 1248#ifdef IEEE80211_DEBUG_REFCNT 1249ieee80211_find_node_debug(struct ieee80211_node_table *nt, 1250 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1251#else 1252ieee80211_find_node(struct ieee80211_node_table *nt, 1253 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1254#endif 1255{ 1256 struct ieee80211_node *ni; 1257 1258 IEEE80211_NODE_LOCK(nt); 1259 ni = ieee80211_find_node_locked(nt, macaddr); 1260 IEEE80211_NODE_UNLOCK(nt); 1261 return ni; 1262} 1263 1264struct ieee80211_node * 1265#ifdef IEEE80211_DEBUG_REFCNT 1266ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt, 1267 const struct ieee80211vap *vap, 1268 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1269#else 1270ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt, 1271 const struct ieee80211vap *vap, 1272 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1273#endif 1274{ 1275 struct ieee80211_node *ni; 1276 int hash; 1277 1278 IEEE80211_NODE_LOCK_ASSERT(nt); 1279 1280 hash = IEEE80211_NODE_HASH(macaddr); 1281 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1282 if (ni->ni_vap == vap && 1283 IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 1284 ieee80211_ref_node(ni); /* mark referenced */ 1285#ifdef IEEE80211_DEBUG_REFCNT 1286 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1287 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1288 func, line, 1289 ni, ether_sprintf(ni->ni_macaddr), 1290 ieee80211_node_refcnt(ni)); 1291#endif 1292 return ni; 1293 } 1294 } 1295 return NULL; 1296} 1297 1298struct ieee80211_node * 1299#ifdef IEEE80211_DEBUG_REFCNT 1300ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt, 1301 const struct ieee80211vap *vap, 1302 const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1303#else 1304ieee80211_find_vap_node(struct ieee80211_node_table *nt, 1305 const struct ieee80211vap *vap, 1306 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1307#endif 1308{ 1309 struct ieee80211_node *ni; 1310 1311 IEEE80211_NODE_LOCK(nt); 1312 ni = ieee80211_find_vap_node_locked(nt, vap, macaddr); 1313 IEEE80211_NODE_UNLOCK(nt); 1314 return ni; 1315} 1316 1317/* 1318 * Fake up a node; this handles node discovery in adhoc mode. 1319 * Note that for the driver's benefit we we treat this like 1320 * an association so the driver has an opportunity to setup 1321 * it's private state. 1322 */ 1323struct ieee80211_node * 1324ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap, 1325 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1326{ 1327 struct ieee80211_node *ni; 1328 1329 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1330 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr)); 1331 ni = ieee80211_dup_bss(vap, macaddr); 1332 if (ni != NULL) { 1333 struct ieee80211com *ic = vap->iv_ic; 1334 1335 /* XXX no rate negotiation; just dup */ 1336 ni->ni_rates = vap->iv_bss->ni_rates; 1337 if (ieee80211_iserp_rateset(&ni->ni_rates)) 1338 ni->ni_flags |= IEEE80211_NODE_ERP; 1339 if (vap->iv_opmode == IEEE80211_M_AHDEMO) { 1340 /* 1341 * In adhoc demo mode there are no management 1342 * frames to use to discover neighbor capabilities, 1343 * so blindly propagate the local configuration 1344 * so we can do interesting things (e.g. use 1345 * WME to disable ACK's). 1346 */ 1347 if (vap->iv_flags & IEEE80211_F_WME) 1348 ni->ni_flags |= IEEE80211_NODE_QOS; 1349#ifdef IEEE80211_SUPPORT_SUPERG 1350 if (vap->iv_flags & IEEE80211_F_FF) 1351 ni->ni_flags |= IEEE80211_NODE_FF; 1352#endif 1353 } 1354 node_setuptxparms(ni); 1355 if (ic->ic_newassoc != NULL) 1356 ic->ic_newassoc(ni, 1); 1357 /* XXX not right for 802.1x/WPA */ 1358 ieee80211_node_authorize(ni); 1359 } 1360 return ni; 1361} 1362 1363void 1364ieee80211_init_neighbor(struct ieee80211_node *ni, 1365 const struct ieee80211_frame *wh, 1366 const struct ieee80211_scanparams *sp) 1367{ 1368 ni->ni_esslen = sp->ssid[1]; 1369 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]); 1370 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3); 1371 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp)); 1372 ni->ni_intval = sp->bintval; 1373 ni->ni_capinfo = sp->capinfo; 1374 ni->ni_chan = ni->ni_ic->ic_curchan; 1375 ni->ni_fhdwell = sp->fhdwell; 1376 ni->ni_fhindex = sp->fhindex; 1377 ni->ni_erp = sp->erp; 1378 ni->ni_timoff = sp->timoff; 1379 1380 if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) { 1381 ieee80211_ies_expand(&ni->ni_ies); 1382 if (ni->ni_ies.wme_ie != NULL) 1383 ni->ni_flags |= IEEE80211_NODE_QOS; 1384 else 1385 ni->ni_flags &= ~IEEE80211_NODE_QOS; 1386#ifdef IEEE80211_SUPPORT_SUPERG 1387 if (ni->ni_ies.ath_ie != NULL) 1388 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie); 1389#endif 1390 } 1391 1392 /* NB: must be after ni_chan is setup */ 1393 ieee80211_setup_rates(ni, sp->rates, sp->xrates, 1394 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | 1395 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1396} 1397 1398/* 1399 * Do node discovery in adhoc mode on receipt of a beacon 1400 * or probe response frame. Note that for the driver's 1401 * benefit we we treat this like an association so the 1402 * driver has an opportunity to setup it's private state. 1403 */ 1404struct ieee80211_node * 1405ieee80211_add_neighbor(struct ieee80211vap *vap, 1406 const struct ieee80211_frame *wh, 1407 const struct ieee80211_scanparams *sp) 1408{ 1409 struct ieee80211_node *ni; 1410 1411 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1412 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2)); 1413 ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */ 1414 if (ni != NULL) { 1415 struct ieee80211com *ic = vap->iv_ic; 1416 1417 ieee80211_init_neighbor(ni, wh, sp); 1418 if (ieee80211_iserp_rateset(&ni->ni_rates)) 1419 ni->ni_flags |= IEEE80211_NODE_ERP; 1420 node_setuptxparms(ni); 1421 if (ic->ic_newassoc != NULL) 1422 ic->ic_newassoc(ni, 1); 1423 /* XXX not right for 802.1x/WPA */ 1424 ieee80211_node_authorize(ni); 1425 } 1426 return ni; 1427} 1428 1429#define IS_CTL(wh) \ 1430 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) 1431#define IS_PSPOLL(wh) \ 1432 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL) 1433#define IS_BAR(wh) \ 1434 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR) 1435#define IS_PROBEREQ(wh) \ 1436 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \ 1437 == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ)) 1438#define IS_BCAST_PROBEREQ(wh) \ 1439 (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \ 1440 ((const struct ieee80211_frame *)(wh))->i_addr3)) 1441 1442static __inline struct ieee80211_node * 1443_find_rxnode(struct ieee80211_node_table *nt, 1444 const struct ieee80211_frame_min *wh) 1445{ 1446 /* XXX 4-address frames? */ 1447 if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/) 1448 return ieee80211_find_node_locked(nt, wh->i_addr1); 1449 if (IS_BCAST_PROBEREQ(wh)) 1450 return NULL; /* spam bcast probe req to all vap's */ 1451 return ieee80211_find_node_locked(nt, wh->i_addr2); 1452} 1453 1454/* 1455 * Locate the node for sender, track state, and then pass the 1456 * (referenced) node up to the 802.11 layer for its use. Note 1457 * we can return NULL if the sender is not in the table. 1458 */ 1459struct ieee80211_node * 1460#ifdef IEEE80211_DEBUG_REFCNT 1461ieee80211_find_rxnode_debug(struct ieee80211com *ic, 1462 const struct ieee80211_frame_min *wh, const char *func, int line) 1463#else 1464ieee80211_find_rxnode(struct ieee80211com *ic, 1465 const struct ieee80211_frame_min *wh) 1466#endif 1467{ 1468 struct ieee80211_node_table *nt; 1469 struct ieee80211_node *ni; 1470 1471 nt = &ic->ic_sta; 1472 IEEE80211_NODE_LOCK(nt); 1473 ni = _find_rxnode(nt, wh); 1474 IEEE80211_NODE_UNLOCK(nt); 1475 1476 return ni; 1477} 1478 1479/* 1480 * Like ieee80211_find_rxnode but use the supplied h/w 1481 * key index as a hint to locate the node in the key 1482 * mapping table. If an entry is present at the key 1483 * index we return it; otherwise do a normal lookup and 1484 * update the mapping table if the station has a unicast 1485 * key assigned to it. 1486 */ 1487struct ieee80211_node * 1488#ifdef IEEE80211_DEBUG_REFCNT 1489ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic, 1490 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix, 1491 const char *func, int line) 1492#else 1493ieee80211_find_rxnode_withkey(struct ieee80211com *ic, 1494 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix) 1495#endif 1496{ 1497 struct ieee80211_node_table *nt; 1498 struct ieee80211_node *ni; 1499 1500 nt = &ic->ic_sta; 1501 IEEE80211_NODE_LOCK(nt); 1502 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) 1503 ni = nt->nt_keyixmap[keyix]; 1504 else 1505 ni = NULL; 1506 if (ni == NULL) { 1507 ni = _find_rxnode(nt, wh); 1508 if (ni != NULL && nt->nt_keyixmap != NULL) { 1509 /* 1510 * If the station has a unicast key cache slot 1511 * assigned update the key->node mapping table. 1512 */ 1513 keyix = ni->ni_ucastkey.wk_rxkeyix; 1514 /* XXX can keyixmap[keyix] != NULL? */ 1515 if (keyix < nt->nt_keyixmax && 1516 nt->nt_keyixmap[keyix] == NULL) { 1517 IEEE80211_DPRINTF(ni->ni_vap, 1518 IEEE80211_MSG_NODE, 1519 "%s: add key map entry %p<%s> refcnt %d\n", 1520 __func__, ni, ether_sprintf(ni->ni_macaddr), 1521 ieee80211_node_refcnt(ni)+1); 1522 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni); 1523 } 1524 } 1525 } else { 1526 if (IS_BCAST_PROBEREQ(wh)) 1527 ni = NULL; /* spam bcast probe req to all vap's */ 1528 else 1529 ieee80211_ref_node(ni); 1530 } 1531 IEEE80211_NODE_UNLOCK(nt); 1532 1533 return ni; 1534} 1535#undef IS_BCAST_PROBEREQ 1536#undef IS_PROBEREQ 1537#undef IS_BAR 1538#undef IS_PSPOLL 1539#undef IS_CTL 1540 1541/* 1542 * Return a reference to the appropriate node for sending 1543 * a data frame. This handles node discovery in adhoc networks. 1544 */ 1545struct ieee80211_node * 1546#ifdef IEEE80211_DEBUG_REFCNT 1547ieee80211_find_txnode_debug(struct ieee80211vap *vap, 1548 const uint8_t macaddr[IEEE80211_ADDR_LEN], 1549 const char *func, int line) 1550#else 1551ieee80211_find_txnode(struct ieee80211vap *vap, 1552 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1553#endif 1554{ 1555 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta; 1556 struct ieee80211_node *ni; 1557 1558 /* 1559 * The destination address should be in the node table 1560 * unless this is a multicast/broadcast frame. We can 1561 * also optimize station mode operation, all frames go 1562 * to the bss node. 1563 */ 1564 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */ 1565 IEEE80211_NODE_LOCK(nt); 1566 if (vap->iv_opmode == IEEE80211_M_STA || 1567 vap->iv_opmode == IEEE80211_M_WDS || 1568 IEEE80211_IS_MULTICAST(macaddr)) 1569 ni = ieee80211_ref_node(vap->iv_bss); 1570 else 1571 ni = ieee80211_find_node_locked(nt, macaddr); 1572 IEEE80211_NODE_UNLOCK(nt); 1573 1574 if (ni == NULL) { 1575 if (vap->iv_opmode == IEEE80211_M_IBSS || 1576 vap->iv_opmode == IEEE80211_M_AHDEMO) { 1577 /* 1578 * In adhoc mode cons up a node for the destination. 1579 * Note that we need an additional reference for the 1580 * caller to be consistent with 1581 * ieee80211_find_node_locked. 1582 */ 1583 ni = ieee80211_fakeup_adhoc_node(vap, macaddr); 1584 if (ni != NULL) 1585 (void) ieee80211_ref_node(ni); 1586 } else { 1587 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr, 1588 "no node, discard frame (%s)", __func__); 1589 vap->iv_stats.is_tx_nonode++; 1590 } 1591 } 1592 return ni; 1593} 1594 1595static void 1596_ieee80211_free_node(struct ieee80211_node *ni) 1597{ 1598 struct ieee80211_node_table *nt = ni->ni_table; 1599 1600 /* 1601 * NB: careful about referencing the vap as it may be 1602 * gone if the last reference was held by a driver. 1603 * We know the com will always be present so it's safe 1604 * to use ni_ic below to reclaim resources. 1605 */ 1606#if 0 1607 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1608 "%s %p<%s> in %s table\n", __func__, ni, 1609 ether_sprintf(ni->ni_macaddr), 1610 nt != NULL ? nt->nt_name : "<gone>"); 1611#endif 1612 if (ni->ni_associd != 0) { 1613 struct ieee80211vap *vap = ni->ni_vap; 1614 if (vap->iv_aid_bitmap != NULL) 1615 IEEE80211_AID_CLR(vap, ni->ni_associd); 1616 } 1617 if (nt != NULL) { 1618 TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1619 LIST_REMOVE(ni, ni_hash); 1620 } 1621 ni->ni_ic->ic_node_free(ni); 1622} 1623 1624void 1625#ifdef IEEE80211_DEBUG_REFCNT 1626ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line) 1627#else 1628ieee80211_free_node(struct ieee80211_node *ni) 1629#endif 1630{ 1631 struct ieee80211_node_table *nt = ni->ni_table; 1632 1633#ifdef IEEE80211_DEBUG_REFCNT 1634 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1635 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 1636 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1637#endif 1638 if (nt != NULL) { 1639 IEEE80211_NODE_LOCK(nt); 1640 if (ieee80211_node_dectestref(ni)) { 1641 /* 1642 * Last reference, reclaim state. 1643 */ 1644 _ieee80211_free_node(ni); 1645 } else if (ieee80211_node_refcnt(ni) == 1 && 1646 nt->nt_keyixmap != NULL) { 1647 ieee80211_keyix keyix; 1648 /* 1649 * Check for a last reference in the key mapping table. 1650 */ 1651 keyix = ni->ni_ucastkey.wk_rxkeyix; 1652 if (keyix < nt->nt_keyixmax && 1653 nt->nt_keyixmap[keyix] == ni) { 1654 IEEE80211_DPRINTF(ni->ni_vap, 1655 IEEE80211_MSG_NODE, 1656 "%s: %p<%s> clear key map entry", __func__, 1657 ni, ether_sprintf(ni->ni_macaddr)); 1658 nt->nt_keyixmap[keyix] = NULL; 1659 ieee80211_node_decref(ni); /* XXX needed? */ 1660 _ieee80211_free_node(ni); 1661 } 1662 } 1663 IEEE80211_NODE_UNLOCK(nt); 1664 } else { 1665 if (ieee80211_node_dectestref(ni)) 1666 _ieee80211_free_node(ni); 1667 } 1668} 1669 1670/* 1671 * Reclaim a unicast key and clear any key cache state. 1672 */ 1673int 1674ieee80211_node_delucastkey(struct ieee80211_node *ni) 1675{ 1676 struct ieee80211com *ic = ni->ni_ic; 1677 struct ieee80211_node_table *nt = &ic->ic_sta; 1678 struct ieee80211_node *nikey; 1679 ieee80211_keyix keyix; 1680 int isowned, status; 1681 1682 /* 1683 * NB: We must beware of LOR here; deleting the key 1684 * can cause the crypto layer to block traffic updates 1685 * which can generate a LOR against the node table lock; 1686 * grab it here and stash the key index for our use below. 1687 * 1688 * Must also beware of recursion on the node table lock. 1689 * When called from node_cleanup we may already have 1690 * the node table lock held. Unfortunately there's no 1691 * way to separate out this path so we must do this 1692 * conditionally. 1693 */ 1694 isowned = IEEE80211_NODE_IS_LOCKED(nt); 1695 if (!isowned) 1696 IEEE80211_NODE_LOCK(nt); 1697 nikey = NULL; 1698 status = 1; /* NB: success */ 1699 if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) { 1700 keyix = ni->ni_ucastkey.wk_rxkeyix; 1701 status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey); 1702 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) { 1703 nikey = nt->nt_keyixmap[keyix]; 1704 nt->nt_keyixmap[keyix] = NULL;; 1705 } 1706 } 1707 if (!isowned) 1708 IEEE80211_NODE_UNLOCK(nt); 1709 1710 if (nikey != NULL) { 1711 KASSERT(nikey == ni, 1712 ("key map out of sync, ni %p nikey %p", ni, nikey)); 1713 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1714 "%s: delete key map entry %p<%s> refcnt %d\n", 1715 __func__, ni, ether_sprintf(ni->ni_macaddr), 1716 ieee80211_node_refcnt(ni)-1); 1717 ieee80211_free_node(ni); 1718 } 1719 return status; 1720} 1721 1722/* 1723 * Reclaim a node. If this is the last reference count then 1724 * do the normal free work. Otherwise remove it from the node 1725 * table and mark it gone by clearing the back-reference. 1726 */ 1727static void 1728node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1729{ 1730 ieee80211_keyix keyix; 1731 1732 IEEE80211_NODE_LOCK_ASSERT(nt); 1733 1734 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1735 "%s: remove %p<%s> from %s table, refcnt %d\n", 1736 __func__, ni, ether_sprintf(ni->ni_macaddr), 1737 nt->nt_name, ieee80211_node_refcnt(ni)-1); 1738 /* 1739 * Clear any entry in the unicast key mapping table. 1740 * We need to do it here so rx lookups don't find it 1741 * in the mapping table even if it's not in the hash 1742 * table. We cannot depend on the mapping table entry 1743 * being cleared because the node may not be free'd. 1744 */ 1745 keyix = ni->ni_ucastkey.wk_rxkeyix; 1746 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax && 1747 nt->nt_keyixmap[keyix] == ni) { 1748 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1749 "%s: %p<%s> clear key map entry %u\n", 1750 __func__, ni, ether_sprintf(ni->ni_macaddr), keyix); 1751 nt->nt_keyixmap[keyix] = NULL; 1752 ieee80211_node_decref(ni); /* NB: don't need free */ 1753 } 1754 if (!ieee80211_node_dectestref(ni)) { 1755 /* 1756 * Other references are present, just remove the 1757 * node from the table so it cannot be found. When 1758 * the references are dropped storage will be 1759 * reclaimed. 1760 */ 1761 TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1762 LIST_REMOVE(ni, ni_hash); 1763 ni->ni_table = NULL; /* clear reference */ 1764 } else 1765 _ieee80211_free_node(ni); 1766} 1767 1768/* 1769 * Node table support. 1770 */ 1771 1772static void 1773ieee80211_node_table_init(struct ieee80211com *ic, 1774 struct ieee80211_node_table *nt, 1775 const char *name, int inact, int keyixmax) 1776{ 1777 struct ifnet *ifp = ic->ic_ifp; 1778 1779 nt->nt_ic = ic; 1780 IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname); 1781 IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname); 1782 TAILQ_INIT(&nt->nt_node); 1783 nt->nt_name = name; 1784 nt->nt_scangen = 1; 1785 nt->nt_inact_init = inact; 1786 nt->nt_keyixmax = keyixmax; 1787 if (nt->nt_keyixmax > 0) { 1788 nt->nt_keyixmap = (struct ieee80211_node **) malloc( 1789 keyixmax * sizeof(struct ieee80211_node *), 1790 M_80211_NODE, M_NOWAIT | M_ZERO); 1791 if (nt->nt_keyixmap == NULL) 1792 if_printf(ic->ic_ifp, 1793 "Cannot allocate key index map with %u entries\n", 1794 keyixmax); 1795 } else 1796 nt->nt_keyixmap = NULL; 1797} 1798 1799static void 1800ieee80211_node_table_reset(struct ieee80211_node_table *nt, 1801 struct ieee80211vap *match) 1802{ 1803 struct ieee80211_node *ni, *next; 1804 1805 IEEE80211_NODE_LOCK(nt); 1806 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) { 1807 if (match != NULL && ni->ni_vap != match) 1808 continue; 1809 /* XXX can this happen? if so need's work */ 1810 if (ni->ni_associd != 0) { 1811 struct ieee80211vap *vap = ni->ni_vap; 1812 1813 if (vap->iv_auth->ia_node_leave != NULL) 1814 vap->iv_auth->ia_node_leave(ni); 1815 if (vap->iv_aid_bitmap != NULL) 1816 IEEE80211_AID_CLR(vap, ni->ni_associd); 1817 } 1818 ni->ni_wdsvap = NULL; /* clear reference */ 1819 node_reclaim(nt, ni); 1820 } 1821 if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) { 1822 /* 1823 * Make a separate pass to clear references to this vap 1824 * held by DWDS entries. They will not be matched above 1825 * because ni_vap will point to the ap vap but we still 1826 * need to clear ni_wdsvap when the WDS vap is destroyed 1827 * and/or reset. 1828 */ 1829 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) 1830 if (ni->ni_wdsvap == match) 1831 ni->ni_wdsvap = NULL; 1832 } 1833 IEEE80211_NODE_UNLOCK(nt); 1834} 1835 1836static void 1837ieee80211_node_table_cleanup(struct ieee80211_node_table *nt) 1838{ 1839 ieee80211_node_table_reset(nt, NULL); 1840 if (nt->nt_keyixmap != NULL) { 1841#ifdef DIAGNOSTIC 1842 /* XXX verify all entries are NULL */ 1843 int i; 1844 for (i = 0; i < nt->nt_keyixmax; i++) 1845 if (nt->nt_keyixmap[i] != NULL) 1846 printf("%s: %s[%u] still active\n", __func__, 1847 nt->nt_name, i); 1848#endif 1849 free(nt->nt_keyixmap, M_80211_NODE); 1850 nt->nt_keyixmap = NULL; 1851 } 1852 IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt); 1853 IEEE80211_NODE_LOCK_DESTROY(nt); 1854} 1855 1856/* 1857 * Timeout inactive stations and do related housekeeping. 1858 * Note that we cannot hold the node lock while sending a 1859 * frame as this would lead to a LOR. Instead we use a 1860 * generation number to mark nodes that we've scanned and 1861 * drop the lock and restart a scan if we have to time out 1862 * a node. Since we are single-threaded by virtue of 1863 * controlling the inactivity timer we can be sure this will 1864 * process each node only once. 1865 */ 1866static void 1867ieee80211_timeout_stations(struct ieee80211com *ic) 1868{ 1869 struct ieee80211_node_table *nt = &ic->ic_sta; 1870 struct ieee80211vap *vap; 1871 struct ieee80211_node *ni; 1872 int gen = 0; 1873 1874 IEEE80211_NODE_ITERATE_LOCK(nt); 1875 gen = ++nt->nt_scangen; 1876restart: 1877 IEEE80211_NODE_LOCK(nt); 1878 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1879 if (ni->ni_scangen == gen) /* previously handled */ 1880 continue; 1881 ni->ni_scangen = gen; 1882 /* 1883 * Ignore entries for which have yet to receive an 1884 * authentication frame. These are transient and 1885 * will be reclaimed when the last reference to them 1886 * goes away (when frame xmits complete). 1887 */ 1888 vap = ni->ni_vap; 1889 /* 1890 * Only process stations when in RUN state. This 1891 * insures, for example, that we don't timeout an 1892 * inactive station during CAC. Note that CSA state 1893 * is actually handled in ieee80211_node_timeout as 1894 * it applies to more than timeout processing. 1895 */ 1896 if (vap->iv_state != IEEE80211_S_RUN) 1897 continue; 1898 /* XXX can vap be NULL? */ 1899 if ((vap->iv_opmode == IEEE80211_M_HOSTAP || 1900 vap->iv_opmode == IEEE80211_M_STA) && 1901 (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 1902 continue; 1903 /* 1904 * Free fragment if not needed anymore 1905 * (last fragment older than 1s). 1906 * XXX doesn't belong here, move to node_age 1907 */ 1908 if (ni->ni_rxfrag[0] != NULL && 1909 ticks > ni->ni_rxfragstamp + hz) { 1910 m_freem(ni->ni_rxfrag[0]); 1911 ni->ni_rxfrag[0] = NULL; 1912 } 1913 if (ni->ni_inact > 0) { 1914 ni->ni_inact--; 1915 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 1916 "%s: inact %u inact_reload %u nrates %u", 1917 __func__, ni->ni_inact, ni->ni_inact_reload, 1918 ni->ni_rates.rs_nrates); 1919 } 1920 /* 1921 * Special case ourself; we may be idle for extended periods 1922 * of time and regardless reclaiming our state is wrong. 1923 * XXX run ic_node_age 1924 */ 1925 if (ni == vap->iv_bss) 1926 continue; 1927 if (ni->ni_associd != 0 || 1928 (vap->iv_opmode == IEEE80211_M_IBSS || 1929 vap->iv_opmode == IEEE80211_M_AHDEMO)) { 1930 /* 1931 * Age/drain resources held by the station. 1932 */ 1933 ic->ic_node_age(ni); 1934 /* 1935 * Probe the station before time it out. We 1936 * send a null data frame which may not be 1937 * universally supported by drivers (need it 1938 * for ps-poll support so it should be...). 1939 * 1940 * XXX don't probe the station unless we've 1941 * received a frame from them (and have 1942 * some idea of the rates they are capable 1943 * of); this will get fixed more properly 1944 * soon with better handling of the rate set. 1945 */ 1946 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) && 1947 (0 < ni->ni_inact && 1948 ni->ni_inact <= vap->iv_inact_probe) && 1949 ni->ni_rates.rs_nrates != 0) { 1950 IEEE80211_NOTE(vap, 1951 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, 1952 ni, "%s", 1953 "probe station due to inactivity"); 1954 /* 1955 * Grab a reference before unlocking the table 1956 * so the node cannot be reclaimed before we 1957 * send the frame. ieee80211_send_nulldata 1958 * understands we've done this and reclaims the 1959 * ref for us as needed. 1960 */ 1961 ieee80211_ref_node(ni); 1962 IEEE80211_NODE_UNLOCK(nt); 1963 ieee80211_send_nulldata(ni); 1964 /* XXX stat? */ 1965 goto restart; 1966 } 1967 } 1968 if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) && 1969 ni->ni_inact <= 0) { 1970 IEEE80211_NOTE(vap, 1971 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni, 1972 "station timed out due to inactivity " 1973 "(refcnt %u)", ieee80211_node_refcnt(ni)); 1974 /* 1975 * Send a deauthenticate frame and drop the station. 1976 * This is somewhat complicated due to reference counts 1977 * and locking. At this point a station will typically 1978 * have a reference count of 1. ieee80211_node_leave 1979 * will do a "free" of the node which will drop the 1980 * reference count. But in the meantime a reference 1981 * wil be held by the deauth frame. The actual reclaim 1982 * of the node will happen either after the tx is 1983 * completed or by ieee80211_node_leave. 1984 * 1985 * Separately we must drop the node lock before sending 1986 * in case the driver takes a lock, as this can result 1987 * in a LOR between the node lock and the driver lock. 1988 */ 1989 ieee80211_ref_node(ni); 1990 IEEE80211_NODE_UNLOCK(nt); 1991 if (ni->ni_associd != 0) { 1992 IEEE80211_SEND_MGMT(ni, 1993 IEEE80211_FC0_SUBTYPE_DEAUTH, 1994 IEEE80211_REASON_AUTH_EXPIRE); 1995 } 1996 ieee80211_node_leave(ni); 1997 ieee80211_free_node(ni); 1998 vap->iv_stats.is_node_timeout++; 1999 goto restart; 2000 } 2001 } 2002 IEEE80211_NODE_UNLOCK(nt); 2003 2004 IEEE80211_NODE_ITERATE_UNLOCK(nt); 2005} 2006 2007/* 2008 * Aggressively reclaim resources. This should be used 2009 * only in a critical situation to reclaim mbuf resources. 2010 */ 2011void 2012ieee80211_drain(struct ieee80211com *ic) 2013{ 2014 struct ieee80211_node_table *nt = &ic->ic_sta; 2015 struct ieee80211vap *vap; 2016 struct ieee80211_node *ni; 2017 2018 IEEE80211_NODE_LOCK(nt); 2019 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 2020 /* 2021 * Ignore entries for which have yet to receive an 2022 * authentication frame. These are transient and 2023 * will be reclaimed when the last reference to them 2024 * goes away (when frame xmits complete). 2025 */ 2026 vap = ni->ni_vap; 2027 /* 2028 * Only process stations when in RUN state. This 2029 * insures, for example, that we don't timeout an 2030 * inactive station during CAC. Note that CSA state 2031 * is actually handled in ieee80211_node_timeout as 2032 * it applies to more than timeout processing. 2033 */ 2034 if (vap->iv_state != IEEE80211_S_RUN) 2035 continue; 2036 /* XXX can vap be NULL? */ 2037 if ((vap->iv_opmode == IEEE80211_M_HOSTAP || 2038 vap->iv_opmode == IEEE80211_M_STA) && 2039 (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 2040 continue; 2041 /* 2042 * Free fragments. 2043 * XXX doesn't belong here, move to node_drain 2044 */ 2045 if (ni->ni_rxfrag[0] != NULL) { 2046 m_freem(ni->ni_rxfrag[0]); 2047 ni->ni_rxfrag[0] = NULL; 2048 } 2049 /* 2050 * Drain resources held by the station. 2051 */ 2052 ic->ic_node_drain(ni); 2053 } 2054 IEEE80211_NODE_UNLOCK(nt); 2055} 2056 2057/* 2058 * Per-ieee80211com inactivity timer callback. 2059 */ 2060void 2061ieee80211_node_timeout(void *arg) 2062{ 2063 struct ieee80211com *ic = arg; 2064 2065 /* 2066 * Defer timeout processing if a channel switch is pending. 2067 * We typically need to be mute so not doing things that 2068 * might generate frames is good to handle in one place. 2069 * Supressing the station timeout processing may extend the 2070 * lifetime of inactive stations (by not decrementing their 2071 * idle counters) but this should be ok unless the CSA is 2072 * active for an unusually long time. 2073 */ 2074 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) { 2075 ieee80211_scan_timeout(ic); 2076 ieee80211_timeout_stations(ic); 2077 2078 IEEE80211_LOCK(ic); 2079 ieee80211_erp_timeout(ic); 2080 ieee80211_ht_timeout(ic); 2081 IEEE80211_UNLOCK(ic); 2082 } 2083 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz, 2084 ieee80211_node_timeout, ic); 2085} 2086 2087void 2088ieee80211_iterate_nodes(struct ieee80211_node_table *nt, 2089 ieee80211_iter_func *f, void *arg) 2090{ 2091 struct ieee80211_node *ni; 2092 u_int gen; 2093 2094 IEEE80211_NODE_ITERATE_LOCK(nt); 2095 gen = ++nt->nt_scangen; 2096restart: 2097 IEEE80211_NODE_LOCK(nt); 2098 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 2099 if (ni->ni_scangen != gen) { 2100 ni->ni_scangen = gen; 2101 (void) ieee80211_ref_node(ni); 2102 IEEE80211_NODE_UNLOCK(nt); 2103 (*f)(arg, ni); 2104 ieee80211_free_node(ni); 2105 goto restart; 2106 } 2107 } 2108 IEEE80211_NODE_UNLOCK(nt); 2109 2110 IEEE80211_NODE_ITERATE_UNLOCK(nt); 2111} 2112 2113void 2114ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 2115{ 2116 printf("0x%p: mac %s refcnt %d\n", ni, 2117 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 2118 printf("\tscangen %u authmode %u flags 0x%x\n", 2119 ni->ni_scangen, ni->ni_authmode, ni->ni_flags); 2120 printf("\tassocid 0x%x txpower %u vlan %u\n", 2121 ni->ni_associd, ni->ni_txpower, ni->ni_vlan); 2122 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n", 2123 ni->ni_txseqs[IEEE80211_NONQOS_TID], 2124 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT, 2125 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK, 2126 ni->ni_rxfragstamp); 2127 printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n", 2128 ni->ni_rstamp, node_getrssi(ni), ni->ni_noise, 2129 ni->ni_intval, ni->ni_capinfo); 2130 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n", 2131 ether_sprintf(ni->ni_bssid), 2132 ni->ni_esslen, ni->ni_essid, 2133 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags); 2134 printf("\tinact %u inact_reload %u txrate %u\n", 2135 ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate); 2136 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n", 2137 ni->ni_htcap, ni->ni_htparam, 2138 ni->ni_htctlchan, ni->ni_ht2ndchan); 2139 printf("\thtopmode %x htstbc %x chw %u\n", 2140 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw); 2141} 2142 2143void 2144ieee80211_dump_nodes(struct ieee80211_node_table *nt) 2145{ 2146 ieee80211_iterate_nodes(nt, 2147 (ieee80211_iter_func *) ieee80211_dump_node, nt); 2148} 2149 2150static void 2151ieee80211_notify_erp_locked(struct ieee80211com *ic) 2152{ 2153 struct ieee80211vap *vap; 2154 2155 IEEE80211_LOCK_ASSERT(ic); 2156 2157 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 2158 if (vap->iv_opmode == IEEE80211_M_HOSTAP) 2159 ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP); 2160} 2161 2162void 2163ieee80211_notify_erp(struct ieee80211com *ic) 2164{ 2165 IEEE80211_LOCK(ic); 2166 ieee80211_notify_erp_locked(ic); 2167 IEEE80211_UNLOCK(ic); 2168} 2169 2170/* 2171 * Handle a station joining an 11g network. 2172 */ 2173static void 2174ieee80211_node_join_11g(struct ieee80211_node *ni) 2175{ 2176 struct ieee80211com *ic = ni->ni_ic; 2177 2178 IEEE80211_LOCK_ASSERT(ic); 2179 2180 /* 2181 * Station isn't capable of short slot time. Bump 2182 * the count of long slot time stations and disable 2183 * use of short slot time. Note that the actual switch 2184 * over to long slot time use may not occur until the 2185 * next beacon transmission (per sec. 7.3.1.4 of 11g). 2186 */ 2187 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2188 ic->ic_longslotsta++; 2189 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2190 "station needs long slot time, count %d", 2191 ic->ic_longslotsta); 2192 /* XXX vap's w/ conflicting needs won't work */ 2193 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) { 2194 /* 2195 * Don't force slot time when switched to turbo 2196 * mode as non-ERP stations won't be present; this 2197 * need only be done when on the normal G channel. 2198 */ 2199 ieee80211_set_shortslottime(ic, 0); 2200 } 2201 } 2202 /* 2203 * If the new station is not an ERP station 2204 * then bump the counter and enable protection 2205 * if configured. 2206 */ 2207 if (!ieee80211_iserp_rateset(&ni->ni_rates)) { 2208 ic->ic_nonerpsta++; 2209 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2210 "station is !ERP, %d non-ERP stations associated", 2211 ic->ic_nonerpsta); 2212 /* 2213 * If station does not support short preamble 2214 * then we must enable use of Barker preamble. 2215 */ 2216 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) { 2217 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2218 "%s", "station needs long preamble"); 2219 ic->ic_flags |= IEEE80211_F_USEBARKER; 2220 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 2221 } 2222 /* 2223 * If protection is configured and this is the first 2224 * indication we should use protection, enable it. 2225 */ 2226 if (ic->ic_protmode != IEEE80211_PROT_NONE && 2227 ic->ic_nonerpsta == 1 && 2228 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) { 2229 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC, 2230 "%s: enable use of protection\n", __func__); 2231 ic->ic_flags |= IEEE80211_F_USEPROT; 2232 ieee80211_notify_erp_locked(ic); 2233 } 2234 } else 2235 ni->ni_flags |= IEEE80211_NODE_ERP; 2236} 2237 2238void 2239ieee80211_node_join(struct ieee80211_node *ni, int resp) 2240{ 2241 struct ieee80211com *ic = ni->ni_ic; 2242 struct ieee80211vap *vap = ni->ni_vap; 2243 int newassoc; 2244 2245 if (ni->ni_associd == 0) { 2246 uint16_t aid; 2247 2248 KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap")); 2249 /* 2250 * It would be good to search the bitmap 2251 * more efficiently, but this will do for now. 2252 */ 2253 for (aid = 1; aid < vap->iv_max_aid; aid++) { 2254 if (!IEEE80211_AID_ISSET(vap, aid)) 2255 break; 2256 } 2257 if (aid >= vap->iv_max_aid) { 2258 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY); 2259 ieee80211_node_leave(ni); 2260 return; 2261 } 2262 ni->ni_associd = aid | 0xc000; 2263 ni->ni_jointime = time_uptime; 2264 IEEE80211_LOCK(ic); 2265 IEEE80211_AID_SET(vap, ni->ni_associd); 2266 vap->iv_sta_assoc++; 2267 ic->ic_sta_assoc++; 2268 2269 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan)) 2270 ieee80211_ht_node_join(ni); 2271 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) && 2272 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan)) 2273 ieee80211_node_join_11g(ni); 2274 IEEE80211_UNLOCK(ic); 2275 2276 newassoc = 1; 2277 } else 2278 newassoc = 0; 2279 2280 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni, 2281 "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s", 2282 IEEE80211_NODE_AID(ni), 2283 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long", 2284 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", 2285 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "", 2286 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "", 2287 ni->ni_flags & IEEE80211_NODE_HT ? 2288 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "", 2289 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "", 2290 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" : 2291 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "", 2292 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "", 2293 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ? 2294 ", fast-frames" : "", 2295 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ? 2296 ", turbo" : "" 2297 ); 2298 2299 node_setuptxparms(ni); 2300 /* give driver a chance to setup state like ni_txrate */ 2301 if (ic->ic_newassoc != NULL) 2302 ic->ic_newassoc(ni, newassoc); 2303 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS); 2304 /* tell the authenticator about new station */ 2305 if (vap->iv_auth->ia_node_join != NULL) 2306 vap->iv_auth->ia_node_join(ni); 2307 ieee80211_notify_node_join(ni, 2308 resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 2309} 2310 2311static void 2312disable_protection(struct ieee80211com *ic) 2313{ 2314 KASSERT(ic->ic_nonerpsta == 0 && 2315 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0, 2316 ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta, 2317 ic->ic_flags_ext)); 2318 2319 ic->ic_flags &= ~IEEE80211_F_USEPROT; 2320 /* XXX verify mode? */ 2321 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) { 2322 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 2323 ic->ic_flags &= ~IEEE80211_F_USEBARKER; 2324 } 2325 ieee80211_notify_erp_locked(ic); 2326} 2327 2328/* 2329 * Handle a station leaving an 11g network. 2330 */ 2331static void 2332ieee80211_node_leave_11g(struct ieee80211_node *ni) 2333{ 2334 struct ieee80211com *ic = ni->ni_ic; 2335 2336 IEEE80211_LOCK_ASSERT(ic); 2337 2338 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan), 2339 ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq, 2340 ic->ic_bsschan->ic_flags)); 2341 2342 /* 2343 * If a long slot station do the slot time bookkeeping. 2344 */ 2345 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2346 KASSERT(ic->ic_longslotsta > 0, 2347 ("bogus long slot station count %d", ic->ic_longslotsta)); 2348 ic->ic_longslotsta--; 2349 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2350 "long slot time station leaves, count now %d", 2351 ic->ic_longslotsta); 2352 if (ic->ic_longslotsta == 0) { 2353 /* 2354 * Re-enable use of short slot time if supported 2355 * and not operating in IBSS mode (per spec). 2356 */ 2357 if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 2358 ic->ic_opmode != IEEE80211_M_IBSS) { 2359 IEEE80211_DPRINTF(ni->ni_vap, 2360 IEEE80211_MSG_ASSOC, 2361 "%s: re-enable use of short slot time\n", 2362 __func__); 2363 ieee80211_set_shortslottime(ic, 1); 2364 } 2365 } 2366 } 2367 /* 2368 * If a non-ERP station do the protection-related bookkeeping. 2369 */ 2370 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) { 2371 KASSERT(ic->ic_nonerpsta > 0, 2372 ("bogus non-ERP station count %d", ic->ic_nonerpsta)); 2373 ic->ic_nonerpsta--; 2374 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2375 "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta, 2376 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ? 2377 " (non-ERP sta present)" : ""); 2378 if (ic->ic_nonerpsta == 0 && 2379 (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) { 2380 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC, 2381 "%s: disable use of protection\n", __func__); 2382 disable_protection(ic); 2383 } 2384 } 2385} 2386 2387/* 2388 * Time out presence of an overlapping bss with non-ERP 2389 * stations. When operating in hostap mode we listen for 2390 * beacons from other stations and if we identify a non-ERP 2391 * station is present we enable protection. To identify 2392 * when all non-ERP stations are gone we time out this 2393 * condition. 2394 */ 2395static void 2396ieee80211_erp_timeout(struct ieee80211com *ic) 2397{ 2398 2399 IEEE80211_LOCK_ASSERT(ic); 2400 2401 if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) && 2402 time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) { 2403#if 0 2404 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni, 2405 "%s", "age out non-ERP sta present on channel"); 2406#endif 2407 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR; 2408 if (ic->ic_nonerpsta == 0) 2409 disable_protection(ic); 2410 } 2411} 2412 2413/* 2414 * Handle bookkeeping for station deauthentication/disassociation 2415 * when operating as an ap. 2416 */ 2417void 2418ieee80211_node_leave(struct ieee80211_node *ni) 2419{ 2420 struct ieee80211com *ic = ni->ni_ic; 2421 struct ieee80211vap *vap = ni->ni_vap; 2422 struct ieee80211_node_table *nt = ni->ni_table; 2423 2424 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni, 2425 "station with aid %d leaves", IEEE80211_NODE_AID(ni)); 2426 2427 KASSERT(vap->iv_opmode != IEEE80211_M_STA, 2428 ("unexpected operating mode %u", vap->iv_opmode)); 2429 /* 2430 * If node wasn't previously associated all 2431 * we need to do is reclaim the reference. 2432 */ 2433 /* XXX ibss mode bypasses 11g and notification */ 2434 if (ni->ni_associd == 0) 2435 goto done; 2436 /* 2437 * Tell the authenticator the station is leaving. 2438 * Note that we must do this before yanking the 2439 * association id as the authenticator uses the 2440 * associd to locate it's state block. 2441 */ 2442 if (vap->iv_auth->ia_node_leave != NULL) 2443 vap->iv_auth->ia_node_leave(ni); 2444 2445 IEEE80211_LOCK(ic); 2446 IEEE80211_AID_CLR(vap, ni->ni_associd); 2447 ni->ni_associd = 0; 2448 vap->iv_sta_assoc--; 2449 ic->ic_sta_assoc--; 2450 2451 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan)) 2452 ieee80211_ht_node_leave(ni); 2453 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) && 2454 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan)) 2455 ieee80211_node_leave_11g(ni); 2456 IEEE80211_UNLOCK(ic); 2457 /* 2458 * Cleanup station state. In particular clear various 2459 * state that might otherwise be reused if the node 2460 * is reused before the reference count goes to zero 2461 * (and memory is reclaimed). 2462 */ 2463 ieee80211_sta_leave(ni); 2464done: 2465 /* 2466 * Remove the node from any table it's recorded in and 2467 * drop the caller's reference. Removal from the table 2468 * is important to insure the node is not reprocessed 2469 * for inactivity. 2470 */ 2471 if (nt != NULL) { 2472 IEEE80211_NODE_LOCK(nt); 2473 node_reclaim(nt, ni); 2474 IEEE80211_NODE_UNLOCK(nt); 2475 } else 2476 ieee80211_free_node(ni); 2477} 2478 2479struct rssiinfo { 2480 struct ieee80211vap *vap; 2481 int rssi_samples; 2482 uint32_t rssi_total; 2483}; 2484 2485static void 2486get_hostap_rssi(void *arg, struct ieee80211_node *ni) 2487{ 2488 struct rssiinfo *info = arg; 2489 struct ieee80211vap *vap = ni->ni_vap; 2490 int8_t rssi; 2491 2492 if (info->vap != vap) 2493 return; 2494 /* only associated stations */ 2495 if (ni->ni_associd == 0) 2496 return; 2497 rssi = vap->iv_ic->ic_node_getrssi(ni); 2498 if (rssi != 0) { 2499 info->rssi_samples++; 2500 info->rssi_total += rssi; 2501 } 2502} 2503 2504static void 2505get_adhoc_rssi(void *arg, struct ieee80211_node *ni) 2506{ 2507 struct rssiinfo *info = arg; 2508 struct ieee80211vap *vap = ni->ni_vap; 2509 int8_t rssi; 2510 2511 if (info->vap != vap) 2512 return; 2513 /* only neighbors */ 2514 /* XXX check bssid */ 2515 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 2516 return; 2517 rssi = vap->iv_ic->ic_node_getrssi(ni); 2518 if (rssi != 0) { 2519 info->rssi_samples++; 2520 info->rssi_total += rssi; 2521 } 2522} 2523 2524int8_t 2525ieee80211_getrssi(struct ieee80211vap *vap) 2526{ 2527#define NZ(x) ((x) == 0 ? 1 : (x)) 2528 struct ieee80211com *ic = vap->iv_ic; 2529 struct rssiinfo info; 2530 2531 info.rssi_total = 0; 2532 info.rssi_samples = 0; 2533 info.vap = vap; 2534 switch (vap->iv_opmode) { 2535 case IEEE80211_M_IBSS: /* average of all ibss neighbors */ 2536 case IEEE80211_M_AHDEMO: /* average of all neighbors */ 2537 ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info); 2538 break; 2539 case IEEE80211_M_HOSTAP: /* average of all associated stations */ 2540 ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info); 2541 break; 2542 case IEEE80211_M_MONITOR: /* XXX */ 2543 case IEEE80211_M_STA: /* use stats from associated ap */ 2544 default: 2545 if (vap->iv_bss != NULL) 2546 info.rssi_total = ic->ic_node_getrssi(vap->iv_bss); 2547 info.rssi_samples = 1; 2548 break; 2549 } 2550 return info.rssi_total / NZ(info.rssi_samples); 2551#undef NZ 2552} 2553 2554void 2555ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise) 2556{ 2557 2558 if (vap->iv_bss == NULL) /* NB: shouldn't happen */ 2559 return; 2560 vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise); 2561 /* for non-station mode return avg'd rssi accounting */ 2562 if (vap->iv_opmode != IEEE80211_M_STA) 2563 *rssi = ieee80211_getrssi(vap); 2564} 2565