xref: /freebsd/sys/net80211/ieee80211_node.c (revision ce8977df)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
3ae8b7333SSam Leffler  * Copyright (c) 2002-2007 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.
141a1e1d21SSam Leffler  *
157535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251a1e1d21SSam Leffler  */
261a1e1d21SSam Leffler 
271a1e1d21SSam Leffler #include <sys/cdefs.h>
281a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
291a1e1d21SSam Leffler 
301a1e1d21SSam Leffler #include <sys/param.h>
311a1e1d21SSam Leffler #include <sys/systm.h>
321a1e1d21SSam Leffler #include <sys/mbuf.h>
331a1e1d21SSam Leffler #include <sys/malloc.h>
341a1e1d21SSam Leffler #include <sys/kernel.h>
351a1e1d21SSam Leffler 
368a1b9b6aSSam Leffler #include <sys/socket.h>
371a1e1d21SSam Leffler 
381a1e1d21SSam Leffler #include <net/if.h>
391a1e1d21SSam Leffler #include <net/if_media.h>
401a1e1d21SSam Leffler #include <net/ethernet.h>
411a1e1d21SSam Leffler 
421a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
431a1e1d21SSam Leffler 
441a1e1d21SSam Leffler #include <net/bpf.h>
451a1e1d21SSam Leffler 
467268fa64SSam Leffler /*
477268fa64SSam Leffler  * Association id's are managed with a bit vector.
487268fa64SSam Leffler  */
497268fa64SSam Leffler #define	IEEE80211_AID_SET(b, w) \
507268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
517268fa64SSam Leffler #define	IEEE80211_AID_CLR(b, w) \
527268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
537268fa64SSam Leffler #define	IEEE80211_AID_ISSET(b, w) \
547268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
557268fa64SSam Leffler 
56132142c5SDiomidis Spinellis #ifdef IEEE80211_DEBUG_REFCNT
57132142c5SDiomidis Spinellis #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
58132142c5SDiomidis Spinellis #else
59132142c5SDiomidis Spinellis #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
60132142c5SDiomidis Spinellis #endif
61132142c5SDiomidis Spinellis 
6268e8e04eSSam Leffler static int ieee80211_sta_join1(struct ieee80211_node *);
6368e8e04eSSam Leffler 
648a1b9b6aSSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
658a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *);
668a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *);
6768e8e04eSSam Leffler static int8_t node_getrssi(const struct ieee80211_node *);
6868e8e04eSSam Leffler static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
691a1e1d21SSam Leffler 
708a1b9b6aSSam Leffler static void ieee80211_setup_node(struct ieee80211_node_table *,
7168e8e04eSSam Leffler 		struct ieee80211_node *, const uint8_t *);
728a1b9b6aSSam Leffler static void _ieee80211_free_node(struct ieee80211_node *);
738a1b9b6aSSam Leffler 
748a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic,
75c1225b52SSam Leffler 	struct ieee80211_node_table *nt, const char *name,
7668e8e04eSSam Leffler 	int inact, int keymaxix);
7768e8e04eSSam Leffler static void ieee80211_node_table_reset(struct ieee80211_node_table *);
788a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
79b105a069SSam Leffler static void ieee80211_erp_timeout(struct ieee80211com *);
801a1e1d21SSam Leffler 
8132346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
8237c150c4SSam Leffler 
831a1e1d21SSam Leffler void
848a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic)
851a1e1d21SSam Leffler {
861a1e1d21SSam Leffler 
878a1b9b6aSSam Leffler 	ic->ic_node_alloc = node_alloc;
888a1b9b6aSSam Leffler 	ic->ic_node_free = node_free;
898a1b9b6aSSam Leffler 	ic->ic_node_cleanup = node_cleanup;
908a1b9b6aSSam Leffler 	ic->ic_node_getrssi = node_getrssi;
9168e8e04eSSam Leffler 	ic->ic_node_getsignal = node_getsignal;
928a1b9b6aSSam Leffler 
938a1b9b6aSSam Leffler 	/* default station inactivity timer setings */
948a1b9b6aSSam Leffler 	ic->ic_inact_init = IEEE80211_INACT_INIT;
958a1b9b6aSSam Leffler 	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
968a1b9b6aSSam Leffler 	ic->ic_inact_run = IEEE80211_INACT_RUN;
978a1b9b6aSSam Leffler 	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
988a1b9b6aSSam Leffler 
9968e8e04eSSam Leffler 	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
10068e8e04eSSam Leffler 
101c1225b52SSam Leffler 	/* NB: driver should override */
1028a1b9b6aSSam Leffler 	ic->ic_max_aid = IEEE80211_AID_DEF;
103c066143cSSam Leffler 
104c066143cSSam Leffler 	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
105c1225b52SSam Leffler }
106c1225b52SSam Leffler 
107c1225b52SSam Leffler void
108c1225b52SSam Leffler ieee80211_node_lateattach(struct ieee80211com *ic)
109c1225b52SSam Leffler {
110c1225b52SSam Leffler 	struct ieee80211_rsnparms *rsn;
111c1225b52SSam Leffler 
112c1225b52SSam Leffler 	if (ic->ic_max_aid > IEEE80211_AID_MAX)
1138a1b9b6aSSam Leffler 		ic->ic_max_aid = IEEE80211_AID_MAX;
11468e8e04eSSam Leffler 	MALLOC(ic->ic_aid_bitmap, uint32_t *,
11568e8e04eSSam Leffler 		howmany(ic->ic_max_aid, 32) * sizeof(uint32_t),
11668e8e04eSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
1178a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap == NULL) {
1188a1b9b6aSSam Leffler 		/* XXX no way to recover */
1198a1b9b6aSSam Leffler 		printf("%s: no memory for AID bitmap!\n", __func__);
1208a1b9b6aSSam Leffler 		ic->ic_max_aid = 0;
1218a1b9b6aSSam Leffler 	}
1228a1b9b6aSSam Leffler 
123c1225b52SSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
12468e8e04eSSam Leffler 		IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix);
1252692bb26SSam Leffler 
126c1225b52SSam Leffler 	ieee80211_reset_bss(ic);
1278a1b9b6aSSam Leffler 	/*
1288a1b9b6aSSam Leffler 	 * Setup "global settings" in the bss node so that
1298a1b9b6aSSam Leffler 	 * each new station automatically inherits them.
1308a1b9b6aSSam Leffler 	 */
131c1225b52SSam Leffler 	rsn = &ic->ic_bss->ni_rsn;
1328a1b9b6aSSam Leffler 	/* WEP, TKIP, and AES-CCM are always supported */
1338a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
1348a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
1358a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
1368a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_AES)
1378a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
1388a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_CKIP)
1398a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
1408a1b9b6aSSam Leffler 	/*
1418a1b9b6aSSam Leffler 	 * Default unicast cipher to WEP for 802.1x use.  If
1428a1b9b6aSSam Leffler 	 * WPA is enabled the management code will set these
1438a1b9b6aSSam Leffler 	 * values to reflect.
1448a1b9b6aSSam Leffler 	 */
1458a1b9b6aSSam Leffler 	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
1468a1b9b6aSSam Leffler 	rsn->rsn_ucastkeylen = 104 / NBBY;
1478a1b9b6aSSam Leffler 	/*
1488a1b9b6aSSam Leffler 	 * WPA says the multicast cipher is the lowest unicast
1498a1b9b6aSSam Leffler 	 * cipher supported.  But we skip WEP which would
1508a1b9b6aSSam Leffler 	 * otherwise be used based on this criteria.
1518a1b9b6aSSam Leffler 	 */
1528a1b9b6aSSam Leffler 	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
1538a1b9b6aSSam Leffler 	rsn->rsn_mcastkeylen = 128 / NBBY;
1548a1b9b6aSSam Leffler 
1558a1b9b6aSSam Leffler 	/*
1568a1b9b6aSSam Leffler 	 * We support both WPA-PSK and 802.1x; the one used
1578a1b9b6aSSam Leffler 	 * is determined by the authentication mode and the
1588a1b9b6aSSam Leffler 	 * setting of the PSK state.
1598a1b9b6aSSam Leffler 	 */
1608a1b9b6aSSam Leffler 	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
1618a1b9b6aSSam Leffler 	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1628a1b9b6aSSam Leffler 
163c1225b52SSam Leffler 	ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
1641a1e1d21SSam Leffler }
1651a1e1d21SSam Leffler 
1661a1e1d21SSam Leffler void
1678a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic)
1681a1e1d21SSam Leffler {
1691a1e1d21SSam Leffler 
1708a1b9b6aSSam Leffler 	if (ic->ic_bss != NULL) {
1718a1b9b6aSSam Leffler 		ieee80211_free_node(ic->ic_bss);
1728a1b9b6aSSam Leffler 		ic->ic_bss = NULL;
1738a1b9b6aSSam Leffler 	}
174acc4f7f5SSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_sta);
1758a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap != NULL) {
17668e8e04eSSam Leffler 		FREE(ic->ic_aid_bitmap, M_80211_NODE);
1778a1b9b6aSSam Leffler 		ic->ic_aid_bitmap = NULL;
1788a1b9b6aSSam Leffler 	}
1798a1b9b6aSSam Leffler }
1808a1b9b6aSSam Leffler 
1818a1b9b6aSSam Leffler /*
1828a1b9b6aSSam Leffler  * Port authorize/unauthorize interfaces for use by an authenticator.
1838a1b9b6aSSam Leffler  */
1848a1b9b6aSSam Leffler 
1858a1b9b6aSSam Leffler void
186e4918ecdSSam Leffler ieee80211_node_authorize(struct ieee80211_node *ni)
1878a1b9b6aSSam Leffler {
188e4918ecdSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
189e4918ecdSSam Leffler 
1908a1b9b6aSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AUTH;
1912045f699SSam Leffler 	ni->ni_inact_reload = ic->ic_inact_run;
192c066143cSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
1938a1b9b6aSSam Leffler }
1948a1b9b6aSSam Leffler 
1958a1b9b6aSSam Leffler void
196e4918ecdSSam Leffler ieee80211_node_unauthorize(struct ieee80211_node *ni)
1978a1b9b6aSSam Leffler {
1987595008bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1997595008bSSam Leffler 
2008a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
201c066143cSSam Leffler 	ni->ni_inact_reload = ic->ic_inact_auth;
202c066143cSSam Leffler 	if (ni->ni_inact > ni->ni_inact_reload)
203c066143cSSam Leffler 		ni->ni_inact = ni->ni_inact_reload;
2048a1b9b6aSSam Leffler }
2058a1b9b6aSSam Leffler 
2068a1b9b6aSSam Leffler /*
2078a1b9b6aSSam Leffler  * Set/change the channel.  The rate set is also updated as
2088a1b9b6aSSam Leffler  * to insure a consistent view by drivers.
2098a1b9b6aSSam Leffler  */
2101b49e120SSam Leffler static void
21168e8e04eSSam Leffler ieee80211_node_set_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
2128a1b9b6aSSam Leffler {
21368e8e04eSSam Leffler 	struct ieee80211_channel *chan = ic->ic_bsschan;
21468e8e04eSSam Leffler 
21568e8e04eSSam Leffler #if 0
21668e8e04eSSam Leffler 	KASSERT(chan != IEEE80211_CHAN_ANYC, ("bss channel not setup"));
21768e8e04eSSam Leffler #else
2181b49e120SSam Leffler 	if (chan == IEEE80211_CHAN_ANYC)	/* XXX while scanning */
2191b49e120SSam Leffler 		chan = ic->ic_curchan;
22068e8e04eSSam Leffler #endif
2218a1b9b6aSSam Leffler 	ni->ni_chan = chan;
22268e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HT(chan)) {
22368e8e04eSSam Leffler 		/*
22468e8e04eSSam Leffler 		 * XXX Gotta be careful here; the rate set returned by
22568e8e04eSSam Leffler 		 * ieee80211_get_suprates is actually any HT rate
22668e8e04eSSam Leffler 		 * set so blindly copying it will be bad.  We must
22768e8e04eSSam Leffler 		 * install the legacy rate est in ni_rates and the
22868e8e04eSSam Leffler 		 * HT rate set in ni_htrates.
22968e8e04eSSam Leffler 		 */
23068e8e04eSSam Leffler 		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
23168e8e04eSSam Leffler 	}
23241b3c790SSam Leffler 	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
2331a1e1d21SSam Leffler }
2341a1e1d21SSam Leffler 
2351a1e1d21SSam Leffler /*
236097131ffSSam Leffler  * Probe the curent channel, if allowed, while scanning.
237097131ffSSam Leffler  * If the channel is not marked passive-only then send
238097131ffSSam Leffler  * a probe request immediately.  Otherwise mark state and
239097131ffSSam Leffler  * listen for beacons on the channel; if we receive something
240097131ffSSam Leffler  * then we'll transmit a probe request.
241097131ffSSam Leffler  */
242097131ffSSam Leffler void
243097131ffSSam Leffler ieee80211_probe_curchan(struct ieee80211com *ic, int force)
244097131ffSSam Leffler {
245097131ffSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
246097131ffSSam Leffler 
247097131ffSSam Leffler 	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) {
248097131ffSSam Leffler 		/*
249097131ffSSam Leffler 		 * XXX send both broadcast+directed probe request
250097131ffSSam Leffler 		 */
251097131ffSSam Leffler 		ieee80211_send_probereq(ic->ic_bss,
252097131ffSSam Leffler 			ic->ic_myaddr, ifp->if_broadcastaddr,
253097131ffSSam Leffler 			ifp->if_broadcastaddr,
25468e8e04eSSam Leffler 			ic->ic_des_ssid[0].ssid, ic->ic_des_ssid[0].len,
255097131ffSSam Leffler 			ic->ic_opt_ie, ic->ic_opt_ie_len);
256097131ffSSam Leffler 	} else
257097131ffSSam Leffler 		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
258097131ffSSam Leffler }
259097131ffSSam Leffler 
260f9cd9174SSam Leffler static __inline void
261f9cd9174SSam Leffler copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
262f9cd9174SSam Leffler {
263f9cd9174SSam Leffler 	/* propagate useful state */
264f9cd9174SSam Leffler 	nbss->ni_authmode = obss->ni_authmode;
265f9cd9174SSam Leffler 	nbss->ni_txpower = obss->ni_txpower;
266f9cd9174SSam Leffler 	nbss->ni_vlan = obss->ni_vlan;
267f9cd9174SSam Leffler 	nbss->ni_rsn = obss->ni_rsn;
268f9cd9174SSam Leffler 	/* XXX statistics? */
269f9cd9174SSam Leffler }
270f9cd9174SSam Leffler 
2711a1e1d21SSam Leffler void
2721a1e1d21SSam Leffler ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
2731a1e1d21SSam Leffler {
274acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt;
2751a1e1d21SSam Leffler 	struct ieee80211_node *ni;
2768a1b9b6aSSam Leffler 
2778a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
2788a1b9b6aSSam Leffler 		"%s: creating ibss\n", __func__);
2798a1b9b6aSSam Leffler 
2808a1b9b6aSSam Leffler 	/*
2818a1b9b6aSSam Leffler 	 * Create the station/neighbor table.  Note that for adhoc
2828a1b9b6aSSam Leffler 	 * mode we make the initial inactivity timer longer since
2838a1b9b6aSSam Leffler 	 * we create nodes only through discovery and they typically
2848a1b9b6aSSam Leffler 	 * are long-lived associations.
2858a1b9b6aSSam Leffler 	 */
286acc4f7f5SSam Leffler 	nt = &ic->ic_sta;
287acc4f7f5SSam Leffler 	IEEE80211_NODE_LOCK(nt);
288acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
289acc4f7f5SSam Leffler 		nt->nt_name = "station";
290acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_init;
291acc4f7f5SSam Leffler 	} else {
292acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
293acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
294acc4f7f5SSam Leffler 	}
295acc4f7f5SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
296acc4f7f5SSam Leffler 
297c1225b52SSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
298acc4f7f5SSam Leffler 	if (ni == NULL) {
299acc4f7f5SSam Leffler 		/* XXX recovery? */
3008a1b9b6aSSam Leffler 		return;
3018a1b9b6aSSam Leffler 	}
3021a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
30368e8e04eSSam Leffler 	ni->ni_esslen = ic->ic_des_ssid[0].len;
30468e8e04eSSam Leffler 	memcpy(ni->ni_essid, ic->ic_des_ssid[0].ssid, ni->ni_esslen);
30568e8e04eSSam Leffler 	if (ic->ic_bss != NULL)
306f9cd9174SSam Leffler 		copy_bss(ni, ic->ic_bss);
307d365f9c7SSam Leffler 	ni->ni_intval = ic->ic_bintval;
3088a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
3091a1e1d21SSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
3101a1e1d21SSam Leffler 	if (ic->ic_phytype == IEEE80211_T_FH) {
3111a1e1d21SSam Leffler 		ni->ni_fhdwell = 200;	/* XXX */
3121a1e1d21SSam Leffler 		ni->ni_fhindex = 1;
3131a1e1d21SSam Leffler 	}
3148a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
3158a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SIBSS;
3168a1b9b6aSSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
31748b0a5beSSam Leffler 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
31848b0a5beSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
319fe49f061SSam Leffler 		else {
320fe49f061SSam Leffler 			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
321fe49f061SSam Leffler 			/* clear group bit, add local bit */
322fe49f061SSam Leffler 			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
323fe49f061SSam Leffler 		}
32450d8b493SSam Leffler 	} else if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
32550d8b493SSam Leffler 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
32650d8b493SSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
32750d8b493SSam Leffler 		else
32850d8b493SSam Leffler 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
3298a1b9b6aSSam Leffler 	}
3308a1b9b6aSSam Leffler 	/*
3318a1b9b6aSSam Leffler 	 * Fix the channel and related attributes.
3328a1b9b6aSSam Leffler 	 */
33368e8e04eSSam Leffler 	ic->ic_bsschan = chan;
33468e8e04eSSam Leffler 	ieee80211_node_set_chan(ic, ni);
33568e8e04eSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(chan);
3368a1b9b6aSSam Leffler 	/*
3378a1b9b6aSSam Leffler 	 * Do mode-specific rate setup.
3388a1b9b6aSSam Leffler 	 */
33968e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_FULL(chan)) {
34068e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(chan)) {
34168e8e04eSSam Leffler 			/*
34268e8e04eSSam Leffler 			 * Use a mixed 11b/11g rate set.
34368e8e04eSSam Leffler 			 */
34468e8e04eSSam Leffler 			ieee80211_set11gbasicrates(&ni->ni_rates,
34568e8e04eSSam Leffler 				IEEE80211_MODE_11G);
34668e8e04eSSam Leffler 		} else if (IEEE80211_IS_CHAN_B(chan)) {
34768e8e04eSSam Leffler 			/*
34868e8e04eSSam Leffler 			 * Force pure 11b rate set.
34968e8e04eSSam Leffler 			 */
35068e8e04eSSam Leffler 			ieee80211_set11gbasicrates(&ni->ni_rates,
35168e8e04eSSam Leffler 				IEEE80211_MODE_11B);
35268e8e04eSSam Leffler 		}
3531a1e1d21SSam Leffler 	}
3541a1e1d21SSam Leffler 
35568e8e04eSSam Leffler 	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
35668e8e04eSSam Leffler }
35768e8e04eSSam Leffler 
35868e8e04eSSam Leffler /*
35968e8e04eSSam Leffler  * Reset bss state on transition to the INIT state.
36068e8e04eSSam Leffler  * Clear any stations from the table (they have been
36168e8e04eSSam Leffler  * deauth'd) and reset the bss node (clears key, rate
36268e8e04eSSam Leffler  * etc. state).
36368e8e04eSSam Leffler  */
3648a1b9b6aSSam Leffler void
3658a1b9b6aSSam Leffler ieee80211_reset_bss(struct ieee80211com *ic)
366b4c5a90fSSam Leffler {
3678a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *obss;
3688a1b9b6aSSam Leffler 
36968e8e04eSSam Leffler 	callout_drain(&ic->ic_inact);
370acc4f7f5SSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta);
37168e8e04eSSam Leffler 	ieee80211_reset_erp(ic);
372acc4f7f5SSam Leffler 
37368e8e04eSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
3748a1b9b6aSSam Leffler 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
3758a1b9b6aSSam Leffler 	obss = ic->ic_bss;
3768a1b9b6aSSam Leffler 	ic->ic_bss = ieee80211_ref_node(ni);
377f9cd9174SSam Leffler 	if (obss != NULL) {
378f9cd9174SSam Leffler 		copy_bss(ni, obss);
379d365f9c7SSam Leffler 		ni->ni_intval = ic->ic_bintval;
3808a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
3818a1b9b6aSSam Leffler 	}
382f9cd9174SSam Leffler }
3838a1b9b6aSSam Leffler 
3848a1b9b6aSSam Leffler static int
38568e8e04eSSam Leffler match_ssid(const struct ieee80211_node *ni,
38668e8e04eSSam Leffler 	int nssid, const struct ieee80211_scan_ssid ssids[])
3878a1b9b6aSSam Leffler {
38868e8e04eSSam Leffler 	int i;
38968e8e04eSSam Leffler 
39068e8e04eSSam Leffler 	for (i = 0; i < nssid; i++) {
39168e8e04eSSam Leffler 		if (ni->ni_esslen == ssids[i].len &&
39268e8e04eSSam Leffler 		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
39368e8e04eSSam Leffler 			return 1;
39468e8e04eSSam Leffler 	}
39568e8e04eSSam Leffler 	return 0;
39668e8e04eSSam Leffler }
39768e8e04eSSam Leffler 
39868e8e04eSSam Leffler /*
39968e8e04eSSam Leffler  * Test a node for suitability/compatibility.
40068e8e04eSSam Leffler  */
40168e8e04eSSam Leffler static int
40268e8e04eSSam Leffler check_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
40368e8e04eSSam Leffler {
40468e8e04eSSam Leffler         uint8_t rate;
40568e8e04eSSam Leffler 
40668e8e04eSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
40768e8e04eSSam Leffler 		return 0;
40868e8e04eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
40968e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
41068e8e04eSSam Leffler 			return 0;
41168e8e04eSSam Leffler 	} else {
41268e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
41368e8e04eSSam Leffler 			return 0;
41468e8e04eSSam Leffler 	}
41568e8e04eSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
41668e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
41768e8e04eSSam Leffler 			return 0;
41868e8e04eSSam Leffler 	} else {
41968e8e04eSSam Leffler 		/* XXX does this mean privacy is supported or required? */
42068e8e04eSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
42168e8e04eSSam Leffler 			return 0;
42268e8e04eSSam Leffler 	}
42368e8e04eSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
42468e8e04eSSam Leffler 	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
42568e8e04eSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
42668e8e04eSSam Leffler 		return 0;
42768e8e04eSSam Leffler 	if (ic->ic_des_nssid != 0 &&
42868e8e04eSSam Leffler 	    !match_ssid(ni, ic->ic_des_nssid, ic->ic_des_ssid))
42968e8e04eSSam Leffler 		return 0;
43068e8e04eSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
43168e8e04eSSam Leffler 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
43268e8e04eSSam Leffler 		return 0;
43368e8e04eSSam Leffler 	return 1;
43468e8e04eSSam Leffler }
43568e8e04eSSam Leffler 
43668e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
43768e8e04eSSam Leffler /*
43868e8e04eSSam Leffler  * Display node suitability/compatibility.
43968e8e04eSSam Leffler  */
44068e8e04eSSam Leffler static void
44168e8e04eSSam Leffler check_bss_debug(struct ieee80211com *ic, struct ieee80211_node *ni)
44268e8e04eSSam Leffler {
44368e8e04eSSam Leffler         uint8_t rate;
444b4c5a90fSSam Leffler         int fail;
445b4c5a90fSSam Leffler 
446b4c5a90fSSam Leffler 	fail = 0;
447b4c5a90fSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
448b4c5a90fSSam Leffler 		fail |= 0x01;
449b4c5a90fSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
450b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
451b4c5a90fSSam Leffler 			fail |= 0x02;
452b4c5a90fSSam Leffler 	} else {
453b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
454b4c5a90fSSam Leffler 			fail |= 0x02;
455b4c5a90fSSam Leffler 	}
4568a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
457b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
458b4c5a90fSSam Leffler 			fail |= 0x04;
459b4c5a90fSSam Leffler 	} else {
460b4c5a90fSSam Leffler 		/* XXX does this mean privacy is supported or required? */
461b4c5a90fSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
462b4c5a90fSSam Leffler 			fail |= 0x04;
463b4c5a90fSSam Leffler 	}
46470e28b9aSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
46579edaebfSSam Leffler 	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
466b4c5a90fSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
467b4c5a90fSSam Leffler 		fail |= 0x08;
46868e8e04eSSam Leffler 	if (ic->ic_des_nssid != 0 &&
46968e8e04eSSam Leffler 	    !match_ssid(ni, ic->ic_des_nssid, ic->ic_des_ssid))
470b4c5a90fSSam Leffler 		fail |= 0x10;
471b4c5a90fSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
472b4c5a90fSSam Leffler 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
473b4c5a90fSSam Leffler 		fail |= 0x20;
47468e8e04eSSam Leffler 
47568e8e04eSSam Leffler 	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
47668e8e04eSSam Leffler 	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
47768e8e04eSSam Leffler 	printf(" %3d%c",
47868e8e04eSSam Leffler 	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
479b4c5a90fSSam Leffler 	printf(" %+4d", ni->ni_rssi);
480b4c5a90fSSam Leffler 	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
481b4c5a90fSSam Leffler 	    fail & 0x08 ? '!' : ' ');
482b4c5a90fSSam Leffler 	printf(" %4s%c",
483b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
484b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
485b4c5a90fSSam Leffler 	    "????",
486b4c5a90fSSam Leffler 	    fail & 0x02 ? '!' : ' ');
487b4c5a90fSSam Leffler 	printf(" %3s%c ",
48868e8e04eSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
489b4c5a90fSSam Leffler 	    fail & 0x04 ? '!' : ' ');
490b4c5a90fSSam Leffler 	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
491b4c5a90fSSam Leffler 	printf("%s\n", fail & 0x10 ? "!" : "");
492b4c5a90fSSam Leffler }
49368e8e04eSSam Leffler #endif /* IEEE80211_DEBUG */
4948a1b9b6aSSam Leffler 
495750d6d0cSSam Leffler /*
4968a1b9b6aSSam Leffler  * Handle 802.11 ad hoc network merge.  The
4978a1b9b6aSSam Leffler  * convention, set by the Wireless Ethernet Compatibility Alliance
4988a1b9b6aSSam Leffler  * (WECA), is that an 802.11 station will change its BSSID to match
4998a1b9b6aSSam Leffler  * the "oldest" 802.11 ad hoc network, on the same channel, that
5008a1b9b6aSSam Leffler  * has the station's desired SSID.  The "oldest" 802.11 network
5018a1b9b6aSSam Leffler  * sends beacons with the greatest TSF timestamp.
5028a1b9b6aSSam Leffler  *
5038a1b9b6aSSam Leffler  * The caller is assumed to validate TSF's before attempting a merge.
5048a1b9b6aSSam Leffler  *
5058a1b9b6aSSam Leffler  * Return !0 if the BSSID changed, 0 otherwise.
506750d6d0cSSam Leffler  */
5078a1b9b6aSSam Leffler int
508641b4d0bSSam Leffler ieee80211_ibss_merge(struct ieee80211_node *ni)
5098a1b9b6aSSam Leffler {
510641b4d0bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
5118a1b9b6aSSam Leffler 
51296acc1b6SSam Leffler 	if (ni == ic->ic_bss ||
51396acc1b6SSam Leffler 	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
5148a1b9b6aSSam Leffler 		/* unchanged, nothing to do */
5158a1b9b6aSSam Leffler 		return 0;
5168a1b9b6aSSam Leffler 	}
51768e8e04eSSam Leffler 	if (!check_bss(ic, ni)) {
51868e8e04eSSam Leffler 		/* capabilities mismatch */
5198a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
5208a1b9b6aSSam Leffler 		    "%s: merge failed, capabilities mismatch\n", __func__);
52168e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
52268e8e04eSSam Leffler 		if (ieee80211_msg_assoc(ic))
52368e8e04eSSam Leffler 			check_bss_debug(ic, ni);
52468e8e04eSSam Leffler #endif
5258a1b9b6aSSam Leffler 		ic->ic_stats.is_ibss_capmismatch++;
5268a1b9b6aSSam Leffler 		return 0;
5278a1b9b6aSSam Leffler 	}
5288a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
5298a1b9b6aSSam Leffler 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
5308a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
5318a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
5328a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
5338a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
5348a1b9b6aSSam Leffler 	);
53568e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
5368a1b9b6aSSam Leffler }
5378a1b9b6aSSam Leffler 
5388a1b9b6aSSam Leffler /*
5391b6167d2SSam Leffler  * Change the bss channel.
5401b6167d2SSam Leffler  */
5411b6167d2SSam Leffler void
5421b6167d2SSam Leffler ieee80211_setbsschan(struct ieee80211com *ic, struct ieee80211_channel *c)
5431b6167d2SSam Leffler {
5441b6167d2SSam Leffler 	ic->ic_bsschan = c;
5451b6167d2SSam Leffler 	ic->ic_curchan = ic->ic_bsschan;
5461b6167d2SSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
5471b6167d2SSam Leffler 	ic->ic_set_channel(ic);
5481b6167d2SSam Leffler }
5491b6167d2SSam Leffler 
5501b6167d2SSam Leffler /*
5518a1b9b6aSSam Leffler  * Join the specified IBSS/BSS network.  The node is assumed to
5528a1b9b6aSSam Leffler  * be passed in with a held reference.
5538a1b9b6aSSam Leffler  */
55468e8e04eSSam Leffler static int
55568e8e04eSSam Leffler ieee80211_sta_join1(struct ieee80211_node *selbs)
5568a1b9b6aSSam Leffler {
55768e8e04eSSam Leffler 	struct ieee80211com *ic = selbs->ni_ic;
5588a1b9b6aSSam Leffler 	struct ieee80211_node *obss;
55968e8e04eSSam Leffler 	int canreassoc;
5608a1b9b6aSSam Leffler 
5618a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
562acc4f7f5SSam Leffler 		struct ieee80211_node_table *nt;
5638a1b9b6aSSam Leffler 		/*
564acc4f7f5SSam Leffler 		 * Fillin the neighbor table; it will already
5653d073929SSam Leffler 		 * exist if we are simply switching mastership.
566acc4f7f5SSam Leffler 		 * XXX ic_sta always setup so this is unnecessary?
5678a1b9b6aSSam Leffler 		 */
568acc4f7f5SSam Leffler 		nt = &ic->ic_sta;
569acc4f7f5SSam Leffler 		IEEE80211_NODE_LOCK(nt);
570acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
571acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
572acc4f7f5SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
5733d073929SSam Leffler 	}
5741a1e1d21SSam Leffler 
5758a1b9b6aSSam Leffler 	/*
5768a1b9b6aSSam Leffler 	 * Committed to selbs, setup state.
5778a1b9b6aSSam Leffler 	 */
5788a1b9b6aSSam Leffler 	obss = ic->ic_bss;
57968e8e04eSSam Leffler 	/*
58068e8e04eSSam Leffler 	 * Check if old+new node have the same address in which
58168e8e04eSSam Leffler 	 * case we can reassociate when operating in sta mode.
58268e8e04eSSam Leffler 	 */
58368e8e04eSSam Leffler 	canreassoc = (obss != NULL &&
58468e8e04eSSam Leffler 		ic->ic_state == IEEE80211_S_RUN &&
58568e8e04eSSam Leffler 		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
586acc4f7f5SSam Leffler 	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
5871fd2349dSSam Leffler 	if (obss != NULL) {
5881fd2349dSSam Leffler 		copy_bss(selbs, obss);
5898a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
5901fd2349dSSam Leffler 	}
59179edaebfSSam Leffler 
59279edaebfSSam Leffler 	/*
59379edaebfSSam Leffler 	 * Delete unusable rates; we've already checked
59479edaebfSSam Leffler 	 * that the negotiated rate set is acceptable.
59579edaebfSSam Leffler 	 */
59670e28b9aSSam Leffler 	ieee80211_fix_rate(ic->ic_bss, &ic->ic_bss->ni_rates,
59770e28b9aSSam Leffler 		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
59879edaebfSSam Leffler 
5991b6167d2SSam Leffler 	ieee80211_setbsschan(ic, selbs->ni_chan);
6008a1b9b6aSSam Leffler 	/*
6018a1b9b6aSSam Leffler 	 * Set the erp state (mostly the slot time) to deal with
6028a1b9b6aSSam Leffler 	 * the auto-select case; this should be redundant if the
6038a1b9b6aSSam Leffler 	 * mode is locked.
6048a1b9b6aSSam Leffler 	 */
6058a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
6068a1b9b6aSSam Leffler 	ieee80211_wme_initparams(ic);
607acc4f7f5SSam Leffler 
60868e8e04eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA) {
60968e8e04eSSam Leffler 		if (canreassoc) {
61068e8e04eSSam Leffler 			/* Reassociate */
61168e8e04eSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC, 1);
61268e8e04eSSam Leffler 		} else {
61368e8e04eSSam Leffler 			/*
61468e8e04eSSam Leffler 			 * Act as if we received a DEAUTH frame in case we
61568e8e04eSSam Leffler 			 * are invoked from the RUN state.  This will cause
61668e8e04eSSam Leffler 			 * us to try to re-authenticate if we are operating
61768e8e04eSSam Leffler 			 * as a station.
61868e8e04eSSam Leffler 			 */
61968e8e04eSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
62068e8e04eSSam Leffler 				IEEE80211_FC0_SUBTYPE_DEAUTH);
62168e8e04eSSam Leffler 		}
62268e8e04eSSam Leffler 	} else
623acc4f7f5SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
6248a1b9b6aSSam Leffler 	return 1;
6258a1b9b6aSSam Leffler }
6268a1b9b6aSSam Leffler 
62768e8e04eSSam Leffler int
62868e8e04eSSam Leffler ieee80211_sta_join(struct ieee80211com *ic,
62968e8e04eSSam Leffler 	const struct ieee80211_scan_entry *se)
63068e8e04eSSam Leffler {
63168e8e04eSSam Leffler 	struct ieee80211_node *ni;
63268e8e04eSSam Leffler 
63368e8e04eSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, se->se_macaddr);
63468e8e04eSSam Leffler 	if (ni == NULL) {
63568e8e04eSSam Leffler 		/* XXX msg */
63668e8e04eSSam Leffler 		return 0;
63768e8e04eSSam Leffler 	}
63868e8e04eSSam Leffler 	/*
63968e8e04eSSam Leffler 	 * Expand scan state into node's format.
64068e8e04eSSam Leffler 	 * XXX may not need all this stuff
64168e8e04eSSam Leffler 	 */
64268e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
64368e8e04eSSam Leffler 	ni->ni_esslen = se->se_ssid[1];
64468e8e04eSSam Leffler 	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
64568e8e04eSSam Leffler 	ni->ni_rstamp = se->se_rstamp;
64668e8e04eSSam Leffler 	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
64768e8e04eSSam Leffler 	ni->ni_intval = se->se_intval;
64868e8e04eSSam Leffler 	ni->ni_capinfo = se->se_capinfo;
64968e8e04eSSam Leffler 	ni->ni_chan = se->se_chan;
65068e8e04eSSam Leffler 	ni->ni_timoff = se->se_timoff;
65168e8e04eSSam Leffler 	ni->ni_fhdwell = se->se_fhdwell;
65268e8e04eSSam Leffler 	ni->ni_fhindex = se->se_fhindex;
65368e8e04eSSam Leffler 	ni->ni_erp = se->se_erp;
65468e8e04eSSam Leffler 	ni->ni_rssi = se->se_rssi;
65568e8e04eSSam Leffler 	ni->ni_noise = se->se_noise;
656cb42c5bbSSam Leffler 	if (se->se_htcap_ie != NULL) {
6571b6167d2SSam Leffler 		ieee80211_saveie(&ni->ni_htcap_ie, se->se_htcap_ie);
658cb42c5bbSSam Leffler 		ieee80211_parse_htcap(ni, ni->ni_htcap_ie);
659cb42c5bbSSam Leffler 	}
66068e8e04eSSam Leffler 	if (se->se_wpa_ie != NULL)
66168e8e04eSSam Leffler 		ieee80211_saveie(&ni->ni_wpa_ie, se->se_wpa_ie);
66268e8e04eSSam Leffler 	if (se->se_rsn_ie != NULL)
66368e8e04eSSam Leffler 		ieee80211_saveie(&ni->ni_rsn_ie, se->se_rsn_ie);
66468e8e04eSSam Leffler 	if (se->se_wme_ie != NULL)
66568e8e04eSSam Leffler 		ieee80211_saveie(&ni->ni_wme_ie, se->se_wme_ie);
66668e8e04eSSam Leffler 	if (se->se_ath_ie != NULL)
66768e8e04eSSam Leffler 		ieee80211_saveath(ni, se->se_ath_ie);
66868e8e04eSSam Leffler 
66968e8e04eSSam Leffler 	ic->ic_dtim_period = se->se_dtimperiod;
67068e8e04eSSam Leffler 	ic->ic_dtim_count = 0;
67168e8e04eSSam Leffler 
67268e8e04eSSam Leffler 	/* NB: must be after ni_chan is setup */
67368e8e04eSSam Leffler 	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
67468e8e04eSSam Leffler 		IEEE80211_F_DOSORT);
67568e8e04eSSam Leffler 
67668e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
67768e8e04eSSam Leffler }
67868e8e04eSSam Leffler 
6798a1b9b6aSSam Leffler /*
6808a1b9b6aSSam Leffler  * Leave the specified IBSS/BSS network.  The node is assumed to
6818a1b9b6aSSam Leffler  * be passed in with a held reference.
6828a1b9b6aSSam Leffler  */
6838a1b9b6aSSam Leffler void
6848a1b9b6aSSam Leffler ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
6858a1b9b6aSSam Leffler {
6868a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
6878a1b9b6aSSam Leffler 	ieee80211_notify_node_leave(ic, ni);
6888a1b9b6aSSam Leffler }
6898a1b9b6aSSam Leffler 
6901a1e1d21SSam Leffler static struct ieee80211_node *
6918a1b9b6aSSam Leffler node_alloc(struct ieee80211_node_table *nt)
6921a1e1d21SSam Leffler {
693410ca74bSSam Leffler 	struct ieee80211_node *ni;
6948a1b9b6aSSam Leffler 
695410ca74bSSam Leffler 	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
696410ca74bSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
697410ca74bSSam Leffler 	return ni;
6981a1e1d21SSam Leffler }
6991a1e1d21SSam Leffler 
7008a1b9b6aSSam Leffler /*
7018a1b9b6aSSam Leffler  * Reclaim any resources in a node and reset any critical
7028a1b9b6aSSam Leffler  * state.  Typically nodes are free'd immediately after,
7038a1b9b6aSSam Leffler  * but in some cases the storage may be reused so we need
7048a1b9b6aSSam Leffler  * to insure consistent state (should probably fix that).
7058a1b9b6aSSam Leffler  */
7061a1e1d21SSam Leffler static void
7078a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni)
7081a1e1d21SSam Leffler {
7098a1b9b6aSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
7108a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
71168e8e04eSSam Leffler 	int i;
7128a1b9b6aSSam Leffler 
7138a1b9b6aSSam Leffler 	/* NB: preserve ni_table */
7148a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
715e5ac9e91SSam Leffler 		if (ic->ic_opmode != IEEE80211_M_STA)
7168a1b9b6aSSam Leffler 			ic->ic_ps_sta--;
7178a1b9b6aSSam Leffler 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
7188a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
7198a1b9b6aSSam Leffler 		    "[%s] power save mode off, %u sta's in ps mode\n",
7208a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
7218a1b9b6aSSam Leffler 	}
722ebdda46cSSam Leffler 	/*
7231b6167d2SSam Leffler 	 * Cleanup any HT-related state.
7241b6167d2SSam Leffler 	 */
7251b6167d2SSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_HT)
7261b6167d2SSam Leffler 		ieee80211_ht_node_cleanup(ni);
7271b6167d2SSam Leffler 	/*
728ebdda46cSSam Leffler 	 * Clear AREF flag that marks the authorization refcnt bump
729ebdda46cSSam Leffler 	 * has happened.  This is probably not needed as the node
730ebdda46cSSam Leffler 	 * should always be removed from the table so not found but
731ebdda46cSSam Leffler 	 * do it just in case.
732ebdda46cSSam Leffler 	 */
733ebdda46cSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AREF;
7348a1b9b6aSSam Leffler 
7358a1b9b6aSSam Leffler 	/*
7368a1b9b6aSSam Leffler 	 * Drain power save queue and, if needed, clear TIM.
7378a1b9b6aSSam Leffler 	 */
73868e8e04eSSam Leffler 	if (ieee80211_node_saveq_drain(ni) != 0 && ic->ic_set_tim != NULL)
739edfa57d0SSam Leffler 		ic->ic_set_tim(ni, 0);
7408a1b9b6aSSam Leffler 
7418a1b9b6aSSam Leffler 	ni->ni_associd = 0;
7428a1b9b6aSSam Leffler 	if (ni->ni_challenge != NULL) {
74368e8e04eSSam Leffler 		FREE(ni->ni_challenge, M_80211_NODE);
7448a1b9b6aSSam Leffler 		ni->ni_challenge = NULL;
7458a1b9b6aSSam Leffler 	}
7468a1b9b6aSSam Leffler 	/*
7478a1b9b6aSSam Leffler 	 * Preserve SSID, WPA, and WME ie's so the bss node is
7488a1b9b6aSSam Leffler 	 * reusable during a re-auth/re-assoc state transition.
7498a1b9b6aSSam Leffler 	 * If we remove these data they will not be recreated
7508a1b9b6aSSam Leffler 	 * because they come from a probe-response or beacon frame
7518a1b9b6aSSam Leffler 	 * which cannot be expected prior to the association-response.
7528a1b9b6aSSam Leffler 	 * This should not be an issue when operating in other modes
7538a1b9b6aSSam Leffler 	 * as stations leaving always go through a full state transition
7548a1b9b6aSSam Leffler 	 * which will rebuild this state.
7558a1b9b6aSSam Leffler 	 *
7568a1b9b6aSSam Leffler 	 * XXX does this leave us open to inheriting old state?
7578a1b9b6aSSam Leffler 	 */
7588a1b9b6aSSam Leffler 	for (i = 0; i < N(ni->ni_rxfrag); i++)
7598a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[i] != NULL) {
7608a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[i]);
7618a1b9b6aSSam Leffler 			ni->ni_rxfrag[i] = NULL;
7628a1b9b6aSSam Leffler 		}
763c1225b52SSam Leffler 	/*
764c1225b52SSam Leffler 	 * Must be careful here to remove any key map entry w/o a LOR.
765c1225b52SSam Leffler 	 */
766c1225b52SSam Leffler 	ieee80211_node_delucastkey(ni);
7678a1b9b6aSSam Leffler #undef N
7688a1b9b6aSSam Leffler }
7698a1b9b6aSSam Leffler 
7708a1b9b6aSSam Leffler static void
7718a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni)
7728a1b9b6aSSam Leffler {
7738a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
7748a1b9b6aSSam Leffler 
7758a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
7768a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
77768e8e04eSSam Leffler 		FREE(ni->ni_wpa_ie, M_80211_NODE);
77868e8e04eSSam Leffler 	if (ni->ni_rsn_ie != NULL)
77968e8e04eSSam Leffler 		FREE(ni->ni_rsn_ie, M_80211_NODE);
7808a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
78168e8e04eSSam Leffler 		FREE(ni->ni_wme_ie, M_80211_NODE);
78268e8e04eSSam Leffler 	if (ni->ni_ath_ie != NULL)
78368e8e04eSSam Leffler 		FREE(ni->ni_ath_ie, M_80211_NODE);
7848a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DESTROY(ni);
785410ca74bSSam Leffler 	FREE(ni, M_80211_NODE);
7861a1e1d21SSam Leffler }
7871a1e1d21SSam Leffler 
78868e8e04eSSam Leffler static int8_t
7898a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni)
790d1e61976SSam Leffler {
791d1e61976SSam Leffler 	return ni->ni_rssi;
792d1e61976SSam Leffler }
793d1e61976SSam Leffler 
7941a1e1d21SSam Leffler static void
79568e8e04eSSam Leffler node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
79668e8e04eSSam Leffler {
79768e8e04eSSam Leffler 	*rssi = ni->ni_rssi;
79868e8e04eSSam Leffler 	*noise = ni->ni_noise;
79968e8e04eSSam Leffler }
80068e8e04eSSam Leffler 
80168e8e04eSSam Leffler static void
8028a1b9b6aSSam Leffler ieee80211_setup_node(struct ieee80211_node_table *nt,
80368e8e04eSSam Leffler 	struct ieee80211_node *ni, const uint8_t *macaddr)
8041a1e1d21SSam Leffler {
8058a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
8061a1e1d21SSam Leffler 	int hash;
8071a1e1d21SSam Leffler 
8088a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
80949a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
8108a1b9b6aSSam Leffler 		ether_sprintf(macaddr), nt->nt_name);
8118a1b9b6aSSam Leffler 
8121a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
8131a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
8148a1b9b6aSSam Leffler 	ieee80211_node_initref(ni);		/* mark referenced */
8158a1b9b6aSSam Leffler 	ni->ni_chan = IEEE80211_CHAN_ANYC;
8168a1b9b6aSSam Leffler 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
8178a1b9b6aSSam Leffler 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
8188a1b9b6aSSam Leffler 	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
8192045f699SSam Leffler 	ni->ni_inact_reload = nt->nt_inact_init;
8202045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
82168e8e04eSSam Leffler 	ni->ni_ath_defkeyix = 0x7fff;
8228a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
8238a1b9b6aSSam Leffler 
8248a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
8258a1b9b6aSSam Leffler 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
8268a1b9b6aSSam Leffler 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
8278a1b9b6aSSam Leffler 	ni->ni_table = nt;
8288a1b9b6aSSam Leffler 	ni->ni_ic = ic;
8298a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
8301a1e1d21SSam Leffler }
8311a1e1d21SSam Leffler 
8321a1e1d21SSam Leffler struct ieee80211_node *
83368e8e04eSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
8341a1e1d21SSam Leffler {
8358a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
8368a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
8378a1b9b6aSSam Leffler 
8388a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
8391a1e1d21SSam Leffler 	if (ni != NULL)
8408a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
841c64bfa0fSSam Leffler 	else
842c64bfa0fSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
8431a1e1d21SSam Leffler 	return ni;
8441a1e1d21SSam Leffler }
8451a1e1d21SSam Leffler 
84697c973adSSam Leffler /*
84797c973adSSam Leffler  * Craft a temporary node suitable for sending a management frame
84897c973adSSam Leffler  * to the specified station.  We craft only as much state as we
84997c973adSSam Leffler  * need to do the work since the node will be immediately reclaimed
85097c973adSSam Leffler  * once the send completes.
85197c973adSSam Leffler  */
85297c973adSSam Leffler struct ieee80211_node *
85368e8e04eSSam Leffler ieee80211_tmp_node(struct ieee80211com *ic, const uint8_t *macaddr)
85497c973adSSam Leffler {
85597c973adSSam Leffler 	struct ieee80211_node *ni;
85697c973adSSam Leffler 
85797c973adSSam Leffler 	ni = ic->ic_node_alloc(&ic->ic_sta);
85897c973adSSam Leffler 	if (ni != NULL) {
85997c973adSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
86097c973adSSam Leffler 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
86197c973adSSam Leffler 
86297c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
86397c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
86497c973adSSam Leffler 		ieee80211_node_initref(ni);		/* mark referenced */
86597c973adSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
86697c973adSSam Leffler 		/* NB: required by ieee80211_fix_rate */
86768e8e04eSSam Leffler 		ieee80211_node_set_chan(ic, ni);
86897c973adSSam Leffler 		ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
86997c973adSSam Leffler 			IEEE80211_KEYIX_NONE);
87097c973adSSam Leffler 		/* XXX optimize away */
87197c973adSSam Leffler 		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
87297c973adSSam Leffler 
87397c973adSSam Leffler 		ni->ni_table = NULL;		/* NB: pedantic */
87497c973adSSam Leffler 		ni->ni_ic = ic;
87597c973adSSam Leffler 	} else {
87697c973adSSam Leffler 		/* XXX msg */
87797c973adSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
87897c973adSSam Leffler 	}
87997c973adSSam Leffler 	return ni;
88097c973adSSam Leffler }
88197c973adSSam Leffler 
8821a1e1d21SSam Leffler struct ieee80211_node *
88368e8e04eSSam Leffler ieee80211_dup_bss(struct ieee80211_node_table *nt, const uint8_t *macaddr)
8841a1e1d21SSam Leffler {
8858a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
8868a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
8878a1b9b6aSSam Leffler 
8888a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
8891a1e1d21SSam Leffler 	if (ni != NULL) {
8908a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
891694dca64SSam Leffler 		/*
892694dca64SSam Leffler 		 * Inherit from ic_bss.
893694dca64SSam Leffler 		 */
8948a1b9b6aSSam Leffler 		ni->ni_authmode = ic->ic_bss->ni_authmode;
8958a1b9b6aSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
8968a1b9b6aSSam Leffler 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
897694dca64SSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
89868e8e04eSSam Leffler 		ieee80211_node_set_chan(ic, ni);
8998a1b9b6aSSam Leffler 		ni->ni_rsn = ic->ic_bss->ni_rsn;
900694dca64SSam Leffler 	} else
901694dca64SSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
9021a1e1d21SSam Leffler 	return ni;
9031a1e1d21SSam Leffler }
9041a1e1d21SSam Leffler 
905750d6d0cSSam Leffler static struct ieee80211_node *
9068a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9078a1b9b6aSSam Leffler _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
90868e8e04eSSam Leffler 	const uint8_t *macaddr, const char *func, int line)
9098a1b9b6aSSam Leffler #else
9108a1b9b6aSSam Leffler _ieee80211_find_node(struct ieee80211_node_table *nt,
91168e8e04eSSam Leffler 	const uint8_t *macaddr)
9128a1b9b6aSSam Leffler #endif
9131a1e1d21SSam Leffler {
9141a1e1d21SSam Leffler 	struct ieee80211_node *ni;
9151a1e1d21SSam Leffler 	int hash;
9161a1e1d21SSam Leffler 
9178a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
918750d6d0cSSam Leffler 
9191a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
9208a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
9211a1e1d21SSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
9228a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
9238a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9248a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
92549a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
92649a15236SSam Leffler 			    func, line,
92749a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
9288a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
9298a1b9b6aSSam Leffler #endif
930750d6d0cSSam Leffler 			return ni;
9311a1e1d21SSam Leffler 		}
9321a1e1d21SSam Leffler 	}
933750d6d0cSSam Leffler 	return NULL;
934750d6d0cSSam Leffler }
9358a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9368a1b9b6aSSam Leffler #define	_ieee80211_find_node(nt, mac) \
9378a1b9b6aSSam Leffler 	_ieee80211_find_node_debug(nt, mac, func, line)
9388a1b9b6aSSam Leffler #endif
939750d6d0cSSam Leffler 
940750d6d0cSSam Leffler struct ieee80211_node *
9418a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9428a1b9b6aSSam Leffler ieee80211_find_node_debug(struct ieee80211_node_table *nt,
94368e8e04eSSam Leffler 	const uint8_t *macaddr, const char *func, int line)
9448a1b9b6aSSam Leffler #else
94568e8e04eSSam Leffler ieee80211_find_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
9468a1b9b6aSSam Leffler #endif
947750d6d0cSSam Leffler {
948750d6d0cSSam Leffler 	struct ieee80211_node *ni;
949750d6d0cSSam Leffler 
9508a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
9518a1b9b6aSSam Leffler 	ni = _ieee80211_find_node(nt, macaddr);
9528a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
9531a1e1d21SSam Leffler 	return ni;
9541a1e1d21SSam Leffler }
9551a1e1d21SSam Leffler 
9561a1e1d21SSam Leffler /*
9578a1b9b6aSSam Leffler  * Fake up a node; this handles node discovery in adhoc mode.
9588a1b9b6aSSam Leffler  * Note that for the driver's benefit we we treat this like
9598a1b9b6aSSam Leffler  * an association so the driver has an opportunity to setup
9608a1b9b6aSSam Leffler  * it's private state.
9618a1b9b6aSSam Leffler  */
9628a1b9b6aSSam Leffler struct ieee80211_node *
9638a1b9b6aSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
96468e8e04eSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
9658a1b9b6aSSam Leffler {
9668a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9678a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9688a1b9b6aSSam Leffler 
969be425a0fSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
970be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
9718a1b9b6aSSam Leffler 	ni = ieee80211_dup_bss(nt, macaddr);
9728a1b9b6aSSam Leffler 	if (ni != NULL) {
9738a1b9b6aSSam Leffler 		/* XXX no rate negotiation; just dup */
9748a1b9b6aSSam Leffler 		ni->ni_rates = ic->ic_bss->ni_rates;
975736b3dc3SSam Leffler 		if (ic->ic_newassoc != NULL)
976e9962332SSam Leffler 			ic->ic_newassoc(ni, 1);
9778e292e8eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
9788e292e8eSSam Leffler 			/*
97968e8e04eSSam Leffler 			 * In adhoc demo mode there are no management
98068e8e04eSSam Leffler 			 * frames to use to discover neighbor capabilities,
98168e8e04eSSam Leffler 			 * so blindly propagate the local configuration
98268e8e04eSSam Leffler 			 * so we can do interesting things (e.g. use
98368e8e04eSSam Leffler 			 * WME to disable ACK's).
9848e292e8eSSam Leffler 			 */
9858e292e8eSSam Leffler 			if (ic->ic_flags & IEEE80211_F_WME)
9868e292e8eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_QOS;
98768e8e04eSSam Leffler 			if (ic->ic_flags & IEEE80211_F_FF)
98868e8e04eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_FF;
9898e292e8eSSam Leffler 		}
99068e8e04eSSam Leffler 		/* XXX not right for 802.1x/WPA */
99168e8e04eSSam Leffler 		ieee80211_node_authorize(ni);
9928a1b9b6aSSam Leffler 	}
9938a1b9b6aSSam Leffler 	return ni;
9948a1b9b6aSSam Leffler }
9958a1b9b6aSSam Leffler 
996be425a0fSSam Leffler void
997be425a0fSSam Leffler ieee80211_init_neighbor(struct ieee80211_node *ni,
998be425a0fSSam Leffler 	const struct ieee80211_frame *wh,
999be425a0fSSam Leffler 	const struct ieee80211_scanparams *sp)
1000be425a0fSSam Leffler {
1001be425a0fSSam Leffler 	ni->ni_esslen = sp->ssid[1];
1002be425a0fSSam Leffler 	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1003be425a0fSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1004be425a0fSSam Leffler 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1005be425a0fSSam Leffler 	ni->ni_intval = sp->bintval;
1006be425a0fSSam Leffler 	ni->ni_capinfo = sp->capinfo;
1007be425a0fSSam Leffler 	ni->ni_chan = ni->ni_ic->ic_curchan;
1008be425a0fSSam Leffler 	ni->ni_fhdwell = sp->fhdwell;
1009be425a0fSSam Leffler 	ni->ni_fhindex = sp->fhindex;
1010be425a0fSSam Leffler 	ni->ni_erp = sp->erp;
1011be425a0fSSam Leffler 	ni->ni_timoff = sp->timoff;
1012be425a0fSSam Leffler 	if (sp->wme != NULL)
1013be425a0fSSam Leffler 		ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
1014be425a0fSSam Leffler 	if (sp->wpa != NULL)
1015be425a0fSSam Leffler 		ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
101668e8e04eSSam Leffler 	if (sp->rsn != NULL)
101768e8e04eSSam Leffler 		ieee80211_saveie(&ni->ni_rsn_ie, sp->rsn);
101868e8e04eSSam Leffler 	if (sp->ath != NULL)
101968e8e04eSSam Leffler 		ieee80211_saveath(ni, sp->ath);
1020be425a0fSSam Leffler 
1021be425a0fSSam Leffler 	/* NB: must be after ni_chan is setup */
102279edaebfSSam Leffler 	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
102379edaebfSSam Leffler 		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
102479edaebfSSam Leffler 		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1025be425a0fSSam Leffler }
1026be425a0fSSam Leffler 
1027b5c99415SSam Leffler /*
1028b5c99415SSam Leffler  * Do node discovery in adhoc mode on receipt of a beacon
1029b5c99415SSam Leffler  * or probe response frame.  Note that for the driver's
1030b5c99415SSam Leffler  * benefit we we treat this like an association so the
1031b5c99415SSam Leffler  * driver has an opportunity to setup it's private state.
1032b5c99415SSam Leffler  */
1033b5c99415SSam Leffler struct ieee80211_node *
1034b5c99415SSam Leffler ieee80211_add_neighbor(struct ieee80211com *ic,
1035b5c99415SSam Leffler 	const struct ieee80211_frame *wh,
1036b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp)
1037b5c99415SSam Leffler {
1038b5c99415SSam Leffler 	struct ieee80211_node *ni;
1039b5c99415SSam Leffler 
1040be425a0fSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1041be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1042b5c99415SSam Leffler 	ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1043b5c99415SSam Leffler 	if (ni != NULL) {
1044be425a0fSSam Leffler 		ieee80211_init_neighbor(ni, wh, sp);
1045b5c99415SSam Leffler 		if (ic->ic_newassoc != NULL)
1046b5c99415SSam Leffler 			ic->ic_newassoc(ni, 1);
1047b5c99415SSam Leffler 		/* XXX not right for 802.1x/WPA */
1048b5c99415SSam Leffler 		ieee80211_node_authorize(ni);
1049b5c99415SSam Leffler 	}
1050b5c99415SSam Leffler 	return ni;
1051b5c99415SSam Leffler }
1052b5c99415SSam Leffler 
1053c1225b52SSam Leffler #define	IS_CTL(wh) \
1054c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1055c1225b52SSam Leffler #define	IS_PSPOLL(wh) \
1056c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
105768e8e04eSSam Leffler #define	IS_BAR(wh) \
105868e8e04eSSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
105968e8e04eSSam Leffler 
10608a1b9b6aSSam Leffler /*
10618a1b9b6aSSam Leffler  * Locate the node for sender, track state, and then pass the
10628a1b9b6aSSam Leffler  * (referenced) node up to the 802.11 layer for its use.  We
10638a1b9b6aSSam Leffler  * are required to pass some node so we fall back to ic_bss
10648a1b9b6aSSam Leffler  * when this frame is from an unknown sender.  The 802.11 layer
10658a1b9b6aSSam Leffler  * knows this means the sender wasn't in the node table and
10668a1b9b6aSSam Leffler  * acts accordingly.
10678a1b9b6aSSam Leffler  */
10688a1b9b6aSSam Leffler struct ieee80211_node *
10698a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10708a1b9b6aSSam Leffler ieee80211_find_rxnode_debug(struct ieee80211com *ic,
10718a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh, const char *func, int line)
10728a1b9b6aSSam Leffler #else
10738a1b9b6aSSam Leffler ieee80211_find_rxnode(struct ieee80211com *ic,
10748a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh)
10758a1b9b6aSSam Leffler #endif
10768a1b9b6aSSam Leffler {
10778a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
10788a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10798a1b9b6aSSam Leffler 
10808a1b9b6aSSam Leffler 	/* XXX check ic_bss first in station mode */
10818a1b9b6aSSam Leffler 	/* XXX 4-address frames? */
108268e8e04eSSam Leffler 	nt = &ic->ic_sta;
10838a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
108468e8e04eSSam Leffler 	if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
10858a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr1);
10868a1b9b6aSSam Leffler 	else
10878a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr2);
1088c1225b52SSam Leffler 	if (ni == NULL)
1089c1225b52SSam Leffler 		ni = ieee80211_ref_node(ic->ic_bss);
10908a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
10918a1b9b6aSSam Leffler 
1092c1225b52SSam Leffler 	return ni;
1093c1225b52SSam Leffler }
1094c1225b52SSam Leffler 
1095c1225b52SSam Leffler /*
1096c1225b52SSam Leffler  * Like ieee80211_find_rxnode but use the supplied h/w
1097c1225b52SSam Leffler  * key index as a hint to locate the node in the key
1098c1225b52SSam Leffler  * mapping table.  If an entry is present at the key
1099c1225b52SSam Leffler  * index we return it; otherwise do a normal lookup and
1100c1225b52SSam Leffler  * update the mapping table if the station has a unicast
1101c1225b52SSam Leffler  * key assigned to it.
1102c1225b52SSam Leffler  */
1103c1225b52SSam Leffler struct ieee80211_node *
1104c1225b52SSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1105c1225b52SSam Leffler ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1106c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1107c1225b52SSam Leffler 	const char *func, int line)
1108c1225b52SSam Leffler #else
1109c1225b52SSam Leffler ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1110c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1111c1225b52SSam Leffler #endif
1112c1225b52SSam Leffler {
1113c1225b52SSam Leffler 	struct ieee80211_node_table *nt;
1114c1225b52SSam Leffler 	struct ieee80211_node *ni;
1115c1225b52SSam Leffler 
1116c1225b52SSam Leffler 	nt = &ic->ic_sta;
1117c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
1118c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1119c1225b52SSam Leffler 		ni = nt->nt_keyixmap[keyix];
1120c1225b52SSam Leffler 	else
1121c1225b52SSam Leffler 		ni = NULL;
1122c1225b52SSam Leffler 	if (ni == NULL) {
112368e8e04eSSam Leffler 		if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1124c1225b52SSam Leffler 			ni = _ieee80211_find_node(nt, wh->i_addr1);
1125c1225b52SSam Leffler 		else
1126c1225b52SSam Leffler 			ni = _ieee80211_find_node(nt, wh->i_addr2);
1127c1225b52SSam Leffler 		if (ni == NULL)
1128c1225b52SSam Leffler 			ni = ieee80211_ref_node(ic->ic_bss);
1129c1225b52SSam Leffler 		if (nt->nt_keyixmap != NULL) {
1130c1225b52SSam Leffler 			/*
1131c1225b52SSam Leffler 			 * If the station has a unicast key cache slot
1132c1225b52SSam Leffler 			 * assigned update the key->node mapping table.
1133c1225b52SSam Leffler 			 */
1134c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1135c1225b52SSam Leffler 			/* XXX can keyixmap[keyix] != NULL? */
1136c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1137c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == NULL) {
1138c1225b52SSam Leffler 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1139c1225b52SSam Leffler 				    "%s: add key map entry %p<%s> refcnt %d\n",
1140c1225b52SSam Leffler 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1141c1225b52SSam Leffler 				    ieee80211_node_refcnt(ni)+1);
1142c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1143c1225b52SSam Leffler 			}
1144c1225b52SSam Leffler 		}
114568e8e04eSSam Leffler 	} else
1146c1225b52SSam Leffler 		ieee80211_ref_node(ni);
1147c1225b52SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1148c1225b52SSam Leffler 
1149c1225b52SSam Leffler 	return ni;
1150c1225b52SSam Leffler }
115168e8e04eSSam Leffler #undef IS_BAR
11528a1b9b6aSSam Leffler #undef IS_PSPOLL
11538a1b9b6aSSam Leffler #undef IS_CTL
11548a1b9b6aSSam Leffler 
11558a1b9b6aSSam Leffler /*
1156750d6d0cSSam Leffler  * Return a reference to the appropriate node for sending
1157750d6d0cSSam Leffler  * a data frame.  This handles node discovery in adhoc networks.
1158750d6d0cSSam Leffler  */
1159750d6d0cSSam Leffler struct ieee80211_node *
11608a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
116168e8e04eSSam Leffler ieee80211_find_txnode_debug(struct ieee80211com *ic, const uint8_t *macaddr,
11628a1b9b6aSSam Leffler 	const char *func, int line)
11638a1b9b6aSSam Leffler #else
116468e8e04eSSam Leffler ieee80211_find_txnode(struct ieee80211com *ic, const uint8_t *macaddr)
11658a1b9b6aSSam Leffler #endif
1166750d6d0cSSam Leffler {
1167acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1168750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1169750d6d0cSSam Leffler 
1170750d6d0cSSam Leffler 	/*
1171750d6d0cSSam Leffler 	 * The destination address should be in the node table
1172c1225b52SSam Leffler 	 * unless this is a multicast/broadcast frame.  We can
1173c1225b52SSam Leffler 	 * also optimize station mode operation, all frames go
1174c1225b52SSam Leffler 	 * to the bss node.
1175750d6d0cSSam Leffler 	 */
1176750d6d0cSSam Leffler 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
11778a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1178c1225b52SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1179c1225b52SSam Leffler 		ni = ieee80211_ref_node(ic->ic_bss);
1180ee25b8dfSSam Leffler 	else {
11818a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, macaddr);
1182ee25b8dfSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1183ee25b8dfSSam Leffler 		    (ni != NULL && ni->ni_associd == 0)) {
1184ee25b8dfSSam Leffler 			/*
1185ee25b8dfSSam Leffler 			 * Station is not associated; don't permit the
1186ee25b8dfSSam Leffler 			 * data frame to be sent by returning NULL.  This
1187ee25b8dfSSam Leffler 			 * is kinda a kludge but the least intrusive way
1188ee25b8dfSSam Leffler 			 * to add this check into all drivers.
1189ee25b8dfSSam Leffler 			 */
1190ee25b8dfSSam Leffler 			ieee80211_unref_node(&ni);	/* NB: null's ni */
1191ee25b8dfSSam Leffler 		}
1192ee25b8dfSSam Leffler 	}
11938a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
11948a1b9b6aSSam Leffler 
11958a1b9b6aSSam Leffler 	if (ni == NULL) {
11968a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
119790d0d036SSam Leffler 		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
119890d0d036SSam Leffler 			/*
119990d0d036SSam Leffler 			 * In adhoc mode cons up a node for the destination.
120090d0d036SSam Leffler 			 * Note that we need an additional reference for the
120190d0d036SSam Leffler 			 * caller to be consistent with _ieee80211_find_node.
120290d0d036SSam Leffler 			 */
12038a1b9b6aSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
120490d0d036SSam Leffler 			if (ni != NULL)
120590d0d036SSam Leffler 				(void) ieee80211_ref_node(ni);
120690d0d036SSam Leffler 		} else {
12078a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
12088a1b9b6aSSam Leffler 				"[%s] no node, discard frame (%s)\n",
12098a1b9b6aSSam Leffler 				ether_sprintf(macaddr), __func__);
12108a1b9b6aSSam Leffler 			ic->ic_stats.is_tx_nonode++;
1211750d6d0cSSam Leffler 		}
1212750d6d0cSSam Leffler 	}
1213750d6d0cSSam Leffler 	return ni;
1214750d6d0cSSam Leffler }
1215750d6d0cSSam Leffler 
1216750d6d0cSSam Leffler /*
12178a1b9b6aSSam Leffler  * Like find but search based on the ssid too.
12188a1b9b6aSSam Leffler  */
12198a1b9b6aSSam Leffler struct ieee80211_node *
12208a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12218a1b9b6aSSam Leffler ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
122268e8e04eSSam Leffler 	const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid,
12238a1b9b6aSSam Leffler 	const char *func, int line)
12248a1b9b6aSSam Leffler #else
12258a1b9b6aSSam Leffler ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
122668e8e04eSSam Leffler 	const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid)
12278a1b9b6aSSam Leffler #endif
12281a1e1d21SSam Leffler {
1229f02a0bd2SSam Leffler #define	MATCH_SSID(ni, ssid, ssidlen) \
1230f02a0bd2SSam Leffler 	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
123168e8e04eSSam Leffler 	static const uint8_t zeromac[IEEE80211_ADDR_LEN];
12321a1e1d21SSam Leffler 	struct ieee80211_node *ni;
12338a1b9b6aSSam Leffler 	int hash;
12341a1e1d21SSam Leffler 
12358a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1236f02a0bd2SSam Leffler 	/*
1237f02a0bd2SSam Leffler 	 * A mac address that is all zero means match only the ssid;
1238f02a0bd2SSam Leffler 	 * otherwise we must match both.
1239f02a0bd2SSam Leffler 	 */
1240f02a0bd2SSam Leffler 	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1241f02a0bd2SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1242f02a0bd2SSam Leffler 			if (MATCH_SSID(ni, ssid, ssidlen))
1243f02a0bd2SSam Leffler 				break;
1244f02a0bd2SSam Leffler 		}
1245f02a0bd2SSam Leffler 	} else {
1246f02a0bd2SSam Leffler 		hash = IEEE80211_NODE_HASH(macaddr);
12478a1b9b6aSSam Leffler 		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
12488a1b9b6aSSam Leffler 			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1249f02a0bd2SSam Leffler 			    MATCH_SSID(ni, ssid, ssidlen))
1250f02a0bd2SSam Leffler 				break;
1251f02a0bd2SSam Leffler 		}
1252f02a0bd2SSam Leffler 	}
1253f02a0bd2SSam Leffler 	if (ni != NULL) {
12548a1b9b6aSSam Leffler 		ieee80211_ref_node(ni);	/* mark referenced */
125568e8e04eSSam Leffler 		IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1256132142c5SDiomidis Spinellis 		     REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
12578a1b9b6aSSam Leffler 		     ieee80211_node_refcnt(ni));
12588a1b9b6aSSam Leffler 	}
12598a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
12608a1b9b6aSSam Leffler 	return ni;
1261f02a0bd2SSam Leffler #undef MATCH_SSID
12628a1b9b6aSSam Leffler }
12638a1b9b6aSSam Leffler 
12648a1b9b6aSSam Leffler static void
12658a1b9b6aSSam Leffler _ieee80211_free_node(struct ieee80211_node *ni)
12668a1b9b6aSSam Leffler {
12678a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
12688a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
12698a1b9b6aSSam Leffler 
12708a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
127149a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
127249a15236SSam Leffler 		ether_sprintf(ni->ni_macaddr),
12738a1b9b6aSSam Leffler 		nt != NULL ? nt->nt_name : "<gone>");
12748a1b9b6aSSam Leffler 
12758a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
12768a1b9b6aSSam Leffler 	if (nt != NULL) {
12778a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
12788a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
12798a1b9b6aSSam Leffler 	}
12808a1b9b6aSSam Leffler 	ic->ic_node_free(ni);
12818a1b9b6aSSam Leffler }
12828a1b9b6aSSam Leffler 
12838a1b9b6aSSam Leffler void
12848a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12858a1b9b6aSSam Leffler ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
12868a1b9b6aSSam Leffler #else
12878a1b9b6aSSam Leffler ieee80211_free_node(struct ieee80211_node *ni)
12888a1b9b6aSSam Leffler #endif
12898a1b9b6aSSam Leffler {
12908a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
12918a1b9b6aSSam Leffler 
12928a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
129329d368a7SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
129449a15236SSam Leffler 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
12958a1b9b6aSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
12968a1b9b6aSSam Leffler #endif
1297c1225b52SSam Leffler 	if (nt != NULL) {
1298c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
12998a1b9b6aSSam Leffler 		if (ieee80211_node_dectestref(ni)) {
13008a1b9b6aSSam Leffler 			/*
1301c1225b52SSam Leffler 			 * Last reference, reclaim state.
13028a1b9b6aSSam Leffler 			 */
13038a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
1304c1225b52SSam Leffler 		} else if (ieee80211_node_refcnt(ni) == 1 &&
1305c1225b52SSam Leffler 		    nt->nt_keyixmap != NULL) {
1306c1225b52SSam Leffler 			ieee80211_keyix keyix;
1307c1225b52SSam Leffler 			/*
1308c1225b52SSam Leffler 			 * Check for a last reference in the key mapping table.
1309c1225b52SSam Leffler 			 */
1310c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1311c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1312c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == ni) {
1313c1225b52SSam Leffler 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1314c1225b52SSam Leffler 				    "%s: %p<%s> clear key map entry", __func__,
1315c1225b52SSam Leffler 				    ni, ether_sprintf(ni->ni_macaddr));
1316c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = NULL;
1317c1225b52SSam Leffler 				ieee80211_node_decref(ni); /* XXX needed? */
13188a1b9b6aSSam Leffler 				_ieee80211_free_node(ni);
13198a1b9b6aSSam Leffler 			}
13201a1e1d21SSam Leffler 		}
1321c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1322c1225b52SSam Leffler 	} else {
1323c1225b52SSam Leffler 		if (ieee80211_node_dectestref(ni))
1324c1225b52SSam Leffler 			_ieee80211_free_node(ni);
1325c1225b52SSam Leffler 	}
1326c1225b52SSam Leffler }
1327c1225b52SSam Leffler 
1328c1225b52SSam Leffler /*
1329c1225b52SSam Leffler  * Reclaim a unicast key and clear any key cache state.
1330c1225b52SSam Leffler  */
1331c1225b52SSam Leffler int
1332c1225b52SSam Leffler ieee80211_node_delucastkey(struct ieee80211_node *ni)
1333c1225b52SSam Leffler {
1334c1225b52SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1335c1225b52SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1336c1225b52SSam Leffler 	struct ieee80211_node *nikey;
1337c1225b52SSam Leffler 	ieee80211_keyix keyix;
1338c1225b52SSam Leffler 	int isowned, status;
1339c1225b52SSam Leffler 
1340c1225b52SSam Leffler 	/*
1341c1225b52SSam Leffler 	 * NB: We must beware of LOR here; deleting the key
1342c1225b52SSam Leffler 	 * can cause the crypto layer to block traffic updates
1343c1225b52SSam Leffler 	 * which can generate a LOR against the node table lock;
1344c1225b52SSam Leffler 	 * grab it here and stash the key index for our use below.
1345c1225b52SSam Leffler 	 *
1346c1225b52SSam Leffler 	 * Must also beware of recursion on the node table lock.
1347c1225b52SSam Leffler 	 * When called from node_cleanup we may already have
1348c1225b52SSam Leffler 	 * the node table lock held.  Unfortunately there's no
1349c1225b52SSam Leffler 	 * way to separate out this path so we must do this
1350c1225b52SSam Leffler 	 * conditionally.
1351c1225b52SSam Leffler 	 */
1352c1225b52SSam Leffler 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1353c1225b52SSam Leffler 	if (!isowned)
1354c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
1355c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1356c1225b52SSam Leffler 	status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1357c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1358c1225b52SSam Leffler 		nikey = nt->nt_keyixmap[keyix];
1359c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;;
1360c1225b52SSam Leffler 	} else
1361c1225b52SSam Leffler 		nikey = NULL;
1362c1225b52SSam Leffler 	if (!isowned)
1363c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(&ic->ic_sta);
1364c1225b52SSam Leffler 
1365c1225b52SSam Leffler 	if (nikey != NULL) {
1366c1225b52SSam Leffler 		KASSERT(nikey == ni,
1367c1225b52SSam Leffler 			("key map out of sync, ni %p nikey %p", ni, nikey));
1368c1225b52SSam Leffler 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1369c1225b52SSam Leffler 			"%s: delete key map entry %p<%s> refcnt %d\n",
1370c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr),
1371c1225b52SSam Leffler 			ieee80211_node_refcnt(ni)-1);
1372c1225b52SSam Leffler 		ieee80211_free_node(ni);
1373c1225b52SSam Leffler 	}
1374c1225b52SSam Leffler 	return status;
1375c1225b52SSam Leffler }
13761a1e1d21SSam Leffler 
1377303ebc3cSSam Leffler /*
13788a1b9b6aSSam Leffler  * Reclaim a node.  If this is the last reference count then
13798a1b9b6aSSam Leffler  * do the normal free work.  Otherwise remove it from the node
13808a1b9b6aSSam Leffler  * table and mark it gone by clearing the back-reference.
13818a1b9b6aSSam Leffler  */
13828a1b9b6aSSam Leffler static void
13838a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
13848a1b9b6aSSam Leffler {
1385c1225b52SSam Leffler 	ieee80211_keyix keyix;
1386c1225b52SSam Leffler 
1387c1225b52SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
13888a1b9b6aSSam Leffler 
138949a15236SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
139049a15236SSam Leffler 		"%s: remove %p<%s> from %s table, refcnt %d\n",
139149a15236SSam Leffler 		__func__, ni, ether_sprintf(ni->ni_macaddr),
139249a15236SSam Leffler 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1393c1225b52SSam Leffler 	/*
1394c1225b52SSam Leffler 	 * Clear any entry in the unicast key mapping table.
1395c1225b52SSam Leffler 	 * We need to do it here so rx lookups don't find it
1396c1225b52SSam Leffler 	 * in the mapping table even if it's not in the hash
1397c1225b52SSam Leffler 	 * table.  We cannot depend on the mapping table entry
1398c1225b52SSam Leffler 	 * being cleared because the node may not be free'd.
1399c1225b52SSam Leffler 	 */
1400c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1401c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1402c1225b52SSam Leffler 	    nt->nt_keyixmap[keyix] == ni) {
1403c1225b52SSam Leffler 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1404c1225b52SSam Leffler 			"%s: %p<%s> clear key map entry\n",
1405c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr));
1406c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;
1407c1225b52SSam Leffler 		ieee80211_node_decref(ni);	/* NB: don't need free */
1408c1225b52SSam Leffler 	}
14098a1b9b6aSSam Leffler 	if (!ieee80211_node_dectestref(ni)) {
14108a1b9b6aSSam Leffler 		/*
14118a1b9b6aSSam Leffler 		 * Other references are present, just remove the
14128a1b9b6aSSam Leffler 		 * node from the table so it cannot be found.  When
14138a1b9b6aSSam Leffler 		 * the references are dropped storage will be
1414acc4f7f5SSam Leffler 		 * reclaimed.
14158a1b9b6aSSam Leffler 		 */
14168a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
14178a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
14188a1b9b6aSSam Leffler 		ni->ni_table = NULL;		/* clear reference */
14198a1b9b6aSSam Leffler 	} else
14208a1b9b6aSSam Leffler 		_ieee80211_free_node(ni);
14218a1b9b6aSSam Leffler }
14228a1b9b6aSSam Leffler 
14238a1b9b6aSSam Leffler static void
14248a1b9b6aSSam Leffler ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
14258a1b9b6aSSam Leffler {
14268a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
14278a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
14288a1b9b6aSSam Leffler 
14298a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
14308a1b9b6aSSam Leffler 		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
14318a1b9b6aSSam Leffler 
14328a1b9b6aSSam Leffler 	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
14338a1b9b6aSSam Leffler 		if (ni->ni_associd != 0) {
14348a1b9b6aSSam Leffler 			if (ic->ic_auth->ia_node_leave != NULL)
14358a1b9b6aSSam Leffler 				ic->ic_auth->ia_node_leave(ic, ni);
14368a1b9b6aSSam Leffler 			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
14378a1b9b6aSSam Leffler 		}
14388a1b9b6aSSam Leffler 		node_reclaim(nt, ni);
14398a1b9b6aSSam Leffler 	}
14408a1b9b6aSSam Leffler }
14418a1b9b6aSSam Leffler 
14428a1b9b6aSSam Leffler /*
14438a1b9b6aSSam Leffler  * Timeout inactive stations and do related housekeeping.
14448a1b9b6aSSam Leffler  * Note that we cannot hold the node lock while sending a
14458a1b9b6aSSam Leffler  * frame as this would lead to a LOR.  Instead we use a
14468a1b9b6aSSam Leffler  * generation number to mark nodes that we've scanned and
14478a1b9b6aSSam Leffler  * drop the lock and restart a scan if we have to time out
14488a1b9b6aSSam Leffler  * a node.  Since we are single-threaded by virtue of
1449303ebc3cSSam Leffler  * controlling the inactivity timer we can be sure this will
1450303ebc3cSSam Leffler  * process each node only once.
1451303ebc3cSSam Leffler  */
14528a1b9b6aSSam Leffler static void
14538a1b9b6aSSam Leffler ieee80211_timeout_stations(struct ieee80211_node_table *nt)
14541a1e1d21SSam Leffler {
14558a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
1456303ebc3cSSam Leffler 	struct ieee80211_node *ni;
14578a1b9b6aSSam Leffler 	u_int gen;
1458f66d97f6SSam Leffler 	int isadhoc;
14591a1e1d21SSam Leffler 
1460f66d97f6SSam Leffler 	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1461f66d97f6SSam Leffler 		   ic->ic_opmode == IEEE80211_M_AHDEMO);
14628a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
1463a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
1464303ebc3cSSam Leffler restart:
14658a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
14668a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1467303ebc3cSSam Leffler 		if (ni->ni_scangen == gen)	/* previously handled */
1468303ebc3cSSam Leffler 			continue;
1469303ebc3cSSam Leffler 		ni->ni_scangen = gen;
14700a915fadSSam Leffler 		/*
1471ebdda46cSSam Leffler 		 * Ignore entries for which have yet to receive an
1472ebdda46cSSam Leffler 		 * authentication frame.  These are transient and
1473ebdda46cSSam Leffler 		 * will be reclaimed when the last reference to them
1474ebdda46cSSam Leffler 		 * goes away (when frame xmits complete).
1475ebdda46cSSam Leffler 		 */
147668e8e04eSSam Leffler 		if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
147768e8e04eSSam Leffler 		     ic->ic_opmode == IEEE80211_M_STA) &&
147844b666cdSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1479ebdda46cSSam Leffler 			continue;
1480ebdda46cSSam Leffler 		/*
14818a1b9b6aSSam Leffler 		 * Free fragment if not needed anymore
14828a1b9b6aSSam Leffler 		 * (last fragment older than 1s).
14838a1b9b6aSSam Leffler 		 * XXX doesn't belong here
14840a915fadSSam Leffler 		 */
14858a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[0] != NULL &&
14868a1b9b6aSSam Leffler 		    ticks > ni->ni_rxfragstamp + hz) {
14878a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
14888a1b9b6aSSam Leffler 			ni->ni_rxfrag[0] = NULL;
14898a1b9b6aSSam Leffler 		}
1490c066143cSSam Leffler 		if (ni->ni_inact > 0)
1491c066143cSSam Leffler 			ni->ni_inact--;
1492ce647032SSam Leffler 		/*
1493ce647032SSam Leffler 		 * Special case ourself; we may be idle for extended periods
1494ce647032SSam Leffler 		 * of time and regardless reclaiming our state is wrong.
1495ce647032SSam Leffler 		 */
1496ce647032SSam Leffler 		if (ni == ic->ic_bss)
1497ce647032SSam Leffler 			continue;
1498f66d97f6SSam Leffler 		if (ni->ni_associd != 0 || isadhoc) {
14998a1b9b6aSSam Leffler 			/*
150068e8e04eSSam Leffler 			 * Age frames on the power save queue.
15018a1b9b6aSSam Leffler 			 */
150268e8e04eSSam Leffler 			if (ieee80211_node_saveq_age(ni) != 0 &&
150368e8e04eSSam Leffler 			    IEEE80211_NODE_SAVEQ_QLEN(ni) == 0 &&
150468e8e04eSSam Leffler 			    ic->ic_set_tim != NULL)
1505edfa57d0SSam Leffler 				ic->ic_set_tim(ni, 0);
15068a1b9b6aSSam Leffler 			/*
15078a1b9b6aSSam Leffler 			 * Probe the station before time it out.  We
15088a1b9b6aSSam Leffler 			 * send a null data frame which may not be
15098a1b9b6aSSam Leffler 			 * universally supported by drivers (need it
15108a1b9b6aSSam Leffler 			 * for ps-poll support so it should be...).
151168e8e04eSSam Leffler 			 *
151268e8e04eSSam Leffler 			 * XXX don't probe the station unless we've
151368e8e04eSSam Leffler 			 *     received a frame from them (and have
151468e8e04eSSam Leffler 			 *     some idea of the rates they are capable
151568e8e04eSSam Leffler 			 *     of); this will get fixed more properly
151668e8e04eSSam Leffler 			 *     soon with better handling of the rate set.
15178a1b9b6aSSam Leffler 			 */
1518c066143cSSam Leffler 			if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) &&
1519c066143cSSam Leffler 			    (0 < ni->ni_inact &&
1520c066143cSSam Leffler 			     ni->ni_inact <= ic->ic_inact_probe) &&
152168e8e04eSSam Leffler 			    ni->ni_rates.rs_nrates != 0) {
1522f66d97f6SSam Leffler 				IEEE80211_NOTE(ic,
1523f66d97f6SSam Leffler 				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1524f66d97f6SSam Leffler 				    ni, "%s",
1525f66d97f6SSam Leffler 				    "probe station due to inactivity");
152619ad2dd7SSam Leffler 				/*
152719ad2dd7SSam Leffler 				 * Grab a reference before unlocking the table
152819ad2dd7SSam Leffler 				 * so the node cannot be reclaimed before we
152919ad2dd7SSam Leffler 				 * send the frame. ieee80211_send_nulldata
153019ad2dd7SSam Leffler 				 * understands we've done this and reclaims the
153119ad2dd7SSam Leffler 				 * ref for us as needed.
153219ad2dd7SSam Leffler 				 */
153319ad2dd7SSam Leffler 				ieee80211_ref_node(ni);
15348a1b9b6aSSam Leffler 				IEEE80211_NODE_UNLOCK(nt);
1535f62121ceSSam Leffler 				ieee80211_send_nulldata(ni);
15368a1b9b6aSSam Leffler 				/* XXX stat? */
15378a1b9b6aSSam Leffler 				goto restart;
15388a1b9b6aSSam Leffler 			}
15398a1b9b6aSSam Leffler 		}
1540c066143cSSam Leffler 		if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) &&
1541c066143cSSam Leffler 		    ni->ni_inact <= 0) {
1542f66d97f6SSam Leffler 			IEEE80211_NOTE(ic,
1543f66d97f6SSam Leffler 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1544f66d97f6SSam Leffler 			    "station timed out due to inactivity "
1545f66d97f6SSam Leffler 			    "(refcnt %u)", ieee80211_node_refcnt(ni));
15468a1b9b6aSSam Leffler 			/*
15478a1b9b6aSSam Leffler 			 * Send a deauthenticate frame and drop the station.
15488a1b9b6aSSam Leffler 			 * This is somewhat complicated due to reference counts
15498a1b9b6aSSam Leffler 			 * and locking.  At this point a station will typically
15508a1b9b6aSSam Leffler 			 * have a reference count of 1.  ieee80211_node_leave
15518a1b9b6aSSam Leffler 			 * will do a "free" of the node which will drop the
15528a1b9b6aSSam Leffler 			 * reference count.  But in the meantime a reference
15538a1b9b6aSSam Leffler 			 * wil be held by the deauth frame.  The actual reclaim
15548a1b9b6aSSam Leffler 			 * of the node will happen either after the tx is
15558a1b9b6aSSam Leffler 			 * completed or by ieee80211_node_leave.
15568a1b9b6aSSam Leffler 			 *
15578a1b9b6aSSam Leffler 			 * Separately we must drop the node lock before sending
155868e8e04eSSam Leffler 			 * in case the driver takes a lock, as this can result
155968e8e04eSSam Leffler 			 * in a LOR between the node lock and the driver lock.
15608a1b9b6aSSam Leffler 			 */
15615698ab1aSSam Leffler 			ieee80211_ref_node(ni);
15628a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
15638a1b9b6aSSam Leffler 			if (ni->ni_associd != 0) {
15641a1e1d21SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
15651a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
15661a1e1d21SSam Leffler 				    IEEE80211_REASON_AUTH_EXPIRE);
15678a1b9b6aSSam Leffler 			}
15688a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
15695698ab1aSSam Leffler 			ieee80211_free_node(ni);
15701be50176SSam Leffler 			ic->ic_stats.is_node_timeout++;
1571303ebc3cSSam Leffler 			goto restart;
1572303ebc3cSSam Leffler 		}
15731a1e1d21SSam Leffler 	}
15748a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15758a1b9b6aSSam Leffler 
15768a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
157768e8e04eSSam Leffler }
15788a1b9b6aSSam Leffler 
157968e8e04eSSam Leffler void
158068e8e04eSSam Leffler ieee80211_node_timeout(void *arg)
158168e8e04eSSam Leffler {
158268e8e04eSSam Leffler 	struct ieee80211com *ic = arg;
158368e8e04eSSam Leffler 
158468e8e04eSSam Leffler 	ieee80211_scan_timeout(ic);
158568e8e04eSSam Leffler 	ieee80211_timeout_stations(&ic->ic_sta);
158668e8e04eSSam Leffler 
1587b105a069SSam Leffler 	IEEE80211_LOCK(ic);
1588b105a069SSam Leffler 	ieee80211_erp_timeout(ic);
15891b6167d2SSam Leffler 	ieee80211_ht_timeout(ic);
1590b105a069SSam Leffler 	IEEE80211_UNLOCK(ic);
1591b105a069SSam Leffler 
159268e8e04eSSam Leffler 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
159368e8e04eSSam Leffler 		ieee80211_node_timeout, ic);
15941a1e1d21SSam Leffler }
15951a1e1d21SSam Leffler 
15961a1e1d21SSam Leffler void
15978a1b9b6aSSam Leffler ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
15981a1e1d21SSam Leffler {
15991a1e1d21SSam Leffler 	struct ieee80211_node *ni;
16008a1b9b6aSSam Leffler 	u_int gen;
16011a1e1d21SSam Leffler 
16028a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
1603a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
16048a1b9b6aSSam Leffler restart:
16058a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
16068a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
16078a1b9b6aSSam Leffler 		if (ni->ni_scangen != gen) {
16088a1b9b6aSSam Leffler 			ni->ni_scangen = gen;
16098a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
16108a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
16111a1e1d21SSam Leffler 			(*f)(arg, ni);
16128a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
16138a1b9b6aSSam Leffler 			goto restart;
16148a1b9b6aSSam Leffler 		}
16158a1b9b6aSSam Leffler 	}
16168a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
16178a1b9b6aSSam Leffler 
16188a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
16198a1b9b6aSSam Leffler }
16208a1b9b6aSSam Leffler 
16218a1b9b6aSSam Leffler void
16228a1b9b6aSSam Leffler ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
16238a1b9b6aSSam Leffler {
16248a1b9b6aSSam Leffler 	printf("0x%p: mac %s refcnt %d\n", ni,
16258a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
16268a1b9b6aSSam Leffler 	printf("\tscangen %u authmode %u flags 0x%x\n",
16278a1b9b6aSSam Leffler 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
16288a1b9b6aSSam Leffler 	printf("\tassocid 0x%x txpower %u vlan %u\n",
16298a1b9b6aSSam Leffler 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
16308a1b9b6aSSam Leffler 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1631801df4a5SSam Leffler 		ni->ni_txseqs[IEEE80211_NONQOS_TID],
1632801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
1633801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
16348a1b9b6aSSam Leffler 		ni->ni_rxfragstamp);
163568e8e04eSSam Leffler 	printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
163668e8e04eSSam Leffler 		ni->ni_rstamp, ni->ni_rssi, ni->ni_noise,
163768e8e04eSSam Leffler 		ni->ni_intval, ni->ni_capinfo);
16388a1b9b6aSSam Leffler 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
16398a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
16408a1b9b6aSSam Leffler 		ni->ni_esslen, ni->ni_essid,
16418a1b9b6aSSam Leffler 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
16428a1b9b6aSSam Leffler 	printf("\tfails %u inact %u txrate %u\n",
16438a1b9b6aSSam Leffler 		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
164468e8e04eSSam Leffler 	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
164568e8e04eSSam Leffler 		ni->ni_htcap, ni->ni_htparam,
164668e8e04eSSam Leffler 		ni->ni_htctlchan, ni->ni_ht2ndchan);
164768e8e04eSSam Leffler 	printf("\thtopmode %x htstbc %x chw %u\n",
164868e8e04eSSam Leffler 		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
16498a1b9b6aSSam Leffler }
16508a1b9b6aSSam Leffler 
16518a1b9b6aSSam Leffler void
16528a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt)
16538a1b9b6aSSam Leffler {
16548a1b9b6aSSam Leffler 	ieee80211_iterate_nodes(nt,
16558a1b9b6aSSam Leffler 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
16568a1b9b6aSSam Leffler }
16578a1b9b6aSSam Leffler 
1658b105a069SSam Leffler void
1659b105a069SSam Leffler ieee80211_notify_erp(struct ieee80211com *ic)
1660b105a069SSam Leffler {
1661b105a069SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1662b105a069SSam Leffler 		ieee80211_beacon_notify(ic, IEEE80211_BEACON_ERP);
1663b105a069SSam Leffler }
1664b105a069SSam Leffler 
16658a1b9b6aSSam Leffler /*
16668a1b9b6aSSam Leffler  * Handle a station joining an 11g network.
16678a1b9b6aSSam Leffler  */
16688a1b9b6aSSam Leffler static void
16698a1b9b6aSSam Leffler ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
16708a1b9b6aSSam Leffler {
16718a1b9b6aSSam Leffler 
16721b6167d2SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
16731b6167d2SSam Leffler 
16748a1b9b6aSSam Leffler 	/*
16758a1b9b6aSSam Leffler 	 * Station isn't capable of short slot time.  Bump
16768a1b9b6aSSam Leffler 	 * the count of long slot time stations and disable
16778a1b9b6aSSam Leffler 	 * use of short slot time.  Note that the actual switch
16788a1b9b6aSSam Leffler 	 * over to long slot time use may not occur until the
16798a1b9b6aSSam Leffler 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
16808a1b9b6aSSam Leffler 	 */
16818a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
16828a1b9b6aSSam Leffler 		ic->ic_longslotsta++;
1683b105a069SSam Leffler 		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1684b105a069SSam Leffler 		    "station needs long slot time, count %d",
1685b105a069SSam Leffler 		    ic->ic_longslotsta);
16868a1b9b6aSSam Leffler 		/* XXX vap's w/ conflicting needs won't work */
168768e8e04eSSam Leffler 		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
168868e8e04eSSam Leffler 			/*
168968e8e04eSSam Leffler 			 * Don't force slot time when switched to turbo
169068e8e04eSSam Leffler 			 * mode as non-ERP stations won't be present; this
169168e8e04eSSam Leffler 			 * need only be done when on the normal G channel.
169268e8e04eSSam Leffler 			 */
16938a1b9b6aSSam Leffler 			ieee80211_set_shortslottime(ic, 0);
16948a1b9b6aSSam Leffler 		}
169568e8e04eSSam Leffler 	}
16968a1b9b6aSSam Leffler 	/*
16978a1b9b6aSSam Leffler 	 * If the new station is not an ERP station
16988a1b9b6aSSam Leffler 	 * then bump the counter and enable protection
16998a1b9b6aSSam Leffler 	 * if configured.
17008a1b9b6aSSam Leffler 	 */
17018a1b9b6aSSam Leffler 	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
17028a1b9b6aSSam Leffler 		ic->ic_nonerpsta++;
1703b105a069SSam Leffler 		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1704b105a069SSam Leffler 		    "station is !ERP, %d non-ERP stations associated",
1705b105a069SSam Leffler 		    ic->ic_nonerpsta);
17068a1b9b6aSSam Leffler 		/*
17078a1b9b6aSSam Leffler 		 * If station does not support short preamble
17088a1b9b6aSSam Leffler 		 * then we must enable use of Barker preamble.
17098a1b9b6aSSam Leffler 		 */
17108a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
1711b105a069SSam Leffler 			IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1712b105a069SSam Leffler 			    "%s", "station needs long preamble");
17138a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
17148a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
17158a1b9b6aSSam Leffler 		}
1716b105a069SSam Leffler 		/*
1717b105a069SSam Leffler 		 * If protection is configured, enable it.
1718b105a069SSam Leffler 		 */
1719b105a069SSam Leffler 		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1720b105a069SSam Leffler 		    ic->ic_nonerpsta == 1 &&
1721b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
1722b105a069SSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1723b105a069SSam Leffler 			    "%s: enable use of protection\n", __func__);
1724b105a069SSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
1725b105a069SSam Leffler 			ieee80211_notify_erp(ic);
1726b105a069SSam Leffler 		}
17278a1b9b6aSSam Leffler 	} else
17288a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
17298a1b9b6aSSam Leffler }
17308a1b9b6aSSam Leffler 
17318a1b9b6aSSam Leffler void
17328a1b9b6aSSam Leffler ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
17338a1b9b6aSSam Leffler {
17348a1b9b6aSSam Leffler 	int newassoc;
17358a1b9b6aSSam Leffler 
17368a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
173768e8e04eSSam Leffler 		uint16_t aid;
17388a1b9b6aSSam Leffler 
17391b6167d2SSam Leffler 		IEEE80211_LOCK(ic);
17408a1b9b6aSSam Leffler 		/*
17418a1b9b6aSSam Leffler 		 * It would be good to search the bitmap
17428a1b9b6aSSam Leffler 		 * more efficiently, but this will do for now.
17438a1b9b6aSSam Leffler 		 */
17448a1b9b6aSSam Leffler 		for (aid = 1; aid < ic->ic_max_aid; aid++) {
17458a1b9b6aSSam Leffler 			if (!IEEE80211_AID_ISSET(aid,
17468a1b9b6aSSam Leffler 			    ic->ic_aid_bitmap))
17478a1b9b6aSSam Leffler 				break;
17488a1b9b6aSSam Leffler 		}
17498a1b9b6aSSam Leffler 		if (aid >= ic->ic_max_aid) {
17501b6167d2SSam Leffler 			IEEE80211_UNLOCK(ic);
17518a1b9b6aSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
17528a1b9b6aSSam Leffler 			    IEEE80211_REASON_ASSOC_TOOMANY);
17538a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
17548a1b9b6aSSam Leffler 			return;
17558a1b9b6aSSam Leffler 		}
17568a1b9b6aSSam Leffler 		ni->ni_associd = aid | 0xc000;
17571b6167d2SSam Leffler 		ni->ni_jointime = time_uptime;
17588a1b9b6aSSam Leffler 		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
17598a1b9b6aSSam Leffler 		ic->ic_sta_assoc++;
17601b6167d2SSam Leffler 
17611b6167d2SSam Leffler 		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
17621b6167d2SSam Leffler 			ieee80211_ht_node_join(ni);
176368e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
176468e8e04eSSam Leffler 		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
17658a1b9b6aSSam Leffler 			ieee80211_node_join_11g(ic, ni);
17661b6167d2SSam Leffler 		IEEE80211_UNLOCK(ic);
17671b6167d2SSam Leffler 
17681b6167d2SSam Leffler 		newassoc = 1;
17698a1b9b6aSSam Leffler 	} else
17708a1b9b6aSSam Leffler 		newassoc = 0;
17718a1b9b6aSSam Leffler 
1772b105a069SSam Leffler 	IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
1773ce8977dfSSam Leffler 	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s",
1774b8fcf546SSam Leffler 	    IEEE80211_NODE_AID(ni),
1775b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
1776b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
1777b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
177868e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
177968e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_HT ?
178068e8e04eSSam Leffler 		(ni->ni_chw == 20 ? ", HT20" : ", HT40") : "",
17811b6167d2SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
178268e8e04eSSam Leffler 	    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_FF) ?
178368e8e04eSSam Leffler 		", fast-frames" : "",
178468e8e04eSSam Leffler 	    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_TURBOP) ?
178568e8e04eSSam Leffler 		", turbo" : ""
1786b8fcf546SSam Leffler 	);
17878a1b9b6aSSam Leffler 
17888a1b9b6aSSam Leffler 	/* give driver a chance to setup state like ni_txrate */
1789736b3dc3SSam Leffler 	if (ic->ic_newassoc != NULL)
1790e9962332SSam Leffler 		ic->ic_newassoc(ni, newassoc);
17918a1b9b6aSSam Leffler 	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
17928a1b9b6aSSam Leffler 	/* tell the authenticator about new station */
17938a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_join != NULL)
17948a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_join(ic, ni);
1795ce8977dfSSam Leffler 	ieee80211_notify_node_join(ic, ni,
1796ce8977dfSSam Leffler 	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
17978a1b9b6aSSam Leffler }
17988a1b9b6aSSam Leffler 
1799b105a069SSam Leffler static void
1800b105a069SSam Leffler disable_protection(struct ieee80211com *ic)
1801b105a069SSam Leffler {
1802b105a069SSam Leffler 	KASSERT(ic->ic_nonerpsta == 0 &&
1803b105a069SSam Leffler 	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
1804b105a069SSam Leffler 	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
1805b105a069SSam Leffler 	   ic->ic_flags_ext));
1806b105a069SSam Leffler 
1807b105a069SSam Leffler 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
1808b105a069SSam Leffler 	/* XXX verify mode? */
1809b105a069SSam Leffler 	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
1810b105a069SSam Leffler 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1811b105a069SSam Leffler 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1812b105a069SSam Leffler 	}
1813b105a069SSam Leffler 	ieee80211_notify_erp(ic);
1814b105a069SSam Leffler }
1815b105a069SSam Leffler 
18168a1b9b6aSSam Leffler /*
18178a1b9b6aSSam Leffler  * Handle a station leaving an 11g network.
18188a1b9b6aSSam Leffler  */
18198a1b9b6aSSam Leffler static void
18208a1b9b6aSSam Leffler ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
18218a1b9b6aSSam Leffler {
18228a1b9b6aSSam Leffler 
18231b6167d2SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
18241b6167d2SSam Leffler 
182568e8e04eSSam Leffler 	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
182668e8e04eSSam Leffler 	     ("not in 11g, bss %u:0x%x, curmode %u", ic->ic_bsschan->ic_freq,
182768e8e04eSSam Leffler 	      ic->ic_bsschan->ic_flags, ic->ic_curmode));
18288a1b9b6aSSam Leffler 
18298a1b9b6aSSam Leffler 	/*
18308a1b9b6aSSam Leffler 	 * If a long slot station do the slot time bookkeeping.
18318a1b9b6aSSam Leffler 	 */
18328a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
18338a1b9b6aSSam Leffler 		KASSERT(ic->ic_longslotsta > 0,
18348a1b9b6aSSam Leffler 		    ("bogus long slot station count %d", ic->ic_longslotsta));
18358a1b9b6aSSam Leffler 		ic->ic_longslotsta--;
1836b105a069SSam Leffler 		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1837b105a069SSam Leffler 		    "long slot time station leaves, count now %d",
1838b105a069SSam Leffler 		    ic->ic_longslotsta);
18398a1b9b6aSSam Leffler 		if (ic->ic_longslotsta == 0) {
18408a1b9b6aSSam Leffler 			/*
18418a1b9b6aSSam Leffler 			 * Re-enable use of short slot time if supported
18428a1b9b6aSSam Leffler 			 * and not operating in IBSS mode (per spec).
18438a1b9b6aSSam Leffler 			 */
18448a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
18458a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_IBSS) {
18468a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
18478a1b9b6aSSam Leffler 				    "%s: re-enable use of short slot time\n",
18488a1b9b6aSSam Leffler 				    __func__);
18498a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic, 1);
18508a1b9b6aSSam Leffler 			}
18518a1b9b6aSSam Leffler 		}
18528a1b9b6aSSam Leffler 	}
18538a1b9b6aSSam Leffler 	/*
18548a1b9b6aSSam Leffler 	 * If a non-ERP station do the protection-related bookkeeping.
18558a1b9b6aSSam Leffler 	 */
18568a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
18578a1b9b6aSSam Leffler 		KASSERT(ic->ic_nonerpsta > 0,
18588a1b9b6aSSam Leffler 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
18598a1b9b6aSSam Leffler 		ic->ic_nonerpsta--;
1860b105a069SSam Leffler 		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1861b105a069SSam Leffler 		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
1862b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
1863b105a069SSam Leffler 			" (non-ERP sta present)" : "");
1864b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0 &&
1865b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
18668a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
18678a1b9b6aSSam Leffler 				"%s: disable use of protection\n", __func__);
1868b105a069SSam Leffler 			disable_protection(ic);
1869b105a069SSam Leffler 		}
1870b105a069SSam Leffler 	}
1871b105a069SSam Leffler }
1872b105a069SSam Leffler 
1873b105a069SSam Leffler /*
1874b105a069SSam Leffler  * Time out presence of an overlapping bss with non-ERP
1875b105a069SSam Leffler  * stations.  When operating in hostap mode we listen for
1876b105a069SSam Leffler  * beacons from other stations and if we identify a non-ERP
1877b105a069SSam Leffler  * station is present we enable protection.  To identify
1878b105a069SSam Leffler  * when all non-ERP stations are gone we time out this
1879b105a069SSam Leffler  * condition.
1880b105a069SSam Leffler  */
1881b105a069SSam Leffler static void
1882b105a069SSam Leffler ieee80211_erp_timeout(struct ieee80211com *ic)
1883b105a069SSam Leffler {
1884b105a069SSam Leffler 
1885b105a069SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1886b105a069SSam Leffler 
1887b105a069SSam Leffler 	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
1888b105a069SSam Leffler 	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
18898a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1890b105a069SSam Leffler 		    "%s\n", "age out non-ERP sta present on channel");
1891b105a069SSam Leffler 		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
1892b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0)
1893b105a069SSam Leffler 			disable_protection(ic);
18948a1b9b6aSSam Leffler 	}
18958a1b9b6aSSam Leffler }
18968a1b9b6aSSam Leffler 
18978a1b9b6aSSam Leffler /*
18988a1b9b6aSSam Leffler  * Handle bookkeeping for station deauthentication/disassociation
18998a1b9b6aSSam Leffler  * when operating as an ap.
19008a1b9b6aSSam Leffler  */
19018a1b9b6aSSam Leffler void
19028a1b9b6aSSam Leffler ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
19038a1b9b6aSSam Leffler {
190444acc00dSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
19058a1b9b6aSSam Leffler 
1906b105a069SSam Leffler 	IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
1907b105a069SSam Leffler 	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
19088a1b9b6aSSam Leffler 
190968e8e04eSSam Leffler 	KASSERT(ic->ic_opmode != IEEE80211_M_STA,
19108a1b9b6aSSam Leffler 		("unexpected operating mode %u", ic->ic_opmode));
19118a1b9b6aSSam Leffler 	/*
19128a1b9b6aSSam Leffler 	 * If node wasn't previously associated all
19138a1b9b6aSSam Leffler 	 * we need to do is reclaim the reference.
19148a1b9b6aSSam Leffler 	 */
19158a1b9b6aSSam Leffler 	/* XXX ibss mode bypasses 11g and notification */
19168a1b9b6aSSam Leffler 	if (ni->ni_associd == 0)
19178a1b9b6aSSam Leffler 		goto done;
19188a1b9b6aSSam Leffler 	/*
19198a1b9b6aSSam Leffler 	 * Tell the authenticator the station is leaving.
19208a1b9b6aSSam Leffler 	 * Note that we must do this before yanking the
19218a1b9b6aSSam Leffler 	 * association id as the authenticator uses the
19228a1b9b6aSSam Leffler 	 * associd to locate it's state block.
19238a1b9b6aSSam Leffler 	 */
19248a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_leave != NULL)
19258a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_leave(ic, ni);
19261b6167d2SSam Leffler 
19271b6167d2SSam Leffler 	IEEE80211_LOCK(ic);
19288a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
19298a1b9b6aSSam Leffler 	ni->ni_associd = 0;
19308a1b9b6aSSam Leffler 	ic->ic_sta_assoc--;
19318a1b9b6aSSam Leffler 
19321b6167d2SSam Leffler 	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
19331b6167d2SSam Leffler 		ieee80211_ht_node_leave(ni);
193468e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
193568e8e04eSSam Leffler 	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
19368a1b9b6aSSam Leffler 		ieee80211_node_leave_11g(ic, ni);
19371b6167d2SSam Leffler 	IEEE80211_UNLOCK(ic);
19388a1b9b6aSSam Leffler 	/*
19398a1b9b6aSSam Leffler 	 * Cleanup station state.  In particular clear various
19408a1b9b6aSSam Leffler 	 * state that might otherwise be reused if the node
19418a1b9b6aSSam Leffler 	 * is reused before the reference count goes to zero
19428a1b9b6aSSam Leffler 	 * (and memory is reclaimed).
19438a1b9b6aSSam Leffler 	 */
19448a1b9b6aSSam Leffler 	ieee80211_sta_leave(ic, ni);
19458a1b9b6aSSam Leffler done:
194644acc00dSSam Leffler 	/*
194744acc00dSSam Leffler 	 * Remove the node from any table it's recorded in and
194844acc00dSSam Leffler 	 * drop the caller's reference.  Removal from the table
194944acc00dSSam Leffler 	 * is important to insure the node is not reprocessed
195044acc00dSSam Leffler 	 * for inactivity.
195144acc00dSSam Leffler 	 */
195244acc00dSSam Leffler 	if (nt != NULL) {
195344acc00dSSam Leffler 		IEEE80211_NODE_LOCK(nt);
195444acc00dSSam Leffler 		node_reclaim(nt, ni);
195544acc00dSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
195644acc00dSSam Leffler 	} else
19578a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
19588a1b9b6aSSam Leffler }
19598a1b9b6aSSam Leffler 
196068e8e04eSSam Leffler int8_t
19618a1b9b6aSSam Leffler ieee80211_getrssi(struct ieee80211com *ic)
19628a1b9b6aSSam Leffler {
19638a1b9b6aSSam Leffler #define	NZ(x)	((x) == 0 ? 1 : (x))
1964acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
196568e8e04eSSam Leffler 	int rssi_samples;
196668e8e04eSSam Leffler 	int32_t rssi_total;
19678a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
19688a1b9b6aSSam Leffler 
19698a1b9b6aSSam Leffler 	rssi_total = 0;
19708a1b9b6aSSam Leffler 	rssi_samples = 0;
19718a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
19728a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
19738a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
19748a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
19758a1b9b6aSSam Leffler 		/* XXX locking */
1976acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
197768e8e04eSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
197868e8e04eSSam Leffler 			    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS)) {
197968e8e04eSSam Leffler 				int8_t rssi = ic->ic_node_getrssi(ni);
198068e8e04eSSam Leffler 				if (rssi != 0) {
19818a1b9b6aSSam Leffler 					rssi_samples++;
198268e8e04eSSam Leffler 					rssi_total += rssi;
198368e8e04eSSam Leffler 				}
19848a1b9b6aSSam Leffler 			}
19858a1b9b6aSSam Leffler 		break;
19868a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:	/* XXX */
19878a1b9b6aSSam Leffler 	case IEEE80211_M_STA:		/* use stats from associated ap */
19888a1b9b6aSSam Leffler 	default:
19898a1b9b6aSSam Leffler 		if (ic->ic_bss != NULL)
19908a1b9b6aSSam Leffler 			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
19918a1b9b6aSSam Leffler 		rssi_samples = 1;
19928a1b9b6aSSam Leffler 		break;
19938a1b9b6aSSam Leffler 	}
19948a1b9b6aSSam Leffler 	return rssi_total / NZ(rssi_samples);
19958a1b9b6aSSam Leffler #undef NZ
19968a1b9b6aSSam Leffler }
19978a1b9b6aSSam Leffler 
199868e8e04eSSam Leffler void
199968e8e04eSSam Leffler ieee80211_getsignal(struct ieee80211com *ic, int8_t *rssi, int8_t *noise)
20008a1b9b6aSSam Leffler {
20018a1b9b6aSSam Leffler 
200268e8e04eSSam Leffler 	if (ic->ic_bss == NULL)		/* NB: shouldn't happen */
200368e8e04eSSam Leffler 		return;
200468e8e04eSSam Leffler 	ic->ic_node_getsignal(ic->ic_bss, rssi, noise);
200568e8e04eSSam Leffler 	/* for non-station mode return avg'd rssi accounting */
200668e8e04eSSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA)
200768e8e04eSSam Leffler 		*rssi = ieee80211_getrssi(ic);
20088a1b9b6aSSam Leffler }
20098a1b9b6aSSam Leffler 
20108a1b9b6aSSam Leffler /*
20118a1b9b6aSSam Leffler  * Node table support.
20128a1b9b6aSSam Leffler  */
20138a1b9b6aSSam Leffler 
20148a1b9b6aSSam Leffler static void
20158a1b9b6aSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic,
20168a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt,
201768e8e04eSSam Leffler 	const char *name, int inact, int keyixmax)
20188a1b9b6aSSam Leffler {
20198a1b9b6aSSam Leffler 
20208a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
20218a1b9b6aSSam Leffler 		"%s %s table, inact %u\n", __func__, name, inact);
20228a1b9b6aSSam Leffler 
20238a1b9b6aSSam Leffler 	nt->nt_ic = ic;
20248a1b9b6aSSam Leffler 	/* XXX need unit */
20258a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
20268a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
20278a1b9b6aSSam Leffler 	TAILQ_INIT(&nt->nt_node);
20288a1b9b6aSSam Leffler 	nt->nt_name = name;
20298a1b9b6aSSam Leffler 	nt->nt_scangen = 1;
20308a1b9b6aSSam Leffler 	nt->nt_inact_init = inact;
2031c1225b52SSam Leffler 	nt->nt_keyixmax = keyixmax;
2032c1225b52SSam Leffler 	if (nt->nt_keyixmax > 0) {
2033c1225b52SSam Leffler 		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
2034c1225b52SSam Leffler 			keyixmax * sizeof(struct ieee80211_node *),
2035c1225b52SSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
2036c1225b52SSam Leffler 		if (nt->nt_keyixmap == NULL)
2037c1225b52SSam Leffler 			if_printf(ic->ic_ifp,
2038c1225b52SSam Leffler 			    "Cannot allocate key index map with %u entries\n",
2039c1225b52SSam Leffler 			    keyixmax);
2040c1225b52SSam Leffler 	} else
2041c1225b52SSam Leffler 		nt->nt_keyixmap = NULL;
20428a1b9b6aSSam Leffler }
20438a1b9b6aSSam Leffler 
204468e8e04eSSam Leffler static void
20458a1b9b6aSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt)
20468a1b9b6aSSam Leffler {
20478a1b9b6aSSam Leffler 
20488a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
20498a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
20508a1b9b6aSSam Leffler 
20518a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
20528a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
20538a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
20548a1b9b6aSSam Leffler }
20558a1b9b6aSSam Leffler 
20568a1b9b6aSSam Leffler static void
20578a1b9b6aSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
20588a1b9b6aSSam Leffler {
20598a1b9b6aSSam Leffler 
20608a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
20618a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
20628a1b9b6aSSam Leffler 
2063c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
20648a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
2065c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL) {
2066c1225b52SSam Leffler 		/* XXX verify all entries are NULL */
2067c1225b52SSam Leffler 		int i;
2068c1225b52SSam Leffler 		for (i = 0; i < nt->nt_keyixmax; i++)
2069c1225b52SSam Leffler 			if (nt->nt_keyixmap[i] != NULL)
2070c1225b52SSam Leffler 				printf("%s: %s[%u] still active\n", __func__,
2071c1225b52SSam Leffler 					nt->nt_name, i);
2072c1225b52SSam Leffler 		FREE(nt->nt_keyixmap, M_80211_NODE);
2073c1225b52SSam Leffler 		nt->nt_keyixmap = NULL;
2074c1225b52SSam Leffler 	}
20758a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_DESTROY(nt);
20768a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_DESTROY(nt);
20778a1b9b6aSSam Leffler }
2078