xref: /netbsd-src/sys/net80211/ieee80211_node.c (revision 481d3881954fd794ca5f2d880b68c53a5db8620e)
1*481d3881Srin /*	$NetBSD: ieee80211_node.c,v 1.84 2024/07/05 04:31:53 rin Exp $	*/
2c153ca3cSmaxv 
3c153ca3cSmaxv /*
440e261aaSdyoung  * Copyright (c) 2001 Atsushi Onoe
590634029Sdyoung  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
640e261aaSdyoung  * All rights reserved.
740e261aaSdyoung  *
840e261aaSdyoung  * Redistribution and use in source and binary forms, with or without
940e261aaSdyoung  * modification, are permitted provided that the following conditions
1040e261aaSdyoung  * are met:
1140e261aaSdyoung  * 1. Redistributions of source code must retain the above copyright
1240e261aaSdyoung  *    notice, this list of conditions and the following disclaimer.
1340e261aaSdyoung  * 2. Redistributions in binary form must reproduce the above copyright
1440e261aaSdyoung  *    notice, this list of conditions and the following disclaimer in the
1540e261aaSdyoung  *    documentation and/or other materials provided with the distribution.
1640e261aaSdyoung  * 3. The name of the author may not be used to endorse or promote products
1740e261aaSdyoung  *    derived from this software without specific prior written permission.
1840e261aaSdyoung  *
1940e261aaSdyoung  * Alternatively, this software may be distributed under the terms of the
2040e261aaSdyoung  * GNU General Public License ("GPL") version 2 as published by the Free
2140e261aaSdyoung  * Software Foundation.
2240e261aaSdyoung  *
2340e261aaSdyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2440e261aaSdyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2540e261aaSdyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2640e261aaSdyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2740e261aaSdyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2840e261aaSdyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2940e261aaSdyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3040e261aaSdyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3140e261aaSdyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3240e261aaSdyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3340e261aaSdyoung  */
3440e261aaSdyoung 
3540e261aaSdyoung #include <sys/cdefs.h>
366e8d0b71Sdyoung #ifdef __FreeBSD__
3787515e34Sskrll __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_node.c,v 1.65 2005/08/13 17:50:21 sam Exp $");
3890634029Sdyoung #endif
3990634029Sdyoung #ifdef __NetBSD__
40*481d3881Srin __KERNEL_RCSID(0, "$NetBSD: ieee80211_node.c,v 1.84 2024/07/05 04:31:53 rin Exp $");
416e8d0b71Sdyoung #endif
4240e261aaSdyoung 
431c4a50f1Spooka #ifdef _KERNEL_OPT
4440e261aaSdyoung #include "opt_inet.h"
451c4a50f1Spooka #endif
4640e261aaSdyoung 
4740e261aaSdyoung #include <sys/param.h>
4840e261aaSdyoung #include <sys/systm.h>
4940e261aaSdyoung #include <sys/mbuf.h>
5040e261aaSdyoung #include <sys/malloc.h>
5140e261aaSdyoung #include <sys/kernel.h>
5290634029Sdyoung 
5340e261aaSdyoung #include <sys/socket.h>
5440e261aaSdyoung #include <sys/sockio.h>
5540e261aaSdyoung #include <sys/endian.h>
5640e261aaSdyoung #include <sys/errno.h>
5740e261aaSdyoung #include <sys/proc.h>
5840e261aaSdyoung #include <sys/sysctl.h>
5940e261aaSdyoung 
6040e261aaSdyoung #include <net/if.h>
6140e261aaSdyoung #include <net/if_media.h>
6240e261aaSdyoung #include <net/if_arp.h>
63608fc215Sdyoung #include <net/if_ether.h>
6440e261aaSdyoung #include <net/if_llc.h>
6540e261aaSdyoung 
6690634029Sdyoung #include <net80211/ieee80211_netbsd.h>
6740e261aaSdyoung #include <net80211/ieee80211_var.h>
6840e261aaSdyoung 
6940e261aaSdyoung #include <net/bpf.h>
7040e261aaSdyoung 
7140e261aaSdyoung #ifdef INET
7240e261aaSdyoung #include <netinet/in.h>
73608fc215Sdyoung #include <net/if_ether.h>
74608fc215Sdyoung #endif
7540e261aaSdyoung 
7661fb42b7Sdyoung /*
7761fb42b7Sdyoung  * Association id's are managed with a bit vector.
7861fb42b7Sdyoung  */
7961fb42b7Sdyoung #define	IEEE80211_AID_SET(b, w) \
8061fb42b7Sdyoung 	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
8161fb42b7Sdyoung #define	IEEE80211_AID_CLR(b, w) \
8261fb42b7Sdyoung 	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
8361fb42b7Sdyoung #define	IEEE80211_AID_ISSET(b, w) \
8461fb42b7Sdyoung 	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
8561fb42b7Sdyoung 
8690634029Sdyoung static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
8790634029Sdyoung static void node_cleanup(struct ieee80211_node *);
8890634029Sdyoung static void node_free(struct ieee80211_node *);
89111ee86dSroy static u_int8_t node_getrssi(const struct ieee80211_node *);
909280f4b4Sdyoung 
9190634029Sdyoung static void ieee80211_setup_node(struct ieee80211_node_table *,
9290634029Sdyoung 		struct ieee80211_node *, const u_int8_t *);
9390634029Sdyoung static void _ieee80211_free_node(struct ieee80211_node *);
9490634029Sdyoung static void ieee80211_free_allnodes(struct ieee80211_node_table *);
9590634029Sdyoung 
9690634029Sdyoung static void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
9790634029Sdyoung static void ieee80211_timeout_stations(struct ieee80211_node_table *);
9890634029Sdyoung 
9987515e34Sskrll static void ieee80211_set_tim(struct ieee80211_node *, int set);
10090634029Sdyoung 
10190634029Sdyoung static void ieee80211_node_table_init(struct ieee80211com *ic,
10287515e34Sskrll 	struct ieee80211_node_table *nt, const char *name,
10387515e34Sskrll 	int inact, int keyixmax,
10490634029Sdyoung 	void (*timeout)(struct ieee80211_node_table *));
10590634029Sdyoung static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
10640e261aaSdyoung 
107bd837921Sdyoung MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
1089280f4b4Sdyoung 
10940e261aaSdyoung void
ieee80211_node_attach(struct ieee80211com * ic)110f526e732Smycroft ieee80211_node_attach(struct ieee80211com *ic)
11140e261aaSdyoung {
11240e261aaSdyoung 
11390634029Sdyoung 	ic->ic_node_alloc = node_alloc;
11490634029Sdyoung 	ic->ic_node_free = node_free;
11590634029Sdyoung 	ic->ic_node_cleanup = node_cleanup;
11690634029Sdyoung 	ic->ic_node_getrssi = node_getrssi;
11790634029Sdyoung 
118a136e22aSandvar 	/* default station inactivity timer settings */
11990634029Sdyoung 	ic->ic_inact_init = IEEE80211_INACT_INIT;
12090634029Sdyoung 	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
12190634029Sdyoung 	ic->ic_inact_run = IEEE80211_INACT_RUN;
12290634029Sdyoung 	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
12390634029Sdyoung 
12487515e34Sskrll 	/* NB: driver should override */
12535515831Smycroft 	ic->ic_max_aid = IEEE80211_AID_DEF;
12687515e34Sskrll 	ic->ic_set_tim = ieee80211_set_tim;
12787515e34Sskrll }
12887515e34Sskrll 
12987515e34Sskrll void
ieee80211_node_lateattach(struct ieee80211com * ic)13087515e34Sskrll ieee80211_node_lateattach(struct ieee80211com *ic)
13187515e34Sskrll {
13287515e34Sskrll 	struct ieee80211_rsnparms *rsn;
13387515e34Sskrll 
13487515e34Sskrll 	if (ic->ic_max_aid > IEEE80211_AID_MAX)
13535515831Smycroft 		ic->ic_max_aid = IEEE80211_AID_MAX;
1365a57baa4Schristos 	ic->ic_aid_bitmap = malloc(howmany(ic->ic_max_aid, 32) *
137d47bcd29Schs 	    sizeof(u_int32_t), M_DEVBUF, M_WAITOK | M_ZERO);
13840e261aaSdyoung 
13990634029Sdyoung 	/* XXX defer until using hostap/ibss mode */
14055977f0bSdyoung 	ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
141d47bcd29Schs 	ic->ic_tim_bitmap = malloc(ic->ic_tim_len, M_DEVBUF, M_WAITOK | M_ZERO);
1428abb07d1Sdyoung 
14387515e34Sskrll 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
14487515e34Sskrll 		IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix,
14587515e34Sskrll 		ieee80211_timeout_stations);
14687515e34Sskrll 	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
14787515e34Sskrll 		IEEE80211_INACT_SCAN, 0,
14887515e34Sskrll 		ieee80211_timeout_scan_candidates);
14940e261aaSdyoung 
15087515e34Sskrll 	ieee80211_reset_bss(ic);
15190634029Sdyoung 	/*
15290634029Sdyoung 	 * Setup "global settings" in the bss node so that
15390634029Sdyoung 	 * each new station automatically inherits them.
15490634029Sdyoung 	 */
15587515e34Sskrll 	rsn = &ic->ic_bss->ni_rsn;
15690634029Sdyoung 	/* WEP, TKIP, and AES-CCM are always supported */
15790634029Sdyoung 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
15890634029Sdyoung 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
15990634029Sdyoung 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
16090634029Sdyoung 	if (ic->ic_caps & IEEE80211_C_AES)
16190634029Sdyoung 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
16290634029Sdyoung 	if (ic->ic_caps & IEEE80211_C_CKIP)
16390634029Sdyoung 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
16490634029Sdyoung 	/*
16590634029Sdyoung 	 * Default unicast cipher to WEP for 802.1x use.  If
16690634029Sdyoung 	 * WPA is enabled the management code will set these
16790634029Sdyoung 	 * values to reflect.
16890634029Sdyoung 	 */
16990634029Sdyoung 	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
17090634029Sdyoung 	rsn->rsn_ucastkeylen = 104 / NBBY;
17190634029Sdyoung 	/*
17290634029Sdyoung 	 * WPA says the multicast cipher is the lowest unicast
17390634029Sdyoung 	 * cipher supported.  But we skip WEP which would
17490634029Sdyoung 	 * otherwise be used based on this criteria.
17590634029Sdyoung 	 */
17690634029Sdyoung 	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
17790634029Sdyoung 	rsn->rsn_mcastkeylen = 128 / NBBY;
17890634029Sdyoung 
17990634029Sdyoung 	/*
18090634029Sdyoung 	 * We support both WPA-PSK and 802.1x; the one used
18190634029Sdyoung 	 * is determined by the authentication mode and the
18290634029Sdyoung 	 * setting of the PSK state.
18390634029Sdyoung 	 */
18490634029Sdyoung 	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
18590634029Sdyoung 	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
18690634029Sdyoung 
18787515e34Sskrll 	ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
18840e261aaSdyoung }
18940e261aaSdyoung 
19040e261aaSdyoung void
ieee80211_node_detach(struct ieee80211com * ic)191f526e732Smycroft ieee80211_node_detach(struct ieee80211com *ic)
19240e261aaSdyoung {
19340e261aaSdyoung 
194f526e732Smycroft 	if (ic->ic_bss != NULL) {
19590634029Sdyoung 		ieee80211_free_node(ic->ic_bss);
196f526e732Smycroft 		ic->ic_bss = NULL;
197f526e732Smycroft 	}
19890634029Sdyoung 	ieee80211_node_table_cleanup(&ic->ic_scan);
19990634029Sdyoung 	ieee80211_node_table_cleanup(&ic->ic_sta);
20090634029Sdyoung 	if (ic->ic_aid_bitmap != NULL) {
2019b87d582Scegger 		free(ic->ic_aid_bitmap, M_DEVBUF);
20290634029Sdyoung 		ic->ic_aid_bitmap = NULL;
20390634029Sdyoung 	}
20490634029Sdyoung 	if (ic->ic_tim_bitmap != NULL) {
2059b87d582Scegger 		free(ic->ic_tim_bitmap, M_DEVBUF);
20690634029Sdyoung 		ic->ic_tim_bitmap = NULL;
20790634029Sdyoung 	}
20890634029Sdyoung }
20990634029Sdyoung 
21090634029Sdyoung /*
21190634029Sdyoung  * Port authorize/unauthorize interfaces for use by an authenticator.
21290634029Sdyoung  */
21390634029Sdyoung 
21490634029Sdyoung void
ieee80211_node_authorize(struct ieee80211_node * ni)21587515e34Sskrll ieee80211_node_authorize(struct ieee80211_node *ni)
21690634029Sdyoung {
21787515e34Sskrll 	struct ieee80211com *ic = ni->ni_ic;
21887515e34Sskrll 
21990634029Sdyoung 	ni->ni_flags |= IEEE80211_NODE_AUTH;
22090634029Sdyoung 	ni->ni_inact_reload = ic->ic_inact_run;
22190634029Sdyoung }
22290634029Sdyoung 
22390634029Sdyoung void
ieee80211_node_unauthorize(struct ieee80211_node * ni)22487515e34Sskrll ieee80211_node_unauthorize(struct ieee80211_node *ni)
22590634029Sdyoung {
22690634029Sdyoung 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
22790634029Sdyoung }
22890634029Sdyoung 
22990634029Sdyoung /*
23090634029Sdyoung  * Set/change the channel.  The rate set is also updated as
23190634029Sdyoung  * to insure a consistent view by drivers.
23290634029Sdyoung  */
23325e9e914Sdyoung static void
ieee80211_set_chan(struct ieee80211com * ic,struct ieee80211_node * ni,struct ieee80211_channel * chan)23490634029Sdyoung ieee80211_set_chan(struct ieee80211com *ic,
23590634029Sdyoung 	struct ieee80211_node *ni, struct ieee80211_channel *chan)
23690634029Sdyoung {
23725e9e914Sdyoung 	if (chan == IEEE80211_CHAN_ANYC)	/* XXX while scanning */
23825e9e914Sdyoung 		chan = ic->ic_curchan;
23990634029Sdyoung 	ni->ni_chan = chan;
24090634029Sdyoung 	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
24140e261aaSdyoung }
24240e261aaSdyoung 
24340e261aaSdyoung /*
24440e261aaSdyoung  * AP scanning support.
24540e261aaSdyoung  */
24640e261aaSdyoung 
24790634029Sdyoung #ifdef IEEE80211_DEBUG
24890634029Sdyoung static void
dump_chanlist(const u_char chans[])24990634029Sdyoung dump_chanlist(const u_char chans[])
25090634029Sdyoung {
25190634029Sdyoung 	const char *sep;
25290634029Sdyoung 	int i;
25390634029Sdyoung 
25490634029Sdyoung 	sep = " ";
25590634029Sdyoung 	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
25690634029Sdyoung 		if (isset(chans, i)) {
25790634029Sdyoung 			printf("%s%u", sep, i);
25890634029Sdyoung 			sep = ", ";
25990634029Sdyoung 		}
26090634029Sdyoung }
26190634029Sdyoung #endif /* IEEE80211_DEBUG */
26290634029Sdyoung 
26340e261aaSdyoung /*
26490634029Sdyoung  * Initialize the channel set to scan based on the
26540e261aaSdyoung  * of available channels and the current PHY mode.
26640e261aaSdyoung  */
26740e261aaSdyoung static void
ieee80211_reset_scan(struct ieee80211com * ic)268f526e732Smycroft ieee80211_reset_scan(struct ieee80211com *ic)
26940e261aaSdyoung {
27040e261aaSdyoung 
27190634029Sdyoung 	/* XXX ic_des_chan should be handled with ic_chan_active */
27290634029Sdyoung 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
27390634029Sdyoung 		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
27490634029Sdyoung 		setbit(ic->ic_chan_scan,
27590634029Sdyoung 			ieee80211_chan2ieee(ic, ic->ic_des_chan));
27690634029Sdyoung 	} else
27740e261aaSdyoung 		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
27840e261aaSdyoung 			sizeof(ic->ic_chan_active));
27990634029Sdyoung #ifdef IEEE80211_DEBUG
28090634029Sdyoung 	if (ieee80211_msg_scan(ic)) {
28190634029Sdyoung 		printf("%s: scan set:", __func__);
28290634029Sdyoung 		dump_chanlist(ic->ic_chan_scan);
28390634029Sdyoung 		printf(" start chan %u\n",
28487515e34Sskrll 			ieee80211_chan2ieee(ic, ic->ic_curchan));
28590634029Sdyoung 	}
28690634029Sdyoung #endif /* IEEE80211_DEBUG */
28740e261aaSdyoung }
28840e261aaSdyoung 
28940e261aaSdyoung /*
29040e261aaSdyoung  * Begin an active scan.
29140e261aaSdyoung  */
29240e261aaSdyoung void
ieee80211_begin_scan(struct ieee80211com * ic,int reset)29390634029Sdyoung ieee80211_begin_scan(struct ieee80211com *ic, int reset)
29440e261aaSdyoung {
29590634029Sdyoung 	ic->ic_scan.nt_scangen++;
296c153ca3cSmaxv 
29740e261aaSdyoung 	/*
29840e261aaSdyoung 	 * In all but hostap mode scanning starts off in
29940e261aaSdyoung 	 * an active mode before switching to passive.
30040e261aaSdyoung 	 */
3019280f4b4Sdyoung 	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
30240e261aaSdyoung 		ic->ic_flags |= IEEE80211_F_ASCAN;
3039280f4b4Sdyoung 		ic->ic_stats.is_scan_active++;
3049280f4b4Sdyoung 	} else
3059280f4b4Sdyoung 		ic->ic_stats.is_scan_passive++;
30690634029Sdyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
30790634029Sdyoung 		"begin %s scan in %s mode, scangen %u\n",
30890634029Sdyoung 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
30990634029Sdyoung 		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
310c153ca3cSmaxv 
31140e261aaSdyoung 	/*
31290634029Sdyoung 	 * Clear scan state and flush any previously seen AP's.
31340e261aaSdyoung 	 */
314f526e732Smycroft 	ieee80211_reset_scan(ic);
31590634029Sdyoung 	if (reset)
31690634029Sdyoung 		ieee80211_free_allnodes(&ic->ic_scan);
31790634029Sdyoung 
31890634029Sdyoung 	ic->ic_flags |= IEEE80211_F_SCAN;
31940e261aaSdyoung 
32040e261aaSdyoung 	/* Scan the next channel. */
321f526e732Smycroft 	ieee80211_next_scan(ic);
32240e261aaSdyoung }
32340e261aaSdyoung 
32440e261aaSdyoung /*
32540e261aaSdyoung  * Switch to the next channel marked for scanning.
32640e261aaSdyoung  */
32790634029Sdyoung int
ieee80211_next_scan(struct ieee80211com * ic)328f526e732Smycroft ieee80211_next_scan(struct ieee80211com *ic)
32940e261aaSdyoung {
33040e261aaSdyoung 	struct ieee80211_channel *chan;
33140e261aaSdyoung 
33290634029Sdyoung 	/*
33390634029Sdyoung 	 * Insure any previous mgt frame timeouts don't fire.
33490634029Sdyoung 	 * This assumes the driver does the right thing in
33590634029Sdyoung 	 * flushing anything queued in the driver and below.
33690634029Sdyoung 	 */
33790634029Sdyoung 	ic->ic_mgt_timer = 0;
3383fb751b1Stacha 	ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
33990634029Sdyoung 
34087515e34Sskrll 	chan = ic->ic_curchan;
34190634029Sdyoung 	do {
34240e261aaSdyoung 		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
34340e261aaSdyoung 			chan = &ic->ic_channels[0];
34440e261aaSdyoung 		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
34540e261aaSdyoung 			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
346f0546001Smycroft 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
34790634029Sdyoung 			    "%s: chan %d->%d\n", __func__,
34887515e34Sskrll 			    ieee80211_chan2ieee(ic, ic->ic_curchan),
34990634029Sdyoung 			    ieee80211_chan2ieee(ic, chan));
35087515e34Sskrll 			ic->ic_curchan = chan;
35190634029Sdyoung 			/*
35287515e34Sskrll 			 * XXX drivers should do this as needed,
35387515e34Sskrll 			 * XXX for now maintain compatibility
35490634029Sdyoung 			 */
35587515e34Sskrll 			ic->ic_bss->ni_rates =
35687515e34Sskrll 				ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
35740e261aaSdyoung 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
35890634029Sdyoung 			return 1;
35990634029Sdyoung 		}
36087515e34Sskrll 	} while (chan != ic->ic_curchan);
361c153ca3cSmaxv 
36290634029Sdyoung 	ieee80211_end_scan(ic);
36390634029Sdyoung 	return 0;
36490634029Sdyoung }
36590634029Sdyoung 
3663fb751b1Stacha /*
367676286b5Smsaitoh  * Probe the current channel, if allowed, while scanning.
3683fb751b1Stacha  * If the channel is not marked passive-only then send
3693fb751b1Stacha  * a probe request immediately.  Otherwise mark state and
3703fb751b1Stacha  * listen for beacons on the channel; if we receive something
3713fb751b1Stacha  * then we'll transmit a probe request.
3723fb751b1Stacha  */
3733fb751b1Stacha void
ieee80211_probe_curchan(struct ieee80211com * ic,int force)3743fb751b1Stacha ieee80211_probe_curchan(struct ieee80211com *ic, int force)
3753fb751b1Stacha {
3763fb751b1Stacha 	struct ifnet *ifp = ic->ic_ifp;
3773fb751b1Stacha 
3783fb751b1Stacha 	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) {
3793fb751b1Stacha 		/*
3803fb751b1Stacha 		 * XXX send both broadcast+directed probe request
3813fb751b1Stacha 		 */
3823fb751b1Stacha 		ieee80211_send_probereq(ic->ic_bss,
3833fb751b1Stacha 			ic->ic_myaddr, ifp->if_broadcastaddr,
3843fb751b1Stacha 			ifp->if_broadcastaddr,
3853fb751b1Stacha 			ic->ic_des_essid, ic->ic_des_esslen,
3863fb751b1Stacha 			ic->ic_opt_ie, ic->ic_opt_ie_len);
3873fb751b1Stacha 	} else
3883fb751b1Stacha 		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
3893fb751b1Stacha }
3903fb751b1Stacha 
39190634029Sdyoung static __inline void
copy_bss(struct ieee80211_node * nbss,const struct ieee80211_node * obss)39290634029Sdyoung copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
39390634029Sdyoung {
39490634029Sdyoung 	/* propagate useful state */
39590634029Sdyoung 	nbss->ni_authmode = obss->ni_authmode;
39690634029Sdyoung 	nbss->ni_txpower = obss->ni_txpower;
39790634029Sdyoung 	nbss->ni_vlan = obss->ni_vlan;
39890634029Sdyoung 	nbss->ni_rsn = obss->ni_rsn;
39990634029Sdyoung 	/* XXX statistics? */
40040e261aaSdyoung }
40140e261aaSdyoung 
40240e261aaSdyoung void
ieee80211_create_ibss(struct ieee80211com * ic,struct ieee80211_channel * chan)40340e261aaSdyoung ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
40440e261aaSdyoung {
40590634029Sdyoung 	struct ieee80211_node_table *nt;
40640e261aaSdyoung 	struct ieee80211_node *ni;
40740e261aaSdyoung 
40890634029Sdyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
40990634029Sdyoung 		"%s: creating ibss\n", __func__);
41090634029Sdyoung 
41190634029Sdyoung 	/*
41290634029Sdyoung 	 * Create the station/neighbor table.  Note that for adhoc
41390634029Sdyoung 	 * mode we make the initial inactivity timer longer since
41490634029Sdyoung 	 * we create nodes only through discovery and they typically
41590634029Sdyoung 	 * are long-lived associations.
41690634029Sdyoung 	 */
41790634029Sdyoung 	nt = &ic->ic_sta;
41890634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
41990634029Sdyoung 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
42090634029Sdyoung 		nt->nt_name = "station";
42190634029Sdyoung 		nt->nt_inact_init = ic->ic_inact_init;
42290634029Sdyoung 	} else {
42390634029Sdyoung 		nt->nt_name = "neighbor";
42490634029Sdyoung 		nt->nt_inact_init = ic->ic_inact_run;
42590634029Sdyoung 	}
42690634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
42790634029Sdyoung 
42887515e34Sskrll 	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
42990634029Sdyoung 	if (ni == NULL) {
43090634029Sdyoung 		/* XXX recovery? */
43190634029Sdyoung 		return;
43290634029Sdyoung 	}
43390634029Sdyoung 	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
43440e261aaSdyoung 	ni->ni_esslen = ic->ic_des_esslen;
43540e261aaSdyoung 	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
43690634029Sdyoung 	copy_bss(ni, ic->ic_bss);
43787515e34Sskrll 	ni->ni_intval = ic->ic_bintval;
43850d44f4fSmycroft 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
43940e261aaSdyoung 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
44040e261aaSdyoung 	if (ic->ic_phytype == IEEE80211_T_FH) {
44140e261aaSdyoung 		ni->ni_fhdwell = 200;	/* XXX */
44240e261aaSdyoung 		ni->ni_fhindex = 1;
44340e261aaSdyoung 	}
44490634029Sdyoung 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
44590634029Sdyoung 		ic->ic_flags |= IEEE80211_F_SIBSS;
44690634029Sdyoung 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
44790634029Sdyoung 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
44890634029Sdyoung 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
44990634029Sdyoung 		else
45090634029Sdyoung 			ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
45190634029Sdyoung 	}
452c153ca3cSmaxv 
45390634029Sdyoung 	/*
45490634029Sdyoung 	 * Fix the channel and related attributes.
45590634029Sdyoung 	 */
45690634029Sdyoung 	ieee80211_set_chan(ic, ni, chan);
45787515e34Sskrll 	ic->ic_curchan = chan;
45890634029Sdyoung 	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
459c153ca3cSmaxv 
46090634029Sdyoung 	/*
46190634029Sdyoung 	 * Do mode-specific rate setup.
46290634029Sdyoung 	 */
46390634029Sdyoung 	if (ic->ic_curmode == IEEE80211_MODE_11G) {
46490634029Sdyoung 		/*
46590634029Sdyoung 		 * Use a mixed 11b/11g rate set.
46690634029Sdyoung 		 */
46790634029Sdyoung 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
46890634029Sdyoung 	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
46990634029Sdyoung 		/*
47090634029Sdyoung 		 * Force pure 11b rate set.
47190634029Sdyoung 		 */
47290634029Sdyoung 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
47340e261aaSdyoung 	}
47440e261aaSdyoung 
47590634029Sdyoung 	(void)ieee80211_sta_join(ic, ieee80211_ref_node(ni));
47690634029Sdyoung }
47790634029Sdyoung 
47890634029Sdyoung void
ieee80211_reset_bss(struct ieee80211com * ic)47990634029Sdyoung ieee80211_reset_bss(struct ieee80211com *ic)
48090634029Sdyoung {
48190634029Sdyoung 	struct ieee80211_node *ni, *obss;
48290634029Sdyoung 
48390634029Sdyoung 	ieee80211_node_table_reset(&ic->ic_scan);
48490634029Sdyoung 	ieee80211_node_table_reset(&ic->ic_sta);
48590634029Sdyoung 
48690634029Sdyoung 	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
487a5effc3cSmsaitoh 	IASSERT(ni != NULL, ("unable to setup initial BSS node"));
48890634029Sdyoung 	obss = ic->ic_bss;
48990634029Sdyoung 	ic->ic_bss = ieee80211_ref_node(ni);
49090634029Sdyoung 	if (obss != NULL) {
49190634029Sdyoung 		copy_bss(ni, obss);
49287515e34Sskrll 		ni->ni_intval = ic->ic_bintval;
49390634029Sdyoung 		ieee80211_free_node(obss);
49490634029Sdyoung 	}
49590634029Sdyoung }
49690634029Sdyoung 
49787515e34Sskrll /* XXX tunable */
49887515e34Sskrll #define	STA_FAILS_MAX	2		/* assoc failures before ignored */
49987515e34Sskrll 
50090634029Sdyoung static int
ieee80211_match_bss(struct ieee80211com * ic,struct ieee80211_node * ni)501730331d5Sdyoung ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
502730331d5Sdyoung {
503730331d5Sdyoung 	u_int8_t rate;
504730331d5Sdyoung 	int fail;
505730331d5Sdyoung 
506730331d5Sdyoung 	fail = 0;
507730331d5Sdyoung 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
508730331d5Sdyoung 		fail |= 0x01;
509730331d5Sdyoung 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
510730331d5Sdyoung 	    ni->ni_chan != ic->ic_des_chan)
511730331d5Sdyoung 		fail |= 0x01;
512c153ca3cSmaxv 
513730331d5Sdyoung 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
514730331d5Sdyoung 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
515730331d5Sdyoung 			fail |= 0x02;
516730331d5Sdyoung 	} else {
517730331d5Sdyoung 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
518730331d5Sdyoung 			fail |= 0x02;
519730331d5Sdyoung 	}
520c153ca3cSmaxv 
52150d44f4fSmycroft 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
522730331d5Sdyoung 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
523730331d5Sdyoung 			fail |= 0x04;
524730331d5Sdyoung 	} else {
525bd837921Sdyoung 		/* XXX does this mean privacy is supported or required? */
526730331d5Sdyoung 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
527730331d5Sdyoung 			fail |= 0x04;
528730331d5Sdyoung 	}
529c153ca3cSmaxv 
53097827674Schristos 	rate = ieee80211_fix_rate(ni, IEEE80211_R_DONEGO | IEEE80211_R_DOFRATE);
531730331d5Sdyoung 	if (rate & IEEE80211_RATE_BASIC)
532730331d5Sdyoung 		fail |= 0x08;
533c153ca3cSmaxv 
534730331d5Sdyoung 	if (ic->ic_des_esslen != 0 &&
535730331d5Sdyoung 	    (ni->ni_esslen != ic->ic_des_esslen ||
536bd837921Sdyoung 	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
537730331d5Sdyoung 		fail |= 0x10;
538c153ca3cSmaxv 
539730331d5Sdyoung 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
540730331d5Sdyoung 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
541730331d5Sdyoung 		fail |= 0x20;
542c153ca3cSmaxv 
54387515e34Sskrll 	if (ni->ni_fails >= STA_FAILS_MAX)
54487515e34Sskrll 		fail |= 0x40;
545c153ca3cSmaxv 
546d906a817Sjakllsch 	/* If no ESS/IBSS is desired, do not match any. */
547f634dfedSjakllsch 	if (ic->ic_des_esslen == 0)
548f634dfedSjakllsch 		fail |= 0x80;
549f634dfedSjakllsch 
550bd837921Sdyoung #ifdef IEEE80211_DEBUG
55190634029Sdyoung 	if (ieee80211_msg_scan(ic)) {
55287515e34Sskrll 		printf(" %c %s",
55387515e34Sskrll 		    fail & 0x40 ? '=' : fail & 0x80 ? '^' : fail ? '-' : '+',
554730331d5Sdyoung 		    ether_sprintf(ni->ni_macaddr));
555730331d5Sdyoung 		printf(" %s%c", ether_sprintf(ni->ni_bssid),
556730331d5Sdyoung 		    fail & 0x20 ? '!' : ' ');
557730331d5Sdyoung 		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
558730331d5Sdyoung 			fail & 0x01 ? '!' : ' ');
559730331d5Sdyoung 		printf(" %+4d", ni->ni_rssi);
560730331d5Sdyoung 		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
561730331d5Sdyoung 		    fail & 0x08 ? '!' : ' ');
562730331d5Sdyoung 		printf(" %4s%c",
563730331d5Sdyoung 		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
564730331d5Sdyoung 		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
565730331d5Sdyoung 		    "????",
566730331d5Sdyoung 		    fail & 0x02 ? '!' : ' ');
567730331d5Sdyoung 		printf(" %3s%c ",
568730331d5Sdyoung 		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
569730331d5Sdyoung 		    "wep" : "no",
570730331d5Sdyoung 		    fail & 0x04 ? '!' : ' ');
571730331d5Sdyoung 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
572730331d5Sdyoung 		printf("%s\n", fail & 0x10 ? "!" : "");
573730331d5Sdyoung 	}
574bd837921Sdyoung #endif
575c153ca3cSmaxv 
576730331d5Sdyoung 	return fail;
577730331d5Sdyoung }
578730331d5Sdyoung 
57990634029Sdyoung static __inline u_int8_t
maxrate(const struct ieee80211_node * ni)58090634029Sdyoung maxrate(const struct ieee80211_node *ni)
58190634029Sdyoung {
58290634029Sdyoung 	const struct ieee80211_rateset *rs = &ni->ni_rates;
58390634029Sdyoung 	/* NB: assumes rate set is sorted (happens on frame receive) */
58490634029Sdyoung 	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
58590634029Sdyoung }
58690634029Sdyoung 
58790634029Sdyoung /*
58890634029Sdyoung  * Compare the capabilities of two nodes and decide which is
58990634029Sdyoung  * more desirable (return >0 if a is considered better).  Note
59090634029Sdyoung  * that we assume compatibility/usability has already been checked
59190634029Sdyoung  * so we don't need to (e.g. validate whether privacy is supported).
59290634029Sdyoung  * Used to select the best scan candidate for association in a BSS.
59390634029Sdyoung  */
59490634029Sdyoung static int
ieee80211_node_compare(struct ieee80211com * ic,const struct ieee80211_node * a,const struct ieee80211_node * b)595c153ca3cSmaxv ieee80211_node_compare(struct ieee80211com *ic, const struct ieee80211_node *a,
59690634029Sdyoung     const struct ieee80211_node *b)
59790634029Sdyoung {
59890634029Sdyoung 	u_int8_t maxa, maxb;
599111ee86dSroy 	u_int8_t rssia, rssib;
60087515e34Sskrll 	int weight;
60190634029Sdyoung 
60290634029Sdyoung 	/* privacy support preferred */
60390634029Sdyoung 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
60490634029Sdyoung 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
60590634029Sdyoung 		return 1;
60690634029Sdyoung 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
60790634029Sdyoung 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
60890634029Sdyoung 		return -1;
60990634029Sdyoung 
61087515e34Sskrll 	/* compare count of previous failures */
61187515e34Sskrll 	weight = b->ni_fails - a->ni_fails;
61287515e34Sskrll 	if (abs(weight) > 1)
61387515e34Sskrll 		return weight;
61487515e34Sskrll 
61590634029Sdyoung 	rssia = ic->ic_node_getrssi(a);
61690634029Sdyoung 	rssib = ic->ic_node_getrssi(b);
61790634029Sdyoung 	if (abs(rssib - rssia) < 5) {
61890634029Sdyoung 		/* best/max rate preferred if signal level close enough XXX */
61990634029Sdyoung 		maxa = maxrate(a);
62090634029Sdyoung 		maxb = maxrate(b);
62190634029Sdyoung 		if (maxa != maxb)
62290634029Sdyoung 			return maxa - maxb;
62390634029Sdyoung 		/* XXX use freq for channel preference */
624a1f606d3Slukem 		/* for now just prefer 5 GHz band to all other bands */
62590634029Sdyoung 		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
62690634029Sdyoung 		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
62790634029Sdyoung 			return 1;
62890634029Sdyoung 		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
62990634029Sdyoung 		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
63090634029Sdyoung 			return -1;
63190634029Sdyoung 	}
63290634029Sdyoung 	/* all things being equal, use signal level */
63390634029Sdyoung 	return rssia - rssib;
63490634029Sdyoung }
63590634029Sdyoung 
63690634029Sdyoung /*
63790634029Sdyoung  * Mark an ongoing scan stopped.
63890634029Sdyoung  */
63990634029Sdyoung void
ieee80211_cancel_scan(struct ieee80211com * ic)64090634029Sdyoung ieee80211_cancel_scan(struct ieee80211com *ic)
64190634029Sdyoung {
64290634029Sdyoung 
64390634029Sdyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
64490634029Sdyoung 		__func__,
64590634029Sdyoung 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
64690634029Sdyoung 
64790634029Sdyoung 	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
6483fb751b1Stacha 	ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
64990634029Sdyoung }
65090634029Sdyoung 
65140e261aaSdyoung /*
65240e261aaSdyoung  * Complete a scan of potential channels.
65340e261aaSdyoung  */
65440e261aaSdyoung void
ieee80211_end_scan(struct ieee80211com * ic)655f526e732Smycroft ieee80211_end_scan(struct ieee80211com *ic)
65640e261aaSdyoung {
65790634029Sdyoung 	struct ieee80211_node_table *nt = &ic->ic_scan;
65890634029Sdyoung 	struct ieee80211_node *ni, *selbs;
65940e261aaSdyoung 
66090634029Sdyoung 	ieee80211_cancel_scan(ic);
66190634029Sdyoung 	ieee80211_notify_scan_done(ic);
66240e261aaSdyoung 
66374988b0fSdyoung #ifndef IEEE80211_NO_HOSTAP
66440e261aaSdyoung 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
66590634029Sdyoung 		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
66690634029Sdyoung 		int i, bestchan;
66790634029Sdyoung 		u_int8_t rssi;
66890634029Sdyoung 
66940e261aaSdyoung 		/*
67040e261aaSdyoung 		 * The passive scan to look for existing AP's completed,
67140e261aaSdyoung 		 * select a channel to camp on.  Identify the channels
67240e261aaSdyoung 		 * that already have one or more AP's and try to locate
67390634029Sdyoung 		 * an unoccupied one.  If that fails, pick a channel that
67490634029Sdyoung 		 * looks to be quietest.
67540e261aaSdyoung 		 */
67690634029Sdyoung 		memset(maxrssi, 0, sizeof(maxrssi));
67790634029Sdyoung 		IEEE80211_NODE_LOCK(nt);
67890634029Sdyoung 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
67990634029Sdyoung 			rssi = ic->ic_node_getrssi(ni);
68090634029Sdyoung 			i = ieee80211_chan2ieee(ic, ni->ni_chan);
68190634029Sdyoung 			if (rssi > maxrssi[i])
68290634029Sdyoung 				maxrssi[i] = rssi;
68340e261aaSdyoung 		}
68490634029Sdyoung 		IEEE80211_NODE_UNLOCK(nt);
685c153ca3cSmaxv 
68690634029Sdyoung 		/* XXX select channel more intelligently */
68790634029Sdyoung 		bestchan = -1;
688c153ca3cSmaxv 		for (i = 0; i < IEEE80211_CHAN_MAX; i++) {
68990634029Sdyoung 			if (isset(ic->ic_chan_active, i)) {
69090634029Sdyoung 				/*
69190634029Sdyoung 				 * If the channel is unoccupied the max rssi
69290634029Sdyoung 				 * should be zero; just take it.  Otherwise
69390634029Sdyoung 				 * track the channel with the lowest rssi and
69490634029Sdyoung 				 * use that when all channels appear occupied.
69590634029Sdyoung 				 */
69690634029Sdyoung 				if (maxrssi[i] == 0) {
69790634029Sdyoung 					bestchan = i;
69840e261aaSdyoung 					break;
69940e261aaSdyoung 				}
70090634029Sdyoung 				if (bestchan == -1 ||
70190634029Sdyoung 				    maxrssi[i] < maxrssi[bestchan])
70290634029Sdyoung 					bestchan = i;
70390634029Sdyoung 			}
704c153ca3cSmaxv 		}
70590634029Sdyoung 		if (bestchan != -1) {
70690634029Sdyoung 			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
70740e261aaSdyoung 			return;
70840e261aaSdyoung 		}
70990634029Sdyoung 		/* no suitable channel, should not happen */
71090634029Sdyoung 	}
71174988b0fSdyoung #endif /* !IEEE80211_NO_HOSTAP */
71290634029Sdyoung 
71390634029Sdyoung 	/*
71490634029Sdyoung 	 * When manually sequencing the state machine; scan just once
71590634029Sdyoung 	 * regardless of whether we have a candidate or not.  The
71690634029Sdyoung 	 * controlling application is expected to setup state and
71790634029Sdyoung 	 * initiate an association.
71890634029Sdyoung 	 */
71990634029Sdyoung 	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
72090634029Sdyoung 		return;
721c153ca3cSmaxv 
72290634029Sdyoung 	/*
72390634029Sdyoung 	 * Automatic sequencing; look for a candidate and
72490634029Sdyoung 	 * if found join the network.
72590634029Sdyoung 	 */
72690634029Sdyoung 	/* NB: unlocked read should be ok */
72790634029Sdyoung 	if (TAILQ_FIRST(&nt->nt_node) == NULL) {
728f0546001Smycroft 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
72990634029Sdyoung 			"%s: no scan candidate\n", __func__);
730c153ca3cSmaxv 
73140e261aaSdyoung notfound:
73240e261aaSdyoung 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
73340e261aaSdyoung 		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
73440e261aaSdyoung 		    ic->ic_des_esslen != 0) {
73540e261aaSdyoung 			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
73640e261aaSdyoung 			return;
73740e261aaSdyoung 		}
738c153ca3cSmaxv 
73940e261aaSdyoung 		/*
74087515e34Sskrll 		 * Decrement the failure counts so entries will be
74187515e34Sskrll 		 * reconsidered the next time around.  We really want
74287515e34Sskrll 		 * to do this only for sta's where we've previously
743e68bcb69Sskrll 		 * had some success.
74487515e34Sskrll 		 */
74587515e34Sskrll 		IEEE80211_NODE_LOCK(nt);
746c153ca3cSmaxv 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
74787515e34Sskrll 			if (ni->ni_fails)
74887515e34Sskrll 				ni->ni_fails--;
749c153ca3cSmaxv 		}
75087515e34Sskrll 		IEEE80211_NODE_UNLOCK(nt);
751c153ca3cSmaxv 
75287515e34Sskrll 		/*
75340e261aaSdyoung 		 * Reset the list of channels to scan and start again.
75440e261aaSdyoung 		 */
755f526e732Smycroft 		ieee80211_reset_scan(ic);
75690634029Sdyoung 		ic->ic_flags |= IEEE80211_F_SCAN;
757f526e732Smycroft 		ieee80211_next_scan(ic);
75840e261aaSdyoung 		return;
75940e261aaSdyoung 	}
760c153ca3cSmaxv 
76140e261aaSdyoung 	selbs = NULL;
76290634029Sdyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
76390634029Sdyoung 	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
764c153ca3cSmaxv 
76590634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
76690634029Sdyoung 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
767730331d5Sdyoung 		if (ieee80211_match_bss(ic, ni) == 0) {
76840e261aaSdyoung 			if (selbs == NULL)
76940e261aaSdyoung 				selbs = ni;
77090634029Sdyoung 			else if (ieee80211_node_compare(ic, ni, selbs) > 0)
77140e261aaSdyoung 				selbs = ni;
77240e261aaSdyoung 		}
77340e261aaSdyoung 	}
77490634029Sdyoung 	if (selbs != NULL)		/* NB: grab ref while dropping lock */
77590634029Sdyoung 		(void)ieee80211_ref_node(selbs);
77690634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
777c153ca3cSmaxv 
77840e261aaSdyoung 	if (selbs == NULL)
77940e261aaSdyoung 		goto notfound;
78090634029Sdyoung 	if (!ieee80211_sta_join(ic, selbs)) {
78190634029Sdyoung 		ieee80211_free_node(selbs);
78240e261aaSdyoung 		goto notfound;
78340e261aaSdyoung 	}
78440e261aaSdyoung }
78540e261aaSdyoung 
78690634029Sdyoung /*
78790634029Sdyoung  * Handle 802.11 ad hoc network merge.  The
78890634029Sdyoung  * convention, set by the Wireless Ethernet Compatibility Alliance
78990634029Sdyoung  * (WECA), is that an 802.11 station will change its BSSID to match
79090634029Sdyoung  * the "oldest" 802.11 ad hoc network, on the same channel, that
79190634029Sdyoung  * has the station's desired SSID.  The "oldest" 802.11 network
79290634029Sdyoung  * sends beacons with the greatest TSF timestamp.
79390634029Sdyoung  *
79490634029Sdyoung  * The caller is assumed to validate TSF's before attempting a merge.
79590634029Sdyoung  *
79690634029Sdyoung  * Return !0 if the BSSID changed, 0 otherwise.
79790634029Sdyoung  */
79890634029Sdyoung int
ieee80211_ibss_merge(struct ieee80211_node * ni)79987515e34Sskrll ieee80211_ibss_merge(struct ieee80211_node *ni)
80090634029Sdyoung {
80187515e34Sskrll 	struct ieee80211com *ic = ni->ni_ic;
80290634029Sdyoung 
80390634029Sdyoung 	if (ni == ic->ic_bss ||
80490634029Sdyoung 	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
80590634029Sdyoung 		/* unchanged, nothing to do */
80690634029Sdyoung 		return 0;
80790634029Sdyoung 	}
80890634029Sdyoung 	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
80990634029Sdyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
81090634029Sdyoung 		    "%s: merge failed, capabilities mismatch\n", __func__);
81190634029Sdyoung 		ic->ic_stats.is_ibss_capmismatch++;
81290634029Sdyoung 		return 0;
81390634029Sdyoung 	}
814ee2b2a75Sdyoung 	if (!ieee80211_sta_join(ic, ieee80211_ref_node(ni)))
815ee2b2a75Sdyoung 		return 0;
81690634029Sdyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
81790634029Sdyoung 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
81890634029Sdyoung 		ether_sprintf(ni->ni_bssid),
81990634029Sdyoung 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
82090634029Sdyoung 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
82190634029Sdyoung 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
82290634029Sdyoung 	);
823ee2b2a75Sdyoung 	ic->ic_flags &= ~IEEE80211_F_SIBSS;
824ee2b2a75Sdyoung 	return 1;
82590634029Sdyoung }
82690634029Sdyoung 
82790634029Sdyoung /*
82890634029Sdyoung  * Join the specified IBSS/BSS network.  The node is assumed to
82990634029Sdyoung  * be passed in with a held reference.
83090634029Sdyoung  */
83190634029Sdyoung int
ieee80211_sta_join(struct ieee80211com * ic,struct ieee80211_node * selbs)83290634029Sdyoung ieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
83390634029Sdyoung {
83490634029Sdyoung 	struct ieee80211_node *obss;
83590634029Sdyoung 
83690634029Sdyoung 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
83790634029Sdyoung 		struct ieee80211_node_table *nt;
838c153ca3cSmaxv 
83990634029Sdyoung 		/*
84090634029Sdyoung 		 * Delete unusable rates; we've already checked
84190634029Sdyoung 		 * that the negotiated rate set is acceptable.
84290634029Sdyoung 		 */
84397827674Schristos 		ieee80211_fix_rate(selbs, IEEE80211_R_DODEL);
844c153ca3cSmaxv 
84590634029Sdyoung 		/*
84690634029Sdyoung 		 * Fill in the neighbor table; it will already
84790634029Sdyoung 		 * exist if we are simply switching mastership.
84890634029Sdyoung 		 * XXX ic_sta always setup so this is unnecessary?
84990634029Sdyoung 		 */
85090634029Sdyoung 		nt = &ic->ic_sta;
85190634029Sdyoung 		IEEE80211_NODE_LOCK(nt);
85290634029Sdyoung 		nt->nt_name = "neighbor";
85390634029Sdyoung 		nt->nt_inact_init = ic->ic_inact_run;
85490634029Sdyoung 		IEEE80211_NODE_UNLOCK(nt);
85590634029Sdyoung 	}
85690634029Sdyoung 
85790634029Sdyoung 	/*
85890634029Sdyoung 	 * Committed to selbs, setup state.
85990634029Sdyoung 	 */
86090634029Sdyoung 	obss = ic->ic_bss;
86190634029Sdyoung 	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
86225e9e914Sdyoung 	if (obss != NULL) {
86325e9e914Sdyoung 		copy_bss(selbs, obss);
86490634029Sdyoung 		ieee80211_free_node(obss);
86525e9e914Sdyoung 	}
866c153ca3cSmaxv 
86790634029Sdyoung 	/*
86890634029Sdyoung 	 * Set the erp state (mostly the slot time) to deal with
86990634029Sdyoung 	 * the auto-select case; this should be redundant if the
87090634029Sdyoung 	 * mode is locked.
87190634029Sdyoung 	 */
87290634029Sdyoung 	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
87387515e34Sskrll 	ic->ic_curchan = selbs->ni_chan;
87490634029Sdyoung 	ieee80211_reset_erp(ic);
87590634029Sdyoung 	ieee80211_wme_initparams(ic);
87690634029Sdyoung 
87790634029Sdyoung 	if (ic->ic_opmode == IEEE80211_M_STA)
87890634029Sdyoung 		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
87990634029Sdyoung 	else
88090634029Sdyoung 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
88190634029Sdyoung 	return 1;
88290634029Sdyoung }
88390634029Sdyoung 
88490634029Sdyoung /*
88590634029Sdyoung  * Leave the specified IBSS/BSS network.  The node is assumed to
88690634029Sdyoung  * be passed in with a held reference.
88790634029Sdyoung  */
88890634029Sdyoung void
ieee80211_sta_leave(struct ieee80211com * ic,struct ieee80211_node * ni)88990634029Sdyoung ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
89090634029Sdyoung {
89190634029Sdyoung 	ic->ic_node_cleanup(ni);
89290634029Sdyoung 	ieee80211_notify_node_leave(ic, ni);
89390634029Sdyoung }
89490634029Sdyoung 
895730331d5Sdyoung int
ieee80211_get_rate(const struct ieee80211_node * const ni)896b307a01eSdyoung ieee80211_get_rate(const struct ieee80211_node * const ni)
897730331d5Sdyoung {
8981e9e4aceSdyoung #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
8991e9e4aceSdyoung 	int ix, rate;
900b307a01eSdyoung 	struct ieee80211com *ic = ni->ni_ic;
9011e9e4aceSdyoung 	const struct ieee80211_rateset *rs;
902730331d5Sdyoung 
903b307a01eSdyoung 	IASSERT(ni != NULL, ("ni != NULL"));
904b307a01eSdyoung 	IASSERT(ieee80211_node_refcnt(ni) > 0,
905b307a01eSdyoung 	    ("refcnt(ni) == %d", ieee80211_node_refcnt(ni)));
906b307a01eSdyoung 	IASSERT(ic != NULL, ("ic != NULL"));
907c153ca3cSmaxv 
9081e9e4aceSdyoung 	if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
9091e9e4aceSdyoung 		rs = &ic->ic_sup_rates[ic->ic_curmode];
9101e9e4aceSdyoung 		rate = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
9111e9e4aceSdyoung 		for (ix = ni->ni_rates.rs_nrates - 1;
9121e9e4aceSdyoung 		     ix >= 0 && RATE(ix) != rate; ix--)
9131e9e4aceSdyoung 			;
914b307a01eSdyoung 		if (ix < 0) {
915b307a01eSdyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG,
916b307a01eSdyoung 			    "%s: fixed rate %d (%d.%d Mb/s) not in rate set",
917b307a01eSdyoung 			    __func__, ic->ic_fixed_rate, (rate * 5) / 10,
918b307a01eSdyoung 			    (rate * 5) % 10);
919b307a01eSdyoung 			goto no_rate;
920b307a01eSdyoung 		}
921c153ca3cSmaxv 	} else if (ic->ic_state == IEEE80211_S_RUN) {
9221e9e4aceSdyoung 		rate = ni->ni_rates.rs_rates[ni->ni_txrate];
923c153ca3cSmaxv 	} else {
924b307a01eSdyoung no_rate:
925b307a01eSdyoung 		rs = &ni->ni_rates;
926b307a01eSdyoung 		/* Choose node's lowest basic rate, or else its lowest rate. */
927b307a01eSdyoung 		for (ix = 0; ix < rs->rs_nrates; ix++) {
928b307a01eSdyoung 			if (rs->rs_rates[ix] & IEEE80211_RATE_BASIC)
929b307a01eSdyoung 				return rs->rs_rates[ix] & IEEE80211_RATE_VAL;
930b307a01eSdyoung 		}
931b307a01eSdyoung 		return ni->ni_rates.rs_rates[0] & IEEE80211_RATE_VAL;
932b307a01eSdyoung 	}
933c153ca3cSmaxv 
934730331d5Sdyoung 	return rate & IEEE80211_RATE_VAL;
935730331d5Sdyoung }
936730331d5Sdyoung 
93740e261aaSdyoung static struct ieee80211_node *
node_alloc(struct ieee80211_node_table * nt)938168cd830Schristos node_alloc(struct ieee80211_node_table *nt)
93940e261aaSdyoung {
940bd837921Sdyoung 	struct ieee80211_node *ni;
94190634029Sdyoung 
9429b87d582Scegger 	ni = malloc(sizeof(struct ieee80211_node),
943bd837921Sdyoung 		M_80211_NODE, M_NOWAIT | M_ZERO);
944bd837921Sdyoung 	return ni;
94540e261aaSdyoung }
94640e261aaSdyoung 
94790634029Sdyoung /*
94890634029Sdyoung  * Reclaim any resources in a node and reset any critical
94990634029Sdyoung  * state.  Typically nodes are free'd immediately after,
95090634029Sdyoung  * but in some cases the storage may be reused so we need
95190634029Sdyoung  * to insure consistent state (should probably fix that).
95290634029Sdyoung  */
95340e261aaSdyoung static void
node_cleanup(struct ieee80211_node * ni)95490634029Sdyoung node_cleanup(struct ieee80211_node *ni)
95540e261aaSdyoung {
95690634029Sdyoung #define	N(a)	(sizeof(a)/sizeof(a[0]))
95790634029Sdyoung 	struct ieee80211com *ic = ni->ni_ic;
95890634029Sdyoung 	int i, qlen;
95990634029Sdyoung 
96090634029Sdyoung 	/* NB: preserve ni_table */
96190634029Sdyoung 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
96290634029Sdyoung 		ic->ic_ps_sta--;
96390634029Sdyoung 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
96490634029Sdyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
96590634029Sdyoung 		    "[%s] power save mode off, %u sta's in ps mode\n",
96690634029Sdyoung 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
96790634029Sdyoung 	}
968c153ca3cSmaxv 
96961fb42b7Sdyoung 	/*
97061fb42b7Sdyoung 	 * Clear AREF flag that marks the authorization refcnt bump
97161fb42b7Sdyoung 	 * has happened.  This is probably not needed as the node
97261fb42b7Sdyoung 	 * should always be removed from the table so not found but
97361fb42b7Sdyoung 	 * do it just in case.
97461fb42b7Sdyoung 	 */
97561fb42b7Sdyoung 	ni->ni_flags &= ~IEEE80211_NODE_AREF;
97690634029Sdyoung 
97790634029Sdyoung 	/*
97890634029Sdyoung 	 * Drain power save queue and, if needed, clear TIM.
97990634029Sdyoung 	 */
98090634029Sdyoung 	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
98190634029Sdyoung 	if (qlen != 0 && ic->ic_set_tim != NULL)
98287515e34Sskrll 		ic->ic_set_tim(ni, 0);
98390634029Sdyoung 
98490634029Sdyoung 	ni->ni_associd = 0;
985db0e43b2Sdyoung 	if (ni->ni_challenge != NULL) {
9869b87d582Scegger 		free(ni->ni_challenge, M_DEVBUF);
987db0e43b2Sdyoung 		ni->ni_challenge = NULL;
988db0e43b2Sdyoung 	}
989c153ca3cSmaxv 
99090634029Sdyoung 	/*
99190634029Sdyoung 	 * Preserve SSID, WPA, and WME ie's so the bss node is
99290634029Sdyoung 	 * reusable during a re-auth/re-assoc state transition.
99390634029Sdyoung 	 * If we remove these data they will not be recreated
99490634029Sdyoung 	 * because they come from a probe-response or beacon frame
99590634029Sdyoung 	 * which cannot be expected prior to the association-response.
99690634029Sdyoung 	 * This should not be an issue when operating in other modes
99790634029Sdyoung 	 * as stations leaving always go through a full state transition
99890634029Sdyoung 	 * which will rebuild this state.
99990634029Sdyoung 	 *
100090634029Sdyoung 	 * XXX does this leave us open to inheriting old state?
100190634029Sdyoung 	 */
1002c153ca3cSmaxv 	for (i = 0; i < N(ni->ni_rxfrag); i++) {
100390634029Sdyoung 		m_freem(ni->ni_rxfrag[i]);
100490634029Sdyoung 		ni->ni_rxfrag[i] = NULL;
100590634029Sdyoung 	}
1006c153ca3cSmaxv 
100787515e34Sskrll 	/*
100887515e34Sskrll 	 * Must be careful here to remove any key map entry w/o a LOR.
100987515e34Sskrll 	 */
101087515e34Sskrll 	ieee80211_node_delucastkey(ni);
101190634029Sdyoung #undef N
101235515831Smycroft }
101335515831Smycroft 
101435515831Smycroft static void
node_free(struct ieee80211_node * ni)101590634029Sdyoung node_free(struct ieee80211_node *ni)
101635515831Smycroft {
101790634029Sdyoung 	struct ieee80211com *ic = ni->ni_ic;
101890634029Sdyoung 
101990634029Sdyoung 	ic->ic_node_cleanup(ni);
102090634029Sdyoung 	if (ni->ni_wpa_ie != NULL)
10219b87d582Scegger 		free(ni->ni_wpa_ie, M_DEVBUF);
102290634029Sdyoung 	if (ni->ni_wme_ie != NULL)
10239b87d582Scegger 		free(ni->ni_wme_ie, M_DEVBUF);
102490634029Sdyoung 	IEEE80211_NODE_SAVEQ_DESTROY(ni);
10259b87d582Scegger 	free(ni, M_80211_NODE);
102640e261aaSdyoung }
102740e261aaSdyoung 
1028111ee86dSroy static u_int8_t
node_getrssi(const struct ieee80211_node * ni)102990634029Sdyoung node_getrssi(const struct ieee80211_node *ni)
10309280f4b4Sdyoung {
10319280f4b4Sdyoung 	return ni->ni_rssi;
10329280f4b4Sdyoung }
10339280f4b4Sdyoung 
103440e261aaSdyoung static void
ieee80211_setup_node(struct ieee80211_node_table * nt,struct ieee80211_node * ni,const u_int8_t * macaddr)103590634029Sdyoung ieee80211_setup_node(struct ieee80211_node_table *nt,
103690634029Sdyoung 	struct ieee80211_node *ni, const u_int8_t *macaddr)
103740e261aaSdyoung {
103890634029Sdyoung 	struct ieee80211com *ic = nt->nt_ic;
103940e261aaSdyoung 	int hash;
104040e261aaSdyoung 
1041f526e732Smycroft 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
104290634029Sdyoung 		"%s %p<%s> in %s table\n", __func__, ni,
104390634029Sdyoung 		ether_sprintf(macaddr), nt->nt_name);
104490634029Sdyoung 
104540e261aaSdyoung 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
104640e261aaSdyoung 	hash = IEEE80211_NODE_HASH(macaddr);
104790634029Sdyoung 	ieee80211_node_initref(ni);		/* mark referenced */
104890634029Sdyoung 	ni->ni_chan = IEEE80211_CHAN_ANYC;
104990634029Sdyoung 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
105090634029Sdyoung 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
105190634029Sdyoung 	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
105290634029Sdyoung 	ni->ni_inact_reload = nt->nt_inact_init;
105390634029Sdyoung 	ni->ni_inact = ni->ni_inact_reload;
105490634029Sdyoung 	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1055f526e732Smycroft 
105690634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
105790634029Sdyoung 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
105890634029Sdyoung 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
105990634029Sdyoung 	ni->ni_table = nt;
106090634029Sdyoung 	ni->ni_ic = ic;
106190634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
106240e261aaSdyoung }
106340e261aaSdyoung 
106440e261aaSdyoung struct ieee80211_node *
ieee80211_alloc_node(struct ieee80211_node_table * nt,const u_int8_t * macaddr)106590634029Sdyoung ieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
106640e261aaSdyoung {
106790634029Sdyoung 	struct ieee80211com *ic = nt->nt_ic;
106890634029Sdyoung 	struct ieee80211_node *ni;
106990634029Sdyoung 
107090634029Sdyoung 	ni = ic->ic_node_alloc(nt);
107190634029Sdyoung 	if (ni != NULL)
107290634029Sdyoung 		ieee80211_setup_node(nt, ni, macaddr);
107390634029Sdyoung 	else
1074bd837921Sdyoung 		ic->ic_stats.is_rx_nodealloc++;
107540e261aaSdyoung 	return ni;
107640e261aaSdyoung }
107740e261aaSdyoung 
107887515e34Sskrll /*
107987515e34Sskrll  * Craft a temporary node suitable for sending a management frame
108087515e34Sskrll  * to the specified station.  We craft only as much state as we
108187515e34Sskrll  * need to do the work since the node will be immediately reclaimed
108287515e34Sskrll  * once the send completes.
108387515e34Sskrll  */
108487515e34Sskrll struct ieee80211_node *
ieee80211_tmp_node(struct ieee80211com * ic,const u_int8_t * macaddr)108587515e34Sskrll ieee80211_tmp_node(struct ieee80211com *ic, const u_int8_t *macaddr)
108687515e34Sskrll {
108787515e34Sskrll 	struct ieee80211_node *ni;
108887515e34Sskrll 
108987515e34Sskrll 	ni = ic->ic_node_alloc(&ic->ic_sta);
109087515e34Sskrll 	if (ni != NULL) {
109187515e34Sskrll 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
109287515e34Sskrll 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
109387515e34Sskrll 
109487515e34Sskrll 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
109587515e34Sskrll 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
109687515e34Sskrll 		ieee80211_node_initref(ni);		/* mark referenced */
109787515e34Sskrll 		ni->ni_txpower = ic->ic_bss->ni_txpower;
109887515e34Sskrll 		/* NB: required by ieee80211_fix_rate */
109987515e34Sskrll 		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
110087515e34Sskrll 		ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
110187515e34Sskrll 			IEEE80211_KEYIX_NONE);
110287515e34Sskrll 		/* XXX optimize away */
110387515e34Sskrll 		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
110487515e34Sskrll 
110587515e34Sskrll 		ni->ni_table = NULL;		/* NB: pedantic */
110687515e34Sskrll 		ni->ni_ic = ic;
110787515e34Sskrll 	} else {
110887515e34Sskrll 		/* XXX msg */
110987515e34Sskrll 		ic->ic_stats.is_rx_nodealloc++;
111087515e34Sskrll 	}
111187515e34Sskrll 	return ni;
111287515e34Sskrll }
111387515e34Sskrll 
111440e261aaSdyoung struct ieee80211_node *
ieee80211_dup_bss(struct ieee80211_node_table * nt,const u_int8_t * macaddr)111590634029Sdyoung ieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
111640e261aaSdyoung {
111790634029Sdyoung 	struct ieee80211com *ic = nt->nt_ic;
111890634029Sdyoung 	struct ieee80211_node *ni;
111990634029Sdyoung 
112090634029Sdyoung 	ni = ic->ic_node_alloc(nt);
112140e261aaSdyoung 	if (ni != NULL) {
112290634029Sdyoung 		ieee80211_setup_node(nt, ni, macaddr);
1123bd837921Sdyoung 		/*
1124bd837921Sdyoung 		 * Inherit from ic_bss.
1125bd837921Sdyoung 		 */
112690634029Sdyoung 		ni->ni_authmode = ic->ic_bss->ni_authmode;
112790634029Sdyoung 		ni->ni_txpower = ic->ic_bss->ni_txpower;
112890634029Sdyoung 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
1129bd837921Sdyoung 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
113090634029Sdyoung 		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
113190634029Sdyoung 		ni->ni_rsn = ic->ic_bss->ni_rsn;
1132c153ca3cSmaxv 	} else {
1133bd837921Sdyoung 		ic->ic_stats.is_rx_nodealloc++;
1134c153ca3cSmaxv 	}
113540e261aaSdyoung 	return ni;
113640e261aaSdyoung }
113740e261aaSdyoung 
1138bd837921Sdyoung static struct ieee80211_node *
113990634029Sdyoung #ifdef IEEE80211_DEBUG_REFCNT
_ieee80211_find_node_debug(struct ieee80211_node_table * nt,const u_int8_t * macaddr,const char * func,int line)114090634029Sdyoung _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
114190634029Sdyoung 	const u_int8_t *macaddr, const char *func, int line)
114290634029Sdyoung #else
114390634029Sdyoung _ieee80211_find_node(struct ieee80211_node_table *nt,
114490634029Sdyoung 	const u_int8_t *macaddr)
114590634029Sdyoung #endif
1146bd837921Sdyoung {
1147bd837921Sdyoung 	struct ieee80211_node *ni;
1148bd837921Sdyoung 	int hash;
1149bd837921Sdyoung 
115090634029Sdyoung 	IEEE80211_NODE_LOCK_ASSERT(nt);
1151bd837921Sdyoung 
1152bd837921Sdyoung 	hash = IEEE80211_NODE_HASH(macaddr);
115390634029Sdyoung 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1154bd837921Sdyoung 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
115590634029Sdyoung 			ieee80211_ref_node(ni);	/* mark referenced */
115690634029Sdyoung #ifdef IEEE80211_DEBUG_REFCNT
115790634029Sdyoung 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
115890634029Sdyoung 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
115990634029Sdyoung 			    func, line,
116090634029Sdyoung 			    ni, ether_sprintf(ni->ni_macaddr),
116190634029Sdyoung 			    ieee80211_node_refcnt(ni));
116290634029Sdyoung #endif
1163bd837921Sdyoung 			return ni;
1164bd837921Sdyoung 		}
1165bd837921Sdyoung 	}
1166bd837921Sdyoung 	return NULL;
1167bd837921Sdyoung }
116890634029Sdyoung #ifdef IEEE80211_DEBUG_REFCNT
116990634029Sdyoung #define	_ieee80211_find_node(nt, mac) \
117090634029Sdyoung 	_ieee80211_find_node_debug(nt, mac, func, line)
117190634029Sdyoung #endif
1172bd837921Sdyoung 
117340e261aaSdyoung struct ieee80211_node *
117490634029Sdyoung #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_find_node_debug(struct ieee80211_node_table * nt,const u_int8_t * macaddr,const char * func,int line)117590634029Sdyoung ieee80211_find_node_debug(struct ieee80211_node_table *nt,
117690634029Sdyoung 	const u_int8_t *macaddr, const char *func, int line)
117790634029Sdyoung #else
117890634029Sdyoung ieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
117990634029Sdyoung #endif
118040e261aaSdyoung {
118140e261aaSdyoung 	struct ieee80211_node *ni;
118240e261aaSdyoung 
118390634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
118490634029Sdyoung 	ni = _ieee80211_find_node(nt, macaddr);
118590634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
118640e261aaSdyoung 	return ni;
118740e261aaSdyoung }
118840e261aaSdyoung 
1189bd837921Sdyoung /*
119090634029Sdyoung  * Fake up a node; this handles node discovery in adhoc mode.
119165b3d068Schristos  * Note that for the driver's benefit we treat this like
119290634029Sdyoung  * an association so the driver has an opportunity to setup
1193f0a7346dSsnj  * its private state.
119490634029Sdyoung  */
119590634029Sdyoung struct ieee80211_node *
ieee80211_fakeup_adhoc_node(struct ieee80211_node_table * nt,const u_int8_t macaddr[IEEE80211_ADDR_LEN])119690634029Sdyoung ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
119790634029Sdyoung 	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
119890634029Sdyoung {
119990634029Sdyoung 	struct ieee80211com *ic = nt->nt_ic;
120090634029Sdyoung 	struct ieee80211_node *ni;
120190634029Sdyoung 
120290634029Sdyoung 	ni = ieee80211_dup_bss(nt, macaddr);
120390634029Sdyoung 	if (ni != NULL) {
120490634029Sdyoung 		/* XXX no rate negotiation; just dup */
120590634029Sdyoung 		ni->ni_rates = ic->ic_bss->ni_rates;
120690634029Sdyoung 		if (ic->ic_newassoc != NULL)
120787515e34Sskrll 			ic->ic_newassoc(ni, 1);
120890634029Sdyoung 		/* XXX not right for 802.1x/WPA */
120987515e34Sskrll 		ieee80211_node_authorize(ni);
121090634029Sdyoung 	}
121190634029Sdyoung 	return ni;
121290634029Sdyoung }
121390634029Sdyoung 
121487515e34Sskrll #ifdef IEEE80211_DEBUG
121587515e34Sskrll static void
dump_probe_beacon(u_int8_t subtype,int isnew,const u_int8_t mac[IEEE80211_ADDR_LEN],const struct ieee80211_scanparams * sp)121687515e34Sskrll dump_probe_beacon(u_int8_t subtype, int isnew,
121787515e34Sskrll 	const u_int8_t mac[IEEE80211_ADDR_LEN],
121887515e34Sskrll 	const struct ieee80211_scanparams *sp)
121987515e34Sskrll {
122087515e34Sskrll 
122187515e34Sskrll 	printf("[%s] %s%s on chan %u (bss chan %u) ",
122287515e34Sskrll 	    ether_sprintf(mac), isnew ? "new " : "",
122387515e34Sskrll 	    ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
12241df75eefSmaxv 	    sp->sp_chan, sp->sp_bchan);
12251df75eefSmaxv 	ieee80211_print_essid(sp->sp_ssid + 2, sp->sp_ssid[1]);
122687515e34Sskrll 	printf("\n");
122787515e34Sskrll 
122887515e34Sskrll 	if (isnew) {
122987515e34Sskrll 		printf("[%s] caps 0x%x bintval %u erp 0x%x",
12301df75eefSmaxv 		    ether_sprintf(mac), sp->sp_capinfo, sp->sp_bintval,
12311df75eefSmaxv 		    sp->sp_erp);
12321df75eefSmaxv 		if (sp->sp_country != NULL) {
123387515e34Sskrll #ifdef __FreeBSD__
123487515e34Sskrll 			printf(" country info %*D",
12351df75eefSmaxv 				sp->sp_country[1], sp->sp_country+2, " ");
123687515e34Sskrll #else
123787515e34Sskrll 			int i;
123887515e34Sskrll 			printf(" country info");
12391df75eefSmaxv 			for (i = 0; i < sp->sp_country[1]; i++)
12401df75eefSmaxv 				printf(" %02x", sp->sp_country[i+2]);
124187515e34Sskrll #endif
124287515e34Sskrll 		}
124387515e34Sskrll 		printf("\n");
124487515e34Sskrll 	}
124587515e34Sskrll }
124687515e34Sskrll #endif /* IEEE80211_DEBUG */
124787515e34Sskrll 
124887515e34Sskrll static void
saveie(u_int8_t ** iep,const u_int8_t * ie)124987515e34Sskrll saveie(u_int8_t **iep, const u_int8_t *ie)
125087515e34Sskrll {
125187515e34Sskrll 
125287515e34Sskrll 	if (ie == NULL)
125387515e34Sskrll 		*iep = NULL;
125487515e34Sskrll 	else
125587515e34Sskrll 		ieee80211_saveie(iep, ie);
125687515e34Sskrll }
125787515e34Sskrll 
125887515e34Sskrll /*
125987515e34Sskrll  * Process a beacon or probe response frame.
126087515e34Sskrll  */
126187515e34Sskrll void
ieee80211_add_scan(struct ieee80211com * ic,const struct ieee80211_scanparams * sp,const struct ieee80211_frame * wh,int subtype,int rssi,int rstamp)126287515e34Sskrll ieee80211_add_scan(struct ieee80211com *ic,
126311a42b5cSmaxv     const struct ieee80211_scanparams *sp, const struct ieee80211_frame *wh,
126487515e34Sskrll     int subtype, int rssi, int rstamp)
126587515e34Sskrll {
126687515e34Sskrll #define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
126787515e34Sskrll 	struct ieee80211_node_table *nt = &ic->ic_scan;
126887515e34Sskrll 	struct ieee80211_node *ni;
126987515e34Sskrll 	int newnode = 0;
127087515e34Sskrll 
127187515e34Sskrll 	ni = ieee80211_find_node(nt, wh->i_addr2);
127287515e34Sskrll 	if (ni == NULL) {
127387515e34Sskrll 		/*
127487515e34Sskrll 		 * Create a new entry.
127587515e34Sskrll 		 */
127687515e34Sskrll 		ni = ic->ic_node_alloc(nt);
127787515e34Sskrll 		if (ni == NULL) {
127887515e34Sskrll 			ic->ic_stats.is_rx_nodealloc++;
127987515e34Sskrll 			return;
128087515e34Sskrll 		}
128187515e34Sskrll 		ieee80211_setup_node(nt, ni, wh->i_addr2);
128211a42b5cSmaxv 
128387515e34Sskrll 		/*
128487515e34Sskrll 		 * XXX inherit from ic_bss.
128587515e34Sskrll 		 */
128687515e34Sskrll 		ni->ni_authmode = ic->ic_bss->ni_authmode;
128787515e34Sskrll 		ni->ni_txpower = ic->ic_bss->ni_txpower;
128887515e34Sskrll 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
128987515e34Sskrll 		ieee80211_set_chan(ic, ni, ic->ic_curchan);
129087515e34Sskrll 		ni->ni_rsn = ic->ic_bss->ni_rsn;
129187515e34Sskrll 		newnode = 1;
129287515e34Sskrll 	}
129311a42b5cSmaxv 
129487515e34Sskrll #ifdef IEEE80211_DEBUG
129587515e34Sskrll 	if (ieee80211_msg_scan(ic) && (ic->ic_flags & IEEE80211_F_SCAN))
129687515e34Sskrll 		dump_probe_beacon(subtype, newnode, wh->i_addr2, sp);
129787515e34Sskrll #endif
129811a42b5cSmaxv 
129987515e34Sskrll 	/* XXX ap beaconing multiple ssid w/ same bssid */
130011a42b5cSmaxv 	if (sp->sp_ssid[1] != 0 && (ISPROBE(subtype) || ni->ni_esslen == 0)) {
13011df75eefSmaxv 		ni->ni_esslen = sp->sp_ssid[1];
130287515e34Sskrll 		memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
13031df75eefSmaxv 		memcpy(ni->ni_essid, sp->sp_ssid + 2, sp->sp_ssid[1]);
130487515e34Sskrll 	}
130511a42b5cSmaxv 
130687515e34Sskrll 	ni->ni_scangen = ic->ic_scan.nt_scangen;
130787515e34Sskrll 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
130887515e34Sskrll 	ni->ni_rssi = rssi;
130987515e34Sskrll 	ni->ni_rstamp = rstamp;
13101df75eefSmaxv 	memcpy(ni->ni_tstamp.data, sp->sp_tstamp, sizeof(ni->ni_tstamp));
13111df75eefSmaxv 	ni->ni_intval = sp->sp_bintval;
13121df75eefSmaxv 	ni->ni_capinfo = sp->sp_capinfo;
13131df75eefSmaxv 	ni->ni_chan = &ic->ic_channels[sp->sp_chan];
13141df75eefSmaxv 	ni->ni_fhdwell = sp->sp_fhdwell;
13151df75eefSmaxv 	ni->ni_fhindex = sp->sp_fhindex;
13161df75eefSmaxv 	ni->ni_erp = sp->sp_erp;
131711a42b5cSmaxv 
13181df75eefSmaxv 	if (sp->sp_tim != NULL) {
131987515e34Sskrll 		struct ieee80211_tim_ie *ie =
13201df75eefSmaxv 		    (struct ieee80211_tim_ie *)sp->sp_tim;
132187515e34Sskrll 
132287515e34Sskrll 		ni->ni_dtim_count = ie->tim_count;
132387515e34Sskrll 		ni->ni_dtim_period = ie->tim_period;
132487515e34Sskrll 	}
132511a42b5cSmaxv 
132687515e34Sskrll 	/*
132787515e34Sskrll 	 * Record the byte offset from the mac header to
132887515e34Sskrll 	 * the start of the TIM information element for
132987515e34Sskrll 	 * use by hardware and/or to speedup software
133087515e34Sskrll 	 * processing of beacon frames.
133187515e34Sskrll 	 */
13321df75eefSmaxv 	ni->ni_timoff = sp->sp_timoff;
133311a42b5cSmaxv 
133487515e34Sskrll 	/*
133587515e34Sskrll 	 * Record optional information elements that might be
133687515e34Sskrll 	 * used by applications or drivers.
133787515e34Sskrll 	 */
13381df75eefSmaxv 	saveie(&ni->ni_wme_ie, sp->sp_wme);
13391df75eefSmaxv 	saveie(&ni->ni_wpa_ie, sp->sp_wpa);
134087515e34Sskrll 
134187515e34Sskrll 	/* NB: must be after ni_chan is setup */
13421df75eefSmaxv 	ieee80211_setup_rates(ni, sp->sp_rates, sp->sp_xrates,
13431df75eefSmaxv 	    IEEE80211_R_DOSORT);
134487515e34Sskrll 
134587515e34Sskrll 	if (!newnode)
134687515e34Sskrll 		ieee80211_free_node(ni);
134787515e34Sskrll #undef ISPROBE
134887515e34Sskrll }
134987515e34Sskrll 
1350461cd4dbSdyoung void
ieee80211_init_neighbor(struct ieee80211com * ic,struct ieee80211_node * ni,const struct ieee80211_frame * wh,const struct ieee80211_scanparams * sp,int isnew)1351461cd4dbSdyoung ieee80211_init_neighbor(struct ieee80211com *ic, struct ieee80211_node *ni,
1352461cd4dbSdyoung     const struct ieee80211_frame *wh, const struct ieee80211_scanparams *sp,
1353461cd4dbSdyoung     int isnew)
135487515e34Sskrll {
13551df75eefSmaxv 	ni->ni_esslen = sp->sp_ssid[1];
13561df75eefSmaxv 	memcpy(ni->ni_essid, sp->sp_ssid + 2, sp->sp_ssid[1]);
135787515e34Sskrll 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
13581df75eefSmaxv 	memcpy(ni->ni_tstamp.data, sp->sp_tstamp, sizeof(ni->ni_tstamp));
13591df75eefSmaxv 	ni->ni_intval = sp->sp_bintval;
13601df75eefSmaxv 	ni->ni_capinfo = sp->sp_capinfo;
136187515e34Sskrll 	ni->ni_chan = ic->ic_bss->ni_chan;
13621df75eefSmaxv 	ni->ni_fhdwell = sp->sp_fhdwell;
13631df75eefSmaxv 	ni->ni_fhindex = sp->sp_fhindex;
13641df75eefSmaxv 	ni->ni_erp = sp->sp_erp;
13651df75eefSmaxv 	ni->ni_timoff = sp->sp_timoff;
13661df75eefSmaxv 	if (sp->sp_wme != NULL)
13671df75eefSmaxv 		ieee80211_saveie(&ni->ni_wme_ie, sp->sp_wme);
13681df75eefSmaxv 	if (sp->sp_wpa != NULL)
13691df75eefSmaxv 		ieee80211_saveie(&ni->ni_wpa_ie, sp->sp_wpa);
137087515e34Sskrll 
137187515e34Sskrll 	/* NB: must be after ni_chan is setup */
13721df75eefSmaxv 	ieee80211_setup_rates(ni, sp->sp_rates, sp->sp_xrates,
137397827674Schristos 	    IEEE80211_R_DODEL | IEEE80211_R_DONEGO | IEEE80211_R_DOSORT);
137487515e34Sskrll 
137587515e34Sskrll 	if (ic->ic_newassoc != NULL)
1376461cd4dbSdyoung 		ic->ic_newassoc(ni, isnew);
1377461cd4dbSdyoung }
1378461cd4dbSdyoung 
1379461cd4dbSdyoung /*
1380461cd4dbSdyoung  * Do node discovery in adhoc mode on receipt of a beacon
1381461cd4dbSdyoung  * or probe response frame.  Note that for the driver's
138248b6679aSmsaitoh  * benefit we treat this like an association so the
1383f0a7346dSsnj  * driver has an opportunity to setup its private state.
1384461cd4dbSdyoung  */
1385461cd4dbSdyoung struct ieee80211_node *
ieee80211_add_neighbor(struct ieee80211com * ic,const struct ieee80211_frame * wh,const struct ieee80211_scanparams * sp)1386461cd4dbSdyoung ieee80211_add_neighbor(struct ieee80211com *ic,
1387461cd4dbSdyoung 	const struct ieee80211_frame *wh,
1388461cd4dbSdyoung 	const struct ieee80211_scanparams *sp)
1389461cd4dbSdyoung {
1390461cd4dbSdyoung 	struct ieee80211_node *ni;
1391461cd4dbSdyoung 
1392461cd4dbSdyoung 	ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1393461cd4dbSdyoung 	if (ni != NULL) {
1394461cd4dbSdyoung 		ieee80211_init_neighbor(ic, ni, wh, sp, 1);
139587515e34Sskrll 		/* XXX not right for 802.1x/WPA */
139687515e34Sskrll 		ieee80211_node_authorize(ni);
139787515e34Sskrll 	}
139887515e34Sskrll 	return ni;
139987515e34Sskrll }
140087515e34Sskrll 
140187515e34Sskrll #define	IS_CTL(wh) \
140287515e34Sskrll 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
140387515e34Sskrll #define	IS_PSPOLL(wh) \
140487515e34Sskrll 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
140590634029Sdyoung /*
140690634029Sdyoung  * Locate the node for sender, track state, and then pass the
140790634029Sdyoung  * (referenced) node up to the 802.11 layer for its use.  We
140890634029Sdyoung  * are required to pass some node so we fall back to ic_bss
140990634029Sdyoung  * when this frame is from an unknown sender.  The 802.11 layer
141090634029Sdyoung  * knows this means the sender wasn't in the node table and
141190634029Sdyoung  * acts accordingly.
141290634029Sdyoung  */
141390634029Sdyoung struct ieee80211_node *
141490634029Sdyoung #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_find_rxnode_debug(struct ieee80211com * ic,const struct ieee80211_frame_min * wh,const char * func,int line)141590634029Sdyoung ieee80211_find_rxnode_debug(struct ieee80211com *ic,
141690634029Sdyoung 	const struct ieee80211_frame_min *wh, const char *func, int line)
141790634029Sdyoung #else
141890634029Sdyoung ieee80211_find_rxnode(struct ieee80211com *ic,
141990634029Sdyoung 	const struct ieee80211_frame_min *wh)
142090634029Sdyoung #endif
142190634029Sdyoung {
142290634029Sdyoung 	struct ieee80211_node_table *nt;
142390634029Sdyoung 	struct ieee80211_node *ni;
142490634029Sdyoung 
142590634029Sdyoung 	/* XXX may want scanned nodes in the neighbor table for adhoc */
142690634029Sdyoung 	if (ic->ic_opmode == IEEE80211_M_STA ||
142790634029Sdyoung 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
142890634029Sdyoung 	    (ic->ic_flags & IEEE80211_F_SCAN))
142990634029Sdyoung 		nt = &ic->ic_scan;
143090634029Sdyoung 	else
143190634029Sdyoung 		nt = &ic->ic_sta;
1432c153ca3cSmaxv 
143390634029Sdyoung 	/* XXX check ic_bss first in station mode */
143490634029Sdyoung 	/* XXX 4-address frames? */
143590634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
143690634029Sdyoung 	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
143790634029Sdyoung 		ni = _ieee80211_find_node(nt, wh->i_addr1);
143890634029Sdyoung 	else
143990634029Sdyoung 		ni = _ieee80211_find_node(nt, wh->i_addr2);
144087515e34Sskrll 	if (ni == NULL)
144187515e34Sskrll 		ni = ieee80211_ref_node(ic->ic_bss);
144290634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
144390634029Sdyoung 
144487515e34Sskrll 	return ni;
144587515e34Sskrll }
144687515e34Sskrll 
144787515e34Sskrll /*
144887515e34Sskrll  * Like ieee80211_find_rxnode but use the supplied h/w
144987515e34Sskrll  * key index as a hint to locate the node in the key
145087515e34Sskrll  * mapping table.  If an entry is present at the key
145187515e34Sskrll  * index we return it; otherwise do a normal lookup and
145287515e34Sskrll  * update the mapping table if the station has a unicast
145387515e34Sskrll  * key assigned to it.
145487515e34Sskrll  */
145587515e34Sskrll struct ieee80211_node *
145687515e34Sskrll #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_find_rxnode_withkey_debug(struct ieee80211com * ic,const struct ieee80211_frame_min * wh,ieee80211_keyix keyix,const char * func,int line)145787515e34Sskrll ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
145887515e34Sskrll 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
145987515e34Sskrll 	const char *func, int line)
146087515e34Sskrll #else
146187515e34Sskrll ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
146287515e34Sskrll 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
146387515e34Sskrll #endif
146487515e34Sskrll {
146587515e34Sskrll 	struct ieee80211_node_table *nt;
146687515e34Sskrll 	struct ieee80211_node *ni;
146787515e34Sskrll 
146887515e34Sskrll 	if (ic->ic_opmode == IEEE80211_M_STA ||
146987515e34Sskrll 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1470c153ca3cSmaxv 	    (ic->ic_flags & IEEE80211_F_SCAN)) {
147187515e34Sskrll 		nt = &ic->ic_scan;
1472c153ca3cSmaxv 	} else {
147387515e34Sskrll 		nt = &ic->ic_sta;
1474c153ca3cSmaxv 	}
1475c153ca3cSmaxv 
147687515e34Sskrll 	IEEE80211_NODE_LOCK(nt);
1477c153ca3cSmaxv 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
147887515e34Sskrll 		ni = nt->nt_keyixmap[keyix];
1479c153ca3cSmaxv 	} else {
148087515e34Sskrll 		ni = NULL;
1481c153ca3cSmaxv 	}
148287515e34Sskrll 	if (ni == NULL) {
148387515e34Sskrll 		if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
148487515e34Sskrll 			ni = _ieee80211_find_node(nt, wh->i_addr1);
148587515e34Sskrll 		else
148687515e34Sskrll 			ni = _ieee80211_find_node(nt, wh->i_addr2);
148787515e34Sskrll 		if (ni == NULL)
148887515e34Sskrll 			ni = ieee80211_ref_node(ic->ic_bss);
148987515e34Sskrll 		if (nt->nt_keyixmap != NULL) {
149087515e34Sskrll 			/*
149187515e34Sskrll 			 * If the station has a unicast key cache slot
149287515e34Sskrll 			 * assigned update the key->node mapping table.
149387515e34Sskrll 			 */
149487515e34Sskrll 			keyix = ni->ni_ucastkey.wk_rxkeyix;
149587515e34Sskrll 			/* XXX can keyixmap[keyix] != NULL? */
149687515e34Sskrll 			if (keyix < nt->nt_keyixmax &&
149787515e34Sskrll 			    nt->nt_keyixmap[keyix] == NULL) {
149887515e34Sskrll 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
149987515e34Sskrll 				    "%s: add key map entry %p<%s> refcnt %d\n",
150087515e34Sskrll 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
150187515e34Sskrll 				    ieee80211_node_refcnt(ni)+1);
150287515e34Sskrll 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
150387515e34Sskrll 			}
150487515e34Sskrll 		}
150587515e34Sskrll 	} else {
150687515e34Sskrll 		ieee80211_ref_node(ni);
150787515e34Sskrll 	}
150887515e34Sskrll 	IEEE80211_NODE_UNLOCK(nt);
150987515e34Sskrll 
151087515e34Sskrll 	return ni;
151187515e34Sskrll }
151290634029Sdyoung #undef IS_PSPOLL
151390634029Sdyoung #undef IS_CTL
151490634029Sdyoung 
151590634029Sdyoung /*
1516bd837921Sdyoung  * Return a reference to the appropriate node for sending
1517bd837921Sdyoung  * a data frame.  This handles node discovery in adhoc networks.
1518bd837921Sdyoung  */
15193ed4d1acSdyoung struct ieee80211_node *
152090634029Sdyoung #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_find_txnode_debug(struct ieee80211com * ic,const u_int8_t * macaddr,const char * func,int line)152190634029Sdyoung ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
152290634029Sdyoung 	const char *func, int line)
152390634029Sdyoung #else
152490634029Sdyoung ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
152590634029Sdyoung #endif
15263ed4d1acSdyoung {
152790634029Sdyoung 	struct ieee80211_node_table *nt = &ic->ic_sta;
15283ed4d1acSdyoung 	struct ieee80211_node *ni;
15293ed4d1acSdyoung 
15303ed4d1acSdyoung 	/*
15313ed4d1acSdyoung 	 * The destination address should be in the node table
153287515e34Sskrll 	 * unless this is a multicast/broadcast frame.  We can
153387515e34Sskrll 	 * also optimize station mode operation, all frames go
153487515e34Sskrll 	 * to the bss node.
15353ed4d1acSdyoung 	 */
1536bd837921Sdyoung 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
153790634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
153887515e34Sskrll 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
153987515e34Sskrll 		ni = ieee80211_ref_node(ic->ic_bss);
154087515e34Sskrll 	else
154190634029Sdyoung 		ni = _ieee80211_find_node(nt, macaddr);
154290634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
154390634029Sdyoung 
1544b01c9a32Sdyoung 	if (ni == NULL) {
15453ed4d1acSdyoung 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
154690634029Sdyoung 		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
154790634029Sdyoung 			/*
154890634029Sdyoung 			 * In adhoc mode cons up a node for the destination.
154990634029Sdyoung 			 * Note that we need an additional reference for the
155090634029Sdyoung 			 * caller to be consistent with _ieee80211_find_node.
15518abb07d1Sdyoung 			 */
155290634029Sdyoung 			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
15534021fab8Sdyoung 			if (ni != NULL)
155490634029Sdyoung 				(void)ieee80211_ref_node(ni);
155590634029Sdyoung 		} else {
155690634029Sdyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
155790634029Sdyoung 				"[%s] no node, discard frame (%s)\n",
155890634029Sdyoung 				ether_sprintf(macaddr), __func__);
155990634029Sdyoung 			ic->ic_stats.is_tx_nonode++;
156090634029Sdyoung 		}
156190634029Sdyoung 	}
156290634029Sdyoung 	return ni;
15633ed4d1acSdyoung }
15643ed4d1acSdyoung 
156540e261aaSdyoung /*
156640e261aaSdyoung  * Like find but search based on the channel too.
156740e261aaSdyoung  */
156840e261aaSdyoung struct ieee80211_node *
156990634029Sdyoung #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_find_node_with_channel_debug(struct ieee80211_node_table * nt,const u_int8_t * macaddr,struct ieee80211_channel * chan,const char * func,int line)157090634029Sdyoung ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
157190634029Sdyoung 	const u_int8_t *macaddr, struct ieee80211_channel *chan,
157290634029Sdyoung 	const char *func, int line)
157390634029Sdyoung #else
157490634029Sdyoung ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
157590634029Sdyoung 	const u_int8_t *macaddr, struct ieee80211_channel *chan)
157690634029Sdyoung #endif
157740e261aaSdyoung {
157890634029Sdyoung 	struct ieee80211_node *ni;
157940e261aaSdyoung 	int hash;
158040e261aaSdyoung 
158140e261aaSdyoung 	hash = IEEE80211_NODE_HASH(macaddr);
1582c153ca3cSmaxv 
158390634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
158490634029Sdyoung 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
158590634029Sdyoung 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
158690634029Sdyoung 		    ni->ni_chan == chan) {
158790634029Sdyoung 			ieee80211_ref_node(ni);		/* mark referenced */
158890634029Sdyoung #ifdef IEEE80211_DEBUG_REFCNT
158943950e78Sgmcgarry 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
159090634029Sdyoung 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
159190634029Sdyoung 			    func, line,
159290634029Sdyoung 			    ni, ether_sprintf(ni->ni_macaddr),
159390634029Sdyoung 			    ieee80211_node_refcnt(ni));
159443950e78Sgmcgarry #else
159543950e78Sgmcgarry 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
159643950e78Sgmcgarry 			    "%s %p<%s> refcnt %d\n", __func__,
159743950e78Sgmcgarry 			    ni, ether_sprintf(ni->ni_macaddr),
159843950e78Sgmcgarry 			    ieee80211_node_refcnt(ni));
159943950e78Sgmcgarry #endif
160090634029Sdyoung 			break;
160190634029Sdyoung 		}
160290634029Sdyoung 	}
160390634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
1604c153ca3cSmaxv 
160590634029Sdyoung 	return ni;
160690634029Sdyoung }
160790634029Sdyoung 
160890634029Sdyoung struct ieee80211_node *
ieee80211_refine_node_for_beacon(struct ieee80211com * ic,struct ieee80211_node * ni0,struct ieee80211_channel * chan,const u_int8_t * ssid)1609168cd830Schristos ieee80211_refine_node_for_beacon(struct ieee80211com *ic,
161090634029Sdyoung 	struct ieee80211_node *ni0, struct ieee80211_channel *chan,
161190634029Sdyoung 	const u_int8_t *ssid)
161290634029Sdyoung {
161390634029Sdyoung 	struct ieee80211_node_table *nt = ni0->ni_table;
161490634029Sdyoung 	struct ieee80211_node *best, *ni;
161590634029Sdyoung 	int best_score = 0, score;
161690634029Sdyoung 
1617930c942cSdyoung 	if (nt == NULL)
1618930c942cSdyoung 		return ni0;
1619930c942cSdyoung 
162090634029Sdyoung 	best = ni0;
162190634029Sdyoung 	if (ssid[1] == 0 || best->ni_esslen == 0)
162290634029Sdyoung 		best_score = 1;
162390634029Sdyoung 	else if (ssid[1] == best->ni_esslen &&
162490634029Sdyoung 		 memcmp(ssid + 2, best->ni_essid, ssid[1]) == 0)
162590634029Sdyoung 		best_score = 2;
162690634029Sdyoung 	else
162790634029Sdyoung 		best_score = 0;
162890634029Sdyoung 
162990634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
163090634029Sdyoung 	for (ni = LIST_NEXT(ni0, ni_hash); ni != NULL;
163190634029Sdyoung 	     ni = LIST_NEXT(ni, ni_hash)) {
163290634029Sdyoung 		if (!IEEE80211_ADDR_EQ(ni->ni_macaddr, best->ni_macaddr) ||
163390634029Sdyoung 		    ni->ni_ic != best->ni_ic || ni->ni_chan != chan)
16348abb07d1Sdyoung 			continue;
16358abb07d1Sdyoung 
16368abb07d1Sdyoung 		if (ssid[1] == 0 || ni->ni_esslen == 0)
163790634029Sdyoung 			score = 1;
163890634029Sdyoung 		else if (ssid[1] == ni->ni_esslen &&
163990634029Sdyoung 			 memcmp(ssid + 2, ni->ni_essid, ssid[1]) == 0)
164090634029Sdyoung 			score = 2;
164190634029Sdyoung 		else
16428abb07d1Sdyoung 			continue;
16438abb07d1Sdyoung 
16448abb07d1Sdyoung 		if (score > best_score) {
164549cfbd04Sdyoung 			best = ni;
16468abb07d1Sdyoung 			best_score = score;
164740e261aaSdyoung 		}
164840e261aaSdyoung 	}
164990634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
1650c153ca3cSmaxv 
16518abb07d1Sdyoung 	return best;
165240e261aaSdyoung }
165340e261aaSdyoung 
165490634029Sdyoung /*
165590634029Sdyoung  * Like find but search based on the ssid too.
165690634029Sdyoung  */
165790634029Sdyoung struct ieee80211_node *
165890634029Sdyoung #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table * nt,const u_int8_t * macaddr,u_int ssidlen,const u_int8_t * ssid,const char * func,int line)165990634029Sdyoung ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
166090634029Sdyoung 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
166190634029Sdyoung 	const char *func, int line)
166290634029Sdyoung #else
166390634029Sdyoung ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
166490634029Sdyoung 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
166590634029Sdyoung #endif
166640e261aaSdyoung {
166761fb42b7Sdyoung #define	MATCH_SSID(ni, ssid, ssidlen) \
166861fb42b7Sdyoung 	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
166961fb42b7Sdyoung 	static const u_int8_t zeromac[IEEE80211_ADDR_LEN];
167090634029Sdyoung 	struct ieee80211com *ic = nt->nt_ic;
167140e261aaSdyoung 	struct ieee80211_node *ni;
167290634029Sdyoung 	int hash;
167340e261aaSdyoung 
167490634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
1675c2ec5838Schristos 	__USE(ic);
1676c2ec5838Schristos 
167761fb42b7Sdyoung 	/*
167861fb42b7Sdyoung 	 * A mac address that is all zero means match only the ssid;
167961fb42b7Sdyoung 	 * otherwise we must match both.
168061fb42b7Sdyoung 	 */
168161fb42b7Sdyoung 	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
168261fb42b7Sdyoung 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
168361fb42b7Sdyoung 			if (MATCH_SSID(ni, ssid, ssidlen))
168461fb42b7Sdyoung 				break;
168561fb42b7Sdyoung 		}
168661fb42b7Sdyoung 	} else {
168761fb42b7Sdyoung 		hash = IEEE80211_NODE_HASH(macaddr);
168890634029Sdyoung 		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
168990634029Sdyoung 			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
169061fb42b7Sdyoung 			    MATCH_SSID(ni, ssid, ssidlen))
169161fb42b7Sdyoung 				break;
169261fb42b7Sdyoung 		}
169361fb42b7Sdyoung 	}
169461fb42b7Sdyoung 	if (ni != NULL) {
169590634029Sdyoung 		ieee80211_ref_node(ni);	/* mark referenced */
169690634029Sdyoung #ifdef IEEE80211_DEBUG_REFCNT
169743950e78Sgmcgarry 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
169890634029Sdyoung 		    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
169990634029Sdyoung 		    func, line,
170090634029Sdyoung 		     ni, ether_sprintf(ni->ni_macaddr),
170190634029Sdyoung 		     ieee80211_node_refcnt(ni));
170243950e78Sgmcgarry #else
170343950e78Sgmcgarry 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
170443950e78Sgmcgarry 		    "%s %p<%s> refcnt %d\n", __func__,
170543950e78Sgmcgarry 		     ni, ether_sprintf(ni->ni_macaddr),
170643950e78Sgmcgarry 		     ieee80211_node_refcnt(ni));
170743950e78Sgmcgarry #endif
170890634029Sdyoung 	}
1709c153ca3cSmaxv 
171090634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
1711c153ca3cSmaxv 
171290634029Sdyoung 	return ni;
171361fb42b7Sdyoung #undef MATCH_SSID
171490634029Sdyoung }
171535515831Smycroft 
171690634029Sdyoung static void
_ieee80211_free_node(struct ieee80211_node * ni)171790634029Sdyoung _ieee80211_free_node(struct ieee80211_node *ni)
171890634029Sdyoung {
171990634029Sdyoung 	struct ieee80211com *ic = ni->ni_ic;
172090634029Sdyoung 	struct ieee80211_node_table *nt = ni->ni_table;
172190634029Sdyoung 
172290634029Sdyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
172390634029Sdyoung 		"%s %p<%s> in %s table\n", __func__, ni,
172490634029Sdyoung 		ether_sprintf(ni->ni_macaddr),
172590634029Sdyoung 		nt != NULL ? nt->nt_name : "<gone>");
172690634029Sdyoung 
172790634029Sdyoung 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
172890634029Sdyoung 	if (nt != NULL) {
172990634029Sdyoung 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
173090634029Sdyoung 		LIST_REMOVE(ni, ni_hash);
173190634029Sdyoung 	}
173290634029Sdyoung 	ic->ic_node_free(ni);
173390634029Sdyoung }
173490634029Sdyoung 
173590634029Sdyoung void
173690634029Sdyoung #ifdef IEEE80211_DEBUG_REFCNT
ieee80211_free_node_debug(struct ieee80211_node * ni,const char * func,int line)173790634029Sdyoung ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
173890634029Sdyoung #else
173990634029Sdyoung ieee80211_free_node(struct ieee80211_node *ni)
174090634029Sdyoung #endif
174190634029Sdyoung {
174290634029Sdyoung 	struct ieee80211_node_table *nt = ni->ni_table;
174390634029Sdyoung 
174490634029Sdyoung #ifdef IEEE80211_DEBUG_REFCNT
174590634029Sdyoung 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
174690634029Sdyoung 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
174790634029Sdyoung 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
174890634029Sdyoung #endif
1749c153ca3cSmaxv 
175087515e34Sskrll 	if (nt != NULL) {
175187515e34Sskrll 		IEEE80211_NODE_LOCK(nt);
175290634029Sdyoung 		if (ieee80211_node_dectestref(ni)) {
175390634029Sdyoung 			/*
175487515e34Sskrll 			 * Last reference, reclaim state.
175590634029Sdyoung 			 */
175690634029Sdyoung 			_ieee80211_free_node(ni);
175787515e34Sskrll 		} else if (ieee80211_node_refcnt(ni) == 1 &&
175887515e34Sskrll 		    nt->nt_keyixmap != NULL) {
175987515e34Sskrll 			ieee80211_keyix keyix;
1760c153ca3cSmaxv 
176187515e34Sskrll 			/*
176287515e34Sskrll 			 * Check for a last reference in the key mapping table.
176387515e34Sskrll 			 */
176487515e34Sskrll 			keyix = ni->ni_ucastkey.wk_rxkeyix;
176587515e34Sskrll 			if (keyix < nt->nt_keyixmax &&
176687515e34Sskrll 			    nt->nt_keyixmap[keyix] == ni) {
176787515e34Sskrll 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
176887515e34Sskrll 				    "%s: %p<%s> clear key map entry", __func__,
176987515e34Sskrll 				    ni, ether_sprintf(ni->ni_macaddr));
177087515e34Sskrll 				nt->nt_keyixmap[keyix] = NULL;
177187515e34Sskrll 				ieee80211_node_decref(ni); /* XXX needed? */
177290634029Sdyoung 				_ieee80211_free_node(ni);
177390634029Sdyoung 			}
177440e261aaSdyoung 		}
177587515e34Sskrll 		IEEE80211_NODE_UNLOCK(nt);
177687515e34Sskrll 	} else {
177787515e34Sskrll 		if (ieee80211_node_dectestref(ni))
177887515e34Sskrll 			_ieee80211_free_node(ni);
177987515e34Sskrll 	}
178087515e34Sskrll }
178187515e34Sskrll 
178287515e34Sskrll /*
178387515e34Sskrll  * Reclaim a unicast key and clear any key cache state.
178487515e34Sskrll  */
178587515e34Sskrll int
ieee80211_node_delucastkey(struct ieee80211_node * ni)178687515e34Sskrll ieee80211_node_delucastkey(struct ieee80211_node *ni)
178787515e34Sskrll {
178887515e34Sskrll 	struct ieee80211com *ic = ni->ni_ic;
178987515e34Sskrll 	struct ieee80211_node_table *nt = &ic->ic_sta;
179087515e34Sskrll 	struct ieee80211_node *nikey;
179187515e34Sskrll 	ieee80211_keyix keyix;
179287515e34Sskrll 	int isowned, status;
179387515e34Sskrll 
179487515e34Sskrll 	/*
179587515e34Sskrll 	 * NB: We must beware of LOR here; deleting the key
179687515e34Sskrll 	 * can cause the crypto layer to block traffic updates
179787515e34Sskrll 	 * which can generate a LOR against the node table lock;
179887515e34Sskrll 	 * grab it here and stash the key index for our use below.
179987515e34Sskrll 	 *
180087515e34Sskrll 	 * Must also beware of recursion on the node table lock.
180187515e34Sskrll 	 * When called from node_cleanup we may already have
180287515e34Sskrll 	 * the node table lock held.  Unfortunately there's no
180387515e34Sskrll 	 * way to separate out this path so we must do this
180487515e34Sskrll 	 * conditionally.
180587515e34Sskrll 	 */
180687515e34Sskrll 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
180787515e34Sskrll 	if (!isowned)
180887515e34Sskrll 		IEEE80211_NODE_LOCK(nt);
180987515e34Sskrll 	keyix = ni->ni_ucastkey.wk_rxkeyix;
181087515e34Sskrll 	status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
181187515e34Sskrll 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
181287515e34Sskrll 		nikey = nt->nt_keyixmap[keyix];
1813b1fea837Syamt 		nt->nt_keyixmap[keyix] = NULL;
181487515e34Sskrll 	} else
181587515e34Sskrll 		nikey = NULL;
181687515e34Sskrll 	if (!isowned)
181787515e34Sskrll 		IEEE80211_NODE_UNLOCK(&ic->ic_sta);
181887515e34Sskrll 
181987515e34Sskrll 	if (nikey != NULL) {
182087515e34Sskrll 		IASSERT(nikey == ni,
182187515e34Sskrll 			("key map out of sync, ni %p nikey %p", ni, nikey));
182287515e34Sskrll 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
182387515e34Sskrll 			"%s: delete key map entry %p<%s> refcnt %d\n",
182487515e34Sskrll 			__func__, ni, ether_sprintf(ni->ni_macaddr),
182587515e34Sskrll 			ieee80211_node_refcnt(ni)-1);
182687515e34Sskrll 		ieee80211_free_node(ni);
182787515e34Sskrll 	}
182887515e34Sskrll 	return status;
182987515e34Sskrll }
183040e261aaSdyoung 
18319280f4b4Sdyoung /*
183290634029Sdyoung  * Reclaim a node.  If this is the last reference count then
183390634029Sdyoung  * do the normal free work.  Otherwise remove it from the node
183490634029Sdyoung  * table and mark it gone by clearing the back-reference.
183590634029Sdyoung  */
183690634029Sdyoung static void
node_reclaim(struct ieee80211_node_table * nt,struct ieee80211_node * ni)183790634029Sdyoung node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
183890634029Sdyoung {
183987515e34Sskrll 	ieee80211_keyix keyix;
184087515e34Sskrll 
184187515e34Sskrll 	IEEE80211_NODE_LOCK_ASSERT(nt);
184290634029Sdyoung 
184390634029Sdyoung 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
184490634029Sdyoung 		"%s: remove %p<%s> from %s table, refcnt %d\n",
184590634029Sdyoung 		__func__, ni, ether_sprintf(ni->ni_macaddr),
184690634029Sdyoung 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
184787515e34Sskrll 	/*
184887515e34Sskrll 	 * Clear any entry in the unicast key mapping table.
184987515e34Sskrll 	 * We need to do it here so rx lookups don't find it
185087515e34Sskrll 	 * in the mapping table even if it's not in the hash
185187515e34Sskrll 	 * table.  We cannot depend on the mapping table entry
185287515e34Sskrll 	 * being cleared because the node may not be free'd.
185387515e34Sskrll 	 */
185487515e34Sskrll 	keyix = ni->ni_ucastkey.wk_rxkeyix;
185587515e34Sskrll 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
185687515e34Sskrll 	    nt->nt_keyixmap[keyix] == ni) {
185787515e34Sskrll 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
185887515e34Sskrll 			"%s: %p<%s> clear key map entry\n",
185987515e34Sskrll 			__func__, ni, ether_sprintf(ni->ni_macaddr));
186087515e34Sskrll 		nt->nt_keyixmap[keyix] = NULL;
186187515e34Sskrll 		ieee80211_node_decref(ni);	/* NB: don't need free */
186287515e34Sskrll 	}
186390634029Sdyoung 	if (!ieee80211_node_dectestref(ni)) {
186490634029Sdyoung 		/*
186590634029Sdyoung 		 * Other references are present, just remove the
186690634029Sdyoung 		 * node from the table so it cannot be found.  When
186790634029Sdyoung 		 * the references are dropped storage will be
186890634029Sdyoung 		 * reclaimed.
186990634029Sdyoung 		 */
187090634029Sdyoung 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
187190634029Sdyoung 		LIST_REMOVE(ni, ni_hash);
187290634029Sdyoung 		ni->ni_table = NULL;		/* clear reference */
187390634029Sdyoung 	} else
187490634029Sdyoung 		_ieee80211_free_node(ni);
187590634029Sdyoung }
187690634029Sdyoung 
187790634029Sdyoung static void
ieee80211_free_allnodes_locked(struct ieee80211_node_table * nt)187890634029Sdyoung ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
187990634029Sdyoung {
188090634029Sdyoung 	struct ieee80211com *ic = nt->nt_ic;
188190634029Sdyoung 	struct ieee80211_node *ni;
188290634029Sdyoung 
188390634029Sdyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
188490634029Sdyoung 		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
188590634029Sdyoung 
188690634029Sdyoung 	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
188790634029Sdyoung 		if (ni->ni_associd != 0) {
188890634029Sdyoung 			if (ic->ic_auth->ia_node_leave != NULL)
188990634029Sdyoung 				ic->ic_auth->ia_node_leave(ic, ni);
189090634029Sdyoung 			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
189190634029Sdyoung 		}
189290634029Sdyoung 		node_reclaim(nt, ni);
189390634029Sdyoung 	}
189490634029Sdyoung 	ieee80211_reset_erp(ic);
189590634029Sdyoung }
189690634029Sdyoung 
189790634029Sdyoung static void
ieee80211_free_allnodes(struct ieee80211_node_table * nt)189890634029Sdyoung ieee80211_free_allnodes(struct ieee80211_node_table *nt)
189990634029Sdyoung {
190090634029Sdyoung 
190190634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
190290634029Sdyoung 	ieee80211_free_allnodes_locked(nt);
190390634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
190490634029Sdyoung }
190590634029Sdyoung 
190690634029Sdyoung /*
190790634029Sdyoung  * Timeout entries in the scan cache.
190890634029Sdyoung  */
190990634029Sdyoung static void
ieee80211_timeout_scan_candidates(struct ieee80211_node_table * nt)191090634029Sdyoung ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
191190634029Sdyoung {
191290634029Sdyoung 	struct ieee80211com *ic = nt->nt_ic;
191390634029Sdyoung 	struct ieee80211_node *ni, *tni;
191490634029Sdyoung 
191590634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
191690634029Sdyoung 	ni = ic->ic_bss;
191790634029Sdyoung 	/* XXX belongs elsewhere */
191890634029Sdyoung 	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
191990634029Sdyoung 		m_freem(ni->ni_rxfrag[0]);
192090634029Sdyoung 		ni->ni_rxfrag[0] = NULL;
192190634029Sdyoung 	}
192290634029Sdyoung 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
192390634029Sdyoung 		if (ni->ni_inact && --ni->ni_inact == 0) {
192490634029Sdyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
192590634029Sdyoung 			    "[%s] scan candidate purged from cache "
192690634029Sdyoung 			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
192790634029Sdyoung 			    ieee80211_node_refcnt(ni));
192890634029Sdyoung 			node_reclaim(nt, ni);
192990634029Sdyoung 		}
193090634029Sdyoung 	}
193190634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
193290634029Sdyoung 
193390634029Sdyoung 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
193490634029Sdyoung }
193590634029Sdyoung 
193690634029Sdyoung /*
193790634029Sdyoung  * Timeout inactive stations and do related housekeeping.
193890634029Sdyoung  * Note that we cannot hold the node lock while sending a
193990634029Sdyoung  * frame as this would lead to a LOR.  Instead we use a
194090634029Sdyoung  * generation number to mark nodes that we've scanned and
194190634029Sdyoung  * drop the lock and restart a scan if we have to time out
194290634029Sdyoung  * a node.  Since we are single-threaded by virtue of
19439280f4b4Sdyoung  * controlling the inactivity timer we can be sure this will
19449280f4b4Sdyoung  * process each node only once.
19459280f4b4Sdyoung  */
194690634029Sdyoung static void
ieee80211_timeout_stations(struct ieee80211_node_table * nt)194790634029Sdyoung ieee80211_timeout_stations(struct ieee80211_node_table *nt)
194840e261aaSdyoung {
194990634029Sdyoung 	struct ieee80211com *ic = nt->nt_ic;
19509280f4b4Sdyoung 	struct ieee80211_node *ni;
195190634029Sdyoung 	u_int gen;
195287515e34Sskrll 	int isadhoc;
195340e261aaSdyoung 
195487515e34Sskrll 	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
195587515e34Sskrll 		   ic->ic_opmode == IEEE80211_M_AHDEMO);
195690634029Sdyoung 	IEEE80211_SCAN_LOCK(nt);
1957550e5499Sseanb 	gen = ++nt->nt_scangen;
1958f3b59d51Schristos 	IEEE80211_SCAN_UNLOCK(nt);
195990634029Sdyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
196090634029Sdyoung 		"%s: %s scangen %u\n", __func__, nt->nt_name, gen);
19619280f4b4Sdyoung restart:
1962f3b59d51Schristos 	IEEE80211_SCAN_LOCK(nt);
1963f3b59d51Schristos 	if (gen != nt->nt_scangen) {
1964f3b59d51Schristos 		printf("%s: scan aborted %u\n", __func__, gen);
1965f3b59d51Schristos 		IEEE80211_SCAN_UNLOCK(nt);
1966f3b59d51Schristos 		return;
1967f3b59d51Schristos 	}
1968f3b59d51Schristos 	IEEE80211_SCAN_UNLOCK(nt);
1969f3b59d51Schristos 
197090634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
197190634029Sdyoung 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
19729280f4b4Sdyoung 		if (ni->ni_scangen == gen)	/* previously handled */
19739280f4b4Sdyoung 			continue;
19749280f4b4Sdyoung 		ni->ni_scangen = gen;
197540e261aaSdyoung 		/*
197661fb42b7Sdyoung 		 * Ignore entries for which have yet to receive an
197761fb42b7Sdyoung 		 * authentication frame.  These are transient and
197861fb42b7Sdyoung 		 * will be reclaimed when the last reference to them
197961fb42b7Sdyoung 		 * goes away (when frame xmits complete).
198061fb42b7Sdyoung 		 */
198187515e34Sskrll 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
198287515e34Sskrll 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
198361fb42b7Sdyoung 			continue;
198461fb42b7Sdyoung 		/*
198590634029Sdyoung 		 * Free fragment if not needed anymore
198690634029Sdyoung 		 * (last fragment older than 1s).
198790634029Sdyoung 		 * XXX doesn't belong here
198840e261aaSdyoung 		 */
198990634029Sdyoung 		if (ni->ni_rxfrag[0] != NULL &&
199090634029Sdyoung 		    ticks > ni->ni_rxfragstamp + hz) {
199190634029Sdyoung 			m_freem(ni->ni_rxfrag[0]);
199290634029Sdyoung 			ni->ni_rxfrag[0] = NULL;
199390634029Sdyoung 		}
199490634029Sdyoung 		/*
199590634029Sdyoung 		 * Special case ourself; we may be idle for extended periods
199690634029Sdyoung 		 * of time and regardless reclaiming our state is wrong.
199790634029Sdyoung 		 */
199890634029Sdyoung 		if (ni == ic->ic_bss)
199990634029Sdyoung 			continue;
200090634029Sdyoung 		ni->ni_inact--;
200187515e34Sskrll 		if (ni->ni_associd != 0 || isadhoc) {
200290634029Sdyoung 			/*
200390634029Sdyoung 			 * Age frames on the power save queue. The
200490634029Sdyoung 			 * aging interval is 4 times the listen
200590634029Sdyoung 			 * interval specified by the station.  This
200690634029Sdyoung 			 * number is factored into the age calculations
200790634029Sdyoung 			 * when the frame is placed on the queue.  We
200890634029Sdyoung 			 * store ages as time differences we can check
200990634029Sdyoung 			 * and/or adjust only the head of the list.
201090634029Sdyoung 			 */
201190634029Sdyoung 			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
201290634029Sdyoung 				struct mbuf *m;
201390634029Sdyoung 				int discard = 0;
201490634029Sdyoung 
201590634029Sdyoung 				IEEE80211_NODE_SAVEQ_LOCK(ni);
201690634029Sdyoung 				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
201790634029Sdyoung 				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
201890634029Sdyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
201990634029Sdyoung 					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
202090634029Sdyoung 					m_freem(m);
202190634029Sdyoung 					discard++;
202290634029Sdyoung 				}
202390634029Sdyoung 				if (m != NULL)
202490634029Sdyoung 					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
202590634029Sdyoung 				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
202690634029Sdyoung 
202790634029Sdyoung 				if (discard != 0) {
202890634029Sdyoung 					IEEE80211_DPRINTF(ic,
202990634029Sdyoung 					    IEEE80211_MSG_POWER,
203090634029Sdyoung 					    "[%s] discard %u frames for age\n",
203190634029Sdyoung 					    ether_sprintf(ni->ni_macaddr),
203290634029Sdyoung 					    discard);
203390634029Sdyoung 					IEEE80211_NODE_STAT_ADD(ni,
203490634029Sdyoung 						ps_discard, discard);
203590634029Sdyoung 					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
203687515e34Sskrll 						ic->ic_set_tim(ni, 0);
203790634029Sdyoung 				}
203890634029Sdyoung 			}
203990634029Sdyoung 			/*
204090634029Sdyoung 			 * Probe the station before time it out.  We
204190634029Sdyoung 			 * send a null data frame which may not be
204290634029Sdyoung 			 * universally supported by drivers (need it
204390634029Sdyoung 			 * for ps-poll support so it should be...).
204490634029Sdyoung 			 */
204590634029Sdyoung 			if (0 < ni->ni_inact &&
204690634029Sdyoung 			    ni->ni_inact <= ic->ic_inact_probe) {
204787515e34Sskrll 				IEEE80211_NOTE(ic,
204887515e34Sskrll 				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
204987515e34Sskrll 				    ni, "%s",
205087515e34Sskrll 				    "probe station due to inactivity");
205187515e34Sskrll 				/*
205287515e34Sskrll 				 * Grab a reference before unlocking the table
205387515e34Sskrll 				 * so the node cannot be reclaimed before we
205487515e34Sskrll 				 * send the frame. ieee80211_send_nulldata
205587515e34Sskrll 				 * understands we've done this and reclaims the
205687515e34Sskrll 				 * ref for us as needed.
205787515e34Sskrll 				 */
205887515e34Sskrll 				ieee80211_ref_node(ni);
205990634029Sdyoung 				IEEE80211_NODE_UNLOCK(nt);
206087515e34Sskrll 				ieee80211_send_nulldata(ni);
206190634029Sdyoung 				/* XXX stat? */
206290634029Sdyoung 				goto restart;
206390634029Sdyoung 			}
206490634029Sdyoung 		}
206590634029Sdyoung 		if (ni->ni_inact <= 0) {
206687515e34Sskrll 			IEEE80211_NOTE(ic,
206787515e34Sskrll 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
206887515e34Sskrll 			    "station timed out due to inactivity "
206987515e34Sskrll 			    "(refcnt %u)", ieee80211_node_refcnt(ni));
207090634029Sdyoung 			/*
207190634029Sdyoung 			 * Send a deauthenticate frame and drop the station.
207290634029Sdyoung 			 * This is somewhat complicated due to reference counts
207390634029Sdyoung 			 * and locking.  At this point a station will typically
207490634029Sdyoung 			 * have a reference count of 1.  ieee80211_node_leave
207590634029Sdyoung 			 * will do a "free" of the node which will drop the
207690634029Sdyoung 			 * reference count.  But in the meantime a reference
20775d1e8b27Swiz 			 * will be held by the deauth frame.  The actual reclaim
207890634029Sdyoung 			 * of the node will happen either after the tx is
207990634029Sdyoung 			 * completed or by ieee80211_node_leave.
208090634029Sdyoung 			 *
208190634029Sdyoung 			 * Separately we must drop the node lock before sending
208290634029Sdyoung 			 * in case the driver takes a lock, as this will result
208390634029Sdyoung 			 * in  LOR between the node lock and the driver lock.
208490634029Sdyoung 			 */
208590634029Sdyoung 			IEEE80211_NODE_UNLOCK(nt);
208690634029Sdyoung 			if (ni->ni_associd != 0) {
208740e261aaSdyoung 				IEEE80211_SEND_MGMT(ic, ni,
208840e261aaSdyoung 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
208940e261aaSdyoung 				    IEEE80211_REASON_AUTH_EXPIRE);
209090634029Sdyoung 			}
209135515831Smycroft 			ieee80211_node_leave(ic, ni);
20929280f4b4Sdyoung 			ic->ic_stats.is_node_timeout++;
20939280f4b4Sdyoung 			goto restart;
20949280f4b4Sdyoung 		}
209590634029Sdyoung 	}
209690634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
209790634029Sdyoung 
209890634029Sdyoung 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
209940e261aaSdyoung }
210040e261aaSdyoung 
210140e261aaSdyoung void
ieee80211_iterate_nodes(struct ieee80211_node_table * nt,ieee80211_iter_func * f,void * arg)210290634029Sdyoung ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
210340e261aaSdyoung {
210440e261aaSdyoung 	struct ieee80211_node *ni;
210590634029Sdyoung 	u_int gen;
210640e261aaSdyoung 
210790634029Sdyoung 	IEEE80211_SCAN_LOCK(nt);
2108550e5499Sseanb 	gen = ++nt->nt_scangen;
2109f3b59d51Schristos 	IEEE80211_SCAN_UNLOCK(nt);
211090634029Sdyoung restart:
2111f3b59d51Schristos 	IEEE80211_SCAN_LOCK(nt);
2112f3b59d51Schristos 	if (gen != nt->nt_scangen) {
2113f3b59d51Schristos 		printf("%s: scan aborted %u\n", __func__, gen);
2114f3b59d51Schristos 		IEEE80211_SCAN_UNLOCK(nt);
2115f3b59d51Schristos 		return;
2116f3b59d51Schristos 	}
2117f3b59d51Schristos 	IEEE80211_SCAN_UNLOCK(nt);
2118f3b59d51Schristos 
211990634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
212090634029Sdyoung 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
212190634029Sdyoung 		if (ni->ni_scangen != gen) {
212290634029Sdyoung 			ni->ni_scangen = gen;
212390634029Sdyoung 			(void) ieee80211_ref_node(ni);
212490634029Sdyoung 			IEEE80211_NODE_UNLOCK(nt);
212540e261aaSdyoung 			(*f)(arg, ni);
212690634029Sdyoung 			ieee80211_free_node(ni);
212790634029Sdyoung 			goto restart;
212890634029Sdyoung 		}
212990634029Sdyoung 	}
213090634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
213190634029Sdyoung }
213290634029Sdyoung 
213390634029Sdyoung void
ieee80211_dump_node(struct ieee80211_node_table * nt,struct ieee80211_node * ni)2134168cd830Schristos ieee80211_dump_node(struct ieee80211_node_table *nt,
21354d595fd7Schristos     struct ieee80211_node *ni)
213690634029Sdyoung {
21374ecd76e5Srin 	printf("%p: mac %s refcnt %d\n", ni,
213890634029Sdyoung 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
213990634029Sdyoung 	printf("\tscangen %u authmode %u flags 0x%x\n",
214090634029Sdyoung 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
214190634029Sdyoung 	printf("\tassocid 0x%x txpower %u vlan %u\n",
214290634029Sdyoung 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
214390634029Sdyoung 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
214490634029Sdyoung 		ni->ni_txseqs[0],
214590634029Sdyoung 		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
214690634029Sdyoung 		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
214790634029Sdyoung 		ni->ni_rxfragstamp);
214890634029Sdyoung 	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
214990634029Sdyoung 		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
215090634029Sdyoung 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
215190634029Sdyoung 		ether_sprintf(ni->ni_bssid),
215290634029Sdyoung 		ni->ni_esslen, ni->ni_essid,
215390634029Sdyoung 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
215490634029Sdyoung 	printf("\tfails %u inact %u txrate %u\n",
215590634029Sdyoung 		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
215690634029Sdyoung }
215790634029Sdyoung 
215890634029Sdyoung void
ieee80211_dump_nodes(struct ieee80211_node_table * nt)215990634029Sdyoung ieee80211_dump_nodes(struct ieee80211_node_table *nt)
216090634029Sdyoung {
216190634029Sdyoung 	ieee80211_iterate_nodes(nt,
216290634029Sdyoung 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
216390634029Sdyoung }
216490634029Sdyoung 
216590634029Sdyoung /*
216690634029Sdyoung  * Handle a station joining an 11g network.
216790634029Sdyoung  */
216890634029Sdyoung static void
ieee80211_node_join_11g(struct ieee80211com * ic,struct ieee80211_node * ni)216990634029Sdyoung ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
217090634029Sdyoung {
217190634029Sdyoung 
217290634029Sdyoung 	/*
217390634029Sdyoung 	 * Station isn't capable of short slot time.  Bump
217490634029Sdyoung 	 * the count of long slot time stations and disable
217590634029Sdyoung 	 * use of short slot time.  Note that the actual switch
217690634029Sdyoung 	 * over to long slot time use may not occur until the
217790634029Sdyoung 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
217890634029Sdyoung 	 */
217990634029Sdyoung 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
218090634029Sdyoung 		ic->ic_longslotsta++;
218190634029Sdyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
218290634029Sdyoung 		    "[%s] station needs long slot time, count %d\n",
218390634029Sdyoung 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
218490634029Sdyoung 		/* XXX vap's w/ conflicting needs won't work */
218590634029Sdyoung 		ieee80211_set_shortslottime(ic, 0);
218690634029Sdyoung 	}
2187c153ca3cSmaxv 
218890634029Sdyoung 	/*
218990634029Sdyoung 	 * If the new station is not an ERP station
219090634029Sdyoung 	 * then bump the counter and enable protection
219190634029Sdyoung 	 * if configured.
219290634029Sdyoung 	 */
219390634029Sdyoung 	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
219490634029Sdyoung 		ic->ic_nonerpsta++;
219590634029Sdyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
219690634029Sdyoung 		    "[%s] station is !ERP, %d non-ERP stations associated\n",
219790634029Sdyoung 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
219890634029Sdyoung 		/*
219990634029Sdyoung 		 * If protection is configured, enable it.
220090634029Sdyoung 		 */
220190634029Sdyoung 		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
220290634029Sdyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
220390634029Sdyoung 			    "%s: enable use of protection\n", __func__);
220490634029Sdyoung 			ic->ic_flags |= IEEE80211_F_USEPROT;
220590634029Sdyoung 		}
220690634029Sdyoung 		/*
220790634029Sdyoung 		 * If station does not support short preamble
220890634029Sdyoung 		 * then we must enable use of Barker preamble.
220990634029Sdyoung 		 */
221090634029Sdyoung 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
221190634029Sdyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
221290634029Sdyoung 			    "[%s] station needs long preamble\n",
221390634029Sdyoung 			    ether_sprintf(ni->ni_macaddr));
221490634029Sdyoung 			ic->ic_flags |= IEEE80211_F_USEBARKER;
221590634029Sdyoung 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
221690634029Sdyoung 		}
2217c153ca3cSmaxv 	} else {
221890634029Sdyoung 		ni->ni_flags |= IEEE80211_NODE_ERP;
221940e261aaSdyoung 	}
2220c153ca3cSmaxv }
222135515831Smycroft 
222235515831Smycroft void
ieee80211_node_join(struct ieee80211com * ic,struct ieee80211_node * ni,int resp)2223c153ca3cSmaxv ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni,
2224c153ca3cSmaxv     int resp)
222535515831Smycroft {
222635515831Smycroft 	int newassoc;
222735515831Smycroft 
222835515831Smycroft 	if (ni->ni_associd == 0) {
222935515831Smycroft 		u_int16_t aid;
223035515831Smycroft 
223135515831Smycroft 		/*
223290634029Sdyoung 		 * It would be good to search the bitmap
223335515831Smycroft 		 * more efficiently, but this will do for now.
223435515831Smycroft 		 */
223535515831Smycroft 		for (aid = 1; aid < ic->ic_max_aid; aid++) {
223635515831Smycroft 			if (!IEEE80211_AID_ISSET(aid,
223735515831Smycroft 			    ic->ic_aid_bitmap))
223835515831Smycroft 				break;
223935515831Smycroft 		}
224035515831Smycroft 		if (aid >= ic->ic_max_aid) {
224135515831Smycroft 			IEEE80211_SEND_MGMT(ic, ni, resp,
224235515831Smycroft 			    IEEE80211_REASON_ASSOC_TOOMANY);
224335515831Smycroft 			ieee80211_node_leave(ic, ni);
224435515831Smycroft 			return;
224535515831Smycroft 		}
224635515831Smycroft 		ni->ni_associd = aid | 0xc000;
224735515831Smycroft 		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
224890634029Sdyoung 		ic->ic_sta_assoc++;
224935515831Smycroft 		newassoc = 1;
225087515e34Sskrll 		if (ic->ic_curmode == IEEE80211_MODE_11G)
225190634029Sdyoung 			ieee80211_node_join_11g(ic, ni);
225235515831Smycroft 	} else
225335515831Smycroft 		newassoc = 0;
225435515831Smycroft 
225535515831Smycroft 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
225690634029Sdyoung 	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
225790634029Sdyoung 	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
225890634029Sdyoung 	    IEEE80211_NODE_AID(ni),
225990634029Sdyoung 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
226090634029Sdyoung 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
226190634029Sdyoung 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
226290634029Sdyoung 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
226390634029Sdyoung 	);
226435515831Smycroft 
226535515831Smycroft 	/* give driver a chance to setup state like ni_txrate */
226690634029Sdyoung 	if (ic->ic_newassoc != NULL)
226787515e34Sskrll 		ic->ic_newassoc(ni, newassoc);
226890634029Sdyoung 	ni->ni_inact_reload = ic->ic_inact_auth;
226990634029Sdyoung 	ni->ni_inact = ni->ni_inact_reload;
227035515831Smycroft 	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
227190634029Sdyoung 	/* tell the authenticator about new station */
227290634029Sdyoung 	if (ic->ic_auth->ia_node_join != NULL)
227390634029Sdyoung 		ic->ic_auth->ia_node_join(ic, ni);
227490634029Sdyoung 	ieee80211_notify_node_join(ic, ni, newassoc);
227590634029Sdyoung }
227690634029Sdyoung 
227790634029Sdyoung /*
227890634029Sdyoung  * Handle a station leaving an 11g network.
227990634029Sdyoung  */
228090634029Sdyoung static void
ieee80211_node_leave_11g(struct ieee80211com * ic,struct ieee80211_node * ni)228190634029Sdyoung ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
228290634029Sdyoung {
228390634029Sdyoung 
228487515e34Sskrll 	IASSERT(ic->ic_curmode == IEEE80211_MODE_11G,
228587515e34Sskrll 	     ("not in 11g, bss %u:0x%x, curmode %u", ni->ni_chan->ic_freq,
228687515e34Sskrll 	      ni->ni_chan->ic_flags, ic->ic_curmode));
228790634029Sdyoung 
228890634029Sdyoung 	/*
228990634029Sdyoung 	 * If a long slot station do the slot time bookkeeping.
229090634029Sdyoung 	 */
229190634029Sdyoung 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
229290634029Sdyoung 		IASSERT(ic->ic_longslotsta > 0,
229390634029Sdyoung 		    ("bogus long slot station count %d", ic->ic_longslotsta));
229490634029Sdyoung 		ic->ic_longslotsta--;
229590634029Sdyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
229690634029Sdyoung 		    "[%s] long slot time station leaves, count now %d\n",
229790634029Sdyoung 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
229890634029Sdyoung 		if (ic->ic_longslotsta == 0) {
229990634029Sdyoung 			/*
230090634029Sdyoung 			 * Re-enable use of short slot time if supported
230190634029Sdyoung 			 * and not operating in IBSS mode (per spec).
230290634029Sdyoung 			 */
230390634029Sdyoung 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
230490634029Sdyoung 			    ic->ic_opmode != IEEE80211_M_IBSS) {
230590634029Sdyoung 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
230690634029Sdyoung 				    "%s: re-enable use of short slot time\n",
230790634029Sdyoung 				    __func__);
230890634029Sdyoung 				ieee80211_set_shortslottime(ic, 1);
230990634029Sdyoung 			}
231090634029Sdyoung 		}
231190634029Sdyoung 	}
2312c153ca3cSmaxv 
231390634029Sdyoung 	/*
231490634029Sdyoung 	 * If a non-ERP station do the protection-related bookkeeping.
231590634029Sdyoung 	 */
231690634029Sdyoung 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
231790634029Sdyoung 		IASSERT(ic->ic_nonerpsta > 0,
231890634029Sdyoung 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
231990634029Sdyoung 		ic->ic_nonerpsta--;
232090634029Sdyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
232190634029Sdyoung 		    "[%s] non-ERP station leaves, count now %d\n",
232290634029Sdyoung 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
232390634029Sdyoung 		if (ic->ic_nonerpsta == 0) {
232490634029Sdyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
232590634029Sdyoung 				"%s: disable use of protection\n", __func__);
232690634029Sdyoung 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
232790634029Sdyoung 			/* XXX verify mode? */
232890634029Sdyoung 			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
232990634029Sdyoung 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
233090634029Sdyoung 				    "%s: re-enable use of short preamble\n",
233190634029Sdyoung 				    __func__);
233290634029Sdyoung 				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
233390634029Sdyoung 				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
233490634029Sdyoung 			}
233590634029Sdyoung 		}
233690634029Sdyoung 	}
233735515831Smycroft }
233835515831Smycroft 
233935515831Smycroft /*
23404388de1eSdyoung  * Handle bookkeeping for station deauthentication/disassociation
23414388de1eSdyoung  * when operating as an ap.
234235515831Smycroft  */
234335515831Smycroft void
ieee80211_node_leave(struct ieee80211com * ic,struct ieee80211_node * ni)234435515831Smycroft ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
234535515831Smycroft {
234690634029Sdyoung 	struct ieee80211_node_table *nt = ni->ni_table;
23474388de1eSdyoung 
234890634029Sdyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
234990634029Sdyoung 	    "[%s] station with aid %d leaves\n",
235090634029Sdyoung 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
235190634029Sdyoung 	IASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
235290634029Sdyoung 		ic->ic_opmode == IEEE80211_M_IBSS ||
235390634029Sdyoung 		ic->ic_opmode == IEEE80211_M_AHDEMO,
235490634029Sdyoung 		("unexpected operating mode %u", ic->ic_opmode));
2355c153ca3cSmaxv 
235635515831Smycroft 	/*
235735515831Smycroft 	 * If node wasn't previously associated all
235835515831Smycroft 	 * we need to do is reclaim the reference.
235935515831Smycroft 	 */
236090634029Sdyoung 	/* XXX ibss mode bypasses 11g and notification */
236135515831Smycroft 	if (ni->ni_associd == 0)
236290634029Sdyoung 		goto done;
2363c153ca3cSmaxv 
236490634029Sdyoung 	/*
236590634029Sdyoung 	 * Tell the authenticator the station is leaving.
236690634029Sdyoung 	 * Note that we must do this before yanking the
236790634029Sdyoung 	 * association id as the authenticator uses the
2368f0a7346dSsnj 	 * associd to locate its state block.
236990634029Sdyoung 	 */
237090634029Sdyoung 	if (ic->ic_auth->ia_node_leave != NULL)
237190634029Sdyoung 		ic->ic_auth->ia_node_leave(ic, ni);
237235515831Smycroft 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
237335515831Smycroft 	ni->ni_associd = 0;
237490634029Sdyoung 	ic->ic_sta_assoc--;
237590634029Sdyoung 
237687515e34Sskrll 	if (ic->ic_curmode == IEEE80211_MODE_11G)
237790634029Sdyoung 		ieee80211_node_leave_11g(ic, ni);
2378c153ca3cSmaxv 
237990634029Sdyoung 	/*
238090634029Sdyoung 	 * Cleanup station state.  In particular clear various
238190634029Sdyoung 	 * state that might otherwise be reused if the node
238290634029Sdyoung 	 * is reused before the reference count goes to zero
238390634029Sdyoung 	 * (and memory is reclaimed).
238490634029Sdyoung 	 */
238590634029Sdyoung 	ieee80211_sta_leave(ic, ni);
2386c153ca3cSmaxv 
238790634029Sdyoung done:
238890634029Sdyoung 	/*
238990634029Sdyoung 	 * Remove the node from any table it's recorded in and
239090634029Sdyoung 	 * drop the caller's reference.  Removal from the table
239190634029Sdyoung 	 * is important to insure the node is not reprocessed
239290634029Sdyoung 	 * for inactivity.
239390634029Sdyoung 	 */
239490634029Sdyoung 	if (nt != NULL) {
239590634029Sdyoung 		IEEE80211_NODE_LOCK(nt);
239690634029Sdyoung 		node_reclaim(nt, ni);
239790634029Sdyoung 		IEEE80211_NODE_UNLOCK(nt);
239890634029Sdyoung 	} else
239990634029Sdyoung 		ieee80211_free_node(ni);
240090634029Sdyoung }
240190634029Sdyoung 
2402111ee86dSroy u_int8_t
ieee80211_getrssi(struct ieee80211com * ic)240390634029Sdyoung ieee80211_getrssi(struct ieee80211com *ic)
240490634029Sdyoung {
240590634029Sdyoung #define	NZ(x)	((x) == 0 ? 1 : (x))
240690634029Sdyoung 	struct ieee80211_node_table *nt = &ic->ic_sta;
2407111ee86dSroy 	u_int32_t rssi_samples, rssi_total;
240890634029Sdyoung 	struct ieee80211_node *ni;
240990634029Sdyoung 
241090634029Sdyoung 	rssi_total = 0;
241190634029Sdyoung 	rssi_samples = 0;
241290634029Sdyoung 	switch (ic->ic_opmode) {
241390634029Sdyoung 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
241490634029Sdyoung 		/* XXX locking */
241590634029Sdyoung 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
241690634029Sdyoung 			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
241790634029Sdyoung 				rssi_samples++;
241890634029Sdyoung 				rssi_total += ic->ic_node_getrssi(ni);
241990634029Sdyoung 			}
242090634029Sdyoung 		break;
242190634029Sdyoung 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
242290634029Sdyoung 		/* XXX locking */
242390634029Sdyoung 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
242490634029Sdyoung 			rssi_samples++;
242590634029Sdyoung 			rssi_total += ic->ic_node_getrssi(ni);
242690634029Sdyoung 		}
242790634029Sdyoung 		break;
242890634029Sdyoung 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
242974988b0fSdyoung #ifndef IEEE80211_NO_HOSTAP
243090634029Sdyoung 		/* XXX locking */
243190634029Sdyoung 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
243290634029Sdyoung 			if (IEEE80211_AID(ni->ni_associd) != 0) {
243390634029Sdyoung 				rssi_samples++;
243490634029Sdyoung 				rssi_total += ic->ic_node_getrssi(ni);
243590634029Sdyoung 			}
243674988b0fSdyoung #endif /* !IEEE80211_NO_HOSTAP */
243790634029Sdyoung 		break;
243890634029Sdyoung 	case IEEE80211_M_MONITOR:	/* XXX */
243990634029Sdyoung 	case IEEE80211_M_STA:		/* use stats from associated ap */
244090634029Sdyoung 	default:
244190634029Sdyoung 		if (ic->ic_bss != NULL)
244290634029Sdyoung 			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
244390634029Sdyoung 		rssi_samples = 1;
244490634029Sdyoung 		break;
244590634029Sdyoung 	}
244690634029Sdyoung 	return rssi_total / NZ(rssi_samples);
244790634029Sdyoung #undef NZ
244890634029Sdyoung }
244990634029Sdyoung 
245090634029Sdyoung /*
245190634029Sdyoung  * Indicate whether there are frames queued for a station in power-save mode.
245290634029Sdyoung  */
245390634029Sdyoung static void
ieee80211_set_tim(struct ieee80211_node * ni,int set)245487515e34Sskrll ieee80211_set_tim(struct ieee80211_node *ni, int set)
245590634029Sdyoung {
245687515e34Sskrll 	struct ieee80211com *ic = ni->ni_ic;
245790634029Sdyoung 	u_int16_t aid;
245890634029Sdyoung 
245990634029Sdyoung 	IASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
246090634029Sdyoung 		ic->ic_opmode == IEEE80211_M_IBSS,
246190634029Sdyoung 		("operating mode %u", ic->ic_opmode));
246290634029Sdyoung 
246390634029Sdyoung 	aid = IEEE80211_AID(ni->ni_associd);
246490634029Sdyoung 	IASSERT(aid < ic->ic_max_aid,
246590634029Sdyoung 		("bogus aid %u, max %u", aid, ic->ic_max_aid));
246690634029Sdyoung 
246790634029Sdyoung 	IEEE80211_BEACON_LOCK(ic);
246890634029Sdyoung 	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
246990634029Sdyoung 		if (set) {
247090634029Sdyoung 			setbit(ic->ic_tim_bitmap, aid);
247190634029Sdyoung 			ic->ic_ps_pending++;
247290634029Sdyoung 		} else {
247390634029Sdyoung 			clrbit(ic->ic_tim_bitmap, aid);
247490634029Sdyoung 			ic->ic_ps_pending--;
247590634029Sdyoung 		}
247690634029Sdyoung 		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
247790634029Sdyoung 	}
247890634029Sdyoung 	IEEE80211_BEACON_UNLOCK(ic);
247990634029Sdyoung }
248090634029Sdyoung 
248190634029Sdyoung /*
248290634029Sdyoung  * Node table support.
248390634029Sdyoung  */
248490634029Sdyoung 
248590634029Sdyoung static void
ieee80211_node_table_init(struct ieee80211com * ic,struct ieee80211_node_table * nt,const char * name,int inact,int keyixmax,void (* timeout)(struct ieee80211_node_table *))248690634029Sdyoung ieee80211_node_table_init(struct ieee80211com *ic,
248790634029Sdyoung 	struct ieee80211_node_table *nt,
248887515e34Sskrll 	const char *name, int inact, int keyixmax,
248990634029Sdyoung 	void (*timeout)(struct ieee80211_node_table *))
249090634029Sdyoung {
249190634029Sdyoung 
249290634029Sdyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
249390634029Sdyoung 		"%s %s table, inact %u\n", __func__, name, inact);
249490634029Sdyoung 
249590634029Sdyoung 	nt->nt_ic = ic;
249690634029Sdyoung 	/* XXX need unit */
249790634029Sdyoung 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
249890634029Sdyoung 	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
249990634029Sdyoung 	TAILQ_INIT(&nt->nt_node);
250090634029Sdyoung 	nt->nt_name = name;
250190634029Sdyoung 	nt->nt_scangen = 1;
250290634029Sdyoung 	nt->nt_inact_init = inact;
250390634029Sdyoung 	nt->nt_timeout = timeout;
250487515e34Sskrll 	nt->nt_keyixmax = keyixmax;
250587515e34Sskrll 	if (nt->nt_keyixmax > 0) {
25065a57baa4Schristos 		nt->nt_keyixmap = malloc(keyixmax *
25075a57baa4Schristos 		    sizeof(struct ieee80211_node *), M_80211_NODE,
2508d47bcd29Schs 		    M_WAITOK | M_ZERO);
250987515e34Sskrll 	} else
251087515e34Sskrll 		nt->nt_keyixmap = NULL;
251190634029Sdyoung }
251290634029Sdyoung 
251390634029Sdyoung void
ieee80211_node_table_reset(struct ieee80211_node_table * nt)251490634029Sdyoung ieee80211_node_table_reset(struct ieee80211_node_table *nt)
251590634029Sdyoung {
251690634029Sdyoung 
251790634029Sdyoung 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
251890634029Sdyoung 		"%s %s table\n", __func__, nt->nt_name);
251990634029Sdyoung 
252090634029Sdyoung 	IEEE80211_NODE_LOCK(nt);
252190634029Sdyoung 	nt->nt_inact_timer = 0;
252290634029Sdyoung 	ieee80211_free_allnodes_locked(nt);
252390634029Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
252490634029Sdyoung }
252590634029Sdyoung 
252690634029Sdyoung static void
ieee80211_node_table_cleanup(struct ieee80211_node_table * nt)252790634029Sdyoung ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
252890634029Sdyoung {
252990634029Sdyoung 
253090634029Sdyoung 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
253190634029Sdyoung 		"%s %s table\n", __func__, nt->nt_name);
253290634029Sdyoung 
253387515e34Sskrll 	IEEE80211_NODE_LOCK(nt);
253490634029Sdyoung 	ieee80211_free_allnodes_locked(nt);
2535d0630858Sdyoung 	IEEE80211_NODE_UNLOCK(nt);
253687515e34Sskrll 	if (nt->nt_keyixmap != NULL) {
253787515e34Sskrll 		/* XXX verify all entries are NULL */
253887515e34Sskrll 		int i;
253987515e34Sskrll 		for (i = 0; i < nt->nt_keyixmax; i++)
254087515e34Sskrll 			if (nt->nt_keyixmap[i] != NULL)
254187515e34Sskrll 				printf("%s: %s[%u] still active\n", __func__,
254287515e34Sskrll 					nt->nt_name, i);
25439b87d582Scegger 		free(nt->nt_keyixmap, M_80211_NODE);
254487515e34Sskrll 		nt->nt_keyixmap = NULL;
254587515e34Sskrll 	}
254690634029Sdyoung 	IEEE80211_SCAN_LOCK_DESTROY(nt);
254790634029Sdyoung 	IEEE80211_NODE_LOCK_DESTROY(nt);
254835515831Smycroft }
2549