1 /*- 2 * Copyright (c) 2001 Atsushi Onoe 3 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * Alternatively, this software may be distributed under the terms of the 18 * GNU General Public License ("GPL") version 2 as published by the Free 19 * Software Foundation. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/mbuf.h> 39 #include <sys/malloc.h> 40 #include <sys/kernel.h> 41 42 #include <sys/socket.h> 43 44 #include <net/if.h> 45 #include <net/if_media.h> 46 #include <net/ethernet.h> 47 48 #include <net80211/ieee80211_var.h> 49 50 #include <net/bpf.h> 51 52 /* 53 * Association id's are managed with a bit vector. 54 */ 55 #define IEEE80211_AID_SET(b, w) \ 56 ((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32))) 57 #define IEEE80211_AID_CLR(b, w) \ 58 ((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32))) 59 #define IEEE80211_AID_ISSET(b, w) \ 60 ((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32))) 61 62 #ifdef IEEE80211_DEBUG_REFCNT 63 #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line 64 #else 65 #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__ 66 #endif 67 68 static struct ieee80211_node *node_alloc(struct ieee80211_node_table *); 69 static void node_cleanup(struct ieee80211_node *); 70 static void node_free(struct ieee80211_node *); 71 static u_int8_t node_getrssi(const struct ieee80211_node *); 72 73 static void ieee80211_setup_node(struct ieee80211_node_table *, 74 struct ieee80211_node *, const u_int8_t *); 75 static void _ieee80211_free_node(struct ieee80211_node *); 76 static void ieee80211_free_allnodes(struct ieee80211_node_table *); 77 78 static void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *); 79 static void ieee80211_timeout_stations(struct ieee80211_node_table *); 80 81 static void ieee80211_set_tim(struct ieee80211_node *, int set); 82 83 static void ieee80211_node_table_init(struct ieee80211com *ic, 84 struct ieee80211_node_table *nt, const char *name, 85 int inact, int keyixmax, 86 void (*timeout)(struct ieee80211_node_table *)); 87 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt); 88 89 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state"); 90 91 void 92 ieee80211_node_attach(struct ieee80211com *ic) 93 { 94 95 ic->ic_node_alloc = node_alloc; 96 ic->ic_node_free = node_free; 97 ic->ic_node_cleanup = node_cleanup; 98 ic->ic_node_getrssi = node_getrssi; 99 100 /* default station inactivity timer setings */ 101 ic->ic_inact_init = IEEE80211_INACT_INIT; 102 ic->ic_inact_auth = IEEE80211_INACT_AUTH; 103 ic->ic_inact_run = IEEE80211_INACT_RUN; 104 ic->ic_inact_probe = IEEE80211_INACT_PROBE; 105 106 /* NB: driver should override */ 107 ic->ic_max_aid = IEEE80211_AID_DEF; 108 ic->ic_set_tim = ieee80211_set_tim; 109 } 110 111 void 112 ieee80211_node_lateattach(struct ieee80211com *ic) 113 { 114 struct ieee80211_rsnparms *rsn; 115 116 if (ic->ic_max_aid > IEEE80211_AID_MAX) 117 ic->ic_max_aid = IEEE80211_AID_MAX; 118 MALLOC(ic->ic_aid_bitmap, u_int32_t *, 119 howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t), 120 M_DEVBUF, M_NOWAIT | M_ZERO); 121 if (ic->ic_aid_bitmap == NULL) { 122 /* XXX no way to recover */ 123 printf("%s: no memory for AID bitmap!\n", __func__); 124 ic->ic_max_aid = 0; 125 } 126 127 /* XXX defer until using hostap/ibss mode */ 128 ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t); 129 MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len, 130 M_DEVBUF, M_NOWAIT | M_ZERO); 131 if (ic->ic_tim_bitmap == NULL) { 132 /* XXX no way to recover */ 133 printf("%s: no memory for TIM bitmap!\n", __func__); 134 } 135 136 ieee80211_node_table_init(ic, &ic->ic_sta, "station", 137 IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix, 138 ieee80211_timeout_stations); 139 ieee80211_node_table_init(ic, &ic->ic_scan, "scan", 140 IEEE80211_INACT_SCAN, 0, 141 ieee80211_timeout_scan_candidates); 142 143 ieee80211_reset_bss(ic); 144 /* 145 * Setup "global settings" in the bss node so that 146 * each new station automatically inherits them. 147 */ 148 rsn = &ic->ic_bss->ni_rsn; 149 /* WEP, TKIP, and AES-CCM are always supported */ 150 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP; 151 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP; 152 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM; 153 if (ic->ic_caps & IEEE80211_C_AES) 154 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB; 155 if (ic->ic_caps & IEEE80211_C_CKIP) 156 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP; 157 /* 158 * Default unicast cipher to WEP for 802.1x use. If 159 * WPA is enabled the management code will set these 160 * values to reflect. 161 */ 162 rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP; 163 rsn->rsn_ucastkeylen = 104 / NBBY; 164 /* 165 * WPA says the multicast cipher is the lowest unicast 166 * cipher supported. But we skip WEP which would 167 * otherwise be used based on this criteria. 168 */ 169 rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP; 170 rsn->rsn_mcastkeylen = 128 / NBBY; 171 172 /* 173 * We support both WPA-PSK and 802.1x; the one used 174 * is determined by the authentication mode and the 175 * setting of the PSK state. 176 */ 177 rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK; 178 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK; 179 180 ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode); 181 } 182 183 void 184 ieee80211_node_detach(struct ieee80211com *ic) 185 { 186 187 if (ic->ic_bss != NULL) { 188 ieee80211_free_node(ic->ic_bss); 189 ic->ic_bss = NULL; 190 } 191 ieee80211_node_table_cleanup(&ic->ic_scan); 192 ieee80211_node_table_cleanup(&ic->ic_sta); 193 if (ic->ic_aid_bitmap != NULL) { 194 FREE(ic->ic_aid_bitmap, M_DEVBUF); 195 ic->ic_aid_bitmap = NULL; 196 } 197 if (ic->ic_tim_bitmap != NULL) { 198 FREE(ic->ic_tim_bitmap, M_DEVBUF); 199 ic->ic_tim_bitmap = NULL; 200 } 201 } 202 203 /* 204 * Port authorize/unauthorize interfaces for use by an authenticator. 205 */ 206 207 void 208 ieee80211_node_authorize(struct ieee80211_node *ni) 209 { 210 struct ieee80211com *ic = ni->ni_ic; 211 212 ni->ni_flags |= IEEE80211_NODE_AUTH; 213 ni->ni_inact_reload = ic->ic_inact_run; 214 } 215 216 void 217 ieee80211_node_unauthorize(struct ieee80211_node *ni) 218 { 219 ni->ni_flags &= ~IEEE80211_NODE_AUTH; 220 } 221 222 /* 223 * Set/change the channel. The rate set is also updated as 224 * to insure a consistent view by drivers. 225 */ 226 static void 227 ieee80211_set_chan(struct ieee80211com *ic, 228 struct ieee80211_node *ni, struct ieee80211_channel *chan) 229 { 230 if (chan == IEEE80211_CHAN_ANYC) /* XXX while scanning */ 231 chan = ic->ic_curchan; 232 ni->ni_chan = chan; 233 ni->ni_rates = *ieee80211_get_suprates(ic, chan); 234 } 235 236 /* 237 * AP scanning support. 238 */ 239 240 #ifdef IEEE80211_DEBUG 241 static void 242 dump_chanlist(const u_char chans[]) 243 { 244 const char *sep; 245 int i; 246 247 sep = " "; 248 for (i = 0; i < IEEE80211_CHAN_MAX; i++) 249 if (isset(chans, i)) { 250 printf("%s%u", sep, i); 251 sep = ", "; 252 } 253 } 254 #endif /* IEEE80211_DEBUG */ 255 256 /* 257 * Initialize the channel set to scan based on the 258 * of available channels and the current PHY mode. 259 */ 260 static void 261 ieee80211_reset_scan(struct ieee80211com *ic) 262 { 263 264 /* XXX ic_des_chan should be handled with ic_chan_active */ 265 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) { 266 memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan)); 267 setbit(ic->ic_chan_scan, 268 ieee80211_chan2ieee(ic, ic->ic_des_chan)); 269 } else 270 memcpy(ic->ic_chan_scan, ic->ic_chan_active, 271 sizeof(ic->ic_chan_active)); 272 #ifdef IEEE80211_DEBUG 273 if (ieee80211_msg_scan(ic)) { 274 printf("%s: scan set:", __func__); 275 dump_chanlist(ic->ic_chan_scan); 276 printf(" start chan %u\n", 277 ieee80211_chan2ieee(ic, ic->ic_curchan)); 278 } 279 #endif /* IEEE80211_DEBUG */ 280 } 281 282 /* 283 * Begin an active scan. 284 */ 285 void 286 ieee80211_begin_scan(struct ieee80211com *ic, int reset) 287 { 288 289 ic->ic_scan.nt_scangen++; 290 /* 291 * In all but hostap mode scanning starts off in 292 * an active mode before switching to passive. 293 */ 294 if (ic->ic_opmode != IEEE80211_M_HOSTAP) { 295 ic->ic_flags |= IEEE80211_F_ASCAN; 296 ic->ic_stats.is_scan_active++; 297 } else 298 ic->ic_stats.is_scan_passive++; 299 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 300 "begin %s scan in %s mode, scangen %u\n", 301 (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive", 302 ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen); 303 /* 304 * Clear scan state and flush any previously seen AP's. 305 */ 306 ieee80211_reset_scan(ic); 307 if (reset) 308 ieee80211_free_allnodes(&ic->ic_scan); 309 310 ic->ic_flags |= IEEE80211_F_SCAN; 311 312 /* Scan the next channel. */ 313 ieee80211_next_scan(ic); 314 } 315 316 /* 317 * Switch to the next channel marked for scanning. 318 */ 319 int 320 ieee80211_next_scan(struct ieee80211com *ic) 321 { 322 struct ieee80211_channel *chan; 323 324 /* 325 * Insure any previous mgt frame timeouts don't fire. 326 * This assumes the driver does the right thing in 327 * flushing anything queued in the driver and below. 328 */ 329 ic->ic_mgt_timer = 0; 330 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN; 331 332 chan = ic->ic_curchan; 333 do { 334 if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX]) 335 chan = &ic->ic_channels[0]; 336 if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) { 337 clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan)); 338 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 339 "%s: chan %d->%d\n", __func__, 340 ieee80211_chan2ieee(ic, ic->ic_curchan), 341 ieee80211_chan2ieee(ic, chan)); 342 ic->ic_curchan = chan; 343 /* 344 * XXX drivers should do this as needed, 345 * XXX for now maintain compatibility 346 */ 347 ic->ic_bss->ni_rates = *ieee80211_get_suprates(ic, chan); 348 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 349 return 1; 350 } 351 } while (chan != ic->ic_curchan); 352 ieee80211_end_scan(ic); 353 return 0; 354 } 355 356 /* 357 * Probe the curent channel, if allowed, while scanning. 358 * If the channel is not marked passive-only then send 359 * a probe request immediately. Otherwise mark state and 360 * listen for beacons on the channel; if we receive something 361 * then we'll transmit a probe request. 362 */ 363 void 364 ieee80211_probe_curchan(struct ieee80211com *ic, int force) 365 { 366 struct ifnet *ifp = ic->ic_ifp; 367 368 if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) { 369 /* 370 * XXX send both broadcast+directed probe request 371 */ 372 ieee80211_send_probereq(ic->ic_bss, 373 ic->ic_myaddr, ifp->if_broadcastaddr, 374 ifp->if_broadcastaddr, 375 ic->ic_des_essid, ic->ic_des_esslen, 376 ic->ic_opt_ie, ic->ic_opt_ie_len); 377 } else 378 ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN; 379 } 380 381 static __inline void 382 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss) 383 { 384 /* propagate useful state */ 385 nbss->ni_authmode = obss->ni_authmode; 386 nbss->ni_txpower = obss->ni_txpower; 387 nbss->ni_vlan = obss->ni_vlan; 388 nbss->ni_rsn = obss->ni_rsn; 389 /* XXX statistics? */ 390 } 391 392 void 393 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 394 { 395 struct ieee80211_node_table *nt; 396 struct ieee80211_node *ni; 397 398 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 399 "%s: creating ibss\n", __func__); 400 401 /* 402 * Create the station/neighbor table. Note that for adhoc 403 * mode we make the initial inactivity timer longer since 404 * we create nodes only through discovery and they typically 405 * are long-lived associations. 406 */ 407 nt = &ic->ic_sta; 408 IEEE80211_NODE_LOCK(nt); 409 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 410 nt->nt_name = "station"; 411 nt->nt_inact_init = ic->ic_inact_init; 412 } else { 413 nt->nt_name = "neighbor"; 414 nt->nt_inact_init = ic->ic_inact_run; 415 } 416 IEEE80211_NODE_UNLOCK(nt); 417 418 ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr); 419 if (ni == NULL) { 420 /* XXX recovery? */ 421 return; 422 } 423 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 424 ni->ni_esslen = ic->ic_des_esslen; 425 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 426 copy_bss(ni, ic->ic_bss); 427 ni->ni_intval = ic->ic_bintval; 428 if (ic->ic_flags & IEEE80211_F_PRIVACY) 429 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 430 if (ic->ic_phytype == IEEE80211_T_FH) { 431 ni->ni_fhdwell = 200; /* XXX */ 432 ni->ni_fhindex = 1; 433 } 434 if (ic->ic_opmode == IEEE80211_M_IBSS) { 435 ic->ic_flags |= IEEE80211_F_SIBSS; 436 ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */ 437 if (ic->ic_flags & IEEE80211_F_DESBSSID) 438 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid); 439 else 440 ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */ 441 } else if (ic->ic_opmode == IEEE80211_M_AHDEMO) { 442 if (ic->ic_flags & IEEE80211_F_DESBSSID) 443 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid); 444 else 445 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN); 446 } 447 /* 448 * Fix the channel and related attributes. 449 */ 450 ieee80211_set_chan(ic, ni, chan); 451 ic->ic_curchan = chan; 452 ic->ic_curmode = ieee80211_chan2mode(ic, chan); 453 /* 454 * Do mode-specific rate setup. 455 */ 456 if (IEEE80211_IS_CHAN_FULL(chan) && 457 (ic->ic_curmode == IEEE80211_MODE_11G || 458 ic->ic_curmode == IEEE80211_MODE_11B)) 459 ieee80211_set11gbasicrates(&ni->ni_rates, ic->ic_curmode); 460 461 (void) ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 462 } 463 464 void 465 ieee80211_reset_bss(struct ieee80211com *ic) 466 { 467 struct ieee80211_node *ni, *obss; 468 469 ieee80211_node_table_reset(&ic->ic_scan); 470 ieee80211_node_table_reset(&ic->ic_sta); 471 472 ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr); 473 KASSERT(ni != NULL, ("unable to setup inital BSS node")); 474 obss = ic->ic_bss; 475 ic->ic_bss = ieee80211_ref_node(ni); 476 if (obss != NULL) { 477 copy_bss(ni, obss); 478 ni->ni_intval = ic->ic_bintval; 479 ieee80211_free_node(obss); 480 } 481 } 482 483 /* XXX tunable */ 484 #define STA_FAILS_MAX 2 /* assoc failures before ignored */ 485 486 static int 487 ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni) 488 { 489 u_int8_t rate; 490 int fail; 491 492 fail = 0; 493 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 494 fail |= 0x01; 495 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 496 ni->ni_chan != ic->ic_des_chan) 497 fail |= 0x01; 498 if (ic->ic_opmode == IEEE80211_M_IBSS) { 499 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 500 fail |= 0x02; 501 } else { 502 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 503 fail |= 0x02; 504 } 505 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 506 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 507 fail |= 0x04; 508 } else { 509 /* XXX does this mean privacy is supported or required? */ 510 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 511 fail |= 0x04; 512 } 513 rate = ieee80211_fix_rate(ni, 514 IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 515 if (rate & IEEE80211_RATE_BASIC) 516 fail |= 0x08; 517 if (ic->ic_des_esslen != 0 && 518 (ni->ni_esslen != ic->ic_des_esslen || 519 memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) 520 fail |= 0x10; 521 if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 522 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 523 fail |= 0x20; 524 if (ni->ni_fails >= STA_FAILS_MAX) 525 fail |= 0x40; 526 #ifdef IEEE80211_DEBUG 527 if (ieee80211_msg_scan(ic)) { 528 printf(" %c %s", 529 fail & 0x40 ? '=' : fail & 0x80 ? '^' : fail ? '-' : '+', 530 ether_sprintf(ni->ni_macaddr)); 531 printf(" %s%c", ether_sprintf(ni->ni_bssid), 532 fail & 0x20 ? '!' : ' '); 533 printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan), 534 fail & 0x01 ? '!' : ' '); 535 printf(" %+4d", ni->ni_rssi); 536 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 537 fail & 0x08 ? '!' : ' '); 538 printf(" %4s%c", 539 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 540 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 541 "????", 542 fail & 0x02 ? '!' : ' '); 543 printf(" %3s%c ", 544 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 545 "wep" : "no", 546 fail & 0x04 ? '!' : ' '); 547 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 548 printf("%s\n", fail & 0x10 ? "!" : ""); 549 } 550 #endif 551 return fail; 552 } 553 554 static __inline u_int8_t 555 maxrate(const struct ieee80211_node *ni) 556 { 557 const struct ieee80211_rateset *rs = &ni->ni_rates; 558 /* NB: assumes rate set is sorted (happens on frame receive) */ 559 return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL; 560 } 561 562 /* 563 * Compare the capabilities of two nodes and decide which is 564 * more desirable (return >0 if a is considered better). Note 565 * that we assume compatibility/usability has already been checked 566 * so we don't need to (e.g. validate whether privacy is supported). 567 * Used to select the best scan candidate for association in a BSS. 568 */ 569 static int 570 ieee80211_node_compare(struct ieee80211com *ic, 571 const struct ieee80211_node *a, 572 const struct ieee80211_node *b) 573 { 574 u_int8_t maxa, maxb; 575 u_int8_t rssia, rssib; 576 int weight; 577 578 /* privacy support preferred */ 579 if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) && 580 (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 581 return 1; 582 if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 && 583 (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)) 584 return -1; 585 586 /* compare count of previous failures */ 587 weight = b->ni_fails - a->ni_fails; 588 if (abs(weight) > 1) 589 return weight; 590 591 rssia = ic->ic_node_getrssi(a); 592 rssib = ic->ic_node_getrssi(b); 593 if (abs(rssib - rssia) < 5) { 594 /* best/max rate preferred if signal level close enough XXX */ 595 maxa = maxrate(a); 596 maxb = maxrate(b); 597 if (maxa != maxb) 598 return maxa - maxb; 599 /* XXX use freq for channel preference */ 600 /* for now just prefer 5Ghz band to all other bands */ 601 if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) && 602 !IEEE80211_IS_CHAN_5GHZ(b->ni_chan)) 603 return 1; 604 if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) && 605 IEEE80211_IS_CHAN_5GHZ(b->ni_chan)) 606 return -1; 607 } 608 /* all things being equal, use signal level */ 609 return rssia - rssib; 610 } 611 612 /* 613 * Mark an ongoing scan stopped. 614 */ 615 void 616 ieee80211_cancel_scan(struct ieee80211com *ic) 617 { 618 619 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n", 620 __func__, 621 (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive"); 622 623 ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN); 624 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN; 625 } 626 627 /* 628 * Complete a scan of potential channels. 629 */ 630 void 631 ieee80211_end_scan(struct ieee80211com *ic) 632 { 633 struct ieee80211_node_table *nt = &ic->ic_scan; 634 struct ieee80211_node *ni, *selbs; 635 636 ieee80211_cancel_scan(ic); 637 ieee80211_notify_scan_done(ic); 638 639 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 640 u_int8_t maxrssi[IEEE80211_CHAN_MAX]; /* XXX off stack? */ 641 int i, bestchan; 642 u_int8_t rssi; 643 644 /* 645 * The passive scan to look for existing AP's completed, 646 * select a channel to camp on. Identify the channels 647 * that already have one or more AP's and try to locate 648 * an unoccupied one. If that fails, pick a channel that 649 * looks to be quietest. 650 */ 651 memset(maxrssi, 0, sizeof(maxrssi)); 652 IEEE80211_NODE_LOCK(nt); 653 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 654 rssi = ic->ic_node_getrssi(ni); 655 i = ieee80211_chan2ieee(ic, ni->ni_chan); 656 if (rssi > maxrssi[i]) 657 maxrssi[i] = rssi; 658 } 659 IEEE80211_NODE_UNLOCK(nt); 660 /* XXX select channel more intelligently */ 661 bestchan = -1; 662 for (i = 0; i < IEEE80211_CHAN_MAX; i++) 663 if (isset(ic->ic_chan_active, i)) { 664 /* 665 * If the channel is unoccupied the max rssi 666 * should be zero; just take it. Otherwise 667 * track the channel with the lowest rssi and 668 * use that when all channels appear occupied. 669 */ 670 if (maxrssi[i] == 0) { 671 bestchan = i; 672 break; 673 } 674 if (bestchan == -1 || 675 maxrssi[i] < maxrssi[bestchan]) 676 bestchan = i; 677 } 678 if (bestchan != -1) { 679 ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]); 680 return; 681 } 682 /* no suitable channel, should not happen */ 683 } 684 685 /* 686 * When manually sequencing the state machine; scan just once 687 * regardless of whether we have a candidate or not. The 688 * controlling application is expected to setup state and 689 * initiate an association. 690 */ 691 if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL) 692 return; 693 /* 694 * Automatic sequencing; look for a candidate and 695 * if found join the network. 696 */ 697 /* NB: unlocked read should be ok */ 698 if (TAILQ_FIRST(&nt->nt_node) == NULL) { 699 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 700 "%s: no scan candidate\n", __func__); 701 notfound: 702 if (ic->ic_opmode == IEEE80211_M_IBSS && 703 (ic->ic_flags & IEEE80211_F_IBSSON) && 704 ic->ic_des_esslen != 0) { 705 ieee80211_create_ibss(ic, ic->ic_ibss_chan); 706 return; 707 } 708 /* 709 * Decrement the failure counts so entries will be 710 * reconsidered the next time around. We really want 711 * to do this only for sta's where we've previously 712 * had some success. 713 */ 714 IEEE80211_NODE_LOCK(nt); 715 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 716 if (ni->ni_fails) 717 ni->ni_fails--; 718 IEEE80211_NODE_UNLOCK(nt); 719 /* 720 * Reset the list of channels to scan and start again. 721 */ 722 ieee80211_reset_scan(ic); 723 ic->ic_flags |= IEEE80211_F_SCAN; 724 ieee80211_next_scan(ic); 725 return; 726 } 727 selbs = NULL; 728 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n", 729 "macaddr bssid chan rssi rate flag wep essid"); 730 IEEE80211_NODE_LOCK(nt); 731 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 732 if (ieee80211_match_bss(ic, ni) == 0) { 733 if (selbs == NULL) 734 selbs = ni; 735 else if (ieee80211_node_compare(ic, ni, selbs) > 0) 736 selbs = ni; 737 } 738 } 739 if (selbs != NULL) /* NB: grab ref while dropping lock */ 740 (void) ieee80211_ref_node(selbs); 741 IEEE80211_NODE_UNLOCK(nt); 742 if (selbs == NULL) 743 goto notfound; 744 if (!ieee80211_sta_join(ic, selbs)) { 745 ieee80211_free_node(selbs); 746 goto notfound; 747 } 748 } 749 750 /* 751 * Handle 802.11 ad hoc network merge. The 752 * convention, set by the Wireless Ethernet Compatibility Alliance 753 * (WECA), is that an 802.11 station will change its BSSID to match 754 * the "oldest" 802.11 ad hoc network, on the same channel, that 755 * has the station's desired SSID. The "oldest" 802.11 network 756 * sends beacons with the greatest TSF timestamp. 757 * 758 * The caller is assumed to validate TSF's before attempting a merge. 759 * 760 * Return !0 if the BSSID changed, 0 otherwise. 761 */ 762 int 763 ieee80211_ibss_merge(struct ieee80211_node *ni) 764 { 765 struct ieee80211com *ic = ni->ni_ic; 766 767 if (ni == ic->ic_bss || 768 IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) { 769 /* unchanged, nothing to do */ 770 return 0; 771 } 772 if (ieee80211_match_bss(ic, ni) != 0) { /* capabilities mismatch */ 773 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 774 "%s: merge failed, capabilities mismatch\n", __func__); 775 ic->ic_stats.is_ibss_capmismatch++; 776 return 0; 777 } 778 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 779 "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__, 780 ether_sprintf(ni->ni_bssid), 781 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 782 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long", 783 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "" 784 ); 785 return ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 786 } 787 788 /* 789 * Join the specified IBSS/BSS network. The node is assumed to 790 * be passed in with a held reference. 791 */ 792 int 793 ieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs) 794 { 795 struct ieee80211_node *obss; 796 797 if (ic->ic_opmode == IEEE80211_M_IBSS) { 798 struct ieee80211_node_table *nt; 799 /* 800 * Fillin the neighbor table; it will already 801 * exist if we are simply switching mastership. 802 * XXX ic_sta always setup so this is unnecessary? 803 */ 804 nt = &ic->ic_sta; 805 IEEE80211_NODE_LOCK(nt); 806 nt->nt_name = "neighbor"; 807 nt->nt_inact_init = ic->ic_inact_run; 808 IEEE80211_NODE_UNLOCK(nt); 809 } 810 811 /* 812 * Committed to selbs, setup state. 813 */ 814 obss = ic->ic_bss; 815 ic->ic_bss = selbs; /* NB: caller assumed to bump refcnt */ 816 if (obss != NULL) { 817 copy_bss(selbs, obss); 818 ieee80211_free_node(obss); 819 } 820 821 /* 822 * Delete unusable rates; we've already checked 823 * that the negotiated rate set is acceptable. 824 */ 825 ieee80211_fix_rate(ic->ic_bss, IEEE80211_F_DODEL | IEEE80211_F_JOIN); 826 827 /* 828 * Set the erp state (mostly the slot time) to deal with 829 * the auto-select case; this should be redundant if the 830 * mode is locked. 831 */ 832 ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan); 833 ic->ic_curchan = selbs->ni_chan; 834 ieee80211_reset_erp(ic); 835 ieee80211_wme_initparams(ic); 836 837 if (ic->ic_opmode == IEEE80211_M_STA) 838 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 839 else 840 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 841 return 1; 842 } 843 844 /* 845 * Leave the specified IBSS/BSS network. The node is assumed to 846 * be passed in with a held reference. 847 */ 848 void 849 ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 850 { 851 ic->ic_node_cleanup(ni); 852 ieee80211_notify_node_leave(ic, ni); 853 } 854 855 static struct ieee80211_node * 856 node_alloc(struct ieee80211_node_table *nt) 857 { 858 struct ieee80211_node *ni; 859 860 MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node), 861 M_80211_NODE, M_NOWAIT | M_ZERO); 862 return ni; 863 } 864 865 /* 866 * Reclaim any resources in a node and reset any critical 867 * state. Typically nodes are free'd immediately after, 868 * but in some cases the storage may be reused so we need 869 * to insure consistent state (should probably fix that). 870 */ 871 static void 872 node_cleanup(struct ieee80211_node *ni) 873 { 874 #define N(a) (sizeof(a)/sizeof(a[0])) 875 struct ieee80211com *ic = ni->ni_ic; 876 int i, qlen; 877 878 /* NB: preserve ni_table */ 879 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 880 ic->ic_ps_sta--; 881 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 882 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 883 "[%s] power save mode off, %u sta's in ps mode\n", 884 ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta); 885 } 886 /* 887 * Clear AREF flag that marks the authorization refcnt bump 888 * has happened. This is probably not needed as the node 889 * should always be removed from the table so not found but 890 * do it just in case. 891 */ 892 ni->ni_flags &= ~IEEE80211_NODE_AREF; 893 894 /* 895 * Drain power save queue and, if needed, clear TIM. 896 */ 897 IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen); 898 if (qlen != 0 && ic->ic_set_tim != NULL) 899 ic->ic_set_tim(ni, 0); 900 901 ni->ni_associd = 0; 902 if (ni->ni_challenge != NULL) { 903 FREE(ni->ni_challenge, M_DEVBUF); 904 ni->ni_challenge = NULL; 905 } 906 /* 907 * Preserve SSID, WPA, and WME ie's so the bss node is 908 * reusable during a re-auth/re-assoc state transition. 909 * If we remove these data they will not be recreated 910 * because they come from a probe-response or beacon frame 911 * which cannot be expected prior to the association-response. 912 * This should not be an issue when operating in other modes 913 * as stations leaving always go through a full state transition 914 * which will rebuild this state. 915 * 916 * XXX does this leave us open to inheriting old state? 917 */ 918 for (i = 0; i < N(ni->ni_rxfrag); i++) 919 if (ni->ni_rxfrag[i] != NULL) { 920 m_freem(ni->ni_rxfrag[i]); 921 ni->ni_rxfrag[i] = NULL; 922 } 923 /* 924 * Must be careful here to remove any key map entry w/o a LOR. 925 */ 926 ieee80211_node_delucastkey(ni); 927 #undef N 928 } 929 930 static void 931 node_free(struct ieee80211_node *ni) 932 { 933 struct ieee80211com *ic = ni->ni_ic; 934 935 ic->ic_node_cleanup(ni); 936 if (ni->ni_wpa_ie != NULL) 937 FREE(ni->ni_wpa_ie, M_DEVBUF); 938 if (ni->ni_wme_ie != NULL) 939 FREE(ni->ni_wme_ie, M_DEVBUF); 940 IEEE80211_NODE_SAVEQ_DESTROY(ni); 941 FREE(ni, M_80211_NODE); 942 } 943 944 static u_int8_t 945 node_getrssi(const struct ieee80211_node *ni) 946 { 947 return ni->ni_rssi; 948 } 949 950 static void 951 ieee80211_setup_node(struct ieee80211_node_table *nt, 952 struct ieee80211_node *ni, const u_int8_t *macaddr) 953 { 954 struct ieee80211com *ic = nt->nt_ic; 955 int hash; 956 957 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 958 "%s %p<%s> in %s table\n", __func__, ni, 959 ether_sprintf(macaddr), nt->nt_name); 960 961 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 962 hash = IEEE80211_NODE_HASH(macaddr); 963 ieee80211_node_initref(ni); /* mark referenced */ 964 ni->ni_chan = IEEE80211_CHAN_ANYC; 965 ni->ni_authmode = IEEE80211_AUTH_OPEN; 966 ni->ni_txpower = ic->ic_txpowlimit; /* max power */ 967 ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE); 968 ni->ni_inact_reload = nt->nt_inact_init; 969 ni->ni_inact = ni->ni_inact_reload; 970 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown"); 971 972 IEEE80211_NODE_LOCK(nt); 973 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list); 974 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash); 975 ni->ni_table = nt; 976 ni->ni_ic = ic; 977 IEEE80211_NODE_UNLOCK(nt); 978 } 979 980 struct ieee80211_node * 981 ieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 982 { 983 struct ieee80211com *ic = nt->nt_ic; 984 struct ieee80211_node *ni; 985 986 ni = ic->ic_node_alloc(nt); 987 if (ni != NULL) 988 ieee80211_setup_node(nt, ni, macaddr); 989 else 990 ic->ic_stats.is_rx_nodealloc++; 991 return ni; 992 } 993 994 /* 995 * Craft a temporary node suitable for sending a management frame 996 * to the specified station. We craft only as much state as we 997 * need to do the work since the node will be immediately reclaimed 998 * once the send completes. 999 */ 1000 struct ieee80211_node * 1001 ieee80211_tmp_node(struct ieee80211com *ic, const u_int8_t *macaddr) 1002 { 1003 struct ieee80211_node *ni; 1004 1005 ni = ic->ic_node_alloc(&ic->ic_sta); 1006 if (ni != NULL) { 1007 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1008 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr)); 1009 1010 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 1011 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 1012 ieee80211_node_initref(ni); /* mark referenced */ 1013 ni->ni_txpower = ic->ic_bss->ni_txpower; 1014 /* NB: required by ieee80211_fix_rate */ 1015 ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan); 1016 ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, 1017 IEEE80211_KEYIX_NONE); 1018 /* XXX optimize away */ 1019 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown"); 1020 1021 ni->ni_table = NULL; /* NB: pedantic */ 1022 ni->ni_ic = ic; 1023 } else { 1024 /* XXX msg */ 1025 ic->ic_stats.is_rx_nodealloc++; 1026 } 1027 return ni; 1028 } 1029 1030 struct ieee80211_node * 1031 ieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 1032 { 1033 struct ieee80211com *ic = nt->nt_ic; 1034 struct ieee80211_node *ni; 1035 1036 ni = ic->ic_node_alloc(nt); 1037 if (ni != NULL) { 1038 ieee80211_setup_node(nt, ni, macaddr); 1039 /* 1040 * Inherit from ic_bss. 1041 */ 1042 ni->ni_authmode = ic->ic_bss->ni_authmode; 1043 ni->ni_txpower = ic->ic_bss->ni_txpower; 1044 ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */ 1045 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 1046 ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan); 1047 ni->ni_rsn = ic->ic_bss->ni_rsn; 1048 } else 1049 ic->ic_stats.is_rx_nodealloc++; 1050 return ni; 1051 } 1052 1053 static struct ieee80211_node * 1054 #ifdef IEEE80211_DEBUG_REFCNT 1055 _ieee80211_find_node_debug(struct ieee80211_node_table *nt, 1056 const u_int8_t *macaddr, const char *func, int line) 1057 #else 1058 _ieee80211_find_node(struct ieee80211_node_table *nt, 1059 const u_int8_t *macaddr) 1060 #endif 1061 { 1062 struct ieee80211_node *ni; 1063 int hash; 1064 1065 IEEE80211_NODE_LOCK_ASSERT(nt); 1066 1067 hash = IEEE80211_NODE_HASH(macaddr); 1068 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1069 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 1070 ieee80211_ref_node(ni); /* mark referenced */ 1071 #ifdef IEEE80211_DEBUG_REFCNT 1072 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1073 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1074 func, line, 1075 ni, ether_sprintf(ni->ni_macaddr), 1076 ieee80211_node_refcnt(ni)); 1077 #endif 1078 return ni; 1079 } 1080 } 1081 return NULL; 1082 } 1083 #ifdef IEEE80211_DEBUG_REFCNT 1084 #define _ieee80211_find_node(nt, mac) \ 1085 _ieee80211_find_node_debug(nt, mac, func, line) 1086 #endif 1087 1088 struct ieee80211_node * 1089 #ifdef IEEE80211_DEBUG_REFCNT 1090 ieee80211_find_node_debug(struct ieee80211_node_table *nt, 1091 const u_int8_t *macaddr, const char *func, int line) 1092 #else 1093 ieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr) 1094 #endif 1095 { 1096 struct ieee80211_node *ni; 1097 1098 IEEE80211_NODE_LOCK(nt); 1099 ni = _ieee80211_find_node(nt, macaddr); 1100 IEEE80211_NODE_UNLOCK(nt); 1101 return ni; 1102 } 1103 1104 /* 1105 * Fake up a node; this handles node discovery in adhoc mode. 1106 * Note that for the driver's benefit we we treat this like 1107 * an association so the driver has an opportunity to setup 1108 * it's private state. 1109 */ 1110 struct ieee80211_node * 1111 ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt, 1112 const u_int8_t macaddr[IEEE80211_ADDR_LEN]) 1113 { 1114 struct ieee80211com *ic = nt->nt_ic; 1115 struct ieee80211_node *ni; 1116 1117 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1118 "%s: mac<%s>\n", __func__, ether_sprintf(macaddr)); 1119 ni = ieee80211_dup_bss(nt, macaddr); 1120 if (ni != NULL) { 1121 /* XXX no rate negotiation; just dup */ 1122 ni->ni_rates = ic->ic_bss->ni_rates; 1123 if (ic->ic_newassoc != NULL) 1124 ic->ic_newassoc(ni, 1); 1125 /* XXX not right for 802.1x/WPA */ 1126 ieee80211_node_authorize(ni); 1127 if (ic->ic_opmode == IEEE80211_M_AHDEMO) { 1128 /* 1129 * Blindly propagate capabilities based on the 1130 * local configuration. In particular this permits 1131 * us to use QoS to disable ACK's. 1132 */ 1133 if (ic->ic_flags & IEEE80211_F_WME) 1134 ni->ni_flags |= IEEE80211_NODE_QOS; 1135 } 1136 } 1137 return ni; 1138 } 1139 1140 #ifdef IEEE80211_DEBUG 1141 static void 1142 dump_probe_beacon(u_int8_t subtype, int isnew, 1143 const u_int8_t mac[IEEE80211_ADDR_LEN], 1144 const struct ieee80211_scanparams *sp) 1145 { 1146 1147 printf("[%s] %s%s on chan %u (bss chan %u) ", 1148 ether_sprintf(mac), isnew ? "new " : "", 1149 ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT], 1150 sp->chan, sp->bchan); 1151 ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]); 1152 printf("\n"); 1153 1154 if (isnew) { 1155 printf("[%s] caps 0x%x bintval %u erp 0x%x", 1156 ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp); 1157 if (sp->country != NULL) { 1158 #ifdef __FreeBSD__ 1159 printf(" country info %*D", 1160 sp->country[1], sp->country+2, " "); 1161 #else 1162 int i; 1163 printf(" country info"); 1164 for (i = 0; i < sp->country[1]; i++) 1165 printf(" %02x", sp->country[i+2]); 1166 #endif 1167 } 1168 printf("\n"); 1169 } 1170 } 1171 #endif /* IEEE80211_DEBUG */ 1172 1173 static void 1174 saveie(u_int8_t **iep, const u_int8_t *ie) 1175 { 1176 1177 if (ie == NULL) 1178 *iep = NULL; 1179 else 1180 ieee80211_saveie(iep, ie); 1181 } 1182 1183 /* 1184 * Process a beacon or probe response frame. 1185 */ 1186 void 1187 ieee80211_add_scan(struct ieee80211com *ic, 1188 const struct ieee80211_scanparams *sp, 1189 const struct ieee80211_frame *wh, 1190 int subtype, int rssi, int rstamp) 1191 { 1192 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1193 struct ieee80211_node_table *nt = &ic->ic_scan; 1194 struct ieee80211_node *ni; 1195 int newnode = 0; 1196 1197 ni = ieee80211_find_node(nt, wh->i_addr2); 1198 if (ni == NULL) { 1199 /* 1200 * Create a new entry. 1201 */ 1202 ni = ic->ic_node_alloc(nt); 1203 if (ni == NULL) { 1204 ic->ic_stats.is_rx_nodealloc++; 1205 return; 1206 } 1207 ieee80211_setup_node(nt, ni, wh->i_addr2); 1208 /* 1209 * XXX inherit from ic_bss. 1210 */ 1211 ni->ni_authmode = ic->ic_bss->ni_authmode; 1212 ni->ni_txpower = ic->ic_bss->ni_txpower; 1213 ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */ 1214 ieee80211_set_chan(ic, ni, ic->ic_curchan); 1215 ni->ni_rsn = ic->ic_bss->ni_rsn; 1216 newnode = 1; 1217 } 1218 #ifdef IEEE80211_DEBUG 1219 if (ieee80211_msg_scan(ic) && (ic->ic_flags & IEEE80211_F_SCAN)) 1220 dump_probe_beacon(subtype, newnode, wh->i_addr2, sp); 1221 #endif 1222 /* XXX ap beaconing multiple ssid w/ same bssid */ 1223 if (sp->ssid[1] != 0 && 1224 (ISPROBE(subtype) || ni->ni_esslen == 0)) { 1225 ni->ni_esslen = sp->ssid[1]; 1226 memset(ni->ni_essid, 0, sizeof(ni->ni_essid)); 1227 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]); 1228 } 1229 ni->ni_scangen = ic->ic_scan.nt_scangen; 1230 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3); 1231 ni->ni_rssi = rssi; 1232 ni->ni_rstamp = rstamp; 1233 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp)); 1234 ni->ni_intval = sp->bintval; 1235 ni->ni_capinfo = sp->capinfo; 1236 ni->ni_chan = &ic->ic_channels[sp->chan]; 1237 ni->ni_fhdwell = sp->fhdwell; 1238 ni->ni_fhindex = sp->fhindex; 1239 ni->ni_erp = sp->erp; 1240 if (sp->tim != NULL) { 1241 struct ieee80211_tim_ie *ie = 1242 (struct ieee80211_tim_ie *) sp->tim; 1243 1244 ni->ni_dtim_count = ie->tim_count; 1245 ni->ni_dtim_period = ie->tim_period; 1246 } 1247 /* 1248 * Record the byte offset from the mac header to 1249 * the start of the TIM information element for 1250 * use by hardware and/or to speedup software 1251 * processing of beacon frames. 1252 */ 1253 ni->ni_timoff = sp->timoff; 1254 /* 1255 * Record optional information elements that might be 1256 * used by applications or drivers. 1257 */ 1258 saveie(&ni->ni_wme_ie, sp->wme); 1259 saveie(&ni->ni_wpa_ie, sp->wpa); 1260 1261 /* NB: must be after ni_chan is setup */ 1262 ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT); 1263 1264 if (!newnode) 1265 ieee80211_free_node(ni); 1266 #undef ISPROBE 1267 } 1268 1269 void 1270 ieee80211_init_neighbor(struct ieee80211_node *ni, 1271 const struct ieee80211_frame *wh, 1272 const struct ieee80211_scanparams *sp) 1273 { 1274 1275 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1276 "%s: %p<%s>\n", __func__, ni, ether_sprintf(ni->ni_macaddr)); 1277 ni->ni_esslen = sp->ssid[1]; 1278 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]); 1279 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3); 1280 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp)); 1281 ni->ni_intval = sp->bintval; 1282 ni->ni_capinfo = sp->capinfo; 1283 ni->ni_chan = ni->ni_ic->ic_curchan; 1284 ni->ni_fhdwell = sp->fhdwell; 1285 ni->ni_fhindex = sp->fhindex; 1286 ni->ni_erp = sp->erp; 1287 ni->ni_timoff = sp->timoff; 1288 if (sp->wme != NULL) 1289 ieee80211_saveie(&ni->ni_wme_ie, sp->wme); 1290 if (sp->wpa != NULL) 1291 ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa); 1292 1293 /* NB: must be after ni_chan is setup */ 1294 ieee80211_setup_rates(ni, sp->rates, sp->xrates, 1295 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | 1296 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1297 } 1298 1299 /* 1300 * Do node discovery in adhoc mode on receipt of a beacon 1301 * or probe response frame. Note that for the driver's 1302 * benefit we we treat this like an association so the 1303 * driver has an opportunity to setup it's private state. 1304 */ 1305 struct ieee80211_node * 1306 ieee80211_add_neighbor(struct ieee80211com *ic, 1307 const struct ieee80211_frame *wh, 1308 const struct ieee80211_scanparams *sp) 1309 { 1310 struct ieee80211_node *ni; 1311 1312 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1313 "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2)); 1314 ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */ 1315 if (ni != NULL) { 1316 ieee80211_init_neighbor(ni, wh, sp); 1317 if (ic->ic_newassoc != NULL) 1318 ic->ic_newassoc(ni, 1); 1319 /* XXX not right for 802.1x/WPA */ 1320 ieee80211_node_authorize(ni); 1321 } 1322 return ni; 1323 } 1324 1325 #define IS_CTL(wh) \ 1326 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) 1327 #define IS_PSPOLL(wh) \ 1328 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL) 1329 /* 1330 * Locate the node for sender, track state, and then pass the 1331 * (referenced) node up to the 802.11 layer for its use. We 1332 * are required to pass some node so we fall back to ic_bss 1333 * when this frame is from an unknown sender. The 802.11 layer 1334 * knows this means the sender wasn't in the node table and 1335 * acts accordingly. 1336 */ 1337 struct ieee80211_node * 1338 #ifdef IEEE80211_DEBUG_REFCNT 1339 ieee80211_find_rxnode_debug(struct ieee80211com *ic, 1340 const struct ieee80211_frame_min *wh, const char *func, int line) 1341 #else 1342 ieee80211_find_rxnode(struct ieee80211com *ic, 1343 const struct ieee80211_frame_min *wh) 1344 #endif 1345 { 1346 struct ieee80211_node_table *nt; 1347 struct ieee80211_node *ni; 1348 1349 /* XXX may want scanned nodes in the neighbor table for adhoc */ 1350 if (ic->ic_opmode == IEEE80211_M_STA || 1351 ic->ic_opmode == IEEE80211_M_MONITOR || 1352 (ic->ic_flags & IEEE80211_F_SCAN)) 1353 nt = &ic->ic_scan; 1354 else 1355 nt = &ic->ic_sta; 1356 /* XXX check ic_bss first in station mode */ 1357 /* XXX 4-address frames? */ 1358 IEEE80211_NODE_LOCK(nt); 1359 if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/) 1360 ni = _ieee80211_find_node(nt, wh->i_addr1); 1361 else 1362 ni = _ieee80211_find_node(nt, wh->i_addr2); 1363 if (ni == NULL) 1364 ni = ieee80211_ref_node(ic->ic_bss); 1365 IEEE80211_NODE_UNLOCK(nt); 1366 1367 return ni; 1368 } 1369 1370 /* 1371 * Like ieee80211_find_rxnode but use the supplied h/w 1372 * key index as a hint to locate the node in the key 1373 * mapping table. If an entry is present at the key 1374 * index we return it; otherwise do a normal lookup and 1375 * update the mapping table if the station has a unicast 1376 * key assigned to it. 1377 */ 1378 struct ieee80211_node * 1379 #ifdef IEEE80211_DEBUG_REFCNT 1380 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic, 1381 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix, 1382 const char *func, int line) 1383 #else 1384 ieee80211_find_rxnode_withkey(struct ieee80211com *ic, 1385 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix) 1386 #endif 1387 { 1388 struct ieee80211_node_table *nt; 1389 struct ieee80211_node *ni; 1390 1391 if (ic->ic_opmode == IEEE80211_M_STA || 1392 ic->ic_opmode == IEEE80211_M_MONITOR || 1393 (ic->ic_flags & IEEE80211_F_SCAN)) 1394 nt = &ic->ic_scan; 1395 else 1396 nt = &ic->ic_sta; 1397 IEEE80211_NODE_LOCK(nt); 1398 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) 1399 ni = nt->nt_keyixmap[keyix]; 1400 else 1401 ni = NULL; 1402 if (ni == NULL) { 1403 if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/) 1404 ni = _ieee80211_find_node(nt, wh->i_addr1); 1405 else 1406 ni = _ieee80211_find_node(nt, wh->i_addr2); 1407 if (ni == NULL) 1408 ni = ieee80211_ref_node(ic->ic_bss); 1409 if (nt->nt_keyixmap != NULL) { 1410 /* 1411 * If the station has a unicast key cache slot 1412 * assigned update the key->node mapping table. 1413 */ 1414 keyix = ni->ni_ucastkey.wk_rxkeyix; 1415 /* XXX can keyixmap[keyix] != NULL? */ 1416 if (keyix < nt->nt_keyixmax && 1417 nt->nt_keyixmap[keyix] == NULL) { 1418 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1419 "%s: add key map entry %p<%s> refcnt %d\n", 1420 __func__, ni, ether_sprintf(ni->ni_macaddr), 1421 ieee80211_node_refcnt(ni)+1); 1422 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni); 1423 } 1424 } 1425 } else { 1426 ieee80211_ref_node(ni); 1427 } 1428 IEEE80211_NODE_UNLOCK(nt); 1429 1430 return ni; 1431 } 1432 #undef IS_PSPOLL 1433 #undef IS_CTL 1434 1435 /* 1436 * Return a reference to the appropriate node for sending 1437 * a data frame. This handles node discovery in adhoc networks. 1438 */ 1439 struct ieee80211_node * 1440 #ifdef IEEE80211_DEBUG_REFCNT 1441 ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr, 1442 const char *func, int line) 1443 #else 1444 ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr) 1445 #endif 1446 { 1447 struct ieee80211_node_table *nt = &ic->ic_sta; 1448 struct ieee80211_node *ni; 1449 1450 /* 1451 * The destination address should be in the node table 1452 * unless this is a multicast/broadcast frame. We can 1453 * also optimize station mode operation, all frames go 1454 * to the bss node. 1455 */ 1456 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */ 1457 IEEE80211_NODE_LOCK(nt); 1458 if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr)) 1459 ni = ieee80211_ref_node(ic->ic_bss); 1460 else { 1461 ni = _ieee80211_find_node(nt, macaddr); 1462 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1463 (ni != NULL && ni->ni_associd == 0)) { 1464 /* 1465 * Station is not associated; don't permit the 1466 * data frame to be sent by returning NULL. This 1467 * is kinda a kludge but the least intrusive way 1468 * to add this check into all drivers. 1469 */ 1470 ieee80211_unref_node(&ni); /* NB: null's ni */ 1471 } 1472 } 1473 IEEE80211_NODE_UNLOCK(nt); 1474 1475 if (ni == NULL) { 1476 if (ic->ic_opmode == IEEE80211_M_IBSS || 1477 ic->ic_opmode == IEEE80211_M_AHDEMO) { 1478 /* 1479 * In adhoc mode cons up a node for the destination. 1480 * Note that we need an additional reference for the 1481 * caller to be consistent with _ieee80211_find_node. 1482 */ 1483 ni = ieee80211_fakeup_adhoc_node(nt, macaddr); 1484 if (ni != NULL) 1485 (void) ieee80211_ref_node(ni); 1486 } else { 1487 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 1488 "[%s] no node, discard frame (%s)\n", 1489 ether_sprintf(macaddr), __func__); 1490 ic->ic_stats.is_tx_nonode++; 1491 } 1492 } 1493 return ni; 1494 } 1495 1496 /* 1497 * Like find but search based on the channel too. 1498 */ 1499 struct ieee80211_node * 1500 #ifdef IEEE80211_DEBUG_REFCNT 1501 ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt, 1502 const u_int8_t *macaddr, struct ieee80211_channel *chan, 1503 const char *func, int line) 1504 #else 1505 ieee80211_find_node_with_channel(struct ieee80211_node_table *nt, 1506 const u_int8_t *macaddr, struct ieee80211_channel *chan) 1507 #endif 1508 { 1509 struct ieee80211_node *ni; 1510 int hash; 1511 1512 hash = IEEE80211_NODE_HASH(macaddr); 1513 IEEE80211_NODE_LOCK(nt); 1514 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1515 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1516 ni->ni_chan == chan) { 1517 ieee80211_ref_node(ni); /* mark referenced */ 1518 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 1519 REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr), 1520 ieee80211_node_refcnt(ni)); 1521 break; 1522 } 1523 } 1524 IEEE80211_NODE_UNLOCK(nt); 1525 return ni; 1526 } 1527 1528 /* 1529 * Like find but search based on the ssid too. 1530 */ 1531 struct ieee80211_node * 1532 #ifdef IEEE80211_DEBUG_REFCNT 1533 ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt, 1534 const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid, 1535 const char *func, int line) 1536 #else 1537 ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt, 1538 const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid) 1539 #endif 1540 { 1541 #define MATCH_SSID(ni, ssid, ssidlen) \ 1542 (ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0) 1543 static const u_int8_t zeromac[IEEE80211_ADDR_LEN]; 1544 struct ieee80211com *ic = nt->nt_ic; 1545 struct ieee80211_node *ni; 1546 int hash; 1547 1548 IEEE80211_NODE_LOCK(nt); 1549 /* 1550 * A mac address that is all zero means match only the ssid; 1551 * otherwise we must match both. 1552 */ 1553 if (IEEE80211_ADDR_EQ(macaddr, zeromac)) { 1554 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1555 if (MATCH_SSID(ni, ssid, ssidlen)) 1556 break; 1557 } 1558 } else { 1559 hash = IEEE80211_NODE_HASH(macaddr); 1560 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1561 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1562 MATCH_SSID(ni, ssid, ssidlen)) 1563 break; 1564 } 1565 } 1566 if (ni != NULL) { 1567 ieee80211_ref_node(ni); /* mark referenced */ 1568 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1569 REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr), 1570 ieee80211_node_refcnt(ni)); 1571 } 1572 IEEE80211_NODE_UNLOCK(nt); 1573 return ni; 1574 #undef MATCH_SSID 1575 } 1576 1577 static void 1578 _ieee80211_free_node(struct ieee80211_node *ni) 1579 { 1580 struct ieee80211com *ic = ni->ni_ic; 1581 struct ieee80211_node_table *nt = ni->ni_table; 1582 1583 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1584 "%s %p<%s> in %s table\n", __func__, ni, 1585 ether_sprintf(ni->ni_macaddr), 1586 nt != NULL ? nt->nt_name : "<gone>"); 1587 1588 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1589 if (nt != NULL) { 1590 TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1591 LIST_REMOVE(ni, ni_hash); 1592 } 1593 ic->ic_node_free(ni); 1594 } 1595 1596 void 1597 #ifdef IEEE80211_DEBUG_REFCNT 1598 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line) 1599 #else 1600 ieee80211_free_node(struct ieee80211_node *ni) 1601 #endif 1602 { 1603 struct ieee80211_node_table *nt = ni->ni_table; 1604 1605 #ifdef IEEE80211_DEBUG_REFCNT 1606 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1607 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 1608 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1609 #endif 1610 if (nt != NULL) { 1611 IEEE80211_NODE_LOCK(nt); 1612 if (ieee80211_node_dectestref(ni)) { 1613 /* 1614 * Last reference, reclaim state. 1615 */ 1616 _ieee80211_free_node(ni); 1617 } else if (ieee80211_node_refcnt(ni) == 1 && 1618 nt->nt_keyixmap != NULL) { 1619 ieee80211_keyix keyix; 1620 /* 1621 * Check for a last reference in the key mapping table. 1622 */ 1623 keyix = ni->ni_ucastkey.wk_rxkeyix; 1624 if (keyix < nt->nt_keyixmax && 1625 nt->nt_keyixmap[keyix] == ni) { 1626 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1627 "%s: %p<%s> clear key map entry", __func__, 1628 ni, ether_sprintf(ni->ni_macaddr)); 1629 nt->nt_keyixmap[keyix] = NULL; 1630 ieee80211_node_decref(ni); /* XXX needed? */ 1631 _ieee80211_free_node(ni); 1632 } 1633 } 1634 IEEE80211_NODE_UNLOCK(nt); 1635 } else { 1636 if (ieee80211_node_dectestref(ni)) 1637 _ieee80211_free_node(ni); 1638 } 1639 } 1640 1641 /* 1642 * Reclaim a unicast key and clear any key cache state. 1643 */ 1644 int 1645 ieee80211_node_delucastkey(struct ieee80211_node *ni) 1646 { 1647 struct ieee80211com *ic = ni->ni_ic; 1648 struct ieee80211_node_table *nt = &ic->ic_sta; 1649 struct ieee80211_node *nikey; 1650 ieee80211_keyix keyix; 1651 int isowned, status; 1652 1653 /* 1654 * NB: We must beware of LOR here; deleting the key 1655 * can cause the crypto layer to block traffic updates 1656 * which can generate a LOR against the node table lock; 1657 * grab it here and stash the key index for our use below. 1658 * 1659 * Must also beware of recursion on the node table lock. 1660 * When called from node_cleanup we may already have 1661 * the node table lock held. Unfortunately there's no 1662 * way to separate out this path so we must do this 1663 * conditionally. 1664 */ 1665 isowned = IEEE80211_NODE_IS_LOCKED(nt); 1666 if (!isowned) 1667 IEEE80211_NODE_LOCK(nt); 1668 keyix = ni->ni_ucastkey.wk_rxkeyix; 1669 status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey); 1670 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) { 1671 nikey = nt->nt_keyixmap[keyix]; 1672 nt->nt_keyixmap[keyix] = NULL;; 1673 } else 1674 nikey = NULL; 1675 if (!isowned) 1676 IEEE80211_NODE_UNLOCK(&ic->ic_sta); 1677 1678 if (nikey != NULL) { 1679 KASSERT(nikey == ni, 1680 ("key map out of sync, ni %p nikey %p", ni, nikey)); 1681 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1682 "%s: delete key map entry %p<%s> refcnt %d\n", 1683 __func__, ni, ether_sprintf(ni->ni_macaddr), 1684 ieee80211_node_refcnt(ni)-1); 1685 ieee80211_free_node(ni); 1686 } 1687 return status; 1688 } 1689 1690 /* 1691 * Reclaim a node. If this is the last reference count then 1692 * do the normal free work. Otherwise remove it from the node 1693 * table and mark it gone by clearing the back-reference. 1694 */ 1695 static void 1696 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1697 { 1698 ieee80211_keyix keyix; 1699 1700 IEEE80211_NODE_LOCK_ASSERT(nt); 1701 1702 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1703 "%s: remove %p<%s> from %s table, refcnt %d\n", 1704 __func__, ni, ether_sprintf(ni->ni_macaddr), 1705 nt->nt_name, ieee80211_node_refcnt(ni)-1); 1706 /* 1707 * Clear any entry in the unicast key mapping table. 1708 * We need to do it here so rx lookups don't find it 1709 * in the mapping table even if it's not in the hash 1710 * table. We cannot depend on the mapping table entry 1711 * being cleared because the node may not be free'd. 1712 */ 1713 keyix = ni->ni_ucastkey.wk_rxkeyix; 1714 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax && 1715 nt->nt_keyixmap[keyix] == ni) { 1716 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1717 "%s: %p<%s> clear key map entry\n", 1718 __func__, ni, ether_sprintf(ni->ni_macaddr)); 1719 nt->nt_keyixmap[keyix] = NULL; 1720 ieee80211_node_decref(ni); /* NB: don't need free */ 1721 } 1722 if (!ieee80211_node_dectestref(ni)) { 1723 /* 1724 * Other references are present, just remove the 1725 * node from the table so it cannot be found. When 1726 * the references are dropped storage will be 1727 * reclaimed. 1728 */ 1729 TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1730 LIST_REMOVE(ni, ni_hash); 1731 ni->ni_table = NULL; /* clear reference */ 1732 } else 1733 _ieee80211_free_node(ni); 1734 } 1735 1736 static void 1737 ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt) 1738 { 1739 struct ieee80211com *ic = nt->nt_ic; 1740 struct ieee80211_node *ni; 1741 1742 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1743 "%s: free all nodes in %s table\n", __func__, nt->nt_name); 1744 1745 while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) { 1746 if (ni->ni_associd != 0) { 1747 if (ic->ic_auth->ia_node_leave != NULL) 1748 ic->ic_auth->ia_node_leave(ic, ni); 1749 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1750 } 1751 node_reclaim(nt, ni); 1752 } 1753 ieee80211_reset_erp(ic); 1754 } 1755 1756 static void 1757 ieee80211_free_allnodes(struct ieee80211_node_table *nt) 1758 { 1759 1760 IEEE80211_NODE_LOCK(nt); 1761 ieee80211_free_allnodes_locked(nt); 1762 IEEE80211_NODE_UNLOCK(nt); 1763 } 1764 1765 /* 1766 * Timeout entries in the scan cache. 1767 */ 1768 static void 1769 ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt) 1770 { 1771 struct ieee80211com *ic = nt->nt_ic; 1772 struct ieee80211_node *ni, *tni; 1773 1774 IEEE80211_NODE_LOCK(nt); 1775 ni = ic->ic_bss; 1776 /* XXX belongs elsewhere */ 1777 if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) { 1778 m_freem(ni->ni_rxfrag[0]); 1779 ni->ni_rxfrag[0] = NULL; 1780 } 1781 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) { 1782 if (ni->ni_inact && --ni->ni_inact == 0) { 1783 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1784 "[%s] scan candidate purged from cache " 1785 "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr), 1786 ieee80211_node_refcnt(ni)); 1787 node_reclaim(nt, ni); 1788 } 1789 } 1790 IEEE80211_NODE_UNLOCK(nt); 1791 1792 nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1793 } 1794 1795 /* 1796 * Timeout inactive stations and do related housekeeping. 1797 * Note that we cannot hold the node lock while sending a 1798 * frame as this would lead to a LOR. Instead we use a 1799 * generation number to mark nodes that we've scanned and 1800 * drop the lock and restart a scan if we have to time out 1801 * a node. Since we are single-threaded by virtue of 1802 * controlling the inactivity timer we can be sure this will 1803 * process each node only once. 1804 */ 1805 static void 1806 ieee80211_timeout_stations(struct ieee80211_node_table *nt) 1807 { 1808 struct ieee80211com *ic = nt->nt_ic; 1809 struct ieee80211_node *ni; 1810 u_int gen; 1811 int isadhoc; 1812 1813 isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS || 1814 ic->ic_opmode == IEEE80211_M_AHDEMO); 1815 IEEE80211_SCAN_LOCK(nt); 1816 gen = ++nt->nt_scangen; 1817 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1818 "%s: %s scangen %u\n", __func__, nt->nt_name, gen); 1819 restart: 1820 IEEE80211_NODE_LOCK(nt); 1821 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1822 if (ni->ni_scangen == gen) /* previously handled */ 1823 continue; 1824 ni->ni_scangen = gen; 1825 /* 1826 * Ignore entries for which have yet to receive an 1827 * authentication frame. These are transient and 1828 * will be reclaimed when the last reference to them 1829 * goes away (when frame xmits complete). 1830 */ 1831 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1832 (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 1833 continue; 1834 /* 1835 * Free fragment if not needed anymore 1836 * (last fragment older than 1s). 1837 * XXX doesn't belong here 1838 */ 1839 if (ni->ni_rxfrag[0] != NULL && 1840 ticks > ni->ni_rxfragstamp + hz) { 1841 m_freem(ni->ni_rxfrag[0]); 1842 ni->ni_rxfrag[0] = NULL; 1843 } 1844 /* 1845 * Special case ourself; we may be idle for extended periods 1846 * of time and regardless reclaiming our state is wrong. 1847 */ 1848 if (ni == ic->ic_bss) 1849 continue; 1850 ni->ni_inact--; 1851 if (ni->ni_associd != 0 || isadhoc) { 1852 /* 1853 * Age frames on the power save queue. The 1854 * aging interval is 4 times the listen 1855 * interval specified by the station. This 1856 * number is factored into the age calculations 1857 * when the frame is placed on the queue. We 1858 * store ages as time differences we can check 1859 * and/or adjust only the head of the list. 1860 */ 1861 if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) { 1862 struct mbuf *m; 1863 int discard = 0; 1864 1865 IEEE80211_NODE_SAVEQ_LOCK(ni); 1866 while (IF_POLL(&ni->ni_savedq, m) != NULL && 1867 M_AGE_GET(m) < IEEE80211_INACT_WAIT) { 1868 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/ 1869 _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m); 1870 m_freem(m); 1871 discard++; 1872 } 1873 if (m != NULL) 1874 M_AGE_SUB(m, IEEE80211_INACT_WAIT); 1875 IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1876 1877 if (discard != 0) { 1878 IEEE80211_DPRINTF(ic, 1879 IEEE80211_MSG_POWER, 1880 "[%s] discard %u frames for age\n", 1881 ether_sprintf(ni->ni_macaddr), 1882 discard); 1883 IEEE80211_NODE_STAT_ADD(ni, 1884 ps_discard, discard); 1885 if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) 1886 ic->ic_set_tim(ni, 0); 1887 } 1888 } 1889 /* 1890 * Probe the station before time it out. We 1891 * send a null data frame which may not be 1892 * universally supported by drivers (need it 1893 * for ps-poll support so it should be...). 1894 */ 1895 if (0 < ni->ni_inact && 1896 ni->ni_inact <= ic->ic_inact_probe) { 1897 IEEE80211_NOTE(ic, 1898 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, 1899 ni, "%s", 1900 "probe station due to inactivity"); 1901 /* 1902 * Grab a reference before unlocking the table 1903 * so the node cannot be reclaimed before we 1904 * send the frame. ieee80211_send_nulldata 1905 * understands we've done this and reclaims the 1906 * ref for us as needed. 1907 */ 1908 ieee80211_ref_node(ni); 1909 IEEE80211_NODE_UNLOCK(nt); 1910 ieee80211_send_nulldata(ni); 1911 /* XXX stat? */ 1912 goto restart; 1913 } 1914 } 1915 if (ni->ni_inact <= 0) { 1916 IEEE80211_NOTE(ic, 1917 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni, 1918 "station timed out due to inactivity " 1919 "(refcnt %u)", ieee80211_node_refcnt(ni)); 1920 /* 1921 * Send a deauthenticate frame and drop the station. 1922 * This is somewhat complicated due to reference counts 1923 * and locking. At this point a station will typically 1924 * have a reference count of 1. ieee80211_node_leave 1925 * will do a "free" of the node which will drop the 1926 * reference count. But in the meantime a reference 1927 * wil be held by the deauth frame. The actual reclaim 1928 * of the node will happen either after the tx is 1929 * completed or by ieee80211_node_leave. 1930 * 1931 * Separately we must drop the node lock before sending 1932 * in case the driver takes a lock, as this will result 1933 * in LOR between the node lock and the driver lock. 1934 */ 1935 IEEE80211_NODE_UNLOCK(nt); 1936 if (ni->ni_associd != 0) { 1937 IEEE80211_SEND_MGMT(ic, ni, 1938 IEEE80211_FC0_SUBTYPE_DEAUTH, 1939 IEEE80211_REASON_AUTH_EXPIRE); 1940 } 1941 ieee80211_node_leave(ic, ni); 1942 ic->ic_stats.is_node_timeout++; 1943 goto restart; 1944 } 1945 } 1946 IEEE80211_NODE_UNLOCK(nt); 1947 1948 IEEE80211_SCAN_UNLOCK(nt); 1949 1950 nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1951 } 1952 1953 void 1954 ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg) 1955 { 1956 struct ieee80211_node *ni; 1957 u_int gen; 1958 1959 IEEE80211_SCAN_LOCK(nt); 1960 gen = ++nt->nt_scangen; 1961 restart: 1962 IEEE80211_NODE_LOCK(nt); 1963 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1964 if (ni->ni_scangen != gen) { 1965 ni->ni_scangen = gen; 1966 (void) ieee80211_ref_node(ni); 1967 IEEE80211_NODE_UNLOCK(nt); 1968 (*f)(arg, ni); 1969 ieee80211_free_node(ni); 1970 goto restart; 1971 } 1972 } 1973 IEEE80211_NODE_UNLOCK(nt); 1974 1975 IEEE80211_SCAN_UNLOCK(nt); 1976 } 1977 1978 void 1979 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1980 { 1981 printf("0x%p: mac %s refcnt %d\n", ni, 1982 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 1983 printf("\tscangen %u authmode %u flags 0x%x\n", 1984 ni->ni_scangen, ni->ni_authmode, ni->ni_flags); 1985 printf("\tassocid 0x%x txpower %u vlan %u\n", 1986 ni->ni_associd, ni->ni_txpower, ni->ni_vlan); 1987 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n", 1988 ni->ni_txseqs[0], 1989 ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT, 1990 ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK, 1991 ni->ni_rxfragstamp); 1992 printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n", 1993 ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo); 1994 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n", 1995 ether_sprintf(ni->ni_bssid), 1996 ni->ni_esslen, ni->ni_essid, 1997 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags); 1998 printf("\tfails %u inact %u txrate %u\n", 1999 ni->ni_fails, ni->ni_inact, ni->ni_txrate); 2000 } 2001 2002 void 2003 ieee80211_dump_nodes(struct ieee80211_node_table *nt) 2004 { 2005 ieee80211_iterate_nodes(nt, 2006 (ieee80211_iter_func *) ieee80211_dump_node, nt); 2007 } 2008 2009 /* 2010 * Handle a station joining an 11g network. 2011 */ 2012 static void 2013 ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 2014 { 2015 2016 /* 2017 * Station isn't capable of short slot time. Bump 2018 * the count of long slot time stations and disable 2019 * use of short slot time. Note that the actual switch 2020 * over to long slot time use may not occur until the 2021 * next beacon transmission (per sec. 7.3.1.4 of 11g). 2022 */ 2023 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2024 ic->ic_longslotsta++; 2025 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2026 "[%s] station needs long slot time, count %d\n", 2027 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 2028 /* XXX vap's w/ conflicting needs won't work */ 2029 ieee80211_set_shortslottime(ic, 0); 2030 } 2031 /* 2032 * If the new station is not an ERP station 2033 * then bump the counter and enable protection 2034 * if configured. 2035 */ 2036 if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) { 2037 ic->ic_nonerpsta++; 2038 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2039 "[%s] station is !ERP, %d non-ERP stations associated\n", 2040 ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 2041 /* 2042 * If protection is configured, enable it. 2043 */ 2044 if (ic->ic_protmode != IEEE80211_PROT_NONE) { 2045 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2046 "%s: enable use of protection\n", __func__); 2047 ic->ic_flags |= IEEE80211_F_USEPROT; 2048 } 2049 /* 2050 * If station does not support short preamble 2051 * then we must enable use of Barker preamble. 2052 */ 2053 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) { 2054 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2055 "[%s] station needs long preamble\n", 2056 ether_sprintf(ni->ni_macaddr)); 2057 ic->ic_flags |= IEEE80211_F_USEBARKER; 2058 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 2059 } 2060 if (ic->ic_nonerpsta == 1) 2061 ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE; 2062 } else 2063 ni->ni_flags |= IEEE80211_NODE_ERP; 2064 } 2065 2066 void 2067 ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp) 2068 { 2069 int newassoc; 2070 2071 if (ni->ni_associd == 0) { 2072 u_int16_t aid; 2073 2074 /* 2075 * It would be good to search the bitmap 2076 * more efficiently, but this will do for now. 2077 */ 2078 for (aid = 1; aid < ic->ic_max_aid; aid++) { 2079 if (!IEEE80211_AID_ISSET(aid, 2080 ic->ic_aid_bitmap)) 2081 break; 2082 } 2083 if (aid >= ic->ic_max_aid) { 2084 IEEE80211_SEND_MGMT(ic, ni, resp, 2085 IEEE80211_REASON_ASSOC_TOOMANY); 2086 ieee80211_node_leave(ic, ni); 2087 return; 2088 } 2089 ni->ni_associd = aid | 0xc000; 2090 IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap); 2091 ic->ic_sta_assoc++; 2092 newassoc = 1; 2093 if (ic->ic_curmode == IEEE80211_MODE_11G && 2094 IEEE80211_IS_CHAN_FULL(ni->ni_chan)) 2095 ieee80211_node_join_11g(ic, ni); 2096 } else 2097 newassoc = 0; 2098 2099 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 2100 "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n", 2101 ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re", 2102 IEEE80211_NODE_AID(ni), 2103 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long", 2104 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", 2105 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "", 2106 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "" 2107 ); 2108 2109 /* give driver a chance to setup state like ni_txrate */ 2110 if (ic->ic_newassoc != NULL) 2111 ic->ic_newassoc(ni, newassoc); 2112 ni->ni_inact_reload = ic->ic_inact_auth; 2113 ni->ni_inact = ni->ni_inact_reload; 2114 IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS); 2115 /* tell the authenticator about new station */ 2116 if (ic->ic_auth->ia_node_join != NULL) 2117 ic->ic_auth->ia_node_join(ic, ni); 2118 ieee80211_notify_node_join(ic, ni, newassoc); 2119 } 2120 2121 /* 2122 * Handle a station leaving an 11g network. 2123 */ 2124 static void 2125 ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 2126 { 2127 2128 KASSERT(ic->ic_curmode == IEEE80211_MODE_11G, 2129 ("not in 11g, bss %u:0x%x, curmode %u", ni->ni_chan->ic_freq, 2130 ni->ni_chan->ic_flags, ic->ic_curmode)); 2131 2132 /* 2133 * If a long slot station do the slot time bookkeeping. 2134 */ 2135 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2136 KASSERT(ic->ic_longslotsta > 0, 2137 ("bogus long slot station count %d", ic->ic_longslotsta)); 2138 ic->ic_longslotsta--; 2139 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2140 "[%s] long slot time station leaves, count now %d\n", 2141 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 2142 if (ic->ic_longslotsta == 0) { 2143 /* 2144 * Re-enable use of short slot time if supported 2145 * and not operating in IBSS mode (per spec). 2146 */ 2147 if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 2148 ic->ic_opmode != IEEE80211_M_IBSS) { 2149 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2150 "%s: re-enable use of short slot time\n", 2151 __func__); 2152 ieee80211_set_shortslottime(ic, 1); 2153 } 2154 } 2155 } 2156 /* 2157 * If a non-ERP station do the protection-related bookkeeping. 2158 */ 2159 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) { 2160 KASSERT(ic->ic_nonerpsta > 0, 2161 ("bogus non-ERP station count %d", ic->ic_nonerpsta)); 2162 ic->ic_nonerpsta--; 2163 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2164 "[%s] non-ERP station leaves, count now %d\n", 2165 ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 2166 if (ic->ic_nonerpsta == 0) { 2167 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2168 "%s: disable use of protection\n", __func__); 2169 ic->ic_flags &= ~IEEE80211_F_USEPROT; 2170 /* XXX verify mode? */ 2171 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) { 2172 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2173 "%s: re-enable use of short preamble\n", 2174 __func__); 2175 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 2176 ic->ic_flags &= ~IEEE80211_F_USEBARKER; 2177 } 2178 ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE; 2179 } 2180 } 2181 } 2182 2183 /* 2184 * Handle bookkeeping for station deauthentication/disassociation 2185 * when operating as an ap. 2186 */ 2187 void 2188 ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 2189 { 2190 struct ieee80211_node_table *nt = ni->ni_table; 2191 2192 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 2193 "[%s] station with aid %d leaves\n", 2194 ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni)); 2195 2196 KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 2197 ic->ic_opmode == IEEE80211_M_IBSS || 2198 ic->ic_opmode == IEEE80211_M_AHDEMO, 2199 ("unexpected operating mode %u", ic->ic_opmode)); 2200 /* 2201 * If node wasn't previously associated all 2202 * we need to do is reclaim the reference. 2203 */ 2204 /* XXX ibss mode bypasses 11g and notification */ 2205 if (ni->ni_associd == 0) 2206 goto done; 2207 /* 2208 * Tell the authenticator the station is leaving. 2209 * Note that we must do this before yanking the 2210 * association id as the authenticator uses the 2211 * associd to locate it's state block. 2212 */ 2213 if (ic->ic_auth->ia_node_leave != NULL) 2214 ic->ic_auth->ia_node_leave(ic, ni); 2215 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 2216 ni->ni_associd = 0; 2217 ic->ic_sta_assoc--; 2218 2219 if (ic->ic_curmode == IEEE80211_MODE_11G && 2220 IEEE80211_IS_CHAN_FULL(ni->ni_chan)) 2221 ieee80211_node_leave_11g(ic, ni); 2222 /* 2223 * Cleanup station state. In particular clear various 2224 * state that might otherwise be reused if the node 2225 * is reused before the reference count goes to zero 2226 * (and memory is reclaimed). 2227 */ 2228 ieee80211_sta_leave(ic, ni); 2229 done: 2230 /* 2231 * Remove the node from any table it's recorded in and 2232 * drop the caller's reference. Removal from the table 2233 * is important to insure the node is not reprocessed 2234 * for inactivity. 2235 */ 2236 if (nt != NULL) { 2237 IEEE80211_NODE_LOCK(nt); 2238 node_reclaim(nt, ni); 2239 IEEE80211_NODE_UNLOCK(nt); 2240 } else 2241 ieee80211_free_node(ni); 2242 } 2243 2244 u_int8_t 2245 ieee80211_getrssi(struct ieee80211com *ic) 2246 { 2247 #define NZ(x) ((x) == 0 ? 1 : (x)) 2248 struct ieee80211_node_table *nt = &ic->ic_sta; 2249 u_int32_t rssi_samples, rssi_total; 2250 struct ieee80211_node *ni; 2251 2252 rssi_total = 0; 2253 rssi_samples = 0; 2254 switch (ic->ic_opmode) { 2255 case IEEE80211_M_IBSS: /* average of all ibss neighbors */ 2256 /* XXX locking */ 2257 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 2258 if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) { 2259 rssi_samples++; 2260 rssi_total += ic->ic_node_getrssi(ni); 2261 } 2262 break; 2263 case IEEE80211_M_AHDEMO: /* average of all neighbors */ 2264 /* XXX locking */ 2265 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 2266 rssi_samples++; 2267 rssi_total += ic->ic_node_getrssi(ni); 2268 } 2269 break; 2270 case IEEE80211_M_HOSTAP: /* average of all associated stations */ 2271 /* XXX locking */ 2272 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 2273 if (IEEE80211_AID(ni->ni_associd) != 0) { 2274 rssi_samples++; 2275 rssi_total += ic->ic_node_getrssi(ni); 2276 } 2277 break; 2278 case IEEE80211_M_MONITOR: /* XXX */ 2279 case IEEE80211_M_STA: /* use stats from associated ap */ 2280 default: 2281 if (ic->ic_bss != NULL) 2282 rssi_total = ic->ic_node_getrssi(ic->ic_bss); 2283 rssi_samples = 1; 2284 break; 2285 } 2286 return rssi_total / NZ(rssi_samples); 2287 #undef NZ 2288 } 2289 2290 /* 2291 * Indicate whether there are frames queued for a station in power-save mode. 2292 */ 2293 static void 2294 ieee80211_set_tim(struct ieee80211_node *ni, int set) 2295 { 2296 struct ieee80211com *ic = ni->ni_ic; 2297 u_int16_t aid; 2298 2299 KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 2300 ic->ic_opmode == IEEE80211_M_IBSS, 2301 ("operating mode %u", ic->ic_opmode)); 2302 2303 aid = IEEE80211_AID(ni->ni_associd); 2304 KASSERT(aid < ic->ic_max_aid, 2305 ("bogus aid %u, max %u", aid, ic->ic_max_aid)); 2306 2307 IEEE80211_BEACON_LOCK(ic); 2308 if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) { 2309 if (set) { 2310 setbit(ic->ic_tim_bitmap, aid); 2311 ic->ic_ps_pending++; 2312 } else { 2313 clrbit(ic->ic_tim_bitmap, aid); 2314 ic->ic_ps_pending--; 2315 } 2316 ic->ic_flags |= IEEE80211_F_TIMUPDATE; 2317 } 2318 IEEE80211_BEACON_UNLOCK(ic); 2319 } 2320 2321 /* 2322 * Node table support. 2323 */ 2324 2325 static void 2326 ieee80211_node_table_init(struct ieee80211com *ic, 2327 struct ieee80211_node_table *nt, 2328 const char *name, int inact, int keyixmax, 2329 void (*timeout)(struct ieee80211_node_table *)) 2330 { 2331 2332 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 2333 "%s %s table, inact %u\n", __func__, name, inact); 2334 2335 nt->nt_ic = ic; 2336 /* XXX need unit */ 2337 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname); 2338 IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname); 2339 TAILQ_INIT(&nt->nt_node); 2340 nt->nt_name = name; 2341 nt->nt_scangen = 1; 2342 nt->nt_inact_init = inact; 2343 nt->nt_timeout = timeout; 2344 nt->nt_keyixmax = keyixmax; 2345 if (nt->nt_keyixmax > 0) { 2346 MALLOC(nt->nt_keyixmap, struct ieee80211_node **, 2347 keyixmax * sizeof(struct ieee80211_node *), 2348 M_80211_NODE, M_NOWAIT | M_ZERO); 2349 if (nt->nt_keyixmap == NULL) 2350 if_printf(ic->ic_ifp, 2351 "Cannot allocate key index map with %u entries\n", 2352 keyixmax); 2353 } else 2354 nt->nt_keyixmap = NULL; 2355 } 2356 2357 void 2358 ieee80211_node_table_reset(struct ieee80211_node_table *nt) 2359 { 2360 2361 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 2362 "%s %s table\n", __func__, nt->nt_name); 2363 2364 IEEE80211_NODE_LOCK(nt); 2365 nt->nt_inact_timer = 0; 2366 ieee80211_free_allnodes_locked(nt); 2367 IEEE80211_NODE_UNLOCK(nt); 2368 } 2369 2370 static void 2371 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt) 2372 { 2373 2374 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 2375 "%s %s table\n", __func__, nt->nt_name); 2376 2377 IEEE80211_NODE_LOCK(nt); 2378 ieee80211_free_allnodes_locked(nt); 2379 if (nt->nt_keyixmap != NULL) { 2380 /* XXX verify all entries are NULL */ 2381 int i; 2382 for (i = 0; i < nt->nt_keyixmax; i++) 2383 if (nt->nt_keyixmap[i] != NULL) 2384 printf("%s: %s[%u] still active\n", __func__, 2385 nt->nt_name, i); 2386 FREE(nt->nt_keyixmap, M_80211_NODE); 2387 nt->nt_keyixmap = NULL; 2388 } 2389 IEEE80211_SCAN_LOCK_DESTROY(nt); 2390 IEEE80211_NODE_LOCK_DESTROY(nt); 2391 } 2392