1 /* $NetBSD: ieee80211_node.c,v 1.14 2004/05/09 09:18:47 dyoung Exp $ */ 2 /*- 3 * Copyright (c) 2001 Atsushi Onoe 4 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * Alternatively, this software may be distributed under the terms of the 19 * GNU General Public License ("GPL") version 2 as published by the Free 20 * Software Foundation. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 #ifdef __FreeBSD__ 36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_node.c,v 1.22 2004/04/05 04:15:55 sam Exp $"); 37 #else 38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_node.c,v 1.14 2004/05/09 09:18:47 dyoung Exp $"); 39 #endif 40 41 #include "opt_inet.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/mbuf.h> 46 #include <sys/malloc.h> 47 #include <sys/kernel.h> 48 #include <sys/socket.h> 49 #include <sys/sockio.h> 50 #include <sys/endian.h> 51 #include <sys/errno.h> 52 #ifdef __FreeBSD__ 53 #include <sys/bus.h> 54 #endif 55 #include <sys/proc.h> 56 #include <sys/sysctl.h> 57 58 #ifdef __FreeBSD__ 59 #include <machine/atomic.h> 60 #endif 61 62 #include <net/if.h> 63 #include <net/if_dl.h> 64 #include <net/if_media.h> 65 #include <net/if_arp.h> 66 #ifdef __FreeBSD__ 67 #include <net/ethernet.h> 68 #else 69 #include <net/if_ether.h> 70 #endif 71 #include <net/if_llc.h> 72 73 #include <net80211/ieee80211_var.h> 74 #include <net80211/ieee80211_compat.h> 75 76 #include <net/bpf.h> 77 78 #ifdef INET 79 #include <netinet/in.h> 80 #ifdef __FreeBSD__ 81 #include <netinet/if_ether.h> 82 #else 83 #include <net/if_ether.h> 84 #endif 85 #endif 86 87 static struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *); 88 static void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *); 89 static void ieee80211_node_copy(struct ieee80211com *, 90 struct ieee80211_node *, const struct ieee80211_node *); 91 static u_int8_t ieee80211_node_getrssi(struct ieee80211com *, 92 struct ieee80211_node *); 93 94 static void ieee80211_setup_node(struct ieee80211com *ic, 95 struct ieee80211_node *ni, u_int8_t *macaddr); 96 static void _ieee80211_free_node(struct ieee80211com *, 97 struct ieee80211_node *); 98 99 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state"); 100 101 void 102 ieee80211_node_attach(struct ifnet *ifp) 103 { 104 struct ieee80211com *ic = (void *)ifp; 105 106 #ifdef __FreeBSD__ 107 /* XXX need unit */ 108 IEEE80211_NODE_LOCK_INIT(ic, ifp->if_xname); 109 #endif 110 TAILQ_INIT(&ic->ic_node); 111 ic->ic_node_alloc = ieee80211_node_alloc; 112 ic->ic_node_free = ieee80211_node_free; 113 ic->ic_node_copy = ieee80211_node_copy; 114 ic->ic_node_getrssi = ieee80211_node_getrssi; 115 ic->ic_scangen = 1; 116 } 117 118 void 119 ieee80211_node_lateattach(struct ifnet *ifp) 120 { 121 struct ieee80211com *ic = (void *)ifp; 122 struct ieee80211_node *ni; 123 124 ni = (*ic->ic_node_alloc)(ic); 125 IASSERT(ni != NULL, ("unable to setup inital BSS node")); 126 ni->ni_chan = IEEE80211_CHAN_ANYC; 127 ic->ic_bss = ni; 128 ic->ic_txpower = IEEE80211_TXPOWER_MAX; 129 } 130 131 void 132 ieee80211_node_detach(struct ifnet *ifp) 133 { 134 struct ieee80211com *ic = (void *)ifp; 135 136 if (ic->ic_bss != NULL) 137 (*ic->ic_node_free)(ic, ic->ic_bss); 138 ieee80211_free_allnodes(ic); 139 #ifdef __FreeBSD__ 140 IEEE80211_NODE_LOCK_DESTROY(ic); 141 #endif 142 } 143 144 /* 145 * AP scanning support. 146 */ 147 148 /* 149 * Initialize the active channel set based on the set 150 * of available channels and the current PHY mode. 151 */ 152 static void 153 ieee80211_reset_scan(struct ifnet *ifp) 154 { 155 struct ieee80211com *ic = (void *)ifp; 156 157 memcpy(ic->ic_chan_scan, ic->ic_chan_active, 158 sizeof(ic->ic_chan_active)); 159 /* NB: hack, setup so next_scan starts with the first channel */ 160 if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC) 161 ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX]; 162 } 163 164 /* 165 * Begin an active scan. 166 */ 167 void 168 ieee80211_begin_scan(struct ifnet *ifp) 169 { 170 struct ieee80211com *ic = (void *)ifp; 171 172 /* 173 * In all but hostap mode scanning starts off in 174 * an active mode before switching to passive. 175 */ 176 if (ic->ic_opmode != IEEE80211_M_HOSTAP) { 177 ic->ic_flags |= IEEE80211_F_ASCAN; 178 ic->ic_stats.is_scan_active++; 179 } else 180 ic->ic_stats.is_scan_passive++; 181 if (ifp->if_flags & IFF_DEBUG) 182 if_printf(ifp, "begin %s scan\n", 183 (ic->ic_flags & IEEE80211_F_ASCAN) ? 184 "active" : "passive"); 185 /* 186 * Clear scan state and flush any previously seen 187 * AP's. Note that the latter assumes we don't act 188 * as both an AP and a station, otherwise we'll 189 * potentially flush state of stations associated 190 * with us. 191 */ 192 ieee80211_reset_scan(ifp); 193 ieee80211_free_allnodes(ic); 194 195 /* Scan the next channel. */ 196 ieee80211_next_scan(ifp); 197 } 198 199 /* 200 * Switch to the next channel marked for scanning. 201 */ 202 void 203 ieee80211_next_scan(struct ifnet *ifp) 204 { 205 struct ieee80211com *ic = (void *)ifp; 206 struct ieee80211_channel *chan; 207 208 chan = ic->ic_bss->ni_chan; 209 for (;;) { 210 if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX]) 211 chan = &ic->ic_channels[0]; 212 if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) { 213 /* 214 * Honor channels marked passive-only 215 * during an active scan. 216 */ 217 if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 || 218 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) 219 break; 220 } 221 if (chan == ic->ic_bss->ni_chan) { 222 ieee80211_end_scan(ifp); 223 return; 224 } 225 } 226 clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan)); 227 IEEE80211_DPRINTF(("ieee80211_next_scan: chan %d->%d\n", 228 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan), 229 ieee80211_chan2ieee(ic, chan))); 230 ic->ic_bss->ni_chan = chan; 231 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 232 } 233 234 void 235 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 236 { 237 struct ieee80211_node *ni; 238 struct ifnet *ifp = &ic->ic_if; 239 240 ni = ic->ic_bss; 241 if (ifp->if_flags & IFF_DEBUG) 242 if_printf(ifp, "creating ibss\n"); 243 ic->ic_flags |= IEEE80211_F_SIBSS; 244 ni->ni_chan = chan; 245 ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)]; 246 IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr); 247 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 248 if (ic->ic_opmode == IEEE80211_M_IBSS) 249 ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */ 250 ni->ni_esslen = ic->ic_des_esslen; 251 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 252 ni->ni_rssi = 0; 253 ni->ni_rstamp = 0; 254 memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp)); 255 ni->ni_intval = ic->ic_lintval; 256 ni->ni_capinfo = IEEE80211_CAPINFO_IBSS; 257 if (ic->ic_flags & IEEE80211_F_WEPON) 258 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 259 if (ic->ic_phytype == IEEE80211_T_FH) { 260 ni->ni_fhdwell = 200; /* XXX */ 261 ni->ni_fhindex = 1; 262 } 263 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 264 } 265 266 int 267 ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni) 268 { 269 u_int8_t rate; 270 int fail; 271 272 fail = 0; 273 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 274 fail |= 0x01; 275 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 276 ni->ni_chan != ic->ic_des_chan) 277 fail |= 0x01; 278 if (ic->ic_opmode == IEEE80211_M_IBSS) { 279 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 280 fail |= 0x02; 281 } else { 282 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 283 fail |= 0x02; 284 } 285 if (ic->ic_flags & IEEE80211_F_WEPON) { 286 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 287 fail |= 0x04; 288 } else { 289 /* XXX does this mean privacy is supported or required? */ 290 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 291 fail |= 0x04; 292 } 293 rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO); 294 if (rate & IEEE80211_RATE_BASIC) 295 fail |= 0x08; 296 if (ic->ic_des_esslen != 0 && 297 (ni->ni_esslen != ic->ic_des_esslen || 298 memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) 299 fail |= 0x10; 300 if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 301 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 302 fail |= 0x20; 303 #ifdef IEEE80211_DEBUG 304 if (ic->ic_if.if_flags & IFF_DEBUG) { 305 printf(" %c %s", fail ? '-' : '+', 306 ether_sprintf(ni->ni_macaddr)); 307 printf(" %s%c", ether_sprintf(ni->ni_bssid), 308 fail & 0x20 ? '!' : ' '); 309 printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan), 310 fail & 0x01 ? '!' : ' '); 311 printf(" %+4d", ni->ni_rssi); 312 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 313 fail & 0x08 ? '!' : ' '); 314 printf(" %4s%c", 315 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 316 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 317 "????", 318 fail & 0x02 ? '!' : ' '); 319 printf(" %3s%c ", 320 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 321 "wep" : "no", 322 fail & 0x04 ? '!' : ' '); 323 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 324 printf("%s\n", fail & 0x10 ? "!" : ""); 325 } 326 #endif 327 return fail; 328 } 329 330 /* 331 * Complete a scan of potential channels. 332 */ 333 void 334 ieee80211_end_scan(struct ifnet *ifp) 335 { 336 struct ieee80211com *ic = (void *)ifp; 337 struct ieee80211_node *ni, *nextbs, *selbs; 338 int i, fail; 339 340 ic->ic_flags &= ~IEEE80211_F_ASCAN; 341 ni = TAILQ_FIRST(&ic->ic_node); 342 343 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 344 /* XXX off stack? */ 345 u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)]; 346 /* 347 * The passive scan to look for existing AP's completed, 348 * select a channel to camp on. Identify the channels 349 * that already have one or more AP's and try to locate 350 * an unnoccupied one. If that fails, pick a random 351 * channel from the active set. 352 */ 353 for (; ni != NULL; ni = nextbs) { 354 ieee80211_ref_node(ni); 355 nextbs = TAILQ_NEXT(ni, ni_list); 356 setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan)); 357 ieee80211_free_node(ic, ni); 358 } 359 for (i = 0; i < IEEE80211_CHAN_MAX; i++) 360 if (isset(ic->ic_chan_active, i) && isclr(occupied, i)) 361 break; 362 if (i == IEEE80211_CHAN_MAX) { 363 fail = arc4random() & 3; /* random 0-3 */ 364 for (i = 0; i < IEEE80211_CHAN_MAX; i++) 365 if (isset(ic->ic_chan_active, i) && fail-- == 0) 366 break; 367 } 368 ieee80211_create_ibss(ic, &ic->ic_channels[i]); 369 return; 370 } 371 if (ni == NULL) { 372 IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__)); 373 notfound: 374 if (ic->ic_opmode == IEEE80211_M_IBSS && 375 (ic->ic_flags & IEEE80211_F_IBSSON) && 376 ic->ic_des_esslen != 0) { 377 ieee80211_create_ibss(ic, ic->ic_ibss_chan); 378 return; 379 } 380 /* 381 * Reset the list of channels to scan and start again. 382 */ 383 ieee80211_reset_scan(ifp); 384 ieee80211_next_scan(ifp); 385 return; 386 } 387 selbs = NULL; 388 if (ifp->if_flags & IFF_DEBUG) 389 if_printf(ifp, "\tmacaddr bssid chan rssi rate flag wep essid\n"); 390 for (; ni != NULL; ni = nextbs) { 391 ieee80211_ref_node(ni); 392 nextbs = TAILQ_NEXT(ni, ni_list); 393 if (ni->ni_fails) { 394 /* 395 * The configuration of the access points may change 396 * during my scan. So delete the entry for the AP 397 * and retry to associate if there is another beacon. 398 */ 399 if (ni->ni_fails++ > 2) 400 ieee80211_free_node(ic, ni); 401 continue; 402 } 403 if (ieee80211_match_bss(ic, ni) == 0) { 404 if (selbs == NULL) 405 selbs = ni; 406 else if (ni->ni_rssi > selbs->ni_rssi) { 407 ieee80211_unref_node(&selbs); 408 selbs = ni; 409 } else 410 ieee80211_unref_node(&ni); 411 } else { 412 ieee80211_unref_node(&ni); 413 } 414 } 415 if (selbs == NULL) 416 goto notfound; 417 (*ic->ic_node_copy)(ic, ic->ic_bss, selbs); 418 if (ic->ic_opmode == IEEE80211_M_IBSS) { 419 ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE | 420 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 421 if (ic->ic_bss->ni_rates.rs_nrates == 0) { 422 selbs->ni_fails++; 423 ieee80211_unref_node(&selbs); 424 goto notfound; 425 } 426 ieee80211_unref_node(&selbs); 427 /* 428 * Discard scan set; the nodes have a refcnt of zero 429 * and have not asked the driver to setup private 430 * node state. Let them be repopulated on demand either 431 * through transmission (ieee80211_find_txnode) or receipt 432 * of a probe response (to be added). 433 */ 434 ieee80211_free_allnodes(ic); 435 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 436 } else { 437 ieee80211_unref_node(&selbs); 438 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 439 } 440 } 441 442 int 443 ieee80211_get_rate(struct ieee80211com *ic) 444 { 445 u_int8_t (*rates)[IEEE80211_RATE_MAXSIZE]; 446 int rate; 447 448 rates = &ic->ic_bss->ni_rates.rs_rates; 449 450 if (ic->ic_fixed_rate != -1) 451 rate = (*rates)[ic->ic_fixed_rate]; 452 else if (ic->ic_state == IEEE80211_S_RUN) 453 rate = (*rates)[ic->ic_bss->ni_txrate]; 454 else 455 rate = 0; 456 457 return rate & IEEE80211_RATE_VAL; 458 } 459 460 static struct ieee80211_node * 461 ieee80211_node_alloc(struct ieee80211com *ic) 462 { 463 struct ieee80211_node *ni; 464 MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node), 465 M_80211_NODE, M_NOWAIT | M_ZERO); 466 return ni; 467 } 468 469 static void 470 ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni) 471 { 472 if (ni->ni_challenge != NULL) { 473 FREE(ni->ni_challenge, M_DEVBUF); 474 ni->ni_challenge = NULL; 475 } 476 FREE(ni, M_80211_NODE); 477 } 478 479 static void 480 ieee80211_node_copy(struct ieee80211com *ic, 481 struct ieee80211_node *dst, const struct ieee80211_node *src) 482 { 483 *dst = *src; 484 dst->ni_challenge = NULL; 485 } 486 487 static u_int8_t 488 ieee80211_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni) 489 { 490 return ni->ni_rssi; 491 } 492 493 static void 494 ieee80211_setup_node(struct ieee80211com *ic, 495 struct ieee80211_node *ni, u_int8_t *macaddr) 496 { 497 int hash; 498 ieee80211_node_critsec_decl(s); 499 500 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 501 hash = IEEE80211_NODE_HASH(macaddr); 502 ni->ni_refcnt = 1; /* mark referenced */ 503 ieee80211_node_critsec_begin(ic, s); 504 TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list); 505 LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash); 506 /* 507 * Note we don't enable the inactive timer when acting 508 * as a station. Nodes created in this mode represent 509 * AP's identified while scanning. If we time them out 510 * then several things happen: we can't return the data 511 * to users to show the list of AP's we encountered, and 512 * more importantly, we'll incorrectly deauthenticate 513 * ourself because the inactivity timer will kick us off. 514 */ 515 if (ic->ic_opmode != IEEE80211_M_STA) 516 ic->ic_inact_timer = IEEE80211_INACT_WAIT; 517 ieee80211_node_critsec_end(ic, s); 518 } 519 520 struct ieee80211_node * 521 ieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr) 522 { 523 struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic); 524 if (ni != NULL) 525 ieee80211_setup_node(ic, ni, macaddr); 526 else 527 ic->ic_stats.is_rx_nodealloc++; 528 return ni; 529 } 530 531 struct ieee80211_node * 532 ieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr) 533 { 534 struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic); 535 if (ni != NULL) { 536 ieee80211_setup_node(ic, ni, macaddr); 537 /* 538 * Inherit from ic_bss. 539 */ 540 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 541 ni->ni_chan = ic->ic_bss->ni_chan; 542 } else 543 ic->ic_stats.is_rx_nodealloc++; 544 return ni; 545 } 546 547 static struct ieee80211_node * 548 _ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr) 549 { 550 struct ieee80211_node *ni; 551 int hash; 552 553 #ifdef __FreeBSD__ 554 IEEE80211_NODE_LOCK_ASSERT(ic); 555 #endif /* __FreeBSD__ */ 556 557 hash = IEEE80211_NODE_HASH(macaddr); 558 LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) { 559 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 560 ieee80211_node_incref(ni); /* mark referenced */ 561 return ni; 562 } 563 } 564 return NULL; 565 } 566 567 struct ieee80211_node * 568 ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr) 569 { 570 struct ieee80211_node *ni; 571 ieee80211_node_critsec_decl(s); 572 573 ieee80211_node_critsec_begin(ic, s); 574 ni = _ieee80211_find_node(ic, macaddr); 575 ieee80211_node_critsec_end(ic, s); 576 return ni; 577 } 578 579 /* 580 * Return a reference to the appropriate node for sending 581 * a data frame. This handles node discovery in adhoc networks. 582 */ 583 struct ieee80211_node * 584 ieee80211_find_txnode(struct ieee80211com *ic, u_int8_t *macaddr) 585 { 586 struct ieee80211_node *ni; 587 ieee80211_node_critsec_decl(s); 588 589 /* 590 * The destination address should be in the node table 591 * unless we are operating in station mode or this is a 592 * multicast/broadcast frame. 593 */ 594 if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr)) 595 return ic->ic_bss; 596 597 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */ 598 ieee80211_node_critsec_begin(ic, s); 599 ni = _ieee80211_find_node(ic, macaddr); 600 ieee80211_node_critsec_end(ic, s); 601 if (ni == NULL && 602 (ic->ic_opmode == IEEE80211_M_IBSS || 603 ic->ic_opmode == IEEE80211_M_AHDEMO)) { 604 /* 605 * Fake up a node; this handles node discovery in 606 * adhoc mode. Note that for the driver's benefit 607 * we we treat this like an association so the driver 608 * has an opportunity to setup it's private state. 609 * 610 * XXX need better way to handle this; issue probe 611 * request so we can deduce rate set, etc. 612 */ 613 ni = ieee80211_dup_bss(ic, macaddr); 614 if (ni != NULL) { 615 /* XXX no rate negotiation; just dup */ 616 ni->ni_rates = ic->ic_bss->ni_rates; 617 if (ic->ic_newassoc) 618 (*ic->ic_newassoc)(ic, ni, 1); 619 } 620 } 621 return ni; 622 } 623 624 /* 625 * For some types of packet and for some operating modes, it is 626 * desirable to process a Rx packet using its sender's node-record 627 * instead of the BSS record, when that is possible. 628 * 629 * - AP mode: it is desirable to keep a node-record for every 630 * authenticated/associated station *in the BSS*. For future use, 631 * we also track neighboring APs, since they might belong to the 632 * same ESSID. 633 * 634 * - IBSS mode: it is desirable to keep a node-record for every 635 * station *in the BSS*. 636 * 637 * - monitor mode: it is desirable to keep a node-record for every 638 * sender, regardless of BSS. 639 * 640 * - STA mode: the only available node-record is the BSS record, 641 * ic->ic_bss. 642 * 643 * Of all the 802.11 Control packets, only the node-records for 644 * RTS packets node-record can be looked up. 645 * 646 * Return non-zero if the packet's node-record is kept, zero 647 * otherwise. 648 */ 649 static __inline int 650 ieee80211_needs_rxnode(struct ieee80211com *ic, struct ieee80211_frame *wh, 651 u_int8_t **bssid) 652 { 653 struct ieee80211_node *bss = ic->ic_bss; 654 int needsnode, rc = 0; 655 656 if (ic->ic_opmode == IEEE80211_M_STA) 657 return 0; 658 659 needsnode = (ic->ic_opmode == IEEE80211_M_MONITOR); 660 661 *bssid = NULL; 662 663 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 664 case IEEE80211_FC0_TYPE_CTL: 665 return (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 666 IEEE80211_FC0_SUBTYPE_RTS; 667 668 case IEEE80211_FC0_TYPE_MGT: 669 *bssid = wh->i_addr3; 670 rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid); 671 break; 672 case IEEE80211_FC0_TYPE_DATA: 673 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 674 case IEEE80211_FC1_DIR_NODS: 675 *bssid = wh->i_addr3; 676 if (ic->ic_opmode == IEEE80211_M_IBSS || 677 ic->ic_opmode == IEEE80211_M_AHDEMO) 678 rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid); 679 break; 680 case IEEE80211_FC1_DIR_TODS: 681 *bssid = wh->i_addr1; 682 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 683 rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid); 684 break; 685 case IEEE80211_FC1_DIR_FROMDS: 686 case IEEE80211_FC1_DIR_DSTODS: 687 *bssid = wh->i_addr2; 688 rc = (ic->ic_opmode == IEEE80211_M_HOSTAP); 689 break; 690 } 691 break; 692 } 693 return needsnode || rc; 694 } 695 696 struct ieee80211_node * 697 ieee80211_find_rxnode(struct ieee80211com *ic, struct ieee80211_frame *wh) 698 { 699 struct ieee80211_node *ni; 700 const static u_int8_t zero[IEEE80211_ADDR_LEN]; 701 u_int8_t *bssid; 702 ieee80211_node_critsec_decl(s); 703 704 if (!ieee80211_needs_rxnode(ic, wh, &bssid)) 705 return ieee80211_ref_node(ic->ic_bss); 706 707 ieee80211_node_critsec_begin(ic, s); 708 ni = _ieee80211_find_node(ic, wh->i_addr2); 709 ieee80211_node_critsec_end(ic, s); 710 711 if (ni == NULL) { 712 if (ic->ic_opmode != IEEE80211_M_HOSTAP) { 713 if ((ni = ieee80211_dup_bss(ic, wh->i_addr2)) != NULL) 714 IEEE80211_ADDR_COPY(ni->ni_bssid, 715 (bssid != NULL) ? bssid : zero); 716 717 /* XXX see remarks in ieee80211_find_txnode */ 718 if (ni != NULL) { 719 /* XXX no rate negotiation; just dup */ 720 ni->ni_rates = ic->ic_bss->ni_rates; 721 if (ic->ic_newassoc) 722 (*ic->ic_newassoc)(ic, ni, 1); 723 } 724 IEEE80211_DPRINTF(("%s: faked-up node %p for %s\n", 725 __func__, ni, ether_sprintf(wh->i_addr2))); 726 } 727 ni = ieee80211_ref_node((ni == NULL) ? ic->ic_bss : ni); 728 } 729 IASSERT(ni != NULL, ("%s: null node", __func__)); 730 return ni; 731 } 732 733 /* 734 * Like find but search based on the channel too. 735 */ 736 struct ieee80211_node * 737 ieee80211_lookup_node(struct ieee80211com *ic, 738 u_int8_t *macaddr, struct ieee80211_channel *chan) 739 { 740 struct ieee80211_node *ni; 741 int hash; 742 ieee80211_node_critsec_decl(s); 743 744 hash = IEEE80211_NODE_HASH(macaddr); 745 ieee80211_node_critsec_begin(ic, s); 746 LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) { 747 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && 748 ni->ni_chan == chan) { 749 ieee80211_node_incref(ni);/* mark referenced */ 750 break; 751 } 752 } 753 ieee80211_node_critsec_end(ic, s); 754 return ni; 755 } 756 757 static void 758 _ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni) 759 { 760 IASSERT(ni != ic->ic_bss, ("freeing bss node")); 761 762 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 763 TAILQ_REMOVE(&ic->ic_node, ni, ni_list); 764 LIST_REMOVE(ni, ni_hash); 765 if (!IF_IS_EMPTY(&ni->ni_savedq)) { 766 IF_PURGE(&ni->ni_savedq); 767 if (ic->ic_set_tim) 768 ic->ic_set_tim(ic, ni->ni_associd, 0); 769 } 770 if (TAILQ_EMPTY(&ic->ic_node)) 771 ic->ic_inact_timer = 0; 772 (*ic->ic_node_free)(ic, ni); 773 } 774 775 void 776 ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni) 777 { 778 ieee80211_node_critsec_decl(s); 779 780 IASSERT(ni != ic->ic_bss, ("freeing ic_bss")); 781 782 if (ieee80211_node_decref(ni) == 0) { 783 ieee80211_node_critsec_begin(ic, s); 784 _ieee80211_free_node(ic, ni); 785 ieee80211_node_critsec_end(ic, s); 786 } 787 } 788 789 void 790 ieee80211_free_allnodes(struct ieee80211com *ic) 791 { 792 struct ieee80211_node *ni; 793 ieee80211_node_critsec_decl(s); 794 795 ieee80211_node_critsec_begin(ic, s); 796 while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL) 797 _ieee80211_free_node(ic, ni); 798 ieee80211_node_critsec_end(ic, s); 799 } 800 801 /* 802 * Timeout inactive nodes. Note that we cannot hold the node 803 * lock while sending a frame as this would lead to a LOR. 804 * Instead we use a generation number to mark nodes that we've 805 * scanned and drop the lock and restart a scan if we have to 806 * time out a node. Since we are single-threaded by virtue of 807 * controlling the inactivity timer we can be sure this will 808 * process each node only once. 809 */ 810 void 811 ieee80211_timeout_nodes(struct ieee80211com *ic) 812 { 813 struct ieee80211_node *ni; 814 ieee80211_node_critsec_decl(s); 815 u_int gen = ic->ic_scangen++; /* NB: ok 'cuz single-threaded*/ 816 817 restart: 818 ieee80211_node_critsec_begin(ic, s); 819 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) { 820 if (ni->ni_scangen == gen) /* previously handled */ 821 continue; 822 ni->ni_scangen = gen; 823 if (++ni->ni_inact > ieee80211_inact_max) { 824 IEEE80211_DPRINTF(("station %s timed out " 825 "due to inactivity (%u secs)\n", 826 ether_sprintf(ni->ni_macaddr), 827 ni->ni_inact)); 828 /* 829 * Send a deauthenticate frame. 830 * 831 * Drop the node lock before sending the 832 * deauthentication frame in case the driver takes 833 * a lock, as this will result in a LOR between the 834 * node lock and the driver lock. 835 */ 836 ieee80211_node_critsec_end(ic, s); 837 IEEE80211_SEND_MGMT(ic, ni, 838 IEEE80211_FC0_SUBTYPE_DEAUTH, 839 IEEE80211_REASON_AUTH_EXPIRE); 840 ieee80211_free_node(ic, ni); 841 ic->ic_stats.is_node_timeout++; 842 goto restart; 843 } 844 } 845 if (!TAILQ_EMPTY(&ic->ic_node)) 846 ic->ic_inact_timer = IEEE80211_INACT_WAIT; 847 ieee80211_node_critsec_end(ic, s); 848 } 849 850 void 851 ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg) 852 { 853 struct ieee80211_node *ni; 854 ieee80211_node_critsec_decl(s); 855 856 ieee80211_node_critsec_begin(ic, s); 857 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) 858 (*f)(arg, ni); 859 ieee80211_node_critsec_end(ic, s); 860 } 861