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