xref: /freebsd/sys/net80211/ieee80211_node.c (revision 5d44f8c024aa8586b7975212a0b0f7bbee969312)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
3b032f27cSSam Leffler  * Copyright (c) 2002-2008 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 
30b032f27cSSam Leffler #include "opt_wlan.h"
31b032f27cSSam Leffler 
321a1e1d21SSam Leffler #include <sys/param.h>
331a1e1d21SSam Leffler #include <sys/systm.h>
341a1e1d21SSam Leffler #include <sys/mbuf.h>
351a1e1d21SSam Leffler #include <sys/malloc.h>
361a1e1d21SSam Leffler #include <sys/kernel.h>
371a1e1d21SSam Leffler 
388a1b9b6aSSam Leffler #include <sys/socket.h>
391a1e1d21SSam Leffler 
401a1e1d21SSam Leffler #include <net/if.h>
411a1e1d21SSam Leffler #include <net/if_media.h>
421a1e1d21SSam Leffler #include <net/ethernet.h>
431a1e1d21SSam Leffler 
441a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
45b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
46b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
471a1e1d21SSam Leffler 
481a1e1d21SSam Leffler #include <net/bpf.h>
491a1e1d21SSam Leffler 
507268fa64SSam Leffler /*
517268fa64SSam Leffler  * Association id's are managed with a bit vector.
527268fa64SSam Leffler  */
53b032f27cSSam Leffler #define	IEEE80211_AID_SET(_vap, b) \
54b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
55b032f27cSSam Leffler 		(1 << (IEEE80211_AID(b) % 32)))
56b032f27cSSam Leffler #define	IEEE80211_AID_CLR(_vap, b) \
57b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
58b032f27cSSam Leffler 		~(1 << (IEEE80211_AID(b) % 32)))
59b032f27cSSam Leffler #define	IEEE80211_AID_ISSET(_vap, b) \
60b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
617268fa64SSam Leffler 
62132142c5SDiomidis Spinellis #ifdef IEEE80211_DEBUG_REFCNT
63132142c5SDiomidis Spinellis #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
64132142c5SDiomidis Spinellis #else
65132142c5SDiomidis Spinellis #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
66132142c5SDiomidis Spinellis #endif
67132142c5SDiomidis Spinellis 
6868e8e04eSSam Leffler static int ieee80211_sta_join1(struct ieee80211_node *);
6968e8e04eSSam Leffler 
7038c208f8SSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211vap *,
7138c208f8SSam Leffler 	const uint8_t [IEEE80211_ADDR_LEN]);
728a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *);
738a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *);
74b032f27cSSam Leffler static void node_age(struct ieee80211_node *);
7568e8e04eSSam Leffler static int8_t node_getrssi(const struct ieee80211_node *);
7668e8e04eSSam Leffler static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
77b032f27cSSam Leffler static void node_getmimoinfo(const struct ieee80211_node *,
78b032f27cSSam Leffler 	struct ieee80211_mimo_info *);
791a1e1d21SSam Leffler 
808a1b9b6aSSam Leffler static void _ieee80211_free_node(struct ieee80211_node *);
818a1b9b6aSSam Leffler 
828a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic,
83c1225b52SSam Leffler 	struct ieee80211_node_table *nt, const char *name,
8468e8e04eSSam Leffler 	int inact, int keymaxix);
85b032f27cSSam Leffler static void ieee80211_node_table_reset(struct ieee80211_node_table *,
86b032f27cSSam Leffler 	struct ieee80211vap *);
87b032f27cSSam Leffler static void ieee80211_node_reclaim(struct ieee80211_node *);
888a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
89b105a069SSam Leffler static void ieee80211_erp_timeout(struct ieee80211com *);
901a1e1d21SSam Leffler 
9132346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
92b032f27cSSam Leffler MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
9337c150c4SSam Leffler 
941a1e1d21SSam Leffler void
958a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic)
961a1e1d21SSam Leffler {
97b032f27cSSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
98b032f27cSSam Leffler 		IEEE80211_INACT_INIT, ic->ic_max_keyix);
99b032f27cSSam Leffler 	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
100b032f27cSSam Leffler 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
101b032f27cSSam Leffler 		ieee80211_node_timeout, ic);
1021a1e1d21SSam Leffler 
1038a1b9b6aSSam Leffler 	ic->ic_node_alloc = node_alloc;
1048a1b9b6aSSam Leffler 	ic->ic_node_free = node_free;
1058a1b9b6aSSam Leffler 	ic->ic_node_cleanup = node_cleanup;
106b032f27cSSam Leffler 	ic->ic_node_age = node_age;
107b032f27cSSam Leffler 	ic->ic_node_drain = node_age;		/* NB: same as age */
1088a1b9b6aSSam Leffler 	ic->ic_node_getrssi = node_getrssi;
10968e8e04eSSam Leffler 	ic->ic_node_getsignal = node_getsignal;
110b032f27cSSam Leffler 	ic->ic_node_getmimoinfo = node_getmimoinfo;
1118a1b9b6aSSam Leffler 
112b032f27cSSam Leffler 	/*
113b032f27cSSam Leffler 	 * Set flags to be propagated to all vap's;
114b032f27cSSam Leffler 	 * these define default behaviour/configuration.
115b032f27cSSam Leffler 	 */
116c066143cSSam Leffler 	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
117c1225b52SSam Leffler }
118c1225b52SSam Leffler 
119c1225b52SSam Leffler void
1208a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic)
1211a1e1d21SSam Leffler {
1221a1e1d21SSam Leffler 
123b032f27cSSam Leffler 	callout_drain(&ic->ic_inact);
124acc4f7f5SSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_sta);
125b032f27cSSam Leffler }
126b032f27cSSam Leffler 
127b032f27cSSam Leffler void
128b032f27cSSam Leffler ieee80211_node_vattach(struct ieee80211vap *vap)
129b032f27cSSam Leffler {
130b032f27cSSam Leffler 	/* NB: driver can override */
131b032f27cSSam Leffler 	vap->iv_max_aid = IEEE80211_AID_DEF;
132b032f27cSSam Leffler 
133b032f27cSSam Leffler 	/* default station inactivity timer setings */
134b032f27cSSam Leffler 	vap->iv_inact_init = IEEE80211_INACT_INIT;
135b032f27cSSam Leffler 	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
136b032f27cSSam Leffler 	vap->iv_inact_run = IEEE80211_INACT_RUN;
137b032f27cSSam Leffler 	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
138be1054edSSam Leffler 
139be1054edSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
140be1054edSSam Leffler 	    "%s: init %u auth %u run %u probe %u\n", __func__,
141be1054edSSam Leffler 	    vap->iv_inact_init, vap->iv_inact_auth,
142be1054edSSam Leffler 	    vap->iv_inact_run, vap->iv_inact_probe);
143b032f27cSSam Leffler }
144b032f27cSSam Leffler 
145b032f27cSSam Leffler void
146b032f27cSSam Leffler ieee80211_node_latevattach(struct ieee80211vap *vap)
147b032f27cSSam Leffler {
148b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
149b032f27cSSam Leffler 		/* XXX should we allow max aid to be zero? */
150b032f27cSSam Leffler 		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
151b032f27cSSam Leffler 			vap->iv_max_aid = IEEE80211_AID_MIN;
152b032f27cSSam Leffler 			if_printf(vap->iv_ifp,
153b032f27cSSam Leffler 			    "WARNING: max aid too small, changed to %d\n",
154b032f27cSSam Leffler 			    vap->iv_max_aid);
155b032f27cSSam Leffler 		}
156c5abbba3SDag-Erling Smørgrav 		MALLOC(vap->iv_aid_bitmap, uint32_t *,
157c5abbba3SDag-Erling Smørgrav 			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
158b032f27cSSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
159b032f27cSSam Leffler 		if (vap->iv_aid_bitmap == NULL) {
160b032f27cSSam Leffler 			/* XXX no way to recover */
161b032f27cSSam Leffler 			printf("%s: no memory for AID bitmap, max aid %d!\n",
162b032f27cSSam Leffler 			    __func__, vap->iv_max_aid);
163b032f27cSSam Leffler 			vap->iv_max_aid = 0;
164b032f27cSSam Leffler 		}
165b032f27cSSam Leffler 	}
166b032f27cSSam Leffler 
167b032f27cSSam Leffler 	ieee80211_reset_bss(vap);
168b032f27cSSam Leffler 
169b032f27cSSam Leffler 	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
170b032f27cSSam Leffler }
171b032f27cSSam Leffler 
172b032f27cSSam Leffler void
173b032f27cSSam Leffler ieee80211_node_vdetach(struct ieee80211vap *vap)
174b032f27cSSam Leffler {
175b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
176b032f27cSSam Leffler 
177b032f27cSSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta, vap);
178b032f27cSSam Leffler 	if (vap->iv_bss != NULL) {
179b032f27cSSam Leffler 		ieee80211_free_node(vap->iv_bss);
180b032f27cSSam Leffler 		vap->iv_bss = NULL;
181b032f27cSSam Leffler 	}
182b032f27cSSam Leffler 	if (vap->iv_aid_bitmap != NULL) {
183c5abbba3SDag-Erling Smørgrav 		FREE(vap->iv_aid_bitmap, M_80211_NODE);
184b032f27cSSam Leffler 		vap->iv_aid_bitmap = NULL;
1858a1b9b6aSSam Leffler 	}
1868a1b9b6aSSam Leffler }
1878a1b9b6aSSam Leffler 
1888a1b9b6aSSam Leffler /*
1898a1b9b6aSSam Leffler  * Port authorize/unauthorize interfaces for use by an authenticator.
1908a1b9b6aSSam Leffler  */
1918a1b9b6aSSam Leffler 
1928a1b9b6aSSam Leffler void
193e4918ecdSSam Leffler ieee80211_node_authorize(struct ieee80211_node *ni)
1948a1b9b6aSSam Leffler {
195be1054edSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
196be1054edSSam Leffler 
1978a1b9b6aSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AUTH;
198be1054edSSam Leffler 	ni->ni_inact_reload = vap->iv_inact_run;
199c066143cSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
200be1054edSSam Leffler 
201be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
202be1054edSSam Leffler 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
2038a1b9b6aSSam Leffler }
2048a1b9b6aSSam Leffler 
2058a1b9b6aSSam Leffler void
206e4918ecdSSam Leffler ieee80211_node_unauthorize(struct ieee80211_node *ni)
2078a1b9b6aSSam Leffler {
208be1054edSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
209be1054edSSam Leffler 
2108a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
211be1054edSSam Leffler 	ni->ni_inact_reload = vap->iv_inact_auth;
212c066143cSSam Leffler 	if (ni->ni_inact > ni->ni_inact_reload)
213c066143cSSam Leffler 		ni->ni_inact = ni->ni_inact_reload;
214be1054edSSam Leffler 
215be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
216be1054edSSam Leffler 	    "%s: inact_reload %u inact %u", __func__,
217be1054edSSam Leffler 	    ni->ni_inact_reload, ni->ni_inact);
2188a1b9b6aSSam Leffler }
2198a1b9b6aSSam Leffler 
2208a1b9b6aSSam Leffler /*
22101a03542SSam Leffler  * Fix tx parameters for a node according to ``association state''.
22201a03542SSam Leffler  */
22301a03542SSam Leffler static void
22401a03542SSam Leffler node_setuptxparms(struct ieee80211_node *ni)
22501a03542SSam Leffler {
22601a03542SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
22701a03542SSam Leffler 
22801a03542SSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_HT) {
22901a03542SSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
23001a03542SSam Leffler 			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NA];
23101a03542SSam Leffler 		else
23201a03542SSam Leffler 			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NG];
23301a03542SSam Leffler 	} else {				/* legacy rate handling */
23401a03542SSam Leffler 		if (IEEE80211_IS_CHAN_A(ni->ni_chan))
23501a03542SSam Leffler 			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11A];
23601a03542SSam Leffler 		else if (ni->ni_flags & IEEE80211_NODE_ERP)
23701a03542SSam Leffler 			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11G];
23801a03542SSam Leffler 		else
23901a03542SSam Leffler 			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11B];
24001a03542SSam Leffler 	}
24101a03542SSam Leffler }
24201a03542SSam Leffler 
24301a03542SSam Leffler /*
2448a1b9b6aSSam Leffler  * Set/change the channel.  The rate set is also updated as
2458a1b9b6aSSam Leffler  * to insure a consistent view by drivers.
246b032f27cSSam Leffler  * XXX should be private but hostap needs it to deal with CSA
2478a1b9b6aSSam Leffler  */
248b032f27cSSam Leffler void
249b032f27cSSam Leffler ieee80211_node_set_chan(struct ieee80211_node *ni,
250b032f27cSSam Leffler 	struct ieee80211_channel *chan)
2518a1b9b6aSSam Leffler {
252b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
25301a03542SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
25401a03542SSam Leffler 	enum ieee80211_phymode mode;
25568e8e04eSSam Leffler 
256b032f27cSSam Leffler 	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
257b032f27cSSam Leffler 
2588a1b9b6aSSam Leffler 	ni->ni_chan = chan;
25901a03542SSam Leffler 	mode = ieee80211_chan2mode(chan);
26068e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HT(chan)) {
26168e8e04eSSam Leffler 		/*
26268e8e04eSSam Leffler 		 * XXX Gotta be careful here; the rate set returned by
26368e8e04eSSam Leffler 		 * ieee80211_get_suprates is actually any HT rate
26468e8e04eSSam Leffler 		 * set so blindly copying it will be bad.  We must
26568e8e04eSSam Leffler 		 * install the legacy rate est in ni_rates and the
26668e8e04eSSam Leffler 		 * HT rate set in ni_htrates.
26768e8e04eSSam Leffler 		 */
26868e8e04eSSam Leffler 		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
26901a03542SSam Leffler 		/*
27001a03542SSam Leffler 		 * Setup bss tx parameters based on operating mode.  We
27101a03542SSam Leffler 		 * use legacy rates when operating in a mixed HT+non-HT bss
27201a03542SSam Leffler 		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
27301a03542SSam Leffler 		 */
27401a03542SSam Leffler 		if (mode == IEEE80211_MODE_11NA &&
27501a03542SSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
27601a03542SSam Leffler 			mode = IEEE80211_MODE_11A;
27701a03542SSam Leffler 		else if (mode == IEEE80211_MODE_11NG &&
27801a03542SSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
27901a03542SSam Leffler 			mode = IEEE80211_MODE_11G;
28001a03542SSam Leffler 		if (mode == IEEE80211_MODE_11G &&
28101a03542SSam Leffler 		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
28201a03542SSam Leffler 			mode = IEEE80211_MODE_11B;
28368e8e04eSSam Leffler 	}
28401a03542SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[mode];
28541b3c790SSam Leffler 	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
2861a1e1d21SSam Leffler }
2871a1e1d21SSam Leffler 
288f9cd9174SSam Leffler static __inline void
289f9cd9174SSam Leffler copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
290f9cd9174SSam Leffler {
291f9cd9174SSam Leffler 	/* propagate useful state */
292f9cd9174SSam Leffler 	nbss->ni_authmode = obss->ni_authmode;
293f9cd9174SSam Leffler 	nbss->ni_txpower = obss->ni_txpower;
294f9cd9174SSam Leffler 	nbss->ni_vlan = obss->ni_vlan;
295f9cd9174SSam Leffler 	/* XXX statistics? */
296b032f27cSSam Leffler 	/* XXX legacy WDS bssid? */
297f9cd9174SSam Leffler }
298f9cd9174SSam Leffler 
2991a1e1d21SSam Leffler void
300b032f27cSSam Leffler ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
3011a1e1d21SSam Leffler {
302b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3031a1e1d21SSam Leffler 	struct ieee80211_node *ni;
3048a1b9b6aSSam Leffler 
305b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
306b032f27cSSam Leffler 		"%s: creating ibss on channel %u\n", __func__,
307b032f27cSSam Leffler 		ieee80211_chan2ieee(ic, chan));
3088a1b9b6aSSam Leffler 
309b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
310acc4f7f5SSam Leffler 	if (ni == NULL) {
311acc4f7f5SSam Leffler 		/* XXX recovery? */
3128a1b9b6aSSam Leffler 		return;
3138a1b9b6aSSam Leffler 	}
314b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
315b032f27cSSam Leffler 	ni->ni_esslen = vap->iv_des_ssid[0].len;
316b032f27cSSam Leffler 	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
317b032f27cSSam Leffler 	if (vap->iv_bss != NULL)
318b032f27cSSam Leffler 		copy_bss(ni, vap->iv_bss);
319d365f9c7SSam Leffler 	ni->ni_intval = ic->ic_bintval;
320b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
3211a1e1d21SSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
3221a1e1d21SSam Leffler 	if (ic->ic_phytype == IEEE80211_T_FH) {
3231a1e1d21SSam Leffler 		ni->ni_fhdwell = 200;	/* XXX */
3241a1e1d21SSam Leffler 		ni->ni_fhindex = 1;
3251a1e1d21SSam Leffler 	}
326b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
327b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_SIBSS;
3288a1b9b6aSSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
329b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
330b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
331fe49f061SSam Leffler 		else {
332fe49f061SSam Leffler 			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
333fe49f061SSam Leffler 			/* clear group bit, add local bit */
334fe49f061SSam Leffler 			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
335fe49f061SSam Leffler 		}
336b032f27cSSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
337b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
338b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
33950d8b493SSam Leffler 		else
34050d8b493SSam Leffler 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
3418a1b9b6aSSam Leffler 	}
3428a1b9b6aSSam Leffler 	/*
3438a1b9b6aSSam Leffler 	 * Fix the channel and related attributes.
3448a1b9b6aSSam Leffler 	 */
345b032f27cSSam Leffler 	/* clear DFS CAC state on previous channel */
346b032f27cSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
347b032f27cSSam Leffler 	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
348b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
349b032f27cSSam Leffler 		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
35068e8e04eSSam Leffler 	ic->ic_bsschan = chan;
351b032f27cSSam Leffler 	ieee80211_node_set_chan(ni, chan);
35268e8e04eSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(chan);
3538a1b9b6aSSam Leffler 	/*
354b032f27cSSam Leffler 	 * Do mode-specific setup.
3558a1b9b6aSSam Leffler 	 */
35668e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_FULL(chan)) {
35768e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(chan)) {
35868e8e04eSSam Leffler 			/*
359b032f27cSSam Leffler 			 * Use a mixed 11b/11g basic rate set.
36068e8e04eSSam Leffler 			 */
361b032f27cSSam Leffler 			ieee80211_setbasicrates(&ni->ni_rates,
36268e8e04eSSam Leffler 			    IEEE80211_MODE_11G);
363b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_PUREG) {
364b032f27cSSam Leffler 				/*
365b032f27cSSam Leffler 				 * Also mark OFDM rates basic so 11b
366b032f27cSSam Leffler 				 * stations do not join (WiFi compliance).
367b032f27cSSam Leffler 				 */
368b032f27cSSam Leffler 				ieee80211_addbasicrates(&ni->ni_rates,
369b032f27cSSam Leffler 				    IEEE80211_MODE_11A);
370b032f27cSSam Leffler 			}
37168e8e04eSSam Leffler 		} else if (IEEE80211_IS_CHAN_B(chan)) {
37268e8e04eSSam Leffler 			/*
37368e8e04eSSam Leffler 			 * Force pure 11b rate set.
37468e8e04eSSam Leffler 			 */
375b032f27cSSam Leffler 			ieee80211_setbasicrates(&ni->ni_rates,
37668e8e04eSSam Leffler 				IEEE80211_MODE_11B);
37768e8e04eSSam Leffler 		}
3781a1e1d21SSam Leffler 	}
3791a1e1d21SSam Leffler 
38068e8e04eSSam Leffler 	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
38168e8e04eSSam Leffler }
38268e8e04eSSam Leffler 
38368e8e04eSSam Leffler /*
38468e8e04eSSam Leffler  * Reset bss state on transition to the INIT state.
38568e8e04eSSam Leffler  * Clear any stations from the table (they have been
38668e8e04eSSam Leffler  * deauth'd) and reset the bss node (clears key, rate
38768e8e04eSSam Leffler  * etc. state).
38868e8e04eSSam Leffler  */
3898a1b9b6aSSam Leffler void
390b032f27cSSam Leffler ieee80211_reset_bss(struct ieee80211vap *vap)
391b4c5a90fSSam Leffler {
392b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3938a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *obss;
3948a1b9b6aSSam Leffler 
395b032f27cSSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta, vap);
396b032f27cSSam Leffler 	/* XXX multi-bss: wrong */
39768e8e04eSSam Leffler 	ieee80211_reset_erp(ic);
398acc4f7f5SSam Leffler 
399b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
4008a1b9b6aSSam Leffler 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
401b032f27cSSam Leffler 	obss = vap->iv_bss;
402b032f27cSSam Leffler 	vap->iv_bss = ieee80211_ref_node(ni);
403f9cd9174SSam Leffler 	if (obss != NULL) {
404f9cd9174SSam Leffler 		copy_bss(ni, obss);
405d365f9c7SSam Leffler 		ni->ni_intval = ic->ic_bintval;
4068a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
407b032f27cSSam Leffler 	} else
408b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
409f9cd9174SSam Leffler }
4108a1b9b6aSSam Leffler 
4118a1b9b6aSSam Leffler static int
41268e8e04eSSam Leffler match_ssid(const struct ieee80211_node *ni,
41368e8e04eSSam Leffler 	int nssid, const struct ieee80211_scan_ssid ssids[])
4148a1b9b6aSSam Leffler {
41568e8e04eSSam Leffler 	int i;
41668e8e04eSSam Leffler 
41768e8e04eSSam Leffler 	for (i = 0; i < nssid; i++) {
41868e8e04eSSam Leffler 		if (ni->ni_esslen == ssids[i].len &&
41968e8e04eSSam Leffler 		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
42068e8e04eSSam Leffler 			return 1;
42168e8e04eSSam Leffler 	}
42268e8e04eSSam Leffler 	return 0;
42368e8e04eSSam Leffler }
42468e8e04eSSam Leffler 
42568e8e04eSSam Leffler /*
42668e8e04eSSam Leffler  * Test a node for suitability/compatibility.
42768e8e04eSSam Leffler  */
42868e8e04eSSam Leffler static int
429b032f27cSSam Leffler check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
43068e8e04eSSam Leffler {
431b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
43268e8e04eSSam Leffler         uint8_t rate;
43368e8e04eSSam Leffler 
43468e8e04eSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
43568e8e04eSSam Leffler 		return 0;
436b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
43768e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
43868e8e04eSSam Leffler 			return 0;
43968e8e04eSSam Leffler 	} else {
44068e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
44168e8e04eSSam Leffler 			return 0;
44268e8e04eSSam Leffler 	}
443b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
44468e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
44568e8e04eSSam Leffler 			return 0;
44668e8e04eSSam Leffler 	} else {
44768e8e04eSSam Leffler 		/* XXX does this mean privacy is supported or required? */
44868e8e04eSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
44968e8e04eSSam Leffler 			return 0;
45068e8e04eSSam Leffler 	}
45168e8e04eSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
45268e8e04eSSam Leffler 	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
45368e8e04eSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
45468e8e04eSSam Leffler 		return 0;
455b032f27cSSam Leffler 	if (vap->iv_des_nssid != 0 &&
456b032f27cSSam Leffler 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
45768e8e04eSSam Leffler 		return 0;
458b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
459b032f27cSSam Leffler 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
46068e8e04eSSam Leffler 		return 0;
46168e8e04eSSam Leffler 	return 1;
46268e8e04eSSam Leffler }
46368e8e04eSSam Leffler 
46468e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
46568e8e04eSSam Leffler /*
46668e8e04eSSam Leffler  * Display node suitability/compatibility.
46768e8e04eSSam Leffler  */
46868e8e04eSSam Leffler static void
469b032f27cSSam Leffler check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
47068e8e04eSSam Leffler {
471b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
47268e8e04eSSam Leffler         uint8_t rate;
473b4c5a90fSSam Leffler         int fail;
474b4c5a90fSSam Leffler 
475b4c5a90fSSam Leffler 	fail = 0;
476b4c5a90fSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
477b4c5a90fSSam Leffler 		fail |= 0x01;
478b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
479b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
480b4c5a90fSSam Leffler 			fail |= 0x02;
481b4c5a90fSSam Leffler 	} else {
482b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
483b4c5a90fSSam Leffler 			fail |= 0x02;
484b4c5a90fSSam Leffler 	}
485b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
486b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
487b4c5a90fSSam Leffler 			fail |= 0x04;
488b4c5a90fSSam Leffler 	} else {
489b4c5a90fSSam Leffler 		/* XXX does this mean privacy is supported or required? */
490b4c5a90fSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
491b4c5a90fSSam Leffler 			fail |= 0x04;
492b4c5a90fSSam Leffler 	}
49370e28b9aSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
49479edaebfSSam Leffler 	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
495b4c5a90fSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
496b4c5a90fSSam Leffler 		fail |= 0x08;
497b032f27cSSam Leffler 	if (vap->iv_des_nssid != 0 &&
498b032f27cSSam Leffler 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
499b4c5a90fSSam Leffler 		fail |= 0x10;
500b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
501b032f27cSSam Leffler 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
502b4c5a90fSSam Leffler 		fail |= 0x20;
50368e8e04eSSam Leffler 
50468e8e04eSSam Leffler 	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
50568e8e04eSSam Leffler 	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
50668e8e04eSSam Leffler 	printf(" %3d%c",
50768e8e04eSSam Leffler 	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
508b4c5a90fSSam Leffler 	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
509b4c5a90fSSam Leffler 	    fail & 0x08 ? '!' : ' ');
510b4c5a90fSSam Leffler 	printf(" %4s%c",
511b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
512b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
513b4c5a90fSSam Leffler 	    "????",
514b4c5a90fSSam Leffler 	    fail & 0x02 ? '!' : ' ');
515b4c5a90fSSam Leffler 	printf(" %3s%c ",
51668e8e04eSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
517b4c5a90fSSam Leffler 	    fail & 0x04 ? '!' : ' ');
518b4c5a90fSSam Leffler 	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
519b4c5a90fSSam Leffler 	printf("%s\n", fail & 0x10 ? "!" : "");
520b4c5a90fSSam Leffler }
52168e8e04eSSam Leffler #endif /* IEEE80211_DEBUG */
5228a1b9b6aSSam Leffler 
523750d6d0cSSam Leffler /*
5248a1b9b6aSSam Leffler  * Handle 802.11 ad hoc network merge.  The
5258a1b9b6aSSam Leffler  * convention, set by the Wireless Ethernet Compatibility Alliance
5268a1b9b6aSSam Leffler  * (WECA), is that an 802.11 station will change its BSSID to match
5278a1b9b6aSSam Leffler  * the "oldest" 802.11 ad hoc network, on the same channel, that
5288a1b9b6aSSam Leffler  * has the station's desired SSID.  The "oldest" 802.11 network
5298a1b9b6aSSam Leffler  * sends beacons with the greatest TSF timestamp.
5308a1b9b6aSSam Leffler  *
5318a1b9b6aSSam Leffler  * The caller is assumed to validate TSF's before attempting a merge.
5328a1b9b6aSSam Leffler  *
5338a1b9b6aSSam Leffler  * Return !0 if the BSSID changed, 0 otherwise.
534750d6d0cSSam Leffler  */
5358a1b9b6aSSam Leffler int
536641b4d0bSSam Leffler ieee80211_ibss_merge(struct ieee80211_node *ni)
5378a1b9b6aSSam Leffler {
538b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
539b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
540641b4d0bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
541b032f27cSSam Leffler #endif
5428a1b9b6aSSam Leffler 
543b032f27cSSam Leffler 	if (ni == vap->iv_bss ||
544b032f27cSSam Leffler 	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
5458a1b9b6aSSam Leffler 		/* unchanged, nothing to do */
5468a1b9b6aSSam Leffler 		return 0;
5478a1b9b6aSSam Leffler 	}
548b032f27cSSam Leffler 	if (!check_bss(vap, ni)) {
54968e8e04eSSam Leffler 		/* capabilities mismatch */
550b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
5518a1b9b6aSSam Leffler 		    "%s: merge failed, capabilities mismatch\n", __func__);
55268e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
553b032f27cSSam Leffler 		if (ieee80211_msg_assoc(vap))
554b032f27cSSam Leffler 			check_bss_debug(vap, ni);
55568e8e04eSSam Leffler #endif
556b032f27cSSam Leffler 		vap->iv_stats.is_ibss_capmismatch++;
5578a1b9b6aSSam Leffler 		return 0;
5588a1b9b6aSSam Leffler 	}
559b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
5608a1b9b6aSSam Leffler 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
5618a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
5628a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
5638a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
5648a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
5658a1b9b6aSSam Leffler 	);
56668e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
5678a1b9b6aSSam Leffler }
5688a1b9b6aSSam Leffler 
5698a1b9b6aSSam Leffler /*
570b032f27cSSam Leffler  * Calculate HT channel promotion flags for all vaps.
571b032f27cSSam Leffler  * This assumes ni_chan have been setup for each vap.
572b032f27cSSam Leffler  */
573b032f27cSSam Leffler static int
574b032f27cSSam Leffler gethtadjustflags(struct ieee80211com *ic)
575b032f27cSSam Leffler {
576b032f27cSSam Leffler 	struct ieee80211vap *vap;
577b032f27cSSam Leffler 	int flags;
578b032f27cSSam Leffler 
579b032f27cSSam Leffler 	flags = 0;
580b032f27cSSam Leffler 	/* XXX locking */
581b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
582b032f27cSSam Leffler 		if (vap->iv_state < IEEE80211_S_RUN)
583b032f27cSSam Leffler 			continue;
584b032f27cSSam Leffler 		switch (vap->iv_opmode) {
585b032f27cSSam Leffler 		case IEEE80211_M_WDS:
586b032f27cSSam Leffler 		case IEEE80211_M_STA:
587b032f27cSSam Leffler 		case IEEE80211_M_AHDEMO:
588b032f27cSSam Leffler 		case IEEE80211_M_HOSTAP:
589b032f27cSSam Leffler 		case IEEE80211_M_IBSS:
590b032f27cSSam Leffler 			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
591b032f27cSSam Leffler 			break;
592b032f27cSSam Leffler 		default:
593b032f27cSSam Leffler 			break;
594b032f27cSSam Leffler 		}
595b032f27cSSam Leffler 	}
596b032f27cSSam Leffler 	return flags;
597b032f27cSSam Leffler }
598b032f27cSSam Leffler 
599b032f27cSSam Leffler /*
600b032f27cSSam Leffler  * Check if the current channel needs to change based on whether
6015d44f8c0SSam Leffler  * any vap's are using HT20/HT40.  This is used to sync the state
6025d44f8c0SSam Leffler  * of ic_curchan after a channel width change on a running vap.
6031b6167d2SSam Leffler  */
6041b6167d2SSam Leffler void
605b032f27cSSam Leffler ieee80211_sync_curchan(struct ieee80211com *ic)
6061b6167d2SSam Leffler {
607b032f27cSSam Leffler 	struct ieee80211_channel *c;
608b032f27cSSam Leffler 
609b032f27cSSam Leffler 	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
610b032f27cSSam Leffler 	if (c != ic->ic_curchan) {
611b032f27cSSam Leffler 		ic->ic_curchan = c;
612b032f27cSSam Leffler 		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
613b032f27cSSam Leffler 		ic->ic_set_channel(ic);
614b032f27cSSam Leffler 	}
615b032f27cSSam Leffler }
616b032f27cSSam Leffler 
617b032f27cSSam Leffler /*
618b032f27cSSam Leffler  * Change the current channel.  The request channel may be
619b032f27cSSam Leffler  * promoted if other vap's are operating with HT20/HT40.
620b032f27cSSam Leffler  */
621b032f27cSSam Leffler void
622b032f27cSSam Leffler ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
623b032f27cSSam Leffler {
624b032f27cSSam Leffler 	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
625b032f27cSSam Leffler 		int flags = gethtadjustflags(ic);
626b032f27cSSam Leffler 		/*
627b032f27cSSam Leffler 		 * Check for channel promotion required to support the
628b032f27cSSam Leffler 		 * set of running vap's.  This assumes we are called
629b032f27cSSam Leffler 		 * after ni_chan is setup for each vap.
630b032f27cSSam Leffler 		 */
631b032f27cSSam Leffler 		/* NB: this assumes IEEE80211_FEXT_USEHT40 > IEEE80211_FEXT_HT */
632b032f27cSSam Leffler 		if (flags > ieee80211_htchanflags(c))
633b032f27cSSam Leffler 			c = ieee80211_ht_adjust_channel(ic, c, flags);
634b032f27cSSam Leffler 	}
635b032f27cSSam Leffler 	ic->ic_bsschan = ic->ic_curchan = c;
6361b6167d2SSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
6371b6167d2SSam Leffler 	ic->ic_set_channel(ic);
6381b6167d2SSam Leffler }
6391b6167d2SSam Leffler 
6401b6167d2SSam Leffler /*
6418a1b9b6aSSam Leffler  * Join the specified IBSS/BSS network.  The node is assumed to
6428a1b9b6aSSam Leffler  * be passed in with a held reference.
6438a1b9b6aSSam Leffler  */
64468e8e04eSSam Leffler static int
64568e8e04eSSam Leffler ieee80211_sta_join1(struct ieee80211_node *selbs)
6468a1b9b6aSSam Leffler {
647b032f27cSSam Leffler 	struct ieee80211vap *vap = selbs->ni_vap;
64868e8e04eSSam Leffler 	struct ieee80211com *ic = selbs->ni_ic;
6498a1b9b6aSSam Leffler 	struct ieee80211_node *obss;
65068e8e04eSSam Leffler 	int canreassoc;
6518a1b9b6aSSam Leffler 
6528a1b9b6aSSam Leffler 	/*
6538a1b9b6aSSam Leffler 	 * Committed to selbs, setup state.
6548a1b9b6aSSam Leffler 	 */
655b032f27cSSam Leffler 	obss = vap->iv_bss;
65668e8e04eSSam Leffler 	/*
65768e8e04eSSam Leffler 	 * Check if old+new node have the same address in which
65868e8e04eSSam Leffler 	 * case we can reassociate when operating in sta mode.
65968e8e04eSSam Leffler 	 */
66068e8e04eSSam Leffler 	canreassoc = (obss != NULL &&
661b032f27cSSam Leffler 		vap->iv_state == IEEE80211_S_RUN &&
66268e8e04eSSam Leffler 		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
663b032f27cSSam Leffler 	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
6641fd2349dSSam Leffler 	if (obss != NULL) {
6651fd2349dSSam Leffler 		copy_bss(selbs, obss);
666b032f27cSSam Leffler 		ieee80211_node_reclaim(obss);
667b032f27cSSam Leffler 		obss = NULL;		/* NB: guard against later use */
6681fd2349dSSam Leffler 	}
66979edaebfSSam Leffler 
67079edaebfSSam Leffler 	/*
67179edaebfSSam Leffler 	 * Delete unusable rates; we've already checked
67279edaebfSSam Leffler 	 * that the negotiated rate set is acceptable.
67379edaebfSSam Leffler 	 */
674b032f27cSSam Leffler 	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
67570e28b9aSSam Leffler 		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
67679edaebfSSam Leffler 
677b032f27cSSam Leffler 	ieee80211_setcurchan(ic, selbs->ni_chan);
6788a1b9b6aSSam Leffler 	/*
6798a1b9b6aSSam Leffler 	 * Set the erp state (mostly the slot time) to deal with
6808a1b9b6aSSam Leffler 	 * the auto-select case; this should be redundant if the
6818a1b9b6aSSam Leffler 	 * mode is locked.
6828a1b9b6aSSam Leffler 	 */
6838a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
684b032f27cSSam Leffler 	ieee80211_wme_initparams(vap);
685acc4f7f5SSam Leffler 
686b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
68768e8e04eSSam Leffler 		if (canreassoc) {
68868e8e04eSSam Leffler 			/* Reassociate */
689b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
69068e8e04eSSam Leffler 		} else {
69168e8e04eSSam Leffler 			/*
69268e8e04eSSam Leffler 			 * Act as if we received a DEAUTH frame in case we
69368e8e04eSSam Leffler 			 * are invoked from the RUN state.  This will cause
69468e8e04eSSam Leffler 			 * us to try to re-authenticate if we are operating
69568e8e04eSSam Leffler 			 * as a station.
69668e8e04eSSam Leffler 			 */
697b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_AUTH,
69868e8e04eSSam Leffler 				IEEE80211_FC0_SUBTYPE_DEAUTH);
69968e8e04eSSam Leffler 		}
70068e8e04eSSam Leffler 	} else
701b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
7028a1b9b6aSSam Leffler 	return 1;
7038a1b9b6aSSam Leffler }
7048a1b9b6aSSam Leffler 
70568e8e04eSSam Leffler int
70610959256SSam Leffler ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
70768e8e04eSSam Leffler 	const struct ieee80211_scan_entry *se)
70868e8e04eSSam Leffler {
709b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
71068e8e04eSSam Leffler 	struct ieee80211_node *ni;
71168e8e04eSSam Leffler 
712b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
71368e8e04eSSam Leffler 	if (ni == NULL) {
71468e8e04eSSam Leffler 		/* XXX msg */
71568e8e04eSSam Leffler 		return 0;
71668e8e04eSSam Leffler 	}
71768e8e04eSSam Leffler 	/*
71868e8e04eSSam Leffler 	 * Expand scan state into node's format.
71968e8e04eSSam Leffler 	 * XXX may not need all this stuff
72068e8e04eSSam Leffler 	 */
72168e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
72268e8e04eSSam Leffler 	ni->ni_esslen = se->se_ssid[1];
72368e8e04eSSam Leffler 	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
72468e8e04eSSam Leffler 	ni->ni_rstamp = se->se_rstamp;
72568e8e04eSSam Leffler 	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
72668e8e04eSSam Leffler 	ni->ni_intval = se->se_intval;
72768e8e04eSSam Leffler 	ni->ni_capinfo = se->se_capinfo;
72810959256SSam Leffler 	ni->ni_chan = chan;
72968e8e04eSSam Leffler 	ni->ni_timoff = se->se_timoff;
73068e8e04eSSam Leffler 	ni->ni_fhdwell = se->se_fhdwell;
73168e8e04eSSam Leffler 	ni->ni_fhindex = se->se_fhindex;
73268e8e04eSSam Leffler 	ni->ni_erp = se->se_erp;
733b032f27cSSam Leffler 	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
73468e8e04eSSam Leffler 	ni->ni_noise = se->se_noise;
73568e8e04eSSam Leffler 
736b032f27cSSam Leffler 	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
737b032f27cSSam Leffler 		ieee80211_ies_expand(&ni->ni_ies);
738b032f27cSSam Leffler 		if (ni->ni_ies.ath_ie != NULL)
739b032f27cSSam Leffler 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
740b032f27cSSam Leffler 		if (ni->ni_ies.htcap_ie != NULL)
741b032f27cSSam Leffler 			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
742b032f27cSSam Leffler 		if (ni->ni_ies.htinfo_ie != NULL)
743b032f27cSSam Leffler 			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
744b032f27cSSam Leffler 	}
745b032f27cSSam Leffler 
746b032f27cSSam Leffler 	vap->iv_dtim_period = se->se_dtimperiod;
747b032f27cSSam Leffler 	vap->iv_dtim_count = 0;
74868e8e04eSSam Leffler 
74968e8e04eSSam Leffler 	/* NB: must be after ni_chan is setup */
75068e8e04eSSam Leffler 	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
75168e8e04eSSam Leffler 		IEEE80211_F_DOSORT);
752dfcd1f4dSSam Leffler 	if (ieee80211_iserp_rateset(&ni->ni_rates))
753dfcd1f4dSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
754dfcd1f4dSSam Leffler 	node_setuptxparms(ni);
75568e8e04eSSam Leffler 
75668e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
75768e8e04eSSam Leffler }
75868e8e04eSSam Leffler 
7598a1b9b6aSSam Leffler /*
7608a1b9b6aSSam Leffler  * Leave the specified IBSS/BSS network.  The node is assumed to
7618a1b9b6aSSam Leffler  * be passed in with a held reference.
7628a1b9b6aSSam Leffler  */
7638a1b9b6aSSam Leffler void
764b032f27cSSam Leffler ieee80211_sta_leave(struct ieee80211_node *ni)
7658a1b9b6aSSam Leffler {
766b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
767b032f27cSSam Leffler 
7688a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
769b032f27cSSam Leffler 	ieee80211_notify_node_leave(ni);
770b032f27cSSam Leffler }
771b032f27cSSam Leffler 
772b032f27cSSam Leffler /*
773b032f27cSSam Leffler  * Send a deauthenticate frame and drop the station.
774b032f27cSSam Leffler  */
775b032f27cSSam Leffler void
776b032f27cSSam Leffler ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
777b032f27cSSam Leffler {
778b032f27cSSam Leffler 	/* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
779b032f27cSSam Leffler 	ieee80211_ref_node(ni);
780b032f27cSSam Leffler 	if (ni->ni_associd != 0)
781b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
782b032f27cSSam Leffler 	ieee80211_node_leave(ni);
783b032f27cSSam Leffler 	ieee80211_free_node(ni);
7848a1b9b6aSSam Leffler }
7858a1b9b6aSSam Leffler 
7861a1e1d21SSam Leffler static struct ieee80211_node *
78738c208f8SSam Leffler node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
7881a1e1d21SSam Leffler {
789410ca74bSSam Leffler 	struct ieee80211_node *ni;
7908a1b9b6aSSam Leffler 
791c5abbba3SDag-Erling Smørgrav 	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
792410ca74bSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
793410ca74bSSam Leffler 	return ni;
7941a1e1d21SSam Leffler }
7951a1e1d21SSam Leffler 
7968a1b9b6aSSam Leffler /*
797b032f27cSSam Leffler  * Initialize an ie blob with the specified data.  If previous
798b032f27cSSam Leffler  * data exists re-use the data block.  As a side effect we clear
799b032f27cSSam Leffler  * all references to specific ie's; the caller is required to
800b032f27cSSam Leffler  * recalculate them.
801b032f27cSSam Leffler  */
802b032f27cSSam Leffler int
803b032f27cSSam Leffler ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
804b032f27cSSam Leffler {
805b032f27cSSam Leffler 	/* NB: assumes data+len are the last fields */
806b032f27cSSam Leffler 	memset(ies, 0, offsetof(struct ieee80211_ies, data));
807b032f27cSSam Leffler 	if (ies->data != NULL && ies->len != len) {
808b032f27cSSam Leffler 		/* data size changed */
809c5abbba3SDag-Erling Smørgrav 		FREE(ies->data, M_80211_NODE_IE);
810b032f27cSSam Leffler 		ies->data = NULL;
811b032f27cSSam Leffler 	}
812b032f27cSSam Leffler 	if (ies->data == NULL) {
813c5abbba3SDag-Erling Smørgrav 		MALLOC(ies->data, uint8_t *, len, M_80211_NODE_IE, M_NOWAIT);
814b032f27cSSam Leffler 		if (ies->data == NULL) {
815b032f27cSSam Leffler 			ies->len = 0;
816b032f27cSSam Leffler 			/* NB: pointers have already been zero'd above */
817b032f27cSSam Leffler 			return 0;
818b032f27cSSam Leffler 		}
819b032f27cSSam Leffler 	}
820b032f27cSSam Leffler 	memcpy(ies->data, data, len);
821b032f27cSSam Leffler 	ies->len = len;
822b032f27cSSam Leffler 	return 1;
823b032f27cSSam Leffler }
824b032f27cSSam Leffler 
825b032f27cSSam Leffler /*
826b032f27cSSam Leffler  * Reclaim storage for an ie blob.
827b032f27cSSam Leffler  */
828b032f27cSSam Leffler void
829b032f27cSSam Leffler ieee80211_ies_cleanup(struct ieee80211_ies *ies)
830b032f27cSSam Leffler {
831b032f27cSSam Leffler 	if (ies->data != NULL)
832c5abbba3SDag-Erling Smørgrav 		FREE(ies->data, M_80211_NODE_IE);
833b032f27cSSam Leffler }
834b032f27cSSam Leffler 
835b032f27cSSam Leffler /*
836b032f27cSSam Leffler  * Expand an ie blob data contents and to fillin individual
837b032f27cSSam Leffler  * ie pointers.  The data blob is assumed to be well-formed;
838b032f27cSSam Leffler  * we don't do any validity checking of ie lengths.
839b032f27cSSam Leffler  */
840b032f27cSSam Leffler void
841b032f27cSSam Leffler ieee80211_ies_expand(struct ieee80211_ies *ies)
842b032f27cSSam Leffler {
843b032f27cSSam Leffler 	uint8_t *ie;
844b032f27cSSam Leffler 	int ielen;
845b032f27cSSam Leffler 
846b032f27cSSam Leffler 	ie = ies->data;
847b032f27cSSam Leffler 	ielen = ies->len;
848b032f27cSSam Leffler 	while (ielen > 0) {
849b032f27cSSam Leffler 		switch (ie[0]) {
850b032f27cSSam Leffler 		case IEEE80211_ELEMID_VENDOR:
851b032f27cSSam Leffler 			if (iswpaoui(ie))
852b032f27cSSam Leffler 				ies->wpa_ie = ie;
853b032f27cSSam Leffler 			else if (iswmeoui(ie))
854b032f27cSSam Leffler 				ies->wme_ie = ie;
855b032f27cSSam Leffler 			else if (isatherosoui(ie))
856b032f27cSSam Leffler 				ies->ath_ie = ie;
857b032f27cSSam Leffler 			break;
858b032f27cSSam Leffler 		case IEEE80211_ELEMID_RSN:
859b032f27cSSam Leffler 			ies->rsn_ie = ie;
860b032f27cSSam Leffler 			break;
861b032f27cSSam Leffler 		case IEEE80211_ELEMID_HTCAP:
862b032f27cSSam Leffler 			ies->htcap_ie = ie;
863b032f27cSSam Leffler 			break;
864b032f27cSSam Leffler 		}
865b032f27cSSam Leffler 		ielen -= 2 + ie[1];
866b032f27cSSam Leffler 		ie += 2 + ie[1];
867b032f27cSSam Leffler 	}
868b032f27cSSam Leffler }
869b032f27cSSam Leffler 
870b032f27cSSam Leffler /*
8718a1b9b6aSSam Leffler  * Reclaim any resources in a node and reset any critical
8728a1b9b6aSSam Leffler  * state.  Typically nodes are free'd immediately after,
8738a1b9b6aSSam Leffler  * but in some cases the storage may be reused so we need
8748a1b9b6aSSam Leffler  * to insure consistent state (should probably fix that).
8758a1b9b6aSSam Leffler  */
8761a1e1d21SSam Leffler static void
8778a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni)
8781a1e1d21SSam Leffler {
8798a1b9b6aSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
880b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
88168e8e04eSSam Leffler 	int i;
8828a1b9b6aSSam Leffler 
8838a1b9b6aSSam Leffler 	/* NB: preserve ni_table */
8848a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
885b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_STA)
886b032f27cSSam Leffler 			vap->iv_ps_sta--;
8878a1b9b6aSSam Leffler 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
888b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
889b032f27cSSam Leffler 		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
8908a1b9b6aSSam Leffler 	}
891ebdda46cSSam Leffler 	/*
8921b6167d2SSam Leffler 	 * Cleanup any HT-related state.
8931b6167d2SSam Leffler 	 */
8941b6167d2SSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_HT)
8951b6167d2SSam Leffler 		ieee80211_ht_node_cleanup(ni);
8961b6167d2SSam Leffler 	/*
897ebdda46cSSam Leffler 	 * Clear AREF flag that marks the authorization refcnt bump
898ebdda46cSSam Leffler 	 * has happened.  This is probably not needed as the node
899ebdda46cSSam Leffler 	 * should always be removed from the table so not found but
900ebdda46cSSam Leffler 	 * do it just in case.
901ebdda46cSSam Leffler 	 */
902ebdda46cSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AREF;
9038a1b9b6aSSam Leffler 
9048a1b9b6aSSam Leffler 	/*
9058a1b9b6aSSam Leffler 	 * Drain power save queue and, if needed, clear TIM.
9068a1b9b6aSSam Leffler 	 */
90763092fceSSam Leffler 	if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
908b032f27cSSam Leffler 		vap->iv_set_tim(ni, 0);
9098a1b9b6aSSam Leffler 
9108a1b9b6aSSam Leffler 	ni->ni_associd = 0;
9118a1b9b6aSSam Leffler 	if (ni->ni_challenge != NULL) {
912c5abbba3SDag-Erling Smørgrav 		FREE(ni->ni_challenge, M_80211_NODE);
9138a1b9b6aSSam Leffler 		ni->ni_challenge = NULL;
9148a1b9b6aSSam Leffler 	}
9158a1b9b6aSSam Leffler 	/*
9168a1b9b6aSSam Leffler 	 * Preserve SSID, WPA, and WME ie's so the bss node is
9178a1b9b6aSSam Leffler 	 * reusable during a re-auth/re-assoc state transition.
9188a1b9b6aSSam Leffler 	 * If we remove these data they will not be recreated
9198a1b9b6aSSam Leffler 	 * because they come from a probe-response or beacon frame
9208a1b9b6aSSam Leffler 	 * which cannot be expected prior to the association-response.
9218a1b9b6aSSam Leffler 	 * This should not be an issue when operating in other modes
9228a1b9b6aSSam Leffler 	 * as stations leaving always go through a full state transition
9238a1b9b6aSSam Leffler 	 * which will rebuild this state.
9248a1b9b6aSSam Leffler 	 *
9258a1b9b6aSSam Leffler 	 * XXX does this leave us open to inheriting old state?
9268a1b9b6aSSam Leffler 	 */
9278a1b9b6aSSam Leffler 	for (i = 0; i < N(ni->ni_rxfrag); i++)
9288a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[i] != NULL) {
9298a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[i]);
9308a1b9b6aSSam Leffler 			ni->ni_rxfrag[i] = NULL;
9318a1b9b6aSSam Leffler 		}
932c1225b52SSam Leffler 	/*
933c1225b52SSam Leffler 	 * Must be careful here to remove any key map entry w/o a LOR.
934c1225b52SSam Leffler 	 */
935c1225b52SSam Leffler 	ieee80211_node_delucastkey(ni);
9368a1b9b6aSSam Leffler #undef N
9378a1b9b6aSSam Leffler }
9388a1b9b6aSSam Leffler 
9398a1b9b6aSSam Leffler static void
9408a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni)
9418a1b9b6aSSam Leffler {
9428a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
9438a1b9b6aSSam Leffler 
9448a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
945b032f27cSSam Leffler 	ieee80211_ies_cleanup(&ni->ni_ies);
94663092fceSSam Leffler 	ieee80211_psq_cleanup(&ni->ni_psq);
947b032f27cSSam Leffler 	IEEE80211_NODE_WDSQ_DESTROY(ni);
948c5abbba3SDag-Erling Smørgrav 	FREE(ni, M_80211_NODE);
9491a1e1d21SSam Leffler }
9501a1e1d21SSam Leffler 
951b032f27cSSam Leffler static void
952b032f27cSSam Leffler node_age(struct ieee80211_node *ni)
953b032f27cSSam Leffler {
954b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
9555d44f8c0SSam Leffler 
9565d44f8c0SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
9575d44f8c0SSam Leffler 
958b032f27cSSam Leffler 	/*
959b032f27cSSam Leffler 	 * Age frames on the power save queue.
960b032f27cSSam Leffler 	 */
96163092fceSSam Leffler 	if (ieee80211_node_psq_age(ni) != 0 &&
96263092fceSSam Leffler 	    ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
963b032f27cSSam Leffler 		vap->iv_set_tim(ni, 0);
964b032f27cSSam Leffler 	/*
965b032f27cSSam Leffler 	 * Age frames on the wds pending queue.
966b032f27cSSam Leffler 	 */
967b032f27cSSam Leffler 	if (IEEE80211_NODE_WDSQ_QLEN(ni) != 0)
968b032f27cSSam Leffler 		ieee80211_node_wdsq_age(ni);
969b032f27cSSam Leffler 	/*
970b032f27cSSam Leffler 	 * Age out HT resources (e.g. frames on the
971b032f27cSSam Leffler 	 * A-MPDU reorder queues).
972b032f27cSSam Leffler 	 */
973b032f27cSSam Leffler 	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
974b032f27cSSam Leffler 		ieee80211_ht_node_age(ni);
975b032f27cSSam Leffler }
976b032f27cSSam Leffler 
97768e8e04eSSam Leffler static int8_t
9788a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni)
979d1e61976SSam Leffler {
980b032f27cSSam Leffler 	uint32_t avgrssi = ni->ni_avgrssi;
981b032f27cSSam Leffler 	int32_t rssi;
982b032f27cSSam Leffler 
983b032f27cSSam Leffler 	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
984b032f27cSSam Leffler 		return 0;
985b032f27cSSam Leffler 	rssi = IEEE80211_RSSI_GET(avgrssi);
986b032f27cSSam Leffler 	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
987d1e61976SSam Leffler }
988d1e61976SSam Leffler 
9891a1e1d21SSam Leffler static void
99068e8e04eSSam Leffler node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
99168e8e04eSSam Leffler {
992b032f27cSSam Leffler 	*rssi = node_getrssi(ni);
99368e8e04eSSam Leffler 	*noise = ni->ni_noise;
99468e8e04eSSam Leffler }
99568e8e04eSSam Leffler 
99668e8e04eSSam Leffler static void
997b032f27cSSam Leffler node_getmimoinfo(const struct ieee80211_node *ni,
998b032f27cSSam Leffler 	struct ieee80211_mimo_info *info)
999b032f27cSSam Leffler {
1000b032f27cSSam Leffler 	/* XXX zero data? */
1001b032f27cSSam Leffler }
1002b032f27cSSam Leffler 
1003b032f27cSSam Leffler struct ieee80211_node *
1004b032f27cSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt,
1005b032f27cSSam Leffler 	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
10061a1e1d21SSam Leffler {
10078a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
1008b032f27cSSam Leffler 	struct ieee80211_node *ni;
10091a1e1d21SSam Leffler 	int hash;
10101a1e1d21SSam Leffler 
101138c208f8SSam Leffler 	ni = ic->ic_node_alloc(vap, macaddr);
1012b032f27cSSam Leffler 	if (ni == NULL) {
1013b032f27cSSam Leffler 		vap->iv_stats.is_rx_nodealloc++;
1014b032f27cSSam Leffler 		return NULL;
1015b032f27cSSam Leffler 	}
1016b032f27cSSam Leffler 
1017b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
101849a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
10198a1b9b6aSSam Leffler 		ether_sprintf(macaddr), nt->nt_name);
10208a1b9b6aSSam Leffler 
10211a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
10221a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
10238a1b9b6aSSam Leffler 	ieee80211_node_initref(ni);		/* mark referenced */
10248a1b9b6aSSam Leffler 	ni->ni_chan = IEEE80211_CHAN_ANYC;
10258a1b9b6aSSam Leffler 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
10268a1b9b6aSSam Leffler 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
102701a03542SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1028b032f27cSSam Leffler 	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1029b032f27cSSam Leffler 	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
10302045f699SSam Leffler 	ni->ni_inact_reload = nt->nt_inact_init;
10312045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
103268e8e04eSSam Leffler 	ni->ni_ath_defkeyix = 0x7fff;
103363092fceSSam Leffler 	ieee80211_psq_init(&ni->ni_psq, "unknown");
1034b032f27cSSam Leffler 	IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
10358a1b9b6aSSam Leffler 
10368a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
10378a1b9b6aSSam Leffler 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
10388a1b9b6aSSam Leffler 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
10398a1b9b6aSSam Leffler 	ni->ni_table = nt;
1040b032f27cSSam Leffler 	ni->ni_vap = vap;
10418a1b9b6aSSam Leffler 	ni->ni_ic = ic;
10428a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
10431a1e1d21SSam Leffler 
1044be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1045be1054edSSam Leffler 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1046be1054edSSam Leffler 
10471a1e1d21SSam Leffler 	return ni;
10481a1e1d21SSam Leffler }
10491a1e1d21SSam Leffler 
105097c973adSSam Leffler /*
105197c973adSSam Leffler  * Craft a temporary node suitable for sending a management frame
105297c973adSSam Leffler  * to the specified station.  We craft only as much state as we
105397c973adSSam Leffler  * need to do the work since the node will be immediately reclaimed
105497c973adSSam Leffler  * once the send completes.
105597c973adSSam Leffler  */
105697c973adSSam Leffler struct ieee80211_node *
1057b032f27cSSam Leffler ieee80211_tmp_node(struct ieee80211vap *vap,
1058b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
105997c973adSSam Leffler {
1060b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
106197c973adSSam Leffler 	struct ieee80211_node *ni;
106297c973adSSam Leffler 
106338c208f8SSam Leffler 	ni = ic->ic_node_alloc(vap, macaddr);
106497c973adSSam Leffler 	if (ni != NULL) {
1065b9b5f07dSSam Leffler 		struct ieee80211_node *bss = vap->iv_bss;
1066b9b5f07dSSam Leffler 
1067b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
106897c973adSSam Leffler 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
106997c973adSSam Leffler 
1070b032f27cSSam Leffler 		ni->ni_table = NULL;		/* NB: pedantic */
1071b032f27cSSam Leffler 		ni->ni_ic = ic;			/* NB: needed to set channel */
1072b032f27cSSam Leffler 		ni->ni_vap = vap;
1073b032f27cSSam Leffler 
107497c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1075b9b5f07dSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
107697c973adSSam Leffler 		ieee80211_node_initref(ni);		/* mark referenced */
107797c973adSSam Leffler 		/* NB: required by ieee80211_fix_rate */
1078b9b5f07dSSam Leffler 		ieee80211_node_set_chan(ni, bss->ni_chan);
1079b032f27cSSam Leffler 		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
108097c973adSSam Leffler 			IEEE80211_KEYIX_NONE);
1081b9b5f07dSSam Leffler 		ni->ni_txpower = bss->ni_txpower;
108297c973adSSam Leffler 		/* XXX optimize away */
108363092fceSSam Leffler 		ieee80211_psq_init(&ni->ni_psq, "unknown");
1084b032f27cSSam Leffler 		IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
108597c973adSSam Leffler 	} else {
108697c973adSSam Leffler 		/* XXX msg */
1087b032f27cSSam Leffler 		vap->iv_stats.is_rx_nodealloc++;
108897c973adSSam Leffler 	}
108997c973adSSam Leffler 	return ni;
109097c973adSSam Leffler }
109197c973adSSam Leffler 
10921a1e1d21SSam Leffler struct ieee80211_node *
1093b032f27cSSam Leffler ieee80211_dup_bss(struct ieee80211vap *vap,
1094b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
10951a1e1d21SSam Leffler {
1096b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
10978a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10988a1b9b6aSSam Leffler 
1099b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
11001a1e1d21SSam Leffler 	if (ni != NULL) {
1101b9b5f07dSSam Leffler 		struct ieee80211_node *bss = vap->iv_bss;
1102694dca64SSam Leffler 		/*
1103b032f27cSSam Leffler 		 * Inherit from iv_bss.
1104694dca64SSam Leffler 		 */
1105b9b5f07dSSam Leffler 		copy_bss(ni, bss);
1106b9b5f07dSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1107b9b5f07dSSam Leffler 		ieee80211_node_set_chan(ni, bss->ni_chan);
1108b032f27cSSam Leffler 	}
11091a1e1d21SSam Leffler 	return ni;
11101a1e1d21SSam Leffler }
11111a1e1d21SSam Leffler 
1112b032f27cSSam Leffler /*
1113b032f27cSSam Leffler  * Create a bss node for a legacy WDS vap.  The far end does
1114b032f27cSSam Leffler  * not associate so we just create create a new node and
1115b032f27cSSam Leffler  * simulate an association.  The caller is responsible for
1116b032f27cSSam Leffler  * installing the node as the bss node and handling any further
1117b032f27cSSam Leffler  * setup work like authorizing the port.
1118b032f27cSSam Leffler  */
1119b032f27cSSam Leffler struct ieee80211_node *
1120b032f27cSSam Leffler ieee80211_node_create_wds(struct ieee80211vap *vap,
1121b032f27cSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1122b032f27cSSam Leffler {
1123b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1124b032f27cSSam Leffler 	struct ieee80211_node *ni;
1125b032f27cSSam Leffler 
1126b032f27cSSam Leffler 	/* XXX check if node already in sta table? */
1127b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1128b032f27cSSam Leffler 	if (ni != NULL) {
1129b032f27cSSam Leffler 		ni->ni_wdsvap = vap;
1130b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1131b032f27cSSam Leffler 		/*
1132b032f27cSSam Leffler 		 * Inherit any manually configured settings.
1133b032f27cSSam Leffler 		 */
1134b9b5f07dSSam Leffler 		copy_bss(ni, vap->iv_bss);
1135b032f27cSSam Leffler 		ieee80211_node_set_chan(ni, chan);
1136b032f27cSSam Leffler 		/* NB: propagate ssid so available to WPA supplicant */
1137b032f27cSSam Leffler 		ni->ni_esslen = vap->iv_des_ssid[0].len;
1138b032f27cSSam Leffler 		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1139b032f27cSSam Leffler 		/* NB: no associd for peer */
1140b032f27cSSam Leffler 		/*
1141b032f27cSSam Leffler 		 * There are no management frames to use to
1142b032f27cSSam Leffler 		 * discover neighbor capabilities, so blindly
1143b032f27cSSam Leffler 		 * propagate the local configuration.
1144b032f27cSSam Leffler 		 */
1145b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WME)
1146b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
1147b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_FF)
1148b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_FF;
1149b032f27cSSam Leffler 		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1150b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1151b032f27cSSam Leffler 			/*
1152b032f27cSSam Leffler 			 * Device is HT-capable and HT is enabled for
1153b032f27cSSam Leffler 			 * the vap; setup HT operation.  On return
1154b032f27cSSam Leffler 			 * ni_chan will be adjusted to an HT channel.
1155b032f27cSSam Leffler 			 */
1156b032f27cSSam Leffler 			ieee80211_ht_wds_init(ni);
1157b032f27cSSam Leffler 		} else {
1158b032f27cSSam Leffler 			struct ieee80211_channel *c = ni->ni_chan;
1159b032f27cSSam Leffler 			/*
1160b032f27cSSam Leffler 			 * Force a legacy channel to be used.
1161b032f27cSSam Leffler 			 */
1162b032f27cSSam Leffler 			c = ieee80211_find_channel(ic,
1163b032f27cSSam Leffler 			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1164b032f27cSSam Leffler 			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1165b032f27cSSam Leffler 			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1166b032f27cSSam Leffler 			ni->ni_chan = c;
1167b032f27cSSam Leffler 		}
1168b032f27cSSam Leffler 	}
1169b032f27cSSam Leffler 	return ni;
1170b032f27cSSam Leffler }
1171b032f27cSSam Leffler 
1172b032f27cSSam Leffler struct ieee80211_node *
11738a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1174b032f27cSSam Leffler ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1175b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
11768a1b9b6aSSam Leffler #else
1177b032f27cSSam Leffler ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1178b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
11798a1b9b6aSSam Leffler #endif
11801a1e1d21SSam Leffler {
11811a1e1d21SSam Leffler 	struct ieee80211_node *ni;
11821a1e1d21SSam Leffler 	int hash;
11831a1e1d21SSam Leffler 
11848a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
1185750d6d0cSSam Leffler 
11861a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
11878a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
11881a1e1d21SSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
11898a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
11908a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1191b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
119249a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
119349a15236SSam Leffler 			    func, line,
119449a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
11958a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
11968a1b9b6aSSam Leffler #endif
1197750d6d0cSSam Leffler 			return ni;
11981a1e1d21SSam Leffler 		}
11991a1e1d21SSam Leffler 	}
1200750d6d0cSSam Leffler 	return NULL;
1201750d6d0cSSam Leffler }
1202750d6d0cSSam Leffler 
1203750d6d0cSSam Leffler struct ieee80211_node *
12048a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12058a1b9b6aSSam Leffler ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1206b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
12078a1b9b6aSSam Leffler #else
1208b032f27cSSam Leffler ieee80211_find_node(struct ieee80211_node_table *nt,
1209b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
12108a1b9b6aSSam Leffler #endif
1211750d6d0cSSam Leffler {
1212750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1213750d6d0cSSam Leffler 
12148a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1215b032f27cSSam Leffler 	ni = ieee80211_find_node_locked(nt, macaddr);
1216b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1217b032f27cSSam Leffler 	return ni;
1218b032f27cSSam Leffler }
1219b032f27cSSam Leffler 
1220b032f27cSSam Leffler struct ieee80211_node *
1221b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1222b032f27cSSam Leffler ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1223b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1224b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1225b032f27cSSam Leffler #else
1226b032f27cSSam Leffler ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1227b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1228b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1229b032f27cSSam Leffler #endif
1230b032f27cSSam Leffler {
1231b032f27cSSam Leffler 	struct ieee80211_node *ni;
1232b032f27cSSam Leffler 	int hash;
1233b032f27cSSam Leffler 
1234b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
1235b032f27cSSam Leffler 
1236b032f27cSSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
1237b032f27cSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1238b032f27cSSam Leffler 		if (ni->ni_vap == vap &&
1239b032f27cSSam Leffler 		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1240b032f27cSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
1241b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1242b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1243b032f27cSSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1244b032f27cSSam Leffler 			    func, line,
1245b032f27cSSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
1246b032f27cSSam Leffler 			    ieee80211_node_refcnt(ni));
1247b032f27cSSam Leffler #endif
1248b032f27cSSam Leffler 			return ni;
1249b032f27cSSam Leffler 		}
1250b032f27cSSam Leffler 	}
1251b032f27cSSam Leffler 	return NULL;
1252b032f27cSSam Leffler }
1253b032f27cSSam Leffler 
1254b032f27cSSam Leffler struct ieee80211_node *
1255b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1256b032f27cSSam Leffler ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1257b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1258b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1259b032f27cSSam Leffler #else
1260b032f27cSSam Leffler ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1261b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1262b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1263b032f27cSSam Leffler #endif
1264b032f27cSSam Leffler {
1265b032f27cSSam Leffler 	struct ieee80211_node *ni;
1266b032f27cSSam Leffler 
1267b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1268b032f27cSSam Leffler 	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
12698a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
12701a1e1d21SSam Leffler 	return ni;
12711a1e1d21SSam Leffler }
12721a1e1d21SSam Leffler 
12731a1e1d21SSam Leffler /*
12748a1b9b6aSSam Leffler  * Fake up a node; this handles node discovery in adhoc mode.
12758a1b9b6aSSam Leffler  * Note that for the driver's benefit we we treat this like
12768a1b9b6aSSam Leffler  * an association so the driver has an opportunity to setup
12778a1b9b6aSSam Leffler  * it's private state.
12788a1b9b6aSSam Leffler  */
12798a1b9b6aSSam Leffler struct ieee80211_node *
1280b032f27cSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
128168e8e04eSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
12828a1b9b6aSSam Leffler {
12838a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
12848a1b9b6aSSam Leffler 
1285b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1286be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1287b032f27cSSam Leffler 	ni = ieee80211_dup_bss(vap, macaddr);
12888a1b9b6aSSam Leffler 	if (ni != NULL) {
1289b032f27cSSam Leffler 		struct ieee80211com *ic = vap->iv_ic;
1290b032f27cSSam Leffler 
12918a1b9b6aSSam Leffler 		/* XXX no rate negotiation; just dup */
1292b032f27cSSam Leffler 		ni->ni_rates = vap->iv_bss->ni_rates;
1293b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
12948e292e8eSSam Leffler 			/*
129568e8e04eSSam Leffler 			 * In adhoc demo mode there are no management
129668e8e04eSSam Leffler 			 * frames to use to discover neighbor capabilities,
129768e8e04eSSam Leffler 			 * so blindly propagate the local configuration
129868e8e04eSSam Leffler 			 * so we can do interesting things (e.g. use
129968e8e04eSSam Leffler 			 * WME to disable ACK's).
13008e292e8eSSam Leffler 			 */
1301b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_WME)
13028e292e8eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_QOS;
1303b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_FF)
130468e8e04eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_FF;
13058e292e8eSSam Leffler 		}
130601a03542SSam Leffler 		node_setuptxparms(ni);
1307b032f27cSSam Leffler 		if (ic->ic_newassoc != NULL)
1308b032f27cSSam Leffler 			ic->ic_newassoc(ni, 1);
130968e8e04eSSam Leffler 		/* XXX not right for 802.1x/WPA */
131068e8e04eSSam Leffler 		ieee80211_node_authorize(ni);
13118a1b9b6aSSam Leffler 	}
13128a1b9b6aSSam Leffler 	return ni;
13138a1b9b6aSSam Leffler }
13148a1b9b6aSSam Leffler 
1315be425a0fSSam Leffler void
1316be425a0fSSam Leffler ieee80211_init_neighbor(struct ieee80211_node *ni,
1317be425a0fSSam Leffler 	const struct ieee80211_frame *wh,
1318be425a0fSSam Leffler 	const struct ieee80211_scanparams *sp)
1319be425a0fSSam Leffler {
1320be425a0fSSam Leffler 	ni->ni_esslen = sp->ssid[1];
1321be425a0fSSam Leffler 	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1322be425a0fSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1323be425a0fSSam Leffler 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1324be425a0fSSam Leffler 	ni->ni_intval = sp->bintval;
1325be425a0fSSam Leffler 	ni->ni_capinfo = sp->capinfo;
1326be425a0fSSam Leffler 	ni->ni_chan = ni->ni_ic->ic_curchan;
1327be425a0fSSam Leffler 	ni->ni_fhdwell = sp->fhdwell;
1328be425a0fSSam Leffler 	ni->ni_fhindex = sp->fhindex;
1329be425a0fSSam Leffler 	ni->ni_erp = sp->erp;
1330be425a0fSSam Leffler 	ni->ni_timoff = sp->timoff;
1331b032f27cSSam Leffler 
1332b032f27cSSam Leffler 	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1333b032f27cSSam Leffler 		ieee80211_ies_expand(&ni->ni_ies);
1334b032f27cSSam Leffler 		if (ni->ni_ies.ath_ie != NULL)
1335b032f27cSSam Leffler 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1336b032f27cSSam Leffler 	}
1337be425a0fSSam Leffler 
1338be425a0fSSam Leffler 	/* NB: must be after ni_chan is setup */
133979edaebfSSam Leffler 	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
134079edaebfSSam Leffler 		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
134179edaebfSSam Leffler 		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1342be425a0fSSam Leffler }
1343be425a0fSSam Leffler 
1344b5c99415SSam Leffler /*
1345b5c99415SSam Leffler  * Do node discovery in adhoc mode on receipt of a beacon
1346b5c99415SSam Leffler  * or probe response frame.  Note that for the driver's
1347b5c99415SSam Leffler  * benefit we we treat this like an association so the
1348b5c99415SSam Leffler  * driver has an opportunity to setup it's private state.
1349b5c99415SSam Leffler  */
1350b5c99415SSam Leffler struct ieee80211_node *
1351b032f27cSSam Leffler ieee80211_add_neighbor(struct ieee80211vap *vap,
1352b5c99415SSam Leffler 	const struct ieee80211_frame *wh,
1353b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp)
1354b5c99415SSam Leffler {
1355b5c99415SSam Leffler 	struct ieee80211_node *ni;
1356b5c99415SSam Leffler 
1357b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1358be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1359b032f27cSSam Leffler 	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1360b5c99415SSam Leffler 	if (ni != NULL) {
1361b032f27cSSam Leffler 		struct ieee80211com *ic = vap->iv_ic;
1362b032f27cSSam Leffler 
1363be425a0fSSam Leffler 		ieee80211_init_neighbor(ni, wh, sp);
136401a03542SSam Leffler 		node_setuptxparms(ni);
1365b5c99415SSam Leffler 		if (ic->ic_newassoc != NULL)
1366b5c99415SSam Leffler 			ic->ic_newassoc(ni, 1);
1367b5c99415SSam Leffler 		/* XXX not right for 802.1x/WPA */
1368b5c99415SSam Leffler 		ieee80211_node_authorize(ni);
1369b5c99415SSam Leffler 	}
1370b5c99415SSam Leffler 	return ni;
1371b5c99415SSam Leffler }
1372b5c99415SSam Leffler 
1373c1225b52SSam Leffler #define	IS_CTL(wh) \
1374c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1375c1225b52SSam Leffler #define	IS_PSPOLL(wh) \
1376c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
137768e8e04eSSam Leffler #define	IS_BAR(wh) \
137868e8e04eSSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
1379a2cfa5b7SSam Leffler #define	IS_PROBEREQ(wh) \
1380a2cfa5b7SSam Leffler 	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1381a2cfa5b7SSam Leffler 	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1382a2cfa5b7SSam Leffler #define	IS_BCAST_PROBEREQ(wh) \
1383a2cfa5b7SSam Leffler 	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1384a2cfa5b7SSam Leffler 	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1385a2cfa5b7SSam Leffler 
1386a2cfa5b7SSam Leffler static __inline struct ieee80211_node *
1387a2cfa5b7SSam Leffler _find_rxnode(struct ieee80211_node_table *nt,
1388a2cfa5b7SSam Leffler     const struct ieee80211_frame_min *wh)
1389a2cfa5b7SSam Leffler {
1390a2cfa5b7SSam Leffler 	/* XXX 4-address frames? */
1391a2cfa5b7SSam Leffler 	if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1392a2cfa5b7SSam Leffler 		return ieee80211_find_node_locked(nt, wh->i_addr1);
1393a2cfa5b7SSam Leffler 	if (IS_BCAST_PROBEREQ(wh))
1394a2cfa5b7SSam Leffler 		return NULL;		/* spam bcast probe req to all vap's */
1395a2cfa5b7SSam Leffler 	return ieee80211_find_node_locked(nt, wh->i_addr2);
1396a2cfa5b7SSam Leffler }
139768e8e04eSSam Leffler 
13988a1b9b6aSSam Leffler /*
13998a1b9b6aSSam Leffler  * Locate the node for sender, track state, and then pass the
1400a2cfa5b7SSam Leffler  * (referenced) node up to the 802.11 layer for its use.  Note
1401a2cfa5b7SSam Leffler  * we can return NULL if the sender is not in the table.
14028a1b9b6aSSam Leffler  */
14038a1b9b6aSSam Leffler struct ieee80211_node *
14048a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
14058a1b9b6aSSam Leffler ieee80211_find_rxnode_debug(struct ieee80211com *ic,
14068a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh, const char *func, int line)
14078a1b9b6aSSam Leffler #else
14088a1b9b6aSSam Leffler ieee80211_find_rxnode(struct ieee80211com *ic,
14098a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh)
14108a1b9b6aSSam Leffler #endif
14118a1b9b6aSSam Leffler {
14128a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
14138a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
14148a1b9b6aSSam Leffler 
141568e8e04eSSam Leffler 	nt = &ic->ic_sta;
14168a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1417a2cfa5b7SSam Leffler 	ni = _find_rxnode(nt, wh);
14188a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
14198a1b9b6aSSam Leffler 
1420c1225b52SSam Leffler 	return ni;
1421c1225b52SSam Leffler }
1422c1225b52SSam Leffler 
1423c1225b52SSam Leffler /*
1424c1225b52SSam Leffler  * Like ieee80211_find_rxnode but use the supplied h/w
1425c1225b52SSam Leffler  * key index as a hint to locate the node in the key
1426c1225b52SSam Leffler  * mapping table.  If an entry is present at the key
1427c1225b52SSam Leffler  * index we return it; otherwise do a normal lookup and
1428c1225b52SSam Leffler  * update the mapping table if the station has a unicast
1429c1225b52SSam Leffler  * key assigned to it.
1430c1225b52SSam Leffler  */
1431c1225b52SSam Leffler struct ieee80211_node *
1432c1225b52SSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1433c1225b52SSam Leffler ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1434c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1435c1225b52SSam Leffler 	const char *func, int line)
1436c1225b52SSam Leffler #else
1437c1225b52SSam Leffler ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1438c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1439c1225b52SSam Leffler #endif
1440c1225b52SSam Leffler {
1441c1225b52SSam Leffler 	struct ieee80211_node_table *nt;
1442c1225b52SSam Leffler 	struct ieee80211_node *ni;
1443c1225b52SSam Leffler 
1444c1225b52SSam Leffler 	nt = &ic->ic_sta;
1445c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
1446c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1447c1225b52SSam Leffler 		ni = nt->nt_keyixmap[keyix];
1448c1225b52SSam Leffler 	else
1449c1225b52SSam Leffler 		ni = NULL;
1450c1225b52SSam Leffler 	if (ni == NULL) {
1451a2cfa5b7SSam Leffler 		ni = _find_rxnode(nt, wh);
1452b032f27cSSam Leffler 		if (ni != NULL && nt->nt_keyixmap != NULL) {
1453c1225b52SSam Leffler 			/*
1454c1225b52SSam Leffler 			 * If the station has a unicast key cache slot
1455c1225b52SSam Leffler 			 * assigned update the key->node mapping table.
1456c1225b52SSam Leffler 			 */
1457c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1458c1225b52SSam Leffler 			/* XXX can keyixmap[keyix] != NULL? */
1459c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1460c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == NULL) {
1461b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
1462b032f27cSSam Leffler 				    IEEE80211_MSG_NODE,
1463c1225b52SSam Leffler 				    "%s: add key map entry %p<%s> refcnt %d\n",
1464c1225b52SSam Leffler 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1465c1225b52SSam Leffler 				    ieee80211_node_refcnt(ni)+1);
1466c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1467c1225b52SSam Leffler 			}
1468c1225b52SSam Leffler 		}
1469a2cfa5b7SSam Leffler 	} else {
1470a2cfa5b7SSam Leffler 		if (IS_BCAST_PROBEREQ(wh))
1471a2cfa5b7SSam Leffler 			ni = NULL;	/* spam bcast probe req to all vap's */
1472a2cfa5b7SSam Leffler 		else
1473c1225b52SSam Leffler 			ieee80211_ref_node(ni);
1474a2cfa5b7SSam Leffler 	}
1475c1225b52SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1476c1225b52SSam Leffler 
1477c1225b52SSam Leffler 	return ni;
1478c1225b52SSam Leffler }
1479a2cfa5b7SSam Leffler #undef IS_BCAST_PROBEREQ
1480a2cfa5b7SSam Leffler #undef IS_PROBEREQ
148168e8e04eSSam Leffler #undef IS_BAR
14828a1b9b6aSSam Leffler #undef IS_PSPOLL
14838a1b9b6aSSam Leffler #undef IS_CTL
14848a1b9b6aSSam Leffler 
14858a1b9b6aSSam Leffler /*
1486750d6d0cSSam Leffler  * Return a reference to the appropriate node for sending
1487750d6d0cSSam Leffler  * a data frame.  This handles node discovery in adhoc networks.
1488750d6d0cSSam Leffler  */
1489750d6d0cSSam Leffler struct ieee80211_node *
14908a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1491b032f27cSSam Leffler ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1492b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN],
14938a1b9b6aSSam Leffler 	const char *func, int line)
14948a1b9b6aSSam Leffler #else
1495b032f27cSSam Leffler ieee80211_find_txnode(struct ieee80211vap *vap,
1496b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
14978a1b9b6aSSam Leffler #endif
1498750d6d0cSSam Leffler {
1499b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1500750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1501750d6d0cSSam Leffler 
1502750d6d0cSSam Leffler 	/*
1503750d6d0cSSam Leffler 	 * The destination address should be in the node table
1504c1225b52SSam Leffler 	 * unless this is a multicast/broadcast frame.  We can
1505c1225b52SSam Leffler 	 * also optimize station mode operation, all frames go
1506c1225b52SSam Leffler 	 * to the bss node.
1507750d6d0cSSam Leffler 	 */
1508750d6d0cSSam Leffler 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
15098a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1510b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA ||
1511b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_WDS ||
1512b032f27cSSam Leffler 	    IEEE80211_IS_MULTICAST(macaddr))
1513b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
1514ee25b8dfSSam Leffler 	else {
1515b032f27cSSam Leffler 		ni = ieee80211_find_node_locked(nt, macaddr);
1516b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1517ee25b8dfSSam Leffler 		    (ni != NULL && ni->ni_associd == 0)) {
1518ee25b8dfSSam Leffler 			/*
1519ee25b8dfSSam Leffler 			 * Station is not associated; don't permit the
1520ee25b8dfSSam Leffler 			 * data frame to be sent by returning NULL.  This
1521ee25b8dfSSam Leffler 			 * is kinda a kludge but the least intrusive way
1522ee25b8dfSSam Leffler 			 * to add this check into all drivers.
1523ee25b8dfSSam Leffler 			 */
1524ee25b8dfSSam Leffler 			ieee80211_unref_node(&ni);	/* NB: null's ni */
1525ee25b8dfSSam Leffler 		}
1526ee25b8dfSSam Leffler 	}
15278a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15288a1b9b6aSSam Leffler 
15298a1b9b6aSSam Leffler 	if (ni == NULL) {
1530b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_IBSS ||
1531b032f27cSSam Leffler 		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
153290d0d036SSam Leffler 			/*
153390d0d036SSam Leffler 			 * In adhoc mode cons up a node for the destination.
153490d0d036SSam Leffler 			 * Note that we need an additional reference for the
1535b032f27cSSam Leffler 			 * caller to be consistent with
1536b032f27cSSam Leffler 			 * ieee80211_find_node_locked.
153790d0d036SSam Leffler 			 */
1538b032f27cSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
153990d0d036SSam Leffler 			if (ni != NULL)
154090d0d036SSam Leffler 				(void) ieee80211_ref_node(ni);
154190d0d036SSam Leffler 		} else {
1542b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1543b032f27cSSam Leffler 			    "no node, discard frame (%s)", __func__);
1544b032f27cSSam Leffler 			vap->iv_stats.is_tx_nonode++;
1545750d6d0cSSam Leffler 		}
1546750d6d0cSSam Leffler 	}
1547750d6d0cSSam Leffler 	return ni;
1548750d6d0cSSam Leffler }
1549750d6d0cSSam Leffler 
15508a1b9b6aSSam Leffler static void
15518a1b9b6aSSam Leffler _ieee80211_free_node(struct ieee80211_node *ni)
15528a1b9b6aSSam Leffler {
15538a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
15548a1b9b6aSSam Leffler 
155537e9743aSSam Leffler 	/*
155637e9743aSSam Leffler 	 * NB: careful about referencing the vap as it may be
155737e9743aSSam Leffler 	 * gone if the last reference was held by a driver.
155837e9743aSSam Leffler 	 * We know the com will always be present so it's safe
155937e9743aSSam Leffler 	 * to use ni_ic below to reclaim resources.
156037e9743aSSam Leffler 	 */
156137e9743aSSam Leffler #if 0
1562b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
156349a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
156449a15236SSam Leffler 		ether_sprintf(ni->ni_macaddr),
15658a1b9b6aSSam Leffler 		nt != NULL ? nt->nt_name : "<gone>");
156637e9743aSSam Leffler #endif
156737e9743aSSam Leffler 	if (ni->ni_associd != 0) {
156837e9743aSSam Leffler 		struct ieee80211vap *vap = ni->ni_vap;
1569b032f27cSSam Leffler 		if (vap->iv_aid_bitmap != NULL)
1570b032f27cSSam Leffler 			IEEE80211_AID_CLR(vap, ni->ni_associd);
157137e9743aSSam Leffler 	}
15728a1b9b6aSSam Leffler 	if (nt != NULL) {
15738a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
15748a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
15758a1b9b6aSSam Leffler 	}
157637e9743aSSam Leffler 	ni->ni_ic->ic_node_free(ni);
15778a1b9b6aSSam Leffler }
15788a1b9b6aSSam Leffler 
15798a1b9b6aSSam Leffler void
15808a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
15818a1b9b6aSSam Leffler ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
15828a1b9b6aSSam Leffler #else
15838a1b9b6aSSam Leffler ieee80211_free_node(struct ieee80211_node *ni)
15848a1b9b6aSSam Leffler #endif
15858a1b9b6aSSam Leffler {
15868a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
15878a1b9b6aSSam Leffler 
15888a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1589b032f27cSSam Leffler 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
159049a15236SSam Leffler 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
15918a1b9b6aSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
15928a1b9b6aSSam Leffler #endif
1593c1225b52SSam Leffler 	if (nt != NULL) {
1594c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
15958a1b9b6aSSam Leffler 		if (ieee80211_node_dectestref(ni)) {
15968a1b9b6aSSam Leffler 			/*
1597c1225b52SSam Leffler 			 * Last reference, reclaim state.
15988a1b9b6aSSam Leffler 			 */
15998a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
1600c1225b52SSam Leffler 		} else if (ieee80211_node_refcnt(ni) == 1 &&
1601c1225b52SSam Leffler 		    nt->nt_keyixmap != NULL) {
1602c1225b52SSam Leffler 			ieee80211_keyix keyix;
1603c1225b52SSam Leffler 			/*
1604c1225b52SSam Leffler 			 * Check for a last reference in the key mapping table.
1605c1225b52SSam Leffler 			 */
1606c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1607c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1608c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == ni) {
1609b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
1610b032f27cSSam Leffler 				    IEEE80211_MSG_NODE,
1611c1225b52SSam Leffler 				    "%s: %p<%s> clear key map entry", __func__,
1612c1225b52SSam Leffler 				    ni, ether_sprintf(ni->ni_macaddr));
1613c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = NULL;
1614c1225b52SSam Leffler 				ieee80211_node_decref(ni); /* XXX needed? */
16158a1b9b6aSSam Leffler 				_ieee80211_free_node(ni);
16168a1b9b6aSSam Leffler 			}
16171a1e1d21SSam Leffler 		}
1618c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1619c1225b52SSam Leffler 	} else {
1620c1225b52SSam Leffler 		if (ieee80211_node_dectestref(ni))
1621c1225b52SSam Leffler 			_ieee80211_free_node(ni);
1622c1225b52SSam Leffler 	}
1623c1225b52SSam Leffler }
1624c1225b52SSam Leffler 
1625c1225b52SSam Leffler /*
1626c1225b52SSam Leffler  * Reclaim a unicast key and clear any key cache state.
1627c1225b52SSam Leffler  */
1628c1225b52SSam Leffler int
1629c1225b52SSam Leffler ieee80211_node_delucastkey(struct ieee80211_node *ni)
1630c1225b52SSam Leffler {
163137e9743aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
163237e9743aSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1633c1225b52SSam Leffler 	struct ieee80211_node *nikey;
1634c1225b52SSam Leffler 	ieee80211_keyix keyix;
1635c1225b52SSam Leffler 	int isowned, status;
1636c1225b52SSam Leffler 
1637c1225b52SSam Leffler 	/*
1638c1225b52SSam Leffler 	 * NB: We must beware of LOR here; deleting the key
1639c1225b52SSam Leffler 	 * can cause the crypto layer to block traffic updates
1640c1225b52SSam Leffler 	 * which can generate a LOR against the node table lock;
1641c1225b52SSam Leffler 	 * grab it here and stash the key index for our use below.
1642c1225b52SSam Leffler 	 *
1643c1225b52SSam Leffler 	 * Must also beware of recursion on the node table lock.
1644c1225b52SSam Leffler 	 * When called from node_cleanup we may already have
1645c1225b52SSam Leffler 	 * the node table lock held.  Unfortunately there's no
1646c1225b52SSam Leffler 	 * way to separate out this path so we must do this
1647c1225b52SSam Leffler 	 * conditionally.
1648c1225b52SSam Leffler 	 */
1649c1225b52SSam Leffler 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1650c1225b52SSam Leffler 	if (!isowned)
1651c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
165237e9743aSSam Leffler 	nikey = NULL;
165337e9743aSSam Leffler 	status = 1;		/* NB: success */
165437e9743aSSam Leffler 	if (!IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1655c1225b52SSam Leffler 		keyix = ni->ni_ucastkey.wk_rxkeyix;
165637e9743aSSam Leffler 		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1657c1225b52SSam Leffler 		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1658c1225b52SSam Leffler 			nikey = nt->nt_keyixmap[keyix];
1659c1225b52SSam Leffler 			nt->nt_keyixmap[keyix] = NULL;;
166037e9743aSSam Leffler 		}
166137e9743aSSam Leffler 	}
1662c1225b52SSam Leffler 	if (!isowned)
1663b032f27cSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1664c1225b52SSam Leffler 
1665c1225b52SSam Leffler 	if (nikey != NULL) {
1666c1225b52SSam Leffler 		KASSERT(nikey == ni,
1667c1225b52SSam Leffler 			("key map out of sync, ni %p nikey %p", ni, nikey));
166837e9743aSSam Leffler 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1669c1225b52SSam Leffler 			"%s: delete key map entry %p<%s> refcnt %d\n",
1670c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr),
1671c1225b52SSam Leffler 			ieee80211_node_refcnt(ni)-1);
1672c1225b52SSam Leffler 		ieee80211_free_node(ni);
1673c1225b52SSam Leffler 	}
1674c1225b52SSam Leffler 	return status;
1675c1225b52SSam Leffler }
16761a1e1d21SSam Leffler 
1677303ebc3cSSam Leffler /*
16788a1b9b6aSSam Leffler  * Reclaim a node.  If this is the last reference count then
16798a1b9b6aSSam Leffler  * do the normal free work.  Otherwise remove it from the node
16808a1b9b6aSSam Leffler  * table and mark it gone by clearing the back-reference.
16818a1b9b6aSSam Leffler  */
16828a1b9b6aSSam Leffler static void
16838a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
16848a1b9b6aSSam Leffler {
1685c1225b52SSam Leffler 	ieee80211_keyix keyix;
1686c1225b52SSam Leffler 
1687c1225b52SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
16888a1b9b6aSSam Leffler 
1689b032f27cSSam Leffler 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
169049a15236SSam Leffler 		"%s: remove %p<%s> from %s table, refcnt %d\n",
169149a15236SSam Leffler 		__func__, ni, ether_sprintf(ni->ni_macaddr),
169249a15236SSam Leffler 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1693c1225b52SSam Leffler 	/*
1694c1225b52SSam Leffler 	 * Clear any entry in the unicast key mapping table.
1695c1225b52SSam Leffler 	 * We need to do it here so rx lookups don't find it
1696c1225b52SSam Leffler 	 * in the mapping table even if it's not in the hash
1697c1225b52SSam Leffler 	 * table.  We cannot depend on the mapping table entry
1698c1225b52SSam Leffler 	 * being cleared because the node may not be free'd.
1699c1225b52SSam Leffler 	 */
1700c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1701c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1702c1225b52SSam Leffler 	    nt->nt_keyixmap[keyix] == ni) {
1703b032f27cSSam Leffler 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1704c1225b52SSam Leffler 			"%s: %p<%s> clear key map entry\n",
1705c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr));
1706c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;
1707c1225b52SSam Leffler 		ieee80211_node_decref(ni);	/* NB: don't need free */
1708c1225b52SSam Leffler 	}
17098a1b9b6aSSam Leffler 	if (!ieee80211_node_dectestref(ni)) {
17108a1b9b6aSSam Leffler 		/*
17118a1b9b6aSSam Leffler 		 * Other references are present, just remove the
17128a1b9b6aSSam Leffler 		 * node from the table so it cannot be found.  When
17138a1b9b6aSSam Leffler 		 * the references are dropped storage will be
1714acc4f7f5SSam Leffler 		 * reclaimed.
17158a1b9b6aSSam Leffler 		 */
17168a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
17178a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
17188a1b9b6aSSam Leffler 		ni->ni_table = NULL;		/* clear reference */
17198a1b9b6aSSam Leffler 	} else
17208a1b9b6aSSam Leffler 		_ieee80211_free_node(ni);
17218a1b9b6aSSam Leffler }
17228a1b9b6aSSam Leffler 
1723b032f27cSSam Leffler /*
1724b032f27cSSam Leffler  * Reclaim a (bss) node.  Decrement the refcnt and reclaim
1725b032f27cSSam Leffler  * the node if the only other reference to it is in the sta
1726b032f27cSSam Leffler  * table.  This is effectively ieee80211_free_node followed
1727b032f27cSSam Leffler  * by node_reclaim when the refcnt is 1 (after the free).
1728b032f27cSSam Leffler  */
17298a1b9b6aSSam Leffler static void
1730b032f27cSSam Leffler ieee80211_node_reclaim(struct ieee80211_node *ni)
17318a1b9b6aSSam Leffler {
1732b032f27cSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
17338a1b9b6aSSam Leffler 
1734b032f27cSSam Leffler 	KASSERT(nt != NULL, ("reclaim node not in table"));
17358a1b9b6aSSam Leffler 
1736b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1737b032f27cSSam Leffler 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
173892172ed8SSam Leffler 		"%s %p<%s> refcnt %d\n", __func__, ni,
1739b032f27cSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1740b032f27cSSam Leffler #endif
1741b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1742b032f27cSSam Leffler 	if (ieee80211_node_dectestref(ni)) {
1743b032f27cSSam Leffler 		/*
1744b032f27cSSam Leffler 		 * Last reference, reclaim state.
1745b032f27cSSam Leffler 		 */
1746b032f27cSSam Leffler 		_ieee80211_free_node(ni);
1747b032f27cSSam Leffler 		nt = NULL;
1748b032f27cSSam Leffler 	} else if (ieee80211_node_refcnt(ni) == 1 &&
1749b032f27cSSam Leffler 	    nt->nt_keyixmap != NULL) {
1750b032f27cSSam Leffler 		ieee80211_keyix keyix;
1751b032f27cSSam Leffler 		/*
1752b032f27cSSam Leffler 		 * Check for a last reference in the key mapping table.
1753b032f27cSSam Leffler 		 */
1754b032f27cSSam Leffler 		keyix = ni->ni_ucastkey.wk_rxkeyix;
1755b032f27cSSam Leffler 		if (keyix < nt->nt_keyixmax &&
1756b032f27cSSam Leffler 		    nt->nt_keyixmap[keyix] == ni) {
1757b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap,
1758b032f27cSSam Leffler 			    IEEE80211_MSG_NODE,
1759b032f27cSSam Leffler 			    "%s: %p<%s> clear key map entry", __func__,
1760b032f27cSSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr));
1761b032f27cSSam Leffler 			nt->nt_keyixmap[keyix] = NULL;
1762b032f27cSSam Leffler 			ieee80211_node_decref(ni); /* XXX needed? */
1763b032f27cSSam Leffler 			_ieee80211_free_node(ni);
1764b032f27cSSam Leffler 			nt = NULL;
17658a1b9b6aSSam Leffler 		}
1766b032f27cSSam Leffler 	}
1767b032f27cSSam Leffler 	if (nt != NULL && ieee80211_node_refcnt(ni) == 1) {
1768b032f27cSSam Leffler 		/*
1769b032f27cSSam Leffler 		 * Last reference is in the sta table; complete
1770b032f27cSSam Leffler 		 * the reclaim.  This handles bss nodes being
1771b032f27cSSam Leffler 		 * recycled: the node has two references, one for
1772b032f27cSSam Leffler 		 * iv_bss and one for the table.  After dropping
1773b032f27cSSam Leffler 		 * the iv_bss ref above we need to reclaim the sta
1774b032f27cSSam Leffler 		 * table reference.
1775b032f27cSSam Leffler 		 */
1776b032f27cSSam Leffler 		ieee80211_node_decref(ni);	/* NB: be pendantic */
1777b032f27cSSam Leffler 		_ieee80211_free_node(ni);
1778b032f27cSSam Leffler 	}
1779b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1780b032f27cSSam Leffler }
1781b032f27cSSam Leffler 
1782b032f27cSSam Leffler /*
1783b032f27cSSam Leffler  * Node table support.
1784b032f27cSSam Leffler  */
1785b032f27cSSam Leffler 
1786b032f27cSSam Leffler static void
1787b032f27cSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic,
1788b032f27cSSam Leffler 	struct ieee80211_node_table *nt,
1789b032f27cSSam Leffler 	const char *name, int inact, int keyixmax)
1790b032f27cSSam Leffler {
1791b032f27cSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1792b032f27cSSam Leffler 
1793b032f27cSSam Leffler 	nt->nt_ic = ic;
1794b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1795b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1796b032f27cSSam Leffler 	TAILQ_INIT(&nt->nt_node);
1797b032f27cSSam Leffler 	nt->nt_name = name;
1798b032f27cSSam Leffler 	nt->nt_scangen = 1;
1799b032f27cSSam Leffler 	nt->nt_inact_init = inact;
1800b032f27cSSam Leffler 	nt->nt_keyixmax = keyixmax;
1801b032f27cSSam Leffler 	if (nt->nt_keyixmax > 0) {
1802c5abbba3SDag-Erling Smørgrav 		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
1803c5abbba3SDag-Erling Smørgrav 			keyixmax * sizeof(struct ieee80211_node *),
1804b032f27cSSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
1805b032f27cSSam Leffler 		if (nt->nt_keyixmap == NULL)
1806b032f27cSSam Leffler 			if_printf(ic->ic_ifp,
1807b032f27cSSam Leffler 			    "Cannot allocate key index map with %u entries\n",
1808b032f27cSSam Leffler 			    keyixmax);
1809b032f27cSSam Leffler 	} else
1810b032f27cSSam Leffler 		nt->nt_keyixmap = NULL;
1811b032f27cSSam Leffler }
1812b032f27cSSam Leffler 
1813b032f27cSSam Leffler static void
1814b032f27cSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt,
1815b032f27cSSam Leffler 	struct ieee80211vap *match)
1816b032f27cSSam Leffler {
1817b032f27cSSam Leffler 	struct ieee80211_node *ni, *next;
1818b032f27cSSam Leffler 
1819b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1820b032f27cSSam Leffler 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1821b032f27cSSam Leffler 		if (match != NULL && ni->ni_vap != match)
1822b032f27cSSam Leffler 			continue;
1823b032f27cSSam Leffler 		/* XXX can this happen?  if so need's work */
1824b032f27cSSam Leffler 		if (ni->ni_associd != 0) {
1825b032f27cSSam Leffler 			struct ieee80211vap *vap = ni->ni_vap;
1826b032f27cSSam Leffler 
1827b032f27cSSam Leffler 			if (vap->iv_auth->ia_node_leave != NULL)
1828b032f27cSSam Leffler 				vap->iv_auth->ia_node_leave(ni);
1829b032f27cSSam Leffler 			if (vap->iv_aid_bitmap != NULL)
1830b032f27cSSam Leffler 				IEEE80211_AID_CLR(vap, ni->ni_associd);
1831b032f27cSSam Leffler 		}
1832b032f27cSSam Leffler 		ni->ni_wdsvap = NULL;		/* clear reference */
18338a1b9b6aSSam Leffler 		node_reclaim(nt, ni);
18348a1b9b6aSSam Leffler 	}
1835b032f27cSSam Leffler 	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1836b032f27cSSam Leffler 		/*
1837b032f27cSSam Leffler 		 * Make a separate pass to clear references to this vap
1838b032f27cSSam Leffler 		 * held by DWDS entries.  They will not be matched above
1839b032f27cSSam Leffler 		 * because ni_vap will point to the ap vap but we still
1840b032f27cSSam Leffler 		 * need to clear ni_wdsvap when the WDS vap is destroyed
1841b032f27cSSam Leffler 		 * and/or reset.
1842b032f27cSSam Leffler 		 */
1843b032f27cSSam Leffler 		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1844b032f27cSSam Leffler 			if (ni->ni_wdsvap == match)
1845b032f27cSSam Leffler 				ni->ni_wdsvap = NULL;
1846b032f27cSSam Leffler 	}
1847b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1848b032f27cSSam Leffler }
1849b032f27cSSam Leffler 
1850b032f27cSSam Leffler static void
1851b032f27cSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1852b032f27cSSam Leffler {
1853b032f27cSSam Leffler 	ieee80211_node_table_reset(nt, NULL);
1854b032f27cSSam Leffler 	if (nt->nt_keyixmap != NULL) {
1855b032f27cSSam Leffler #ifdef DIAGNOSTIC
1856b032f27cSSam Leffler 		/* XXX verify all entries are NULL */
1857b032f27cSSam Leffler 		int i;
1858b032f27cSSam Leffler 		for (i = 0; i < nt->nt_keyixmax; i++)
1859b032f27cSSam Leffler 			if (nt->nt_keyixmap[i] != NULL)
1860b032f27cSSam Leffler 				printf("%s: %s[%u] still active\n", __func__,
1861b032f27cSSam Leffler 					nt->nt_name, i);
1862b032f27cSSam Leffler #endif
1863c5abbba3SDag-Erling Smørgrav 		FREE(nt->nt_keyixmap, M_80211_NODE);
1864b032f27cSSam Leffler 		nt->nt_keyixmap = NULL;
1865b032f27cSSam Leffler 	}
1866b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1867b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_DESTROY(nt);
18688a1b9b6aSSam Leffler }
18698a1b9b6aSSam Leffler 
18708a1b9b6aSSam Leffler /*
18718a1b9b6aSSam Leffler  * Timeout inactive stations and do related housekeeping.
18728a1b9b6aSSam Leffler  * Note that we cannot hold the node lock while sending a
18738a1b9b6aSSam Leffler  * frame as this would lead to a LOR.  Instead we use a
18748a1b9b6aSSam Leffler  * generation number to mark nodes that we've scanned and
18758a1b9b6aSSam Leffler  * drop the lock and restart a scan if we have to time out
18768a1b9b6aSSam Leffler  * a node.  Since we are single-threaded by virtue of
1877303ebc3cSSam Leffler  * controlling the inactivity timer we can be sure this will
1878303ebc3cSSam Leffler  * process each node only once.
1879303ebc3cSSam Leffler  */
18808a1b9b6aSSam Leffler static void
1881b032f27cSSam Leffler ieee80211_timeout_stations(struct ieee80211com *ic)
18821a1e1d21SSam Leffler {
1883b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1884b032f27cSSam Leffler 	struct ieee80211vap *vap;
1885303ebc3cSSam Leffler 	struct ieee80211_node *ni;
1886b032f27cSSam Leffler 	int gen = 0;
18871a1e1d21SSam Leffler 
1888b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK(nt);
1889a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
1890303ebc3cSSam Leffler restart:
18918a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
18928a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1893303ebc3cSSam Leffler 		if (ni->ni_scangen == gen)	/* previously handled */
1894303ebc3cSSam Leffler 			continue;
1895303ebc3cSSam Leffler 		ni->ni_scangen = gen;
18960a915fadSSam Leffler 		/*
1897ebdda46cSSam Leffler 		 * Ignore entries for which have yet to receive an
1898ebdda46cSSam Leffler 		 * authentication frame.  These are transient and
1899ebdda46cSSam Leffler 		 * will be reclaimed when the last reference to them
1900ebdda46cSSam Leffler 		 * goes away (when frame xmits complete).
1901ebdda46cSSam Leffler 		 */
1902b032f27cSSam Leffler 		vap = ni->ni_vap;
1903b032f27cSSam Leffler 		/*
1904b032f27cSSam Leffler 		 * Only process stations when in RUN state.  This
1905b032f27cSSam Leffler 		 * insures, for example, that we don't timeout an
1906b032f27cSSam Leffler 		 * inactive station during CAC.  Note that CSA state
1907b032f27cSSam Leffler 		 * is actually handled in ieee80211_node_timeout as
1908b032f27cSSam Leffler 		 * it applies to more than timeout processing.
1909b032f27cSSam Leffler 		 */
1910b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN)
1911b032f27cSSam Leffler 			continue;
1912b032f27cSSam Leffler 		/* XXX can vap be NULL? */
1913b032f27cSSam Leffler 		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
1914b032f27cSSam Leffler 		     vap->iv_opmode == IEEE80211_M_STA) &&
191544b666cdSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1916ebdda46cSSam Leffler 			continue;
1917ebdda46cSSam Leffler 		/*
19188a1b9b6aSSam Leffler 		 * Free fragment if not needed anymore
19198a1b9b6aSSam Leffler 		 * (last fragment older than 1s).
1920b032f27cSSam Leffler 		 * XXX doesn't belong here, move to node_age
19210a915fadSSam Leffler 		 */
19228a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[0] != NULL &&
19238a1b9b6aSSam Leffler 		    ticks > ni->ni_rxfragstamp + hz) {
19248a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
19258a1b9b6aSSam Leffler 			ni->ni_rxfrag[0] = NULL;
19268a1b9b6aSSam Leffler 		}
1927be1054edSSam Leffler 		if (ni->ni_inact > 0) {
1928c066143cSSam Leffler 			ni->ni_inact--;
1929be1054edSSam Leffler 			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1930be1054edSSam Leffler 			    "%s: inact %u inact_reload %u nrates %u",
1931be1054edSSam Leffler 			    __func__, ni->ni_inact, ni->ni_inact_reload,
1932be1054edSSam Leffler 			    ni->ni_rates.rs_nrates);
1933be1054edSSam Leffler 		}
1934ce647032SSam Leffler 		/*
1935ce647032SSam Leffler 		 * Special case ourself; we may be idle for extended periods
1936ce647032SSam Leffler 		 * of time and regardless reclaiming our state is wrong.
1937b032f27cSSam Leffler 		 * XXX run ic_node_age
1938ce647032SSam Leffler 		 */
1939b032f27cSSam Leffler 		if (ni == vap->iv_bss)
1940ce647032SSam Leffler 			continue;
1941b032f27cSSam Leffler 		if (ni->ni_associd != 0 ||
1942b032f27cSSam Leffler 		    (vap->iv_opmode == IEEE80211_M_IBSS ||
1943b032f27cSSam Leffler 		     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
19448a1b9b6aSSam Leffler 			/*
1945b032f27cSSam Leffler 			 * Age/drain resources held by the station.
19468a1b9b6aSSam Leffler 			 */
1947b032f27cSSam Leffler 			ic->ic_node_age(ni);
19488a1b9b6aSSam Leffler 			/*
19498a1b9b6aSSam Leffler 			 * Probe the station before time it out.  We
19508a1b9b6aSSam Leffler 			 * send a null data frame which may not be
19518a1b9b6aSSam Leffler 			 * universally supported by drivers (need it
19528a1b9b6aSSam Leffler 			 * for ps-poll support so it should be...).
195368e8e04eSSam Leffler 			 *
195468e8e04eSSam Leffler 			 * XXX don't probe the station unless we've
195568e8e04eSSam Leffler 			 *     received a frame from them (and have
195668e8e04eSSam Leffler 			 *     some idea of the rates they are capable
195768e8e04eSSam Leffler 			 *     of); this will get fixed more properly
195868e8e04eSSam Leffler 			 *     soon with better handling of the rate set.
19598a1b9b6aSSam Leffler 			 */
1960b032f27cSSam Leffler 			if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1961c066143cSSam Leffler 			    (0 < ni->ni_inact &&
1962b032f27cSSam Leffler 			     ni->ni_inact <= vap->iv_inact_probe) &&
196368e8e04eSSam Leffler 			    ni->ni_rates.rs_nrates != 0) {
1964b032f27cSSam Leffler 				IEEE80211_NOTE(vap,
1965f66d97f6SSam Leffler 				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1966f66d97f6SSam Leffler 				    ni, "%s",
1967f66d97f6SSam Leffler 				    "probe station due to inactivity");
196819ad2dd7SSam Leffler 				/*
196919ad2dd7SSam Leffler 				 * Grab a reference before unlocking the table
197019ad2dd7SSam Leffler 				 * so the node cannot be reclaimed before we
197119ad2dd7SSam Leffler 				 * send the frame. ieee80211_send_nulldata
197219ad2dd7SSam Leffler 				 * understands we've done this and reclaims the
197319ad2dd7SSam Leffler 				 * ref for us as needed.
197419ad2dd7SSam Leffler 				 */
197519ad2dd7SSam Leffler 				ieee80211_ref_node(ni);
19768a1b9b6aSSam Leffler 				IEEE80211_NODE_UNLOCK(nt);
1977f62121ceSSam Leffler 				ieee80211_send_nulldata(ni);
19788a1b9b6aSSam Leffler 				/* XXX stat? */
19798a1b9b6aSSam Leffler 				goto restart;
19808a1b9b6aSSam Leffler 			}
19818a1b9b6aSSam Leffler 		}
1982b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1983c066143cSSam Leffler 		    ni->ni_inact <= 0) {
1984b032f27cSSam Leffler 			IEEE80211_NOTE(vap,
1985f66d97f6SSam Leffler 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1986f66d97f6SSam Leffler 			    "station timed out due to inactivity "
1987f66d97f6SSam Leffler 			    "(refcnt %u)", ieee80211_node_refcnt(ni));
19888a1b9b6aSSam Leffler 			/*
19898a1b9b6aSSam Leffler 			 * Send a deauthenticate frame and drop the station.
19908a1b9b6aSSam Leffler 			 * This is somewhat complicated due to reference counts
19918a1b9b6aSSam Leffler 			 * and locking.  At this point a station will typically
19928a1b9b6aSSam Leffler 			 * have a reference count of 1.  ieee80211_node_leave
19938a1b9b6aSSam Leffler 			 * will do a "free" of the node which will drop the
19948a1b9b6aSSam Leffler 			 * reference count.  But in the meantime a reference
19958a1b9b6aSSam Leffler 			 * wil be held by the deauth frame.  The actual reclaim
19968a1b9b6aSSam Leffler 			 * of the node will happen either after the tx is
19978a1b9b6aSSam Leffler 			 * completed or by ieee80211_node_leave.
19988a1b9b6aSSam Leffler 			 *
19998a1b9b6aSSam Leffler 			 * Separately we must drop the node lock before sending
200068e8e04eSSam Leffler 			 * in case the driver takes a lock, as this can result
200168e8e04eSSam Leffler 			 * in a LOR between the node lock and the driver lock.
20028a1b9b6aSSam Leffler 			 */
20035698ab1aSSam Leffler 			ieee80211_ref_node(ni);
20048a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
20058a1b9b6aSSam Leffler 			if (ni->ni_associd != 0) {
2006b032f27cSSam Leffler 				IEEE80211_SEND_MGMT(ni,
20071a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
20081a1e1d21SSam Leffler 				    IEEE80211_REASON_AUTH_EXPIRE);
20098a1b9b6aSSam Leffler 			}
2010b032f27cSSam Leffler 			ieee80211_node_leave(ni);
20115698ab1aSSam Leffler 			ieee80211_free_node(ni);
2012b032f27cSSam Leffler 			vap->iv_stats.is_node_timeout++;
2013303ebc3cSSam Leffler 			goto restart;
2014303ebc3cSSam Leffler 		}
20151a1e1d21SSam Leffler 	}
20168a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
20178a1b9b6aSSam Leffler 
2018b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_UNLOCK(nt);
201968e8e04eSSam Leffler }
20208a1b9b6aSSam Leffler 
2021b032f27cSSam Leffler /*
2022b032f27cSSam Leffler  * Aggressively reclaim resources.  This should be used
2023b032f27cSSam Leffler  * only in a critical situation to reclaim mbuf resources.
2024b032f27cSSam Leffler  */
2025b032f27cSSam Leffler void
2026b032f27cSSam Leffler ieee80211_drain(struct ieee80211com *ic)
2027b032f27cSSam Leffler {
2028b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
2029b032f27cSSam Leffler 	struct ieee80211vap *vap;
2030b032f27cSSam Leffler 	struct ieee80211_node *ni;
2031b032f27cSSam Leffler 
2032b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
2033b032f27cSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2034b032f27cSSam Leffler 		/*
2035b032f27cSSam Leffler 		 * Ignore entries for which have yet to receive an
2036b032f27cSSam Leffler 		 * authentication frame.  These are transient and
2037b032f27cSSam Leffler 		 * will be reclaimed when the last reference to them
2038b032f27cSSam Leffler 		 * goes away (when frame xmits complete).
2039b032f27cSSam Leffler 		 */
2040b032f27cSSam Leffler 		vap = ni->ni_vap;
2041b032f27cSSam Leffler 		/*
2042b032f27cSSam Leffler 		 * Only process stations when in RUN state.  This
2043b032f27cSSam Leffler 		 * insures, for example, that we don't timeout an
2044b032f27cSSam Leffler 		 * inactive station during CAC.  Note that CSA state
2045b032f27cSSam Leffler 		 * is actually handled in ieee80211_node_timeout as
2046b032f27cSSam Leffler 		 * it applies to more than timeout processing.
2047b032f27cSSam Leffler 		 */
2048b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN)
2049b032f27cSSam Leffler 			continue;
2050b032f27cSSam Leffler 		/* XXX can vap be NULL? */
2051b032f27cSSam Leffler 		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2052b032f27cSSam Leffler 		     vap->iv_opmode == IEEE80211_M_STA) &&
2053b032f27cSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2054b032f27cSSam Leffler 			continue;
2055b032f27cSSam Leffler 		/*
2056b032f27cSSam Leffler 		 * Free fragments.
2057b032f27cSSam Leffler 		 * XXX doesn't belong here, move to node_drain
2058b032f27cSSam Leffler 		 */
2059b032f27cSSam Leffler 		if (ni->ni_rxfrag[0] != NULL) {
2060b032f27cSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
2061b032f27cSSam Leffler 			ni->ni_rxfrag[0] = NULL;
2062b032f27cSSam Leffler 		}
2063b032f27cSSam Leffler 		/*
2064b032f27cSSam Leffler 		 * Drain resources held by the station.
2065b032f27cSSam Leffler 		 */
2066b032f27cSSam Leffler 		ic->ic_node_drain(ni);
2067b032f27cSSam Leffler 	}
2068b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
2069b032f27cSSam Leffler }
2070b032f27cSSam Leffler 
2071b032f27cSSam Leffler /*
2072b032f27cSSam Leffler  * Per-ieee80211com inactivity timer callback.
2073b032f27cSSam Leffler  */
207468e8e04eSSam Leffler void
207568e8e04eSSam Leffler ieee80211_node_timeout(void *arg)
207668e8e04eSSam Leffler {
207768e8e04eSSam Leffler 	struct ieee80211com *ic = arg;
207868e8e04eSSam Leffler 
2079b032f27cSSam Leffler 	/*
2080b032f27cSSam Leffler 	 * Defer timeout processing if a channel switch is pending.
2081b032f27cSSam Leffler 	 * We typically need to be mute so not doing things that
2082b032f27cSSam Leffler 	 * might generate frames is good to handle in one place.
2083b032f27cSSam Leffler 	 * Supressing the station timeout processing may extend the
2084b032f27cSSam Leffler 	 * lifetime of inactive stations (by not decrementing their
2085b032f27cSSam Leffler 	 * idle counters) but this should be ok unless the CSA is
2086b032f27cSSam Leffler 	 * active for an unusually long time.
2087b032f27cSSam Leffler 	 */
2088b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
208968e8e04eSSam Leffler 		ieee80211_scan_timeout(ic);
2090b032f27cSSam Leffler 		ieee80211_timeout_stations(ic);
209168e8e04eSSam Leffler 
2092b105a069SSam Leffler 		IEEE80211_LOCK(ic);
2093b105a069SSam Leffler 		ieee80211_erp_timeout(ic);
20941b6167d2SSam Leffler 		ieee80211_ht_timeout(ic);
2095b105a069SSam Leffler 		IEEE80211_UNLOCK(ic);
2096b032f27cSSam Leffler 	}
209768e8e04eSSam Leffler 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
209868e8e04eSSam Leffler 		ieee80211_node_timeout, ic);
20991a1e1d21SSam Leffler }
21001a1e1d21SSam Leffler 
21011a1e1d21SSam Leffler void
2102b032f27cSSam Leffler ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2103b032f27cSSam Leffler 	ieee80211_iter_func *f, void *arg)
21041a1e1d21SSam Leffler {
21051a1e1d21SSam Leffler 	struct ieee80211_node *ni;
21068a1b9b6aSSam Leffler 	u_int gen;
21071a1e1d21SSam Leffler 
2108b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK(nt);
2109a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
21108a1b9b6aSSam Leffler restart:
21118a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
21128a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
21138a1b9b6aSSam Leffler 		if (ni->ni_scangen != gen) {
21148a1b9b6aSSam Leffler 			ni->ni_scangen = gen;
21158a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
21168a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
21171a1e1d21SSam Leffler 			(*f)(arg, ni);
21188a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
21198a1b9b6aSSam Leffler 			goto restart;
21208a1b9b6aSSam Leffler 		}
21218a1b9b6aSSam Leffler 	}
21228a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
21238a1b9b6aSSam Leffler 
2124b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_UNLOCK(nt);
21258a1b9b6aSSam Leffler }
21268a1b9b6aSSam Leffler 
21278a1b9b6aSSam Leffler void
21288a1b9b6aSSam Leffler ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
21298a1b9b6aSSam Leffler {
21308a1b9b6aSSam Leffler 	printf("0x%p: mac %s refcnt %d\n", ni,
21318a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
21328a1b9b6aSSam Leffler 	printf("\tscangen %u authmode %u flags 0x%x\n",
21338a1b9b6aSSam Leffler 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
21348a1b9b6aSSam Leffler 	printf("\tassocid 0x%x txpower %u vlan %u\n",
21358a1b9b6aSSam Leffler 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
21368a1b9b6aSSam Leffler 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2137801df4a5SSam Leffler 		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2138801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2139801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
21408a1b9b6aSSam Leffler 		ni->ni_rxfragstamp);
214168e8e04eSSam Leffler 	printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
2142b032f27cSSam Leffler 		ni->ni_rstamp, node_getrssi(ni), ni->ni_noise,
214368e8e04eSSam Leffler 		ni->ni_intval, ni->ni_capinfo);
21448a1b9b6aSSam Leffler 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
21458a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
21468a1b9b6aSSam Leffler 		ni->ni_esslen, ni->ni_essid,
21478a1b9b6aSSam Leffler 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2148be1054edSSam Leffler 	printf("\tinact %u inact_reload %u txrate %u\n",
2149be1054edSSam Leffler 		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
215068e8e04eSSam Leffler 	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
215168e8e04eSSam Leffler 		ni->ni_htcap, ni->ni_htparam,
215268e8e04eSSam Leffler 		ni->ni_htctlchan, ni->ni_ht2ndchan);
215368e8e04eSSam Leffler 	printf("\thtopmode %x htstbc %x chw %u\n",
215468e8e04eSSam Leffler 		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
21558a1b9b6aSSam Leffler }
21568a1b9b6aSSam Leffler 
21578a1b9b6aSSam Leffler void
21588a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt)
21598a1b9b6aSSam Leffler {
21608a1b9b6aSSam Leffler 	ieee80211_iterate_nodes(nt,
21618a1b9b6aSSam Leffler 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
21628a1b9b6aSSam Leffler }
21638a1b9b6aSSam Leffler 
21642dc4d8dcSSam Leffler static void
21652dc4d8dcSSam Leffler ieee80211_notify_erp_locked(struct ieee80211com *ic)
2166b105a069SSam Leffler {
2167b032f27cSSam Leffler 	struct ieee80211vap *vap;
2168b032f27cSSam Leffler 
2169b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2170b032f27cSSam Leffler 
2171b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2172b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2173b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2174b105a069SSam Leffler }
2175b105a069SSam Leffler 
21762dc4d8dcSSam Leffler void
21772dc4d8dcSSam Leffler ieee80211_notify_erp(struct ieee80211com *ic)
21782dc4d8dcSSam Leffler {
21792dc4d8dcSSam Leffler 	IEEE80211_LOCK(ic);
21802dc4d8dcSSam Leffler 	ieee80211_notify_erp_locked(ic);
21812dc4d8dcSSam Leffler 	IEEE80211_UNLOCK(ic);
21822dc4d8dcSSam Leffler }
21832dc4d8dcSSam Leffler 
21848a1b9b6aSSam Leffler /*
21858a1b9b6aSSam Leffler  * Handle a station joining an 11g network.
21868a1b9b6aSSam Leffler  */
21878a1b9b6aSSam Leffler static void
2188b032f27cSSam Leffler ieee80211_node_join_11g(struct ieee80211_node *ni)
21898a1b9b6aSSam Leffler {
2190b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
21918a1b9b6aSSam Leffler 
21921b6167d2SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
21931b6167d2SSam Leffler 
21948a1b9b6aSSam Leffler 	/*
21958a1b9b6aSSam Leffler 	 * Station isn't capable of short slot time.  Bump
21968a1b9b6aSSam Leffler 	 * the count of long slot time stations and disable
21978a1b9b6aSSam Leffler 	 * use of short slot time.  Note that the actual switch
21988a1b9b6aSSam Leffler 	 * over to long slot time use may not occur until the
21998a1b9b6aSSam Leffler 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
22008a1b9b6aSSam Leffler 	 */
22018a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
22028a1b9b6aSSam Leffler 		ic->ic_longslotsta++;
2203b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2204b105a069SSam Leffler 		    "station needs long slot time, count %d",
2205b105a069SSam Leffler 		    ic->ic_longslotsta);
22068a1b9b6aSSam Leffler 		/* XXX vap's w/ conflicting needs won't work */
220768e8e04eSSam Leffler 		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
220868e8e04eSSam Leffler 			/*
220968e8e04eSSam Leffler 			 * Don't force slot time when switched to turbo
221068e8e04eSSam Leffler 			 * mode as non-ERP stations won't be present; this
221168e8e04eSSam Leffler 			 * need only be done when on the normal G channel.
221268e8e04eSSam Leffler 			 */
22138a1b9b6aSSam Leffler 			ieee80211_set_shortslottime(ic, 0);
22148a1b9b6aSSam Leffler 		}
221568e8e04eSSam Leffler 	}
22168a1b9b6aSSam Leffler 	/*
22178a1b9b6aSSam Leffler 	 * If the new station is not an ERP station
22188a1b9b6aSSam Leffler 	 * then bump the counter and enable protection
22198a1b9b6aSSam Leffler 	 * if configured.
22208a1b9b6aSSam Leffler 	 */
2221b032f27cSSam Leffler 	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
22228a1b9b6aSSam Leffler 		ic->ic_nonerpsta++;
2223b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2224b105a069SSam Leffler 		    "station is !ERP, %d non-ERP stations associated",
2225b105a069SSam Leffler 		    ic->ic_nonerpsta);
22268a1b9b6aSSam Leffler 		/*
22278a1b9b6aSSam Leffler 		 * If station does not support short preamble
22288a1b9b6aSSam Leffler 		 * then we must enable use of Barker preamble.
22298a1b9b6aSSam Leffler 		 */
22308a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2231b032f27cSSam Leffler 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2232b105a069SSam Leffler 			    "%s", "station needs long preamble");
22338a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
22348a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
22358a1b9b6aSSam Leffler 		}
2236b105a069SSam Leffler 		/*
2237b032f27cSSam Leffler 		 * If protection is configured and this is the first
2238b032f27cSSam Leffler 		 * indication we should use protection, enable it.
2239b105a069SSam Leffler 		 */
2240b105a069SSam Leffler 		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2241b105a069SSam Leffler 		    ic->ic_nonerpsta == 1 &&
2242b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2243b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2244b105a069SSam Leffler 			    "%s: enable use of protection\n", __func__);
2245b105a069SSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
22462dc4d8dcSSam Leffler 			ieee80211_notify_erp_locked(ic);
2247b105a069SSam Leffler 		}
22488a1b9b6aSSam Leffler 	} else
22498a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
22508a1b9b6aSSam Leffler }
22518a1b9b6aSSam Leffler 
22528a1b9b6aSSam Leffler void
2253b032f27cSSam Leffler ieee80211_node_join(struct ieee80211_node *ni, int resp)
22548a1b9b6aSSam Leffler {
2255b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2256b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
22578a1b9b6aSSam Leffler 	int newassoc;
22588a1b9b6aSSam Leffler 
22598a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
226068e8e04eSSam Leffler 		uint16_t aid;
22618a1b9b6aSSam Leffler 
2262b032f27cSSam Leffler 		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
22638a1b9b6aSSam Leffler 		/*
22648a1b9b6aSSam Leffler 		 * It would be good to search the bitmap
22658a1b9b6aSSam Leffler 		 * more efficiently, but this will do for now.
22668a1b9b6aSSam Leffler 		 */
2267b032f27cSSam Leffler 		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2268b032f27cSSam Leffler 			if (!IEEE80211_AID_ISSET(vap, aid))
22698a1b9b6aSSam Leffler 				break;
22708a1b9b6aSSam Leffler 		}
2271b032f27cSSam Leffler 		if (aid >= vap->iv_max_aid) {
227213f91245SSam Leffler 			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2273b032f27cSSam Leffler 			ieee80211_node_leave(ni);
22748a1b9b6aSSam Leffler 			return;
22758a1b9b6aSSam Leffler 		}
22768a1b9b6aSSam Leffler 		ni->ni_associd = aid | 0xc000;
22771b6167d2SSam Leffler 		ni->ni_jointime = time_uptime;
2278b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
2279b032f27cSSam Leffler 		IEEE80211_AID_SET(vap, ni->ni_associd);
2280b032f27cSSam Leffler 		vap->iv_sta_assoc++;
22818a1b9b6aSSam Leffler 		ic->ic_sta_assoc++;
22821b6167d2SSam Leffler 
22831b6167d2SSam Leffler 		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
22841b6167d2SSam Leffler 			ieee80211_ht_node_join(ni);
228568e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
228668e8e04eSSam Leffler 		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2287b032f27cSSam Leffler 			ieee80211_node_join_11g(ni);
22881b6167d2SSam Leffler 		IEEE80211_UNLOCK(ic);
22891b6167d2SSam Leffler 
22901b6167d2SSam Leffler 		newassoc = 1;
22918a1b9b6aSSam Leffler 	} else
22928a1b9b6aSSam Leffler 		newassoc = 0;
22938a1b9b6aSSam Leffler 
2294b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
229544f7a6edSSam Leffler 	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2296b8fcf546SSam Leffler 	    IEEE80211_NODE_AID(ni),
2297b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2298b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2299b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
230068e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
230168e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_HT ?
23020f52b1c4SSam Leffler 		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
23031b6167d2SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
23048c070d69SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
23058c070d69SSam Leffler 	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
230644f7a6edSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2307b032f27cSSam Leffler 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
230868e8e04eSSam Leffler 		", fast-frames" : "",
2309b032f27cSSam Leffler 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
231068e8e04eSSam Leffler 		", turbo" : ""
2311b8fcf546SSam Leffler 	);
23128a1b9b6aSSam Leffler 
231301a03542SSam Leffler 	node_setuptxparms(ni);
23148a1b9b6aSSam Leffler 	/* give driver a chance to setup state like ni_txrate */
2315736b3dc3SSam Leffler 	if (ic->ic_newassoc != NULL)
2316e9962332SSam Leffler 		ic->ic_newassoc(ni, newassoc);
2317b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
23188a1b9b6aSSam Leffler 	/* tell the authenticator about new station */
2319b032f27cSSam Leffler 	if (vap->iv_auth->ia_node_join != NULL)
2320b032f27cSSam Leffler 		vap->iv_auth->ia_node_join(ni);
2321b032f27cSSam Leffler 	ieee80211_notify_node_join(ni,
2322ce8977dfSSam Leffler 	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
23238a1b9b6aSSam Leffler }
23248a1b9b6aSSam Leffler 
2325b105a069SSam Leffler static void
2326b105a069SSam Leffler disable_protection(struct ieee80211com *ic)
2327b105a069SSam Leffler {
2328b105a069SSam Leffler 	KASSERT(ic->ic_nonerpsta == 0 &&
2329b105a069SSam Leffler 	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2330b105a069SSam Leffler 	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2331b105a069SSam Leffler 	   ic->ic_flags_ext));
2332b105a069SSam Leffler 
2333b105a069SSam Leffler 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2334b105a069SSam Leffler 	/* XXX verify mode? */
2335b105a069SSam Leffler 	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2336b105a069SSam Leffler 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2337b105a069SSam Leffler 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2338b105a069SSam Leffler 	}
23392dc4d8dcSSam Leffler 	ieee80211_notify_erp_locked(ic);
2340b105a069SSam Leffler }
2341b105a069SSam Leffler 
23428a1b9b6aSSam Leffler /*
23438a1b9b6aSSam Leffler  * Handle a station leaving an 11g network.
23448a1b9b6aSSam Leffler  */
23458a1b9b6aSSam Leffler static void
2346b032f27cSSam Leffler ieee80211_node_leave_11g(struct ieee80211_node *ni)
23478a1b9b6aSSam Leffler {
2348b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
23498a1b9b6aSSam Leffler 
23501b6167d2SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
23511b6167d2SSam Leffler 
235268e8e04eSSam Leffler 	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2353b032f27cSSam Leffler 	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2354b032f27cSSam Leffler 	      ic->ic_bsschan->ic_flags));
23558a1b9b6aSSam Leffler 
23568a1b9b6aSSam Leffler 	/*
23578a1b9b6aSSam Leffler 	 * If a long slot station do the slot time bookkeeping.
23588a1b9b6aSSam Leffler 	 */
23598a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
23608a1b9b6aSSam Leffler 		KASSERT(ic->ic_longslotsta > 0,
23618a1b9b6aSSam Leffler 		    ("bogus long slot station count %d", ic->ic_longslotsta));
23628a1b9b6aSSam Leffler 		ic->ic_longslotsta--;
2363b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2364b105a069SSam Leffler 		    "long slot time station leaves, count now %d",
2365b105a069SSam Leffler 		    ic->ic_longslotsta);
23668a1b9b6aSSam Leffler 		if (ic->ic_longslotsta == 0) {
23678a1b9b6aSSam Leffler 			/*
23688a1b9b6aSSam Leffler 			 * Re-enable use of short slot time if supported
23698a1b9b6aSSam Leffler 			 * and not operating in IBSS mode (per spec).
23708a1b9b6aSSam Leffler 			 */
23718a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
23728a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_IBSS) {
2373b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
2374b032f27cSSam Leffler 				    IEEE80211_MSG_ASSOC,
23758a1b9b6aSSam Leffler 				    "%s: re-enable use of short slot time\n",
23768a1b9b6aSSam Leffler 				    __func__);
23778a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic, 1);
23788a1b9b6aSSam Leffler 			}
23798a1b9b6aSSam Leffler 		}
23808a1b9b6aSSam Leffler 	}
23818a1b9b6aSSam Leffler 	/*
23828a1b9b6aSSam Leffler 	 * If a non-ERP station do the protection-related bookkeeping.
23838a1b9b6aSSam Leffler 	 */
23848a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
23858a1b9b6aSSam Leffler 		KASSERT(ic->ic_nonerpsta > 0,
23868a1b9b6aSSam Leffler 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
23878a1b9b6aSSam Leffler 		ic->ic_nonerpsta--;
2388b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2389b105a069SSam Leffler 		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2390b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2391b105a069SSam Leffler 			" (non-ERP sta present)" : "");
2392b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0 &&
2393b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2394b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
23958a1b9b6aSSam Leffler 				"%s: disable use of protection\n", __func__);
2396b105a069SSam Leffler 			disable_protection(ic);
2397b105a069SSam Leffler 		}
2398b105a069SSam Leffler 	}
2399b105a069SSam Leffler }
2400b105a069SSam Leffler 
2401b105a069SSam Leffler /*
2402b105a069SSam Leffler  * Time out presence of an overlapping bss with non-ERP
2403b105a069SSam Leffler  * stations.  When operating in hostap mode we listen for
2404b105a069SSam Leffler  * beacons from other stations and if we identify a non-ERP
2405b105a069SSam Leffler  * station is present we enable protection.  To identify
2406b105a069SSam Leffler  * when all non-ERP stations are gone we time out this
2407b105a069SSam Leffler  * condition.
2408b105a069SSam Leffler  */
2409b105a069SSam Leffler static void
2410b105a069SSam Leffler ieee80211_erp_timeout(struct ieee80211com *ic)
2411b105a069SSam Leffler {
2412b105a069SSam Leffler 
2413b105a069SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2414b105a069SSam Leffler 
2415b105a069SSam Leffler 	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2416b105a069SSam Leffler 	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2417b032f27cSSam Leffler #if 0
2418b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2419b032f27cSSam Leffler 		    "%s", "age out non-ERP sta present on channel");
2420b032f27cSSam Leffler #endif
2421b105a069SSam Leffler 		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2422b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0)
2423b105a069SSam Leffler 			disable_protection(ic);
24248a1b9b6aSSam Leffler 	}
24258a1b9b6aSSam Leffler }
24268a1b9b6aSSam Leffler 
24278a1b9b6aSSam Leffler /*
24288a1b9b6aSSam Leffler  * Handle bookkeeping for station deauthentication/disassociation
24298a1b9b6aSSam Leffler  * when operating as an ap.
24308a1b9b6aSSam Leffler  */
24318a1b9b6aSSam Leffler void
2432b032f27cSSam Leffler ieee80211_node_leave(struct ieee80211_node *ni)
24338a1b9b6aSSam Leffler {
2434b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2435b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
243644acc00dSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
24378a1b9b6aSSam Leffler 
2438b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2439b105a069SSam Leffler 	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
24408a1b9b6aSSam Leffler 
2441b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2442b032f27cSSam Leffler 		("unexpected operating mode %u", vap->iv_opmode));
24438a1b9b6aSSam Leffler 	/*
24448a1b9b6aSSam Leffler 	 * If node wasn't previously associated all
24458a1b9b6aSSam Leffler 	 * we need to do is reclaim the reference.
24468a1b9b6aSSam Leffler 	 */
24478a1b9b6aSSam Leffler 	/* XXX ibss mode bypasses 11g and notification */
24488a1b9b6aSSam Leffler 	if (ni->ni_associd == 0)
24498a1b9b6aSSam Leffler 		goto done;
24508a1b9b6aSSam Leffler 	/*
24518a1b9b6aSSam Leffler 	 * Tell the authenticator the station is leaving.
24528a1b9b6aSSam Leffler 	 * Note that we must do this before yanking the
24538a1b9b6aSSam Leffler 	 * association id as the authenticator uses the
24548a1b9b6aSSam Leffler 	 * associd to locate it's state block.
24558a1b9b6aSSam Leffler 	 */
2456b032f27cSSam Leffler 	if (vap->iv_auth->ia_node_leave != NULL)
2457b032f27cSSam Leffler 		vap->iv_auth->ia_node_leave(ni);
24581b6167d2SSam Leffler 
24591b6167d2SSam Leffler 	IEEE80211_LOCK(ic);
2460b032f27cSSam Leffler 	IEEE80211_AID_CLR(vap, ni->ni_associd);
24618a1b9b6aSSam Leffler 	ni->ni_associd = 0;
2462b032f27cSSam Leffler 	vap->iv_sta_assoc--;
24638a1b9b6aSSam Leffler 	ic->ic_sta_assoc--;
24648a1b9b6aSSam Leffler 
24651b6167d2SSam Leffler 	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
24661b6167d2SSam Leffler 		ieee80211_ht_node_leave(ni);
246768e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
246868e8e04eSSam Leffler 	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2469b032f27cSSam Leffler 		ieee80211_node_leave_11g(ni);
24701b6167d2SSam Leffler 	IEEE80211_UNLOCK(ic);
24718a1b9b6aSSam Leffler 	/*
24728a1b9b6aSSam Leffler 	 * Cleanup station state.  In particular clear various
24738a1b9b6aSSam Leffler 	 * state that might otherwise be reused if the node
24748a1b9b6aSSam Leffler 	 * is reused before the reference count goes to zero
24758a1b9b6aSSam Leffler 	 * (and memory is reclaimed).
24768a1b9b6aSSam Leffler 	 */
2477b032f27cSSam Leffler 	ieee80211_sta_leave(ni);
24788a1b9b6aSSam Leffler done:
247944acc00dSSam Leffler 	/*
248044acc00dSSam Leffler 	 * Remove the node from any table it's recorded in and
248144acc00dSSam Leffler 	 * drop the caller's reference.  Removal from the table
248244acc00dSSam Leffler 	 * is important to insure the node is not reprocessed
248344acc00dSSam Leffler 	 * for inactivity.
248444acc00dSSam Leffler 	 */
248544acc00dSSam Leffler 	if (nt != NULL) {
248644acc00dSSam Leffler 		IEEE80211_NODE_LOCK(nt);
248744acc00dSSam Leffler 		node_reclaim(nt, ni);
248844acc00dSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
248944acc00dSSam Leffler 	} else
24908a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
24918a1b9b6aSSam Leffler }
24928a1b9b6aSSam Leffler 
2493b032f27cSSam Leffler struct rssiinfo {
2494b032f27cSSam Leffler 	struct ieee80211vap *vap;
2495b032f27cSSam Leffler 	int	rssi_samples;
2496b032f27cSSam Leffler 	uint32_t rssi_total;
2497b032f27cSSam Leffler };
2498b032f27cSSam Leffler 
2499b032f27cSSam Leffler static void
2500b032f27cSSam Leffler get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2501b032f27cSSam Leffler {
2502b032f27cSSam Leffler 	struct rssiinfo *info = arg;
2503b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2504b032f27cSSam Leffler 	int8_t rssi;
2505b032f27cSSam Leffler 
2506b032f27cSSam Leffler 	if (info->vap != vap)
2507b032f27cSSam Leffler 		return;
2508b032f27cSSam Leffler 	/* only associated stations */
2509b032f27cSSam Leffler 	if (ni->ni_associd == 0)
2510b032f27cSSam Leffler 		return;
2511b032f27cSSam Leffler 	rssi = vap->iv_ic->ic_node_getrssi(ni);
2512b032f27cSSam Leffler 	if (rssi != 0) {
2513b032f27cSSam Leffler 		info->rssi_samples++;
2514b032f27cSSam Leffler 		info->rssi_total += rssi;
2515b032f27cSSam Leffler 	}
2516b032f27cSSam Leffler }
2517b032f27cSSam Leffler 
2518b032f27cSSam Leffler static void
2519b032f27cSSam Leffler get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2520b032f27cSSam Leffler {
2521b032f27cSSam Leffler 	struct rssiinfo *info = arg;
2522b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2523b032f27cSSam Leffler 	int8_t rssi;
2524b032f27cSSam Leffler 
2525b032f27cSSam Leffler 	if (info->vap != vap)
2526b032f27cSSam Leffler 		return;
2527b032f27cSSam Leffler 	/* only neighbors */
2528b032f27cSSam Leffler 	/* XXX check bssid */
2529b032f27cSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2530b032f27cSSam Leffler 		return;
2531b032f27cSSam Leffler 	rssi = vap->iv_ic->ic_node_getrssi(ni);
2532b032f27cSSam Leffler 	if (rssi != 0) {
2533b032f27cSSam Leffler 		info->rssi_samples++;
2534b032f27cSSam Leffler 		info->rssi_total += rssi;
2535b032f27cSSam Leffler 	}
2536b032f27cSSam Leffler }
2537b032f27cSSam Leffler 
253868e8e04eSSam Leffler int8_t
2539b032f27cSSam Leffler ieee80211_getrssi(struct ieee80211vap *vap)
25408a1b9b6aSSam Leffler {
25418a1b9b6aSSam Leffler #define	NZ(x)	((x) == 0 ? 1 : (x))
2542b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2543b032f27cSSam Leffler 	struct rssiinfo info;
25448a1b9b6aSSam Leffler 
2545b032f27cSSam Leffler 	info.rssi_total = 0;
2546b032f27cSSam Leffler 	info.rssi_samples = 0;
2547b032f27cSSam Leffler 	info.vap = vap;
2548b032f27cSSam Leffler 	switch (vap->iv_opmode) {
25498a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
25508a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2551b032f27cSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2552b032f27cSSam Leffler 		break;
25538a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2554b032f27cSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
25558a1b9b6aSSam Leffler 		break;
25568a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:	/* XXX */
25578a1b9b6aSSam Leffler 	case IEEE80211_M_STA:		/* use stats from associated ap */
25588a1b9b6aSSam Leffler 	default:
2559b032f27cSSam Leffler 		if (vap->iv_bss != NULL)
2560b032f27cSSam Leffler 			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2561b032f27cSSam Leffler 		info.rssi_samples = 1;
25628a1b9b6aSSam Leffler 		break;
25638a1b9b6aSSam Leffler 	}
2564b032f27cSSam Leffler 	return info.rssi_total / NZ(info.rssi_samples);
25658a1b9b6aSSam Leffler #undef NZ
25668a1b9b6aSSam Leffler }
25678a1b9b6aSSam Leffler 
256868e8e04eSSam Leffler void
2569b032f27cSSam Leffler ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
25708a1b9b6aSSam Leffler {
25718a1b9b6aSSam Leffler 
2572b032f27cSSam Leffler 	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
257368e8e04eSSam Leffler 		return;
2574b032f27cSSam Leffler 	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
257568e8e04eSSam Leffler 	/* for non-station mode return avg'd rssi accounting */
2576b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA)
2577b032f27cSSam Leffler 		*rssi = ieee80211_getrssi(vap);
25788a1b9b6aSSam Leffler }
2579