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