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