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