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