xref: /freebsd/sys/net80211/ieee80211_node.c (revision 49a152366e008c20b795c0c86c701b11bb416230)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
31f1d7810SSam Leffler  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
147535e66aSSam Leffler  * 3. The name of the author may not be used to endorse or promote products
157535e66aSSam Leffler  *    derived from this software without specific prior written permission.
161a1e1d21SSam Leffler  *
171a1e1d21SSam Leffler  * Alternatively, this software may be distributed under the terms of the
181a1e1d21SSam Leffler  * GNU General Public License ("GPL") version 2 as published by the Free
191a1e1d21SSam Leffler  * Software Foundation.
201a1e1d21SSam Leffler  *
217535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
227535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
237535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
247535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
257535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
267535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
277535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
287535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
297535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
307535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311a1e1d21SSam Leffler  */
321a1e1d21SSam Leffler 
331a1e1d21SSam Leffler #include <sys/cdefs.h>
341a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
351a1e1d21SSam Leffler 
361a1e1d21SSam Leffler #include <sys/param.h>
371a1e1d21SSam Leffler #include <sys/systm.h>
381a1e1d21SSam Leffler #include <sys/mbuf.h>
391a1e1d21SSam Leffler #include <sys/malloc.h>
401a1e1d21SSam Leffler #include <sys/kernel.h>
411a1e1d21SSam Leffler 
428a1b9b6aSSam Leffler #include <sys/socket.h>
431a1e1d21SSam Leffler 
441a1e1d21SSam Leffler #include <net/if.h>
451a1e1d21SSam Leffler #include <net/if_media.h>
461a1e1d21SSam Leffler #include <net/ethernet.h>
471a1e1d21SSam Leffler 
481a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
491a1e1d21SSam Leffler 
501a1e1d21SSam Leffler #include <net/bpf.h>
511a1e1d21SSam Leffler 
528a1b9b6aSSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
538a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *);
548a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *);
558a1b9b6aSSam Leffler static u_int8_t node_getrssi(const struct ieee80211_node *);
561a1e1d21SSam Leffler 
578a1b9b6aSSam Leffler static void ieee80211_setup_node(struct ieee80211_node_table *,
588a1b9b6aSSam Leffler 		struct ieee80211_node *, const u_int8_t *);
598a1b9b6aSSam Leffler static void _ieee80211_free_node(struct ieee80211_node *);
608a1b9b6aSSam Leffler static void ieee80211_free_allnodes(struct ieee80211_node_table *);
61d1e61976SSam Leffler 
628a1b9b6aSSam Leffler static void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
638a1b9b6aSSam Leffler static void ieee80211_timeout_stations(struct ieee80211_node_table *);
648a1b9b6aSSam Leffler 
658a1b9b6aSSam Leffler static void ieee80211_set_tim(struct ieee80211com *,
668a1b9b6aSSam Leffler 		struct ieee80211_node *, int set);
678a1b9b6aSSam Leffler 
688a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic,
698a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt, const char *name, int inact,
708a1b9b6aSSam Leffler 	void (*timeout)(struct ieee80211_node_table *));
718a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
721a1e1d21SSam Leffler 
7332346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
7437c150c4SSam Leffler 
751a1e1d21SSam Leffler void
768a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic)
771a1e1d21SSam Leffler {
781a1e1d21SSam Leffler 
79acc4f7f5SSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
80acc4f7f5SSam Leffler 		IEEE80211_INACT_INIT, ieee80211_timeout_stations);
818a1b9b6aSSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
828a1b9b6aSSam Leffler 		IEEE80211_INACT_SCAN, ieee80211_timeout_scan_candidates);
838a1b9b6aSSam Leffler 
848a1b9b6aSSam Leffler 	ic->ic_node_alloc = node_alloc;
858a1b9b6aSSam Leffler 	ic->ic_node_free = node_free;
868a1b9b6aSSam Leffler 	ic->ic_node_cleanup = node_cleanup;
878a1b9b6aSSam Leffler 	ic->ic_node_getrssi = node_getrssi;
888a1b9b6aSSam Leffler 
898a1b9b6aSSam Leffler 	/* default station inactivity timer setings */
908a1b9b6aSSam Leffler 	ic->ic_inact_init = IEEE80211_INACT_INIT;
918a1b9b6aSSam Leffler 	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
928a1b9b6aSSam Leffler 	ic->ic_inact_run = IEEE80211_INACT_RUN;
938a1b9b6aSSam Leffler 	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
948a1b9b6aSSam Leffler 
958a1b9b6aSSam Leffler 	/* XXX defer */
968a1b9b6aSSam Leffler 	if (ic->ic_max_aid == 0)
978a1b9b6aSSam Leffler 		ic->ic_max_aid = IEEE80211_AID_DEF;
988a1b9b6aSSam Leffler 	else if (ic->ic_max_aid > IEEE80211_AID_MAX)
998a1b9b6aSSam Leffler 		ic->ic_max_aid = IEEE80211_AID_MAX;
1008a1b9b6aSSam Leffler 	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
1018a1b9b6aSSam Leffler 		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
1028a1b9b6aSSam Leffler 		M_DEVBUF, M_NOWAIT | M_ZERO);
1038a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap == NULL) {
1048a1b9b6aSSam Leffler 		/* XXX no way to recover */
1058a1b9b6aSSam Leffler 		printf("%s: no memory for AID bitmap!\n", __func__);
1068a1b9b6aSSam Leffler 		ic->ic_max_aid = 0;
1078a1b9b6aSSam Leffler 	}
1088a1b9b6aSSam Leffler 
1098a1b9b6aSSam Leffler 	/* XXX defer until using hostap/ibss mode */
1108a1b9b6aSSam Leffler 	ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
1118a1b9b6aSSam Leffler 	MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
1128a1b9b6aSSam Leffler 		M_DEVBUF, M_NOWAIT | M_ZERO);
1138a1b9b6aSSam Leffler 	if (ic->ic_tim_bitmap == NULL) {
1148a1b9b6aSSam Leffler 		/* XXX no way to recover */
1158a1b9b6aSSam Leffler 		printf("%s: no memory for TIM bitmap!\n", __func__);
1168a1b9b6aSSam Leffler 	}
1178a1b9b6aSSam Leffler 	ic->ic_set_tim = ieee80211_set_tim;	/* NB: driver should override */
1182692bb26SSam Leffler }
1192692bb26SSam Leffler 
1202692bb26SSam Leffler void
1218a1b9b6aSSam Leffler ieee80211_node_lateattach(struct ieee80211com *ic)
1222692bb26SSam Leffler {
123849b8980SSam Leffler 	struct ieee80211_node *ni;
1248a1b9b6aSSam Leffler 	struct ieee80211_rsnparms *rsn;
1252692bb26SSam Leffler 
1268a1b9b6aSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
127849b8980SSam Leffler 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
1288a1b9b6aSSam Leffler 	/*
1298a1b9b6aSSam Leffler 	 * Setup "global settings" in the bss node so that
1308a1b9b6aSSam Leffler 	 * each new station automatically inherits them.
1318a1b9b6aSSam Leffler 	 */
1328a1b9b6aSSam Leffler 	rsn = &ni->ni_rsn;
1338a1b9b6aSSam Leffler 	/* WEP, TKIP, and AES-CCM are always supported */
1348a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
1358a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
1368a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
1378a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_AES)
1388a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
1398a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_CKIP)
1408a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
1418a1b9b6aSSam Leffler 	/*
1428a1b9b6aSSam Leffler 	 * Default unicast cipher to WEP for 802.1x use.  If
1438a1b9b6aSSam Leffler 	 * WPA is enabled the management code will set these
1448a1b9b6aSSam Leffler 	 * values to reflect.
1458a1b9b6aSSam Leffler 	 */
1468a1b9b6aSSam Leffler 	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
1478a1b9b6aSSam Leffler 	rsn->rsn_ucastkeylen = 104 / NBBY;
1488a1b9b6aSSam Leffler 	/*
1498a1b9b6aSSam Leffler 	 * WPA says the multicast cipher is the lowest unicast
1508a1b9b6aSSam Leffler 	 * cipher supported.  But we skip WEP which would
1518a1b9b6aSSam Leffler 	 * otherwise be used based on this criteria.
1528a1b9b6aSSam Leffler 	 */
1538a1b9b6aSSam Leffler 	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
1548a1b9b6aSSam Leffler 	rsn->rsn_mcastkeylen = 128 / NBBY;
1558a1b9b6aSSam Leffler 
1568a1b9b6aSSam Leffler 	/*
1578a1b9b6aSSam Leffler 	 * We support both WPA-PSK and 802.1x; the one used
1588a1b9b6aSSam Leffler 	 * is determined by the authentication mode and the
1598a1b9b6aSSam Leffler 	 * setting of the PSK state.
1608a1b9b6aSSam Leffler 	 */
1618a1b9b6aSSam Leffler 	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
1628a1b9b6aSSam Leffler 	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1638a1b9b6aSSam Leffler 
1648a1b9b6aSSam Leffler 	ic->ic_bss = ieee80211_ref_node(ni);		/* hold reference */
1658a1b9b6aSSam Leffler 	ic->ic_auth = ieee80211_authenticator_get(ni->ni_authmode);
1661a1e1d21SSam Leffler }
1671a1e1d21SSam Leffler 
1681a1e1d21SSam Leffler void
1698a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic)
1701a1e1d21SSam Leffler {
1711a1e1d21SSam Leffler 
1728a1b9b6aSSam Leffler 	if (ic->ic_bss != NULL) {
1738a1b9b6aSSam Leffler 		ieee80211_free_node(ic->ic_bss);
1748a1b9b6aSSam Leffler 		ic->ic_bss = NULL;
1758a1b9b6aSSam Leffler 	}
1768a1b9b6aSSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_scan);
177acc4f7f5SSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_sta);
1788a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap != NULL) {
1798a1b9b6aSSam Leffler 		FREE(ic->ic_aid_bitmap, M_DEVBUF);
1808a1b9b6aSSam Leffler 		ic->ic_aid_bitmap = NULL;
1818a1b9b6aSSam Leffler 	}
1828a1b9b6aSSam Leffler 	if (ic->ic_tim_bitmap != NULL) {
1838a1b9b6aSSam Leffler 		FREE(ic->ic_tim_bitmap, M_DEVBUF);
1848a1b9b6aSSam Leffler 		ic->ic_tim_bitmap = NULL;
1858a1b9b6aSSam Leffler 	}
1868a1b9b6aSSam Leffler }
1878a1b9b6aSSam Leffler 
1888a1b9b6aSSam Leffler /*
1898a1b9b6aSSam Leffler  * Port authorize/unauthorize interfaces for use by an authenticator.
1908a1b9b6aSSam Leffler  */
1918a1b9b6aSSam Leffler 
1928a1b9b6aSSam Leffler void
1938a1b9b6aSSam Leffler ieee80211_node_authorize(struct ieee80211com *ic, struct ieee80211_node *ni)
1948a1b9b6aSSam Leffler {
1958a1b9b6aSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AUTH;
1962045f699SSam Leffler 	ni->ni_inact_reload = ic->ic_inact_run;
1978a1b9b6aSSam Leffler }
1988a1b9b6aSSam Leffler 
1998a1b9b6aSSam Leffler void
2008a1b9b6aSSam Leffler ieee80211_node_unauthorize(struct ieee80211com *ic, struct ieee80211_node *ni)
2018a1b9b6aSSam Leffler {
2028a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
2038a1b9b6aSSam Leffler }
2048a1b9b6aSSam Leffler 
2058a1b9b6aSSam Leffler /*
2068a1b9b6aSSam Leffler  * Set/change the channel.  The rate set is also updated as
2078a1b9b6aSSam Leffler  * to insure a consistent view by drivers.
2088a1b9b6aSSam Leffler  */
2098a1b9b6aSSam Leffler static __inline void
2108a1b9b6aSSam Leffler ieee80211_set_chan(struct ieee80211com *ic,
2118a1b9b6aSSam Leffler 	struct ieee80211_node *ni, struct ieee80211_channel *chan)
2128a1b9b6aSSam Leffler {
2138a1b9b6aSSam Leffler 	ni->ni_chan = chan;
2148a1b9b6aSSam Leffler 	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
2151a1e1d21SSam Leffler }
2161a1e1d21SSam Leffler 
2171a1e1d21SSam Leffler /*
2181a1e1d21SSam Leffler  * AP scanning support.
2191a1e1d21SSam Leffler  */
2201a1e1d21SSam Leffler 
2218a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
2228a1b9b6aSSam Leffler static void
2238a1b9b6aSSam Leffler dump_chanlist(const u_char chans[])
2248a1b9b6aSSam Leffler {
2258a1b9b6aSSam Leffler 	const char *sep;
2268a1b9b6aSSam Leffler 	int i;
2278a1b9b6aSSam Leffler 
2288a1b9b6aSSam Leffler 	sep = " ";
2298a1b9b6aSSam Leffler 	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
2308a1b9b6aSSam Leffler 		if (isset(chans, i)) {
2318a1b9b6aSSam Leffler 			printf("%s%u", sep, i);
2328a1b9b6aSSam Leffler 			sep = ", ";
2338a1b9b6aSSam Leffler 		}
2348a1b9b6aSSam Leffler }
2358a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2368a1b9b6aSSam Leffler 
2371a1e1d21SSam Leffler /*
2388a1b9b6aSSam Leffler  * Initialize the channel set to scan based on the
2391a1e1d21SSam Leffler  * of available channels and the current PHY mode.
2401a1e1d21SSam Leffler  */
241a11c9a5cSSam Leffler static void
2428a1b9b6aSSam Leffler ieee80211_reset_scan(struct ieee80211com *ic)
2431a1e1d21SSam Leffler {
2441a1e1d21SSam Leffler 
2458a1b9b6aSSam Leffler 	/* XXX ic_des_chan should be handled with ic_chan_active */
2468a1b9b6aSSam Leffler 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
2478a1b9b6aSSam Leffler 		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
2488a1b9b6aSSam Leffler 		setbit(ic->ic_chan_scan,
2498a1b9b6aSSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_des_chan));
2508a1b9b6aSSam Leffler 	} else
2511a1e1d21SSam Leffler 		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
2521a1e1d21SSam Leffler 			sizeof(ic->ic_chan_active));
253a11c9a5cSSam Leffler 	/* NB: hack, setup so next_scan starts with the first channel */
254a11c9a5cSSam Leffler 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
2558a1b9b6aSSam Leffler 		ieee80211_set_chan(ic, ic->ic_bss,
2568a1b9b6aSSam Leffler 			&ic->ic_channels[IEEE80211_CHAN_MAX]);
2578a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
2588a1b9b6aSSam Leffler 	if (ieee80211_msg_scan(ic)) {
2598a1b9b6aSSam Leffler 		printf("%s: scan set:", __func__);
2608a1b9b6aSSam Leffler 		dump_chanlist(ic->ic_chan_scan);
2618a1b9b6aSSam Leffler 		printf(" start chan %u\n",
2628a1b9b6aSSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
2638a1b9b6aSSam Leffler 	}
2648a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2651a1e1d21SSam Leffler }
2661a1e1d21SSam Leffler 
2671a1e1d21SSam Leffler /*
2681a1e1d21SSam Leffler  * Begin an active scan.
2691a1e1d21SSam Leffler  */
2701a1e1d21SSam Leffler void
2718a1b9b6aSSam Leffler ieee80211_begin_scan(struct ieee80211com *ic, int reset)
2721a1e1d21SSam Leffler {
2731a1e1d21SSam Leffler 
2748a1b9b6aSSam Leffler 	ic->ic_scan.nt_scangen++;
275a11c9a5cSSam Leffler 	/*
276a11c9a5cSSam Leffler 	 * In all but hostap mode scanning starts off in
277a11c9a5cSSam Leffler 	 * an active mode before switching to passive.
278a11c9a5cSSam Leffler 	 */
2791be50176SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
280a11c9a5cSSam Leffler 		ic->ic_flags |= IEEE80211_F_ASCAN;
2811be50176SSam Leffler 		ic->ic_stats.is_scan_active++;
2821be50176SSam Leffler 	} else
2831be50176SSam Leffler 		ic->ic_stats.is_scan_passive++;
2843ea67c54SSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
2853ea67c54SSam Leffler 		"begin %s scan in %s mode, scangen %u\n",
2868a1b9b6aSSam Leffler 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
2873ea67c54SSam Leffler 		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
2881a1e1d21SSam Leffler 	/*
2898a1b9b6aSSam Leffler 	 * Clear scan state and flush any previously seen AP's.
2901a1e1d21SSam Leffler 	 */
2918a1b9b6aSSam Leffler 	ieee80211_reset_scan(ic);
2928a1b9b6aSSam Leffler 	if (reset)
2938a1b9b6aSSam Leffler 		ieee80211_free_allnodes(&ic->ic_scan);
2948a1b9b6aSSam Leffler 
2958a1b9b6aSSam Leffler 	ic->ic_flags |= IEEE80211_F_SCAN;
2961a1e1d21SSam Leffler 
297a11c9a5cSSam Leffler 	/* Scan the next channel. */
2988a1b9b6aSSam Leffler 	ieee80211_next_scan(ic);
2991a1e1d21SSam Leffler }
3001a1e1d21SSam Leffler 
3011a1e1d21SSam Leffler /*
3021a1e1d21SSam Leffler  * Switch to the next channel marked for scanning.
3031a1e1d21SSam Leffler  */
3048a1b9b6aSSam Leffler int
3058a1b9b6aSSam Leffler ieee80211_next_scan(struct ieee80211com *ic)
3061a1e1d21SSam Leffler {
3071a1e1d21SSam Leffler 	struct ieee80211_channel *chan;
3081a1e1d21SSam Leffler 
3098a1b9b6aSSam Leffler 	/*
3108a1b9b6aSSam Leffler 	 * Insure any previous mgt frame timeouts don't fire.
3118a1b9b6aSSam Leffler 	 * This assumes the driver does the right thing in
3128a1b9b6aSSam Leffler 	 * flushing anything queued in the driver and below.
3138a1b9b6aSSam Leffler 	 */
3148a1b9b6aSSam Leffler 	ic->ic_mgt_timer = 0;
3158a1b9b6aSSam Leffler 
3161a1e1d21SSam Leffler 	chan = ic->ic_bss->ni_chan;
3178a1b9b6aSSam Leffler 	do {
3181a1e1d21SSam Leffler 		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
3191a1e1d21SSam Leffler 			chan = &ic->ic_channels[0];
3201a1e1d21SSam Leffler 		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
3211a1e1d21SSam Leffler 			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
3228a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
3238a1b9b6aSSam Leffler 			    "%s: chan %d->%d\n", __func__,
3241a1e1d21SSam Leffler 			    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
3258a1b9b6aSSam Leffler 			    ieee80211_chan2ieee(ic, chan));
3268a1b9b6aSSam Leffler 			ieee80211_set_chan(ic, ic->ic_bss, chan);
3278a1b9b6aSSam Leffler #ifdef notyet
3288a1b9b6aSSam Leffler 			/* XXX driver state change */
3298a1b9b6aSSam Leffler 			/*
3308a1b9b6aSSam Leffler 			 * Scan next channel. If doing an active scan
3318a1b9b6aSSam Leffler 			 * and the channel is not marked passive-only
3328a1b9b6aSSam Leffler 			 * then send a probe request.  Otherwise just
3338a1b9b6aSSam Leffler 			 * listen for beacons on the channel.
3348a1b9b6aSSam Leffler 			 */
3358a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_ASCAN) &&
3368a1b9b6aSSam Leffler 			    (ni->ni_chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) {
3378a1b9b6aSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
3388a1b9b6aSSam Leffler 				    IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
3398a1b9b6aSSam Leffler 			}
3408a1b9b6aSSam Leffler #else
341a11c9a5cSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
3428a1b9b6aSSam Leffler #endif
3438a1b9b6aSSam Leffler 			return 1;
3448a1b9b6aSSam Leffler 		}
3458a1b9b6aSSam Leffler 	} while (chan != ic->ic_bss->ni_chan);
3468a1b9b6aSSam Leffler 	ieee80211_end_scan(ic);
3478a1b9b6aSSam Leffler 	return 0;
3481a1e1d21SSam Leffler }
3491a1e1d21SSam Leffler 
3501a1e1d21SSam Leffler void
3511a1e1d21SSam Leffler ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
3521a1e1d21SSam Leffler {
353acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt;
3541a1e1d21SSam Leffler 	struct ieee80211_node *ni;
3558a1b9b6aSSam Leffler 
3568a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
3578a1b9b6aSSam Leffler 		"%s: creating ibss\n", __func__);
3588a1b9b6aSSam Leffler 
3598a1b9b6aSSam Leffler 	/*
3608a1b9b6aSSam Leffler 	 * Create the station/neighbor table.  Note that for adhoc
3618a1b9b6aSSam Leffler 	 * mode we make the initial inactivity timer longer since
3628a1b9b6aSSam Leffler 	 * we create nodes only through discovery and they typically
3638a1b9b6aSSam Leffler 	 * are long-lived associations.
3648a1b9b6aSSam Leffler 	 */
365acc4f7f5SSam Leffler 	nt = &ic->ic_sta;
366acc4f7f5SSam Leffler 	IEEE80211_NODE_LOCK(nt);
367acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
368acc4f7f5SSam Leffler 		nt->nt_name = "station";
369acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_init;
370acc4f7f5SSam Leffler 	} else {
371acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
372acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
373acc4f7f5SSam Leffler 	}
374acc4f7f5SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
375acc4f7f5SSam Leffler 
376acc4f7f5SSam Leffler 	ni = ieee80211_alloc_node(nt, ic->ic_myaddr);
377acc4f7f5SSam Leffler 	if (ni == NULL) {
378acc4f7f5SSam Leffler 		/* XXX recovery? */
3798a1b9b6aSSam Leffler 		return;
3808a1b9b6aSSam Leffler 	}
3811a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
3821a1e1d21SSam Leffler 	ni->ni_esslen = ic->ic_des_esslen;
3831a1e1d21SSam Leffler 	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
3841a1e1d21SSam Leffler 	ni->ni_intval = ic->ic_lintval;
3858a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
3861a1e1d21SSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
3871a1e1d21SSam Leffler 	if (ic->ic_phytype == IEEE80211_T_FH) {
3881a1e1d21SSam Leffler 		ni->ni_fhdwell = 200;	/* XXX */
3891a1e1d21SSam Leffler 		ni->ni_fhindex = 1;
3901a1e1d21SSam Leffler 	}
3918a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
3928a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SIBSS;
3938a1b9b6aSSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
3948a1b9b6aSSam Leffler 		ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
3958a1b9b6aSSam Leffler 	}
3968a1b9b6aSSam Leffler 	/*
3978a1b9b6aSSam Leffler 	 * Fix the channel and related attributes.
3988a1b9b6aSSam Leffler 	 */
3998a1b9b6aSSam Leffler 	ieee80211_set_chan(ic, ni, chan);
4008a1b9b6aSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
4018a1b9b6aSSam Leffler 	/*
4028a1b9b6aSSam Leffler 	 * Do mode-specific rate setup.
4038a1b9b6aSSam Leffler 	 */
4048a1b9b6aSSam Leffler 	if (ic->ic_curmode == IEEE80211_MODE_11G) {
4058a1b9b6aSSam Leffler 		/*
4068a1b9b6aSSam Leffler 		 * Use a mixed 11b/11g rate set.
4078a1b9b6aSSam Leffler 		 */
4088a1b9b6aSSam Leffler 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
4098a1b9b6aSSam Leffler 	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
4108a1b9b6aSSam Leffler 		/*
4118a1b9b6aSSam Leffler 		 * Force pure 11b rate set.
4128a1b9b6aSSam Leffler 		 */
4138a1b9b6aSSam Leffler 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
4148a1b9b6aSSam Leffler 	}
4158a1b9b6aSSam Leffler 
416acc4f7f5SSam Leffler 	(void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
4171a1e1d21SSam Leffler }
4181a1e1d21SSam Leffler 
4198a1b9b6aSSam Leffler void
4208a1b9b6aSSam Leffler ieee80211_reset_bss(struct ieee80211com *ic)
421b4c5a90fSSam Leffler {
4228a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *obss;
4238a1b9b6aSSam Leffler 
4248a1b9b6aSSam Leffler 	ieee80211_node_table_reset(&ic->ic_scan);
425acc4f7f5SSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta);
426acc4f7f5SSam Leffler 
4278a1b9b6aSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
4288a1b9b6aSSam Leffler 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
4298a1b9b6aSSam Leffler 	obss = ic->ic_bss;
4308a1b9b6aSSam Leffler 	ic->ic_bss = ieee80211_ref_node(ni);
4318a1b9b6aSSam Leffler 	if (obss != NULL)
4328a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
4338a1b9b6aSSam Leffler }
4348a1b9b6aSSam Leffler 
4358a1b9b6aSSam Leffler static int
4368a1b9b6aSSam Leffler ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
4378a1b9b6aSSam Leffler {
438b4c5a90fSSam Leffler         u_int8_t rate;
439b4c5a90fSSam Leffler         int fail;
440b4c5a90fSSam Leffler 
441b4c5a90fSSam Leffler 	fail = 0;
442b4c5a90fSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
443b4c5a90fSSam Leffler 		fail |= 0x01;
444b4c5a90fSSam Leffler 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
445b4c5a90fSSam Leffler 	    ni->ni_chan != ic->ic_des_chan)
446b4c5a90fSSam Leffler 		fail |= 0x01;
447b4c5a90fSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
448b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
449b4c5a90fSSam Leffler 			fail |= 0x02;
450b4c5a90fSSam Leffler 	} else {
451b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
452b4c5a90fSSam Leffler 			fail |= 0x02;
453b4c5a90fSSam Leffler 	}
4548a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
455b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
456b4c5a90fSSam Leffler 			fail |= 0x04;
457b4c5a90fSSam Leffler 	} else {
458b4c5a90fSSam Leffler 		/* XXX does this mean privacy is supported or required? */
459b4c5a90fSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
460b4c5a90fSSam Leffler 			fail |= 0x04;
461b4c5a90fSSam Leffler 	}
46298ff6263SSam Leffler 	rate = ieee80211_fix_rate(ic, ni,
46398ff6263SSam Leffler 			IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
464b4c5a90fSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
465b4c5a90fSSam Leffler 		fail |= 0x08;
466b4c5a90fSSam Leffler 	if (ic->ic_des_esslen != 0 &&
467b4c5a90fSSam Leffler 	    (ni->ni_esslen != ic->ic_des_esslen ||
468b4c5a90fSSam Leffler 	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
469b4c5a90fSSam Leffler 		fail |= 0x10;
470b4c5a90fSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
471b4c5a90fSSam Leffler 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
472b4c5a90fSSam Leffler 		fail |= 0x20;
473b4c5a90fSSam Leffler #ifdef IEEE80211_DEBUG
4748a1b9b6aSSam Leffler 	if (ieee80211_msg_scan(ic)) {
475b4c5a90fSSam Leffler 		printf(" %c %s", fail ? '-' : '+',
476b4c5a90fSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
477b4c5a90fSSam Leffler 		printf(" %s%c", ether_sprintf(ni->ni_bssid),
478b4c5a90fSSam Leffler 		    fail & 0x20 ? '!' : ' ');
479b4c5a90fSSam Leffler 		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
480b4c5a90fSSam Leffler 			fail & 0x01 ? '!' : ' ');
481b4c5a90fSSam Leffler 		printf(" %+4d", ni->ni_rssi);
482b4c5a90fSSam Leffler 		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
483b4c5a90fSSam Leffler 		    fail & 0x08 ? '!' : ' ');
484b4c5a90fSSam Leffler 		printf(" %4s%c",
485b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
486b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
487b4c5a90fSSam Leffler 		    "????",
488b4c5a90fSSam Leffler 		    fail & 0x02 ? '!' : ' ');
489b4c5a90fSSam Leffler 		printf(" %3s%c ",
490b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
491b4c5a90fSSam Leffler 		    "wep" : "no",
492b4c5a90fSSam Leffler 		    fail & 0x04 ? '!' : ' ');
493b4c5a90fSSam Leffler 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
494b4c5a90fSSam Leffler 		printf("%s\n", fail & 0x10 ? "!" : "");
495b4c5a90fSSam Leffler 	}
496b4c5a90fSSam Leffler #endif
497b4c5a90fSSam Leffler 	return fail;
498b4c5a90fSSam Leffler }
499b4c5a90fSSam Leffler 
5008a1b9b6aSSam Leffler static __inline u_int8_t
5018a1b9b6aSSam Leffler maxrate(const struct ieee80211_node *ni)
5028a1b9b6aSSam Leffler {
5038a1b9b6aSSam Leffler 	const struct ieee80211_rateset *rs = &ni->ni_rates;
5048a1b9b6aSSam Leffler 	/* NB: assumes rate set is sorted (happens on frame receive) */
5058a1b9b6aSSam Leffler 	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
5068a1b9b6aSSam Leffler }
5078a1b9b6aSSam Leffler 
5088a1b9b6aSSam Leffler /*
5098a1b9b6aSSam Leffler  * Compare the capabilities of two nodes and decide which is
5108a1b9b6aSSam Leffler  * more desirable (return >0 if a is considered better).  Note
5118a1b9b6aSSam Leffler  * that we assume compatibility/usability has already been checked
5128a1b9b6aSSam Leffler  * so we don't need to (e.g. validate whether privacy is supported).
5138a1b9b6aSSam Leffler  * Used to select the best scan candidate for association in a BSS.
5148a1b9b6aSSam Leffler  */
5158a1b9b6aSSam Leffler static int
5168a1b9b6aSSam Leffler ieee80211_node_compare(struct ieee80211com *ic,
5178a1b9b6aSSam Leffler 		       const struct ieee80211_node *a,
5188a1b9b6aSSam Leffler 		       const struct ieee80211_node *b)
5198a1b9b6aSSam Leffler {
5208a1b9b6aSSam Leffler 	u_int8_t maxa, maxb;
5218a1b9b6aSSam Leffler 	u_int8_t rssia, rssib;
5228a1b9b6aSSam Leffler 
5238a1b9b6aSSam Leffler 	/* privacy support preferred */
5248a1b9b6aSSam Leffler 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
5258a1b9b6aSSam Leffler 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
5268a1b9b6aSSam Leffler 		return 1;
5278a1b9b6aSSam Leffler 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
5288a1b9b6aSSam Leffler 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
5298a1b9b6aSSam Leffler 		return -1;
5308a1b9b6aSSam Leffler 
531ef92bcdcSSam Leffler 	rssia = ic->ic_node_getrssi(a);
532ef92bcdcSSam Leffler 	rssib = ic->ic_node_getrssi(b);
533ef92bcdcSSam Leffler 	if (abs(rssib - rssia) < 5) {
5348a1b9b6aSSam Leffler 		/* best/max rate preferred if signal level close enough XXX */
5358a1b9b6aSSam Leffler 		maxa = maxrate(a);
5368a1b9b6aSSam Leffler 		maxb = maxrate(b);
537ef92bcdcSSam Leffler 		if (maxa != maxb)
5388a1b9b6aSSam Leffler 			return maxa - maxb;
5398a1b9b6aSSam Leffler 		/* XXX use freq for channel preference */
5408a1b9b6aSSam Leffler 		/* for now just prefer 5Ghz band to all other bands */
5418a1b9b6aSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
5428a1b9b6aSSam Leffler 		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
5438a1b9b6aSSam Leffler 			return 1;
5448a1b9b6aSSam Leffler 		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
5458a1b9b6aSSam Leffler 		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
5468a1b9b6aSSam Leffler 			return -1;
547ef92bcdcSSam Leffler 	}
5488a1b9b6aSSam Leffler 	/* all things being equal, use signal level */
5498a1b9b6aSSam Leffler 	return rssia - rssib;
5508a1b9b6aSSam Leffler }
5518a1b9b6aSSam Leffler 
5521a1e1d21SSam Leffler /*
553c75ac469SSam Leffler  * Mark an ongoing scan stopped.
554c75ac469SSam Leffler  */
555c75ac469SSam Leffler void
556c75ac469SSam Leffler ieee80211_cancel_scan(struct ieee80211com *ic)
557c75ac469SSam Leffler {
558c75ac469SSam Leffler 
559c75ac469SSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
560c75ac469SSam Leffler 		__func__,
561c75ac469SSam Leffler 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
562c75ac469SSam Leffler 
563c75ac469SSam Leffler 	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
564c75ac469SSam Leffler }
565c75ac469SSam Leffler 
566c75ac469SSam Leffler /*
5671a1e1d21SSam Leffler  * Complete a scan of potential channels.
5681a1e1d21SSam Leffler  */
5691a1e1d21SSam Leffler void
5708a1b9b6aSSam Leffler ieee80211_end_scan(struct ieee80211com *ic)
5711a1e1d21SSam Leffler {
5723fcfbbfaSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_scan;
5733fcfbbfaSSam Leffler 	struct ieee80211_node *ni, *selbs;
5741a1e1d21SSam Leffler 
575c75ac469SSam Leffler 	ieee80211_cancel_scan(ic);
576c75ac469SSam Leffler 	ieee80211_notify_scan_done(ic);
5778a1b9b6aSSam Leffler 
5781a1e1d21SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
5798a1b9b6aSSam Leffler 		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
5808a1b9b6aSSam Leffler 		int i, bestchan;
5818a1b9b6aSSam Leffler 		u_int8_t rssi;
5828a1b9b6aSSam Leffler 
5831a1e1d21SSam Leffler 		/*
5841a1e1d21SSam Leffler 		 * The passive scan to look for existing AP's completed,
5851a1e1d21SSam Leffler 		 * select a channel to camp on.  Identify the channels
5861a1e1d21SSam Leffler 		 * that already have one or more AP's and try to locate
587acc4f7f5SSam Leffler 		 * an unoccupied one.  If that fails, pick a channel that
5888a1b9b6aSSam Leffler 		 * looks to be quietest.
5891a1e1d21SSam Leffler 		 */
5908a1b9b6aSSam Leffler 		memset(maxrssi, 0, sizeof(maxrssi));
5913fcfbbfaSSam Leffler 		IEEE80211_NODE_LOCK(nt);
5923fcfbbfaSSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
5938a1b9b6aSSam Leffler 			rssi = ic->ic_node_getrssi(ni);
5948a1b9b6aSSam Leffler 			i = ieee80211_chan2ieee(ic, ni->ni_chan);
5958a1b9b6aSSam Leffler 			if (rssi > maxrssi[i])
5968a1b9b6aSSam Leffler 				maxrssi[i] = rssi;
5971a1e1d21SSam Leffler 		}
5983fcfbbfaSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
5998a1b9b6aSSam Leffler 		/* XXX select channel more intelligently */
6008a1b9b6aSSam Leffler 		bestchan = -1;
6011a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
6028a1b9b6aSSam Leffler 			if (isset(ic->ic_chan_active, i)) {
6038a1b9b6aSSam Leffler 				/*
6048a1b9b6aSSam Leffler 				 * If the channel is unoccupied the max rssi
6058a1b9b6aSSam Leffler 				 * should be zero; just take it.  Otherwise
6068a1b9b6aSSam Leffler 				 * track the channel with the lowest rssi and
6078a1b9b6aSSam Leffler 				 * use that when all channels appear occupied.
6088a1b9b6aSSam Leffler 				 */
6098a1b9b6aSSam Leffler 				if (maxrssi[i] == 0) {
6108a1b9b6aSSam Leffler 					bestchan = i;
6111a1e1d21SSam Leffler 					break;
6121a1e1d21SSam Leffler 				}
6138a1b9b6aSSam Leffler 				if (maxrssi[i] < maxrssi[bestchan])
6148a1b9b6aSSam Leffler 					bestchan = i;
6158a1b9b6aSSam Leffler 			}
6168a1b9b6aSSam Leffler 		if (bestchan != -1) {
6178a1b9b6aSSam Leffler 			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
6181a1e1d21SSam Leffler 			return;
6191a1e1d21SSam Leffler 		}
6208a1b9b6aSSam Leffler 		/* no suitable channel, should not happen */
6218a1b9b6aSSam Leffler 	}
6228a1b9b6aSSam Leffler 
6238a1b9b6aSSam Leffler 	/*
6248a1b9b6aSSam Leffler 	 * When manually sequencing the state machine; scan just once
6258a1b9b6aSSam Leffler 	 * regardless of whether we have a candidate or not.  The
6268a1b9b6aSSam Leffler 	 * controlling application is expected to setup state and
6278a1b9b6aSSam Leffler 	 * initiate an association.
6288a1b9b6aSSam Leffler 	 */
6298a1b9b6aSSam Leffler 	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
6308a1b9b6aSSam Leffler 		return;
6318a1b9b6aSSam Leffler 	/*
6328a1b9b6aSSam Leffler 	 * Automatic sequencing; look for a candidate and
6338a1b9b6aSSam Leffler 	 * if found join the network.
6348a1b9b6aSSam Leffler 	 */
6353fcfbbfaSSam Leffler 	/* NB: unlocked read should be ok */
6363fcfbbfaSSam Leffler 	if (TAILQ_FIRST(&nt->nt_node) == NULL) {
6378a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
6388a1b9b6aSSam Leffler 			"%s: no scan candidate\n", __func__);
6391a1e1d21SSam Leffler   notfound:
6401a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
6411a1e1d21SSam Leffler 		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
6421a1e1d21SSam Leffler 		    ic->ic_des_esslen != 0) {
6431a1e1d21SSam Leffler 			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
6441a1e1d21SSam Leffler 			return;
6451a1e1d21SSam Leffler 		}
6461a1e1d21SSam Leffler 		/*
6471a1e1d21SSam Leffler 		 * Reset the list of channels to scan and start again.
6481a1e1d21SSam Leffler 		 */
6498a1b9b6aSSam Leffler 		ieee80211_reset_scan(ic);
6508a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SCAN;
6518a1b9b6aSSam Leffler 		ieee80211_next_scan(ic);
6521a1e1d21SSam Leffler 		return;
6531a1e1d21SSam Leffler 	}
6541a1e1d21SSam Leffler 	selbs = NULL;
6558a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
6568a1b9b6aSSam Leffler 	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
6573fcfbbfaSSam Leffler 	IEEE80211_NODE_LOCK(nt);
6583fcfbbfaSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
6591a1e1d21SSam Leffler 		if (ni->ni_fails) {
6601a1e1d21SSam Leffler 			/*
6611a1e1d21SSam Leffler 			 * The configuration of the access points may change
6621a1e1d21SSam Leffler 			 * during my scan.  So delete the entry for the AP
6631a1e1d21SSam Leffler 			 * and retry to associate if there is another beacon.
6641a1e1d21SSam Leffler 			 */
6658a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
6668a1b9b6aSSam Leffler 				"%s: skip scan candidate %s, fails %u\n",
6678a1b9b6aSSam Leffler 				__func__, ether_sprintf(ni->ni_macaddr),
6688a1b9b6aSSam Leffler 				ni->ni_fails);
6693fcfbbfaSSam Leffler 			ni->ni_fails++;
6703fcfbbfaSSam Leffler #if 0
6711a1e1d21SSam Leffler 			if (ni->ni_fails++ > 2)
6728a1b9b6aSSam Leffler 				ieee80211_free_node(ni);
6733fcfbbfaSSam Leffler #endif
6741a1e1d21SSam Leffler 			continue;
6751a1e1d21SSam Leffler 		}
6768a1b9b6aSSam Leffler 		if (ieee80211_match_bss(ic, ni) == 0) {
6771a1e1d21SSam Leffler 			if (selbs == NULL)
6781a1e1d21SSam Leffler 				selbs = ni;
6793fcfbbfaSSam Leffler 			else if (ieee80211_node_compare(ic, ni, selbs) > 0)
6801a1e1d21SSam Leffler 				selbs = ni;
6811a1e1d21SSam Leffler 		}
6821a1e1d21SSam Leffler 	}
6833fcfbbfaSSam Leffler 	if (selbs != NULL)		/* NB: grab ref while dropping lock */
6843fcfbbfaSSam Leffler 		(void) ieee80211_ref_node(selbs);
6853fcfbbfaSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
6861a1e1d21SSam Leffler 	if (selbs == NULL)
6871a1e1d21SSam Leffler 		goto notfound;
6888a1b9b6aSSam Leffler 	if (!ieee80211_sta_join(ic, selbs)) {
6893fcfbbfaSSam Leffler 		ieee80211_free_node(selbs);
6901a1e1d21SSam Leffler 		goto notfound;
6911a1e1d21SSam Leffler 	}
6928a1b9b6aSSam Leffler }
6938a1b9b6aSSam Leffler 
694750d6d0cSSam Leffler /*
6958a1b9b6aSSam Leffler  * Handle 802.11 ad hoc network merge.  The
6968a1b9b6aSSam Leffler  * convention, set by the Wireless Ethernet Compatibility Alliance
6978a1b9b6aSSam Leffler  * (WECA), is that an 802.11 station will change its BSSID to match
6988a1b9b6aSSam Leffler  * the "oldest" 802.11 ad hoc network, on the same channel, that
6998a1b9b6aSSam Leffler  * has the station's desired SSID.  The "oldest" 802.11 network
7008a1b9b6aSSam Leffler  * sends beacons with the greatest TSF timestamp.
7018a1b9b6aSSam Leffler  *
7028a1b9b6aSSam Leffler  * The caller is assumed to validate TSF's before attempting a merge.
7038a1b9b6aSSam Leffler  *
7048a1b9b6aSSam Leffler  * Return !0 if the BSSID changed, 0 otherwise.
705750d6d0cSSam Leffler  */
7068a1b9b6aSSam Leffler int
7078a1b9b6aSSam Leffler ieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni)
7088a1b9b6aSSam Leffler {
7098a1b9b6aSSam Leffler 
71096acc1b6SSam Leffler 	if (ni == ic->ic_bss ||
71196acc1b6SSam Leffler 	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
7128a1b9b6aSSam Leffler 		/* unchanged, nothing to do */
7138a1b9b6aSSam Leffler 		return 0;
7148a1b9b6aSSam Leffler 	}
7158a1b9b6aSSam Leffler 	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
7168a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
7178a1b9b6aSSam Leffler 		    "%s: merge failed, capabilities mismatch\n", __func__);
7188a1b9b6aSSam Leffler 		ic->ic_stats.is_ibss_capmismatch++;
7198a1b9b6aSSam Leffler 		return 0;
7208a1b9b6aSSam Leffler 	}
7218a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
7228a1b9b6aSSam Leffler 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
7238a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
7248a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
7258a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
7268a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
7278a1b9b6aSSam Leffler 	);
728acc4f7f5SSam Leffler 	return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
7298a1b9b6aSSam Leffler }
7308a1b9b6aSSam Leffler 
7318a1b9b6aSSam Leffler /*
7328a1b9b6aSSam Leffler  * Join the specified IBSS/BSS network.  The node is assumed to
7338a1b9b6aSSam Leffler  * be passed in with a held reference.
7348a1b9b6aSSam Leffler  */
7358a1b9b6aSSam Leffler int
7368a1b9b6aSSam Leffler ieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
7378a1b9b6aSSam Leffler {
7388a1b9b6aSSam Leffler 	struct ieee80211_node *obss;
7398a1b9b6aSSam Leffler 
7408a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
741acc4f7f5SSam Leffler 		struct ieee80211_node_table *nt;
7428a1b9b6aSSam Leffler 		/*
74398ff6263SSam Leffler 		 * Delete unusable rates; we've already checked
74498ff6263SSam Leffler 		 * that the negotiated rate set is acceptable.
7458a1b9b6aSSam Leffler 		 */
74698ff6263SSam Leffler 		ieee80211_fix_rate(ic, selbs, IEEE80211_F_DODEL);
7478a1b9b6aSSam Leffler 		/*
748acc4f7f5SSam Leffler 		 * Fillin the neighbor table; it will already
7493d073929SSam Leffler 		 * exist if we are simply switching mastership.
750acc4f7f5SSam Leffler 		 * XXX ic_sta always setup so this is unnecessary?
7518a1b9b6aSSam Leffler 		 */
752acc4f7f5SSam Leffler 		nt = &ic->ic_sta;
753acc4f7f5SSam Leffler 		IEEE80211_NODE_LOCK(nt);
754acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
755acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
756acc4f7f5SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
7573d073929SSam Leffler 	}
7581a1e1d21SSam Leffler 
7598a1b9b6aSSam Leffler 	/*
7608a1b9b6aSSam Leffler 	 * Committed to selbs, setup state.
7618a1b9b6aSSam Leffler 	 */
7628a1b9b6aSSam Leffler 	obss = ic->ic_bss;
763acc4f7f5SSam Leffler 	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
7648a1b9b6aSSam Leffler 	if (obss != NULL)
7658a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
7668a1b9b6aSSam Leffler 	/*
7678a1b9b6aSSam Leffler 	 * Set the erp state (mostly the slot time) to deal with
7688a1b9b6aSSam Leffler 	 * the auto-select case; this should be redundant if the
7698a1b9b6aSSam Leffler 	 * mode is locked.
7708a1b9b6aSSam Leffler 	 */
7718a1b9b6aSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
7728a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
7738a1b9b6aSSam Leffler 	ieee80211_wme_initparams(ic);
774acc4f7f5SSam Leffler 
775acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA)
7768a1b9b6aSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
777acc4f7f5SSam Leffler 	else
778acc4f7f5SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
7798a1b9b6aSSam Leffler 	return 1;
7808a1b9b6aSSam Leffler }
7818a1b9b6aSSam Leffler 
7828a1b9b6aSSam Leffler /*
7838a1b9b6aSSam Leffler  * Leave the specified IBSS/BSS network.  The node is assumed to
7848a1b9b6aSSam Leffler  * be passed in with a held reference.
7858a1b9b6aSSam Leffler  */
7868a1b9b6aSSam Leffler void
7878a1b9b6aSSam Leffler ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
7888a1b9b6aSSam Leffler {
7898a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
7908a1b9b6aSSam Leffler 	ieee80211_notify_node_leave(ic, ni);
7918a1b9b6aSSam Leffler }
7928a1b9b6aSSam Leffler 
7931a1e1d21SSam Leffler static struct ieee80211_node *
7948a1b9b6aSSam Leffler node_alloc(struct ieee80211_node_table *nt)
7951a1e1d21SSam Leffler {
796410ca74bSSam Leffler 	struct ieee80211_node *ni;
7978a1b9b6aSSam Leffler 
798410ca74bSSam Leffler 	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
799410ca74bSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
800410ca74bSSam Leffler 	return ni;
8011a1e1d21SSam Leffler }
8021a1e1d21SSam Leffler 
8038a1b9b6aSSam Leffler /*
8048a1b9b6aSSam Leffler  * Reclaim any resources in a node and reset any critical
8058a1b9b6aSSam Leffler  * state.  Typically nodes are free'd immediately after,
8068a1b9b6aSSam Leffler  * but in some cases the storage may be reused so we need
8078a1b9b6aSSam Leffler  * to insure consistent state (should probably fix that).
8088a1b9b6aSSam Leffler  */
8091a1e1d21SSam Leffler static void
8108a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni)
8111a1e1d21SSam Leffler {
8128a1b9b6aSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
8138a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
8148a1b9b6aSSam Leffler 	int i, qlen;
8158a1b9b6aSSam Leffler 
8168a1b9b6aSSam Leffler 	/* NB: preserve ni_table */
8178a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
8188a1b9b6aSSam Leffler 		ic->ic_ps_sta--;
8198a1b9b6aSSam Leffler 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
8208a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
8218a1b9b6aSSam Leffler 		    "[%s] power save mode off, %u sta's in ps mode\n",
8228a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
8238a1b9b6aSSam Leffler 	}
8248a1b9b6aSSam Leffler 
8258a1b9b6aSSam Leffler 	/*
8268a1b9b6aSSam Leffler 	 * Drain power save queue and, if needed, clear TIM.
8278a1b9b6aSSam Leffler 	 */
8288a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
8298a1b9b6aSSam Leffler 	if (qlen != 0 && ic->ic_set_tim != NULL)
8308a1b9b6aSSam Leffler 		ic->ic_set_tim(ic, ni, 0);
8318a1b9b6aSSam Leffler 
8328a1b9b6aSSam Leffler 	ni->ni_associd = 0;
8338a1b9b6aSSam Leffler 	if (ni->ni_challenge != NULL) {
8348a1b9b6aSSam Leffler 		FREE(ni->ni_challenge, M_DEVBUF);
8358a1b9b6aSSam Leffler 		ni->ni_challenge = NULL;
8368a1b9b6aSSam Leffler 	}
8378a1b9b6aSSam Leffler 	/*
8388a1b9b6aSSam Leffler 	 * Preserve SSID, WPA, and WME ie's so the bss node is
8398a1b9b6aSSam Leffler 	 * reusable during a re-auth/re-assoc state transition.
8408a1b9b6aSSam Leffler 	 * If we remove these data they will not be recreated
8418a1b9b6aSSam Leffler 	 * because they come from a probe-response or beacon frame
8428a1b9b6aSSam Leffler 	 * which cannot be expected prior to the association-response.
8438a1b9b6aSSam Leffler 	 * This should not be an issue when operating in other modes
8448a1b9b6aSSam Leffler 	 * as stations leaving always go through a full state transition
8458a1b9b6aSSam Leffler 	 * which will rebuild this state.
8468a1b9b6aSSam Leffler 	 *
8478a1b9b6aSSam Leffler 	 * XXX does this leave us open to inheriting old state?
8488a1b9b6aSSam Leffler 	 */
8498a1b9b6aSSam Leffler 	for (i = 0; i < N(ni->ni_rxfrag); i++)
8508a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[i] != NULL) {
8518a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[i]);
8528a1b9b6aSSam Leffler 			ni->ni_rxfrag[i] = NULL;
8538a1b9b6aSSam Leffler 		}
8548a1b9b6aSSam Leffler 	ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
8558a1b9b6aSSam Leffler #undef N
8568a1b9b6aSSam Leffler }
8578a1b9b6aSSam Leffler 
8588a1b9b6aSSam Leffler static void
8598a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni)
8608a1b9b6aSSam Leffler {
8618a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
8628a1b9b6aSSam Leffler 
8638a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
8648a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
8658a1b9b6aSSam Leffler 		FREE(ni->ni_wpa_ie, M_DEVBUF);
8668a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
8678a1b9b6aSSam Leffler 		FREE(ni->ni_wme_ie, M_DEVBUF);
8688a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DESTROY(ni);
869410ca74bSSam Leffler 	FREE(ni, M_80211_NODE);
8701a1e1d21SSam Leffler }
8711a1e1d21SSam Leffler 
872d1e61976SSam Leffler static u_int8_t
8738a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni)
874d1e61976SSam Leffler {
875d1e61976SSam Leffler 	return ni->ni_rssi;
876d1e61976SSam Leffler }
877d1e61976SSam Leffler 
8781a1e1d21SSam Leffler static void
8798a1b9b6aSSam Leffler ieee80211_setup_node(struct ieee80211_node_table *nt,
8808a1b9b6aSSam Leffler 	struct ieee80211_node *ni, const u_int8_t *macaddr)
8811a1e1d21SSam Leffler {
8828a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
8831a1e1d21SSam Leffler 	int hash;
8841a1e1d21SSam Leffler 
8858a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
88649a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
8878a1b9b6aSSam Leffler 		ether_sprintf(macaddr), nt->nt_name);
8888a1b9b6aSSam Leffler 
8891a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
8901a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
8918a1b9b6aSSam Leffler 	ieee80211_node_initref(ni);		/* mark referenced */
8928a1b9b6aSSam Leffler 	ni->ni_chan = IEEE80211_CHAN_ANYC;
8938a1b9b6aSSam Leffler 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
8948a1b9b6aSSam Leffler 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
8958a1b9b6aSSam Leffler 	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
8962045f699SSam Leffler 	ni->ni_inact_reload = nt->nt_inact_init;
8972045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
8988a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
8998a1b9b6aSSam Leffler 
9008a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
9018a1b9b6aSSam Leffler 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
9028a1b9b6aSSam Leffler 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
9038a1b9b6aSSam Leffler 	ni->ni_table = nt;
9048a1b9b6aSSam Leffler 	ni->ni_ic = ic;
9058a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
9061a1e1d21SSam Leffler }
9071a1e1d21SSam Leffler 
9081a1e1d21SSam Leffler struct ieee80211_node *
9098a1b9b6aSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
9101a1e1d21SSam Leffler {
9118a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9128a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9138a1b9b6aSSam Leffler 
9148a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
9151a1e1d21SSam Leffler 	if (ni != NULL)
9168a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
917c64bfa0fSSam Leffler 	else
918c64bfa0fSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
9191a1e1d21SSam Leffler 	return ni;
9201a1e1d21SSam Leffler }
9211a1e1d21SSam Leffler 
9221a1e1d21SSam Leffler struct ieee80211_node *
9238a1b9b6aSSam Leffler ieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
9241a1e1d21SSam Leffler {
9258a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9268a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9278a1b9b6aSSam Leffler 
9288a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
9291a1e1d21SSam Leffler 	if (ni != NULL) {
9308a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
931694dca64SSam Leffler 		/*
932694dca64SSam Leffler 		 * Inherit from ic_bss.
933694dca64SSam Leffler 		 */
9348a1b9b6aSSam Leffler 		ni->ni_authmode = ic->ic_bss->ni_authmode;
9358a1b9b6aSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
9368a1b9b6aSSam Leffler 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
937694dca64SSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
9388a1b9b6aSSam Leffler 		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
9398a1b9b6aSSam Leffler 		ni->ni_rsn = ic->ic_bss->ni_rsn;
940694dca64SSam Leffler 	} else
941694dca64SSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
9421a1e1d21SSam Leffler 	return ni;
9431a1e1d21SSam Leffler }
9441a1e1d21SSam Leffler 
945750d6d0cSSam Leffler static struct ieee80211_node *
9468a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9478a1b9b6aSSam Leffler _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
9488a1b9b6aSSam Leffler 	const u_int8_t *macaddr, const char *func, int line)
9498a1b9b6aSSam Leffler #else
9508a1b9b6aSSam Leffler _ieee80211_find_node(struct ieee80211_node_table *nt,
9518a1b9b6aSSam Leffler 	const u_int8_t *macaddr)
9528a1b9b6aSSam Leffler #endif
9531a1e1d21SSam Leffler {
9541a1e1d21SSam Leffler 	struct ieee80211_node *ni;
9551a1e1d21SSam Leffler 	int hash;
9561a1e1d21SSam Leffler 
9578a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
958750d6d0cSSam Leffler 
9591a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
9608a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
9611a1e1d21SSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
9628a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
9638a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9648a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
96549a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
96649a15236SSam Leffler 			    func, line,
96749a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
9688a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
9698a1b9b6aSSam Leffler #endif
970750d6d0cSSam Leffler 			return ni;
9711a1e1d21SSam Leffler 		}
9721a1e1d21SSam Leffler 	}
973750d6d0cSSam Leffler 	return NULL;
974750d6d0cSSam Leffler }
9758a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9768a1b9b6aSSam Leffler #define	_ieee80211_find_node(nt, mac) \
9778a1b9b6aSSam Leffler 	_ieee80211_find_node_debug(nt, mac, func, line)
9788a1b9b6aSSam Leffler #endif
979750d6d0cSSam Leffler 
980750d6d0cSSam Leffler struct ieee80211_node *
9818a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9828a1b9b6aSSam Leffler ieee80211_find_node_debug(struct ieee80211_node_table *nt,
9838a1b9b6aSSam Leffler 	const u_int8_t *macaddr, const char *func, int line)
9848a1b9b6aSSam Leffler #else
9858a1b9b6aSSam Leffler ieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
9868a1b9b6aSSam Leffler #endif
987750d6d0cSSam Leffler {
988750d6d0cSSam Leffler 	struct ieee80211_node *ni;
989750d6d0cSSam Leffler 
9908a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
9918a1b9b6aSSam Leffler 	ni = _ieee80211_find_node(nt, macaddr);
9928a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
9931a1e1d21SSam Leffler 	return ni;
9941a1e1d21SSam Leffler }
9951a1e1d21SSam Leffler 
9961a1e1d21SSam Leffler /*
9978a1b9b6aSSam Leffler  * Fake up a node; this handles node discovery in adhoc mode.
9988a1b9b6aSSam Leffler  * Note that for the driver's benefit we we treat this like
9998a1b9b6aSSam Leffler  * an association so the driver has an opportunity to setup
10008a1b9b6aSSam Leffler  * it's private state.
10018a1b9b6aSSam Leffler  */
10028a1b9b6aSSam Leffler struct ieee80211_node *
10038a1b9b6aSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
10048a1b9b6aSSam Leffler 	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
10058a1b9b6aSSam Leffler {
10068a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
10078a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10088a1b9b6aSSam Leffler 
10098a1b9b6aSSam Leffler 	ni = ieee80211_dup_bss(nt, macaddr);
10108a1b9b6aSSam Leffler 	if (ni != NULL) {
10118a1b9b6aSSam Leffler 		/* XXX no rate negotiation; just dup */
10128a1b9b6aSSam Leffler 		ni->ni_rates = ic->ic_bss->ni_rates;
1013736b3dc3SSam Leffler 		if (ic->ic_newassoc != NULL)
10148a1b9b6aSSam Leffler 			ic->ic_newassoc(ic, ni, 1);
10158a1b9b6aSSam Leffler 		/* XXX not right for 802.1x/WPA */
10168a1b9b6aSSam Leffler 		ieee80211_node_authorize(ic, ni);
10178a1b9b6aSSam Leffler 	}
10188a1b9b6aSSam Leffler 	return ni;
10198a1b9b6aSSam Leffler }
10208a1b9b6aSSam Leffler 
10218a1b9b6aSSam Leffler /*
10228a1b9b6aSSam Leffler  * Locate the node for sender, track state, and then pass the
10238a1b9b6aSSam Leffler  * (referenced) node up to the 802.11 layer for its use.  We
10248a1b9b6aSSam Leffler  * are required to pass some node so we fall back to ic_bss
10258a1b9b6aSSam Leffler  * when this frame is from an unknown sender.  The 802.11 layer
10268a1b9b6aSSam Leffler  * knows this means the sender wasn't in the node table and
10278a1b9b6aSSam Leffler  * acts accordingly.
10288a1b9b6aSSam Leffler  */
10298a1b9b6aSSam Leffler struct ieee80211_node *
10308a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10318a1b9b6aSSam Leffler ieee80211_find_rxnode_debug(struct ieee80211com *ic,
10328a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh, const char *func, int line)
10338a1b9b6aSSam Leffler #else
10348a1b9b6aSSam Leffler ieee80211_find_rxnode(struct ieee80211com *ic,
10358a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh)
10368a1b9b6aSSam Leffler #endif
10378a1b9b6aSSam Leffler {
10388a1b9b6aSSam Leffler #define	IS_CTL(wh) \
10398a1b9b6aSSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
10408a1b9b6aSSam Leffler #define	IS_PSPOLL(wh) \
10418a1b9b6aSSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
10428a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
10438a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10448a1b9b6aSSam Leffler 
10458a1b9b6aSSam Leffler 	/* XXX may want scanned nodes in the neighbor table for adhoc */
10468a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
10478a1b9b6aSSam Leffler 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
10488a1b9b6aSSam Leffler 	    (ic->ic_flags & IEEE80211_F_SCAN))
10498a1b9b6aSSam Leffler 		nt = &ic->ic_scan;
10508a1b9b6aSSam Leffler 	else
1051acc4f7f5SSam Leffler 		nt = &ic->ic_sta;
10528a1b9b6aSSam Leffler 	/* XXX check ic_bss first in station mode */
10538a1b9b6aSSam Leffler 	/* XXX 4-address frames? */
10548a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
10558a1b9b6aSSam Leffler 	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
10568a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr1);
10578a1b9b6aSSam Leffler 	else
10588a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr2);
10598a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
10608a1b9b6aSSam Leffler 
10618a1b9b6aSSam Leffler 	return (ni != NULL ? ni : ieee80211_ref_node(ic->ic_bss));
10628a1b9b6aSSam Leffler #undef IS_PSPOLL
10638a1b9b6aSSam Leffler #undef IS_CTL
10648a1b9b6aSSam Leffler }
10658a1b9b6aSSam Leffler 
10668a1b9b6aSSam Leffler /*
1067750d6d0cSSam Leffler  * Return a reference to the appropriate node for sending
1068750d6d0cSSam Leffler  * a data frame.  This handles node discovery in adhoc networks.
1069750d6d0cSSam Leffler  */
1070750d6d0cSSam Leffler struct ieee80211_node *
10718a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10728a1b9b6aSSam Leffler ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
10738a1b9b6aSSam Leffler 	const char *func, int line)
10748a1b9b6aSSam Leffler #else
10758a1b9b6aSSam Leffler ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
10768a1b9b6aSSam Leffler #endif
1077750d6d0cSSam Leffler {
1078acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1079750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1080750d6d0cSSam Leffler 
1081750d6d0cSSam Leffler 	/*
1082750d6d0cSSam Leffler 	 * The destination address should be in the node table
1083750d6d0cSSam Leffler 	 * unless we are operating in station mode or this is a
1084750d6d0cSSam Leffler 	 * multicast/broadcast frame.
1085750d6d0cSSam Leffler 	 */
1086acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
10878a1b9b6aSSam Leffler 		return ieee80211_ref_node(ic->ic_bss);
1088750d6d0cSSam Leffler 
1089750d6d0cSSam Leffler 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
10908a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
10918a1b9b6aSSam Leffler 	ni = _ieee80211_find_node(nt, macaddr);
10928a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
10938a1b9b6aSSam Leffler 
10948a1b9b6aSSam Leffler 	if (ni == NULL) {
10958a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
109690d0d036SSam Leffler 		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
109790d0d036SSam Leffler 			/*
109890d0d036SSam Leffler 			 * In adhoc mode cons up a node for the destination.
109990d0d036SSam Leffler 			 * Note that we need an additional reference for the
110090d0d036SSam Leffler 			 * caller to be consistent with _ieee80211_find_node.
110190d0d036SSam Leffler 			 */
11028a1b9b6aSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
110390d0d036SSam Leffler 			if (ni != NULL)
110490d0d036SSam Leffler 				(void) ieee80211_ref_node(ni);
110590d0d036SSam Leffler 		} else {
11068a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
11078a1b9b6aSSam Leffler 				"[%s] no node, discard frame (%s)\n",
11088a1b9b6aSSam Leffler 				ether_sprintf(macaddr), __func__);
11098a1b9b6aSSam Leffler 			ic->ic_stats.is_tx_nonode++;
1110750d6d0cSSam Leffler 		}
1111750d6d0cSSam Leffler 	}
1112750d6d0cSSam Leffler 	return ni;
1113750d6d0cSSam Leffler }
1114750d6d0cSSam Leffler 
1115750d6d0cSSam Leffler /*
11161a1e1d21SSam Leffler  * Like find but search based on the channel too.
11171a1e1d21SSam Leffler  */
11181a1e1d21SSam Leffler struct ieee80211_node *
11198a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
11208a1b9b6aSSam Leffler ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
11218a1b9b6aSSam Leffler 	const u_int8_t *macaddr, struct ieee80211_channel *chan,
11228a1b9b6aSSam Leffler 	const char *func, int line)
11238a1b9b6aSSam Leffler #else
11248a1b9b6aSSam Leffler ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
11258a1b9b6aSSam Leffler 	const u_int8_t *macaddr, struct ieee80211_channel *chan)
11268a1b9b6aSSam Leffler #endif
11271a1e1d21SSam Leffler {
11281a1e1d21SSam Leffler 	struct ieee80211_node *ni;
11291a1e1d21SSam Leffler 	int hash;
11301a1e1d21SSam Leffler 
11311a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
11328a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
11338a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1134750d6d0cSSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1135750d6d0cSSam Leffler 		    ni->ni_chan == chan) {
11368a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);		/* mark referenced */
11378a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
11388a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
113949a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
114049a15236SSam Leffler 			    func, line,
11418a1b9b6aSSam Leffler #else
114249a15236SSam Leffler 			    "%s %p<%s> refcnt %d\n", __func__,
11438a1b9b6aSSam Leffler #endif
114449a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
11458a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
11461a1e1d21SSam Leffler 			break;
11471a1e1d21SSam Leffler 		}
11481a1e1d21SSam Leffler 	}
11498a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
11501a1e1d21SSam Leffler 	return ni;
11511a1e1d21SSam Leffler }
11521a1e1d21SSam Leffler 
11538a1b9b6aSSam Leffler /*
11548a1b9b6aSSam Leffler  * Like find but search based on the ssid too.
11558a1b9b6aSSam Leffler  */
11568a1b9b6aSSam Leffler struct ieee80211_node *
11578a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
11588a1b9b6aSSam Leffler ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
11598a1b9b6aSSam Leffler 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
11608a1b9b6aSSam Leffler 	const char *func, int line)
11618a1b9b6aSSam Leffler #else
11628a1b9b6aSSam Leffler ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
11638a1b9b6aSSam Leffler 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
11648a1b9b6aSSam Leffler #endif
11651a1e1d21SSam Leffler {
11668a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
11671a1e1d21SSam Leffler 	struct ieee80211_node *ni;
11688a1b9b6aSSam Leffler 	int hash;
11691a1e1d21SSam Leffler 
11708a1b9b6aSSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
11718a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
11728a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
11738a1b9b6aSSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
11748a1b9b6aSSam Leffler 		    ni->ni_esslen == ic->ic_des_esslen &&
11758a1b9b6aSSam Leffler 		    memcmp(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen) == 0) {
11768a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);		/* mark referenced */
11778a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
11788a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
117949a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
118049a15236SSam Leffler 			    func, line,
11818a1b9b6aSSam Leffler #else
118249a15236SSam Leffler 			    "%s %p<%s> refcnt %d\n", __func__,
11838a1b9b6aSSam Leffler #endif
118449a15236SSam Leffler 			     ni, ether_sprintf(ni->ni_macaddr),
11858a1b9b6aSSam Leffler 			     ieee80211_node_refcnt(ni));
11868a1b9b6aSSam Leffler 			break;
11878a1b9b6aSSam Leffler 		}
11888a1b9b6aSSam Leffler 	}
11898a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
11908a1b9b6aSSam Leffler 	return ni;
11918a1b9b6aSSam Leffler }
11928a1b9b6aSSam Leffler 
11938a1b9b6aSSam Leffler static void
11948a1b9b6aSSam Leffler _ieee80211_free_node(struct ieee80211_node *ni)
11958a1b9b6aSSam Leffler {
11968a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
11978a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
11988a1b9b6aSSam Leffler 
11998a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
120049a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
120149a15236SSam Leffler 		ether_sprintf(ni->ni_macaddr),
12028a1b9b6aSSam Leffler 		nt != NULL ? nt->nt_name : "<gone>");
12038a1b9b6aSSam Leffler 
12048a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
12058a1b9b6aSSam Leffler 	if (nt != NULL) {
12068a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
12078a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
12088a1b9b6aSSam Leffler 	}
12098a1b9b6aSSam Leffler 	ic->ic_node_free(ni);
12108a1b9b6aSSam Leffler }
12118a1b9b6aSSam Leffler 
12128a1b9b6aSSam Leffler void
12138a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12148a1b9b6aSSam Leffler ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
12158a1b9b6aSSam Leffler #else
12168a1b9b6aSSam Leffler ieee80211_free_node(struct ieee80211_node *ni)
12178a1b9b6aSSam Leffler #endif
12188a1b9b6aSSam Leffler {
12198a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
12208a1b9b6aSSam Leffler 
12218a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
122229d368a7SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
122349a15236SSam Leffler 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
12248a1b9b6aSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
12258a1b9b6aSSam Leffler #endif
12268a1b9b6aSSam Leffler 	if (ieee80211_node_dectestref(ni)) {
12278a1b9b6aSSam Leffler 		/*
12288a1b9b6aSSam Leffler 		 * Beware; if the node is marked gone then it's already
12298a1b9b6aSSam Leffler 		 * been removed from the table and we cannot assume the
12308a1b9b6aSSam Leffler 		 * table still exists.  Regardless, there's no need to lock
12318a1b9b6aSSam Leffler 		 * the table.
12328a1b9b6aSSam Leffler 		 */
12338a1b9b6aSSam Leffler 		if (ni->ni_table != NULL) {
12348a1b9b6aSSam Leffler 			IEEE80211_NODE_LOCK(nt);
12358a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
12368a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
12378a1b9b6aSSam Leffler 		} else
12388a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
12398a1b9b6aSSam Leffler 	}
12401a1e1d21SSam Leffler }
12411a1e1d21SSam Leffler 
1242303ebc3cSSam Leffler /*
12438a1b9b6aSSam Leffler  * Reclaim a node.  If this is the last reference count then
12448a1b9b6aSSam Leffler  * do the normal free work.  Otherwise remove it from the node
12458a1b9b6aSSam Leffler  * table and mark it gone by clearing the back-reference.
12468a1b9b6aSSam Leffler  */
12478a1b9b6aSSam Leffler static void
12488a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
12498a1b9b6aSSam Leffler {
12508a1b9b6aSSam Leffler 
125149a15236SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
125249a15236SSam Leffler 		"%s: remove %p<%s> from %s table, refcnt %d\n",
125349a15236SSam Leffler 		__func__, ni, ether_sprintf(ni->ni_macaddr),
125449a15236SSam Leffler 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
12558a1b9b6aSSam Leffler 	if (!ieee80211_node_dectestref(ni)) {
12568a1b9b6aSSam Leffler 		/*
12578a1b9b6aSSam Leffler 		 * Other references are present, just remove the
12588a1b9b6aSSam Leffler 		 * node from the table so it cannot be found.  When
12598a1b9b6aSSam Leffler 		 * the references are dropped storage will be
1260acc4f7f5SSam Leffler 		 * reclaimed.
12618a1b9b6aSSam Leffler 		 */
12628a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
12638a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
12648a1b9b6aSSam Leffler 		ni->ni_table = NULL;		/* clear reference */
12658a1b9b6aSSam Leffler 	} else
12668a1b9b6aSSam Leffler 		_ieee80211_free_node(ni);
12678a1b9b6aSSam Leffler }
12688a1b9b6aSSam Leffler 
12698a1b9b6aSSam Leffler static void
12708a1b9b6aSSam Leffler ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
12718a1b9b6aSSam Leffler {
12728a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
12738a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
12748a1b9b6aSSam Leffler 
12758a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
12768a1b9b6aSSam Leffler 		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
12778a1b9b6aSSam Leffler 
12788a1b9b6aSSam Leffler 	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
12798a1b9b6aSSam Leffler 		if (ni->ni_associd != 0) {
12808a1b9b6aSSam Leffler 			if (ic->ic_auth->ia_node_leave != NULL)
12818a1b9b6aSSam Leffler 				ic->ic_auth->ia_node_leave(ic, ni);
12828a1b9b6aSSam Leffler 			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
12838a1b9b6aSSam Leffler 		}
12848a1b9b6aSSam Leffler 		node_reclaim(nt, ni);
12858a1b9b6aSSam Leffler 	}
12868a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
12878a1b9b6aSSam Leffler }
12888a1b9b6aSSam Leffler 
12898a1b9b6aSSam Leffler static void
12908a1b9b6aSSam Leffler ieee80211_free_allnodes(struct ieee80211_node_table *nt)
12918a1b9b6aSSam Leffler {
12928a1b9b6aSSam Leffler 
12938a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
12948a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
12958a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
12968a1b9b6aSSam Leffler }
12978a1b9b6aSSam Leffler 
12988a1b9b6aSSam Leffler /*
12998a1b9b6aSSam Leffler  * Timeout entries in the scan cache.
13008a1b9b6aSSam Leffler  */
13018a1b9b6aSSam Leffler static void
13028a1b9b6aSSam Leffler ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
13038a1b9b6aSSam Leffler {
13048a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
13058a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *tni;
13068a1b9b6aSSam Leffler 
13078a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
13088a1b9b6aSSam Leffler 	ni = ic->ic_bss;
13098a1b9b6aSSam Leffler 	/* XXX belongs elsewhere */
13108a1b9b6aSSam Leffler 	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
13118a1b9b6aSSam Leffler 		m_freem(ni->ni_rxfrag[0]);
13128a1b9b6aSSam Leffler 		ni->ni_rxfrag[0] = NULL;
13138a1b9b6aSSam Leffler 	}
13148a1b9b6aSSam Leffler 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
13158a1b9b6aSSam Leffler 		if (ni->ni_inact && --ni->ni_inact == 0) {
13168a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
13178a1b9b6aSSam Leffler 			    "[%s] scan candidate purged from cache "
13188a1b9b6aSSam Leffler 			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
131949a15236SSam Leffler 			    ieee80211_node_refcnt(ni));
13208a1b9b6aSSam Leffler 			node_reclaim(nt, ni);
13218a1b9b6aSSam Leffler 		}
13228a1b9b6aSSam Leffler 	}
13238a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
13248a1b9b6aSSam Leffler 
13258a1b9b6aSSam Leffler 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
13268a1b9b6aSSam Leffler }
13278a1b9b6aSSam Leffler 
13288a1b9b6aSSam Leffler /*
13298a1b9b6aSSam Leffler  * Timeout inactive stations and do related housekeeping.
13308a1b9b6aSSam Leffler  * Note that we cannot hold the node lock while sending a
13318a1b9b6aSSam Leffler  * frame as this would lead to a LOR.  Instead we use a
13328a1b9b6aSSam Leffler  * generation number to mark nodes that we've scanned and
13338a1b9b6aSSam Leffler  * drop the lock and restart a scan if we have to time out
13348a1b9b6aSSam Leffler  * a node.  Since we are single-threaded by virtue of
1335303ebc3cSSam Leffler  * controlling the inactivity timer we can be sure this will
1336303ebc3cSSam Leffler  * process each node only once.
1337303ebc3cSSam Leffler  */
13388a1b9b6aSSam Leffler static void
13398a1b9b6aSSam Leffler ieee80211_timeout_stations(struct ieee80211_node_table *nt)
13401a1e1d21SSam Leffler {
13418a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
1342303ebc3cSSam Leffler 	struct ieee80211_node *ni;
13438a1b9b6aSSam Leffler 	u_int gen;
13441a1e1d21SSam Leffler 
13458a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
13468a1b9b6aSSam Leffler 	gen = nt->nt_scangen++;
13478a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
134849a15236SSam Leffler 		"%s: %s scangen %u\n", __func__, nt->nt_name, gen);
1349303ebc3cSSam Leffler restart:
13508a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
13518a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1352303ebc3cSSam Leffler 		if (ni->ni_scangen == gen)	/* previously handled */
1353303ebc3cSSam Leffler 			continue;
1354303ebc3cSSam Leffler 		ni->ni_scangen = gen;
13550a915fadSSam Leffler 		/*
13568a1b9b6aSSam Leffler 		 * Free fragment if not needed anymore
13578a1b9b6aSSam Leffler 		 * (last fragment older than 1s).
13588a1b9b6aSSam Leffler 		 * XXX doesn't belong here
13590a915fadSSam Leffler 		 */
13608a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[0] != NULL &&
13618a1b9b6aSSam Leffler 		    ticks > ni->ni_rxfragstamp + hz) {
13628a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
13638a1b9b6aSSam Leffler 			ni->ni_rxfrag[0] = NULL;
13648a1b9b6aSSam Leffler 		}
1365ce647032SSam Leffler 		/*
1366ce647032SSam Leffler 		 * Special case ourself; we may be idle for extended periods
1367ce647032SSam Leffler 		 * of time and regardless reclaiming our state is wrong.
1368ce647032SSam Leffler 		 */
1369ce647032SSam Leffler 		if (ni == ic->ic_bss)
1370ce647032SSam Leffler 			continue;
13718a1b9b6aSSam Leffler 		ni->ni_inact--;
13728a1b9b6aSSam Leffler 		if (ni->ni_associd != 0) {
13738a1b9b6aSSam Leffler 			/*
13748a1b9b6aSSam Leffler 			 * Age frames on the power save queue. The
13758a1b9b6aSSam Leffler 			 * aging interval is 4 times the listen
13768a1b9b6aSSam Leffler 			 * interval specified by the station.  This
13778a1b9b6aSSam Leffler 			 * number is factored into the age calculations
13788a1b9b6aSSam Leffler 			 * when the frame is placed on the queue.  We
13798a1b9b6aSSam Leffler 			 * store ages as time differences we can check
13808a1b9b6aSSam Leffler 			 * and/or adjust only the head of the list.
13818a1b9b6aSSam Leffler 			 */
13828a1b9b6aSSam Leffler 			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
13838a1b9b6aSSam Leffler 				struct mbuf *m;
13848a1b9b6aSSam Leffler 				int discard = 0;
13858a1b9b6aSSam Leffler 
13868a1b9b6aSSam Leffler 				IEEE80211_NODE_SAVEQ_LOCK(ni);
13878a1b9b6aSSam Leffler 				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
13888a1b9b6aSSam Leffler 				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
13898a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
13908a1b9b6aSSam Leffler 					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
13918a1b9b6aSSam Leffler 					m_freem(m);
13928a1b9b6aSSam Leffler 					discard++;
13938a1b9b6aSSam Leffler 				}
13948a1b9b6aSSam Leffler 				if (m != NULL)
13958a1b9b6aSSam Leffler 					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
13968a1b9b6aSSam Leffler 				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
13978a1b9b6aSSam Leffler 
13988a1b9b6aSSam Leffler 				if (discard != 0) {
13998a1b9b6aSSam Leffler 					IEEE80211_DPRINTF(ic,
14008a1b9b6aSSam Leffler 					    IEEE80211_MSG_POWER,
14018a1b9b6aSSam Leffler 					    "[%s] discard %u frames for age\n",
14028a1b9b6aSSam Leffler 					    ether_sprintf(ni->ni_macaddr),
14038a1b9b6aSSam Leffler 					    discard);
14048a1b9b6aSSam Leffler 					IEEE80211_NODE_STAT_ADD(ni,
14058a1b9b6aSSam Leffler 						ps_discard, discard);
14068a1b9b6aSSam Leffler 					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
14078a1b9b6aSSam Leffler 						ic->ic_set_tim(ic, ni, 0);
14088a1b9b6aSSam Leffler 				}
14098a1b9b6aSSam Leffler 			}
14108a1b9b6aSSam Leffler 			/*
14118a1b9b6aSSam Leffler 			 * Probe the station before time it out.  We
14128a1b9b6aSSam Leffler 			 * send a null data frame which may not be
14138a1b9b6aSSam Leffler 			 * universally supported by drivers (need it
14148a1b9b6aSSam Leffler 			 * for ps-poll support so it should be...).
14158a1b9b6aSSam Leffler 			 */
14162045f699SSam Leffler 			if (0 < ni->ni_inact &&
14172045f699SSam Leffler 			    ni->ni_inact <= ic->ic_inact_probe) {
14188a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
14198a1b9b6aSSam Leffler 				    "[%s] probe station due to inactivity\n",
14208a1b9b6aSSam Leffler 				    ether_sprintf(ni->ni_macaddr));
14218a1b9b6aSSam Leffler 				IEEE80211_NODE_UNLOCK(nt);
14228a1b9b6aSSam Leffler 				ieee80211_send_nulldata(ic, ni);
14238a1b9b6aSSam Leffler 				/* XXX stat? */
14248a1b9b6aSSam Leffler 				goto restart;
14258a1b9b6aSSam Leffler 			}
14268a1b9b6aSSam Leffler 		}
14278a1b9b6aSSam Leffler 		if (ni->ni_inact <= 0) {
14288a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
14298a1b9b6aSSam Leffler 			    "[%s] station timed out due to inactivity "
14308a1b9b6aSSam Leffler 			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
14318a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
14328a1b9b6aSSam Leffler 			/*
14338a1b9b6aSSam Leffler 			 * Send a deauthenticate frame and drop the station.
14348a1b9b6aSSam Leffler 			 * This is somewhat complicated due to reference counts
14358a1b9b6aSSam Leffler 			 * and locking.  At this point a station will typically
14368a1b9b6aSSam Leffler 			 * have a reference count of 1.  ieee80211_node_leave
14378a1b9b6aSSam Leffler 			 * will do a "free" of the node which will drop the
14388a1b9b6aSSam Leffler 			 * reference count.  But in the meantime a reference
14398a1b9b6aSSam Leffler 			 * wil be held by the deauth frame.  The actual reclaim
14408a1b9b6aSSam Leffler 			 * of the node will happen either after the tx is
14418a1b9b6aSSam Leffler 			 * completed or by ieee80211_node_leave.
14428a1b9b6aSSam Leffler 			 *
14438a1b9b6aSSam Leffler 			 * Separately we must drop the node lock before sending
14448a1b9b6aSSam Leffler 			 * in case the driver takes a lock, as this will result
14458a1b9b6aSSam Leffler 			 * in  LOR between the node lock and the driver lock.
14468a1b9b6aSSam Leffler 			 */
14478a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
14488a1b9b6aSSam Leffler 			if (ni->ni_associd != 0) {
14491a1e1d21SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
14501a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
14511a1e1d21SSam Leffler 				    IEEE80211_REASON_AUTH_EXPIRE);
14528a1b9b6aSSam Leffler 			}
14538a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
14541be50176SSam Leffler 			ic->ic_stats.is_node_timeout++;
1455303ebc3cSSam Leffler 			goto restart;
1456303ebc3cSSam Leffler 		}
14571a1e1d21SSam Leffler 	}
14588a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
14598a1b9b6aSSam Leffler 
14608a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
14618a1b9b6aSSam Leffler 
14628a1b9b6aSSam Leffler 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
14631a1e1d21SSam Leffler }
14641a1e1d21SSam Leffler 
14651a1e1d21SSam Leffler void
14668a1b9b6aSSam Leffler ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
14671a1e1d21SSam Leffler {
14681a1e1d21SSam Leffler 	struct ieee80211_node *ni;
14698a1b9b6aSSam Leffler 	u_int gen;
14701a1e1d21SSam Leffler 
14718a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
14728a1b9b6aSSam Leffler 	gen = nt->nt_scangen++;
14738a1b9b6aSSam Leffler restart:
14748a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
14758a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
14768a1b9b6aSSam Leffler 		if (ni->ni_scangen != gen) {
14778a1b9b6aSSam Leffler 			ni->ni_scangen = gen;
14788a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
14798a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
14801a1e1d21SSam Leffler 			(*f)(arg, ni);
14818a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
14828a1b9b6aSSam Leffler 			goto restart;
14838a1b9b6aSSam Leffler 		}
14848a1b9b6aSSam Leffler 	}
14858a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
14868a1b9b6aSSam Leffler 
14878a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
14888a1b9b6aSSam Leffler }
14898a1b9b6aSSam Leffler 
14908a1b9b6aSSam Leffler void
14918a1b9b6aSSam Leffler ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
14928a1b9b6aSSam Leffler {
14938a1b9b6aSSam Leffler 	printf("0x%p: mac %s refcnt %d\n", ni,
14948a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
14958a1b9b6aSSam Leffler 	printf("\tscangen %u authmode %u flags 0x%x\n",
14968a1b9b6aSSam Leffler 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
14978a1b9b6aSSam Leffler 	printf("\tassocid 0x%x txpower %u vlan %u\n",
14988a1b9b6aSSam Leffler 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
14998a1b9b6aSSam Leffler 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
15008a1b9b6aSSam Leffler 		ni->ni_txseqs[0],
15018a1b9b6aSSam Leffler 		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
15028a1b9b6aSSam Leffler 		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
15038a1b9b6aSSam Leffler 		ni->ni_rxfragstamp);
15048a1b9b6aSSam Leffler 	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
15058a1b9b6aSSam Leffler 		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
15068a1b9b6aSSam Leffler 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
15078a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
15088a1b9b6aSSam Leffler 		ni->ni_esslen, ni->ni_essid,
15098a1b9b6aSSam Leffler 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
15108a1b9b6aSSam Leffler 	printf("\tfails %u inact %u txrate %u\n",
15118a1b9b6aSSam Leffler 		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
15128a1b9b6aSSam Leffler }
15138a1b9b6aSSam Leffler 
15148a1b9b6aSSam Leffler void
15158a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt)
15168a1b9b6aSSam Leffler {
15178a1b9b6aSSam Leffler 	ieee80211_iterate_nodes(nt,
15188a1b9b6aSSam Leffler 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
15198a1b9b6aSSam Leffler }
15208a1b9b6aSSam Leffler 
15218a1b9b6aSSam Leffler /*
15228a1b9b6aSSam Leffler  * Handle a station joining an 11g network.
15238a1b9b6aSSam Leffler  */
15248a1b9b6aSSam Leffler static void
15258a1b9b6aSSam Leffler ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
15268a1b9b6aSSam Leffler {
15278a1b9b6aSSam Leffler 
15288a1b9b6aSSam Leffler 	/*
15298a1b9b6aSSam Leffler 	 * Station isn't capable of short slot time.  Bump
15308a1b9b6aSSam Leffler 	 * the count of long slot time stations and disable
15318a1b9b6aSSam Leffler 	 * use of short slot time.  Note that the actual switch
15328a1b9b6aSSam Leffler 	 * over to long slot time use may not occur until the
15338a1b9b6aSSam Leffler 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
15348a1b9b6aSSam Leffler 	 */
15358a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
15368a1b9b6aSSam Leffler 		ic->ic_longslotsta++;
15378a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
15388a1b9b6aSSam Leffler 		    "[%s] station needs long slot time, count %d\n",
15398a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
15408a1b9b6aSSam Leffler 		/* XXX vap's w/ conflicting needs won't work */
15418a1b9b6aSSam Leffler 		ieee80211_set_shortslottime(ic, 0);
15428a1b9b6aSSam Leffler 	}
15438a1b9b6aSSam Leffler 	/*
15448a1b9b6aSSam Leffler 	 * If the new station is not an ERP station
15458a1b9b6aSSam Leffler 	 * then bump the counter and enable protection
15468a1b9b6aSSam Leffler 	 * if configured.
15478a1b9b6aSSam Leffler 	 */
15488a1b9b6aSSam Leffler 	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
15498a1b9b6aSSam Leffler 		ic->ic_nonerpsta++;
15508a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
15518a1b9b6aSSam Leffler 		    "[%s] station is !ERP, %d non-ERP stations associated\n",
15528a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
15538a1b9b6aSSam Leffler 		/*
15548a1b9b6aSSam Leffler 		 * If protection is configured, enable it.
15558a1b9b6aSSam Leffler 		 */
15568a1b9b6aSSam Leffler 		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
15578a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
15588a1b9b6aSSam Leffler 			    "%s: enable use of protection\n", __func__);
15598a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
15608a1b9b6aSSam Leffler 		}
15618a1b9b6aSSam Leffler 		/*
15628a1b9b6aSSam Leffler 		 * If station does not support short preamble
15638a1b9b6aSSam Leffler 		 * then we must enable use of Barker preamble.
15648a1b9b6aSSam Leffler 		 */
15658a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
15668a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
15678a1b9b6aSSam Leffler 			    "[%s] station needs long preamble\n",
15688a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr));
15698a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
15708a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
15718a1b9b6aSSam Leffler 		}
15728a1b9b6aSSam Leffler 	} else
15738a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
15748a1b9b6aSSam Leffler }
15758a1b9b6aSSam Leffler 
15768a1b9b6aSSam Leffler void
15778a1b9b6aSSam Leffler ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
15788a1b9b6aSSam Leffler {
15798a1b9b6aSSam Leffler 	int newassoc;
15808a1b9b6aSSam Leffler 
15818a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
15828a1b9b6aSSam Leffler 		u_int16_t aid;
15838a1b9b6aSSam Leffler 
15848a1b9b6aSSam Leffler 		/*
15858a1b9b6aSSam Leffler 		 * It would be good to search the bitmap
15868a1b9b6aSSam Leffler 		 * more efficiently, but this will do for now.
15878a1b9b6aSSam Leffler 		 */
15888a1b9b6aSSam Leffler 		for (aid = 1; aid < ic->ic_max_aid; aid++) {
15898a1b9b6aSSam Leffler 			if (!IEEE80211_AID_ISSET(aid,
15908a1b9b6aSSam Leffler 			    ic->ic_aid_bitmap))
15918a1b9b6aSSam Leffler 				break;
15928a1b9b6aSSam Leffler 		}
15938a1b9b6aSSam Leffler 		if (aid >= ic->ic_max_aid) {
15948a1b9b6aSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
15958a1b9b6aSSam Leffler 			    IEEE80211_REASON_ASSOC_TOOMANY);
15968a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
15978a1b9b6aSSam Leffler 			return;
15988a1b9b6aSSam Leffler 		}
15998a1b9b6aSSam Leffler 		ni->ni_associd = aid | 0xc000;
16008a1b9b6aSSam Leffler 		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
16018a1b9b6aSSam Leffler 		ic->ic_sta_assoc++;
16028a1b9b6aSSam Leffler 		newassoc = 1;
1603624a1bdbSSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G ||
1604624a1bdbSSam Leffler 		    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
16058a1b9b6aSSam Leffler 			ieee80211_node_join_11g(ic, ni);
16068a1b9b6aSSam Leffler 	} else
16078a1b9b6aSSam Leffler 		newassoc = 0;
16088a1b9b6aSSam Leffler 
16098a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1610b8fcf546SSam Leffler 	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
1611b8fcf546SSam Leffler 	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
1612b8fcf546SSam Leffler 	    IEEE80211_NODE_AID(ni),
1613b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
1614b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
1615b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
1616b8fcf546SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
1617b8fcf546SSam Leffler 	);
16188a1b9b6aSSam Leffler 
16198a1b9b6aSSam Leffler 	/* give driver a chance to setup state like ni_txrate */
1620736b3dc3SSam Leffler 	if (ic->ic_newassoc != NULL)
16218a1b9b6aSSam Leffler 		ic->ic_newassoc(ic, ni, newassoc);
16222045f699SSam Leffler 	ni->ni_inact_reload = ic->ic_inact_auth;
16232045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
16248a1b9b6aSSam Leffler 	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
16258a1b9b6aSSam Leffler 	/* tell the authenticator about new station */
16268a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_join != NULL)
16278a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_join(ic, ni);
16288a1b9b6aSSam Leffler 	ieee80211_notify_node_join(ic, ni, newassoc);
16298a1b9b6aSSam Leffler }
16308a1b9b6aSSam Leffler 
16318a1b9b6aSSam Leffler /*
16328a1b9b6aSSam Leffler  * Handle a station leaving an 11g network.
16338a1b9b6aSSam Leffler  */
16348a1b9b6aSSam Leffler static void
16358a1b9b6aSSam Leffler ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
16368a1b9b6aSSam Leffler {
16378a1b9b6aSSam Leffler 
1638624a1bdbSSam Leffler 	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G ||
1639624a1bdbSSam Leffler 		ic->ic_curmode == IEEE80211_MODE_TURBO_G,
16408a1b9b6aSSam Leffler 		("not in 11g, curmode %x", ic->ic_curmode));
16418a1b9b6aSSam Leffler 
16428a1b9b6aSSam Leffler 	/*
16438a1b9b6aSSam Leffler 	 * If a long slot station do the slot time bookkeeping.
16448a1b9b6aSSam Leffler 	 */
16458a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
16468a1b9b6aSSam Leffler 		KASSERT(ic->ic_longslotsta > 0,
16478a1b9b6aSSam Leffler 		    ("bogus long slot station count %d", ic->ic_longslotsta));
16488a1b9b6aSSam Leffler 		ic->ic_longslotsta--;
16498a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
16508a1b9b6aSSam Leffler 		    "[%s] long slot time station leaves, count now %d\n",
16518a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
16528a1b9b6aSSam Leffler 		if (ic->ic_longslotsta == 0) {
16538a1b9b6aSSam Leffler 			/*
16548a1b9b6aSSam Leffler 			 * Re-enable use of short slot time if supported
16558a1b9b6aSSam Leffler 			 * and not operating in IBSS mode (per spec).
16568a1b9b6aSSam Leffler 			 */
16578a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
16588a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_IBSS) {
16598a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
16608a1b9b6aSSam Leffler 				    "%s: re-enable use of short slot time\n",
16618a1b9b6aSSam Leffler 				    __func__);
16628a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic, 1);
16638a1b9b6aSSam Leffler 			}
16648a1b9b6aSSam Leffler 		}
16658a1b9b6aSSam Leffler 	}
16668a1b9b6aSSam Leffler 	/*
16678a1b9b6aSSam Leffler 	 * If a non-ERP station do the protection-related bookkeeping.
16688a1b9b6aSSam Leffler 	 */
16698a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
16708a1b9b6aSSam Leffler 		KASSERT(ic->ic_nonerpsta > 0,
16718a1b9b6aSSam Leffler 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
16728a1b9b6aSSam Leffler 		ic->ic_nonerpsta--;
16738a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
16748a1b9b6aSSam Leffler 		    "[%s] non-ERP station leaves, count now %d\n",
16758a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
16768a1b9b6aSSam Leffler 		if (ic->ic_nonerpsta == 0) {
16778a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
16788a1b9b6aSSam Leffler 				"%s: disable use of protection\n", __func__);
16798a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
16808a1b9b6aSSam Leffler 			/* XXX verify mode? */
16818a1b9b6aSSam Leffler 			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
16828a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
16838a1b9b6aSSam Leffler 				    "%s: re-enable use of short preamble\n",
16848a1b9b6aSSam Leffler 				    __func__);
16858a1b9b6aSSam Leffler 				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
16868a1b9b6aSSam Leffler 				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
16878a1b9b6aSSam Leffler 			}
16888a1b9b6aSSam Leffler 		}
16898a1b9b6aSSam Leffler 	}
16908a1b9b6aSSam Leffler }
16918a1b9b6aSSam Leffler 
16928a1b9b6aSSam Leffler /*
16938a1b9b6aSSam Leffler  * Handle bookkeeping for station deauthentication/disassociation
16948a1b9b6aSSam Leffler  * when operating as an ap.
16958a1b9b6aSSam Leffler  */
16968a1b9b6aSSam Leffler void
16978a1b9b6aSSam Leffler ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
16988a1b9b6aSSam Leffler {
169944acc00dSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
17008a1b9b6aSSam Leffler 
17018a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
17028a1b9b6aSSam Leffler 	    "[%s] station with aid %d leaves\n",
17038a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
17048a1b9b6aSSam Leffler 
17058a1b9b6aSSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
17068a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_IBSS ||
17078a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_AHDEMO,
17088a1b9b6aSSam Leffler 		("unexpected operating mode %u", ic->ic_opmode));
17098a1b9b6aSSam Leffler 	/*
17108a1b9b6aSSam Leffler 	 * If node wasn't previously associated all
17118a1b9b6aSSam Leffler 	 * we need to do is reclaim the reference.
17128a1b9b6aSSam Leffler 	 */
17138a1b9b6aSSam Leffler 	/* XXX ibss mode bypasses 11g and notification */
17148a1b9b6aSSam Leffler 	if (ni->ni_associd == 0)
17158a1b9b6aSSam Leffler 		goto done;
17168a1b9b6aSSam Leffler 	/*
17178a1b9b6aSSam Leffler 	 * Tell the authenticator the station is leaving.
17188a1b9b6aSSam Leffler 	 * Note that we must do this before yanking the
17198a1b9b6aSSam Leffler 	 * association id as the authenticator uses the
17208a1b9b6aSSam Leffler 	 * associd to locate it's state block.
17218a1b9b6aSSam Leffler 	 */
17228a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_leave != NULL)
17238a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_leave(ic, ni);
17248a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
17258a1b9b6aSSam Leffler 	ni->ni_associd = 0;
17268a1b9b6aSSam Leffler 	ic->ic_sta_assoc--;
17278a1b9b6aSSam Leffler 
1728624a1bdbSSam Leffler 	if (ic->ic_curmode == IEEE80211_MODE_11G ||
1729624a1bdbSSam Leffler 	    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
17308a1b9b6aSSam Leffler 		ieee80211_node_leave_11g(ic, ni);
17318a1b9b6aSSam Leffler 	/*
17328a1b9b6aSSam Leffler 	 * Cleanup station state.  In particular clear various
17338a1b9b6aSSam Leffler 	 * state that might otherwise be reused if the node
17348a1b9b6aSSam Leffler 	 * is reused before the reference count goes to zero
17358a1b9b6aSSam Leffler 	 * (and memory is reclaimed).
17368a1b9b6aSSam Leffler 	 */
17378a1b9b6aSSam Leffler 	ieee80211_sta_leave(ic, ni);
17388a1b9b6aSSam Leffler done:
173944acc00dSSam Leffler 	/*
174044acc00dSSam Leffler 	 * Remove the node from any table it's recorded in and
174144acc00dSSam Leffler 	 * drop the caller's reference.  Removal from the table
174244acc00dSSam Leffler 	 * is important to insure the node is not reprocessed
174344acc00dSSam Leffler 	 * for inactivity.
174444acc00dSSam Leffler 	 */
174544acc00dSSam Leffler 	if (nt != NULL) {
174644acc00dSSam Leffler 		IEEE80211_NODE_LOCK(nt);
174744acc00dSSam Leffler 		node_reclaim(nt, ni);
174844acc00dSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
174944acc00dSSam Leffler 	} else
17508a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
17518a1b9b6aSSam Leffler }
17528a1b9b6aSSam Leffler 
17538a1b9b6aSSam Leffler u_int8_t
17548a1b9b6aSSam Leffler ieee80211_getrssi(struct ieee80211com *ic)
17558a1b9b6aSSam Leffler {
17568a1b9b6aSSam Leffler #define	NZ(x)	((x) == 0 ? 1 : (x))
1757acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
17588a1b9b6aSSam Leffler 	u_int32_t rssi_samples, rssi_total;
17598a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
17608a1b9b6aSSam Leffler 
17618a1b9b6aSSam Leffler 	rssi_total = 0;
17628a1b9b6aSSam Leffler 	rssi_samples = 0;
17638a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
17648a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
17658a1b9b6aSSam Leffler 		/* XXX locking */
1766acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
17678a1b9b6aSSam Leffler 			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
17688a1b9b6aSSam Leffler 				rssi_samples++;
17698a1b9b6aSSam Leffler 				rssi_total += ic->ic_node_getrssi(ni);
17708a1b9b6aSSam Leffler 			}
17718a1b9b6aSSam Leffler 		break;
17728a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
17738a1b9b6aSSam Leffler 		/* XXX locking */
1774acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
17758a1b9b6aSSam Leffler 			rssi_samples++;
17768a1b9b6aSSam Leffler 			rssi_total += ic->ic_node_getrssi(ni);
17778a1b9b6aSSam Leffler 		}
17788a1b9b6aSSam Leffler 		break;
17798a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
17808a1b9b6aSSam Leffler 		/* XXX locking */
1781acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
17828a1b9b6aSSam Leffler 			if (IEEE80211_AID(ni->ni_associd) != 0) {
17838a1b9b6aSSam Leffler 				rssi_samples++;
17848a1b9b6aSSam Leffler 				rssi_total += ic->ic_node_getrssi(ni);
17858a1b9b6aSSam Leffler 			}
17868a1b9b6aSSam Leffler 		break;
17878a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:	/* XXX */
17888a1b9b6aSSam Leffler 	case IEEE80211_M_STA:		/* use stats from associated ap */
17898a1b9b6aSSam Leffler 	default:
17908a1b9b6aSSam Leffler 		if (ic->ic_bss != NULL)
17918a1b9b6aSSam Leffler 			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
17928a1b9b6aSSam Leffler 		rssi_samples = 1;
17938a1b9b6aSSam Leffler 		break;
17948a1b9b6aSSam Leffler 	}
17958a1b9b6aSSam Leffler 	return rssi_total / NZ(rssi_samples);
17968a1b9b6aSSam Leffler #undef NZ
17978a1b9b6aSSam Leffler }
17988a1b9b6aSSam Leffler 
17998a1b9b6aSSam Leffler /*
18008a1b9b6aSSam Leffler  * Indicate whether there are frames queued for a station in power-save mode.
18018a1b9b6aSSam Leffler  */
18028a1b9b6aSSam Leffler static void
18038a1b9b6aSSam Leffler ieee80211_set_tim(struct ieee80211com *ic, struct ieee80211_node *ni, int set)
18048a1b9b6aSSam Leffler {
18058a1b9b6aSSam Leffler 	u_int16_t aid;
18068a1b9b6aSSam Leffler 
18078a1b9b6aSSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
18088a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_IBSS,
18098a1b9b6aSSam Leffler 		("operating mode %u", ic->ic_opmode));
18108a1b9b6aSSam Leffler 
18118a1b9b6aSSam Leffler 	aid = IEEE80211_AID(ni->ni_associd);
18128a1b9b6aSSam Leffler 	KASSERT(aid < ic->ic_max_aid,
18138a1b9b6aSSam Leffler 		("bogus aid %u, max %u", aid, ic->ic_max_aid));
18148a1b9b6aSSam Leffler 
18158a1b9b6aSSam Leffler 	IEEE80211_BEACON_LOCK(ic);
18168a1b9b6aSSam Leffler 	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
18178a1b9b6aSSam Leffler 		if (set) {
18188a1b9b6aSSam Leffler 			setbit(ic->ic_tim_bitmap, aid);
18198a1b9b6aSSam Leffler 			ic->ic_ps_pending++;
18208a1b9b6aSSam Leffler 		} else {
18218a1b9b6aSSam Leffler 			clrbit(ic->ic_tim_bitmap, aid);
18228a1b9b6aSSam Leffler 			ic->ic_ps_pending--;
18238a1b9b6aSSam Leffler 		}
18248a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
18258a1b9b6aSSam Leffler 	}
18268a1b9b6aSSam Leffler 	IEEE80211_BEACON_UNLOCK(ic);
18278a1b9b6aSSam Leffler }
18288a1b9b6aSSam Leffler 
18298a1b9b6aSSam Leffler /*
18308a1b9b6aSSam Leffler  * Node table support.
18318a1b9b6aSSam Leffler  */
18328a1b9b6aSSam Leffler 
18338a1b9b6aSSam Leffler static void
18348a1b9b6aSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic,
18358a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt,
18368a1b9b6aSSam Leffler 	const char *name, int inact,
18378a1b9b6aSSam Leffler 	void (*timeout)(struct ieee80211_node_table *))
18388a1b9b6aSSam Leffler {
18398a1b9b6aSSam Leffler 
18408a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
18418a1b9b6aSSam Leffler 		"%s %s table, inact %u\n", __func__, name, inact);
18428a1b9b6aSSam Leffler 
18438a1b9b6aSSam Leffler 	nt->nt_ic = ic;
18448a1b9b6aSSam Leffler 	/* XXX need unit */
18458a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
18468a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
18478a1b9b6aSSam Leffler 	TAILQ_INIT(&nt->nt_node);
18488a1b9b6aSSam Leffler 	nt->nt_name = name;
18498a1b9b6aSSam Leffler 	nt->nt_scangen = 1;
18508a1b9b6aSSam Leffler 	nt->nt_inact_init = inact;
18518a1b9b6aSSam Leffler 	nt->nt_timeout = timeout;
18528a1b9b6aSSam Leffler }
18538a1b9b6aSSam Leffler 
18548a1b9b6aSSam Leffler void
18558a1b9b6aSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt)
18568a1b9b6aSSam Leffler {
18578a1b9b6aSSam Leffler 
18588a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
18598a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
18608a1b9b6aSSam Leffler 
18618a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
18628a1b9b6aSSam Leffler 	nt->nt_inact_timer = 0;
18638a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
18648a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
18658a1b9b6aSSam Leffler }
18668a1b9b6aSSam Leffler 
18678a1b9b6aSSam Leffler static void
18688a1b9b6aSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
18698a1b9b6aSSam Leffler {
18708a1b9b6aSSam Leffler 
18718a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
18728a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
18738a1b9b6aSSam Leffler 
18748a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
18758a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_DESTROY(nt);
18768a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_DESTROY(nt);
18778a1b9b6aSSam Leffler }
1878