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