xref: /freebsd/sys/net80211/ieee80211_node.c (revision 1fd2349de846a8792cf07fb5c42880bc9408f95e)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
31f1d7810SSam Leffler  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
147535e66aSSam Leffler  * 3. The name of the author may not be used to endorse or promote products
157535e66aSSam Leffler  *    derived from this software without specific prior written permission.
161a1e1d21SSam Leffler  *
171a1e1d21SSam Leffler  * Alternatively, this software may be distributed under the terms of the
181a1e1d21SSam Leffler  * GNU General Public License ("GPL") version 2 as published by the Free
191a1e1d21SSam Leffler  * Software Foundation.
201a1e1d21SSam Leffler  *
217535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
227535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
237535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
247535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
257535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
267535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
277535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
287535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
297535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
307535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311a1e1d21SSam Leffler  */
321a1e1d21SSam Leffler 
331a1e1d21SSam Leffler #include <sys/cdefs.h>
341a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
351a1e1d21SSam Leffler 
361a1e1d21SSam Leffler #include <sys/param.h>
371a1e1d21SSam Leffler #include <sys/systm.h>
381a1e1d21SSam Leffler #include <sys/mbuf.h>
391a1e1d21SSam Leffler #include <sys/malloc.h>
401a1e1d21SSam Leffler #include <sys/kernel.h>
411a1e1d21SSam Leffler 
428a1b9b6aSSam Leffler #include <sys/socket.h>
431a1e1d21SSam Leffler 
441a1e1d21SSam Leffler #include <net/if.h>
451a1e1d21SSam Leffler #include <net/if_media.h>
461a1e1d21SSam Leffler #include <net/ethernet.h>
471a1e1d21SSam Leffler 
481a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
491a1e1d21SSam Leffler 
501a1e1d21SSam Leffler #include <net/bpf.h>
511a1e1d21SSam Leffler 
527268fa64SSam Leffler /*
537268fa64SSam Leffler  * Association id's are managed with a bit vector.
547268fa64SSam Leffler  */
557268fa64SSam Leffler #define	IEEE80211_AID_SET(b, w) \
567268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
577268fa64SSam Leffler #define	IEEE80211_AID_CLR(b, w) \
587268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
597268fa64SSam Leffler #define	IEEE80211_AID_ISSET(b, w) \
607268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
617268fa64SSam Leffler 
628a1b9b6aSSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
638a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *);
648a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *);
658a1b9b6aSSam Leffler static u_int8_t node_getrssi(const struct ieee80211_node *);
661a1e1d21SSam Leffler 
678a1b9b6aSSam Leffler static void ieee80211_setup_node(struct ieee80211_node_table *,
688a1b9b6aSSam Leffler 		struct ieee80211_node *, const u_int8_t *);
698a1b9b6aSSam Leffler static void _ieee80211_free_node(struct ieee80211_node *);
708a1b9b6aSSam Leffler static void ieee80211_free_allnodes(struct ieee80211_node_table *);
71d1e61976SSam Leffler 
728a1b9b6aSSam Leffler static void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
738a1b9b6aSSam Leffler static void ieee80211_timeout_stations(struct ieee80211_node_table *);
748a1b9b6aSSam Leffler 
75edfa57d0SSam Leffler static void ieee80211_set_tim(struct ieee80211_node *, int set);
768a1b9b6aSSam Leffler 
778a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic,
78c1225b52SSam Leffler 	struct ieee80211_node_table *nt, const char *name,
79c1225b52SSam Leffler 	int inact, int keyixmax,
808a1b9b6aSSam Leffler 	void (*timeout)(struct ieee80211_node_table *));
818a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
821a1e1d21SSam Leffler 
8332346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
8437c150c4SSam Leffler 
851a1e1d21SSam Leffler void
868a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic)
871a1e1d21SSam Leffler {
881a1e1d21SSam Leffler 
898a1b9b6aSSam Leffler 	ic->ic_node_alloc = node_alloc;
908a1b9b6aSSam Leffler 	ic->ic_node_free = node_free;
918a1b9b6aSSam Leffler 	ic->ic_node_cleanup = node_cleanup;
928a1b9b6aSSam Leffler 	ic->ic_node_getrssi = node_getrssi;
938a1b9b6aSSam Leffler 
948a1b9b6aSSam Leffler 	/* default station inactivity timer setings */
958a1b9b6aSSam Leffler 	ic->ic_inact_init = IEEE80211_INACT_INIT;
968a1b9b6aSSam Leffler 	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
978a1b9b6aSSam Leffler 	ic->ic_inact_run = IEEE80211_INACT_RUN;
988a1b9b6aSSam Leffler 	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
998a1b9b6aSSam Leffler 
100c1225b52SSam Leffler 	/* NB: driver should override */
1018a1b9b6aSSam Leffler 	ic->ic_max_aid = IEEE80211_AID_DEF;
102c1225b52SSam Leffler 	ic->ic_set_tim = ieee80211_set_tim;
103c1225b52SSam Leffler }
104c1225b52SSam Leffler 
105c1225b52SSam Leffler void
106c1225b52SSam Leffler ieee80211_node_lateattach(struct ieee80211com *ic)
107c1225b52SSam Leffler {
108c1225b52SSam Leffler 	struct ieee80211_rsnparms *rsn;
109c1225b52SSam Leffler 
110c1225b52SSam Leffler 	if (ic->ic_max_aid > IEEE80211_AID_MAX)
1118a1b9b6aSSam Leffler 		ic->ic_max_aid = IEEE80211_AID_MAX;
1128a1b9b6aSSam Leffler 	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
1138a1b9b6aSSam Leffler 		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
1148a1b9b6aSSam Leffler 		M_DEVBUF, M_NOWAIT | M_ZERO);
1158a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap == NULL) {
1168a1b9b6aSSam Leffler 		/* XXX no way to recover */
1178a1b9b6aSSam Leffler 		printf("%s: no memory for AID bitmap!\n", __func__);
1188a1b9b6aSSam Leffler 		ic->ic_max_aid = 0;
1198a1b9b6aSSam Leffler 	}
1208a1b9b6aSSam Leffler 
1218a1b9b6aSSam Leffler 	/* XXX defer until using hostap/ibss mode */
1228a1b9b6aSSam Leffler 	ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
1238a1b9b6aSSam Leffler 	MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
1248a1b9b6aSSam Leffler 		M_DEVBUF, M_NOWAIT | M_ZERO);
1258a1b9b6aSSam Leffler 	if (ic->ic_tim_bitmap == NULL) {
1268a1b9b6aSSam Leffler 		/* XXX no way to recover */
1278a1b9b6aSSam Leffler 		printf("%s: no memory for TIM bitmap!\n", __func__);
1288a1b9b6aSSam Leffler 	}
1292692bb26SSam Leffler 
130c1225b52SSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
131c1225b52SSam Leffler 		IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix,
132c1225b52SSam Leffler 		ieee80211_timeout_stations);
133c1225b52SSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
134c1225b52SSam Leffler 		IEEE80211_INACT_SCAN, 0,
135c1225b52SSam Leffler 		ieee80211_timeout_scan_candidates);
1362692bb26SSam Leffler 
137c1225b52SSam Leffler 	ieee80211_reset_bss(ic);
1388a1b9b6aSSam Leffler 	/*
1398a1b9b6aSSam Leffler 	 * Setup "global settings" in the bss node so that
1408a1b9b6aSSam Leffler 	 * each new station automatically inherits them.
1418a1b9b6aSSam Leffler 	 */
142c1225b52SSam Leffler 	rsn = &ic->ic_bss->ni_rsn;
1438a1b9b6aSSam Leffler 	/* WEP, TKIP, and AES-CCM are always supported */
1448a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
1458a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
1468a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
1478a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_AES)
1488a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
1498a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_CKIP)
1508a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
1518a1b9b6aSSam Leffler 	/*
1528a1b9b6aSSam Leffler 	 * Default unicast cipher to WEP for 802.1x use.  If
1538a1b9b6aSSam Leffler 	 * WPA is enabled the management code will set these
1548a1b9b6aSSam Leffler 	 * values to reflect.
1558a1b9b6aSSam Leffler 	 */
1568a1b9b6aSSam Leffler 	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
1578a1b9b6aSSam Leffler 	rsn->rsn_ucastkeylen = 104 / NBBY;
1588a1b9b6aSSam Leffler 	/*
1598a1b9b6aSSam Leffler 	 * WPA says the multicast cipher is the lowest unicast
1608a1b9b6aSSam Leffler 	 * cipher supported.  But we skip WEP which would
1618a1b9b6aSSam Leffler 	 * otherwise be used based on this criteria.
1628a1b9b6aSSam Leffler 	 */
1638a1b9b6aSSam Leffler 	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
1648a1b9b6aSSam Leffler 	rsn->rsn_mcastkeylen = 128 / NBBY;
1658a1b9b6aSSam Leffler 
1668a1b9b6aSSam Leffler 	/*
1678a1b9b6aSSam Leffler 	 * We support both WPA-PSK and 802.1x; the one used
1688a1b9b6aSSam Leffler 	 * is determined by the authentication mode and the
1698a1b9b6aSSam Leffler 	 * setting of the PSK state.
1708a1b9b6aSSam Leffler 	 */
1718a1b9b6aSSam Leffler 	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
1728a1b9b6aSSam Leffler 	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1738a1b9b6aSSam Leffler 
174c1225b52SSam Leffler 	ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
1751a1e1d21SSam Leffler }
1761a1e1d21SSam Leffler 
1771a1e1d21SSam Leffler void
1788a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic)
1791a1e1d21SSam Leffler {
1801a1e1d21SSam Leffler 
1818a1b9b6aSSam Leffler 	if (ic->ic_bss != NULL) {
1828a1b9b6aSSam Leffler 		ieee80211_free_node(ic->ic_bss);
1838a1b9b6aSSam Leffler 		ic->ic_bss = NULL;
1848a1b9b6aSSam Leffler 	}
1858a1b9b6aSSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_scan);
186acc4f7f5SSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_sta);
1878a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap != NULL) {
1888a1b9b6aSSam Leffler 		FREE(ic->ic_aid_bitmap, M_DEVBUF);
1898a1b9b6aSSam Leffler 		ic->ic_aid_bitmap = NULL;
1908a1b9b6aSSam Leffler 	}
1918a1b9b6aSSam Leffler 	if (ic->ic_tim_bitmap != NULL) {
1928a1b9b6aSSam Leffler 		FREE(ic->ic_tim_bitmap, M_DEVBUF);
1938a1b9b6aSSam Leffler 		ic->ic_tim_bitmap = NULL;
1948a1b9b6aSSam Leffler 	}
1958a1b9b6aSSam Leffler }
1968a1b9b6aSSam Leffler 
1978a1b9b6aSSam Leffler /*
1988a1b9b6aSSam Leffler  * Port authorize/unauthorize interfaces for use by an authenticator.
1998a1b9b6aSSam Leffler  */
2008a1b9b6aSSam Leffler 
2018a1b9b6aSSam Leffler void
202e4918ecdSSam Leffler ieee80211_node_authorize(struct ieee80211_node *ni)
2038a1b9b6aSSam Leffler {
204e4918ecdSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
205e4918ecdSSam Leffler 
2068a1b9b6aSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AUTH;
2072045f699SSam Leffler 	ni->ni_inact_reload = ic->ic_inact_run;
2088a1b9b6aSSam Leffler }
2098a1b9b6aSSam Leffler 
2108a1b9b6aSSam Leffler void
211e4918ecdSSam Leffler ieee80211_node_unauthorize(struct ieee80211_node *ni)
2128a1b9b6aSSam Leffler {
2138a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
2148a1b9b6aSSam Leffler }
2158a1b9b6aSSam Leffler 
2168a1b9b6aSSam Leffler /*
2178a1b9b6aSSam Leffler  * Set/change the channel.  The rate set is also updated as
2188a1b9b6aSSam Leffler  * to insure a consistent view by drivers.
2198a1b9b6aSSam Leffler  */
2201b49e120SSam Leffler static void
2218a1b9b6aSSam Leffler ieee80211_set_chan(struct ieee80211com *ic,
2228a1b9b6aSSam Leffler 	struct ieee80211_node *ni, struct ieee80211_channel *chan)
2238a1b9b6aSSam Leffler {
2241b49e120SSam Leffler 	if (chan == IEEE80211_CHAN_ANYC)	/* XXX while scanning */
2251b49e120SSam Leffler 		chan = ic->ic_curchan;
2268a1b9b6aSSam Leffler 	ni->ni_chan = chan;
2278a1b9b6aSSam Leffler 	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
2281a1e1d21SSam Leffler }
2291a1e1d21SSam Leffler 
2301a1e1d21SSam Leffler /*
2311a1e1d21SSam Leffler  * AP scanning support.
2321a1e1d21SSam Leffler  */
2331a1e1d21SSam Leffler 
2348a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
2358a1b9b6aSSam Leffler static void
2368a1b9b6aSSam Leffler dump_chanlist(const u_char chans[])
2378a1b9b6aSSam Leffler {
2388a1b9b6aSSam Leffler 	const char *sep;
2398a1b9b6aSSam Leffler 	int i;
2408a1b9b6aSSam Leffler 
2418a1b9b6aSSam Leffler 	sep = " ";
2428a1b9b6aSSam Leffler 	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
2438a1b9b6aSSam Leffler 		if (isset(chans, i)) {
2448a1b9b6aSSam Leffler 			printf("%s%u", sep, i);
2458a1b9b6aSSam Leffler 			sep = ", ";
2468a1b9b6aSSam Leffler 		}
2478a1b9b6aSSam Leffler }
2488a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2498a1b9b6aSSam Leffler 
2501a1e1d21SSam Leffler /*
2518a1b9b6aSSam Leffler  * Initialize the channel set to scan based on the
2521a1e1d21SSam Leffler  * of available channels and the current PHY mode.
2531a1e1d21SSam Leffler  */
254a11c9a5cSSam Leffler static void
2558a1b9b6aSSam Leffler ieee80211_reset_scan(struct ieee80211com *ic)
2561a1e1d21SSam Leffler {
2571a1e1d21SSam Leffler 
2588a1b9b6aSSam Leffler 	/* XXX ic_des_chan should be handled with ic_chan_active */
2598a1b9b6aSSam Leffler 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
2608a1b9b6aSSam Leffler 		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
2618a1b9b6aSSam Leffler 		setbit(ic->ic_chan_scan,
2628a1b9b6aSSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_des_chan));
2638a1b9b6aSSam Leffler 	} else
2641a1e1d21SSam Leffler 		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
2651a1e1d21SSam Leffler 			sizeof(ic->ic_chan_active));
2668a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
2678a1b9b6aSSam Leffler 	if (ieee80211_msg_scan(ic)) {
2688a1b9b6aSSam Leffler 		printf("%s: scan set:", __func__);
2698a1b9b6aSSam Leffler 		dump_chanlist(ic->ic_chan_scan);
2708a1b9b6aSSam Leffler 		printf(" start chan %u\n",
271b5c99415SSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_curchan));
2728a1b9b6aSSam Leffler 	}
2738a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2741a1e1d21SSam Leffler }
2751a1e1d21SSam Leffler 
2761a1e1d21SSam Leffler /*
2771a1e1d21SSam Leffler  * Begin an active scan.
2781a1e1d21SSam Leffler  */
2791a1e1d21SSam Leffler void
2808a1b9b6aSSam Leffler ieee80211_begin_scan(struct ieee80211com *ic, int reset)
2811a1e1d21SSam Leffler {
2821a1e1d21SSam Leffler 
2838a1b9b6aSSam Leffler 	ic->ic_scan.nt_scangen++;
284a11c9a5cSSam Leffler 	/*
285a11c9a5cSSam Leffler 	 * In all but hostap mode scanning starts off in
286a11c9a5cSSam Leffler 	 * an active mode before switching to passive.
287a11c9a5cSSam Leffler 	 */
2881be50176SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
289a11c9a5cSSam Leffler 		ic->ic_flags |= IEEE80211_F_ASCAN;
2901be50176SSam Leffler 		ic->ic_stats.is_scan_active++;
2911be50176SSam Leffler 	} else
2921be50176SSam Leffler 		ic->ic_stats.is_scan_passive++;
2933ea67c54SSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
2943ea67c54SSam Leffler 		"begin %s scan in %s mode, scangen %u\n",
2958a1b9b6aSSam Leffler 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
2963ea67c54SSam Leffler 		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
2971a1e1d21SSam Leffler 	/*
2988a1b9b6aSSam Leffler 	 * Clear scan state and flush any previously seen AP's.
2991a1e1d21SSam Leffler 	 */
3008a1b9b6aSSam Leffler 	ieee80211_reset_scan(ic);
3018a1b9b6aSSam Leffler 	if (reset)
3028a1b9b6aSSam Leffler 		ieee80211_free_allnodes(&ic->ic_scan);
3038a1b9b6aSSam Leffler 
3048a1b9b6aSSam Leffler 	ic->ic_flags |= IEEE80211_F_SCAN;
3051a1e1d21SSam Leffler 
306a11c9a5cSSam Leffler 	/* Scan the next channel. */
3078a1b9b6aSSam Leffler 	ieee80211_next_scan(ic);
3081a1e1d21SSam Leffler }
3091a1e1d21SSam Leffler 
3101a1e1d21SSam Leffler /*
3111a1e1d21SSam Leffler  * Switch to the next channel marked for scanning.
3121a1e1d21SSam Leffler  */
3138a1b9b6aSSam Leffler int
3148a1b9b6aSSam Leffler ieee80211_next_scan(struct ieee80211com *ic)
3151a1e1d21SSam Leffler {
3161a1e1d21SSam Leffler 	struct ieee80211_channel *chan;
3171a1e1d21SSam Leffler 
3188a1b9b6aSSam Leffler 	/*
3198a1b9b6aSSam Leffler 	 * Insure any previous mgt frame timeouts don't fire.
3208a1b9b6aSSam Leffler 	 * This assumes the driver does the right thing in
3218a1b9b6aSSam Leffler 	 * flushing anything queued in the driver and below.
3228a1b9b6aSSam Leffler 	 */
3238a1b9b6aSSam Leffler 	ic->ic_mgt_timer = 0;
3248a1b9b6aSSam Leffler 
325b5c99415SSam Leffler 	chan = ic->ic_curchan;
3268a1b9b6aSSam Leffler 	do {
3271a1e1d21SSam Leffler 		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
3281a1e1d21SSam Leffler 			chan = &ic->ic_channels[0];
3291a1e1d21SSam Leffler 		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
3301a1e1d21SSam Leffler 			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
3318a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
3328a1b9b6aSSam Leffler 			    "%s: chan %d->%d\n", __func__,
333b5c99415SSam Leffler 			    ieee80211_chan2ieee(ic, ic->ic_curchan),
3348a1b9b6aSSam Leffler 			    ieee80211_chan2ieee(ic, chan));
335b5c99415SSam Leffler 			ic->ic_curchan = chan;
336b5c99415SSam Leffler 			/*
337b5c99415SSam Leffler 			 * XXX drivers should do this as needed,
338b5c99415SSam Leffler 			 * XXX for now maintain compatibility
339b5c99415SSam Leffler 			 */
340b5c99415SSam Leffler 			ic->ic_bss->ni_rates =
341b5c99415SSam Leffler 				ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
342a11c9a5cSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
3438a1b9b6aSSam Leffler 			return 1;
3448a1b9b6aSSam Leffler 		}
345b5c99415SSam Leffler 	} while (chan != ic->ic_curchan);
3468a1b9b6aSSam Leffler 	ieee80211_end_scan(ic);
3478a1b9b6aSSam Leffler 	return 0;
3481a1e1d21SSam Leffler }
3491a1e1d21SSam Leffler 
350f9cd9174SSam Leffler static __inline void
351f9cd9174SSam Leffler copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
352f9cd9174SSam Leffler {
353f9cd9174SSam Leffler 	/* propagate useful state */
354f9cd9174SSam Leffler 	nbss->ni_authmode = obss->ni_authmode;
355f9cd9174SSam Leffler 	nbss->ni_txpower = obss->ni_txpower;
356f9cd9174SSam Leffler 	nbss->ni_vlan = obss->ni_vlan;
357f9cd9174SSam Leffler 	nbss->ni_rsn = obss->ni_rsn;
358f9cd9174SSam Leffler 	/* XXX statistics? */
359f9cd9174SSam Leffler }
360f9cd9174SSam Leffler 
3611a1e1d21SSam Leffler void
3621a1e1d21SSam Leffler ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
3631a1e1d21SSam Leffler {
364acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt;
3651a1e1d21SSam Leffler 	struct ieee80211_node *ni;
3668a1b9b6aSSam Leffler 
3678a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
3688a1b9b6aSSam Leffler 		"%s: creating ibss\n", __func__);
3698a1b9b6aSSam Leffler 
3708a1b9b6aSSam Leffler 	/*
3718a1b9b6aSSam Leffler 	 * Create the station/neighbor table.  Note that for adhoc
3728a1b9b6aSSam Leffler 	 * mode we make the initial inactivity timer longer since
3738a1b9b6aSSam Leffler 	 * we create nodes only through discovery and they typically
3748a1b9b6aSSam Leffler 	 * are long-lived associations.
3758a1b9b6aSSam Leffler 	 */
376acc4f7f5SSam Leffler 	nt = &ic->ic_sta;
377acc4f7f5SSam Leffler 	IEEE80211_NODE_LOCK(nt);
378acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
379acc4f7f5SSam Leffler 		nt->nt_name = "station";
380acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_init;
381acc4f7f5SSam Leffler 	} else {
382acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
383acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
384acc4f7f5SSam Leffler 	}
385acc4f7f5SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
386acc4f7f5SSam Leffler 
387c1225b52SSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
388acc4f7f5SSam Leffler 	if (ni == NULL) {
389acc4f7f5SSam Leffler 		/* XXX recovery? */
3908a1b9b6aSSam Leffler 		return;
3918a1b9b6aSSam Leffler 	}
3921a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
3931a1e1d21SSam Leffler 	ni->ni_esslen = ic->ic_des_esslen;
3941a1e1d21SSam Leffler 	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
395f9cd9174SSam Leffler 	copy_bss(ni, ic->ic_bss);
396d365f9c7SSam Leffler 	ni->ni_intval = ic->ic_bintval;
3978a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
3981a1e1d21SSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
3991a1e1d21SSam Leffler 	if (ic->ic_phytype == IEEE80211_T_FH) {
4001a1e1d21SSam Leffler 		ni->ni_fhdwell = 200;	/* XXX */
4011a1e1d21SSam Leffler 		ni->ni_fhindex = 1;
4021a1e1d21SSam Leffler 	}
4038a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
4048a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SIBSS;
4058a1b9b6aSSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
40648b0a5beSSam Leffler 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
40748b0a5beSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
40848b0a5beSSam Leffler 		else
4098a1b9b6aSSam Leffler 			ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
4108a1b9b6aSSam Leffler 	}
4118a1b9b6aSSam Leffler 	/*
4128a1b9b6aSSam Leffler 	 * Fix the channel and related attributes.
4138a1b9b6aSSam Leffler 	 */
4148a1b9b6aSSam Leffler 	ieee80211_set_chan(ic, ni, chan);
415b5c99415SSam Leffler 	ic->ic_curchan = chan;
4168a1b9b6aSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
4178a1b9b6aSSam Leffler 	/*
4188a1b9b6aSSam Leffler 	 * Do mode-specific rate setup.
4198a1b9b6aSSam Leffler 	 */
4208a1b9b6aSSam Leffler 	if (ic->ic_curmode == IEEE80211_MODE_11G) {
4218a1b9b6aSSam Leffler 		/*
4228a1b9b6aSSam Leffler 		 * Use a mixed 11b/11g rate set.
4238a1b9b6aSSam Leffler 		 */
4248a1b9b6aSSam Leffler 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
4258a1b9b6aSSam Leffler 	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
4268a1b9b6aSSam Leffler 		/*
4278a1b9b6aSSam Leffler 		 * Force pure 11b rate set.
4288a1b9b6aSSam Leffler 		 */
4298a1b9b6aSSam Leffler 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
4308a1b9b6aSSam Leffler 	}
4318a1b9b6aSSam Leffler 
432acc4f7f5SSam Leffler 	(void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
4331a1e1d21SSam Leffler }
4341a1e1d21SSam Leffler 
4358a1b9b6aSSam Leffler void
4368a1b9b6aSSam Leffler ieee80211_reset_bss(struct ieee80211com *ic)
437b4c5a90fSSam Leffler {
4388a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *obss;
4398a1b9b6aSSam Leffler 
4408a1b9b6aSSam Leffler 	ieee80211_node_table_reset(&ic->ic_scan);
441acc4f7f5SSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta);
442acc4f7f5SSam Leffler 
4438a1b9b6aSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
4448a1b9b6aSSam Leffler 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
4458a1b9b6aSSam Leffler 	obss = ic->ic_bss;
4468a1b9b6aSSam Leffler 	ic->ic_bss = ieee80211_ref_node(ni);
447f9cd9174SSam Leffler 	if (obss != NULL) {
448f9cd9174SSam Leffler 		copy_bss(ni, obss);
449d365f9c7SSam Leffler 		ni->ni_intval = ic->ic_bintval;
4508a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
4518a1b9b6aSSam Leffler 	}
452f9cd9174SSam Leffler }
4538a1b9b6aSSam Leffler 
454936f15d2SSam Leffler /* XXX tunable */
455936f15d2SSam Leffler #define	STA_FAILS_MAX	2		/* assoc failures before ignored */
456936f15d2SSam Leffler 
4578a1b9b6aSSam Leffler static int
4588a1b9b6aSSam Leffler ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
4598a1b9b6aSSam Leffler {
460b4c5a90fSSam Leffler         u_int8_t rate;
461b4c5a90fSSam Leffler         int fail;
462b4c5a90fSSam Leffler 
463b4c5a90fSSam Leffler 	fail = 0;
464b4c5a90fSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
465b4c5a90fSSam Leffler 		fail |= 0x01;
466b4c5a90fSSam Leffler 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
467b4c5a90fSSam Leffler 	    ni->ni_chan != ic->ic_des_chan)
468b4c5a90fSSam Leffler 		fail |= 0x01;
469b4c5a90fSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
470b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
471b4c5a90fSSam Leffler 			fail |= 0x02;
472b4c5a90fSSam Leffler 	} else {
473b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
474b4c5a90fSSam Leffler 			fail |= 0x02;
475b4c5a90fSSam Leffler 	}
4768a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
477b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
478b4c5a90fSSam Leffler 			fail |= 0x04;
479b4c5a90fSSam Leffler 	} else {
480b4c5a90fSSam Leffler 		/* XXX does this mean privacy is supported or required? */
481b4c5a90fSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
482b4c5a90fSSam Leffler 			fail |= 0x04;
483b4c5a90fSSam Leffler 	}
4847d77cd53SSam Leffler 	rate = ieee80211_fix_rate(ni, IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
485b4c5a90fSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
486b4c5a90fSSam Leffler 		fail |= 0x08;
487b4c5a90fSSam Leffler 	if (ic->ic_des_esslen != 0 &&
488b4c5a90fSSam Leffler 	    (ni->ni_esslen != ic->ic_des_esslen ||
489b4c5a90fSSam Leffler 	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
490b4c5a90fSSam Leffler 		fail |= 0x10;
491b4c5a90fSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
492b4c5a90fSSam Leffler 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
493b4c5a90fSSam Leffler 		fail |= 0x20;
494936f15d2SSam Leffler 	if (ni->ni_fails >= STA_FAILS_MAX)
495936f15d2SSam Leffler 		fail |= 0x40;
496b4c5a90fSSam Leffler #ifdef IEEE80211_DEBUG
4978a1b9b6aSSam Leffler 	if (ieee80211_msg_scan(ic)) {
498936f15d2SSam Leffler 		printf(" %c %s",
499936f15d2SSam Leffler 		    fail & 0x40 ? '=' : fail & 0x80 ? '^' : fail ? '-' : '+',
500b4c5a90fSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
501b4c5a90fSSam Leffler 		printf(" %s%c", ether_sprintf(ni->ni_bssid),
502b4c5a90fSSam Leffler 		    fail & 0x20 ? '!' : ' ');
503b4c5a90fSSam Leffler 		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
504b4c5a90fSSam Leffler 			fail & 0x01 ? '!' : ' ');
505b4c5a90fSSam Leffler 		printf(" %+4d", ni->ni_rssi);
506b4c5a90fSSam Leffler 		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
507b4c5a90fSSam Leffler 		    fail & 0x08 ? '!' : ' ');
508b4c5a90fSSam Leffler 		printf(" %4s%c",
509b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
510b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
511b4c5a90fSSam Leffler 		    "????",
512b4c5a90fSSam Leffler 		    fail & 0x02 ? '!' : ' ');
513b4c5a90fSSam Leffler 		printf(" %3s%c ",
514b4c5a90fSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
515b4c5a90fSSam Leffler 		    "wep" : "no",
516b4c5a90fSSam Leffler 		    fail & 0x04 ? '!' : ' ');
517b4c5a90fSSam Leffler 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
518b4c5a90fSSam Leffler 		printf("%s\n", fail & 0x10 ? "!" : "");
519b4c5a90fSSam Leffler 	}
520b4c5a90fSSam Leffler #endif
521b4c5a90fSSam Leffler 	return fail;
522b4c5a90fSSam Leffler }
523b4c5a90fSSam Leffler 
5248a1b9b6aSSam Leffler static __inline u_int8_t
5258a1b9b6aSSam Leffler maxrate(const struct ieee80211_node *ni)
5268a1b9b6aSSam Leffler {
5278a1b9b6aSSam Leffler 	const struct ieee80211_rateset *rs = &ni->ni_rates;
5288a1b9b6aSSam Leffler 	/* NB: assumes rate set is sorted (happens on frame receive) */
5298a1b9b6aSSam Leffler 	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
5308a1b9b6aSSam Leffler }
5318a1b9b6aSSam Leffler 
5328a1b9b6aSSam Leffler /*
5338a1b9b6aSSam Leffler  * Compare the capabilities of two nodes and decide which is
5348a1b9b6aSSam Leffler  * more desirable (return >0 if a is considered better).  Note
5358a1b9b6aSSam Leffler  * that we assume compatibility/usability has already been checked
5368a1b9b6aSSam Leffler  * so we don't need to (e.g. validate whether privacy is supported).
5378a1b9b6aSSam Leffler  * Used to select the best scan candidate for association in a BSS.
5388a1b9b6aSSam Leffler  */
5398a1b9b6aSSam Leffler static int
5408a1b9b6aSSam Leffler ieee80211_node_compare(struct ieee80211com *ic,
5418a1b9b6aSSam Leffler 		       const struct ieee80211_node *a,
5428a1b9b6aSSam Leffler 		       const struct ieee80211_node *b)
5438a1b9b6aSSam Leffler {
5448a1b9b6aSSam Leffler 	u_int8_t maxa, maxb;
5458a1b9b6aSSam Leffler 	u_int8_t rssia, rssib;
546936f15d2SSam Leffler 	int weight;
5478a1b9b6aSSam Leffler 
5488a1b9b6aSSam Leffler 	/* privacy support preferred */
5498a1b9b6aSSam Leffler 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
5508a1b9b6aSSam Leffler 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
5518a1b9b6aSSam Leffler 		return 1;
5528a1b9b6aSSam Leffler 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
5538a1b9b6aSSam Leffler 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
5548a1b9b6aSSam Leffler 		return -1;
5558a1b9b6aSSam Leffler 
556936f15d2SSam Leffler 	/* compare count of previous failures */
557936f15d2SSam Leffler 	weight = b->ni_fails - a->ni_fails;
558936f15d2SSam Leffler 	if (abs(weight) > 1)
559936f15d2SSam Leffler 		return weight;
560936f15d2SSam Leffler 
561ef92bcdcSSam Leffler 	rssia = ic->ic_node_getrssi(a);
562ef92bcdcSSam Leffler 	rssib = ic->ic_node_getrssi(b);
563ef92bcdcSSam Leffler 	if (abs(rssib - rssia) < 5) {
5648a1b9b6aSSam Leffler 		/* best/max rate preferred if signal level close enough XXX */
5658a1b9b6aSSam Leffler 		maxa = maxrate(a);
5668a1b9b6aSSam Leffler 		maxb = maxrate(b);
567ef92bcdcSSam Leffler 		if (maxa != maxb)
5688a1b9b6aSSam Leffler 			return maxa - maxb;
5698a1b9b6aSSam Leffler 		/* XXX use freq for channel preference */
5708a1b9b6aSSam Leffler 		/* for now just prefer 5Ghz band to all other bands */
5718a1b9b6aSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
5728a1b9b6aSSam Leffler 		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
5738a1b9b6aSSam Leffler 			return 1;
5748a1b9b6aSSam Leffler 		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
5758a1b9b6aSSam Leffler 		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
5768a1b9b6aSSam Leffler 			return -1;
577ef92bcdcSSam Leffler 	}
5788a1b9b6aSSam Leffler 	/* all things being equal, use signal level */
5798a1b9b6aSSam Leffler 	return rssia - rssib;
5808a1b9b6aSSam Leffler }
5818a1b9b6aSSam Leffler 
5821a1e1d21SSam Leffler /*
583c75ac469SSam Leffler  * Mark an ongoing scan stopped.
584c75ac469SSam Leffler  */
585c75ac469SSam Leffler void
586c75ac469SSam Leffler ieee80211_cancel_scan(struct ieee80211com *ic)
587c75ac469SSam Leffler {
588c75ac469SSam Leffler 
589c75ac469SSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
590c75ac469SSam Leffler 		__func__,
591c75ac469SSam Leffler 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
592c75ac469SSam Leffler 
593c75ac469SSam Leffler 	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
594c75ac469SSam Leffler }
595c75ac469SSam Leffler 
596c75ac469SSam Leffler /*
5971a1e1d21SSam Leffler  * Complete a scan of potential channels.
5981a1e1d21SSam Leffler  */
5991a1e1d21SSam Leffler void
6008a1b9b6aSSam Leffler ieee80211_end_scan(struct ieee80211com *ic)
6011a1e1d21SSam Leffler {
6023fcfbbfaSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_scan;
6033fcfbbfaSSam Leffler 	struct ieee80211_node *ni, *selbs;
6041a1e1d21SSam Leffler 
605c75ac469SSam Leffler 	ieee80211_cancel_scan(ic);
606c75ac469SSam Leffler 	ieee80211_notify_scan_done(ic);
6078a1b9b6aSSam Leffler 
6081a1e1d21SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
6098a1b9b6aSSam Leffler 		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
6108a1b9b6aSSam Leffler 		int i, bestchan;
6118a1b9b6aSSam Leffler 		u_int8_t rssi;
6128a1b9b6aSSam Leffler 
6131a1e1d21SSam Leffler 		/*
6141a1e1d21SSam Leffler 		 * The passive scan to look for existing AP's completed,
6151a1e1d21SSam Leffler 		 * select a channel to camp on.  Identify the channels
6161a1e1d21SSam Leffler 		 * that already have one or more AP's and try to locate
617acc4f7f5SSam Leffler 		 * an unoccupied one.  If that fails, pick a channel that
6188a1b9b6aSSam Leffler 		 * looks to be quietest.
6191a1e1d21SSam Leffler 		 */
6208a1b9b6aSSam Leffler 		memset(maxrssi, 0, sizeof(maxrssi));
6213fcfbbfaSSam Leffler 		IEEE80211_NODE_LOCK(nt);
6223fcfbbfaSSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
6238a1b9b6aSSam Leffler 			rssi = ic->ic_node_getrssi(ni);
6248a1b9b6aSSam Leffler 			i = ieee80211_chan2ieee(ic, ni->ni_chan);
6258a1b9b6aSSam Leffler 			if (rssi > maxrssi[i])
6268a1b9b6aSSam Leffler 				maxrssi[i] = rssi;
6271a1e1d21SSam Leffler 		}
6283fcfbbfaSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
6298a1b9b6aSSam Leffler 		/* XXX select channel more intelligently */
6308a1b9b6aSSam Leffler 		bestchan = -1;
6311a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
6328a1b9b6aSSam Leffler 			if (isset(ic->ic_chan_active, i)) {
6338a1b9b6aSSam Leffler 				/*
6348a1b9b6aSSam Leffler 				 * If the channel is unoccupied the max rssi
6358a1b9b6aSSam Leffler 				 * should be zero; just take it.  Otherwise
6368a1b9b6aSSam Leffler 				 * track the channel with the lowest rssi and
6378a1b9b6aSSam Leffler 				 * use that when all channels appear occupied.
6388a1b9b6aSSam Leffler 				 */
6398a1b9b6aSSam Leffler 				if (maxrssi[i] == 0) {
6408a1b9b6aSSam Leffler 					bestchan = i;
6411a1e1d21SSam Leffler 					break;
6421a1e1d21SSam Leffler 				}
6436edf09a6SSam Leffler 				if (bestchan == -1 ||
6446edf09a6SSam Leffler 				    maxrssi[i] < maxrssi[bestchan])
6458a1b9b6aSSam Leffler 					bestchan = i;
6468a1b9b6aSSam Leffler 			}
6478a1b9b6aSSam Leffler 		if (bestchan != -1) {
6488a1b9b6aSSam Leffler 			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
6491a1e1d21SSam Leffler 			return;
6501a1e1d21SSam Leffler 		}
6518a1b9b6aSSam Leffler 		/* no suitable channel, should not happen */
6528a1b9b6aSSam Leffler 	}
6538a1b9b6aSSam Leffler 
6548a1b9b6aSSam Leffler 	/*
6558a1b9b6aSSam Leffler 	 * When manually sequencing the state machine; scan just once
6568a1b9b6aSSam Leffler 	 * regardless of whether we have a candidate or not.  The
6578a1b9b6aSSam Leffler 	 * controlling application is expected to setup state and
6588a1b9b6aSSam Leffler 	 * initiate an association.
6598a1b9b6aSSam Leffler 	 */
6608a1b9b6aSSam Leffler 	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
6618a1b9b6aSSam Leffler 		return;
6628a1b9b6aSSam Leffler 	/*
6638a1b9b6aSSam Leffler 	 * Automatic sequencing; look for a candidate and
6648a1b9b6aSSam Leffler 	 * if found join the network.
6658a1b9b6aSSam Leffler 	 */
6663fcfbbfaSSam Leffler 	/* NB: unlocked read should be ok */
6673fcfbbfaSSam Leffler 	if (TAILQ_FIRST(&nt->nt_node) == NULL) {
6688a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
6698a1b9b6aSSam Leffler 			"%s: no scan candidate\n", __func__);
6701a1e1d21SSam Leffler   notfound:
6711a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
6721a1e1d21SSam Leffler 		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
6731a1e1d21SSam Leffler 		    ic->ic_des_esslen != 0) {
6741a1e1d21SSam Leffler 			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
6751a1e1d21SSam Leffler 			return;
6761a1e1d21SSam Leffler 		}
6771a1e1d21SSam Leffler 		/*
678936f15d2SSam Leffler 		 * Decrement the failure counts so entries will be
679936f15d2SSam Leffler 		 * reconsidered the next time around.  We really want
680936f15d2SSam Leffler 		 * to do this only for sta's where we've previously
6811fd2349dSSam Leffler 		 * had some success.
682936f15d2SSam Leffler 		 */
683936f15d2SSam Leffler 		IEEE80211_NODE_LOCK(nt);
684936f15d2SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
685936f15d2SSam Leffler 			if (ni->ni_fails)
686936f15d2SSam Leffler 				ni->ni_fails--;
687936f15d2SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
688936f15d2SSam Leffler 		/*
6891a1e1d21SSam Leffler 		 * Reset the list of channels to scan and start again.
6901a1e1d21SSam Leffler 		 */
6918a1b9b6aSSam Leffler 		ieee80211_reset_scan(ic);
6928a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SCAN;
6938a1b9b6aSSam Leffler 		ieee80211_next_scan(ic);
6941a1e1d21SSam Leffler 		return;
6951a1e1d21SSam Leffler 	}
6961a1e1d21SSam Leffler 	selbs = NULL;
6978a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
6988a1b9b6aSSam Leffler 	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
6993fcfbbfaSSam Leffler 	IEEE80211_NODE_LOCK(nt);
7003fcfbbfaSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
7018a1b9b6aSSam Leffler 		if (ieee80211_match_bss(ic, ni) == 0) {
7021a1e1d21SSam Leffler 			if (selbs == NULL)
7031a1e1d21SSam Leffler 				selbs = ni;
7043fcfbbfaSSam Leffler 			else if (ieee80211_node_compare(ic, ni, selbs) > 0)
7051a1e1d21SSam Leffler 				selbs = ni;
7061a1e1d21SSam Leffler 		}
7071a1e1d21SSam Leffler 	}
7083fcfbbfaSSam Leffler 	if (selbs != NULL)		/* NB: grab ref while dropping lock */
7093fcfbbfaSSam Leffler 		(void) ieee80211_ref_node(selbs);
7103fcfbbfaSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
7111a1e1d21SSam Leffler 	if (selbs == NULL)
7121a1e1d21SSam Leffler 		goto notfound;
7138a1b9b6aSSam Leffler 	if (!ieee80211_sta_join(ic, selbs)) {
7143fcfbbfaSSam Leffler 		ieee80211_free_node(selbs);
7151a1e1d21SSam Leffler 		goto notfound;
7161a1e1d21SSam Leffler 	}
7178a1b9b6aSSam Leffler }
7188a1b9b6aSSam Leffler 
719750d6d0cSSam Leffler /*
7208a1b9b6aSSam Leffler  * Handle 802.11 ad hoc network merge.  The
7218a1b9b6aSSam Leffler  * convention, set by the Wireless Ethernet Compatibility Alliance
7228a1b9b6aSSam Leffler  * (WECA), is that an 802.11 station will change its BSSID to match
7238a1b9b6aSSam Leffler  * the "oldest" 802.11 ad hoc network, on the same channel, that
7248a1b9b6aSSam Leffler  * has the station's desired SSID.  The "oldest" 802.11 network
7258a1b9b6aSSam Leffler  * sends beacons with the greatest TSF timestamp.
7268a1b9b6aSSam Leffler  *
7278a1b9b6aSSam Leffler  * The caller is assumed to validate TSF's before attempting a merge.
7288a1b9b6aSSam Leffler  *
7298a1b9b6aSSam Leffler  * Return !0 if the BSSID changed, 0 otherwise.
730750d6d0cSSam Leffler  */
7318a1b9b6aSSam Leffler int
732641b4d0bSSam Leffler ieee80211_ibss_merge(struct ieee80211_node *ni)
7338a1b9b6aSSam Leffler {
734641b4d0bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
7358a1b9b6aSSam Leffler 
73696acc1b6SSam Leffler 	if (ni == ic->ic_bss ||
73796acc1b6SSam Leffler 	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
7388a1b9b6aSSam Leffler 		/* unchanged, nothing to do */
7398a1b9b6aSSam Leffler 		return 0;
7408a1b9b6aSSam Leffler 	}
7418a1b9b6aSSam Leffler 	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
7428a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
7438a1b9b6aSSam Leffler 		    "%s: merge failed, capabilities mismatch\n", __func__);
7448a1b9b6aSSam Leffler 		ic->ic_stats.is_ibss_capmismatch++;
7458a1b9b6aSSam Leffler 		return 0;
7468a1b9b6aSSam Leffler 	}
7478a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
7488a1b9b6aSSam Leffler 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
7498a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
7508a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
7518a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
7528a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
7538a1b9b6aSSam Leffler 	);
754acc4f7f5SSam Leffler 	return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
7558a1b9b6aSSam Leffler }
7568a1b9b6aSSam Leffler 
7578a1b9b6aSSam Leffler /*
7588a1b9b6aSSam Leffler  * Join the specified IBSS/BSS network.  The node is assumed to
7598a1b9b6aSSam Leffler  * be passed in with a held reference.
7608a1b9b6aSSam Leffler  */
7618a1b9b6aSSam Leffler int
7628a1b9b6aSSam Leffler ieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
7638a1b9b6aSSam Leffler {
7648a1b9b6aSSam Leffler 	struct ieee80211_node *obss;
7658a1b9b6aSSam Leffler 
7668a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
767acc4f7f5SSam Leffler 		struct ieee80211_node_table *nt;
7688a1b9b6aSSam Leffler 		/*
76998ff6263SSam Leffler 		 * Delete unusable rates; we've already checked
77098ff6263SSam Leffler 		 * that the negotiated rate set is acceptable.
7718a1b9b6aSSam Leffler 		 */
7727d77cd53SSam Leffler 		ieee80211_fix_rate(selbs, IEEE80211_F_DODEL);
7738a1b9b6aSSam Leffler 		/*
774acc4f7f5SSam Leffler 		 * Fillin the neighbor table; it will already
7753d073929SSam Leffler 		 * exist if we are simply switching mastership.
776acc4f7f5SSam Leffler 		 * XXX ic_sta always setup so this is unnecessary?
7778a1b9b6aSSam Leffler 		 */
778acc4f7f5SSam Leffler 		nt = &ic->ic_sta;
779acc4f7f5SSam Leffler 		IEEE80211_NODE_LOCK(nt);
780acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
781acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
782acc4f7f5SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
7833d073929SSam Leffler 	}
7841a1e1d21SSam Leffler 
7858a1b9b6aSSam Leffler 	/*
7868a1b9b6aSSam Leffler 	 * Committed to selbs, setup state.
7878a1b9b6aSSam Leffler 	 */
7888a1b9b6aSSam Leffler 	obss = ic->ic_bss;
789acc4f7f5SSam Leffler 	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
7901fd2349dSSam Leffler 	if (obss != NULL) {
7911fd2349dSSam Leffler 		copy_bss(selbs, obss);
7928a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
7931fd2349dSSam Leffler 	}
7948a1b9b6aSSam Leffler 	/*
7958a1b9b6aSSam Leffler 	 * Set the erp state (mostly the slot time) to deal with
7968a1b9b6aSSam Leffler 	 * the auto-select case; this should be redundant if the
7978a1b9b6aSSam Leffler 	 * mode is locked.
7988a1b9b6aSSam Leffler 	 */
7998a1b9b6aSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
800b5c99415SSam Leffler 	ic->ic_curchan = selbs->ni_chan;
8018a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
8028a1b9b6aSSam Leffler 	ieee80211_wme_initparams(ic);
803acc4f7f5SSam Leffler 
804acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA)
8058a1b9b6aSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
806acc4f7f5SSam Leffler 	else
807acc4f7f5SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
8088a1b9b6aSSam Leffler 	return 1;
8098a1b9b6aSSam Leffler }
8108a1b9b6aSSam Leffler 
8118a1b9b6aSSam Leffler /*
8128a1b9b6aSSam Leffler  * Leave the specified IBSS/BSS network.  The node is assumed to
8138a1b9b6aSSam Leffler  * be passed in with a held reference.
8148a1b9b6aSSam Leffler  */
8158a1b9b6aSSam Leffler void
8168a1b9b6aSSam Leffler ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
8178a1b9b6aSSam Leffler {
8188a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
8198a1b9b6aSSam Leffler 	ieee80211_notify_node_leave(ic, ni);
8208a1b9b6aSSam Leffler }
8218a1b9b6aSSam Leffler 
8221a1e1d21SSam Leffler static struct ieee80211_node *
8238a1b9b6aSSam Leffler node_alloc(struct ieee80211_node_table *nt)
8241a1e1d21SSam Leffler {
825410ca74bSSam Leffler 	struct ieee80211_node *ni;
8268a1b9b6aSSam Leffler 
827410ca74bSSam Leffler 	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
828410ca74bSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
829410ca74bSSam Leffler 	return ni;
8301a1e1d21SSam Leffler }
8311a1e1d21SSam Leffler 
8328a1b9b6aSSam Leffler /*
8338a1b9b6aSSam Leffler  * Reclaim any resources in a node and reset any critical
8348a1b9b6aSSam Leffler  * state.  Typically nodes are free'd immediately after,
8358a1b9b6aSSam Leffler  * but in some cases the storage may be reused so we need
8368a1b9b6aSSam Leffler  * to insure consistent state (should probably fix that).
8378a1b9b6aSSam Leffler  */
8381a1e1d21SSam Leffler static void
8398a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni)
8401a1e1d21SSam Leffler {
8418a1b9b6aSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
8428a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
8438a1b9b6aSSam Leffler 	int i, qlen;
8448a1b9b6aSSam Leffler 
8458a1b9b6aSSam Leffler 	/* NB: preserve ni_table */
8468a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
8478a1b9b6aSSam Leffler 		ic->ic_ps_sta--;
8488a1b9b6aSSam Leffler 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
8498a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
8508a1b9b6aSSam Leffler 		    "[%s] power save mode off, %u sta's in ps mode\n",
8518a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
8528a1b9b6aSSam Leffler 	}
853ebdda46cSSam Leffler 	/*
854ebdda46cSSam Leffler 	 * Clear AREF flag that marks the authorization refcnt bump
855ebdda46cSSam Leffler 	 * has happened.  This is probably not needed as the node
856ebdda46cSSam Leffler 	 * should always be removed from the table so not found but
857ebdda46cSSam Leffler 	 * do it just in case.
858ebdda46cSSam Leffler 	 */
859ebdda46cSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AREF;
8608a1b9b6aSSam Leffler 
8618a1b9b6aSSam Leffler 	/*
8628a1b9b6aSSam Leffler 	 * Drain power save queue and, if needed, clear TIM.
8638a1b9b6aSSam Leffler 	 */
8648a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
8658a1b9b6aSSam Leffler 	if (qlen != 0 && ic->ic_set_tim != NULL)
866edfa57d0SSam Leffler 		ic->ic_set_tim(ni, 0);
8678a1b9b6aSSam Leffler 
8688a1b9b6aSSam Leffler 	ni->ni_associd = 0;
8698a1b9b6aSSam Leffler 	if (ni->ni_challenge != NULL) {
8708a1b9b6aSSam Leffler 		FREE(ni->ni_challenge, M_DEVBUF);
8718a1b9b6aSSam Leffler 		ni->ni_challenge = NULL;
8728a1b9b6aSSam Leffler 	}
8738a1b9b6aSSam Leffler 	/*
8748a1b9b6aSSam Leffler 	 * Preserve SSID, WPA, and WME ie's so the bss node is
8758a1b9b6aSSam Leffler 	 * reusable during a re-auth/re-assoc state transition.
8768a1b9b6aSSam Leffler 	 * If we remove these data they will not be recreated
8778a1b9b6aSSam Leffler 	 * because they come from a probe-response or beacon frame
8788a1b9b6aSSam Leffler 	 * which cannot be expected prior to the association-response.
8798a1b9b6aSSam Leffler 	 * This should not be an issue when operating in other modes
8808a1b9b6aSSam Leffler 	 * as stations leaving always go through a full state transition
8818a1b9b6aSSam Leffler 	 * which will rebuild this state.
8828a1b9b6aSSam Leffler 	 *
8838a1b9b6aSSam Leffler 	 * XXX does this leave us open to inheriting old state?
8848a1b9b6aSSam Leffler 	 */
8858a1b9b6aSSam Leffler 	for (i = 0; i < N(ni->ni_rxfrag); i++)
8868a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[i] != NULL) {
8878a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[i]);
8888a1b9b6aSSam Leffler 			ni->ni_rxfrag[i] = NULL;
8898a1b9b6aSSam Leffler 		}
890c1225b52SSam Leffler 	/*
891c1225b52SSam Leffler 	 * Must be careful here to remove any key map entry w/o a LOR.
892c1225b52SSam Leffler 	 */
893c1225b52SSam Leffler 	ieee80211_node_delucastkey(ni);
8948a1b9b6aSSam Leffler #undef N
8958a1b9b6aSSam Leffler }
8968a1b9b6aSSam Leffler 
8978a1b9b6aSSam Leffler static void
8988a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni)
8998a1b9b6aSSam Leffler {
9008a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
9018a1b9b6aSSam Leffler 
9028a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
9038a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
9048a1b9b6aSSam Leffler 		FREE(ni->ni_wpa_ie, M_DEVBUF);
9058a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
9068a1b9b6aSSam Leffler 		FREE(ni->ni_wme_ie, M_DEVBUF);
9078a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DESTROY(ni);
908410ca74bSSam Leffler 	FREE(ni, M_80211_NODE);
9091a1e1d21SSam Leffler }
9101a1e1d21SSam Leffler 
911d1e61976SSam Leffler static u_int8_t
9128a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni)
913d1e61976SSam Leffler {
914d1e61976SSam Leffler 	return ni->ni_rssi;
915d1e61976SSam Leffler }
916d1e61976SSam Leffler 
9171a1e1d21SSam Leffler static void
9188a1b9b6aSSam Leffler ieee80211_setup_node(struct ieee80211_node_table *nt,
9198a1b9b6aSSam Leffler 	struct ieee80211_node *ni, const u_int8_t *macaddr)
9201a1e1d21SSam Leffler {
9218a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9221a1e1d21SSam Leffler 	int hash;
9231a1e1d21SSam Leffler 
9248a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
92549a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
9268a1b9b6aSSam Leffler 		ether_sprintf(macaddr), nt->nt_name);
9278a1b9b6aSSam Leffler 
9281a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
9291a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
9308a1b9b6aSSam Leffler 	ieee80211_node_initref(ni);		/* mark referenced */
9318a1b9b6aSSam Leffler 	ni->ni_chan = IEEE80211_CHAN_ANYC;
9328a1b9b6aSSam Leffler 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
9338a1b9b6aSSam Leffler 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
9348a1b9b6aSSam Leffler 	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
9352045f699SSam Leffler 	ni->ni_inact_reload = nt->nt_inact_init;
9362045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
9378a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
9388a1b9b6aSSam Leffler 
9398a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
9408a1b9b6aSSam Leffler 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
9418a1b9b6aSSam Leffler 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
9428a1b9b6aSSam Leffler 	ni->ni_table = nt;
9438a1b9b6aSSam Leffler 	ni->ni_ic = ic;
9448a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
9451a1e1d21SSam Leffler }
9461a1e1d21SSam Leffler 
9471a1e1d21SSam Leffler struct ieee80211_node *
9488a1b9b6aSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
9491a1e1d21SSam Leffler {
9508a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9518a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9528a1b9b6aSSam Leffler 
9538a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
9541a1e1d21SSam Leffler 	if (ni != NULL)
9558a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
956c64bfa0fSSam Leffler 	else
957c64bfa0fSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
9581a1e1d21SSam Leffler 	return ni;
9591a1e1d21SSam Leffler }
9601a1e1d21SSam Leffler 
96197c973adSSam Leffler /*
96297c973adSSam Leffler  * Craft a temporary node suitable for sending a management frame
96397c973adSSam Leffler  * to the specified station.  We craft only as much state as we
96497c973adSSam Leffler  * need to do the work since the node will be immediately reclaimed
96597c973adSSam Leffler  * once the send completes.
96697c973adSSam Leffler  */
96797c973adSSam Leffler struct ieee80211_node *
96897c973adSSam Leffler ieee80211_tmp_node(struct ieee80211com *ic, const u_int8_t *macaddr)
96997c973adSSam Leffler {
97097c973adSSam Leffler 	struct ieee80211_node *ni;
97197c973adSSam Leffler 
97297c973adSSam Leffler 	ni = ic->ic_node_alloc(&ic->ic_sta);
97397c973adSSam Leffler 	if (ni != NULL) {
97497c973adSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
97597c973adSSam Leffler 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
97697c973adSSam Leffler 
97797c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
97897c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
97997c973adSSam Leffler 		ieee80211_node_initref(ni);		/* mark referenced */
98097c973adSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
98197c973adSSam Leffler 		/* NB: required by ieee80211_fix_rate */
98297c973adSSam Leffler 		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
98397c973adSSam Leffler 		ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
98497c973adSSam Leffler 			IEEE80211_KEYIX_NONE);
98597c973adSSam Leffler 		/* XXX optimize away */
98697c973adSSam Leffler 		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
98797c973adSSam Leffler 
98897c973adSSam Leffler 		ni->ni_table = NULL;		/* NB: pedantic */
98997c973adSSam Leffler 		ni->ni_ic = ic;
99097c973adSSam Leffler 	} else {
99197c973adSSam Leffler 		/* XXX msg */
99297c973adSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
99397c973adSSam Leffler 	}
99497c973adSSam Leffler 	return ni;
99597c973adSSam Leffler }
99697c973adSSam Leffler 
9971a1e1d21SSam Leffler struct ieee80211_node *
9988a1b9b6aSSam Leffler ieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
9991a1e1d21SSam Leffler {
10008a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
10018a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10028a1b9b6aSSam Leffler 
10038a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
10041a1e1d21SSam Leffler 	if (ni != NULL) {
10058a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
1006694dca64SSam Leffler 		/*
1007694dca64SSam Leffler 		 * Inherit from ic_bss.
1008694dca64SSam Leffler 		 */
10098a1b9b6aSSam Leffler 		ni->ni_authmode = ic->ic_bss->ni_authmode;
10108a1b9b6aSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
10118a1b9b6aSSam Leffler 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
1012694dca64SSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
10138a1b9b6aSSam Leffler 		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
10148a1b9b6aSSam Leffler 		ni->ni_rsn = ic->ic_bss->ni_rsn;
1015694dca64SSam Leffler 	} else
1016694dca64SSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
10171a1e1d21SSam Leffler 	return ni;
10181a1e1d21SSam Leffler }
10191a1e1d21SSam Leffler 
1020750d6d0cSSam Leffler static struct ieee80211_node *
10218a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10228a1b9b6aSSam Leffler _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
10238a1b9b6aSSam Leffler 	const u_int8_t *macaddr, const char *func, int line)
10248a1b9b6aSSam Leffler #else
10258a1b9b6aSSam Leffler _ieee80211_find_node(struct ieee80211_node_table *nt,
10268a1b9b6aSSam Leffler 	const u_int8_t *macaddr)
10278a1b9b6aSSam Leffler #endif
10281a1e1d21SSam Leffler {
10291a1e1d21SSam Leffler 	struct ieee80211_node *ni;
10301a1e1d21SSam Leffler 	int hash;
10311a1e1d21SSam Leffler 
10328a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
1033750d6d0cSSam Leffler 
10341a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
10358a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
10361a1e1d21SSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
10378a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
10388a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10398a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
104049a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
104149a15236SSam Leffler 			    func, line,
104249a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
10438a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
10448a1b9b6aSSam Leffler #endif
1045750d6d0cSSam Leffler 			return ni;
10461a1e1d21SSam Leffler 		}
10471a1e1d21SSam Leffler 	}
1048750d6d0cSSam Leffler 	return NULL;
1049750d6d0cSSam Leffler }
10508a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10518a1b9b6aSSam Leffler #define	_ieee80211_find_node(nt, mac) \
10528a1b9b6aSSam Leffler 	_ieee80211_find_node_debug(nt, mac, func, line)
10538a1b9b6aSSam Leffler #endif
1054750d6d0cSSam Leffler 
1055750d6d0cSSam Leffler struct ieee80211_node *
10568a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10578a1b9b6aSSam Leffler ieee80211_find_node_debug(struct ieee80211_node_table *nt,
10588a1b9b6aSSam Leffler 	const u_int8_t *macaddr, const char *func, int line)
10598a1b9b6aSSam Leffler #else
10608a1b9b6aSSam Leffler ieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
10618a1b9b6aSSam Leffler #endif
1062750d6d0cSSam Leffler {
1063750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1064750d6d0cSSam Leffler 
10658a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
10668a1b9b6aSSam Leffler 	ni = _ieee80211_find_node(nt, macaddr);
10678a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
10681a1e1d21SSam Leffler 	return ni;
10691a1e1d21SSam Leffler }
10701a1e1d21SSam Leffler 
10711a1e1d21SSam Leffler /*
10728a1b9b6aSSam Leffler  * Fake up a node; this handles node discovery in adhoc mode.
10738a1b9b6aSSam Leffler  * Note that for the driver's benefit we we treat this like
10748a1b9b6aSSam Leffler  * an association so the driver has an opportunity to setup
10758a1b9b6aSSam Leffler  * it's private state.
10768a1b9b6aSSam Leffler  */
10778a1b9b6aSSam Leffler struct ieee80211_node *
10788a1b9b6aSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
10798a1b9b6aSSam Leffler 	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
10808a1b9b6aSSam Leffler {
10818a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
10828a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10838a1b9b6aSSam Leffler 
1084be425a0fSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1085be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
10868a1b9b6aSSam Leffler 	ni = ieee80211_dup_bss(nt, macaddr);
10878a1b9b6aSSam Leffler 	if (ni != NULL) {
10888a1b9b6aSSam Leffler 		/* XXX no rate negotiation; just dup */
10898a1b9b6aSSam Leffler 		ni->ni_rates = ic->ic_bss->ni_rates;
1090736b3dc3SSam Leffler 		if (ic->ic_newassoc != NULL)
1091e9962332SSam Leffler 			ic->ic_newassoc(ni, 1);
10928a1b9b6aSSam Leffler 		/* XXX not right for 802.1x/WPA */
1093e4918ecdSSam Leffler 		ieee80211_node_authorize(ni);
10948a1b9b6aSSam Leffler 	}
10958a1b9b6aSSam Leffler 	return ni;
10968a1b9b6aSSam Leffler }
10978a1b9b6aSSam Leffler 
1098b5c99415SSam Leffler #ifdef IEEE80211_DEBUG
1099b5c99415SSam Leffler static void
1100b5c99415SSam Leffler dump_probe_beacon(u_int8_t subtype, int isnew,
1101b5c99415SSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN],
1102b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp)
1103b5c99415SSam Leffler {
1104b5c99415SSam Leffler 
1105b5c99415SSam Leffler 	printf("[%s] %s%s on chan %u (bss chan %u) ",
1106b5c99415SSam Leffler 	    ether_sprintf(mac), isnew ? "new " : "",
1107b5c99415SSam Leffler 	    ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
1108b5c99415SSam Leffler 	    sp->chan, sp->bchan);
1109b5c99415SSam Leffler 	ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
1110b5c99415SSam Leffler 	printf("\n");
1111b5c99415SSam Leffler 
1112b5c99415SSam Leffler 	if (isnew) {
1113b5c99415SSam Leffler 		printf("[%s] caps 0x%x bintval %u erp 0x%x",
1114b5c99415SSam Leffler 			ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
1115b5c99415SSam Leffler 		if (sp->country != NULL) {
1116b5c99415SSam Leffler #ifdef __FreeBSD__
1117b5c99415SSam Leffler 			printf(" country info %*D",
1118b5c99415SSam Leffler 				sp->country[1], sp->country+2, " ");
1119b5c99415SSam Leffler #else
1120b5c99415SSam Leffler 			int i;
1121b5c99415SSam Leffler 			printf(" country info");
1122b5c99415SSam Leffler 			for (i = 0; i < sp->country[1]; i++)
1123b5c99415SSam Leffler 				printf(" %02x", sp->country[i+2]);
1124b5c99415SSam Leffler #endif
1125b5c99415SSam Leffler 		}
1126b5c99415SSam Leffler 		printf("\n");
1127b5c99415SSam Leffler 	}
1128b5c99415SSam Leffler }
1129b5c99415SSam Leffler #endif /* IEEE80211_DEBUG */
1130b5c99415SSam Leffler 
1131b5c99415SSam Leffler static void
1132b5c99415SSam Leffler saveie(u_int8_t **iep, const u_int8_t *ie)
1133b5c99415SSam Leffler {
1134b5c99415SSam Leffler 
1135b5c99415SSam Leffler 	if (ie == NULL)
1136b5c99415SSam Leffler 		*iep = NULL;
1137b5c99415SSam Leffler 	else
1138b5c99415SSam Leffler 		ieee80211_saveie(iep, ie);
1139b5c99415SSam Leffler }
1140b5c99415SSam Leffler 
1141b5c99415SSam Leffler /*
1142b5c99415SSam Leffler  * Process a beacon or probe response frame.
1143b5c99415SSam Leffler  */
1144b5c99415SSam Leffler void
1145b5c99415SSam Leffler ieee80211_add_scan(struct ieee80211com *ic,
1146b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp,
1147b5c99415SSam Leffler 	const struct ieee80211_frame *wh,
1148b5c99415SSam Leffler 	int subtype, int rssi, int rstamp)
1149b5c99415SSam Leffler {
1150b5c99415SSam Leffler #define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1151b5c99415SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_scan;
1152b5c99415SSam Leffler 	struct ieee80211_node *ni;
1153b5c99415SSam Leffler 	int newnode = 0;
1154b5c99415SSam Leffler 
1155b5c99415SSam Leffler 	ni = ieee80211_find_node(nt, wh->i_addr2);
1156b5c99415SSam Leffler 	if (ni == NULL) {
1157b5c99415SSam Leffler 		/*
1158b5c99415SSam Leffler 		 * Create a new entry.
1159b5c99415SSam Leffler 		 */
1160b5c99415SSam Leffler 		ni = ic->ic_node_alloc(nt);
1161b5c99415SSam Leffler 		if (ni == NULL) {
1162b5c99415SSam Leffler 			ic->ic_stats.is_rx_nodealloc++;
1163b5c99415SSam Leffler 			return;
1164b5c99415SSam Leffler 		}
1165b5c99415SSam Leffler 		ieee80211_setup_node(nt, ni, wh->i_addr2);
1166b5c99415SSam Leffler 		/*
1167b5c99415SSam Leffler 		 * XXX inherit from ic_bss.
1168b5c99415SSam Leffler 		 */
1169b5c99415SSam Leffler 		ni->ni_authmode = ic->ic_bss->ni_authmode;
1170b5c99415SSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
1171b5c99415SSam Leffler 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
1172b5c99415SSam Leffler 		ieee80211_set_chan(ic, ni, ic->ic_curchan);
1173b5c99415SSam Leffler 		ni->ni_rsn = ic->ic_bss->ni_rsn;
1174b5c99415SSam Leffler 		newnode = 1;
1175b5c99415SSam Leffler 	}
1176b5c99415SSam Leffler #ifdef IEEE80211_DEBUG
1177b5c99415SSam Leffler 	if (ieee80211_msg_scan(ic) && (ic->ic_flags & IEEE80211_F_SCAN))
1178b5c99415SSam Leffler 		dump_probe_beacon(subtype, newnode, wh->i_addr2, sp);
1179b5c99415SSam Leffler #endif
1180b5c99415SSam Leffler 	/* XXX ap beaconing multiple ssid w/ same bssid */
1181b5c99415SSam Leffler 	if (sp->ssid[1] != 0 &&
1182b5c99415SSam Leffler 	    (ISPROBE(subtype) || ni->ni_esslen == 0)) {
1183b5c99415SSam Leffler 		ni->ni_esslen = sp->ssid[1];
1184b5c99415SSam Leffler 		memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
1185b5c99415SSam Leffler 		memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1186b5c99415SSam Leffler 	}
1187b5c99415SSam Leffler 	ni->ni_scangen = ic->ic_scan.nt_scangen;
1188b5c99415SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1189b5c99415SSam Leffler 	ni->ni_rssi = rssi;
1190b5c99415SSam Leffler 	ni->ni_rstamp = rstamp;
1191b5c99415SSam Leffler 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1192b5c99415SSam Leffler 	ni->ni_intval = sp->bintval;
1193b5c99415SSam Leffler 	ni->ni_capinfo = sp->capinfo;
1194b5c99415SSam Leffler 	ni->ni_chan = &ic->ic_channels[sp->chan];
1195b5c99415SSam Leffler 	ni->ni_fhdwell = sp->fhdwell;
1196b5c99415SSam Leffler 	ni->ni_fhindex = sp->fhindex;
1197b5c99415SSam Leffler 	ni->ni_erp = sp->erp;
1198b5c99415SSam Leffler 	if (sp->tim != NULL) {
1199b5c99415SSam Leffler 		struct ieee80211_tim_ie *ie =
1200b5c99415SSam Leffler 		    (struct ieee80211_tim_ie *) sp->tim;
1201b5c99415SSam Leffler 
1202b5c99415SSam Leffler 		ni->ni_dtim_count = ie->tim_count;
1203b5c99415SSam Leffler 		ni->ni_dtim_period = ie->tim_period;
1204b5c99415SSam Leffler 	}
1205b5c99415SSam Leffler 	/*
1206b5c99415SSam Leffler 	 * Record the byte offset from the mac header to
1207b5c99415SSam Leffler 	 * the start of the TIM information element for
1208b5c99415SSam Leffler 	 * use by hardware and/or to speedup software
1209b5c99415SSam Leffler 	 * processing of beacon frames.
1210b5c99415SSam Leffler 	 */
1211b5c99415SSam Leffler 	ni->ni_timoff = sp->timoff;
1212b5c99415SSam Leffler 	/*
1213b5c99415SSam Leffler 	 * Record optional information elements that might be
1214b5c99415SSam Leffler 	 * used by applications or drivers.
1215b5c99415SSam Leffler 	 */
1216b5c99415SSam Leffler 	saveie(&ni->ni_wme_ie, sp->wme);
1217b5c99415SSam Leffler 	saveie(&ni->ni_wpa_ie, sp->wpa);
1218b5c99415SSam Leffler 
1219b5c99415SSam Leffler 	/* NB: must be after ni_chan is setup */
1220b5c99415SSam Leffler 	ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT);
1221b5c99415SSam Leffler 
1222b5c99415SSam Leffler 	if (!newnode)
1223b5c99415SSam Leffler 		ieee80211_free_node(ni);
1224b5c99415SSam Leffler #undef ISPROBE
1225b5c99415SSam Leffler }
1226b5c99415SSam Leffler 
1227be425a0fSSam Leffler void
1228be425a0fSSam Leffler ieee80211_init_neighbor(struct ieee80211_node *ni,
1229be425a0fSSam Leffler 	const struct ieee80211_frame *wh,
1230be425a0fSSam Leffler 	const struct ieee80211_scanparams *sp)
1231be425a0fSSam Leffler {
1232be425a0fSSam Leffler 
1233be425a0fSSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1234be425a0fSSam Leffler 	    "%s: %p<%s>\n", __func__, ni, ether_sprintf(ni->ni_macaddr));
1235be425a0fSSam Leffler 	ni->ni_esslen = sp->ssid[1];
1236be425a0fSSam Leffler 	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1237be425a0fSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1238be425a0fSSam Leffler 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1239be425a0fSSam Leffler 	ni->ni_intval = sp->bintval;
1240be425a0fSSam Leffler 	ni->ni_capinfo = sp->capinfo;
1241be425a0fSSam Leffler 	ni->ni_chan = ni->ni_ic->ic_curchan;
1242be425a0fSSam Leffler 	ni->ni_fhdwell = sp->fhdwell;
1243be425a0fSSam Leffler 	ni->ni_fhindex = sp->fhindex;
1244be425a0fSSam Leffler 	ni->ni_erp = sp->erp;
1245be425a0fSSam Leffler 	ni->ni_timoff = sp->timoff;
1246be425a0fSSam Leffler 	if (sp->wme != NULL)
1247be425a0fSSam Leffler 		ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
1248be425a0fSSam Leffler 	if (sp->wpa != NULL)
1249be425a0fSSam Leffler 		ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
1250be425a0fSSam Leffler 
1251be425a0fSSam Leffler 	/* NB: must be after ni_chan is setup */
1252be425a0fSSam Leffler 	ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT);
1253be425a0fSSam Leffler }
1254be425a0fSSam Leffler 
1255b5c99415SSam Leffler /*
1256b5c99415SSam Leffler  * Do node discovery in adhoc mode on receipt of a beacon
1257b5c99415SSam Leffler  * or probe response frame.  Note that for the driver's
1258b5c99415SSam Leffler  * benefit we we treat this like an association so the
1259b5c99415SSam Leffler  * driver has an opportunity to setup it's private state.
1260b5c99415SSam Leffler  */
1261b5c99415SSam Leffler struct ieee80211_node *
1262b5c99415SSam Leffler ieee80211_add_neighbor(struct ieee80211com *ic,
1263b5c99415SSam Leffler 	const struct ieee80211_frame *wh,
1264b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp)
1265b5c99415SSam Leffler {
1266b5c99415SSam Leffler 	struct ieee80211_node *ni;
1267b5c99415SSam Leffler 
1268be425a0fSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1269be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1270b5c99415SSam Leffler 	ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1271b5c99415SSam Leffler 	if (ni != NULL) {
1272be425a0fSSam Leffler 		ieee80211_init_neighbor(ni, wh, sp);
1273b5c99415SSam Leffler 		if (ic->ic_newassoc != NULL)
1274b5c99415SSam Leffler 			ic->ic_newassoc(ni, 1);
1275b5c99415SSam Leffler 		/* XXX not right for 802.1x/WPA */
1276b5c99415SSam Leffler 		ieee80211_node_authorize(ni);
1277b5c99415SSam Leffler 	}
1278b5c99415SSam Leffler 	return ni;
1279b5c99415SSam Leffler }
1280b5c99415SSam Leffler 
1281c1225b52SSam Leffler #define	IS_CTL(wh) \
1282c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1283c1225b52SSam Leffler #define	IS_PSPOLL(wh) \
1284c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
12858a1b9b6aSSam Leffler /*
12868a1b9b6aSSam Leffler  * Locate the node for sender, track state, and then pass the
12878a1b9b6aSSam Leffler  * (referenced) node up to the 802.11 layer for its use.  We
12888a1b9b6aSSam Leffler  * are required to pass some node so we fall back to ic_bss
12898a1b9b6aSSam Leffler  * when this frame is from an unknown sender.  The 802.11 layer
12908a1b9b6aSSam Leffler  * knows this means the sender wasn't in the node table and
12918a1b9b6aSSam Leffler  * acts accordingly.
12928a1b9b6aSSam Leffler  */
12938a1b9b6aSSam Leffler struct ieee80211_node *
12948a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12958a1b9b6aSSam Leffler ieee80211_find_rxnode_debug(struct ieee80211com *ic,
12968a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh, const char *func, int line)
12978a1b9b6aSSam Leffler #else
12988a1b9b6aSSam Leffler ieee80211_find_rxnode(struct ieee80211com *ic,
12998a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh)
13008a1b9b6aSSam Leffler #endif
13018a1b9b6aSSam Leffler {
13028a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
13038a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
13048a1b9b6aSSam Leffler 
13058a1b9b6aSSam Leffler 	/* XXX may want scanned nodes in the neighbor table for adhoc */
13068a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
13078a1b9b6aSSam Leffler 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
13088a1b9b6aSSam Leffler 	    (ic->ic_flags & IEEE80211_F_SCAN))
13098a1b9b6aSSam Leffler 		nt = &ic->ic_scan;
13108a1b9b6aSSam Leffler 	else
1311acc4f7f5SSam Leffler 		nt = &ic->ic_sta;
13128a1b9b6aSSam Leffler 	/* XXX check ic_bss first in station mode */
13138a1b9b6aSSam Leffler 	/* XXX 4-address frames? */
13148a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
13158a1b9b6aSSam Leffler 	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
13168a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr1);
13178a1b9b6aSSam Leffler 	else
13188a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr2);
1319c1225b52SSam Leffler 	if (ni == NULL)
1320c1225b52SSam Leffler 		ni = ieee80211_ref_node(ic->ic_bss);
13218a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
13228a1b9b6aSSam Leffler 
1323c1225b52SSam Leffler 	return ni;
1324c1225b52SSam Leffler }
1325c1225b52SSam Leffler 
1326c1225b52SSam Leffler /*
1327c1225b52SSam Leffler  * Like ieee80211_find_rxnode but use the supplied h/w
1328c1225b52SSam Leffler  * key index as a hint to locate the node in the key
1329c1225b52SSam Leffler  * mapping table.  If an entry is present at the key
1330c1225b52SSam Leffler  * index we return it; otherwise do a normal lookup and
1331c1225b52SSam Leffler  * update the mapping table if the station has a unicast
1332c1225b52SSam Leffler  * key assigned to it.
1333c1225b52SSam Leffler  */
1334c1225b52SSam Leffler struct ieee80211_node *
1335c1225b52SSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1336c1225b52SSam Leffler ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1337c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1338c1225b52SSam Leffler 	const char *func, int line)
1339c1225b52SSam Leffler #else
1340c1225b52SSam Leffler ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1341c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1342c1225b52SSam Leffler #endif
1343c1225b52SSam Leffler {
1344c1225b52SSam Leffler 	struct ieee80211_node_table *nt;
1345c1225b52SSam Leffler 	struct ieee80211_node *ni;
1346c1225b52SSam Leffler 
1347c1225b52SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
1348c1225b52SSam Leffler 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1349c1225b52SSam Leffler 	    (ic->ic_flags & IEEE80211_F_SCAN))
1350c1225b52SSam Leffler 		nt = &ic->ic_scan;
1351c1225b52SSam Leffler 	else
1352c1225b52SSam Leffler 		nt = &ic->ic_sta;
1353c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
1354c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1355c1225b52SSam Leffler 		ni = nt->nt_keyixmap[keyix];
1356c1225b52SSam Leffler 	else
1357c1225b52SSam Leffler 		ni = NULL;
1358c1225b52SSam Leffler 	if (ni == NULL) {
1359c1225b52SSam Leffler 		if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1360c1225b52SSam Leffler 			ni = _ieee80211_find_node(nt, wh->i_addr1);
1361c1225b52SSam Leffler 		else
1362c1225b52SSam Leffler 			ni = _ieee80211_find_node(nt, wh->i_addr2);
1363c1225b52SSam Leffler 		if (ni == NULL)
1364c1225b52SSam Leffler 			ni = ieee80211_ref_node(ic->ic_bss);
1365c1225b52SSam Leffler 		if (nt->nt_keyixmap != NULL) {
1366c1225b52SSam Leffler 			/*
1367c1225b52SSam Leffler 			 * If the station has a unicast key cache slot
1368c1225b52SSam Leffler 			 * assigned update the key->node mapping table.
1369c1225b52SSam Leffler 			 */
1370c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1371c1225b52SSam Leffler 			/* XXX can keyixmap[keyix] != NULL? */
1372c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1373c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == NULL) {
1374c1225b52SSam Leffler 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1375c1225b52SSam Leffler 				    "%s: add key map entry %p<%s> refcnt %d\n",
1376c1225b52SSam Leffler 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1377c1225b52SSam Leffler 				    ieee80211_node_refcnt(ni)+1);
1378c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1379c1225b52SSam Leffler 			}
1380c1225b52SSam Leffler 		}
1381c1225b52SSam Leffler 	} else {
1382c1225b52SSam Leffler 		ieee80211_ref_node(ni);
1383c1225b52SSam Leffler 	}
1384c1225b52SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1385c1225b52SSam Leffler 
1386c1225b52SSam Leffler 	return ni;
1387c1225b52SSam Leffler }
13888a1b9b6aSSam Leffler #undef IS_PSPOLL
13898a1b9b6aSSam Leffler #undef IS_CTL
13908a1b9b6aSSam Leffler 
13918a1b9b6aSSam Leffler /*
1392750d6d0cSSam Leffler  * Return a reference to the appropriate node for sending
1393750d6d0cSSam Leffler  * a data frame.  This handles node discovery in adhoc networks.
1394750d6d0cSSam Leffler  */
1395750d6d0cSSam Leffler struct ieee80211_node *
13968a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
13978a1b9b6aSSam Leffler ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
13988a1b9b6aSSam Leffler 	const char *func, int line)
13998a1b9b6aSSam Leffler #else
14008a1b9b6aSSam Leffler ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
14018a1b9b6aSSam Leffler #endif
1402750d6d0cSSam Leffler {
1403acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1404750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1405750d6d0cSSam Leffler 
1406750d6d0cSSam Leffler 	/*
1407750d6d0cSSam Leffler 	 * The destination address should be in the node table
1408c1225b52SSam Leffler 	 * unless this is a multicast/broadcast frame.  We can
1409c1225b52SSam Leffler 	 * also optimize station mode operation, all frames go
1410c1225b52SSam Leffler 	 * to the bss node.
1411750d6d0cSSam Leffler 	 */
1412750d6d0cSSam Leffler 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
14138a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1414c1225b52SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1415c1225b52SSam Leffler 		ni = ieee80211_ref_node(ic->ic_bss);
1416c1225b52SSam Leffler 	else
14178a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, macaddr);
14188a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
14198a1b9b6aSSam Leffler 
14208a1b9b6aSSam Leffler 	if (ni == NULL) {
14218a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
142290d0d036SSam Leffler 		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
142390d0d036SSam Leffler 			/*
142490d0d036SSam Leffler 			 * In adhoc mode cons up a node for the destination.
142590d0d036SSam Leffler 			 * Note that we need an additional reference for the
142690d0d036SSam Leffler 			 * caller to be consistent with _ieee80211_find_node.
142790d0d036SSam Leffler 			 */
14288a1b9b6aSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
142990d0d036SSam Leffler 			if (ni != NULL)
143090d0d036SSam Leffler 				(void) ieee80211_ref_node(ni);
143190d0d036SSam Leffler 		} else {
14328a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
14338a1b9b6aSSam Leffler 				"[%s] no node, discard frame (%s)\n",
14348a1b9b6aSSam Leffler 				ether_sprintf(macaddr), __func__);
14358a1b9b6aSSam Leffler 			ic->ic_stats.is_tx_nonode++;
1436750d6d0cSSam Leffler 		}
1437750d6d0cSSam Leffler 	}
1438750d6d0cSSam Leffler 	return ni;
1439750d6d0cSSam Leffler }
1440750d6d0cSSam Leffler 
1441750d6d0cSSam Leffler /*
14421a1e1d21SSam Leffler  * Like find but search based on the channel too.
14431a1e1d21SSam Leffler  */
14441a1e1d21SSam Leffler struct ieee80211_node *
14458a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
14468a1b9b6aSSam Leffler ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
14478a1b9b6aSSam Leffler 	const u_int8_t *macaddr, struct ieee80211_channel *chan,
14488a1b9b6aSSam Leffler 	const char *func, int line)
14498a1b9b6aSSam Leffler #else
14508a1b9b6aSSam Leffler ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
14518a1b9b6aSSam Leffler 	const u_int8_t *macaddr, struct ieee80211_channel *chan)
14528a1b9b6aSSam Leffler #endif
14531a1e1d21SSam Leffler {
14541a1e1d21SSam Leffler 	struct ieee80211_node *ni;
14551a1e1d21SSam Leffler 	int hash;
14561a1e1d21SSam Leffler 
14571a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
14588a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
14598a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1460750d6d0cSSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1461750d6d0cSSam Leffler 		    ni->ni_chan == chan) {
14628a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);		/* mark referenced */
14638a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
14648a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
146549a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
146649a15236SSam Leffler 			    func, line,
14678a1b9b6aSSam Leffler #else
146849a15236SSam Leffler 			    "%s %p<%s> refcnt %d\n", __func__,
14698a1b9b6aSSam Leffler #endif
147049a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
14718a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
14721a1e1d21SSam Leffler 			break;
14731a1e1d21SSam Leffler 		}
14741a1e1d21SSam Leffler 	}
14758a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
14761a1e1d21SSam Leffler 	return ni;
14771a1e1d21SSam Leffler }
14781a1e1d21SSam Leffler 
14798a1b9b6aSSam Leffler /*
14808a1b9b6aSSam Leffler  * Like find but search based on the ssid too.
14818a1b9b6aSSam Leffler  */
14828a1b9b6aSSam Leffler struct ieee80211_node *
14838a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
14848a1b9b6aSSam Leffler ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
14858a1b9b6aSSam Leffler 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
14868a1b9b6aSSam Leffler 	const char *func, int line)
14878a1b9b6aSSam Leffler #else
14888a1b9b6aSSam Leffler ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
14898a1b9b6aSSam Leffler 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
14908a1b9b6aSSam Leffler #endif
14911a1e1d21SSam Leffler {
1492f02a0bd2SSam Leffler #define	MATCH_SSID(ni, ssid, ssidlen) \
1493f02a0bd2SSam Leffler 	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1494f02a0bd2SSam Leffler 	static const u_int8_t zeromac[IEEE80211_ADDR_LEN];
14958a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
14961a1e1d21SSam Leffler 	struct ieee80211_node *ni;
14978a1b9b6aSSam Leffler 	int hash;
14981a1e1d21SSam Leffler 
14998a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1500f02a0bd2SSam Leffler 	/*
1501f02a0bd2SSam Leffler 	 * A mac address that is all zero means match only the ssid;
1502f02a0bd2SSam Leffler 	 * otherwise we must match both.
1503f02a0bd2SSam Leffler 	 */
1504f02a0bd2SSam Leffler 	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1505f02a0bd2SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1506f02a0bd2SSam Leffler 			if (MATCH_SSID(ni, ssid, ssidlen))
1507f02a0bd2SSam Leffler 				break;
1508f02a0bd2SSam Leffler 		}
1509f02a0bd2SSam Leffler 	} else {
1510f02a0bd2SSam Leffler 		hash = IEEE80211_NODE_HASH(macaddr);
15118a1b9b6aSSam Leffler 		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
15128a1b9b6aSSam Leffler 			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1513f02a0bd2SSam Leffler 			    MATCH_SSID(ni, ssid, ssidlen))
1514f02a0bd2SSam Leffler 				break;
1515f02a0bd2SSam Leffler 		}
1516f02a0bd2SSam Leffler 	}
1517f02a0bd2SSam Leffler 	if (ni != NULL) {
15188a1b9b6aSSam Leffler 		ieee80211_ref_node(ni);	/* mark referenced */
15198a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
15208a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
152149a15236SSam Leffler 		    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
152249a15236SSam Leffler 		    func, line,
15238a1b9b6aSSam Leffler #else
152449a15236SSam Leffler 		    "%s %p<%s> refcnt %d\n", __func__,
15258a1b9b6aSSam Leffler #endif
152649a15236SSam Leffler 		     ni, ether_sprintf(ni->ni_macaddr),
15278a1b9b6aSSam Leffler 		     ieee80211_node_refcnt(ni));
15288a1b9b6aSSam Leffler 	}
15298a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15308a1b9b6aSSam Leffler 	return ni;
1531f02a0bd2SSam Leffler #undef MATCH_SSID
15328a1b9b6aSSam Leffler }
15338a1b9b6aSSam Leffler 
15348a1b9b6aSSam Leffler static void
15358a1b9b6aSSam Leffler _ieee80211_free_node(struct ieee80211_node *ni)
15368a1b9b6aSSam Leffler {
15378a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
15388a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
15398a1b9b6aSSam Leffler 
15408a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
154149a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
154249a15236SSam Leffler 		ether_sprintf(ni->ni_macaddr),
15438a1b9b6aSSam Leffler 		nt != NULL ? nt->nt_name : "<gone>");
15448a1b9b6aSSam Leffler 
15458a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
15468a1b9b6aSSam Leffler 	if (nt != NULL) {
15478a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
15488a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
15498a1b9b6aSSam Leffler 	}
15508a1b9b6aSSam Leffler 	ic->ic_node_free(ni);
15518a1b9b6aSSam Leffler }
15528a1b9b6aSSam Leffler 
15538a1b9b6aSSam Leffler void
15548a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
15558a1b9b6aSSam Leffler ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
15568a1b9b6aSSam Leffler #else
15578a1b9b6aSSam Leffler ieee80211_free_node(struct ieee80211_node *ni)
15588a1b9b6aSSam Leffler #endif
15598a1b9b6aSSam Leffler {
15608a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
15618a1b9b6aSSam Leffler 
15628a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
156329d368a7SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
156449a15236SSam Leffler 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
15658a1b9b6aSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
15668a1b9b6aSSam Leffler #endif
1567c1225b52SSam Leffler 	if (nt != NULL) {
1568c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
15698a1b9b6aSSam Leffler 		if (ieee80211_node_dectestref(ni)) {
15708a1b9b6aSSam Leffler 			/*
1571c1225b52SSam Leffler 			 * Last reference, reclaim state.
15728a1b9b6aSSam Leffler 			 */
15738a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
1574c1225b52SSam Leffler 		} else if (ieee80211_node_refcnt(ni) == 1 &&
1575c1225b52SSam Leffler 		    nt->nt_keyixmap != NULL) {
1576c1225b52SSam Leffler 			ieee80211_keyix keyix;
1577c1225b52SSam Leffler 			/*
1578c1225b52SSam Leffler 			 * Check for a last reference in the key mapping table.
1579c1225b52SSam Leffler 			 */
1580c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1581c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1582c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == ni) {
1583c1225b52SSam Leffler 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1584c1225b52SSam Leffler 				    "%s: %p<%s> clear key map entry", __func__,
1585c1225b52SSam Leffler 				    ni, ether_sprintf(ni->ni_macaddr));
1586c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = NULL;
1587c1225b52SSam Leffler 				ieee80211_node_decref(ni); /* XXX needed? */
15888a1b9b6aSSam Leffler 				_ieee80211_free_node(ni);
15898a1b9b6aSSam Leffler 			}
15901a1e1d21SSam Leffler 		}
1591c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1592c1225b52SSam Leffler 	} else {
1593c1225b52SSam Leffler 		if (ieee80211_node_dectestref(ni))
1594c1225b52SSam Leffler 			_ieee80211_free_node(ni);
1595c1225b52SSam Leffler 	}
1596c1225b52SSam Leffler }
1597c1225b52SSam Leffler 
1598c1225b52SSam Leffler /*
1599c1225b52SSam Leffler  * Reclaim a unicast key and clear any key cache state.
1600c1225b52SSam Leffler  */
1601c1225b52SSam Leffler int
1602c1225b52SSam Leffler ieee80211_node_delucastkey(struct ieee80211_node *ni)
1603c1225b52SSam Leffler {
1604c1225b52SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1605c1225b52SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1606c1225b52SSam Leffler 	struct ieee80211_node *nikey;
1607c1225b52SSam Leffler 	ieee80211_keyix keyix;
1608c1225b52SSam Leffler 	int isowned, status;
1609c1225b52SSam Leffler 
1610c1225b52SSam Leffler 	/*
1611c1225b52SSam Leffler 	 * NB: We must beware of LOR here; deleting the key
1612c1225b52SSam Leffler 	 * can cause the crypto layer to block traffic updates
1613c1225b52SSam Leffler 	 * which can generate a LOR against the node table lock;
1614c1225b52SSam Leffler 	 * grab it here and stash the key index for our use below.
1615c1225b52SSam Leffler 	 *
1616c1225b52SSam Leffler 	 * Must also beware of recursion on the node table lock.
1617c1225b52SSam Leffler 	 * When called from node_cleanup we may already have
1618c1225b52SSam Leffler 	 * the node table lock held.  Unfortunately there's no
1619c1225b52SSam Leffler 	 * way to separate out this path so we must do this
1620c1225b52SSam Leffler 	 * conditionally.
1621c1225b52SSam Leffler 	 */
1622c1225b52SSam Leffler 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1623c1225b52SSam Leffler 	if (!isowned)
1624c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
1625c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1626c1225b52SSam Leffler 	status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1627c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1628c1225b52SSam Leffler 		nikey = nt->nt_keyixmap[keyix];
1629c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;;
1630c1225b52SSam Leffler 	} else
1631c1225b52SSam Leffler 		nikey = NULL;
1632c1225b52SSam Leffler 	if (!isowned)
1633c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(&ic->ic_sta);
1634c1225b52SSam Leffler 
1635c1225b52SSam Leffler 	if (nikey != NULL) {
1636c1225b52SSam Leffler 		KASSERT(nikey == ni,
1637c1225b52SSam Leffler 			("key map out of sync, ni %p nikey %p", ni, nikey));
1638c1225b52SSam Leffler 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1639c1225b52SSam Leffler 			"%s: delete key map entry %p<%s> refcnt %d\n",
1640c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr),
1641c1225b52SSam Leffler 			ieee80211_node_refcnt(ni)-1);
1642c1225b52SSam Leffler 		ieee80211_free_node(ni);
1643c1225b52SSam Leffler 	}
1644c1225b52SSam Leffler 	return status;
1645c1225b52SSam Leffler }
16461a1e1d21SSam Leffler 
1647303ebc3cSSam Leffler /*
16488a1b9b6aSSam Leffler  * Reclaim a node.  If this is the last reference count then
16498a1b9b6aSSam Leffler  * do the normal free work.  Otherwise remove it from the node
16508a1b9b6aSSam Leffler  * table and mark it gone by clearing the back-reference.
16518a1b9b6aSSam Leffler  */
16528a1b9b6aSSam Leffler static void
16538a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
16548a1b9b6aSSam Leffler {
1655c1225b52SSam Leffler 	ieee80211_keyix keyix;
1656c1225b52SSam Leffler 
1657c1225b52SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
16588a1b9b6aSSam Leffler 
165949a15236SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
166049a15236SSam Leffler 		"%s: remove %p<%s> from %s table, refcnt %d\n",
166149a15236SSam Leffler 		__func__, ni, ether_sprintf(ni->ni_macaddr),
166249a15236SSam Leffler 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1663c1225b52SSam Leffler 	/*
1664c1225b52SSam Leffler 	 * Clear any entry in the unicast key mapping table.
1665c1225b52SSam Leffler 	 * We need to do it here so rx lookups don't find it
1666c1225b52SSam Leffler 	 * in the mapping table even if it's not in the hash
1667c1225b52SSam Leffler 	 * table.  We cannot depend on the mapping table entry
1668c1225b52SSam Leffler 	 * being cleared because the node may not be free'd.
1669c1225b52SSam Leffler 	 */
1670c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1671c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1672c1225b52SSam Leffler 	    nt->nt_keyixmap[keyix] == ni) {
1673c1225b52SSam Leffler 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1674c1225b52SSam Leffler 			"%s: %p<%s> clear key map entry\n",
1675c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr));
1676c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;
1677c1225b52SSam Leffler 		ieee80211_node_decref(ni);	/* NB: don't need free */
1678c1225b52SSam Leffler 	}
16798a1b9b6aSSam Leffler 	if (!ieee80211_node_dectestref(ni)) {
16808a1b9b6aSSam Leffler 		/*
16818a1b9b6aSSam Leffler 		 * Other references are present, just remove the
16828a1b9b6aSSam Leffler 		 * node from the table so it cannot be found.  When
16838a1b9b6aSSam Leffler 		 * the references are dropped storage will be
1684acc4f7f5SSam Leffler 		 * reclaimed.
16858a1b9b6aSSam Leffler 		 */
16868a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
16878a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
16888a1b9b6aSSam Leffler 		ni->ni_table = NULL;		/* clear reference */
16898a1b9b6aSSam Leffler 	} else
16908a1b9b6aSSam Leffler 		_ieee80211_free_node(ni);
16918a1b9b6aSSam Leffler }
16928a1b9b6aSSam Leffler 
16938a1b9b6aSSam Leffler static void
16948a1b9b6aSSam Leffler ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
16958a1b9b6aSSam Leffler {
16968a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
16978a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
16988a1b9b6aSSam Leffler 
16998a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
17008a1b9b6aSSam Leffler 		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
17018a1b9b6aSSam Leffler 
17028a1b9b6aSSam Leffler 	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
17038a1b9b6aSSam Leffler 		if (ni->ni_associd != 0) {
17048a1b9b6aSSam Leffler 			if (ic->ic_auth->ia_node_leave != NULL)
17058a1b9b6aSSam Leffler 				ic->ic_auth->ia_node_leave(ic, ni);
17068a1b9b6aSSam Leffler 			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
17078a1b9b6aSSam Leffler 		}
17088a1b9b6aSSam Leffler 		node_reclaim(nt, ni);
17098a1b9b6aSSam Leffler 	}
17108a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
17118a1b9b6aSSam Leffler }
17128a1b9b6aSSam Leffler 
17138a1b9b6aSSam Leffler static void
17148a1b9b6aSSam Leffler ieee80211_free_allnodes(struct ieee80211_node_table *nt)
17158a1b9b6aSSam Leffler {
17168a1b9b6aSSam Leffler 
17178a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
17188a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
17198a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
17208a1b9b6aSSam Leffler }
17218a1b9b6aSSam Leffler 
17228a1b9b6aSSam Leffler /*
17238a1b9b6aSSam Leffler  * Timeout entries in the scan cache.
17248a1b9b6aSSam Leffler  */
17258a1b9b6aSSam Leffler static void
17268a1b9b6aSSam Leffler ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
17278a1b9b6aSSam Leffler {
17288a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
17298a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *tni;
17308a1b9b6aSSam Leffler 
17318a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
17328a1b9b6aSSam Leffler 	ni = ic->ic_bss;
17338a1b9b6aSSam Leffler 	/* XXX belongs elsewhere */
17348a1b9b6aSSam Leffler 	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
17358a1b9b6aSSam Leffler 		m_freem(ni->ni_rxfrag[0]);
17368a1b9b6aSSam Leffler 		ni->ni_rxfrag[0] = NULL;
17378a1b9b6aSSam Leffler 	}
17388a1b9b6aSSam Leffler 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
17398a1b9b6aSSam Leffler 		if (ni->ni_inact && --ni->ni_inact == 0) {
17408a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
17418a1b9b6aSSam Leffler 			    "[%s] scan candidate purged from cache "
17428a1b9b6aSSam Leffler 			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
174349a15236SSam Leffler 			    ieee80211_node_refcnt(ni));
17448a1b9b6aSSam Leffler 			node_reclaim(nt, ni);
17458a1b9b6aSSam Leffler 		}
17468a1b9b6aSSam Leffler 	}
17478a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
17488a1b9b6aSSam Leffler 
17498a1b9b6aSSam Leffler 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
17508a1b9b6aSSam Leffler }
17518a1b9b6aSSam Leffler 
17528a1b9b6aSSam Leffler /*
17538a1b9b6aSSam Leffler  * Timeout inactive stations and do related housekeeping.
17548a1b9b6aSSam Leffler  * Note that we cannot hold the node lock while sending a
17558a1b9b6aSSam Leffler  * frame as this would lead to a LOR.  Instead we use a
17568a1b9b6aSSam Leffler  * generation number to mark nodes that we've scanned and
17578a1b9b6aSSam Leffler  * drop the lock and restart a scan if we have to time out
17588a1b9b6aSSam Leffler  * a node.  Since we are single-threaded by virtue of
1759303ebc3cSSam Leffler  * controlling the inactivity timer we can be sure this will
1760303ebc3cSSam Leffler  * process each node only once.
1761303ebc3cSSam Leffler  */
17628a1b9b6aSSam Leffler static void
17638a1b9b6aSSam Leffler ieee80211_timeout_stations(struct ieee80211_node_table *nt)
17641a1e1d21SSam Leffler {
17658a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
1766303ebc3cSSam Leffler 	struct ieee80211_node *ni;
17678a1b9b6aSSam Leffler 	u_int gen;
1768f66d97f6SSam Leffler 	int isadhoc;
17691a1e1d21SSam Leffler 
1770f66d97f6SSam Leffler 	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1771f66d97f6SSam Leffler 		   ic->ic_opmode == IEEE80211_M_AHDEMO);
17728a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
17738a1b9b6aSSam Leffler 	gen = nt->nt_scangen++;
17748a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
177549a15236SSam Leffler 		"%s: %s scangen %u\n", __func__, nt->nt_name, gen);
1776303ebc3cSSam Leffler restart:
17778a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
17788a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1779303ebc3cSSam Leffler 		if (ni->ni_scangen == gen)	/* previously handled */
1780303ebc3cSSam Leffler 			continue;
1781303ebc3cSSam Leffler 		ni->ni_scangen = gen;
17820a915fadSSam Leffler 		/*
1783ebdda46cSSam Leffler 		 * Ignore entries for which have yet to receive an
1784ebdda46cSSam Leffler 		 * authentication frame.  These are transient and
1785ebdda46cSSam Leffler 		 * will be reclaimed when the last reference to them
1786ebdda46cSSam Leffler 		 * goes away (when frame xmits complete).
1787ebdda46cSSam Leffler 		 */
178844b666cdSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
178944b666cdSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1790ebdda46cSSam Leffler 			continue;
1791ebdda46cSSam Leffler 		/*
17928a1b9b6aSSam Leffler 		 * Free fragment if not needed anymore
17938a1b9b6aSSam Leffler 		 * (last fragment older than 1s).
17948a1b9b6aSSam Leffler 		 * XXX doesn't belong here
17950a915fadSSam Leffler 		 */
17968a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[0] != NULL &&
17978a1b9b6aSSam Leffler 		    ticks > ni->ni_rxfragstamp + hz) {
17988a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
17998a1b9b6aSSam Leffler 			ni->ni_rxfrag[0] = NULL;
18008a1b9b6aSSam Leffler 		}
1801ce647032SSam Leffler 		/*
1802ce647032SSam Leffler 		 * Special case ourself; we may be idle for extended periods
1803ce647032SSam Leffler 		 * of time and regardless reclaiming our state is wrong.
1804ce647032SSam Leffler 		 */
1805ce647032SSam Leffler 		if (ni == ic->ic_bss)
1806ce647032SSam Leffler 			continue;
18078a1b9b6aSSam Leffler 		ni->ni_inact--;
1808f66d97f6SSam Leffler 		if (ni->ni_associd != 0 || isadhoc) {
18098a1b9b6aSSam Leffler 			/*
18108a1b9b6aSSam Leffler 			 * Age frames on the power save queue. The
18118a1b9b6aSSam Leffler 			 * aging interval is 4 times the listen
18128a1b9b6aSSam Leffler 			 * interval specified by the station.  This
18138a1b9b6aSSam Leffler 			 * number is factored into the age calculations
18148a1b9b6aSSam Leffler 			 * when the frame is placed on the queue.  We
18158a1b9b6aSSam Leffler 			 * store ages as time differences we can check
18168a1b9b6aSSam Leffler 			 * and/or adjust only the head of the list.
18178a1b9b6aSSam Leffler 			 */
18188a1b9b6aSSam Leffler 			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
18198a1b9b6aSSam Leffler 				struct mbuf *m;
18208a1b9b6aSSam Leffler 				int discard = 0;
18218a1b9b6aSSam Leffler 
18228a1b9b6aSSam Leffler 				IEEE80211_NODE_SAVEQ_LOCK(ni);
18238a1b9b6aSSam Leffler 				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
18248a1b9b6aSSam Leffler 				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
18258a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
18268a1b9b6aSSam Leffler 					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
18278a1b9b6aSSam Leffler 					m_freem(m);
18288a1b9b6aSSam Leffler 					discard++;
18298a1b9b6aSSam Leffler 				}
18308a1b9b6aSSam Leffler 				if (m != NULL)
18318a1b9b6aSSam Leffler 					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
18328a1b9b6aSSam Leffler 				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
18338a1b9b6aSSam Leffler 
18348a1b9b6aSSam Leffler 				if (discard != 0) {
18358a1b9b6aSSam Leffler 					IEEE80211_DPRINTF(ic,
18368a1b9b6aSSam Leffler 					    IEEE80211_MSG_POWER,
18378a1b9b6aSSam Leffler 					    "[%s] discard %u frames for age\n",
18388a1b9b6aSSam Leffler 					    ether_sprintf(ni->ni_macaddr),
18398a1b9b6aSSam Leffler 					    discard);
18408a1b9b6aSSam Leffler 					IEEE80211_NODE_STAT_ADD(ni,
18418a1b9b6aSSam Leffler 						ps_discard, discard);
18428a1b9b6aSSam Leffler 					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1843edfa57d0SSam Leffler 						ic->ic_set_tim(ni, 0);
18448a1b9b6aSSam Leffler 				}
18458a1b9b6aSSam Leffler 			}
18468a1b9b6aSSam Leffler 			/*
18478a1b9b6aSSam Leffler 			 * Probe the station before time it out.  We
18488a1b9b6aSSam Leffler 			 * send a null data frame which may not be
18498a1b9b6aSSam Leffler 			 * universally supported by drivers (need it
18508a1b9b6aSSam Leffler 			 * for ps-poll support so it should be...).
18518a1b9b6aSSam Leffler 			 */
18522045f699SSam Leffler 			if (0 < ni->ni_inact &&
18532045f699SSam Leffler 			    ni->ni_inact <= ic->ic_inact_probe) {
1854f66d97f6SSam Leffler 				IEEE80211_NOTE(ic,
1855f66d97f6SSam Leffler 				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1856f66d97f6SSam Leffler 				    ni, "%s",
1857f66d97f6SSam Leffler 				    "probe station due to inactivity");
185819ad2dd7SSam Leffler 				/*
185919ad2dd7SSam Leffler 				 * Grab a reference before unlocking the table
186019ad2dd7SSam Leffler 				 * so the node cannot be reclaimed before we
186119ad2dd7SSam Leffler 				 * send the frame. ieee80211_send_nulldata
186219ad2dd7SSam Leffler 				 * understands we've done this and reclaims the
186319ad2dd7SSam Leffler 				 * ref for us as needed.
186419ad2dd7SSam Leffler 				 */
186519ad2dd7SSam Leffler 				ieee80211_ref_node(ni);
18668a1b9b6aSSam Leffler 				IEEE80211_NODE_UNLOCK(nt);
1867f62121ceSSam Leffler 				ieee80211_send_nulldata(ni);
18688a1b9b6aSSam Leffler 				/* XXX stat? */
18698a1b9b6aSSam Leffler 				goto restart;
18708a1b9b6aSSam Leffler 			}
18718a1b9b6aSSam Leffler 		}
18728a1b9b6aSSam Leffler 		if (ni->ni_inact <= 0) {
1873f66d97f6SSam Leffler 			IEEE80211_NOTE(ic,
1874f66d97f6SSam Leffler 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1875f66d97f6SSam Leffler 			    "station timed out due to inactivity "
1876f66d97f6SSam Leffler 			    "(refcnt %u)", ieee80211_node_refcnt(ni));
18778a1b9b6aSSam Leffler 			/*
18788a1b9b6aSSam Leffler 			 * Send a deauthenticate frame and drop the station.
18798a1b9b6aSSam Leffler 			 * This is somewhat complicated due to reference counts
18808a1b9b6aSSam Leffler 			 * and locking.  At this point a station will typically
18818a1b9b6aSSam Leffler 			 * have a reference count of 1.  ieee80211_node_leave
18828a1b9b6aSSam Leffler 			 * will do a "free" of the node which will drop the
18838a1b9b6aSSam Leffler 			 * reference count.  But in the meantime a reference
18848a1b9b6aSSam Leffler 			 * wil be held by the deauth frame.  The actual reclaim
18858a1b9b6aSSam Leffler 			 * of the node will happen either after the tx is
18868a1b9b6aSSam Leffler 			 * completed or by ieee80211_node_leave.
18878a1b9b6aSSam Leffler 			 *
18888a1b9b6aSSam Leffler 			 * Separately we must drop the node lock before sending
18898a1b9b6aSSam Leffler 			 * in case the driver takes a lock, as this will result
18908a1b9b6aSSam Leffler 			 * in  LOR between the node lock and the driver lock.
18918a1b9b6aSSam Leffler 			 */
18928a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
18938a1b9b6aSSam Leffler 			if (ni->ni_associd != 0) {
18941a1e1d21SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
18951a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
18961a1e1d21SSam Leffler 				    IEEE80211_REASON_AUTH_EXPIRE);
18978a1b9b6aSSam Leffler 			}
18988a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
18991be50176SSam Leffler 			ic->ic_stats.is_node_timeout++;
1900303ebc3cSSam Leffler 			goto restart;
1901303ebc3cSSam Leffler 		}
19021a1e1d21SSam Leffler 	}
19038a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
19048a1b9b6aSSam Leffler 
19058a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
19068a1b9b6aSSam Leffler 
19078a1b9b6aSSam Leffler 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
19081a1e1d21SSam Leffler }
19091a1e1d21SSam Leffler 
19101a1e1d21SSam Leffler void
19118a1b9b6aSSam Leffler ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
19121a1e1d21SSam Leffler {
19131a1e1d21SSam Leffler 	struct ieee80211_node *ni;
19148a1b9b6aSSam Leffler 	u_int gen;
19151a1e1d21SSam Leffler 
19168a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
19178a1b9b6aSSam Leffler 	gen = nt->nt_scangen++;
19188a1b9b6aSSam Leffler restart:
19198a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
19208a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
19218a1b9b6aSSam Leffler 		if (ni->ni_scangen != gen) {
19228a1b9b6aSSam Leffler 			ni->ni_scangen = gen;
19238a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
19248a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
19251a1e1d21SSam Leffler 			(*f)(arg, ni);
19268a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
19278a1b9b6aSSam Leffler 			goto restart;
19288a1b9b6aSSam Leffler 		}
19298a1b9b6aSSam Leffler 	}
19308a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
19318a1b9b6aSSam Leffler 
19328a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
19338a1b9b6aSSam Leffler }
19348a1b9b6aSSam Leffler 
19358a1b9b6aSSam Leffler void
19368a1b9b6aSSam Leffler ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
19378a1b9b6aSSam Leffler {
19388a1b9b6aSSam Leffler 	printf("0x%p: mac %s refcnt %d\n", ni,
19398a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
19408a1b9b6aSSam Leffler 	printf("\tscangen %u authmode %u flags 0x%x\n",
19418a1b9b6aSSam Leffler 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
19428a1b9b6aSSam Leffler 	printf("\tassocid 0x%x txpower %u vlan %u\n",
19438a1b9b6aSSam Leffler 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
19448a1b9b6aSSam Leffler 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
19458a1b9b6aSSam Leffler 		ni->ni_txseqs[0],
19468a1b9b6aSSam Leffler 		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
19478a1b9b6aSSam Leffler 		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
19488a1b9b6aSSam Leffler 		ni->ni_rxfragstamp);
19498a1b9b6aSSam Leffler 	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
19508a1b9b6aSSam Leffler 		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
19518a1b9b6aSSam Leffler 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
19528a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
19538a1b9b6aSSam Leffler 		ni->ni_esslen, ni->ni_essid,
19548a1b9b6aSSam Leffler 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
19558a1b9b6aSSam Leffler 	printf("\tfails %u inact %u txrate %u\n",
19568a1b9b6aSSam Leffler 		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
19578a1b9b6aSSam Leffler }
19588a1b9b6aSSam Leffler 
19598a1b9b6aSSam Leffler void
19608a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt)
19618a1b9b6aSSam Leffler {
19628a1b9b6aSSam Leffler 	ieee80211_iterate_nodes(nt,
19638a1b9b6aSSam Leffler 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
19648a1b9b6aSSam Leffler }
19658a1b9b6aSSam Leffler 
19668a1b9b6aSSam Leffler /*
19678a1b9b6aSSam Leffler  * Handle a station joining an 11g network.
19688a1b9b6aSSam Leffler  */
19698a1b9b6aSSam Leffler static void
19708a1b9b6aSSam Leffler ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
19718a1b9b6aSSam Leffler {
19728a1b9b6aSSam Leffler 
19738a1b9b6aSSam Leffler 	/*
19748a1b9b6aSSam Leffler 	 * Station isn't capable of short slot time.  Bump
19758a1b9b6aSSam Leffler 	 * the count of long slot time stations and disable
19768a1b9b6aSSam Leffler 	 * use of short slot time.  Note that the actual switch
19778a1b9b6aSSam Leffler 	 * over to long slot time use may not occur until the
19788a1b9b6aSSam Leffler 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
19798a1b9b6aSSam Leffler 	 */
19808a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
19818a1b9b6aSSam Leffler 		ic->ic_longslotsta++;
19828a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
19838a1b9b6aSSam Leffler 		    "[%s] station needs long slot time, count %d\n",
19848a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
19858a1b9b6aSSam Leffler 		/* XXX vap's w/ conflicting needs won't work */
19868a1b9b6aSSam Leffler 		ieee80211_set_shortslottime(ic, 0);
19878a1b9b6aSSam Leffler 	}
19888a1b9b6aSSam Leffler 	/*
19898a1b9b6aSSam Leffler 	 * If the new station is not an ERP station
19908a1b9b6aSSam Leffler 	 * then bump the counter and enable protection
19918a1b9b6aSSam Leffler 	 * if configured.
19928a1b9b6aSSam Leffler 	 */
19938a1b9b6aSSam Leffler 	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
19948a1b9b6aSSam Leffler 		ic->ic_nonerpsta++;
19958a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
19968a1b9b6aSSam Leffler 		    "[%s] station is !ERP, %d non-ERP stations associated\n",
19978a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
19988a1b9b6aSSam Leffler 		/*
19998a1b9b6aSSam Leffler 		 * If protection is configured, enable it.
20008a1b9b6aSSam Leffler 		 */
20018a1b9b6aSSam Leffler 		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
20028a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
20038a1b9b6aSSam Leffler 			    "%s: enable use of protection\n", __func__);
20048a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
20058a1b9b6aSSam Leffler 		}
20068a1b9b6aSSam Leffler 		/*
20078a1b9b6aSSam Leffler 		 * If station does not support short preamble
20088a1b9b6aSSam Leffler 		 * then we must enable use of Barker preamble.
20098a1b9b6aSSam Leffler 		 */
20108a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
20118a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
20128a1b9b6aSSam Leffler 			    "[%s] station needs long preamble\n",
20138a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr));
20148a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
20158a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
20168a1b9b6aSSam Leffler 		}
20178a1b9b6aSSam Leffler 	} else
20188a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
20198a1b9b6aSSam Leffler }
20208a1b9b6aSSam Leffler 
20218a1b9b6aSSam Leffler void
20228a1b9b6aSSam Leffler ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
20238a1b9b6aSSam Leffler {
20248a1b9b6aSSam Leffler 	int newassoc;
20258a1b9b6aSSam Leffler 
20268a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
20278a1b9b6aSSam Leffler 		u_int16_t aid;
20288a1b9b6aSSam Leffler 
20298a1b9b6aSSam Leffler 		/*
20308a1b9b6aSSam Leffler 		 * It would be good to search the bitmap
20318a1b9b6aSSam Leffler 		 * more efficiently, but this will do for now.
20328a1b9b6aSSam Leffler 		 */
20338a1b9b6aSSam Leffler 		for (aid = 1; aid < ic->ic_max_aid; aid++) {
20348a1b9b6aSSam Leffler 			if (!IEEE80211_AID_ISSET(aid,
20358a1b9b6aSSam Leffler 			    ic->ic_aid_bitmap))
20368a1b9b6aSSam Leffler 				break;
20378a1b9b6aSSam Leffler 		}
20388a1b9b6aSSam Leffler 		if (aid >= ic->ic_max_aid) {
20398a1b9b6aSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
20408a1b9b6aSSam Leffler 			    IEEE80211_REASON_ASSOC_TOOMANY);
20418a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
20428a1b9b6aSSam Leffler 			return;
20438a1b9b6aSSam Leffler 		}
20448a1b9b6aSSam Leffler 		ni->ni_associd = aid | 0xc000;
20458a1b9b6aSSam Leffler 		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
20468a1b9b6aSSam Leffler 		ic->ic_sta_assoc++;
20478a1b9b6aSSam Leffler 		newassoc = 1;
20485ff80921SSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G)
20498a1b9b6aSSam Leffler 			ieee80211_node_join_11g(ic, ni);
20508a1b9b6aSSam Leffler 	} else
20518a1b9b6aSSam Leffler 		newassoc = 0;
20528a1b9b6aSSam Leffler 
20538a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
2054b8fcf546SSam Leffler 	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
2055b8fcf546SSam Leffler 	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
2056b8fcf546SSam Leffler 	    IEEE80211_NODE_AID(ni),
2057b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2058b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2059b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2060b8fcf546SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
2061b8fcf546SSam Leffler 	);
20628a1b9b6aSSam Leffler 
20638a1b9b6aSSam Leffler 	/* give driver a chance to setup state like ni_txrate */
2064736b3dc3SSam Leffler 	if (ic->ic_newassoc != NULL)
2065e9962332SSam Leffler 		ic->ic_newassoc(ni, newassoc);
20662045f699SSam Leffler 	ni->ni_inact_reload = ic->ic_inact_auth;
20672045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
20688a1b9b6aSSam Leffler 	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
20698a1b9b6aSSam Leffler 	/* tell the authenticator about new station */
20708a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_join != NULL)
20718a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_join(ic, ni);
20728a1b9b6aSSam Leffler 	ieee80211_notify_node_join(ic, ni, newassoc);
20738a1b9b6aSSam Leffler }
20748a1b9b6aSSam Leffler 
20758a1b9b6aSSam Leffler /*
20768a1b9b6aSSam Leffler  * Handle a station leaving an 11g network.
20778a1b9b6aSSam Leffler  */
20788a1b9b6aSSam Leffler static void
20798a1b9b6aSSam Leffler ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
20808a1b9b6aSSam Leffler {
20818a1b9b6aSSam Leffler 
20825ff80921SSam Leffler 	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G,
2083efefac40SSam Leffler 	     ("not in 11g, bss %u:0x%x, curmode %u", ni->ni_chan->ic_freq,
2084efefac40SSam Leffler 	      ni->ni_chan->ic_flags, ic->ic_curmode));
20858a1b9b6aSSam Leffler 
20868a1b9b6aSSam Leffler 	/*
20878a1b9b6aSSam Leffler 	 * If a long slot station do the slot time bookkeeping.
20888a1b9b6aSSam Leffler 	 */
20898a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
20908a1b9b6aSSam Leffler 		KASSERT(ic->ic_longslotsta > 0,
20918a1b9b6aSSam Leffler 		    ("bogus long slot station count %d", ic->ic_longslotsta));
20928a1b9b6aSSam Leffler 		ic->ic_longslotsta--;
20938a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
20948a1b9b6aSSam Leffler 		    "[%s] long slot time station leaves, count now %d\n",
20958a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
20968a1b9b6aSSam Leffler 		if (ic->ic_longslotsta == 0) {
20978a1b9b6aSSam Leffler 			/*
20988a1b9b6aSSam Leffler 			 * Re-enable use of short slot time if supported
20998a1b9b6aSSam Leffler 			 * and not operating in IBSS mode (per spec).
21008a1b9b6aSSam Leffler 			 */
21018a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
21028a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_IBSS) {
21038a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
21048a1b9b6aSSam Leffler 				    "%s: re-enable use of short slot time\n",
21058a1b9b6aSSam Leffler 				    __func__);
21068a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic, 1);
21078a1b9b6aSSam Leffler 			}
21088a1b9b6aSSam Leffler 		}
21098a1b9b6aSSam Leffler 	}
21108a1b9b6aSSam Leffler 	/*
21118a1b9b6aSSam Leffler 	 * If a non-ERP station do the protection-related bookkeeping.
21128a1b9b6aSSam Leffler 	 */
21138a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
21148a1b9b6aSSam Leffler 		KASSERT(ic->ic_nonerpsta > 0,
21158a1b9b6aSSam Leffler 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
21168a1b9b6aSSam Leffler 		ic->ic_nonerpsta--;
21178a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
21188a1b9b6aSSam Leffler 		    "[%s] non-ERP station leaves, count now %d\n",
21198a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
21208a1b9b6aSSam Leffler 		if (ic->ic_nonerpsta == 0) {
21218a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
21228a1b9b6aSSam Leffler 				"%s: disable use of protection\n", __func__);
21238a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
21248a1b9b6aSSam Leffler 			/* XXX verify mode? */
21258a1b9b6aSSam Leffler 			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
21268a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
21278a1b9b6aSSam Leffler 				    "%s: re-enable use of short preamble\n",
21288a1b9b6aSSam Leffler 				    __func__);
21298a1b9b6aSSam Leffler 				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
21308a1b9b6aSSam Leffler 				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
21318a1b9b6aSSam Leffler 			}
21328a1b9b6aSSam Leffler 		}
21338a1b9b6aSSam Leffler 	}
21348a1b9b6aSSam Leffler }
21358a1b9b6aSSam Leffler 
21368a1b9b6aSSam Leffler /*
21378a1b9b6aSSam Leffler  * Handle bookkeeping for station deauthentication/disassociation
21388a1b9b6aSSam Leffler  * when operating as an ap.
21398a1b9b6aSSam Leffler  */
21408a1b9b6aSSam Leffler void
21418a1b9b6aSSam Leffler ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
21428a1b9b6aSSam Leffler {
214344acc00dSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
21448a1b9b6aSSam Leffler 
21458a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
21468a1b9b6aSSam Leffler 	    "[%s] station with aid %d leaves\n",
21478a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
21488a1b9b6aSSam Leffler 
21498a1b9b6aSSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
21508a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_IBSS ||
21518a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_AHDEMO,
21528a1b9b6aSSam Leffler 		("unexpected operating mode %u", ic->ic_opmode));
21538a1b9b6aSSam Leffler 	/*
21548a1b9b6aSSam Leffler 	 * If node wasn't previously associated all
21558a1b9b6aSSam Leffler 	 * we need to do is reclaim the reference.
21568a1b9b6aSSam Leffler 	 */
21578a1b9b6aSSam Leffler 	/* XXX ibss mode bypasses 11g and notification */
21588a1b9b6aSSam Leffler 	if (ni->ni_associd == 0)
21598a1b9b6aSSam Leffler 		goto done;
21608a1b9b6aSSam Leffler 	/*
21618a1b9b6aSSam Leffler 	 * Tell the authenticator the station is leaving.
21628a1b9b6aSSam Leffler 	 * Note that we must do this before yanking the
21638a1b9b6aSSam Leffler 	 * association id as the authenticator uses the
21648a1b9b6aSSam Leffler 	 * associd to locate it's state block.
21658a1b9b6aSSam Leffler 	 */
21668a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_leave != NULL)
21678a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_leave(ic, ni);
21688a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
21698a1b9b6aSSam Leffler 	ni->ni_associd = 0;
21708a1b9b6aSSam Leffler 	ic->ic_sta_assoc--;
21718a1b9b6aSSam Leffler 
21725ff80921SSam Leffler 	if (ic->ic_curmode == IEEE80211_MODE_11G)
21738a1b9b6aSSam Leffler 		ieee80211_node_leave_11g(ic, ni);
21748a1b9b6aSSam Leffler 	/*
21758a1b9b6aSSam Leffler 	 * Cleanup station state.  In particular clear various
21768a1b9b6aSSam Leffler 	 * state that might otherwise be reused if the node
21778a1b9b6aSSam Leffler 	 * is reused before the reference count goes to zero
21788a1b9b6aSSam Leffler 	 * (and memory is reclaimed).
21798a1b9b6aSSam Leffler 	 */
21808a1b9b6aSSam Leffler 	ieee80211_sta_leave(ic, ni);
21818a1b9b6aSSam Leffler done:
218244acc00dSSam Leffler 	/*
218344acc00dSSam Leffler 	 * Remove the node from any table it's recorded in and
218444acc00dSSam Leffler 	 * drop the caller's reference.  Removal from the table
218544acc00dSSam Leffler 	 * is important to insure the node is not reprocessed
218644acc00dSSam Leffler 	 * for inactivity.
218744acc00dSSam Leffler 	 */
218844acc00dSSam Leffler 	if (nt != NULL) {
218944acc00dSSam Leffler 		IEEE80211_NODE_LOCK(nt);
219044acc00dSSam Leffler 		node_reclaim(nt, ni);
219144acc00dSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
219244acc00dSSam Leffler 	} else
21938a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
21948a1b9b6aSSam Leffler }
21958a1b9b6aSSam Leffler 
21968a1b9b6aSSam Leffler u_int8_t
21978a1b9b6aSSam Leffler ieee80211_getrssi(struct ieee80211com *ic)
21988a1b9b6aSSam Leffler {
21998a1b9b6aSSam Leffler #define	NZ(x)	((x) == 0 ? 1 : (x))
2200acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
22018a1b9b6aSSam Leffler 	u_int32_t rssi_samples, rssi_total;
22028a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
22038a1b9b6aSSam Leffler 
22048a1b9b6aSSam Leffler 	rssi_total = 0;
22058a1b9b6aSSam Leffler 	rssi_samples = 0;
22068a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
22078a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
22088a1b9b6aSSam Leffler 		/* XXX locking */
2209acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
22108a1b9b6aSSam Leffler 			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
22118a1b9b6aSSam Leffler 				rssi_samples++;
22128a1b9b6aSSam Leffler 				rssi_total += ic->ic_node_getrssi(ni);
22138a1b9b6aSSam Leffler 			}
22148a1b9b6aSSam Leffler 		break;
22158a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
22168a1b9b6aSSam Leffler 		/* XXX locking */
2217acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
22188a1b9b6aSSam Leffler 			rssi_samples++;
22198a1b9b6aSSam Leffler 			rssi_total += ic->ic_node_getrssi(ni);
22208a1b9b6aSSam Leffler 		}
22218a1b9b6aSSam Leffler 		break;
22228a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
22238a1b9b6aSSam Leffler 		/* XXX locking */
2224acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
22258a1b9b6aSSam Leffler 			if (IEEE80211_AID(ni->ni_associd) != 0) {
22268a1b9b6aSSam Leffler 				rssi_samples++;
22278a1b9b6aSSam Leffler 				rssi_total += ic->ic_node_getrssi(ni);
22288a1b9b6aSSam Leffler 			}
22298a1b9b6aSSam Leffler 		break;
22308a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:	/* XXX */
22318a1b9b6aSSam Leffler 	case IEEE80211_M_STA:		/* use stats from associated ap */
22328a1b9b6aSSam Leffler 	default:
22338a1b9b6aSSam Leffler 		if (ic->ic_bss != NULL)
22348a1b9b6aSSam Leffler 			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
22358a1b9b6aSSam Leffler 		rssi_samples = 1;
22368a1b9b6aSSam Leffler 		break;
22378a1b9b6aSSam Leffler 	}
22388a1b9b6aSSam Leffler 	return rssi_total / NZ(rssi_samples);
22398a1b9b6aSSam Leffler #undef NZ
22408a1b9b6aSSam Leffler }
22418a1b9b6aSSam Leffler 
22428a1b9b6aSSam Leffler /*
22438a1b9b6aSSam Leffler  * Indicate whether there are frames queued for a station in power-save mode.
22448a1b9b6aSSam Leffler  */
22458a1b9b6aSSam Leffler static void
2246edfa57d0SSam Leffler ieee80211_set_tim(struct ieee80211_node *ni, int set)
22478a1b9b6aSSam Leffler {
2248edfa57d0SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
22498a1b9b6aSSam Leffler 	u_int16_t aid;
22508a1b9b6aSSam Leffler 
22518a1b9b6aSSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
22528a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_IBSS,
22538a1b9b6aSSam Leffler 		("operating mode %u", ic->ic_opmode));
22548a1b9b6aSSam Leffler 
22558a1b9b6aSSam Leffler 	aid = IEEE80211_AID(ni->ni_associd);
22568a1b9b6aSSam Leffler 	KASSERT(aid < ic->ic_max_aid,
22578a1b9b6aSSam Leffler 		("bogus aid %u, max %u", aid, ic->ic_max_aid));
22588a1b9b6aSSam Leffler 
22598a1b9b6aSSam Leffler 	IEEE80211_BEACON_LOCK(ic);
22608a1b9b6aSSam Leffler 	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
22618a1b9b6aSSam Leffler 		if (set) {
22628a1b9b6aSSam Leffler 			setbit(ic->ic_tim_bitmap, aid);
22638a1b9b6aSSam Leffler 			ic->ic_ps_pending++;
22648a1b9b6aSSam Leffler 		} else {
22658a1b9b6aSSam Leffler 			clrbit(ic->ic_tim_bitmap, aid);
22668a1b9b6aSSam Leffler 			ic->ic_ps_pending--;
22678a1b9b6aSSam Leffler 		}
22688a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
22698a1b9b6aSSam Leffler 	}
22708a1b9b6aSSam Leffler 	IEEE80211_BEACON_UNLOCK(ic);
22718a1b9b6aSSam Leffler }
22728a1b9b6aSSam Leffler 
22738a1b9b6aSSam Leffler /*
22748a1b9b6aSSam Leffler  * Node table support.
22758a1b9b6aSSam Leffler  */
22768a1b9b6aSSam Leffler 
22778a1b9b6aSSam Leffler static void
22788a1b9b6aSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic,
22798a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt,
2280c1225b52SSam Leffler 	const char *name, int inact, int keyixmax,
22818a1b9b6aSSam Leffler 	void (*timeout)(struct ieee80211_node_table *))
22828a1b9b6aSSam Leffler {
22838a1b9b6aSSam Leffler 
22848a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
22858a1b9b6aSSam Leffler 		"%s %s table, inact %u\n", __func__, name, inact);
22868a1b9b6aSSam Leffler 
22878a1b9b6aSSam Leffler 	nt->nt_ic = ic;
22888a1b9b6aSSam Leffler 	/* XXX need unit */
22898a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
22908a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
22918a1b9b6aSSam Leffler 	TAILQ_INIT(&nt->nt_node);
22928a1b9b6aSSam Leffler 	nt->nt_name = name;
22938a1b9b6aSSam Leffler 	nt->nt_scangen = 1;
22948a1b9b6aSSam Leffler 	nt->nt_inact_init = inact;
22958a1b9b6aSSam Leffler 	nt->nt_timeout = timeout;
2296c1225b52SSam Leffler 	nt->nt_keyixmax = keyixmax;
2297c1225b52SSam Leffler 	if (nt->nt_keyixmax > 0) {
2298c1225b52SSam Leffler 		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
2299c1225b52SSam Leffler 			keyixmax * sizeof(struct ieee80211_node *),
2300c1225b52SSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
2301c1225b52SSam Leffler 		if (nt->nt_keyixmap == NULL)
2302c1225b52SSam Leffler 			if_printf(ic->ic_ifp,
2303c1225b52SSam Leffler 			    "Cannot allocate key index map with %u entries\n",
2304c1225b52SSam Leffler 			    keyixmax);
2305c1225b52SSam Leffler 	} else
2306c1225b52SSam Leffler 		nt->nt_keyixmap = NULL;
23078a1b9b6aSSam Leffler }
23088a1b9b6aSSam Leffler 
23098a1b9b6aSSam Leffler void
23108a1b9b6aSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt)
23118a1b9b6aSSam Leffler {
23128a1b9b6aSSam Leffler 
23138a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
23148a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
23158a1b9b6aSSam Leffler 
23168a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
23178a1b9b6aSSam Leffler 	nt->nt_inact_timer = 0;
23188a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
23198a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
23208a1b9b6aSSam Leffler }
23218a1b9b6aSSam Leffler 
23228a1b9b6aSSam Leffler static void
23238a1b9b6aSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
23248a1b9b6aSSam Leffler {
23258a1b9b6aSSam Leffler 
23268a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
23278a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
23288a1b9b6aSSam Leffler 
2329c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
23308a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
2331c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL) {
2332c1225b52SSam Leffler 		/* XXX verify all entries are NULL */
2333c1225b52SSam Leffler 		int i;
2334c1225b52SSam Leffler 		for (i = 0; i < nt->nt_keyixmax; i++)
2335c1225b52SSam Leffler 			if (nt->nt_keyixmap[i] != NULL)
2336c1225b52SSam Leffler 				printf("%s: %s[%u] still active\n", __func__,
2337c1225b52SSam Leffler 					nt->nt_name, i);
2338c1225b52SSam Leffler 		FREE(nt->nt_keyixmap, M_80211_NODE);
2339c1225b52SSam Leffler 		nt->nt_keyixmap = NULL;
2340c1225b52SSam Leffler 	}
23418a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_DESTROY(nt);
23428a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_DESTROY(nt);
23438a1b9b6aSSam Leffler }
2344