xref: /freebsd/sys/net80211/ieee80211_node.c (revision e9962332)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
31f1d7810SSam Leffler  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
147535e66aSSam Leffler  * 3. The name of the author may not be used to endorse or promote products
157535e66aSSam Leffler  *    derived from this software without specific prior written permission.
161a1e1d21SSam Leffler  *
171a1e1d21SSam Leffler  * Alternatively, this software may be distributed under the terms of the
181a1e1d21SSam Leffler  * GNU General Public License ("GPL") version 2 as published by the Free
191a1e1d21SSam Leffler  * Software Foundation.
201a1e1d21SSam Leffler  *
217535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
227535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
237535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
247535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
257535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
267535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
277535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
287535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
297535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
307535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311a1e1d21SSam Leffler  */
321a1e1d21SSam Leffler 
331a1e1d21SSam Leffler #include <sys/cdefs.h>
341a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
351a1e1d21SSam Leffler 
361a1e1d21SSam Leffler #include <sys/param.h>
371a1e1d21SSam Leffler #include <sys/systm.h>
381a1e1d21SSam Leffler #include <sys/mbuf.h>
391a1e1d21SSam Leffler #include <sys/malloc.h>
401a1e1d21SSam Leffler #include <sys/kernel.h>
411a1e1d21SSam Leffler 
428a1b9b6aSSam Leffler #include <sys/socket.h>
431a1e1d21SSam Leffler 
441a1e1d21SSam Leffler #include <net/if.h>
451a1e1d21SSam Leffler #include <net/if_media.h>
461a1e1d21SSam Leffler #include <net/ethernet.h>
471a1e1d21SSam Leffler 
481a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
491a1e1d21SSam Leffler 
501a1e1d21SSam Leffler #include <net/bpf.h>
511a1e1d21SSam Leffler 
527268fa64SSam Leffler /*
537268fa64SSam Leffler  * Association id's are managed with a bit vector.
547268fa64SSam Leffler  */
557268fa64SSam Leffler #define	IEEE80211_AID_SET(b, w) \
567268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
577268fa64SSam Leffler #define	IEEE80211_AID_CLR(b, w) \
587268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
597268fa64SSam Leffler #define	IEEE80211_AID_ISSET(b, w) \
607268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
617268fa64SSam Leffler 
628a1b9b6aSSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
638a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *);
648a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *);
658a1b9b6aSSam Leffler static u_int8_t node_getrssi(const struct ieee80211_node *);
661a1e1d21SSam Leffler 
678a1b9b6aSSam Leffler static void ieee80211_setup_node(struct ieee80211_node_table *,
688a1b9b6aSSam Leffler 		struct ieee80211_node *, const u_int8_t *);
698a1b9b6aSSam Leffler static void _ieee80211_free_node(struct ieee80211_node *);
708a1b9b6aSSam Leffler static void ieee80211_free_allnodes(struct ieee80211_node_table *);
71d1e61976SSam Leffler 
728a1b9b6aSSam Leffler static void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
738a1b9b6aSSam Leffler static void ieee80211_timeout_stations(struct ieee80211_node_table *);
748a1b9b6aSSam Leffler 
75edfa57d0SSam Leffler static void ieee80211_set_tim(struct ieee80211_node *, int set);
768a1b9b6aSSam Leffler 
778a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic,
788a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt, const char *name, int inact,
798a1b9b6aSSam Leffler 	void (*timeout)(struct ieee80211_node_table *));
808a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
811a1e1d21SSam Leffler 
8232346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
8337c150c4SSam Leffler 
841a1e1d21SSam Leffler void
858a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic)
861a1e1d21SSam Leffler {
871a1e1d21SSam Leffler 
88acc4f7f5SSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
89acc4f7f5SSam Leffler 		IEEE80211_INACT_INIT, ieee80211_timeout_stations);
908a1b9b6aSSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
918a1b9b6aSSam Leffler 		IEEE80211_INACT_SCAN, ieee80211_timeout_scan_candidates);
928a1b9b6aSSam Leffler 
938a1b9b6aSSam Leffler 	ic->ic_node_alloc = node_alloc;
948a1b9b6aSSam Leffler 	ic->ic_node_free = node_free;
958a1b9b6aSSam Leffler 	ic->ic_node_cleanup = node_cleanup;
968a1b9b6aSSam Leffler 	ic->ic_node_getrssi = node_getrssi;
978a1b9b6aSSam Leffler 
988a1b9b6aSSam Leffler 	/* default station inactivity timer setings */
998a1b9b6aSSam Leffler 	ic->ic_inact_init = IEEE80211_INACT_INIT;
1008a1b9b6aSSam Leffler 	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
1018a1b9b6aSSam Leffler 	ic->ic_inact_run = IEEE80211_INACT_RUN;
1028a1b9b6aSSam Leffler 	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
1038a1b9b6aSSam Leffler 
1048a1b9b6aSSam Leffler 	/* XXX defer */
1058a1b9b6aSSam Leffler 	if (ic->ic_max_aid == 0)
1068a1b9b6aSSam Leffler 		ic->ic_max_aid = IEEE80211_AID_DEF;
1078a1b9b6aSSam Leffler 	else if (ic->ic_max_aid > IEEE80211_AID_MAX)
1088a1b9b6aSSam Leffler 		ic->ic_max_aid = IEEE80211_AID_MAX;
1098a1b9b6aSSam Leffler 	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
1108a1b9b6aSSam Leffler 		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
1118a1b9b6aSSam Leffler 		M_DEVBUF, M_NOWAIT | M_ZERO);
1128a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap == NULL) {
1138a1b9b6aSSam Leffler 		/* XXX no way to recover */
1148a1b9b6aSSam Leffler 		printf("%s: no memory for AID bitmap!\n", __func__);
1158a1b9b6aSSam Leffler 		ic->ic_max_aid = 0;
1168a1b9b6aSSam Leffler 	}
1178a1b9b6aSSam Leffler 
1188a1b9b6aSSam Leffler 	/* XXX defer until using hostap/ibss mode */
1198a1b9b6aSSam Leffler 	ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
1208a1b9b6aSSam Leffler 	MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
1218a1b9b6aSSam Leffler 		M_DEVBUF, M_NOWAIT | M_ZERO);
1228a1b9b6aSSam Leffler 	if (ic->ic_tim_bitmap == NULL) {
1238a1b9b6aSSam Leffler 		/* XXX no way to recover */
1248a1b9b6aSSam Leffler 		printf("%s: no memory for TIM bitmap!\n", __func__);
1258a1b9b6aSSam Leffler 	}
1268a1b9b6aSSam Leffler 	ic->ic_set_tim = ieee80211_set_tim;	/* NB: driver should override */
1272692bb26SSam Leffler }
1282692bb26SSam Leffler 
1292692bb26SSam Leffler void
1308a1b9b6aSSam Leffler ieee80211_node_lateattach(struct ieee80211com *ic)
1312692bb26SSam Leffler {
132849b8980SSam Leffler 	struct ieee80211_node *ni;
1338a1b9b6aSSam Leffler 	struct ieee80211_rsnparms *rsn;
1342692bb26SSam Leffler 
1358a1b9b6aSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
136849b8980SSam Leffler 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
1378a1b9b6aSSam Leffler 	/*
1388a1b9b6aSSam Leffler 	 * Setup "global settings" in the bss node so that
1398a1b9b6aSSam Leffler 	 * each new station automatically inherits them.
1408a1b9b6aSSam Leffler 	 */
1418a1b9b6aSSam Leffler 	rsn = &ni->ni_rsn;
1428a1b9b6aSSam Leffler 	/* WEP, TKIP, and AES-CCM are always supported */
1438a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
1448a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
1458a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
1468a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_AES)
1478a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
1488a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_CKIP)
1498a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
1508a1b9b6aSSam Leffler 	/*
1518a1b9b6aSSam Leffler 	 * Default unicast cipher to WEP for 802.1x use.  If
1528a1b9b6aSSam Leffler 	 * WPA is enabled the management code will set these
1538a1b9b6aSSam Leffler 	 * values to reflect.
1548a1b9b6aSSam Leffler 	 */
1558a1b9b6aSSam Leffler 	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
1568a1b9b6aSSam Leffler 	rsn->rsn_ucastkeylen = 104 / NBBY;
1578a1b9b6aSSam Leffler 	/*
1588a1b9b6aSSam Leffler 	 * WPA says the multicast cipher is the lowest unicast
1598a1b9b6aSSam Leffler 	 * cipher supported.  But we skip WEP which would
1608a1b9b6aSSam Leffler 	 * otherwise be used based on this criteria.
1618a1b9b6aSSam Leffler 	 */
1628a1b9b6aSSam Leffler 	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
1638a1b9b6aSSam Leffler 	rsn->rsn_mcastkeylen = 128 / NBBY;
1648a1b9b6aSSam Leffler 
1658a1b9b6aSSam Leffler 	/*
1668a1b9b6aSSam Leffler 	 * We support both WPA-PSK and 802.1x; the one used
1678a1b9b6aSSam Leffler 	 * is determined by the authentication mode and the
1688a1b9b6aSSam Leffler 	 * setting of the PSK state.
1698a1b9b6aSSam Leffler 	 */
1708a1b9b6aSSam Leffler 	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
1718a1b9b6aSSam Leffler 	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1728a1b9b6aSSam Leffler 
1738a1b9b6aSSam Leffler 	ic->ic_bss = ieee80211_ref_node(ni);		/* hold reference */
1748a1b9b6aSSam Leffler 	ic->ic_auth = ieee80211_authenticator_get(ni->ni_authmode);
1751a1e1d21SSam Leffler }
1761a1e1d21SSam Leffler 
1771a1e1d21SSam Leffler void
1788a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic)
1791a1e1d21SSam Leffler {
1801a1e1d21SSam Leffler 
1818a1b9b6aSSam Leffler 	if (ic->ic_bss != NULL) {
1828a1b9b6aSSam Leffler 		ieee80211_free_node(ic->ic_bss);
1838a1b9b6aSSam Leffler 		ic->ic_bss = NULL;
1848a1b9b6aSSam Leffler 	}
1858a1b9b6aSSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_scan);
186acc4f7f5SSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_sta);
1878a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap != NULL) {
1888a1b9b6aSSam Leffler 		FREE(ic->ic_aid_bitmap, M_DEVBUF);
1898a1b9b6aSSam Leffler 		ic->ic_aid_bitmap = NULL;
1908a1b9b6aSSam Leffler 	}
1918a1b9b6aSSam Leffler 	if (ic->ic_tim_bitmap != NULL) {
1928a1b9b6aSSam Leffler 		FREE(ic->ic_tim_bitmap, M_DEVBUF);
1938a1b9b6aSSam Leffler 		ic->ic_tim_bitmap = NULL;
1948a1b9b6aSSam Leffler 	}
1958a1b9b6aSSam Leffler }
1968a1b9b6aSSam Leffler 
1978a1b9b6aSSam Leffler /*
1988a1b9b6aSSam Leffler  * Port authorize/unauthorize interfaces for use by an authenticator.
1998a1b9b6aSSam Leffler  */
2008a1b9b6aSSam Leffler 
2018a1b9b6aSSam Leffler void
202e4918ecdSSam Leffler ieee80211_node_authorize(struct ieee80211_node *ni)
2038a1b9b6aSSam Leffler {
204e4918ecdSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
205e4918ecdSSam Leffler 
2068a1b9b6aSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AUTH;
2072045f699SSam Leffler 	ni->ni_inact_reload = ic->ic_inact_run;
2088a1b9b6aSSam Leffler }
2098a1b9b6aSSam Leffler 
2108a1b9b6aSSam Leffler void
211e4918ecdSSam Leffler ieee80211_node_unauthorize(struct ieee80211_node *ni)
2128a1b9b6aSSam Leffler {
2138a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
2148a1b9b6aSSam Leffler }
2158a1b9b6aSSam Leffler 
2168a1b9b6aSSam Leffler /*
2178a1b9b6aSSam Leffler  * Set/change the channel.  The rate set is also updated as
2188a1b9b6aSSam Leffler  * to insure a consistent view by drivers.
2198a1b9b6aSSam Leffler  */
2208a1b9b6aSSam Leffler static __inline void
2218a1b9b6aSSam Leffler ieee80211_set_chan(struct ieee80211com *ic,
2228a1b9b6aSSam Leffler 	struct ieee80211_node *ni, struct ieee80211_channel *chan)
2238a1b9b6aSSam Leffler {
2248a1b9b6aSSam Leffler 	ni->ni_chan = chan;
2258a1b9b6aSSam Leffler 	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
2261a1e1d21SSam Leffler }
2271a1e1d21SSam Leffler 
2281a1e1d21SSam Leffler /*
2291a1e1d21SSam Leffler  * AP scanning support.
2301a1e1d21SSam Leffler  */
2311a1e1d21SSam Leffler 
2328a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
2338a1b9b6aSSam Leffler static void
2348a1b9b6aSSam Leffler dump_chanlist(const u_char chans[])
2358a1b9b6aSSam Leffler {
2368a1b9b6aSSam Leffler 	const char *sep;
2378a1b9b6aSSam Leffler 	int i;
2388a1b9b6aSSam Leffler 
2398a1b9b6aSSam Leffler 	sep = " ";
2408a1b9b6aSSam Leffler 	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
2418a1b9b6aSSam Leffler 		if (isset(chans, i)) {
2428a1b9b6aSSam Leffler 			printf("%s%u", sep, i);
2438a1b9b6aSSam Leffler 			sep = ", ";
2448a1b9b6aSSam Leffler 		}
2458a1b9b6aSSam Leffler }
2468a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2478a1b9b6aSSam Leffler 
2481a1e1d21SSam Leffler /*
2498a1b9b6aSSam Leffler  * Initialize the channel set to scan based on the
2501a1e1d21SSam Leffler  * of available channels and the current PHY mode.
2511a1e1d21SSam Leffler  */
252a11c9a5cSSam Leffler static void
2538a1b9b6aSSam Leffler ieee80211_reset_scan(struct ieee80211com *ic)
2541a1e1d21SSam Leffler {
2551a1e1d21SSam Leffler 
2568a1b9b6aSSam Leffler 	/* XXX ic_des_chan should be handled with ic_chan_active */
2578a1b9b6aSSam Leffler 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
2588a1b9b6aSSam Leffler 		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
2598a1b9b6aSSam Leffler 		setbit(ic->ic_chan_scan,
2608a1b9b6aSSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_des_chan));
2618a1b9b6aSSam Leffler 	} else
2621a1e1d21SSam Leffler 		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
2631a1e1d21SSam Leffler 			sizeof(ic->ic_chan_active));
264a11c9a5cSSam Leffler 	/* NB: hack, setup so next_scan starts with the first channel */
265a11c9a5cSSam Leffler 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
2668a1b9b6aSSam Leffler 		ieee80211_set_chan(ic, ic->ic_bss,
2678a1b9b6aSSam Leffler 			&ic->ic_channels[IEEE80211_CHAN_MAX]);
2688a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
2698a1b9b6aSSam Leffler 	if (ieee80211_msg_scan(ic)) {
2708a1b9b6aSSam Leffler 		printf("%s: scan set:", __func__);
2718a1b9b6aSSam Leffler 		dump_chanlist(ic->ic_chan_scan);
2728a1b9b6aSSam Leffler 		printf(" start chan %u\n",
2738a1b9b6aSSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
2748a1b9b6aSSam Leffler 	}
2758a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2761a1e1d21SSam Leffler }
2771a1e1d21SSam Leffler 
2781a1e1d21SSam Leffler /*
2791a1e1d21SSam Leffler  * Begin an active scan.
2801a1e1d21SSam Leffler  */
2811a1e1d21SSam Leffler void
2828a1b9b6aSSam Leffler ieee80211_begin_scan(struct ieee80211com *ic, int reset)
2831a1e1d21SSam Leffler {
2841a1e1d21SSam Leffler 
2858a1b9b6aSSam Leffler 	ic->ic_scan.nt_scangen++;
286a11c9a5cSSam Leffler 	/*
287a11c9a5cSSam Leffler 	 * In all but hostap mode scanning starts off in
288a11c9a5cSSam Leffler 	 * an active mode before switching to passive.
289a11c9a5cSSam Leffler 	 */
2901be50176SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
291a11c9a5cSSam Leffler 		ic->ic_flags |= IEEE80211_F_ASCAN;
2921be50176SSam Leffler 		ic->ic_stats.is_scan_active++;
2931be50176SSam Leffler 	} else
2941be50176SSam Leffler 		ic->ic_stats.is_scan_passive++;
2953ea67c54SSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
2963ea67c54SSam Leffler 		"begin %s scan in %s mode, scangen %u\n",
2978a1b9b6aSSam Leffler 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
2983ea67c54SSam Leffler 		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
2991a1e1d21SSam Leffler 	/*
3008a1b9b6aSSam Leffler 	 * Clear scan state and flush any previously seen AP's.
3011a1e1d21SSam Leffler 	 */
3028a1b9b6aSSam Leffler 	ieee80211_reset_scan(ic);
3038a1b9b6aSSam Leffler 	if (reset)
3048a1b9b6aSSam Leffler 		ieee80211_free_allnodes(&ic->ic_scan);
3058a1b9b6aSSam Leffler 
3068a1b9b6aSSam Leffler 	ic->ic_flags |= IEEE80211_F_SCAN;
3071a1e1d21SSam Leffler 
308a11c9a5cSSam Leffler 	/* Scan the next channel. */
3098a1b9b6aSSam Leffler 	ieee80211_next_scan(ic);
3101a1e1d21SSam Leffler }
3111a1e1d21SSam Leffler 
3121a1e1d21SSam Leffler /*
3131a1e1d21SSam Leffler  * Switch to the next channel marked for scanning.
3141a1e1d21SSam Leffler  */
3158a1b9b6aSSam Leffler int
3168a1b9b6aSSam Leffler ieee80211_next_scan(struct ieee80211com *ic)
3171a1e1d21SSam Leffler {
3181a1e1d21SSam Leffler 	struct ieee80211_channel *chan;
3191a1e1d21SSam Leffler 
3208a1b9b6aSSam Leffler 	/*
3218a1b9b6aSSam Leffler 	 * Insure any previous mgt frame timeouts don't fire.
3228a1b9b6aSSam Leffler 	 * This assumes the driver does the right thing in
3238a1b9b6aSSam Leffler 	 * flushing anything queued in the driver and below.
3248a1b9b6aSSam Leffler 	 */
3258a1b9b6aSSam Leffler 	ic->ic_mgt_timer = 0;
3268a1b9b6aSSam Leffler 
3271a1e1d21SSam Leffler 	chan = ic->ic_bss->ni_chan;
3288a1b9b6aSSam Leffler 	do {
3291a1e1d21SSam Leffler 		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
3301a1e1d21SSam Leffler 			chan = &ic->ic_channels[0];
3311a1e1d21SSam Leffler 		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
3321a1e1d21SSam Leffler 			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
3338a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
3348a1b9b6aSSam Leffler 			    "%s: chan %d->%d\n", __func__,
3351a1e1d21SSam Leffler 			    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
3368a1b9b6aSSam Leffler 			    ieee80211_chan2ieee(ic, chan));
3378a1b9b6aSSam Leffler 			ieee80211_set_chan(ic, ic->ic_bss, chan);
3388a1b9b6aSSam Leffler #ifdef notyet
3398a1b9b6aSSam Leffler 			/* XXX driver state change */
3408a1b9b6aSSam Leffler 			/*
3418a1b9b6aSSam Leffler 			 * Scan next channel. If doing an active scan
3428a1b9b6aSSam Leffler 			 * and the channel is not marked passive-only
3438a1b9b6aSSam Leffler 			 * then send a probe request.  Otherwise just
3448a1b9b6aSSam Leffler 			 * listen for beacons on the channel.
3458a1b9b6aSSam Leffler 			 */
3468a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_ASCAN) &&
3478a1b9b6aSSam Leffler 			    (ni->ni_chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) {
3488a1b9b6aSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
3498a1b9b6aSSam Leffler 				    IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
3508a1b9b6aSSam Leffler 			}
3518a1b9b6aSSam Leffler #else
352a11c9a5cSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
3538a1b9b6aSSam Leffler #endif
3548a1b9b6aSSam Leffler 			return 1;
3558a1b9b6aSSam Leffler 		}
3568a1b9b6aSSam Leffler 	} while (chan != ic->ic_bss->ni_chan);
3578a1b9b6aSSam Leffler 	ieee80211_end_scan(ic);
3588a1b9b6aSSam Leffler 	return 0;
3591a1e1d21SSam Leffler }
3601a1e1d21SSam Leffler 
361f9cd9174SSam Leffler static __inline void
362f9cd9174SSam Leffler copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
363f9cd9174SSam Leffler {
364f9cd9174SSam Leffler 	/* propagate useful state */
365f9cd9174SSam Leffler 	nbss->ni_authmode = obss->ni_authmode;
366f9cd9174SSam Leffler 	nbss->ni_txpower = obss->ni_txpower;
367f9cd9174SSam Leffler 	nbss->ni_vlan = obss->ni_vlan;
368f9cd9174SSam Leffler 	nbss->ni_rsn = obss->ni_rsn;
369f9cd9174SSam Leffler 	/* XXX statistics? */
370f9cd9174SSam Leffler }
371f9cd9174SSam Leffler 
3721a1e1d21SSam Leffler void
3731a1e1d21SSam Leffler ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
3741a1e1d21SSam Leffler {
375acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt;
3761a1e1d21SSam Leffler 	struct ieee80211_node *ni;
3778a1b9b6aSSam Leffler 
3788a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
3798a1b9b6aSSam Leffler 		"%s: creating ibss\n", __func__);
3808a1b9b6aSSam Leffler 
3818a1b9b6aSSam Leffler 	/*
3828a1b9b6aSSam Leffler 	 * Create the station/neighbor table.  Note that for adhoc
3838a1b9b6aSSam Leffler 	 * mode we make the initial inactivity timer longer since
3848a1b9b6aSSam Leffler 	 * we create nodes only through discovery and they typically
3858a1b9b6aSSam Leffler 	 * are long-lived associations.
3868a1b9b6aSSam Leffler 	 */
387acc4f7f5SSam Leffler 	nt = &ic->ic_sta;
388acc4f7f5SSam Leffler 	IEEE80211_NODE_LOCK(nt);
389acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
390acc4f7f5SSam Leffler 		nt->nt_name = "station";
391acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_init;
392acc4f7f5SSam Leffler 	} else {
393acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
394acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
395acc4f7f5SSam Leffler 	}
396acc4f7f5SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
397acc4f7f5SSam Leffler 
398acc4f7f5SSam Leffler 	ni = ieee80211_alloc_node(nt, ic->ic_myaddr);
399acc4f7f5SSam Leffler 	if (ni == NULL) {
400acc4f7f5SSam Leffler 		/* XXX recovery? */
4018a1b9b6aSSam Leffler 		return;
4028a1b9b6aSSam Leffler 	}
4031a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
4041a1e1d21SSam Leffler 	ni->ni_esslen = ic->ic_des_esslen;
4051a1e1d21SSam Leffler 	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
406f9cd9174SSam Leffler 	copy_bss(ni, ic->ic_bss);
4071a1e1d21SSam Leffler 	ni->ni_intval = ic->ic_lintval;
4088a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
4091a1e1d21SSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
4101a1e1d21SSam Leffler 	if (ic->ic_phytype == IEEE80211_T_FH) {
4111a1e1d21SSam Leffler 		ni->ni_fhdwell = 200;	/* XXX */
4121a1e1d21SSam Leffler 		ni->ni_fhindex = 1;
4131a1e1d21SSam Leffler 	}
4148a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
4158a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SIBSS;
4168a1b9b6aSSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
41748b0a5beSSam Leffler 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
41848b0a5beSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
41948b0a5beSSam Leffler 		else
4208a1b9b6aSSam Leffler 			ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
4218a1b9b6aSSam Leffler 	}
4228a1b9b6aSSam Leffler 	/*
4238a1b9b6aSSam Leffler 	 * Fix the channel and related attributes.
4248a1b9b6aSSam Leffler 	 */
4258a1b9b6aSSam Leffler 	ieee80211_set_chan(ic, ni, chan);
4268a1b9b6aSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
4278a1b9b6aSSam Leffler 	/*
4288a1b9b6aSSam Leffler 	 * Do mode-specific rate setup.
4298a1b9b6aSSam Leffler 	 */
4308a1b9b6aSSam Leffler 	if (ic->ic_curmode == IEEE80211_MODE_11G) {
4318a1b9b6aSSam Leffler 		/*
4328a1b9b6aSSam Leffler 		 * Use a mixed 11b/11g rate set.
4338a1b9b6aSSam Leffler 		 */
4348a1b9b6aSSam Leffler 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
4358a1b9b6aSSam Leffler 	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
4368a1b9b6aSSam Leffler 		/*
4378a1b9b6aSSam Leffler 		 * Force pure 11b rate set.
4388a1b9b6aSSam Leffler 		 */
4398a1b9b6aSSam Leffler 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
4408a1b9b6aSSam Leffler 	}
4418a1b9b6aSSam Leffler 
442acc4f7f5SSam Leffler 	(void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
4431a1e1d21SSam Leffler }
4441a1e1d21SSam Leffler 
4458a1b9b6aSSam Leffler void
4468a1b9b6aSSam Leffler ieee80211_reset_bss(struct ieee80211com *ic)
447b4c5a90fSSam Leffler {
4488a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *obss;
4498a1b9b6aSSam Leffler 
4508a1b9b6aSSam Leffler 	ieee80211_node_table_reset(&ic->ic_scan);
451acc4f7f5SSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta);
452acc4f7f5SSam Leffler 
4538a1b9b6aSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
4548a1b9b6aSSam Leffler 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
4558a1b9b6aSSam Leffler 	obss = ic->ic_bss;
4568a1b9b6aSSam Leffler 	ic->ic_bss = ieee80211_ref_node(ni);
457f9cd9174SSam Leffler 	if (obss != NULL) {
458f9cd9174SSam Leffler 		copy_bss(ni, obss);
459f9cd9174SSam Leffler 		ni->ni_intval = ic->ic_lintval;
4608a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
4618a1b9b6aSSam Leffler 	}
462f9cd9174SSam Leffler }
4638a1b9b6aSSam Leffler 
4648a1b9b6aSSam Leffler static int
4658a1b9b6aSSam Leffler ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
4668a1b9b6aSSam Leffler {
467b4c5a90fSSam Leffler         u_int8_t rate;
468b4c5a90fSSam Leffler         int fail;
469b4c5a90fSSam Leffler 
470b4c5a90fSSam Leffler 	fail = 0;
471b4c5a90fSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
472b4c5a90fSSam Leffler 		fail |= 0x01;
473b4c5a90fSSam Leffler 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
474b4c5a90fSSam Leffler 	    ni->ni_chan != ic->ic_des_chan)
475b4c5a90fSSam Leffler 		fail |= 0x01;
476b4c5a90fSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
477b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
478b4c5a90fSSam Leffler 			fail |= 0x02;
479b4c5a90fSSam Leffler 	} else {
480b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
481b4c5a90fSSam Leffler 			fail |= 0x02;
482b4c5a90fSSam Leffler 	}
4838a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
484b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
485b4c5a90fSSam Leffler 			fail |= 0x04;
486b4c5a90fSSam Leffler 	} else {
487b4c5a90fSSam Leffler 		/* XXX does this mean privacy is supported or required? */
488b4c5a90fSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
489b4c5a90fSSam Leffler 			fail |= 0x04;
490b4c5a90fSSam Leffler 	}
4917d77cd53SSam Leffler 	rate = ieee80211_fix_rate(ni, IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
492b4c5a90fSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
493b4c5a90fSSam Leffler 		fail |= 0x08;
494b4c5a90fSSam Leffler 	if (ic->ic_des_esslen != 0 &&
495b4c5a90fSSam Leffler 	    (ni->ni_esslen != ic->ic_des_esslen ||
496b4c5a90fSSam Leffler 	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
497b4c5a90fSSam Leffler 		fail |= 0x10;
498b4c5a90fSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
499b4c5a90fSSam Leffler 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
500b4c5a90fSSam Leffler 		fail |= 0x20;
501b4c5a90fSSam Leffler #ifdef IEEE80211_DEBUG
5028a1b9b6aSSam Leffler 	if (ieee80211_msg_scan(ic)) {
503b4c5a90fSSam Leffler 		printf(" %c %s", fail ? '-' : '+',
504b4c5a90fSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
505b4c5a90fSSam Leffler 		printf(" %s%c", ether_sprintf(ni->ni_bssid),
506b4c5a90fSSam Leffler 		    fail & 0x20 ? '!' : ' ');
507b4c5a90fSSam Leffler 		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
508b4c5a90fSSam Leffler 			fail & 0x01 ? '!' : ' ');
509b4c5a90fSSam Leffler 		printf(" %+4d", ni->ni_rssi);
510b4c5a90fSSam Leffler 		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
511b4c5a90fSSam Leffler 		    fail & 0x08 ? '!' : ' ');
512b4c5a90fSSam Leffler 		printf(" %4s%c",
513b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
514b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
515b4c5a90fSSam Leffler 		    "????",
516b4c5a90fSSam Leffler 		    fail & 0x02 ? '!' : ' ');
517b4c5a90fSSam Leffler 		printf(" %3s%c ",
518b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
519b4c5a90fSSam Leffler 		    "wep" : "no",
520b4c5a90fSSam Leffler 		    fail & 0x04 ? '!' : ' ');
521b4c5a90fSSam Leffler 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
522b4c5a90fSSam Leffler 		printf("%s\n", fail & 0x10 ? "!" : "");
523b4c5a90fSSam Leffler 	}
524b4c5a90fSSam Leffler #endif
525b4c5a90fSSam Leffler 	return fail;
526b4c5a90fSSam Leffler }
527b4c5a90fSSam Leffler 
5288a1b9b6aSSam Leffler static __inline u_int8_t
5298a1b9b6aSSam Leffler maxrate(const struct ieee80211_node *ni)
5308a1b9b6aSSam Leffler {
5318a1b9b6aSSam Leffler 	const struct ieee80211_rateset *rs = &ni->ni_rates;
5328a1b9b6aSSam Leffler 	/* NB: assumes rate set is sorted (happens on frame receive) */
5338a1b9b6aSSam Leffler 	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
5348a1b9b6aSSam Leffler }
5358a1b9b6aSSam Leffler 
5368a1b9b6aSSam Leffler /*
5378a1b9b6aSSam Leffler  * Compare the capabilities of two nodes and decide which is
5388a1b9b6aSSam Leffler  * more desirable (return >0 if a is considered better).  Note
5398a1b9b6aSSam Leffler  * that we assume compatibility/usability has already been checked
5408a1b9b6aSSam Leffler  * so we don't need to (e.g. validate whether privacy is supported).
5418a1b9b6aSSam Leffler  * Used to select the best scan candidate for association in a BSS.
5428a1b9b6aSSam Leffler  */
5438a1b9b6aSSam Leffler static int
5448a1b9b6aSSam Leffler ieee80211_node_compare(struct ieee80211com *ic,
5458a1b9b6aSSam Leffler 		       const struct ieee80211_node *a,
5468a1b9b6aSSam Leffler 		       const struct ieee80211_node *b)
5478a1b9b6aSSam Leffler {
5488a1b9b6aSSam Leffler 	u_int8_t maxa, maxb;
5498a1b9b6aSSam Leffler 	u_int8_t rssia, rssib;
5508a1b9b6aSSam Leffler 
5518a1b9b6aSSam Leffler 	/* privacy support preferred */
5528a1b9b6aSSam Leffler 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
5538a1b9b6aSSam Leffler 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
5548a1b9b6aSSam Leffler 		return 1;
5558a1b9b6aSSam Leffler 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
5568a1b9b6aSSam Leffler 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
5578a1b9b6aSSam Leffler 		return -1;
5588a1b9b6aSSam Leffler 
559ef92bcdcSSam Leffler 	rssia = ic->ic_node_getrssi(a);
560ef92bcdcSSam Leffler 	rssib = ic->ic_node_getrssi(b);
561ef92bcdcSSam Leffler 	if (abs(rssib - rssia) < 5) {
5628a1b9b6aSSam Leffler 		/* best/max rate preferred if signal level close enough XXX */
5638a1b9b6aSSam Leffler 		maxa = maxrate(a);
5648a1b9b6aSSam Leffler 		maxb = maxrate(b);
565ef92bcdcSSam Leffler 		if (maxa != maxb)
5668a1b9b6aSSam Leffler 			return maxa - maxb;
5678a1b9b6aSSam Leffler 		/* XXX use freq for channel preference */
5688a1b9b6aSSam Leffler 		/* for now just prefer 5Ghz band to all other bands */
5698a1b9b6aSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
5708a1b9b6aSSam Leffler 		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
5718a1b9b6aSSam Leffler 			return 1;
5728a1b9b6aSSam Leffler 		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
5738a1b9b6aSSam Leffler 		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
5748a1b9b6aSSam Leffler 			return -1;
575ef92bcdcSSam Leffler 	}
5768a1b9b6aSSam Leffler 	/* all things being equal, use signal level */
5778a1b9b6aSSam Leffler 	return rssia - rssib;
5788a1b9b6aSSam Leffler }
5798a1b9b6aSSam Leffler 
5801a1e1d21SSam Leffler /*
581c75ac469SSam Leffler  * Mark an ongoing scan stopped.
582c75ac469SSam Leffler  */
583c75ac469SSam Leffler void
584c75ac469SSam Leffler ieee80211_cancel_scan(struct ieee80211com *ic)
585c75ac469SSam Leffler {
586c75ac469SSam Leffler 
587c75ac469SSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
588c75ac469SSam Leffler 		__func__,
589c75ac469SSam Leffler 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
590c75ac469SSam Leffler 
591c75ac469SSam Leffler 	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
592c75ac469SSam Leffler }
593c75ac469SSam Leffler 
594c75ac469SSam Leffler /*
5951a1e1d21SSam Leffler  * Complete a scan of potential channels.
5961a1e1d21SSam Leffler  */
5971a1e1d21SSam Leffler void
5988a1b9b6aSSam Leffler ieee80211_end_scan(struct ieee80211com *ic)
5991a1e1d21SSam Leffler {
6003fcfbbfaSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_scan;
6013fcfbbfaSSam Leffler 	struct ieee80211_node *ni, *selbs;
6021a1e1d21SSam Leffler 
603c75ac469SSam Leffler 	ieee80211_cancel_scan(ic);
604c75ac469SSam Leffler 	ieee80211_notify_scan_done(ic);
6058a1b9b6aSSam Leffler 
6061a1e1d21SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
6078a1b9b6aSSam Leffler 		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
6088a1b9b6aSSam Leffler 		int i, bestchan;
6098a1b9b6aSSam Leffler 		u_int8_t rssi;
6108a1b9b6aSSam Leffler 
6111a1e1d21SSam Leffler 		/*
6121a1e1d21SSam Leffler 		 * The passive scan to look for existing AP's completed,
6131a1e1d21SSam Leffler 		 * select a channel to camp on.  Identify the channels
6141a1e1d21SSam Leffler 		 * that already have one or more AP's and try to locate
615acc4f7f5SSam Leffler 		 * an unoccupied one.  If that fails, pick a channel that
6168a1b9b6aSSam Leffler 		 * looks to be quietest.
6171a1e1d21SSam Leffler 		 */
6188a1b9b6aSSam Leffler 		memset(maxrssi, 0, sizeof(maxrssi));
6193fcfbbfaSSam Leffler 		IEEE80211_NODE_LOCK(nt);
6203fcfbbfaSSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
6218a1b9b6aSSam Leffler 			rssi = ic->ic_node_getrssi(ni);
6228a1b9b6aSSam Leffler 			i = ieee80211_chan2ieee(ic, ni->ni_chan);
6238a1b9b6aSSam Leffler 			if (rssi > maxrssi[i])
6248a1b9b6aSSam Leffler 				maxrssi[i] = rssi;
6251a1e1d21SSam Leffler 		}
6263fcfbbfaSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
6278a1b9b6aSSam Leffler 		/* XXX select channel more intelligently */
6288a1b9b6aSSam Leffler 		bestchan = -1;
6291a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
6308a1b9b6aSSam Leffler 			if (isset(ic->ic_chan_active, i)) {
6318a1b9b6aSSam Leffler 				/*
6328a1b9b6aSSam Leffler 				 * If the channel is unoccupied the max rssi
6338a1b9b6aSSam Leffler 				 * should be zero; just take it.  Otherwise
6348a1b9b6aSSam Leffler 				 * track the channel with the lowest rssi and
6358a1b9b6aSSam Leffler 				 * use that when all channels appear occupied.
6368a1b9b6aSSam Leffler 				 */
6378a1b9b6aSSam Leffler 				if (maxrssi[i] == 0) {
6388a1b9b6aSSam Leffler 					bestchan = i;
6391a1e1d21SSam Leffler 					break;
6401a1e1d21SSam Leffler 				}
6416edf09a6SSam Leffler 				if (bestchan == -1 ||
6426edf09a6SSam Leffler 				    maxrssi[i] < maxrssi[bestchan])
6438a1b9b6aSSam Leffler 					bestchan = i;
6448a1b9b6aSSam Leffler 			}
6458a1b9b6aSSam Leffler 		if (bestchan != -1) {
6468a1b9b6aSSam Leffler 			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
6471a1e1d21SSam Leffler 			return;
6481a1e1d21SSam Leffler 		}
6498a1b9b6aSSam Leffler 		/* no suitable channel, should not happen */
6508a1b9b6aSSam Leffler 	}
6518a1b9b6aSSam Leffler 
6528a1b9b6aSSam Leffler 	/*
6538a1b9b6aSSam Leffler 	 * When manually sequencing the state machine; scan just once
6548a1b9b6aSSam Leffler 	 * regardless of whether we have a candidate or not.  The
6558a1b9b6aSSam Leffler 	 * controlling application is expected to setup state and
6568a1b9b6aSSam Leffler 	 * initiate an association.
6578a1b9b6aSSam Leffler 	 */
6588a1b9b6aSSam Leffler 	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
6598a1b9b6aSSam Leffler 		return;
6608a1b9b6aSSam Leffler 	/*
6618a1b9b6aSSam Leffler 	 * Automatic sequencing; look for a candidate and
6628a1b9b6aSSam Leffler 	 * if found join the network.
6638a1b9b6aSSam Leffler 	 */
6643fcfbbfaSSam Leffler 	/* NB: unlocked read should be ok */
6653fcfbbfaSSam Leffler 	if (TAILQ_FIRST(&nt->nt_node) == NULL) {
6668a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
6678a1b9b6aSSam Leffler 			"%s: no scan candidate\n", __func__);
6681a1e1d21SSam Leffler   notfound:
6691a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
6701a1e1d21SSam Leffler 		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
6711a1e1d21SSam Leffler 		    ic->ic_des_esslen != 0) {
6721a1e1d21SSam Leffler 			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
6731a1e1d21SSam Leffler 			return;
6741a1e1d21SSam Leffler 		}
6751a1e1d21SSam Leffler 		/*
6761a1e1d21SSam Leffler 		 * Reset the list of channels to scan and start again.
6771a1e1d21SSam Leffler 		 */
6788a1b9b6aSSam Leffler 		ieee80211_reset_scan(ic);
6798a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SCAN;
6808a1b9b6aSSam Leffler 		ieee80211_next_scan(ic);
6811a1e1d21SSam Leffler 		return;
6821a1e1d21SSam Leffler 	}
6831a1e1d21SSam Leffler 	selbs = NULL;
6848a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
6858a1b9b6aSSam Leffler 	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
6863fcfbbfaSSam Leffler 	IEEE80211_NODE_LOCK(nt);
6873fcfbbfaSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
6881a1e1d21SSam Leffler 		if (ni->ni_fails) {
6891a1e1d21SSam Leffler 			/*
6901a1e1d21SSam Leffler 			 * The configuration of the access points may change
6911a1e1d21SSam Leffler 			 * during my scan.  So delete the entry for the AP
6921a1e1d21SSam Leffler 			 * and retry to associate if there is another beacon.
6931a1e1d21SSam Leffler 			 */
6948a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
6958a1b9b6aSSam Leffler 				"%s: skip scan candidate %s, fails %u\n",
6968a1b9b6aSSam Leffler 				__func__, ether_sprintf(ni->ni_macaddr),
6978a1b9b6aSSam Leffler 				ni->ni_fails);
6983fcfbbfaSSam Leffler 			ni->ni_fails++;
6993fcfbbfaSSam Leffler #if 0
7001a1e1d21SSam Leffler 			if (ni->ni_fails++ > 2)
7018a1b9b6aSSam Leffler 				ieee80211_free_node(ni);
7023fcfbbfaSSam Leffler #endif
7031a1e1d21SSam Leffler 			continue;
7041a1e1d21SSam Leffler 		}
7058a1b9b6aSSam Leffler 		if (ieee80211_match_bss(ic, ni) == 0) {
7061a1e1d21SSam Leffler 			if (selbs == NULL)
7071a1e1d21SSam Leffler 				selbs = ni;
7083fcfbbfaSSam Leffler 			else if (ieee80211_node_compare(ic, ni, selbs) > 0)
7091a1e1d21SSam Leffler 				selbs = ni;
7101a1e1d21SSam Leffler 		}
7111a1e1d21SSam Leffler 	}
7123fcfbbfaSSam Leffler 	if (selbs != NULL)		/* NB: grab ref while dropping lock */
7133fcfbbfaSSam Leffler 		(void) ieee80211_ref_node(selbs);
7143fcfbbfaSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
7151a1e1d21SSam Leffler 	if (selbs == NULL)
7161a1e1d21SSam Leffler 		goto notfound;
7178a1b9b6aSSam Leffler 	if (!ieee80211_sta_join(ic, selbs)) {
7183fcfbbfaSSam Leffler 		ieee80211_free_node(selbs);
7191a1e1d21SSam Leffler 		goto notfound;
7201a1e1d21SSam Leffler 	}
7218a1b9b6aSSam Leffler }
7228a1b9b6aSSam Leffler 
723750d6d0cSSam Leffler /*
7248a1b9b6aSSam Leffler  * Handle 802.11 ad hoc network merge.  The
7258a1b9b6aSSam Leffler  * convention, set by the Wireless Ethernet Compatibility Alliance
7268a1b9b6aSSam Leffler  * (WECA), is that an 802.11 station will change its BSSID to match
7278a1b9b6aSSam Leffler  * the "oldest" 802.11 ad hoc network, on the same channel, that
7288a1b9b6aSSam Leffler  * has the station's desired SSID.  The "oldest" 802.11 network
7298a1b9b6aSSam Leffler  * sends beacons with the greatest TSF timestamp.
7308a1b9b6aSSam Leffler  *
7318a1b9b6aSSam Leffler  * The caller is assumed to validate TSF's before attempting a merge.
7328a1b9b6aSSam Leffler  *
7338a1b9b6aSSam Leffler  * Return !0 if the BSSID changed, 0 otherwise.
734750d6d0cSSam Leffler  */
7358a1b9b6aSSam Leffler int
736641b4d0bSSam Leffler ieee80211_ibss_merge(struct ieee80211_node *ni)
7378a1b9b6aSSam Leffler {
738641b4d0bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
7398a1b9b6aSSam Leffler 
74096acc1b6SSam Leffler 	if (ni == ic->ic_bss ||
74196acc1b6SSam Leffler 	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
7428a1b9b6aSSam Leffler 		/* unchanged, nothing to do */
7438a1b9b6aSSam Leffler 		return 0;
7448a1b9b6aSSam Leffler 	}
7458a1b9b6aSSam Leffler 	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
7468a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
7478a1b9b6aSSam Leffler 		    "%s: merge failed, capabilities mismatch\n", __func__);
7488a1b9b6aSSam Leffler 		ic->ic_stats.is_ibss_capmismatch++;
7498a1b9b6aSSam Leffler 		return 0;
7508a1b9b6aSSam Leffler 	}
7518a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
7528a1b9b6aSSam Leffler 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
7538a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
7548a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
7558a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
7568a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
7578a1b9b6aSSam Leffler 	);
758acc4f7f5SSam Leffler 	return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
7598a1b9b6aSSam Leffler }
7608a1b9b6aSSam Leffler 
7618a1b9b6aSSam Leffler /*
7628a1b9b6aSSam Leffler  * Join the specified IBSS/BSS network.  The node is assumed to
7638a1b9b6aSSam Leffler  * be passed in with a held reference.
7648a1b9b6aSSam Leffler  */
7658a1b9b6aSSam Leffler int
7668a1b9b6aSSam Leffler ieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
7678a1b9b6aSSam Leffler {
7688a1b9b6aSSam Leffler 	struct ieee80211_node *obss;
7698a1b9b6aSSam Leffler 
7708a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
771acc4f7f5SSam Leffler 		struct ieee80211_node_table *nt;
7728a1b9b6aSSam Leffler 		/*
77398ff6263SSam Leffler 		 * Delete unusable rates; we've already checked
77498ff6263SSam Leffler 		 * that the negotiated rate set is acceptable.
7758a1b9b6aSSam Leffler 		 */
7767d77cd53SSam Leffler 		ieee80211_fix_rate(selbs, IEEE80211_F_DODEL);
7778a1b9b6aSSam Leffler 		/*
778acc4f7f5SSam Leffler 		 * Fillin the neighbor table; it will already
7793d073929SSam Leffler 		 * exist if we are simply switching mastership.
780acc4f7f5SSam Leffler 		 * XXX ic_sta always setup so this is unnecessary?
7818a1b9b6aSSam Leffler 		 */
782acc4f7f5SSam Leffler 		nt = &ic->ic_sta;
783acc4f7f5SSam Leffler 		IEEE80211_NODE_LOCK(nt);
784acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
785acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
786acc4f7f5SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
7873d073929SSam Leffler 	}
7881a1e1d21SSam Leffler 
7898a1b9b6aSSam Leffler 	/*
7908a1b9b6aSSam Leffler 	 * Committed to selbs, setup state.
7918a1b9b6aSSam Leffler 	 */
7928a1b9b6aSSam Leffler 	obss = ic->ic_bss;
793acc4f7f5SSam Leffler 	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
7948a1b9b6aSSam Leffler 	if (obss != NULL)
7958a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
7968a1b9b6aSSam Leffler 	/*
7978a1b9b6aSSam Leffler 	 * Set the erp state (mostly the slot time) to deal with
7988a1b9b6aSSam Leffler 	 * the auto-select case; this should be redundant if the
7998a1b9b6aSSam Leffler 	 * mode is locked.
8008a1b9b6aSSam Leffler 	 */
8018a1b9b6aSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
8028a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
8038a1b9b6aSSam Leffler 	ieee80211_wme_initparams(ic);
804acc4f7f5SSam Leffler 
805acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA)
8068a1b9b6aSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
807acc4f7f5SSam Leffler 	else
808acc4f7f5SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
8098a1b9b6aSSam Leffler 	return 1;
8108a1b9b6aSSam Leffler }
8118a1b9b6aSSam Leffler 
8128a1b9b6aSSam Leffler /*
8138a1b9b6aSSam Leffler  * Leave the specified IBSS/BSS network.  The node is assumed to
8148a1b9b6aSSam Leffler  * be passed in with a held reference.
8158a1b9b6aSSam Leffler  */
8168a1b9b6aSSam Leffler void
8178a1b9b6aSSam Leffler ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
8188a1b9b6aSSam Leffler {
8198a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
8208a1b9b6aSSam Leffler 	ieee80211_notify_node_leave(ic, ni);
8218a1b9b6aSSam Leffler }
8228a1b9b6aSSam Leffler 
8231a1e1d21SSam Leffler static struct ieee80211_node *
8248a1b9b6aSSam Leffler node_alloc(struct ieee80211_node_table *nt)
8251a1e1d21SSam Leffler {
826410ca74bSSam Leffler 	struct ieee80211_node *ni;
8278a1b9b6aSSam Leffler 
828410ca74bSSam Leffler 	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
829410ca74bSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
830410ca74bSSam Leffler 	return ni;
8311a1e1d21SSam Leffler }
8321a1e1d21SSam Leffler 
8338a1b9b6aSSam Leffler /*
8348a1b9b6aSSam Leffler  * Reclaim any resources in a node and reset any critical
8358a1b9b6aSSam Leffler  * state.  Typically nodes are free'd immediately after,
8368a1b9b6aSSam Leffler  * but in some cases the storage may be reused so we need
8378a1b9b6aSSam Leffler  * to insure consistent state (should probably fix that).
8388a1b9b6aSSam Leffler  */
8391a1e1d21SSam Leffler static void
8408a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni)
8411a1e1d21SSam Leffler {
8428a1b9b6aSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
8438a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
8448a1b9b6aSSam Leffler 	int i, qlen;
8458a1b9b6aSSam Leffler 
8468a1b9b6aSSam Leffler 	/* NB: preserve ni_table */
8478a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
8488a1b9b6aSSam Leffler 		ic->ic_ps_sta--;
8498a1b9b6aSSam Leffler 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
8508a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
8518a1b9b6aSSam Leffler 		    "[%s] power save mode off, %u sta's in ps mode\n",
8528a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
8538a1b9b6aSSam Leffler 	}
854ebdda46cSSam Leffler 	/*
855ebdda46cSSam Leffler 	 * Clear AREF flag that marks the authorization refcnt bump
856ebdda46cSSam Leffler 	 * has happened.  This is probably not needed as the node
857ebdda46cSSam Leffler 	 * should always be removed from the table so not found but
858ebdda46cSSam Leffler 	 * do it just in case.
859ebdda46cSSam Leffler 	 */
860ebdda46cSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AREF;
8618a1b9b6aSSam Leffler 
8628a1b9b6aSSam Leffler 	/*
8638a1b9b6aSSam Leffler 	 * Drain power save queue and, if needed, clear TIM.
8648a1b9b6aSSam Leffler 	 */
8658a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
8668a1b9b6aSSam Leffler 	if (qlen != 0 && ic->ic_set_tim != NULL)
867edfa57d0SSam Leffler 		ic->ic_set_tim(ni, 0);
8688a1b9b6aSSam Leffler 
8698a1b9b6aSSam Leffler 	ni->ni_associd = 0;
8708a1b9b6aSSam Leffler 	if (ni->ni_challenge != NULL) {
8718a1b9b6aSSam Leffler 		FREE(ni->ni_challenge, M_DEVBUF);
8728a1b9b6aSSam Leffler 		ni->ni_challenge = NULL;
8738a1b9b6aSSam Leffler 	}
8748a1b9b6aSSam Leffler 	/*
8758a1b9b6aSSam Leffler 	 * Preserve SSID, WPA, and WME ie's so the bss node is
8768a1b9b6aSSam Leffler 	 * reusable during a re-auth/re-assoc state transition.
8778a1b9b6aSSam Leffler 	 * If we remove these data they will not be recreated
8788a1b9b6aSSam Leffler 	 * because they come from a probe-response or beacon frame
8798a1b9b6aSSam Leffler 	 * which cannot be expected prior to the association-response.
8808a1b9b6aSSam Leffler 	 * This should not be an issue when operating in other modes
8818a1b9b6aSSam Leffler 	 * as stations leaving always go through a full state transition
8828a1b9b6aSSam Leffler 	 * which will rebuild this state.
8838a1b9b6aSSam Leffler 	 *
8848a1b9b6aSSam Leffler 	 * XXX does this leave us open to inheriting old state?
8858a1b9b6aSSam Leffler 	 */
8868a1b9b6aSSam Leffler 	for (i = 0; i < N(ni->ni_rxfrag); i++)
8878a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[i] != NULL) {
8888a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[i]);
8898a1b9b6aSSam Leffler 			ni->ni_rxfrag[i] = NULL;
8908a1b9b6aSSam Leffler 		}
8918a1b9b6aSSam Leffler 	ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
8928a1b9b6aSSam Leffler #undef N
8938a1b9b6aSSam Leffler }
8948a1b9b6aSSam Leffler 
8958a1b9b6aSSam Leffler static void
8968a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni)
8978a1b9b6aSSam Leffler {
8988a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
8998a1b9b6aSSam Leffler 
9008a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
9018a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
9028a1b9b6aSSam Leffler 		FREE(ni->ni_wpa_ie, M_DEVBUF);
9038a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
9048a1b9b6aSSam Leffler 		FREE(ni->ni_wme_ie, M_DEVBUF);
9058a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DESTROY(ni);
906410ca74bSSam Leffler 	FREE(ni, M_80211_NODE);
9071a1e1d21SSam Leffler }
9081a1e1d21SSam Leffler 
909d1e61976SSam Leffler static u_int8_t
9108a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni)
911d1e61976SSam Leffler {
912d1e61976SSam Leffler 	return ni->ni_rssi;
913d1e61976SSam Leffler }
914d1e61976SSam Leffler 
9151a1e1d21SSam Leffler static void
9168a1b9b6aSSam Leffler ieee80211_setup_node(struct ieee80211_node_table *nt,
9178a1b9b6aSSam Leffler 	struct ieee80211_node *ni, const u_int8_t *macaddr)
9181a1e1d21SSam Leffler {
9198a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9201a1e1d21SSam Leffler 	int hash;
9211a1e1d21SSam Leffler 
9228a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
92349a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
9248a1b9b6aSSam Leffler 		ether_sprintf(macaddr), nt->nt_name);
9258a1b9b6aSSam Leffler 
9261a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
9271a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
9288a1b9b6aSSam Leffler 	ieee80211_node_initref(ni);		/* mark referenced */
9298a1b9b6aSSam Leffler 	ni->ni_chan = IEEE80211_CHAN_ANYC;
9308a1b9b6aSSam Leffler 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
9318a1b9b6aSSam Leffler 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
9328a1b9b6aSSam Leffler 	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
9332045f699SSam Leffler 	ni->ni_inact_reload = nt->nt_inact_init;
9342045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
9358a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
9368a1b9b6aSSam Leffler 
9378a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
9388a1b9b6aSSam Leffler 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
9398a1b9b6aSSam Leffler 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
9408a1b9b6aSSam Leffler 	ni->ni_table = nt;
9418a1b9b6aSSam Leffler 	ni->ni_ic = ic;
9428a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
9431a1e1d21SSam Leffler }
9441a1e1d21SSam Leffler 
9451a1e1d21SSam Leffler struct ieee80211_node *
9468a1b9b6aSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
9471a1e1d21SSam Leffler {
9488a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9498a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9508a1b9b6aSSam Leffler 
9518a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
9521a1e1d21SSam Leffler 	if (ni != NULL)
9538a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
954c64bfa0fSSam Leffler 	else
955c64bfa0fSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
9561a1e1d21SSam Leffler 	return ni;
9571a1e1d21SSam Leffler }
9581a1e1d21SSam Leffler 
9591a1e1d21SSam Leffler struct ieee80211_node *
9608a1b9b6aSSam Leffler ieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
9611a1e1d21SSam Leffler {
9628a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9638a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9648a1b9b6aSSam Leffler 
9658a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
9661a1e1d21SSam Leffler 	if (ni != NULL) {
9678a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
968694dca64SSam Leffler 		/*
969694dca64SSam Leffler 		 * Inherit from ic_bss.
970694dca64SSam Leffler 		 */
9718a1b9b6aSSam Leffler 		ni->ni_authmode = ic->ic_bss->ni_authmode;
9728a1b9b6aSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
9738a1b9b6aSSam Leffler 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
974694dca64SSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
9758a1b9b6aSSam Leffler 		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
9768a1b9b6aSSam Leffler 		ni->ni_rsn = ic->ic_bss->ni_rsn;
977694dca64SSam Leffler 	} else
978694dca64SSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
9791a1e1d21SSam Leffler 	return ni;
9801a1e1d21SSam Leffler }
9811a1e1d21SSam Leffler 
982750d6d0cSSam Leffler static struct ieee80211_node *
9838a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9848a1b9b6aSSam Leffler _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
9858a1b9b6aSSam Leffler 	const u_int8_t *macaddr, const char *func, int line)
9868a1b9b6aSSam Leffler #else
9878a1b9b6aSSam Leffler _ieee80211_find_node(struct ieee80211_node_table *nt,
9888a1b9b6aSSam Leffler 	const u_int8_t *macaddr)
9898a1b9b6aSSam Leffler #endif
9901a1e1d21SSam Leffler {
9911a1e1d21SSam Leffler 	struct ieee80211_node *ni;
9921a1e1d21SSam Leffler 	int hash;
9931a1e1d21SSam Leffler 
9948a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
995750d6d0cSSam Leffler 
9961a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
9978a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
9981a1e1d21SSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
9998a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
10008a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10018a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
100249a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
100349a15236SSam Leffler 			    func, line,
100449a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
10058a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
10068a1b9b6aSSam Leffler #endif
1007750d6d0cSSam Leffler 			return ni;
10081a1e1d21SSam Leffler 		}
10091a1e1d21SSam Leffler 	}
1010750d6d0cSSam Leffler 	return NULL;
1011750d6d0cSSam Leffler }
10128a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10138a1b9b6aSSam Leffler #define	_ieee80211_find_node(nt, mac) \
10148a1b9b6aSSam Leffler 	_ieee80211_find_node_debug(nt, mac, func, line)
10158a1b9b6aSSam Leffler #endif
1016750d6d0cSSam Leffler 
1017750d6d0cSSam Leffler struct ieee80211_node *
10188a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10198a1b9b6aSSam Leffler ieee80211_find_node_debug(struct ieee80211_node_table *nt,
10208a1b9b6aSSam Leffler 	const u_int8_t *macaddr, const char *func, int line)
10218a1b9b6aSSam Leffler #else
10228a1b9b6aSSam Leffler ieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
10238a1b9b6aSSam Leffler #endif
1024750d6d0cSSam Leffler {
1025750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1026750d6d0cSSam Leffler 
10278a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
10288a1b9b6aSSam Leffler 	ni = _ieee80211_find_node(nt, macaddr);
10298a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
10301a1e1d21SSam Leffler 	return ni;
10311a1e1d21SSam Leffler }
10321a1e1d21SSam Leffler 
10331a1e1d21SSam Leffler /*
10348a1b9b6aSSam Leffler  * Fake up a node; this handles node discovery in adhoc mode.
10358a1b9b6aSSam Leffler  * Note that for the driver's benefit we we treat this like
10368a1b9b6aSSam Leffler  * an association so the driver has an opportunity to setup
10378a1b9b6aSSam Leffler  * it's private state.
10388a1b9b6aSSam Leffler  */
10398a1b9b6aSSam Leffler struct ieee80211_node *
10408a1b9b6aSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
10418a1b9b6aSSam Leffler 	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
10428a1b9b6aSSam Leffler {
10438a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
10448a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10458a1b9b6aSSam Leffler 
10468a1b9b6aSSam Leffler 	ni = ieee80211_dup_bss(nt, macaddr);
10478a1b9b6aSSam Leffler 	if (ni != NULL) {
10488a1b9b6aSSam Leffler 		/* XXX no rate negotiation; just dup */
10498a1b9b6aSSam Leffler 		ni->ni_rates = ic->ic_bss->ni_rates;
1050736b3dc3SSam Leffler 		if (ic->ic_newassoc != NULL)
1051e9962332SSam Leffler 			ic->ic_newassoc(ni, 1);
10528a1b9b6aSSam Leffler 		/* XXX not right for 802.1x/WPA */
1053e4918ecdSSam Leffler 		ieee80211_node_authorize(ni);
10548a1b9b6aSSam Leffler 	}
10558a1b9b6aSSam Leffler 	return ni;
10568a1b9b6aSSam Leffler }
10578a1b9b6aSSam Leffler 
10588a1b9b6aSSam Leffler /*
10598a1b9b6aSSam Leffler  * Locate the node for sender, track state, and then pass the
10608a1b9b6aSSam Leffler  * (referenced) node up to the 802.11 layer for its use.  We
10618a1b9b6aSSam Leffler  * are required to pass some node so we fall back to ic_bss
10628a1b9b6aSSam Leffler  * when this frame is from an unknown sender.  The 802.11 layer
10638a1b9b6aSSam Leffler  * knows this means the sender wasn't in the node table and
10648a1b9b6aSSam Leffler  * acts accordingly.
10658a1b9b6aSSam Leffler  */
10668a1b9b6aSSam Leffler struct ieee80211_node *
10678a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10688a1b9b6aSSam Leffler ieee80211_find_rxnode_debug(struct ieee80211com *ic,
10698a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh, const char *func, int line)
10708a1b9b6aSSam Leffler #else
10718a1b9b6aSSam Leffler ieee80211_find_rxnode(struct ieee80211com *ic,
10728a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh)
10738a1b9b6aSSam Leffler #endif
10748a1b9b6aSSam Leffler {
10758a1b9b6aSSam Leffler #define	IS_CTL(wh) \
10768a1b9b6aSSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
10778a1b9b6aSSam Leffler #define	IS_PSPOLL(wh) \
10788a1b9b6aSSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
10798a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
10808a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10818a1b9b6aSSam Leffler 
10828a1b9b6aSSam Leffler 	/* XXX may want scanned nodes in the neighbor table for adhoc */
10838a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
10848a1b9b6aSSam Leffler 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
10858a1b9b6aSSam Leffler 	    (ic->ic_flags & IEEE80211_F_SCAN))
10868a1b9b6aSSam Leffler 		nt = &ic->ic_scan;
10878a1b9b6aSSam Leffler 	else
1088acc4f7f5SSam Leffler 		nt = &ic->ic_sta;
10898a1b9b6aSSam Leffler 	/* XXX check ic_bss first in station mode */
10908a1b9b6aSSam Leffler 	/* XXX 4-address frames? */
10918a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
10928a1b9b6aSSam Leffler 	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
10938a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr1);
10948a1b9b6aSSam Leffler 	else
10958a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr2);
10968a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
10978a1b9b6aSSam Leffler 
10988a1b9b6aSSam Leffler 	return (ni != NULL ? ni : ieee80211_ref_node(ic->ic_bss));
10998a1b9b6aSSam Leffler #undef IS_PSPOLL
11008a1b9b6aSSam Leffler #undef IS_CTL
11018a1b9b6aSSam Leffler }
11028a1b9b6aSSam Leffler 
11038a1b9b6aSSam Leffler /*
1104750d6d0cSSam Leffler  * Return a reference to the appropriate node for sending
1105750d6d0cSSam Leffler  * a data frame.  This handles node discovery in adhoc networks.
1106750d6d0cSSam Leffler  */
1107750d6d0cSSam Leffler struct ieee80211_node *
11088a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
11098a1b9b6aSSam Leffler ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
11108a1b9b6aSSam Leffler 	const char *func, int line)
11118a1b9b6aSSam Leffler #else
11128a1b9b6aSSam Leffler ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
11138a1b9b6aSSam Leffler #endif
1114750d6d0cSSam Leffler {
1115acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1116750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1117750d6d0cSSam Leffler 
1118750d6d0cSSam Leffler 	/*
1119750d6d0cSSam Leffler 	 * The destination address should be in the node table
1120750d6d0cSSam Leffler 	 * unless we are operating in station mode or this is a
1121750d6d0cSSam Leffler 	 * multicast/broadcast frame.
1122750d6d0cSSam Leffler 	 */
1123acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
11248a1b9b6aSSam Leffler 		return ieee80211_ref_node(ic->ic_bss);
1125750d6d0cSSam Leffler 
1126750d6d0cSSam Leffler 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
11278a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
11288a1b9b6aSSam Leffler 	ni = _ieee80211_find_node(nt, macaddr);
11298a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
11308a1b9b6aSSam Leffler 
11318a1b9b6aSSam Leffler 	if (ni == NULL) {
11328a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
113390d0d036SSam Leffler 		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
113490d0d036SSam Leffler 			/*
113590d0d036SSam Leffler 			 * In adhoc mode cons up a node for the destination.
113690d0d036SSam Leffler 			 * Note that we need an additional reference for the
113790d0d036SSam Leffler 			 * caller to be consistent with _ieee80211_find_node.
113890d0d036SSam Leffler 			 */
11398a1b9b6aSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
114090d0d036SSam Leffler 			if (ni != NULL)
114190d0d036SSam Leffler 				(void) ieee80211_ref_node(ni);
114290d0d036SSam Leffler 		} else {
11438a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
11448a1b9b6aSSam Leffler 				"[%s] no node, discard frame (%s)\n",
11458a1b9b6aSSam Leffler 				ether_sprintf(macaddr), __func__);
11468a1b9b6aSSam Leffler 			ic->ic_stats.is_tx_nonode++;
1147750d6d0cSSam Leffler 		}
1148750d6d0cSSam Leffler 	}
1149750d6d0cSSam Leffler 	return ni;
1150750d6d0cSSam Leffler }
1151750d6d0cSSam Leffler 
1152750d6d0cSSam Leffler /*
11531a1e1d21SSam Leffler  * Like find but search based on the channel too.
11541a1e1d21SSam Leffler  */
11551a1e1d21SSam Leffler struct ieee80211_node *
11568a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
11578a1b9b6aSSam Leffler ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
11588a1b9b6aSSam Leffler 	const u_int8_t *macaddr, struct ieee80211_channel *chan,
11598a1b9b6aSSam Leffler 	const char *func, int line)
11608a1b9b6aSSam Leffler #else
11618a1b9b6aSSam Leffler ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
11628a1b9b6aSSam Leffler 	const u_int8_t *macaddr, struct ieee80211_channel *chan)
11638a1b9b6aSSam Leffler #endif
11641a1e1d21SSam Leffler {
11651a1e1d21SSam Leffler 	struct ieee80211_node *ni;
11661a1e1d21SSam Leffler 	int hash;
11671a1e1d21SSam Leffler 
11681a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
11698a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
11708a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1171750d6d0cSSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1172750d6d0cSSam Leffler 		    ni->ni_chan == chan) {
11738a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);		/* mark referenced */
11748a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
11758a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
117649a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
117749a15236SSam Leffler 			    func, line,
11788a1b9b6aSSam Leffler #else
117949a15236SSam Leffler 			    "%s %p<%s> refcnt %d\n", __func__,
11808a1b9b6aSSam Leffler #endif
118149a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
11828a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
11831a1e1d21SSam Leffler 			break;
11841a1e1d21SSam Leffler 		}
11851a1e1d21SSam Leffler 	}
11868a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
11871a1e1d21SSam Leffler 	return ni;
11881a1e1d21SSam Leffler }
11891a1e1d21SSam Leffler 
11908a1b9b6aSSam Leffler /*
11918a1b9b6aSSam Leffler  * Like find but search based on the ssid too.
11928a1b9b6aSSam Leffler  */
11938a1b9b6aSSam Leffler struct ieee80211_node *
11948a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
11958a1b9b6aSSam Leffler ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
11968a1b9b6aSSam Leffler 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
11978a1b9b6aSSam Leffler 	const char *func, int line)
11988a1b9b6aSSam Leffler #else
11998a1b9b6aSSam Leffler ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
12008a1b9b6aSSam Leffler 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
12018a1b9b6aSSam Leffler #endif
12021a1e1d21SSam Leffler {
1203f02a0bd2SSam Leffler #define	MATCH_SSID(ni, ssid, ssidlen) \
1204f02a0bd2SSam Leffler 	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1205f02a0bd2SSam Leffler 	static const u_int8_t zeromac[IEEE80211_ADDR_LEN];
12068a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
12071a1e1d21SSam Leffler 	struct ieee80211_node *ni;
12088a1b9b6aSSam Leffler 	int hash;
12091a1e1d21SSam Leffler 
12108a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1211f02a0bd2SSam Leffler 	/*
1212f02a0bd2SSam Leffler 	 * A mac address that is all zero means match only the ssid;
1213f02a0bd2SSam Leffler 	 * otherwise we must match both.
1214f02a0bd2SSam Leffler 	 */
1215f02a0bd2SSam Leffler 	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1216f02a0bd2SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1217f02a0bd2SSam Leffler 			if (MATCH_SSID(ni, ssid, ssidlen))
1218f02a0bd2SSam Leffler 				break;
1219f02a0bd2SSam Leffler 		}
1220f02a0bd2SSam Leffler 	} else {
1221f02a0bd2SSam Leffler 		hash = IEEE80211_NODE_HASH(macaddr);
12228a1b9b6aSSam Leffler 		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
12238a1b9b6aSSam Leffler 			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1224f02a0bd2SSam Leffler 			    MATCH_SSID(ni, ssid, ssidlen))
1225f02a0bd2SSam Leffler 				break;
1226f02a0bd2SSam Leffler 		}
1227f02a0bd2SSam Leffler 	}
1228f02a0bd2SSam Leffler 	if (ni != NULL) {
12298a1b9b6aSSam Leffler 		ieee80211_ref_node(ni);	/* mark referenced */
12308a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
12318a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
123249a15236SSam Leffler 		    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
123349a15236SSam Leffler 		    func, line,
12348a1b9b6aSSam Leffler #else
123549a15236SSam Leffler 		    "%s %p<%s> refcnt %d\n", __func__,
12368a1b9b6aSSam Leffler #endif
123749a15236SSam Leffler 		     ni, ether_sprintf(ni->ni_macaddr),
12388a1b9b6aSSam Leffler 		     ieee80211_node_refcnt(ni));
12398a1b9b6aSSam Leffler 	}
12408a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
12418a1b9b6aSSam Leffler 	return ni;
1242f02a0bd2SSam Leffler #undef MATCH_SSID
12438a1b9b6aSSam Leffler }
12448a1b9b6aSSam Leffler 
12458a1b9b6aSSam Leffler static void
12468a1b9b6aSSam Leffler _ieee80211_free_node(struct ieee80211_node *ni)
12478a1b9b6aSSam Leffler {
12488a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
12498a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
12508a1b9b6aSSam Leffler 
12518a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
125249a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
125349a15236SSam Leffler 		ether_sprintf(ni->ni_macaddr),
12548a1b9b6aSSam Leffler 		nt != NULL ? nt->nt_name : "<gone>");
12558a1b9b6aSSam Leffler 
12568a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
12578a1b9b6aSSam Leffler 	if (nt != NULL) {
12588a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
12598a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
12608a1b9b6aSSam Leffler 	}
12618a1b9b6aSSam Leffler 	ic->ic_node_free(ni);
12628a1b9b6aSSam Leffler }
12638a1b9b6aSSam Leffler 
12648a1b9b6aSSam Leffler void
12658a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12668a1b9b6aSSam Leffler ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
12678a1b9b6aSSam Leffler #else
12688a1b9b6aSSam Leffler ieee80211_free_node(struct ieee80211_node *ni)
12698a1b9b6aSSam Leffler #endif
12708a1b9b6aSSam Leffler {
12718a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
12728a1b9b6aSSam Leffler 
12738a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
127429d368a7SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
127549a15236SSam Leffler 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
12768a1b9b6aSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
12778a1b9b6aSSam Leffler #endif
12788a1b9b6aSSam Leffler 	if (ieee80211_node_dectestref(ni)) {
12798a1b9b6aSSam Leffler 		/*
12808a1b9b6aSSam Leffler 		 * Beware; if the node is marked gone then it's already
12818a1b9b6aSSam Leffler 		 * been removed from the table and we cannot assume the
12828a1b9b6aSSam Leffler 		 * table still exists.  Regardless, there's no need to lock
12838a1b9b6aSSam Leffler 		 * the table.
12848a1b9b6aSSam Leffler 		 */
12858a1b9b6aSSam Leffler 		if (ni->ni_table != NULL) {
12868a1b9b6aSSam Leffler 			IEEE80211_NODE_LOCK(nt);
12878a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
12888a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
12898a1b9b6aSSam Leffler 		} else
12908a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
12918a1b9b6aSSam Leffler 	}
12921a1e1d21SSam Leffler }
12931a1e1d21SSam Leffler 
1294303ebc3cSSam Leffler /*
12958a1b9b6aSSam Leffler  * Reclaim a node.  If this is the last reference count then
12968a1b9b6aSSam Leffler  * do the normal free work.  Otherwise remove it from the node
12978a1b9b6aSSam Leffler  * table and mark it gone by clearing the back-reference.
12988a1b9b6aSSam Leffler  */
12998a1b9b6aSSam Leffler static void
13008a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
13018a1b9b6aSSam Leffler {
13028a1b9b6aSSam Leffler 
130349a15236SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
130449a15236SSam Leffler 		"%s: remove %p<%s> from %s table, refcnt %d\n",
130549a15236SSam Leffler 		__func__, ni, ether_sprintf(ni->ni_macaddr),
130649a15236SSam Leffler 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
13078a1b9b6aSSam Leffler 	if (!ieee80211_node_dectestref(ni)) {
13088a1b9b6aSSam Leffler 		/*
13098a1b9b6aSSam Leffler 		 * Other references are present, just remove the
13108a1b9b6aSSam Leffler 		 * node from the table so it cannot be found.  When
13118a1b9b6aSSam Leffler 		 * the references are dropped storage will be
1312acc4f7f5SSam Leffler 		 * reclaimed.
13138a1b9b6aSSam Leffler 		 */
13148a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
13158a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
13168a1b9b6aSSam Leffler 		ni->ni_table = NULL;		/* clear reference */
13178a1b9b6aSSam Leffler 	} else
13188a1b9b6aSSam Leffler 		_ieee80211_free_node(ni);
13198a1b9b6aSSam Leffler }
13208a1b9b6aSSam Leffler 
13218a1b9b6aSSam Leffler static void
13228a1b9b6aSSam Leffler ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
13238a1b9b6aSSam Leffler {
13248a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
13258a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
13268a1b9b6aSSam Leffler 
13278a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
13288a1b9b6aSSam Leffler 		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
13298a1b9b6aSSam Leffler 
13308a1b9b6aSSam Leffler 	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
13318a1b9b6aSSam Leffler 		if (ni->ni_associd != 0) {
13328a1b9b6aSSam Leffler 			if (ic->ic_auth->ia_node_leave != NULL)
13338a1b9b6aSSam Leffler 				ic->ic_auth->ia_node_leave(ic, ni);
13348a1b9b6aSSam Leffler 			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
13358a1b9b6aSSam Leffler 		}
13368a1b9b6aSSam Leffler 		node_reclaim(nt, ni);
13378a1b9b6aSSam Leffler 	}
13388a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
13398a1b9b6aSSam Leffler }
13408a1b9b6aSSam Leffler 
13418a1b9b6aSSam Leffler static void
13428a1b9b6aSSam Leffler ieee80211_free_allnodes(struct ieee80211_node_table *nt)
13438a1b9b6aSSam Leffler {
13448a1b9b6aSSam Leffler 
13458a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
13468a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
13478a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
13488a1b9b6aSSam Leffler }
13498a1b9b6aSSam Leffler 
13508a1b9b6aSSam Leffler /*
13518a1b9b6aSSam Leffler  * Timeout entries in the scan cache.
13528a1b9b6aSSam Leffler  */
13538a1b9b6aSSam Leffler static void
13548a1b9b6aSSam Leffler ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
13558a1b9b6aSSam Leffler {
13568a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
13578a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *tni;
13588a1b9b6aSSam Leffler 
13598a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
13608a1b9b6aSSam Leffler 	ni = ic->ic_bss;
13618a1b9b6aSSam Leffler 	/* XXX belongs elsewhere */
13628a1b9b6aSSam Leffler 	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
13638a1b9b6aSSam Leffler 		m_freem(ni->ni_rxfrag[0]);
13648a1b9b6aSSam Leffler 		ni->ni_rxfrag[0] = NULL;
13658a1b9b6aSSam Leffler 	}
13668a1b9b6aSSam Leffler 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
13678a1b9b6aSSam Leffler 		if (ni->ni_inact && --ni->ni_inact == 0) {
13688a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
13698a1b9b6aSSam Leffler 			    "[%s] scan candidate purged from cache "
13708a1b9b6aSSam Leffler 			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
137149a15236SSam Leffler 			    ieee80211_node_refcnt(ni));
13728a1b9b6aSSam Leffler 			node_reclaim(nt, ni);
13738a1b9b6aSSam Leffler 		}
13748a1b9b6aSSam Leffler 	}
13758a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
13768a1b9b6aSSam Leffler 
13778a1b9b6aSSam Leffler 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
13788a1b9b6aSSam Leffler }
13798a1b9b6aSSam Leffler 
13808a1b9b6aSSam Leffler /*
13818a1b9b6aSSam Leffler  * Timeout inactive stations and do related housekeeping.
13828a1b9b6aSSam Leffler  * Note that we cannot hold the node lock while sending a
13838a1b9b6aSSam Leffler  * frame as this would lead to a LOR.  Instead we use a
13848a1b9b6aSSam Leffler  * generation number to mark nodes that we've scanned and
13858a1b9b6aSSam Leffler  * drop the lock and restart a scan if we have to time out
13868a1b9b6aSSam Leffler  * a node.  Since we are single-threaded by virtue of
1387303ebc3cSSam Leffler  * controlling the inactivity timer we can be sure this will
1388303ebc3cSSam Leffler  * process each node only once.
1389303ebc3cSSam Leffler  */
13908a1b9b6aSSam Leffler static void
13918a1b9b6aSSam Leffler ieee80211_timeout_stations(struct ieee80211_node_table *nt)
13921a1e1d21SSam Leffler {
13938a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
1394303ebc3cSSam Leffler 	struct ieee80211_node *ni;
13958a1b9b6aSSam Leffler 	u_int gen;
13961a1e1d21SSam Leffler 
13978a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
13988a1b9b6aSSam Leffler 	gen = nt->nt_scangen++;
13998a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
140049a15236SSam Leffler 		"%s: %s scangen %u\n", __func__, nt->nt_name, gen);
1401303ebc3cSSam Leffler restart:
14028a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
14038a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1404303ebc3cSSam Leffler 		if (ni->ni_scangen == gen)	/* previously handled */
1405303ebc3cSSam Leffler 			continue;
1406303ebc3cSSam Leffler 		ni->ni_scangen = gen;
14070a915fadSSam Leffler 		/*
1408ebdda46cSSam Leffler 		 * Ignore entries for which have yet to receive an
1409ebdda46cSSam Leffler 		 * authentication frame.  These are transient and
1410ebdda46cSSam Leffler 		 * will be reclaimed when the last reference to them
1411ebdda46cSSam Leffler 		 * goes away (when frame xmits complete).
1412ebdda46cSSam Leffler 		 */
1413ebdda46cSSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1414ebdda46cSSam Leffler 			continue;
1415ebdda46cSSam Leffler 		/*
14168a1b9b6aSSam Leffler 		 * Free fragment if not needed anymore
14178a1b9b6aSSam Leffler 		 * (last fragment older than 1s).
14188a1b9b6aSSam Leffler 		 * XXX doesn't belong here
14190a915fadSSam Leffler 		 */
14208a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[0] != NULL &&
14218a1b9b6aSSam Leffler 		    ticks > ni->ni_rxfragstamp + hz) {
14228a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
14238a1b9b6aSSam Leffler 			ni->ni_rxfrag[0] = NULL;
14248a1b9b6aSSam Leffler 		}
1425ce647032SSam Leffler 		/*
1426ce647032SSam Leffler 		 * Special case ourself; we may be idle for extended periods
1427ce647032SSam Leffler 		 * of time and regardless reclaiming our state is wrong.
1428ce647032SSam Leffler 		 */
1429ce647032SSam Leffler 		if (ni == ic->ic_bss)
1430ce647032SSam Leffler 			continue;
14318a1b9b6aSSam Leffler 		ni->ni_inact--;
14328a1b9b6aSSam Leffler 		if (ni->ni_associd != 0) {
14338a1b9b6aSSam Leffler 			/*
14348a1b9b6aSSam Leffler 			 * Age frames on the power save queue. The
14358a1b9b6aSSam Leffler 			 * aging interval is 4 times the listen
14368a1b9b6aSSam Leffler 			 * interval specified by the station.  This
14378a1b9b6aSSam Leffler 			 * number is factored into the age calculations
14388a1b9b6aSSam Leffler 			 * when the frame is placed on the queue.  We
14398a1b9b6aSSam Leffler 			 * store ages as time differences we can check
14408a1b9b6aSSam Leffler 			 * and/or adjust only the head of the list.
14418a1b9b6aSSam Leffler 			 */
14428a1b9b6aSSam Leffler 			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
14438a1b9b6aSSam Leffler 				struct mbuf *m;
14448a1b9b6aSSam Leffler 				int discard = 0;
14458a1b9b6aSSam Leffler 
14468a1b9b6aSSam Leffler 				IEEE80211_NODE_SAVEQ_LOCK(ni);
14478a1b9b6aSSam Leffler 				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
14488a1b9b6aSSam Leffler 				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
14498a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
14508a1b9b6aSSam Leffler 					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
14518a1b9b6aSSam Leffler 					m_freem(m);
14528a1b9b6aSSam Leffler 					discard++;
14538a1b9b6aSSam Leffler 				}
14548a1b9b6aSSam Leffler 				if (m != NULL)
14558a1b9b6aSSam Leffler 					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
14568a1b9b6aSSam Leffler 				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
14578a1b9b6aSSam Leffler 
14588a1b9b6aSSam Leffler 				if (discard != 0) {
14598a1b9b6aSSam Leffler 					IEEE80211_DPRINTF(ic,
14608a1b9b6aSSam Leffler 					    IEEE80211_MSG_POWER,
14618a1b9b6aSSam Leffler 					    "[%s] discard %u frames for age\n",
14628a1b9b6aSSam Leffler 					    ether_sprintf(ni->ni_macaddr),
14638a1b9b6aSSam Leffler 					    discard);
14648a1b9b6aSSam Leffler 					IEEE80211_NODE_STAT_ADD(ni,
14658a1b9b6aSSam Leffler 						ps_discard, discard);
14668a1b9b6aSSam Leffler 					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1467edfa57d0SSam Leffler 						ic->ic_set_tim(ni, 0);
14688a1b9b6aSSam Leffler 				}
14698a1b9b6aSSam Leffler 			}
14708a1b9b6aSSam Leffler 			/*
14718a1b9b6aSSam Leffler 			 * Probe the station before time it out.  We
14728a1b9b6aSSam Leffler 			 * send a null data frame which may not be
14738a1b9b6aSSam Leffler 			 * universally supported by drivers (need it
14748a1b9b6aSSam Leffler 			 * for ps-poll support so it should be...).
14758a1b9b6aSSam Leffler 			 */
14762045f699SSam Leffler 			if (0 < ni->ni_inact &&
14772045f699SSam Leffler 			    ni->ni_inact <= ic->ic_inact_probe) {
14788a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
14798a1b9b6aSSam Leffler 				    "[%s] probe station due to inactivity\n",
14808a1b9b6aSSam Leffler 				    ether_sprintf(ni->ni_macaddr));
14818a1b9b6aSSam Leffler 				IEEE80211_NODE_UNLOCK(nt);
1482f62121ceSSam Leffler 				ieee80211_send_nulldata(ni);
14838a1b9b6aSSam Leffler 				/* XXX stat? */
14848a1b9b6aSSam Leffler 				goto restart;
14858a1b9b6aSSam Leffler 			}
14868a1b9b6aSSam Leffler 		}
14878a1b9b6aSSam Leffler 		if (ni->ni_inact <= 0) {
14888a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
14898a1b9b6aSSam Leffler 			    "[%s] station timed out due to inactivity "
14908a1b9b6aSSam Leffler 			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
14918a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
14928a1b9b6aSSam Leffler 			/*
14938a1b9b6aSSam Leffler 			 * Send a deauthenticate frame and drop the station.
14948a1b9b6aSSam Leffler 			 * This is somewhat complicated due to reference counts
14958a1b9b6aSSam Leffler 			 * and locking.  At this point a station will typically
14968a1b9b6aSSam Leffler 			 * have a reference count of 1.  ieee80211_node_leave
14978a1b9b6aSSam Leffler 			 * will do a "free" of the node which will drop the
14988a1b9b6aSSam Leffler 			 * reference count.  But in the meantime a reference
14998a1b9b6aSSam Leffler 			 * wil be held by the deauth frame.  The actual reclaim
15008a1b9b6aSSam Leffler 			 * of the node will happen either after the tx is
15018a1b9b6aSSam Leffler 			 * completed or by ieee80211_node_leave.
15028a1b9b6aSSam Leffler 			 *
15038a1b9b6aSSam Leffler 			 * Separately we must drop the node lock before sending
15048a1b9b6aSSam Leffler 			 * in case the driver takes a lock, as this will result
15058a1b9b6aSSam Leffler 			 * in  LOR between the node lock and the driver lock.
15068a1b9b6aSSam Leffler 			 */
15078a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
15088a1b9b6aSSam Leffler 			if (ni->ni_associd != 0) {
15091a1e1d21SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
15101a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
15111a1e1d21SSam Leffler 				    IEEE80211_REASON_AUTH_EXPIRE);
15128a1b9b6aSSam Leffler 			}
15138a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
15141be50176SSam Leffler 			ic->ic_stats.is_node_timeout++;
1515303ebc3cSSam Leffler 			goto restart;
1516303ebc3cSSam Leffler 		}
15171a1e1d21SSam Leffler 	}
15188a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15198a1b9b6aSSam Leffler 
15208a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
15218a1b9b6aSSam Leffler 
15228a1b9b6aSSam Leffler 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
15231a1e1d21SSam Leffler }
15241a1e1d21SSam Leffler 
15251a1e1d21SSam Leffler void
15268a1b9b6aSSam Leffler ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
15271a1e1d21SSam Leffler {
15281a1e1d21SSam Leffler 	struct ieee80211_node *ni;
15298a1b9b6aSSam Leffler 	u_int gen;
15301a1e1d21SSam Leffler 
15318a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
15328a1b9b6aSSam Leffler 	gen = nt->nt_scangen++;
15338a1b9b6aSSam Leffler restart:
15348a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
15358a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
15368a1b9b6aSSam Leffler 		if (ni->ni_scangen != gen) {
15378a1b9b6aSSam Leffler 			ni->ni_scangen = gen;
15388a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
15398a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
15401a1e1d21SSam Leffler 			(*f)(arg, ni);
15418a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
15428a1b9b6aSSam Leffler 			goto restart;
15438a1b9b6aSSam Leffler 		}
15448a1b9b6aSSam Leffler 	}
15458a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15468a1b9b6aSSam Leffler 
15478a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
15488a1b9b6aSSam Leffler }
15498a1b9b6aSSam Leffler 
15508a1b9b6aSSam Leffler void
15518a1b9b6aSSam Leffler ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
15528a1b9b6aSSam Leffler {
15538a1b9b6aSSam Leffler 	printf("0x%p: mac %s refcnt %d\n", ni,
15548a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
15558a1b9b6aSSam Leffler 	printf("\tscangen %u authmode %u flags 0x%x\n",
15568a1b9b6aSSam Leffler 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
15578a1b9b6aSSam Leffler 	printf("\tassocid 0x%x txpower %u vlan %u\n",
15588a1b9b6aSSam Leffler 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
15598a1b9b6aSSam Leffler 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
15608a1b9b6aSSam Leffler 		ni->ni_txseqs[0],
15618a1b9b6aSSam Leffler 		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
15628a1b9b6aSSam Leffler 		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
15638a1b9b6aSSam Leffler 		ni->ni_rxfragstamp);
15648a1b9b6aSSam Leffler 	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
15658a1b9b6aSSam Leffler 		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
15668a1b9b6aSSam Leffler 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
15678a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
15688a1b9b6aSSam Leffler 		ni->ni_esslen, ni->ni_essid,
15698a1b9b6aSSam Leffler 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
15708a1b9b6aSSam Leffler 	printf("\tfails %u inact %u txrate %u\n",
15718a1b9b6aSSam Leffler 		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
15728a1b9b6aSSam Leffler }
15738a1b9b6aSSam Leffler 
15748a1b9b6aSSam Leffler void
15758a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt)
15768a1b9b6aSSam Leffler {
15778a1b9b6aSSam Leffler 	ieee80211_iterate_nodes(nt,
15788a1b9b6aSSam Leffler 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
15798a1b9b6aSSam Leffler }
15808a1b9b6aSSam Leffler 
15818a1b9b6aSSam Leffler /*
15828a1b9b6aSSam Leffler  * Handle a station joining an 11g network.
15838a1b9b6aSSam Leffler  */
15848a1b9b6aSSam Leffler static void
15858a1b9b6aSSam Leffler ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
15868a1b9b6aSSam Leffler {
15878a1b9b6aSSam Leffler 
15888a1b9b6aSSam Leffler 	/*
15898a1b9b6aSSam Leffler 	 * Station isn't capable of short slot time.  Bump
15908a1b9b6aSSam Leffler 	 * the count of long slot time stations and disable
15918a1b9b6aSSam Leffler 	 * use of short slot time.  Note that the actual switch
15928a1b9b6aSSam Leffler 	 * over to long slot time use may not occur until the
15938a1b9b6aSSam Leffler 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
15948a1b9b6aSSam Leffler 	 */
15958a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
15968a1b9b6aSSam Leffler 		ic->ic_longslotsta++;
15978a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
15988a1b9b6aSSam Leffler 		    "[%s] station needs long slot time, count %d\n",
15998a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
16008a1b9b6aSSam Leffler 		/* XXX vap's w/ conflicting needs won't work */
16018a1b9b6aSSam Leffler 		ieee80211_set_shortslottime(ic, 0);
16028a1b9b6aSSam Leffler 	}
16038a1b9b6aSSam Leffler 	/*
16048a1b9b6aSSam Leffler 	 * If the new station is not an ERP station
16058a1b9b6aSSam Leffler 	 * then bump the counter and enable protection
16068a1b9b6aSSam Leffler 	 * if configured.
16078a1b9b6aSSam Leffler 	 */
16088a1b9b6aSSam Leffler 	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
16098a1b9b6aSSam Leffler 		ic->ic_nonerpsta++;
16108a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
16118a1b9b6aSSam Leffler 		    "[%s] station is !ERP, %d non-ERP stations associated\n",
16128a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
16138a1b9b6aSSam Leffler 		/*
16148a1b9b6aSSam Leffler 		 * If protection is configured, enable it.
16158a1b9b6aSSam Leffler 		 */
16168a1b9b6aSSam Leffler 		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
16178a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
16188a1b9b6aSSam Leffler 			    "%s: enable use of protection\n", __func__);
16198a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
16208a1b9b6aSSam Leffler 		}
16218a1b9b6aSSam Leffler 		/*
16228a1b9b6aSSam Leffler 		 * If station does not support short preamble
16238a1b9b6aSSam Leffler 		 * then we must enable use of Barker preamble.
16248a1b9b6aSSam Leffler 		 */
16258a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
16268a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
16278a1b9b6aSSam Leffler 			    "[%s] station needs long preamble\n",
16288a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr));
16298a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
16308a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
16318a1b9b6aSSam Leffler 		}
16328a1b9b6aSSam Leffler 	} else
16338a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
16348a1b9b6aSSam Leffler }
16358a1b9b6aSSam Leffler 
16368a1b9b6aSSam Leffler void
16378a1b9b6aSSam Leffler ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
16388a1b9b6aSSam Leffler {
16398a1b9b6aSSam Leffler 	int newassoc;
16408a1b9b6aSSam Leffler 
16418a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
16428a1b9b6aSSam Leffler 		u_int16_t aid;
16438a1b9b6aSSam Leffler 
16448a1b9b6aSSam Leffler 		/*
16458a1b9b6aSSam Leffler 		 * It would be good to search the bitmap
16468a1b9b6aSSam Leffler 		 * more efficiently, but this will do for now.
16478a1b9b6aSSam Leffler 		 */
16488a1b9b6aSSam Leffler 		for (aid = 1; aid < ic->ic_max_aid; aid++) {
16498a1b9b6aSSam Leffler 			if (!IEEE80211_AID_ISSET(aid,
16508a1b9b6aSSam Leffler 			    ic->ic_aid_bitmap))
16518a1b9b6aSSam Leffler 				break;
16528a1b9b6aSSam Leffler 		}
16538a1b9b6aSSam Leffler 		if (aid >= ic->ic_max_aid) {
16548a1b9b6aSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
16558a1b9b6aSSam Leffler 			    IEEE80211_REASON_ASSOC_TOOMANY);
16568a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
16578a1b9b6aSSam Leffler 			return;
16588a1b9b6aSSam Leffler 		}
16598a1b9b6aSSam Leffler 		ni->ni_associd = aid | 0xc000;
16608a1b9b6aSSam Leffler 		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
16618a1b9b6aSSam Leffler 		ic->ic_sta_assoc++;
16628a1b9b6aSSam Leffler 		newassoc = 1;
1663624a1bdbSSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G ||
1664624a1bdbSSam Leffler 		    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
16658a1b9b6aSSam Leffler 			ieee80211_node_join_11g(ic, ni);
16668a1b9b6aSSam Leffler 	} else
16678a1b9b6aSSam Leffler 		newassoc = 0;
16688a1b9b6aSSam Leffler 
16698a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1670b8fcf546SSam Leffler 	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
1671b8fcf546SSam Leffler 	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
1672b8fcf546SSam Leffler 	    IEEE80211_NODE_AID(ni),
1673b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
1674b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
1675b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
1676b8fcf546SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
1677b8fcf546SSam Leffler 	);
16788a1b9b6aSSam Leffler 
16798a1b9b6aSSam Leffler 	/* give driver a chance to setup state like ni_txrate */
1680736b3dc3SSam Leffler 	if (ic->ic_newassoc != NULL)
1681e9962332SSam Leffler 		ic->ic_newassoc(ni, newassoc);
16822045f699SSam Leffler 	ni->ni_inact_reload = ic->ic_inact_auth;
16832045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
16848a1b9b6aSSam Leffler 	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
16858a1b9b6aSSam Leffler 	/* tell the authenticator about new station */
16868a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_join != NULL)
16878a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_join(ic, ni);
16888a1b9b6aSSam Leffler 	ieee80211_notify_node_join(ic, ni, newassoc);
16898a1b9b6aSSam Leffler }
16908a1b9b6aSSam Leffler 
16918a1b9b6aSSam Leffler /*
16928a1b9b6aSSam Leffler  * Handle a station leaving an 11g network.
16938a1b9b6aSSam Leffler  */
16948a1b9b6aSSam Leffler static void
16958a1b9b6aSSam Leffler ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
16968a1b9b6aSSam Leffler {
16978a1b9b6aSSam Leffler 
1698624a1bdbSSam Leffler 	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G ||
1699624a1bdbSSam Leffler 		ic->ic_curmode == IEEE80211_MODE_TURBO_G,
17008a1b9b6aSSam Leffler 		("not in 11g, curmode %x", ic->ic_curmode));
17018a1b9b6aSSam Leffler 
17028a1b9b6aSSam Leffler 	/*
17038a1b9b6aSSam Leffler 	 * If a long slot station do the slot time bookkeeping.
17048a1b9b6aSSam Leffler 	 */
17058a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
17068a1b9b6aSSam Leffler 		KASSERT(ic->ic_longslotsta > 0,
17078a1b9b6aSSam Leffler 		    ("bogus long slot station count %d", ic->ic_longslotsta));
17088a1b9b6aSSam Leffler 		ic->ic_longslotsta--;
17098a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
17108a1b9b6aSSam Leffler 		    "[%s] long slot time station leaves, count now %d\n",
17118a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
17128a1b9b6aSSam Leffler 		if (ic->ic_longslotsta == 0) {
17138a1b9b6aSSam Leffler 			/*
17148a1b9b6aSSam Leffler 			 * Re-enable use of short slot time if supported
17158a1b9b6aSSam Leffler 			 * and not operating in IBSS mode (per spec).
17168a1b9b6aSSam Leffler 			 */
17178a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
17188a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_IBSS) {
17198a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
17208a1b9b6aSSam Leffler 				    "%s: re-enable use of short slot time\n",
17218a1b9b6aSSam Leffler 				    __func__);
17228a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic, 1);
17238a1b9b6aSSam Leffler 			}
17248a1b9b6aSSam Leffler 		}
17258a1b9b6aSSam Leffler 	}
17268a1b9b6aSSam Leffler 	/*
17278a1b9b6aSSam Leffler 	 * If a non-ERP station do the protection-related bookkeeping.
17288a1b9b6aSSam Leffler 	 */
17298a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
17308a1b9b6aSSam Leffler 		KASSERT(ic->ic_nonerpsta > 0,
17318a1b9b6aSSam Leffler 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
17328a1b9b6aSSam Leffler 		ic->ic_nonerpsta--;
17338a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
17348a1b9b6aSSam Leffler 		    "[%s] non-ERP station leaves, count now %d\n",
17358a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
17368a1b9b6aSSam Leffler 		if (ic->ic_nonerpsta == 0) {
17378a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
17388a1b9b6aSSam Leffler 				"%s: disable use of protection\n", __func__);
17398a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
17408a1b9b6aSSam Leffler 			/* XXX verify mode? */
17418a1b9b6aSSam Leffler 			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
17428a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
17438a1b9b6aSSam Leffler 				    "%s: re-enable use of short preamble\n",
17448a1b9b6aSSam Leffler 				    __func__);
17458a1b9b6aSSam Leffler 				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
17468a1b9b6aSSam Leffler 				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
17478a1b9b6aSSam Leffler 			}
17488a1b9b6aSSam Leffler 		}
17498a1b9b6aSSam Leffler 	}
17508a1b9b6aSSam Leffler }
17518a1b9b6aSSam Leffler 
17528a1b9b6aSSam Leffler /*
17538a1b9b6aSSam Leffler  * Handle bookkeeping for station deauthentication/disassociation
17548a1b9b6aSSam Leffler  * when operating as an ap.
17558a1b9b6aSSam Leffler  */
17568a1b9b6aSSam Leffler void
17578a1b9b6aSSam Leffler ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
17588a1b9b6aSSam Leffler {
175944acc00dSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
17608a1b9b6aSSam Leffler 
17618a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
17628a1b9b6aSSam Leffler 	    "[%s] station with aid %d leaves\n",
17638a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
17648a1b9b6aSSam Leffler 
17658a1b9b6aSSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
17668a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_IBSS ||
17678a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_AHDEMO,
17688a1b9b6aSSam Leffler 		("unexpected operating mode %u", ic->ic_opmode));
17698a1b9b6aSSam Leffler 	/*
17708a1b9b6aSSam Leffler 	 * If node wasn't previously associated all
17718a1b9b6aSSam Leffler 	 * we need to do is reclaim the reference.
17728a1b9b6aSSam Leffler 	 */
17738a1b9b6aSSam Leffler 	/* XXX ibss mode bypasses 11g and notification */
17748a1b9b6aSSam Leffler 	if (ni->ni_associd == 0)
17758a1b9b6aSSam Leffler 		goto done;
17768a1b9b6aSSam Leffler 	/*
17778a1b9b6aSSam Leffler 	 * Tell the authenticator the station is leaving.
17788a1b9b6aSSam Leffler 	 * Note that we must do this before yanking the
17798a1b9b6aSSam Leffler 	 * association id as the authenticator uses the
17808a1b9b6aSSam Leffler 	 * associd to locate it's state block.
17818a1b9b6aSSam Leffler 	 */
17828a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_leave != NULL)
17838a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_leave(ic, ni);
17848a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
17858a1b9b6aSSam Leffler 	ni->ni_associd = 0;
17868a1b9b6aSSam Leffler 	ic->ic_sta_assoc--;
17878a1b9b6aSSam Leffler 
1788624a1bdbSSam Leffler 	if (ic->ic_curmode == IEEE80211_MODE_11G ||
1789624a1bdbSSam Leffler 	    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
17908a1b9b6aSSam Leffler 		ieee80211_node_leave_11g(ic, ni);
17918a1b9b6aSSam Leffler 	/*
17928a1b9b6aSSam Leffler 	 * Cleanup station state.  In particular clear various
17938a1b9b6aSSam Leffler 	 * state that might otherwise be reused if the node
17948a1b9b6aSSam Leffler 	 * is reused before the reference count goes to zero
17958a1b9b6aSSam Leffler 	 * (and memory is reclaimed).
17968a1b9b6aSSam Leffler 	 */
17978a1b9b6aSSam Leffler 	ieee80211_sta_leave(ic, ni);
17988a1b9b6aSSam Leffler done:
179944acc00dSSam Leffler 	/*
180044acc00dSSam Leffler 	 * Remove the node from any table it's recorded in and
180144acc00dSSam Leffler 	 * drop the caller's reference.  Removal from the table
180244acc00dSSam Leffler 	 * is important to insure the node is not reprocessed
180344acc00dSSam Leffler 	 * for inactivity.
180444acc00dSSam Leffler 	 */
180544acc00dSSam Leffler 	if (nt != NULL) {
180644acc00dSSam Leffler 		IEEE80211_NODE_LOCK(nt);
180744acc00dSSam Leffler 		node_reclaim(nt, ni);
180844acc00dSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
180944acc00dSSam Leffler 	} else
18108a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
18118a1b9b6aSSam Leffler }
18128a1b9b6aSSam Leffler 
18138a1b9b6aSSam Leffler u_int8_t
18148a1b9b6aSSam Leffler ieee80211_getrssi(struct ieee80211com *ic)
18158a1b9b6aSSam Leffler {
18168a1b9b6aSSam Leffler #define	NZ(x)	((x) == 0 ? 1 : (x))
1817acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
18188a1b9b6aSSam Leffler 	u_int32_t rssi_samples, rssi_total;
18198a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
18208a1b9b6aSSam Leffler 
18218a1b9b6aSSam Leffler 	rssi_total = 0;
18228a1b9b6aSSam Leffler 	rssi_samples = 0;
18238a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
18248a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
18258a1b9b6aSSam Leffler 		/* XXX locking */
1826acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
18278a1b9b6aSSam Leffler 			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
18288a1b9b6aSSam Leffler 				rssi_samples++;
18298a1b9b6aSSam Leffler 				rssi_total += ic->ic_node_getrssi(ni);
18308a1b9b6aSSam Leffler 			}
18318a1b9b6aSSam Leffler 		break;
18328a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
18338a1b9b6aSSam Leffler 		/* XXX locking */
1834acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
18358a1b9b6aSSam Leffler 			rssi_samples++;
18368a1b9b6aSSam Leffler 			rssi_total += ic->ic_node_getrssi(ni);
18378a1b9b6aSSam Leffler 		}
18388a1b9b6aSSam Leffler 		break;
18398a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
18408a1b9b6aSSam Leffler 		/* XXX locking */
1841acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
18428a1b9b6aSSam Leffler 			if (IEEE80211_AID(ni->ni_associd) != 0) {
18438a1b9b6aSSam Leffler 				rssi_samples++;
18448a1b9b6aSSam Leffler 				rssi_total += ic->ic_node_getrssi(ni);
18458a1b9b6aSSam Leffler 			}
18468a1b9b6aSSam Leffler 		break;
18478a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:	/* XXX */
18488a1b9b6aSSam Leffler 	case IEEE80211_M_STA:		/* use stats from associated ap */
18498a1b9b6aSSam Leffler 	default:
18508a1b9b6aSSam Leffler 		if (ic->ic_bss != NULL)
18518a1b9b6aSSam Leffler 			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
18528a1b9b6aSSam Leffler 		rssi_samples = 1;
18538a1b9b6aSSam Leffler 		break;
18548a1b9b6aSSam Leffler 	}
18558a1b9b6aSSam Leffler 	return rssi_total / NZ(rssi_samples);
18568a1b9b6aSSam Leffler #undef NZ
18578a1b9b6aSSam Leffler }
18588a1b9b6aSSam Leffler 
18598a1b9b6aSSam Leffler /*
18608a1b9b6aSSam Leffler  * Indicate whether there are frames queued for a station in power-save mode.
18618a1b9b6aSSam Leffler  */
18628a1b9b6aSSam Leffler static void
1863edfa57d0SSam Leffler ieee80211_set_tim(struct ieee80211_node *ni, int set)
18648a1b9b6aSSam Leffler {
1865edfa57d0SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
18668a1b9b6aSSam Leffler 	u_int16_t aid;
18678a1b9b6aSSam Leffler 
18688a1b9b6aSSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
18698a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_IBSS,
18708a1b9b6aSSam Leffler 		("operating mode %u", ic->ic_opmode));
18718a1b9b6aSSam Leffler 
18728a1b9b6aSSam Leffler 	aid = IEEE80211_AID(ni->ni_associd);
18738a1b9b6aSSam Leffler 	KASSERT(aid < ic->ic_max_aid,
18748a1b9b6aSSam Leffler 		("bogus aid %u, max %u", aid, ic->ic_max_aid));
18758a1b9b6aSSam Leffler 
18768a1b9b6aSSam Leffler 	IEEE80211_BEACON_LOCK(ic);
18778a1b9b6aSSam Leffler 	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
18788a1b9b6aSSam Leffler 		if (set) {
18798a1b9b6aSSam Leffler 			setbit(ic->ic_tim_bitmap, aid);
18808a1b9b6aSSam Leffler 			ic->ic_ps_pending++;
18818a1b9b6aSSam Leffler 		} else {
18828a1b9b6aSSam Leffler 			clrbit(ic->ic_tim_bitmap, aid);
18838a1b9b6aSSam Leffler 			ic->ic_ps_pending--;
18848a1b9b6aSSam Leffler 		}
18858a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
18868a1b9b6aSSam Leffler 	}
18878a1b9b6aSSam Leffler 	IEEE80211_BEACON_UNLOCK(ic);
18888a1b9b6aSSam Leffler }
18898a1b9b6aSSam Leffler 
18908a1b9b6aSSam Leffler /*
18918a1b9b6aSSam Leffler  * Node table support.
18928a1b9b6aSSam Leffler  */
18938a1b9b6aSSam Leffler 
18948a1b9b6aSSam Leffler static void
18958a1b9b6aSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic,
18968a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt,
18978a1b9b6aSSam Leffler 	const char *name, int inact,
18988a1b9b6aSSam Leffler 	void (*timeout)(struct ieee80211_node_table *))
18998a1b9b6aSSam Leffler {
19008a1b9b6aSSam Leffler 
19018a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
19028a1b9b6aSSam Leffler 		"%s %s table, inact %u\n", __func__, name, inact);
19038a1b9b6aSSam Leffler 
19048a1b9b6aSSam Leffler 	nt->nt_ic = ic;
19058a1b9b6aSSam Leffler 	/* XXX need unit */
19068a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
19078a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
19088a1b9b6aSSam Leffler 	TAILQ_INIT(&nt->nt_node);
19098a1b9b6aSSam Leffler 	nt->nt_name = name;
19108a1b9b6aSSam Leffler 	nt->nt_scangen = 1;
19118a1b9b6aSSam Leffler 	nt->nt_inact_init = inact;
19128a1b9b6aSSam Leffler 	nt->nt_timeout = timeout;
19138a1b9b6aSSam Leffler }
19148a1b9b6aSSam Leffler 
19158a1b9b6aSSam Leffler void
19168a1b9b6aSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt)
19178a1b9b6aSSam Leffler {
19188a1b9b6aSSam Leffler 
19198a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
19208a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
19218a1b9b6aSSam Leffler 
19228a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
19238a1b9b6aSSam Leffler 	nt->nt_inact_timer = 0;
19248a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
19258a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
19268a1b9b6aSSam Leffler }
19278a1b9b6aSSam Leffler 
19288a1b9b6aSSam Leffler static void
19298a1b9b6aSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
19308a1b9b6aSSam Leffler {
19318a1b9b6aSSam Leffler 
19328a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
19338a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
19348a1b9b6aSSam Leffler 
19358a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
19368a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_DESTROY(nt);
19378a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_DESTROY(nt);
19388a1b9b6aSSam Leffler }
1939