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