1 /* 2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 /* 7 * Copyright (c) 2001 Atsushi Onoe 8 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * Alternatively, this software may be distributed under the terms of the 23 * GNU General Public License ("GPL") version 2 as published by the Free 24 * Software Foundation. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #pragma ident "%Z%%M% %I% %E% SMI" 39 40 /* 41 * Node management routines 42 */ 43 44 #include "net80211_impl.h" 45 46 static ieee80211_node_t *ieee80211_node_alloc(ieee80211com_t *); 47 static void ieee80211_node_cleanup(ieee80211_node_t *); 48 static void ieee80211_node_free(ieee80211_node_t *); 49 static uint8_t ieee80211_node_getrssi(const ieee80211_node_t *); 50 static void ieee80211_setup_node(ieee80211com_t *, ieee80211_node_table_t *, 51 ieee80211_node_t *, const uint8_t *); 52 static void ieee80211_node_reclaim(ieee80211_node_table_t *, 53 ieee80211_node_t *); 54 static void ieee80211_free_node_locked(ieee80211_node_t *); 55 static void ieee80211_free_allnodes(ieee80211_node_table_t *); 56 static void ieee80211_node_leave(ieee80211com_t *, ieee80211_node_t *); 57 static void ieee80211_timeout_scan_candidates(ieee80211_node_table_t *); 58 static void ieee80211_timeout_stations(ieee80211_node_table_t *); 59 static void ieee80211_node_table_init(ieee80211com_t *, 60 ieee80211_node_table_t *, const char *, int, int, 61 void (*timeout)(ieee80211_node_table_t *)); 62 static void ieee80211_node_table_cleanup(ieee80211_node_table_t *); 63 64 /* 65 * association failures before ignored 66 * The failure may be caused by the response frame is lost for 67 * environmental reason. So Try associate more than once before 68 * ignore the node 69 */ 70 #define IEEE80211_STA_FAILS_MAX 2 71 72 /* 73 * Initialize node database management callbacks for the interface. 74 * This function is called by ieee80211_attach(). These callback 75 * functions may be overridden in special circumstances, as long as 76 * as this is done after calling ieee80211_attach() and prior to any 77 * other call which may allocate a node 78 */ 79 void 80 ieee80211_node_attach(ieee80211com_t *ic) 81 { 82 struct ieee80211_impl *im = ic->ic_private; 83 84 ic->ic_node_alloc = ieee80211_node_alloc; 85 ic->ic_node_free = ieee80211_node_free; 86 ic->ic_node_cleanup = ieee80211_node_cleanup; 87 ic->ic_node_getrssi = ieee80211_node_getrssi; 88 89 /* default station inactivity timer setings */ 90 im->im_inact_init = IEEE80211_INACT_INIT; 91 im->im_inact_assoc = IEEE80211_INACT_ASSOC; 92 im->im_inact_run = IEEE80211_INACT_RUN; 93 im->im_inact_probe = IEEE80211_INACT_PROBE; 94 } 95 96 /* 97 * Initialize node databases and the ic_bss node element. 98 */ 99 void 100 ieee80211_node_lateattach(ieee80211com_t *ic) 101 { 102 /* 103 * Calculate ic_tim_bitmap size in bytes 104 * IEEE80211_AID_MAX defines maximum bits in ic_tim_bitmap 105 */ 106 ic->ic_tim_len = howmany(IEEE80211_AID_MAX, 8) * sizeof (uint8_t); 107 108 ieee80211_node_table_init(ic, &ic->ic_sta, "station", 109 IEEE80211_INACT_INIT, IEEE80211_WEP_NKID, 110 ieee80211_timeout_stations); 111 ieee80211_node_table_init(ic, &ic->ic_scan, "scan", 112 IEEE80211_INACT_SCAN, 0, ieee80211_timeout_scan_candidates); 113 114 ieee80211_reset_bss(ic); 115 } 116 117 /* 118 * Destroy all node databases and is usually called during device detach 119 */ 120 void 121 ieee80211_node_detach(ieee80211com_t *ic) 122 { 123 /* Node Detach */ 124 if (ic->ic_bss != NULL) { 125 ieee80211_free_node(ic->ic_bss); 126 ic->ic_bss = NULL; 127 } 128 ieee80211_node_table_cleanup(&ic->ic_scan); 129 ieee80211_node_table_cleanup(&ic->ic_sta); 130 } 131 132 /* 133 * Increase a node's reference count 134 * 135 * Return pointer to the node 136 */ 137 ieee80211_node_t * 138 ieee80211_ref_node(ieee80211_node_t *in) 139 { 140 ieee80211_node_incref(in); 141 return (in); 142 } 143 144 /* 145 * Dexrease a node's reference count 146 */ 147 void 148 ieee80211_unref_node(ieee80211_node_t **in) 149 { 150 ieee80211_node_decref(*in); 151 *in = NULL; /* guard against use */ 152 } 153 154 /* 155 * Mark ports authorized for data traffic. This function is usually 156 * used by 802.1x authenticator. 157 */ 158 void 159 ieee80211_node_authorize(ieee80211_node_t *in) 160 { 161 ieee80211_impl_t *im = in->in_ic->ic_private; 162 163 in->in_flags |= IEEE80211_NODE_AUTH; 164 in->in_inact_reload = im->im_inact_run; 165 } 166 167 /* 168 * Mark ports unauthorized for data traffic. This function is usually 169 * used by 802.1x authenticator. 170 */ 171 void 172 ieee80211_node_unauthorize(ieee80211_node_t *in) 173 { 174 in->in_flags &= ~IEEE80211_NODE_AUTH; 175 } 176 177 /* 178 * Set/change the channel. The rate set is also updated as 179 * to insure a consistent view by drivers. 180 */ 181 static void 182 ieee80211_node_setchan(ieee80211com_t *ic, ieee80211_node_t *in, 183 struct ieee80211_channel *chan) 184 { 185 if (chan == IEEE80211_CHAN_ANYC) 186 chan = ic->ic_curchan; 187 in->in_chan = chan; 188 in->in_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)]; 189 } 190 191 /* 192 * Initialize the channel set to scan based on the available channels 193 * and the current PHY mode. 194 */ 195 static void 196 ieee80211_reset_scan(ieee80211com_t *ic) 197 { 198 ieee80211_impl_t *im = ic->ic_private; 199 200 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) { 201 (void) memset(im->im_chan_scan, 0, sizeof (im->im_chan_scan)); 202 ieee80211_setbit(im->im_chan_scan, 203 ieee80211_chan2ieee(ic, ic->ic_des_chan)); 204 } else { 205 bcopy(ic->ic_chan_active, im->im_chan_scan, 206 sizeof (ic->ic_chan_active)); 207 } 208 ieee80211_dbg(IEEE80211_MSG_SCAN, "ieee80211_reset_scan(): " 209 "start chan %u\n", ieee80211_chan2ieee(ic, ic->ic_curchan)); 210 } 211 212 /* 213 * Begin an active scan. Initialize the node cache. The scan 214 * begins on the next radio channel by calling ieee80211_next_scan(). 215 * The actual scanning is not automated. The driver itself 216 * only handles setting the radio frequency and stepping through 217 * the channels. 218 */ 219 void 220 ieee80211_begin_scan(ieee80211com_t *ic, boolean_t reset) 221 { 222 IEEE80211_LOCK(ic); 223 224 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 225 ic->ic_flags |= IEEE80211_F_ASCAN; 226 ieee80211_dbg(IEEE80211_MSG_SCAN, 227 "begin %s scan in %s mode on channel %u\n", 228 (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive", 229 ieee80211_phymode_name[ic->ic_curmode], 230 ieee80211_chan2ieee(ic, ic->ic_curchan)); 231 232 /* 233 * Clear scan state and flush any previously seen AP's. 234 */ 235 ieee80211_reset_scan(ic); 236 if (reset) 237 ieee80211_free_allnodes(&ic->ic_scan); 238 239 ic->ic_flags |= IEEE80211_F_SCAN; 240 IEEE80211_UNLOCK(ic); 241 242 /* Scan the next channel. */ 243 ieee80211_next_scan(ic); 244 } 245 246 /* 247 * Switch to the next channel marked for scanning. 248 * A driver is expected to first call ieee80211_begin_scan(), 249 * to initialize the node cache, then set the radio channel 250 * on the device. And then after a certain time has elapsed, 251 * call ieee80211_next_scan() to move to the next channel. 252 * Typically, a timeout routine is used to automate this process. 253 */ 254 void 255 ieee80211_next_scan(ieee80211com_t *ic) 256 { 257 ieee80211_impl_t *im = ic->ic_private; 258 struct ieee80211_channel *chan; 259 260 IEEE80211_LOCK(ic); 261 /* 262 * Insure any previous mgt frame timeouts don't fire. 263 * This assumes the driver does the right thing in 264 * flushing anything queued in the driver and below. 265 */ 266 im->im_mgt_timer = 0; 267 268 chan = ic->ic_curchan; 269 do { 270 if (++chan > &ic->ic_sup_channels[IEEE80211_CHAN_MAX]) 271 chan = &ic->ic_sup_channels[0]; 272 if (ieee80211_isset(im->im_chan_scan, 273 ieee80211_chan2ieee(ic, chan))) { 274 ieee80211_clrbit(im->im_chan_scan, 275 ieee80211_chan2ieee(ic, chan)); 276 ieee80211_dbg(IEEE80211_MSG_SCAN, 277 "ieee80211_next_scan: chan %d->%d\n", 278 ieee80211_chan2ieee(ic, ic->ic_curchan), 279 ieee80211_chan2ieee(ic, chan)); 280 ic->ic_curchan = chan; 281 /* 282 * drivers should do this as needed, 283 * for now maintain compatibility 284 */ 285 ic->ic_bss->in_rates = 286 ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)]; 287 IEEE80211_UNLOCK(ic); 288 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 289 return; 290 } 291 } while (chan != ic->ic_curchan); 292 IEEE80211_UNLOCK(ic); 293 ieee80211_end_scan(ic); 294 } 295 296 /* 297 * Copy useful state from node obss into nbss. 298 */ 299 static void 300 ieee80211_copy_bss(ieee80211_node_t *nbss, const ieee80211_node_t *obss) 301 { 302 /* propagate useful state */ 303 nbss->in_authmode = obss->in_authmode; 304 nbss->in_txpower = obss->in_txpower; 305 nbss->in_vlan = obss->in_vlan; 306 } 307 308 /* 309 * Setup the net80211 specific portion of an interface's softc, ic, 310 * for use in IBSS mode 311 */ 312 void 313 ieee80211_create_ibss(ieee80211com_t *ic, struct ieee80211_channel *chan) 314 { 315 ieee80211_impl_t *im = ic->ic_private; 316 ieee80211_node_table_t *nt; 317 ieee80211_node_t *in; 318 319 IEEE80211_LOCK_ASSERT(ic); 320 ieee80211_dbg(IEEE80211_MSG_SCAN, "ieee80211_create_ibss: " 321 "creating ibss\n"); 322 323 /* 324 * Create the station/neighbor table. Note that for adhoc 325 * mode we make the initial inactivity timer longer since 326 * we create nodes only through discovery and they typically 327 * are long-lived associations. 328 */ 329 nt = &ic->ic_sta; 330 IEEE80211_NODE_LOCK(nt); 331 nt->nt_name = "neighbor"; 332 nt->nt_inact_init = im->im_inact_run; 333 IEEE80211_NODE_UNLOCK(nt); 334 335 in = ieee80211_alloc_node(ic, &ic->ic_sta, ic->ic_macaddr); 336 if (in == NULL) { 337 ieee80211_err("ieee80211_create_ibss(): alloc node failed\n"); 338 return; 339 } 340 IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_macaddr); 341 in->in_esslen = ic->ic_des_esslen; 342 (void) memcpy(in->in_essid, ic->ic_des_essid, in->in_esslen); 343 ieee80211_copy_bss(in, ic->ic_bss); 344 in->in_intval = ic->ic_bintval; 345 if (ic->ic_flags & IEEE80211_F_PRIVACY) 346 in->in_capinfo |= IEEE80211_CAPINFO_PRIVACY; 347 if (ic->ic_phytype == IEEE80211_T_FH) { 348 in->in_fhdwell = 200; 349 in->in_fhindex = 1; 350 } 351 switch (ic->ic_opmode) { 352 case IEEE80211_M_IBSS: 353 ic->ic_flags |= IEEE80211_F_SIBSS; 354 in->in_capinfo |= IEEE80211_CAPINFO_IBSS; 355 if (ic->ic_flags & IEEE80211_F_DESBSSID) 356 IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_des_bssid); 357 else 358 in->in_bssid[0] |= 0x02; /* local bit for IBSS */ 359 break; 360 case IEEE80211_M_AHDEMO: 361 if (ic->ic_flags & IEEE80211_F_DESBSSID) 362 IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_des_bssid); 363 else 364 (void) memset(in->in_bssid, 0, IEEE80211_ADDR_LEN); 365 break; 366 default: 367 ieee80211_err("ieee80211_create_ibss(): " 368 "wrong opmode %u to creat IBSS, abort\n", 369 ic->ic_opmode); 370 ieee80211_free_node(in); 371 return; 372 } 373 374 /* 375 * Fix the channel and related attributes. 376 */ 377 ieee80211_node_setchan(ic, in, chan); 378 ic->ic_curchan = chan; 379 ic->ic_curmode = ieee80211_chan2mode(ic, chan); 380 /* 381 * Do mode-specific rate setup. 382 */ 383 ieee80211_setbasicrates(&in->in_rates, ic->ic_curmode); 384 IEEE80211_UNLOCK(ic); 385 ieee80211_sta_join(ic, in); 386 IEEE80211_LOCK(ic); 387 } 388 389 void 390 ieee80211_reset_bss(ieee80211com_t *ic) 391 { 392 ieee80211_node_t *in; 393 ieee80211_node_t *obss; 394 395 in = ieee80211_alloc_node(ic, &ic->ic_scan, ic->ic_macaddr); 396 ASSERT(in != NULL); 397 obss = ic->ic_bss; 398 ic->ic_bss = ieee80211_ref_node(in); 399 if (obss != NULL) { 400 ieee80211_copy_bss(in, obss); 401 in->in_intval = ic->ic_bintval; 402 ieee80211_free_node(obss); 403 } 404 } 405 406 static int 407 ieee80211_match_bss(ieee80211com_t *ic, ieee80211_node_t *in) 408 { 409 uint8_t rate; 410 int fail; 411 412 fail = 0; 413 if (ieee80211_isclr(ic->ic_chan_active, 414 ieee80211_chan2ieee(ic, in->in_chan))) { 415 fail |= IEEE80211_BADCHAN; 416 } 417 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 418 in->in_chan != ic->ic_des_chan) { 419 fail |= IEEE80211_BADCHAN; 420 } 421 if (ic->ic_opmode == IEEE80211_M_IBSS) { 422 if (!(in->in_capinfo & IEEE80211_CAPINFO_IBSS)) 423 fail |= IEEE80211_BADOPMODE; 424 } else { 425 if (!(in->in_capinfo & IEEE80211_CAPINFO_ESS)) 426 fail |= IEEE80211_BADOPMODE; 427 } 428 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 429 if (!(in->in_capinfo & IEEE80211_CAPINFO_PRIVACY)) 430 fail |= IEEE80211_BADPRIVACY; 431 } else { 432 if (in->in_capinfo & IEEE80211_CAPINFO_PRIVACY) 433 fail |= IEEE80211_BADPRIVACY; 434 } 435 rate = ieee80211_fix_rate(in, IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 436 if (rate & IEEE80211_RATE_BASIC) 437 fail |= IEEE80211_BADRATE; 438 if (ic->ic_des_esslen != 0 && 439 (in->in_esslen != ic->ic_des_esslen || 440 memcmp(in->in_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) { 441 fail |= IEEE80211_BADESSID; 442 } 443 if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 444 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, in->in_bssid)) { 445 fail |= IEEE80211_BADBSSID; 446 } 447 if (in->in_fails >= IEEE80211_STA_FAILS_MAX) 448 fail |= IEEE80211_NODEFAIL; 449 450 return (fail); 451 } 452 453 #define IEEE80211_MAXRATE(_rs) \ 454 ((_rs).ir_rates[(_rs).ir_nrates - 1] & IEEE80211_RATE_VAL) 455 456 /* 457 * Compare the capabilities of node a with node b and decide which is 458 * more desirable (return b if b is considered better than a). Note 459 * that we assume compatibility/usability has already been checked 460 * so we don't need to (e.g. validate whether privacy is supported). 461 * Used to select the best scan candidate for association in a BSS. 462 * 463 * Return desired node 464 */ 465 static ieee80211_node_t * 466 ieee80211_node_compare(ieee80211com_t *ic, ieee80211_node_t *a, 467 ieee80211_node_t *b) 468 { 469 uint8_t maxa; 470 uint8_t maxb; 471 uint8_t rssia; 472 uint8_t rssib; 473 474 /* privacy support preferred */ 475 if ((a->in_capinfo & IEEE80211_CAPINFO_PRIVACY) && 476 !(b->in_capinfo & IEEE80211_CAPINFO_PRIVACY)) { 477 return (a); 478 } 479 if (!(a->in_capinfo & IEEE80211_CAPINFO_PRIVACY) && 480 (b->in_capinfo & IEEE80211_CAPINFO_PRIVACY)) { 481 return (b); 482 } 483 484 /* compare count of previous failures */ 485 if (b->in_fails != a->in_fails) 486 return ((a->in_fails > b->in_fails) ? b : a); 487 488 rssia = ic->ic_node_getrssi(a); 489 rssib = ic->ic_node_getrssi(b); 490 if (ABS(rssib - rssia) < IEEE80211_RSSI_CMP_THRESHOLD) { 491 /* best/max rate preferred if signal level close enough */ 492 maxa = IEEE80211_MAXRATE(a->in_rates); 493 maxb = IEEE80211_MAXRATE(b->in_rates); 494 if (maxa != maxb) 495 return ((maxb > maxa) ? b : a); 496 /* for now just prefer 5Ghz band to all other bands */ 497 if (IEEE80211_IS_CHAN_5GHZ(a->in_chan) && 498 !IEEE80211_IS_CHAN_5GHZ(b->in_chan)) { 499 return (a); 500 } 501 if (!IEEE80211_IS_CHAN_5GHZ(a->in_chan) && 502 IEEE80211_IS_CHAN_5GHZ(b->in_chan)) { 503 return (b); 504 } 505 } 506 /* all things being equal, compare signal level */ 507 return ((rssib > rssia) ? b : a); 508 } 509 510 /* 511 * Mark an ongoing scan stopped. 512 */ 513 void 514 ieee80211_cancel_scan(ieee80211com_t *ic) 515 { 516 IEEE80211_LOCK(ic); 517 ieee80211_dbg(IEEE80211_MSG_SCAN, "ieee80211_cancel_scan()" 518 "end %s scan\n", 519 (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive"); 520 ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN); 521 cv_broadcast(&((ieee80211_impl_t *)ic->ic_private)->im_scan_cv); 522 IEEE80211_UNLOCK(ic); 523 } 524 525 /* 526 * Complete a scan of potential channels. It is called by 527 * ieee80211_next_scan() when the state machine has performed 528 * a full cycle of scaning on all available radio channels. 529 * ieee80211_end_scan() will inspect the node cache for suitable 530 * APs found during scaning, and associate with one, should 531 * the parameters of the node match those of the configuration 532 * requested from userland. 533 */ 534 void 535 ieee80211_end_scan(ieee80211com_t *ic) 536 { 537 ieee80211_node_table_t *nt = &ic->ic_scan; 538 ieee80211_node_t *in; 539 ieee80211_node_t *selbs; 540 541 ieee80211_cancel_scan(ic); 542 IEEE80211_LOCK(ic); 543 544 /* 545 * Automatic sequencing; look for a candidate and 546 * if found join the network. 547 */ 548 /* NB: unlocked read should be ok */ 549 in = list_head(&nt->nt_node); 550 if (in == NULL) { 551 ieee80211_dbg(IEEE80211_MSG_SCAN, "ieee80211_end_scan: " 552 "no scan candidate\n"); 553 notfound: 554 if (ic->ic_opmode == IEEE80211_M_IBSS && 555 (ic->ic_flags & IEEE80211_F_IBSSON) && 556 ic->ic_des_esslen != 0) { 557 ieee80211_create_ibss(ic, ic->ic_ibss_chan); 558 IEEE80211_UNLOCK(ic); 559 return; 560 } 561 562 /* 563 * Reset the list of channels to scan and start again. 564 */ 565 ieee80211_reset_scan(ic); 566 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN; 567 IEEE80211_UNLOCK(ic); 568 569 ieee80211_next_scan(ic); 570 return; 571 } 572 573 if (ic->ic_flags & IEEE80211_F_SCANONLY) { /* scan only */ 574 ic->ic_flags &= ~IEEE80211_F_SCANONLY; 575 IEEE80211_UNLOCK(ic); 576 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 577 return; 578 } 579 580 selbs = NULL; 581 IEEE80211_NODE_LOCK(nt); 582 while (in != NULL) { 583 if (in->in_fails >= IEEE80211_STA_FAILS_MAX) { 584 ieee80211_node_t *tmpin = in; 585 586 /* 587 * The configuration of the access points may change 588 * during my scan. So delete the entry for the AP 589 * and retry to associate if there is another beacon. 590 */ 591 in = list_next(&nt->nt_node, tmpin); 592 ieee80211_node_reclaim(nt, tmpin); 593 continue; 594 } 595 if (ieee80211_match_bss(ic, in) == 0) { 596 if (selbs == NULL) 597 selbs = in; 598 else 599 selbs = ieee80211_node_compare(ic, selbs, in); 600 } 601 in = list_next(&nt->nt_node, in); 602 } 603 IEEE80211_NODE_UNLOCK(nt); 604 if (selbs == NULL) 605 goto notfound; 606 IEEE80211_UNLOCK(ic); 607 ieee80211_sta_join(ic, selbs); 608 } 609 610 611 /* 612 * Handle 802.11 ad hoc network merge. The convention, set by the 613 * Wireless Ethernet Compatibility Alliance (WECA), is that an 802.11 614 * station will change its BSSID to match the "oldest" 802.11 ad hoc 615 * network, on the same channel, that has the station's desired SSID. 616 * The "oldest" 802.11 network sends beacons with the greatest TSF 617 * timestamp. 618 * The caller is assumed to validate TSF's before attempting a merge. 619 * 620 * Return B_TRUE if the BSSID changed, B_FALSE otherwise. 621 */ 622 boolean_t 623 ieee80211_ibss_merge(ieee80211_node_t *in) 624 { 625 ieee80211com_t *ic = in->in_ic; 626 627 if (in == ic->ic_bss || 628 IEEE80211_ADDR_EQ(in->in_bssid, ic->ic_bss->in_bssid)) { 629 /* unchanged, nothing to do */ 630 return (B_FALSE); 631 } 632 if (ieee80211_match_bss(ic, in) != 0) { /* capabilities mismatch */ 633 ieee80211_dbg(IEEE80211_MSG_ASSOC, "ieee80211_ibss_merge: " 634 " merge failed, capabilities mismatch\n"); 635 return (B_FALSE); 636 } 637 ieee80211_dbg(IEEE80211_MSG_ASSOC, "ieee80211_ibss_merge: " 638 "new bssid %s: %s preamble, %s slot time%s\n", 639 ieee80211_macaddr_sprintf(in->in_bssid), 640 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long", 641 (ic->ic_flags & IEEE80211_F_SHSLOT) ? "short" : "long", 642 (ic->ic_flags&IEEE80211_F_USEPROT) ? ", protection" : ""); 643 ieee80211_sta_join(ic, in); 644 return (B_TRUE); 645 } 646 647 /* 648 * Join the specified IBSS/BSS network. The node is assumed to 649 * be passed in with a held reference. 650 */ 651 void 652 ieee80211_sta_join(ieee80211com_t *ic, ieee80211_node_t *selbs) 653 { 654 ieee80211_impl_t *im = ic->ic_private; 655 ieee80211_node_t *obss; 656 657 IEEE80211_LOCK(ic); 658 if (ic->ic_opmode == IEEE80211_M_IBSS) { 659 ieee80211_node_table_t *nt; 660 661 /* 662 * Delete unusable rates; we've already checked 663 * that the negotiated rate set is acceptable. 664 */ 665 (void) ieee80211_fix_rate(selbs, IEEE80211_F_DODEL); 666 /* 667 * Fillin the neighbor table 668 */ 669 nt = &ic->ic_sta; 670 IEEE80211_NODE_LOCK(nt); 671 nt->nt_name = "neighbor"; 672 nt->nt_inact_init = im->im_inact_run; 673 IEEE80211_NODE_UNLOCK(nt); 674 } 675 676 /* 677 * Committed to selbs, setup state. 678 */ 679 obss = ic->ic_bss; 680 ic->ic_bss = ieee80211_ref_node(selbs); /* Grab reference */ 681 if (obss != NULL) { 682 ieee80211_copy_bss(selbs, obss); 683 ieee80211_free_node(obss); 684 } 685 ic->ic_curmode = ieee80211_chan2mode(ic, selbs->in_chan); 686 ic->ic_curchan = selbs->in_chan; 687 /* 688 * Set the erp state (mostly the slot time) to deal with 689 * the auto-select case; this should be redundant if the 690 * mode is locked. 691 */ 692 ieee80211_reset_erp(ic); 693 694 IEEE80211_UNLOCK(ic); 695 if (ic->ic_opmode == IEEE80211_M_STA) 696 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 697 else 698 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 699 } 700 701 /* 702 * Leave the specified IBSS/BSS network. The node is assumed to 703 * be passed in with a held reference. 704 */ 705 void 706 ieee80211_sta_leave(ieee80211com_t *ic, ieee80211_node_t *in) 707 { 708 IEEE80211_LOCK(ic); 709 ic->ic_node_cleanup(in); 710 ieee80211_notify_node_leave(ic, in); 711 IEEE80211_UNLOCK(ic); 712 } 713 714 /* 715 * Allocate a node. This is the default callback function for 716 * ic_node_alloc. This function may be overridden by the driver 717 * to allocate device specific node structure. 718 */ 719 /* ARGSUSED */ 720 static ieee80211_node_t * 721 ieee80211_node_alloc(ieee80211com_t *ic) 722 { 723 return (kmem_zalloc(sizeof (ieee80211_node_t), KM_SLEEP)); 724 } 725 726 /* 727 * Cleanup a node, free any memory associated with the node. 728 * This is the default callback function for ic_node_cleanup 729 * and may be overridden by the driver. 730 */ 731 static void 732 ieee80211_node_cleanup(ieee80211_node_t *in) 733 { 734 in->in_associd = 0; 735 in->in_rssi = 0; 736 in->in_rstamp = 0; 737 if (in->in_challenge != NULL) { 738 kmem_free(in->in_challenge, IEEE80211_CHALLENGE_LEN); 739 in->in_challenge = NULL; 740 } 741 if (in->in_rxfrag != NULL) { 742 freemsg(in->in_rxfrag); 743 in->in_rxfrag = NULL; 744 } 745 } 746 747 /* 748 * Free a node. This is the default callback function for ic_node_free 749 * and may be overridden by the driver to free memory used by device 750 * specific node structure 751 */ 752 static void 753 ieee80211_node_free(ieee80211_node_t *in) 754 { 755 ieee80211com_t *ic = in->in_ic; 756 757 ic->ic_node_cleanup(in); 758 kmem_free(in, sizeof (ieee80211_node_t)); 759 } 760 761 /* 762 * Get a node current RSSI value. This is the default callback function 763 * for ic_node_getrssi and may be overridden by the driver to provide 764 * device specific RSSI calculation algorithm. 765 */ 766 static uint8_t 767 ieee80211_node_getrssi(const ieee80211_node_t *in) 768 { 769 return (in->in_rssi); 770 } 771 772 /* Free fragment if not needed anymore */ 773 static void 774 node_cleanfrag(ieee80211_node_t *in) 775 { 776 clock_t ticks; 777 778 ticks = ddi_get_lbolt(); 779 if (in->in_rxfrag != NULL && ticks > (in->in_rxfragstamp + hz)) { 780 freemsg(in->in_rxfrag); 781 in->in_rxfrag = NULL; 782 } 783 } 784 785 /* 786 * Setup a node. Initialize the node with specified macaddr. Associate 787 * with the interface softc, ic, and add it to the specified node 788 * database. 789 */ 790 static void 791 ieee80211_setup_node(ieee80211com_t *ic, ieee80211_node_table_t *nt, 792 ieee80211_node_t *in, const uint8_t *macaddr) 793 { 794 int32_t hash; 795 796 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_setup_node(): " 797 "%p<%s> in %s table\n", in, 798 ieee80211_macaddr_sprintf(macaddr), 799 (nt != NULL) ? nt->nt_name : "NULL"); 800 801 in->in_ic = ic; 802 IEEE80211_ADDR_COPY(in->in_macaddr, macaddr); 803 hash = ieee80211_node_hash(macaddr); 804 ieee80211_node_initref(in); /* mark referenced */ 805 in->in_authmode = IEEE80211_AUTH_OPEN; 806 in->in_txpower = ic->ic_txpowlimit; /* max power */ 807 in->in_chan = IEEE80211_CHAN_ANYC; 808 in->in_inact_reload = IEEE80211_INACT_INIT; 809 in->in_inact = in->in_inact_reload; 810 ieee80211_crypto_resetkey(ic, &in->in_ucastkey, IEEE80211_KEYIX_NONE); 811 812 if (nt != NULL) { 813 IEEE80211_NODE_LOCK(nt); 814 list_insert_tail(&nt->nt_node, in); 815 list_insert_tail(&nt->nt_hash[hash], in); 816 in->in_table = nt; 817 in->in_inact_reload = nt->nt_inact_init; 818 IEEE80211_NODE_UNLOCK(nt); 819 } 820 } 821 822 /* 823 * Allocates and initialize a node with specified MAC address. 824 * Associate the node with the interface ic. If the allocation 825 * is successful, the node structure is initialized by 826 * ieee80211_setup_node(); otherwise, NULL is returned 827 */ 828 ieee80211_node_t * 829 ieee80211_alloc_node(ieee80211com_t *ic, ieee80211_node_table_t *nt, 830 const uint8_t *macaddr) 831 { 832 ieee80211_node_t *in; 833 834 in = ic->ic_node_alloc(ic); 835 if (in != NULL) 836 ieee80211_setup_node(ic, nt, in, macaddr); 837 return (in); 838 } 839 840 /* 841 * Craft a temporary node suitable for sending a management frame 842 * to the specified station. We craft only as much state as we 843 * need to do the work since the node will be immediately reclaimed 844 * once the send completes. 845 */ 846 ieee80211_node_t * 847 ieee80211_tmp_node(ieee80211com_t *ic, const uint8_t *macaddr) 848 { 849 ieee80211_node_t *in; 850 851 in = ic->ic_node_alloc(ic); 852 if (in != NULL) { 853 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_tmp_node: " 854 "%p<%s>\n", in, ieee80211_macaddr_sprintf(macaddr)); 855 856 IEEE80211_ADDR_COPY(in->in_macaddr, macaddr); 857 IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_bss->in_bssid); 858 ieee80211_node_initref(in); /* mark referenced */ 859 in->in_txpower = ic->ic_bss->in_txpower; 860 /* NB: required by ieee80211_fix_rate */ 861 ieee80211_node_setchan(ic, in, ic->ic_bss->in_chan); 862 ieee80211_crypto_resetkey(ic, &in->in_ucastkey, 863 IEEE80211_KEYIX_NONE); 864 865 in->in_table = NULL; /* NB: pedantic */ 866 in->in_ic = ic; 867 } 868 869 return (in); 870 } 871 872 /* 873 * ieee80211_dup_bss() is similar to ieee80211_alloc_node(), 874 * but is instead used to create a node database entry for 875 * the specified BSSID. If the allocation is successful, the 876 * node is initialized, otherwise, NULL is returned. 877 */ 878 ieee80211_node_t * 879 ieee80211_dup_bss(ieee80211_node_table_t *nt, const uint8_t *macaddr) 880 { 881 ieee80211com_t *ic = nt->nt_ic; 882 ieee80211_node_t *in; 883 884 in = ieee80211_alloc_node(ic, nt, macaddr); 885 if (in != NULL) { 886 /* 887 * Inherit from ic_bss. 888 */ 889 ieee80211_copy_bss(in, ic->ic_bss); 890 IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_bss->in_bssid); 891 ieee80211_node_setchan(ic, in, ic->ic_bss->in_chan); 892 } 893 894 return (in); 895 } 896 897 /* 898 * Iterate through the node table, searching for a node entry which 899 * matches macaddr. If the entry is found, its reference count is 900 * incremented, and a pointer to the node is returned; otherwise, 901 * NULL will be returned. 902 * The node table lock is acquired by the caller. 903 */ 904 static ieee80211_node_t * 905 ieee80211_find_node_locked(ieee80211_node_table_t *nt, const uint8_t *macaddr) 906 { 907 ieee80211_node_t *in; 908 int hash; 909 910 ASSERT(IEEE80211_NODE_IS_LOCKED(nt)); 911 912 hash = ieee80211_node_hash(macaddr); 913 in = list_head(&nt->nt_hash[hash]); 914 while (in != NULL) { 915 if (IEEE80211_ADDR_EQ(in->in_macaddr, macaddr)) 916 return (ieee80211_ref_node(in)); /* mark referenced */ 917 in = list_next(&nt->nt_hash[hash], in); 918 } 919 return (NULL); 920 } 921 922 /* 923 * Iterate through the node table, searching for a node entry 924 * which match specified mac address. 925 * Return NULL if no matching node found. 926 */ 927 ieee80211_node_t * 928 ieee80211_find_node(ieee80211_node_table_t *nt, const uint8_t *macaddr) 929 { 930 ieee80211_node_t *in; 931 932 IEEE80211_NODE_LOCK(nt); 933 in = ieee80211_find_node_locked(nt, macaddr); 934 IEEE80211_NODE_UNLOCK(nt); 935 return (in); 936 } 937 938 /* 939 * Fake up a node; this handles node discovery in adhoc mode. 940 * Note that for the driver's benefit we treat this like an 941 * association so the driver has an opportunity to setup it's 942 * private state. 943 */ 944 ieee80211_node_t * 945 ieee80211_fakeup_adhoc_node(ieee80211_node_table_t *nt, const uint8_t *macaddr) 946 { 947 ieee80211com_t *ic = nt->nt_ic; 948 ieee80211_node_t *in; 949 950 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_fakeup_adhoc_node: " 951 "mac<%s>\n", ieee80211_macaddr_sprintf(macaddr)); 952 in = ieee80211_dup_bss(nt, macaddr); 953 if (in != NULL) { 954 /* no rate negotiation; just dup */ 955 in->in_rates = ic->ic_bss->in_rates; 956 if (ic->ic_node_newassoc != NULL) 957 ic->ic_node_newassoc(in, 1); 958 ieee80211_node_authorize(in); 959 } 960 return (in); 961 } 962 963 /* 964 * Process a beacon or probe response frame. 965 */ 966 void 967 ieee80211_add_scan(ieee80211com_t *ic, const struct ieee80211_scanparams *sp, 968 const struct ieee80211_frame *wh, int subtype, int rssi, int rstamp) 969 { 970 ieee80211_node_table_t *nt = &ic->ic_scan; 971 ieee80211_node_t *in; 972 boolean_t newnode = B_FALSE; 973 974 in = ieee80211_find_node(nt, wh->i_addr2); 975 if (in == NULL) { 976 /* 977 * Create a new entry. 978 */ 979 in = ieee80211_alloc_node(ic, nt, wh->i_addr2); 980 if (in == NULL) { 981 ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_add_scan: " 982 "alloc node failed\n"); 983 return; 984 } 985 /* 986 * inherit from ic_bss. 987 */ 988 ieee80211_copy_bss(in, ic->ic_bss); 989 ieee80211_node_setchan(ic, in, ic->ic_curchan); 990 newnode = B_TRUE; 991 } 992 993 /* ap beaconing multiple ssid w/ same bssid */ 994 995 /* 996 * sp->ssid[0] - element ID 997 * sp->ssid[1] - length 998 * sp->ssid[2]... - ssid 999 */ 1000 if (sp->ssid[1] != 0 && 1001 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP || 1002 in->in_esslen == 0) { 1003 in->in_esslen = sp->ssid[1]; 1004 bzero(in->in_essid, sizeof (in->in_essid)); 1005 bcopy(sp->ssid + 2, in->in_essid, sp->ssid[1]); 1006 } 1007 IEEE80211_ADDR_COPY(in->in_bssid, wh->i_addr3); 1008 in->in_rssi = (uint8_t)rssi; 1009 in->in_rstamp = rstamp; 1010 bcopy(sp->tstamp, in->in_tstamp.data, sizeof (in->in_tstamp)); 1011 in->in_intval = sp->bintval; 1012 in->in_capinfo = sp->capinfo; 1013 in->in_chan = &ic->ic_sup_channels[sp->chan]; 1014 in->in_phytype = sp->phytype; 1015 in->in_fhdwell = sp->fhdwell; 1016 in->in_fhindex = sp->fhindex; 1017 in->in_erp = sp->erp; 1018 if (sp->tim != NULL) { 1019 struct ieee80211_tim_ie *ie; 1020 1021 ie = (struct ieee80211_tim_ie *)sp->tim; 1022 in->in_dtim_count = ie->tim_count; 1023 in->in_dtim_period = ie->tim_period; 1024 } 1025 /* 1026 * Record the byte offset from the mac header to 1027 * the start of the TIM information element for 1028 * use by hardware and/or to speedup software 1029 * processing of beacon frames. 1030 */ 1031 in->in_tim_off = sp->timoff; 1032 1033 /* NB: must be after in_chan is setup */ 1034 (void) ieee80211_setup_rates(in, sp->rates, sp->xrates, 1035 IEEE80211_F_DOSORT); 1036 1037 if (!newnode) 1038 ieee80211_free_node(in); 1039 } 1040 1041 /* 1042 * Initialize/update an ad-hoc node with contents from a received 1043 * beacon frame. 1044 */ 1045 void 1046 ieee80211_init_neighbor(ieee80211_node_t *in, const struct ieee80211_frame *wh, 1047 const struct ieee80211_scanparams *sp) 1048 { 1049 in->in_esslen = sp->ssid[1]; 1050 (void) memcpy(in->in_essid, sp->ssid + 2, sp->ssid[1]); 1051 IEEE80211_ADDR_COPY(in->in_bssid, wh->i_addr3); 1052 (void) memcpy(in->in_tstamp.data, sp->tstamp, sizeof (in->in_tstamp)); 1053 in->in_intval = sp->bintval; 1054 in->in_capinfo = sp->capinfo; 1055 in->in_chan = in->in_ic->ic_curchan; 1056 in->in_fhdwell = sp->fhdwell; 1057 in->in_fhindex = sp->fhindex; 1058 in->in_erp = sp->erp; 1059 in->in_tim_off = sp->timoff; 1060 1061 /* NB: must be after in_chan is setup */ 1062 (void) ieee80211_setup_rates(in, sp->rates, sp->xrates, 1063 IEEE80211_F_DOSORT); 1064 } 1065 1066 /* 1067 * Do node discovery in adhoc mode on receipt of a beacon 1068 * or probe response frame. Note that for the driver's 1069 * benefit we we treat this like an association so the 1070 * driver has an opportuinty to setup it's private state. 1071 */ 1072 ieee80211_node_t * 1073 ieee80211_add_neighbor(ieee80211com_t *ic, const struct ieee80211_frame *wh, 1074 const struct ieee80211_scanparams *sp) 1075 { 1076 ieee80211_node_t *in; 1077 1078 in = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2); 1079 if (in != NULL) { 1080 ieee80211_init_neighbor(in, wh, sp); 1081 if (ic->ic_node_newassoc != NULL) 1082 ic->ic_node_newassoc(in, 1); 1083 } 1084 return (in); 1085 } 1086 1087 #define IEEE80211_IS_CTL(wh) \ 1088 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) 1089 1090 /* 1091 * Locate the node for sender, track state, and then pass the 1092 * (referenced) node up to the 802.11 layer for its use. We 1093 * are required to pass some node so we fall back to ic_bss 1094 * when this frame is from an unknown sender. The 802.11 layer 1095 * knows this means the sender wasn't in the node table and 1096 * acts accordingly. 1097 */ 1098 ieee80211_node_t * 1099 ieee80211_find_rxnode(ieee80211com_t *ic, const struct ieee80211_frame *wh) 1100 { 1101 ieee80211_node_table_t *nt; 1102 ieee80211_node_t *in; 1103 1104 /* may want scanned nodes in the neighbor table for adhoc */ 1105 if (ic->ic_opmode == IEEE80211_M_STA || 1106 (ic->ic_flags & IEEE80211_F_SCAN)) { 1107 nt = &ic->ic_scan; 1108 } else { 1109 nt = &ic->ic_sta; 1110 } 1111 1112 IEEE80211_NODE_LOCK(nt); 1113 if (IEEE80211_IS_CTL(wh)) 1114 in = ieee80211_find_node_locked(nt, wh->i_addr1); 1115 else 1116 in = ieee80211_find_node_locked(nt, wh->i_addr2); 1117 IEEE80211_NODE_UNLOCK(nt); 1118 1119 if (in == NULL) 1120 in = ieee80211_ref_node(ic->ic_bss); 1121 1122 return (in); 1123 } 1124 1125 /* 1126 * Return a reference to the appropriate node for sending 1127 * a data frame. This handles node discovery in adhoc networks. 1128 */ 1129 ieee80211_node_t * 1130 ieee80211_find_txnode(ieee80211com_t *ic, const uint8_t *daddr) 1131 { 1132 ieee80211_node_table_t *nt = &ic->ic_sta; 1133 ieee80211_node_t *in; 1134 1135 /* 1136 * The destination address should be in the node table 1137 * unless this is a multicast/broadcast frame. We can 1138 * also optimize station mode operation, all frames go 1139 * to the bss node. 1140 */ 1141 IEEE80211_NODE_LOCK(nt); 1142 if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(daddr)) 1143 in = ieee80211_ref_node(ic->ic_bss); 1144 else 1145 in = ieee80211_find_node_locked(nt, daddr); 1146 IEEE80211_NODE_UNLOCK(nt); 1147 1148 if (in == NULL) { 1149 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1150 /* 1151 * In adhoc mode cons up a node for the destination. 1152 * Note that we need an additional reference for the 1153 * caller to be consistent with 1154 * ieee80211_find_node_locked 1155 * can't hold lock across ieee80211_dup_bss 'cuz of 1156 * recursive locking 1157 */ 1158 in = ieee80211_fakeup_adhoc_node(nt, daddr); 1159 if (in != NULL) 1160 (void) ieee80211_ref_node(in); 1161 } else { 1162 ieee80211_dbg(IEEE80211_MSG_OUTPUT, 1163 "ieee80211_find_txnode: " 1164 "[%s] no node, discard frame\n", 1165 ieee80211_macaddr_sprintf(daddr)); 1166 } 1167 } 1168 return (in); 1169 } 1170 1171 /* 1172 * Remove a node from the node database entries and free memory 1173 * associated with the node. The node table lock is acquired by 1174 * the caller. 1175 */ 1176 static void 1177 ieee80211_free_node_locked(ieee80211_node_t *in) 1178 { 1179 ieee80211com_t *ic = in->in_ic; 1180 ieee80211_node_table_t *nt = in->in_table; 1181 int32_t hash; 1182 1183 if (nt != NULL) { 1184 hash = ieee80211_node_hash(in->in_macaddr); 1185 list_remove(&nt->nt_hash[hash], in); 1186 list_remove(&nt->nt_node, in); 1187 } 1188 ic->ic_node_free(in); 1189 } 1190 1191 /* 1192 * Remove a node from the node database entries and free any 1193 * memory associated with the node. 1194 * This method can be overridden in ieee80211_attach() 1195 */ 1196 void 1197 ieee80211_free_node(ieee80211_node_t *in) 1198 { 1199 ieee80211_node_table_t *nt = in->in_table; 1200 1201 if (nt != NULL) 1202 IEEE80211_NODE_LOCK(nt); 1203 if (ieee80211_node_decref_nv(in) == 0) 1204 ieee80211_free_node_locked(in); 1205 if (nt != NULL) 1206 IEEE80211_NODE_UNLOCK(nt); 1207 } 1208 1209 /* 1210 * Reclaim a node. If this is the last reference count then 1211 * do the normal free work. Otherwise remove it from the node 1212 * table and mark it gone by clearing the back-reference. 1213 */ 1214 static void 1215 ieee80211_node_reclaim(ieee80211_node_table_t *nt, ieee80211_node_t *in) 1216 { 1217 int32_t hash; 1218 1219 IEEE80211_NODE_LOCK_ASSERT(nt); 1220 ieee80211_dbg(IEEE80211_MSG_NODE, "node_reclaim: " 1221 " remove %p<%s> from %s table, refcnt %d\n", 1222 in, ieee80211_macaddr_sprintf(in->in_macaddr), nt->nt_name, 1223 ieee80211_node_refcnt(in)); 1224 1225 if (ieee80211_node_decref_nv(in) != 0) { 1226 /* 1227 * Clear any entry in the unicast key mapping table. 1228 * We need to do it here so rx lookups don't find it 1229 * in the mapping table even if it's not in the hash 1230 * table. We cannot depend on the mapping table entry 1231 * being cleared because the node may not be free'd. 1232 */ 1233 hash = ieee80211_node_hash(in->in_macaddr); 1234 list_remove(&nt->nt_hash[hash], in); 1235 list_remove(&nt->nt_node, in); 1236 in->in_table = NULL; 1237 } else { 1238 ieee80211_free_node_locked(in); 1239 } 1240 } 1241 1242 /* 1243 * Iterate through the node list and reclaim all node in the node table. 1244 * The node table lock is acquired by the caller 1245 */ 1246 static void 1247 ieee80211_free_allnodes_locked(ieee80211_node_table_t *nt) 1248 { 1249 ieee80211_node_t *in; 1250 1251 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_free_allnodes_locked(): " 1252 "free all nodes in %s table\n", nt->nt_name); 1253 1254 in = list_head(&nt->nt_node); 1255 while (in != NULL) { 1256 ieee80211_node_reclaim(nt, in); 1257 in = list_head(&nt->nt_node); 1258 } 1259 ieee80211_reset_erp(nt->nt_ic); 1260 } 1261 1262 /* 1263 * Iterate through the node list, calling ieee80211_node_reclaim() for 1264 * all nodes associated with the interface. 1265 */ 1266 static void 1267 ieee80211_free_allnodes(ieee80211_node_table_t *nt) 1268 { 1269 IEEE80211_NODE_LOCK(nt); 1270 ieee80211_free_allnodes_locked(nt); 1271 IEEE80211_NODE_UNLOCK(nt); 1272 } 1273 1274 /* 1275 * Timeout entries in the scan cache. This is the timeout callback 1276 * function of node table ic_scan which is called when the inactivity 1277 * timer expires. 1278 */ 1279 static void 1280 ieee80211_timeout_scan_candidates(ieee80211_node_table_t *nt) 1281 { 1282 ieee80211com_t *ic = nt->nt_ic; 1283 ieee80211_node_t *in; 1284 1285 IEEE80211_NODE_LOCK(nt); 1286 in = ic->ic_bss; 1287 node_cleanfrag(in); /* Free fragment if not needed */ 1288 nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1289 IEEE80211_NODE_UNLOCK(nt); 1290 } 1291 1292 /* 1293 * Timeout inactive stations and do related housekeeping. 1294 * Note that we cannot hold the node lock while sending a 1295 * frame as this would lead to a LOR. Instead we use a 1296 * generation number to mark nodes that we've scanned and 1297 * drop the lock and restart a scan if we have to time out 1298 * a node. Since we are single-threaded by virtue of 1299 * controlling the inactivity timer we can be sure this will 1300 * process each node only once. 1301 */ 1302 static void 1303 ieee80211_timeout_stations(ieee80211_node_table_t *nt) 1304 { 1305 ieee80211com_t *ic = nt->nt_ic; 1306 ieee80211_impl_t *im = ic->ic_private; 1307 ieee80211_node_t *in = NULL; 1308 uint32_t gen; 1309 boolean_t isadhoc; 1310 1311 IEEE80211_LOCK_ASSERT(ic); 1312 isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS || 1313 ic->ic_opmode == IEEE80211_M_AHDEMO); 1314 IEEE80211_SCAN_LOCK(nt); 1315 gen = ++nt->nt_scangen; 1316 restart: 1317 IEEE80211_NODE_LOCK(nt); 1318 for (in = list_head(&nt->nt_node); in != NULL; 1319 in = list_next(&nt->nt_node, in)) { 1320 if (in->in_scangen == gen) /* previously handled */ 1321 continue; 1322 in->in_scangen = gen; 1323 node_cleanfrag(in); /* free fragment if not needed */ 1324 1325 /* 1326 * Special case ourself; we may be idle for extended periods 1327 * of time and regardless reclaiming our state is wrong. 1328 */ 1329 if (in == ic->ic_bss) 1330 continue; 1331 in->in_inact--; 1332 if (in->in_associd != 0 || isadhoc) { 1333 /* 1334 * Probe the station before time it out. We 1335 * send a null data frame which may not be 1336 * uinversally supported by drivers (need it 1337 * for ps-poll support so it should be...). 1338 */ 1339 if (0 < in->in_inact && 1340 in->in_inact <= im->im_inact_probe) { 1341 ieee80211_dbg(IEEE80211_MSG_NODE, "net80211: " 1342 "probe station due to inactivity\n"); 1343 IEEE80211_NODE_UNLOCK(nt); 1344 IEEE80211_UNLOCK(ic); 1345 (void) ieee80211_send_nulldata(in); 1346 IEEE80211_LOCK(ic); 1347 goto restart; 1348 } 1349 } 1350 if (in->in_inact <= 0) { 1351 ieee80211_dbg(IEEE80211_MSG_NODE, "net80211: " 1352 "station timed out due to inact (refcnt %u)\n", 1353 ieee80211_node_refcnt(in)); 1354 /* 1355 * Send a deauthenticate frame and drop the station. 1356 * This is somewhat complicated due to reference counts 1357 * and locking. At this point a station will typically 1358 * have a reference count of 1. ieee80211_node_leave 1359 * will do a "free" of the node which will drop the 1360 * reference count. But in the meantime a reference 1361 * wil be held by the deauth frame. The actual reclaim 1362 * of the node will happen either after the tx is 1363 * completed or by ieee80211_node_leave. 1364 * 1365 * Separately we must drop the node lock before sending 1366 * in case the driver takes a lock, as this will result 1367 * in LOR between the node lock and the driver lock. 1368 */ 1369 IEEE80211_NODE_UNLOCK(nt); 1370 if (in->in_associd != 0) { 1371 IEEE80211_UNLOCK(ic); 1372 IEEE80211_SEND_MGMT(ic, in, 1373 IEEE80211_FC0_SUBTYPE_DEAUTH, 1374 IEEE80211_REASON_AUTH_EXPIRE); 1375 IEEE80211_LOCK(ic); 1376 } 1377 ieee80211_node_leave(ic, in); 1378 goto restart; 1379 } 1380 } 1381 IEEE80211_NODE_UNLOCK(nt); 1382 1383 IEEE80211_SCAN_UNLOCK(nt); 1384 1385 nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1386 } 1387 1388 /* 1389 * Call the user-defined call back function for all nodes in 1390 * the node cache. The callback is invoked with the user-supplied 1391 * value and a pointer to the current node. 1392 */ 1393 void 1394 ieee80211_iterate_nodes(ieee80211_node_table_t *nt, ieee80211_iter_func *f, 1395 void *arg) 1396 { 1397 ieee80211_node_t *in; 1398 1399 IEEE80211_NODE_LOCK(nt); 1400 in = list_head(&nt->nt_node); 1401 while (in != NULL) { 1402 (void) ieee80211_ref_node(in); 1403 IEEE80211_NODE_UNLOCK(nt); 1404 (*f)(arg, in); 1405 ieee80211_free_node(in); 1406 IEEE80211_NODE_LOCK(nt); 1407 in = list_next(&nt->nt_node, in); 1408 } 1409 IEEE80211_NODE_UNLOCK(nt); 1410 } 1411 1412 /* 1413 * Handle bookkeeping for station deauthentication/disassociation 1414 * when operating as an ap. 1415 */ 1416 static void 1417 ieee80211_node_leave(ieee80211com_t *ic, ieee80211_node_t *in) 1418 { 1419 ieee80211_node_table_t *nt = in->in_table; 1420 1421 ASSERT(ic->ic_opmode == IEEE80211_M_IBSS); 1422 1423 /* 1424 * Remove the node from any table it's recorded in and 1425 * drop the caller's reference. Removal from the table 1426 * is important to insure the node is not reprocessed 1427 * for inactivity. 1428 */ 1429 if (nt != NULL) { 1430 IEEE80211_NODE_LOCK(nt); 1431 ieee80211_node_reclaim(nt, in); 1432 IEEE80211_NODE_UNLOCK(nt); 1433 } else { 1434 ieee80211_free_node(in); 1435 } 1436 } 1437 1438 /* 1439 * Initialize a node table with specified name, inactivity timer value 1440 * and callback inactivity timeout function. Associate the node table 1441 * with interface softc, ic. 1442 */ 1443 static void 1444 ieee80211_node_table_init(ieee80211com_t *ic, ieee80211_node_table_t *nt, 1445 const char *name, int inact, int keyixmax, 1446 void (*timeout)(ieee80211_node_table_t *)) 1447 { 1448 int i; 1449 1450 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_init():" 1451 "%s table, inact %d\n", name, inact); 1452 1453 nt->nt_ic = ic; 1454 nt->nt_name = name; 1455 nt->nt_inact_timer = 0; 1456 nt->nt_inact_init = inact; 1457 nt->nt_timeout = timeout; 1458 nt->nt_keyixmax = keyixmax; 1459 nt->nt_scangen = 1; 1460 mutex_init(&nt->nt_scanlock, NULL, MUTEX_DRIVER, NULL); 1461 mutex_init(&nt->nt_nodelock, NULL, MUTEX_DRIVER, NULL); 1462 1463 list_create(&nt->nt_node, sizeof (ieee80211_node_t), 1464 offsetof(ieee80211_node_t, in_node)); 1465 for (i = 0; i < IEEE80211_NODE_HASHSIZE; i++) { 1466 list_create(&nt->nt_hash[i], sizeof (ieee80211_node_t), 1467 offsetof(ieee80211_node_t, in_hash)); 1468 } 1469 } 1470 1471 /* 1472 * Reset a node table. Clean its inactivity timer and call 1473 * ieee80211_free_allnodes_locked() to free all nodes in the 1474 * node table. 1475 */ 1476 void 1477 ieee80211_node_table_reset(ieee80211_node_table_t *nt) 1478 { 1479 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_reset(): " 1480 "%s table\n", nt->nt_name); 1481 1482 IEEE80211_NODE_LOCK(nt); 1483 nt->nt_inact_timer = 0; 1484 ieee80211_free_allnodes_locked(nt); 1485 IEEE80211_NODE_UNLOCK(nt); 1486 } 1487 1488 /* 1489 * Destroy a node table. Free all nodes in the node table. 1490 * This function is usually called by node detach function. 1491 */ 1492 static void 1493 ieee80211_node_table_cleanup(ieee80211_node_table_t *nt) 1494 { 1495 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_cleanup(): " 1496 "%s table\n", nt->nt_name); 1497 1498 IEEE80211_NODE_LOCK(nt); 1499 ieee80211_free_allnodes_locked(nt); 1500 IEEE80211_NODE_UNLOCK(nt); 1501 mutex_destroy(&nt->nt_nodelock); 1502 mutex_destroy(&nt->nt_scanlock); 1503 } 1504