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