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