xref: /freebsd/sys/net80211/ieee80211_node.c (revision c1225b52f6a0f0ff6834dc34d5b6f94020097784)
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 
527268fa64SSam Leffler /*
537268fa64SSam Leffler  * Association id's are managed with a bit vector.
547268fa64SSam Leffler  */
557268fa64SSam Leffler #define	IEEE80211_AID_SET(b, w) \
567268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
577268fa64SSam Leffler #define	IEEE80211_AID_CLR(b, w) \
587268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
597268fa64SSam Leffler #define	IEEE80211_AID_ISSET(b, w) \
607268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
617268fa64SSam Leffler 
628a1b9b6aSSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
638a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *);
648a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *);
658a1b9b6aSSam Leffler static u_int8_t node_getrssi(const struct ieee80211_node *);
661a1e1d21SSam Leffler 
678a1b9b6aSSam Leffler static void ieee80211_setup_node(struct ieee80211_node_table *,
688a1b9b6aSSam Leffler 		struct ieee80211_node *, const u_int8_t *);
698a1b9b6aSSam Leffler static void _ieee80211_free_node(struct ieee80211_node *);
708a1b9b6aSSam Leffler static void ieee80211_free_allnodes(struct ieee80211_node_table *);
71d1e61976SSam Leffler 
728a1b9b6aSSam Leffler static void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
738a1b9b6aSSam Leffler static void ieee80211_timeout_stations(struct ieee80211_node_table *);
748a1b9b6aSSam Leffler 
75edfa57d0SSam Leffler static void ieee80211_set_tim(struct ieee80211_node *, int set);
768a1b9b6aSSam Leffler 
778a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic,
78c1225b52SSam Leffler 	struct ieee80211_node_table *nt, const char *name,
79c1225b52SSam Leffler 	int inact, int keyixmax,
808a1b9b6aSSam Leffler 	void (*timeout)(struct ieee80211_node_table *));
818a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
821a1e1d21SSam Leffler 
8332346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
8437c150c4SSam Leffler 
851a1e1d21SSam Leffler void
868a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic)
871a1e1d21SSam Leffler {
881a1e1d21SSam Leffler 
898a1b9b6aSSam Leffler 	ic->ic_node_alloc = node_alloc;
908a1b9b6aSSam Leffler 	ic->ic_node_free = node_free;
918a1b9b6aSSam Leffler 	ic->ic_node_cleanup = node_cleanup;
928a1b9b6aSSam Leffler 	ic->ic_node_getrssi = node_getrssi;
938a1b9b6aSSam Leffler 
948a1b9b6aSSam Leffler 	/* default station inactivity timer setings */
958a1b9b6aSSam Leffler 	ic->ic_inact_init = IEEE80211_INACT_INIT;
968a1b9b6aSSam Leffler 	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
978a1b9b6aSSam Leffler 	ic->ic_inact_run = IEEE80211_INACT_RUN;
988a1b9b6aSSam Leffler 	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
998a1b9b6aSSam Leffler 
100c1225b52SSam Leffler 	/* NB: driver should override */
1018a1b9b6aSSam Leffler 	ic->ic_max_aid = IEEE80211_AID_DEF;
102c1225b52SSam Leffler 	ic->ic_set_tim = ieee80211_set_tim;
103c1225b52SSam Leffler }
104c1225b52SSam Leffler 
105c1225b52SSam Leffler void
106c1225b52SSam Leffler ieee80211_node_lateattach(struct ieee80211com *ic)
107c1225b52SSam Leffler {
108c1225b52SSam Leffler 	struct ieee80211_rsnparms *rsn;
109c1225b52SSam Leffler 
110c1225b52SSam Leffler 	if (ic->ic_max_aid > IEEE80211_AID_MAX)
1118a1b9b6aSSam Leffler 		ic->ic_max_aid = IEEE80211_AID_MAX;
1128a1b9b6aSSam Leffler 	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
1138a1b9b6aSSam Leffler 		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
1148a1b9b6aSSam Leffler 		M_DEVBUF, M_NOWAIT | M_ZERO);
1158a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap == NULL) {
1168a1b9b6aSSam Leffler 		/* XXX no way to recover */
1178a1b9b6aSSam Leffler 		printf("%s: no memory for AID bitmap!\n", __func__);
1188a1b9b6aSSam Leffler 		ic->ic_max_aid = 0;
1198a1b9b6aSSam Leffler 	}
1208a1b9b6aSSam Leffler 
1218a1b9b6aSSam Leffler 	/* XXX defer until using hostap/ibss mode */
1228a1b9b6aSSam Leffler 	ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
1238a1b9b6aSSam Leffler 	MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
1248a1b9b6aSSam Leffler 		M_DEVBUF, M_NOWAIT | M_ZERO);
1258a1b9b6aSSam Leffler 	if (ic->ic_tim_bitmap == NULL) {
1268a1b9b6aSSam Leffler 		/* XXX no way to recover */
1278a1b9b6aSSam Leffler 		printf("%s: no memory for TIM bitmap!\n", __func__);
1288a1b9b6aSSam Leffler 	}
1292692bb26SSam Leffler 
130c1225b52SSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
131c1225b52SSam Leffler 		IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix,
132c1225b52SSam Leffler 		ieee80211_timeout_stations);
133c1225b52SSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
134c1225b52SSam Leffler 		IEEE80211_INACT_SCAN, 0,
135c1225b52SSam Leffler 		ieee80211_timeout_scan_candidates);
1362692bb26SSam Leffler 
137c1225b52SSam Leffler 	ieee80211_reset_bss(ic);
1388a1b9b6aSSam Leffler 	/*
1398a1b9b6aSSam Leffler 	 * Setup "global settings" in the bss node so that
1408a1b9b6aSSam Leffler 	 * each new station automatically inherits them.
1418a1b9b6aSSam Leffler 	 */
142c1225b52SSam Leffler 	rsn = &ic->ic_bss->ni_rsn;
1438a1b9b6aSSam Leffler 	/* WEP, TKIP, and AES-CCM are always supported */
1448a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
1458a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
1468a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
1478a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_AES)
1488a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
1498a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_CKIP)
1508a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
1518a1b9b6aSSam Leffler 	/*
1528a1b9b6aSSam Leffler 	 * Default unicast cipher to WEP for 802.1x use.  If
1538a1b9b6aSSam Leffler 	 * WPA is enabled the management code will set these
1548a1b9b6aSSam Leffler 	 * values to reflect.
1558a1b9b6aSSam Leffler 	 */
1568a1b9b6aSSam Leffler 	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
1578a1b9b6aSSam Leffler 	rsn->rsn_ucastkeylen = 104 / NBBY;
1588a1b9b6aSSam Leffler 	/*
1598a1b9b6aSSam Leffler 	 * WPA says the multicast cipher is the lowest unicast
1608a1b9b6aSSam Leffler 	 * cipher supported.  But we skip WEP which would
1618a1b9b6aSSam Leffler 	 * otherwise be used based on this criteria.
1628a1b9b6aSSam Leffler 	 */
1638a1b9b6aSSam Leffler 	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
1648a1b9b6aSSam Leffler 	rsn->rsn_mcastkeylen = 128 / NBBY;
1658a1b9b6aSSam Leffler 
1668a1b9b6aSSam Leffler 	/*
1678a1b9b6aSSam Leffler 	 * We support both WPA-PSK and 802.1x; the one used
1688a1b9b6aSSam Leffler 	 * is determined by the authentication mode and the
1698a1b9b6aSSam Leffler 	 * setting of the PSK state.
1708a1b9b6aSSam Leffler 	 */
1718a1b9b6aSSam Leffler 	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
1728a1b9b6aSSam Leffler 	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1738a1b9b6aSSam Leffler 
174c1225b52SSam Leffler 	ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
1751a1e1d21SSam Leffler }
1761a1e1d21SSam Leffler 
1771a1e1d21SSam Leffler void
1788a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic)
1791a1e1d21SSam Leffler {
1801a1e1d21SSam Leffler 
1818a1b9b6aSSam Leffler 	if (ic->ic_bss != NULL) {
1828a1b9b6aSSam Leffler 		ieee80211_free_node(ic->ic_bss);
1838a1b9b6aSSam Leffler 		ic->ic_bss = NULL;
1848a1b9b6aSSam Leffler 	}
1858a1b9b6aSSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_scan);
186acc4f7f5SSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_sta);
1878a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap != NULL) {
1888a1b9b6aSSam Leffler 		FREE(ic->ic_aid_bitmap, M_DEVBUF);
1898a1b9b6aSSam Leffler 		ic->ic_aid_bitmap = NULL;
1908a1b9b6aSSam Leffler 	}
1918a1b9b6aSSam Leffler 	if (ic->ic_tim_bitmap != NULL) {
1928a1b9b6aSSam Leffler 		FREE(ic->ic_tim_bitmap, M_DEVBUF);
1938a1b9b6aSSam Leffler 		ic->ic_tim_bitmap = NULL;
1948a1b9b6aSSam Leffler 	}
1958a1b9b6aSSam Leffler }
1968a1b9b6aSSam Leffler 
1978a1b9b6aSSam Leffler /*
1988a1b9b6aSSam Leffler  * Port authorize/unauthorize interfaces for use by an authenticator.
1998a1b9b6aSSam Leffler  */
2008a1b9b6aSSam Leffler 
2018a1b9b6aSSam Leffler void
202e4918ecdSSam Leffler ieee80211_node_authorize(struct ieee80211_node *ni)
2038a1b9b6aSSam Leffler {
204e4918ecdSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
205e4918ecdSSam Leffler 
2068a1b9b6aSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AUTH;
2072045f699SSam Leffler 	ni->ni_inact_reload = ic->ic_inact_run;
2088a1b9b6aSSam Leffler }
2098a1b9b6aSSam Leffler 
2108a1b9b6aSSam Leffler void
211e4918ecdSSam Leffler ieee80211_node_unauthorize(struct ieee80211_node *ni)
2128a1b9b6aSSam Leffler {
2138a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
2148a1b9b6aSSam Leffler }
2158a1b9b6aSSam Leffler 
2168a1b9b6aSSam Leffler /*
2178a1b9b6aSSam Leffler  * Set/change the channel.  The rate set is also updated as
2188a1b9b6aSSam Leffler  * to insure a consistent view by drivers.
2198a1b9b6aSSam Leffler  */
2208a1b9b6aSSam Leffler static __inline void
2218a1b9b6aSSam Leffler ieee80211_set_chan(struct ieee80211com *ic,
2228a1b9b6aSSam Leffler 	struct ieee80211_node *ni, struct ieee80211_channel *chan)
2238a1b9b6aSSam Leffler {
2248a1b9b6aSSam Leffler 	ni->ni_chan = chan;
2258a1b9b6aSSam Leffler 	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
2261a1e1d21SSam Leffler }
2271a1e1d21SSam Leffler 
2281a1e1d21SSam Leffler /*
2291a1e1d21SSam Leffler  * AP scanning support.
2301a1e1d21SSam Leffler  */
2311a1e1d21SSam Leffler 
2328a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
2338a1b9b6aSSam Leffler static void
2348a1b9b6aSSam Leffler dump_chanlist(const u_char chans[])
2358a1b9b6aSSam Leffler {
2368a1b9b6aSSam Leffler 	const char *sep;
2378a1b9b6aSSam Leffler 	int i;
2388a1b9b6aSSam Leffler 
2398a1b9b6aSSam Leffler 	sep = " ";
2408a1b9b6aSSam Leffler 	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
2418a1b9b6aSSam Leffler 		if (isset(chans, i)) {
2428a1b9b6aSSam Leffler 			printf("%s%u", sep, i);
2438a1b9b6aSSam Leffler 			sep = ", ";
2448a1b9b6aSSam Leffler 		}
2458a1b9b6aSSam Leffler }
2468a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2478a1b9b6aSSam Leffler 
2481a1e1d21SSam Leffler /*
2498a1b9b6aSSam Leffler  * Initialize the channel set to scan based on the
2501a1e1d21SSam Leffler  * of available channels and the current PHY mode.
2511a1e1d21SSam Leffler  */
252a11c9a5cSSam Leffler static void
2538a1b9b6aSSam Leffler ieee80211_reset_scan(struct ieee80211com *ic)
2541a1e1d21SSam Leffler {
2551a1e1d21SSam Leffler 
2568a1b9b6aSSam Leffler 	/* XXX ic_des_chan should be handled with ic_chan_active */
2578a1b9b6aSSam Leffler 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
2588a1b9b6aSSam Leffler 		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
2598a1b9b6aSSam Leffler 		setbit(ic->ic_chan_scan,
2608a1b9b6aSSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_des_chan));
2618a1b9b6aSSam Leffler 	} else
2621a1e1d21SSam Leffler 		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
2631a1e1d21SSam Leffler 			sizeof(ic->ic_chan_active));
264a11c9a5cSSam Leffler 	/* NB: hack, setup so next_scan starts with the first channel */
265a11c9a5cSSam Leffler 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
2668a1b9b6aSSam Leffler 		ieee80211_set_chan(ic, ic->ic_bss,
2678a1b9b6aSSam Leffler 			&ic->ic_channels[IEEE80211_CHAN_MAX]);
2688a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
2698a1b9b6aSSam Leffler 	if (ieee80211_msg_scan(ic)) {
2708a1b9b6aSSam Leffler 		printf("%s: scan set:", __func__);
2718a1b9b6aSSam Leffler 		dump_chanlist(ic->ic_chan_scan);
2728a1b9b6aSSam Leffler 		printf(" start chan %u\n",
2738a1b9b6aSSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
2748a1b9b6aSSam Leffler 	}
2758a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2761a1e1d21SSam Leffler }
2771a1e1d21SSam Leffler 
2781a1e1d21SSam Leffler /*
2791a1e1d21SSam Leffler  * Begin an active scan.
2801a1e1d21SSam Leffler  */
2811a1e1d21SSam Leffler void
2828a1b9b6aSSam Leffler ieee80211_begin_scan(struct ieee80211com *ic, int reset)
2831a1e1d21SSam Leffler {
2841a1e1d21SSam Leffler 
2858a1b9b6aSSam Leffler 	ic->ic_scan.nt_scangen++;
286a11c9a5cSSam Leffler 	/*
287a11c9a5cSSam Leffler 	 * In all but hostap mode scanning starts off in
288a11c9a5cSSam Leffler 	 * an active mode before switching to passive.
289a11c9a5cSSam Leffler 	 */
2901be50176SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
291a11c9a5cSSam Leffler 		ic->ic_flags |= IEEE80211_F_ASCAN;
2921be50176SSam Leffler 		ic->ic_stats.is_scan_active++;
2931be50176SSam Leffler 	} else
2941be50176SSam Leffler 		ic->ic_stats.is_scan_passive++;
2953ea67c54SSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
2963ea67c54SSam Leffler 		"begin %s scan in %s mode, scangen %u\n",
2978a1b9b6aSSam Leffler 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
2983ea67c54SSam Leffler 		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
2991a1e1d21SSam Leffler 	/*
3008a1b9b6aSSam Leffler 	 * Clear scan state and flush any previously seen AP's.
3011a1e1d21SSam Leffler 	 */
3028a1b9b6aSSam Leffler 	ieee80211_reset_scan(ic);
3038a1b9b6aSSam Leffler 	if (reset)
3048a1b9b6aSSam Leffler 		ieee80211_free_allnodes(&ic->ic_scan);
3058a1b9b6aSSam Leffler 
3068a1b9b6aSSam Leffler 	ic->ic_flags |= IEEE80211_F_SCAN;
3071a1e1d21SSam Leffler 
308a11c9a5cSSam Leffler 	/* Scan the next channel. */
3098a1b9b6aSSam Leffler 	ieee80211_next_scan(ic);
3101a1e1d21SSam Leffler }
3111a1e1d21SSam Leffler 
3121a1e1d21SSam Leffler /*
3131a1e1d21SSam Leffler  * Switch to the next channel marked for scanning.
3141a1e1d21SSam Leffler  */
3158a1b9b6aSSam Leffler int
3168a1b9b6aSSam Leffler ieee80211_next_scan(struct ieee80211com *ic)
3171a1e1d21SSam Leffler {
3181a1e1d21SSam Leffler 	struct ieee80211_channel *chan;
3191a1e1d21SSam Leffler 
3208a1b9b6aSSam Leffler 	/*
3218a1b9b6aSSam Leffler 	 * Insure any previous mgt frame timeouts don't fire.
3228a1b9b6aSSam Leffler 	 * This assumes the driver does the right thing in
3238a1b9b6aSSam Leffler 	 * flushing anything queued in the driver and below.
3248a1b9b6aSSam Leffler 	 */
3258a1b9b6aSSam Leffler 	ic->ic_mgt_timer = 0;
3268a1b9b6aSSam Leffler 
3271a1e1d21SSam Leffler 	chan = ic->ic_bss->ni_chan;
3288a1b9b6aSSam Leffler 	do {
3291a1e1d21SSam Leffler 		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
3301a1e1d21SSam Leffler 			chan = &ic->ic_channels[0];
3311a1e1d21SSam Leffler 		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
3321a1e1d21SSam Leffler 			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
3338a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
3348a1b9b6aSSam Leffler 			    "%s: chan %d->%d\n", __func__,
3351a1e1d21SSam Leffler 			    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
3368a1b9b6aSSam Leffler 			    ieee80211_chan2ieee(ic, chan));
3378a1b9b6aSSam Leffler 			ieee80211_set_chan(ic, ic->ic_bss, chan);
338a11c9a5cSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
3398a1b9b6aSSam Leffler 			return 1;
3408a1b9b6aSSam Leffler 		}
3418a1b9b6aSSam Leffler 	} while (chan != ic->ic_bss->ni_chan);
3428a1b9b6aSSam Leffler 	ieee80211_end_scan(ic);
3438a1b9b6aSSam Leffler 	return 0;
3441a1e1d21SSam Leffler }
3451a1e1d21SSam Leffler 
346f9cd9174SSam Leffler static __inline void
347f9cd9174SSam Leffler copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
348f9cd9174SSam Leffler {
349f9cd9174SSam Leffler 	/* propagate useful state */
350f9cd9174SSam Leffler 	nbss->ni_authmode = obss->ni_authmode;
351f9cd9174SSam Leffler 	nbss->ni_txpower = obss->ni_txpower;
352f9cd9174SSam Leffler 	nbss->ni_vlan = obss->ni_vlan;
353f9cd9174SSam Leffler 	nbss->ni_rsn = obss->ni_rsn;
354f9cd9174SSam Leffler 	/* XXX statistics? */
355f9cd9174SSam Leffler }
356f9cd9174SSam Leffler 
3571a1e1d21SSam Leffler void
3581a1e1d21SSam Leffler ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
3591a1e1d21SSam Leffler {
360acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt;
3611a1e1d21SSam Leffler 	struct ieee80211_node *ni;
3628a1b9b6aSSam Leffler 
3638a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
3648a1b9b6aSSam Leffler 		"%s: creating ibss\n", __func__);
3658a1b9b6aSSam Leffler 
3668a1b9b6aSSam Leffler 	/*
3678a1b9b6aSSam Leffler 	 * Create the station/neighbor table.  Note that for adhoc
3688a1b9b6aSSam Leffler 	 * mode we make the initial inactivity timer longer since
3698a1b9b6aSSam Leffler 	 * we create nodes only through discovery and they typically
3708a1b9b6aSSam Leffler 	 * are long-lived associations.
3718a1b9b6aSSam Leffler 	 */
372acc4f7f5SSam Leffler 	nt = &ic->ic_sta;
373acc4f7f5SSam Leffler 	IEEE80211_NODE_LOCK(nt);
374acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
375acc4f7f5SSam Leffler 		nt->nt_name = "station";
376acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_init;
377acc4f7f5SSam Leffler 	} else {
378acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
379acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
380acc4f7f5SSam Leffler 	}
381acc4f7f5SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
382acc4f7f5SSam Leffler 
383c1225b52SSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
384acc4f7f5SSam Leffler 	if (ni == NULL) {
385acc4f7f5SSam Leffler 		/* XXX recovery? */
3868a1b9b6aSSam Leffler 		return;
3878a1b9b6aSSam Leffler 	}
3881a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
3891a1e1d21SSam Leffler 	ni->ni_esslen = ic->ic_des_esslen;
3901a1e1d21SSam Leffler 	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
391f9cd9174SSam Leffler 	copy_bss(ni, ic->ic_bss);
392d365f9c7SSam Leffler 	ni->ni_intval = ic->ic_bintval;
3938a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
3941a1e1d21SSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
3951a1e1d21SSam Leffler 	if (ic->ic_phytype == IEEE80211_T_FH) {
3961a1e1d21SSam Leffler 		ni->ni_fhdwell = 200;	/* XXX */
3971a1e1d21SSam Leffler 		ni->ni_fhindex = 1;
3981a1e1d21SSam Leffler 	}
3998a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
4008a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SIBSS;
4018a1b9b6aSSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
40248b0a5beSSam Leffler 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
40348b0a5beSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
40448b0a5beSSam Leffler 		else
4058a1b9b6aSSam Leffler 			ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
4068a1b9b6aSSam Leffler 	}
4078a1b9b6aSSam Leffler 	/*
4088a1b9b6aSSam Leffler 	 * Fix the channel and related attributes.
4098a1b9b6aSSam Leffler 	 */
4108a1b9b6aSSam Leffler 	ieee80211_set_chan(ic, ni, chan);
4118a1b9b6aSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
4128a1b9b6aSSam Leffler 	/*
4138a1b9b6aSSam Leffler 	 * Do mode-specific rate setup.
4148a1b9b6aSSam Leffler 	 */
4158a1b9b6aSSam Leffler 	if (ic->ic_curmode == IEEE80211_MODE_11G) {
4168a1b9b6aSSam Leffler 		/*
4178a1b9b6aSSam Leffler 		 * Use a mixed 11b/11g rate set.
4188a1b9b6aSSam Leffler 		 */
4198a1b9b6aSSam Leffler 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
4208a1b9b6aSSam Leffler 	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
4218a1b9b6aSSam Leffler 		/*
4228a1b9b6aSSam Leffler 		 * Force pure 11b rate set.
4238a1b9b6aSSam Leffler 		 */
4248a1b9b6aSSam Leffler 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
4258a1b9b6aSSam Leffler 	}
4268a1b9b6aSSam Leffler 
427acc4f7f5SSam Leffler 	(void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
4281a1e1d21SSam Leffler }
4291a1e1d21SSam Leffler 
4308a1b9b6aSSam Leffler void
4318a1b9b6aSSam Leffler ieee80211_reset_bss(struct ieee80211com *ic)
432b4c5a90fSSam Leffler {
4338a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *obss;
4348a1b9b6aSSam Leffler 
4358a1b9b6aSSam Leffler 	ieee80211_node_table_reset(&ic->ic_scan);
436acc4f7f5SSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta);
437acc4f7f5SSam Leffler 
4388a1b9b6aSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
4398a1b9b6aSSam Leffler 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
4408a1b9b6aSSam Leffler 	obss = ic->ic_bss;
4418a1b9b6aSSam Leffler 	ic->ic_bss = ieee80211_ref_node(ni);
442f9cd9174SSam Leffler 	if (obss != NULL) {
443f9cd9174SSam Leffler 		copy_bss(ni, obss);
444d365f9c7SSam Leffler 		ni->ni_intval = ic->ic_bintval;
4458a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
4468a1b9b6aSSam Leffler 	}
447f9cd9174SSam Leffler }
4488a1b9b6aSSam Leffler 
449936f15d2SSam Leffler /* XXX tunable */
450936f15d2SSam Leffler #define	STA_FAILS_MAX	2		/* assoc failures before ignored */
451936f15d2SSam Leffler 
4528a1b9b6aSSam Leffler static int
4538a1b9b6aSSam Leffler ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
4548a1b9b6aSSam Leffler {
455b4c5a90fSSam Leffler         u_int8_t rate;
456b4c5a90fSSam Leffler         int fail;
457b4c5a90fSSam Leffler 
458b4c5a90fSSam Leffler 	fail = 0;
459b4c5a90fSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
460b4c5a90fSSam Leffler 		fail |= 0x01;
461b4c5a90fSSam Leffler 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
462b4c5a90fSSam Leffler 	    ni->ni_chan != ic->ic_des_chan)
463b4c5a90fSSam Leffler 		fail |= 0x01;
464b4c5a90fSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
465b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
466b4c5a90fSSam Leffler 			fail |= 0x02;
467b4c5a90fSSam Leffler 	} else {
468b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
469b4c5a90fSSam Leffler 			fail |= 0x02;
470b4c5a90fSSam Leffler 	}
4718a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
472b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
473b4c5a90fSSam Leffler 			fail |= 0x04;
474b4c5a90fSSam Leffler 	} else {
475b4c5a90fSSam Leffler 		/* XXX does this mean privacy is supported or required? */
476b4c5a90fSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
477b4c5a90fSSam Leffler 			fail |= 0x04;
478b4c5a90fSSam Leffler 	}
4797d77cd53SSam Leffler 	rate = ieee80211_fix_rate(ni, IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
480b4c5a90fSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
481b4c5a90fSSam Leffler 		fail |= 0x08;
482b4c5a90fSSam Leffler 	if (ic->ic_des_esslen != 0 &&
483b4c5a90fSSam Leffler 	    (ni->ni_esslen != ic->ic_des_esslen ||
484b4c5a90fSSam Leffler 	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
485b4c5a90fSSam Leffler 		fail |= 0x10;
486b4c5a90fSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
487b4c5a90fSSam Leffler 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
488b4c5a90fSSam Leffler 		fail |= 0x20;
489936f15d2SSam Leffler 	if (ni->ni_fails >= STA_FAILS_MAX)
490936f15d2SSam Leffler 		fail |= 0x40;
491b4c5a90fSSam Leffler #ifdef IEEE80211_DEBUG
4928a1b9b6aSSam Leffler 	if (ieee80211_msg_scan(ic)) {
493936f15d2SSam Leffler 		printf(" %c %s",
494936f15d2SSam Leffler 		    fail & 0x40 ? '=' : fail & 0x80 ? '^' : fail ? '-' : '+',
495b4c5a90fSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
496b4c5a90fSSam Leffler 		printf(" %s%c", ether_sprintf(ni->ni_bssid),
497b4c5a90fSSam Leffler 		    fail & 0x20 ? '!' : ' ');
498b4c5a90fSSam Leffler 		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
499b4c5a90fSSam Leffler 			fail & 0x01 ? '!' : ' ');
500b4c5a90fSSam Leffler 		printf(" %+4d", ni->ni_rssi);
501b4c5a90fSSam Leffler 		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
502b4c5a90fSSam Leffler 		    fail & 0x08 ? '!' : ' ');
503b4c5a90fSSam Leffler 		printf(" %4s%c",
504b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
505b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
506b4c5a90fSSam Leffler 		    "????",
507b4c5a90fSSam Leffler 		    fail & 0x02 ? '!' : ' ');
508b4c5a90fSSam Leffler 		printf(" %3s%c ",
509b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
510b4c5a90fSSam Leffler 		    "wep" : "no",
511b4c5a90fSSam Leffler 		    fail & 0x04 ? '!' : ' ');
512b4c5a90fSSam Leffler 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
513b4c5a90fSSam Leffler 		printf("%s\n", fail & 0x10 ? "!" : "");
514b4c5a90fSSam Leffler 	}
515b4c5a90fSSam Leffler #endif
516b4c5a90fSSam Leffler 	return fail;
517b4c5a90fSSam Leffler }
518b4c5a90fSSam Leffler 
5198a1b9b6aSSam Leffler static __inline u_int8_t
5208a1b9b6aSSam Leffler maxrate(const struct ieee80211_node *ni)
5218a1b9b6aSSam Leffler {
5228a1b9b6aSSam Leffler 	const struct ieee80211_rateset *rs = &ni->ni_rates;
5238a1b9b6aSSam Leffler 	/* NB: assumes rate set is sorted (happens on frame receive) */
5248a1b9b6aSSam Leffler 	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
5258a1b9b6aSSam Leffler }
5268a1b9b6aSSam Leffler 
5278a1b9b6aSSam Leffler /*
5288a1b9b6aSSam Leffler  * Compare the capabilities of two nodes and decide which is
5298a1b9b6aSSam Leffler  * more desirable (return >0 if a is considered better).  Note
5308a1b9b6aSSam Leffler  * that we assume compatibility/usability has already been checked
5318a1b9b6aSSam Leffler  * so we don't need to (e.g. validate whether privacy is supported).
5328a1b9b6aSSam Leffler  * Used to select the best scan candidate for association in a BSS.
5338a1b9b6aSSam Leffler  */
5348a1b9b6aSSam Leffler static int
5358a1b9b6aSSam Leffler ieee80211_node_compare(struct ieee80211com *ic,
5368a1b9b6aSSam Leffler 		       const struct ieee80211_node *a,
5378a1b9b6aSSam Leffler 		       const struct ieee80211_node *b)
5388a1b9b6aSSam Leffler {
5398a1b9b6aSSam Leffler 	u_int8_t maxa, maxb;
5408a1b9b6aSSam Leffler 	u_int8_t rssia, rssib;
541936f15d2SSam Leffler 	int weight;
5428a1b9b6aSSam Leffler 
5438a1b9b6aSSam Leffler 	/* privacy support preferred */
5448a1b9b6aSSam Leffler 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
5458a1b9b6aSSam Leffler 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
5468a1b9b6aSSam Leffler 		return 1;
5478a1b9b6aSSam Leffler 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
5488a1b9b6aSSam Leffler 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
5498a1b9b6aSSam Leffler 		return -1;
5508a1b9b6aSSam Leffler 
551936f15d2SSam Leffler 	/* compare count of previous failures */
552936f15d2SSam Leffler 	weight = b->ni_fails - a->ni_fails;
553936f15d2SSam Leffler 	if (abs(weight) > 1)
554936f15d2SSam Leffler 		return weight;
555936f15d2SSam Leffler 
556ef92bcdcSSam Leffler 	rssia = ic->ic_node_getrssi(a);
557ef92bcdcSSam Leffler 	rssib = ic->ic_node_getrssi(b);
558ef92bcdcSSam Leffler 	if (abs(rssib - rssia) < 5) {
5598a1b9b6aSSam Leffler 		/* best/max rate preferred if signal level close enough XXX */
5608a1b9b6aSSam Leffler 		maxa = maxrate(a);
5618a1b9b6aSSam Leffler 		maxb = maxrate(b);
562ef92bcdcSSam Leffler 		if (maxa != maxb)
5638a1b9b6aSSam Leffler 			return maxa - maxb;
5648a1b9b6aSSam Leffler 		/* XXX use freq for channel preference */
5658a1b9b6aSSam Leffler 		/* for now just prefer 5Ghz band to all other bands */
5668a1b9b6aSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
5678a1b9b6aSSam Leffler 		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
5688a1b9b6aSSam Leffler 			return 1;
5698a1b9b6aSSam Leffler 		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
5708a1b9b6aSSam Leffler 		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
5718a1b9b6aSSam Leffler 			return -1;
572ef92bcdcSSam Leffler 	}
5738a1b9b6aSSam Leffler 	/* all things being equal, use signal level */
5748a1b9b6aSSam Leffler 	return rssia - rssib;
5758a1b9b6aSSam Leffler }
5768a1b9b6aSSam Leffler 
5771a1e1d21SSam Leffler /*
578c75ac469SSam Leffler  * Mark an ongoing scan stopped.
579c75ac469SSam Leffler  */
580c75ac469SSam Leffler void
581c75ac469SSam Leffler ieee80211_cancel_scan(struct ieee80211com *ic)
582c75ac469SSam Leffler {
583c75ac469SSam Leffler 
584c75ac469SSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
585c75ac469SSam Leffler 		__func__,
586c75ac469SSam Leffler 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
587c75ac469SSam Leffler 
588c75ac469SSam Leffler 	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
589c75ac469SSam Leffler }
590c75ac469SSam Leffler 
591c75ac469SSam Leffler /*
5921a1e1d21SSam Leffler  * Complete a scan of potential channels.
5931a1e1d21SSam Leffler  */
5941a1e1d21SSam Leffler void
5958a1b9b6aSSam Leffler ieee80211_end_scan(struct ieee80211com *ic)
5961a1e1d21SSam Leffler {
5973fcfbbfaSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_scan;
5983fcfbbfaSSam Leffler 	struct ieee80211_node *ni, *selbs;
5991a1e1d21SSam Leffler 
600c75ac469SSam Leffler 	ieee80211_cancel_scan(ic);
601c75ac469SSam Leffler 	ieee80211_notify_scan_done(ic);
6028a1b9b6aSSam Leffler 
6031a1e1d21SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
6048a1b9b6aSSam Leffler 		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
6058a1b9b6aSSam Leffler 		int i, bestchan;
6068a1b9b6aSSam Leffler 		u_int8_t rssi;
6078a1b9b6aSSam Leffler 
6081a1e1d21SSam Leffler 		/*
6091a1e1d21SSam Leffler 		 * The passive scan to look for existing AP's completed,
6101a1e1d21SSam Leffler 		 * select a channel to camp on.  Identify the channels
6111a1e1d21SSam Leffler 		 * that already have one or more AP's and try to locate
612acc4f7f5SSam Leffler 		 * an unoccupied one.  If that fails, pick a channel that
6138a1b9b6aSSam Leffler 		 * looks to be quietest.
6141a1e1d21SSam Leffler 		 */
6158a1b9b6aSSam Leffler 		memset(maxrssi, 0, sizeof(maxrssi));
6163fcfbbfaSSam Leffler 		IEEE80211_NODE_LOCK(nt);
6173fcfbbfaSSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
6188a1b9b6aSSam Leffler 			rssi = ic->ic_node_getrssi(ni);
6198a1b9b6aSSam Leffler 			i = ieee80211_chan2ieee(ic, ni->ni_chan);
6208a1b9b6aSSam Leffler 			if (rssi > maxrssi[i])
6218a1b9b6aSSam Leffler 				maxrssi[i] = rssi;
6221a1e1d21SSam Leffler 		}
6233fcfbbfaSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
6248a1b9b6aSSam Leffler 		/* XXX select channel more intelligently */
6258a1b9b6aSSam Leffler 		bestchan = -1;
6261a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
6278a1b9b6aSSam Leffler 			if (isset(ic->ic_chan_active, i)) {
6288a1b9b6aSSam Leffler 				/*
6298a1b9b6aSSam Leffler 				 * If the channel is unoccupied the max rssi
6308a1b9b6aSSam Leffler 				 * should be zero; just take it.  Otherwise
6318a1b9b6aSSam Leffler 				 * track the channel with the lowest rssi and
6328a1b9b6aSSam Leffler 				 * use that when all channels appear occupied.
6338a1b9b6aSSam Leffler 				 */
6348a1b9b6aSSam Leffler 				if (maxrssi[i] == 0) {
6358a1b9b6aSSam Leffler 					bestchan = i;
6361a1e1d21SSam Leffler 					break;
6371a1e1d21SSam Leffler 				}
6386edf09a6SSam Leffler 				if (bestchan == -1 ||
6396edf09a6SSam Leffler 				    maxrssi[i] < maxrssi[bestchan])
6408a1b9b6aSSam Leffler 					bestchan = i;
6418a1b9b6aSSam Leffler 			}
6428a1b9b6aSSam Leffler 		if (bestchan != -1) {
6438a1b9b6aSSam Leffler 			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
6441a1e1d21SSam Leffler 			return;
6451a1e1d21SSam Leffler 		}
6468a1b9b6aSSam Leffler 		/* no suitable channel, should not happen */
6478a1b9b6aSSam Leffler 	}
6488a1b9b6aSSam Leffler 
6498a1b9b6aSSam Leffler 	/*
6508a1b9b6aSSam Leffler 	 * When manually sequencing the state machine; scan just once
6518a1b9b6aSSam Leffler 	 * regardless of whether we have a candidate or not.  The
6528a1b9b6aSSam Leffler 	 * controlling application is expected to setup state and
6538a1b9b6aSSam Leffler 	 * initiate an association.
6548a1b9b6aSSam Leffler 	 */
6558a1b9b6aSSam Leffler 	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
6568a1b9b6aSSam Leffler 		return;
6578a1b9b6aSSam Leffler 	/*
6588a1b9b6aSSam Leffler 	 * Automatic sequencing; look for a candidate and
6598a1b9b6aSSam Leffler 	 * if found join the network.
6608a1b9b6aSSam Leffler 	 */
6613fcfbbfaSSam Leffler 	/* NB: unlocked read should be ok */
6623fcfbbfaSSam Leffler 	if (TAILQ_FIRST(&nt->nt_node) == NULL) {
6638a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
6648a1b9b6aSSam Leffler 			"%s: no scan candidate\n", __func__);
6651a1e1d21SSam Leffler   notfound:
6661a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
6671a1e1d21SSam Leffler 		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
6681a1e1d21SSam Leffler 		    ic->ic_des_esslen != 0) {
6691a1e1d21SSam Leffler 			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
6701a1e1d21SSam Leffler 			return;
6711a1e1d21SSam Leffler 		}
6721a1e1d21SSam Leffler 		/*
673936f15d2SSam Leffler 		 * Decrement the failure counts so entries will be
674936f15d2SSam Leffler 		 * reconsidered the next time around.  We really want
675936f15d2SSam Leffler 		 * to do this only for sta's where we've previously
676936f15d2SSam Leffler 		 had some success.
677936f15d2SSam Leffler 		 */
678936f15d2SSam Leffler 		IEEE80211_NODE_LOCK(nt);
679936f15d2SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
680936f15d2SSam Leffler 			if (ni->ni_fails)
681936f15d2SSam Leffler 				ni->ni_fails--;
682936f15d2SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
683936f15d2SSam Leffler 		/*
6841a1e1d21SSam Leffler 		 * Reset the list of channels to scan and start again.
6851a1e1d21SSam Leffler 		 */
6868a1b9b6aSSam Leffler 		ieee80211_reset_scan(ic);
6878a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SCAN;
6888a1b9b6aSSam Leffler 		ieee80211_next_scan(ic);
6891a1e1d21SSam Leffler 		return;
6901a1e1d21SSam Leffler 	}
6911a1e1d21SSam Leffler 	selbs = NULL;
6928a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
6938a1b9b6aSSam Leffler 	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
6943fcfbbfaSSam Leffler 	IEEE80211_NODE_LOCK(nt);
6953fcfbbfaSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
6968a1b9b6aSSam Leffler 		if (ieee80211_match_bss(ic, ni) == 0) {
6971a1e1d21SSam Leffler 			if (selbs == NULL)
6981a1e1d21SSam Leffler 				selbs = ni;
6993fcfbbfaSSam Leffler 			else if (ieee80211_node_compare(ic, ni, selbs) > 0)
7001a1e1d21SSam Leffler 				selbs = ni;
7011a1e1d21SSam Leffler 		}
7021a1e1d21SSam Leffler 	}
7033fcfbbfaSSam Leffler 	if (selbs != NULL)		/* NB: grab ref while dropping lock */
7043fcfbbfaSSam Leffler 		(void) ieee80211_ref_node(selbs);
7053fcfbbfaSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
7061a1e1d21SSam Leffler 	if (selbs == NULL)
7071a1e1d21SSam Leffler 		goto notfound;
7088a1b9b6aSSam Leffler 	if (!ieee80211_sta_join(ic, selbs)) {
7093fcfbbfaSSam Leffler 		ieee80211_free_node(selbs);
7101a1e1d21SSam Leffler 		goto notfound;
7111a1e1d21SSam Leffler 	}
7128a1b9b6aSSam Leffler }
7138a1b9b6aSSam Leffler 
714750d6d0cSSam Leffler /*
7158a1b9b6aSSam Leffler  * Handle 802.11 ad hoc network merge.  The
7168a1b9b6aSSam Leffler  * convention, set by the Wireless Ethernet Compatibility Alliance
7178a1b9b6aSSam Leffler  * (WECA), is that an 802.11 station will change its BSSID to match
7188a1b9b6aSSam Leffler  * the "oldest" 802.11 ad hoc network, on the same channel, that
7198a1b9b6aSSam Leffler  * has the station's desired SSID.  The "oldest" 802.11 network
7208a1b9b6aSSam Leffler  * sends beacons with the greatest TSF timestamp.
7218a1b9b6aSSam Leffler  *
7228a1b9b6aSSam Leffler  * The caller is assumed to validate TSF's before attempting a merge.
7238a1b9b6aSSam Leffler  *
7248a1b9b6aSSam Leffler  * Return !0 if the BSSID changed, 0 otherwise.
725750d6d0cSSam Leffler  */
7268a1b9b6aSSam Leffler int
727641b4d0bSSam Leffler ieee80211_ibss_merge(struct ieee80211_node *ni)
7288a1b9b6aSSam Leffler {
729641b4d0bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
7308a1b9b6aSSam Leffler 
73196acc1b6SSam Leffler 	if (ni == ic->ic_bss ||
73296acc1b6SSam Leffler 	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
7338a1b9b6aSSam Leffler 		/* unchanged, nothing to do */
7348a1b9b6aSSam Leffler 		return 0;
7358a1b9b6aSSam Leffler 	}
7368a1b9b6aSSam Leffler 	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
7378a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
7388a1b9b6aSSam Leffler 		    "%s: merge failed, capabilities mismatch\n", __func__);
7398a1b9b6aSSam Leffler 		ic->ic_stats.is_ibss_capmismatch++;
7408a1b9b6aSSam Leffler 		return 0;
7418a1b9b6aSSam Leffler 	}
7428a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
7438a1b9b6aSSam Leffler 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
7448a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
7458a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
7468a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
7478a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
7488a1b9b6aSSam Leffler 	);
749acc4f7f5SSam Leffler 	return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
7508a1b9b6aSSam Leffler }
7518a1b9b6aSSam Leffler 
7528a1b9b6aSSam Leffler /*
7538a1b9b6aSSam Leffler  * Join the specified IBSS/BSS network.  The node is assumed to
7548a1b9b6aSSam Leffler  * be passed in with a held reference.
7558a1b9b6aSSam Leffler  */
7568a1b9b6aSSam Leffler int
7578a1b9b6aSSam Leffler ieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
7588a1b9b6aSSam Leffler {
7598a1b9b6aSSam Leffler 	struct ieee80211_node *obss;
7608a1b9b6aSSam Leffler 
7618a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
762acc4f7f5SSam Leffler 		struct ieee80211_node_table *nt;
7638a1b9b6aSSam Leffler 		/*
76498ff6263SSam Leffler 		 * Delete unusable rates; we've already checked
76598ff6263SSam Leffler 		 * that the negotiated rate set is acceptable.
7668a1b9b6aSSam Leffler 		 */
7677d77cd53SSam Leffler 		ieee80211_fix_rate(selbs, IEEE80211_F_DODEL);
7688a1b9b6aSSam Leffler 		/*
769acc4f7f5SSam Leffler 		 * Fillin the neighbor table; it will already
7703d073929SSam Leffler 		 * exist if we are simply switching mastership.
771acc4f7f5SSam Leffler 		 * XXX ic_sta always setup so this is unnecessary?
7728a1b9b6aSSam Leffler 		 */
773acc4f7f5SSam Leffler 		nt = &ic->ic_sta;
774acc4f7f5SSam Leffler 		IEEE80211_NODE_LOCK(nt);
775acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
776acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
777acc4f7f5SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
7783d073929SSam Leffler 	}
7791a1e1d21SSam Leffler 
7808a1b9b6aSSam Leffler 	/*
7818a1b9b6aSSam Leffler 	 * Committed to selbs, setup state.
7828a1b9b6aSSam Leffler 	 */
7838a1b9b6aSSam Leffler 	obss = ic->ic_bss;
784acc4f7f5SSam Leffler 	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
7858a1b9b6aSSam Leffler 	if (obss != NULL)
7868a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
7878a1b9b6aSSam Leffler 	/*
7888a1b9b6aSSam Leffler 	 * Set the erp state (mostly the slot time) to deal with
7898a1b9b6aSSam Leffler 	 * the auto-select case; this should be redundant if the
7908a1b9b6aSSam Leffler 	 * mode is locked.
7918a1b9b6aSSam Leffler 	 */
7928a1b9b6aSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
7938a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
7948a1b9b6aSSam Leffler 	ieee80211_wme_initparams(ic);
795acc4f7f5SSam Leffler 
796acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA)
7978a1b9b6aSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
798acc4f7f5SSam Leffler 	else
799acc4f7f5SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
8008a1b9b6aSSam Leffler 	return 1;
8018a1b9b6aSSam Leffler }
8028a1b9b6aSSam Leffler 
8038a1b9b6aSSam Leffler /*
8048a1b9b6aSSam Leffler  * Leave the specified IBSS/BSS network.  The node is assumed to
8058a1b9b6aSSam Leffler  * be passed in with a held reference.
8068a1b9b6aSSam Leffler  */
8078a1b9b6aSSam Leffler void
8088a1b9b6aSSam Leffler ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
8098a1b9b6aSSam Leffler {
8108a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
8118a1b9b6aSSam Leffler 	ieee80211_notify_node_leave(ic, ni);
8128a1b9b6aSSam Leffler }
8138a1b9b6aSSam Leffler 
8141a1e1d21SSam Leffler static struct ieee80211_node *
8158a1b9b6aSSam Leffler node_alloc(struct ieee80211_node_table *nt)
8161a1e1d21SSam Leffler {
817410ca74bSSam Leffler 	struct ieee80211_node *ni;
8188a1b9b6aSSam Leffler 
819410ca74bSSam Leffler 	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
820410ca74bSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
821410ca74bSSam Leffler 	return ni;
8221a1e1d21SSam Leffler }
8231a1e1d21SSam Leffler 
8248a1b9b6aSSam Leffler /*
8258a1b9b6aSSam Leffler  * Reclaim any resources in a node and reset any critical
8268a1b9b6aSSam Leffler  * state.  Typically nodes are free'd immediately after,
8278a1b9b6aSSam Leffler  * but in some cases the storage may be reused so we need
8288a1b9b6aSSam Leffler  * to insure consistent state (should probably fix that).
8298a1b9b6aSSam Leffler  */
8301a1e1d21SSam Leffler static void
8318a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni)
8321a1e1d21SSam Leffler {
8338a1b9b6aSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
8348a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
8358a1b9b6aSSam Leffler 	int i, qlen;
8368a1b9b6aSSam Leffler 
8378a1b9b6aSSam Leffler 	/* NB: preserve ni_table */
8388a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
8398a1b9b6aSSam Leffler 		ic->ic_ps_sta--;
8408a1b9b6aSSam Leffler 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
8418a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
8428a1b9b6aSSam Leffler 		    "[%s] power save mode off, %u sta's in ps mode\n",
8438a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
8448a1b9b6aSSam Leffler 	}
845ebdda46cSSam Leffler 	/*
846ebdda46cSSam Leffler 	 * Clear AREF flag that marks the authorization refcnt bump
847ebdda46cSSam Leffler 	 * has happened.  This is probably not needed as the node
848ebdda46cSSam Leffler 	 * should always be removed from the table so not found but
849ebdda46cSSam Leffler 	 * do it just in case.
850ebdda46cSSam Leffler 	 */
851ebdda46cSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AREF;
8528a1b9b6aSSam Leffler 
8538a1b9b6aSSam Leffler 	/*
8548a1b9b6aSSam Leffler 	 * Drain power save queue and, if needed, clear TIM.
8558a1b9b6aSSam Leffler 	 */
8568a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
8578a1b9b6aSSam Leffler 	if (qlen != 0 && ic->ic_set_tim != NULL)
858edfa57d0SSam Leffler 		ic->ic_set_tim(ni, 0);
8598a1b9b6aSSam Leffler 
8608a1b9b6aSSam Leffler 	ni->ni_associd = 0;
8618a1b9b6aSSam Leffler 	if (ni->ni_challenge != NULL) {
8628a1b9b6aSSam Leffler 		FREE(ni->ni_challenge, M_DEVBUF);
8638a1b9b6aSSam Leffler 		ni->ni_challenge = NULL;
8648a1b9b6aSSam Leffler 	}
8658a1b9b6aSSam Leffler 	/*
8668a1b9b6aSSam Leffler 	 * Preserve SSID, WPA, and WME ie's so the bss node is
8678a1b9b6aSSam Leffler 	 * reusable during a re-auth/re-assoc state transition.
8688a1b9b6aSSam Leffler 	 * If we remove these data they will not be recreated
8698a1b9b6aSSam Leffler 	 * because they come from a probe-response or beacon frame
8708a1b9b6aSSam Leffler 	 * which cannot be expected prior to the association-response.
8718a1b9b6aSSam Leffler 	 * This should not be an issue when operating in other modes
8728a1b9b6aSSam Leffler 	 * as stations leaving always go through a full state transition
8738a1b9b6aSSam Leffler 	 * which will rebuild this state.
8748a1b9b6aSSam Leffler 	 *
8758a1b9b6aSSam Leffler 	 * XXX does this leave us open to inheriting old state?
8768a1b9b6aSSam Leffler 	 */
8778a1b9b6aSSam Leffler 	for (i = 0; i < N(ni->ni_rxfrag); i++)
8788a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[i] != NULL) {
8798a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[i]);
8808a1b9b6aSSam Leffler 			ni->ni_rxfrag[i] = NULL;
8818a1b9b6aSSam Leffler 		}
882c1225b52SSam Leffler 	/*
883c1225b52SSam Leffler 	 * Must be careful here to remove any key map entry w/o a LOR.
884c1225b52SSam Leffler 	 */
885c1225b52SSam Leffler 	ieee80211_node_delucastkey(ni);
8868a1b9b6aSSam Leffler #undef N
8878a1b9b6aSSam Leffler }
8888a1b9b6aSSam Leffler 
8898a1b9b6aSSam Leffler static void
8908a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni)
8918a1b9b6aSSam Leffler {
8928a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
8938a1b9b6aSSam Leffler 
8948a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
8958a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
8968a1b9b6aSSam Leffler 		FREE(ni->ni_wpa_ie, M_DEVBUF);
8978a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
8988a1b9b6aSSam Leffler 		FREE(ni->ni_wme_ie, M_DEVBUF);
8998a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DESTROY(ni);
900410ca74bSSam Leffler 	FREE(ni, M_80211_NODE);
9011a1e1d21SSam Leffler }
9021a1e1d21SSam Leffler 
903d1e61976SSam Leffler static u_int8_t
9048a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni)
905d1e61976SSam Leffler {
906d1e61976SSam Leffler 	return ni->ni_rssi;
907d1e61976SSam Leffler }
908d1e61976SSam Leffler 
9091a1e1d21SSam Leffler static void
9108a1b9b6aSSam Leffler ieee80211_setup_node(struct ieee80211_node_table *nt,
9118a1b9b6aSSam Leffler 	struct ieee80211_node *ni, const u_int8_t *macaddr)
9121a1e1d21SSam Leffler {
9138a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9141a1e1d21SSam Leffler 	int hash;
9151a1e1d21SSam Leffler 
9168a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
91749a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
9188a1b9b6aSSam Leffler 		ether_sprintf(macaddr), nt->nt_name);
9198a1b9b6aSSam Leffler 
9201a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
9211a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
9228a1b9b6aSSam Leffler 	ieee80211_node_initref(ni);		/* mark referenced */
9238a1b9b6aSSam Leffler 	ni->ni_chan = IEEE80211_CHAN_ANYC;
9248a1b9b6aSSam Leffler 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
9258a1b9b6aSSam Leffler 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
9268a1b9b6aSSam Leffler 	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
9272045f699SSam Leffler 	ni->ni_inact_reload = nt->nt_inact_init;
9282045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
9298a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
9308a1b9b6aSSam Leffler 
9318a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
9328a1b9b6aSSam Leffler 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
9338a1b9b6aSSam Leffler 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
9348a1b9b6aSSam Leffler 	ni->ni_table = nt;
9358a1b9b6aSSam Leffler 	ni->ni_ic = ic;
9368a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
9371a1e1d21SSam Leffler }
9381a1e1d21SSam Leffler 
9391a1e1d21SSam Leffler struct ieee80211_node *
9408a1b9b6aSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
9411a1e1d21SSam Leffler {
9428a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9438a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9448a1b9b6aSSam Leffler 
9458a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
9461a1e1d21SSam Leffler 	if (ni != NULL)
9478a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
948c64bfa0fSSam Leffler 	else
949c64bfa0fSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
9501a1e1d21SSam Leffler 	return ni;
9511a1e1d21SSam Leffler }
9521a1e1d21SSam Leffler 
95397c973adSSam Leffler /*
95497c973adSSam Leffler  * Craft a temporary node suitable for sending a management frame
95597c973adSSam Leffler  * to the specified station.  We craft only as much state as we
95697c973adSSam Leffler  * need to do the work since the node will be immediately reclaimed
95797c973adSSam Leffler  * once the send completes.
95897c973adSSam Leffler  */
95997c973adSSam Leffler struct ieee80211_node *
96097c973adSSam Leffler ieee80211_tmp_node(struct ieee80211com *ic, const u_int8_t *macaddr)
96197c973adSSam Leffler {
96297c973adSSam Leffler 	struct ieee80211_node *ni;
96397c973adSSam Leffler 
96497c973adSSam Leffler 	ni = ic->ic_node_alloc(&ic->ic_sta);
96597c973adSSam Leffler 	if (ni != NULL) {
96697c973adSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
96797c973adSSam Leffler 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
96897c973adSSam Leffler 
96997c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
97097c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
97197c973adSSam Leffler 		ieee80211_node_initref(ni);		/* mark referenced */
97297c973adSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
97397c973adSSam Leffler 		/* NB: required by ieee80211_fix_rate */
97497c973adSSam Leffler 		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
97597c973adSSam Leffler 		ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
97697c973adSSam Leffler 			IEEE80211_KEYIX_NONE);
97797c973adSSam Leffler 		/* XXX optimize away */
97897c973adSSam Leffler 		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
97997c973adSSam Leffler 
98097c973adSSam Leffler 		ni->ni_table = NULL;		/* NB: pedantic */
98197c973adSSam Leffler 		ni->ni_ic = ic;
98297c973adSSam Leffler 	} else {
98397c973adSSam Leffler 		/* XXX msg */
98497c973adSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
98597c973adSSam Leffler 	}
98697c973adSSam Leffler 	return ni;
98797c973adSSam Leffler }
98897c973adSSam Leffler 
9891a1e1d21SSam Leffler struct ieee80211_node *
9908a1b9b6aSSam Leffler ieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
9911a1e1d21SSam Leffler {
9928a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9938a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9948a1b9b6aSSam Leffler 
9958a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
9961a1e1d21SSam Leffler 	if (ni != NULL) {
9978a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
998694dca64SSam Leffler 		/*
999694dca64SSam Leffler 		 * Inherit from ic_bss.
1000694dca64SSam Leffler 		 */
10018a1b9b6aSSam Leffler 		ni->ni_authmode = ic->ic_bss->ni_authmode;
10028a1b9b6aSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
10038a1b9b6aSSam Leffler 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
1004694dca64SSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
10058a1b9b6aSSam Leffler 		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
10068a1b9b6aSSam Leffler 		ni->ni_rsn = ic->ic_bss->ni_rsn;
1007694dca64SSam Leffler 	} else
1008694dca64SSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
10091a1e1d21SSam Leffler 	return ni;
10101a1e1d21SSam Leffler }
10111a1e1d21SSam Leffler 
1012750d6d0cSSam Leffler static struct ieee80211_node *
10138a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10148a1b9b6aSSam Leffler _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
10158a1b9b6aSSam Leffler 	const u_int8_t *macaddr, const char *func, int line)
10168a1b9b6aSSam Leffler #else
10178a1b9b6aSSam Leffler _ieee80211_find_node(struct ieee80211_node_table *nt,
10188a1b9b6aSSam Leffler 	const u_int8_t *macaddr)
10198a1b9b6aSSam Leffler #endif
10201a1e1d21SSam Leffler {
10211a1e1d21SSam Leffler 	struct ieee80211_node *ni;
10221a1e1d21SSam Leffler 	int hash;
10231a1e1d21SSam Leffler 
10248a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
1025750d6d0cSSam Leffler 
10261a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
10278a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
10281a1e1d21SSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
10298a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
10308a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10318a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
103249a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
103349a15236SSam Leffler 			    func, line,
103449a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
10358a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
10368a1b9b6aSSam Leffler #endif
1037750d6d0cSSam Leffler 			return ni;
10381a1e1d21SSam Leffler 		}
10391a1e1d21SSam Leffler 	}
1040750d6d0cSSam Leffler 	return NULL;
1041750d6d0cSSam Leffler }
10428a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10438a1b9b6aSSam Leffler #define	_ieee80211_find_node(nt, mac) \
10448a1b9b6aSSam Leffler 	_ieee80211_find_node_debug(nt, mac, func, line)
10458a1b9b6aSSam Leffler #endif
1046750d6d0cSSam Leffler 
1047750d6d0cSSam Leffler struct ieee80211_node *
10488a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10498a1b9b6aSSam Leffler ieee80211_find_node_debug(struct ieee80211_node_table *nt,
10508a1b9b6aSSam Leffler 	const u_int8_t *macaddr, const char *func, int line)
10518a1b9b6aSSam Leffler #else
10528a1b9b6aSSam Leffler ieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
10538a1b9b6aSSam Leffler #endif
1054750d6d0cSSam Leffler {
1055750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1056750d6d0cSSam Leffler 
10578a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
10588a1b9b6aSSam Leffler 	ni = _ieee80211_find_node(nt, macaddr);
10598a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
10601a1e1d21SSam Leffler 	return ni;
10611a1e1d21SSam Leffler }
10621a1e1d21SSam Leffler 
10631a1e1d21SSam Leffler /*
10648a1b9b6aSSam Leffler  * Fake up a node; this handles node discovery in adhoc mode.
10658a1b9b6aSSam Leffler  * Note that for the driver's benefit we we treat this like
10668a1b9b6aSSam Leffler  * an association so the driver has an opportunity to setup
10678a1b9b6aSSam Leffler  * it's private state.
10688a1b9b6aSSam Leffler  */
10698a1b9b6aSSam Leffler struct ieee80211_node *
10708a1b9b6aSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
10718a1b9b6aSSam Leffler 	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
10728a1b9b6aSSam Leffler {
10738a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
10748a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10758a1b9b6aSSam Leffler 
10768a1b9b6aSSam Leffler 	ni = ieee80211_dup_bss(nt, macaddr);
10778a1b9b6aSSam Leffler 	if (ni != NULL) {
10788a1b9b6aSSam Leffler 		/* XXX no rate negotiation; just dup */
10798a1b9b6aSSam Leffler 		ni->ni_rates = ic->ic_bss->ni_rates;
1080736b3dc3SSam Leffler 		if (ic->ic_newassoc != NULL)
1081e9962332SSam Leffler 			ic->ic_newassoc(ni, 1);
10828a1b9b6aSSam Leffler 		/* XXX not right for 802.1x/WPA */
1083e4918ecdSSam Leffler 		ieee80211_node_authorize(ni);
10848a1b9b6aSSam Leffler 	}
10858a1b9b6aSSam Leffler 	return ni;
10868a1b9b6aSSam Leffler }
10878a1b9b6aSSam Leffler 
1088c1225b52SSam Leffler #define	IS_CTL(wh) \
1089c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1090c1225b52SSam Leffler #define	IS_PSPOLL(wh) \
1091c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
10928a1b9b6aSSam Leffler /*
10938a1b9b6aSSam Leffler  * Locate the node for sender, track state, and then pass the
10948a1b9b6aSSam Leffler  * (referenced) node up to the 802.11 layer for its use.  We
10958a1b9b6aSSam Leffler  * are required to pass some node so we fall back to ic_bss
10968a1b9b6aSSam Leffler  * when this frame is from an unknown sender.  The 802.11 layer
10978a1b9b6aSSam Leffler  * knows this means the sender wasn't in the node table and
10988a1b9b6aSSam Leffler  * acts accordingly.
10998a1b9b6aSSam Leffler  */
11008a1b9b6aSSam Leffler struct ieee80211_node *
11018a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
11028a1b9b6aSSam Leffler ieee80211_find_rxnode_debug(struct ieee80211com *ic,
11038a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh, const char *func, int line)
11048a1b9b6aSSam Leffler #else
11058a1b9b6aSSam Leffler ieee80211_find_rxnode(struct ieee80211com *ic,
11068a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh)
11078a1b9b6aSSam Leffler #endif
11088a1b9b6aSSam Leffler {
11098a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
11108a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
11118a1b9b6aSSam Leffler 
11128a1b9b6aSSam Leffler 	/* XXX may want scanned nodes in the neighbor table for adhoc */
11138a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
11148a1b9b6aSSam Leffler 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
11158a1b9b6aSSam Leffler 	    (ic->ic_flags & IEEE80211_F_SCAN))
11168a1b9b6aSSam Leffler 		nt = &ic->ic_scan;
11178a1b9b6aSSam Leffler 	else
1118acc4f7f5SSam Leffler 		nt = &ic->ic_sta;
11198a1b9b6aSSam Leffler 	/* XXX check ic_bss first in station mode */
11208a1b9b6aSSam Leffler 	/* XXX 4-address frames? */
11218a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
11228a1b9b6aSSam Leffler 	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
11238a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr1);
11248a1b9b6aSSam Leffler 	else
11258a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr2);
1126c1225b52SSam Leffler 	if (ni == NULL)
1127c1225b52SSam Leffler 		ni = ieee80211_ref_node(ic->ic_bss);
11288a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
11298a1b9b6aSSam Leffler 
1130c1225b52SSam Leffler 	return ni;
1131c1225b52SSam Leffler }
1132c1225b52SSam Leffler 
1133c1225b52SSam Leffler /*
1134c1225b52SSam Leffler  * Like ieee80211_find_rxnode but use the supplied h/w
1135c1225b52SSam Leffler  * key index as a hint to locate the node in the key
1136c1225b52SSam Leffler  * mapping table.  If an entry is present at the key
1137c1225b52SSam Leffler  * index we return it; otherwise do a normal lookup and
1138c1225b52SSam Leffler  * update the mapping table if the station has a unicast
1139c1225b52SSam Leffler  * key assigned to it.
1140c1225b52SSam Leffler  */
1141c1225b52SSam Leffler struct ieee80211_node *
1142c1225b52SSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1143c1225b52SSam Leffler ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1144c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1145c1225b52SSam Leffler 	const char *func, int line)
1146c1225b52SSam Leffler #else
1147c1225b52SSam Leffler ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1148c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1149c1225b52SSam Leffler #endif
1150c1225b52SSam Leffler {
1151c1225b52SSam Leffler 	struct ieee80211_node_table *nt;
1152c1225b52SSam Leffler 	struct ieee80211_node *ni;
1153c1225b52SSam Leffler 
1154c1225b52SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
1155c1225b52SSam Leffler 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1156c1225b52SSam Leffler 	    (ic->ic_flags & IEEE80211_F_SCAN))
1157c1225b52SSam Leffler 		nt = &ic->ic_scan;
1158c1225b52SSam Leffler 	else
1159c1225b52SSam Leffler 		nt = &ic->ic_sta;
1160c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
1161c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1162c1225b52SSam Leffler 		ni = nt->nt_keyixmap[keyix];
1163c1225b52SSam Leffler 	else
1164c1225b52SSam Leffler 		ni = NULL;
1165c1225b52SSam Leffler 	if (ni == NULL) {
1166c1225b52SSam Leffler 		if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1167c1225b52SSam Leffler 			ni = _ieee80211_find_node(nt, wh->i_addr1);
1168c1225b52SSam Leffler 		else
1169c1225b52SSam Leffler 			ni = _ieee80211_find_node(nt, wh->i_addr2);
1170c1225b52SSam Leffler 		if (ni == NULL)
1171c1225b52SSam Leffler 			ni = ieee80211_ref_node(ic->ic_bss);
1172c1225b52SSam Leffler 		if (nt->nt_keyixmap != NULL) {
1173c1225b52SSam Leffler 			/*
1174c1225b52SSam Leffler 			 * If the station has a unicast key cache slot
1175c1225b52SSam Leffler 			 * assigned update the key->node mapping table.
1176c1225b52SSam Leffler 			 */
1177c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1178c1225b52SSam Leffler 			/* XXX can keyixmap[keyix] != NULL? */
1179c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1180c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == NULL) {
1181c1225b52SSam Leffler 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1182c1225b52SSam Leffler 				    "%s: add key map entry %p<%s> refcnt %d\n",
1183c1225b52SSam Leffler 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1184c1225b52SSam Leffler 				    ieee80211_node_refcnt(ni)+1);
1185c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1186c1225b52SSam Leffler 			}
1187c1225b52SSam Leffler 		}
1188c1225b52SSam Leffler 	} else {
1189c1225b52SSam Leffler 		ieee80211_ref_node(ni);
1190c1225b52SSam Leffler 	}
1191c1225b52SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1192c1225b52SSam Leffler 
1193c1225b52SSam Leffler 	return ni;
1194c1225b52SSam Leffler }
11958a1b9b6aSSam Leffler #undef IS_PSPOLL
11968a1b9b6aSSam Leffler #undef IS_CTL
11978a1b9b6aSSam Leffler 
11988a1b9b6aSSam Leffler /*
1199750d6d0cSSam Leffler  * Return a reference to the appropriate node for sending
1200750d6d0cSSam Leffler  * a data frame.  This handles node discovery in adhoc networks.
1201750d6d0cSSam Leffler  */
1202750d6d0cSSam Leffler struct ieee80211_node *
12038a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12048a1b9b6aSSam Leffler ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
12058a1b9b6aSSam Leffler 	const char *func, int line)
12068a1b9b6aSSam Leffler #else
12078a1b9b6aSSam Leffler ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
12088a1b9b6aSSam Leffler #endif
1209750d6d0cSSam Leffler {
1210acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1211750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1212750d6d0cSSam Leffler 
1213750d6d0cSSam Leffler 	/*
1214750d6d0cSSam Leffler 	 * The destination address should be in the node table
1215c1225b52SSam Leffler 	 * unless this is a multicast/broadcast frame.  We can
1216c1225b52SSam Leffler 	 * also optimize station mode operation, all frames go
1217c1225b52SSam Leffler 	 * to the bss node.
1218750d6d0cSSam Leffler 	 */
1219750d6d0cSSam Leffler 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
12208a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1221c1225b52SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1222c1225b52SSam Leffler 		ni = ieee80211_ref_node(ic->ic_bss);
1223c1225b52SSam Leffler 	else
12248a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, macaddr);
12258a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
12268a1b9b6aSSam Leffler 
12278a1b9b6aSSam Leffler 	if (ni == NULL) {
12288a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
122990d0d036SSam Leffler 		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
123090d0d036SSam Leffler 			/*
123190d0d036SSam Leffler 			 * In adhoc mode cons up a node for the destination.
123290d0d036SSam Leffler 			 * Note that we need an additional reference for the
123390d0d036SSam Leffler 			 * caller to be consistent with _ieee80211_find_node.
123490d0d036SSam Leffler 			 */
12358a1b9b6aSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
123690d0d036SSam Leffler 			if (ni != NULL)
123790d0d036SSam Leffler 				(void) ieee80211_ref_node(ni);
123890d0d036SSam Leffler 		} else {
12398a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
12408a1b9b6aSSam Leffler 				"[%s] no node, discard frame (%s)\n",
12418a1b9b6aSSam Leffler 				ether_sprintf(macaddr), __func__);
12428a1b9b6aSSam Leffler 			ic->ic_stats.is_tx_nonode++;
1243750d6d0cSSam Leffler 		}
1244750d6d0cSSam Leffler 	}
1245750d6d0cSSam Leffler 	return ni;
1246750d6d0cSSam Leffler }
1247750d6d0cSSam Leffler 
1248750d6d0cSSam Leffler /*
12491a1e1d21SSam Leffler  * Like find but search based on the channel too.
12501a1e1d21SSam Leffler  */
12511a1e1d21SSam Leffler struct ieee80211_node *
12528a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12538a1b9b6aSSam Leffler ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
12548a1b9b6aSSam Leffler 	const u_int8_t *macaddr, struct ieee80211_channel *chan,
12558a1b9b6aSSam Leffler 	const char *func, int line)
12568a1b9b6aSSam Leffler #else
12578a1b9b6aSSam Leffler ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
12588a1b9b6aSSam Leffler 	const u_int8_t *macaddr, struct ieee80211_channel *chan)
12598a1b9b6aSSam Leffler #endif
12601a1e1d21SSam Leffler {
12611a1e1d21SSam Leffler 	struct ieee80211_node *ni;
12621a1e1d21SSam Leffler 	int hash;
12631a1e1d21SSam Leffler 
12641a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
12658a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
12668a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1267750d6d0cSSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1268750d6d0cSSam Leffler 		    ni->ni_chan == chan) {
12698a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);		/* mark referenced */
12708a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
12718a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
127249a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
127349a15236SSam Leffler 			    func, line,
12748a1b9b6aSSam Leffler #else
127549a15236SSam Leffler 			    "%s %p<%s> refcnt %d\n", __func__,
12768a1b9b6aSSam Leffler #endif
127749a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
12788a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
12791a1e1d21SSam Leffler 			break;
12801a1e1d21SSam Leffler 		}
12811a1e1d21SSam Leffler 	}
12828a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
12831a1e1d21SSam Leffler 	return ni;
12841a1e1d21SSam Leffler }
12851a1e1d21SSam Leffler 
12868a1b9b6aSSam Leffler /*
12878a1b9b6aSSam Leffler  * Like find but search based on the ssid too.
12888a1b9b6aSSam Leffler  */
12898a1b9b6aSSam Leffler struct ieee80211_node *
12908a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12918a1b9b6aSSam Leffler ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
12928a1b9b6aSSam Leffler 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
12938a1b9b6aSSam Leffler 	const char *func, int line)
12948a1b9b6aSSam Leffler #else
12958a1b9b6aSSam Leffler ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
12968a1b9b6aSSam Leffler 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
12978a1b9b6aSSam Leffler #endif
12981a1e1d21SSam Leffler {
1299f02a0bd2SSam Leffler #define	MATCH_SSID(ni, ssid, ssidlen) \
1300f02a0bd2SSam Leffler 	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1301f02a0bd2SSam Leffler 	static const u_int8_t zeromac[IEEE80211_ADDR_LEN];
13028a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
13031a1e1d21SSam Leffler 	struct ieee80211_node *ni;
13048a1b9b6aSSam Leffler 	int hash;
13051a1e1d21SSam Leffler 
13068a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1307f02a0bd2SSam Leffler 	/*
1308f02a0bd2SSam Leffler 	 * A mac address that is all zero means match only the ssid;
1309f02a0bd2SSam Leffler 	 * otherwise we must match both.
1310f02a0bd2SSam Leffler 	 */
1311f02a0bd2SSam Leffler 	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1312f02a0bd2SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1313f02a0bd2SSam Leffler 			if (MATCH_SSID(ni, ssid, ssidlen))
1314f02a0bd2SSam Leffler 				break;
1315f02a0bd2SSam Leffler 		}
1316f02a0bd2SSam Leffler 	} else {
1317f02a0bd2SSam Leffler 		hash = IEEE80211_NODE_HASH(macaddr);
13188a1b9b6aSSam Leffler 		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
13198a1b9b6aSSam Leffler 			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1320f02a0bd2SSam Leffler 			    MATCH_SSID(ni, ssid, ssidlen))
1321f02a0bd2SSam Leffler 				break;
1322f02a0bd2SSam Leffler 		}
1323f02a0bd2SSam Leffler 	}
1324f02a0bd2SSam Leffler 	if (ni != NULL) {
13258a1b9b6aSSam Leffler 		ieee80211_ref_node(ni);	/* mark referenced */
13268a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
13278a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
132849a15236SSam Leffler 		    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
132949a15236SSam Leffler 		    func, line,
13308a1b9b6aSSam Leffler #else
133149a15236SSam Leffler 		    "%s %p<%s> refcnt %d\n", __func__,
13328a1b9b6aSSam Leffler #endif
133349a15236SSam Leffler 		     ni, ether_sprintf(ni->ni_macaddr),
13348a1b9b6aSSam Leffler 		     ieee80211_node_refcnt(ni));
13358a1b9b6aSSam Leffler 	}
13368a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
13378a1b9b6aSSam Leffler 	return ni;
1338f02a0bd2SSam Leffler #undef MATCH_SSID
13398a1b9b6aSSam Leffler }
13408a1b9b6aSSam Leffler 
13418a1b9b6aSSam Leffler static void
13428a1b9b6aSSam Leffler _ieee80211_free_node(struct ieee80211_node *ni)
13438a1b9b6aSSam Leffler {
13448a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
13458a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
13468a1b9b6aSSam Leffler 
13478a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
134849a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
134949a15236SSam Leffler 		ether_sprintf(ni->ni_macaddr),
13508a1b9b6aSSam Leffler 		nt != NULL ? nt->nt_name : "<gone>");
13518a1b9b6aSSam Leffler 
13528a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
13538a1b9b6aSSam Leffler 	if (nt != NULL) {
13548a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
13558a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
13568a1b9b6aSSam Leffler 	}
13578a1b9b6aSSam Leffler 	ic->ic_node_free(ni);
13588a1b9b6aSSam Leffler }
13598a1b9b6aSSam Leffler 
13608a1b9b6aSSam Leffler void
13618a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
13628a1b9b6aSSam Leffler ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
13638a1b9b6aSSam Leffler #else
13648a1b9b6aSSam Leffler ieee80211_free_node(struct ieee80211_node *ni)
13658a1b9b6aSSam Leffler #endif
13668a1b9b6aSSam Leffler {
13678a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
13688a1b9b6aSSam Leffler 
13698a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
137029d368a7SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
137149a15236SSam Leffler 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
13728a1b9b6aSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
13738a1b9b6aSSam Leffler #endif
1374c1225b52SSam Leffler 	if (nt != NULL) {
1375c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
13768a1b9b6aSSam Leffler 		if (ieee80211_node_dectestref(ni)) {
13778a1b9b6aSSam Leffler 			/*
1378c1225b52SSam Leffler 			 * Last reference, reclaim state.
13798a1b9b6aSSam Leffler 			 */
13808a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
1381c1225b52SSam Leffler 		} else if (ieee80211_node_refcnt(ni) == 1 &&
1382c1225b52SSam Leffler 		    nt->nt_keyixmap != NULL) {
1383c1225b52SSam Leffler 			ieee80211_keyix keyix;
1384c1225b52SSam Leffler 			/*
1385c1225b52SSam Leffler 			 * Check for a last reference in the key mapping table.
1386c1225b52SSam Leffler 			 */
1387c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1388c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1389c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == ni) {
1390c1225b52SSam Leffler 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1391c1225b52SSam Leffler 				    "%s: %p<%s> clear key map entry", __func__,
1392c1225b52SSam Leffler 				    ni, ether_sprintf(ni->ni_macaddr));
1393c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = NULL;
1394c1225b52SSam Leffler 				ieee80211_node_decref(ni); /* XXX needed? */
13958a1b9b6aSSam Leffler 				_ieee80211_free_node(ni);
13968a1b9b6aSSam Leffler 			}
13971a1e1d21SSam Leffler 		}
1398c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1399c1225b52SSam Leffler 	} else {
1400c1225b52SSam Leffler 		if (ieee80211_node_dectestref(ni))
1401c1225b52SSam Leffler 			_ieee80211_free_node(ni);
1402c1225b52SSam Leffler 	}
1403c1225b52SSam Leffler }
1404c1225b52SSam Leffler 
1405c1225b52SSam Leffler /*
1406c1225b52SSam Leffler  * Reclaim a unicast key and clear any key cache state.
1407c1225b52SSam Leffler  */
1408c1225b52SSam Leffler int
1409c1225b52SSam Leffler ieee80211_node_delucastkey(struct ieee80211_node *ni)
1410c1225b52SSam Leffler {
1411c1225b52SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1412c1225b52SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1413c1225b52SSam Leffler 	struct ieee80211_node *nikey;
1414c1225b52SSam Leffler 	ieee80211_keyix keyix;
1415c1225b52SSam Leffler 	int isowned, status;
1416c1225b52SSam Leffler 
1417c1225b52SSam Leffler 	/*
1418c1225b52SSam Leffler 	 * NB: We must beware of LOR here; deleting the key
1419c1225b52SSam Leffler 	 * can cause the crypto layer to block traffic updates
1420c1225b52SSam Leffler 	 * which can generate a LOR against the node table lock;
1421c1225b52SSam Leffler 	 * grab it here and stash the key index for our use below.
1422c1225b52SSam Leffler 	 *
1423c1225b52SSam Leffler 	 * Must also beware of recursion on the node table lock.
1424c1225b52SSam Leffler 	 * When called from node_cleanup we may already have
1425c1225b52SSam Leffler 	 * the node table lock held.  Unfortunately there's no
1426c1225b52SSam Leffler 	 * way to separate out this path so we must do this
1427c1225b52SSam Leffler 	 * conditionally.
1428c1225b52SSam Leffler 	 */
1429c1225b52SSam Leffler 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1430c1225b52SSam Leffler 	if (!isowned)
1431c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
1432c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1433c1225b52SSam Leffler 	status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1434c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1435c1225b52SSam Leffler 		nikey = nt->nt_keyixmap[keyix];
1436c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;;
1437c1225b52SSam Leffler 	} else
1438c1225b52SSam Leffler 		nikey = NULL;
1439c1225b52SSam Leffler 	if (!isowned)
1440c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(&ic->ic_sta);
1441c1225b52SSam Leffler 
1442c1225b52SSam Leffler 	if (nikey != NULL) {
1443c1225b52SSam Leffler 		KASSERT(nikey == ni,
1444c1225b52SSam Leffler 			("key map out of sync, ni %p nikey %p", ni, nikey));
1445c1225b52SSam Leffler 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1446c1225b52SSam Leffler 			"%s: delete key map entry %p<%s> refcnt %d\n",
1447c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr),
1448c1225b52SSam Leffler 			ieee80211_node_refcnt(ni)-1);
1449c1225b52SSam Leffler 		ieee80211_free_node(ni);
1450c1225b52SSam Leffler 	}
1451c1225b52SSam Leffler 	return status;
1452c1225b52SSam Leffler }
14531a1e1d21SSam Leffler 
1454303ebc3cSSam Leffler /*
14558a1b9b6aSSam Leffler  * Reclaim a node.  If this is the last reference count then
14568a1b9b6aSSam Leffler  * do the normal free work.  Otherwise remove it from the node
14578a1b9b6aSSam Leffler  * table and mark it gone by clearing the back-reference.
14588a1b9b6aSSam Leffler  */
14598a1b9b6aSSam Leffler static void
14608a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
14618a1b9b6aSSam Leffler {
1462c1225b52SSam Leffler 	ieee80211_keyix keyix;
1463c1225b52SSam Leffler 
1464c1225b52SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
14658a1b9b6aSSam Leffler 
146649a15236SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
146749a15236SSam Leffler 		"%s: remove %p<%s> from %s table, refcnt %d\n",
146849a15236SSam Leffler 		__func__, ni, ether_sprintf(ni->ni_macaddr),
146949a15236SSam Leffler 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1470c1225b52SSam Leffler 	/*
1471c1225b52SSam Leffler 	 * Clear any entry in the unicast key mapping table.
1472c1225b52SSam Leffler 	 * We need to do it here so rx lookups don't find it
1473c1225b52SSam Leffler 	 * in the mapping table even if it's not in the hash
1474c1225b52SSam Leffler 	 * table.  We cannot depend on the mapping table entry
1475c1225b52SSam Leffler 	 * being cleared because the node may not be free'd.
1476c1225b52SSam Leffler 	 */
1477c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1478c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1479c1225b52SSam Leffler 	    nt->nt_keyixmap[keyix] == ni) {
1480c1225b52SSam Leffler 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1481c1225b52SSam Leffler 			"%s: %p<%s> clear key map entry\n",
1482c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr));
1483c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;
1484c1225b52SSam Leffler 		ieee80211_node_decref(ni);	/* NB: don't need free */
1485c1225b52SSam Leffler 	}
14868a1b9b6aSSam Leffler 	if (!ieee80211_node_dectestref(ni)) {
14878a1b9b6aSSam Leffler 		/*
14888a1b9b6aSSam Leffler 		 * Other references are present, just remove the
14898a1b9b6aSSam Leffler 		 * node from the table so it cannot be found.  When
14908a1b9b6aSSam Leffler 		 * the references are dropped storage will be
1491acc4f7f5SSam Leffler 		 * reclaimed.
14928a1b9b6aSSam Leffler 		 */
14938a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
14948a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
14958a1b9b6aSSam Leffler 		ni->ni_table = NULL;		/* clear reference */
14968a1b9b6aSSam Leffler 	} else
14978a1b9b6aSSam Leffler 		_ieee80211_free_node(ni);
14988a1b9b6aSSam Leffler }
14998a1b9b6aSSam Leffler 
15008a1b9b6aSSam Leffler static void
15018a1b9b6aSSam Leffler ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
15028a1b9b6aSSam Leffler {
15038a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
15048a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
15058a1b9b6aSSam Leffler 
15068a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
15078a1b9b6aSSam Leffler 		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
15088a1b9b6aSSam Leffler 
15098a1b9b6aSSam Leffler 	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
15108a1b9b6aSSam Leffler 		if (ni->ni_associd != 0) {
15118a1b9b6aSSam Leffler 			if (ic->ic_auth->ia_node_leave != NULL)
15128a1b9b6aSSam Leffler 				ic->ic_auth->ia_node_leave(ic, ni);
15138a1b9b6aSSam Leffler 			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
15148a1b9b6aSSam Leffler 		}
15158a1b9b6aSSam Leffler 		node_reclaim(nt, ni);
15168a1b9b6aSSam Leffler 	}
15178a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
15188a1b9b6aSSam Leffler }
15198a1b9b6aSSam Leffler 
15208a1b9b6aSSam Leffler static void
15218a1b9b6aSSam Leffler ieee80211_free_allnodes(struct ieee80211_node_table *nt)
15228a1b9b6aSSam Leffler {
15238a1b9b6aSSam Leffler 
15248a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
15258a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
15268a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15278a1b9b6aSSam Leffler }
15288a1b9b6aSSam Leffler 
15298a1b9b6aSSam Leffler /*
15308a1b9b6aSSam Leffler  * Timeout entries in the scan cache.
15318a1b9b6aSSam Leffler  */
15328a1b9b6aSSam Leffler static void
15338a1b9b6aSSam Leffler ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
15348a1b9b6aSSam Leffler {
15358a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
15368a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *tni;
15378a1b9b6aSSam Leffler 
15388a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
15398a1b9b6aSSam Leffler 	ni = ic->ic_bss;
15408a1b9b6aSSam Leffler 	/* XXX belongs elsewhere */
15418a1b9b6aSSam Leffler 	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
15428a1b9b6aSSam Leffler 		m_freem(ni->ni_rxfrag[0]);
15438a1b9b6aSSam Leffler 		ni->ni_rxfrag[0] = NULL;
15448a1b9b6aSSam Leffler 	}
15458a1b9b6aSSam Leffler 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
15468a1b9b6aSSam Leffler 		if (ni->ni_inact && --ni->ni_inact == 0) {
15478a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
15488a1b9b6aSSam Leffler 			    "[%s] scan candidate purged from cache "
15498a1b9b6aSSam Leffler 			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
155049a15236SSam Leffler 			    ieee80211_node_refcnt(ni));
15518a1b9b6aSSam Leffler 			node_reclaim(nt, ni);
15528a1b9b6aSSam Leffler 		}
15538a1b9b6aSSam Leffler 	}
15548a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15558a1b9b6aSSam Leffler 
15568a1b9b6aSSam Leffler 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
15578a1b9b6aSSam Leffler }
15588a1b9b6aSSam Leffler 
15598a1b9b6aSSam Leffler /*
15608a1b9b6aSSam Leffler  * Timeout inactive stations and do related housekeeping.
15618a1b9b6aSSam Leffler  * Note that we cannot hold the node lock while sending a
15628a1b9b6aSSam Leffler  * frame as this would lead to a LOR.  Instead we use a
15638a1b9b6aSSam Leffler  * generation number to mark nodes that we've scanned and
15648a1b9b6aSSam Leffler  * drop the lock and restart a scan if we have to time out
15658a1b9b6aSSam Leffler  * a node.  Since we are single-threaded by virtue of
1566303ebc3cSSam Leffler  * controlling the inactivity timer we can be sure this will
1567303ebc3cSSam Leffler  * process each node only once.
1568303ebc3cSSam Leffler  */
15698a1b9b6aSSam Leffler static void
15708a1b9b6aSSam Leffler ieee80211_timeout_stations(struct ieee80211_node_table *nt)
15711a1e1d21SSam Leffler {
15728a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
1573303ebc3cSSam Leffler 	struct ieee80211_node *ni;
15748a1b9b6aSSam Leffler 	u_int gen;
1575f66d97f6SSam Leffler 	int isadhoc;
15761a1e1d21SSam Leffler 
1577f66d97f6SSam Leffler 	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1578f66d97f6SSam Leffler 		   ic->ic_opmode == IEEE80211_M_AHDEMO);
15798a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
15808a1b9b6aSSam Leffler 	gen = nt->nt_scangen++;
15818a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
158249a15236SSam Leffler 		"%s: %s scangen %u\n", __func__, nt->nt_name, gen);
1583303ebc3cSSam Leffler restart:
15848a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
15858a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1586303ebc3cSSam Leffler 		if (ni->ni_scangen == gen)	/* previously handled */
1587303ebc3cSSam Leffler 			continue;
1588303ebc3cSSam Leffler 		ni->ni_scangen = gen;
15890a915fadSSam Leffler 		/*
1590ebdda46cSSam Leffler 		 * Ignore entries for which have yet to receive an
1591ebdda46cSSam Leffler 		 * authentication frame.  These are transient and
1592ebdda46cSSam Leffler 		 * will be reclaimed when the last reference to them
1593ebdda46cSSam Leffler 		 * goes away (when frame xmits complete).
1594ebdda46cSSam Leffler 		 */
159544b666cdSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
159644b666cdSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1597ebdda46cSSam Leffler 			continue;
1598ebdda46cSSam Leffler 		/*
15998a1b9b6aSSam Leffler 		 * Free fragment if not needed anymore
16008a1b9b6aSSam Leffler 		 * (last fragment older than 1s).
16018a1b9b6aSSam Leffler 		 * XXX doesn't belong here
16020a915fadSSam Leffler 		 */
16038a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[0] != NULL &&
16048a1b9b6aSSam Leffler 		    ticks > ni->ni_rxfragstamp + hz) {
16058a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
16068a1b9b6aSSam Leffler 			ni->ni_rxfrag[0] = NULL;
16078a1b9b6aSSam Leffler 		}
1608ce647032SSam Leffler 		/*
1609ce647032SSam Leffler 		 * Special case ourself; we may be idle for extended periods
1610ce647032SSam Leffler 		 * of time and regardless reclaiming our state is wrong.
1611ce647032SSam Leffler 		 */
1612ce647032SSam Leffler 		if (ni == ic->ic_bss)
1613ce647032SSam Leffler 			continue;
16148a1b9b6aSSam Leffler 		ni->ni_inact--;
1615f66d97f6SSam Leffler 		if (ni->ni_associd != 0 || isadhoc) {
16168a1b9b6aSSam Leffler 			/*
16178a1b9b6aSSam Leffler 			 * Age frames on the power save queue. The
16188a1b9b6aSSam Leffler 			 * aging interval is 4 times the listen
16198a1b9b6aSSam Leffler 			 * interval specified by the station.  This
16208a1b9b6aSSam Leffler 			 * number is factored into the age calculations
16218a1b9b6aSSam Leffler 			 * when the frame is placed on the queue.  We
16228a1b9b6aSSam Leffler 			 * store ages as time differences we can check
16238a1b9b6aSSam Leffler 			 * and/or adjust only the head of the list.
16248a1b9b6aSSam Leffler 			 */
16258a1b9b6aSSam Leffler 			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
16268a1b9b6aSSam Leffler 				struct mbuf *m;
16278a1b9b6aSSam Leffler 				int discard = 0;
16288a1b9b6aSSam Leffler 
16298a1b9b6aSSam Leffler 				IEEE80211_NODE_SAVEQ_LOCK(ni);
16308a1b9b6aSSam Leffler 				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
16318a1b9b6aSSam Leffler 				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
16328a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
16338a1b9b6aSSam Leffler 					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
16348a1b9b6aSSam Leffler 					m_freem(m);
16358a1b9b6aSSam Leffler 					discard++;
16368a1b9b6aSSam Leffler 				}
16378a1b9b6aSSam Leffler 				if (m != NULL)
16388a1b9b6aSSam Leffler 					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
16398a1b9b6aSSam Leffler 				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
16408a1b9b6aSSam Leffler 
16418a1b9b6aSSam Leffler 				if (discard != 0) {
16428a1b9b6aSSam Leffler 					IEEE80211_DPRINTF(ic,
16438a1b9b6aSSam Leffler 					    IEEE80211_MSG_POWER,
16448a1b9b6aSSam Leffler 					    "[%s] discard %u frames for age\n",
16458a1b9b6aSSam Leffler 					    ether_sprintf(ni->ni_macaddr),
16468a1b9b6aSSam Leffler 					    discard);
16478a1b9b6aSSam Leffler 					IEEE80211_NODE_STAT_ADD(ni,
16488a1b9b6aSSam Leffler 						ps_discard, discard);
16498a1b9b6aSSam Leffler 					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1650edfa57d0SSam Leffler 						ic->ic_set_tim(ni, 0);
16518a1b9b6aSSam Leffler 				}
16528a1b9b6aSSam Leffler 			}
16538a1b9b6aSSam Leffler 			/*
16548a1b9b6aSSam Leffler 			 * Probe the station before time it out.  We
16558a1b9b6aSSam Leffler 			 * send a null data frame which may not be
16568a1b9b6aSSam Leffler 			 * universally supported by drivers (need it
16578a1b9b6aSSam Leffler 			 * for ps-poll support so it should be...).
16588a1b9b6aSSam Leffler 			 */
16592045f699SSam Leffler 			if (0 < ni->ni_inact &&
16602045f699SSam Leffler 			    ni->ni_inact <= ic->ic_inact_probe) {
1661f66d97f6SSam Leffler 				IEEE80211_NOTE(ic,
1662f66d97f6SSam Leffler 				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1663f66d97f6SSam Leffler 				    ni, "%s",
1664f66d97f6SSam Leffler 				    "probe station due to inactivity");
166519ad2dd7SSam Leffler 				/*
166619ad2dd7SSam Leffler 				 * Grab a reference before unlocking the table
166719ad2dd7SSam Leffler 				 * so the node cannot be reclaimed before we
166819ad2dd7SSam Leffler 				 * send the frame. ieee80211_send_nulldata
166919ad2dd7SSam Leffler 				 * understands we've done this and reclaims the
167019ad2dd7SSam Leffler 				 * ref for us as needed.
167119ad2dd7SSam Leffler 				 */
167219ad2dd7SSam Leffler 				ieee80211_ref_node(ni);
16738a1b9b6aSSam Leffler 				IEEE80211_NODE_UNLOCK(nt);
1674f62121ceSSam Leffler 				ieee80211_send_nulldata(ni);
16758a1b9b6aSSam Leffler 				/* XXX stat? */
16768a1b9b6aSSam Leffler 				goto restart;
16778a1b9b6aSSam Leffler 			}
16788a1b9b6aSSam Leffler 		}
16798a1b9b6aSSam Leffler 		if (ni->ni_inact <= 0) {
1680f66d97f6SSam Leffler 			IEEE80211_NOTE(ic,
1681f66d97f6SSam Leffler 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1682f66d97f6SSam Leffler 			    "station timed out due to inactivity "
1683f66d97f6SSam Leffler 			    "(refcnt %u)", ieee80211_node_refcnt(ni));
16848a1b9b6aSSam Leffler 			/*
16858a1b9b6aSSam Leffler 			 * Send a deauthenticate frame and drop the station.
16868a1b9b6aSSam Leffler 			 * This is somewhat complicated due to reference counts
16878a1b9b6aSSam Leffler 			 * and locking.  At this point a station will typically
16888a1b9b6aSSam Leffler 			 * have a reference count of 1.  ieee80211_node_leave
16898a1b9b6aSSam Leffler 			 * will do a "free" of the node which will drop the
16908a1b9b6aSSam Leffler 			 * reference count.  But in the meantime a reference
16918a1b9b6aSSam Leffler 			 * wil be held by the deauth frame.  The actual reclaim
16928a1b9b6aSSam Leffler 			 * of the node will happen either after the tx is
16938a1b9b6aSSam Leffler 			 * completed or by ieee80211_node_leave.
16948a1b9b6aSSam Leffler 			 *
16958a1b9b6aSSam Leffler 			 * Separately we must drop the node lock before sending
16968a1b9b6aSSam Leffler 			 * in case the driver takes a lock, as this will result
16978a1b9b6aSSam Leffler 			 * in  LOR between the node lock and the driver lock.
16988a1b9b6aSSam Leffler 			 */
16998a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
17008a1b9b6aSSam Leffler 			if (ni->ni_associd != 0) {
17011a1e1d21SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
17021a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
17031a1e1d21SSam Leffler 				    IEEE80211_REASON_AUTH_EXPIRE);
17048a1b9b6aSSam Leffler 			}
17058a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
17061be50176SSam Leffler 			ic->ic_stats.is_node_timeout++;
1707303ebc3cSSam Leffler 			goto restart;
1708303ebc3cSSam Leffler 		}
17091a1e1d21SSam Leffler 	}
17108a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
17118a1b9b6aSSam Leffler 
17128a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
17138a1b9b6aSSam Leffler 
17148a1b9b6aSSam Leffler 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
17151a1e1d21SSam Leffler }
17161a1e1d21SSam Leffler 
17171a1e1d21SSam Leffler void
17188a1b9b6aSSam Leffler ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
17191a1e1d21SSam Leffler {
17201a1e1d21SSam Leffler 	struct ieee80211_node *ni;
17218a1b9b6aSSam Leffler 	u_int gen;
17221a1e1d21SSam Leffler 
17238a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
17248a1b9b6aSSam Leffler 	gen = nt->nt_scangen++;
17258a1b9b6aSSam Leffler restart:
17268a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
17278a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
17288a1b9b6aSSam Leffler 		if (ni->ni_scangen != gen) {
17298a1b9b6aSSam Leffler 			ni->ni_scangen = gen;
17308a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
17318a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
17321a1e1d21SSam Leffler 			(*f)(arg, ni);
17338a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
17348a1b9b6aSSam Leffler 			goto restart;
17358a1b9b6aSSam Leffler 		}
17368a1b9b6aSSam Leffler 	}
17378a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
17388a1b9b6aSSam Leffler 
17398a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
17408a1b9b6aSSam Leffler }
17418a1b9b6aSSam Leffler 
17428a1b9b6aSSam Leffler void
17438a1b9b6aSSam Leffler ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
17448a1b9b6aSSam Leffler {
17458a1b9b6aSSam Leffler 	printf("0x%p: mac %s refcnt %d\n", ni,
17468a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
17478a1b9b6aSSam Leffler 	printf("\tscangen %u authmode %u flags 0x%x\n",
17488a1b9b6aSSam Leffler 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
17498a1b9b6aSSam Leffler 	printf("\tassocid 0x%x txpower %u vlan %u\n",
17508a1b9b6aSSam Leffler 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
17518a1b9b6aSSam Leffler 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
17528a1b9b6aSSam Leffler 		ni->ni_txseqs[0],
17538a1b9b6aSSam Leffler 		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
17548a1b9b6aSSam Leffler 		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
17558a1b9b6aSSam Leffler 		ni->ni_rxfragstamp);
17568a1b9b6aSSam Leffler 	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
17578a1b9b6aSSam Leffler 		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
17588a1b9b6aSSam Leffler 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
17598a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
17608a1b9b6aSSam Leffler 		ni->ni_esslen, ni->ni_essid,
17618a1b9b6aSSam Leffler 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
17628a1b9b6aSSam Leffler 	printf("\tfails %u inact %u txrate %u\n",
17638a1b9b6aSSam Leffler 		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
17648a1b9b6aSSam Leffler }
17658a1b9b6aSSam Leffler 
17668a1b9b6aSSam Leffler void
17678a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt)
17688a1b9b6aSSam Leffler {
17698a1b9b6aSSam Leffler 	ieee80211_iterate_nodes(nt,
17708a1b9b6aSSam Leffler 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
17718a1b9b6aSSam Leffler }
17728a1b9b6aSSam Leffler 
17738a1b9b6aSSam Leffler /*
17748a1b9b6aSSam Leffler  * Handle a station joining an 11g network.
17758a1b9b6aSSam Leffler  */
17768a1b9b6aSSam Leffler static void
17778a1b9b6aSSam Leffler ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
17788a1b9b6aSSam Leffler {
17798a1b9b6aSSam Leffler 
17808a1b9b6aSSam Leffler 	/*
17818a1b9b6aSSam Leffler 	 * Station isn't capable of short slot time.  Bump
17828a1b9b6aSSam Leffler 	 * the count of long slot time stations and disable
17838a1b9b6aSSam Leffler 	 * use of short slot time.  Note that the actual switch
17848a1b9b6aSSam Leffler 	 * over to long slot time use may not occur until the
17858a1b9b6aSSam Leffler 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
17868a1b9b6aSSam Leffler 	 */
17878a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
17888a1b9b6aSSam Leffler 		ic->ic_longslotsta++;
17898a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
17908a1b9b6aSSam Leffler 		    "[%s] station needs long slot time, count %d\n",
17918a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
17928a1b9b6aSSam Leffler 		/* XXX vap's w/ conflicting needs won't work */
17938a1b9b6aSSam Leffler 		ieee80211_set_shortslottime(ic, 0);
17948a1b9b6aSSam Leffler 	}
17958a1b9b6aSSam Leffler 	/*
17968a1b9b6aSSam Leffler 	 * If the new station is not an ERP station
17978a1b9b6aSSam Leffler 	 * then bump the counter and enable protection
17988a1b9b6aSSam Leffler 	 * if configured.
17998a1b9b6aSSam Leffler 	 */
18008a1b9b6aSSam Leffler 	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
18018a1b9b6aSSam Leffler 		ic->ic_nonerpsta++;
18028a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
18038a1b9b6aSSam Leffler 		    "[%s] station is !ERP, %d non-ERP stations associated\n",
18048a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
18058a1b9b6aSSam Leffler 		/*
18068a1b9b6aSSam Leffler 		 * If protection is configured, enable it.
18078a1b9b6aSSam Leffler 		 */
18088a1b9b6aSSam Leffler 		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
18098a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
18108a1b9b6aSSam Leffler 			    "%s: enable use of protection\n", __func__);
18118a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
18128a1b9b6aSSam Leffler 		}
18138a1b9b6aSSam Leffler 		/*
18148a1b9b6aSSam Leffler 		 * If station does not support short preamble
18158a1b9b6aSSam Leffler 		 * then we must enable use of Barker preamble.
18168a1b9b6aSSam Leffler 		 */
18178a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
18188a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
18198a1b9b6aSSam Leffler 			    "[%s] station needs long preamble\n",
18208a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr));
18218a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
18228a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
18238a1b9b6aSSam Leffler 		}
18248a1b9b6aSSam Leffler 	} else
18258a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
18268a1b9b6aSSam Leffler }
18278a1b9b6aSSam Leffler 
18288a1b9b6aSSam Leffler void
18298a1b9b6aSSam Leffler ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
18308a1b9b6aSSam Leffler {
18318a1b9b6aSSam Leffler 	int newassoc;
18328a1b9b6aSSam Leffler 
18338a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
18348a1b9b6aSSam Leffler 		u_int16_t aid;
18358a1b9b6aSSam Leffler 
18368a1b9b6aSSam Leffler 		/*
18378a1b9b6aSSam Leffler 		 * It would be good to search the bitmap
18388a1b9b6aSSam Leffler 		 * more efficiently, but this will do for now.
18398a1b9b6aSSam Leffler 		 */
18408a1b9b6aSSam Leffler 		for (aid = 1; aid < ic->ic_max_aid; aid++) {
18418a1b9b6aSSam Leffler 			if (!IEEE80211_AID_ISSET(aid,
18428a1b9b6aSSam Leffler 			    ic->ic_aid_bitmap))
18438a1b9b6aSSam Leffler 				break;
18448a1b9b6aSSam Leffler 		}
18458a1b9b6aSSam Leffler 		if (aid >= ic->ic_max_aid) {
18468a1b9b6aSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
18478a1b9b6aSSam Leffler 			    IEEE80211_REASON_ASSOC_TOOMANY);
18488a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
18498a1b9b6aSSam Leffler 			return;
18508a1b9b6aSSam Leffler 		}
18518a1b9b6aSSam Leffler 		ni->ni_associd = aid | 0xc000;
18528a1b9b6aSSam Leffler 		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
18538a1b9b6aSSam Leffler 		ic->ic_sta_assoc++;
18548a1b9b6aSSam Leffler 		newassoc = 1;
1855624a1bdbSSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G ||
1856624a1bdbSSam Leffler 		    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
18578a1b9b6aSSam Leffler 			ieee80211_node_join_11g(ic, ni);
18588a1b9b6aSSam Leffler 	} else
18598a1b9b6aSSam Leffler 		newassoc = 0;
18608a1b9b6aSSam Leffler 
18618a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1862b8fcf546SSam Leffler 	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
1863b8fcf546SSam Leffler 	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
1864b8fcf546SSam Leffler 	    IEEE80211_NODE_AID(ni),
1865b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
1866b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
1867b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
1868b8fcf546SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
1869b8fcf546SSam Leffler 	);
18708a1b9b6aSSam Leffler 
18718a1b9b6aSSam Leffler 	/* give driver a chance to setup state like ni_txrate */
1872736b3dc3SSam Leffler 	if (ic->ic_newassoc != NULL)
1873e9962332SSam Leffler 		ic->ic_newassoc(ni, newassoc);
18742045f699SSam Leffler 	ni->ni_inact_reload = ic->ic_inact_auth;
18752045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
18768a1b9b6aSSam Leffler 	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
18778a1b9b6aSSam Leffler 	/* tell the authenticator about new station */
18788a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_join != NULL)
18798a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_join(ic, ni);
18808a1b9b6aSSam Leffler 	ieee80211_notify_node_join(ic, ni, newassoc);
18818a1b9b6aSSam Leffler }
18828a1b9b6aSSam Leffler 
18838a1b9b6aSSam Leffler /*
18848a1b9b6aSSam Leffler  * Handle a station leaving an 11g network.
18858a1b9b6aSSam Leffler  */
18868a1b9b6aSSam Leffler static void
18878a1b9b6aSSam Leffler ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
18888a1b9b6aSSam Leffler {
18898a1b9b6aSSam Leffler 
1890624a1bdbSSam Leffler 	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G ||
1891624a1bdbSSam Leffler 		ic->ic_curmode == IEEE80211_MODE_TURBO_G,
18928a1b9b6aSSam Leffler 		("not in 11g, curmode %x", ic->ic_curmode));
18938a1b9b6aSSam Leffler 
18948a1b9b6aSSam Leffler 	/*
18958a1b9b6aSSam Leffler 	 * If a long slot station do the slot time bookkeeping.
18968a1b9b6aSSam Leffler 	 */
18978a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
18988a1b9b6aSSam Leffler 		KASSERT(ic->ic_longslotsta > 0,
18998a1b9b6aSSam Leffler 		    ("bogus long slot station count %d", ic->ic_longslotsta));
19008a1b9b6aSSam Leffler 		ic->ic_longslotsta--;
19018a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
19028a1b9b6aSSam Leffler 		    "[%s] long slot time station leaves, count now %d\n",
19038a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
19048a1b9b6aSSam Leffler 		if (ic->ic_longslotsta == 0) {
19058a1b9b6aSSam Leffler 			/*
19068a1b9b6aSSam Leffler 			 * Re-enable use of short slot time if supported
19078a1b9b6aSSam Leffler 			 * and not operating in IBSS mode (per spec).
19088a1b9b6aSSam Leffler 			 */
19098a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
19108a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_IBSS) {
19118a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
19128a1b9b6aSSam Leffler 				    "%s: re-enable use of short slot time\n",
19138a1b9b6aSSam Leffler 				    __func__);
19148a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic, 1);
19158a1b9b6aSSam Leffler 			}
19168a1b9b6aSSam Leffler 		}
19178a1b9b6aSSam Leffler 	}
19188a1b9b6aSSam Leffler 	/*
19198a1b9b6aSSam Leffler 	 * If a non-ERP station do the protection-related bookkeeping.
19208a1b9b6aSSam Leffler 	 */
19218a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
19228a1b9b6aSSam Leffler 		KASSERT(ic->ic_nonerpsta > 0,
19238a1b9b6aSSam Leffler 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
19248a1b9b6aSSam Leffler 		ic->ic_nonerpsta--;
19258a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
19268a1b9b6aSSam Leffler 		    "[%s] non-ERP station leaves, count now %d\n",
19278a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
19288a1b9b6aSSam Leffler 		if (ic->ic_nonerpsta == 0) {
19298a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
19308a1b9b6aSSam Leffler 				"%s: disable use of protection\n", __func__);
19318a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
19328a1b9b6aSSam Leffler 			/* XXX verify mode? */
19338a1b9b6aSSam Leffler 			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
19348a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
19358a1b9b6aSSam Leffler 				    "%s: re-enable use of short preamble\n",
19368a1b9b6aSSam Leffler 				    __func__);
19378a1b9b6aSSam Leffler 				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
19388a1b9b6aSSam Leffler 				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
19398a1b9b6aSSam Leffler 			}
19408a1b9b6aSSam Leffler 		}
19418a1b9b6aSSam Leffler 	}
19428a1b9b6aSSam Leffler }
19438a1b9b6aSSam Leffler 
19448a1b9b6aSSam Leffler /*
19458a1b9b6aSSam Leffler  * Handle bookkeeping for station deauthentication/disassociation
19468a1b9b6aSSam Leffler  * when operating as an ap.
19478a1b9b6aSSam Leffler  */
19488a1b9b6aSSam Leffler void
19498a1b9b6aSSam Leffler ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
19508a1b9b6aSSam Leffler {
195144acc00dSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
19528a1b9b6aSSam Leffler 
19538a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
19548a1b9b6aSSam Leffler 	    "[%s] station with aid %d leaves\n",
19558a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
19568a1b9b6aSSam Leffler 
19578a1b9b6aSSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
19588a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_IBSS ||
19598a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_AHDEMO,
19608a1b9b6aSSam Leffler 		("unexpected operating mode %u", ic->ic_opmode));
19618a1b9b6aSSam Leffler 	/*
19628a1b9b6aSSam Leffler 	 * If node wasn't previously associated all
19638a1b9b6aSSam Leffler 	 * we need to do is reclaim the reference.
19648a1b9b6aSSam Leffler 	 */
19658a1b9b6aSSam Leffler 	/* XXX ibss mode bypasses 11g and notification */
19668a1b9b6aSSam Leffler 	if (ni->ni_associd == 0)
19678a1b9b6aSSam Leffler 		goto done;
19688a1b9b6aSSam Leffler 	/*
19698a1b9b6aSSam Leffler 	 * Tell the authenticator the station is leaving.
19708a1b9b6aSSam Leffler 	 * Note that we must do this before yanking the
19718a1b9b6aSSam Leffler 	 * association id as the authenticator uses the
19728a1b9b6aSSam Leffler 	 * associd to locate it's state block.
19738a1b9b6aSSam Leffler 	 */
19748a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_leave != NULL)
19758a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_leave(ic, ni);
19768a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
19778a1b9b6aSSam Leffler 	ni->ni_associd = 0;
19788a1b9b6aSSam Leffler 	ic->ic_sta_assoc--;
19798a1b9b6aSSam Leffler 
1980624a1bdbSSam Leffler 	if (ic->ic_curmode == IEEE80211_MODE_11G ||
1981624a1bdbSSam Leffler 	    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
19828a1b9b6aSSam Leffler 		ieee80211_node_leave_11g(ic, ni);
19838a1b9b6aSSam Leffler 	/*
19848a1b9b6aSSam Leffler 	 * Cleanup station state.  In particular clear various
19858a1b9b6aSSam Leffler 	 * state that might otherwise be reused if the node
19868a1b9b6aSSam Leffler 	 * is reused before the reference count goes to zero
19878a1b9b6aSSam Leffler 	 * (and memory is reclaimed).
19888a1b9b6aSSam Leffler 	 */
19898a1b9b6aSSam Leffler 	ieee80211_sta_leave(ic, ni);
19908a1b9b6aSSam Leffler done:
199144acc00dSSam Leffler 	/*
199244acc00dSSam Leffler 	 * Remove the node from any table it's recorded in and
199344acc00dSSam Leffler 	 * drop the caller's reference.  Removal from the table
199444acc00dSSam Leffler 	 * is important to insure the node is not reprocessed
199544acc00dSSam Leffler 	 * for inactivity.
199644acc00dSSam Leffler 	 */
199744acc00dSSam Leffler 	if (nt != NULL) {
199844acc00dSSam Leffler 		IEEE80211_NODE_LOCK(nt);
199944acc00dSSam Leffler 		node_reclaim(nt, ni);
200044acc00dSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
200144acc00dSSam Leffler 	} else
20028a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
20038a1b9b6aSSam Leffler }
20048a1b9b6aSSam Leffler 
20058a1b9b6aSSam Leffler u_int8_t
20068a1b9b6aSSam Leffler ieee80211_getrssi(struct ieee80211com *ic)
20078a1b9b6aSSam Leffler {
20088a1b9b6aSSam Leffler #define	NZ(x)	((x) == 0 ? 1 : (x))
2009acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
20108a1b9b6aSSam Leffler 	u_int32_t rssi_samples, rssi_total;
20118a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
20128a1b9b6aSSam Leffler 
20138a1b9b6aSSam Leffler 	rssi_total = 0;
20148a1b9b6aSSam Leffler 	rssi_samples = 0;
20158a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
20168a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
20178a1b9b6aSSam Leffler 		/* XXX locking */
2018acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
20198a1b9b6aSSam Leffler 			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
20208a1b9b6aSSam Leffler 				rssi_samples++;
20218a1b9b6aSSam Leffler 				rssi_total += ic->ic_node_getrssi(ni);
20228a1b9b6aSSam Leffler 			}
20238a1b9b6aSSam Leffler 		break;
20248a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
20258a1b9b6aSSam Leffler 		/* XXX locking */
2026acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
20278a1b9b6aSSam Leffler 			rssi_samples++;
20288a1b9b6aSSam Leffler 			rssi_total += ic->ic_node_getrssi(ni);
20298a1b9b6aSSam Leffler 		}
20308a1b9b6aSSam Leffler 		break;
20318a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
20328a1b9b6aSSam Leffler 		/* XXX locking */
2033acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
20348a1b9b6aSSam Leffler 			if (IEEE80211_AID(ni->ni_associd) != 0) {
20358a1b9b6aSSam Leffler 				rssi_samples++;
20368a1b9b6aSSam Leffler 				rssi_total += ic->ic_node_getrssi(ni);
20378a1b9b6aSSam Leffler 			}
20388a1b9b6aSSam Leffler 		break;
20398a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:	/* XXX */
20408a1b9b6aSSam Leffler 	case IEEE80211_M_STA:		/* use stats from associated ap */
20418a1b9b6aSSam Leffler 	default:
20428a1b9b6aSSam Leffler 		if (ic->ic_bss != NULL)
20438a1b9b6aSSam Leffler 			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
20448a1b9b6aSSam Leffler 		rssi_samples = 1;
20458a1b9b6aSSam Leffler 		break;
20468a1b9b6aSSam Leffler 	}
20478a1b9b6aSSam Leffler 	return rssi_total / NZ(rssi_samples);
20488a1b9b6aSSam Leffler #undef NZ
20498a1b9b6aSSam Leffler }
20508a1b9b6aSSam Leffler 
20518a1b9b6aSSam Leffler /*
20528a1b9b6aSSam Leffler  * Indicate whether there are frames queued for a station in power-save mode.
20538a1b9b6aSSam Leffler  */
20548a1b9b6aSSam Leffler static void
2055edfa57d0SSam Leffler ieee80211_set_tim(struct ieee80211_node *ni, int set)
20568a1b9b6aSSam Leffler {
2057edfa57d0SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
20588a1b9b6aSSam Leffler 	u_int16_t aid;
20598a1b9b6aSSam Leffler 
20608a1b9b6aSSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
20618a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_IBSS,
20628a1b9b6aSSam Leffler 		("operating mode %u", ic->ic_opmode));
20638a1b9b6aSSam Leffler 
20648a1b9b6aSSam Leffler 	aid = IEEE80211_AID(ni->ni_associd);
20658a1b9b6aSSam Leffler 	KASSERT(aid < ic->ic_max_aid,
20668a1b9b6aSSam Leffler 		("bogus aid %u, max %u", aid, ic->ic_max_aid));
20678a1b9b6aSSam Leffler 
20688a1b9b6aSSam Leffler 	IEEE80211_BEACON_LOCK(ic);
20698a1b9b6aSSam Leffler 	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
20708a1b9b6aSSam Leffler 		if (set) {
20718a1b9b6aSSam Leffler 			setbit(ic->ic_tim_bitmap, aid);
20728a1b9b6aSSam Leffler 			ic->ic_ps_pending++;
20738a1b9b6aSSam Leffler 		} else {
20748a1b9b6aSSam Leffler 			clrbit(ic->ic_tim_bitmap, aid);
20758a1b9b6aSSam Leffler 			ic->ic_ps_pending--;
20768a1b9b6aSSam Leffler 		}
20778a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
20788a1b9b6aSSam Leffler 	}
20798a1b9b6aSSam Leffler 	IEEE80211_BEACON_UNLOCK(ic);
20808a1b9b6aSSam Leffler }
20818a1b9b6aSSam Leffler 
20828a1b9b6aSSam Leffler /*
20838a1b9b6aSSam Leffler  * Node table support.
20848a1b9b6aSSam Leffler  */
20858a1b9b6aSSam Leffler 
20868a1b9b6aSSam Leffler static void
20878a1b9b6aSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic,
20888a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt,
2089c1225b52SSam Leffler 	const char *name, int inact, int keyixmax,
20908a1b9b6aSSam Leffler 	void (*timeout)(struct ieee80211_node_table *))
20918a1b9b6aSSam Leffler {
20928a1b9b6aSSam Leffler 
20938a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
20948a1b9b6aSSam Leffler 		"%s %s table, inact %u\n", __func__, name, inact);
20958a1b9b6aSSam Leffler 
20968a1b9b6aSSam Leffler 	nt->nt_ic = ic;
20978a1b9b6aSSam Leffler 	/* XXX need unit */
20988a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
20998a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
21008a1b9b6aSSam Leffler 	TAILQ_INIT(&nt->nt_node);
21018a1b9b6aSSam Leffler 	nt->nt_name = name;
21028a1b9b6aSSam Leffler 	nt->nt_scangen = 1;
21038a1b9b6aSSam Leffler 	nt->nt_inact_init = inact;
21048a1b9b6aSSam Leffler 	nt->nt_timeout = timeout;
2105c1225b52SSam Leffler 	nt->nt_keyixmax = keyixmax;
2106c1225b52SSam Leffler 	if (nt->nt_keyixmax > 0) {
2107c1225b52SSam Leffler 		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
2108c1225b52SSam Leffler 			keyixmax * sizeof(struct ieee80211_node *),
2109c1225b52SSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
2110c1225b52SSam Leffler 		if (nt->nt_keyixmap == NULL)
2111c1225b52SSam Leffler 			if_printf(ic->ic_ifp,
2112c1225b52SSam Leffler 			    "Cannot allocate key index map with %u entries\n",
2113c1225b52SSam Leffler 			    keyixmax);
2114c1225b52SSam Leffler 	} else
2115c1225b52SSam Leffler 		nt->nt_keyixmap = NULL;
21168a1b9b6aSSam Leffler }
21178a1b9b6aSSam Leffler 
21188a1b9b6aSSam Leffler void
21198a1b9b6aSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt)
21208a1b9b6aSSam Leffler {
21218a1b9b6aSSam Leffler 
21228a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
21238a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
21248a1b9b6aSSam Leffler 
21258a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
21268a1b9b6aSSam Leffler 	nt->nt_inact_timer = 0;
21278a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
21288a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
21298a1b9b6aSSam Leffler }
21308a1b9b6aSSam Leffler 
21318a1b9b6aSSam Leffler static void
21328a1b9b6aSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
21338a1b9b6aSSam Leffler {
21348a1b9b6aSSam Leffler 
21358a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
21368a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
21378a1b9b6aSSam Leffler 
2138c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
21398a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
2140c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL) {
2141c1225b52SSam Leffler 		/* XXX verify all entries are NULL */
2142c1225b52SSam Leffler 		int i;
2143c1225b52SSam Leffler 		for (i = 0; i < nt->nt_keyixmax; i++)
2144c1225b52SSam Leffler 			if (nt->nt_keyixmap[i] != NULL)
2145c1225b52SSam Leffler 				printf("%s: %s[%u] still active\n", __func__,
2146c1225b52SSam Leffler 					nt->nt_name, i);
2147c1225b52SSam Leffler 		FREE(nt->nt_keyixmap, M_80211_NODE);
2148c1225b52SSam Leffler 		nt->nt_keyixmap = NULL;
2149c1225b52SSam Leffler 	}
21508a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_DESTROY(nt);
21518a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_DESTROY(nt);
21528a1b9b6aSSam Leffler }
2153