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