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