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