ieee80211_node.c revision 120481
1/*- 2 * Copyright (c) 2001 Atsushi Onoe 3 * Copyright (c) 2002, 2003 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 120481 2003-09-26 16:52:12Z sam $"); 35 36#include "opt_inet.h" 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/mbuf.h> 41#include <sys/malloc.h> 42#include <sys/kernel.h> 43#include <sys/socket.h> 44#include <sys/sockio.h> 45#include <sys/endian.h> 46#include <sys/errno.h> 47#include <sys/bus.h> 48#include <sys/proc.h> 49#include <sys/sysctl.h> 50 51#include <machine/atomic.h> 52 53#include <net/if.h> 54#include <net/if_dl.h> 55#include <net/if_media.h> 56#include <net/if_arp.h> 57#include <net/ethernet.h> 58#include <net/if_llc.h> 59 60#include <net80211/ieee80211_var.h> 61 62#include <net/bpf.h> 63 64#ifdef INET 65#include <netinet/in.h> 66#include <netinet/if_ether.h> 67#endif 68 69static struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *); 70static void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *); 71static void ieee80211_node_copy(struct ieee80211com *, 72 struct ieee80211_node *, const struct ieee80211_node *); 73static u_int8_t ieee80211_node_getrssi(struct ieee80211com *, 74 struct ieee80211_node *); 75 76static void ieee80211_setup_node(struct ieee80211com *ic, 77 struct ieee80211_node *ni, u_int8_t *macaddr); 78static void _ieee80211_free_node(struct ieee80211com *, 79 struct ieee80211_node *); 80 81MALLOC_DEFINE(M_80211_NODE, "node", "802.11 node state"); 82 83void 84ieee80211_node_attach(struct ifnet *ifp) 85{ 86 struct ieee80211com *ic = (void *)ifp; 87 88 /* XXX need unit */ 89 mtx_init(&ic->ic_nodelock, ifp->if_name, "802.11 node table", MTX_DEF); 90 TAILQ_INIT(&ic->ic_node); 91 ic->ic_node_alloc = ieee80211_node_alloc; 92 ic->ic_node_free = ieee80211_node_free; 93 ic->ic_node_copy = ieee80211_node_copy; 94 ic->ic_node_getrssi = ieee80211_node_getrssi; 95} 96 97void 98ieee80211_node_lateattach(struct ifnet *ifp) 99{ 100 struct ieee80211com *ic = (void *)ifp; 101 102 ic->ic_bss = (*ic->ic_node_alloc)(ic); 103 KASSERT(ic->ic_bss != NULL, ("unable to setup inital BSS node")); 104 ic->ic_bss->ni_chan = IEEE80211_CHAN_ANYC; 105} 106 107void 108ieee80211_node_detach(struct ifnet *ifp) 109{ 110 struct ieee80211com *ic = (void *)ifp; 111 112 if (ic->ic_bss != NULL) 113 (*ic->ic_node_free)(ic, ic->ic_bss); 114 ieee80211_free_allnodes(ic); 115 mtx_destroy(&ic->ic_nodelock); 116} 117 118/* 119 * AP scanning support. 120 */ 121 122/* 123 * Initialize the active channel set based on the set 124 * of available channels and the current PHY mode. 125 */ 126static void 127ieee80211_reset_scan(struct ifnet *ifp) 128{ 129 struct ieee80211com *ic = (void *)ifp; 130 131 memcpy(ic->ic_chan_scan, ic->ic_chan_active, 132 sizeof(ic->ic_chan_active)); 133 /* NB: hack, setup so next_scan starts with the first channel */ 134 if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC) 135 ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX]; 136} 137 138/* 139 * Begin an active scan. 140 */ 141void 142ieee80211_begin_scan(struct ifnet *ifp) 143{ 144 struct ieee80211com *ic = (void *)ifp; 145 146 /* 147 * In all but hostap mode scanning starts off in 148 * an active mode before switching to passive. 149 */ 150 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 151 ic->ic_flags |= IEEE80211_F_ASCAN; 152 if (ifp->if_flags & IFF_DEBUG) 153 if_printf(ifp, "begin %s scan\n", 154 (ic->ic_flags & IEEE80211_F_ASCAN) ? 155 "active" : "passive"); 156 /* 157 * Clear scan state and flush any previously seen 158 * AP's. Note that the latter assumes we don't act 159 * as both an AP and a station, otherwise we'll 160 * potentially flush state of stations associated 161 * with us. 162 */ 163 ieee80211_reset_scan(ifp); 164 ieee80211_free_allnodes(ic); 165 166 /* Scan the next channel. */ 167 ieee80211_next_scan(ifp); 168} 169 170/* 171 * Switch to the next channel marked for scanning. 172 */ 173void 174ieee80211_next_scan(struct ifnet *ifp) 175{ 176 struct ieee80211com *ic = (void *)ifp; 177 struct ieee80211_channel *chan; 178 179 chan = ic->ic_bss->ni_chan; 180 for (;;) { 181 if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX]) 182 chan = &ic->ic_channels[0]; 183 if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) { 184 /* 185 * Honor channels marked passive-only 186 * during an active scan. 187 */ 188 if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 || 189 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) 190 break; 191 } 192 if (chan == ic->ic_bss->ni_chan) { 193 ieee80211_end_scan(ifp); 194 return; 195 } 196 } 197 clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan)); 198 IEEE80211_DPRINTF(("ieee80211_next_scan: chan %d->%d\n", 199 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan), 200 ieee80211_chan2ieee(ic, chan))); 201 ic->ic_bss->ni_chan = chan; 202 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 203} 204 205void 206ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 207{ 208 struct ieee80211_node *ni; 209 struct ifnet *ifp = &ic->ic_if; 210 211 ni = ic->ic_bss; 212 if (ifp->if_flags & IFF_DEBUG) 213 if_printf(ifp, "creating ibss\n"); 214 ic->ic_flags |= IEEE80211_F_SIBSS; 215 ni->ni_chan = chan; 216 ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)]; 217 IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr); 218 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 219 if (ic->ic_opmode == IEEE80211_M_IBSS) 220 ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */ 221 ni->ni_esslen = ic->ic_des_esslen; 222 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 223 ni->ni_rssi = 0; 224 ni->ni_rstamp = 0; 225 memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp)); 226 ni->ni_intval = ic->ic_lintval; 227 ni->ni_capinfo = IEEE80211_CAPINFO_IBSS; 228 if (ic->ic_flags & IEEE80211_F_WEPON) 229 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 230 if (ic->ic_phytype == IEEE80211_T_FH) { 231 ni->ni_fhdwell = 200; /* XXX */ 232 ni->ni_fhindex = 1; 233 } 234 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 235} 236 237/* 238 * Complete a scan of potential channels. 239 */ 240void 241ieee80211_end_scan(struct ifnet *ifp) 242{ 243 struct ieee80211com *ic = (void *)ifp; 244 struct ieee80211_node *ni, *nextbs, *selbs; 245 u_int8_t rate; 246 int i, fail; 247 248 ic->ic_flags &= ~IEEE80211_F_ASCAN; 249 ni = TAILQ_FIRST(&ic->ic_node); 250 251 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 252 /* XXX off stack? */ 253 u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)]; 254 /* 255 * The passive scan to look for existing AP's completed, 256 * select a channel to camp on. Identify the channels 257 * that already have one or more AP's and try to locate 258 * an unnoccupied one. If that fails, pick a random 259 * channel from the active set. 260 */ 261 for (; ni != NULL; ni = nextbs) { 262 ieee80211_ref_node(ni); 263 nextbs = TAILQ_NEXT(ni, ni_list); 264 setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan)); 265 ieee80211_free_node(ic, ni); 266 } 267 for (i = 0; i < IEEE80211_CHAN_MAX; i++) 268 if (isset(ic->ic_chan_active, i) && isclr(occupied, i)) 269 break; 270 if (i == IEEE80211_CHAN_MAX) { 271 fail = arc4random() & 3; /* random 0-3 */ 272 for (i = 0; i < IEEE80211_CHAN_MAX; i++) 273 if (isset(ic->ic_chan_active, i) && fail-- == 0) 274 break; 275 } 276 ieee80211_create_ibss(ic, &ic->ic_channels[i]); 277 return; 278 } 279 if (ni == NULL) { 280 IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__)); 281 notfound: 282 if (ic->ic_opmode == IEEE80211_M_IBSS && 283 (ic->ic_flags & IEEE80211_F_IBSSON) && 284 ic->ic_des_esslen != 0) { 285 ieee80211_create_ibss(ic, ic->ic_ibss_chan); 286 return; 287 } 288 /* 289 * Reset the list of channels to scan and start again. 290 */ 291 ieee80211_reset_scan(ifp); 292 ieee80211_next_scan(ifp); 293 return; 294 } 295 selbs = NULL; 296 if (ifp->if_flags & IFF_DEBUG) 297 if_printf(ifp, "\tmacaddr bssid chan rssi rate flag wep essid\n"); 298 for (; ni != NULL; ni = nextbs) { 299 ieee80211_ref_node(ni); 300 nextbs = TAILQ_NEXT(ni, ni_list); 301 if (ni->ni_fails) { 302 /* 303 * The configuration of the access points may change 304 * during my scan. So delete the entry for the AP 305 * and retry to associate if there is another beacon. 306 */ 307 if (ni->ni_fails++ > 2) 308 ieee80211_free_node(ic, ni); 309 continue; 310 } 311 fail = 0; 312 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 313 fail |= 0x01; 314 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 315 ni->ni_chan != ic->ic_des_chan) 316 fail |= 0x01; 317 if (ic->ic_opmode == IEEE80211_M_IBSS) { 318 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 319 fail |= 0x02; 320 } else { 321 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 322 fail |= 0x02; 323 } 324 if (ic->ic_flags & IEEE80211_F_WEPON) { 325 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 326 fail |= 0x04; 327 } else { 328 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 329 fail |= 0x04; 330 } 331 rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO); 332 if (rate & IEEE80211_RATE_BASIC) 333 fail |= 0x08; 334 if (ic->ic_des_esslen != 0 && 335 (ni->ni_esslen != ic->ic_des_esslen || 336 memcmp(ni->ni_essid, ic->ic_des_essid, 337 ic->ic_des_esslen != 0))) 338 fail |= 0x10; 339 if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 340 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 341 fail |= 0x20; 342 if (ifp->if_flags & IFF_DEBUG) { 343 printf(" %c %s", fail ? '-' : '+', 344 ether_sprintf(ni->ni_macaddr)); 345 printf(" %s%c", ether_sprintf(ni->ni_bssid), 346 fail & 0x20 ? '!' : ' '); 347 printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan), 348 fail & 0x01 ? '!' : ' '); 349 printf(" %+4d", ni->ni_rssi); 350 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 351 fail & 0x08 ? '!' : ' '); 352 printf(" %4s%c", 353 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 354 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 355 "????", 356 fail & 0x02 ? '!' : ' '); 357 printf(" %3s%c ", 358 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 359 "wep" : "no", 360 fail & 0x04 ? '!' : ' '); 361 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 362 printf("%s\n", fail & 0x10 ? "!" : ""); 363 } 364 if (!fail) { 365 if (selbs == NULL) 366 selbs = ni; 367 else if (ni->ni_rssi > selbs->ni_rssi) { 368 ieee80211_unref_node(&selbs); 369 selbs = ni; 370 } else 371 ieee80211_unref_node(&ni); 372 } else { 373 ieee80211_unref_node(&ni); 374 } 375 } 376 if (selbs == NULL) 377 goto notfound; 378 (*ic->ic_node_copy)(ic, ic->ic_bss, selbs); 379 if (ic->ic_opmode == IEEE80211_M_IBSS) { 380 ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE | 381 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 382 if (ic->ic_bss->ni_rates.rs_nrates == 0) { 383 selbs->ni_fails++; 384 ieee80211_unref_node(&selbs); 385 goto notfound; 386 } 387 ieee80211_unref_node(&selbs); 388 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 389 } else { 390 ieee80211_unref_node(&selbs); 391 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 392 } 393} 394 395static struct ieee80211_node * 396ieee80211_node_alloc(struct ieee80211com *ic) 397{ 398 return malloc(sizeof(struct ieee80211_node), M_80211_NODE, 399 M_NOWAIT | M_ZERO); 400} 401 402static void 403ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni) 404{ 405 free(ni, M_80211_NODE); 406} 407 408static void 409ieee80211_node_copy(struct ieee80211com *ic, 410 struct ieee80211_node *dst, const struct ieee80211_node *src) 411{ 412 *dst = *src; 413} 414 415static u_int8_t 416ieee80211_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni) 417{ 418 return ni->ni_rssi; 419} 420 421static void 422ieee80211_setup_node(struct ieee80211com *ic, 423 struct ieee80211_node *ni, u_int8_t *macaddr) 424{ 425 int hash; 426 427 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 428 hash = IEEE80211_NODE_HASH(macaddr); 429 ni->ni_refcnt = 1; /* mark referenced */ 430 mtx_lock(&ic->ic_nodelock); 431 TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list); 432 LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash); 433 /* 434 * Note we don't enable the inactive timer when acting 435 * as a station. Nodes created in this mode represent 436 * AP's identified while scanning. If we time them out 437 * then several things happen: we can't return the data 438 * to users to show the list of AP's we encountered, and 439 * more importantly, we'll incorrectly deauthenticate 440 * ourself because the inactivity timer will kick us off. 441 */ 442 if (ic->ic_opmode != IEEE80211_M_STA) 443 ic->ic_inact_timer = IEEE80211_INACT_WAIT; 444 mtx_unlock(&ic->ic_nodelock); 445} 446 447struct ieee80211_node * 448ieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr) 449{ 450 struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic); 451 if (ni != NULL) 452 ieee80211_setup_node(ic, ni, macaddr); 453 return ni; 454} 455 456struct ieee80211_node * 457ieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr) 458{ 459 struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic); 460 if (ni != NULL) { 461 memcpy(ni, ic->ic_bss, sizeof(struct ieee80211_node)); 462 ieee80211_setup_node(ic, ni, macaddr); 463 } 464 return ni; 465} 466 467struct ieee80211_node * 468ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr) 469{ 470 struct ieee80211_node *ni; 471 int hash; 472 473 hash = IEEE80211_NODE_HASH(macaddr); 474 mtx_lock(&ic->ic_nodelock); 475 LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) { 476 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 477 atomic_add_int(&ni->ni_refcnt, 1); /* mark referenced */ 478 break; 479 } 480 } 481 mtx_unlock(&ic->ic_nodelock); 482 return ni; 483} 484 485/* 486 * Like find but search based on the channel too. 487 */ 488struct ieee80211_node * 489ieee80211_lookup_node(struct ieee80211com *ic, 490 u_int8_t *macaddr, struct ieee80211_channel *chan) 491{ 492 struct ieee80211_node *ni; 493 int hash; 494 495 hash = IEEE80211_NODE_HASH(macaddr); 496 mtx_lock(&ic->ic_nodelock); 497 LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) { 498 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && ni->ni_chan == chan) { 499 atomic_add_int(&ni->ni_refcnt, 1);/* mark referenced */ 500 break; 501 } 502 } 503 mtx_unlock(&ic->ic_nodelock); 504 return ni; 505} 506 507static void 508_ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni) 509{ 510 KASSERT(ni != ic->ic_bss, ("freeing bss node")); 511 512 TAILQ_REMOVE(&ic->ic_node, ni, ni_list); 513 LIST_REMOVE(ni, ni_hash); 514 if (TAILQ_EMPTY(&ic->ic_node)) 515 ic->ic_inact_timer = 0; 516 (*ic->ic_node_free)(ic, ni); 517} 518 519void 520ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni) 521{ 522 KASSERT(ni != ic->ic_bss, ("freeing ic_bss")); 523 524 /* XXX need equivalent of atomic_dec_and_test */ 525 atomic_subtract_int(&ni->ni_refcnt, 1); 526 if (atomic_cmpset_int(&ni->ni_refcnt, 0, 1)) { 527 mtx_lock(&ic->ic_nodelock); 528 _ieee80211_free_node(ic, ni); 529 mtx_unlock(&ic->ic_nodelock); 530 } 531} 532 533void 534ieee80211_free_allnodes(struct ieee80211com *ic) 535{ 536 struct ieee80211_node *ni; 537 538 mtx_lock(&ic->ic_nodelock); 539 while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL) 540 _ieee80211_free_node(ic, ni); 541 mtx_unlock(&ic->ic_nodelock); 542} 543 544void 545ieee80211_timeout_nodes(struct ieee80211com *ic) 546{ 547 struct ieee80211_node *ni, *nextbs; 548 549 mtx_lock(&ic->ic_nodelock); 550 for (ni = TAILQ_FIRST(&ic->ic_node); ni != NULL;) { 551 if (++ni->ni_inact > IEEE80211_INACT_MAX) { 552 IEEE80211_DPRINTF(("station %s timed out " 553 "due to inactivity (%u secs)\n", 554 ether_sprintf(ni->ni_macaddr), 555 ni->ni_inact)); 556 nextbs = TAILQ_NEXT(ni, ni_list); 557 /* 558 * Send a deauthenticate frame. 559 */ 560 IEEE80211_SEND_MGMT(ic, ni, 561 IEEE80211_FC0_SUBTYPE_DEAUTH, 562 IEEE80211_REASON_AUTH_EXPIRE); 563 ieee80211_free_node(ic, ni); 564 ni = nextbs; 565 } else 566 ni = TAILQ_NEXT(ni, ni_list); 567 } 568 if (!TAILQ_EMPTY(&ic->ic_node)) 569 ic->ic_inact_timer = IEEE80211_INACT_WAIT; 570 mtx_unlock(&ic->ic_nodelock); 571} 572 573void 574ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg) 575{ 576 struct ieee80211_node *ni; 577 578 mtx_lock(&ic->ic_nodelock); 579 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) 580 (*f)(arg, ni); 581 mtx_unlock(&ic->ic_nodelock); 582} 583