ieee80211_node.c revision 172063
1/*- 2 * Copyright (c) 2001 Atsushi Onoe 3 * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 172063 2007-09-06 00:08:02Z sam $"); 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/mbuf.h> 33#include <sys/malloc.h> 34#include <sys/kernel.h> 35 36#include <sys/socket.h> 37 38#include <net/if.h> 39#include <net/if_media.h> 40#include <net/ethernet.h> 41 42#include <net80211/ieee80211_var.h> 43 44#include <net/bpf.h> 45 46/* 47 * Association id's are managed with a bit vector. 48 */ 49#define IEEE80211_AID_SET(b, w) \ 50 ((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32))) 51#define IEEE80211_AID_CLR(b, w) \ 52 ((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32))) 53#define IEEE80211_AID_ISSET(b, w) \ 54 ((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32))) 55 56#ifdef IEEE80211_DEBUG_REFCNT 57#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line 58#else 59#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__ 60#endif 61 62static int ieee80211_sta_join1(struct ieee80211_node *); 63 64static struct ieee80211_node *node_alloc(struct ieee80211_node_table *); 65static void node_cleanup(struct ieee80211_node *); 66static void node_free(struct ieee80211_node *); 67static int8_t node_getrssi(const struct ieee80211_node *); 68static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *); 69 70static void ieee80211_setup_node(struct ieee80211_node_table *, 71 struct ieee80211_node *, const uint8_t *); 72static void _ieee80211_free_node(struct ieee80211_node *); 73 74static void ieee80211_node_table_init(struct ieee80211com *ic, 75 struct ieee80211_node_table *nt, const char *name, 76 int inact, int keymaxix); 77static void ieee80211_node_table_reset(struct ieee80211_node_table *); 78static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt); 79 80MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state"); 81 82void 83ieee80211_node_attach(struct ieee80211com *ic) 84{ 85 86 ic->ic_node_alloc = node_alloc; 87 ic->ic_node_free = node_free; 88 ic->ic_node_cleanup = node_cleanup; 89 ic->ic_node_getrssi = node_getrssi; 90 ic->ic_node_getsignal = node_getsignal; 91 92 /* default station inactivity timer setings */ 93 ic->ic_inact_init = IEEE80211_INACT_INIT; 94 ic->ic_inact_auth = IEEE80211_INACT_AUTH; 95 ic->ic_inact_run = IEEE80211_INACT_RUN; 96 ic->ic_inact_probe = IEEE80211_INACT_PROBE; 97 98 callout_init(&ic->ic_inact, CALLOUT_MPSAFE); 99 100 /* NB: driver should override */ 101 ic->ic_max_aid = IEEE80211_AID_DEF; 102 103 ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */ 104} 105 106void 107ieee80211_node_lateattach(struct ieee80211com *ic) 108{ 109 struct ieee80211_rsnparms *rsn; 110 111 if (ic->ic_max_aid > IEEE80211_AID_MAX) 112 ic->ic_max_aid = IEEE80211_AID_MAX; 113 MALLOC(ic->ic_aid_bitmap, uint32_t *, 114 howmany(ic->ic_max_aid, 32) * sizeof(uint32_t), 115 M_80211_NODE, M_NOWAIT | M_ZERO); 116 if (ic->ic_aid_bitmap == NULL) { 117 /* XXX no way to recover */ 118 printf("%s: no memory for AID bitmap!\n", __func__); 119 ic->ic_max_aid = 0; 120 } 121 122 ieee80211_node_table_init(ic, &ic->ic_sta, "station", 123 IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix); 124 125 ieee80211_reset_bss(ic); 126 /* 127 * Setup "global settings" in the bss node so that 128 * each new station automatically inherits them. 129 */ 130 rsn = &ic->ic_bss->ni_rsn; 131 /* WEP, TKIP, and AES-CCM are always supported */ 132 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP; 133 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP; 134 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM; 135 if (ic->ic_caps & IEEE80211_C_AES) 136 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB; 137 if (ic->ic_caps & IEEE80211_C_CKIP) 138 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP; 139 /* 140 * Default unicast cipher to WEP for 802.1x use. If 141 * WPA is enabled the management code will set these 142 * values to reflect. 143 */ 144 rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP; 145 rsn->rsn_ucastkeylen = 104 / NBBY; 146 /* 147 * WPA says the multicast cipher is the lowest unicast 148 * cipher supported. But we skip WEP which would 149 * otherwise be used based on this criteria. 150 */ 151 rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP; 152 rsn->rsn_mcastkeylen = 128 / NBBY; 153 154 /* 155 * We support both WPA-PSK and 802.1x; the one used 156 * is determined by the authentication mode and the 157 * setting of the PSK state. 158 */ 159 rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK; 160 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK; 161 162 ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode); 163} 164 165void 166ieee80211_node_detach(struct ieee80211com *ic) 167{ 168 169 if (ic->ic_bss != NULL) { 170 ieee80211_free_node(ic->ic_bss); 171 ic->ic_bss = NULL; 172 } 173 ieee80211_node_table_cleanup(&ic->ic_sta); 174 if (ic->ic_aid_bitmap != NULL) { 175 FREE(ic->ic_aid_bitmap, M_80211_NODE); 176 ic->ic_aid_bitmap = NULL; 177 } 178} 179 180/* 181 * Port authorize/unauthorize interfaces for use by an authenticator. 182 */ 183 184void 185ieee80211_node_authorize(struct ieee80211_node *ni) 186{ 187 struct ieee80211com *ic = ni->ni_ic; 188 189 ni->ni_flags |= IEEE80211_NODE_AUTH; 190 ni->ni_inact_reload = ic->ic_inact_run; 191 ni->ni_inact = ni->ni_inact_reload; 192} 193 194void 195ieee80211_node_unauthorize(struct ieee80211_node *ni) 196{ 197 struct ieee80211com *ic = ni->ni_ic; 198 199 ni->ni_flags &= ~IEEE80211_NODE_AUTH; 200 ni->ni_inact_reload = ic->ic_inact_auth; 201 if (ni->ni_inact > ni->ni_inact_reload) 202 ni->ni_inact = ni->ni_inact_reload; 203} 204 205/* 206 * Set/change the channel. The rate set is also updated as 207 * to insure a consistent view by drivers. 208 */ 209static void 210ieee80211_node_set_chan(struct ieee80211com *ic, struct ieee80211_node *ni) 211{ 212 struct ieee80211_channel *chan = ic->ic_bsschan; 213 214#if 0 215 KASSERT(chan != IEEE80211_CHAN_ANYC, ("bss channel not setup")); 216#else 217 if (chan == IEEE80211_CHAN_ANYC) /* XXX while scanning */ 218 chan = ic->ic_curchan; 219#endif 220 ni->ni_chan = chan; 221 if (IEEE80211_IS_CHAN_HT(chan)) { 222 /* 223 * XXX Gotta be careful here; the rate set returned by 224 * ieee80211_get_suprates is actually any HT rate 225 * set so blindly copying it will be bad. We must 226 * install the legacy rate est in ni_rates and the 227 * HT rate set in ni_htrates. 228 */ 229 ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan); 230 } 231 ni->ni_rates = *ieee80211_get_suprates(ic, chan); 232} 233 234/* 235 * Probe the curent channel, if allowed, while scanning. 236 * If the channel is not marked passive-only then send 237 * a probe request immediately. Otherwise mark state and 238 * listen for beacons on the channel; if we receive something 239 * then we'll transmit a probe request. 240 */ 241void 242ieee80211_probe_curchan(struct ieee80211com *ic, int force) 243{ 244 struct ifnet *ifp = ic->ic_ifp; 245 246 if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) { 247 /* 248 * XXX send both broadcast+directed probe request 249 */ 250 ieee80211_send_probereq(ic->ic_bss, 251 ic->ic_myaddr, ifp->if_broadcastaddr, 252 ifp->if_broadcastaddr, 253 ic->ic_des_ssid[0].ssid, ic->ic_des_ssid[0].len, 254 ic->ic_opt_ie, ic->ic_opt_ie_len); 255 } else 256 ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN; 257} 258 259static __inline void 260copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss) 261{ 262 /* propagate useful state */ 263 nbss->ni_authmode = obss->ni_authmode; 264 nbss->ni_txpower = obss->ni_txpower; 265 nbss->ni_vlan = obss->ni_vlan; 266 nbss->ni_rsn = obss->ni_rsn; 267 /* XXX statistics? */ 268} 269 270void 271ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 272{ 273 struct ieee80211_node_table *nt; 274 struct ieee80211_node *ni; 275 276 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 277 "%s: creating ibss\n", __func__); 278 279 /* 280 * Create the station/neighbor table. Note that for adhoc 281 * mode we make the initial inactivity timer longer since 282 * we create nodes only through discovery and they typically 283 * are long-lived associations. 284 */ 285 nt = &ic->ic_sta; 286 IEEE80211_NODE_LOCK(nt); 287 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 288 nt->nt_name = "station"; 289 nt->nt_inact_init = ic->ic_inact_init; 290 } else { 291 nt->nt_name = "neighbor"; 292 nt->nt_inact_init = ic->ic_inact_run; 293 } 294 IEEE80211_NODE_UNLOCK(nt); 295 296 ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr); 297 if (ni == NULL) { 298 /* XXX recovery? */ 299 return; 300 } 301 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 302 ni->ni_esslen = ic->ic_des_ssid[0].len; 303 memcpy(ni->ni_essid, ic->ic_des_ssid[0].ssid, ni->ni_esslen); 304 if (ic->ic_bss != NULL) 305 copy_bss(ni, ic->ic_bss); 306 ni->ni_intval = ic->ic_bintval; 307 if (ic->ic_flags & IEEE80211_F_PRIVACY) 308 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 309 if (ic->ic_phytype == IEEE80211_T_FH) { 310 ni->ni_fhdwell = 200; /* XXX */ 311 ni->ni_fhindex = 1; 312 } 313 if (ic->ic_opmode == IEEE80211_M_IBSS) { 314 ic->ic_flags |= IEEE80211_F_SIBSS; 315 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */ 316 if (ic->ic_flags & IEEE80211_F_DESBSSID) 317 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid); 318 else { 319 get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN); 320 /* clear group bit, add local bit */ 321 ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02; 322 } 323 } else if (ic->ic_opmode == IEEE80211_M_AHDEMO) { 324 if (ic->ic_flags & IEEE80211_F_DESBSSID) 325 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid); 326 else 327 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN); 328 } 329 /* 330 * Fix the channel and related attributes. 331 */ 332 ic->ic_bsschan = chan; 333 ieee80211_node_set_chan(ic, ni); 334 ic->ic_curmode = ieee80211_chan2mode(chan); 335 /* 336 * Do mode-specific rate setup. 337 */ 338 if (IEEE80211_IS_CHAN_FULL(chan)) { 339 if (IEEE80211_IS_CHAN_ANYG(chan)) { 340 /* 341 * Use a mixed 11b/11g rate set. 342 */ 343 ieee80211_set11gbasicrates(&ni->ni_rates, 344 IEEE80211_MODE_11G); 345 } else if (IEEE80211_IS_CHAN_B(chan)) { 346 /* 347 * Force pure 11b rate set. 348 */ 349 ieee80211_set11gbasicrates(&ni->ni_rates, 350 IEEE80211_MODE_11B); 351 } 352 } 353 354 (void) ieee80211_sta_join1(ieee80211_ref_node(ni)); 355} 356 357/* 358 * Reset bss state on transition to the INIT state. 359 * Clear any stations from the table (they have been 360 * deauth'd) and reset the bss node (clears key, rate 361 * etc. state). 362 */ 363void 364ieee80211_reset_bss(struct ieee80211com *ic) 365{ 366 struct ieee80211_node *ni, *obss; 367 368 callout_drain(&ic->ic_inact); 369 ieee80211_node_table_reset(&ic->ic_sta); 370 ieee80211_reset_erp(ic); 371 372 ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr); 373 KASSERT(ni != NULL, ("unable to setup inital BSS node")); 374 obss = ic->ic_bss; 375 ic->ic_bss = ieee80211_ref_node(ni); 376 if (obss != NULL) { 377 copy_bss(ni, obss); 378 ni->ni_intval = ic->ic_bintval; 379 ieee80211_free_node(obss); 380 } 381} 382 383static int 384match_ssid(const struct ieee80211_node *ni, 385 int nssid, const struct ieee80211_scan_ssid ssids[]) 386{ 387 int i; 388 389 for (i = 0; i < nssid; i++) { 390 if (ni->ni_esslen == ssids[i].len && 391 memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0) 392 return 1; 393 } 394 return 0; 395} 396 397/* 398 * Test a node for suitability/compatibility. 399 */ 400static int 401check_bss(struct ieee80211com *ic, struct ieee80211_node *ni) 402{ 403 uint8_t rate; 404 405 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 406 return 0; 407 if (ic->ic_opmode == IEEE80211_M_IBSS) { 408 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 409 return 0; 410 } else { 411 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 412 return 0; 413 } 414 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 415 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 416 return 0; 417 } else { 418 /* XXX does this mean privacy is supported or required? */ 419 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 420 return 0; 421 } 422 rate = ieee80211_fix_rate(ni, &ni->ni_rates, 423 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 424 if (rate & IEEE80211_RATE_BASIC) 425 return 0; 426 if (ic->ic_des_nssid != 0 && 427 !match_ssid(ni, ic->ic_des_nssid, ic->ic_des_ssid)) 428 return 0; 429 if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 430 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 431 return 0; 432 return 1; 433} 434 435#ifdef IEEE80211_DEBUG 436/* 437 * Display node suitability/compatibility. 438 */ 439static void 440check_bss_debug(struct ieee80211com *ic, struct ieee80211_node *ni) 441{ 442 uint8_t rate; 443 int fail; 444 445 fail = 0; 446 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 447 fail |= 0x01; 448 if (ic->ic_opmode == IEEE80211_M_IBSS) { 449 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 450 fail |= 0x02; 451 } else { 452 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 453 fail |= 0x02; 454 } 455 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 456 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 457 fail |= 0x04; 458 } else { 459 /* XXX does this mean privacy is supported or required? */ 460 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 461 fail |= 0x04; 462 } 463 rate = ieee80211_fix_rate(ni, &ni->ni_rates, 464 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 465 if (rate & IEEE80211_RATE_BASIC) 466 fail |= 0x08; 467 if (ic->ic_des_nssid != 0 && 468 !match_ssid(ni, ic->ic_des_nssid, ic->ic_des_ssid)) 469 fail |= 0x10; 470 if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 471 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 472 fail |= 0x20; 473 474 printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr)); 475 printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' '); 476 printf(" %3d%c", 477 ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' '); 478 printf(" %+4d", ni->ni_rssi); 479 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 480 fail & 0x08 ? '!' : ' '); 481 printf(" %4s%c", 482 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 483 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 484 "????", 485 fail & 0x02 ? '!' : ' '); 486 printf(" %3s%c ", 487 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no", 488 fail & 0x04 ? '!' : ' '); 489 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 490 printf("%s\n", fail & 0x10 ? "!" : ""); 491} 492#endif /* IEEE80211_DEBUG */ 493 494/* 495 * Handle 802.11 ad hoc network merge. The 496 * convention, set by the Wireless Ethernet Compatibility Alliance 497 * (WECA), is that an 802.11 station will change its BSSID to match 498 * the "oldest" 802.11 ad hoc network, on the same channel, that 499 * has the station's desired SSID. The "oldest" 802.11 network 500 * sends beacons with the greatest TSF timestamp. 501 * 502 * The caller is assumed to validate TSF's before attempting a merge. 503 * 504 * Return !0 if the BSSID changed, 0 otherwise. 505 */ 506int 507ieee80211_ibss_merge(struct ieee80211_node *ni) 508{ 509 struct ieee80211com *ic = ni->ni_ic; 510 511 if (ni == ic->ic_bss || 512 IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) { 513 /* unchanged, nothing to do */ 514 return 0; 515 } 516 if (!check_bss(ic, ni)) { 517 /* capabilities mismatch */ 518 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 519 "%s: merge failed, capabilities mismatch\n", __func__); 520#ifdef IEEE80211_DEBUG 521 if (ieee80211_msg_assoc(ic)) 522 check_bss_debug(ic, ni); 523#endif 524 ic->ic_stats.is_ibss_capmismatch++; 525 return 0; 526 } 527 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 528 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__, 529 ether_sprintf(ni->ni_bssid), 530 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 531 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long", 532 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "" 533 ); 534 return ieee80211_sta_join1(ieee80211_ref_node(ni)); 535} 536 537/* 538 * Join the specified IBSS/BSS network. The node is assumed to 539 * be passed in with a held reference. 540 */ 541static int 542ieee80211_sta_join1(struct ieee80211_node *selbs) 543{ 544 struct ieee80211com *ic = selbs->ni_ic; 545 struct ieee80211_node *obss; 546 int canreassoc; 547 548 if (ic->ic_opmode == IEEE80211_M_IBSS) { 549 struct ieee80211_node_table *nt; 550 /* 551 * Fillin the neighbor table; it will already 552 * exist if we are simply switching mastership. 553 * XXX ic_sta always setup so this is unnecessary? 554 */ 555 nt = &ic->ic_sta; 556 IEEE80211_NODE_LOCK(nt); 557 nt->nt_name = "neighbor"; 558 nt->nt_inact_init = ic->ic_inact_run; 559 IEEE80211_NODE_UNLOCK(nt); 560 } 561 562 /* 563 * Committed to selbs, setup state. 564 */ 565 obss = ic->ic_bss; 566 /* 567 * Check if old+new node have the same address in which 568 * case we can reassociate when operating in sta mode. 569 */ 570 canreassoc = (obss != NULL && 571 ic->ic_state == IEEE80211_S_RUN && 572 IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr)); 573 ic->ic_bss = selbs; /* NB: caller assumed to bump refcnt */ 574 if (obss != NULL) { 575 copy_bss(selbs, obss); 576 ieee80211_free_node(obss); 577 } 578 579 /* 580 * Delete unusable rates; we've already checked 581 * that the negotiated rate set is acceptable. 582 */ 583 ieee80211_fix_rate(ic->ic_bss, &ic->ic_bss->ni_rates, 584 IEEE80211_F_DODEL | IEEE80211_F_JOIN); 585 586 ic->ic_bsschan = selbs->ni_chan; 587 ic->ic_curchan = ic->ic_bsschan; 588 ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan); 589 ic->ic_set_channel(ic); 590 /* 591 * Set the erp state (mostly the slot time) to deal with 592 * the auto-select case; this should be redundant if the 593 * mode is locked. 594 */ 595 ieee80211_reset_erp(ic); 596 ieee80211_wme_initparams(ic); 597 598 if (ic->ic_opmode == IEEE80211_M_STA) { 599 if (canreassoc) { 600 /* Reassociate */ 601 ieee80211_new_state(ic, IEEE80211_S_ASSOC, 1); 602 } else { 603 /* 604 * Act as if we received a DEAUTH frame in case we 605 * are invoked from the RUN state. This will cause 606 * us to try to re-authenticate if we are operating 607 * as a station. 608 */ 609 ieee80211_new_state(ic, IEEE80211_S_AUTH, 610 IEEE80211_FC0_SUBTYPE_DEAUTH); 611 } 612 } else 613 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 614 return 1; 615} 616 617int 618ieee80211_sta_join(struct ieee80211com *ic, 619 const struct ieee80211_scan_entry *se) 620{ 621 struct ieee80211_node *ni; 622 623 ni = ieee80211_alloc_node(&ic->ic_sta, se->se_macaddr); 624 if (ni == NULL) { 625 /* XXX msg */ 626 return 0; 627 } 628 /* 629 * Expand scan state into node's format. 630 * XXX may not need all this stuff 631 */ 632 IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid); 633 ni->ni_esslen = se->se_ssid[1]; 634 memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen); 635 ni->ni_rstamp = se->se_rstamp; 636 ni->ni_tstamp.tsf = se->se_tstamp.tsf; 637 ni->ni_intval = se->se_intval; 638 ni->ni_capinfo = se->se_capinfo; 639 /* XXX shift to 11n channel if htinfo present */ 640 ni->ni_chan = se->se_chan; 641 ni->ni_timoff = se->se_timoff; 642 ni->ni_fhdwell = se->se_fhdwell; 643 ni->ni_fhindex = se->se_fhindex; 644 ni->ni_erp = se->se_erp; 645 ni->ni_rssi = se->se_rssi; 646 ni->ni_noise = se->se_noise; 647 if (se->se_htcap_ie != NULL) 648 ieee80211_ht_node_init(ni, se->se_htcap_ie); 649 if (se->se_htinfo_ie != NULL) 650 ieee80211_parse_htinfo(ni, se->se_htinfo_ie); 651 if (se->se_wpa_ie != NULL) 652 ieee80211_saveie(&ni->ni_wpa_ie, se->se_wpa_ie); 653 if (se->se_rsn_ie != NULL) 654 ieee80211_saveie(&ni->ni_rsn_ie, se->se_rsn_ie); 655 if (se->se_wme_ie != NULL) 656 ieee80211_saveie(&ni->ni_wme_ie, se->se_wme_ie); 657 if (se->se_ath_ie != NULL) 658 ieee80211_saveath(ni, se->se_ath_ie); 659 660 ic->ic_dtim_period = se->se_dtimperiod; 661 ic->ic_dtim_count = 0; 662 663 /* NB: must be after ni_chan is setup */ 664 ieee80211_setup_rates(ni, se->se_rates, se->se_xrates, 665 IEEE80211_F_DOSORT); 666 if (se->se_htcap_ie != NULL) 667 ieee80211_setup_htrates(ni, se->se_htcap_ie, IEEE80211_F_JOIN); 668 if (se->se_htinfo_ie != NULL) 669 ieee80211_setup_basic_htrates(ni, se->se_htinfo_ie); 670 671 return ieee80211_sta_join1(ieee80211_ref_node(ni)); 672} 673 674/* 675 * Leave the specified IBSS/BSS network. The node is assumed to 676 * be passed in with a held reference. 677 */ 678void 679ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 680{ 681 ic->ic_node_cleanup(ni); 682 ieee80211_notify_node_leave(ic, ni); 683} 684 685static struct ieee80211_node * 686node_alloc(struct ieee80211_node_table *nt) 687{ 688 struct ieee80211_node *ni; 689 690 MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node), 691 M_80211_NODE, M_NOWAIT | M_ZERO); 692 return ni; 693} 694 695/* 696 * Reclaim any resources in a node and reset any critical 697 * state. Typically nodes are free'd immediately after, 698 * but in some cases the storage may be reused so we need 699 * to insure consistent state (should probably fix that). 700 */ 701static void 702node_cleanup(struct ieee80211_node *ni) 703{ 704#define N(a) (sizeof(a)/sizeof(a[0])) 705 struct ieee80211com *ic = ni->ni_ic; 706 int i; 707 708 /* NB: preserve ni_table */ 709 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 710 ic->ic_ps_sta--; 711 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 712 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 713 "[%s] power save mode off, %u sta's in ps mode\n", 714 ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta); 715 } 716 /* 717 * Clear AREF flag that marks the authorization refcnt bump 718 * has happened. This is probably not needed as the node 719 * should always be removed from the table so not found but 720 * do it just in case. 721 */ 722 ni->ni_flags &= ~IEEE80211_NODE_AREF; 723 724 /* 725 * Drain power save queue and, if needed, clear TIM. 726 */ 727 if (ieee80211_node_saveq_drain(ni) != 0 && ic->ic_set_tim != NULL) 728 ic->ic_set_tim(ni, 0); 729 730 ni->ni_associd = 0; 731 if (ni->ni_challenge != NULL) { 732 FREE(ni->ni_challenge, M_80211_NODE); 733 ni->ni_challenge = NULL; 734 } 735 /* 736 * Preserve SSID, WPA, and WME ie's so the bss node is 737 * reusable during a re-auth/re-assoc state transition. 738 * If we remove these data they will not be recreated 739 * because they come from a probe-response or beacon frame 740 * which cannot be expected prior to the association-response. 741 * This should not be an issue when operating in other modes 742 * as stations leaving always go through a full state transition 743 * which will rebuild this state. 744 * 745 * XXX does this leave us open to inheriting old state? 746 */ 747 for (i = 0; i < N(ni->ni_rxfrag); i++) 748 if (ni->ni_rxfrag[i] != NULL) { 749 m_freem(ni->ni_rxfrag[i]); 750 ni->ni_rxfrag[i] = NULL; 751 } 752 /* 753 * Must be careful here to remove any key map entry w/o a LOR. 754 */ 755 ieee80211_node_delucastkey(ni); 756#undef N 757} 758 759static void 760node_free(struct ieee80211_node *ni) 761{ 762 struct ieee80211com *ic = ni->ni_ic; 763 764 ic->ic_node_cleanup(ni); 765 if (ni->ni_wpa_ie != NULL) 766 FREE(ni->ni_wpa_ie, M_80211_NODE); 767 if (ni->ni_rsn_ie != NULL) 768 FREE(ni->ni_rsn_ie, M_80211_NODE); 769 if (ni->ni_wme_ie != NULL) 770 FREE(ni->ni_wme_ie, M_80211_NODE); 771 if (ni->ni_ath_ie != NULL) 772 FREE(ni->ni_ath_ie, M_80211_NODE); 773 IEEE80211_NODE_SAVEQ_DESTROY(ni); 774 FREE(ni, M_80211_NODE); 775} 776 777static int8_t 778node_getrssi(const struct ieee80211_node *ni) 779{ 780 return ni->ni_rssi; 781} 782 783static void 784node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise) 785{ 786 *rssi = ni->ni_rssi; 787 *noise = ni->ni_noise; 788} 789 790static void 791ieee80211_setup_node(struct ieee80211_node_table *nt, 792 struct ieee80211_node *ni, const uint8_t *macaddr) 793{ 794 struct ieee80211com *ic = nt->nt_ic; 795 int hash; 796 797 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 798 "%s %p<%s> in %s table\n", __func__, ni, 799 ether_sprintf(macaddr), nt->nt_name); 800 801 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 802 hash = IEEE80211_NODE_HASH(macaddr); 803 ieee80211_node_initref(ni); /* mark referenced */ 804 ni->ni_chan = IEEE80211_CHAN_ANYC; 805 ni->ni_authmode = IEEE80211_AUTH_OPEN; 806 ni->ni_txpower = ic->ic_txpowlimit; /* max power */ 807 ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE); 808 ni->ni_inact_reload = nt->nt_inact_init; 809 ni->ni_inact = ni->ni_inact_reload; 810 ni->ni_ath_defkeyix = 0x7fff; 811 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown"); 812 813 IEEE80211_NODE_LOCK(nt); 814 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list); 815 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash); 816 ni->ni_table = nt; 817 ni->ni_ic = ic; 818 IEEE80211_NODE_UNLOCK(nt); 819} 820 821struct ieee80211_node * 822ieee80211_alloc_node(struct ieee80211_node_table *nt, const uint8_t *macaddr) 823{ 824 struct ieee80211com *ic = nt->nt_ic; 825 struct ieee80211_node *ni; 826 827 ni = ic->ic_node_alloc(nt); 828 if (ni != NULL) 829 ieee80211_setup_node(nt, ni, macaddr); 830 else 831 ic->ic_stats.is_rx_nodealloc++; 832 return ni; 833} 834 835/* 836 * Craft a temporary node suitable for sending a management frame 837 * to the specified station. We craft only as much state as we 838 * need to do the work since the node will be immediately reclaimed 839 * once the send completes. 840 */ 841struct ieee80211_node * 842ieee80211_tmp_node(struct ieee80211com *ic, const uint8_t *macaddr) 843{ 844 struct ieee80211_node *ni; 845 846 ni = ic->ic_node_alloc(&ic->ic_sta); 847 if (ni != NULL) { 848 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 849 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr)); 850 851 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 852 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 853 ieee80211_node_initref(ni); /* mark referenced */ 854 ni->ni_txpower = ic->ic_bss->ni_txpower; 855 /* NB: required by ieee80211_fix_rate */ 856 ieee80211_node_set_chan(ic, ni); 857 ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, 858 IEEE80211_KEYIX_NONE); 859 /* XXX optimize away */ 860 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown"); 861 862 ni->ni_table = NULL; /* NB: pedantic */ 863 ni->ni_ic = ic; 864 } else { 865 /* XXX msg */ 866 ic->ic_stats.is_rx_nodealloc++; 867 } 868 return ni; 869} 870 871struct ieee80211_node * 872ieee80211_dup_bss(struct ieee80211_node_table *nt, const uint8_t *macaddr) 873{ 874 struct ieee80211com *ic = nt->nt_ic; 875 struct ieee80211_node *ni; 876 877 ni = ic->ic_node_alloc(nt); 878 if (ni != NULL) { 879 ieee80211_setup_node(nt, ni, macaddr); 880 /* 881 * Inherit from ic_bss. 882 */ 883 ni->ni_authmode = ic->ic_bss->ni_authmode; 884 ni->ni_txpower = ic->ic_bss->ni_txpower; 885 ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */ 886 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 887 ieee80211_node_set_chan(ic, ni); 888 ni->ni_rsn = ic->ic_bss->ni_rsn; 889 } else 890 ic->ic_stats.is_rx_nodealloc++; 891 return ni; 892} 893 894static struct ieee80211_node * 895#ifdef IEEE80211_DEBUG_REFCNT 896_ieee80211_find_node_debug(struct ieee80211_node_table *nt, 897 const uint8_t *macaddr, const char *func, int line) 898#else 899_ieee80211_find_node(struct ieee80211_node_table *nt, 900 const uint8_t *macaddr) 901#endif 902{ 903 struct ieee80211_node *ni; 904 int hash; 905 906 IEEE80211_NODE_LOCK_ASSERT(nt); 907 908 hash = IEEE80211_NODE_HASH(macaddr); 909 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 910 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 911 ieee80211_ref_node(ni); /* mark referenced */ 912#ifdef IEEE80211_DEBUG_REFCNT 913 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 914 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 915 func, line, 916 ni, ether_sprintf(ni->ni_macaddr), 917 ieee80211_node_refcnt(ni)); 918#endif 919 return ni; 920 } 921 } 922 return NULL; 923} 924#ifdef IEEE80211_DEBUG_REFCNT 925#define _ieee80211_find_node(nt, mac) \ 926 _ieee80211_find_node_debug(nt, mac, func, line) 927#endif 928 929struct ieee80211_node * 930#ifdef IEEE80211_DEBUG_REFCNT 931ieee80211_find_node_debug(struct ieee80211_node_table *nt, 932 const uint8_t *macaddr, const char *func, int line) 933#else 934ieee80211_find_node(struct ieee80211_node_table *nt, const uint8_t *macaddr) 935#endif 936{ 937 struct ieee80211_node *ni; 938 939 IEEE80211_NODE_LOCK(nt); 940 ni = _ieee80211_find_node(nt, macaddr); 941 IEEE80211_NODE_UNLOCK(nt); 942 return ni; 943} 944 945/* 946 * Fake up a node; this handles node discovery in adhoc mode. 947 * Note that for the driver's benefit we we treat this like 948 * an association so the driver has an opportunity to setup 949 * it's private state. 950 */ 951struct ieee80211_node * 952ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt, 953 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 954{ 955 struct ieee80211com *ic = nt->nt_ic; 956 struct ieee80211_node *ni; 957 958 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 959 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr)); 960 ni = ieee80211_dup_bss(nt, macaddr); 961 if (ni != NULL) { 962 /* XXX no rate negotiation; just dup */ 963 ni->ni_rates = ic->ic_bss->ni_rates; 964 if (ic->ic_newassoc != NULL) 965 ic->ic_newassoc(ni, 1); 966 if (ic->ic_opmode == IEEE80211_M_AHDEMO) { 967 /* 968 * In adhoc demo mode there are no management 969 * frames to use to discover neighbor capabilities, 970 * so blindly propagate the local configuration 971 * so we can do interesting things (e.g. use 972 * WME to disable ACK's). 973 */ 974 if (ic->ic_flags & IEEE80211_F_WME) 975 ni->ni_flags |= IEEE80211_NODE_QOS; 976 if (ic->ic_flags & IEEE80211_F_FF) 977 ni->ni_flags |= IEEE80211_NODE_FF; 978 } 979 /* XXX not right for 802.1x/WPA */ 980 ieee80211_node_authorize(ni); 981 } 982 return ni; 983} 984 985void 986ieee80211_init_neighbor(struct ieee80211_node *ni, 987 const struct ieee80211_frame *wh, 988 const struct ieee80211_scanparams *sp) 989{ 990 ni->ni_esslen = sp->ssid[1]; 991 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]); 992 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3); 993 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp)); 994 ni->ni_intval = sp->bintval; 995 ni->ni_capinfo = sp->capinfo; 996 ni->ni_chan = ni->ni_ic->ic_curchan; 997 ni->ni_fhdwell = sp->fhdwell; 998 ni->ni_fhindex = sp->fhindex; 999 ni->ni_erp = sp->erp; 1000 ni->ni_timoff = sp->timoff; 1001 if (sp->wme != NULL) 1002 ieee80211_saveie(&ni->ni_wme_ie, sp->wme); 1003 if (sp->wpa != NULL) 1004 ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa); 1005 if (sp->rsn != NULL) 1006 ieee80211_saveie(&ni->ni_rsn_ie, sp->rsn); 1007 if (sp->ath != NULL) 1008 ieee80211_saveath(ni, sp->ath); 1009 1010 /* NB: must be after ni_chan is setup */ 1011 ieee80211_setup_rates(ni, sp->rates, sp->xrates, 1012 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | 1013 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1014} 1015 1016/* 1017 * Do node discovery in adhoc mode on receipt of a beacon 1018 * or probe response frame. Note that for the driver's 1019 * benefit we we treat this like an association so the 1020 * driver has an opportunity to setup it's private state. 1021 */ 1022struct ieee80211_node * 1023ieee80211_add_neighbor(struct ieee80211com *ic, 1024 const struct ieee80211_frame *wh, 1025 const struct ieee80211_scanparams *sp) 1026{ 1027 struct ieee80211_node *ni; 1028 1029 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1030 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2)); 1031 ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */ 1032 if (ni != NULL) { 1033 ieee80211_init_neighbor(ni, wh, sp); 1034 if (ic->ic_newassoc != NULL) 1035 ic->ic_newassoc(ni, 1); 1036 /* XXX not right for 802.1x/WPA */ 1037 ieee80211_node_authorize(ni); 1038 } 1039 return ni; 1040} 1041 1042#define IS_CTL(wh) \ 1043 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) 1044#define IS_PSPOLL(wh) \ 1045 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL) 1046#define IS_BAR(wh) \ 1047 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR) 1048 1049/* 1050 * Locate the node for sender, track state, and then pass the 1051 * (referenced) node up to the 802.11 layer for its use. We 1052 * are required to pass some node so we fall back to ic_bss 1053 * when this frame is from an unknown sender. The 802.11 layer 1054 * knows this means the sender wasn't in the node table and 1055 * acts accordingly. 1056 */ 1057struct ieee80211_node * 1058#ifdef IEEE80211_DEBUG_REFCNT 1059ieee80211_find_rxnode_debug(struct ieee80211com *ic, 1060 const struct ieee80211_frame_min *wh, const char *func, int line) 1061#else 1062ieee80211_find_rxnode(struct ieee80211com *ic, 1063 const struct ieee80211_frame_min *wh) 1064#endif 1065{ 1066 struct ieee80211_node_table *nt; 1067 struct ieee80211_node *ni; 1068 1069 /* XXX check ic_bss first in station mode */ 1070 /* XXX 4-address frames? */ 1071 nt = &ic->ic_sta; 1072 IEEE80211_NODE_LOCK(nt); 1073 if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/) 1074 ni = _ieee80211_find_node(nt, wh->i_addr1); 1075 else 1076 ni = _ieee80211_find_node(nt, wh->i_addr2); 1077 if (ni == NULL) 1078 ni = ieee80211_ref_node(ic->ic_bss); 1079 IEEE80211_NODE_UNLOCK(nt); 1080 1081 return ni; 1082} 1083 1084/* 1085 * Like ieee80211_find_rxnode but use the supplied h/w 1086 * key index as a hint to locate the node in the key 1087 * mapping table. If an entry is present at the key 1088 * index we return it; otherwise do a normal lookup and 1089 * update the mapping table if the station has a unicast 1090 * key assigned to it. 1091 */ 1092struct ieee80211_node * 1093#ifdef IEEE80211_DEBUG_REFCNT 1094ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic, 1095 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix, 1096 const char *func, int line) 1097#else 1098ieee80211_find_rxnode_withkey(struct ieee80211com *ic, 1099 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix) 1100#endif 1101{ 1102 struct ieee80211_node_table *nt; 1103 struct ieee80211_node *ni; 1104 1105 nt = &ic->ic_sta; 1106 IEEE80211_NODE_LOCK(nt); 1107 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) 1108 ni = nt->nt_keyixmap[keyix]; 1109 else 1110 ni = NULL; 1111 if (ni == NULL) { 1112 if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/) 1113 ni = _ieee80211_find_node(nt, wh->i_addr1); 1114 else 1115 ni = _ieee80211_find_node(nt, wh->i_addr2); 1116 if (ni == NULL) 1117 ni = ieee80211_ref_node(ic->ic_bss); 1118 if (nt->nt_keyixmap != NULL) { 1119 /* 1120 * If the station has a unicast key cache slot 1121 * assigned update the key->node mapping table. 1122 */ 1123 keyix = ni->ni_ucastkey.wk_rxkeyix; 1124 /* XXX can keyixmap[keyix] != NULL? */ 1125 if (keyix < nt->nt_keyixmax && 1126 nt->nt_keyixmap[keyix] == NULL) { 1127 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1128 "%s: add key map entry %p<%s> refcnt %d\n", 1129 __func__, ni, ether_sprintf(ni->ni_macaddr), 1130 ieee80211_node_refcnt(ni)+1); 1131 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni); 1132 } 1133 } 1134 } else 1135 ieee80211_ref_node(ni); 1136 IEEE80211_NODE_UNLOCK(nt); 1137 1138 return ni; 1139} 1140#undef IS_BAR 1141#undef IS_PSPOLL 1142#undef IS_CTL 1143 1144/* 1145 * Return a reference to the appropriate node for sending 1146 * a data frame. This handles node discovery in adhoc networks. 1147 */ 1148struct ieee80211_node * 1149#ifdef IEEE80211_DEBUG_REFCNT 1150ieee80211_find_txnode_debug(struct ieee80211com *ic, const uint8_t *macaddr, 1151 const char *func, int line) 1152#else 1153ieee80211_find_txnode(struct ieee80211com *ic, const uint8_t *macaddr) 1154#endif 1155{ 1156 struct ieee80211_node_table *nt = &ic->ic_sta; 1157 struct ieee80211_node *ni; 1158 1159 /* 1160 * The destination address should be in the node table 1161 * unless this is a multicast/broadcast frame. We can 1162 * also optimize station mode operation, all frames go 1163 * to the bss node. 1164 */ 1165 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */ 1166 IEEE80211_NODE_LOCK(nt); 1167 if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr)) 1168 ni = ieee80211_ref_node(ic->ic_bss); 1169 else { 1170 ni = _ieee80211_find_node(nt, macaddr); 1171 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1172 (ni != NULL && ni->ni_associd == 0)) { 1173 /* 1174 * Station is not associated; don't permit the 1175 * data frame to be sent by returning NULL. This 1176 * is kinda a kludge but the least intrusive way 1177 * to add this check into all drivers. 1178 */ 1179 ieee80211_unref_node(&ni); /* NB: null's ni */ 1180 } 1181 } 1182 IEEE80211_NODE_UNLOCK(nt); 1183 1184 if (ni == NULL) { 1185 if (ic->ic_opmode == IEEE80211_M_IBSS || 1186 ic->ic_opmode == IEEE80211_M_AHDEMO) { 1187 /* 1188 * In adhoc mode cons up a node for the destination. 1189 * Note that we need an additional reference for the 1190 * caller to be consistent with _ieee80211_find_node. 1191 */ 1192 ni = ieee80211_fakeup_adhoc_node(nt, macaddr); 1193 if (ni != NULL) 1194 (void) ieee80211_ref_node(ni); 1195 } else { 1196 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 1197 "[%s] no node, discard frame (%s)\n", 1198 ether_sprintf(macaddr), __func__); 1199 ic->ic_stats.is_tx_nonode++; 1200 } 1201 } 1202 return ni; 1203} 1204 1205/* 1206 * Like find but search based on the ssid too. 1207 */ 1208struct ieee80211_node * 1209#ifdef IEEE80211_DEBUG_REFCNT 1210ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt, 1211 const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid, 1212 const char *func, int line) 1213#else 1214ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt, 1215 const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid) 1216#endif 1217{ 1218#define MATCH_SSID(ni, ssid, ssidlen) \ 1219 (ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0) 1220 static const uint8_t zeromac[IEEE80211_ADDR_LEN]; 1221 struct ieee80211_node *ni; 1222 int hash; 1223 1224 IEEE80211_NODE_LOCK(nt); 1225 /* 1226 * A mac address that is all zero means match only the ssid; 1227 * otherwise we must match both. 1228 */ 1229 if (IEEE80211_ADDR_EQ(macaddr, zeromac)) { 1230 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1231 if (MATCH_SSID(ni, ssid, ssidlen)) 1232 break; 1233 } 1234 } else { 1235 hash = IEEE80211_NODE_HASH(macaddr); 1236 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1237 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1238 MATCH_SSID(ni, ssid, ssidlen)) 1239 break; 1240 } 1241 } 1242 if (ni != NULL) { 1243 ieee80211_ref_node(ni); /* mark referenced */ 1244 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1245 REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr), 1246 ieee80211_node_refcnt(ni)); 1247 } 1248 IEEE80211_NODE_UNLOCK(nt); 1249 return ni; 1250#undef MATCH_SSID 1251} 1252 1253static void 1254_ieee80211_free_node(struct ieee80211_node *ni) 1255{ 1256 struct ieee80211com *ic = ni->ni_ic; 1257 struct ieee80211_node_table *nt = ni->ni_table; 1258 1259 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1260 "%s %p<%s> in %s table\n", __func__, ni, 1261 ether_sprintf(ni->ni_macaddr), 1262 nt != NULL ? nt->nt_name : "<gone>"); 1263 1264 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1265 if (nt != NULL) { 1266 TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1267 LIST_REMOVE(ni, ni_hash); 1268 } 1269 ic->ic_node_free(ni); 1270} 1271 1272void 1273#ifdef IEEE80211_DEBUG_REFCNT 1274ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line) 1275#else 1276ieee80211_free_node(struct ieee80211_node *ni) 1277#endif 1278{ 1279 struct ieee80211_node_table *nt = ni->ni_table; 1280 1281#ifdef IEEE80211_DEBUG_REFCNT 1282 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1283 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 1284 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1285#endif 1286 if (nt != NULL) { 1287 IEEE80211_NODE_LOCK(nt); 1288 if (ieee80211_node_dectestref(ni)) { 1289 /* 1290 * Last reference, reclaim state. 1291 */ 1292 _ieee80211_free_node(ni); 1293 } else if (ieee80211_node_refcnt(ni) == 1 && 1294 nt->nt_keyixmap != NULL) { 1295 ieee80211_keyix keyix; 1296 /* 1297 * Check for a last reference in the key mapping table. 1298 */ 1299 keyix = ni->ni_ucastkey.wk_rxkeyix; 1300 if (keyix < nt->nt_keyixmax && 1301 nt->nt_keyixmap[keyix] == ni) { 1302 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1303 "%s: %p<%s> clear key map entry", __func__, 1304 ni, ether_sprintf(ni->ni_macaddr)); 1305 nt->nt_keyixmap[keyix] = NULL; 1306 ieee80211_node_decref(ni); /* XXX needed? */ 1307 _ieee80211_free_node(ni); 1308 } 1309 } 1310 IEEE80211_NODE_UNLOCK(nt); 1311 } else { 1312 if (ieee80211_node_dectestref(ni)) 1313 _ieee80211_free_node(ni); 1314 } 1315} 1316 1317/* 1318 * Reclaim a unicast key and clear any key cache state. 1319 */ 1320int 1321ieee80211_node_delucastkey(struct ieee80211_node *ni) 1322{ 1323 struct ieee80211com *ic = ni->ni_ic; 1324 struct ieee80211_node_table *nt = &ic->ic_sta; 1325 struct ieee80211_node *nikey; 1326 ieee80211_keyix keyix; 1327 int isowned, status; 1328 1329 /* 1330 * NB: We must beware of LOR here; deleting the key 1331 * can cause the crypto layer to block traffic updates 1332 * which can generate a LOR against the node table lock; 1333 * grab it here and stash the key index for our use below. 1334 * 1335 * Must also beware of recursion on the node table lock. 1336 * When called from node_cleanup we may already have 1337 * the node table lock held. Unfortunately there's no 1338 * way to separate out this path so we must do this 1339 * conditionally. 1340 */ 1341 isowned = IEEE80211_NODE_IS_LOCKED(nt); 1342 if (!isowned) 1343 IEEE80211_NODE_LOCK(nt); 1344 keyix = ni->ni_ucastkey.wk_rxkeyix; 1345 status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey); 1346 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) { 1347 nikey = nt->nt_keyixmap[keyix]; 1348 nt->nt_keyixmap[keyix] = NULL;; 1349 } else 1350 nikey = NULL; 1351 if (!isowned) 1352 IEEE80211_NODE_UNLOCK(&ic->ic_sta); 1353 1354 if (nikey != NULL) { 1355 KASSERT(nikey == ni, 1356 ("key map out of sync, ni %p nikey %p", ni, nikey)); 1357 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1358 "%s: delete key map entry %p<%s> refcnt %d\n", 1359 __func__, ni, ether_sprintf(ni->ni_macaddr), 1360 ieee80211_node_refcnt(ni)-1); 1361 ieee80211_free_node(ni); 1362 } 1363 return status; 1364} 1365 1366/* 1367 * Reclaim a node. If this is the last reference count then 1368 * do the normal free work. Otherwise remove it from the node 1369 * table and mark it gone by clearing the back-reference. 1370 */ 1371static void 1372node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1373{ 1374 ieee80211_keyix keyix; 1375 1376 IEEE80211_NODE_LOCK_ASSERT(nt); 1377 1378 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1379 "%s: remove %p<%s> from %s table, refcnt %d\n", 1380 __func__, ni, ether_sprintf(ni->ni_macaddr), 1381 nt->nt_name, ieee80211_node_refcnt(ni)-1); 1382 /* 1383 * Clear any entry in the unicast key mapping table. 1384 * We need to do it here so rx lookups don't find it 1385 * in the mapping table even if it's not in the hash 1386 * table. We cannot depend on the mapping table entry 1387 * being cleared because the node may not be free'd. 1388 */ 1389 keyix = ni->ni_ucastkey.wk_rxkeyix; 1390 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax && 1391 nt->nt_keyixmap[keyix] == ni) { 1392 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1393 "%s: %p<%s> clear key map entry\n", 1394 __func__, ni, ether_sprintf(ni->ni_macaddr)); 1395 nt->nt_keyixmap[keyix] = NULL; 1396 ieee80211_node_decref(ni); /* NB: don't need free */ 1397 } 1398 if (!ieee80211_node_dectestref(ni)) { 1399 /* 1400 * Other references are present, just remove the 1401 * node from the table so it cannot be found. When 1402 * the references are dropped storage will be 1403 * reclaimed. 1404 */ 1405 TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1406 LIST_REMOVE(ni, ni_hash); 1407 ni->ni_table = NULL; /* clear reference */ 1408 } else 1409 _ieee80211_free_node(ni); 1410} 1411 1412static void 1413ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt) 1414{ 1415 struct ieee80211com *ic = nt->nt_ic; 1416 struct ieee80211_node *ni; 1417 1418 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1419 "%s: free all nodes in %s table\n", __func__, nt->nt_name); 1420 1421 while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) { 1422 if (ni->ni_associd != 0) { 1423 if (ic->ic_auth->ia_node_leave != NULL) 1424 ic->ic_auth->ia_node_leave(ic, ni); 1425 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1426 } 1427 node_reclaim(nt, ni); 1428 } 1429} 1430 1431/* 1432 * Timeout inactive stations and do related housekeeping. 1433 * Note that we cannot hold the node lock while sending a 1434 * frame as this would lead to a LOR. Instead we use a 1435 * generation number to mark nodes that we've scanned and 1436 * drop the lock and restart a scan if we have to time out 1437 * a node. Since we are single-threaded by virtue of 1438 * controlling the inactivity timer we can be sure this will 1439 * process each node only once. 1440 */ 1441static void 1442ieee80211_timeout_stations(struct ieee80211_node_table *nt) 1443{ 1444 struct ieee80211com *ic = nt->nt_ic; 1445 struct ieee80211_node *ni; 1446 u_int gen; 1447 int isadhoc; 1448 1449 isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS || 1450 ic->ic_opmode == IEEE80211_M_AHDEMO); 1451 IEEE80211_SCAN_LOCK(nt); 1452 gen = ++nt->nt_scangen; 1453restart: 1454 IEEE80211_NODE_LOCK(nt); 1455 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1456 if (ni->ni_scangen == gen) /* previously handled */ 1457 continue; 1458 ni->ni_scangen = gen; 1459 /* 1460 * Ignore entries for which have yet to receive an 1461 * authentication frame. These are transient and 1462 * will be reclaimed when the last reference to them 1463 * goes away (when frame xmits complete). 1464 */ 1465 if ((ic->ic_opmode == IEEE80211_M_HOSTAP || 1466 ic->ic_opmode == IEEE80211_M_STA) && 1467 (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 1468 continue; 1469 /* 1470 * Free fragment if not needed anymore 1471 * (last fragment older than 1s). 1472 * XXX doesn't belong here 1473 */ 1474 if (ni->ni_rxfrag[0] != NULL && 1475 ticks > ni->ni_rxfragstamp + hz) { 1476 m_freem(ni->ni_rxfrag[0]); 1477 ni->ni_rxfrag[0] = NULL; 1478 } 1479 if (ni->ni_inact > 0) 1480 ni->ni_inact--; 1481 /* 1482 * Special case ourself; we may be idle for extended periods 1483 * of time and regardless reclaiming our state is wrong. 1484 */ 1485 if (ni == ic->ic_bss) 1486 continue; 1487 if (ni->ni_associd != 0 || isadhoc) { 1488 /* 1489 * Age frames on the power save queue. 1490 */ 1491 if (ieee80211_node_saveq_age(ni) != 0 && 1492 IEEE80211_NODE_SAVEQ_QLEN(ni) == 0 && 1493 ic->ic_set_tim != NULL) 1494 ic->ic_set_tim(ni, 0); 1495 /* 1496 * Probe the station before time it out. We 1497 * send a null data frame which may not be 1498 * universally supported by drivers (need it 1499 * for ps-poll support so it should be...). 1500 * 1501 * XXX don't probe the station unless we've 1502 * received a frame from them (and have 1503 * some idea of the rates they are capable 1504 * of); this will get fixed more properly 1505 * soon with better handling of the rate set. 1506 */ 1507 if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) && 1508 (0 < ni->ni_inact && 1509 ni->ni_inact <= ic->ic_inact_probe) && 1510 ni->ni_rates.rs_nrates != 0) { 1511 IEEE80211_NOTE(ic, 1512 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, 1513 ni, "%s", 1514 "probe station due to inactivity"); 1515 /* 1516 * Grab a reference before unlocking the table 1517 * so the node cannot be reclaimed before we 1518 * send the frame. ieee80211_send_nulldata 1519 * understands we've done this and reclaims the 1520 * ref for us as needed. 1521 */ 1522 ieee80211_ref_node(ni); 1523 IEEE80211_NODE_UNLOCK(nt); 1524 ieee80211_send_nulldata(ni); 1525 /* XXX stat? */ 1526 goto restart; 1527 } 1528 } 1529 if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) && 1530 ni->ni_inact <= 0) { 1531 IEEE80211_NOTE(ic, 1532 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni, 1533 "station timed out due to inactivity " 1534 "(refcnt %u)", ieee80211_node_refcnt(ni)); 1535 /* 1536 * Send a deauthenticate frame and drop the station. 1537 * This is somewhat complicated due to reference counts 1538 * and locking. At this point a station will typically 1539 * have a reference count of 1. ieee80211_node_leave 1540 * will do a "free" of the node which will drop the 1541 * reference count. But in the meantime a reference 1542 * wil be held by the deauth frame. The actual reclaim 1543 * of the node will happen either after the tx is 1544 * completed or by ieee80211_node_leave. 1545 * 1546 * Separately we must drop the node lock before sending 1547 * in case the driver takes a lock, as this can result 1548 * in a LOR between the node lock and the driver lock. 1549 */ 1550 IEEE80211_NODE_UNLOCK(nt); 1551 if (ni->ni_associd != 0) { 1552 IEEE80211_SEND_MGMT(ic, ni, 1553 IEEE80211_FC0_SUBTYPE_DEAUTH, 1554 IEEE80211_REASON_AUTH_EXPIRE); 1555 } 1556 ieee80211_node_leave(ic, ni); 1557 ic->ic_stats.is_node_timeout++; 1558 goto restart; 1559 } 1560 } 1561 IEEE80211_NODE_UNLOCK(nt); 1562 1563 IEEE80211_SCAN_UNLOCK(nt); 1564} 1565 1566void 1567ieee80211_node_timeout(void *arg) 1568{ 1569 struct ieee80211com *ic = arg; 1570 1571 ieee80211_scan_timeout(ic); 1572 ieee80211_timeout_stations(&ic->ic_sta); 1573 1574 callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz, 1575 ieee80211_node_timeout, ic); 1576} 1577 1578void 1579ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg) 1580{ 1581 struct ieee80211_node *ni; 1582 u_int gen; 1583 1584 IEEE80211_SCAN_LOCK(nt); 1585 gen = ++nt->nt_scangen; 1586restart: 1587 IEEE80211_NODE_LOCK(nt); 1588 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1589 if (ni->ni_scangen != gen) { 1590 ni->ni_scangen = gen; 1591 (void) ieee80211_ref_node(ni); 1592 IEEE80211_NODE_UNLOCK(nt); 1593 (*f)(arg, ni); 1594 ieee80211_free_node(ni); 1595 goto restart; 1596 } 1597 } 1598 IEEE80211_NODE_UNLOCK(nt); 1599 1600 IEEE80211_SCAN_UNLOCK(nt); 1601} 1602 1603void 1604ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1605{ 1606 printf("0x%p: mac %s refcnt %d\n", ni, 1607 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 1608 printf("\tscangen %u authmode %u flags 0x%x\n", 1609 ni->ni_scangen, ni->ni_authmode, ni->ni_flags); 1610 printf("\tassocid 0x%x txpower %u vlan %u\n", 1611 ni->ni_associd, ni->ni_txpower, ni->ni_vlan); 1612 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n", 1613 ni->ni_txseqs[IEEE80211_NONQOS_TID], 1614 ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT, 1615 ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK, 1616 ni->ni_rxfragstamp); 1617 printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n", 1618 ni->ni_rstamp, ni->ni_rssi, ni->ni_noise, 1619 ni->ni_intval, ni->ni_capinfo); 1620 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n", 1621 ether_sprintf(ni->ni_bssid), 1622 ni->ni_esslen, ni->ni_essid, 1623 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags); 1624 printf("\tfails %u inact %u txrate %u\n", 1625 ni->ni_fails, ni->ni_inact, ni->ni_txrate); 1626 printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n", 1627 ni->ni_htcap, ni->ni_htparam, 1628 ni->ni_htctlchan, ni->ni_ht2ndchan); 1629 printf("\thtopmode %x htstbc %x chw %u\n", 1630 ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw); 1631} 1632 1633void 1634ieee80211_dump_nodes(struct ieee80211_node_table *nt) 1635{ 1636 ieee80211_iterate_nodes(nt, 1637 (ieee80211_iter_func *) ieee80211_dump_node, nt); 1638} 1639 1640/* 1641 * Handle a station joining an 11g network. 1642 */ 1643static void 1644ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 1645{ 1646 1647 /* 1648 * Station isn't capable of short slot time. Bump 1649 * the count of long slot time stations and disable 1650 * use of short slot time. Note that the actual switch 1651 * over to long slot time use may not occur until the 1652 * next beacon transmission (per sec. 7.3.1.4 of 11g). 1653 */ 1654 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 1655 ic->ic_longslotsta++; 1656 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1657 "[%s] station needs long slot time, count %d\n", 1658 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 1659 /* XXX vap's w/ conflicting needs won't work */ 1660 if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) { 1661 /* 1662 * Don't force slot time when switched to turbo 1663 * mode as non-ERP stations won't be present; this 1664 * need only be done when on the normal G channel. 1665 */ 1666 ieee80211_set_shortslottime(ic, 0); 1667 } 1668 } 1669 /* 1670 * If the new station is not an ERP station 1671 * then bump the counter and enable protection 1672 * if configured. 1673 */ 1674 if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) { 1675 ic->ic_nonerpsta++; 1676 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1677 "[%s] station is !ERP, %d non-ERP stations associated\n", 1678 ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 1679 /* 1680 * If protection is configured, enable it. 1681 */ 1682 if (ic->ic_protmode != IEEE80211_PROT_NONE) { 1683 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1684 "%s: enable use of protection\n", __func__); 1685 ic->ic_flags |= IEEE80211_F_USEPROT; 1686 } 1687 /* 1688 * If station does not support short preamble 1689 * then we must enable use of Barker preamble. 1690 */ 1691 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) { 1692 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1693 "[%s] station needs long preamble\n", 1694 ether_sprintf(ni->ni_macaddr)); 1695 ic->ic_flags |= IEEE80211_F_USEBARKER; 1696 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 1697 } 1698 if (ic->ic_nonerpsta == 1) 1699 ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE; 1700 } else 1701 ni->ni_flags |= IEEE80211_NODE_ERP; 1702} 1703 1704void 1705ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp) 1706{ 1707 int newassoc; 1708 1709 if (ni->ni_associd == 0) { 1710 uint16_t aid; 1711 1712 /* 1713 * It would be good to search the bitmap 1714 * more efficiently, but this will do for now. 1715 */ 1716 for (aid = 1; aid < ic->ic_max_aid; aid++) { 1717 if (!IEEE80211_AID_ISSET(aid, 1718 ic->ic_aid_bitmap)) 1719 break; 1720 } 1721 if (aid >= ic->ic_max_aid) { 1722 IEEE80211_SEND_MGMT(ic, ni, resp, 1723 IEEE80211_REASON_ASSOC_TOOMANY); 1724 ieee80211_node_leave(ic, ni); 1725 return; 1726 } 1727 ni->ni_associd = aid | 0xc000; 1728 IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap); 1729 ic->ic_sta_assoc++; 1730 newassoc = 1; 1731 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) && 1732 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan)) 1733 ieee80211_node_join_11g(ic, ni); 1734 } else 1735 newassoc = 0; 1736 1737 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 1738 "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s%s%s%s\n", 1739 ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re", 1740 IEEE80211_NODE_AID(ni), 1741 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long", 1742 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", 1743 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "", 1744 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "", 1745 ni->ni_flags & IEEE80211_NODE_HT ? 1746 (ni->ni_chw == 20 ? ", HT20" : ", HT40") : "", 1747 IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_FF) ? 1748 ", fast-frames" : "", 1749 IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_TURBOP) ? 1750 ", turbo" : "" 1751 ); 1752 1753 /* give driver a chance to setup state like ni_txrate */ 1754 if (ic->ic_newassoc != NULL) 1755 ic->ic_newassoc(ni, newassoc); 1756 IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS); 1757 /* tell the authenticator about new station */ 1758 if (ic->ic_auth->ia_node_join != NULL) 1759 ic->ic_auth->ia_node_join(ic, ni); 1760 ieee80211_notify_node_join(ic, ni, newassoc); 1761} 1762 1763/* 1764 * Handle a station leaving an 11g network. 1765 */ 1766static void 1767ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 1768{ 1769 1770 KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan), 1771 ("not in 11g, bss %u:0x%x, curmode %u", ic->ic_bsschan->ic_freq, 1772 ic->ic_bsschan->ic_flags, ic->ic_curmode)); 1773 1774 /* 1775 * If a long slot station do the slot time bookkeeping. 1776 */ 1777 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 1778 KASSERT(ic->ic_longslotsta > 0, 1779 ("bogus long slot station count %d", ic->ic_longslotsta)); 1780 ic->ic_longslotsta--; 1781 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1782 "[%s] long slot time station leaves, count now %d\n", 1783 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 1784 if (ic->ic_longslotsta == 0) { 1785 /* 1786 * Re-enable use of short slot time if supported 1787 * and not operating in IBSS mode (per spec). 1788 */ 1789 if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 1790 ic->ic_opmode != IEEE80211_M_IBSS) { 1791 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1792 "%s: re-enable use of short slot time\n", 1793 __func__); 1794 ieee80211_set_shortslottime(ic, 1); 1795 } 1796 } 1797 } 1798 /* 1799 * If a non-ERP station do the protection-related bookkeeping. 1800 */ 1801 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) { 1802 KASSERT(ic->ic_nonerpsta > 0, 1803 ("bogus non-ERP station count %d", ic->ic_nonerpsta)); 1804 ic->ic_nonerpsta--; 1805 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1806 "[%s] non-ERP station leaves, count now %d\n", 1807 ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 1808 if (ic->ic_nonerpsta == 0) { 1809 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1810 "%s: disable use of protection\n", __func__); 1811 ic->ic_flags &= ~IEEE80211_F_USEPROT; 1812 /* XXX verify mode? */ 1813 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) { 1814 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1815 "%s: re-enable use of short preamble\n", 1816 __func__); 1817 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 1818 ic->ic_flags &= ~IEEE80211_F_USEBARKER; 1819 } 1820 ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE; 1821 } 1822 } 1823} 1824 1825/* 1826 * Handle bookkeeping for station deauthentication/disassociation 1827 * when operating as an ap. 1828 */ 1829void 1830ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 1831{ 1832 struct ieee80211_node_table *nt = ni->ni_table; 1833 1834 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 1835 "[%s] station with aid %d leaves\n", 1836 ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni)); 1837 1838 KASSERT(ic->ic_opmode != IEEE80211_M_STA, 1839 ("unexpected operating mode %u", ic->ic_opmode)); 1840 /* 1841 * If node wasn't previously associated all 1842 * we need to do is reclaim the reference. 1843 */ 1844 /* XXX ibss mode bypasses 11g and notification */ 1845 if (ni->ni_associd == 0) 1846 goto done; 1847 /* 1848 * Tell the authenticator the station is leaving. 1849 * Note that we must do this before yanking the 1850 * association id as the authenticator uses the 1851 * associd to locate it's state block. 1852 */ 1853 if (ic->ic_auth->ia_node_leave != NULL) 1854 ic->ic_auth->ia_node_leave(ic, ni); 1855 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1856 ni->ni_associd = 0; 1857 ic->ic_sta_assoc--; 1858 1859 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) && 1860 IEEE80211_IS_CHAN_FULL(ic->ic_bsschan)) 1861 ieee80211_node_leave_11g(ic, ni); 1862 /* 1863 * Cleanup station state. In particular clear various 1864 * state that might otherwise be reused if the node 1865 * is reused before the reference count goes to zero 1866 * (and memory is reclaimed). 1867 */ 1868 ieee80211_sta_leave(ic, ni); 1869done: 1870 /* 1871 * Remove the node from any table it's recorded in and 1872 * drop the caller's reference. Removal from the table 1873 * is important to insure the node is not reprocessed 1874 * for inactivity. 1875 */ 1876 if (nt != NULL) { 1877 IEEE80211_NODE_LOCK(nt); 1878 node_reclaim(nt, ni); 1879 IEEE80211_NODE_UNLOCK(nt); 1880 } else 1881 ieee80211_free_node(ni); 1882} 1883 1884int8_t 1885ieee80211_getrssi(struct ieee80211com *ic) 1886{ 1887#define NZ(x) ((x) == 0 ? 1 : (x)) 1888 struct ieee80211_node_table *nt = &ic->ic_sta; 1889 int rssi_samples; 1890 int32_t rssi_total; 1891 struct ieee80211_node *ni; 1892 1893 rssi_total = 0; 1894 rssi_samples = 0; 1895 switch (ic->ic_opmode) { 1896 case IEEE80211_M_IBSS: /* average of all ibss neighbors */ 1897 case IEEE80211_M_AHDEMO: /* average of all neighbors */ 1898 case IEEE80211_M_HOSTAP: /* average of all associated stations */ 1899 /* XXX locking */ 1900 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 1901 if (ic->ic_opmode == IEEE80211_M_HOSTAP || 1902 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS)) { 1903 int8_t rssi = ic->ic_node_getrssi(ni); 1904 if (rssi != 0) { 1905 rssi_samples++; 1906 rssi_total += rssi; 1907 } 1908 } 1909 break; 1910 case IEEE80211_M_MONITOR: /* XXX */ 1911 case IEEE80211_M_STA: /* use stats from associated ap */ 1912 default: 1913 if (ic->ic_bss != NULL) 1914 rssi_total = ic->ic_node_getrssi(ic->ic_bss); 1915 rssi_samples = 1; 1916 break; 1917 } 1918 return rssi_total / NZ(rssi_samples); 1919#undef NZ 1920} 1921 1922void 1923ieee80211_getsignal(struct ieee80211com *ic, int8_t *rssi, int8_t *noise) 1924{ 1925 1926 if (ic->ic_bss == NULL) /* NB: shouldn't happen */ 1927 return; 1928 ic->ic_node_getsignal(ic->ic_bss, rssi, noise); 1929 /* for non-station mode return avg'd rssi accounting */ 1930 if (ic->ic_opmode != IEEE80211_M_STA) 1931 *rssi = ieee80211_getrssi(ic); 1932} 1933 1934/* 1935 * Node table support. 1936 */ 1937 1938static void 1939ieee80211_node_table_init(struct ieee80211com *ic, 1940 struct ieee80211_node_table *nt, 1941 const char *name, int inact, int keyixmax) 1942{ 1943 1944 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1945 "%s %s table, inact %u\n", __func__, name, inact); 1946 1947 nt->nt_ic = ic; 1948 /* XXX need unit */ 1949 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname); 1950 IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname); 1951 TAILQ_INIT(&nt->nt_node); 1952 nt->nt_name = name; 1953 nt->nt_scangen = 1; 1954 nt->nt_inact_init = inact; 1955 nt->nt_keyixmax = keyixmax; 1956 if (nt->nt_keyixmax > 0) { 1957 MALLOC(nt->nt_keyixmap, struct ieee80211_node **, 1958 keyixmax * sizeof(struct ieee80211_node *), 1959 M_80211_NODE, M_NOWAIT | M_ZERO); 1960 if (nt->nt_keyixmap == NULL) 1961 if_printf(ic->ic_ifp, 1962 "Cannot allocate key index map with %u entries\n", 1963 keyixmax); 1964 } else 1965 nt->nt_keyixmap = NULL; 1966} 1967 1968static void 1969ieee80211_node_table_reset(struct ieee80211_node_table *nt) 1970{ 1971 1972 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1973 "%s %s table\n", __func__, nt->nt_name); 1974 1975 IEEE80211_NODE_LOCK(nt); 1976 ieee80211_free_allnodes_locked(nt); 1977 IEEE80211_NODE_UNLOCK(nt); 1978} 1979 1980static void 1981ieee80211_node_table_cleanup(struct ieee80211_node_table *nt) 1982{ 1983 1984 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1985 "%s %s table\n", __func__, nt->nt_name); 1986 1987 IEEE80211_NODE_LOCK(nt); 1988 ieee80211_free_allnodes_locked(nt); 1989 if (nt->nt_keyixmap != NULL) { 1990 /* XXX verify all entries are NULL */ 1991 int i; 1992 for (i = 0; i < nt->nt_keyixmax; i++) 1993 if (nt->nt_keyixmap[i] != NULL) 1994 printf("%s: %s[%u] still active\n", __func__, 1995 nt->nt_name, i); 1996 FREE(nt->nt_keyixmap, M_80211_NODE); 1997 nt->nt_keyixmap = NULL; 1998 } 1999 IEEE80211_SCAN_LOCK_DESTROY(nt); 2000 IEEE80211_NODE_LOCK_DESTROY(nt); 2001} 2002