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