1 /* $NetBSD: ieee80211_node.c,v 1.69 2015/08/24 22:21:26 pooka 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.69 2015/08/24 22:21:26 pooka 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 /* 1637 * A mac address that is all zero means match only the ssid; 1638 * otherwise we must match both. 1639 */ 1640 if (IEEE80211_ADDR_EQ(macaddr, zeromac)) { 1641 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1642 if (MATCH_SSID(ni, ssid, ssidlen)) 1643 break; 1644 } 1645 } else { 1646 hash = IEEE80211_NODE_HASH(macaddr); 1647 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1648 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 1649 MATCH_SSID(ni, ssid, ssidlen)) 1650 break; 1651 } 1652 } 1653 if (ni != NULL) { 1654 ieee80211_ref_node(ni); /* mark referenced */ 1655 #ifdef IEEE80211_DEBUG_REFCNT 1656 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1657 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1658 func, line, 1659 ni, ether_sprintf(ni->ni_macaddr), 1660 ieee80211_node_refcnt(ni)); 1661 #else 1662 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1663 "%s %p<%s> refcnt %d\n", __func__, 1664 ni, ether_sprintf(ni->ni_macaddr), 1665 ieee80211_node_refcnt(ni)); 1666 #endif 1667 } 1668 IEEE80211_NODE_UNLOCK(nt); 1669 return ni; 1670 #undef MATCH_SSID 1671 } 1672 1673 static void 1674 _ieee80211_free_node(struct ieee80211_node *ni) 1675 { 1676 struct ieee80211com *ic = ni->ni_ic; 1677 struct ieee80211_node_table *nt = ni->ni_table; 1678 1679 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1680 "%s %p<%s> in %s table\n", __func__, ni, 1681 ether_sprintf(ni->ni_macaddr), 1682 nt != NULL ? nt->nt_name : "<gone>"); 1683 1684 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1685 if (nt != NULL) { 1686 TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1687 LIST_REMOVE(ni, ni_hash); 1688 } 1689 ic->ic_node_free(ni); 1690 } 1691 1692 void 1693 #ifdef IEEE80211_DEBUG_REFCNT 1694 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line) 1695 #else 1696 ieee80211_free_node(struct ieee80211_node *ni) 1697 #endif 1698 { 1699 struct ieee80211_node_table *nt = ni->ni_table; 1700 1701 #ifdef IEEE80211_DEBUG_REFCNT 1702 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1703 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 1704 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1705 #endif 1706 if (nt != NULL) { 1707 IEEE80211_NODE_LOCK(nt); 1708 if (ieee80211_node_dectestref(ni)) { 1709 /* 1710 * Last reference, reclaim state. 1711 */ 1712 _ieee80211_free_node(ni); 1713 } else if (ieee80211_node_refcnt(ni) == 1 && 1714 nt->nt_keyixmap != NULL) { 1715 ieee80211_keyix keyix; 1716 /* 1717 * Check for a last reference in the key mapping table. 1718 */ 1719 keyix = ni->ni_ucastkey.wk_rxkeyix; 1720 if (keyix < nt->nt_keyixmax && 1721 nt->nt_keyixmap[keyix] == ni) { 1722 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1723 "%s: %p<%s> clear key map entry", __func__, 1724 ni, ether_sprintf(ni->ni_macaddr)); 1725 nt->nt_keyixmap[keyix] = NULL; 1726 ieee80211_node_decref(ni); /* XXX needed? */ 1727 _ieee80211_free_node(ni); 1728 } 1729 } 1730 IEEE80211_NODE_UNLOCK(nt); 1731 } else { 1732 if (ieee80211_node_dectestref(ni)) 1733 _ieee80211_free_node(ni); 1734 } 1735 } 1736 1737 /* 1738 * Reclaim a unicast key and clear any key cache state. 1739 */ 1740 int 1741 ieee80211_node_delucastkey(struct ieee80211_node *ni) 1742 { 1743 struct ieee80211com *ic = ni->ni_ic; 1744 struct ieee80211_node_table *nt = &ic->ic_sta; 1745 struct ieee80211_node *nikey; 1746 ieee80211_keyix keyix; 1747 int isowned, status; 1748 1749 /* 1750 * NB: We must beware of LOR here; deleting the key 1751 * can cause the crypto layer to block traffic updates 1752 * which can generate a LOR against the node table lock; 1753 * grab it here and stash the key index for our use below. 1754 * 1755 * Must also beware of recursion on the node table lock. 1756 * When called from node_cleanup we may already have 1757 * the node table lock held. Unfortunately there's no 1758 * way to separate out this path so we must do this 1759 * conditionally. 1760 */ 1761 isowned = IEEE80211_NODE_IS_LOCKED(nt); 1762 if (!isowned) 1763 IEEE80211_NODE_LOCK(nt); 1764 keyix = ni->ni_ucastkey.wk_rxkeyix; 1765 status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey); 1766 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) { 1767 nikey = nt->nt_keyixmap[keyix]; 1768 nt->nt_keyixmap[keyix] = NULL; 1769 } else 1770 nikey = NULL; 1771 if (!isowned) 1772 IEEE80211_NODE_UNLOCK(&ic->ic_sta); 1773 1774 if (nikey != NULL) { 1775 IASSERT(nikey == ni, 1776 ("key map out of sync, ni %p nikey %p", ni, nikey)); 1777 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1778 "%s: delete key map entry %p<%s> refcnt %d\n", 1779 __func__, ni, ether_sprintf(ni->ni_macaddr), 1780 ieee80211_node_refcnt(ni)-1); 1781 ieee80211_free_node(ni); 1782 } 1783 return status; 1784 } 1785 1786 /* 1787 * Reclaim a node. If this is the last reference count then 1788 * do the normal free work. Otherwise remove it from the node 1789 * table and mark it gone by clearing the back-reference. 1790 */ 1791 static void 1792 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1793 { 1794 ieee80211_keyix keyix; 1795 1796 IEEE80211_NODE_LOCK_ASSERT(nt); 1797 1798 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1799 "%s: remove %p<%s> from %s table, refcnt %d\n", 1800 __func__, ni, ether_sprintf(ni->ni_macaddr), 1801 nt->nt_name, ieee80211_node_refcnt(ni)-1); 1802 /* 1803 * Clear any entry in the unicast key mapping table. 1804 * We need to do it here so rx lookups don't find it 1805 * in the mapping table even if it's not in the hash 1806 * table. We cannot depend on the mapping table entry 1807 * being cleared because the node may not be free'd. 1808 */ 1809 keyix = ni->ni_ucastkey.wk_rxkeyix; 1810 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax && 1811 nt->nt_keyixmap[keyix] == ni) { 1812 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE, 1813 "%s: %p<%s> clear key map entry\n", 1814 __func__, ni, ether_sprintf(ni->ni_macaddr)); 1815 nt->nt_keyixmap[keyix] = NULL; 1816 ieee80211_node_decref(ni); /* NB: don't need free */ 1817 } 1818 if (!ieee80211_node_dectestref(ni)) { 1819 /* 1820 * Other references are present, just remove the 1821 * node from the table so it cannot be found. When 1822 * the references are dropped storage will be 1823 * reclaimed. 1824 */ 1825 TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1826 LIST_REMOVE(ni, ni_hash); 1827 ni->ni_table = NULL; /* clear reference */ 1828 } else 1829 _ieee80211_free_node(ni); 1830 } 1831 1832 static void 1833 ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt) 1834 { 1835 struct ieee80211com *ic = nt->nt_ic; 1836 struct ieee80211_node *ni; 1837 1838 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1839 "%s: free all nodes in %s table\n", __func__, nt->nt_name); 1840 1841 while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) { 1842 if (ni->ni_associd != 0) { 1843 if (ic->ic_auth->ia_node_leave != NULL) 1844 ic->ic_auth->ia_node_leave(ic, ni); 1845 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1846 } 1847 node_reclaim(nt, ni); 1848 } 1849 ieee80211_reset_erp(ic); 1850 } 1851 1852 static void 1853 ieee80211_free_allnodes(struct ieee80211_node_table *nt) 1854 { 1855 1856 IEEE80211_NODE_LOCK(nt); 1857 ieee80211_free_allnodes_locked(nt); 1858 IEEE80211_NODE_UNLOCK(nt); 1859 } 1860 1861 /* 1862 * Timeout entries in the scan cache. 1863 */ 1864 static void 1865 ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt) 1866 { 1867 struct ieee80211com *ic = nt->nt_ic; 1868 struct ieee80211_node *ni, *tni; 1869 1870 IEEE80211_NODE_LOCK(nt); 1871 ni = ic->ic_bss; 1872 /* XXX belongs elsewhere */ 1873 if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) { 1874 m_freem(ni->ni_rxfrag[0]); 1875 ni->ni_rxfrag[0] = NULL; 1876 } 1877 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) { 1878 if (ni->ni_inact && --ni->ni_inact == 0) { 1879 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1880 "[%s] scan candidate purged from cache " 1881 "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr), 1882 ieee80211_node_refcnt(ni)); 1883 node_reclaim(nt, ni); 1884 } 1885 } 1886 IEEE80211_NODE_UNLOCK(nt); 1887 1888 nt->nt_inact_timer = IEEE80211_INACT_WAIT; 1889 } 1890 1891 /* 1892 * Timeout inactive stations and do related housekeeping. 1893 * Note that we cannot hold the node lock while sending a 1894 * frame as this would lead to a LOR. Instead we use a 1895 * generation number to mark nodes that we've scanned and 1896 * drop the lock and restart a scan if we have to time out 1897 * a node. Since we are single-threaded by virtue of 1898 * controlling the inactivity timer we can be sure this will 1899 * process each node only once. 1900 */ 1901 static void 1902 ieee80211_timeout_stations(struct ieee80211_node_table *nt) 1903 { 1904 struct ieee80211com *ic = nt->nt_ic; 1905 struct ieee80211_node *ni; 1906 u_int gen; 1907 int isadhoc; 1908 1909 isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS || 1910 ic->ic_opmode == IEEE80211_M_AHDEMO); 1911 IEEE80211_SCAN_LOCK(nt); 1912 gen = ++nt->nt_scangen; 1913 IEEE80211_SCAN_UNLOCK(nt); 1914 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1915 "%s: %s scangen %u\n", __func__, nt->nt_name, gen); 1916 restart: 1917 IEEE80211_SCAN_LOCK(nt); 1918 if (gen != nt->nt_scangen) { 1919 printf("%s: scan aborted %u\n", __func__, gen); 1920 IEEE80211_SCAN_UNLOCK(nt); 1921 return; 1922 } 1923 IEEE80211_SCAN_UNLOCK(nt); 1924 1925 IEEE80211_NODE_LOCK(nt); 1926 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 1927 if (ni->ni_scangen == gen) /* previously handled */ 1928 continue; 1929 ni->ni_scangen = gen; 1930 /* 1931 * Ignore entries for which have yet to receive an 1932 * authentication frame. These are transient and 1933 * will be reclaimed when the last reference to them 1934 * goes away (when frame xmits complete). 1935 */ 1936 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1937 (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 1938 continue; 1939 /* 1940 * Free fragment if not needed anymore 1941 * (last fragment older than 1s). 1942 * XXX doesn't belong here 1943 */ 1944 if (ni->ni_rxfrag[0] != NULL && 1945 ticks > ni->ni_rxfragstamp + hz) { 1946 m_freem(ni->ni_rxfrag[0]); 1947 ni->ni_rxfrag[0] = NULL; 1948 } 1949 /* 1950 * Special case ourself; we may be idle for extended periods 1951 * of time and regardless reclaiming our state is wrong. 1952 */ 1953 if (ni == ic->ic_bss) 1954 continue; 1955 ni->ni_inact--; 1956 if (ni->ni_associd != 0 || isadhoc) { 1957 /* 1958 * Age frames on the power save queue. The 1959 * aging interval is 4 times the listen 1960 * interval specified by the station. This 1961 * number is factored into the age calculations 1962 * when the frame is placed on the queue. We 1963 * store ages as time differences we can check 1964 * and/or adjust only the head of the list. 1965 */ 1966 if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) { 1967 struct mbuf *m; 1968 int discard = 0; 1969 1970 IEEE80211_NODE_SAVEQ_LOCK(ni); 1971 while (IF_POLL(&ni->ni_savedq, m) != NULL && 1972 M_AGE_GET(m) < IEEE80211_INACT_WAIT) { 1973 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/ 1974 _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m); 1975 m_freem(m); 1976 discard++; 1977 } 1978 if (m != NULL) 1979 M_AGE_SUB(m, IEEE80211_INACT_WAIT); 1980 IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1981 1982 if (discard != 0) { 1983 IEEE80211_DPRINTF(ic, 1984 IEEE80211_MSG_POWER, 1985 "[%s] discard %u frames for age\n", 1986 ether_sprintf(ni->ni_macaddr), 1987 discard); 1988 IEEE80211_NODE_STAT_ADD(ni, 1989 ps_discard, discard); 1990 if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) 1991 ic->ic_set_tim(ni, 0); 1992 } 1993 } 1994 /* 1995 * Probe the station before time it out. We 1996 * send a null data frame which may not be 1997 * universally supported by drivers (need it 1998 * for ps-poll support so it should be...). 1999 */ 2000 if (0 < ni->ni_inact && 2001 ni->ni_inact <= ic->ic_inact_probe) { 2002 IEEE80211_NOTE(ic, 2003 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, 2004 ni, "%s", 2005 "probe station due to inactivity"); 2006 /* 2007 * Grab a reference before unlocking the table 2008 * so the node cannot be reclaimed before we 2009 * send the frame. ieee80211_send_nulldata 2010 * understands we've done this and reclaims the 2011 * ref for us as needed. 2012 */ 2013 ieee80211_ref_node(ni); 2014 IEEE80211_NODE_UNLOCK(nt); 2015 ieee80211_send_nulldata(ni); 2016 /* XXX stat? */ 2017 goto restart; 2018 } 2019 } 2020 if (ni->ni_inact <= 0) { 2021 IEEE80211_NOTE(ic, 2022 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni, 2023 "station timed out due to inactivity " 2024 "(refcnt %u)", ieee80211_node_refcnt(ni)); 2025 /* 2026 * Send a deauthenticate frame and drop the station. 2027 * This is somewhat complicated due to reference counts 2028 * and locking. At this point a station will typically 2029 * have a reference count of 1. ieee80211_node_leave 2030 * will do a "free" of the node which will drop the 2031 * reference count. But in the meantime a reference 2032 * will be held by the deauth frame. The actual reclaim 2033 * of the node will happen either after the tx is 2034 * completed or by ieee80211_node_leave. 2035 * 2036 * Separately we must drop the node lock before sending 2037 * in case the driver takes a lock, as this will result 2038 * in LOR between the node lock and the driver lock. 2039 */ 2040 IEEE80211_NODE_UNLOCK(nt); 2041 if (ni->ni_associd != 0) { 2042 IEEE80211_SEND_MGMT(ic, ni, 2043 IEEE80211_FC0_SUBTYPE_DEAUTH, 2044 IEEE80211_REASON_AUTH_EXPIRE); 2045 } 2046 ieee80211_node_leave(ic, ni); 2047 ic->ic_stats.is_node_timeout++; 2048 goto restart; 2049 } 2050 } 2051 IEEE80211_NODE_UNLOCK(nt); 2052 2053 nt->nt_inact_timer = IEEE80211_INACT_WAIT; 2054 } 2055 2056 void 2057 ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg) 2058 { 2059 struct ieee80211_node *ni; 2060 u_int gen; 2061 2062 IEEE80211_SCAN_LOCK(nt); 2063 gen = ++nt->nt_scangen; 2064 IEEE80211_SCAN_UNLOCK(nt); 2065 restart: 2066 IEEE80211_SCAN_LOCK(nt); 2067 if (gen != nt->nt_scangen) { 2068 printf("%s: scan aborted %u\n", __func__, gen); 2069 IEEE80211_SCAN_UNLOCK(nt); 2070 return; 2071 } 2072 IEEE80211_SCAN_UNLOCK(nt); 2073 2074 IEEE80211_NODE_LOCK(nt); 2075 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 2076 if (ni->ni_scangen != gen) { 2077 ni->ni_scangen = gen; 2078 (void) ieee80211_ref_node(ni); 2079 IEEE80211_NODE_UNLOCK(nt); 2080 (*f)(arg, ni); 2081 ieee80211_free_node(ni); 2082 goto restart; 2083 } 2084 } 2085 IEEE80211_NODE_UNLOCK(nt); 2086 } 2087 2088 void 2089 ieee80211_dump_node(struct ieee80211_node_table *nt, 2090 struct ieee80211_node *ni) 2091 { 2092 printf("0x%p: mac %s refcnt %d\n", ni, 2093 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 2094 printf("\tscangen %u authmode %u flags 0x%x\n", 2095 ni->ni_scangen, ni->ni_authmode, ni->ni_flags); 2096 printf("\tassocid 0x%x txpower %u vlan %u\n", 2097 ni->ni_associd, ni->ni_txpower, ni->ni_vlan); 2098 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n", 2099 ni->ni_txseqs[0], 2100 ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT, 2101 ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK, 2102 ni->ni_rxfragstamp); 2103 printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n", 2104 ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo); 2105 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n", 2106 ether_sprintf(ni->ni_bssid), 2107 ni->ni_esslen, ni->ni_essid, 2108 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags); 2109 printf("\tfails %u inact %u txrate %u\n", 2110 ni->ni_fails, ni->ni_inact, ni->ni_txrate); 2111 } 2112 2113 void 2114 ieee80211_dump_nodes(struct ieee80211_node_table *nt) 2115 { 2116 ieee80211_iterate_nodes(nt, 2117 (ieee80211_iter_func *) ieee80211_dump_node, nt); 2118 } 2119 2120 /* 2121 * Handle a station joining an 11g network. 2122 */ 2123 static void 2124 ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 2125 { 2126 2127 /* 2128 * Station isn't capable of short slot time. Bump 2129 * the count of long slot time stations and disable 2130 * use of short slot time. Note that the actual switch 2131 * over to long slot time use may not occur until the 2132 * next beacon transmission (per sec. 7.3.1.4 of 11g). 2133 */ 2134 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2135 ic->ic_longslotsta++; 2136 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2137 "[%s] station needs long slot time, count %d\n", 2138 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 2139 /* XXX vap's w/ conflicting needs won't work */ 2140 ieee80211_set_shortslottime(ic, 0); 2141 } 2142 /* 2143 * If the new station is not an ERP station 2144 * then bump the counter and enable protection 2145 * if configured. 2146 */ 2147 if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) { 2148 ic->ic_nonerpsta++; 2149 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2150 "[%s] station is !ERP, %d non-ERP stations associated\n", 2151 ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 2152 /* 2153 * If protection is configured, enable it. 2154 */ 2155 if (ic->ic_protmode != IEEE80211_PROT_NONE) { 2156 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2157 "%s: enable use of protection\n", __func__); 2158 ic->ic_flags |= IEEE80211_F_USEPROT; 2159 } 2160 /* 2161 * If station does not support short preamble 2162 * then we must enable use of Barker preamble. 2163 */ 2164 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) { 2165 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2166 "[%s] station needs long preamble\n", 2167 ether_sprintf(ni->ni_macaddr)); 2168 ic->ic_flags |= IEEE80211_F_USEBARKER; 2169 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 2170 } 2171 } else 2172 ni->ni_flags |= IEEE80211_NODE_ERP; 2173 } 2174 2175 void 2176 ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp) 2177 { 2178 int newassoc; 2179 2180 if (ni->ni_associd == 0) { 2181 u_int16_t aid; 2182 2183 /* 2184 * It would be good to search the bitmap 2185 * more efficiently, but this will do for now. 2186 */ 2187 for (aid = 1; aid < ic->ic_max_aid; aid++) { 2188 if (!IEEE80211_AID_ISSET(aid, 2189 ic->ic_aid_bitmap)) 2190 break; 2191 } 2192 if (aid >= ic->ic_max_aid) { 2193 IEEE80211_SEND_MGMT(ic, ni, resp, 2194 IEEE80211_REASON_ASSOC_TOOMANY); 2195 ieee80211_node_leave(ic, ni); 2196 return; 2197 } 2198 ni->ni_associd = aid | 0xc000; 2199 IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap); 2200 ic->ic_sta_assoc++; 2201 newassoc = 1; 2202 if (ic->ic_curmode == IEEE80211_MODE_11G) 2203 ieee80211_node_join_11g(ic, ni); 2204 } else 2205 newassoc = 0; 2206 2207 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 2208 "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n", 2209 ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re", 2210 IEEE80211_NODE_AID(ni), 2211 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long", 2212 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", 2213 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "", 2214 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "" 2215 ); 2216 2217 /* give driver a chance to setup state like ni_txrate */ 2218 if (ic->ic_newassoc != NULL) 2219 ic->ic_newassoc(ni, newassoc); 2220 ni->ni_inact_reload = ic->ic_inact_auth; 2221 ni->ni_inact = ni->ni_inact_reload; 2222 IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS); 2223 /* tell the authenticator about new station */ 2224 if (ic->ic_auth->ia_node_join != NULL) 2225 ic->ic_auth->ia_node_join(ic, ni); 2226 ieee80211_notify_node_join(ic, ni, newassoc); 2227 } 2228 2229 /* 2230 * Handle a station leaving an 11g network. 2231 */ 2232 static void 2233 ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 2234 { 2235 2236 IASSERT(ic->ic_curmode == IEEE80211_MODE_11G, 2237 ("not in 11g, bss %u:0x%x, curmode %u", ni->ni_chan->ic_freq, 2238 ni->ni_chan->ic_flags, ic->ic_curmode)); 2239 2240 /* 2241 * If a long slot station do the slot time bookkeeping. 2242 */ 2243 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2244 IASSERT(ic->ic_longslotsta > 0, 2245 ("bogus long slot station count %d", ic->ic_longslotsta)); 2246 ic->ic_longslotsta--; 2247 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2248 "[%s] long slot time station leaves, count now %d\n", 2249 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta); 2250 if (ic->ic_longslotsta == 0) { 2251 /* 2252 * Re-enable use of short slot time if supported 2253 * and not operating in IBSS mode (per spec). 2254 */ 2255 if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 2256 ic->ic_opmode != IEEE80211_M_IBSS) { 2257 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2258 "%s: re-enable use of short slot time\n", 2259 __func__); 2260 ieee80211_set_shortslottime(ic, 1); 2261 } 2262 } 2263 } 2264 /* 2265 * If a non-ERP station do the protection-related bookkeeping. 2266 */ 2267 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) { 2268 IASSERT(ic->ic_nonerpsta > 0, 2269 ("bogus non-ERP station count %d", ic->ic_nonerpsta)); 2270 ic->ic_nonerpsta--; 2271 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2272 "[%s] non-ERP station leaves, count now %d\n", 2273 ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta); 2274 if (ic->ic_nonerpsta == 0) { 2275 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2276 "%s: disable use of protection\n", __func__); 2277 ic->ic_flags &= ~IEEE80211_F_USEPROT; 2278 /* XXX verify mode? */ 2279 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) { 2280 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2281 "%s: re-enable use of short preamble\n", 2282 __func__); 2283 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 2284 ic->ic_flags &= ~IEEE80211_F_USEBARKER; 2285 } 2286 } 2287 } 2288 } 2289 2290 /* 2291 * Handle bookkeeping for station deauthentication/disassociation 2292 * when operating as an ap. 2293 */ 2294 void 2295 ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 2296 { 2297 struct ieee80211_node_table *nt = ni->ni_table; 2298 2299 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, 2300 "[%s] station with aid %d leaves\n", 2301 ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni)); 2302 2303 IASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 2304 ic->ic_opmode == IEEE80211_M_IBSS || 2305 ic->ic_opmode == IEEE80211_M_AHDEMO, 2306 ("unexpected operating mode %u", ic->ic_opmode)); 2307 /* 2308 * If node wasn't previously associated all 2309 * we need to do is reclaim the reference. 2310 */ 2311 /* XXX ibss mode bypasses 11g and notification */ 2312 if (ni->ni_associd == 0) 2313 goto done; 2314 /* 2315 * Tell the authenticator the station is leaving. 2316 * Note that we must do this before yanking the 2317 * association id as the authenticator uses the 2318 * associd to locate its state block. 2319 */ 2320 if (ic->ic_auth->ia_node_leave != NULL) 2321 ic->ic_auth->ia_node_leave(ic, ni); 2322 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 2323 ni->ni_associd = 0; 2324 ic->ic_sta_assoc--; 2325 2326 if (ic->ic_curmode == IEEE80211_MODE_11G) 2327 ieee80211_node_leave_11g(ic, ni); 2328 /* 2329 * Cleanup station state. In particular clear various 2330 * state that might otherwise be reused if the node 2331 * is reused before the reference count goes to zero 2332 * (and memory is reclaimed). 2333 */ 2334 ieee80211_sta_leave(ic, ni); 2335 done: 2336 /* 2337 * Remove the node from any table it's recorded in and 2338 * drop the caller's reference. Removal from the table 2339 * is important to insure the node is not reprocessed 2340 * for inactivity. 2341 */ 2342 if (nt != NULL) { 2343 IEEE80211_NODE_LOCK(nt); 2344 node_reclaim(nt, ni); 2345 IEEE80211_NODE_UNLOCK(nt); 2346 } else 2347 ieee80211_free_node(ni); 2348 } 2349 2350 u_int8_t 2351 ieee80211_getrssi(struct ieee80211com *ic) 2352 { 2353 #define NZ(x) ((x) == 0 ? 1 : (x)) 2354 struct ieee80211_node_table *nt = &ic->ic_sta; 2355 u_int32_t rssi_samples, rssi_total; 2356 struct ieee80211_node *ni; 2357 2358 rssi_total = 0; 2359 rssi_samples = 0; 2360 switch (ic->ic_opmode) { 2361 case IEEE80211_M_IBSS: /* average of all ibss neighbors */ 2362 /* XXX locking */ 2363 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 2364 if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) { 2365 rssi_samples++; 2366 rssi_total += ic->ic_node_getrssi(ni); 2367 } 2368 break; 2369 case IEEE80211_M_AHDEMO: /* average of all neighbors */ 2370 /* XXX locking */ 2371 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 2372 rssi_samples++; 2373 rssi_total += ic->ic_node_getrssi(ni); 2374 } 2375 break; 2376 case IEEE80211_M_HOSTAP: /* average of all associated stations */ 2377 #ifndef IEEE80211_NO_HOSTAP 2378 /* XXX locking */ 2379 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) 2380 if (IEEE80211_AID(ni->ni_associd) != 0) { 2381 rssi_samples++; 2382 rssi_total += ic->ic_node_getrssi(ni); 2383 } 2384 #endif /* !IEEE80211_NO_HOSTAP */ 2385 break; 2386 case IEEE80211_M_MONITOR: /* XXX */ 2387 case IEEE80211_M_STA: /* use stats from associated ap */ 2388 default: 2389 if (ic->ic_bss != NULL) 2390 rssi_total = ic->ic_node_getrssi(ic->ic_bss); 2391 rssi_samples = 1; 2392 break; 2393 } 2394 return rssi_total / NZ(rssi_samples); 2395 #undef NZ 2396 } 2397 2398 /* 2399 * Indicate whether there are frames queued for a station in power-save mode. 2400 */ 2401 static void 2402 ieee80211_set_tim(struct ieee80211_node *ni, int set) 2403 { 2404 struct ieee80211com *ic = ni->ni_ic; 2405 u_int16_t aid; 2406 2407 IASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP || 2408 ic->ic_opmode == IEEE80211_M_IBSS, 2409 ("operating mode %u", ic->ic_opmode)); 2410 2411 aid = IEEE80211_AID(ni->ni_associd); 2412 IASSERT(aid < ic->ic_max_aid, 2413 ("bogus aid %u, max %u", aid, ic->ic_max_aid)); 2414 2415 IEEE80211_BEACON_LOCK(ic); 2416 if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) { 2417 if (set) { 2418 setbit(ic->ic_tim_bitmap, aid); 2419 ic->ic_ps_pending++; 2420 } else { 2421 clrbit(ic->ic_tim_bitmap, aid); 2422 ic->ic_ps_pending--; 2423 } 2424 ic->ic_flags |= IEEE80211_F_TIMUPDATE; 2425 } 2426 IEEE80211_BEACON_UNLOCK(ic); 2427 } 2428 2429 /* 2430 * Node table support. 2431 */ 2432 2433 static void 2434 ieee80211_node_table_init(struct ieee80211com *ic, 2435 struct ieee80211_node_table *nt, 2436 const char *name, int inact, int keyixmax, 2437 void (*timeout)(struct ieee80211_node_table *)) 2438 { 2439 2440 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 2441 "%s %s table, inact %u\n", __func__, name, inact); 2442 2443 nt->nt_ic = ic; 2444 /* XXX need unit */ 2445 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname); 2446 IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname); 2447 TAILQ_INIT(&nt->nt_node); 2448 nt->nt_name = name; 2449 nt->nt_scangen = 1; 2450 nt->nt_inact_init = inact; 2451 nt->nt_timeout = timeout; 2452 nt->nt_keyixmax = keyixmax; 2453 if (nt->nt_keyixmax > 0) { 2454 nt->nt_keyixmap = malloc(keyixmax * 2455 sizeof(struct ieee80211_node *), M_80211_NODE, 2456 M_NOWAIT | M_ZERO); 2457 if (nt->nt_keyixmap == NULL) 2458 if_printf(ic->ic_ifp, 2459 "Cannot allocate key index map with %u entries\n", 2460 keyixmax); 2461 } else 2462 nt->nt_keyixmap = NULL; 2463 } 2464 2465 void 2466 ieee80211_node_table_reset(struct ieee80211_node_table *nt) 2467 { 2468 2469 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 2470 "%s %s table\n", __func__, nt->nt_name); 2471 2472 IEEE80211_NODE_LOCK(nt); 2473 nt->nt_inact_timer = 0; 2474 ieee80211_free_allnodes_locked(nt); 2475 IEEE80211_NODE_UNLOCK(nt); 2476 } 2477 2478 static void 2479 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt) 2480 { 2481 2482 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE, 2483 "%s %s table\n", __func__, nt->nt_name); 2484 2485 IEEE80211_NODE_LOCK(nt); 2486 ieee80211_free_allnodes_locked(nt); 2487 IEEE80211_NODE_UNLOCK(nt); 2488 if (nt->nt_keyixmap != NULL) { 2489 /* XXX verify all entries are NULL */ 2490 int i; 2491 for (i = 0; i < nt->nt_keyixmax; i++) 2492 if (nt->nt_keyixmap[i] != NULL) 2493 printf("%s: %s[%u] still active\n", __func__, 2494 nt->nt_name, i); 2495 free(nt->nt_keyixmap, M_80211_NODE); 2496 nt->nt_keyixmap = NULL; 2497 } 2498 IEEE80211_SCAN_LOCK_DESTROY(nt); 2499 IEEE80211_NODE_LOCK_DESTROY(nt); 2500 } 2501