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