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