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