xref: /freebsd/sys/net80211/ieee80211_node.c (revision fe49f061b48ac0d987de7a3e6a8dc9146a58e8ec)
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 
62132142c5SDiomidis Spinellis #ifdef IEEE80211_DEBUG_REFCNT
63132142c5SDiomidis Spinellis #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
64132142c5SDiomidis Spinellis #else
65132142c5SDiomidis Spinellis #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
66132142c5SDiomidis Spinellis #endif
67132142c5SDiomidis Spinellis 
688a1b9b6aSSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
698a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *);
708a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *);
718a1b9b6aSSam Leffler static u_int8_t node_getrssi(const struct ieee80211_node *);
721a1e1d21SSam Leffler 
738a1b9b6aSSam Leffler static void ieee80211_setup_node(struct ieee80211_node_table *,
748a1b9b6aSSam Leffler 		struct ieee80211_node *, const u_int8_t *);
758a1b9b6aSSam Leffler static void _ieee80211_free_node(struct ieee80211_node *);
768a1b9b6aSSam Leffler static void ieee80211_free_allnodes(struct ieee80211_node_table *);
77d1e61976SSam Leffler 
788a1b9b6aSSam Leffler static void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
798a1b9b6aSSam Leffler static void ieee80211_timeout_stations(struct ieee80211_node_table *);
808a1b9b6aSSam Leffler 
81edfa57d0SSam Leffler static void ieee80211_set_tim(struct ieee80211_node *, int set);
828a1b9b6aSSam Leffler 
838a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic,
84c1225b52SSam Leffler 	struct ieee80211_node_table *nt, const char *name,
85c1225b52SSam Leffler 	int inact, int keyixmax,
868a1b9b6aSSam Leffler 	void (*timeout)(struct ieee80211_node_table *));
878a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
881a1e1d21SSam Leffler 
8932346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
9037c150c4SSam Leffler 
911a1e1d21SSam Leffler void
928a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic)
931a1e1d21SSam Leffler {
941a1e1d21SSam Leffler 
958a1b9b6aSSam Leffler 	ic->ic_node_alloc = node_alloc;
968a1b9b6aSSam Leffler 	ic->ic_node_free = node_free;
978a1b9b6aSSam Leffler 	ic->ic_node_cleanup = node_cleanup;
988a1b9b6aSSam Leffler 	ic->ic_node_getrssi = node_getrssi;
998a1b9b6aSSam Leffler 
1008a1b9b6aSSam Leffler 	/* default station inactivity timer setings */
1018a1b9b6aSSam Leffler 	ic->ic_inact_init = IEEE80211_INACT_INIT;
1028a1b9b6aSSam Leffler 	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
1038a1b9b6aSSam Leffler 	ic->ic_inact_run = IEEE80211_INACT_RUN;
1048a1b9b6aSSam Leffler 	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
1058a1b9b6aSSam Leffler 
106c1225b52SSam Leffler 	/* NB: driver should override */
1078a1b9b6aSSam Leffler 	ic->ic_max_aid = IEEE80211_AID_DEF;
108c1225b52SSam Leffler 	ic->ic_set_tim = ieee80211_set_tim;
109c1225b52SSam Leffler }
110c1225b52SSam Leffler 
111c1225b52SSam Leffler void
112c1225b52SSam Leffler ieee80211_node_lateattach(struct ieee80211com *ic)
113c1225b52SSam Leffler {
114c1225b52SSam Leffler 	struct ieee80211_rsnparms *rsn;
115c1225b52SSam Leffler 
116c1225b52SSam Leffler 	if (ic->ic_max_aid > IEEE80211_AID_MAX)
1178a1b9b6aSSam Leffler 		ic->ic_max_aid = IEEE80211_AID_MAX;
1188a1b9b6aSSam Leffler 	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
1198a1b9b6aSSam Leffler 		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
1208a1b9b6aSSam Leffler 		M_DEVBUF, M_NOWAIT | M_ZERO);
1218a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap == NULL) {
1228a1b9b6aSSam Leffler 		/* XXX no way to recover */
1238a1b9b6aSSam Leffler 		printf("%s: no memory for AID bitmap!\n", __func__);
1248a1b9b6aSSam Leffler 		ic->ic_max_aid = 0;
1258a1b9b6aSSam Leffler 	}
1268a1b9b6aSSam Leffler 
1278a1b9b6aSSam Leffler 	/* XXX defer until using hostap/ibss mode */
1288a1b9b6aSSam Leffler 	ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
1298a1b9b6aSSam Leffler 	MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
1308a1b9b6aSSam Leffler 		M_DEVBUF, M_NOWAIT | M_ZERO);
1318a1b9b6aSSam Leffler 	if (ic->ic_tim_bitmap == NULL) {
1328a1b9b6aSSam Leffler 		/* XXX no way to recover */
1338a1b9b6aSSam Leffler 		printf("%s: no memory for TIM bitmap!\n", __func__);
1348a1b9b6aSSam Leffler 	}
1352692bb26SSam Leffler 
136c1225b52SSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
137c1225b52SSam Leffler 		IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix,
138c1225b52SSam Leffler 		ieee80211_timeout_stations);
139c1225b52SSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
140c1225b52SSam Leffler 		IEEE80211_INACT_SCAN, 0,
141c1225b52SSam Leffler 		ieee80211_timeout_scan_candidates);
1422692bb26SSam Leffler 
143c1225b52SSam Leffler 	ieee80211_reset_bss(ic);
1448a1b9b6aSSam Leffler 	/*
1458a1b9b6aSSam Leffler 	 * Setup "global settings" in the bss node so that
1468a1b9b6aSSam Leffler 	 * each new station automatically inherits them.
1478a1b9b6aSSam Leffler 	 */
148c1225b52SSam Leffler 	rsn = &ic->ic_bss->ni_rsn;
1498a1b9b6aSSam Leffler 	/* WEP, TKIP, and AES-CCM are always supported */
1508a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
1518a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
1528a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
1538a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_AES)
1548a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
1558a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_CKIP)
1568a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
1578a1b9b6aSSam Leffler 	/*
1588a1b9b6aSSam Leffler 	 * Default unicast cipher to WEP for 802.1x use.  If
1598a1b9b6aSSam Leffler 	 * WPA is enabled the management code will set these
1608a1b9b6aSSam Leffler 	 * values to reflect.
1618a1b9b6aSSam Leffler 	 */
1628a1b9b6aSSam Leffler 	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
1638a1b9b6aSSam Leffler 	rsn->rsn_ucastkeylen = 104 / NBBY;
1648a1b9b6aSSam Leffler 	/*
1658a1b9b6aSSam Leffler 	 * WPA says the multicast cipher is the lowest unicast
1668a1b9b6aSSam Leffler 	 * cipher supported.  But we skip WEP which would
1678a1b9b6aSSam Leffler 	 * otherwise be used based on this criteria.
1688a1b9b6aSSam Leffler 	 */
1698a1b9b6aSSam Leffler 	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
1708a1b9b6aSSam Leffler 	rsn->rsn_mcastkeylen = 128 / NBBY;
1718a1b9b6aSSam Leffler 
1728a1b9b6aSSam Leffler 	/*
1738a1b9b6aSSam Leffler 	 * We support both WPA-PSK and 802.1x; the one used
1748a1b9b6aSSam Leffler 	 * is determined by the authentication mode and the
1758a1b9b6aSSam Leffler 	 * setting of the PSK state.
1768a1b9b6aSSam Leffler 	 */
1778a1b9b6aSSam Leffler 	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
1788a1b9b6aSSam Leffler 	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1798a1b9b6aSSam Leffler 
180c1225b52SSam Leffler 	ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
1811a1e1d21SSam Leffler }
1821a1e1d21SSam Leffler 
1831a1e1d21SSam Leffler void
1848a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic)
1851a1e1d21SSam Leffler {
1861a1e1d21SSam Leffler 
1878a1b9b6aSSam Leffler 	if (ic->ic_bss != NULL) {
1888a1b9b6aSSam Leffler 		ieee80211_free_node(ic->ic_bss);
1898a1b9b6aSSam Leffler 		ic->ic_bss = NULL;
1908a1b9b6aSSam Leffler 	}
1918a1b9b6aSSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_scan);
192acc4f7f5SSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_sta);
1938a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap != NULL) {
1948a1b9b6aSSam Leffler 		FREE(ic->ic_aid_bitmap, M_DEVBUF);
1958a1b9b6aSSam Leffler 		ic->ic_aid_bitmap = NULL;
1968a1b9b6aSSam Leffler 	}
1978a1b9b6aSSam Leffler 	if (ic->ic_tim_bitmap != NULL) {
1988a1b9b6aSSam Leffler 		FREE(ic->ic_tim_bitmap, M_DEVBUF);
1998a1b9b6aSSam Leffler 		ic->ic_tim_bitmap = NULL;
2008a1b9b6aSSam Leffler 	}
2018a1b9b6aSSam Leffler }
2028a1b9b6aSSam Leffler 
2038a1b9b6aSSam Leffler /*
2048a1b9b6aSSam Leffler  * Port authorize/unauthorize interfaces for use by an authenticator.
2058a1b9b6aSSam Leffler  */
2068a1b9b6aSSam Leffler 
2078a1b9b6aSSam Leffler void
208e4918ecdSSam Leffler ieee80211_node_authorize(struct ieee80211_node *ni)
2098a1b9b6aSSam Leffler {
210e4918ecdSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
211e4918ecdSSam Leffler 
2128a1b9b6aSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AUTH;
2132045f699SSam Leffler 	ni->ni_inact_reload = ic->ic_inact_run;
2148a1b9b6aSSam Leffler }
2158a1b9b6aSSam Leffler 
2168a1b9b6aSSam Leffler void
217e4918ecdSSam Leffler ieee80211_node_unauthorize(struct ieee80211_node *ni)
2188a1b9b6aSSam Leffler {
2198a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
2208a1b9b6aSSam Leffler }
2218a1b9b6aSSam Leffler 
2228a1b9b6aSSam Leffler /*
2238a1b9b6aSSam Leffler  * Set/change the channel.  The rate set is also updated as
2248a1b9b6aSSam Leffler  * to insure a consistent view by drivers.
2258a1b9b6aSSam Leffler  */
2261b49e120SSam Leffler static void
2278a1b9b6aSSam Leffler ieee80211_set_chan(struct ieee80211com *ic,
2288a1b9b6aSSam Leffler 	struct ieee80211_node *ni, struct ieee80211_channel *chan)
2298a1b9b6aSSam Leffler {
2301b49e120SSam Leffler 	if (chan == IEEE80211_CHAN_ANYC)	/* XXX while scanning */
2311b49e120SSam Leffler 		chan = ic->ic_curchan;
2328a1b9b6aSSam Leffler 	ni->ni_chan = chan;
23341b3c790SSam Leffler 	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
2341a1e1d21SSam Leffler }
2351a1e1d21SSam Leffler 
2361a1e1d21SSam Leffler /*
2371a1e1d21SSam Leffler  * AP scanning support.
2381a1e1d21SSam Leffler  */
2391a1e1d21SSam Leffler 
2408a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
2418a1b9b6aSSam Leffler static void
2428a1b9b6aSSam Leffler dump_chanlist(const u_char chans[])
2438a1b9b6aSSam Leffler {
2448a1b9b6aSSam Leffler 	const char *sep;
2458a1b9b6aSSam Leffler 	int i;
2468a1b9b6aSSam Leffler 
2478a1b9b6aSSam Leffler 	sep = " ";
2488a1b9b6aSSam Leffler 	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
2498a1b9b6aSSam Leffler 		if (isset(chans, i)) {
2508a1b9b6aSSam Leffler 			printf("%s%u", sep, i);
2518a1b9b6aSSam Leffler 			sep = ", ";
2528a1b9b6aSSam Leffler 		}
2538a1b9b6aSSam Leffler }
2548a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2558a1b9b6aSSam Leffler 
2561a1e1d21SSam Leffler /*
2578a1b9b6aSSam Leffler  * Initialize the channel set to scan based on the
2581a1e1d21SSam Leffler  * of available channels and the current PHY mode.
2591a1e1d21SSam Leffler  */
260a11c9a5cSSam Leffler static void
2618a1b9b6aSSam Leffler ieee80211_reset_scan(struct ieee80211com *ic)
2621a1e1d21SSam Leffler {
2631a1e1d21SSam Leffler 
2648a1b9b6aSSam Leffler 	/* XXX ic_des_chan should be handled with ic_chan_active */
2658a1b9b6aSSam Leffler 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
2668a1b9b6aSSam Leffler 		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
2678a1b9b6aSSam Leffler 		setbit(ic->ic_chan_scan,
2688a1b9b6aSSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_des_chan));
2698a1b9b6aSSam Leffler 	} else
2701a1e1d21SSam Leffler 		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
2711a1e1d21SSam Leffler 			sizeof(ic->ic_chan_active));
2728a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
2738a1b9b6aSSam Leffler 	if (ieee80211_msg_scan(ic)) {
2748a1b9b6aSSam Leffler 		printf("%s: scan set:", __func__);
2758a1b9b6aSSam Leffler 		dump_chanlist(ic->ic_chan_scan);
2768a1b9b6aSSam Leffler 		printf(" start chan %u\n",
277b5c99415SSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_curchan));
2788a1b9b6aSSam Leffler 	}
2798a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2801a1e1d21SSam Leffler }
2811a1e1d21SSam Leffler 
2821a1e1d21SSam Leffler /*
2831a1e1d21SSam Leffler  * Begin an active scan.
2841a1e1d21SSam Leffler  */
2851a1e1d21SSam Leffler void
2868a1b9b6aSSam Leffler ieee80211_begin_scan(struct ieee80211com *ic, int reset)
2871a1e1d21SSam Leffler {
2881a1e1d21SSam Leffler 
2898a1b9b6aSSam Leffler 	ic->ic_scan.nt_scangen++;
290a11c9a5cSSam Leffler 	/*
291a11c9a5cSSam Leffler 	 * In all but hostap mode scanning starts off in
292a11c9a5cSSam Leffler 	 * an active mode before switching to passive.
293a11c9a5cSSam Leffler 	 */
2941be50176SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
295a11c9a5cSSam Leffler 		ic->ic_flags |= IEEE80211_F_ASCAN;
2961be50176SSam Leffler 		ic->ic_stats.is_scan_active++;
2971be50176SSam Leffler 	} else
2981be50176SSam Leffler 		ic->ic_stats.is_scan_passive++;
2993ea67c54SSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
3003ea67c54SSam Leffler 		"begin %s scan in %s mode, scangen %u\n",
3018a1b9b6aSSam Leffler 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
3023ea67c54SSam Leffler 		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
3031a1e1d21SSam Leffler 	/*
3048a1b9b6aSSam Leffler 	 * Clear scan state and flush any previously seen AP's.
3051a1e1d21SSam Leffler 	 */
3068a1b9b6aSSam Leffler 	ieee80211_reset_scan(ic);
3078a1b9b6aSSam Leffler 	if (reset)
3088a1b9b6aSSam Leffler 		ieee80211_free_allnodes(&ic->ic_scan);
3098a1b9b6aSSam Leffler 
3108a1b9b6aSSam Leffler 	ic->ic_flags |= IEEE80211_F_SCAN;
3111a1e1d21SSam Leffler 
312a11c9a5cSSam Leffler 	/* Scan the next channel. */
3138a1b9b6aSSam Leffler 	ieee80211_next_scan(ic);
3141a1e1d21SSam Leffler }
3151a1e1d21SSam Leffler 
3161a1e1d21SSam Leffler /*
3171a1e1d21SSam Leffler  * Switch to the next channel marked for scanning.
3181a1e1d21SSam Leffler  */
3198a1b9b6aSSam Leffler int
3208a1b9b6aSSam Leffler ieee80211_next_scan(struct ieee80211com *ic)
3211a1e1d21SSam Leffler {
3221a1e1d21SSam Leffler 	struct ieee80211_channel *chan;
3231a1e1d21SSam Leffler 
3248a1b9b6aSSam Leffler 	/*
3258a1b9b6aSSam Leffler 	 * Insure any previous mgt frame timeouts don't fire.
3268a1b9b6aSSam Leffler 	 * This assumes the driver does the right thing in
3278a1b9b6aSSam Leffler 	 * flushing anything queued in the driver and below.
3288a1b9b6aSSam Leffler 	 */
3298a1b9b6aSSam Leffler 	ic->ic_mgt_timer = 0;
330097131ffSSam Leffler 	ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
3318a1b9b6aSSam Leffler 
332b5c99415SSam Leffler 	chan = ic->ic_curchan;
3338a1b9b6aSSam Leffler 	do {
3341a1e1d21SSam Leffler 		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
3351a1e1d21SSam Leffler 			chan = &ic->ic_channels[0];
3361a1e1d21SSam Leffler 		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
3371a1e1d21SSam Leffler 			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
3388a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
3398a1b9b6aSSam Leffler 			    "%s: chan %d->%d\n", __func__,
340b5c99415SSam Leffler 			    ieee80211_chan2ieee(ic, ic->ic_curchan),
3418a1b9b6aSSam Leffler 			    ieee80211_chan2ieee(ic, chan));
342b5c99415SSam Leffler 			ic->ic_curchan = chan;
343b5c99415SSam Leffler 			/*
344b5c99415SSam Leffler 			 * XXX drivers should do this as needed,
345b5c99415SSam Leffler 			 * XXX for now maintain compatibility
346b5c99415SSam Leffler 			 */
34741b3c790SSam Leffler 			ic->ic_bss->ni_rates = *ieee80211_get_suprates(ic, chan);
348a11c9a5cSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
3498a1b9b6aSSam Leffler 			return 1;
3508a1b9b6aSSam Leffler 		}
351b5c99415SSam Leffler 	} while (chan != ic->ic_curchan);
3528a1b9b6aSSam Leffler 	ieee80211_end_scan(ic);
3538a1b9b6aSSam Leffler 	return 0;
3541a1e1d21SSam Leffler }
3551a1e1d21SSam Leffler 
356097131ffSSam Leffler /*
357097131ffSSam Leffler  * Probe the curent channel, if allowed, while scanning.
358097131ffSSam Leffler  * If the channel is not marked passive-only then send
359097131ffSSam Leffler  * a probe request immediately.  Otherwise mark state and
360097131ffSSam Leffler  * listen for beacons on the channel; if we receive something
361097131ffSSam Leffler  * then we'll transmit a probe request.
362097131ffSSam Leffler  */
363097131ffSSam Leffler void
364097131ffSSam Leffler ieee80211_probe_curchan(struct ieee80211com *ic, int force)
365097131ffSSam Leffler {
366097131ffSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
367097131ffSSam Leffler 
368097131ffSSam Leffler 	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) {
369097131ffSSam Leffler 		/*
370097131ffSSam Leffler 		 * XXX send both broadcast+directed probe request
371097131ffSSam Leffler 		 */
372097131ffSSam Leffler 		ieee80211_send_probereq(ic->ic_bss,
373097131ffSSam Leffler 			ic->ic_myaddr, ifp->if_broadcastaddr,
374097131ffSSam Leffler 			ifp->if_broadcastaddr,
375097131ffSSam Leffler 			ic->ic_des_essid, ic->ic_des_esslen,
376097131ffSSam Leffler 			ic->ic_opt_ie, ic->ic_opt_ie_len);
377097131ffSSam Leffler 	} else
378097131ffSSam Leffler 		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
379097131ffSSam Leffler }
380097131ffSSam Leffler 
381f9cd9174SSam Leffler static __inline void
382f9cd9174SSam Leffler copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
383f9cd9174SSam Leffler {
384f9cd9174SSam Leffler 	/* propagate useful state */
385f9cd9174SSam Leffler 	nbss->ni_authmode = obss->ni_authmode;
386f9cd9174SSam Leffler 	nbss->ni_txpower = obss->ni_txpower;
387f9cd9174SSam Leffler 	nbss->ni_vlan = obss->ni_vlan;
388f9cd9174SSam Leffler 	nbss->ni_rsn = obss->ni_rsn;
389f9cd9174SSam Leffler 	/* XXX statistics? */
390f9cd9174SSam Leffler }
391f9cd9174SSam Leffler 
3921a1e1d21SSam Leffler void
3931a1e1d21SSam Leffler ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
3941a1e1d21SSam Leffler {
395acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt;
3961a1e1d21SSam Leffler 	struct ieee80211_node *ni;
3978a1b9b6aSSam Leffler 
3988a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
3998a1b9b6aSSam Leffler 		"%s: creating ibss\n", __func__);
4008a1b9b6aSSam Leffler 
4018a1b9b6aSSam Leffler 	/*
4028a1b9b6aSSam Leffler 	 * Create the station/neighbor table.  Note that for adhoc
4038a1b9b6aSSam Leffler 	 * mode we make the initial inactivity timer longer since
4048a1b9b6aSSam Leffler 	 * we create nodes only through discovery and they typically
4058a1b9b6aSSam Leffler 	 * are long-lived associations.
4068a1b9b6aSSam Leffler 	 */
407acc4f7f5SSam Leffler 	nt = &ic->ic_sta;
408acc4f7f5SSam Leffler 	IEEE80211_NODE_LOCK(nt);
409acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
410acc4f7f5SSam Leffler 		nt->nt_name = "station";
411acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_init;
412acc4f7f5SSam Leffler 	} else {
413acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
414acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
415acc4f7f5SSam Leffler 	}
416acc4f7f5SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
417acc4f7f5SSam Leffler 
418c1225b52SSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
419acc4f7f5SSam Leffler 	if (ni == NULL) {
420acc4f7f5SSam Leffler 		/* XXX recovery? */
4218a1b9b6aSSam Leffler 		return;
4228a1b9b6aSSam Leffler 	}
4231a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
4241a1e1d21SSam Leffler 	ni->ni_esslen = ic->ic_des_esslen;
4251a1e1d21SSam Leffler 	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
426f9cd9174SSam Leffler 	copy_bss(ni, ic->ic_bss);
427d365f9c7SSam Leffler 	ni->ni_intval = ic->ic_bintval;
4288a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
4291a1e1d21SSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
4301a1e1d21SSam Leffler 	if (ic->ic_phytype == IEEE80211_T_FH) {
4311a1e1d21SSam Leffler 		ni->ni_fhdwell = 200;	/* XXX */
4321a1e1d21SSam Leffler 		ni->ni_fhindex = 1;
4331a1e1d21SSam Leffler 	}
4348a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
4358a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SIBSS;
4368a1b9b6aSSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
43748b0a5beSSam Leffler 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
43848b0a5beSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
439fe49f061SSam Leffler 		else {
440fe49f061SSam Leffler 			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
441fe49f061SSam Leffler 			/* clear group bit, add local bit */
442fe49f061SSam Leffler 			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
443fe49f061SSam Leffler 		}
44450d8b493SSam Leffler 	} else if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
44550d8b493SSam Leffler 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
44650d8b493SSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
44750d8b493SSam Leffler 		else
44850d8b493SSam Leffler 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
4498a1b9b6aSSam Leffler 	}
4508a1b9b6aSSam Leffler 	/*
4518a1b9b6aSSam Leffler 	 * Fix the channel and related attributes.
4528a1b9b6aSSam Leffler 	 */
4538a1b9b6aSSam Leffler 	ieee80211_set_chan(ic, ni, chan);
454b5c99415SSam Leffler 	ic->ic_curchan = chan;
4558a1b9b6aSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
4568a1b9b6aSSam Leffler 	/*
4578a1b9b6aSSam Leffler 	 * Do mode-specific rate setup.
4588a1b9b6aSSam Leffler 	 */
459ca4ac7aeSSam Leffler 	if (IEEE80211_IS_CHAN_FULL(chan) &&
460ca4ac7aeSSam Leffler 	    (ic->ic_curmode == IEEE80211_MODE_11G ||
461ca4ac7aeSSam Leffler 	     ic->ic_curmode == IEEE80211_MODE_11B))
462ca4ac7aeSSam Leffler 		ieee80211_set11gbasicrates(&ni->ni_rates, ic->ic_curmode);
4638a1b9b6aSSam Leffler 
464acc4f7f5SSam Leffler 	(void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
4651a1e1d21SSam Leffler }
4661a1e1d21SSam Leffler 
4678a1b9b6aSSam Leffler void
4688a1b9b6aSSam Leffler ieee80211_reset_bss(struct ieee80211com *ic)
469b4c5a90fSSam Leffler {
4708a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *obss;
4718a1b9b6aSSam Leffler 
4728a1b9b6aSSam Leffler 	ieee80211_node_table_reset(&ic->ic_scan);
473acc4f7f5SSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta);
474acc4f7f5SSam Leffler 
4758a1b9b6aSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
4768a1b9b6aSSam Leffler 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
4778a1b9b6aSSam Leffler 	obss = ic->ic_bss;
4788a1b9b6aSSam Leffler 	ic->ic_bss = ieee80211_ref_node(ni);
479f9cd9174SSam Leffler 	if (obss != NULL) {
480f9cd9174SSam Leffler 		copy_bss(ni, obss);
481d365f9c7SSam Leffler 		ni->ni_intval = ic->ic_bintval;
4828a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
4838a1b9b6aSSam Leffler 	}
484f9cd9174SSam Leffler }
4858a1b9b6aSSam Leffler 
486936f15d2SSam Leffler /* XXX tunable */
487936f15d2SSam Leffler #define	STA_FAILS_MAX	2		/* assoc failures before ignored */
488936f15d2SSam Leffler 
4898a1b9b6aSSam Leffler static int
4908a1b9b6aSSam Leffler ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
4918a1b9b6aSSam Leffler {
492b4c5a90fSSam Leffler         u_int8_t rate;
493b4c5a90fSSam Leffler         int fail;
494b4c5a90fSSam Leffler 
495b4c5a90fSSam Leffler 	fail = 0;
496b4c5a90fSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
497b4c5a90fSSam Leffler 		fail |= 0x01;
498b4c5a90fSSam Leffler 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
499b4c5a90fSSam Leffler 	    ni->ni_chan != ic->ic_des_chan)
500b4c5a90fSSam Leffler 		fail |= 0x01;
501b4c5a90fSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
502b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
503b4c5a90fSSam Leffler 			fail |= 0x02;
504b4c5a90fSSam Leffler 	} else {
505b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
506b4c5a90fSSam Leffler 			fail |= 0x02;
507b4c5a90fSSam Leffler 	}
5088a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
509b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
510b4c5a90fSSam Leffler 			fail |= 0x04;
511b4c5a90fSSam Leffler 	} else {
512b4c5a90fSSam Leffler 		/* XXX does this mean privacy is supported or required? */
513b4c5a90fSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
514b4c5a90fSSam Leffler 			fail |= 0x04;
515b4c5a90fSSam Leffler 	}
51679edaebfSSam Leffler 	rate = ieee80211_fix_rate(ni,
51779edaebfSSam Leffler 	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
518b4c5a90fSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
519b4c5a90fSSam Leffler 		fail |= 0x08;
520b4c5a90fSSam Leffler 	if (ic->ic_des_esslen != 0 &&
521b4c5a90fSSam Leffler 	    (ni->ni_esslen != ic->ic_des_esslen ||
522b4c5a90fSSam Leffler 	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
523b4c5a90fSSam Leffler 		fail |= 0x10;
524b4c5a90fSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
525b4c5a90fSSam Leffler 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
526b4c5a90fSSam Leffler 		fail |= 0x20;
527936f15d2SSam Leffler 	if (ni->ni_fails >= STA_FAILS_MAX)
528936f15d2SSam Leffler 		fail |= 0x40;
529b4c5a90fSSam Leffler #ifdef IEEE80211_DEBUG
5308a1b9b6aSSam Leffler 	if (ieee80211_msg_scan(ic)) {
531936f15d2SSam Leffler 		printf(" %c %s",
532936f15d2SSam Leffler 		    fail & 0x40 ? '=' : fail & 0x80 ? '^' : fail ? '-' : '+',
533b4c5a90fSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
534b4c5a90fSSam Leffler 		printf(" %s%c", ether_sprintf(ni->ni_bssid),
535b4c5a90fSSam Leffler 		    fail & 0x20 ? '!' : ' ');
536b4c5a90fSSam Leffler 		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
537b4c5a90fSSam Leffler 			fail & 0x01 ? '!' : ' ');
538b4c5a90fSSam Leffler 		printf(" %+4d", ni->ni_rssi);
539b4c5a90fSSam Leffler 		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
540b4c5a90fSSam Leffler 		    fail & 0x08 ? '!' : ' ');
541b4c5a90fSSam Leffler 		printf(" %4s%c",
542b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
543b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
544b4c5a90fSSam Leffler 		    "????",
545b4c5a90fSSam Leffler 		    fail & 0x02 ? '!' : ' ');
546b4c5a90fSSam Leffler 		printf(" %3s%c ",
547b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
548b4c5a90fSSam Leffler 		    "wep" : "no",
549b4c5a90fSSam Leffler 		    fail & 0x04 ? '!' : ' ');
550b4c5a90fSSam Leffler 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
551b4c5a90fSSam Leffler 		printf("%s\n", fail & 0x10 ? "!" : "");
552b4c5a90fSSam Leffler 	}
553b4c5a90fSSam Leffler #endif
554b4c5a90fSSam Leffler 	return fail;
555b4c5a90fSSam Leffler }
556b4c5a90fSSam Leffler 
5578a1b9b6aSSam Leffler static __inline u_int8_t
5588a1b9b6aSSam Leffler maxrate(const struct ieee80211_node *ni)
5598a1b9b6aSSam Leffler {
5608a1b9b6aSSam Leffler 	const struct ieee80211_rateset *rs = &ni->ni_rates;
5618a1b9b6aSSam Leffler 	/* NB: assumes rate set is sorted (happens on frame receive) */
5628a1b9b6aSSam Leffler 	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
5638a1b9b6aSSam Leffler }
5648a1b9b6aSSam Leffler 
5658a1b9b6aSSam Leffler /*
5668a1b9b6aSSam Leffler  * Compare the capabilities of two nodes and decide which is
5678a1b9b6aSSam Leffler  * more desirable (return >0 if a is considered better).  Note
5688a1b9b6aSSam Leffler  * that we assume compatibility/usability has already been checked
5698a1b9b6aSSam Leffler  * so we don't need to (e.g. validate whether privacy is supported).
5708a1b9b6aSSam Leffler  * Used to select the best scan candidate for association in a BSS.
5718a1b9b6aSSam Leffler  */
5728a1b9b6aSSam Leffler static int
5738a1b9b6aSSam Leffler ieee80211_node_compare(struct ieee80211com *ic,
5748a1b9b6aSSam Leffler 		       const struct ieee80211_node *a,
5758a1b9b6aSSam Leffler 		       const struct ieee80211_node *b)
5768a1b9b6aSSam Leffler {
5778a1b9b6aSSam Leffler 	u_int8_t maxa, maxb;
5788a1b9b6aSSam Leffler 	u_int8_t rssia, rssib;
579936f15d2SSam Leffler 	int weight;
5808a1b9b6aSSam Leffler 
5818a1b9b6aSSam Leffler 	/* privacy support preferred */
5828a1b9b6aSSam Leffler 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
5838a1b9b6aSSam Leffler 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
5848a1b9b6aSSam Leffler 		return 1;
5858a1b9b6aSSam Leffler 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
5868a1b9b6aSSam Leffler 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
5878a1b9b6aSSam Leffler 		return -1;
5888a1b9b6aSSam Leffler 
589936f15d2SSam Leffler 	/* compare count of previous failures */
590936f15d2SSam Leffler 	weight = b->ni_fails - a->ni_fails;
591936f15d2SSam Leffler 	if (abs(weight) > 1)
592936f15d2SSam Leffler 		return weight;
593936f15d2SSam Leffler 
594ef92bcdcSSam Leffler 	rssia = ic->ic_node_getrssi(a);
595ef92bcdcSSam Leffler 	rssib = ic->ic_node_getrssi(b);
596ef92bcdcSSam Leffler 	if (abs(rssib - rssia) < 5) {
5978a1b9b6aSSam Leffler 		/* best/max rate preferred if signal level close enough XXX */
5988a1b9b6aSSam Leffler 		maxa = maxrate(a);
5998a1b9b6aSSam Leffler 		maxb = maxrate(b);
600ef92bcdcSSam Leffler 		if (maxa != maxb)
6018a1b9b6aSSam Leffler 			return maxa - maxb;
6028a1b9b6aSSam Leffler 		/* XXX use freq for channel preference */
6038a1b9b6aSSam Leffler 		/* for now just prefer 5Ghz band to all other bands */
6048a1b9b6aSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
6058a1b9b6aSSam Leffler 		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
6068a1b9b6aSSam Leffler 			return 1;
6078a1b9b6aSSam Leffler 		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
6088a1b9b6aSSam Leffler 		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
6098a1b9b6aSSam Leffler 			return -1;
610ef92bcdcSSam Leffler 	}
6118a1b9b6aSSam Leffler 	/* all things being equal, use signal level */
6128a1b9b6aSSam Leffler 	return rssia - rssib;
6138a1b9b6aSSam Leffler }
6148a1b9b6aSSam Leffler 
6151a1e1d21SSam Leffler /*
616c75ac469SSam Leffler  * Mark an ongoing scan stopped.
617c75ac469SSam Leffler  */
618c75ac469SSam Leffler void
619c75ac469SSam Leffler ieee80211_cancel_scan(struct ieee80211com *ic)
620c75ac469SSam Leffler {
621c75ac469SSam Leffler 
622c75ac469SSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
623c75ac469SSam Leffler 		__func__,
624c75ac469SSam Leffler 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
625c75ac469SSam Leffler 
626c75ac469SSam Leffler 	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
627097131ffSSam Leffler 	ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
628c75ac469SSam Leffler }
629c75ac469SSam Leffler 
630c75ac469SSam Leffler /*
6311a1e1d21SSam Leffler  * Complete a scan of potential channels.
6321a1e1d21SSam Leffler  */
6331a1e1d21SSam Leffler void
6348a1b9b6aSSam Leffler ieee80211_end_scan(struct ieee80211com *ic)
6351a1e1d21SSam Leffler {
6363fcfbbfaSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_scan;
6373fcfbbfaSSam Leffler 	struct ieee80211_node *ni, *selbs;
6381a1e1d21SSam Leffler 
639c75ac469SSam Leffler 	ieee80211_cancel_scan(ic);
640c75ac469SSam Leffler 	ieee80211_notify_scan_done(ic);
6418a1b9b6aSSam Leffler 
6421a1e1d21SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
6438a1b9b6aSSam Leffler 		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
6448a1b9b6aSSam Leffler 		int i, bestchan;
6458a1b9b6aSSam Leffler 		u_int8_t rssi;
6468a1b9b6aSSam Leffler 
6471a1e1d21SSam Leffler 		/*
6481a1e1d21SSam Leffler 		 * The passive scan to look for existing AP's completed,
6491a1e1d21SSam Leffler 		 * select a channel to camp on.  Identify the channels
6501a1e1d21SSam Leffler 		 * that already have one or more AP's and try to locate
651acc4f7f5SSam Leffler 		 * an unoccupied one.  If that fails, pick a channel that
6528a1b9b6aSSam Leffler 		 * looks to be quietest.
6531a1e1d21SSam Leffler 		 */
6548a1b9b6aSSam Leffler 		memset(maxrssi, 0, sizeof(maxrssi));
6553fcfbbfaSSam Leffler 		IEEE80211_NODE_LOCK(nt);
6563fcfbbfaSSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
6578a1b9b6aSSam Leffler 			rssi = ic->ic_node_getrssi(ni);
6588a1b9b6aSSam Leffler 			i = ieee80211_chan2ieee(ic, ni->ni_chan);
6598a1b9b6aSSam Leffler 			if (rssi > maxrssi[i])
6608a1b9b6aSSam Leffler 				maxrssi[i] = rssi;
6611a1e1d21SSam Leffler 		}
6623fcfbbfaSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
6638a1b9b6aSSam Leffler 		/* XXX select channel more intelligently */
6648a1b9b6aSSam Leffler 		bestchan = -1;
6651a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
6668a1b9b6aSSam Leffler 			if (isset(ic->ic_chan_active, i)) {
6678a1b9b6aSSam Leffler 				/*
6688a1b9b6aSSam Leffler 				 * If the channel is unoccupied the max rssi
6698a1b9b6aSSam Leffler 				 * should be zero; just take it.  Otherwise
6708a1b9b6aSSam Leffler 				 * track the channel with the lowest rssi and
6718a1b9b6aSSam Leffler 				 * use that when all channels appear occupied.
6728a1b9b6aSSam Leffler 				 */
6738a1b9b6aSSam Leffler 				if (maxrssi[i] == 0) {
6748a1b9b6aSSam Leffler 					bestchan = i;
6751a1e1d21SSam Leffler 					break;
6761a1e1d21SSam Leffler 				}
6776edf09a6SSam Leffler 				if (bestchan == -1 ||
6786edf09a6SSam Leffler 				    maxrssi[i] < maxrssi[bestchan])
6798a1b9b6aSSam Leffler 					bestchan = i;
6808a1b9b6aSSam Leffler 			}
6818a1b9b6aSSam Leffler 		if (bestchan != -1) {
6828a1b9b6aSSam Leffler 			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
6831a1e1d21SSam Leffler 			return;
6841a1e1d21SSam Leffler 		}
6858a1b9b6aSSam Leffler 		/* no suitable channel, should not happen */
6868a1b9b6aSSam Leffler 	}
6878a1b9b6aSSam Leffler 
6888a1b9b6aSSam Leffler 	/*
6898a1b9b6aSSam Leffler 	 * When manually sequencing the state machine; scan just once
6908a1b9b6aSSam Leffler 	 * regardless of whether we have a candidate or not.  The
6918a1b9b6aSSam Leffler 	 * controlling application is expected to setup state and
6928a1b9b6aSSam Leffler 	 * initiate an association.
6938a1b9b6aSSam Leffler 	 */
6948a1b9b6aSSam Leffler 	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
6958a1b9b6aSSam Leffler 		return;
6968a1b9b6aSSam Leffler 	/*
6978a1b9b6aSSam Leffler 	 * Automatic sequencing; look for a candidate and
6988a1b9b6aSSam Leffler 	 * if found join the network.
6998a1b9b6aSSam Leffler 	 */
7003fcfbbfaSSam Leffler 	/* NB: unlocked read should be ok */
7013fcfbbfaSSam Leffler 	if (TAILQ_FIRST(&nt->nt_node) == NULL) {
7028a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
7038a1b9b6aSSam Leffler 			"%s: no scan candidate\n", __func__);
7041a1e1d21SSam Leffler   notfound:
7051a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
7061a1e1d21SSam Leffler 		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
7071a1e1d21SSam Leffler 		    ic->ic_des_esslen != 0) {
7081a1e1d21SSam Leffler 			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
7091a1e1d21SSam Leffler 			return;
7101a1e1d21SSam Leffler 		}
7111a1e1d21SSam Leffler 		/*
712936f15d2SSam Leffler 		 * Decrement the failure counts so entries will be
713936f15d2SSam Leffler 		 * reconsidered the next time around.  We really want
714936f15d2SSam Leffler 		 * to do this only for sta's where we've previously
7151fd2349dSSam Leffler 		 * had some success.
716936f15d2SSam Leffler 		 */
717936f15d2SSam Leffler 		IEEE80211_NODE_LOCK(nt);
718936f15d2SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
719936f15d2SSam Leffler 			if (ni->ni_fails)
720936f15d2SSam Leffler 				ni->ni_fails--;
721936f15d2SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
722936f15d2SSam Leffler 		/*
7231a1e1d21SSam Leffler 		 * Reset the list of channels to scan and start again.
7241a1e1d21SSam Leffler 		 */
7258a1b9b6aSSam Leffler 		ieee80211_reset_scan(ic);
7268a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SCAN;
7278a1b9b6aSSam Leffler 		ieee80211_next_scan(ic);
7281a1e1d21SSam Leffler 		return;
7291a1e1d21SSam Leffler 	}
7301a1e1d21SSam Leffler 	selbs = NULL;
7318a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
7328a1b9b6aSSam Leffler 	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
7333fcfbbfaSSam Leffler 	IEEE80211_NODE_LOCK(nt);
7343fcfbbfaSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
7358a1b9b6aSSam Leffler 		if (ieee80211_match_bss(ic, ni) == 0) {
7361a1e1d21SSam Leffler 			if (selbs == NULL)
7371a1e1d21SSam Leffler 				selbs = ni;
7383fcfbbfaSSam Leffler 			else if (ieee80211_node_compare(ic, ni, selbs) > 0)
7391a1e1d21SSam Leffler 				selbs = ni;
7401a1e1d21SSam Leffler 		}
7411a1e1d21SSam Leffler 	}
7423fcfbbfaSSam Leffler 	if (selbs != NULL)		/* NB: grab ref while dropping lock */
7433fcfbbfaSSam Leffler 		(void) ieee80211_ref_node(selbs);
7443fcfbbfaSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
7451a1e1d21SSam Leffler 	if (selbs == NULL)
7461a1e1d21SSam Leffler 		goto notfound;
7478a1b9b6aSSam Leffler 	if (!ieee80211_sta_join(ic, selbs)) {
7483fcfbbfaSSam Leffler 		ieee80211_free_node(selbs);
7491a1e1d21SSam Leffler 		goto notfound;
7501a1e1d21SSam Leffler 	}
7518a1b9b6aSSam Leffler }
7528a1b9b6aSSam Leffler 
753750d6d0cSSam Leffler /*
7548a1b9b6aSSam Leffler  * Handle 802.11 ad hoc network merge.  The
7558a1b9b6aSSam Leffler  * convention, set by the Wireless Ethernet Compatibility Alliance
7568a1b9b6aSSam Leffler  * (WECA), is that an 802.11 station will change its BSSID to match
7578a1b9b6aSSam Leffler  * the "oldest" 802.11 ad hoc network, on the same channel, that
7588a1b9b6aSSam Leffler  * has the station's desired SSID.  The "oldest" 802.11 network
7598a1b9b6aSSam Leffler  * sends beacons with the greatest TSF timestamp.
7608a1b9b6aSSam Leffler  *
7618a1b9b6aSSam Leffler  * The caller is assumed to validate TSF's before attempting a merge.
7628a1b9b6aSSam Leffler  *
7638a1b9b6aSSam Leffler  * Return !0 if the BSSID changed, 0 otherwise.
764750d6d0cSSam Leffler  */
7658a1b9b6aSSam Leffler int
766641b4d0bSSam Leffler ieee80211_ibss_merge(struct ieee80211_node *ni)
7678a1b9b6aSSam Leffler {
768641b4d0bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
7698a1b9b6aSSam Leffler 
77096acc1b6SSam Leffler 	if (ni == ic->ic_bss ||
77196acc1b6SSam Leffler 	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
7728a1b9b6aSSam Leffler 		/* unchanged, nothing to do */
7738a1b9b6aSSam Leffler 		return 0;
7748a1b9b6aSSam Leffler 	}
7758a1b9b6aSSam Leffler 	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
7768a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
7778a1b9b6aSSam Leffler 		    "%s: merge failed, capabilities mismatch\n", __func__);
7788a1b9b6aSSam Leffler 		ic->ic_stats.is_ibss_capmismatch++;
7798a1b9b6aSSam Leffler 		return 0;
7808a1b9b6aSSam Leffler 	}
7818a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
7828a1b9b6aSSam Leffler 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
7838a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
7848a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
7858a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
7868a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
7878a1b9b6aSSam Leffler 	);
788acc4f7f5SSam Leffler 	return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
7898a1b9b6aSSam Leffler }
7908a1b9b6aSSam Leffler 
7918a1b9b6aSSam Leffler /*
7928a1b9b6aSSam Leffler  * Join the specified IBSS/BSS network.  The node is assumed to
7938a1b9b6aSSam Leffler  * be passed in with a held reference.
7948a1b9b6aSSam Leffler  */
7958a1b9b6aSSam Leffler int
7968a1b9b6aSSam Leffler ieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
7978a1b9b6aSSam Leffler {
7988a1b9b6aSSam Leffler 	struct ieee80211_node *obss;
7998a1b9b6aSSam Leffler 
8008a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
801acc4f7f5SSam Leffler 		struct ieee80211_node_table *nt;
8028a1b9b6aSSam Leffler 		/*
803acc4f7f5SSam Leffler 		 * Fillin the neighbor table; it will already
8043d073929SSam Leffler 		 * exist if we are simply switching mastership.
805acc4f7f5SSam Leffler 		 * XXX ic_sta always setup so this is unnecessary?
8068a1b9b6aSSam Leffler 		 */
807acc4f7f5SSam Leffler 		nt = &ic->ic_sta;
808acc4f7f5SSam Leffler 		IEEE80211_NODE_LOCK(nt);
809acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
810acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
811acc4f7f5SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
8123d073929SSam Leffler 	}
8131a1e1d21SSam Leffler 
8148a1b9b6aSSam Leffler 	/*
8158a1b9b6aSSam Leffler 	 * Committed to selbs, setup state.
8168a1b9b6aSSam Leffler 	 */
8178a1b9b6aSSam Leffler 	obss = ic->ic_bss;
818acc4f7f5SSam Leffler 	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
8191fd2349dSSam Leffler 	if (obss != NULL) {
8201fd2349dSSam Leffler 		copy_bss(selbs, obss);
8218a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
8221fd2349dSSam Leffler 	}
82379edaebfSSam Leffler 
82479edaebfSSam Leffler 	/*
82579edaebfSSam Leffler 	 * Delete unusable rates; we've already checked
82679edaebfSSam Leffler 	 * that the negotiated rate set is acceptable.
82779edaebfSSam Leffler 	 */
82879edaebfSSam Leffler 	ieee80211_fix_rate(ic->ic_bss, IEEE80211_F_DODEL | IEEE80211_F_JOIN);
82979edaebfSSam Leffler 
8308a1b9b6aSSam Leffler 	/*
8318a1b9b6aSSam Leffler 	 * Set the erp state (mostly the slot time) to deal with
8328a1b9b6aSSam Leffler 	 * the auto-select case; this should be redundant if the
8338a1b9b6aSSam Leffler 	 * mode is locked.
8348a1b9b6aSSam Leffler 	 */
8358a1b9b6aSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
836b5c99415SSam Leffler 	ic->ic_curchan = selbs->ni_chan;
8378a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
8388a1b9b6aSSam Leffler 	ieee80211_wme_initparams(ic);
839acc4f7f5SSam Leffler 
840acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA)
8418a1b9b6aSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
842acc4f7f5SSam Leffler 	else
843acc4f7f5SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
8448a1b9b6aSSam Leffler 	return 1;
8458a1b9b6aSSam Leffler }
8468a1b9b6aSSam Leffler 
8478a1b9b6aSSam Leffler /*
8488a1b9b6aSSam Leffler  * Leave the specified IBSS/BSS network.  The node is assumed to
8498a1b9b6aSSam Leffler  * be passed in with a held reference.
8508a1b9b6aSSam Leffler  */
8518a1b9b6aSSam Leffler void
8528a1b9b6aSSam Leffler ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
8538a1b9b6aSSam Leffler {
8548a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
8558a1b9b6aSSam Leffler 	ieee80211_notify_node_leave(ic, ni);
8568a1b9b6aSSam Leffler }
8578a1b9b6aSSam Leffler 
8581a1e1d21SSam Leffler static struct ieee80211_node *
8598a1b9b6aSSam Leffler node_alloc(struct ieee80211_node_table *nt)
8601a1e1d21SSam Leffler {
861410ca74bSSam Leffler 	struct ieee80211_node *ni;
8628a1b9b6aSSam Leffler 
863410ca74bSSam Leffler 	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
864410ca74bSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
865410ca74bSSam Leffler 	return ni;
8661a1e1d21SSam Leffler }
8671a1e1d21SSam Leffler 
8688a1b9b6aSSam Leffler /*
8698a1b9b6aSSam Leffler  * Reclaim any resources in a node and reset any critical
8708a1b9b6aSSam Leffler  * state.  Typically nodes are free'd immediately after,
8718a1b9b6aSSam Leffler  * but in some cases the storage may be reused so we need
8728a1b9b6aSSam Leffler  * to insure consistent state (should probably fix that).
8738a1b9b6aSSam Leffler  */
8741a1e1d21SSam Leffler static void
8758a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni)
8761a1e1d21SSam Leffler {
8778a1b9b6aSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
8788a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
8798a1b9b6aSSam Leffler 	int i, qlen;
8808a1b9b6aSSam Leffler 
8818a1b9b6aSSam Leffler 	/* NB: preserve ni_table */
8828a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
8838a1b9b6aSSam Leffler 		ic->ic_ps_sta--;
8848a1b9b6aSSam Leffler 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
8858a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
8868a1b9b6aSSam Leffler 		    "[%s] power save mode off, %u sta's in ps mode\n",
8878a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
8888a1b9b6aSSam Leffler 	}
889ebdda46cSSam Leffler 	/*
890ebdda46cSSam Leffler 	 * Clear AREF flag that marks the authorization refcnt bump
891ebdda46cSSam Leffler 	 * has happened.  This is probably not needed as the node
892ebdda46cSSam Leffler 	 * should always be removed from the table so not found but
893ebdda46cSSam Leffler 	 * do it just in case.
894ebdda46cSSam Leffler 	 */
895ebdda46cSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AREF;
8968a1b9b6aSSam Leffler 
8978a1b9b6aSSam Leffler 	/*
8988a1b9b6aSSam Leffler 	 * Drain power save queue and, if needed, clear TIM.
8998a1b9b6aSSam Leffler 	 */
9008a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
9018a1b9b6aSSam Leffler 	if (qlen != 0 && ic->ic_set_tim != NULL)
902edfa57d0SSam Leffler 		ic->ic_set_tim(ni, 0);
9038a1b9b6aSSam Leffler 
9048a1b9b6aSSam Leffler 	ni->ni_associd = 0;
9058a1b9b6aSSam Leffler 	if (ni->ni_challenge != NULL) {
9068a1b9b6aSSam Leffler 		FREE(ni->ni_challenge, M_DEVBUF);
9078a1b9b6aSSam Leffler 		ni->ni_challenge = NULL;
9088a1b9b6aSSam Leffler 	}
9098a1b9b6aSSam Leffler 	/*
9108a1b9b6aSSam Leffler 	 * Preserve SSID, WPA, and WME ie's so the bss node is
9118a1b9b6aSSam Leffler 	 * reusable during a re-auth/re-assoc state transition.
9128a1b9b6aSSam Leffler 	 * If we remove these data they will not be recreated
9138a1b9b6aSSam Leffler 	 * because they come from a probe-response or beacon frame
9148a1b9b6aSSam Leffler 	 * which cannot be expected prior to the association-response.
9158a1b9b6aSSam Leffler 	 * This should not be an issue when operating in other modes
9168a1b9b6aSSam Leffler 	 * as stations leaving always go through a full state transition
9178a1b9b6aSSam Leffler 	 * which will rebuild this state.
9188a1b9b6aSSam Leffler 	 *
9198a1b9b6aSSam Leffler 	 * XXX does this leave us open to inheriting old state?
9208a1b9b6aSSam Leffler 	 */
9218a1b9b6aSSam Leffler 	for (i = 0; i < N(ni->ni_rxfrag); i++)
9228a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[i] != NULL) {
9238a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[i]);
9248a1b9b6aSSam Leffler 			ni->ni_rxfrag[i] = NULL;
9258a1b9b6aSSam Leffler 		}
926c1225b52SSam Leffler 	/*
927c1225b52SSam Leffler 	 * Must be careful here to remove any key map entry w/o a LOR.
928c1225b52SSam Leffler 	 */
929c1225b52SSam Leffler 	ieee80211_node_delucastkey(ni);
9308a1b9b6aSSam Leffler #undef N
9318a1b9b6aSSam Leffler }
9328a1b9b6aSSam Leffler 
9338a1b9b6aSSam Leffler static void
9348a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni)
9358a1b9b6aSSam Leffler {
9368a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
9378a1b9b6aSSam Leffler 
9388a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
9398a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
9408a1b9b6aSSam Leffler 		FREE(ni->ni_wpa_ie, M_DEVBUF);
9418a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
9428a1b9b6aSSam Leffler 		FREE(ni->ni_wme_ie, M_DEVBUF);
9438a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DESTROY(ni);
944410ca74bSSam Leffler 	FREE(ni, M_80211_NODE);
9451a1e1d21SSam Leffler }
9461a1e1d21SSam Leffler 
947d1e61976SSam Leffler static u_int8_t
9488a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni)
949d1e61976SSam Leffler {
950d1e61976SSam Leffler 	return ni->ni_rssi;
951d1e61976SSam Leffler }
952d1e61976SSam Leffler 
9531a1e1d21SSam Leffler static void
9548a1b9b6aSSam Leffler ieee80211_setup_node(struct ieee80211_node_table *nt,
9558a1b9b6aSSam Leffler 	struct ieee80211_node *ni, const u_int8_t *macaddr)
9561a1e1d21SSam Leffler {
9578a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9581a1e1d21SSam Leffler 	int hash;
9591a1e1d21SSam Leffler 
9608a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
96149a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
9628a1b9b6aSSam Leffler 		ether_sprintf(macaddr), nt->nt_name);
9638a1b9b6aSSam Leffler 
9641a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
9651a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
9668a1b9b6aSSam Leffler 	ieee80211_node_initref(ni);		/* mark referenced */
9678a1b9b6aSSam Leffler 	ni->ni_chan = IEEE80211_CHAN_ANYC;
9688a1b9b6aSSam Leffler 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
9698a1b9b6aSSam Leffler 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
9708a1b9b6aSSam Leffler 	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
9712045f699SSam Leffler 	ni->ni_inact_reload = nt->nt_inact_init;
9722045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
9738a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
9748a1b9b6aSSam Leffler 
9758a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
9768a1b9b6aSSam Leffler 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
9778a1b9b6aSSam Leffler 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
9788a1b9b6aSSam Leffler 	ni->ni_table = nt;
9798a1b9b6aSSam Leffler 	ni->ni_ic = ic;
9808a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
9811a1e1d21SSam Leffler }
9821a1e1d21SSam Leffler 
9831a1e1d21SSam Leffler struct ieee80211_node *
9848a1b9b6aSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
9851a1e1d21SSam Leffler {
9868a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9878a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9888a1b9b6aSSam Leffler 
9898a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
9901a1e1d21SSam Leffler 	if (ni != NULL)
9918a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
992c64bfa0fSSam Leffler 	else
993c64bfa0fSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
9941a1e1d21SSam Leffler 	return ni;
9951a1e1d21SSam Leffler }
9961a1e1d21SSam Leffler 
99797c973adSSam Leffler /*
99897c973adSSam Leffler  * Craft a temporary node suitable for sending a management frame
99997c973adSSam Leffler  * to the specified station.  We craft only as much state as we
100097c973adSSam Leffler  * need to do the work since the node will be immediately reclaimed
100197c973adSSam Leffler  * once the send completes.
100297c973adSSam Leffler  */
100397c973adSSam Leffler struct ieee80211_node *
100497c973adSSam Leffler ieee80211_tmp_node(struct ieee80211com *ic, const u_int8_t *macaddr)
100597c973adSSam Leffler {
100697c973adSSam Leffler 	struct ieee80211_node *ni;
100797c973adSSam Leffler 
100897c973adSSam Leffler 	ni = ic->ic_node_alloc(&ic->ic_sta);
100997c973adSSam Leffler 	if (ni != NULL) {
101097c973adSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
101197c973adSSam Leffler 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
101297c973adSSam Leffler 
101397c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
101497c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
101597c973adSSam Leffler 		ieee80211_node_initref(ni);		/* mark referenced */
101697c973adSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
101797c973adSSam Leffler 		/* NB: required by ieee80211_fix_rate */
101897c973adSSam Leffler 		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
101997c973adSSam Leffler 		ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
102097c973adSSam Leffler 			IEEE80211_KEYIX_NONE);
102197c973adSSam Leffler 		/* XXX optimize away */
102297c973adSSam Leffler 		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
102397c973adSSam Leffler 
102497c973adSSam Leffler 		ni->ni_table = NULL;		/* NB: pedantic */
102597c973adSSam Leffler 		ni->ni_ic = ic;
102697c973adSSam Leffler 	} else {
102797c973adSSam Leffler 		/* XXX msg */
102897c973adSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
102997c973adSSam Leffler 	}
103097c973adSSam Leffler 	return ni;
103197c973adSSam Leffler }
103297c973adSSam Leffler 
10331a1e1d21SSam Leffler struct ieee80211_node *
10348a1b9b6aSSam Leffler ieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
10351a1e1d21SSam Leffler {
10368a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
10378a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10388a1b9b6aSSam Leffler 
10398a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
10401a1e1d21SSam Leffler 	if (ni != NULL) {
10418a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
1042694dca64SSam Leffler 		/*
1043694dca64SSam Leffler 		 * Inherit from ic_bss.
1044694dca64SSam Leffler 		 */
10458a1b9b6aSSam Leffler 		ni->ni_authmode = ic->ic_bss->ni_authmode;
10468a1b9b6aSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
10478a1b9b6aSSam Leffler 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
1048694dca64SSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
10498a1b9b6aSSam Leffler 		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
10508a1b9b6aSSam Leffler 		ni->ni_rsn = ic->ic_bss->ni_rsn;
1051694dca64SSam Leffler 	} else
1052694dca64SSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
10531a1e1d21SSam Leffler 	return ni;
10541a1e1d21SSam Leffler }
10551a1e1d21SSam Leffler 
1056750d6d0cSSam Leffler static struct ieee80211_node *
10578a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10588a1b9b6aSSam Leffler _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
10598a1b9b6aSSam Leffler 	const u_int8_t *macaddr, const char *func, int line)
10608a1b9b6aSSam Leffler #else
10618a1b9b6aSSam Leffler _ieee80211_find_node(struct ieee80211_node_table *nt,
10628a1b9b6aSSam Leffler 	const u_int8_t *macaddr)
10638a1b9b6aSSam Leffler #endif
10641a1e1d21SSam Leffler {
10651a1e1d21SSam Leffler 	struct ieee80211_node *ni;
10661a1e1d21SSam Leffler 	int hash;
10671a1e1d21SSam Leffler 
10688a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
1069750d6d0cSSam Leffler 
10701a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
10718a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
10721a1e1d21SSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
10738a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
10748a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10758a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
107649a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
107749a15236SSam Leffler 			    func, line,
107849a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
10798a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
10808a1b9b6aSSam Leffler #endif
1081750d6d0cSSam Leffler 			return ni;
10821a1e1d21SSam Leffler 		}
10831a1e1d21SSam Leffler 	}
1084750d6d0cSSam Leffler 	return NULL;
1085750d6d0cSSam Leffler }
10868a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10878a1b9b6aSSam Leffler #define	_ieee80211_find_node(nt, mac) \
10888a1b9b6aSSam Leffler 	_ieee80211_find_node_debug(nt, mac, func, line)
10898a1b9b6aSSam Leffler #endif
1090750d6d0cSSam Leffler 
1091750d6d0cSSam Leffler struct ieee80211_node *
10928a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10938a1b9b6aSSam Leffler ieee80211_find_node_debug(struct ieee80211_node_table *nt,
10948a1b9b6aSSam Leffler 	const u_int8_t *macaddr, const char *func, int line)
10958a1b9b6aSSam Leffler #else
10968a1b9b6aSSam Leffler ieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
10978a1b9b6aSSam Leffler #endif
1098750d6d0cSSam Leffler {
1099750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1100750d6d0cSSam Leffler 
11018a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
11028a1b9b6aSSam Leffler 	ni = _ieee80211_find_node(nt, macaddr);
11038a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
11041a1e1d21SSam Leffler 	return ni;
11051a1e1d21SSam Leffler }
11061a1e1d21SSam Leffler 
11071a1e1d21SSam Leffler /*
11088a1b9b6aSSam Leffler  * Fake up a node; this handles node discovery in adhoc mode.
11098a1b9b6aSSam Leffler  * Note that for the driver's benefit we we treat this like
11108a1b9b6aSSam Leffler  * an association so the driver has an opportunity to setup
11118a1b9b6aSSam Leffler  * it's private state.
11128a1b9b6aSSam Leffler  */
11138a1b9b6aSSam Leffler struct ieee80211_node *
11148a1b9b6aSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
11158a1b9b6aSSam Leffler 	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
11168a1b9b6aSSam Leffler {
11178a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
11188a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
11198a1b9b6aSSam Leffler 
1120be425a0fSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1121be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
11228a1b9b6aSSam Leffler 	ni = ieee80211_dup_bss(nt, macaddr);
11238a1b9b6aSSam Leffler 	if (ni != NULL) {
11248a1b9b6aSSam Leffler 		/* XXX no rate negotiation; just dup */
11258a1b9b6aSSam Leffler 		ni->ni_rates = ic->ic_bss->ni_rates;
1126736b3dc3SSam Leffler 		if (ic->ic_newassoc != NULL)
1127e9962332SSam Leffler 			ic->ic_newassoc(ni, 1);
11288a1b9b6aSSam Leffler 		/* XXX not right for 802.1x/WPA */
1129e4918ecdSSam Leffler 		ieee80211_node_authorize(ni);
11308e292e8eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
11318e292e8eSSam Leffler 			/*
11328e292e8eSSam Leffler 			 * Blindly propagate capabilities based on the
11338e292e8eSSam Leffler 			 * local configuration.  In particular this permits
11348e292e8eSSam Leffler 			 * us to use QoS to disable ACK's.
11358e292e8eSSam Leffler 			 */
11368e292e8eSSam Leffler 			if (ic->ic_flags & IEEE80211_F_WME)
11378e292e8eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_QOS;
11388e292e8eSSam Leffler 		}
11398a1b9b6aSSam Leffler 	}
11408a1b9b6aSSam Leffler 	return ni;
11418a1b9b6aSSam Leffler }
11428a1b9b6aSSam Leffler 
1143b5c99415SSam Leffler #ifdef IEEE80211_DEBUG
1144b5c99415SSam Leffler static void
1145b5c99415SSam Leffler dump_probe_beacon(u_int8_t subtype, int isnew,
1146b5c99415SSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN],
1147b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp)
1148b5c99415SSam Leffler {
1149b5c99415SSam Leffler 
1150b5c99415SSam Leffler 	printf("[%s] %s%s on chan %u (bss chan %u) ",
1151b5c99415SSam Leffler 	    ether_sprintf(mac), isnew ? "new " : "",
1152b5c99415SSam Leffler 	    ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
1153b5c99415SSam Leffler 	    sp->chan, sp->bchan);
1154b5c99415SSam Leffler 	ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
1155b5c99415SSam Leffler 	printf("\n");
1156b5c99415SSam Leffler 
1157b5c99415SSam Leffler 	if (isnew) {
1158b5c99415SSam Leffler 		printf("[%s] caps 0x%x bintval %u erp 0x%x",
1159b5c99415SSam Leffler 			ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
1160b5c99415SSam Leffler 		if (sp->country != NULL) {
1161b5c99415SSam Leffler #ifdef __FreeBSD__
1162b5c99415SSam Leffler 			printf(" country info %*D",
1163b5c99415SSam Leffler 				sp->country[1], sp->country+2, " ");
1164b5c99415SSam Leffler #else
1165b5c99415SSam Leffler 			int i;
1166b5c99415SSam Leffler 			printf(" country info");
1167b5c99415SSam Leffler 			for (i = 0; i < sp->country[1]; i++)
1168b5c99415SSam Leffler 				printf(" %02x", sp->country[i+2]);
1169b5c99415SSam Leffler #endif
1170b5c99415SSam Leffler 		}
1171b5c99415SSam Leffler 		printf("\n");
1172b5c99415SSam Leffler 	}
1173b5c99415SSam Leffler }
1174b5c99415SSam Leffler #endif /* IEEE80211_DEBUG */
1175b5c99415SSam Leffler 
1176b5c99415SSam Leffler static void
1177b5c99415SSam Leffler saveie(u_int8_t **iep, const u_int8_t *ie)
1178b5c99415SSam Leffler {
1179b5c99415SSam Leffler 
1180b5c99415SSam Leffler 	if (ie == NULL)
1181b5c99415SSam Leffler 		*iep = NULL;
1182b5c99415SSam Leffler 	else
1183b5c99415SSam Leffler 		ieee80211_saveie(iep, ie);
1184b5c99415SSam Leffler }
1185b5c99415SSam Leffler 
1186b5c99415SSam Leffler /*
1187b5c99415SSam Leffler  * Process a beacon or probe response frame.
1188b5c99415SSam Leffler  */
1189b5c99415SSam Leffler void
1190b5c99415SSam Leffler ieee80211_add_scan(struct ieee80211com *ic,
1191b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp,
1192b5c99415SSam Leffler 	const struct ieee80211_frame *wh,
1193b5c99415SSam Leffler 	int subtype, int rssi, int rstamp)
1194b5c99415SSam Leffler {
1195b5c99415SSam Leffler #define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1196b5c99415SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_scan;
1197b5c99415SSam Leffler 	struct ieee80211_node *ni;
1198b5c99415SSam Leffler 	int newnode = 0;
1199b5c99415SSam Leffler 
1200b5c99415SSam Leffler 	ni = ieee80211_find_node(nt, wh->i_addr2);
1201b5c99415SSam Leffler 	if (ni == NULL) {
1202b5c99415SSam Leffler 		/*
1203b5c99415SSam Leffler 		 * Create a new entry.
1204b5c99415SSam Leffler 		 */
1205b5c99415SSam Leffler 		ni = ic->ic_node_alloc(nt);
1206b5c99415SSam Leffler 		if (ni == NULL) {
1207b5c99415SSam Leffler 			ic->ic_stats.is_rx_nodealloc++;
1208b5c99415SSam Leffler 			return;
1209b5c99415SSam Leffler 		}
1210b5c99415SSam Leffler 		ieee80211_setup_node(nt, ni, wh->i_addr2);
1211b5c99415SSam Leffler 		/*
1212b5c99415SSam Leffler 		 * XXX inherit from ic_bss.
1213b5c99415SSam Leffler 		 */
1214b5c99415SSam Leffler 		ni->ni_authmode = ic->ic_bss->ni_authmode;
1215b5c99415SSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
1216b5c99415SSam Leffler 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
1217b5c99415SSam Leffler 		ieee80211_set_chan(ic, ni, ic->ic_curchan);
1218b5c99415SSam Leffler 		ni->ni_rsn = ic->ic_bss->ni_rsn;
1219b5c99415SSam Leffler 		newnode = 1;
1220b5c99415SSam Leffler 	}
1221b5c99415SSam Leffler #ifdef IEEE80211_DEBUG
1222b5c99415SSam Leffler 	if (ieee80211_msg_scan(ic) && (ic->ic_flags & IEEE80211_F_SCAN))
1223b5c99415SSam Leffler 		dump_probe_beacon(subtype, newnode, wh->i_addr2, sp);
1224b5c99415SSam Leffler #endif
1225b5c99415SSam Leffler 	/* XXX ap beaconing multiple ssid w/ same bssid */
1226b5c99415SSam Leffler 	if (sp->ssid[1] != 0 &&
1227b5c99415SSam Leffler 	    (ISPROBE(subtype) || ni->ni_esslen == 0)) {
1228b5c99415SSam Leffler 		ni->ni_esslen = sp->ssid[1];
1229b5c99415SSam Leffler 		memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
1230b5c99415SSam Leffler 		memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1231b5c99415SSam Leffler 	}
1232b5c99415SSam Leffler 	ni->ni_scangen = ic->ic_scan.nt_scangen;
1233b5c99415SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1234b5c99415SSam Leffler 	ni->ni_rssi = rssi;
1235b5c99415SSam Leffler 	ni->ni_rstamp = rstamp;
1236b5c99415SSam Leffler 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1237b5c99415SSam Leffler 	ni->ni_intval = sp->bintval;
1238b5c99415SSam Leffler 	ni->ni_capinfo = sp->capinfo;
1239b5c99415SSam Leffler 	ni->ni_chan = &ic->ic_channels[sp->chan];
1240b5c99415SSam Leffler 	ni->ni_fhdwell = sp->fhdwell;
1241b5c99415SSam Leffler 	ni->ni_fhindex = sp->fhindex;
1242b5c99415SSam Leffler 	ni->ni_erp = sp->erp;
1243b5c99415SSam Leffler 	if (sp->tim != NULL) {
1244b5c99415SSam Leffler 		struct ieee80211_tim_ie *ie =
1245b5c99415SSam Leffler 		    (struct ieee80211_tim_ie *) sp->tim;
1246b5c99415SSam Leffler 
1247b5c99415SSam Leffler 		ni->ni_dtim_count = ie->tim_count;
1248b5c99415SSam Leffler 		ni->ni_dtim_period = ie->tim_period;
1249b5c99415SSam Leffler 	}
1250b5c99415SSam Leffler 	/*
1251b5c99415SSam Leffler 	 * Record the byte offset from the mac header to
1252b5c99415SSam Leffler 	 * the start of the TIM information element for
1253b5c99415SSam Leffler 	 * use by hardware and/or to speedup software
1254b5c99415SSam Leffler 	 * processing of beacon frames.
1255b5c99415SSam Leffler 	 */
1256b5c99415SSam Leffler 	ni->ni_timoff = sp->timoff;
1257b5c99415SSam Leffler 	/*
1258b5c99415SSam Leffler 	 * Record optional information elements that might be
1259b5c99415SSam Leffler 	 * used by applications or drivers.
1260b5c99415SSam Leffler 	 */
1261b5c99415SSam Leffler 	saveie(&ni->ni_wme_ie, sp->wme);
1262b5c99415SSam Leffler 	saveie(&ni->ni_wpa_ie, sp->wpa);
1263b5c99415SSam Leffler 
1264b5c99415SSam Leffler 	/* NB: must be after ni_chan is setup */
1265b5c99415SSam Leffler 	ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT);
1266b5c99415SSam Leffler 
1267b5c99415SSam Leffler 	if (!newnode)
1268b5c99415SSam Leffler 		ieee80211_free_node(ni);
1269b5c99415SSam Leffler #undef ISPROBE
1270b5c99415SSam Leffler }
1271b5c99415SSam Leffler 
1272be425a0fSSam Leffler void
1273be425a0fSSam Leffler ieee80211_init_neighbor(struct ieee80211_node *ni,
1274be425a0fSSam Leffler 	const struct ieee80211_frame *wh,
1275be425a0fSSam Leffler 	const struct ieee80211_scanparams *sp)
1276be425a0fSSam Leffler {
1277be425a0fSSam Leffler 
1278be425a0fSSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1279be425a0fSSam Leffler 	    "%s: %p<%s>\n", __func__, ni, ether_sprintf(ni->ni_macaddr));
1280be425a0fSSam Leffler 	ni->ni_esslen = sp->ssid[1];
1281be425a0fSSam Leffler 	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1282be425a0fSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1283be425a0fSSam Leffler 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1284be425a0fSSam Leffler 	ni->ni_intval = sp->bintval;
1285be425a0fSSam Leffler 	ni->ni_capinfo = sp->capinfo;
1286be425a0fSSam Leffler 	ni->ni_chan = ni->ni_ic->ic_curchan;
1287be425a0fSSam Leffler 	ni->ni_fhdwell = sp->fhdwell;
1288be425a0fSSam Leffler 	ni->ni_fhindex = sp->fhindex;
1289be425a0fSSam Leffler 	ni->ni_erp = sp->erp;
1290be425a0fSSam Leffler 	ni->ni_timoff = sp->timoff;
1291be425a0fSSam Leffler 	if (sp->wme != NULL)
1292be425a0fSSam Leffler 		ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
1293be425a0fSSam Leffler 	if (sp->wpa != NULL)
1294be425a0fSSam Leffler 		ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
1295be425a0fSSam Leffler 
1296be425a0fSSam Leffler 	/* NB: must be after ni_chan is setup */
129779edaebfSSam Leffler 	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
129879edaebfSSam Leffler 		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
129979edaebfSSam Leffler 		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1300be425a0fSSam Leffler }
1301be425a0fSSam Leffler 
1302b5c99415SSam Leffler /*
1303b5c99415SSam Leffler  * Do node discovery in adhoc mode on receipt of a beacon
1304b5c99415SSam Leffler  * or probe response frame.  Note that for the driver's
1305b5c99415SSam Leffler  * benefit we we treat this like an association so the
1306b5c99415SSam Leffler  * driver has an opportunity to setup it's private state.
1307b5c99415SSam Leffler  */
1308b5c99415SSam Leffler struct ieee80211_node *
1309b5c99415SSam Leffler ieee80211_add_neighbor(struct ieee80211com *ic,
1310b5c99415SSam Leffler 	const struct ieee80211_frame *wh,
1311b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp)
1312b5c99415SSam Leffler {
1313b5c99415SSam Leffler 	struct ieee80211_node *ni;
1314b5c99415SSam Leffler 
1315be425a0fSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1316be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1317b5c99415SSam Leffler 	ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1318b5c99415SSam Leffler 	if (ni != NULL) {
1319be425a0fSSam Leffler 		ieee80211_init_neighbor(ni, wh, sp);
1320b5c99415SSam Leffler 		if (ic->ic_newassoc != NULL)
1321b5c99415SSam Leffler 			ic->ic_newassoc(ni, 1);
1322b5c99415SSam Leffler 		/* XXX not right for 802.1x/WPA */
1323b5c99415SSam Leffler 		ieee80211_node_authorize(ni);
1324b5c99415SSam Leffler 	}
1325b5c99415SSam Leffler 	return ni;
1326b5c99415SSam Leffler }
1327b5c99415SSam Leffler 
1328c1225b52SSam Leffler #define	IS_CTL(wh) \
1329c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1330c1225b52SSam Leffler #define	IS_PSPOLL(wh) \
1331c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
13328a1b9b6aSSam Leffler /*
13338a1b9b6aSSam Leffler  * Locate the node for sender, track state, and then pass the
13348a1b9b6aSSam Leffler  * (referenced) node up to the 802.11 layer for its use.  We
13358a1b9b6aSSam Leffler  * are required to pass some node so we fall back to ic_bss
13368a1b9b6aSSam Leffler  * when this frame is from an unknown sender.  The 802.11 layer
13378a1b9b6aSSam Leffler  * knows this means the sender wasn't in the node table and
13388a1b9b6aSSam Leffler  * acts accordingly.
13398a1b9b6aSSam Leffler  */
13408a1b9b6aSSam Leffler struct ieee80211_node *
13418a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
13428a1b9b6aSSam Leffler ieee80211_find_rxnode_debug(struct ieee80211com *ic,
13438a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh, const char *func, int line)
13448a1b9b6aSSam Leffler #else
13458a1b9b6aSSam Leffler ieee80211_find_rxnode(struct ieee80211com *ic,
13468a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh)
13478a1b9b6aSSam Leffler #endif
13488a1b9b6aSSam Leffler {
13498a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
13508a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
13518a1b9b6aSSam Leffler 
13528a1b9b6aSSam Leffler 	/* XXX may want scanned nodes in the neighbor table for adhoc */
13538a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
13548a1b9b6aSSam Leffler 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
13558a1b9b6aSSam Leffler 	    (ic->ic_flags & IEEE80211_F_SCAN))
13568a1b9b6aSSam Leffler 		nt = &ic->ic_scan;
13578a1b9b6aSSam Leffler 	else
1358acc4f7f5SSam Leffler 		nt = &ic->ic_sta;
13598a1b9b6aSSam Leffler 	/* XXX check ic_bss first in station mode */
13608a1b9b6aSSam Leffler 	/* XXX 4-address frames? */
13618a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
13628a1b9b6aSSam Leffler 	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
13638a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr1);
13648a1b9b6aSSam Leffler 	else
13658a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr2);
1366c1225b52SSam Leffler 	if (ni == NULL)
1367c1225b52SSam Leffler 		ni = ieee80211_ref_node(ic->ic_bss);
13688a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
13698a1b9b6aSSam Leffler 
1370c1225b52SSam Leffler 	return ni;
1371c1225b52SSam Leffler }
1372c1225b52SSam Leffler 
1373c1225b52SSam Leffler /*
1374c1225b52SSam Leffler  * Like ieee80211_find_rxnode but use the supplied h/w
1375c1225b52SSam Leffler  * key index as a hint to locate the node in the key
1376c1225b52SSam Leffler  * mapping table.  If an entry is present at the key
1377c1225b52SSam Leffler  * index we return it; otherwise do a normal lookup and
1378c1225b52SSam Leffler  * update the mapping table if the station has a unicast
1379c1225b52SSam Leffler  * key assigned to it.
1380c1225b52SSam Leffler  */
1381c1225b52SSam Leffler struct ieee80211_node *
1382c1225b52SSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1383c1225b52SSam Leffler ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1384c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1385c1225b52SSam Leffler 	const char *func, int line)
1386c1225b52SSam Leffler #else
1387c1225b52SSam Leffler ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1388c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1389c1225b52SSam Leffler #endif
1390c1225b52SSam Leffler {
1391c1225b52SSam Leffler 	struct ieee80211_node_table *nt;
1392c1225b52SSam Leffler 	struct ieee80211_node *ni;
1393c1225b52SSam Leffler 
1394c1225b52SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
1395c1225b52SSam Leffler 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1396c1225b52SSam Leffler 	    (ic->ic_flags & IEEE80211_F_SCAN))
1397c1225b52SSam Leffler 		nt = &ic->ic_scan;
1398c1225b52SSam Leffler 	else
1399c1225b52SSam Leffler 		nt = &ic->ic_sta;
1400c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
1401c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1402c1225b52SSam Leffler 		ni = nt->nt_keyixmap[keyix];
1403c1225b52SSam Leffler 	else
1404c1225b52SSam Leffler 		ni = NULL;
1405c1225b52SSam Leffler 	if (ni == NULL) {
1406c1225b52SSam Leffler 		if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1407c1225b52SSam Leffler 			ni = _ieee80211_find_node(nt, wh->i_addr1);
1408c1225b52SSam Leffler 		else
1409c1225b52SSam Leffler 			ni = _ieee80211_find_node(nt, wh->i_addr2);
1410c1225b52SSam Leffler 		if (ni == NULL)
1411c1225b52SSam Leffler 			ni = ieee80211_ref_node(ic->ic_bss);
1412c1225b52SSam Leffler 		if (nt->nt_keyixmap != NULL) {
1413c1225b52SSam Leffler 			/*
1414c1225b52SSam Leffler 			 * If the station has a unicast key cache slot
1415c1225b52SSam Leffler 			 * assigned update the key->node mapping table.
1416c1225b52SSam Leffler 			 */
1417c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1418c1225b52SSam Leffler 			/* XXX can keyixmap[keyix] != NULL? */
1419c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1420c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == NULL) {
1421c1225b52SSam Leffler 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1422c1225b52SSam Leffler 				    "%s: add key map entry %p<%s> refcnt %d\n",
1423c1225b52SSam Leffler 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1424c1225b52SSam Leffler 				    ieee80211_node_refcnt(ni)+1);
1425c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1426c1225b52SSam Leffler 			}
1427c1225b52SSam Leffler 		}
1428c1225b52SSam Leffler 	} else {
1429c1225b52SSam Leffler 		ieee80211_ref_node(ni);
1430c1225b52SSam Leffler 	}
1431c1225b52SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1432c1225b52SSam Leffler 
1433c1225b52SSam Leffler 	return ni;
1434c1225b52SSam Leffler }
14358a1b9b6aSSam Leffler #undef IS_PSPOLL
14368a1b9b6aSSam Leffler #undef IS_CTL
14378a1b9b6aSSam Leffler 
14388a1b9b6aSSam Leffler /*
1439750d6d0cSSam Leffler  * Return a reference to the appropriate node for sending
1440750d6d0cSSam Leffler  * a data frame.  This handles node discovery in adhoc networks.
1441750d6d0cSSam Leffler  */
1442750d6d0cSSam Leffler struct ieee80211_node *
14438a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
14448a1b9b6aSSam Leffler ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
14458a1b9b6aSSam Leffler 	const char *func, int line)
14468a1b9b6aSSam Leffler #else
14478a1b9b6aSSam Leffler ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
14488a1b9b6aSSam Leffler #endif
1449750d6d0cSSam Leffler {
1450acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1451750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1452750d6d0cSSam Leffler 
1453750d6d0cSSam Leffler 	/*
1454750d6d0cSSam Leffler 	 * The destination address should be in the node table
1455c1225b52SSam Leffler 	 * unless this is a multicast/broadcast frame.  We can
1456c1225b52SSam Leffler 	 * also optimize station mode operation, all frames go
1457c1225b52SSam Leffler 	 * to the bss node.
1458750d6d0cSSam Leffler 	 */
1459750d6d0cSSam Leffler 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
14608a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1461c1225b52SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1462c1225b52SSam Leffler 		ni = ieee80211_ref_node(ic->ic_bss);
1463ee25b8dfSSam Leffler 	else {
14648a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, macaddr);
1465ee25b8dfSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1466ee25b8dfSSam Leffler 		    (ni != NULL && ni->ni_associd == 0)) {
1467ee25b8dfSSam Leffler 			/*
1468ee25b8dfSSam Leffler 			 * Station is not associated; don't permit the
1469ee25b8dfSSam Leffler 			 * data frame to be sent by returning NULL.  This
1470ee25b8dfSSam Leffler 			 * is kinda a kludge but the least intrusive way
1471ee25b8dfSSam Leffler 			 * to add this check into all drivers.
1472ee25b8dfSSam Leffler 			 */
1473ee25b8dfSSam Leffler 			ieee80211_unref_node(&ni);	/* NB: null's ni */
1474ee25b8dfSSam Leffler 		}
1475ee25b8dfSSam Leffler 	}
14768a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
14778a1b9b6aSSam Leffler 
14788a1b9b6aSSam Leffler 	if (ni == NULL) {
14798a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
148090d0d036SSam Leffler 		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
148190d0d036SSam Leffler 			/*
148290d0d036SSam Leffler 			 * In adhoc mode cons up a node for the destination.
148390d0d036SSam Leffler 			 * Note that we need an additional reference for the
148490d0d036SSam Leffler 			 * caller to be consistent with _ieee80211_find_node.
148590d0d036SSam Leffler 			 */
14868a1b9b6aSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
148790d0d036SSam Leffler 			if (ni != NULL)
148890d0d036SSam Leffler 				(void) ieee80211_ref_node(ni);
148990d0d036SSam Leffler 		} else {
14908a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
14918a1b9b6aSSam Leffler 				"[%s] no node, discard frame (%s)\n",
14928a1b9b6aSSam Leffler 				ether_sprintf(macaddr), __func__);
14938a1b9b6aSSam Leffler 			ic->ic_stats.is_tx_nonode++;
1494750d6d0cSSam Leffler 		}
1495750d6d0cSSam Leffler 	}
1496750d6d0cSSam Leffler 	return ni;
1497750d6d0cSSam Leffler }
1498750d6d0cSSam Leffler 
1499750d6d0cSSam Leffler /*
15001a1e1d21SSam Leffler  * Like find but search based on the channel too.
15011a1e1d21SSam Leffler  */
15021a1e1d21SSam Leffler struct ieee80211_node *
15038a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
15048a1b9b6aSSam Leffler ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
15058a1b9b6aSSam Leffler 	const u_int8_t *macaddr, struct ieee80211_channel *chan,
15068a1b9b6aSSam Leffler 	const char *func, int line)
15078a1b9b6aSSam Leffler #else
15088a1b9b6aSSam Leffler ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
15098a1b9b6aSSam Leffler 	const u_int8_t *macaddr, struct ieee80211_channel *chan)
15108a1b9b6aSSam Leffler #endif
15111a1e1d21SSam Leffler {
15121a1e1d21SSam Leffler 	struct ieee80211_node *ni;
15131a1e1d21SSam Leffler 	int hash;
15141a1e1d21SSam Leffler 
15151a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
15168a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
15178a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1518750d6d0cSSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1519750d6d0cSSam Leffler 		    ni->ni_chan == chan) {
15208a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);		/* mark referenced */
15218a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1522132142c5SDiomidis Spinellis 			    REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
15238a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
15241a1e1d21SSam Leffler 			break;
15251a1e1d21SSam Leffler 		}
15261a1e1d21SSam Leffler 	}
15278a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15281a1e1d21SSam Leffler 	return ni;
15291a1e1d21SSam Leffler }
15301a1e1d21SSam Leffler 
15318a1b9b6aSSam Leffler /*
15328a1b9b6aSSam Leffler  * Like find but search based on the ssid too.
15338a1b9b6aSSam Leffler  */
15348a1b9b6aSSam Leffler struct ieee80211_node *
15358a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
15368a1b9b6aSSam Leffler ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
15378a1b9b6aSSam Leffler 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
15388a1b9b6aSSam Leffler 	const char *func, int line)
15398a1b9b6aSSam Leffler #else
15408a1b9b6aSSam Leffler ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
15418a1b9b6aSSam Leffler 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
15428a1b9b6aSSam Leffler #endif
15431a1e1d21SSam Leffler {
1544f02a0bd2SSam Leffler #define	MATCH_SSID(ni, ssid, ssidlen) \
1545f02a0bd2SSam Leffler 	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1546f02a0bd2SSam Leffler 	static const u_int8_t zeromac[IEEE80211_ADDR_LEN];
15478a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
15481a1e1d21SSam Leffler 	struct ieee80211_node *ni;
15498a1b9b6aSSam Leffler 	int hash;
15501a1e1d21SSam Leffler 
15518a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1552f02a0bd2SSam Leffler 	/*
1553f02a0bd2SSam Leffler 	 * A mac address that is all zero means match only the ssid;
1554f02a0bd2SSam Leffler 	 * otherwise we must match both.
1555f02a0bd2SSam Leffler 	 */
1556f02a0bd2SSam Leffler 	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1557f02a0bd2SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1558f02a0bd2SSam Leffler 			if (MATCH_SSID(ni, ssid, ssidlen))
1559f02a0bd2SSam Leffler 				break;
1560f02a0bd2SSam Leffler 		}
1561f02a0bd2SSam Leffler 	} else {
1562f02a0bd2SSam Leffler 		hash = IEEE80211_NODE_HASH(macaddr);
15638a1b9b6aSSam Leffler 		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
15648a1b9b6aSSam Leffler 			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1565f02a0bd2SSam Leffler 			    MATCH_SSID(ni, ssid, ssidlen))
1566f02a0bd2SSam Leffler 				break;
1567f02a0bd2SSam Leffler 		}
1568f02a0bd2SSam Leffler 	}
1569f02a0bd2SSam Leffler 	if (ni != NULL) {
15708a1b9b6aSSam Leffler 		ieee80211_ref_node(ni);	/* mark referenced */
15718a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1572132142c5SDiomidis Spinellis 		     REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
15738a1b9b6aSSam Leffler 		     ieee80211_node_refcnt(ni));
15748a1b9b6aSSam Leffler 	}
15758a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15768a1b9b6aSSam Leffler 	return ni;
1577f02a0bd2SSam Leffler #undef MATCH_SSID
15788a1b9b6aSSam Leffler }
15798a1b9b6aSSam Leffler 
15808a1b9b6aSSam Leffler static void
15818a1b9b6aSSam Leffler _ieee80211_free_node(struct ieee80211_node *ni)
15828a1b9b6aSSam Leffler {
15838a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
15848a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
15858a1b9b6aSSam Leffler 
15868a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
158749a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
158849a15236SSam Leffler 		ether_sprintf(ni->ni_macaddr),
15898a1b9b6aSSam Leffler 		nt != NULL ? nt->nt_name : "<gone>");
15908a1b9b6aSSam Leffler 
15918a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
15928a1b9b6aSSam Leffler 	if (nt != NULL) {
15938a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
15948a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
15958a1b9b6aSSam Leffler 	}
15968a1b9b6aSSam Leffler 	ic->ic_node_free(ni);
15978a1b9b6aSSam Leffler }
15988a1b9b6aSSam Leffler 
15998a1b9b6aSSam Leffler void
16008a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
16018a1b9b6aSSam Leffler ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
16028a1b9b6aSSam Leffler #else
16038a1b9b6aSSam Leffler ieee80211_free_node(struct ieee80211_node *ni)
16048a1b9b6aSSam Leffler #endif
16058a1b9b6aSSam Leffler {
16068a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
16078a1b9b6aSSam Leffler 
16088a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
160929d368a7SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
161049a15236SSam Leffler 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
16118a1b9b6aSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
16128a1b9b6aSSam Leffler #endif
1613c1225b52SSam Leffler 	if (nt != NULL) {
1614c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
16158a1b9b6aSSam Leffler 		if (ieee80211_node_dectestref(ni)) {
16168a1b9b6aSSam Leffler 			/*
1617c1225b52SSam Leffler 			 * Last reference, reclaim state.
16188a1b9b6aSSam Leffler 			 */
16198a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
1620c1225b52SSam Leffler 		} else if (ieee80211_node_refcnt(ni) == 1 &&
1621c1225b52SSam Leffler 		    nt->nt_keyixmap != NULL) {
1622c1225b52SSam Leffler 			ieee80211_keyix keyix;
1623c1225b52SSam Leffler 			/*
1624c1225b52SSam Leffler 			 * Check for a last reference in the key mapping table.
1625c1225b52SSam Leffler 			 */
1626c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1627c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1628c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == ni) {
1629c1225b52SSam Leffler 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1630c1225b52SSam Leffler 				    "%s: %p<%s> clear key map entry", __func__,
1631c1225b52SSam Leffler 				    ni, ether_sprintf(ni->ni_macaddr));
1632c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = NULL;
1633c1225b52SSam Leffler 				ieee80211_node_decref(ni); /* XXX needed? */
16348a1b9b6aSSam Leffler 				_ieee80211_free_node(ni);
16358a1b9b6aSSam Leffler 			}
16361a1e1d21SSam Leffler 		}
1637c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1638c1225b52SSam Leffler 	} else {
1639c1225b52SSam Leffler 		if (ieee80211_node_dectestref(ni))
1640c1225b52SSam Leffler 			_ieee80211_free_node(ni);
1641c1225b52SSam Leffler 	}
1642c1225b52SSam Leffler }
1643c1225b52SSam Leffler 
1644c1225b52SSam Leffler /*
1645c1225b52SSam Leffler  * Reclaim a unicast key and clear any key cache state.
1646c1225b52SSam Leffler  */
1647c1225b52SSam Leffler int
1648c1225b52SSam Leffler ieee80211_node_delucastkey(struct ieee80211_node *ni)
1649c1225b52SSam Leffler {
1650c1225b52SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1651c1225b52SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1652c1225b52SSam Leffler 	struct ieee80211_node *nikey;
1653c1225b52SSam Leffler 	ieee80211_keyix keyix;
1654c1225b52SSam Leffler 	int isowned, status;
1655c1225b52SSam Leffler 
1656c1225b52SSam Leffler 	/*
1657c1225b52SSam Leffler 	 * NB: We must beware of LOR here; deleting the key
1658c1225b52SSam Leffler 	 * can cause the crypto layer to block traffic updates
1659c1225b52SSam Leffler 	 * which can generate a LOR against the node table lock;
1660c1225b52SSam Leffler 	 * grab it here and stash the key index for our use below.
1661c1225b52SSam Leffler 	 *
1662c1225b52SSam Leffler 	 * Must also beware of recursion on the node table lock.
1663c1225b52SSam Leffler 	 * When called from node_cleanup we may already have
1664c1225b52SSam Leffler 	 * the node table lock held.  Unfortunately there's no
1665c1225b52SSam Leffler 	 * way to separate out this path so we must do this
1666c1225b52SSam Leffler 	 * conditionally.
1667c1225b52SSam Leffler 	 */
1668c1225b52SSam Leffler 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1669c1225b52SSam Leffler 	if (!isowned)
1670c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
1671c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1672c1225b52SSam Leffler 	status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1673c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1674c1225b52SSam Leffler 		nikey = nt->nt_keyixmap[keyix];
1675c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;;
1676c1225b52SSam Leffler 	} else
1677c1225b52SSam Leffler 		nikey = NULL;
1678c1225b52SSam Leffler 	if (!isowned)
1679c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(&ic->ic_sta);
1680c1225b52SSam Leffler 
1681c1225b52SSam Leffler 	if (nikey != NULL) {
1682c1225b52SSam Leffler 		KASSERT(nikey == ni,
1683c1225b52SSam Leffler 			("key map out of sync, ni %p nikey %p", ni, nikey));
1684c1225b52SSam Leffler 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1685c1225b52SSam Leffler 			"%s: delete key map entry %p<%s> refcnt %d\n",
1686c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr),
1687c1225b52SSam Leffler 			ieee80211_node_refcnt(ni)-1);
1688c1225b52SSam Leffler 		ieee80211_free_node(ni);
1689c1225b52SSam Leffler 	}
1690c1225b52SSam Leffler 	return status;
1691c1225b52SSam Leffler }
16921a1e1d21SSam Leffler 
1693303ebc3cSSam Leffler /*
16948a1b9b6aSSam Leffler  * Reclaim a node.  If this is the last reference count then
16958a1b9b6aSSam Leffler  * do the normal free work.  Otherwise remove it from the node
16968a1b9b6aSSam Leffler  * table and mark it gone by clearing the back-reference.
16978a1b9b6aSSam Leffler  */
16988a1b9b6aSSam Leffler static void
16998a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
17008a1b9b6aSSam Leffler {
1701c1225b52SSam Leffler 	ieee80211_keyix keyix;
1702c1225b52SSam Leffler 
1703c1225b52SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
17048a1b9b6aSSam Leffler 
170549a15236SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
170649a15236SSam Leffler 		"%s: remove %p<%s> from %s table, refcnt %d\n",
170749a15236SSam Leffler 		__func__, ni, ether_sprintf(ni->ni_macaddr),
170849a15236SSam Leffler 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1709c1225b52SSam Leffler 	/*
1710c1225b52SSam Leffler 	 * Clear any entry in the unicast key mapping table.
1711c1225b52SSam Leffler 	 * We need to do it here so rx lookups don't find it
1712c1225b52SSam Leffler 	 * in the mapping table even if it's not in the hash
1713c1225b52SSam Leffler 	 * table.  We cannot depend on the mapping table entry
1714c1225b52SSam Leffler 	 * being cleared because the node may not be free'd.
1715c1225b52SSam Leffler 	 */
1716c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1717c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1718c1225b52SSam Leffler 	    nt->nt_keyixmap[keyix] == ni) {
1719c1225b52SSam Leffler 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1720c1225b52SSam Leffler 			"%s: %p<%s> clear key map entry\n",
1721c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr));
1722c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;
1723c1225b52SSam Leffler 		ieee80211_node_decref(ni);	/* NB: don't need free */
1724c1225b52SSam Leffler 	}
17258a1b9b6aSSam Leffler 	if (!ieee80211_node_dectestref(ni)) {
17268a1b9b6aSSam Leffler 		/*
17278a1b9b6aSSam Leffler 		 * Other references are present, just remove the
17288a1b9b6aSSam Leffler 		 * node from the table so it cannot be found.  When
17298a1b9b6aSSam Leffler 		 * the references are dropped storage will be
1730acc4f7f5SSam Leffler 		 * reclaimed.
17318a1b9b6aSSam Leffler 		 */
17328a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
17338a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
17348a1b9b6aSSam Leffler 		ni->ni_table = NULL;		/* clear reference */
17358a1b9b6aSSam Leffler 	} else
17368a1b9b6aSSam Leffler 		_ieee80211_free_node(ni);
17378a1b9b6aSSam Leffler }
17388a1b9b6aSSam Leffler 
17398a1b9b6aSSam Leffler static void
17408a1b9b6aSSam Leffler ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
17418a1b9b6aSSam Leffler {
17428a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
17438a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
17448a1b9b6aSSam Leffler 
17458a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
17468a1b9b6aSSam Leffler 		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
17478a1b9b6aSSam Leffler 
17488a1b9b6aSSam Leffler 	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
17498a1b9b6aSSam Leffler 		if (ni->ni_associd != 0) {
17508a1b9b6aSSam Leffler 			if (ic->ic_auth->ia_node_leave != NULL)
17518a1b9b6aSSam Leffler 				ic->ic_auth->ia_node_leave(ic, ni);
17528a1b9b6aSSam Leffler 			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
17538a1b9b6aSSam Leffler 		}
17548a1b9b6aSSam Leffler 		node_reclaim(nt, ni);
17558a1b9b6aSSam Leffler 	}
17568a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
17578a1b9b6aSSam Leffler }
17588a1b9b6aSSam Leffler 
17598a1b9b6aSSam Leffler static void
17608a1b9b6aSSam Leffler ieee80211_free_allnodes(struct ieee80211_node_table *nt)
17618a1b9b6aSSam Leffler {
17628a1b9b6aSSam Leffler 
17638a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
17648a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
17658a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
17668a1b9b6aSSam Leffler }
17678a1b9b6aSSam Leffler 
17688a1b9b6aSSam Leffler /*
17698a1b9b6aSSam Leffler  * Timeout entries in the scan cache.
17708a1b9b6aSSam Leffler  */
17718a1b9b6aSSam Leffler static void
17728a1b9b6aSSam Leffler ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
17738a1b9b6aSSam Leffler {
17748a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
17758a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *tni;
17768a1b9b6aSSam Leffler 
17778a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
17788a1b9b6aSSam Leffler 	ni = ic->ic_bss;
17798a1b9b6aSSam Leffler 	/* XXX belongs elsewhere */
17808a1b9b6aSSam Leffler 	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
17818a1b9b6aSSam Leffler 		m_freem(ni->ni_rxfrag[0]);
17828a1b9b6aSSam Leffler 		ni->ni_rxfrag[0] = NULL;
17838a1b9b6aSSam Leffler 	}
17848a1b9b6aSSam Leffler 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
17858a1b9b6aSSam Leffler 		if (ni->ni_inact && --ni->ni_inact == 0) {
17868a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
17878a1b9b6aSSam Leffler 			    "[%s] scan candidate purged from cache "
17888a1b9b6aSSam Leffler 			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
178949a15236SSam Leffler 			    ieee80211_node_refcnt(ni));
17908a1b9b6aSSam Leffler 			node_reclaim(nt, ni);
17918a1b9b6aSSam Leffler 		}
17928a1b9b6aSSam Leffler 	}
17938a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
17948a1b9b6aSSam Leffler 
17958a1b9b6aSSam Leffler 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
17968a1b9b6aSSam Leffler }
17978a1b9b6aSSam Leffler 
17988a1b9b6aSSam Leffler /*
17998a1b9b6aSSam Leffler  * Timeout inactive stations and do related housekeeping.
18008a1b9b6aSSam Leffler  * Note that we cannot hold the node lock while sending a
18018a1b9b6aSSam Leffler  * frame as this would lead to a LOR.  Instead we use a
18028a1b9b6aSSam Leffler  * generation number to mark nodes that we've scanned and
18038a1b9b6aSSam Leffler  * drop the lock and restart a scan if we have to time out
18048a1b9b6aSSam Leffler  * a node.  Since we are single-threaded by virtue of
1805303ebc3cSSam Leffler  * controlling the inactivity timer we can be sure this will
1806303ebc3cSSam Leffler  * process each node only once.
1807303ebc3cSSam Leffler  */
18088a1b9b6aSSam Leffler static void
18098a1b9b6aSSam Leffler ieee80211_timeout_stations(struct ieee80211_node_table *nt)
18101a1e1d21SSam Leffler {
18118a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
1812303ebc3cSSam Leffler 	struct ieee80211_node *ni;
18138a1b9b6aSSam Leffler 	u_int gen;
1814f66d97f6SSam Leffler 	int isadhoc;
18151a1e1d21SSam Leffler 
1816f66d97f6SSam Leffler 	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1817f66d97f6SSam Leffler 		   ic->ic_opmode == IEEE80211_M_AHDEMO);
18188a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
1819a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
18208a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
182149a15236SSam Leffler 		"%s: %s scangen %u\n", __func__, nt->nt_name, gen);
1822303ebc3cSSam Leffler restart:
18238a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
18248a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1825303ebc3cSSam Leffler 		if (ni->ni_scangen == gen)	/* previously handled */
1826303ebc3cSSam Leffler 			continue;
1827303ebc3cSSam Leffler 		ni->ni_scangen = gen;
18280a915fadSSam Leffler 		/*
1829ebdda46cSSam Leffler 		 * Ignore entries for which have yet to receive an
1830ebdda46cSSam Leffler 		 * authentication frame.  These are transient and
1831ebdda46cSSam Leffler 		 * will be reclaimed when the last reference to them
1832ebdda46cSSam Leffler 		 * goes away (when frame xmits complete).
1833ebdda46cSSam Leffler 		 */
183444b666cdSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
183544b666cdSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1836ebdda46cSSam Leffler 			continue;
1837ebdda46cSSam Leffler 		/*
18388a1b9b6aSSam Leffler 		 * Free fragment if not needed anymore
18398a1b9b6aSSam Leffler 		 * (last fragment older than 1s).
18408a1b9b6aSSam Leffler 		 * XXX doesn't belong here
18410a915fadSSam Leffler 		 */
18428a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[0] != NULL &&
18438a1b9b6aSSam Leffler 		    ticks > ni->ni_rxfragstamp + hz) {
18448a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
18458a1b9b6aSSam Leffler 			ni->ni_rxfrag[0] = NULL;
18468a1b9b6aSSam Leffler 		}
1847ce647032SSam Leffler 		/*
1848ce647032SSam Leffler 		 * Special case ourself; we may be idle for extended periods
1849ce647032SSam Leffler 		 * of time and regardless reclaiming our state is wrong.
1850ce647032SSam Leffler 		 */
1851ce647032SSam Leffler 		if (ni == ic->ic_bss)
1852ce647032SSam Leffler 			continue;
18538a1b9b6aSSam Leffler 		ni->ni_inact--;
1854f66d97f6SSam Leffler 		if (ni->ni_associd != 0 || isadhoc) {
18558a1b9b6aSSam Leffler 			/*
18568a1b9b6aSSam Leffler 			 * Age frames on the power save queue. The
18578a1b9b6aSSam Leffler 			 * aging interval is 4 times the listen
18588a1b9b6aSSam Leffler 			 * interval specified by the station.  This
18598a1b9b6aSSam Leffler 			 * number is factored into the age calculations
18608a1b9b6aSSam Leffler 			 * when the frame is placed on the queue.  We
18618a1b9b6aSSam Leffler 			 * store ages as time differences we can check
18628a1b9b6aSSam Leffler 			 * and/or adjust only the head of the list.
18638a1b9b6aSSam Leffler 			 */
18648a1b9b6aSSam Leffler 			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
18658a1b9b6aSSam Leffler 				struct mbuf *m;
18668a1b9b6aSSam Leffler 				int discard = 0;
18678a1b9b6aSSam Leffler 
18688a1b9b6aSSam Leffler 				IEEE80211_NODE_SAVEQ_LOCK(ni);
18698a1b9b6aSSam Leffler 				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
18708a1b9b6aSSam Leffler 				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
18718a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
18728a1b9b6aSSam Leffler 					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
18738a1b9b6aSSam Leffler 					m_freem(m);
18748a1b9b6aSSam Leffler 					discard++;
18758a1b9b6aSSam Leffler 				}
18768a1b9b6aSSam Leffler 				if (m != NULL)
18778a1b9b6aSSam Leffler 					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
18788a1b9b6aSSam Leffler 				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
18798a1b9b6aSSam Leffler 
18808a1b9b6aSSam Leffler 				if (discard != 0) {
18818a1b9b6aSSam Leffler 					IEEE80211_DPRINTF(ic,
18828a1b9b6aSSam Leffler 					    IEEE80211_MSG_POWER,
18838a1b9b6aSSam Leffler 					    "[%s] discard %u frames for age\n",
18848a1b9b6aSSam Leffler 					    ether_sprintf(ni->ni_macaddr),
18858a1b9b6aSSam Leffler 					    discard);
18868a1b9b6aSSam Leffler 					IEEE80211_NODE_STAT_ADD(ni,
18878a1b9b6aSSam Leffler 						ps_discard, discard);
18888a1b9b6aSSam Leffler 					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1889edfa57d0SSam Leffler 						ic->ic_set_tim(ni, 0);
18908a1b9b6aSSam Leffler 				}
18918a1b9b6aSSam Leffler 			}
18928a1b9b6aSSam Leffler 			/*
18938a1b9b6aSSam Leffler 			 * Probe the station before time it out.  We
18948a1b9b6aSSam Leffler 			 * send a null data frame which may not be
18958a1b9b6aSSam Leffler 			 * universally supported by drivers (need it
18968a1b9b6aSSam Leffler 			 * for ps-poll support so it should be...).
18978a1b9b6aSSam Leffler 			 */
18982045f699SSam Leffler 			if (0 < ni->ni_inact &&
18992045f699SSam Leffler 			    ni->ni_inact <= ic->ic_inact_probe) {
1900f66d97f6SSam Leffler 				IEEE80211_NOTE(ic,
1901f66d97f6SSam Leffler 				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1902f66d97f6SSam Leffler 				    ni, "%s",
1903f66d97f6SSam Leffler 				    "probe station due to inactivity");
190419ad2dd7SSam Leffler 				/*
190519ad2dd7SSam Leffler 				 * Grab a reference before unlocking the table
190619ad2dd7SSam Leffler 				 * so the node cannot be reclaimed before we
190719ad2dd7SSam Leffler 				 * send the frame. ieee80211_send_nulldata
190819ad2dd7SSam Leffler 				 * understands we've done this and reclaims the
190919ad2dd7SSam Leffler 				 * ref for us as needed.
191019ad2dd7SSam Leffler 				 */
191119ad2dd7SSam Leffler 				ieee80211_ref_node(ni);
19128a1b9b6aSSam Leffler 				IEEE80211_NODE_UNLOCK(nt);
1913f62121ceSSam Leffler 				ieee80211_send_nulldata(ni);
19148a1b9b6aSSam Leffler 				/* XXX stat? */
19158a1b9b6aSSam Leffler 				goto restart;
19168a1b9b6aSSam Leffler 			}
19178a1b9b6aSSam Leffler 		}
19188a1b9b6aSSam Leffler 		if (ni->ni_inact <= 0) {
1919f66d97f6SSam Leffler 			IEEE80211_NOTE(ic,
1920f66d97f6SSam Leffler 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1921f66d97f6SSam Leffler 			    "station timed out due to inactivity "
1922f66d97f6SSam Leffler 			    "(refcnt %u)", ieee80211_node_refcnt(ni));
19238a1b9b6aSSam Leffler 			/*
19248a1b9b6aSSam Leffler 			 * Send a deauthenticate frame and drop the station.
19258a1b9b6aSSam Leffler 			 * This is somewhat complicated due to reference counts
19268a1b9b6aSSam Leffler 			 * and locking.  At this point a station will typically
19278a1b9b6aSSam Leffler 			 * have a reference count of 1.  ieee80211_node_leave
19288a1b9b6aSSam Leffler 			 * will do a "free" of the node which will drop the
19298a1b9b6aSSam Leffler 			 * reference count.  But in the meantime a reference
19308a1b9b6aSSam Leffler 			 * wil be held by the deauth frame.  The actual reclaim
19318a1b9b6aSSam Leffler 			 * of the node will happen either after the tx is
19328a1b9b6aSSam Leffler 			 * completed or by ieee80211_node_leave.
19338a1b9b6aSSam Leffler 			 *
19348a1b9b6aSSam Leffler 			 * Separately we must drop the node lock before sending
19358a1b9b6aSSam Leffler 			 * in case the driver takes a lock, as this will result
19368a1b9b6aSSam Leffler 			 * in  LOR between the node lock and the driver lock.
19378a1b9b6aSSam Leffler 			 */
19388a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
19398a1b9b6aSSam Leffler 			if (ni->ni_associd != 0) {
19401a1e1d21SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
19411a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
19421a1e1d21SSam Leffler 				    IEEE80211_REASON_AUTH_EXPIRE);
19438a1b9b6aSSam Leffler 			}
19448a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
19451be50176SSam Leffler 			ic->ic_stats.is_node_timeout++;
1946303ebc3cSSam Leffler 			goto restart;
1947303ebc3cSSam Leffler 		}
19481a1e1d21SSam Leffler 	}
19498a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
19508a1b9b6aSSam Leffler 
19518a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
19528a1b9b6aSSam Leffler 
19538a1b9b6aSSam Leffler 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
19541a1e1d21SSam Leffler }
19551a1e1d21SSam Leffler 
19561a1e1d21SSam Leffler void
19578a1b9b6aSSam Leffler ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
19581a1e1d21SSam Leffler {
19591a1e1d21SSam Leffler 	struct ieee80211_node *ni;
19608a1b9b6aSSam Leffler 	u_int gen;
19611a1e1d21SSam Leffler 
19628a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
1963a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
19648a1b9b6aSSam Leffler restart:
19658a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
19668a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
19678a1b9b6aSSam Leffler 		if (ni->ni_scangen != gen) {
19688a1b9b6aSSam Leffler 			ni->ni_scangen = gen;
19698a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
19708a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
19711a1e1d21SSam Leffler 			(*f)(arg, ni);
19728a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
19738a1b9b6aSSam Leffler 			goto restart;
19748a1b9b6aSSam Leffler 		}
19758a1b9b6aSSam Leffler 	}
19768a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
19778a1b9b6aSSam Leffler 
19788a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
19798a1b9b6aSSam Leffler }
19808a1b9b6aSSam Leffler 
19818a1b9b6aSSam Leffler void
19828a1b9b6aSSam Leffler ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
19838a1b9b6aSSam Leffler {
19848a1b9b6aSSam Leffler 	printf("0x%p: mac %s refcnt %d\n", ni,
19858a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
19868a1b9b6aSSam Leffler 	printf("\tscangen %u authmode %u flags 0x%x\n",
19878a1b9b6aSSam Leffler 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
19888a1b9b6aSSam Leffler 	printf("\tassocid 0x%x txpower %u vlan %u\n",
19898a1b9b6aSSam Leffler 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
19908a1b9b6aSSam Leffler 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
19918a1b9b6aSSam Leffler 		ni->ni_txseqs[0],
19928a1b9b6aSSam Leffler 		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
19938a1b9b6aSSam Leffler 		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
19948a1b9b6aSSam Leffler 		ni->ni_rxfragstamp);
19958a1b9b6aSSam Leffler 	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
19968a1b9b6aSSam Leffler 		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
19978a1b9b6aSSam Leffler 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
19988a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
19998a1b9b6aSSam Leffler 		ni->ni_esslen, ni->ni_essid,
20008a1b9b6aSSam Leffler 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
20018a1b9b6aSSam Leffler 	printf("\tfails %u inact %u txrate %u\n",
20028a1b9b6aSSam Leffler 		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
20038a1b9b6aSSam Leffler }
20048a1b9b6aSSam Leffler 
20058a1b9b6aSSam Leffler void
20068a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt)
20078a1b9b6aSSam Leffler {
20088a1b9b6aSSam Leffler 	ieee80211_iterate_nodes(nt,
20098a1b9b6aSSam Leffler 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
20108a1b9b6aSSam Leffler }
20118a1b9b6aSSam Leffler 
20128a1b9b6aSSam Leffler /*
20138a1b9b6aSSam Leffler  * Handle a station joining an 11g network.
20148a1b9b6aSSam Leffler  */
20158a1b9b6aSSam Leffler static void
20168a1b9b6aSSam Leffler ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
20178a1b9b6aSSam Leffler {
20188a1b9b6aSSam Leffler 
20198a1b9b6aSSam Leffler 	/*
20208a1b9b6aSSam Leffler 	 * Station isn't capable of short slot time.  Bump
20218a1b9b6aSSam Leffler 	 * the count of long slot time stations and disable
20228a1b9b6aSSam Leffler 	 * use of short slot time.  Note that the actual switch
20238a1b9b6aSSam Leffler 	 * over to long slot time use may not occur until the
20248a1b9b6aSSam Leffler 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
20258a1b9b6aSSam Leffler 	 */
20268a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
20278a1b9b6aSSam Leffler 		ic->ic_longslotsta++;
20288a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
20298a1b9b6aSSam Leffler 		    "[%s] station needs long slot time, count %d\n",
20308a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
20318a1b9b6aSSam Leffler 		/* XXX vap's w/ conflicting needs won't work */
20328a1b9b6aSSam Leffler 		ieee80211_set_shortslottime(ic, 0);
20338a1b9b6aSSam Leffler 	}
20348a1b9b6aSSam Leffler 	/*
20358a1b9b6aSSam Leffler 	 * If the new station is not an ERP station
20368a1b9b6aSSam Leffler 	 * then bump the counter and enable protection
20378a1b9b6aSSam Leffler 	 * if configured.
20388a1b9b6aSSam Leffler 	 */
20398a1b9b6aSSam Leffler 	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
20408a1b9b6aSSam Leffler 		ic->ic_nonerpsta++;
20418a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
20428a1b9b6aSSam Leffler 		    "[%s] station is !ERP, %d non-ERP stations associated\n",
20438a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
20448a1b9b6aSSam Leffler 		/*
20458a1b9b6aSSam Leffler 		 * If protection is configured, enable it.
20468a1b9b6aSSam Leffler 		 */
20478a1b9b6aSSam Leffler 		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
20488a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
20498a1b9b6aSSam Leffler 			    "%s: enable use of protection\n", __func__);
20508a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
20518a1b9b6aSSam Leffler 		}
20528a1b9b6aSSam Leffler 		/*
20538a1b9b6aSSam Leffler 		 * If station does not support short preamble
20548a1b9b6aSSam Leffler 		 * then we must enable use of Barker preamble.
20558a1b9b6aSSam Leffler 		 */
20568a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
20578a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
20588a1b9b6aSSam Leffler 			    "[%s] station needs long preamble\n",
20598a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr));
20608a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
20618a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
20628a1b9b6aSSam Leffler 		}
20630912bac9SSam Leffler 		if (ic->ic_nonerpsta == 1)
20640912bac9SSam Leffler 			ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
20658a1b9b6aSSam Leffler 	} else
20668a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
20678a1b9b6aSSam Leffler }
20688a1b9b6aSSam Leffler 
20698a1b9b6aSSam Leffler void
20708a1b9b6aSSam Leffler ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
20718a1b9b6aSSam Leffler {
20728a1b9b6aSSam Leffler 	int newassoc;
20738a1b9b6aSSam Leffler 
20748a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
20758a1b9b6aSSam Leffler 		u_int16_t aid;
20768a1b9b6aSSam Leffler 
20778a1b9b6aSSam Leffler 		/*
20788a1b9b6aSSam Leffler 		 * It would be good to search the bitmap
20798a1b9b6aSSam Leffler 		 * more efficiently, but this will do for now.
20808a1b9b6aSSam Leffler 		 */
20818a1b9b6aSSam Leffler 		for (aid = 1; aid < ic->ic_max_aid; aid++) {
20828a1b9b6aSSam Leffler 			if (!IEEE80211_AID_ISSET(aid,
20838a1b9b6aSSam Leffler 			    ic->ic_aid_bitmap))
20848a1b9b6aSSam Leffler 				break;
20858a1b9b6aSSam Leffler 		}
20868a1b9b6aSSam Leffler 		if (aid >= ic->ic_max_aid) {
20878a1b9b6aSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
20888a1b9b6aSSam Leffler 			    IEEE80211_REASON_ASSOC_TOOMANY);
20898a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
20908a1b9b6aSSam Leffler 			return;
20918a1b9b6aSSam Leffler 		}
20928a1b9b6aSSam Leffler 		ni->ni_associd = aid | 0xc000;
20938a1b9b6aSSam Leffler 		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
20948a1b9b6aSSam Leffler 		ic->ic_sta_assoc++;
20958a1b9b6aSSam Leffler 		newassoc = 1;
2096ca4ac7aeSSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G &&
2097ca4ac7aeSSam Leffler 		    IEEE80211_IS_CHAN_FULL(ni->ni_chan))
20988a1b9b6aSSam Leffler 			ieee80211_node_join_11g(ic, ni);
20998a1b9b6aSSam Leffler 	} else
21008a1b9b6aSSam Leffler 		newassoc = 0;
21018a1b9b6aSSam Leffler 
21028a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
2103b8fcf546SSam Leffler 	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
2104b8fcf546SSam Leffler 	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
2105b8fcf546SSam Leffler 	    IEEE80211_NODE_AID(ni),
2106b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2107b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2108b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2109b8fcf546SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
2110b8fcf546SSam Leffler 	);
21118a1b9b6aSSam Leffler 
21128a1b9b6aSSam Leffler 	/* give driver a chance to setup state like ni_txrate */
2113736b3dc3SSam Leffler 	if (ic->ic_newassoc != NULL)
2114e9962332SSam Leffler 		ic->ic_newassoc(ni, newassoc);
21152045f699SSam Leffler 	ni->ni_inact_reload = ic->ic_inact_auth;
21162045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
21178a1b9b6aSSam Leffler 	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
21188a1b9b6aSSam Leffler 	/* tell the authenticator about new station */
21198a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_join != NULL)
21208a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_join(ic, ni);
21218a1b9b6aSSam Leffler 	ieee80211_notify_node_join(ic, ni, newassoc);
21228a1b9b6aSSam Leffler }
21238a1b9b6aSSam Leffler 
21248a1b9b6aSSam Leffler /*
21258a1b9b6aSSam Leffler  * Handle a station leaving an 11g network.
21268a1b9b6aSSam Leffler  */
21278a1b9b6aSSam Leffler static void
21288a1b9b6aSSam Leffler ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
21298a1b9b6aSSam Leffler {
21308a1b9b6aSSam Leffler 
21315ff80921SSam Leffler 	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G,
2132efefac40SSam Leffler 	     ("not in 11g, bss %u:0x%x, curmode %u", ni->ni_chan->ic_freq,
2133efefac40SSam Leffler 	      ni->ni_chan->ic_flags, ic->ic_curmode));
21348a1b9b6aSSam Leffler 
21358a1b9b6aSSam Leffler 	/*
21368a1b9b6aSSam Leffler 	 * If a long slot station do the slot time bookkeeping.
21378a1b9b6aSSam Leffler 	 */
21388a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
21398a1b9b6aSSam Leffler 		KASSERT(ic->ic_longslotsta > 0,
21408a1b9b6aSSam Leffler 		    ("bogus long slot station count %d", ic->ic_longslotsta));
21418a1b9b6aSSam Leffler 		ic->ic_longslotsta--;
21428a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
21438a1b9b6aSSam Leffler 		    "[%s] long slot time station leaves, count now %d\n",
21448a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
21458a1b9b6aSSam Leffler 		if (ic->ic_longslotsta == 0) {
21468a1b9b6aSSam Leffler 			/*
21478a1b9b6aSSam Leffler 			 * Re-enable use of short slot time if supported
21488a1b9b6aSSam Leffler 			 * and not operating in IBSS mode (per spec).
21498a1b9b6aSSam Leffler 			 */
21508a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
21518a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_IBSS) {
21528a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
21538a1b9b6aSSam Leffler 				    "%s: re-enable use of short slot time\n",
21548a1b9b6aSSam Leffler 				    __func__);
21558a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic, 1);
21568a1b9b6aSSam Leffler 			}
21578a1b9b6aSSam Leffler 		}
21588a1b9b6aSSam Leffler 	}
21598a1b9b6aSSam Leffler 	/*
21608a1b9b6aSSam Leffler 	 * If a non-ERP station do the protection-related bookkeeping.
21618a1b9b6aSSam Leffler 	 */
21628a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
21638a1b9b6aSSam Leffler 		KASSERT(ic->ic_nonerpsta > 0,
21648a1b9b6aSSam Leffler 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
21658a1b9b6aSSam Leffler 		ic->ic_nonerpsta--;
21668a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
21678a1b9b6aSSam Leffler 		    "[%s] non-ERP station leaves, count now %d\n",
21688a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
21698a1b9b6aSSam Leffler 		if (ic->ic_nonerpsta == 0) {
21708a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
21718a1b9b6aSSam Leffler 				"%s: disable use of protection\n", __func__);
21728a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
21738a1b9b6aSSam Leffler 			/* XXX verify mode? */
21748a1b9b6aSSam Leffler 			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
21758a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
21768a1b9b6aSSam Leffler 				    "%s: re-enable use of short preamble\n",
21778a1b9b6aSSam Leffler 				    __func__);
21788a1b9b6aSSam Leffler 				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
21798a1b9b6aSSam Leffler 				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
21808a1b9b6aSSam Leffler 			}
21810912bac9SSam Leffler 			ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
21828a1b9b6aSSam Leffler 		}
21838a1b9b6aSSam Leffler 	}
21848a1b9b6aSSam Leffler }
21858a1b9b6aSSam Leffler 
21868a1b9b6aSSam Leffler /*
21878a1b9b6aSSam Leffler  * Handle bookkeeping for station deauthentication/disassociation
21888a1b9b6aSSam Leffler  * when operating as an ap.
21898a1b9b6aSSam Leffler  */
21908a1b9b6aSSam Leffler void
21918a1b9b6aSSam Leffler ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
21928a1b9b6aSSam Leffler {
219344acc00dSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
21948a1b9b6aSSam Leffler 
21958a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
21968a1b9b6aSSam Leffler 	    "[%s] station with aid %d leaves\n",
21978a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
21988a1b9b6aSSam Leffler 
21998a1b9b6aSSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
22008a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_IBSS ||
22018a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_AHDEMO,
22028a1b9b6aSSam Leffler 		("unexpected operating mode %u", ic->ic_opmode));
22038a1b9b6aSSam Leffler 	/*
22048a1b9b6aSSam Leffler 	 * If node wasn't previously associated all
22058a1b9b6aSSam Leffler 	 * we need to do is reclaim the reference.
22068a1b9b6aSSam Leffler 	 */
22078a1b9b6aSSam Leffler 	/* XXX ibss mode bypasses 11g and notification */
22088a1b9b6aSSam Leffler 	if (ni->ni_associd == 0)
22098a1b9b6aSSam Leffler 		goto done;
22108a1b9b6aSSam Leffler 	/*
22118a1b9b6aSSam Leffler 	 * Tell the authenticator the station is leaving.
22128a1b9b6aSSam Leffler 	 * Note that we must do this before yanking the
22138a1b9b6aSSam Leffler 	 * association id as the authenticator uses the
22148a1b9b6aSSam Leffler 	 * associd to locate it's state block.
22158a1b9b6aSSam Leffler 	 */
22168a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_leave != NULL)
22178a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_leave(ic, ni);
22188a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
22198a1b9b6aSSam Leffler 	ni->ni_associd = 0;
22208a1b9b6aSSam Leffler 	ic->ic_sta_assoc--;
22218a1b9b6aSSam Leffler 
2222ca4ac7aeSSam Leffler 	if (ic->ic_curmode == IEEE80211_MODE_11G &&
2223ca4ac7aeSSam Leffler 	    IEEE80211_IS_CHAN_FULL(ni->ni_chan))
22248a1b9b6aSSam Leffler 		ieee80211_node_leave_11g(ic, ni);
22258a1b9b6aSSam Leffler 	/*
22268a1b9b6aSSam Leffler 	 * Cleanup station state.  In particular clear various
22278a1b9b6aSSam Leffler 	 * state that might otherwise be reused if the node
22288a1b9b6aSSam Leffler 	 * is reused before the reference count goes to zero
22298a1b9b6aSSam Leffler 	 * (and memory is reclaimed).
22308a1b9b6aSSam Leffler 	 */
22318a1b9b6aSSam Leffler 	ieee80211_sta_leave(ic, ni);
22328a1b9b6aSSam Leffler done:
223344acc00dSSam Leffler 	/*
223444acc00dSSam Leffler 	 * Remove the node from any table it's recorded in and
223544acc00dSSam Leffler 	 * drop the caller's reference.  Removal from the table
223644acc00dSSam Leffler 	 * is important to insure the node is not reprocessed
223744acc00dSSam Leffler 	 * for inactivity.
223844acc00dSSam Leffler 	 */
223944acc00dSSam Leffler 	if (nt != NULL) {
224044acc00dSSam Leffler 		IEEE80211_NODE_LOCK(nt);
224144acc00dSSam Leffler 		node_reclaim(nt, ni);
224244acc00dSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
224344acc00dSSam Leffler 	} else
22448a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
22458a1b9b6aSSam Leffler }
22468a1b9b6aSSam Leffler 
22478a1b9b6aSSam Leffler u_int8_t
22488a1b9b6aSSam Leffler ieee80211_getrssi(struct ieee80211com *ic)
22498a1b9b6aSSam Leffler {
22508a1b9b6aSSam Leffler #define	NZ(x)	((x) == 0 ? 1 : (x))
2251acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
22528a1b9b6aSSam Leffler 	u_int32_t rssi_samples, rssi_total;
22538a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
22548a1b9b6aSSam Leffler 
22558a1b9b6aSSam Leffler 	rssi_total = 0;
22568a1b9b6aSSam Leffler 	rssi_samples = 0;
22578a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
22588a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
22598a1b9b6aSSam Leffler 		/* XXX locking */
2260acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
22618a1b9b6aSSam Leffler 			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
22628a1b9b6aSSam Leffler 				rssi_samples++;
22638a1b9b6aSSam Leffler 				rssi_total += ic->ic_node_getrssi(ni);
22648a1b9b6aSSam Leffler 			}
22658a1b9b6aSSam Leffler 		break;
22668a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
22678a1b9b6aSSam Leffler 		/* XXX locking */
2268acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
22698a1b9b6aSSam Leffler 			rssi_samples++;
22708a1b9b6aSSam Leffler 			rssi_total += ic->ic_node_getrssi(ni);
22718a1b9b6aSSam Leffler 		}
22728a1b9b6aSSam Leffler 		break;
22738a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
22748a1b9b6aSSam Leffler 		/* XXX locking */
2275acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
22768a1b9b6aSSam Leffler 			if (IEEE80211_AID(ni->ni_associd) != 0) {
22778a1b9b6aSSam Leffler 				rssi_samples++;
22788a1b9b6aSSam Leffler 				rssi_total += ic->ic_node_getrssi(ni);
22798a1b9b6aSSam Leffler 			}
22808a1b9b6aSSam Leffler 		break;
22818a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:	/* XXX */
22828a1b9b6aSSam Leffler 	case IEEE80211_M_STA:		/* use stats from associated ap */
22838a1b9b6aSSam Leffler 	default:
22848a1b9b6aSSam Leffler 		if (ic->ic_bss != NULL)
22858a1b9b6aSSam Leffler 			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
22868a1b9b6aSSam Leffler 		rssi_samples = 1;
22878a1b9b6aSSam Leffler 		break;
22888a1b9b6aSSam Leffler 	}
22898a1b9b6aSSam Leffler 	return rssi_total / NZ(rssi_samples);
22908a1b9b6aSSam Leffler #undef NZ
22918a1b9b6aSSam Leffler }
22928a1b9b6aSSam Leffler 
22938a1b9b6aSSam Leffler /*
22948a1b9b6aSSam Leffler  * Indicate whether there are frames queued for a station in power-save mode.
22958a1b9b6aSSam Leffler  */
22968a1b9b6aSSam Leffler static void
2297edfa57d0SSam Leffler ieee80211_set_tim(struct ieee80211_node *ni, int set)
22988a1b9b6aSSam Leffler {
2299edfa57d0SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
23008a1b9b6aSSam Leffler 	u_int16_t aid;
23018a1b9b6aSSam Leffler 
23028a1b9b6aSSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
23038a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_IBSS,
23048a1b9b6aSSam Leffler 		("operating mode %u", ic->ic_opmode));
23058a1b9b6aSSam Leffler 
23068a1b9b6aSSam Leffler 	aid = IEEE80211_AID(ni->ni_associd);
23078a1b9b6aSSam Leffler 	KASSERT(aid < ic->ic_max_aid,
23088a1b9b6aSSam Leffler 		("bogus aid %u, max %u", aid, ic->ic_max_aid));
23098a1b9b6aSSam Leffler 
23108a1b9b6aSSam Leffler 	IEEE80211_BEACON_LOCK(ic);
23118a1b9b6aSSam Leffler 	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
23128a1b9b6aSSam Leffler 		if (set) {
23138a1b9b6aSSam Leffler 			setbit(ic->ic_tim_bitmap, aid);
23148a1b9b6aSSam Leffler 			ic->ic_ps_pending++;
23158a1b9b6aSSam Leffler 		} else {
23168a1b9b6aSSam Leffler 			clrbit(ic->ic_tim_bitmap, aid);
23178a1b9b6aSSam Leffler 			ic->ic_ps_pending--;
23188a1b9b6aSSam Leffler 		}
23198a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
23208a1b9b6aSSam Leffler 	}
23218a1b9b6aSSam Leffler 	IEEE80211_BEACON_UNLOCK(ic);
23228a1b9b6aSSam Leffler }
23238a1b9b6aSSam Leffler 
23248a1b9b6aSSam Leffler /*
23258a1b9b6aSSam Leffler  * Node table support.
23268a1b9b6aSSam Leffler  */
23278a1b9b6aSSam Leffler 
23288a1b9b6aSSam Leffler static void
23298a1b9b6aSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic,
23308a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt,
2331c1225b52SSam Leffler 	const char *name, int inact, int keyixmax,
23328a1b9b6aSSam Leffler 	void (*timeout)(struct ieee80211_node_table *))
23338a1b9b6aSSam Leffler {
23348a1b9b6aSSam Leffler 
23358a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
23368a1b9b6aSSam Leffler 		"%s %s table, inact %u\n", __func__, name, inact);
23378a1b9b6aSSam Leffler 
23388a1b9b6aSSam Leffler 	nt->nt_ic = ic;
23398a1b9b6aSSam Leffler 	/* XXX need unit */
23408a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
23418a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
23428a1b9b6aSSam Leffler 	TAILQ_INIT(&nt->nt_node);
23438a1b9b6aSSam Leffler 	nt->nt_name = name;
23448a1b9b6aSSam Leffler 	nt->nt_scangen = 1;
23458a1b9b6aSSam Leffler 	nt->nt_inact_init = inact;
23468a1b9b6aSSam Leffler 	nt->nt_timeout = timeout;
2347c1225b52SSam Leffler 	nt->nt_keyixmax = keyixmax;
2348c1225b52SSam Leffler 	if (nt->nt_keyixmax > 0) {
2349c1225b52SSam Leffler 		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
2350c1225b52SSam Leffler 			keyixmax * sizeof(struct ieee80211_node *),
2351c1225b52SSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
2352c1225b52SSam Leffler 		if (nt->nt_keyixmap == NULL)
2353c1225b52SSam Leffler 			if_printf(ic->ic_ifp,
2354c1225b52SSam Leffler 			    "Cannot allocate key index map with %u entries\n",
2355c1225b52SSam Leffler 			    keyixmax);
2356c1225b52SSam Leffler 	} else
2357c1225b52SSam Leffler 		nt->nt_keyixmap = NULL;
23588a1b9b6aSSam Leffler }
23598a1b9b6aSSam Leffler 
23608a1b9b6aSSam Leffler void
23618a1b9b6aSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt)
23628a1b9b6aSSam Leffler {
23638a1b9b6aSSam Leffler 
23648a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
23658a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
23668a1b9b6aSSam Leffler 
23678a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
23688a1b9b6aSSam Leffler 	nt->nt_inact_timer = 0;
23698a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
23708a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
23718a1b9b6aSSam Leffler }
23728a1b9b6aSSam Leffler 
23738a1b9b6aSSam Leffler static void
23748a1b9b6aSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
23758a1b9b6aSSam Leffler {
23768a1b9b6aSSam Leffler 
23778a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
23788a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
23798a1b9b6aSSam Leffler 
2380c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
23818a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
2382c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL) {
2383c1225b52SSam Leffler 		/* XXX verify all entries are NULL */
2384c1225b52SSam Leffler 		int i;
2385c1225b52SSam Leffler 		for (i = 0; i < nt->nt_keyixmax; i++)
2386c1225b52SSam Leffler 			if (nt->nt_keyixmap[i] != NULL)
2387c1225b52SSam Leffler 				printf("%s: %s[%u] still active\n", __func__,
2388c1225b52SSam Leffler 					nt->nt_name, i);
2389c1225b52SSam Leffler 		FREE(nt->nt_keyixmap, M_80211_NODE);
2390c1225b52SSam Leffler 		nt->nt_keyixmap = NULL;
2391c1225b52SSam Leffler 	}
23928a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_DESTROY(nt);
23938a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_DESTROY(nt);
23948a1b9b6aSSam Leffler }
2395