Home | History | Annotate | Line # | Download | only in net80211
ieee80211_node.c revision 1.1.1.5
      1      1.1  dyoung /*-
      2      1.1  dyoung  * Copyright (c) 2001 Atsushi Onoe
      3  1.1.1.4  dyoung  * Copyright (c) 2002-2005 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.5  dyoung __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_node.c,v 1.48 2005/07/06 01:51:44 sam Exp $");
     35      1.1  dyoung 
     36      1.1  dyoung #include <sys/param.h>
     37      1.1  dyoung #include <sys/systm.h>
     38      1.1  dyoung #include <sys/mbuf.h>
     39      1.1  dyoung #include <sys/malloc.h>
     40      1.1  dyoung #include <sys/kernel.h>
     41      1.1  dyoung 
     42  1.1.1.4  dyoung #include <sys/socket.h>
     43      1.1  dyoung 
     44      1.1  dyoung #include <net/if.h>
     45      1.1  dyoung #include <net/if_media.h>
     46      1.1  dyoung #include <net/ethernet.h>
     47      1.1  dyoung 
     48      1.1  dyoung #include <net80211/ieee80211_var.h>
     49      1.1  dyoung 
     50      1.1  dyoung #include <net/bpf.h>
     51      1.1  dyoung 
     52  1.1.1.5  dyoung /*
     53  1.1.1.5  dyoung  * Association id's are managed with a bit vector.
     54  1.1.1.5  dyoung  */
     55  1.1.1.5  dyoung #define	IEEE80211_AID_SET(b, w) \
     56  1.1.1.5  dyoung 	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
     57  1.1.1.5  dyoung #define	IEEE80211_AID_CLR(b, w) \
     58  1.1.1.5  dyoung 	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
     59  1.1.1.5  dyoung #define	IEEE80211_AID_ISSET(b, w) \
     60  1.1.1.5  dyoung 	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
     61  1.1.1.5  dyoung 
     62  1.1.1.4  dyoung static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
     63  1.1.1.4  dyoung static void node_cleanup(struct ieee80211_node *);
     64  1.1.1.4  dyoung static void node_free(struct ieee80211_node *);
     65  1.1.1.4  dyoung static u_int8_t node_getrssi(const struct ieee80211_node *);
     66  1.1.1.4  dyoung 
     67  1.1.1.4  dyoung static void ieee80211_setup_node(struct ieee80211_node_table *,
     68  1.1.1.4  dyoung 		struct ieee80211_node *, const u_int8_t *);
     69  1.1.1.4  dyoung static void _ieee80211_free_node(struct ieee80211_node *);
     70  1.1.1.4  dyoung static void ieee80211_free_allnodes(struct ieee80211_node_table *);
     71  1.1.1.4  dyoung 
     72  1.1.1.4  dyoung static void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
     73  1.1.1.4  dyoung static void ieee80211_timeout_stations(struct ieee80211_node_table *);
     74  1.1.1.4  dyoung 
     75  1.1.1.4  dyoung static void ieee80211_set_tim(struct ieee80211com *,
     76  1.1.1.4  dyoung 		struct ieee80211_node *, int set);
     77  1.1.1.4  dyoung 
     78  1.1.1.4  dyoung static void ieee80211_node_table_init(struct ieee80211com *ic,
     79  1.1.1.4  dyoung 	struct ieee80211_node_table *nt, const char *name, int inact,
     80  1.1.1.4  dyoung 	void (*timeout)(struct ieee80211_node_table *));
     81  1.1.1.4  dyoung static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
     82      1.1  dyoung 
     83  1.1.1.3  dyoung MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
     84  1.1.1.2  dyoung 
     85      1.1  dyoung void
     86  1.1.1.4  dyoung ieee80211_node_attach(struct ieee80211com *ic)
     87      1.1  dyoung {
     88      1.1  dyoung 
     89  1.1.1.4  dyoung 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
     90  1.1.1.4  dyoung 		IEEE80211_INACT_INIT, ieee80211_timeout_stations);
     91  1.1.1.4  dyoung 	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
     92  1.1.1.4  dyoung 		IEEE80211_INACT_SCAN, ieee80211_timeout_scan_candidates);
     93  1.1.1.4  dyoung 
     94  1.1.1.4  dyoung 	ic->ic_node_alloc = node_alloc;
     95  1.1.1.4  dyoung 	ic->ic_node_free = node_free;
     96  1.1.1.4  dyoung 	ic->ic_node_cleanup = node_cleanup;
     97  1.1.1.4  dyoung 	ic->ic_node_getrssi = node_getrssi;
     98  1.1.1.4  dyoung 
     99  1.1.1.4  dyoung 	/* default station inactivity timer setings */
    100  1.1.1.4  dyoung 	ic->ic_inact_init = IEEE80211_INACT_INIT;
    101  1.1.1.4  dyoung 	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
    102  1.1.1.4  dyoung 	ic->ic_inact_run = IEEE80211_INACT_RUN;
    103  1.1.1.4  dyoung 	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
    104  1.1.1.4  dyoung 
    105  1.1.1.4  dyoung 	/* XXX defer */
    106  1.1.1.4  dyoung 	if (ic->ic_max_aid == 0)
    107  1.1.1.4  dyoung 		ic->ic_max_aid = IEEE80211_AID_DEF;
    108  1.1.1.4  dyoung 	else if (ic->ic_max_aid > IEEE80211_AID_MAX)
    109  1.1.1.4  dyoung 		ic->ic_max_aid = IEEE80211_AID_MAX;
    110  1.1.1.4  dyoung 	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
    111  1.1.1.4  dyoung 		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
    112  1.1.1.4  dyoung 		M_DEVBUF, M_NOWAIT | M_ZERO);
    113  1.1.1.4  dyoung 	if (ic->ic_aid_bitmap == NULL) {
    114  1.1.1.4  dyoung 		/* XXX no way to recover */
    115  1.1.1.4  dyoung 		printf("%s: no memory for AID bitmap!\n", __func__);
    116  1.1.1.4  dyoung 		ic->ic_max_aid = 0;
    117  1.1.1.4  dyoung 	}
    118  1.1.1.4  dyoung 
    119  1.1.1.4  dyoung 	/* XXX defer until using hostap/ibss mode */
    120  1.1.1.4  dyoung 	ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
    121  1.1.1.4  dyoung 	MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
    122  1.1.1.4  dyoung 		M_DEVBUF, M_NOWAIT | M_ZERO);
    123  1.1.1.4  dyoung 	if (ic->ic_tim_bitmap == NULL) {
    124  1.1.1.4  dyoung 		/* XXX no way to recover */
    125  1.1.1.4  dyoung 		printf("%s: no memory for TIM bitmap!\n", __func__);
    126  1.1.1.4  dyoung 	}
    127  1.1.1.4  dyoung 	ic->ic_set_tim = ieee80211_set_tim;	/* NB: driver should override */
    128      1.1  dyoung }
    129      1.1  dyoung 
    130      1.1  dyoung void
    131  1.1.1.4  dyoung ieee80211_node_lateattach(struct ieee80211com *ic)
    132      1.1  dyoung {
    133  1.1.1.3  dyoung 	struct ieee80211_node *ni;
    134  1.1.1.4  dyoung 	struct ieee80211_rsnparms *rsn;
    135      1.1  dyoung 
    136  1.1.1.4  dyoung 	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
    137  1.1.1.3  dyoung 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
    138  1.1.1.4  dyoung 	/*
    139  1.1.1.4  dyoung 	 * Setup "global settings" in the bss node so that
    140  1.1.1.4  dyoung 	 * each new station automatically inherits them.
    141  1.1.1.4  dyoung 	 */
    142  1.1.1.4  dyoung 	rsn = &ni->ni_rsn;
    143  1.1.1.4  dyoung 	/* WEP, TKIP, and AES-CCM are always supported */
    144  1.1.1.4  dyoung 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
    145  1.1.1.4  dyoung 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
    146  1.1.1.4  dyoung 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
    147  1.1.1.4  dyoung 	if (ic->ic_caps & IEEE80211_C_AES)
    148  1.1.1.4  dyoung 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
    149  1.1.1.4  dyoung 	if (ic->ic_caps & IEEE80211_C_CKIP)
    150  1.1.1.4  dyoung 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
    151  1.1.1.4  dyoung 	/*
    152  1.1.1.4  dyoung 	 * Default unicast cipher to WEP for 802.1x use.  If
    153  1.1.1.4  dyoung 	 * WPA is enabled the management code will set these
    154  1.1.1.4  dyoung 	 * values to reflect.
    155  1.1.1.4  dyoung 	 */
    156  1.1.1.4  dyoung 	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
    157  1.1.1.4  dyoung 	rsn->rsn_ucastkeylen = 104 / NBBY;
    158  1.1.1.4  dyoung 	/*
    159  1.1.1.4  dyoung 	 * WPA says the multicast cipher is the lowest unicast
    160  1.1.1.4  dyoung 	 * cipher supported.  But we skip WEP which would
    161  1.1.1.4  dyoung 	 * otherwise be used based on this criteria.
    162  1.1.1.4  dyoung 	 */
    163  1.1.1.4  dyoung 	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
    164  1.1.1.4  dyoung 	rsn->rsn_mcastkeylen = 128 / NBBY;
    165  1.1.1.4  dyoung 
    166  1.1.1.4  dyoung 	/*
    167  1.1.1.4  dyoung 	 * We support both WPA-PSK and 802.1x; the one used
    168  1.1.1.4  dyoung 	 * is determined by the authentication mode and the
    169  1.1.1.4  dyoung 	 * setting of the PSK state.
    170  1.1.1.4  dyoung 	 */
    171  1.1.1.4  dyoung 	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
    172  1.1.1.4  dyoung 	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
    173  1.1.1.4  dyoung 
    174  1.1.1.4  dyoung 	ic->ic_bss = ieee80211_ref_node(ni);		/* hold reference */
    175  1.1.1.4  dyoung 	ic->ic_auth = ieee80211_authenticator_get(ni->ni_authmode);
    176      1.1  dyoung }
    177      1.1  dyoung 
    178      1.1  dyoung void
    179  1.1.1.4  dyoung ieee80211_node_detach(struct ieee80211com *ic)
    180      1.1  dyoung {
    181      1.1  dyoung 
    182  1.1.1.4  dyoung 	if (ic->ic_bss != NULL) {
    183  1.1.1.4  dyoung 		ieee80211_free_node(ic->ic_bss);
    184  1.1.1.4  dyoung 		ic->ic_bss = NULL;
    185  1.1.1.4  dyoung 	}
    186  1.1.1.4  dyoung 	ieee80211_node_table_cleanup(&ic->ic_scan);
    187  1.1.1.4  dyoung 	ieee80211_node_table_cleanup(&ic->ic_sta);
    188  1.1.1.4  dyoung 	if (ic->ic_aid_bitmap != NULL) {
    189  1.1.1.4  dyoung 		FREE(ic->ic_aid_bitmap, M_DEVBUF);
    190  1.1.1.4  dyoung 		ic->ic_aid_bitmap = NULL;
    191  1.1.1.4  dyoung 	}
    192  1.1.1.4  dyoung 	if (ic->ic_tim_bitmap != NULL) {
    193  1.1.1.4  dyoung 		FREE(ic->ic_tim_bitmap, M_DEVBUF);
    194  1.1.1.4  dyoung 		ic->ic_tim_bitmap = NULL;
    195  1.1.1.4  dyoung 	}
    196  1.1.1.4  dyoung }
    197  1.1.1.4  dyoung 
    198  1.1.1.4  dyoung /*
    199  1.1.1.4  dyoung  * Port authorize/unauthorize interfaces for use by an authenticator.
    200  1.1.1.4  dyoung  */
    201  1.1.1.4  dyoung 
    202  1.1.1.4  dyoung void
    203  1.1.1.4  dyoung ieee80211_node_authorize(struct ieee80211com *ic, struct ieee80211_node *ni)
    204  1.1.1.4  dyoung {
    205  1.1.1.4  dyoung 	ni->ni_flags |= IEEE80211_NODE_AUTH;
    206  1.1.1.4  dyoung 	ni->ni_inact_reload = ic->ic_inact_run;
    207  1.1.1.4  dyoung }
    208  1.1.1.4  dyoung 
    209  1.1.1.4  dyoung void
    210  1.1.1.4  dyoung ieee80211_node_unauthorize(struct ieee80211com *ic, struct ieee80211_node *ni)
    211  1.1.1.4  dyoung {
    212  1.1.1.4  dyoung 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
    213  1.1.1.4  dyoung }
    214  1.1.1.4  dyoung 
    215  1.1.1.4  dyoung /*
    216  1.1.1.4  dyoung  * Set/change the channel.  The rate set is also updated as
    217  1.1.1.4  dyoung  * to insure a consistent view by drivers.
    218  1.1.1.4  dyoung  */
    219  1.1.1.4  dyoung static __inline void
    220  1.1.1.4  dyoung ieee80211_set_chan(struct ieee80211com *ic,
    221  1.1.1.4  dyoung 	struct ieee80211_node *ni, struct ieee80211_channel *chan)
    222  1.1.1.4  dyoung {
    223  1.1.1.4  dyoung 	ni->ni_chan = chan;
    224  1.1.1.4  dyoung 	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
    225      1.1  dyoung }
    226      1.1  dyoung 
    227      1.1  dyoung /*
    228      1.1  dyoung  * AP scanning support.
    229      1.1  dyoung  */
    230      1.1  dyoung 
    231  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG
    232  1.1.1.4  dyoung static void
    233  1.1.1.4  dyoung dump_chanlist(const u_char chans[])
    234  1.1.1.4  dyoung {
    235  1.1.1.4  dyoung 	const char *sep;
    236  1.1.1.4  dyoung 	int i;
    237  1.1.1.4  dyoung 
    238  1.1.1.4  dyoung 	sep = " ";
    239  1.1.1.4  dyoung 	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
    240  1.1.1.4  dyoung 		if (isset(chans, i)) {
    241  1.1.1.4  dyoung 			printf("%s%u", sep, i);
    242  1.1.1.4  dyoung 			sep = ", ";
    243  1.1.1.4  dyoung 		}
    244  1.1.1.4  dyoung }
    245  1.1.1.4  dyoung #endif /* IEEE80211_DEBUG */
    246  1.1.1.4  dyoung 
    247      1.1  dyoung /*
    248  1.1.1.4  dyoung  * Initialize the channel set to scan based on the
    249      1.1  dyoung  * of available channels and the current PHY mode.
    250      1.1  dyoung  */
    251      1.1  dyoung static void
    252  1.1.1.4  dyoung ieee80211_reset_scan(struct ieee80211com *ic)
    253      1.1  dyoung {
    254      1.1  dyoung 
    255  1.1.1.4  dyoung 	/* XXX ic_des_chan should be handled with ic_chan_active */
    256  1.1.1.4  dyoung 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
    257  1.1.1.4  dyoung 		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
    258  1.1.1.4  dyoung 		setbit(ic->ic_chan_scan,
    259  1.1.1.4  dyoung 			ieee80211_chan2ieee(ic, ic->ic_des_chan));
    260  1.1.1.4  dyoung 	} else
    261  1.1.1.4  dyoung 		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
    262  1.1.1.4  dyoung 			sizeof(ic->ic_chan_active));
    263      1.1  dyoung 	/* NB: hack, setup so next_scan starts with the first channel */
    264      1.1  dyoung 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
    265  1.1.1.4  dyoung 		ieee80211_set_chan(ic, ic->ic_bss,
    266  1.1.1.4  dyoung 			&ic->ic_channels[IEEE80211_CHAN_MAX]);
    267  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG
    268  1.1.1.4  dyoung 	if (ieee80211_msg_scan(ic)) {
    269  1.1.1.4  dyoung 		printf("%s: scan set:", __func__);
    270  1.1.1.4  dyoung 		dump_chanlist(ic->ic_chan_scan);
    271  1.1.1.4  dyoung 		printf(" start chan %u\n",
    272  1.1.1.4  dyoung 			ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
    273  1.1.1.4  dyoung 	}
    274  1.1.1.4  dyoung #endif /* IEEE80211_DEBUG */
    275      1.1  dyoung }
    276      1.1  dyoung 
    277      1.1  dyoung /*
    278      1.1  dyoung  * Begin an active scan.
    279      1.1  dyoung  */
    280      1.1  dyoung void
    281  1.1.1.4  dyoung ieee80211_begin_scan(struct ieee80211com *ic, int reset)
    282      1.1  dyoung {
    283      1.1  dyoung 
    284  1.1.1.4  dyoung 	ic->ic_scan.nt_scangen++;
    285      1.1  dyoung 	/*
    286      1.1  dyoung 	 * In all but hostap mode scanning starts off in
    287      1.1  dyoung 	 * an active mode before switching to passive.
    288      1.1  dyoung 	 */
    289  1.1.1.2  dyoung 	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
    290      1.1  dyoung 		ic->ic_flags |= IEEE80211_F_ASCAN;
    291  1.1.1.2  dyoung 		ic->ic_stats.is_scan_active++;
    292  1.1.1.2  dyoung 	} else
    293  1.1.1.2  dyoung 		ic->ic_stats.is_scan_passive++;
    294  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    295  1.1.1.4  dyoung 		"begin %s scan in %s mode, scangen %u\n",
    296  1.1.1.4  dyoung 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
    297  1.1.1.4  dyoung 		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
    298  1.1.1.4  dyoung 	/*
    299  1.1.1.4  dyoung 	 * Clear scan state and flush any previously seen AP's.
    300      1.1  dyoung 	 */
    301  1.1.1.4  dyoung 	ieee80211_reset_scan(ic);
    302  1.1.1.4  dyoung 	if (reset)
    303  1.1.1.4  dyoung 		ieee80211_free_allnodes(&ic->ic_scan);
    304  1.1.1.4  dyoung 
    305  1.1.1.4  dyoung 	ic->ic_flags |= IEEE80211_F_SCAN;
    306      1.1  dyoung 
    307      1.1  dyoung 	/* Scan the next channel. */
    308  1.1.1.4  dyoung 	ieee80211_next_scan(ic);
    309      1.1  dyoung }
    310      1.1  dyoung 
    311      1.1  dyoung /*
    312      1.1  dyoung  * Switch to the next channel marked for scanning.
    313      1.1  dyoung  */
    314  1.1.1.4  dyoung int
    315  1.1.1.4  dyoung ieee80211_next_scan(struct ieee80211com *ic)
    316      1.1  dyoung {
    317      1.1  dyoung 	struct ieee80211_channel *chan;
    318      1.1  dyoung 
    319  1.1.1.4  dyoung 	/*
    320  1.1.1.4  dyoung 	 * Insure any previous mgt frame timeouts don't fire.
    321  1.1.1.4  dyoung 	 * This assumes the driver does the right thing in
    322  1.1.1.4  dyoung 	 * flushing anything queued in the driver and below.
    323  1.1.1.4  dyoung 	 */
    324  1.1.1.4  dyoung 	ic->ic_mgt_timer = 0;
    325  1.1.1.4  dyoung 
    326      1.1  dyoung 	chan = ic->ic_bss->ni_chan;
    327  1.1.1.4  dyoung 	do {
    328      1.1  dyoung 		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
    329      1.1  dyoung 			chan = &ic->ic_channels[0];
    330      1.1  dyoung 		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
    331  1.1.1.4  dyoung 			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
    332  1.1.1.4  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    333  1.1.1.4  dyoung 			    "%s: chan %d->%d\n", __func__,
    334  1.1.1.4  dyoung 			    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
    335  1.1.1.4  dyoung 			    ieee80211_chan2ieee(ic, chan));
    336  1.1.1.4  dyoung 			ieee80211_set_chan(ic, ic->ic_bss, chan);
    337  1.1.1.4  dyoung #ifdef notyet
    338  1.1.1.4  dyoung 			/* XXX driver state change */
    339      1.1  dyoung 			/*
    340  1.1.1.4  dyoung 			 * Scan next channel. If doing an active scan
    341  1.1.1.4  dyoung 			 * and the channel is not marked passive-only
    342  1.1.1.4  dyoung 			 * then send a probe request.  Otherwise just
    343  1.1.1.4  dyoung 			 * listen for beacons on the channel.
    344      1.1  dyoung 			 */
    345  1.1.1.4  dyoung 			if ((ic->ic_flags & IEEE80211_F_ASCAN) &&
    346  1.1.1.4  dyoung 			    (ni->ni_chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) {
    347  1.1.1.4  dyoung 				IEEE80211_SEND_MGMT(ic, ni,
    348  1.1.1.4  dyoung 				    IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
    349  1.1.1.4  dyoung 			}
    350  1.1.1.4  dyoung #else
    351  1.1.1.4  dyoung 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    352  1.1.1.4  dyoung #endif
    353  1.1.1.4  dyoung 			return 1;
    354      1.1  dyoung 		}
    355  1.1.1.4  dyoung 	} while (chan != ic->ic_bss->ni_chan);
    356  1.1.1.4  dyoung 	ieee80211_end_scan(ic);
    357  1.1.1.4  dyoung 	return 0;
    358  1.1.1.4  dyoung }
    359  1.1.1.4  dyoung 
    360  1.1.1.4  dyoung static __inline void
    361  1.1.1.4  dyoung copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
    362  1.1.1.4  dyoung {
    363  1.1.1.4  dyoung 	/* propagate useful state */
    364  1.1.1.4  dyoung 	nbss->ni_authmode = obss->ni_authmode;
    365  1.1.1.4  dyoung 	nbss->ni_txpower = obss->ni_txpower;
    366  1.1.1.4  dyoung 	nbss->ni_vlan = obss->ni_vlan;
    367  1.1.1.4  dyoung 	nbss->ni_rsn = obss->ni_rsn;
    368  1.1.1.4  dyoung 	/* XXX statistics? */
    369      1.1  dyoung }
    370      1.1  dyoung 
    371      1.1  dyoung void
    372      1.1  dyoung ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
    373      1.1  dyoung {
    374  1.1.1.4  dyoung 	struct ieee80211_node_table *nt;
    375      1.1  dyoung 	struct ieee80211_node *ni;
    376      1.1  dyoung 
    377  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    378  1.1.1.4  dyoung 		"%s: creating ibss\n", __func__);
    379  1.1.1.4  dyoung 
    380  1.1.1.4  dyoung 	/*
    381  1.1.1.4  dyoung 	 * Create the station/neighbor table.  Note that for adhoc
    382  1.1.1.4  dyoung 	 * mode we make the initial inactivity timer longer since
    383  1.1.1.4  dyoung 	 * we create nodes only through discovery and they typically
    384  1.1.1.4  dyoung 	 * are long-lived associations.
    385  1.1.1.4  dyoung 	 */
    386  1.1.1.4  dyoung 	nt = &ic->ic_sta;
    387  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK(nt);
    388  1.1.1.4  dyoung 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    389  1.1.1.4  dyoung 		nt->nt_name = "station";
    390  1.1.1.4  dyoung 		nt->nt_inact_init = ic->ic_inact_init;
    391  1.1.1.4  dyoung 	} else {
    392  1.1.1.4  dyoung 		nt->nt_name = "neighbor";
    393  1.1.1.4  dyoung 		nt->nt_inact_init = ic->ic_inact_run;
    394  1.1.1.4  dyoung 	}
    395  1.1.1.4  dyoung 	IEEE80211_NODE_UNLOCK(nt);
    396  1.1.1.4  dyoung 
    397  1.1.1.4  dyoung 	ni = ieee80211_alloc_node(nt, ic->ic_myaddr);
    398  1.1.1.4  dyoung 	if (ni == NULL) {
    399  1.1.1.4  dyoung 		/* XXX recovery? */
    400  1.1.1.4  dyoung 		return;
    401  1.1.1.4  dyoung 	}
    402      1.1  dyoung 	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
    403      1.1  dyoung 	ni->ni_esslen = ic->ic_des_esslen;
    404      1.1  dyoung 	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
    405  1.1.1.4  dyoung 	copy_bss(ni, ic->ic_bss);
    406      1.1  dyoung 	ni->ni_intval = ic->ic_lintval;
    407  1.1.1.4  dyoung 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
    408      1.1  dyoung 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
    409      1.1  dyoung 	if (ic->ic_phytype == IEEE80211_T_FH) {
    410      1.1  dyoung 		ni->ni_fhdwell = 200;	/* XXX */
    411      1.1  dyoung 		ni->ni_fhindex = 1;
    412      1.1  dyoung 	}
    413  1.1.1.4  dyoung 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
    414  1.1.1.4  dyoung 		ic->ic_flags |= IEEE80211_F_SIBSS;
    415  1.1.1.4  dyoung 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
    416  1.1.1.4  dyoung 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
    417  1.1.1.4  dyoung 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
    418  1.1.1.4  dyoung 		else
    419  1.1.1.4  dyoung 			ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
    420  1.1.1.4  dyoung 	}
    421  1.1.1.4  dyoung 	/*
    422  1.1.1.4  dyoung 	 * Fix the channel and related attributes.
    423  1.1.1.4  dyoung 	 */
    424  1.1.1.4  dyoung 	ieee80211_set_chan(ic, ni, chan);
    425  1.1.1.4  dyoung 	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
    426  1.1.1.4  dyoung 	/*
    427  1.1.1.4  dyoung 	 * Do mode-specific rate setup.
    428  1.1.1.4  dyoung 	 */
    429  1.1.1.4  dyoung 	if (ic->ic_curmode == IEEE80211_MODE_11G) {
    430  1.1.1.4  dyoung 		/*
    431  1.1.1.4  dyoung 		 * Use a mixed 11b/11g rate set.
    432  1.1.1.4  dyoung 		 */
    433  1.1.1.4  dyoung 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
    434  1.1.1.4  dyoung 	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
    435  1.1.1.4  dyoung 		/*
    436  1.1.1.4  dyoung 		 * Force pure 11b rate set.
    437  1.1.1.4  dyoung 		 */
    438  1.1.1.4  dyoung 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
    439  1.1.1.4  dyoung 	}
    440  1.1.1.4  dyoung 
    441  1.1.1.4  dyoung 	(void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
    442  1.1.1.4  dyoung }
    443  1.1.1.4  dyoung 
    444  1.1.1.4  dyoung void
    445  1.1.1.4  dyoung ieee80211_reset_bss(struct ieee80211com *ic)
    446  1.1.1.4  dyoung {
    447  1.1.1.4  dyoung 	struct ieee80211_node *ni, *obss;
    448  1.1.1.4  dyoung 
    449  1.1.1.4  dyoung 	ieee80211_node_table_reset(&ic->ic_scan);
    450  1.1.1.4  dyoung 	ieee80211_node_table_reset(&ic->ic_sta);
    451  1.1.1.4  dyoung 
    452  1.1.1.4  dyoung 	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
    453  1.1.1.4  dyoung 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
    454  1.1.1.4  dyoung 	obss = ic->ic_bss;
    455  1.1.1.4  dyoung 	ic->ic_bss = ieee80211_ref_node(ni);
    456  1.1.1.4  dyoung 	if (obss != NULL) {
    457  1.1.1.4  dyoung 		copy_bss(ni, obss);
    458  1.1.1.4  dyoung 		ni->ni_intval = ic->ic_lintval;
    459  1.1.1.4  dyoung 		ieee80211_free_node(obss);
    460  1.1.1.4  dyoung 	}
    461      1.1  dyoung }
    462      1.1  dyoung 
    463  1.1.1.3  dyoung static int
    464  1.1.1.4  dyoung ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
    465  1.1.1.3  dyoung {
    466  1.1.1.3  dyoung         u_int8_t rate;
    467  1.1.1.3  dyoung         int fail;
    468  1.1.1.3  dyoung 
    469  1.1.1.3  dyoung 	fail = 0;
    470  1.1.1.3  dyoung 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
    471  1.1.1.3  dyoung 		fail |= 0x01;
    472  1.1.1.3  dyoung 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
    473  1.1.1.3  dyoung 	    ni->ni_chan != ic->ic_des_chan)
    474  1.1.1.3  dyoung 		fail |= 0x01;
    475  1.1.1.3  dyoung 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
    476  1.1.1.3  dyoung 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
    477  1.1.1.3  dyoung 			fail |= 0x02;
    478  1.1.1.3  dyoung 	} else {
    479  1.1.1.3  dyoung 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
    480  1.1.1.3  dyoung 			fail |= 0x02;
    481  1.1.1.3  dyoung 	}
    482  1.1.1.4  dyoung 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
    483  1.1.1.3  dyoung 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
    484  1.1.1.3  dyoung 			fail |= 0x04;
    485  1.1.1.3  dyoung 	} else {
    486  1.1.1.3  dyoung 		/* XXX does this mean privacy is supported or required? */
    487  1.1.1.3  dyoung 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
    488  1.1.1.3  dyoung 			fail |= 0x04;
    489  1.1.1.3  dyoung 	}
    490  1.1.1.4  dyoung 	rate = ieee80211_fix_rate(ic, ni,
    491  1.1.1.4  dyoung 			IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
    492  1.1.1.3  dyoung 	if (rate & IEEE80211_RATE_BASIC)
    493  1.1.1.3  dyoung 		fail |= 0x08;
    494  1.1.1.3  dyoung 	if (ic->ic_des_esslen != 0 &&
    495  1.1.1.3  dyoung 	    (ni->ni_esslen != ic->ic_des_esslen ||
    496  1.1.1.3  dyoung 	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
    497  1.1.1.3  dyoung 		fail |= 0x10;
    498  1.1.1.3  dyoung 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
    499  1.1.1.3  dyoung 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
    500  1.1.1.3  dyoung 		fail |= 0x20;
    501  1.1.1.3  dyoung #ifdef IEEE80211_DEBUG
    502  1.1.1.4  dyoung 	if (ieee80211_msg_scan(ic)) {
    503  1.1.1.3  dyoung 		printf(" %c %s", fail ? '-' : '+',
    504  1.1.1.3  dyoung 		    ether_sprintf(ni->ni_macaddr));
    505  1.1.1.3  dyoung 		printf(" %s%c", ether_sprintf(ni->ni_bssid),
    506  1.1.1.3  dyoung 		    fail & 0x20 ? '!' : ' ');
    507  1.1.1.3  dyoung 		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
    508  1.1.1.3  dyoung 			fail & 0x01 ? '!' : ' ');
    509  1.1.1.3  dyoung 		printf(" %+4d", ni->ni_rssi);
    510  1.1.1.3  dyoung 		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
    511  1.1.1.3  dyoung 		    fail & 0x08 ? '!' : ' ');
    512  1.1.1.3  dyoung 		printf(" %4s%c",
    513  1.1.1.3  dyoung 		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
    514  1.1.1.3  dyoung 		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
    515  1.1.1.3  dyoung 		    "????",
    516  1.1.1.3  dyoung 		    fail & 0x02 ? '!' : ' ');
    517  1.1.1.3  dyoung 		printf(" %3s%c ",
    518  1.1.1.3  dyoung 		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
    519  1.1.1.3  dyoung 		    "wep" : "no",
    520  1.1.1.3  dyoung 		    fail & 0x04 ? '!' : ' ');
    521  1.1.1.3  dyoung 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
    522  1.1.1.3  dyoung 		printf("%s\n", fail & 0x10 ? "!" : "");
    523  1.1.1.3  dyoung 	}
    524  1.1.1.3  dyoung #endif
    525  1.1.1.3  dyoung 	return fail;
    526  1.1.1.3  dyoung }
    527  1.1.1.3  dyoung 
    528  1.1.1.4  dyoung static __inline u_int8_t
    529  1.1.1.4  dyoung maxrate(const struct ieee80211_node *ni)
    530  1.1.1.4  dyoung {
    531  1.1.1.4  dyoung 	const struct ieee80211_rateset *rs = &ni->ni_rates;
    532  1.1.1.4  dyoung 	/* NB: assumes rate set is sorted (happens on frame receive) */
    533  1.1.1.4  dyoung 	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
    534  1.1.1.4  dyoung }
    535  1.1.1.4  dyoung 
    536  1.1.1.4  dyoung /*
    537  1.1.1.4  dyoung  * Compare the capabilities of two nodes and decide which is
    538  1.1.1.4  dyoung  * more desirable (return >0 if a is considered better).  Note
    539  1.1.1.4  dyoung  * that we assume compatibility/usability has already been checked
    540  1.1.1.4  dyoung  * so we don't need to (e.g. validate whether privacy is supported).
    541  1.1.1.4  dyoung  * Used to select the best scan candidate for association in a BSS.
    542  1.1.1.4  dyoung  */
    543  1.1.1.4  dyoung static int
    544  1.1.1.4  dyoung ieee80211_node_compare(struct ieee80211com *ic,
    545  1.1.1.4  dyoung 		       const struct ieee80211_node *a,
    546  1.1.1.4  dyoung 		       const struct ieee80211_node *b)
    547  1.1.1.4  dyoung {
    548  1.1.1.4  dyoung 	u_int8_t maxa, maxb;
    549  1.1.1.4  dyoung 	u_int8_t rssia, rssib;
    550  1.1.1.4  dyoung 
    551  1.1.1.4  dyoung 	/* privacy support preferred */
    552  1.1.1.4  dyoung 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
    553  1.1.1.4  dyoung 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
    554  1.1.1.4  dyoung 		return 1;
    555  1.1.1.4  dyoung 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
    556  1.1.1.4  dyoung 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
    557  1.1.1.4  dyoung 		return -1;
    558  1.1.1.4  dyoung 
    559  1.1.1.4  dyoung 	rssia = ic->ic_node_getrssi(a);
    560  1.1.1.4  dyoung 	rssib = ic->ic_node_getrssi(b);
    561  1.1.1.4  dyoung 	if (abs(rssib - rssia) < 5) {
    562  1.1.1.4  dyoung 		/* best/max rate preferred if signal level close enough XXX */
    563  1.1.1.4  dyoung 		maxa = maxrate(a);
    564  1.1.1.4  dyoung 		maxb = maxrate(b);
    565  1.1.1.4  dyoung 		if (maxa != maxb)
    566  1.1.1.4  dyoung 			return maxa - maxb;
    567  1.1.1.4  dyoung 		/* XXX use freq for channel preference */
    568  1.1.1.4  dyoung 		/* for now just prefer 5Ghz band to all other bands */
    569  1.1.1.4  dyoung 		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
    570  1.1.1.4  dyoung 		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
    571  1.1.1.4  dyoung 			return 1;
    572  1.1.1.4  dyoung 		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
    573  1.1.1.4  dyoung 		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
    574  1.1.1.4  dyoung 			return -1;
    575  1.1.1.4  dyoung 	}
    576  1.1.1.4  dyoung 	/* all things being equal, use signal level */
    577  1.1.1.4  dyoung 	return rssia - rssib;
    578  1.1.1.4  dyoung }
    579  1.1.1.4  dyoung 
    580  1.1.1.4  dyoung /*
    581  1.1.1.4  dyoung  * Mark an ongoing scan stopped.
    582  1.1.1.4  dyoung  */
    583  1.1.1.4  dyoung void
    584  1.1.1.4  dyoung ieee80211_cancel_scan(struct ieee80211com *ic)
    585  1.1.1.4  dyoung {
    586  1.1.1.4  dyoung 
    587  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
    588  1.1.1.4  dyoung 		__func__,
    589  1.1.1.4  dyoung 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
    590  1.1.1.4  dyoung 
    591  1.1.1.4  dyoung 	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
    592  1.1.1.4  dyoung }
    593  1.1.1.4  dyoung 
    594      1.1  dyoung /*
    595      1.1  dyoung  * Complete a scan of potential channels.
    596      1.1  dyoung  */
    597      1.1  dyoung void
    598  1.1.1.4  dyoung ieee80211_end_scan(struct ieee80211com *ic)
    599      1.1  dyoung {
    600  1.1.1.4  dyoung 	struct ieee80211_node_table *nt = &ic->ic_scan;
    601  1.1.1.4  dyoung 	struct ieee80211_node *ni, *selbs;
    602      1.1  dyoung 
    603  1.1.1.4  dyoung 	ieee80211_cancel_scan(ic);
    604  1.1.1.4  dyoung 	ieee80211_notify_scan_done(ic);
    605      1.1  dyoung 
    606      1.1  dyoung 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    607  1.1.1.4  dyoung 		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
    608  1.1.1.4  dyoung 		int i, bestchan;
    609  1.1.1.4  dyoung 		u_int8_t rssi;
    610  1.1.1.4  dyoung 
    611      1.1  dyoung 		/*
    612      1.1  dyoung 		 * The passive scan to look for existing AP's completed,
    613      1.1  dyoung 		 * select a channel to camp on.  Identify the channels
    614      1.1  dyoung 		 * that already have one or more AP's and try to locate
    615  1.1.1.4  dyoung 		 * an unoccupied one.  If that fails, pick a channel that
    616  1.1.1.4  dyoung 		 * looks to be quietest.
    617      1.1  dyoung 		 */
    618  1.1.1.4  dyoung 		memset(maxrssi, 0, sizeof(maxrssi));
    619  1.1.1.4  dyoung 		IEEE80211_NODE_LOCK(nt);
    620  1.1.1.4  dyoung 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
    621  1.1.1.4  dyoung 			rssi = ic->ic_node_getrssi(ni);
    622  1.1.1.4  dyoung 			i = ieee80211_chan2ieee(ic, ni->ni_chan);
    623  1.1.1.4  dyoung 			if (rssi > maxrssi[i])
    624  1.1.1.4  dyoung 				maxrssi[i] = rssi;
    625      1.1  dyoung 		}
    626  1.1.1.4  dyoung 		IEEE80211_NODE_UNLOCK(nt);
    627  1.1.1.4  dyoung 		/* XXX select channel more intelligently */
    628  1.1.1.4  dyoung 		bestchan = -1;
    629      1.1  dyoung 		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
    630  1.1.1.4  dyoung 			if (isset(ic->ic_chan_active, i)) {
    631  1.1.1.4  dyoung 				/*
    632  1.1.1.4  dyoung 				 * If the channel is unoccupied the max rssi
    633  1.1.1.4  dyoung 				 * should be zero; just take it.  Otherwise
    634  1.1.1.4  dyoung 				 * track the channel with the lowest rssi and
    635  1.1.1.4  dyoung 				 * use that when all channels appear occupied.
    636  1.1.1.4  dyoung 				 */
    637  1.1.1.4  dyoung 				if (maxrssi[i] == 0) {
    638  1.1.1.4  dyoung 					bestchan = i;
    639      1.1  dyoung 					break;
    640  1.1.1.4  dyoung 				}
    641  1.1.1.4  dyoung 				if (bestchan == -1 ||
    642  1.1.1.4  dyoung 				    maxrssi[i] < maxrssi[bestchan])
    643  1.1.1.4  dyoung 					bestchan = i;
    644  1.1.1.4  dyoung 			}
    645  1.1.1.4  dyoung 		if (bestchan != -1) {
    646  1.1.1.4  dyoung 			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
    647  1.1.1.4  dyoung 			return;
    648      1.1  dyoung 		}
    649  1.1.1.4  dyoung 		/* no suitable channel, should not happen */
    650      1.1  dyoung 	}
    651  1.1.1.4  dyoung 
    652  1.1.1.4  dyoung 	/*
    653  1.1.1.4  dyoung 	 * When manually sequencing the state machine; scan just once
    654  1.1.1.4  dyoung 	 * regardless of whether we have a candidate or not.  The
    655  1.1.1.4  dyoung 	 * controlling application is expected to setup state and
    656  1.1.1.4  dyoung 	 * initiate an association.
    657  1.1.1.4  dyoung 	 */
    658  1.1.1.4  dyoung 	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
    659  1.1.1.4  dyoung 		return;
    660  1.1.1.4  dyoung 	/*
    661  1.1.1.4  dyoung 	 * Automatic sequencing; look for a candidate and
    662  1.1.1.4  dyoung 	 * if found join the network.
    663  1.1.1.4  dyoung 	 */
    664  1.1.1.4  dyoung 	/* NB: unlocked read should be ok */
    665  1.1.1.4  dyoung 	if (TAILQ_FIRST(&nt->nt_node) == NULL) {
    666  1.1.1.4  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    667  1.1.1.4  dyoung 			"%s: no scan candidate\n", __func__);
    668      1.1  dyoung   notfound:
    669      1.1  dyoung 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
    670      1.1  dyoung 		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
    671      1.1  dyoung 		    ic->ic_des_esslen != 0) {
    672      1.1  dyoung 			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
    673      1.1  dyoung 			return;
    674      1.1  dyoung 		}
    675      1.1  dyoung 		/*
    676      1.1  dyoung 		 * Reset the list of channels to scan and start again.
    677      1.1  dyoung 		 */
    678  1.1.1.4  dyoung 		ieee80211_reset_scan(ic);
    679  1.1.1.4  dyoung 		ic->ic_flags |= IEEE80211_F_SCAN;
    680  1.1.1.4  dyoung 		ieee80211_next_scan(ic);
    681      1.1  dyoung 		return;
    682      1.1  dyoung 	}
    683      1.1  dyoung 	selbs = NULL;
    684  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
    685  1.1.1.4  dyoung 	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
    686  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK(nt);
    687  1.1.1.4  dyoung 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
    688      1.1  dyoung 		if (ni->ni_fails) {
    689      1.1  dyoung 			/*
    690      1.1  dyoung 			 * The configuration of the access points may change
    691      1.1  dyoung 			 * during my scan.  So delete the entry for the AP
    692      1.1  dyoung 			 * and retry to associate if there is another beacon.
    693      1.1  dyoung 			 */
    694  1.1.1.4  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    695  1.1.1.4  dyoung 				"%s: skip scan candidate %s, fails %u\n",
    696  1.1.1.4  dyoung 				__func__, ether_sprintf(ni->ni_macaddr),
    697  1.1.1.4  dyoung 				ni->ni_fails);
    698  1.1.1.4  dyoung 			ni->ni_fails++;
    699  1.1.1.4  dyoung #if 0
    700      1.1  dyoung 			if (ni->ni_fails++ > 2)
    701  1.1.1.4  dyoung 				ieee80211_free_node(ni);
    702  1.1.1.4  dyoung #endif
    703      1.1  dyoung 			continue;
    704      1.1  dyoung 		}
    705  1.1.1.4  dyoung 		if (ieee80211_match_bss(ic, ni) == 0) {
    706      1.1  dyoung 			if (selbs == NULL)
    707      1.1  dyoung 				selbs = ni;
    708  1.1.1.4  dyoung 			else if (ieee80211_node_compare(ic, ni, selbs) > 0)
    709      1.1  dyoung 				selbs = ni;
    710      1.1  dyoung 		}
    711      1.1  dyoung 	}
    712  1.1.1.4  dyoung 	if (selbs != NULL)		/* NB: grab ref while dropping lock */
    713  1.1.1.4  dyoung 		(void) ieee80211_ref_node(selbs);
    714  1.1.1.4  dyoung 	IEEE80211_NODE_UNLOCK(nt);
    715      1.1  dyoung 	if (selbs == NULL)
    716      1.1  dyoung 		goto notfound;
    717  1.1.1.4  dyoung 	if (!ieee80211_sta_join(ic, selbs)) {
    718  1.1.1.4  dyoung 		ieee80211_free_node(selbs);
    719  1.1.1.4  dyoung 		goto notfound;
    720  1.1.1.4  dyoung 	}
    721  1.1.1.4  dyoung }
    722  1.1.1.4  dyoung 
    723  1.1.1.4  dyoung /*
    724  1.1.1.4  dyoung  * Handle 802.11 ad hoc network merge.  The
    725  1.1.1.4  dyoung  * convention, set by the Wireless Ethernet Compatibility Alliance
    726  1.1.1.4  dyoung  * (WECA), is that an 802.11 station will change its BSSID to match
    727  1.1.1.4  dyoung  * the "oldest" 802.11 ad hoc network, on the same channel, that
    728  1.1.1.4  dyoung  * has the station's desired SSID.  The "oldest" 802.11 network
    729  1.1.1.4  dyoung  * sends beacons with the greatest TSF timestamp.
    730  1.1.1.4  dyoung  *
    731  1.1.1.4  dyoung  * The caller is assumed to validate TSF's before attempting a merge.
    732  1.1.1.4  dyoung  *
    733  1.1.1.4  dyoung  * Return !0 if the BSSID changed, 0 otherwise.
    734  1.1.1.4  dyoung  */
    735  1.1.1.4  dyoung int
    736  1.1.1.4  dyoung ieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni)
    737  1.1.1.4  dyoung {
    738  1.1.1.4  dyoung 
    739  1.1.1.4  dyoung 	if (ni == ic->ic_bss ||
    740  1.1.1.4  dyoung 	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
    741  1.1.1.4  dyoung 		/* unchanged, nothing to do */
    742  1.1.1.4  dyoung 		return 0;
    743  1.1.1.4  dyoung 	}
    744  1.1.1.4  dyoung 	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
    745  1.1.1.4  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
    746  1.1.1.4  dyoung 		    "%s: merge failed, capabilities mismatch\n", __func__);
    747  1.1.1.4  dyoung 		ic->ic_stats.is_ibss_capmismatch++;
    748  1.1.1.4  dyoung 		return 0;
    749  1.1.1.4  dyoung 	}
    750  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
    751  1.1.1.4  dyoung 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
    752  1.1.1.4  dyoung 		ether_sprintf(ni->ni_bssid),
    753  1.1.1.4  dyoung 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
    754  1.1.1.4  dyoung 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
    755  1.1.1.4  dyoung 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
    756  1.1.1.4  dyoung 	);
    757  1.1.1.4  dyoung 	return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
    758  1.1.1.4  dyoung }
    759  1.1.1.4  dyoung 
    760  1.1.1.4  dyoung /*
    761  1.1.1.4  dyoung  * Join the specified IBSS/BSS network.  The node is assumed to
    762  1.1.1.4  dyoung  * be passed in with a held reference.
    763  1.1.1.4  dyoung  */
    764  1.1.1.4  dyoung int
    765  1.1.1.4  dyoung ieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
    766  1.1.1.4  dyoung {
    767  1.1.1.4  dyoung 	struct ieee80211_node *obss;
    768  1.1.1.4  dyoung 
    769      1.1  dyoung 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
    770  1.1.1.4  dyoung 		struct ieee80211_node_table *nt;
    771  1.1.1.4  dyoung 		/*
    772  1.1.1.4  dyoung 		 * Delete unusable rates; we've already checked
    773  1.1.1.4  dyoung 		 * that the negotiated rate set is acceptable.
    774  1.1.1.3  dyoung 		 */
    775  1.1.1.4  dyoung 		ieee80211_fix_rate(ic, selbs, IEEE80211_F_DODEL);
    776  1.1.1.4  dyoung 		/*
    777  1.1.1.4  dyoung 		 * Fillin the neighbor table; it will already
    778  1.1.1.4  dyoung 		 * exist if we are simply switching mastership.
    779  1.1.1.4  dyoung 		 * XXX ic_sta always setup so this is unnecessary?
    780  1.1.1.4  dyoung 		 */
    781  1.1.1.4  dyoung 		nt = &ic->ic_sta;
    782  1.1.1.4  dyoung 		IEEE80211_NODE_LOCK(nt);
    783  1.1.1.4  dyoung 		nt->nt_name = "neighbor";
    784  1.1.1.4  dyoung 		nt->nt_inact_init = ic->ic_inact_run;
    785  1.1.1.4  dyoung 		IEEE80211_NODE_UNLOCK(nt);
    786      1.1  dyoung 	}
    787  1.1.1.4  dyoung 
    788  1.1.1.4  dyoung 	/*
    789  1.1.1.4  dyoung 	 * Committed to selbs, setup state.
    790  1.1.1.4  dyoung 	 */
    791  1.1.1.4  dyoung 	obss = ic->ic_bss;
    792  1.1.1.4  dyoung 	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
    793  1.1.1.4  dyoung 	if (obss != NULL)
    794  1.1.1.4  dyoung 		ieee80211_free_node(obss);
    795  1.1.1.4  dyoung 	/*
    796  1.1.1.4  dyoung 	 * Set the erp state (mostly the slot time) to deal with
    797  1.1.1.4  dyoung 	 * the auto-select case; this should be redundant if the
    798  1.1.1.4  dyoung 	 * mode is locked.
    799  1.1.1.4  dyoung 	 */
    800  1.1.1.4  dyoung 	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
    801  1.1.1.4  dyoung 	ieee80211_reset_erp(ic);
    802  1.1.1.4  dyoung 	ieee80211_wme_initparams(ic);
    803  1.1.1.4  dyoung 
    804  1.1.1.4  dyoung 	if (ic->ic_opmode == IEEE80211_M_STA)
    805  1.1.1.4  dyoung 		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
    806  1.1.1.4  dyoung 	else
    807  1.1.1.4  dyoung 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    808  1.1.1.4  dyoung 	return 1;
    809  1.1.1.4  dyoung }
    810  1.1.1.4  dyoung 
    811  1.1.1.4  dyoung /*
    812  1.1.1.4  dyoung  * Leave the specified IBSS/BSS network.  The node is assumed to
    813  1.1.1.4  dyoung  * be passed in with a held reference.
    814  1.1.1.4  dyoung  */
    815  1.1.1.4  dyoung void
    816  1.1.1.4  dyoung ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
    817  1.1.1.4  dyoung {
    818  1.1.1.4  dyoung 	ic->ic_node_cleanup(ni);
    819  1.1.1.4  dyoung 	ieee80211_notify_node_leave(ic, ni);
    820      1.1  dyoung }
    821      1.1  dyoung 
    822      1.1  dyoung static struct ieee80211_node *
    823  1.1.1.4  dyoung node_alloc(struct ieee80211_node_table *nt)
    824      1.1  dyoung {
    825  1.1.1.3  dyoung 	struct ieee80211_node *ni;
    826  1.1.1.4  dyoung 
    827  1.1.1.3  dyoung 	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
    828  1.1.1.3  dyoung 		M_80211_NODE, M_NOWAIT | M_ZERO);
    829  1.1.1.3  dyoung 	return ni;
    830      1.1  dyoung }
    831      1.1  dyoung 
    832  1.1.1.4  dyoung /*
    833  1.1.1.4  dyoung  * Reclaim any resources in a node and reset any critical
    834  1.1.1.4  dyoung  * state.  Typically nodes are free'd immediately after,
    835  1.1.1.4  dyoung  * but in some cases the storage may be reused so we need
    836  1.1.1.4  dyoung  * to insure consistent state (should probably fix that).
    837  1.1.1.4  dyoung  */
    838      1.1  dyoung static void
    839  1.1.1.4  dyoung node_cleanup(struct ieee80211_node *ni)
    840      1.1  dyoung {
    841  1.1.1.4  dyoung #define	N(a)	(sizeof(a)/sizeof(a[0]))
    842  1.1.1.4  dyoung 	struct ieee80211com *ic = ni->ni_ic;
    843  1.1.1.4  dyoung 	int i, qlen;
    844  1.1.1.4  dyoung 
    845  1.1.1.4  dyoung 	/* NB: preserve ni_table */
    846  1.1.1.4  dyoung 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
    847  1.1.1.4  dyoung 		ic->ic_ps_sta--;
    848  1.1.1.4  dyoung 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
    849  1.1.1.4  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
    850  1.1.1.4  dyoung 		    "[%s] power save mode off, %u sta's in ps mode\n",
    851  1.1.1.4  dyoung 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
    852  1.1.1.4  dyoung 	}
    853  1.1.1.5  dyoung 	/*
    854  1.1.1.5  dyoung 	 * Clear AREF flag that marks the authorization refcnt bump
    855  1.1.1.5  dyoung 	 * has happened.  This is probably not needed as the node
    856  1.1.1.5  dyoung 	 * should always be removed from the table so not found but
    857  1.1.1.5  dyoung 	 * do it just in case.
    858  1.1.1.5  dyoung 	 */
    859  1.1.1.5  dyoung 	ni->ni_flags &= ~IEEE80211_NODE_AREF;
    860  1.1.1.4  dyoung 
    861  1.1.1.4  dyoung 	/*
    862  1.1.1.4  dyoung 	 * Drain power save queue and, if needed, clear TIM.
    863  1.1.1.4  dyoung 	 */
    864  1.1.1.4  dyoung 	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
    865  1.1.1.4  dyoung 	if (qlen != 0 && ic->ic_set_tim != NULL)
    866  1.1.1.4  dyoung 		ic->ic_set_tim(ic, ni, 0);
    867  1.1.1.4  dyoung 
    868  1.1.1.4  dyoung 	ni->ni_associd = 0;
    869  1.1.1.4  dyoung 	if (ni->ni_challenge != NULL) {
    870  1.1.1.4  dyoung 		FREE(ni->ni_challenge, M_DEVBUF);
    871  1.1.1.4  dyoung 		ni->ni_challenge = NULL;
    872  1.1.1.4  dyoung 	}
    873  1.1.1.4  dyoung 	/*
    874  1.1.1.4  dyoung 	 * Preserve SSID, WPA, and WME ie's so the bss node is
    875  1.1.1.4  dyoung 	 * reusable during a re-auth/re-assoc state transition.
    876  1.1.1.4  dyoung 	 * If we remove these data they will not be recreated
    877  1.1.1.4  dyoung 	 * because they come from a probe-response or beacon frame
    878  1.1.1.4  dyoung 	 * which cannot be expected prior to the association-response.
    879  1.1.1.4  dyoung 	 * This should not be an issue when operating in other modes
    880  1.1.1.4  dyoung 	 * as stations leaving always go through a full state transition
    881  1.1.1.4  dyoung 	 * which will rebuild this state.
    882  1.1.1.4  dyoung 	 *
    883  1.1.1.4  dyoung 	 * XXX does this leave us open to inheriting old state?
    884  1.1.1.4  dyoung 	 */
    885  1.1.1.4  dyoung 	for (i = 0; i < N(ni->ni_rxfrag); i++)
    886  1.1.1.4  dyoung 		if (ni->ni_rxfrag[i] != NULL) {
    887  1.1.1.4  dyoung 			m_freem(ni->ni_rxfrag[i]);
    888  1.1.1.4  dyoung 			ni->ni_rxfrag[i] = NULL;
    889  1.1.1.4  dyoung 		}
    890  1.1.1.4  dyoung 	ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
    891  1.1.1.4  dyoung #undef N
    892      1.1  dyoung }
    893      1.1  dyoung 
    894      1.1  dyoung static void
    895  1.1.1.4  dyoung node_free(struct ieee80211_node *ni)
    896      1.1  dyoung {
    897  1.1.1.4  dyoung 	struct ieee80211com *ic = ni->ni_ic;
    898  1.1.1.4  dyoung 
    899  1.1.1.4  dyoung 	ic->ic_node_cleanup(ni);
    900  1.1.1.4  dyoung 	if (ni->ni_wpa_ie != NULL)
    901  1.1.1.4  dyoung 		FREE(ni->ni_wpa_ie, M_DEVBUF);
    902  1.1.1.4  dyoung 	if (ni->ni_wme_ie != NULL)
    903  1.1.1.4  dyoung 		FREE(ni->ni_wme_ie, M_DEVBUF);
    904  1.1.1.4  dyoung 	IEEE80211_NODE_SAVEQ_DESTROY(ni);
    905  1.1.1.4  dyoung 	FREE(ni, M_80211_NODE);
    906      1.1  dyoung }
    907      1.1  dyoung 
    908  1.1.1.2  dyoung static u_int8_t
    909  1.1.1.4  dyoung node_getrssi(const struct ieee80211_node *ni)
    910  1.1.1.2  dyoung {
    911  1.1.1.2  dyoung 	return ni->ni_rssi;
    912  1.1.1.2  dyoung }
    913  1.1.1.2  dyoung 
    914      1.1  dyoung static void
    915  1.1.1.4  dyoung ieee80211_setup_node(struct ieee80211_node_table *nt,
    916  1.1.1.4  dyoung 	struct ieee80211_node *ni, const u_int8_t *macaddr)
    917      1.1  dyoung {
    918  1.1.1.4  dyoung 	struct ieee80211com *ic = nt->nt_ic;
    919      1.1  dyoung 	int hash;
    920      1.1  dyoung 
    921  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
    922  1.1.1.4  dyoung 		"%s %p<%s> in %s table\n", __func__, ni,
    923  1.1.1.4  dyoung 		ether_sprintf(macaddr), nt->nt_name);
    924  1.1.1.4  dyoung 
    925      1.1  dyoung 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
    926      1.1  dyoung 	hash = IEEE80211_NODE_HASH(macaddr);
    927  1.1.1.4  dyoung 	ieee80211_node_initref(ni);		/* mark referenced */
    928  1.1.1.4  dyoung 	ni->ni_chan = IEEE80211_CHAN_ANYC;
    929  1.1.1.4  dyoung 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
    930  1.1.1.4  dyoung 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
    931  1.1.1.4  dyoung 	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
    932  1.1.1.4  dyoung 	ni->ni_inact_reload = nt->nt_inact_init;
    933  1.1.1.4  dyoung 	ni->ni_inact = ni->ni_inact_reload;
    934  1.1.1.4  dyoung 	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
    935  1.1.1.4  dyoung 
    936  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK(nt);
    937  1.1.1.4  dyoung 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
    938  1.1.1.4  dyoung 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
    939  1.1.1.4  dyoung 	ni->ni_table = nt;
    940  1.1.1.4  dyoung 	ni->ni_ic = ic;
    941  1.1.1.4  dyoung 	IEEE80211_NODE_UNLOCK(nt);
    942      1.1  dyoung }
    943      1.1  dyoung 
    944      1.1  dyoung struct ieee80211_node *
    945  1.1.1.4  dyoung ieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
    946      1.1  dyoung {
    947  1.1.1.4  dyoung 	struct ieee80211com *ic = nt->nt_ic;
    948  1.1.1.4  dyoung 	struct ieee80211_node *ni;
    949  1.1.1.4  dyoung 
    950  1.1.1.4  dyoung 	ni = ic->ic_node_alloc(nt);
    951      1.1  dyoung 	if (ni != NULL)
    952  1.1.1.4  dyoung 		ieee80211_setup_node(nt, ni, macaddr);
    953  1.1.1.3  dyoung 	else
    954  1.1.1.3  dyoung 		ic->ic_stats.is_rx_nodealloc++;
    955      1.1  dyoung 	return ni;
    956      1.1  dyoung }
    957      1.1  dyoung 
    958      1.1  dyoung struct ieee80211_node *
    959  1.1.1.4  dyoung ieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
    960      1.1  dyoung {
    961  1.1.1.4  dyoung 	struct ieee80211com *ic = nt->nt_ic;
    962  1.1.1.4  dyoung 	struct ieee80211_node *ni;
    963  1.1.1.4  dyoung 
    964  1.1.1.4  dyoung 	ni = ic->ic_node_alloc(nt);
    965      1.1  dyoung 	if (ni != NULL) {
    966  1.1.1.4  dyoung 		ieee80211_setup_node(nt, ni, macaddr);
    967  1.1.1.3  dyoung 		/*
    968  1.1.1.3  dyoung 		 * Inherit from ic_bss.
    969  1.1.1.3  dyoung 		 */
    970  1.1.1.4  dyoung 		ni->ni_authmode = ic->ic_bss->ni_authmode;
    971  1.1.1.4  dyoung 		ni->ni_txpower = ic->ic_bss->ni_txpower;
    972  1.1.1.4  dyoung 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
    973  1.1.1.3  dyoung 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
    974  1.1.1.4  dyoung 		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
    975  1.1.1.4  dyoung 		ni->ni_rsn = ic->ic_bss->ni_rsn;
    976  1.1.1.3  dyoung 	} else
    977  1.1.1.3  dyoung 		ic->ic_stats.is_rx_nodealloc++;
    978      1.1  dyoung 	return ni;
    979      1.1  dyoung }
    980      1.1  dyoung 
    981  1.1.1.3  dyoung static struct ieee80211_node *
    982  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG_REFCNT
    983  1.1.1.4  dyoung _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
    984  1.1.1.4  dyoung 	const u_int8_t *macaddr, const char *func, int line)
    985  1.1.1.4  dyoung #else
    986  1.1.1.4  dyoung _ieee80211_find_node(struct ieee80211_node_table *nt,
    987  1.1.1.4  dyoung 	const u_int8_t *macaddr)
    988  1.1.1.4  dyoung #endif
    989      1.1  dyoung {
    990      1.1  dyoung 	struct ieee80211_node *ni;
    991      1.1  dyoung 	int hash;
    992      1.1  dyoung 
    993  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK_ASSERT(nt);
    994  1.1.1.3  dyoung 
    995      1.1  dyoung 	hash = IEEE80211_NODE_HASH(macaddr);
    996  1.1.1.4  dyoung 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
    997      1.1  dyoung 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
    998  1.1.1.4  dyoung 			ieee80211_ref_node(ni);	/* mark referenced */
    999  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1000  1.1.1.4  dyoung 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
   1001  1.1.1.4  dyoung 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
   1002  1.1.1.4  dyoung 			    func, line,
   1003  1.1.1.4  dyoung 			    ni, ether_sprintf(ni->ni_macaddr),
   1004  1.1.1.4  dyoung 			    ieee80211_node_refcnt(ni));
   1005  1.1.1.4  dyoung #endif
   1006  1.1.1.3  dyoung 			return ni;
   1007      1.1  dyoung 		}
   1008      1.1  dyoung 	}
   1009  1.1.1.3  dyoung 	return NULL;
   1010  1.1.1.3  dyoung }
   1011  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1012  1.1.1.4  dyoung #define	_ieee80211_find_node(nt, mac) \
   1013  1.1.1.4  dyoung 	_ieee80211_find_node_debug(nt, mac, func, line)
   1014  1.1.1.4  dyoung #endif
   1015  1.1.1.3  dyoung 
   1016  1.1.1.3  dyoung struct ieee80211_node *
   1017  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1018  1.1.1.4  dyoung ieee80211_find_node_debug(struct ieee80211_node_table *nt,
   1019  1.1.1.4  dyoung 	const u_int8_t *macaddr, const char *func, int line)
   1020  1.1.1.4  dyoung #else
   1021  1.1.1.4  dyoung ieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
   1022  1.1.1.4  dyoung #endif
   1023  1.1.1.3  dyoung {
   1024  1.1.1.3  dyoung 	struct ieee80211_node *ni;
   1025  1.1.1.3  dyoung 
   1026  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK(nt);
   1027  1.1.1.4  dyoung 	ni = _ieee80211_find_node(nt, macaddr);
   1028  1.1.1.4  dyoung 	IEEE80211_NODE_UNLOCK(nt);
   1029      1.1  dyoung 	return ni;
   1030      1.1  dyoung }
   1031      1.1  dyoung 
   1032      1.1  dyoung /*
   1033  1.1.1.4  dyoung  * Fake up a node; this handles node discovery in adhoc mode.
   1034  1.1.1.4  dyoung  * Note that for the driver's benefit we we treat this like
   1035  1.1.1.4  dyoung  * an association so the driver has an opportunity to setup
   1036  1.1.1.4  dyoung  * it's private state.
   1037  1.1.1.4  dyoung  */
   1038  1.1.1.4  dyoung struct ieee80211_node *
   1039  1.1.1.4  dyoung ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
   1040  1.1.1.4  dyoung 	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
   1041  1.1.1.4  dyoung {
   1042  1.1.1.4  dyoung 	struct ieee80211com *ic = nt->nt_ic;
   1043  1.1.1.4  dyoung 	struct ieee80211_node *ni;
   1044  1.1.1.4  dyoung 
   1045  1.1.1.4  dyoung 	ni = ieee80211_dup_bss(nt, macaddr);
   1046  1.1.1.4  dyoung 	if (ni != NULL) {
   1047  1.1.1.4  dyoung 		/* XXX no rate negotiation; just dup */
   1048  1.1.1.4  dyoung 		ni->ni_rates = ic->ic_bss->ni_rates;
   1049  1.1.1.4  dyoung 		if (ic->ic_newassoc != NULL)
   1050  1.1.1.4  dyoung 			ic->ic_newassoc(ic, ni, 1);
   1051  1.1.1.4  dyoung 		/* XXX not right for 802.1x/WPA */
   1052  1.1.1.4  dyoung 		ieee80211_node_authorize(ic, ni);
   1053  1.1.1.4  dyoung 	}
   1054  1.1.1.4  dyoung 	return ni;
   1055  1.1.1.4  dyoung }
   1056  1.1.1.4  dyoung 
   1057  1.1.1.4  dyoung /*
   1058  1.1.1.4  dyoung  * Locate the node for sender, track state, and then pass the
   1059  1.1.1.4  dyoung  * (referenced) node up to the 802.11 layer for its use.  We
   1060  1.1.1.4  dyoung  * are required to pass some node so we fall back to ic_bss
   1061  1.1.1.4  dyoung  * when this frame is from an unknown sender.  The 802.11 layer
   1062  1.1.1.4  dyoung  * knows this means the sender wasn't in the node table and
   1063  1.1.1.4  dyoung  * acts accordingly.
   1064  1.1.1.4  dyoung  */
   1065  1.1.1.4  dyoung struct ieee80211_node *
   1066  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1067  1.1.1.4  dyoung ieee80211_find_rxnode_debug(struct ieee80211com *ic,
   1068  1.1.1.4  dyoung 	const struct ieee80211_frame_min *wh, const char *func, int line)
   1069  1.1.1.4  dyoung #else
   1070  1.1.1.4  dyoung ieee80211_find_rxnode(struct ieee80211com *ic,
   1071  1.1.1.4  dyoung 	const struct ieee80211_frame_min *wh)
   1072  1.1.1.4  dyoung #endif
   1073  1.1.1.4  dyoung {
   1074  1.1.1.4  dyoung #define	IS_CTL(wh) \
   1075  1.1.1.4  dyoung 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
   1076  1.1.1.4  dyoung #define	IS_PSPOLL(wh) \
   1077  1.1.1.4  dyoung 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
   1078  1.1.1.4  dyoung 	struct ieee80211_node_table *nt;
   1079  1.1.1.4  dyoung 	struct ieee80211_node *ni;
   1080  1.1.1.4  dyoung 
   1081  1.1.1.4  dyoung 	/* XXX may want scanned nodes in the neighbor table for adhoc */
   1082  1.1.1.4  dyoung 	if (ic->ic_opmode == IEEE80211_M_STA ||
   1083  1.1.1.4  dyoung 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
   1084  1.1.1.4  dyoung 	    (ic->ic_flags & IEEE80211_F_SCAN))
   1085  1.1.1.4  dyoung 		nt = &ic->ic_scan;
   1086  1.1.1.4  dyoung 	else
   1087  1.1.1.4  dyoung 		nt = &ic->ic_sta;
   1088  1.1.1.4  dyoung 	/* XXX check ic_bss first in station mode */
   1089  1.1.1.4  dyoung 	/* XXX 4-address frames? */
   1090  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK(nt);
   1091  1.1.1.4  dyoung 	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
   1092  1.1.1.4  dyoung 		ni = _ieee80211_find_node(nt, wh->i_addr1);
   1093  1.1.1.4  dyoung 	else
   1094  1.1.1.4  dyoung 		ni = _ieee80211_find_node(nt, wh->i_addr2);
   1095  1.1.1.4  dyoung 	IEEE80211_NODE_UNLOCK(nt);
   1096  1.1.1.4  dyoung 
   1097  1.1.1.4  dyoung 	return (ni != NULL ? ni : ieee80211_ref_node(ic->ic_bss));
   1098  1.1.1.4  dyoung #undef IS_PSPOLL
   1099  1.1.1.4  dyoung #undef IS_CTL
   1100  1.1.1.4  dyoung }
   1101  1.1.1.4  dyoung 
   1102  1.1.1.4  dyoung /*
   1103  1.1.1.3  dyoung  * Return a reference to the appropriate node for sending
   1104  1.1.1.3  dyoung  * a data frame.  This handles node discovery in adhoc networks.
   1105  1.1.1.3  dyoung  */
   1106  1.1.1.3  dyoung struct ieee80211_node *
   1107  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1108  1.1.1.4  dyoung ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
   1109  1.1.1.4  dyoung 	const char *func, int line)
   1110  1.1.1.4  dyoung #else
   1111  1.1.1.4  dyoung ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
   1112  1.1.1.4  dyoung #endif
   1113  1.1.1.3  dyoung {
   1114  1.1.1.4  dyoung 	struct ieee80211_node_table *nt = &ic->ic_sta;
   1115  1.1.1.3  dyoung 	struct ieee80211_node *ni;
   1116  1.1.1.3  dyoung 
   1117  1.1.1.3  dyoung 	/*
   1118  1.1.1.3  dyoung 	 * The destination address should be in the node table
   1119  1.1.1.3  dyoung 	 * unless we are operating in station mode or this is a
   1120  1.1.1.3  dyoung 	 * multicast/broadcast frame.
   1121  1.1.1.3  dyoung 	 */
   1122  1.1.1.3  dyoung 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
   1123  1.1.1.4  dyoung 		return ieee80211_ref_node(ic->ic_bss);
   1124  1.1.1.3  dyoung 
   1125  1.1.1.3  dyoung 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
   1126  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK(nt);
   1127  1.1.1.4  dyoung 	ni = _ieee80211_find_node(nt, macaddr);
   1128  1.1.1.4  dyoung 	IEEE80211_NODE_UNLOCK(nt);
   1129  1.1.1.4  dyoung 
   1130  1.1.1.4  dyoung 	if (ni == NULL) {
   1131  1.1.1.4  dyoung 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
   1132  1.1.1.4  dyoung 		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
   1133  1.1.1.4  dyoung 			/*
   1134  1.1.1.4  dyoung 			 * In adhoc mode cons up a node for the destination.
   1135  1.1.1.4  dyoung 			 * Note that we need an additional reference for the
   1136  1.1.1.4  dyoung 			 * caller to be consistent with _ieee80211_find_node.
   1137  1.1.1.4  dyoung 			 */
   1138  1.1.1.4  dyoung 			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
   1139  1.1.1.4  dyoung 			if (ni != NULL)
   1140  1.1.1.4  dyoung 				(void) ieee80211_ref_node(ni);
   1141  1.1.1.4  dyoung 		} else {
   1142  1.1.1.4  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
   1143  1.1.1.4  dyoung 				"[%s] no node, discard frame (%s)\n",
   1144  1.1.1.4  dyoung 				ether_sprintf(macaddr), __func__);
   1145  1.1.1.4  dyoung 			ic->ic_stats.is_tx_nonode++;
   1146  1.1.1.3  dyoung 		}
   1147  1.1.1.3  dyoung 	}
   1148  1.1.1.3  dyoung 	return ni;
   1149  1.1.1.3  dyoung }
   1150  1.1.1.3  dyoung 
   1151  1.1.1.3  dyoung /*
   1152      1.1  dyoung  * Like find but search based on the channel too.
   1153      1.1  dyoung  */
   1154      1.1  dyoung struct ieee80211_node *
   1155  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1156  1.1.1.4  dyoung ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
   1157  1.1.1.4  dyoung 	const u_int8_t *macaddr, struct ieee80211_channel *chan,
   1158  1.1.1.4  dyoung 	const char *func, int line)
   1159  1.1.1.4  dyoung #else
   1160  1.1.1.4  dyoung ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
   1161  1.1.1.4  dyoung 	const u_int8_t *macaddr, struct ieee80211_channel *chan)
   1162  1.1.1.4  dyoung #endif
   1163      1.1  dyoung {
   1164      1.1  dyoung 	struct ieee80211_node *ni;
   1165      1.1  dyoung 	int hash;
   1166      1.1  dyoung 
   1167      1.1  dyoung 	hash = IEEE80211_NODE_HASH(macaddr);
   1168  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK(nt);
   1169  1.1.1.4  dyoung 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
   1170  1.1.1.3  dyoung 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
   1171  1.1.1.3  dyoung 		    ni->ni_chan == chan) {
   1172  1.1.1.4  dyoung 			ieee80211_ref_node(ni);		/* mark referenced */
   1173  1.1.1.4  dyoung 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
   1174  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1175  1.1.1.4  dyoung 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
   1176  1.1.1.4  dyoung 			    func, line,
   1177  1.1.1.4  dyoung #else
   1178  1.1.1.4  dyoung 			    "%s %p<%s> refcnt %d\n", __func__,
   1179  1.1.1.4  dyoung #endif
   1180  1.1.1.4  dyoung 			    ni, ether_sprintf(ni->ni_macaddr),
   1181  1.1.1.4  dyoung 			    ieee80211_node_refcnt(ni));
   1182      1.1  dyoung 			break;
   1183      1.1  dyoung 		}
   1184      1.1  dyoung 	}
   1185  1.1.1.4  dyoung 	IEEE80211_NODE_UNLOCK(nt);
   1186  1.1.1.4  dyoung 	return ni;
   1187  1.1.1.4  dyoung }
   1188  1.1.1.4  dyoung 
   1189  1.1.1.4  dyoung /*
   1190  1.1.1.4  dyoung  * Like find but search based on the ssid too.
   1191  1.1.1.4  dyoung  */
   1192  1.1.1.4  dyoung struct ieee80211_node *
   1193  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1194  1.1.1.4  dyoung ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
   1195  1.1.1.4  dyoung 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
   1196  1.1.1.4  dyoung 	const char *func, int line)
   1197  1.1.1.4  dyoung #else
   1198  1.1.1.4  dyoung ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
   1199  1.1.1.4  dyoung 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
   1200  1.1.1.4  dyoung #endif
   1201  1.1.1.4  dyoung {
   1202  1.1.1.5  dyoung #define	MATCH_SSID(ni, ssid, ssidlen) \
   1203  1.1.1.5  dyoung 	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
   1204  1.1.1.5  dyoung 	static const u_int8_t zeromac[IEEE80211_ADDR_LEN];
   1205  1.1.1.4  dyoung 	struct ieee80211com *ic = nt->nt_ic;
   1206  1.1.1.4  dyoung 	struct ieee80211_node *ni;
   1207  1.1.1.4  dyoung 	int hash;
   1208  1.1.1.4  dyoung 
   1209  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK(nt);
   1210  1.1.1.5  dyoung 	/*
   1211  1.1.1.5  dyoung 	 * A mac address that is all zero means match only the ssid;
   1212  1.1.1.5  dyoung 	 * otherwise we must match both.
   1213  1.1.1.5  dyoung 	 */
   1214  1.1.1.5  dyoung 	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
   1215  1.1.1.5  dyoung 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   1216  1.1.1.5  dyoung 			if (MATCH_SSID(ni, ssid, ssidlen))
   1217  1.1.1.5  dyoung 				break;
   1218  1.1.1.5  dyoung 		}
   1219  1.1.1.5  dyoung 	} else {
   1220  1.1.1.5  dyoung 		hash = IEEE80211_NODE_HASH(macaddr);
   1221  1.1.1.5  dyoung 		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
   1222  1.1.1.5  dyoung 			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
   1223  1.1.1.5  dyoung 			    MATCH_SSID(ni, ssid, ssidlen))
   1224  1.1.1.5  dyoung 				break;
   1225  1.1.1.5  dyoung 		}
   1226  1.1.1.5  dyoung 	}
   1227  1.1.1.5  dyoung 	if (ni != NULL) {
   1228  1.1.1.5  dyoung 		ieee80211_ref_node(ni);	/* mark referenced */
   1229  1.1.1.5  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1230  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1231  1.1.1.5  dyoung 		    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
   1232  1.1.1.5  dyoung 		    func, line,
   1233  1.1.1.4  dyoung #else
   1234  1.1.1.5  dyoung 		    "%s %p<%s> refcnt %d\n", __func__,
   1235  1.1.1.4  dyoung #endif
   1236  1.1.1.5  dyoung 		     ni, ether_sprintf(ni->ni_macaddr),
   1237  1.1.1.5  dyoung 		     ieee80211_node_refcnt(ni));
   1238  1.1.1.4  dyoung 	}
   1239  1.1.1.4  dyoung 	IEEE80211_NODE_UNLOCK(nt);
   1240      1.1  dyoung 	return ni;
   1241  1.1.1.5  dyoung #undef MATCH_SSID
   1242      1.1  dyoung }
   1243      1.1  dyoung 
   1244      1.1  dyoung static void
   1245  1.1.1.4  dyoung _ieee80211_free_node(struct ieee80211_node *ni)
   1246      1.1  dyoung {
   1247  1.1.1.4  dyoung 	struct ieee80211com *ic = ni->ni_ic;
   1248  1.1.1.4  dyoung 	struct ieee80211_node_table *nt = ni->ni_table;
   1249      1.1  dyoung 
   1250  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1251  1.1.1.4  dyoung 		"%s %p<%s> in %s table\n", __func__, ni,
   1252  1.1.1.4  dyoung 		ether_sprintf(ni->ni_macaddr),
   1253  1.1.1.4  dyoung 		nt != NULL ? nt->nt_name : "<gone>");
   1254  1.1.1.4  dyoung 
   1255  1.1.1.4  dyoung 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
   1256  1.1.1.4  dyoung 	if (nt != NULL) {
   1257  1.1.1.4  dyoung 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
   1258  1.1.1.4  dyoung 		LIST_REMOVE(ni, ni_hash);
   1259  1.1.1.4  dyoung 	}
   1260  1.1.1.4  dyoung 	ic->ic_node_free(ni);
   1261      1.1  dyoung }
   1262      1.1  dyoung 
   1263      1.1  dyoung void
   1264  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1265  1.1.1.4  dyoung ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
   1266  1.1.1.4  dyoung #else
   1267  1.1.1.4  dyoung ieee80211_free_node(struct ieee80211_node *ni)
   1268  1.1.1.4  dyoung #endif
   1269      1.1  dyoung {
   1270  1.1.1.4  dyoung 	struct ieee80211_node_table *nt = ni->ni_table;
   1271      1.1  dyoung 
   1272  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1273  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
   1274  1.1.1.4  dyoung 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
   1275  1.1.1.4  dyoung 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
   1276  1.1.1.4  dyoung #endif
   1277  1.1.1.4  dyoung 	if (ieee80211_node_dectestref(ni)) {
   1278  1.1.1.4  dyoung 		/*
   1279  1.1.1.4  dyoung 		 * Beware; if the node is marked gone then it's already
   1280  1.1.1.4  dyoung 		 * been removed from the table and we cannot assume the
   1281  1.1.1.4  dyoung 		 * table still exists.  Regardless, there's no need to lock
   1282  1.1.1.4  dyoung 		 * the table.
   1283  1.1.1.4  dyoung 		 */
   1284  1.1.1.4  dyoung 		if (ni->ni_table != NULL) {
   1285  1.1.1.4  dyoung 			IEEE80211_NODE_LOCK(nt);
   1286  1.1.1.4  dyoung 			_ieee80211_free_node(ni);
   1287  1.1.1.4  dyoung 			IEEE80211_NODE_UNLOCK(nt);
   1288  1.1.1.4  dyoung 		} else
   1289  1.1.1.4  dyoung 			_ieee80211_free_node(ni);
   1290      1.1  dyoung 	}
   1291      1.1  dyoung }
   1292      1.1  dyoung 
   1293  1.1.1.4  dyoung /*
   1294  1.1.1.4  dyoung  * Reclaim a node.  If this is the last reference count then
   1295  1.1.1.4  dyoung  * do the normal free work.  Otherwise remove it from the node
   1296  1.1.1.4  dyoung  * table and mark it gone by clearing the back-reference.
   1297  1.1.1.4  dyoung  */
   1298  1.1.1.4  dyoung static void
   1299  1.1.1.4  dyoung node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
   1300  1.1.1.4  dyoung {
   1301  1.1.1.4  dyoung 
   1302  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
   1303  1.1.1.4  dyoung 		"%s: remove %p<%s> from %s table, refcnt %d\n",
   1304  1.1.1.4  dyoung 		__func__, ni, ether_sprintf(ni->ni_macaddr),
   1305  1.1.1.4  dyoung 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
   1306  1.1.1.4  dyoung 	if (!ieee80211_node_dectestref(ni)) {
   1307  1.1.1.4  dyoung 		/*
   1308  1.1.1.4  dyoung 		 * Other references are present, just remove the
   1309  1.1.1.4  dyoung 		 * node from the table so it cannot be found.  When
   1310  1.1.1.4  dyoung 		 * the references are dropped storage will be
   1311  1.1.1.4  dyoung 		 * reclaimed.
   1312  1.1.1.4  dyoung 		 */
   1313  1.1.1.4  dyoung 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
   1314  1.1.1.4  dyoung 		LIST_REMOVE(ni, ni_hash);
   1315  1.1.1.4  dyoung 		ni->ni_table = NULL;		/* clear reference */
   1316  1.1.1.4  dyoung 	} else
   1317  1.1.1.4  dyoung 		_ieee80211_free_node(ni);
   1318  1.1.1.4  dyoung }
   1319  1.1.1.4  dyoung 
   1320  1.1.1.4  dyoung static void
   1321  1.1.1.4  dyoung ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
   1322      1.1  dyoung {
   1323  1.1.1.4  dyoung 	struct ieee80211com *ic = nt->nt_ic;
   1324      1.1  dyoung 	struct ieee80211_node *ni;
   1325      1.1  dyoung 
   1326  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1327  1.1.1.4  dyoung 		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
   1328  1.1.1.4  dyoung 
   1329  1.1.1.4  dyoung 	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
   1330  1.1.1.4  dyoung 		if (ni->ni_associd != 0) {
   1331  1.1.1.4  dyoung 			if (ic->ic_auth->ia_node_leave != NULL)
   1332  1.1.1.4  dyoung 				ic->ic_auth->ia_node_leave(ic, ni);
   1333  1.1.1.4  dyoung 			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
   1334  1.1.1.4  dyoung 		}
   1335  1.1.1.4  dyoung 		node_reclaim(nt, ni);
   1336  1.1.1.4  dyoung 	}
   1337  1.1.1.4  dyoung 	ieee80211_reset_erp(ic);
   1338  1.1.1.4  dyoung }
   1339  1.1.1.4  dyoung 
   1340  1.1.1.4  dyoung static void
   1341  1.1.1.4  dyoung ieee80211_free_allnodes(struct ieee80211_node_table *nt)
   1342  1.1.1.4  dyoung {
   1343  1.1.1.4  dyoung 
   1344  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK(nt);
   1345  1.1.1.4  dyoung 	ieee80211_free_allnodes_locked(nt);
   1346  1.1.1.4  dyoung 	IEEE80211_NODE_UNLOCK(nt);
   1347      1.1  dyoung }
   1348      1.1  dyoung 
   1349  1.1.1.2  dyoung /*
   1350  1.1.1.4  dyoung  * Timeout entries in the scan cache.
   1351  1.1.1.4  dyoung  */
   1352  1.1.1.4  dyoung static void
   1353  1.1.1.4  dyoung ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
   1354  1.1.1.4  dyoung {
   1355  1.1.1.4  dyoung 	struct ieee80211com *ic = nt->nt_ic;
   1356  1.1.1.4  dyoung 	struct ieee80211_node *ni, *tni;
   1357  1.1.1.4  dyoung 
   1358  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK(nt);
   1359  1.1.1.4  dyoung 	ni = ic->ic_bss;
   1360  1.1.1.4  dyoung 	/* XXX belongs elsewhere */
   1361  1.1.1.4  dyoung 	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
   1362  1.1.1.4  dyoung 		m_freem(ni->ni_rxfrag[0]);
   1363  1.1.1.4  dyoung 		ni->ni_rxfrag[0] = NULL;
   1364  1.1.1.4  dyoung 	}
   1365  1.1.1.4  dyoung 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
   1366  1.1.1.4  dyoung 		if (ni->ni_inact && --ni->ni_inact == 0) {
   1367  1.1.1.4  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1368  1.1.1.4  dyoung 			    "[%s] scan candidate purged from cache "
   1369  1.1.1.4  dyoung 			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
   1370  1.1.1.4  dyoung 			    ieee80211_node_refcnt(ni));
   1371  1.1.1.4  dyoung 			node_reclaim(nt, ni);
   1372  1.1.1.4  dyoung 		}
   1373  1.1.1.4  dyoung 	}
   1374  1.1.1.4  dyoung 	IEEE80211_NODE_UNLOCK(nt);
   1375  1.1.1.4  dyoung 
   1376  1.1.1.4  dyoung 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
   1377  1.1.1.4  dyoung }
   1378  1.1.1.4  dyoung 
   1379  1.1.1.4  dyoung /*
   1380  1.1.1.4  dyoung  * Timeout inactive stations and do related housekeeping.
   1381  1.1.1.4  dyoung  * Note that we cannot hold the node lock while sending a
   1382  1.1.1.4  dyoung  * frame as this would lead to a LOR.  Instead we use a
   1383  1.1.1.4  dyoung  * generation number to mark nodes that we've scanned and
   1384  1.1.1.4  dyoung  * drop the lock and restart a scan if we have to time out
   1385  1.1.1.4  dyoung  * a node.  Since we are single-threaded by virtue of
   1386  1.1.1.2  dyoung  * controlling the inactivity timer we can be sure this will
   1387  1.1.1.2  dyoung  * process each node only once.
   1388  1.1.1.2  dyoung  */
   1389  1.1.1.4  dyoung static void
   1390  1.1.1.4  dyoung ieee80211_timeout_stations(struct ieee80211_node_table *nt)
   1391      1.1  dyoung {
   1392  1.1.1.4  dyoung 	struct ieee80211com *ic = nt->nt_ic;
   1393  1.1.1.2  dyoung 	struct ieee80211_node *ni;
   1394  1.1.1.4  dyoung 	u_int gen;
   1395      1.1  dyoung 
   1396  1.1.1.4  dyoung 	IEEE80211_SCAN_LOCK(nt);
   1397  1.1.1.4  dyoung 	gen = nt->nt_scangen++;
   1398  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1399  1.1.1.4  dyoung 		"%s: %s scangen %u\n", __func__, nt->nt_name, gen);
   1400  1.1.1.2  dyoung restart:
   1401  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK(nt);
   1402  1.1.1.4  dyoung 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   1403  1.1.1.2  dyoung 		if (ni->ni_scangen == gen)	/* previously handled */
   1404  1.1.1.2  dyoung 			continue;
   1405  1.1.1.2  dyoung 		ni->ni_scangen = gen;
   1406  1.1.1.4  dyoung 		/*
   1407  1.1.1.5  dyoung 		 * Ignore entries for which have yet to receive an
   1408  1.1.1.5  dyoung 		 * authentication frame.  These are transient and
   1409  1.1.1.5  dyoung 		 * will be reclaimed when the last reference to them
   1410  1.1.1.5  dyoung 		 * goes away (when frame xmits complete).
   1411  1.1.1.5  dyoung 		 */
   1412  1.1.1.5  dyoung 		if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
   1413  1.1.1.5  dyoung 			continue;
   1414  1.1.1.5  dyoung 		/*
   1415  1.1.1.4  dyoung 		 * Free fragment if not needed anymore
   1416  1.1.1.4  dyoung 		 * (last fragment older than 1s).
   1417  1.1.1.4  dyoung 		 * XXX doesn't belong here
   1418  1.1.1.4  dyoung 		 */
   1419  1.1.1.4  dyoung 		if (ni->ni_rxfrag[0] != NULL &&
   1420  1.1.1.4  dyoung 		    ticks > ni->ni_rxfragstamp + hz) {
   1421  1.1.1.4  dyoung 			m_freem(ni->ni_rxfrag[0]);
   1422  1.1.1.4  dyoung 			ni->ni_rxfrag[0] = NULL;
   1423  1.1.1.4  dyoung 		}
   1424  1.1.1.4  dyoung 		/*
   1425  1.1.1.4  dyoung 		 * Special case ourself; we may be idle for extended periods
   1426  1.1.1.4  dyoung 		 * of time and regardless reclaiming our state is wrong.
   1427  1.1.1.4  dyoung 		 */
   1428  1.1.1.4  dyoung 		if (ni == ic->ic_bss)
   1429  1.1.1.4  dyoung 			continue;
   1430  1.1.1.4  dyoung 		ni->ni_inact--;
   1431  1.1.1.4  dyoung 		if (ni->ni_associd != 0) {
   1432      1.1  dyoung 			/*
   1433  1.1.1.4  dyoung 			 * Age frames on the power save queue. The
   1434  1.1.1.4  dyoung 			 * aging interval is 4 times the listen
   1435  1.1.1.4  dyoung 			 * interval specified by the station.  This
   1436  1.1.1.4  dyoung 			 * number is factored into the age calculations
   1437  1.1.1.4  dyoung 			 * when the frame is placed on the queue.  We
   1438  1.1.1.4  dyoung 			 * store ages as time differences we can check
   1439  1.1.1.4  dyoung 			 * and/or adjust only the head of the list.
   1440  1.1.1.4  dyoung 			 */
   1441  1.1.1.4  dyoung 			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
   1442  1.1.1.4  dyoung 				struct mbuf *m;
   1443  1.1.1.4  dyoung 				int discard = 0;
   1444  1.1.1.4  dyoung 
   1445  1.1.1.4  dyoung 				IEEE80211_NODE_SAVEQ_LOCK(ni);
   1446  1.1.1.4  dyoung 				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
   1447  1.1.1.4  dyoung 				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
   1448  1.1.1.4  dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
   1449  1.1.1.4  dyoung 					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
   1450  1.1.1.4  dyoung 					m_freem(m);
   1451  1.1.1.4  dyoung 					discard++;
   1452  1.1.1.4  dyoung 				}
   1453  1.1.1.4  dyoung 				if (m != NULL)
   1454  1.1.1.4  dyoung 					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
   1455  1.1.1.4  dyoung 				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
   1456  1.1.1.4  dyoung 
   1457  1.1.1.4  dyoung 				if (discard != 0) {
   1458  1.1.1.4  dyoung 					IEEE80211_DPRINTF(ic,
   1459  1.1.1.4  dyoung 					    IEEE80211_MSG_POWER,
   1460  1.1.1.4  dyoung 					    "[%s] discard %u frames for age\n",
   1461  1.1.1.4  dyoung 					    ether_sprintf(ni->ni_macaddr),
   1462  1.1.1.4  dyoung 					    discard);
   1463  1.1.1.4  dyoung 					IEEE80211_NODE_STAT_ADD(ni,
   1464  1.1.1.4  dyoung 						ps_discard, discard);
   1465  1.1.1.4  dyoung 					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
   1466  1.1.1.4  dyoung 						ic->ic_set_tim(ic, ni, 0);
   1467  1.1.1.4  dyoung 				}
   1468  1.1.1.4  dyoung 			}
   1469  1.1.1.4  dyoung 			/*
   1470  1.1.1.4  dyoung 			 * Probe the station before time it out.  We
   1471  1.1.1.4  dyoung 			 * send a null data frame which may not be
   1472  1.1.1.4  dyoung 			 * universally supported by drivers (need it
   1473  1.1.1.4  dyoung 			 * for ps-poll support so it should be...).
   1474  1.1.1.4  dyoung 			 */
   1475  1.1.1.4  dyoung 			if (0 < ni->ni_inact &&
   1476  1.1.1.4  dyoung 			    ni->ni_inact <= ic->ic_inact_probe) {
   1477  1.1.1.4  dyoung 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1478  1.1.1.4  dyoung 				    "[%s] probe station due to inactivity\n",
   1479  1.1.1.4  dyoung 				    ether_sprintf(ni->ni_macaddr));
   1480  1.1.1.4  dyoung 				IEEE80211_NODE_UNLOCK(nt);
   1481  1.1.1.4  dyoung 				ieee80211_send_nulldata(ic, ni);
   1482  1.1.1.4  dyoung 				/* XXX stat? */
   1483  1.1.1.4  dyoung 				goto restart;
   1484  1.1.1.4  dyoung 			}
   1485  1.1.1.4  dyoung 		}
   1486  1.1.1.4  dyoung 		if (ni->ni_inact <= 0) {
   1487  1.1.1.4  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1488  1.1.1.4  dyoung 			    "[%s] station timed out due to inactivity "
   1489  1.1.1.4  dyoung 			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
   1490  1.1.1.4  dyoung 			    ieee80211_node_refcnt(ni));
   1491  1.1.1.4  dyoung 			/*
   1492  1.1.1.4  dyoung 			 * Send a deauthenticate frame and drop the station.
   1493  1.1.1.4  dyoung 			 * This is somewhat complicated due to reference counts
   1494  1.1.1.4  dyoung 			 * and locking.  At this point a station will typically
   1495  1.1.1.4  dyoung 			 * have a reference count of 1.  ieee80211_node_leave
   1496  1.1.1.4  dyoung 			 * will do a "free" of the node which will drop the
   1497  1.1.1.4  dyoung 			 * reference count.  But in the meantime a reference
   1498  1.1.1.4  dyoung 			 * wil be held by the deauth frame.  The actual reclaim
   1499  1.1.1.4  dyoung 			 * of the node will happen either after the tx is
   1500  1.1.1.4  dyoung 			 * completed or by ieee80211_node_leave.
   1501  1.1.1.2  dyoung 			 *
   1502  1.1.1.4  dyoung 			 * Separately we must drop the node lock before sending
   1503  1.1.1.4  dyoung 			 * in case the driver takes a lock, as this will result
   1504  1.1.1.4  dyoung 			 * in  LOR between the node lock and the driver lock.
   1505      1.1  dyoung 			 */
   1506  1.1.1.4  dyoung 			IEEE80211_NODE_UNLOCK(nt);
   1507  1.1.1.4  dyoung 			if (ni->ni_associd != 0) {
   1508  1.1.1.4  dyoung 				IEEE80211_SEND_MGMT(ic, ni,
   1509  1.1.1.4  dyoung 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
   1510  1.1.1.4  dyoung 				    IEEE80211_REASON_AUTH_EXPIRE);
   1511  1.1.1.4  dyoung 			}
   1512  1.1.1.4  dyoung 			ieee80211_node_leave(ic, ni);
   1513  1.1.1.2  dyoung 			ic->ic_stats.is_node_timeout++;
   1514  1.1.1.2  dyoung 			goto restart;
   1515  1.1.1.2  dyoung 		}
   1516      1.1  dyoung 	}
   1517  1.1.1.4  dyoung 	IEEE80211_NODE_UNLOCK(nt);
   1518  1.1.1.4  dyoung 
   1519  1.1.1.4  dyoung 	IEEE80211_SCAN_UNLOCK(nt);
   1520  1.1.1.4  dyoung 
   1521  1.1.1.4  dyoung 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
   1522  1.1.1.4  dyoung }
   1523  1.1.1.4  dyoung 
   1524  1.1.1.4  dyoung void
   1525  1.1.1.4  dyoung ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
   1526  1.1.1.4  dyoung {
   1527  1.1.1.4  dyoung 	struct ieee80211_node *ni;
   1528  1.1.1.4  dyoung 	u_int gen;
   1529  1.1.1.4  dyoung 
   1530  1.1.1.4  dyoung 	IEEE80211_SCAN_LOCK(nt);
   1531  1.1.1.4  dyoung 	gen = nt->nt_scangen++;
   1532  1.1.1.4  dyoung restart:
   1533  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK(nt);
   1534  1.1.1.4  dyoung 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   1535  1.1.1.4  dyoung 		if (ni->ni_scangen != gen) {
   1536  1.1.1.4  dyoung 			ni->ni_scangen = gen;
   1537  1.1.1.4  dyoung 			(void) ieee80211_ref_node(ni);
   1538  1.1.1.4  dyoung 			IEEE80211_NODE_UNLOCK(nt);
   1539  1.1.1.4  dyoung 			(*f)(arg, ni);
   1540  1.1.1.4  dyoung 			ieee80211_free_node(ni);
   1541  1.1.1.4  dyoung 			goto restart;
   1542  1.1.1.4  dyoung 		}
   1543  1.1.1.4  dyoung 	}
   1544  1.1.1.4  dyoung 	IEEE80211_NODE_UNLOCK(nt);
   1545  1.1.1.4  dyoung 
   1546  1.1.1.4  dyoung 	IEEE80211_SCAN_UNLOCK(nt);
   1547  1.1.1.4  dyoung }
   1548  1.1.1.4  dyoung 
   1549  1.1.1.4  dyoung void
   1550  1.1.1.4  dyoung ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
   1551  1.1.1.4  dyoung {
   1552  1.1.1.4  dyoung 	printf("0x%p: mac %s refcnt %d\n", ni,
   1553  1.1.1.4  dyoung 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
   1554  1.1.1.4  dyoung 	printf("\tscangen %u authmode %u flags 0x%x\n",
   1555  1.1.1.4  dyoung 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
   1556  1.1.1.4  dyoung 	printf("\tassocid 0x%x txpower %u vlan %u\n",
   1557  1.1.1.4  dyoung 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
   1558  1.1.1.4  dyoung 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
   1559  1.1.1.4  dyoung 		ni->ni_txseqs[0],
   1560  1.1.1.4  dyoung 		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
   1561  1.1.1.4  dyoung 		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
   1562  1.1.1.4  dyoung 		ni->ni_rxfragstamp);
   1563  1.1.1.4  dyoung 	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
   1564  1.1.1.4  dyoung 		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
   1565  1.1.1.4  dyoung 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
   1566  1.1.1.4  dyoung 		ether_sprintf(ni->ni_bssid),
   1567  1.1.1.4  dyoung 		ni->ni_esslen, ni->ni_essid,
   1568  1.1.1.4  dyoung 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
   1569  1.1.1.4  dyoung 	printf("\tfails %u inact %u txrate %u\n",
   1570  1.1.1.4  dyoung 		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
   1571  1.1.1.4  dyoung }
   1572  1.1.1.4  dyoung 
   1573  1.1.1.4  dyoung void
   1574  1.1.1.4  dyoung ieee80211_dump_nodes(struct ieee80211_node_table *nt)
   1575  1.1.1.4  dyoung {
   1576  1.1.1.4  dyoung 	ieee80211_iterate_nodes(nt,
   1577  1.1.1.4  dyoung 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
   1578  1.1.1.4  dyoung }
   1579  1.1.1.4  dyoung 
   1580  1.1.1.4  dyoung /*
   1581  1.1.1.4  dyoung  * Handle a station joining an 11g network.
   1582  1.1.1.4  dyoung  */
   1583  1.1.1.4  dyoung static void
   1584  1.1.1.4  dyoung ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
   1585  1.1.1.4  dyoung {
   1586  1.1.1.4  dyoung 
   1587  1.1.1.4  dyoung 	/*
   1588  1.1.1.4  dyoung 	 * Station isn't capable of short slot time.  Bump
   1589  1.1.1.4  dyoung 	 * the count of long slot time stations and disable
   1590  1.1.1.4  dyoung 	 * use of short slot time.  Note that the actual switch
   1591  1.1.1.4  dyoung 	 * over to long slot time use may not occur until the
   1592  1.1.1.4  dyoung 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
   1593  1.1.1.4  dyoung 	 */
   1594  1.1.1.4  dyoung 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
   1595  1.1.1.4  dyoung 		ic->ic_longslotsta++;
   1596  1.1.1.4  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1597  1.1.1.4  dyoung 		    "[%s] station needs long slot time, count %d\n",
   1598  1.1.1.4  dyoung 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
   1599  1.1.1.4  dyoung 		/* XXX vap's w/ conflicting needs won't work */
   1600  1.1.1.4  dyoung 		ieee80211_set_shortslottime(ic, 0);
   1601  1.1.1.4  dyoung 	}
   1602  1.1.1.4  dyoung 	/*
   1603  1.1.1.4  dyoung 	 * If the new station is not an ERP station
   1604  1.1.1.4  dyoung 	 * then bump the counter and enable protection
   1605  1.1.1.4  dyoung 	 * if configured.
   1606  1.1.1.4  dyoung 	 */
   1607  1.1.1.4  dyoung 	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
   1608  1.1.1.4  dyoung 		ic->ic_nonerpsta++;
   1609  1.1.1.4  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1610  1.1.1.4  dyoung 		    "[%s] station is !ERP, %d non-ERP stations associated\n",
   1611  1.1.1.4  dyoung 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
   1612  1.1.1.4  dyoung 		/*
   1613  1.1.1.4  dyoung 		 * If protection is configured, enable it.
   1614  1.1.1.4  dyoung 		 */
   1615  1.1.1.4  dyoung 		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
   1616  1.1.1.4  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1617  1.1.1.4  dyoung 			    "%s: enable use of protection\n", __func__);
   1618  1.1.1.4  dyoung 			ic->ic_flags |= IEEE80211_F_USEPROT;
   1619  1.1.1.4  dyoung 		}
   1620  1.1.1.4  dyoung 		/*
   1621  1.1.1.4  dyoung 		 * If station does not support short preamble
   1622  1.1.1.4  dyoung 		 * then we must enable use of Barker preamble.
   1623  1.1.1.4  dyoung 		 */
   1624  1.1.1.4  dyoung 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
   1625  1.1.1.4  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1626  1.1.1.4  dyoung 			    "[%s] station needs long preamble\n",
   1627  1.1.1.4  dyoung 			    ether_sprintf(ni->ni_macaddr));
   1628  1.1.1.4  dyoung 			ic->ic_flags |= IEEE80211_F_USEBARKER;
   1629  1.1.1.4  dyoung 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
   1630  1.1.1.4  dyoung 		}
   1631  1.1.1.4  dyoung 	} else
   1632  1.1.1.4  dyoung 		ni->ni_flags |= IEEE80211_NODE_ERP;
   1633  1.1.1.4  dyoung }
   1634  1.1.1.4  dyoung 
   1635  1.1.1.4  dyoung void
   1636  1.1.1.4  dyoung ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
   1637  1.1.1.4  dyoung {
   1638  1.1.1.4  dyoung 	int newassoc;
   1639  1.1.1.4  dyoung 
   1640  1.1.1.4  dyoung 	if (ni->ni_associd == 0) {
   1641  1.1.1.4  dyoung 		u_int16_t aid;
   1642  1.1.1.4  dyoung 
   1643  1.1.1.4  dyoung 		/*
   1644  1.1.1.4  dyoung 		 * It would be good to search the bitmap
   1645  1.1.1.4  dyoung 		 * more efficiently, but this will do for now.
   1646  1.1.1.4  dyoung 		 */
   1647  1.1.1.4  dyoung 		for (aid = 1; aid < ic->ic_max_aid; aid++) {
   1648  1.1.1.4  dyoung 			if (!IEEE80211_AID_ISSET(aid,
   1649  1.1.1.4  dyoung 			    ic->ic_aid_bitmap))
   1650  1.1.1.4  dyoung 				break;
   1651  1.1.1.4  dyoung 		}
   1652  1.1.1.4  dyoung 		if (aid >= ic->ic_max_aid) {
   1653  1.1.1.4  dyoung 			IEEE80211_SEND_MGMT(ic, ni, resp,
   1654  1.1.1.4  dyoung 			    IEEE80211_REASON_ASSOC_TOOMANY);
   1655  1.1.1.4  dyoung 			ieee80211_node_leave(ic, ni);
   1656  1.1.1.4  dyoung 			return;
   1657  1.1.1.4  dyoung 		}
   1658  1.1.1.4  dyoung 		ni->ni_associd = aid | 0xc000;
   1659  1.1.1.4  dyoung 		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
   1660  1.1.1.4  dyoung 		ic->ic_sta_assoc++;
   1661  1.1.1.4  dyoung 		newassoc = 1;
   1662  1.1.1.4  dyoung 		if (ic->ic_curmode == IEEE80211_MODE_11G ||
   1663  1.1.1.4  dyoung 		    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
   1664  1.1.1.4  dyoung 			ieee80211_node_join_11g(ic, ni);
   1665  1.1.1.4  dyoung 	} else
   1666  1.1.1.4  dyoung 		newassoc = 0;
   1667  1.1.1.4  dyoung 
   1668  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
   1669  1.1.1.4  dyoung 	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
   1670  1.1.1.4  dyoung 	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
   1671  1.1.1.4  dyoung 	    IEEE80211_NODE_AID(ni),
   1672  1.1.1.4  dyoung 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
   1673  1.1.1.4  dyoung 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
   1674  1.1.1.4  dyoung 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
   1675  1.1.1.4  dyoung 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
   1676  1.1.1.4  dyoung 	);
   1677  1.1.1.4  dyoung 
   1678  1.1.1.4  dyoung 	/* give driver a chance to setup state like ni_txrate */
   1679  1.1.1.4  dyoung 	if (ic->ic_newassoc != NULL)
   1680  1.1.1.4  dyoung 		ic->ic_newassoc(ic, ni, newassoc);
   1681  1.1.1.4  dyoung 	ni->ni_inact_reload = ic->ic_inact_auth;
   1682  1.1.1.4  dyoung 	ni->ni_inact = ni->ni_inact_reload;
   1683  1.1.1.4  dyoung 	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
   1684  1.1.1.4  dyoung 	/* tell the authenticator about new station */
   1685  1.1.1.4  dyoung 	if (ic->ic_auth->ia_node_join != NULL)
   1686  1.1.1.4  dyoung 		ic->ic_auth->ia_node_join(ic, ni);
   1687  1.1.1.4  dyoung 	ieee80211_notify_node_join(ic, ni, newassoc);
   1688  1.1.1.4  dyoung }
   1689  1.1.1.4  dyoung 
   1690  1.1.1.4  dyoung /*
   1691  1.1.1.4  dyoung  * Handle a station leaving an 11g network.
   1692  1.1.1.4  dyoung  */
   1693  1.1.1.4  dyoung static void
   1694  1.1.1.4  dyoung ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
   1695  1.1.1.4  dyoung {
   1696  1.1.1.4  dyoung 
   1697  1.1.1.4  dyoung 	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G ||
   1698  1.1.1.4  dyoung 		ic->ic_curmode == IEEE80211_MODE_TURBO_G,
   1699  1.1.1.4  dyoung 		("not in 11g, curmode %x", ic->ic_curmode));
   1700  1.1.1.4  dyoung 
   1701  1.1.1.4  dyoung 	/*
   1702  1.1.1.4  dyoung 	 * If a long slot station do the slot time bookkeeping.
   1703  1.1.1.4  dyoung 	 */
   1704  1.1.1.4  dyoung 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
   1705  1.1.1.4  dyoung 		KASSERT(ic->ic_longslotsta > 0,
   1706  1.1.1.4  dyoung 		    ("bogus long slot station count %d", ic->ic_longslotsta));
   1707  1.1.1.4  dyoung 		ic->ic_longslotsta--;
   1708  1.1.1.4  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1709  1.1.1.4  dyoung 		    "[%s] long slot time station leaves, count now %d\n",
   1710  1.1.1.4  dyoung 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
   1711  1.1.1.4  dyoung 		if (ic->ic_longslotsta == 0) {
   1712  1.1.1.4  dyoung 			/*
   1713  1.1.1.4  dyoung 			 * Re-enable use of short slot time if supported
   1714  1.1.1.4  dyoung 			 * and not operating in IBSS mode (per spec).
   1715  1.1.1.4  dyoung 			 */
   1716  1.1.1.4  dyoung 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
   1717  1.1.1.4  dyoung 			    ic->ic_opmode != IEEE80211_M_IBSS) {
   1718  1.1.1.4  dyoung 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1719  1.1.1.4  dyoung 				    "%s: re-enable use of short slot time\n",
   1720  1.1.1.4  dyoung 				    __func__);
   1721  1.1.1.4  dyoung 				ieee80211_set_shortslottime(ic, 1);
   1722  1.1.1.4  dyoung 			}
   1723  1.1.1.4  dyoung 		}
   1724  1.1.1.4  dyoung 	}
   1725  1.1.1.4  dyoung 	/*
   1726  1.1.1.4  dyoung 	 * If a non-ERP station do the protection-related bookkeeping.
   1727  1.1.1.4  dyoung 	 */
   1728  1.1.1.4  dyoung 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
   1729  1.1.1.4  dyoung 		KASSERT(ic->ic_nonerpsta > 0,
   1730  1.1.1.4  dyoung 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
   1731  1.1.1.4  dyoung 		ic->ic_nonerpsta--;
   1732  1.1.1.4  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1733  1.1.1.4  dyoung 		    "[%s] non-ERP station leaves, count now %d\n",
   1734  1.1.1.4  dyoung 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
   1735  1.1.1.4  dyoung 		if (ic->ic_nonerpsta == 0) {
   1736  1.1.1.4  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1737  1.1.1.4  dyoung 				"%s: disable use of protection\n", __func__);
   1738  1.1.1.4  dyoung 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
   1739  1.1.1.4  dyoung 			/* XXX verify mode? */
   1740  1.1.1.4  dyoung 			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
   1741  1.1.1.4  dyoung 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1742  1.1.1.4  dyoung 				    "%s: re-enable use of short preamble\n",
   1743  1.1.1.4  dyoung 				    __func__);
   1744  1.1.1.4  dyoung 				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
   1745  1.1.1.4  dyoung 				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
   1746  1.1.1.4  dyoung 			}
   1747  1.1.1.4  dyoung 		}
   1748  1.1.1.4  dyoung 	}
   1749      1.1  dyoung }
   1750      1.1  dyoung 
   1751  1.1.1.4  dyoung /*
   1752  1.1.1.4  dyoung  * Handle bookkeeping for station deauthentication/disassociation
   1753  1.1.1.4  dyoung  * when operating as an ap.
   1754  1.1.1.4  dyoung  */
   1755      1.1  dyoung void
   1756  1.1.1.4  dyoung ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
   1757      1.1  dyoung {
   1758  1.1.1.4  dyoung 	struct ieee80211_node_table *nt = ni->ni_table;
   1759  1.1.1.4  dyoung 
   1760  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
   1761  1.1.1.4  dyoung 	    "[%s] station with aid %d leaves\n",
   1762  1.1.1.4  dyoung 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
   1763  1.1.1.4  dyoung 
   1764  1.1.1.4  dyoung 	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
   1765  1.1.1.4  dyoung 		ic->ic_opmode == IEEE80211_M_IBSS ||
   1766  1.1.1.4  dyoung 		ic->ic_opmode == IEEE80211_M_AHDEMO,
   1767  1.1.1.4  dyoung 		("unexpected operating mode %u", ic->ic_opmode));
   1768  1.1.1.4  dyoung 	/*
   1769  1.1.1.4  dyoung 	 * If node wasn't previously associated all
   1770  1.1.1.4  dyoung 	 * we need to do is reclaim the reference.
   1771  1.1.1.4  dyoung 	 */
   1772  1.1.1.4  dyoung 	/* XXX ibss mode bypasses 11g and notification */
   1773  1.1.1.4  dyoung 	if (ni->ni_associd == 0)
   1774  1.1.1.4  dyoung 		goto done;
   1775  1.1.1.4  dyoung 	/*
   1776  1.1.1.4  dyoung 	 * Tell the authenticator the station is leaving.
   1777  1.1.1.4  dyoung 	 * Note that we must do this before yanking the
   1778  1.1.1.4  dyoung 	 * association id as the authenticator uses the
   1779  1.1.1.4  dyoung 	 * associd to locate it's state block.
   1780  1.1.1.4  dyoung 	 */
   1781  1.1.1.4  dyoung 	if (ic->ic_auth->ia_node_leave != NULL)
   1782  1.1.1.4  dyoung 		ic->ic_auth->ia_node_leave(ic, ni);
   1783  1.1.1.4  dyoung 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
   1784  1.1.1.4  dyoung 	ni->ni_associd = 0;
   1785  1.1.1.4  dyoung 	ic->ic_sta_assoc--;
   1786  1.1.1.4  dyoung 
   1787  1.1.1.4  dyoung 	if (ic->ic_curmode == IEEE80211_MODE_11G ||
   1788  1.1.1.4  dyoung 	    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
   1789  1.1.1.4  dyoung 		ieee80211_node_leave_11g(ic, ni);
   1790  1.1.1.4  dyoung 	/*
   1791  1.1.1.4  dyoung 	 * Cleanup station state.  In particular clear various
   1792  1.1.1.4  dyoung 	 * state that might otherwise be reused if the node
   1793  1.1.1.4  dyoung 	 * is reused before the reference count goes to zero
   1794  1.1.1.4  dyoung 	 * (and memory is reclaimed).
   1795  1.1.1.4  dyoung 	 */
   1796  1.1.1.4  dyoung 	ieee80211_sta_leave(ic, ni);
   1797  1.1.1.4  dyoung done:
   1798  1.1.1.4  dyoung 	/*
   1799  1.1.1.4  dyoung 	 * Remove the node from any table it's recorded in and
   1800  1.1.1.4  dyoung 	 * drop the caller's reference.  Removal from the table
   1801  1.1.1.4  dyoung 	 * is important to insure the node is not reprocessed
   1802  1.1.1.4  dyoung 	 * for inactivity.
   1803  1.1.1.4  dyoung 	 */
   1804  1.1.1.4  dyoung 	if (nt != NULL) {
   1805  1.1.1.4  dyoung 		IEEE80211_NODE_LOCK(nt);
   1806  1.1.1.4  dyoung 		node_reclaim(nt, ni);
   1807  1.1.1.4  dyoung 		IEEE80211_NODE_UNLOCK(nt);
   1808  1.1.1.4  dyoung 	} else
   1809  1.1.1.4  dyoung 		ieee80211_free_node(ni);
   1810  1.1.1.4  dyoung }
   1811  1.1.1.4  dyoung 
   1812  1.1.1.4  dyoung u_int8_t
   1813  1.1.1.4  dyoung ieee80211_getrssi(struct ieee80211com *ic)
   1814  1.1.1.4  dyoung {
   1815  1.1.1.4  dyoung #define	NZ(x)	((x) == 0 ? 1 : (x))
   1816  1.1.1.4  dyoung 	struct ieee80211_node_table *nt = &ic->ic_sta;
   1817  1.1.1.4  dyoung 	u_int32_t rssi_samples, rssi_total;
   1818      1.1  dyoung 	struct ieee80211_node *ni;
   1819      1.1  dyoung 
   1820  1.1.1.4  dyoung 	rssi_total = 0;
   1821  1.1.1.4  dyoung 	rssi_samples = 0;
   1822  1.1.1.4  dyoung 	switch (ic->ic_opmode) {
   1823  1.1.1.4  dyoung 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
   1824  1.1.1.4  dyoung 		/* XXX locking */
   1825  1.1.1.4  dyoung 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
   1826  1.1.1.4  dyoung 			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
   1827  1.1.1.4  dyoung 				rssi_samples++;
   1828  1.1.1.4  dyoung 				rssi_total += ic->ic_node_getrssi(ni);
   1829  1.1.1.4  dyoung 			}
   1830  1.1.1.4  dyoung 		break;
   1831  1.1.1.4  dyoung 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
   1832  1.1.1.4  dyoung 		/* XXX locking */
   1833  1.1.1.4  dyoung 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   1834  1.1.1.4  dyoung 			rssi_samples++;
   1835  1.1.1.4  dyoung 			rssi_total += ic->ic_node_getrssi(ni);
   1836  1.1.1.4  dyoung 		}
   1837  1.1.1.4  dyoung 		break;
   1838  1.1.1.4  dyoung 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
   1839  1.1.1.4  dyoung 		/* XXX locking */
   1840  1.1.1.4  dyoung 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
   1841  1.1.1.4  dyoung 			if (IEEE80211_AID(ni->ni_associd) != 0) {
   1842  1.1.1.4  dyoung 				rssi_samples++;
   1843  1.1.1.4  dyoung 				rssi_total += ic->ic_node_getrssi(ni);
   1844  1.1.1.4  dyoung 			}
   1845  1.1.1.4  dyoung 		break;
   1846  1.1.1.4  dyoung 	case IEEE80211_M_MONITOR:	/* XXX */
   1847  1.1.1.4  dyoung 	case IEEE80211_M_STA:		/* use stats from associated ap */
   1848  1.1.1.4  dyoung 	default:
   1849  1.1.1.4  dyoung 		if (ic->ic_bss != NULL)
   1850  1.1.1.4  dyoung 			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
   1851  1.1.1.4  dyoung 		rssi_samples = 1;
   1852  1.1.1.4  dyoung 		break;
   1853  1.1.1.4  dyoung 	}
   1854  1.1.1.4  dyoung 	return rssi_total / NZ(rssi_samples);
   1855  1.1.1.4  dyoung #undef NZ
   1856  1.1.1.4  dyoung }
   1857  1.1.1.4  dyoung 
   1858  1.1.1.4  dyoung /*
   1859  1.1.1.4  dyoung  * Indicate whether there are frames queued for a station in power-save mode.
   1860  1.1.1.4  dyoung  */
   1861  1.1.1.4  dyoung static void
   1862  1.1.1.4  dyoung ieee80211_set_tim(struct ieee80211com *ic, struct ieee80211_node *ni, int set)
   1863  1.1.1.4  dyoung {
   1864  1.1.1.4  dyoung 	u_int16_t aid;
   1865  1.1.1.4  dyoung 
   1866  1.1.1.4  dyoung 	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
   1867  1.1.1.4  dyoung 		ic->ic_opmode == IEEE80211_M_IBSS,
   1868  1.1.1.4  dyoung 		("operating mode %u", ic->ic_opmode));
   1869  1.1.1.4  dyoung 
   1870  1.1.1.4  dyoung 	aid = IEEE80211_AID(ni->ni_associd);
   1871  1.1.1.4  dyoung 	KASSERT(aid < ic->ic_max_aid,
   1872  1.1.1.4  dyoung 		("bogus aid %u, max %u", aid, ic->ic_max_aid));
   1873  1.1.1.4  dyoung 
   1874  1.1.1.4  dyoung 	IEEE80211_BEACON_LOCK(ic);
   1875  1.1.1.4  dyoung 	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
   1876  1.1.1.4  dyoung 		if (set) {
   1877  1.1.1.4  dyoung 			setbit(ic->ic_tim_bitmap, aid);
   1878  1.1.1.4  dyoung 			ic->ic_ps_pending++;
   1879  1.1.1.4  dyoung 		} else {
   1880  1.1.1.4  dyoung 			clrbit(ic->ic_tim_bitmap, aid);
   1881  1.1.1.4  dyoung 			ic->ic_ps_pending--;
   1882  1.1.1.4  dyoung 		}
   1883  1.1.1.4  dyoung 		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
   1884  1.1.1.4  dyoung 	}
   1885  1.1.1.4  dyoung 	IEEE80211_BEACON_UNLOCK(ic);
   1886  1.1.1.4  dyoung }
   1887  1.1.1.4  dyoung 
   1888  1.1.1.4  dyoung /*
   1889  1.1.1.4  dyoung  * Node table support.
   1890  1.1.1.4  dyoung  */
   1891  1.1.1.4  dyoung 
   1892  1.1.1.4  dyoung static void
   1893  1.1.1.4  dyoung ieee80211_node_table_init(struct ieee80211com *ic,
   1894  1.1.1.4  dyoung 	struct ieee80211_node_table *nt,
   1895  1.1.1.4  dyoung 	const char *name, int inact,
   1896  1.1.1.4  dyoung 	void (*timeout)(struct ieee80211_node_table *))
   1897  1.1.1.4  dyoung {
   1898  1.1.1.4  dyoung 
   1899  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1900  1.1.1.4  dyoung 		"%s %s table, inact %u\n", __func__, name, inact);
   1901  1.1.1.4  dyoung 
   1902  1.1.1.4  dyoung 	nt->nt_ic = ic;
   1903  1.1.1.4  dyoung 	/* XXX need unit */
   1904  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
   1905  1.1.1.4  dyoung 	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
   1906  1.1.1.4  dyoung 	TAILQ_INIT(&nt->nt_node);
   1907  1.1.1.4  dyoung 	nt->nt_name = name;
   1908  1.1.1.4  dyoung 	nt->nt_scangen = 1;
   1909  1.1.1.4  dyoung 	nt->nt_inact_init = inact;
   1910  1.1.1.4  dyoung 	nt->nt_timeout = timeout;
   1911  1.1.1.4  dyoung }
   1912  1.1.1.4  dyoung 
   1913  1.1.1.4  dyoung void
   1914  1.1.1.4  dyoung ieee80211_node_table_reset(struct ieee80211_node_table *nt)
   1915  1.1.1.4  dyoung {
   1916  1.1.1.4  dyoung 
   1917  1.1.1.4  dyoung 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
   1918  1.1.1.4  dyoung 		"%s %s table\n", __func__, nt->nt_name);
   1919  1.1.1.4  dyoung 
   1920  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK(nt);
   1921  1.1.1.4  dyoung 	nt->nt_inact_timer = 0;
   1922  1.1.1.4  dyoung 	ieee80211_free_allnodes_locked(nt);
   1923  1.1.1.4  dyoung 	IEEE80211_NODE_UNLOCK(nt);
   1924  1.1.1.4  dyoung }
   1925  1.1.1.4  dyoung 
   1926  1.1.1.4  dyoung static void
   1927  1.1.1.4  dyoung ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
   1928  1.1.1.4  dyoung {
   1929  1.1.1.4  dyoung 
   1930  1.1.1.4  dyoung 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
   1931  1.1.1.4  dyoung 		"%s %s table\n", __func__, nt->nt_name);
   1932  1.1.1.4  dyoung 
   1933  1.1.1.4  dyoung 	ieee80211_free_allnodes_locked(nt);
   1934  1.1.1.4  dyoung 	IEEE80211_SCAN_LOCK_DESTROY(nt);
   1935  1.1.1.4  dyoung 	IEEE80211_NODE_LOCK_DESTROY(nt);
   1936      1.1  dyoung }
   1937