xref: /freebsd/sys/net80211/ieee80211_node.c (revision c066143c)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
3ae8b7333SSam Leffler  * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
141a1e1d21SSam Leffler  *
157535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251a1e1d21SSam Leffler  */
261a1e1d21SSam Leffler 
271a1e1d21SSam Leffler #include <sys/cdefs.h>
281a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
291a1e1d21SSam Leffler 
301a1e1d21SSam Leffler #include <sys/param.h>
311a1e1d21SSam Leffler #include <sys/systm.h>
321a1e1d21SSam Leffler #include <sys/mbuf.h>
331a1e1d21SSam Leffler #include <sys/malloc.h>
341a1e1d21SSam Leffler #include <sys/kernel.h>
351a1e1d21SSam Leffler 
368a1b9b6aSSam Leffler #include <sys/socket.h>
371a1e1d21SSam Leffler 
381a1e1d21SSam Leffler #include <net/if.h>
391a1e1d21SSam Leffler #include <net/if_media.h>
401a1e1d21SSam Leffler #include <net/ethernet.h>
411a1e1d21SSam Leffler 
421a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
431a1e1d21SSam Leffler 
441a1e1d21SSam Leffler #include <net/bpf.h>
451a1e1d21SSam Leffler 
467268fa64SSam Leffler /*
477268fa64SSam Leffler  * Association id's are managed with a bit vector.
487268fa64SSam Leffler  */
497268fa64SSam Leffler #define	IEEE80211_AID_SET(b, w) \
507268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
517268fa64SSam Leffler #define	IEEE80211_AID_CLR(b, w) \
527268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
537268fa64SSam Leffler #define	IEEE80211_AID_ISSET(b, w) \
547268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
557268fa64SSam Leffler 
56132142c5SDiomidis Spinellis #ifdef IEEE80211_DEBUG_REFCNT
57132142c5SDiomidis Spinellis #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
58132142c5SDiomidis Spinellis #else
59132142c5SDiomidis Spinellis #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
60132142c5SDiomidis Spinellis #endif
61132142c5SDiomidis Spinellis 
6268e8e04eSSam Leffler static int ieee80211_sta_join1(struct ieee80211_node *);
6368e8e04eSSam Leffler 
648a1b9b6aSSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
658a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *);
668a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *);
6768e8e04eSSam Leffler static int8_t node_getrssi(const struct ieee80211_node *);
6868e8e04eSSam Leffler static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
691a1e1d21SSam Leffler 
708a1b9b6aSSam Leffler static void ieee80211_setup_node(struct ieee80211_node_table *,
7168e8e04eSSam Leffler 		struct ieee80211_node *, const uint8_t *);
728a1b9b6aSSam Leffler static void _ieee80211_free_node(struct ieee80211_node *);
738a1b9b6aSSam Leffler 
748a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic,
75c1225b52SSam Leffler 	struct ieee80211_node_table *nt, const char *name,
7668e8e04eSSam Leffler 	int inact, int keymaxix);
7768e8e04eSSam Leffler static void ieee80211_node_table_reset(struct ieee80211_node_table *);
788a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
791a1e1d21SSam Leffler 
8032346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
8137c150c4SSam Leffler 
821a1e1d21SSam Leffler void
838a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic)
841a1e1d21SSam Leffler {
851a1e1d21SSam Leffler 
868a1b9b6aSSam Leffler 	ic->ic_node_alloc = node_alloc;
878a1b9b6aSSam Leffler 	ic->ic_node_free = node_free;
888a1b9b6aSSam Leffler 	ic->ic_node_cleanup = node_cleanup;
898a1b9b6aSSam Leffler 	ic->ic_node_getrssi = node_getrssi;
9068e8e04eSSam Leffler 	ic->ic_node_getsignal = node_getsignal;
918a1b9b6aSSam Leffler 
928a1b9b6aSSam Leffler 	/* default station inactivity timer setings */
938a1b9b6aSSam Leffler 	ic->ic_inact_init = IEEE80211_INACT_INIT;
948a1b9b6aSSam Leffler 	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
958a1b9b6aSSam Leffler 	ic->ic_inact_run = IEEE80211_INACT_RUN;
968a1b9b6aSSam Leffler 	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
978a1b9b6aSSam Leffler 
9868e8e04eSSam Leffler 	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
9968e8e04eSSam Leffler 
100c1225b52SSam Leffler 	/* NB: driver should override */
1018a1b9b6aSSam Leffler 	ic->ic_max_aid = IEEE80211_AID_DEF;
102c066143cSSam Leffler 
103c066143cSSam Leffler 	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
104c1225b52SSam Leffler }
105c1225b52SSam Leffler 
106c1225b52SSam Leffler void
107c1225b52SSam Leffler ieee80211_node_lateattach(struct ieee80211com *ic)
108c1225b52SSam Leffler {
109c1225b52SSam Leffler 	struct ieee80211_rsnparms *rsn;
110c1225b52SSam Leffler 
111c1225b52SSam Leffler 	if (ic->ic_max_aid > IEEE80211_AID_MAX)
1128a1b9b6aSSam Leffler 		ic->ic_max_aid = IEEE80211_AID_MAX;
11368e8e04eSSam Leffler 	MALLOC(ic->ic_aid_bitmap, uint32_t *,
11468e8e04eSSam Leffler 		howmany(ic->ic_max_aid, 32) * sizeof(uint32_t),
11568e8e04eSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
1168a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap == NULL) {
1178a1b9b6aSSam Leffler 		/* XXX no way to recover */
1188a1b9b6aSSam Leffler 		printf("%s: no memory for AID bitmap!\n", __func__);
1198a1b9b6aSSam Leffler 		ic->ic_max_aid = 0;
1208a1b9b6aSSam Leffler 	}
1218a1b9b6aSSam Leffler 
122c1225b52SSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
12368e8e04eSSam Leffler 		IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix);
1242692bb26SSam Leffler 
125c1225b52SSam Leffler 	ieee80211_reset_bss(ic);
1268a1b9b6aSSam Leffler 	/*
1278a1b9b6aSSam Leffler 	 * Setup "global settings" in the bss node so that
1288a1b9b6aSSam Leffler 	 * each new station automatically inherits them.
1298a1b9b6aSSam Leffler 	 */
130c1225b52SSam Leffler 	rsn = &ic->ic_bss->ni_rsn;
1318a1b9b6aSSam Leffler 	/* WEP, TKIP, and AES-CCM are always supported */
1328a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
1338a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
1348a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
1358a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_AES)
1368a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
1378a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_CKIP)
1388a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
1398a1b9b6aSSam Leffler 	/*
1408a1b9b6aSSam Leffler 	 * Default unicast cipher to WEP for 802.1x use.  If
1418a1b9b6aSSam Leffler 	 * WPA is enabled the management code will set these
1428a1b9b6aSSam Leffler 	 * values to reflect.
1438a1b9b6aSSam Leffler 	 */
1448a1b9b6aSSam Leffler 	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
1458a1b9b6aSSam Leffler 	rsn->rsn_ucastkeylen = 104 / NBBY;
1468a1b9b6aSSam Leffler 	/*
1478a1b9b6aSSam Leffler 	 * WPA says the multicast cipher is the lowest unicast
1488a1b9b6aSSam Leffler 	 * cipher supported.  But we skip WEP which would
1498a1b9b6aSSam Leffler 	 * otherwise be used based on this criteria.
1508a1b9b6aSSam Leffler 	 */
1518a1b9b6aSSam Leffler 	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
1528a1b9b6aSSam Leffler 	rsn->rsn_mcastkeylen = 128 / NBBY;
1538a1b9b6aSSam Leffler 
1548a1b9b6aSSam Leffler 	/*
1558a1b9b6aSSam Leffler 	 * We support both WPA-PSK and 802.1x; the one used
1568a1b9b6aSSam Leffler 	 * is determined by the authentication mode and the
1578a1b9b6aSSam Leffler 	 * setting of the PSK state.
1588a1b9b6aSSam Leffler 	 */
1598a1b9b6aSSam Leffler 	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
1608a1b9b6aSSam Leffler 	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1618a1b9b6aSSam Leffler 
162c1225b52SSam Leffler 	ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
1631a1e1d21SSam Leffler }
1641a1e1d21SSam Leffler 
1651a1e1d21SSam Leffler void
1668a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic)
1671a1e1d21SSam Leffler {
1681a1e1d21SSam Leffler 
1698a1b9b6aSSam Leffler 	if (ic->ic_bss != NULL) {
1708a1b9b6aSSam Leffler 		ieee80211_free_node(ic->ic_bss);
1718a1b9b6aSSam Leffler 		ic->ic_bss = NULL;
1728a1b9b6aSSam Leffler 	}
173acc4f7f5SSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_sta);
1748a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap != NULL) {
17568e8e04eSSam Leffler 		FREE(ic->ic_aid_bitmap, M_80211_NODE);
1768a1b9b6aSSam Leffler 		ic->ic_aid_bitmap = NULL;
1778a1b9b6aSSam Leffler 	}
1788a1b9b6aSSam Leffler }
1798a1b9b6aSSam Leffler 
1808a1b9b6aSSam Leffler /*
1818a1b9b6aSSam Leffler  * Port authorize/unauthorize interfaces for use by an authenticator.
1828a1b9b6aSSam Leffler  */
1838a1b9b6aSSam Leffler 
1848a1b9b6aSSam Leffler void
185e4918ecdSSam Leffler ieee80211_node_authorize(struct ieee80211_node *ni)
1868a1b9b6aSSam Leffler {
187e4918ecdSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
188e4918ecdSSam Leffler 
1898a1b9b6aSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AUTH;
1902045f699SSam Leffler 	ni->ni_inact_reload = ic->ic_inact_run;
191c066143cSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
1928a1b9b6aSSam Leffler }
1938a1b9b6aSSam Leffler 
1948a1b9b6aSSam Leffler void
195e4918ecdSSam Leffler ieee80211_node_unauthorize(struct ieee80211_node *ni)
1968a1b9b6aSSam Leffler {
1978a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
198c066143cSSam Leffler 	ni->ni_inact_reload = ic->ic_inact_auth;
199c066143cSSam Leffler 	if (ni->ni_inact > ni->ni_inact_reload)
200c066143cSSam Leffler 		ni->ni_inact = ni->ni_inact_reload;
2018a1b9b6aSSam Leffler }
2028a1b9b6aSSam Leffler 
2038a1b9b6aSSam Leffler /*
2048a1b9b6aSSam Leffler  * Set/change the channel.  The rate set is also updated as
2058a1b9b6aSSam Leffler  * to insure a consistent view by drivers.
2068a1b9b6aSSam Leffler  */
2071b49e120SSam Leffler static void
20868e8e04eSSam Leffler ieee80211_node_set_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
2098a1b9b6aSSam Leffler {
21068e8e04eSSam Leffler 	struct ieee80211_channel *chan = ic->ic_bsschan;
21168e8e04eSSam Leffler 
21268e8e04eSSam Leffler #if 0
21368e8e04eSSam Leffler 	KASSERT(chan != IEEE80211_CHAN_ANYC, ("bss channel not setup"));
21468e8e04eSSam Leffler #else
2151b49e120SSam Leffler 	if (chan == IEEE80211_CHAN_ANYC)	/* XXX while scanning */
2161b49e120SSam Leffler 		chan = ic->ic_curchan;
21768e8e04eSSam Leffler #endif
2188a1b9b6aSSam Leffler 	ni->ni_chan = chan;
21968e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HT(chan)) {
22068e8e04eSSam Leffler 		/*
22168e8e04eSSam Leffler 		 * XXX Gotta be careful here; the rate set returned by
22268e8e04eSSam Leffler 		 * ieee80211_get_suprates is actually any HT rate
22368e8e04eSSam Leffler 		 * set so blindly copying it will be bad.  We must
22468e8e04eSSam Leffler 		 * install the legacy rate est in ni_rates and the
22568e8e04eSSam Leffler 		 * HT rate set in ni_htrates.
22668e8e04eSSam Leffler 		 */
22768e8e04eSSam Leffler 		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
22868e8e04eSSam Leffler 	}
22941b3c790SSam Leffler 	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
2301a1e1d21SSam Leffler }
2311a1e1d21SSam Leffler 
2321a1e1d21SSam Leffler /*
233097131ffSSam Leffler  * Probe the curent channel, if allowed, while scanning.
234097131ffSSam Leffler  * If the channel is not marked passive-only then send
235097131ffSSam Leffler  * a probe request immediately.  Otherwise mark state and
236097131ffSSam Leffler  * listen for beacons on the channel; if we receive something
237097131ffSSam Leffler  * then we'll transmit a probe request.
238097131ffSSam Leffler  */
239097131ffSSam Leffler void
240097131ffSSam Leffler ieee80211_probe_curchan(struct ieee80211com *ic, int force)
241097131ffSSam Leffler {
242097131ffSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
243097131ffSSam Leffler 
244097131ffSSam Leffler 	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) {
245097131ffSSam Leffler 		/*
246097131ffSSam Leffler 		 * XXX send both broadcast+directed probe request
247097131ffSSam Leffler 		 */
248097131ffSSam Leffler 		ieee80211_send_probereq(ic->ic_bss,
249097131ffSSam Leffler 			ic->ic_myaddr, ifp->if_broadcastaddr,
250097131ffSSam Leffler 			ifp->if_broadcastaddr,
25168e8e04eSSam Leffler 			ic->ic_des_ssid[0].ssid, ic->ic_des_ssid[0].len,
252097131ffSSam Leffler 			ic->ic_opt_ie, ic->ic_opt_ie_len);
253097131ffSSam Leffler 	} else
254097131ffSSam Leffler 		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
255097131ffSSam Leffler }
256097131ffSSam Leffler 
257f9cd9174SSam Leffler static __inline void
258f9cd9174SSam Leffler copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
259f9cd9174SSam Leffler {
260f9cd9174SSam Leffler 	/* propagate useful state */
261f9cd9174SSam Leffler 	nbss->ni_authmode = obss->ni_authmode;
262f9cd9174SSam Leffler 	nbss->ni_txpower = obss->ni_txpower;
263f9cd9174SSam Leffler 	nbss->ni_vlan = obss->ni_vlan;
264f9cd9174SSam Leffler 	nbss->ni_rsn = obss->ni_rsn;
265f9cd9174SSam Leffler 	/* XXX statistics? */
266f9cd9174SSam Leffler }
267f9cd9174SSam Leffler 
2681a1e1d21SSam Leffler void
2691a1e1d21SSam Leffler ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
2701a1e1d21SSam Leffler {
271acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt;
2721a1e1d21SSam Leffler 	struct ieee80211_node *ni;
2738a1b9b6aSSam Leffler 
2748a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
2758a1b9b6aSSam Leffler 		"%s: creating ibss\n", __func__);
2768a1b9b6aSSam Leffler 
2778a1b9b6aSSam Leffler 	/*
2788a1b9b6aSSam Leffler 	 * Create the station/neighbor table.  Note that for adhoc
2798a1b9b6aSSam Leffler 	 * mode we make the initial inactivity timer longer since
2808a1b9b6aSSam Leffler 	 * we create nodes only through discovery and they typically
2818a1b9b6aSSam Leffler 	 * are long-lived associations.
2828a1b9b6aSSam Leffler 	 */
283acc4f7f5SSam Leffler 	nt = &ic->ic_sta;
284acc4f7f5SSam Leffler 	IEEE80211_NODE_LOCK(nt);
285acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
286acc4f7f5SSam Leffler 		nt->nt_name = "station";
287acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_init;
288acc4f7f5SSam Leffler 	} else {
289acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
290acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
291acc4f7f5SSam Leffler 	}
292acc4f7f5SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
293acc4f7f5SSam Leffler 
294c1225b52SSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
295acc4f7f5SSam Leffler 	if (ni == NULL) {
296acc4f7f5SSam Leffler 		/* XXX recovery? */
2978a1b9b6aSSam Leffler 		return;
2988a1b9b6aSSam Leffler 	}
2991a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
30068e8e04eSSam Leffler 	ni->ni_esslen = ic->ic_des_ssid[0].len;
30168e8e04eSSam Leffler 	memcpy(ni->ni_essid, ic->ic_des_ssid[0].ssid, ni->ni_esslen);
30268e8e04eSSam Leffler 	if (ic->ic_bss != NULL)
303f9cd9174SSam Leffler 		copy_bss(ni, ic->ic_bss);
304d365f9c7SSam Leffler 	ni->ni_intval = ic->ic_bintval;
3058a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
3061a1e1d21SSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
3071a1e1d21SSam Leffler 	if (ic->ic_phytype == IEEE80211_T_FH) {
3081a1e1d21SSam Leffler 		ni->ni_fhdwell = 200;	/* XXX */
3091a1e1d21SSam Leffler 		ni->ni_fhindex = 1;
3101a1e1d21SSam Leffler 	}
3118a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
3128a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SIBSS;
3138a1b9b6aSSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
31448b0a5beSSam Leffler 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
31548b0a5beSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
316fe49f061SSam Leffler 		else {
317fe49f061SSam Leffler 			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
318fe49f061SSam Leffler 			/* clear group bit, add local bit */
319fe49f061SSam Leffler 			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
320fe49f061SSam Leffler 		}
32150d8b493SSam Leffler 	} else if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
32250d8b493SSam Leffler 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
32350d8b493SSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
32450d8b493SSam Leffler 		else
32550d8b493SSam Leffler 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
3268a1b9b6aSSam Leffler 	}
3278a1b9b6aSSam Leffler 	/*
3288a1b9b6aSSam Leffler 	 * Fix the channel and related attributes.
3298a1b9b6aSSam Leffler 	 */
33068e8e04eSSam Leffler 	ic->ic_bsschan = chan;
33168e8e04eSSam Leffler 	ieee80211_node_set_chan(ic, ni);
33268e8e04eSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(chan);
3338a1b9b6aSSam Leffler 	/*
3348a1b9b6aSSam Leffler 	 * Do mode-specific rate setup.
3358a1b9b6aSSam Leffler 	 */
33668e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_FULL(chan)) {
33768e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(chan)) {
33868e8e04eSSam Leffler 			/*
33968e8e04eSSam Leffler 			 * Use a mixed 11b/11g rate set.
34068e8e04eSSam Leffler 			 */
34168e8e04eSSam Leffler 			ieee80211_set11gbasicrates(&ni->ni_rates,
34268e8e04eSSam Leffler 				IEEE80211_MODE_11G);
34368e8e04eSSam Leffler 		} else if (IEEE80211_IS_CHAN_B(chan)) {
34468e8e04eSSam Leffler 			/*
34568e8e04eSSam Leffler 			 * Force pure 11b rate set.
34668e8e04eSSam Leffler 			 */
34768e8e04eSSam Leffler 			ieee80211_set11gbasicrates(&ni->ni_rates,
34868e8e04eSSam Leffler 				IEEE80211_MODE_11B);
34968e8e04eSSam Leffler 		}
3501a1e1d21SSam Leffler 	}
3511a1e1d21SSam Leffler 
35268e8e04eSSam Leffler 	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
35368e8e04eSSam Leffler }
35468e8e04eSSam Leffler 
35568e8e04eSSam Leffler /*
35668e8e04eSSam Leffler  * Reset bss state on transition to the INIT state.
35768e8e04eSSam Leffler  * Clear any stations from the table (they have been
35868e8e04eSSam Leffler  * deauth'd) and reset the bss node (clears key, rate
35968e8e04eSSam Leffler  * etc. state).
36068e8e04eSSam Leffler  */
3618a1b9b6aSSam Leffler void
3628a1b9b6aSSam Leffler ieee80211_reset_bss(struct ieee80211com *ic)
363b4c5a90fSSam Leffler {
3648a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *obss;
3658a1b9b6aSSam Leffler 
36668e8e04eSSam Leffler 	callout_drain(&ic->ic_inact);
367acc4f7f5SSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta);
36868e8e04eSSam Leffler 	ieee80211_reset_erp(ic);
369acc4f7f5SSam Leffler 
37068e8e04eSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
3718a1b9b6aSSam Leffler 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
3728a1b9b6aSSam Leffler 	obss = ic->ic_bss;
3738a1b9b6aSSam Leffler 	ic->ic_bss = ieee80211_ref_node(ni);
374f9cd9174SSam Leffler 	if (obss != NULL) {
375f9cd9174SSam Leffler 		copy_bss(ni, obss);
376d365f9c7SSam Leffler 		ni->ni_intval = ic->ic_bintval;
3778a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
3788a1b9b6aSSam Leffler 	}
379f9cd9174SSam Leffler }
3808a1b9b6aSSam Leffler 
3818a1b9b6aSSam Leffler static int
38268e8e04eSSam Leffler match_ssid(const struct ieee80211_node *ni,
38368e8e04eSSam Leffler 	int nssid, const struct ieee80211_scan_ssid ssids[])
3848a1b9b6aSSam Leffler {
38568e8e04eSSam Leffler 	int i;
38668e8e04eSSam Leffler 
38768e8e04eSSam Leffler 	for (i = 0; i < nssid; i++) {
38868e8e04eSSam Leffler 		if (ni->ni_esslen == ssids[i].len &&
38968e8e04eSSam Leffler 		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
39068e8e04eSSam Leffler 			return 1;
39168e8e04eSSam Leffler 	}
39268e8e04eSSam Leffler 	return 0;
39368e8e04eSSam Leffler }
39468e8e04eSSam Leffler 
39568e8e04eSSam Leffler /*
39668e8e04eSSam Leffler  * Test a node for suitability/compatibility.
39768e8e04eSSam Leffler  */
39868e8e04eSSam Leffler static int
39968e8e04eSSam Leffler check_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
40068e8e04eSSam Leffler {
40168e8e04eSSam Leffler         uint8_t rate;
40268e8e04eSSam Leffler 
40368e8e04eSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
40468e8e04eSSam Leffler 		return 0;
40568e8e04eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
40668e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
40768e8e04eSSam Leffler 			return 0;
40868e8e04eSSam Leffler 	} else {
40968e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
41068e8e04eSSam Leffler 			return 0;
41168e8e04eSSam Leffler 	}
41268e8e04eSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
41368e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
41468e8e04eSSam Leffler 			return 0;
41568e8e04eSSam Leffler 	} else {
41668e8e04eSSam Leffler 		/* XXX does this mean privacy is supported or required? */
41768e8e04eSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
41868e8e04eSSam Leffler 			return 0;
41968e8e04eSSam Leffler 	}
42068e8e04eSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
42168e8e04eSSam Leffler 	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
42268e8e04eSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
42368e8e04eSSam Leffler 		return 0;
42468e8e04eSSam Leffler 	if (ic->ic_des_nssid != 0 &&
42568e8e04eSSam Leffler 	    !match_ssid(ni, ic->ic_des_nssid, ic->ic_des_ssid))
42668e8e04eSSam Leffler 		return 0;
42768e8e04eSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
42868e8e04eSSam Leffler 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
42968e8e04eSSam Leffler 		return 0;
43068e8e04eSSam Leffler 	return 1;
43168e8e04eSSam Leffler }
43268e8e04eSSam Leffler 
43368e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
43468e8e04eSSam Leffler /*
43568e8e04eSSam Leffler  * Display node suitability/compatibility.
43668e8e04eSSam Leffler  */
43768e8e04eSSam Leffler static void
43868e8e04eSSam Leffler check_bss_debug(struct ieee80211com *ic, struct ieee80211_node *ni)
43968e8e04eSSam Leffler {
44068e8e04eSSam Leffler         uint8_t rate;
441b4c5a90fSSam Leffler         int fail;
442b4c5a90fSSam Leffler 
443b4c5a90fSSam Leffler 	fail = 0;
444b4c5a90fSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
445b4c5a90fSSam Leffler 		fail |= 0x01;
446b4c5a90fSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
447b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
448b4c5a90fSSam Leffler 			fail |= 0x02;
449b4c5a90fSSam Leffler 	} else {
450b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
451b4c5a90fSSam Leffler 			fail |= 0x02;
452b4c5a90fSSam Leffler 	}
4538a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
454b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
455b4c5a90fSSam Leffler 			fail |= 0x04;
456b4c5a90fSSam Leffler 	} else {
457b4c5a90fSSam Leffler 		/* XXX does this mean privacy is supported or required? */
458b4c5a90fSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
459b4c5a90fSSam Leffler 			fail |= 0x04;
460b4c5a90fSSam Leffler 	}
46170e28b9aSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
46279edaebfSSam Leffler 	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
463b4c5a90fSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
464b4c5a90fSSam Leffler 		fail |= 0x08;
46568e8e04eSSam Leffler 	if (ic->ic_des_nssid != 0 &&
46668e8e04eSSam Leffler 	    !match_ssid(ni, ic->ic_des_nssid, ic->ic_des_ssid))
467b4c5a90fSSam Leffler 		fail |= 0x10;
468b4c5a90fSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
469b4c5a90fSSam Leffler 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
470b4c5a90fSSam Leffler 		fail |= 0x20;
47168e8e04eSSam Leffler 
47268e8e04eSSam Leffler 	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
47368e8e04eSSam Leffler 	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
47468e8e04eSSam Leffler 	printf(" %3d%c",
47568e8e04eSSam Leffler 	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
476b4c5a90fSSam Leffler 	printf(" %+4d", ni->ni_rssi);
477b4c5a90fSSam Leffler 	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
478b4c5a90fSSam Leffler 	    fail & 0x08 ? '!' : ' ');
479b4c5a90fSSam Leffler 	printf(" %4s%c",
480b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
481b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
482b4c5a90fSSam Leffler 	    "????",
483b4c5a90fSSam Leffler 	    fail & 0x02 ? '!' : ' ');
484b4c5a90fSSam Leffler 	printf(" %3s%c ",
48568e8e04eSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
486b4c5a90fSSam Leffler 	    fail & 0x04 ? '!' : ' ');
487b4c5a90fSSam Leffler 	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
488b4c5a90fSSam Leffler 	printf("%s\n", fail & 0x10 ? "!" : "");
489b4c5a90fSSam Leffler }
49068e8e04eSSam Leffler #endif /* IEEE80211_DEBUG */
4918a1b9b6aSSam Leffler 
492750d6d0cSSam Leffler /*
4938a1b9b6aSSam Leffler  * Handle 802.11 ad hoc network merge.  The
4948a1b9b6aSSam Leffler  * convention, set by the Wireless Ethernet Compatibility Alliance
4958a1b9b6aSSam Leffler  * (WECA), is that an 802.11 station will change its BSSID to match
4968a1b9b6aSSam Leffler  * the "oldest" 802.11 ad hoc network, on the same channel, that
4978a1b9b6aSSam Leffler  * has the station's desired SSID.  The "oldest" 802.11 network
4988a1b9b6aSSam Leffler  * sends beacons with the greatest TSF timestamp.
4998a1b9b6aSSam Leffler  *
5008a1b9b6aSSam Leffler  * The caller is assumed to validate TSF's before attempting a merge.
5018a1b9b6aSSam Leffler  *
5028a1b9b6aSSam Leffler  * Return !0 if the BSSID changed, 0 otherwise.
503750d6d0cSSam Leffler  */
5048a1b9b6aSSam Leffler int
505641b4d0bSSam Leffler ieee80211_ibss_merge(struct ieee80211_node *ni)
5068a1b9b6aSSam Leffler {
507641b4d0bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
5088a1b9b6aSSam Leffler 
50996acc1b6SSam Leffler 	if (ni == ic->ic_bss ||
51096acc1b6SSam Leffler 	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
5118a1b9b6aSSam Leffler 		/* unchanged, nothing to do */
5128a1b9b6aSSam Leffler 		return 0;
5138a1b9b6aSSam Leffler 	}
51468e8e04eSSam Leffler 	if (!check_bss(ic, ni)) {
51568e8e04eSSam Leffler 		/* capabilities mismatch */
5168a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
5178a1b9b6aSSam Leffler 		    "%s: merge failed, capabilities mismatch\n", __func__);
51868e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
51968e8e04eSSam Leffler 		if (ieee80211_msg_assoc(ic))
52068e8e04eSSam Leffler 			check_bss_debug(ic, ni);
52168e8e04eSSam Leffler #endif
5228a1b9b6aSSam Leffler 		ic->ic_stats.is_ibss_capmismatch++;
5238a1b9b6aSSam Leffler 		return 0;
5248a1b9b6aSSam Leffler 	}
5258a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
5268a1b9b6aSSam Leffler 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
5278a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
5288a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
5298a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
5308a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
5318a1b9b6aSSam Leffler 	);
53268e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
5338a1b9b6aSSam Leffler }
5348a1b9b6aSSam Leffler 
5358a1b9b6aSSam Leffler /*
5368a1b9b6aSSam Leffler  * Join the specified IBSS/BSS network.  The node is assumed to
5378a1b9b6aSSam Leffler  * be passed in with a held reference.
5388a1b9b6aSSam Leffler  */
53968e8e04eSSam Leffler static int
54068e8e04eSSam Leffler ieee80211_sta_join1(struct ieee80211_node *selbs)
5418a1b9b6aSSam Leffler {
54268e8e04eSSam Leffler 	struct ieee80211com *ic = selbs->ni_ic;
5438a1b9b6aSSam Leffler 	struct ieee80211_node *obss;
54468e8e04eSSam Leffler 	int canreassoc;
5458a1b9b6aSSam Leffler 
5468a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
547acc4f7f5SSam Leffler 		struct ieee80211_node_table *nt;
5488a1b9b6aSSam Leffler 		/*
549acc4f7f5SSam Leffler 		 * Fillin the neighbor table; it will already
5503d073929SSam Leffler 		 * exist if we are simply switching mastership.
551acc4f7f5SSam Leffler 		 * XXX ic_sta always setup so this is unnecessary?
5528a1b9b6aSSam Leffler 		 */
553acc4f7f5SSam Leffler 		nt = &ic->ic_sta;
554acc4f7f5SSam Leffler 		IEEE80211_NODE_LOCK(nt);
555acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
556acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
557acc4f7f5SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
5583d073929SSam Leffler 	}
5591a1e1d21SSam Leffler 
5608a1b9b6aSSam Leffler 	/*
5618a1b9b6aSSam Leffler 	 * Committed to selbs, setup state.
5628a1b9b6aSSam Leffler 	 */
5638a1b9b6aSSam Leffler 	obss = ic->ic_bss;
56468e8e04eSSam Leffler 	/*
56568e8e04eSSam Leffler 	 * Check if old+new node have the same address in which
56668e8e04eSSam Leffler 	 * case we can reassociate when operating in sta mode.
56768e8e04eSSam Leffler 	 */
56868e8e04eSSam Leffler 	canreassoc = (obss != NULL &&
56968e8e04eSSam Leffler 		ic->ic_state == IEEE80211_S_RUN &&
57068e8e04eSSam Leffler 		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
571acc4f7f5SSam Leffler 	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
5721fd2349dSSam Leffler 	if (obss != NULL) {
5731fd2349dSSam Leffler 		copy_bss(selbs, obss);
5748a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
5751fd2349dSSam Leffler 	}
57679edaebfSSam Leffler 
57779edaebfSSam Leffler 	/*
57879edaebfSSam Leffler 	 * Delete unusable rates; we've already checked
57979edaebfSSam Leffler 	 * that the negotiated rate set is acceptable.
58079edaebfSSam Leffler 	 */
58170e28b9aSSam Leffler 	ieee80211_fix_rate(ic->ic_bss, &ic->ic_bss->ni_rates,
58270e28b9aSSam Leffler 		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
58379edaebfSSam Leffler 
58468e8e04eSSam Leffler 	ic->ic_bsschan = selbs->ni_chan;
58568e8e04eSSam Leffler 	ic->ic_curchan = ic->ic_bsschan;
58668e8e04eSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
58768e8e04eSSam Leffler 	ic->ic_set_channel(ic);
5888a1b9b6aSSam Leffler 	/*
5898a1b9b6aSSam Leffler 	 * Set the erp state (mostly the slot time) to deal with
5908a1b9b6aSSam Leffler 	 * the auto-select case; this should be redundant if the
5918a1b9b6aSSam Leffler 	 * mode is locked.
5928a1b9b6aSSam Leffler 	 */
5938a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
5948a1b9b6aSSam Leffler 	ieee80211_wme_initparams(ic);
595acc4f7f5SSam Leffler 
59668e8e04eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA) {
59768e8e04eSSam Leffler 		if (canreassoc) {
59868e8e04eSSam Leffler 			/* Reassociate */
59968e8e04eSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC, 1);
60068e8e04eSSam Leffler 		} else {
60168e8e04eSSam Leffler 			/*
60268e8e04eSSam Leffler 			 * Act as if we received a DEAUTH frame in case we
60368e8e04eSSam Leffler 			 * are invoked from the RUN state.  This will cause
60468e8e04eSSam Leffler 			 * us to try to re-authenticate if we are operating
60568e8e04eSSam Leffler 			 * as a station.
60668e8e04eSSam Leffler 			 */
60768e8e04eSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
60868e8e04eSSam Leffler 				IEEE80211_FC0_SUBTYPE_DEAUTH);
60968e8e04eSSam Leffler 		}
61068e8e04eSSam Leffler 	} else
611acc4f7f5SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
6128a1b9b6aSSam Leffler 	return 1;
6138a1b9b6aSSam Leffler }
6148a1b9b6aSSam Leffler 
61568e8e04eSSam Leffler int
61668e8e04eSSam Leffler ieee80211_sta_join(struct ieee80211com *ic,
61768e8e04eSSam Leffler 	const struct ieee80211_scan_entry *se)
61868e8e04eSSam Leffler {
61968e8e04eSSam Leffler 	struct ieee80211_node *ni;
62068e8e04eSSam Leffler 
62168e8e04eSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, se->se_macaddr);
62268e8e04eSSam Leffler 	if (ni == NULL) {
62368e8e04eSSam Leffler 		/* XXX msg */
62468e8e04eSSam Leffler 		return 0;
62568e8e04eSSam Leffler 	}
62668e8e04eSSam Leffler 	/*
62768e8e04eSSam Leffler 	 * Expand scan state into node's format.
62868e8e04eSSam Leffler 	 * XXX may not need all this stuff
62968e8e04eSSam Leffler 	 */
63068e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
63168e8e04eSSam Leffler 	ni->ni_esslen = se->se_ssid[1];
63268e8e04eSSam Leffler 	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
63368e8e04eSSam Leffler 	ni->ni_rstamp = se->se_rstamp;
63468e8e04eSSam Leffler 	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
63568e8e04eSSam Leffler 	ni->ni_intval = se->se_intval;
63668e8e04eSSam Leffler 	ni->ni_capinfo = se->se_capinfo;
63768e8e04eSSam Leffler 	/* XXX shift to 11n channel if htinfo present */
63868e8e04eSSam Leffler 	ni->ni_chan = se->se_chan;
63968e8e04eSSam Leffler 	ni->ni_timoff = se->se_timoff;
64068e8e04eSSam Leffler 	ni->ni_fhdwell = se->se_fhdwell;
64168e8e04eSSam Leffler 	ni->ni_fhindex = se->se_fhindex;
64268e8e04eSSam Leffler 	ni->ni_erp = se->se_erp;
64368e8e04eSSam Leffler 	ni->ni_rssi = se->se_rssi;
64468e8e04eSSam Leffler 	ni->ni_noise = se->se_noise;
64568e8e04eSSam Leffler 	if (se->se_htcap_ie != NULL)
64668e8e04eSSam Leffler 		ieee80211_ht_node_init(ni, se->se_htcap_ie);
64768e8e04eSSam Leffler 	if (se->se_htinfo_ie != NULL)
64868e8e04eSSam Leffler 		ieee80211_parse_htinfo(ni, se->se_htinfo_ie);
64968e8e04eSSam Leffler 	if (se->se_wpa_ie != NULL)
65068e8e04eSSam Leffler 		ieee80211_saveie(&ni->ni_wpa_ie, se->se_wpa_ie);
65168e8e04eSSam Leffler 	if (se->se_rsn_ie != NULL)
65268e8e04eSSam Leffler 		ieee80211_saveie(&ni->ni_rsn_ie, se->se_rsn_ie);
65368e8e04eSSam Leffler 	if (se->se_wme_ie != NULL)
65468e8e04eSSam Leffler 		ieee80211_saveie(&ni->ni_wme_ie, se->se_wme_ie);
65568e8e04eSSam Leffler 	if (se->se_ath_ie != NULL)
65668e8e04eSSam Leffler 		ieee80211_saveath(ni, se->se_ath_ie);
65768e8e04eSSam Leffler 
65868e8e04eSSam Leffler 	ic->ic_dtim_period = se->se_dtimperiod;
65968e8e04eSSam Leffler 	ic->ic_dtim_count = 0;
66068e8e04eSSam Leffler 
66168e8e04eSSam Leffler 	/* NB: must be after ni_chan is setup */
66268e8e04eSSam Leffler 	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
66368e8e04eSSam Leffler 		IEEE80211_F_DOSORT);
66468e8e04eSSam Leffler 	if (se->se_htcap_ie != NULL)
66568e8e04eSSam Leffler 		ieee80211_setup_htrates(ni, se->se_htcap_ie, IEEE80211_F_JOIN);
66668e8e04eSSam Leffler 	if (se->se_htinfo_ie != NULL)
66768e8e04eSSam Leffler 		ieee80211_setup_basic_htrates(ni, se->se_htinfo_ie);
66868e8e04eSSam Leffler 
66968e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
67068e8e04eSSam Leffler }
67168e8e04eSSam Leffler 
6728a1b9b6aSSam Leffler /*
6738a1b9b6aSSam Leffler  * Leave the specified IBSS/BSS network.  The node is assumed to
6748a1b9b6aSSam Leffler  * be passed in with a held reference.
6758a1b9b6aSSam Leffler  */
6768a1b9b6aSSam Leffler void
6778a1b9b6aSSam Leffler ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
6788a1b9b6aSSam Leffler {
6798a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
6808a1b9b6aSSam Leffler 	ieee80211_notify_node_leave(ic, ni);
6818a1b9b6aSSam Leffler }
6828a1b9b6aSSam Leffler 
6831a1e1d21SSam Leffler static struct ieee80211_node *
6848a1b9b6aSSam Leffler node_alloc(struct ieee80211_node_table *nt)
6851a1e1d21SSam Leffler {
686410ca74bSSam Leffler 	struct ieee80211_node *ni;
6878a1b9b6aSSam Leffler 
688410ca74bSSam Leffler 	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
689410ca74bSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
690410ca74bSSam Leffler 	return ni;
6911a1e1d21SSam Leffler }
6921a1e1d21SSam Leffler 
6938a1b9b6aSSam Leffler /*
6948a1b9b6aSSam Leffler  * Reclaim any resources in a node and reset any critical
6958a1b9b6aSSam Leffler  * state.  Typically nodes are free'd immediately after,
6968a1b9b6aSSam Leffler  * but in some cases the storage may be reused so we need
6978a1b9b6aSSam Leffler  * to insure consistent state (should probably fix that).
6988a1b9b6aSSam Leffler  */
6991a1e1d21SSam Leffler static void
7008a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni)
7011a1e1d21SSam Leffler {
7028a1b9b6aSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
7038a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
70468e8e04eSSam Leffler 	int i;
7058a1b9b6aSSam Leffler 
7068a1b9b6aSSam Leffler 	/* NB: preserve ni_table */
7078a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
7088a1b9b6aSSam Leffler 		ic->ic_ps_sta--;
7098a1b9b6aSSam Leffler 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
7108a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
7118a1b9b6aSSam Leffler 		    "[%s] power save mode off, %u sta's in ps mode\n",
7128a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
7138a1b9b6aSSam Leffler 	}
714ebdda46cSSam Leffler 	/*
715ebdda46cSSam Leffler 	 * Clear AREF flag that marks the authorization refcnt bump
716ebdda46cSSam Leffler 	 * has happened.  This is probably not needed as the node
717ebdda46cSSam Leffler 	 * should always be removed from the table so not found but
718ebdda46cSSam Leffler 	 * do it just in case.
719ebdda46cSSam Leffler 	 */
720ebdda46cSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AREF;
7218a1b9b6aSSam Leffler 
7228a1b9b6aSSam Leffler 	/*
7238a1b9b6aSSam Leffler 	 * Drain power save queue and, if needed, clear TIM.
7248a1b9b6aSSam Leffler 	 */
72568e8e04eSSam Leffler 	if (ieee80211_node_saveq_drain(ni) != 0 && ic->ic_set_tim != NULL)
726edfa57d0SSam Leffler 		ic->ic_set_tim(ni, 0);
7278a1b9b6aSSam Leffler 
7288a1b9b6aSSam Leffler 	ni->ni_associd = 0;
7298a1b9b6aSSam Leffler 	if (ni->ni_challenge != NULL) {
73068e8e04eSSam Leffler 		FREE(ni->ni_challenge, M_80211_NODE);
7318a1b9b6aSSam Leffler 		ni->ni_challenge = NULL;
7328a1b9b6aSSam Leffler 	}
7338a1b9b6aSSam Leffler 	/*
7348a1b9b6aSSam Leffler 	 * Preserve SSID, WPA, and WME ie's so the bss node is
7358a1b9b6aSSam Leffler 	 * reusable during a re-auth/re-assoc state transition.
7368a1b9b6aSSam Leffler 	 * If we remove these data they will not be recreated
7378a1b9b6aSSam Leffler 	 * because they come from a probe-response or beacon frame
7388a1b9b6aSSam Leffler 	 * which cannot be expected prior to the association-response.
7398a1b9b6aSSam Leffler 	 * This should not be an issue when operating in other modes
7408a1b9b6aSSam Leffler 	 * as stations leaving always go through a full state transition
7418a1b9b6aSSam Leffler 	 * which will rebuild this state.
7428a1b9b6aSSam Leffler 	 *
7438a1b9b6aSSam Leffler 	 * XXX does this leave us open to inheriting old state?
7448a1b9b6aSSam Leffler 	 */
7458a1b9b6aSSam Leffler 	for (i = 0; i < N(ni->ni_rxfrag); i++)
7468a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[i] != NULL) {
7478a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[i]);
7488a1b9b6aSSam Leffler 			ni->ni_rxfrag[i] = NULL;
7498a1b9b6aSSam Leffler 		}
750c1225b52SSam Leffler 	/*
751c1225b52SSam Leffler 	 * Must be careful here to remove any key map entry w/o a LOR.
752c1225b52SSam Leffler 	 */
753c1225b52SSam Leffler 	ieee80211_node_delucastkey(ni);
7548a1b9b6aSSam Leffler #undef N
7558a1b9b6aSSam Leffler }
7568a1b9b6aSSam Leffler 
7578a1b9b6aSSam Leffler static void
7588a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni)
7598a1b9b6aSSam Leffler {
7608a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
7618a1b9b6aSSam Leffler 
7628a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
7638a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
76468e8e04eSSam Leffler 		FREE(ni->ni_wpa_ie, M_80211_NODE);
76568e8e04eSSam Leffler 	if (ni->ni_rsn_ie != NULL)
76668e8e04eSSam Leffler 		FREE(ni->ni_rsn_ie, M_80211_NODE);
7678a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
76868e8e04eSSam Leffler 		FREE(ni->ni_wme_ie, M_80211_NODE);
76968e8e04eSSam Leffler 	if (ni->ni_ath_ie != NULL)
77068e8e04eSSam Leffler 		FREE(ni->ni_ath_ie, M_80211_NODE);
7718a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DESTROY(ni);
772410ca74bSSam Leffler 	FREE(ni, M_80211_NODE);
7731a1e1d21SSam Leffler }
7741a1e1d21SSam Leffler 
77568e8e04eSSam Leffler static int8_t
7768a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni)
777d1e61976SSam Leffler {
778d1e61976SSam Leffler 	return ni->ni_rssi;
779d1e61976SSam Leffler }
780d1e61976SSam Leffler 
7811a1e1d21SSam Leffler static void
78268e8e04eSSam Leffler node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
78368e8e04eSSam Leffler {
78468e8e04eSSam Leffler 	*rssi = ni->ni_rssi;
78568e8e04eSSam Leffler 	*noise = ni->ni_noise;
78668e8e04eSSam Leffler }
78768e8e04eSSam Leffler 
78868e8e04eSSam Leffler static void
7898a1b9b6aSSam Leffler ieee80211_setup_node(struct ieee80211_node_table *nt,
79068e8e04eSSam Leffler 	struct ieee80211_node *ni, const uint8_t *macaddr)
7911a1e1d21SSam Leffler {
7928a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
7931a1e1d21SSam Leffler 	int hash;
7941a1e1d21SSam Leffler 
7958a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
79649a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
7978a1b9b6aSSam Leffler 		ether_sprintf(macaddr), nt->nt_name);
7988a1b9b6aSSam Leffler 
7991a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
8001a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
8018a1b9b6aSSam Leffler 	ieee80211_node_initref(ni);		/* mark referenced */
8028a1b9b6aSSam Leffler 	ni->ni_chan = IEEE80211_CHAN_ANYC;
8038a1b9b6aSSam Leffler 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
8048a1b9b6aSSam Leffler 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
8058a1b9b6aSSam Leffler 	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
8062045f699SSam Leffler 	ni->ni_inact_reload = nt->nt_inact_init;
8072045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
80868e8e04eSSam Leffler 	ni->ni_ath_defkeyix = 0x7fff;
8098a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
8108a1b9b6aSSam Leffler 
8118a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
8128a1b9b6aSSam Leffler 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
8138a1b9b6aSSam Leffler 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
8148a1b9b6aSSam Leffler 	ni->ni_table = nt;
8158a1b9b6aSSam Leffler 	ni->ni_ic = ic;
8168a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
8171a1e1d21SSam Leffler }
8181a1e1d21SSam Leffler 
8191a1e1d21SSam Leffler struct ieee80211_node *
82068e8e04eSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
8211a1e1d21SSam Leffler {
8228a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
8238a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
8248a1b9b6aSSam Leffler 
8258a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
8261a1e1d21SSam Leffler 	if (ni != NULL)
8278a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
828c64bfa0fSSam Leffler 	else
829c64bfa0fSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
8301a1e1d21SSam Leffler 	return ni;
8311a1e1d21SSam Leffler }
8321a1e1d21SSam Leffler 
83397c973adSSam Leffler /*
83497c973adSSam Leffler  * Craft a temporary node suitable for sending a management frame
83597c973adSSam Leffler  * to the specified station.  We craft only as much state as we
83697c973adSSam Leffler  * need to do the work since the node will be immediately reclaimed
83797c973adSSam Leffler  * once the send completes.
83897c973adSSam Leffler  */
83997c973adSSam Leffler struct ieee80211_node *
84068e8e04eSSam Leffler ieee80211_tmp_node(struct ieee80211com *ic, const uint8_t *macaddr)
84197c973adSSam Leffler {
84297c973adSSam Leffler 	struct ieee80211_node *ni;
84397c973adSSam Leffler 
84497c973adSSam Leffler 	ni = ic->ic_node_alloc(&ic->ic_sta);
84597c973adSSam Leffler 	if (ni != NULL) {
84697c973adSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
84797c973adSSam Leffler 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
84897c973adSSam Leffler 
84997c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
85097c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
85197c973adSSam Leffler 		ieee80211_node_initref(ni);		/* mark referenced */
85297c973adSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
85397c973adSSam Leffler 		/* NB: required by ieee80211_fix_rate */
85468e8e04eSSam Leffler 		ieee80211_node_set_chan(ic, ni);
85597c973adSSam Leffler 		ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
85697c973adSSam Leffler 			IEEE80211_KEYIX_NONE);
85797c973adSSam Leffler 		/* XXX optimize away */
85897c973adSSam Leffler 		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
85997c973adSSam Leffler 
86097c973adSSam Leffler 		ni->ni_table = NULL;		/* NB: pedantic */
86197c973adSSam Leffler 		ni->ni_ic = ic;
86297c973adSSam Leffler 	} else {
86397c973adSSam Leffler 		/* XXX msg */
86497c973adSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
86597c973adSSam Leffler 	}
86697c973adSSam Leffler 	return ni;
86797c973adSSam Leffler }
86897c973adSSam Leffler 
8691a1e1d21SSam Leffler struct ieee80211_node *
87068e8e04eSSam Leffler ieee80211_dup_bss(struct ieee80211_node_table *nt, const uint8_t *macaddr)
8711a1e1d21SSam Leffler {
8728a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
8738a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
8748a1b9b6aSSam Leffler 
8758a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
8761a1e1d21SSam Leffler 	if (ni != NULL) {
8778a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
878694dca64SSam Leffler 		/*
879694dca64SSam Leffler 		 * Inherit from ic_bss.
880694dca64SSam Leffler 		 */
8818a1b9b6aSSam Leffler 		ni->ni_authmode = ic->ic_bss->ni_authmode;
8828a1b9b6aSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
8838a1b9b6aSSam Leffler 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
884694dca64SSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
88568e8e04eSSam Leffler 		ieee80211_node_set_chan(ic, ni);
8868a1b9b6aSSam Leffler 		ni->ni_rsn = ic->ic_bss->ni_rsn;
887694dca64SSam Leffler 	} else
888694dca64SSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
8891a1e1d21SSam Leffler 	return ni;
8901a1e1d21SSam Leffler }
8911a1e1d21SSam Leffler 
892750d6d0cSSam Leffler static struct ieee80211_node *
8938a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
8948a1b9b6aSSam Leffler _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
89568e8e04eSSam Leffler 	const uint8_t *macaddr, const char *func, int line)
8968a1b9b6aSSam Leffler #else
8978a1b9b6aSSam Leffler _ieee80211_find_node(struct ieee80211_node_table *nt,
89868e8e04eSSam Leffler 	const uint8_t *macaddr)
8998a1b9b6aSSam Leffler #endif
9001a1e1d21SSam Leffler {
9011a1e1d21SSam Leffler 	struct ieee80211_node *ni;
9021a1e1d21SSam Leffler 	int hash;
9031a1e1d21SSam Leffler 
9048a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
905750d6d0cSSam Leffler 
9061a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
9078a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
9081a1e1d21SSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
9098a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
9108a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9118a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
91249a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
91349a15236SSam Leffler 			    func, line,
91449a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
9158a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
9168a1b9b6aSSam Leffler #endif
917750d6d0cSSam Leffler 			return ni;
9181a1e1d21SSam Leffler 		}
9191a1e1d21SSam Leffler 	}
920750d6d0cSSam Leffler 	return NULL;
921750d6d0cSSam Leffler }
9228a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9238a1b9b6aSSam Leffler #define	_ieee80211_find_node(nt, mac) \
9248a1b9b6aSSam Leffler 	_ieee80211_find_node_debug(nt, mac, func, line)
9258a1b9b6aSSam Leffler #endif
926750d6d0cSSam Leffler 
927750d6d0cSSam Leffler struct ieee80211_node *
9288a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9298a1b9b6aSSam Leffler ieee80211_find_node_debug(struct ieee80211_node_table *nt,
93068e8e04eSSam Leffler 	const uint8_t *macaddr, const char *func, int line)
9318a1b9b6aSSam Leffler #else
93268e8e04eSSam Leffler ieee80211_find_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
9338a1b9b6aSSam Leffler #endif
934750d6d0cSSam Leffler {
935750d6d0cSSam Leffler 	struct ieee80211_node *ni;
936750d6d0cSSam Leffler 
9378a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
9388a1b9b6aSSam Leffler 	ni = _ieee80211_find_node(nt, macaddr);
9398a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
9401a1e1d21SSam Leffler 	return ni;
9411a1e1d21SSam Leffler }
9421a1e1d21SSam Leffler 
9431a1e1d21SSam Leffler /*
9448a1b9b6aSSam Leffler  * Fake up a node; this handles node discovery in adhoc mode.
9458a1b9b6aSSam Leffler  * Note that for the driver's benefit we we treat this like
9468a1b9b6aSSam Leffler  * an association so the driver has an opportunity to setup
9478a1b9b6aSSam Leffler  * it's private state.
9488a1b9b6aSSam Leffler  */
9498a1b9b6aSSam Leffler struct ieee80211_node *
9508a1b9b6aSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
95168e8e04eSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
9528a1b9b6aSSam Leffler {
9538a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9548a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9558a1b9b6aSSam Leffler 
956be425a0fSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
957be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
9588a1b9b6aSSam Leffler 	ni = ieee80211_dup_bss(nt, macaddr);
9598a1b9b6aSSam Leffler 	if (ni != NULL) {
9608a1b9b6aSSam Leffler 		/* XXX no rate negotiation; just dup */
9618a1b9b6aSSam Leffler 		ni->ni_rates = ic->ic_bss->ni_rates;
962736b3dc3SSam Leffler 		if (ic->ic_newassoc != NULL)
963e9962332SSam Leffler 			ic->ic_newassoc(ni, 1);
9648e292e8eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
9658e292e8eSSam Leffler 			/*
96668e8e04eSSam Leffler 			 * In adhoc demo mode there are no management
96768e8e04eSSam Leffler 			 * frames to use to discover neighbor capabilities,
96868e8e04eSSam Leffler 			 * so blindly propagate the local configuration
96968e8e04eSSam Leffler 			 * so we can do interesting things (e.g. use
97068e8e04eSSam Leffler 			 * WME to disable ACK's).
9718e292e8eSSam Leffler 			 */
9728e292e8eSSam Leffler 			if (ic->ic_flags & IEEE80211_F_WME)
9738e292e8eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_QOS;
97468e8e04eSSam Leffler 			if (ic->ic_flags & IEEE80211_F_FF)
97568e8e04eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_FF;
9768e292e8eSSam Leffler 		}
97768e8e04eSSam Leffler 		/* XXX not right for 802.1x/WPA */
97868e8e04eSSam Leffler 		ieee80211_node_authorize(ni);
9798a1b9b6aSSam Leffler 	}
9808a1b9b6aSSam Leffler 	return ni;
9818a1b9b6aSSam Leffler }
9828a1b9b6aSSam Leffler 
983be425a0fSSam Leffler void
984be425a0fSSam Leffler ieee80211_init_neighbor(struct ieee80211_node *ni,
985be425a0fSSam Leffler 	const struct ieee80211_frame *wh,
986be425a0fSSam Leffler 	const struct ieee80211_scanparams *sp)
987be425a0fSSam Leffler {
988be425a0fSSam Leffler 	ni->ni_esslen = sp->ssid[1];
989be425a0fSSam Leffler 	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
990be425a0fSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
991be425a0fSSam Leffler 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
992be425a0fSSam Leffler 	ni->ni_intval = sp->bintval;
993be425a0fSSam Leffler 	ni->ni_capinfo = sp->capinfo;
994be425a0fSSam Leffler 	ni->ni_chan = ni->ni_ic->ic_curchan;
995be425a0fSSam Leffler 	ni->ni_fhdwell = sp->fhdwell;
996be425a0fSSam Leffler 	ni->ni_fhindex = sp->fhindex;
997be425a0fSSam Leffler 	ni->ni_erp = sp->erp;
998be425a0fSSam Leffler 	ni->ni_timoff = sp->timoff;
999be425a0fSSam Leffler 	if (sp->wme != NULL)
1000be425a0fSSam Leffler 		ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
1001be425a0fSSam Leffler 	if (sp->wpa != NULL)
1002be425a0fSSam Leffler 		ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
100368e8e04eSSam Leffler 	if (sp->rsn != NULL)
100468e8e04eSSam Leffler 		ieee80211_saveie(&ni->ni_rsn_ie, sp->rsn);
100568e8e04eSSam Leffler 	if (sp->ath != NULL)
100668e8e04eSSam Leffler 		ieee80211_saveath(ni, sp->ath);
1007be425a0fSSam Leffler 
1008be425a0fSSam Leffler 	/* NB: must be after ni_chan is setup */
100979edaebfSSam Leffler 	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
101079edaebfSSam Leffler 		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
101179edaebfSSam Leffler 		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1012be425a0fSSam Leffler }
1013be425a0fSSam Leffler 
1014b5c99415SSam Leffler /*
1015b5c99415SSam Leffler  * Do node discovery in adhoc mode on receipt of a beacon
1016b5c99415SSam Leffler  * or probe response frame.  Note that for the driver's
1017b5c99415SSam Leffler  * benefit we we treat this like an association so the
1018b5c99415SSam Leffler  * driver has an opportunity to setup it's private state.
1019b5c99415SSam Leffler  */
1020b5c99415SSam Leffler struct ieee80211_node *
1021b5c99415SSam Leffler ieee80211_add_neighbor(struct ieee80211com *ic,
1022b5c99415SSam Leffler 	const struct ieee80211_frame *wh,
1023b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp)
1024b5c99415SSam Leffler {
1025b5c99415SSam Leffler 	struct ieee80211_node *ni;
1026b5c99415SSam Leffler 
1027be425a0fSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1028be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1029b5c99415SSam Leffler 	ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1030b5c99415SSam Leffler 	if (ni != NULL) {
1031be425a0fSSam Leffler 		ieee80211_init_neighbor(ni, wh, sp);
1032b5c99415SSam Leffler 		if (ic->ic_newassoc != NULL)
1033b5c99415SSam Leffler 			ic->ic_newassoc(ni, 1);
1034b5c99415SSam Leffler 		/* XXX not right for 802.1x/WPA */
1035b5c99415SSam Leffler 		ieee80211_node_authorize(ni);
1036b5c99415SSam Leffler 	}
1037b5c99415SSam Leffler 	return ni;
1038b5c99415SSam Leffler }
1039b5c99415SSam Leffler 
1040c1225b52SSam Leffler #define	IS_CTL(wh) \
1041c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1042c1225b52SSam Leffler #define	IS_PSPOLL(wh) \
1043c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
104468e8e04eSSam Leffler #define	IS_BAR(wh) \
104568e8e04eSSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
104668e8e04eSSam Leffler 
10478a1b9b6aSSam Leffler /*
10488a1b9b6aSSam Leffler  * Locate the node for sender, track state, and then pass the
10498a1b9b6aSSam Leffler  * (referenced) node up to the 802.11 layer for its use.  We
10508a1b9b6aSSam Leffler  * are required to pass some node so we fall back to ic_bss
10518a1b9b6aSSam Leffler  * when this frame is from an unknown sender.  The 802.11 layer
10528a1b9b6aSSam Leffler  * knows this means the sender wasn't in the node table and
10538a1b9b6aSSam Leffler  * acts accordingly.
10548a1b9b6aSSam Leffler  */
10558a1b9b6aSSam Leffler struct ieee80211_node *
10568a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10578a1b9b6aSSam Leffler ieee80211_find_rxnode_debug(struct ieee80211com *ic,
10588a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh, const char *func, int line)
10598a1b9b6aSSam Leffler #else
10608a1b9b6aSSam Leffler ieee80211_find_rxnode(struct ieee80211com *ic,
10618a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh)
10628a1b9b6aSSam Leffler #endif
10638a1b9b6aSSam Leffler {
10648a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
10658a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10668a1b9b6aSSam Leffler 
10678a1b9b6aSSam Leffler 	/* XXX check ic_bss first in station mode */
10688a1b9b6aSSam Leffler 	/* XXX 4-address frames? */
106968e8e04eSSam Leffler 	nt = &ic->ic_sta;
10708a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
107168e8e04eSSam Leffler 	if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
10728a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr1);
10738a1b9b6aSSam Leffler 	else
10748a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr2);
1075c1225b52SSam Leffler 	if (ni == NULL)
1076c1225b52SSam Leffler 		ni = ieee80211_ref_node(ic->ic_bss);
10778a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
10788a1b9b6aSSam Leffler 
1079c1225b52SSam Leffler 	return ni;
1080c1225b52SSam Leffler }
1081c1225b52SSam Leffler 
1082c1225b52SSam Leffler /*
1083c1225b52SSam Leffler  * Like ieee80211_find_rxnode but use the supplied h/w
1084c1225b52SSam Leffler  * key index as a hint to locate the node in the key
1085c1225b52SSam Leffler  * mapping table.  If an entry is present at the key
1086c1225b52SSam Leffler  * index we return it; otherwise do a normal lookup and
1087c1225b52SSam Leffler  * update the mapping table if the station has a unicast
1088c1225b52SSam Leffler  * key assigned to it.
1089c1225b52SSam Leffler  */
1090c1225b52SSam Leffler struct ieee80211_node *
1091c1225b52SSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1092c1225b52SSam Leffler ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1093c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1094c1225b52SSam Leffler 	const char *func, int line)
1095c1225b52SSam Leffler #else
1096c1225b52SSam Leffler ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1097c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1098c1225b52SSam Leffler #endif
1099c1225b52SSam Leffler {
1100c1225b52SSam Leffler 	struct ieee80211_node_table *nt;
1101c1225b52SSam Leffler 	struct ieee80211_node *ni;
1102c1225b52SSam Leffler 
1103c1225b52SSam Leffler 	nt = &ic->ic_sta;
1104c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
1105c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1106c1225b52SSam Leffler 		ni = nt->nt_keyixmap[keyix];
1107c1225b52SSam Leffler 	else
1108c1225b52SSam Leffler 		ni = NULL;
1109c1225b52SSam Leffler 	if (ni == NULL) {
111068e8e04eSSam Leffler 		if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1111c1225b52SSam Leffler 			ni = _ieee80211_find_node(nt, wh->i_addr1);
1112c1225b52SSam Leffler 		else
1113c1225b52SSam Leffler 			ni = _ieee80211_find_node(nt, wh->i_addr2);
1114c1225b52SSam Leffler 		if (ni == NULL)
1115c1225b52SSam Leffler 			ni = ieee80211_ref_node(ic->ic_bss);
1116c1225b52SSam Leffler 		if (nt->nt_keyixmap != NULL) {
1117c1225b52SSam Leffler 			/*
1118c1225b52SSam Leffler 			 * If the station has a unicast key cache slot
1119c1225b52SSam Leffler 			 * assigned update the key->node mapping table.
1120c1225b52SSam Leffler 			 */
1121c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1122c1225b52SSam Leffler 			/* XXX can keyixmap[keyix] != NULL? */
1123c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1124c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == NULL) {
1125c1225b52SSam Leffler 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1126c1225b52SSam Leffler 				    "%s: add key map entry %p<%s> refcnt %d\n",
1127c1225b52SSam Leffler 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1128c1225b52SSam Leffler 				    ieee80211_node_refcnt(ni)+1);
1129c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1130c1225b52SSam Leffler 			}
1131c1225b52SSam Leffler 		}
113268e8e04eSSam Leffler 	} else
1133c1225b52SSam Leffler 		ieee80211_ref_node(ni);
1134c1225b52SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1135c1225b52SSam Leffler 
1136c1225b52SSam Leffler 	return ni;
1137c1225b52SSam Leffler }
113868e8e04eSSam Leffler #undef IS_BAR
11398a1b9b6aSSam Leffler #undef IS_PSPOLL
11408a1b9b6aSSam Leffler #undef IS_CTL
11418a1b9b6aSSam Leffler 
11428a1b9b6aSSam Leffler /*
1143750d6d0cSSam Leffler  * Return a reference to the appropriate node for sending
1144750d6d0cSSam Leffler  * a data frame.  This handles node discovery in adhoc networks.
1145750d6d0cSSam Leffler  */
1146750d6d0cSSam Leffler struct ieee80211_node *
11478a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
114868e8e04eSSam Leffler ieee80211_find_txnode_debug(struct ieee80211com *ic, const uint8_t *macaddr,
11498a1b9b6aSSam Leffler 	const char *func, int line)
11508a1b9b6aSSam Leffler #else
115168e8e04eSSam Leffler ieee80211_find_txnode(struct ieee80211com *ic, const uint8_t *macaddr)
11528a1b9b6aSSam Leffler #endif
1153750d6d0cSSam Leffler {
1154acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1155750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1156750d6d0cSSam Leffler 
1157750d6d0cSSam Leffler 	/*
1158750d6d0cSSam Leffler 	 * The destination address should be in the node table
1159c1225b52SSam Leffler 	 * unless this is a multicast/broadcast frame.  We can
1160c1225b52SSam Leffler 	 * also optimize station mode operation, all frames go
1161c1225b52SSam Leffler 	 * to the bss node.
1162750d6d0cSSam Leffler 	 */
1163750d6d0cSSam Leffler 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
11648a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1165c1225b52SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1166c1225b52SSam Leffler 		ni = ieee80211_ref_node(ic->ic_bss);
1167ee25b8dfSSam Leffler 	else {
11688a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, macaddr);
1169ee25b8dfSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1170ee25b8dfSSam Leffler 		    (ni != NULL && ni->ni_associd == 0)) {
1171ee25b8dfSSam Leffler 			/*
1172ee25b8dfSSam Leffler 			 * Station is not associated; don't permit the
1173ee25b8dfSSam Leffler 			 * data frame to be sent by returning NULL.  This
1174ee25b8dfSSam Leffler 			 * is kinda a kludge but the least intrusive way
1175ee25b8dfSSam Leffler 			 * to add this check into all drivers.
1176ee25b8dfSSam Leffler 			 */
1177ee25b8dfSSam Leffler 			ieee80211_unref_node(&ni);	/* NB: null's ni */
1178ee25b8dfSSam Leffler 		}
1179ee25b8dfSSam Leffler 	}
11808a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
11818a1b9b6aSSam Leffler 
11828a1b9b6aSSam Leffler 	if (ni == NULL) {
11838a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
118490d0d036SSam Leffler 		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
118590d0d036SSam Leffler 			/*
118690d0d036SSam Leffler 			 * In adhoc mode cons up a node for the destination.
118790d0d036SSam Leffler 			 * Note that we need an additional reference for the
118890d0d036SSam Leffler 			 * caller to be consistent with _ieee80211_find_node.
118990d0d036SSam Leffler 			 */
11908a1b9b6aSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
119190d0d036SSam Leffler 			if (ni != NULL)
119290d0d036SSam Leffler 				(void) ieee80211_ref_node(ni);
119390d0d036SSam Leffler 		} else {
11948a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
11958a1b9b6aSSam Leffler 				"[%s] no node, discard frame (%s)\n",
11968a1b9b6aSSam Leffler 				ether_sprintf(macaddr), __func__);
11978a1b9b6aSSam Leffler 			ic->ic_stats.is_tx_nonode++;
1198750d6d0cSSam Leffler 		}
1199750d6d0cSSam Leffler 	}
1200750d6d0cSSam Leffler 	return ni;
1201750d6d0cSSam Leffler }
1202750d6d0cSSam Leffler 
1203750d6d0cSSam Leffler /*
12048a1b9b6aSSam Leffler  * Like find but search based on the ssid too.
12058a1b9b6aSSam Leffler  */
12068a1b9b6aSSam Leffler struct ieee80211_node *
12078a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12088a1b9b6aSSam Leffler ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
120968e8e04eSSam Leffler 	const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid,
12108a1b9b6aSSam Leffler 	const char *func, int line)
12118a1b9b6aSSam Leffler #else
12128a1b9b6aSSam Leffler ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
121368e8e04eSSam Leffler 	const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid)
12148a1b9b6aSSam Leffler #endif
12151a1e1d21SSam Leffler {
1216f02a0bd2SSam Leffler #define	MATCH_SSID(ni, ssid, ssidlen) \
1217f02a0bd2SSam Leffler 	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
121868e8e04eSSam Leffler 	static const uint8_t zeromac[IEEE80211_ADDR_LEN];
12191a1e1d21SSam Leffler 	struct ieee80211_node *ni;
12208a1b9b6aSSam Leffler 	int hash;
12211a1e1d21SSam Leffler 
12228a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1223f02a0bd2SSam Leffler 	/*
1224f02a0bd2SSam Leffler 	 * A mac address that is all zero means match only the ssid;
1225f02a0bd2SSam Leffler 	 * otherwise we must match both.
1226f02a0bd2SSam Leffler 	 */
1227f02a0bd2SSam Leffler 	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1228f02a0bd2SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1229f02a0bd2SSam Leffler 			if (MATCH_SSID(ni, ssid, ssidlen))
1230f02a0bd2SSam Leffler 				break;
1231f02a0bd2SSam Leffler 		}
1232f02a0bd2SSam Leffler 	} else {
1233f02a0bd2SSam Leffler 		hash = IEEE80211_NODE_HASH(macaddr);
12348a1b9b6aSSam Leffler 		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
12358a1b9b6aSSam Leffler 			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1236f02a0bd2SSam Leffler 			    MATCH_SSID(ni, ssid, ssidlen))
1237f02a0bd2SSam Leffler 				break;
1238f02a0bd2SSam Leffler 		}
1239f02a0bd2SSam Leffler 	}
1240f02a0bd2SSam Leffler 	if (ni != NULL) {
12418a1b9b6aSSam Leffler 		ieee80211_ref_node(ni);	/* mark referenced */
124268e8e04eSSam Leffler 		IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1243132142c5SDiomidis Spinellis 		     REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
12448a1b9b6aSSam Leffler 		     ieee80211_node_refcnt(ni));
12458a1b9b6aSSam Leffler 	}
12468a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
12478a1b9b6aSSam Leffler 	return ni;
1248f02a0bd2SSam Leffler #undef MATCH_SSID
12498a1b9b6aSSam Leffler }
12508a1b9b6aSSam Leffler 
12518a1b9b6aSSam Leffler static void
12528a1b9b6aSSam Leffler _ieee80211_free_node(struct ieee80211_node *ni)
12538a1b9b6aSSam Leffler {
12548a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
12558a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
12568a1b9b6aSSam Leffler 
12578a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
125849a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
125949a15236SSam Leffler 		ether_sprintf(ni->ni_macaddr),
12608a1b9b6aSSam Leffler 		nt != NULL ? nt->nt_name : "<gone>");
12618a1b9b6aSSam Leffler 
12628a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
12638a1b9b6aSSam Leffler 	if (nt != NULL) {
12648a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
12658a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
12668a1b9b6aSSam Leffler 	}
12678a1b9b6aSSam Leffler 	ic->ic_node_free(ni);
12688a1b9b6aSSam Leffler }
12698a1b9b6aSSam Leffler 
12708a1b9b6aSSam Leffler void
12718a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12728a1b9b6aSSam Leffler ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
12738a1b9b6aSSam Leffler #else
12748a1b9b6aSSam Leffler ieee80211_free_node(struct ieee80211_node *ni)
12758a1b9b6aSSam Leffler #endif
12768a1b9b6aSSam Leffler {
12778a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
12788a1b9b6aSSam Leffler 
12798a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
128029d368a7SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
128149a15236SSam Leffler 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
12828a1b9b6aSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
12838a1b9b6aSSam Leffler #endif
1284c1225b52SSam Leffler 	if (nt != NULL) {
1285c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
12868a1b9b6aSSam Leffler 		if (ieee80211_node_dectestref(ni)) {
12878a1b9b6aSSam Leffler 			/*
1288c1225b52SSam Leffler 			 * Last reference, reclaim state.
12898a1b9b6aSSam Leffler 			 */
12908a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
1291c1225b52SSam Leffler 		} else if (ieee80211_node_refcnt(ni) == 1 &&
1292c1225b52SSam Leffler 		    nt->nt_keyixmap != NULL) {
1293c1225b52SSam Leffler 			ieee80211_keyix keyix;
1294c1225b52SSam Leffler 			/*
1295c1225b52SSam Leffler 			 * Check for a last reference in the key mapping table.
1296c1225b52SSam Leffler 			 */
1297c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1298c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1299c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == ni) {
1300c1225b52SSam Leffler 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1301c1225b52SSam Leffler 				    "%s: %p<%s> clear key map entry", __func__,
1302c1225b52SSam Leffler 				    ni, ether_sprintf(ni->ni_macaddr));
1303c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = NULL;
1304c1225b52SSam Leffler 				ieee80211_node_decref(ni); /* XXX needed? */
13058a1b9b6aSSam Leffler 				_ieee80211_free_node(ni);
13068a1b9b6aSSam Leffler 			}
13071a1e1d21SSam Leffler 		}
1308c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1309c1225b52SSam Leffler 	} else {
1310c1225b52SSam Leffler 		if (ieee80211_node_dectestref(ni))
1311c1225b52SSam Leffler 			_ieee80211_free_node(ni);
1312c1225b52SSam Leffler 	}
1313c1225b52SSam Leffler }
1314c1225b52SSam Leffler 
1315c1225b52SSam Leffler /*
1316c1225b52SSam Leffler  * Reclaim a unicast key and clear any key cache state.
1317c1225b52SSam Leffler  */
1318c1225b52SSam Leffler int
1319c1225b52SSam Leffler ieee80211_node_delucastkey(struct ieee80211_node *ni)
1320c1225b52SSam Leffler {
1321c1225b52SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1322c1225b52SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1323c1225b52SSam Leffler 	struct ieee80211_node *nikey;
1324c1225b52SSam Leffler 	ieee80211_keyix keyix;
1325c1225b52SSam Leffler 	int isowned, status;
1326c1225b52SSam Leffler 
1327c1225b52SSam Leffler 	/*
1328c1225b52SSam Leffler 	 * NB: We must beware of LOR here; deleting the key
1329c1225b52SSam Leffler 	 * can cause the crypto layer to block traffic updates
1330c1225b52SSam Leffler 	 * which can generate a LOR against the node table lock;
1331c1225b52SSam Leffler 	 * grab it here and stash the key index for our use below.
1332c1225b52SSam Leffler 	 *
1333c1225b52SSam Leffler 	 * Must also beware of recursion on the node table lock.
1334c1225b52SSam Leffler 	 * When called from node_cleanup we may already have
1335c1225b52SSam Leffler 	 * the node table lock held.  Unfortunately there's no
1336c1225b52SSam Leffler 	 * way to separate out this path so we must do this
1337c1225b52SSam Leffler 	 * conditionally.
1338c1225b52SSam Leffler 	 */
1339c1225b52SSam Leffler 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1340c1225b52SSam Leffler 	if (!isowned)
1341c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
1342c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1343c1225b52SSam Leffler 	status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1344c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1345c1225b52SSam Leffler 		nikey = nt->nt_keyixmap[keyix];
1346c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;;
1347c1225b52SSam Leffler 	} else
1348c1225b52SSam Leffler 		nikey = NULL;
1349c1225b52SSam Leffler 	if (!isowned)
1350c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(&ic->ic_sta);
1351c1225b52SSam Leffler 
1352c1225b52SSam Leffler 	if (nikey != NULL) {
1353c1225b52SSam Leffler 		KASSERT(nikey == ni,
1354c1225b52SSam Leffler 			("key map out of sync, ni %p nikey %p", ni, nikey));
1355c1225b52SSam Leffler 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1356c1225b52SSam Leffler 			"%s: delete key map entry %p<%s> refcnt %d\n",
1357c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr),
1358c1225b52SSam Leffler 			ieee80211_node_refcnt(ni)-1);
1359c1225b52SSam Leffler 		ieee80211_free_node(ni);
1360c1225b52SSam Leffler 	}
1361c1225b52SSam Leffler 	return status;
1362c1225b52SSam Leffler }
13631a1e1d21SSam Leffler 
1364303ebc3cSSam Leffler /*
13658a1b9b6aSSam Leffler  * Reclaim a node.  If this is the last reference count then
13668a1b9b6aSSam Leffler  * do the normal free work.  Otherwise remove it from the node
13678a1b9b6aSSam Leffler  * table and mark it gone by clearing the back-reference.
13688a1b9b6aSSam Leffler  */
13698a1b9b6aSSam Leffler static void
13708a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
13718a1b9b6aSSam Leffler {
1372c1225b52SSam Leffler 	ieee80211_keyix keyix;
1373c1225b52SSam Leffler 
1374c1225b52SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
13758a1b9b6aSSam Leffler 
137649a15236SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
137749a15236SSam Leffler 		"%s: remove %p<%s> from %s table, refcnt %d\n",
137849a15236SSam Leffler 		__func__, ni, ether_sprintf(ni->ni_macaddr),
137949a15236SSam Leffler 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1380c1225b52SSam Leffler 	/*
1381c1225b52SSam Leffler 	 * Clear any entry in the unicast key mapping table.
1382c1225b52SSam Leffler 	 * We need to do it here so rx lookups don't find it
1383c1225b52SSam Leffler 	 * in the mapping table even if it's not in the hash
1384c1225b52SSam Leffler 	 * table.  We cannot depend on the mapping table entry
1385c1225b52SSam Leffler 	 * being cleared because the node may not be free'd.
1386c1225b52SSam Leffler 	 */
1387c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1388c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1389c1225b52SSam Leffler 	    nt->nt_keyixmap[keyix] == ni) {
1390c1225b52SSam Leffler 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1391c1225b52SSam Leffler 			"%s: %p<%s> clear key map entry\n",
1392c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr));
1393c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;
1394c1225b52SSam Leffler 		ieee80211_node_decref(ni);	/* NB: don't need free */
1395c1225b52SSam Leffler 	}
13968a1b9b6aSSam Leffler 	if (!ieee80211_node_dectestref(ni)) {
13978a1b9b6aSSam Leffler 		/*
13988a1b9b6aSSam Leffler 		 * Other references are present, just remove the
13998a1b9b6aSSam Leffler 		 * node from the table so it cannot be found.  When
14008a1b9b6aSSam Leffler 		 * the references are dropped storage will be
1401acc4f7f5SSam Leffler 		 * reclaimed.
14028a1b9b6aSSam Leffler 		 */
14038a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
14048a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
14058a1b9b6aSSam Leffler 		ni->ni_table = NULL;		/* clear reference */
14068a1b9b6aSSam Leffler 	} else
14078a1b9b6aSSam Leffler 		_ieee80211_free_node(ni);
14088a1b9b6aSSam Leffler }
14098a1b9b6aSSam Leffler 
14108a1b9b6aSSam Leffler static void
14118a1b9b6aSSam Leffler ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
14128a1b9b6aSSam Leffler {
14138a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
14148a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
14158a1b9b6aSSam Leffler 
14168a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
14178a1b9b6aSSam Leffler 		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
14188a1b9b6aSSam Leffler 
14198a1b9b6aSSam Leffler 	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
14208a1b9b6aSSam Leffler 		if (ni->ni_associd != 0) {
14218a1b9b6aSSam Leffler 			if (ic->ic_auth->ia_node_leave != NULL)
14228a1b9b6aSSam Leffler 				ic->ic_auth->ia_node_leave(ic, ni);
14238a1b9b6aSSam Leffler 			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
14248a1b9b6aSSam Leffler 		}
14258a1b9b6aSSam Leffler 		node_reclaim(nt, ni);
14268a1b9b6aSSam Leffler 	}
14278a1b9b6aSSam Leffler }
14288a1b9b6aSSam Leffler 
14298a1b9b6aSSam Leffler /*
14308a1b9b6aSSam Leffler  * Timeout inactive stations and do related housekeeping.
14318a1b9b6aSSam Leffler  * Note that we cannot hold the node lock while sending a
14328a1b9b6aSSam Leffler  * frame as this would lead to a LOR.  Instead we use a
14338a1b9b6aSSam Leffler  * generation number to mark nodes that we've scanned and
14348a1b9b6aSSam Leffler  * drop the lock and restart a scan if we have to time out
14358a1b9b6aSSam Leffler  * a node.  Since we are single-threaded by virtue of
1436303ebc3cSSam Leffler  * controlling the inactivity timer we can be sure this will
1437303ebc3cSSam Leffler  * process each node only once.
1438303ebc3cSSam Leffler  */
14398a1b9b6aSSam Leffler static void
14408a1b9b6aSSam Leffler ieee80211_timeout_stations(struct ieee80211_node_table *nt)
14411a1e1d21SSam Leffler {
14428a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
1443303ebc3cSSam Leffler 	struct ieee80211_node *ni;
14448a1b9b6aSSam Leffler 	u_int gen;
1445f66d97f6SSam Leffler 	int isadhoc;
14461a1e1d21SSam Leffler 
1447f66d97f6SSam Leffler 	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1448f66d97f6SSam Leffler 		   ic->ic_opmode == IEEE80211_M_AHDEMO);
14498a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
1450a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
1451303ebc3cSSam Leffler restart:
14528a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
14538a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1454303ebc3cSSam Leffler 		if (ni->ni_scangen == gen)	/* previously handled */
1455303ebc3cSSam Leffler 			continue;
1456303ebc3cSSam Leffler 		ni->ni_scangen = gen;
14570a915fadSSam Leffler 		/*
1458ebdda46cSSam Leffler 		 * Ignore entries for which have yet to receive an
1459ebdda46cSSam Leffler 		 * authentication frame.  These are transient and
1460ebdda46cSSam Leffler 		 * will be reclaimed when the last reference to them
1461ebdda46cSSam Leffler 		 * goes away (when frame xmits complete).
1462ebdda46cSSam Leffler 		 */
146368e8e04eSSam Leffler 		if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
146468e8e04eSSam Leffler 		     ic->ic_opmode == IEEE80211_M_STA) &&
146544b666cdSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1466ebdda46cSSam Leffler 			continue;
1467ebdda46cSSam Leffler 		/*
14688a1b9b6aSSam Leffler 		 * Free fragment if not needed anymore
14698a1b9b6aSSam Leffler 		 * (last fragment older than 1s).
14708a1b9b6aSSam Leffler 		 * XXX doesn't belong here
14710a915fadSSam Leffler 		 */
14728a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[0] != NULL &&
14738a1b9b6aSSam Leffler 		    ticks > ni->ni_rxfragstamp + hz) {
14748a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
14758a1b9b6aSSam Leffler 			ni->ni_rxfrag[0] = NULL;
14768a1b9b6aSSam Leffler 		}
1477c066143cSSam Leffler 		if (ni->ni_inact > 0)
1478c066143cSSam Leffler 			ni->ni_inact--;
1479ce647032SSam Leffler 		/*
1480ce647032SSam Leffler 		 * Special case ourself; we may be idle for extended periods
1481ce647032SSam Leffler 		 * of time and regardless reclaiming our state is wrong.
1482ce647032SSam Leffler 		 */
1483ce647032SSam Leffler 		if (ni == ic->ic_bss)
1484ce647032SSam Leffler 			continue;
1485f66d97f6SSam Leffler 		if (ni->ni_associd != 0 || isadhoc) {
14868a1b9b6aSSam Leffler 			/*
148768e8e04eSSam Leffler 			 * Age frames on the power save queue.
14888a1b9b6aSSam Leffler 			 */
148968e8e04eSSam Leffler 			if (ieee80211_node_saveq_age(ni) != 0 &&
149068e8e04eSSam Leffler 			    IEEE80211_NODE_SAVEQ_QLEN(ni) == 0 &&
149168e8e04eSSam Leffler 			    ic->ic_set_tim != NULL)
1492edfa57d0SSam Leffler 				ic->ic_set_tim(ni, 0);
14938a1b9b6aSSam Leffler 			/*
14948a1b9b6aSSam Leffler 			 * Probe the station before time it out.  We
14958a1b9b6aSSam Leffler 			 * send a null data frame which may not be
14968a1b9b6aSSam Leffler 			 * universally supported by drivers (need it
14978a1b9b6aSSam Leffler 			 * for ps-poll support so it should be...).
149868e8e04eSSam Leffler 			 *
149968e8e04eSSam Leffler 			 * XXX don't probe the station unless we've
150068e8e04eSSam Leffler 			 *     received a frame from them (and have
150168e8e04eSSam Leffler 			 *     some idea of the rates they are capable
150268e8e04eSSam Leffler 			 *     of); this will get fixed more properly
150368e8e04eSSam Leffler 			 *     soon with better handling of the rate set.
15048a1b9b6aSSam Leffler 			 */
1505c066143cSSam Leffler 			if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) &&
1506c066143cSSam Leffler 			    (0 < ni->ni_inact &&
1507c066143cSSam Leffler 			     ni->ni_inact <= ic->ic_inact_probe) &&
150868e8e04eSSam Leffler 			    ni->ni_rates.rs_nrates != 0) {
1509f66d97f6SSam Leffler 				IEEE80211_NOTE(ic,
1510f66d97f6SSam Leffler 				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1511f66d97f6SSam Leffler 				    ni, "%s",
1512f66d97f6SSam Leffler 				    "probe station due to inactivity");
151319ad2dd7SSam Leffler 				/*
151419ad2dd7SSam Leffler 				 * Grab a reference before unlocking the table
151519ad2dd7SSam Leffler 				 * so the node cannot be reclaimed before we
151619ad2dd7SSam Leffler 				 * send the frame. ieee80211_send_nulldata
151719ad2dd7SSam Leffler 				 * understands we've done this and reclaims the
151819ad2dd7SSam Leffler 				 * ref for us as needed.
151919ad2dd7SSam Leffler 				 */
152019ad2dd7SSam Leffler 				ieee80211_ref_node(ni);
15218a1b9b6aSSam Leffler 				IEEE80211_NODE_UNLOCK(nt);
1522f62121ceSSam Leffler 				ieee80211_send_nulldata(ni);
15238a1b9b6aSSam Leffler 				/* XXX stat? */
15248a1b9b6aSSam Leffler 				goto restart;
15258a1b9b6aSSam Leffler 			}
15268a1b9b6aSSam Leffler 		}
1527c066143cSSam Leffler 		if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) &&
1528c066143cSSam Leffler 		    ni->ni_inact <= 0) {
1529f66d97f6SSam Leffler 			IEEE80211_NOTE(ic,
1530f66d97f6SSam Leffler 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1531f66d97f6SSam Leffler 			    "station timed out due to inactivity "
1532f66d97f6SSam Leffler 			    "(refcnt %u)", ieee80211_node_refcnt(ni));
15338a1b9b6aSSam Leffler 			/*
15348a1b9b6aSSam Leffler 			 * Send a deauthenticate frame and drop the station.
15358a1b9b6aSSam Leffler 			 * This is somewhat complicated due to reference counts
15368a1b9b6aSSam Leffler 			 * and locking.  At this point a station will typically
15378a1b9b6aSSam Leffler 			 * have a reference count of 1.  ieee80211_node_leave
15388a1b9b6aSSam Leffler 			 * will do a "free" of the node which will drop the
15398a1b9b6aSSam Leffler 			 * reference count.  But in the meantime a reference
15408a1b9b6aSSam Leffler 			 * wil be held by the deauth frame.  The actual reclaim
15418a1b9b6aSSam Leffler 			 * of the node will happen either after the tx is
15428a1b9b6aSSam Leffler 			 * completed or by ieee80211_node_leave.
15438a1b9b6aSSam Leffler 			 *
15448a1b9b6aSSam Leffler 			 * Separately we must drop the node lock before sending
154568e8e04eSSam Leffler 			 * in case the driver takes a lock, as this can result
154668e8e04eSSam Leffler 			 * in a LOR between the node lock and the driver lock.
15478a1b9b6aSSam Leffler 			 */
15488a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
15498a1b9b6aSSam Leffler 			if (ni->ni_associd != 0) {
15501a1e1d21SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
15511a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
15521a1e1d21SSam Leffler 				    IEEE80211_REASON_AUTH_EXPIRE);
15538a1b9b6aSSam Leffler 			}
15548a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
15551be50176SSam Leffler 			ic->ic_stats.is_node_timeout++;
1556303ebc3cSSam Leffler 			goto restart;
1557303ebc3cSSam Leffler 		}
15581a1e1d21SSam Leffler 	}
15598a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15608a1b9b6aSSam Leffler 
15618a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
156268e8e04eSSam Leffler }
15638a1b9b6aSSam Leffler 
156468e8e04eSSam Leffler void
156568e8e04eSSam Leffler ieee80211_node_timeout(void *arg)
156668e8e04eSSam Leffler {
156768e8e04eSSam Leffler 	struct ieee80211com *ic = arg;
156868e8e04eSSam Leffler 
156968e8e04eSSam Leffler 	ieee80211_scan_timeout(ic);
157068e8e04eSSam Leffler 	ieee80211_timeout_stations(&ic->ic_sta);
157168e8e04eSSam Leffler 
157268e8e04eSSam Leffler 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
157368e8e04eSSam Leffler 		ieee80211_node_timeout, ic);
15741a1e1d21SSam Leffler }
15751a1e1d21SSam Leffler 
15761a1e1d21SSam Leffler void
15778a1b9b6aSSam Leffler ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
15781a1e1d21SSam Leffler {
15791a1e1d21SSam Leffler 	struct ieee80211_node *ni;
15808a1b9b6aSSam Leffler 	u_int gen;
15811a1e1d21SSam Leffler 
15828a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
1583a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
15848a1b9b6aSSam Leffler restart:
15858a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
15868a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
15878a1b9b6aSSam Leffler 		if (ni->ni_scangen != gen) {
15888a1b9b6aSSam Leffler 			ni->ni_scangen = gen;
15898a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
15908a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
15911a1e1d21SSam Leffler 			(*f)(arg, ni);
15928a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
15938a1b9b6aSSam Leffler 			goto restart;
15948a1b9b6aSSam Leffler 		}
15958a1b9b6aSSam Leffler 	}
15968a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15978a1b9b6aSSam Leffler 
15988a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
15998a1b9b6aSSam Leffler }
16008a1b9b6aSSam Leffler 
16018a1b9b6aSSam Leffler void
16028a1b9b6aSSam Leffler ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
16038a1b9b6aSSam Leffler {
16048a1b9b6aSSam Leffler 	printf("0x%p: mac %s refcnt %d\n", ni,
16058a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
16068a1b9b6aSSam Leffler 	printf("\tscangen %u authmode %u flags 0x%x\n",
16078a1b9b6aSSam Leffler 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
16088a1b9b6aSSam Leffler 	printf("\tassocid 0x%x txpower %u vlan %u\n",
16098a1b9b6aSSam Leffler 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
16108a1b9b6aSSam Leffler 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1611801df4a5SSam Leffler 		ni->ni_txseqs[IEEE80211_NONQOS_TID],
1612801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
1613801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
16148a1b9b6aSSam Leffler 		ni->ni_rxfragstamp);
161568e8e04eSSam Leffler 	printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
161668e8e04eSSam Leffler 		ni->ni_rstamp, ni->ni_rssi, ni->ni_noise,
161768e8e04eSSam Leffler 		ni->ni_intval, ni->ni_capinfo);
16188a1b9b6aSSam Leffler 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
16198a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
16208a1b9b6aSSam Leffler 		ni->ni_esslen, ni->ni_essid,
16218a1b9b6aSSam Leffler 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
16228a1b9b6aSSam Leffler 	printf("\tfails %u inact %u txrate %u\n",
16238a1b9b6aSSam Leffler 		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
162468e8e04eSSam Leffler 	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
162568e8e04eSSam Leffler 		ni->ni_htcap, ni->ni_htparam,
162668e8e04eSSam Leffler 		ni->ni_htctlchan, ni->ni_ht2ndchan);
162768e8e04eSSam Leffler 	printf("\thtopmode %x htstbc %x chw %u\n",
162868e8e04eSSam Leffler 		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
16298a1b9b6aSSam Leffler }
16308a1b9b6aSSam Leffler 
16318a1b9b6aSSam Leffler void
16328a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt)
16338a1b9b6aSSam Leffler {
16348a1b9b6aSSam Leffler 	ieee80211_iterate_nodes(nt,
16358a1b9b6aSSam Leffler 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
16368a1b9b6aSSam Leffler }
16378a1b9b6aSSam Leffler 
16388a1b9b6aSSam Leffler /*
16398a1b9b6aSSam Leffler  * Handle a station joining an 11g network.
16408a1b9b6aSSam Leffler  */
16418a1b9b6aSSam Leffler static void
16428a1b9b6aSSam Leffler ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
16438a1b9b6aSSam Leffler {
16448a1b9b6aSSam Leffler 
16458a1b9b6aSSam Leffler 	/*
16468a1b9b6aSSam Leffler 	 * Station isn't capable of short slot time.  Bump
16478a1b9b6aSSam Leffler 	 * the count of long slot time stations and disable
16488a1b9b6aSSam Leffler 	 * use of short slot time.  Note that the actual switch
16498a1b9b6aSSam Leffler 	 * over to long slot time use may not occur until the
16508a1b9b6aSSam Leffler 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
16518a1b9b6aSSam Leffler 	 */
16528a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
16538a1b9b6aSSam Leffler 		ic->ic_longslotsta++;
16548a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
16558a1b9b6aSSam Leffler 		    "[%s] station needs long slot time, count %d\n",
16568a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
16578a1b9b6aSSam Leffler 		/* XXX vap's w/ conflicting needs won't work */
165868e8e04eSSam Leffler 		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
165968e8e04eSSam Leffler 			/*
166068e8e04eSSam Leffler 			 * Don't force slot time when switched to turbo
166168e8e04eSSam Leffler 			 * mode as non-ERP stations won't be present; this
166268e8e04eSSam Leffler 			 * need only be done when on the normal G channel.
166368e8e04eSSam Leffler 			 */
16648a1b9b6aSSam Leffler 			ieee80211_set_shortslottime(ic, 0);
16658a1b9b6aSSam Leffler 		}
166668e8e04eSSam Leffler 	}
16678a1b9b6aSSam Leffler 	/*
16688a1b9b6aSSam Leffler 	 * If the new station is not an ERP station
16698a1b9b6aSSam Leffler 	 * then bump the counter and enable protection
16708a1b9b6aSSam Leffler 	 * if configured.
16718a1b9b6aSSam Leffler 	 */
16728a1b9b6aSSam Leffler 	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
16738a1b9b6aSSam Leffler 		ic->ic_nonerpsta++;
16748a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
16758a1b9b6aSSam Leffler 		    "[%s] station is !ERP, %d non-ERP stations associated\n",
16768a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
16778a1b9b6aSSam Leffler 		/*
16788a1b9b6aSSam Leffler 		 * If protection is configured, enable it.
16798a1b9b6aSSam Leffler 		 */
16808a1b9b6aSSam Leffler 		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
16818a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
16828a1b9b6aSSam Leffler 			    "%s: enable use of protection\n", __func__);
16838a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
16848a1b9b6aSSam Leffler 		}
16858a1b9b6aSSam Leffler 		/*
16868a1b9b6aSSam Leffler 		 * If station does not support short preamble
16878a1b9b6aSSam Leffler 		 * then we must enable use of Barker preamble.
16888a1b9b6aSSam Leffler 		 */
16898a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
16908a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
16918a1b9b6aSSam Leffler 			    "[%s] station needs long preamble\n",
16928a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr));
16938a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
16948a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
16958a1b9b6aSSam Leffler 		}
16960912bac9SSam Leffler 		if (ic->ic_nonerpsta == 1)
16970912bac9SSam Leffler 			ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
16988a1b9b6aSSam Leffler 	} else
16998a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
17008a1b9b6aSSam Leffler }
17018a1b9b6aSSam Leffler 
17028a1b9b6aSSam Leffler void
17038a1b9b6aSSam Leffler ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
17048a1b9b6aSSam Leffler {
17058a1b9b6aSSam Leffler 	int newassoc;
17068a1b9b6aSSam Leffler 
17078a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
170868e8e04eSSam Leffler 		uint16_t aid;
17098a1b9b6aSSam Leffler 
17108a1b9b6aSSam Leffler 		/*
17118a1b9b6aSSam Leffler 		 * It would be good to search the bitmap
17128a1b9b6aSSam Leffler 		 * more efficiently, but this will do for now.
17138a1b9b6aSSam Leffler 		 */
17148a1b9b6aSSam Leffler 		for (aid = 1; aid < ic->ic_max_aid; aid++) {
17158a1b9b6aSSam Leffler 			if (!IEEE80211_AID_ISSET(aid,
17168a1b9b6aSSam Leffler 			    ic->ic_aid_bitmap))
17178a1b9b6aSSam Leffler 				break;
17188a1b9b6aSSam Leffler 		}
17198a1b9b6aSSam Leffler 		if (aid >= ic->ic_max_aid) {
17208a1b9b6aSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
17218a1b9b6aSSam Leffler 			    IEEE80211_REASON_ASSOC_TOOMANY);
17228a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
17238a1b9b6aSSam Leffler 			return;
17248a1b9b6aSSam Leffler 		}
17258a1b9b6aSSam Leffler 		ni->ni_associd = aid | 0xc000;
17268a1b9b6aSSam Leffler 		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
17278a1b9b6aSSam Leffler 		ic->ic_sta_assoc++;
17288a1b9b6aSSam Leffler 		newassoc = 1;
172968e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
173068e8e04eSSam Leffler 		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
17318a1b9b6aSSam Leffler 			ieee80211_node_join_11g(ic, ni);
17328a1b9b6aSSam Leffler 	} else
17338a1b9b6aSSam Leffler 		newassoc = 0;
17348a1b9b6aSSam Leffler 
17358a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
173668e8e04eSSam Leffler 	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s%s%s%s\n",
1737b8fcf546SSam Leffler 	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
1738b8fcf546SSam Leffler 	    IEEE80211_NODE_AID(ni),
1739b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
1740b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
1741b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
174268e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
174368e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_HT ?
174468e8e04eSSam Leffler 		(ni->ni_chw == 20 ? ", HT20" : ", HT40") : "",
174568e8e04eSSam Leffler 	    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_FF) ?
174668e8e04eSSam Leffler 		", fast-frames" : "",
174768e8e04eSSam Leffler 	    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_TURBOP) ?
174868e8e04eSSam Leffler 		", turbo" : ""
1749b8fcf546SSam Leffler 	);
17508a1b9b6aSSam Leffler 
17518a1b9b6aSSam Leffler 	/* give driver a chance to setup state like ni_txrate */
1752736b3dc3SSam Leffler 	if (ic->ic_newassoc != NULL)
1753e9962332SSam Leffler 		ic->ic_newassoc(ni, newassoc);
17548a1b9b6aSSam Leffler 	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
17558a1b9b6aSSam Leffler 	/* tell the authenticator about new station */
17568a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_join != NULL)
17578a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_join(ic, ni);
17588a1b9b6aSSam Leffler 	ieee80211_notify_node_join(ic, ni, newassoc);
17598a1b9b6aSSam Leffler }
17608a1b9b6aSSam Leffler 
17618a1b9b6aSSam Leffler /*
17628a1b9b6aSSam Leffler  * Handle a station leaving an 11g network.
17638a1b9b6aSSam Leffler  */
17648a1b9b6aSSam Leffler static void
17658a1b9b6aSSam Leffler ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
17668a1b9b6aSSam Leffler {
17678a1b9b6aSSam Leffler 
176868e8e04eSSam Leffler 	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
176968e8e04eSSam Leffler 	     ("not in 11g, bss %u:0x%x, curmode %u", ic->ic_bsschan->ic_freq,
177068e8e04eSSam Leffler 	      ic->ic_bsschan->ic_flags, ic->ic_curmode));
17718a1b9b6aSSam Leffler 
17728a1b9b6aSSam Leffler 	/*
17738a1b9b6aSSam Leffler 	 * If a long slot station do the slot time bookkeeping.
17748a1b9b6aSSam Leffler 	 */
17758a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
17768a1b9b6aSSam Leffler 		KASSERT(ic->ic_longslotsta > 0,
17778a1b9b6aSSam Leffler 		    ("bogus long slot station count %d", ic->ic_longslotsta));
17788a1b9b6aSSam Leffler 		ic->ic_longslotsta--;
17798a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
17808a1b9b6aSSam Leffler 		    "[%s] long slot time station leaves, count now %d\n",
17818a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
17828a1b9b6aSSam Leffler 		if (ic->ic_longslotsta == 0) {
17838a1b9b6aSSam Leffler 			/*
17848a1b9b6aSSam Leffler 			 * Re-enable use of short slot time if supported
17858a1b9b6aSSam Leffler 			 * and not operating in IBSS mode (per spec).
17868a1b9b6aSSam Leffler 			 */
17878a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
17888a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_IBSS) {
17898a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
17908a1b9b6aSSam Leffler 				    "%s: re-enable use of short slot time\n",
17918a1b9b6aSSam Leffler 				    __func__);
17928a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic, 1);
17938a1b9b6aSSam Leffler 			}
17948a1b9b6aSSam Leffler 		}
17958a1b9b6aSSam Leffler 	}
17968a1b9b6aSSam Leffler 	/*
17978a1b9b6aSSam Leffler 	 * If a non-ERP station do the protection-related bookkeeping.
17988a1b9b6aSSam Leffler 	 */
17998a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
18008a1b9b6aSSam Leffler 		KASSERT(ic->ic_nonerpsta > 0,
18018a1b9b6aSSam Leffler 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
18028a1b9b6aSSam Leffler 		ic->ic_nonerpsta--;
18038a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
18048a1b9b6aSSam Leffler 		    "[%s] non-ERP station leaves, count now %d\n",
18058a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
18068a1b9b6aSSam Leffler 		if (ic->ic_nonerpsta == 0) {
18078a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
18088a1b9b6aSSam Leffler 				"%s: disable use of protection\n", __func__);
18098a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
18108a1b9b6aSSam Leffler 			/* XXX verify mode? */
18118a1b9b6aSSam Leffler 			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
18128a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
18138a1b9b6aSSam Leffler 				    "%s: re-enable use of short preamble\n",
18148a1b9b6aSSam Leffler 				    __func__);
18158a1b9b6aSSam Leffler 				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
18168a1b9b6aSSam Leffler 				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
18178a1b9b6aSSam Leffler 			}
18180912bac9SSam Leffler 			ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
18198a1b9b6aSSam Leffler 		}
18208a1b9b6aSSam Leffler 	}
18218a1b9b6aSSam Leffler }
18228a1b9b6aSSam Leffler 
18238a1b9b6aSSam Leffler /*
18248a1b9b6aSSam Leffler  * Handle bookkeeping for station deauthentication/disassociation
18258a1b9b6aSSam Leffler  * when operating as an ap.
18268a1b9b6aSSam Leffler  */
18278a1b9b6aSSam Leffler void
18288a1b9b6aSSam Leffler ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
18298a1b9b6aSSam Leffler {
183044acc00dSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
18318a1b9b6aSSam Leffler 
18328a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
18338a1b9b6aSSam Leffler 	    "[%s] station with aid %d leaves\n",
18348a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
18358a1b9b6aSSam Leffler 
183668e8e04eSSam Leffler 	KASSERT(ic->ic_opmode != IEEE80211_M_STA,
18378a1b9b6aSSam Leffler 		("unexpected operating mode %u", ic->ic_opmode));
18388a1b9b6aSSam Leffler 	/*
18398a1b9b6aSSam Leffler 	 * If node wasn't previously associated all
18408a1b9b6aSSam Leffler 	 * we need to do is reclaim the reference.
18418a1b9b6aSSam Leffler 	 */
18428a1b9b6aSSam Leffler 	/* XXX ibss mode bypasses 11g and notification */
18438a1b9b6aSSam Leffler 	if (ni->ni_associd == 0)
18448a1b9b6aSSam Leffler 		goto done;
18458a1b9b6aSSam Leffler 	/*
18468a1b9b6aSSam Leffler 	 * Tell the authenticator the station is leaving.
18478a1b9b6aSSam Leffler 	 * Note that we must do this before yanking the
18488a1b9b6aSSam Leffler 	 * association id as the authenticator uses the
18498a1b9b6aSSam Leffler 	 * associd to locate it's state block.
18508a1b9b6aSSam Leffler 	 */
18518a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_leave != NULL)
18528a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_leave(ic, ni);
18538a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
18548a1b9b6aSSam Leffler 	ni->ni_associd = 0;
18558a1b9b6aSSam Leffler 	ic->ic_sta_assoc--;
18568a1b9b6aSSam Leffler 
185768e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
185868e8e04eSSam Leffler 	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
18598a1b9b6aSSam Leffler 		ieee80211_node_leave_11g(ic, ni);
18608a1b9b6aSSam Leffler 	/*
18618a1b9b6aSSam Leffler 	 * Cleanup station state.  In particular clear various
18628a1b9b6aSSam Leffler 	 * state that might otherwise be reused if the node
18638a1b9b6aSSam Leffler 	 * is reused before the reference count goes to zero
18648a1b9b6aSSam Leffler 	 * (and memory is reclaimed).
18658a1b9b6aSSam Leffler 	 */
18668a1b9b6aSSam Leffler 	ieee80211_sta_leave(ic, ni);
18678a1b9b6aSSam Leffler done:
186844acc00dSSam Leffler 	/*
186944acc00dSSam Leffler 	 * Remove the node from any table it's recorded in and
187044acc00dSSam Leffler 	 * drop the caller's reference.  Removal from the table
187144acc00dSSam Leffler 	 * is important to insure the node is not reprocessed
187244acc00dSSam Leffler 	 * for inactivity.
187344acc00dSSam Leffler 	 */
187444acc00dSSam Leffler 	if (nt != NULL) {
187544acc00dSSam Leffler 		IEEE80211_NODE_LOCK(nt);
187644acc00dSSam Leffler 		node_reclaim(nt, ni);
187744acc00dSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
187844acc00dSSam Leffler 	} else
18798a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
18808a1b9b6aSSam Leffler }
18818a1b9b6aSSam Leffler 
188268e8e04eSSam Leffler int8_t
18838a1b9b6aSSam Leffler ieee80211_getrssi(struct ieee80211com *ic)
18848a1b9b6aSSam Leffler {
18858a1b9b6aSSam Leffler #define	NZ(x)	((x) == 0 ? 1 : (x))
1886acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
188768e8e04eSSam Leffler 	int rssi_samples;
188868e8e04eSSam Leffler 	int32_t rssi_total;
18898a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
18908a1b9b6aSSam Leffler 
18918a1b9b6aSSam Leffler 	rssi_total = 0;
18928a1b9b6aSSam Leffler 	rssi_samples = 0;
18938a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
18948a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
18958a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
18968a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
18978a1b9b6aSSam Leffler 		/* XXX locking */
1898acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
189968e8e04eSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
190068e8e04eSSam Leffler 			    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS)) {
190168e8e04eSSam Leffler 				int8_t rssi = ic->ic_node_getrssi(ni);
190268e8e04eSSam Leffler 				if (rssi != 0) {
19038a1b9b6aSSam Leffler 					rssi_samples++;
190468e8e04eSSam Leffler 					rssi_total += rssi;
190568e8e04eSSam Leffler 				}
19068a1b9b6aSSam Leffler 			}
19078a1b9b6aSSam Leffler 		break;
19088a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:	/* XXX */
19098a1b9b6aSSam Leffler 	case IEEE80211_M_STA:		/* use stats from associated ap */
19108a1b9b6aSSam Leffler 	default:
19118a1b9b6aSSam Leffler 		if (ic->ic_bss != NULL)
19128a1b9b6aSSam Leffler 			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
19138a1b9b6aSSam Leffler 		rssi_samples = 1;
19148a1b9b6aSSam Leffler 		break;
19158a1b9b6aSSam Leffler 	}
19168a1b9b6aSSam Leffler 	return rssi_total / NZ(rssi_samples);
19178a1b9b6aSSam Leffler #undef NZ
19188a1b9b6aSSam Leffler }
19198a1b9b6aSSam Leffler 
192068e8e04eSSam Leffler void
192168e8e04eSSam Leffler ieee80211_getsignal(struct ieee80211com *ic, int8_t *rssi, int8_t *noise)
19228a1b9b6aSSam Leffler {
19238a1b9b6aSSam Leffler 
192468e8e04eSSam Leffler 	if (ic->ic_bss == NULL)		/* NB: shouldn't happen */
192568e8e04eSSam Leffler 		return;
192668e8e04eSSam Leffler 	ic->ic_node_getsignal(ic->ic_bss, rssi, noise);
192768e8e04eSSam Leffler 	/* for non-station mode return avg'd rssi accounting */
192868e8e04eSSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA)
192968e8e04eSSam Leffler 		*rssi = ieee80211_getrssi(ic);
19308a1b9b6aSSam Leffler }
19318a1b9b6aSSam Leffler 
19328a1b9b6aSSam Leffler /*
19338a1b9b6aSSam Leffler  * Node table support.
19348a1b9b6aSSam Leffler  */
19358a1b9b6aSSam Leffler 
19368a1b9b6aSSam Leffler static void
19378a1b9b6aSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic,
19388a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt,
193968e8e04eSSam Leffler 	const char *name, int inact, int keyixmax)
19408a1b9b6aSSam Leffler {
19418a1b9b6aSSam Leffler 
19428a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
19438a1b9b6aSSam Leffler 		"%s %s table, inact %u\n", __func__, name, inact);
19448a1b9b6aSSam Leffler 
19458a1b9b6aSSam Leffler 	nt->nt_ic = ic;
19468a1b9b6aSSam Leffler 	/* XXX need unit */
19478a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
19488a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
19498a1b9b6aSSam Leffler 	TAILQ_INIT(&nt->nt_node);
19508a1b9b6aSSam Leffler 	nt->nt_name = name;
19518a1b9b6aSSam Leffler 	nt->nt_scangen = 1;
19528a1b9b6aSSam Leffler 	nt->nt_inact_init = inact;
1953c1225b52SSam Leffler 	nt->nt_keyixmax = keyixmax;
1954c1225b52SSam Leffler 	if (nt->nt_keyixmax > 0) {
1955c1225b52SSam Leffler 		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
1956c1225b52SSam Leffler 			keyixmax * sizeof(struct ieee80211_node *),
1957c1225b52SSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
1958c1225b52SSam Leffler 		if (nt->nt_keyixmap == NULL)
1959c1225b52SSam Leffler 			if_printf(ic->ic_ifp,
1960c1225b52SSam Leffler 			    "Cannot allocate key index map with %u entries\n",
1961c1225b52SSam Leffler 			    keyixmax);
1962c1225b52SSam Leffler 	} else
1963c1225b52SSam Leffler 		nt->nt_keyixmap = NULL;
19648a1b9b6aSSam Leffler }
19658a1b9b6aSSam Leffler 
196668e8e04eSSam Leffler static void
19678a1b9b6aSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt)
19688a1b9b6aSSam Leffler {
19698a1b9b6aSSam Leffler 
19708a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
19718a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
19728a1b9b6aSSam Leffler 
19738a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
19748a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
19758a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
19768a1b9b6aSSam Leffler }
19778a1b9b6aSSam Leffler 
19788a1b9b6aSSam Leffler static void
19798a1b9b6aSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
19808a1b9b6aSSam Leffler {
19818a1b9b6aSSam Leffler 
19828a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
19838a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
19848a1b9b6aSSam Leffler 
1985c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
19868a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
1987c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL) {
1988c1225b52SSam Leffler 		/* XXX verify all entries are NULL */
1989c1225b52SSam Leffler 		int i;
1990c1225b52SSam Leffler 		for (i = 0; i < nt->nt_keyixmax; i++)
1991c1225b52SSam Leffler 			if (nt->nt_keyixmap[i] != NULL)
1992c1225b52SSam Leffler 				printf("%s: %s[%u] still active\n", __func__,
1993c1225b52SSam Leffler 					nt->nt_name, i);
1994c1225b52SSam Leffler 		FREE(nt->nt_keyixmap, M_80211_NODE);
1995c1225b52SSam Leffler 		nt->nt_keyixmap = NULL;
1996c1225b52SSam Leffler 	}
19978a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_DESTROY(nt);
19988a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_DESTROY(nt);
19998a1b9b6aSSam Leffler }
2000