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