1 /* 2 * Copyright 2007 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 /* 596 * It's possible at some special moments, the in_chan will 597 * be none. Need to skip the null node. 598 */ 599 if (in->in_chan == IEEE80211_CHAN_ANYC) { 600 in = list_next(&nt->nt_node, in); 601 continue; 602 } 603 if (ieee80211_match_bss(ic, in) == 0) { 604 if (selbs == NULL) 605 selbs = in; 606 else 607 selbs = ieee80211_node_compare(ic, selbs, in); 608 } 609 in = list_next(&nt->nt_node, in); 610 } 611 IEEE80211_NODE_UNLOCK(nt); 612 if (selbs == NULL) 613 goto notfound; 614 IEEE80211_UNLOCK(ic); 615 ieee80211_sta_join(ic, selbs); 616 } 617 618 619 /* 620 * Handle 802.11 ad hoc network merge. The convention, set by the 621 * Wireless Ethernet Compatibility Alliance (WECA), is that an 802.11 622 * station will change its BSSID to match the "oldest" 802.11 ad hoc 623 * network, on the same channel, that has the station's desired SSID. 624 * The "oldest" 802.11 network sends beacons with the greatest TSF 625 * timestamp. 626 * The caller is assumed to validate TSF's before attempting a merge. 627 * 628 * Return B_TRUE if the BSSID changed, B_FALSE otherwise. 629 */ 630 boolean_t 631 ieee80211_ibss_merge(ieee80211_node_t *in) 632 { 633 ieee80211com_t *ic = in->in_ic; 634 635 if (in == ic->ic_bss || 636 IEEE80211_ADDR_EQ(in->in_bssid, ic->ic_bss->in_bssid)) { 637 /* unchanged, nothing to do */ 638 return (B_FALSE); 639 } 640 if (ieee80211_match_bss(ic, in) != 0) { /* capabilities mismatch */ 641 ieee80211_dbg(IEEE80211_MSG_ASSOC, "ieee80211_ibss_merge: " 642 " merge failed, capabilities mismatch\n"); 643 return (B_FALSE); 644 } 645 ieee80211_dbg(IEEE80211_MSG_ASSOC, "ieee80211_ibss_merge: " 646 "new bssid %s: %s preamble, %s slot time%s\n", 647 ieee80211_macaddr_sprintf(in->in_bssid), 648 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long", 649 (ic->ic_flags & IEEE80211_F_SHSLOT) ? "short" : "long", 650 (ic->ic_flags&IEEE80211_F_USEPROT) ? ", protection" : ""); 651 ieee80211_sta_join(ic, in); 652 return (B_TRUE); 653 } 654 655 /* 656 * Join the specified IBSS/BSS network. The node is assumed to 657 * be passed in with a held reference. 658 */ 659 void 660 ieee80211_sta_join(ieee80211com_t *ic, ieee80211_node_t *selbs) 661 { 662 ieee80211_impl_t *im = ic->ic_private; 663 ieee80211_node_t *obss; 664 665 IEEE80211_LOCK(ic); 666 if (ic->ic_opmode == IEEE80211_M_IBSS) { 667 ieee80211_node_table_t *nt; 668 669 /* 670 * Delete unusable rates; we've already checked 671 * that the negotiated rate set is acceptable. 672 */ 673 (void) ieee80211_fix_rate(selbs, IEEE80211_F_DODEL); 674 /* 675 * Fillin the neighbor table 676 */ 677 nt = &ic->ic_sta; 678 IEEE80211_NODE_LOCK(nt); 679 nt->nt_name = "neighbor"; 680 nt->nt_inact_init = im->im_inact_run; 681 IEEE80211_NODE_UNLOCK(nt); 682 } 683 684 /* 685 * Committed to selbs, setup state. 686 */ 687 obss = ic->ic_bss; 688 ic->ic_bss = ieee80211_ref_node(selbs); /* Grab reference */ 689 if (obss != NULL) { 690 ieee80211_copy_bss(selbs, obss); 691 ieee80211_free_node(obss); 692 } 693 ic->ic_curmode = ieee80211_chan2mode(ic, selbs->in_chan); 694 ic->ic_curchan = selbs->in_chan; 695 /* 696 * Set the erp state (mostly the slot time) to deal with 697 * the auto-select case; this should be redundant if the 698 * mode is locked. 699 */ 700 ieee80211_reset_erp(ic); 701 702 IEEE80211_UNLOCK(ic); 703 if (ic->ic_opmode == IEEE80211_M_STA) 704 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 705 else 706 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 707 } 708 709 /* 710 * Leave the specified IBSS/BSS network. The node is assumed to 711 * be passed in with a held reference. 712 */ 713 void 714 ieee80211_sta_leave(ieee80211com_t *ic, ieee80211_node_t *in) 715 { 716 IEEE80211_LOCK(ic); 717 ic->ic_node_cleanup(in); 718 ieee80211_notify_node_leave(ic, in); 719 IEEE80211_UNLOCK(ic); 720 } 721 722 /* 723 * Allocate a node. This is the default callback function for 724 * ic_node_alloc. This function may be overridden by the driver 725 * to allocate device specific node structure. 726 */ 727 /* ARGSUSED */ 728 static ieee80211_node_t * 729 ieee80211_node_alloc(ieee80211com_t *ic) 730 { 731 return (kmem_zalloc(sizeof (ieee80211_node_t), KM_SLEEP)); 732 } 733 734 /* 735 * Cleanup a node, free any memory associated with the node. 736 * This is the default callback function for ic_node_cleanup 737 * and may be overridden by the driver. 738 */ 739 static void 740 ieee80211_node_cleanup(ieee80211_node_t *in) 741 { 742 in->in_associd = 0; 743 in->in_rssi = 0; 744 in->in_rstamp = 0; 745 if (in->in_challenge != NULL) { 746 kmem_free(in->in_challenge, IEEE80211_CHALLENGE_LEN); 747 in->in_challenge = NULL; 748 } 749 if (in->in_rxfrag != NULL) { 750 freemsg(in->in_rxfrag); 751 in->in_rxfrag = NULL; 752 } 753 } 754 755 /* 756 * Free a node. This is the default callback function for ic_node_free 757 * and may be overridden by the driver to free memory used by device 758 * specific node structure 759 */ 760 static void 761 ieee80211_node_free(ieee80211_node_t *in) 762 { 763 ieee80211com_t *ic = in->in_ic; 764 765 ic->ic_node_cleanup(in); 766 kmem_free(in, sizeof (ieee80211_node_t)); 767 } 768 769 /* 770 * Get a node current RSSI value. This is the default callback function 771 * for ic_node_getrssi and may be overridden by the driver to provide 772 * device specific RSSI calculation algorithm. 773 */ 774 static uint8_t 775 ieee80211_node_getrssi(const ieee80211_node_t *in) 776 { 777 return (in->in_rssi); 778 } 779 780 /* Free fragment if not needed anymore */ 781 static void 782 node_cleanfrag(ieee80211_node_t *in) 783 { 784 clock_t ticks; 785 786 ticks = ddi_get_lbolt(); 787 if (in->in_rxfrag != NULL && ticks > (in->in_rxfragstamp + hz)) { 788 freemsg(in->in_rxfrag); 789 in->in_rxfrag = NULL; 790 } 791 } 792 793 /* 794 * Setup a node. Initialize the node with specified macaddr. Associate 795 * with the interface softc, ic, and add it to the specified node 796 * database. 797 */ 798 static void 799 ieee80211_setup_node(ieee80211com_t *ic, ieee80211_node_table_t *nt, 800 ieee80211_node_t *in, const uint8_t *macaddr) 801 { 802 int32_t hash; 803 804 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_setup_node(): " 805 "%p<%s> in %s table\n", in, 806 ieee80211_macaddr_sprintf(macaddr), 807 (nt != NULL) ? nt->nt_name : "NULL"); 808 809 in->in_ic = ic; 810 IEEE80211_ADDR_COPY(in->in_macaddr, macaddr); 811 hash = ieee80211_node_hash(macaddr); 812 ieee80211_node_initref(in); /* mark referenced */ 813 in->in_authmode = IEEE80211_AUTH_OPEN; 814 in->in_txpower = ic->ic_txpowlimit; /* max power */ 815 in->in_chan = IEEE80211_CHAN_ANYC; 816 in->in_inact_reload = IEEE80211_INACT_INIT; 817 in->in_inact = in->in_inact_reload; 818 ieee80211_crypto_resetkey(ic, &in->in_ucastkey, IEEE80211_KEYIX_NONE); 819 820 if (nt != NULL) { 821 IEEE80211_NODE_LOCK(nt); 822 list_insert_tail(&nt->nt_node, in); 823 list_insert_tail(&nt->nt_hash[hash], in); 824 in->in_table = nt; 825 in->in_inact_reload = nt->nt_inact_init; 826 IEEE80211_NODE_UNLOCK(nt); 827 } 828 } 829 830 /* 831 * Allocates and initialize a node with specified MAC address. 832 * Associate the node with the interface ic. If the allocation 833 * is successful, the node structure is initialized by 834 * ieee80211_setup_node(); otherwise, NULL is returned 835 */ 836 ieee80211_node_t * 837 ieee80211_alloc_node(ieee80211com_t *ic, ieee80211_node_table_t *nt, 838 const uint8_t *macaddr) 839 { 840 ieee80211_node_t *in; 841 842 in = ic->ic_node_alloc(ic); 843 if (in != NULL) 844 ieee80211_setup_node(ic, nt, in, macaddr); 845 return (in); 846 } 847 848 /* 849 * Craft a temporary node suitable for sending a management frame 850 * to the specified station. We craft only as much state as we 851 * need to do the work since the node will be immediately reclaimed 852 * once the send completes. 853 */ 854 ieee80211_node_t * 855 ieee80211_tmp_node(ieee80211com_t *ic, const uint8_t *macaddr) 856 { 857 ieee80211_node_t *in; 858 859 in = ic->ic_node_alloc(ic); 860 if (in != NULL) { 861 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_tmp_node: " 862 "%p<%s>\n", in, ieee80211_macaddr_sprintf(macaddr)); 863 864 IEEE80211_ADDR_COPY(in->in_macaddr, macaddr); 865 IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_bss->in_bssid); 866 ieee80211_node_initref(in); /* mark referenced */ 867 in->in_txpower = ic->ic_bss->in_txpower; 868 /* NB: required by ieee80211_fix_rate */ 869 ieee80211_node_setchan(ic, in, ic->ic_bss->in_chan); 870 ieee80211_crypto_resetkey(ic, &in->in_ucastkey, 871 IEEE80211_KEYIX_NONE); 872 873 in->in_table = NULL; /* NB: pedantic */ 874 in->in_ic = ic; 875 } 876 877 return (in); 878 } 879 880 /* 881 * ieee80211_dup_bss() is similar to ieee80211_alloc_node(), 882 * but is instead used to create a node database entry for 883 * the specified BSSID. If the allocation is successful, the 884 * node is initialized, otherwise, NULL is returned. 885 */ 886 ieee80211_node_t * 887 ieee80211_dup_bss(ieee80211_node_table_t *nt, const uint8_t *macaddr) 888 { 889 ieee80211com_t *ic = nt->nt_ic; 890 ieee80211_node_t *in; 891 892 in = ieee80211_alloc_node(ic, nt, macaddr); 893 if (in != NULL) { 894 /* 895 * Inherit from ic_bss. 896 */ 897 ieee80211_copy_bss(in, ic->ic_bss); 898 IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_bss->in_bssid); 899 ieee80211_node_setchan(ic, in, ic->ic_bss->in_chan); 900 } 901 902 return (in); 903 } 904 905 /* 906 * Iterate through the node table, searching for a node entry which 907 * matches macaddr. If the entry is found, its reference count is 908 * incremented, and a pointer to the node is returned; otherwise, 909 * NULL will be returned. 910 * The node table lock is acquired by the caller. 911 */ 912 static ieee80211_node_t * 913 ieee80211_find_node_locked(ieee80211_node_table_t *nt, const uint8_t *macaddr) 914 { 915 ieee80211_node_t *in; 916 int hash; 917 918 ASSERT(IEEE80211_NODE_IS_LOCKED(nt)); 919 920 hash = ieee80211_node_hash(macaddr); 921 in = list_head(&nt->nt_hash[hash]); 922 while (in != NULL) { 923 if (IEEE80211_ADDR_EQ(in->in_macaddr, macaddr)) 924 return (ieee80211_ref_node(in)); /* mark referenced */ 925 in = list_next(&nt->nt_hash[hash], in); 926 } 927 return (NULL); 928 } 929 930 /* 931 * Iterate through the node table, searching for a node entry 932 * which match specified mac address. 933 * Return NULL if no matching node found. 934 */ 935 ieee80211_node_t * 936 ieee80211_find_node(ieee80211_node_table_t *nt, const uint8_t *macaddr) 937 { 938 ieee80211_node_t *in; 939 940 IEEE80211_NODE_LOCK(nt); 941 in = ieee80211_find_node_locked(nt, macaddr); 942 IEEE80211_NODE_UNLOCK(nt); 943 return (in); 944 } 945 946 /* 947 * Fake up a node; this handles node discovery in adhoc mode. 948 * Note that for the driver's benefit we treat this like an 949 * association so the driver has an opportunity to setup it's 950 * private state. 951 */ 952 ieee80211_node_t * 953 ieee80211_fakeup_adhoc_node(ieee80211_node_table_t *nt, const uint8_t *macaddr) 954 { 955 ieee80211com_t *ic = nt->nt_ic; 956 ieee80211_node_t *in; 957 958 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_fakeup_adhoc_node: " 959 "mac<%s>\n", ieee80211_macaddr_sprintf(macaddr)); 960 in = ieee80211_dup_bss(nt, macaddr); 961 if (in != NULL) { 962 /* no rate negotiation; just dup */ 963 in->in_rates = ic->ic_bss->in_rates; 964 if (ic->ic_node_newassoc != NULL) 965 ic->ic_node_newassoc(in, 1); 966 ieee80211_node_authorize(in); 967 } 968 return (in); 969 } 970 971 /* 972 * Process a beacon or probe response frame. 973 */ 974 void 975 ieee80211_add_scan(ieee80211com_t *ic, const struct ieee80211_scanparams *sp, 976 const struct ieee80211_frame *wh, int subtype, int rssi, int rstamp) 977 { 978 ieee80211_node_table_t *nt = &ic->ic_scan; 979 ieee80211_node_t *in; 980 boolean_t newnode = B_FALSE; 981 982 in = ieee80211_find_node(nt, wh->i_addr2); 983 if (in == NULL) { 984 /* 985 * Create a new entry. 986 */ 987 in = ieee80211_alloc_node(ic, nt, wh->i_addr2); 988 if (in == NULL) { 989 ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_add_scan: " 990 "alloc node failed\n"); 991 return; 992 } 993 /* 994 * inherit from ic_bss. 995 */ 996 ieee80211_copy_bss(in, ic->ic_bss); 997 ieee80211_node_setchan(ic, in, ic->ic_curchan); 998 newnode = B_TRUE; 999 } 1000 1001 /* ap beaconing multiple ssid w/ same bssid */ 1002 1003 /* 1004 * sp->ssid[0] - element ID 1005 * sp->ssid[1] - length 1006 * sp->ssid[2]... - ssid 1007 */ 1008 if (sp->ssid[1] != 0 && 1009 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP || 1010 in->in_esslen == 0) { 1011 in->in_esslen = sp->ssid[1]; 1012 bzero(in->in_essid, sizeof (in->in_essid)); 1013 bcopy(sp->ssid + 2, in->in_essid, sp->ssid[1]); 1014 } 1015 IEEE80211_ADDR_COPY(in->in_bssid, wh->i_addr3); 1016 in->in_rssi = (uint8_t)rssi; 1017 in->in_rstamp = rstamp; 1018 bcopy(sp->tstamp, in->in_tstamp.data, sizeof (in->in_tstamp)); 1019 in->in_intval = sp->bintval; 1020 in->in_capinfo = sp->capinfo; 1021 in->in_chan = &ic->ic_sup_channels[sp->chan]; 1022 in->in_phytype = sp->phytype; 1023 in->in_fhdwell = sp->fhdwell; 1024 in->in_fhindex = sp->fhindex; 1025 in->in_erp = sp->erp; 1026 if (sp->tim != NULL) { 1027 struct ieee80211_tim_ie *ie; 1028 1029 ie = (struct ieee80211_tim_ie *)sp->tim; 1030 in->in_dtim_count = ie->tim_count; 1031 in->in_dtim_period = ie->tim_period; 1032 } 1033 /* 1034 * Record the byte offset from the mac header to 1035 * the start of the TIM information element for 1036 * use by hardware and/or to speedup software 1037 * processing of beacon frames. 1038 */ 1039 in->in_tim_off = sp->timoff; 1040 1041 /* NB: must be after in_chan is setup */ 1042 (void) ieee80211_setup_rates(in, sp->rates, sp->xrates, 1043 IEEE80211_F_DOSORT); 1044 1045 if (!newnode) 1046 ieee80211_free_node(in); 1047 } 1048 1049 /* 1050 * Initialize/update an ad-hoc node with contents from a received 1051 * beacon frame. 1052 */ 1053 void 1054 ieee80211_init_neighbor(ieee80211_node_t *in, const struct ieee80211_frame *wh, 1055 const struct ieee80211_scanparams *sp) 1056 { 1057 in->in_esslen = sp->ssid[1]; 1058 (void) memcpy(in->in_essid, sp->ssid + 2, sp->ssid[1]); 1059 IEEE80211_ADDR_COPY(in->in_bssid, wh->i_addr3); 1060 (void) memcpy(in->in_tstamp.data, sp->tstamp, sizeof (in->in_tstamp)); 1061 in->in_intval = sp->bintval; 1062 in->in_capinfo = sp->capinfo; 1063 in->in_chan = in->in_ic->ic_curchan; 1064 in->in_fhdwell = sp->fhdwell; 1065 in->in_fhindex = sp->fhindex; 1066 in->in_erp = sp->erp; 1067 in->in_tim_off = sp->timoff; 1068 1069 /* NB: must be after in_chan is setup */ 1070 (void) ieee80211_setup_rates(in, sp->rates, sp->xrates, 1071 IEEE80211_F_DOSORT); 1072 } 1073 1074 /* 1075 * Do node discovery in adhoc mode on receipt of a beacon 1076 * or probe response frame. Note that for the driver's 1077 * benefit we we treat this like an association so the 1078 * driver has an opportuinty to setup it's private state. 1079 */ 1080 ieee80211_node_t * 1081 ieee80211_add_neighbor(ieee80211com_t *ic, const struct ieee80211_frame *wh, 1082 const struct ieee80211_scanparams *sp) 1083 { 1084 ieee80211_node_t *in; 1085 1086 in = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2); 1087 if (in != NULL) { 1088 ieee80211_init_neighbor(in, wh, sp); 1089 if (ic->ic_node_newassoc != NULL) 1090 ic->ic_node_newassoc(in, 1); 1091 } 1092 return (in); 1093 } 1094 1095 #define IEEE80211_IS_CTL(wh) \ 1096 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) 1097 1098 /* 1099 * Locate the node for sender, track state, and then pass the 1100 * (referenced) node up to the 802.11 layer for its use. We 1101 * are required to pass some node so we fall back to ic_bss 1102 * when this frame is from an unknown sender. The 802.11 layer 1103 * knows this means the sender wasn't in the node table and 1104 * acts accordingly. 1105 */ 1106 ieee80211_node_t * 1107 ieee80211_find_rxnode(ieee80211com_t *ic, const struct ieee80211_frame *wh) 1108 { 1109 ieee80211_node_table_t *nt; 1110 ieee80211_node_t *in; 1111 1112 /* may want scanned nodes in the neighbor table for adhoc */ 1113 if (ic->ic_opmode == IEEE80211_M_STA || 1114 (ic->ic_flags & IEEE80211_F_SCAN)) { 1115 nt = &ic->ic_scan; 1116 } else { 1117 nt = &ic->ic_sta; 1118 } 1119 1120 IEEE80211_NODE_LOCK(nt); 1121 if (IEEE80211_IS_CTL(wh)) 1122 in = ieee80211_find_node_locked(nt, wh->i_addr1); 1123 else 1124 in = ieee80211_find_node_locked(nt, wh->i_addr2); 1125 IEEE80211_NODE_UNLOCK(nt); 1126 1127 if (in == NULL) 1128 in = ieee80211_ref_node(ic->ic_bss); 1129 1130 return (in); 1131 } 1132 1133 /* 1134 * Return a reference to the appropriate node for sending 1135 * a data frame. This handles node discovery in adhoc networks. 1136 */ 1137 ieee80211_node_t * 1138 ieee80211_find_txnode(ieee80211com_t *ic, const uint8_t *daddr) 1139 { 1140 ieee80211_node_table_t *nt = &ic->ic_sta; 1141 ieee80211_node_t *in; 1142 1143 /* 1144 * The destination address should be in the node table 1145 * unless this is a multicast/broadcast frame. We can 1146 * also optimize station mode operation, all frames go 1147 * to the bss node. 1148 */ 1149 IEEE80211_NODE_LOCK(nt); 1150 if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(daddr)) 1151 in = ieee80211_ref_node(ic->ic_bss); 1152 else 1153 in = ieee80211_find_node_locked(nt, daddr); 1154 IEEE80211_NODE_UNLOCK(nt); 1155 1156 if (in == NULL) { 1157 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1158 /* 1159 * In adhoc mode cons up a node for the destination. 1160 * Note that we need an additional reference for the 1161 * caller to be consistent with 1162 * ieee80211_find_node_locked 1163 * can't hold lock across ieee80211_dup_bss 'cuz of 1164 * recursive locking 1165 */ 1166 in = ieee80211_fakeup_adhoc_node(nt, daddr); 1167 if (in != NULL) 1168 (void) ieee80211_ref_node(in); 1169 } else { 1170 ieee80211_dbg(IEEE80211_MSG_OUTPUT, 1171 "ieee80211_find_txnode: " 1172 "[%s] no node, discard frame\n", 1173 ieee80211_macaddr_sprintf(daddr)); 1174 } 1175 } 1176 return (in); 1177 } 1178 1179 /* 1180 * Remove a node from the node database entries and free memory 1181 * associated with the node. The node table lock is acquired by 1182 * the caller. 1183 */ 1184 static void 1185 ieee80211_free_node_locked(ieee80211_node_t *in) 1186 { 1187 ieee80211com_t *ic = in->in_ic; 1188 ieee80211_node_table_t *nt = in->in_table; 1189 int32_t hash; 1190 1191 if (nt != NULL) { 1192 hash = ieee80211_node_hash(in->in_macaddr); 1193 list_remove(&nt->nt_hash[hash], in); 1194 list_remove(&nt->nt_node, in); 1195 } 1196 ic->ic_node_free(in); 1197 } 1198 1199 /* 1200 * Remove a node from the node database entries and free any 1201 * memory associated with the node. 1202 * This method can be overridden in ieee80211_attach() 1203 */ 1204 void 1205 ieee80211_free_node(ieee80211_node_t *in) 1206 { 1207 ieee80211_node_table_t *nt = in->in_table; 1208 1209 if (nt != NULL) 1210 IEEE80211_NODE_LOCK(nt); 1211 if (ieee80211_node_decref_nv(in) == 0) 1212 ieee80211_free_node_locked(in); 1213 if (nt != NULL) 1214 IEEE80211_NODE_UNLOCK(nt); 1215 } 1216 1217 /* 1218 * Reclaim a node. If this is the last reference count then 1219 * do the normal free work. Otherwise remove it from the node 1220 * table and mark it gone by clearing the back-reference. 1221 */ 1222 static void 1223 ieee80211_node_reclaim(ieee80211_node_table_t *nt, ieee80211_node_t *in) 1224 { 1225 int32_t hash; 1226 1227 IEEE80211_NODE_LOCK_ASSERT(nt); 1228 ieee80211_dbg(IEEE80211_MSG_NODE, "node_reclaim: " 1229 " remove %p<%s> from %s table, refcnt %d\n", 1230 in, ieee80211_macaddr_sprintf(in->in_macaddr), nt->nt_name, 1231 ieee80211_node_refcnt(in)); 1232 1233 if (ieee80211_node_decref_nv(in) != 0) { 1234 /* 1235 * Clear any entry in the unicast key mapping table. 1236 * We need to do it here so rx lookups don't find it 1237 * in the mapping table even if it's not in the hash 1238 * table. We cannot depend on the mapping table entry 1239 * being cleared because the node may not be free'd. 1240 */ 1241 hash = ieee80211_node_hash(in->in_macaddr); 1242 list_remove(&nt->nt_hash[hash], in); 1243 list_remove(&nt->nt_node, in); 1244 in->in_table = NULL; 1245 } else { 1246 ieee80211_free_node_locked(in); 1247 } 1248 } 1249 1250 /* 1251 * Iterate through the node list and reclaim all node in the node table. 1252 * The node table lock is acquired by the caller 1253 */ 1254 static void 1255 ieee80211_free_allnodes_locked(ieee80211_node_table_t *nt) 1256 { 1257 ieee80211_node_t *in; 1258 1259 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_free_allnodes_locked(): " 1260 "free all nodes in %s table\n", nt->nt_name); 1261 1262 in = list_head(&nt->nt_node); 1263 while (in != NULL) { 1264 ieee80211_node_reclaim(nt, in); 1265 in = list_head(&nt->nt_node); 1266 } 1267 ieee80211_reset_erp(nt->nt_ic); 1268 } 1269 1270 /* 1271 * Iterate through the node list, calling ieee80211_node_reclaim() for 1272 * all nodes associated with the interface. 1273 */ 1274 static void 1275 ieee80211_free_allnodes(ieee80211_node_table_t *nt) 1276 { 1277 IEEE80211_NODE_LOCK(nt); 1278 ieee80211_free_allnodes_locked(nt); 1279 IEEE80211_NODE_UNLOCK(nt); 1280 } 1281 1282 /* 1283 * Timeout entries in the scan cache. This is the timeout callback 1284 * function of node table ic_scan which is called when the inactivity 1285 * timer expires. 1286 */ 1287 static void 1288 ieee80211_timeout_scan_candidates(ieee80211_node_table_t *nt) 1289 { 1290 ieee80211com_t *ic = nt->nt_ic; 1291 ieee80211_node_t *in; 1292 1293 IEEE80211_NODE_LOCK(nt); 1294 in = ic->ic_bss; 1295 node_cleanfrag(in); /* Free fragment if not needed */ 1296 nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1297 IEEE80211_NODE_UNLOCK(nt); 1298 } 1299 1300 /* 1301 * Timeout inactive stations and do related housekeeping. 1302 * Note that we cannot hold the node lock while sending a 1303 * frame as this would lead to a LOR. Instead we use a 1304 * generation number to mark nodes that we've scanned and 1305 * drop the lock and restart a scan if we have to time out 1306 * a node. Since we are single-threaded by virtue of 1307 * controlling the inactivity timer we can be sure this will 1308 * process each node only once. 1309 */ 1310 static void 1311 ieee80211_timeout_stations(ieee80211_node_table_t *nt) 1312 { 1313 ieee80211com_t *ic = nt->nt_ic; 1314 ieee80211_impl_t *im = ic->ic_private; 1315 ieee80211_node_t *in = NULL; 1316 uint32_t gen; 1317 boolean_t isadhoc; 1318 1319 IEEE80211_LOCK_ASSERT(ic); 1320 isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS || 1321 ic->ic_opmode == IEEE80211_M_AHDEMO); 1322 IEEE80211_SCAN_LOCK(nt); 1323 gen = ++nt->nt_scangen; 1324 restart: 1325 IEEE80211_NODE_LOCK(nt); 1326 for (in = list_head(&nt->nt_node); in != NULL; 1327 in = list_next(&nt->nt_node, in)) { 1328 if (in->in_scangen == gen) /* previously handled */ 1329 continue; 1330 in->in_scangen = gen; 1331 node_cleanfrag(in); /* free fragment if not needed */ 1332 1333 /* 1334 * Special case ourself; we may be idle for extended periods 1335 * of time and regardless reclaiming our state is wrong. 1336 */ 1337 if (in == ic->ic_bss) 1338 continue; 1339 in->in_inact--; 1340 if (in->in_associd != 0 || isadhoc) { 1341 /* 1342 * Probe the station before time it out. We 1343 * send a null data frame which may not be 1344 * uinversally supported by drivers (need it 1345 * for ps-poll support so it should be...). 1346 */ 1347 if (0 < in->in_inact && 1348 in->in_inact <= im->im_inact_probe) { 1349 ieee80211_dbg(IEEE80211_MSG_NODE, "net80211: " 1350 "probe station due to inactivity\n"); 1351 IEEE80211_NODE_UNLOCK(nt); 1352 IEEE80211_UNLOCK(ic); 1353 (void) ieee80211_send_nulldata(in); 1354 IEEE80211_LOCK(ic); 1355 goto restart; 1356 } 1357 } 1358 if (in->in_inact <= 0) { 1359 ieee80211_dbg(IEEE80211_MSG_NODE, "net80211: " 1360 "station timed out due to inact (refcnt %u)\n", 1361 ieee80211_node_refcnt(in)); 1362 /* 1363 * Send a deauthenticate frame and drop the station. 1364 * This is somewhat complicated due to reference counts 1365 * and locking. At this point a station will typically 1366 * have a reference count of 1. ieee80211_node_leave 1367 * will do a "free" of the node which will drop the 1368 * reference count. But in the meantime a reference 1369 * wil be held by the deauth frame. The actual reclaim 1370 * of the node will happen either after the tx is 1371 * completed or by ieee80211_node_leave. 1372 * 1373 * Separately we must drop the node lock before sending 1374 * in case the driver takes a lock, as this will result 1375 * in LOR between the node lock and the driver lock. 1376 */ 1377 IEEE80211_NODE_UNLOCK(nt); 1378 if (in->in_associd != 0) { 1379 IEEE80211_UNLOCK(ic); 1380 IEEE80211_SEND_MGMT(ic, in, 1381 IEEE80211_FC0_SUBTYPE_DEAUTH, 1382 IEEE80211_REASON_AUTH_EXPIRE); 1383 IEEE80211_LOCK(ic); 1384 } 1385 ieee80211_node_leave(ic, in); 1386 goto restart; 1387 } 1388 } 1389 IEEE80211_NODE_UNLOCK(nt); 1390 1391 IEEE80211_SCAN_UNLOCK(nt); 1392 1393 nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1394 } 1395 1396 /* 1397 * Call the user-defined call back function for all nodes in 1398 * the node cache. The callback is invoked with the user-supplied 1399 * value and a pointer to the current node. 1400 */ 1401 void 1402 ieee80211_iterate_nodes(ieee80211_node_table_t *nt, ieee80211_iter_func *f, 1403 void *arg) 1404 { 1405 ieee80211_node_t *in; 1406 1407 IEEE80211_NODE_LOCK(nt); 1408 in = list_head(&nt->nt_node); 1409 while (in != NULL) { 1410 (void) ieee80211_ref_node(in); 1411 IEEE80211_NODE_UNLOCK(nt); 1412 (*f)(arg, in); 1413 ieee80211_free_node(in); 1414 IEEE80211_NODE_LOCK(nt); 1415 in = list_next(&nt->nt_node, in); 1416 } 1417 IEEE80211_NODE_UNLOCK(nt); 1418 } 1419 1420 /* 1421 * Handle bookkeeping for station deauthentication/disassociation 1422 * when operating as an ap. 1423 */ 1424 static void 1425 ieee80211_node_leave(ieee80211com_t *ic, ieee80211_node_t *in) 1426 { 1427 ieee80211_node_table_t *nt = in->in_table; 1428 1429 ASSERT(ic->ic_opmode == IEEE80211_M_IBSS); 1430 1431 /* 1432 * Remove the node from any table it's recorded in and 1433 * drop the caller's reference. Removal from the table 1434 * is important to insure the node is not reprocessed 1435 * for inactivity. 1436 */ 1437 if (nt != NULL) { 1438 IEEE80211_NODE_LOCK(nt); 1439 ieee80211_node_reclaim(nt, in); 1440 IEEE80211_NODE_UNLOCK(nt); 1441 } else { 1442 ieee80211_free_node(in); 1443 } 1444 } 1445 1446 /* 1447 * Initialize a node table with specified name, inactivity timer value 1448 * and callback inactivity timeout function. Associate the node table 1449 * with interface softc, ic. 1450 */ 1451 static void 1452 ieee80211_node_table_init(ieee80211com_t *ic, ieee80211_node_table_t *nt, 1453 const char *name, int inact, int keyixmax, 1454 void (*timeout)(ieee80211_node_table_t *)) 1455 { 1456 int i; 1457 1458 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_init():" 1459 "%s table, inact %d\n", name, inact); 1460 1461 nt->nt_ic = ic; 1462 nt->nt_name = name; 1463 nt->nt_inact_timer = 0; 1464 nt->nt_inact_init = inact; 1465 nt->nt_timeout = timeout; 1466 nt->nt_keyixmax = keyixmax; 1467 nt->nt_scangen = 1; 1468 mutex_init(&nt->nt_scanlock, NULL, MUTEX_DRIVER, NULL); 1469 mutex_init(&nt->nt_nodelock, NULL, MUTEX_DRIVER, NULL); 1470 1471 list_create(&nt->nt_node, sizeof (ieee80211_node_t), 1472 offsetof(ieee80211_node_t, in_node)); 1473 for (i = 0; i < IEEE80211_NODE_HASHSIZE; i++) { 1474 list_create(&nt->nt_hash[i], sizeof (ieee80211_node_t), 1475 offsetof(ieee80211_node_t, in_hash)); 1476 } 1477 } 1478 1479 /* 1480 * Reset a node table. Clean its inactivity timer and call 1481 * ieee80211_free_allnodes_locked() to free all nodes in the 1482 * node table. 1483 */ 1484 void 1485 ieee80211_node_table_reset(ieee80211_node_table_t *nt) 1486 { 1487 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_reset(): " 1488 "%s table\n", nt->nt_name); 1489 1490 IEEE80211_NODE_LOCK(nt); 1491 nt->nt_inact_timer = 0; 1492 ieee80211_free_allnodes_locked(nt); 1493 IEEE80211_NODE_UNLOCK(nt); 1494 } 1495 1496 /* 1497 * Destroy a node table. Free all nodes in the node table. 1498 * This function is usually called by node detach function. 1499 */ 1500 static void 1501 ieee80211_node_table_cleanup(ieee80211_node_table_t *nt) 1502 { 1503 ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_cleanup(): " 1504 "%s table\n", nt->nt_name); 1505 1506 IEEE80211_NODE_LOCK(nt); 1507 ieee80211_free_allnodes_locked(nt); 1508 IEEE80211_NODE_UNLOCK(nt); 1509 mutex_destroy(&nt->nt_nodelock); 1510 mutex_destroy(&nt->nt_scanlock); 1511 } 1512