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