13147Sxc151355 /*
2*8594SFei.Feng@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
33147Sxc151355  * Use is subject to license terms.
43147Sxc151355  */
53147Sxc151355 
63147Sxc151355 /*
73147Sxc151355  * Copyright (c) 2001 Atsushi Onoe
8*8594SFei.Feng@Sun.COM  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
93147Sxc151355  * All rights reserved.
103147Sxc151355  *
113147Sxc151355  * Redistribution and use in source and binary forms, with or without
123147Sxc151355  * modification, are permitted provided that the following conditions
133147Sxc151355  * are met:
143147Sxc151355  * 1. Redistributions of source code must retain the above copyright
153147Sxc151355  *    notice, this list of conditions and the following disclaimer.
163147Sxc151355  * 2. Redistributions in binary form must reproduce the above copyright
173147Sxc151355  *    notice, this list of conditions and the following disclaimer in the
183147Sxc151355  *    documentation and/or other materials provided with the distribution.
193147Sxc151355  * 3. The name of the author may not be used to endorse or promote products
203147Sxc151355  *    derived from this software without specific prior written permission.
213147Sxc151355  *
223147Sxc151355  * Alternatively, this software may be distributed under the terms of the
233147Sxc151355  * GNU General Public License ("GPL") version 2 as published by the Free
243147Sxc151355  * Software Foundation.
253147Sxc151355  *
263147Sxc151355  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
273147Sxc151355  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
283147Sxc151355  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
293147Sxc151355  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
303147Sxc151355  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
313147Sxc151355  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
323147Sxc151355  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
333147Sxc151355  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
343147Sxc151355  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
353147Sxc151355  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
363147Sxc151355  */
373147Sxc151355 
383147Sxc151355 /*
393147Sxc151355  * Process received frame
403147Sxc151355  */
413147Sxc151355 
428275SEric Cheng #include <sys/mac_provider.h>
433147Sxc151355 #include <sys/byteorder.h>
447249Sff224033 #include <sys/strsun.h>
453147Sxc151355 #include "net80211_impl.h"
463147Sxc151355 
473147Sxc151355 static mblk_t *ieee80211_defrag(ieee80211com_t *, ieee80211_node_t *,
483147Sxc151355     mblk_t *, int);
493147Sxc151355 
503147Sxc151355 /*
513147Sxc151355  * Process a received frame.  The node associated with the sender
523147Sxc151355  * should be supplied.  If nothing was found in the node table then
533147Sxc151355  * the caller is assumed to supply a reference to ic_bss instead.
543147Sxc151355  * The RSSI and a timestamp are also supplied.  The RSSI data is used
553147Sxc151355  * during AP scanning to select a AP to associate with; it can have
563147Sxc151355  * any units so long as values have consistent units and higher values
573147Sxc151355  * mean ``better signal''.  The receive timestamp is currently not used
583147Sxc151355  * by the 802.11 layer.
593147Sxc151355  */
603147Sxc151355 int
613147Sxc151355 ieee80211_input(ieee80211com_t *ic, mblk_t *mp, struct ieee80211_node *in,
623147Sxc151355     int32_t rssi, uint32_t rstamp)
633147Sxc151355 {
643147Sxc151355 	struct ieee80211_frame *wh;
653147Sxc151355 	struct ieee80211_key *key;
663147Sxc151355 	uint8_t *bssid;
673147Sxc151355 	int hdrspace;
683147Sxc151355 	int len;
693147Sxc151355 	uint16_t rxseq;
703147Sxc151355 	uint8_t dir;
713147Sxc151355 	uint8_t type;
723147Sxc151355 	uint8_t subtype;
733147Sxc151355 	uint8_t tid;
743147Sxc151355 
753147Sxc151355 	ASSERT(in != NULL);
76*8594SFei.Feng@Sun.COM 	in->in_inact = in->in_inact_reload;
773147Sxc151355 	type = (uint8_t)-1;		/* undefined */
787249Sff224033 	len = MBLKL(mp);
793147Sxc151355 	if (len < sizeof (struct ieee80211_frame_min)) {
803147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
817249Sff224033 		    "too short (1): len %u", len);
823147Sxc151355 		goto out;
833147Sxc151355 	}
843147Sxc151355 	/*
853147Sxc151355 	 * Bit of a cheat here, we use a pointer for a 3-address
863147Sxc151355 	 * frame format but don't reference fields past outside
873147Sxc151355 	 * ieee80211_frame_min w/o first validating the data is
883147Sxc151355 	 * present.
893147Sxc151355 	 */
903147Sxc151355 	wh = (struct ieee80211_frame *)mp->b_rptr;
913147Sxc151355 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
923147Sxc151355 	    IEEE80211_FC0_VERSION_0) {
933147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
947249Sff224033 		    "discard pkt with wrong version %x", wh->i_fc[0]);
953147Sxc151355 		goto out;
963147Sxc151355 	}
973147Sxc151355 
983147Sxc151355 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
993147Sxc151355 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1003147Sxc151355 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1013147Sxc151355 
1023147Sxc151355 	IEEE80211_LOCK(ic);
1033147Sxc151355 	if (!(ic->ic_flags & IEEE80211_F_SCAN)) {
1043147Sxc151355 		switch (ic->ic_opmode) {
1053147Sxc151355 		case IEEE80211_M_STA:
1063147Sxc151355 			bssid = wh->i_addr2;
1073147Sxc151355 			if (!IEEE80211_ADDR_EQ(bssid, in->in_bssid))
1083147Sxc151355 				goto out_exit_mutex;
1093147Sxc151355 			break;
1103147Sxc151355 		case IEEE80211_M_IBSS:
1113147Sxc151355 		case IEEE80211_M_AHDEMO:
1123147Sxc151355 			if (dir != IEEE80211_FC1_DIR_NODS) {
1133147Sxc151355 				bssid = wh->i_addr1;
1143147Sxc151355 			} else if (type == IEEE80211_FC0_TYPE_CTL) {
1153147Sxc151355 				bssid = wh->i_addr1;
1163147Sxc151355 			} else {
1173147Sxc151355 				if (len < sizeof (struct ieee80211_frame)) {
1183147Sxc151355 					ieee80211_dbg(IEEE80211_MSG_ANY,
1197249Sff224033 					    "ieee80211_input: too short(2):"
1207249Sff224033 					    "len %u\n", len);
1213147Sxc151355 					goto out_exit_mutex;
1223147Sxc151355 				}
1233147Sxc151355 				bssid = wh->i_addr3;
1243147Sxc151355 			}
1253147Sxc151355 			if (type != IEEE80211_FC0_TYPE_DATA)
1263147Sxc151355 				break;
1273147Sxc151355 			/*
1283147Sxc151355 			 * Data frame, validate the bssid.
1293147Sxc151355 			 */
1303147Sxc151355 			if (!IEEE80211_ADDR_EQ(bssid, ic->ic_bss->in_bssid) &&
1313147Sxc151355 			    !IEEE80211_ADDR_EQ(bssid, wifi_bcastaddr)) {
1323147Sxc151355 				/* not interested in */
1333147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
1347249Sff224033 				    "ieee80211_input: not to bss %s\n",
1357249Sff224033 				    ieee80211_macaddr_sprintf(bssid));
1363147Sxc151355 				goto out_exit_mutex;
1373147Sxc151355 			}
1383147Sxc151355 			/*
1393147Sxc151355 			 * For adhoc mode we cons up a node when it doesn't
1403147Sxc151355 			 * exist. This should probably done after an ACL check.
1413147Sxc151355 			 */
1423147Sxc151355 			if (in == ic->ic_bss &&
1433147Sxc151355 			    ic->ic_opmode != IEEE80211_M_HOSTAP &&
1443147Sxc151355 			    !IEEE80211_ADDR_EQ(wh->i_addr2, in->in_macaddr)) {
1453147Sxc151355 				/*
1463147Sxc151355 				 * Fake up a node for this newly
1473147Sxc151355 				 * discovered member of the IBSS.
1483147Sxc151355 				 */
1493147Sxc151355 				in = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
1507249Sff224033 				    wh->i_addr2);
1513147Sxc151355 				if (in == NULL) {
1523147Sxc151355 					/* NB: stat kept for alloc failure */
1533147Sxc151355 					goto out_exit_mutex;
1543147Sxc151355 				}
1553147Sxc151355 			}
1563147Sxc151355 			break;
1573147Sxc151355 		default:
1583147Sxc151355 			goto out_exit_mutex;
1593147Sxc151355 		}
1603147Sxc151355 		in->in_rssi = (uint8_t)rssi;
1613147Sxc151355 		in->in_rstamp = rstamp;
1623147Sxc151355 		if (!(type & IEEE80211_FC0_TYPE_CTL)) {
1633147Sxc151355 			tid = 0;
1643147Sxc151355 			rxseq = (*(uint16_t *)wh->i_seq);
1653147Sxc151355 			if ((wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
1663147Sxc151355 			    (rxseq - in->in_rxseqs[tid]) <= 0) {
1673147Sxc151355 				/* duplicate, discard */
1683147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
1697249Sff224033 				    "ieee80211_input: duplicate",
1707249Sff224033 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
1717249Sff224033 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
1727249Sff224033 				    in->in_rxseqs[tid] >>
1737249Sff224033 				    IEEE80211_SEQ_SEQ_SHIFT,
1747249Sff224033 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
1757249Sff224033 				    in->in_rxseqs[tid] &
1767249Sff224033 				    IEEE80211_SEQ_FRAG_MASK,
1777249Sff224033 				    tid);
1783147Sxc151355 				ic->ic_stats.is_rx_dups++;
1793147Sxc151355 				goto out_exit_mutex;
1803147Sxc151355 			}
1813147Sxc151355 			in->in_rxseqs[tid] = rxseq;
1823147Sxc151355 		}
1833147Sxc151355 	}
1843147Sxc151355 
1853147Sxc151355 	switch (type) {
1863147Sxc151355 	case IEEE80211_FC0_TYPE_DATA:
1878099SQuaker.Fang@Sun.COM 		hdrspace = ieee80211_hdrspace(wh);
1883147Sxc151355 		if (len < hdrspace) {
1893147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
1907249Sff224033 			    "data too short: expecting %u", hdrspace);
1913147Sxc151355 			goto out_exit_mutex;
1923147Sxc151355 		}
1933147Sxc151355 		switch (ic->ic_opmode) {
1943147Sxc151355 		case IEEE80211_M_STA:
1953147Sxc151355 			if (dir != IEEE80211_FC1_DIR_FROMDS) {
1963147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
1977249Sff224033 				    "ieee80211_input: data ",
1987249Sff224033 				    "unknown dir 0x%x", dir);
1993147Sxc151355 				goto out_exit_mutex;
2003147Sxc151355 			}
2013147Sxc151355 			if (IEEE80211_IS_MULTICAST(wh->i_addr1) &&
2023147Sxc151355 			    IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_macaddr)) {
2033147Sxc151355 				/*
2043147Sxc151355 				 * In IEEE802.11 network, multicast packet
2053147Sxc151355 				 * sent from me is broadcasted from AP.
2063147Sxc151355 				 * It should be silently discarded for
2073147Sxc151355 				 * SIMPLEX interface.
2083147Sxc151355 				 */
2093147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
2107249Sff224033 				    "ieee80211_input: multicast echo\n");
2113147Sxc151355 				goto out_exit_mutex;
2123147Sxc151355 			}
2133147Sxc151355 			break;
2143147Sxc151355 		case IEEE80211_M_IBSS:
2153147Sxc151355 		case IEEE80211_M_AHDEMO:
2163147Sxc151355 			if (dir != IEEE80211_FC1_DIR_NODS) {
2173147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
2187249Sff224033 				    "ieee80211_input: unknown dir 0x%x",
2197249Sff224033 				    dir);
2203147Sxc151355 				goto out_exit_mutex;
2213147Sxc151355 			}
2223147Sxc151355 			break;
2233147Sxc151355 		default:
2243147Sxc151355 			ieee80211_err("ieee80211_input: "
2257249Sff224033 			    "receive data, unknown opmode %u, skip\n",
2267249Sff224033 			    ic->ic_opmode);
2273147Sxc151355 			goto out_exit_mutex;
2283147Sxc151355 		}
2293147Sxc151355 
2303147Sxc151355 		/*
2313147Sxc151355 		 * Handle privacy requirements.
2323147Sxc151355 		 */
2333147Sxc151355 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2343147Sxc151355 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
2353147Sxc151355 				/*
2363147Sxc151355 				 * Discard encrypted frames when privacy off.
2373147Sxc151355 				 */
2383147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
2397249Sff224033 				    "ieee80211_input: ""WEP PRIVACY off");
2403147Sxc151355 				ic->ic_stats.is_wep_errors++;
2413147Sxc151355 				goto out_exit_mutex;
2423147Sxc151355 			}
2433147Sxc151355 			key = ieee80211_crypto_decap(ic, mp, hdrspace);
2443147Sxc151355 			if (key == NULL) {
2453147Sxc151355 				/* NB: stats+msgs handled in crypto_decap */
2463147Sxc151355 				ic->ic_stats.is_wep_errors++;
2473147Sxc151355 				goto out_exit_mutex;
2483147Sxc151355 			}
2493147Sxc151355 			wh = (struct ieee80211_frame *)mp->b_rptr;
2503147Sxc151355 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
2513147Sxc151355 		} else {
2523147Sxc151355 			key = NULL;
2533147Sxc151355 		}
2543147Sxc151355 
2553147Sxc151355 		/*
2563147Sxc151355 		 * Next up, any fragmentation
2573147Sxc151355 		 */
2583147Sxc151355 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2593147Sxc151355 			mp = ieee80211_defrag(ic, in, mp, hdrspace);
2603147Sxc151355 			if (mp == NULL) {
2613147Sxc151355 				/* Fragment dropped or frame not complete yet */
2623147Sxc151355 				goto out_exit_mutex;
2633147Sxc151355 			}
2643147Sxc151355 		}
2653147Sxc151355 		wh = NULL;	/* no longer valid, catch any uses */
2663147Sxc151355 
2673147Sxc151355 		/*
2683147Sxc151355 		 * Next strip any MSDU crypto bits.
2693147Sxc151355 		 */
2703147Sxc151355 		if (key != NULL && !ieee80211_crypto_demic(ic, key, mp, 0)) {
2713147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_INPUT, "ieee80211_input: "
2727249Sff224033 			    "data demic error\n");
2733147Sxc151355 			goto out_exit_mutex;
2743147Sxc151355 		}
2753147Sxc151355 
2763147Sxc151355 		ic->ic_stats.is_rx_frags++;
2773147Sxc151355 		ic->ic_stats.is_rx_bytes += len;
2783147Sxc151355 		IEEE80211_UNLOCK(ic);
2793147Sxc151355 		mac_rx(ic->ic_mach, NULL, mp);
2803147Sxc151355 		return (IEEE80211_FC0_TYPE_DATA);
2813147Sxc151355 
2823147Sxc151355 	case IEEE80211_FC0_TYPE_MGT:
2833147Sxc151355 		if (dir != IEEE80211_FC1_DIR_NODS)
2843147Sxc151355 			goto out_exit_mutex;
2853147Sxc151355 		if (len < sizeof (struct ieee80211_frame))
2863147Sxc151355 			goto out_exit_mutex;
2873147Sxc151355 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2883147Sxc151355 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
2893147Sxc151355 				/*
2903147Sxc151355 				 * Only shared key auth frames with a challenge
2913147Sxc151355 				 * should be encrypted, discard all others.
2923147Sxc151355 				 */
2933147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
2947249Sff224033 				    "ieee80211_input: "
2957249Sff224033 				    "%s WEP set but not permitted",
2967249Sff224033 				    IEEE80211_SUBTYPE_NAME(subtype));
2973147Sxc151355 				ic->ic_stats.is_wep_errors++;
2983147Sxc151355 				goto out_exit_mutex;
2993147Sxc151355 			}
3003147Sxc151355 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
3013147Sxc151355 				/*
3023147Sxc151355 				 * Discard encrypted frames when privacy off.
3033147Sxc151355 				 */
3043147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
3057249Sff224033 				    "ieee80211_input: "
3067249Sff224033 				    "mgt WEP set but PRIVACY off");
3073147Sxc151355 				ic->ic_stats.is_wep_errors++;
3083147Sxc151355 				goto out_exit_mutex;
3093147Sxc151355 			}
3108099SQuaker.Fang@Sun.COM 			hdrspace = ieee80211_hdrspace(wh);
3113147Sxc151355 			key = ieee80211_crypto_decap(ic, mp, hdrspace);
3123147Sxc151355 			if (key == NULL) {
3133147Sxc151355 				/* NB: stats+msgs handled in crypto_decap */
3143147Sxc151355 				goto out_exit_mutex;
3153147Sxc151355 			}
3163147Sxc151355 			wh = (struct ieee80211_frame *)mp->b_rptr;
3173147Sxc151355 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3183147Sxc151355 		}
3193147Sxc151355 		IEEE80211_UNLOCK(ic);
3203147Sxc151355 		ic->ic_recv_mgmt(ic, mp, in, subtype, rssi, rstamp);
3213147Sxc151355 		goto out;
3223147Sxc151355 
3233147Sxc151355 	case IEEE80211_FC0_TYPE_CTL:
3243147Sxc151355 	default:
3253147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
3267249Sff224033 		    "bad frame type 0x%x", type);
3273147Sxc151355 		/* should not come here */
3283147Sxc151355 		break;
3293147Sxc151355 	}
3303147Sxc151355 out_exit_mutex:
3313147Sxc151355 	IEEE80211_UNLOCK(ic);
3323147Sxc151355 out:
3333147Sxc151355 	if (mp != NULL)
3343147Sxc151355 		freemsg(mp);
3353147Sxc151355 
3363147Sxc151355 	return (type);
3373147Sxc151355 }
3383147Sxc151355 
3393147Sxc151355 /*
3403147Sxc151355  * This function reassemble fragments.
3413147Sxc151355  * More fragments bit in the frame control means the packet is fragmented.
3423147Sxc151355  * While the sequence control field consists of 4-bit fragment number
3433147Sxc151355  * field and a 12-bit sequence number field.
3443147Sxc151355  */
3453147Sxc151355 /* ARGSUSED */
3463147Sxc151355 static mblk_t *
3473147Sxc151355 ieee80211_defrag(ieee80211com_t *ic, struct ieee80211_node *in, mblk_t *mp,
3483147Sxc151355     int hdrspace)
3493147Sxc151355 {
3503147Sxc151355 	struct ieee80211_frame *wh = (struct ieee80211_frame *)mp->b_rptr;
3513147Sxc151355 	struct ieee80211_frame *lwh;
3523147Sxc151355 	mblk_t *mfrag;
3533147Sxc151355 	uint16_t rxseq;
3543147Sxc151355 	uint8_t fragno;
3553147Sxc151355 	uint8_t more_frag;
3563147Sxc151355 
3573147Sxc151355 	ASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1));
3583147Sxc151355 	more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG;
3593147Sxc151355 	rxseq = LE_16(*(uint16_t *)wh->i_seq);
3603147Sxc151355 	fragno = rxseq & IEEE80211_SEQ_FRAG_MASK;
3613147Sxc151355 
3623147Sxc151355 	/* Quick way out, if there's nothing to defragment */
3633147Sxc151355 	if (!more_frag && fragno == 0 && in->in_rxfrag == NULL)
3643147Sxc151355 		return (mp);
3653147Sxc151355 
3663147Sxc151355 	/*
3673147Sxc151355 	 * Remove frag to insure it doesn't get reaped by timer.
3683147Sxc151355 	 */
3693147Sxc151355 	if (in->in_table == NULL) {
3703147Sxc151355 		/*
3713147Sxc151355 		 * Should never happen.  If the node is orphaned (not in
3723147Sxc151355 		 * the table) then input packets should not reach here.
3733147Sxc151355 		 * Otherwise, a concurrent request that yanks the table
3743147Sxc151355 		 * should be blocked by other interlocking and/or by first
3753147Sxc151355 		 * shutting the driver down.  Regardless, be defensive
3763147Sxc151355 		 * here and just bail
3773147Sxc151355 		 */
3783147Sxc151355 		freemsg(mp);
3793147Sxc151355 		return (NULL);
3803147Sxc151355 	}
3813147Sxc151355 	IEEE80211_NODE_LOCK(in->in_table);
3823147Sxc151355 	mfrag = in->in_rxfrag;
3833147Sxc151355 	in->in_rxfrag = NULL;
3843147Sxc151355 	IEEE80211_NODE_UNLOCK(in->in_table);
3853147Sxc151355 
3863147Sxc151355 	/*
3873147Sxc151355 	 * Validate new fragment is in order and
3883147Sxc151355 	 * related to the previous ones.
3893147Sxc151355 	 */
3903147Sxc151355 	if (mfrag != NULL) {
3913147Sxc151355 		uint16_t last_rxseq;
3923147Sxc151355 
3933147Sxc151355 		lwh = (struct ieee80211_frame *)mfrag->b_rptr;
3943147Sxc151355 		last_rxseq = LE_16(*(uint16_t *)lwh->i_seq);
3953147Sxc151355 		/*
3963147Sxc151355 		 * Sequence control field contains 12-bit sequence no
3973147Sxc151355 		 * and 4-bit fragment number. For fragemnts, the
3983147Sxc151355 		 * sequence no is not changed.
3993147Sxc151355 		 * NB: check seq # and frag together
4003147Sxc151355 		 */
4013147Sxc151355 		if (rxseq != last_rxseq + 1 ||
4023147Sxc151355 		    !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) ||
4033147Sxc151355 		    !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) {
4043147Sxc151355 			/*
4053147Sxc151355 			 * Unrelated fragment or no space for it,
4063147Sxc151355 			 * clear current fragments.
4073147Sxc151355 			 */
4083147Sxc151355 			freemsg(mfrag);
4093147Sxc151355 			mfrag = NULL;
4103147Sxc151355 		}
4113147Sxc151355 	}
4123147Sxc151355 
4133147Sxc151355 	if (mfrag == NULL) {
4143147Sxc151355 		if (fragno != 0) {	/* !first fragment, discard */
4153147Sxc151355 			freemsg(mp);
4163147Sxc151355 			return (NULL);
4173147Sxc151355 		}
4183147Sxc151355 		mfrag = mp;
4193147Sxc151355 	} else {			/* concatenate */
4203147Sxc151355 		(void) adjmsg(mp, hdrspace);
4213147Sxc151355 		linkb(mfrag, mp);
4223147Sxc151355 		/* track last seqnum and fragno */
4233147Sxc151355 		lwh = (struct ieee80211_frame *)mfrag->b_rptr;
4243147Sxc151355 		*(uint16_t *)lwh->i_seq = *(uint16_t *)wh->i_seq;
4253147Sxc151355 	}
4263147Sxc151355 	if (more_frag != 0) {		/* more to come, save */
4273147Sxc151355 		in->in_rxfragstamp = ddi_get_lbolt();
4283147Sxc151355 		in->in_rxfrag = mfrag;
4293147Sxc151355 		mfrag = NULL;
4303147Sxc151355 	}
4313147Sxc151355 
4323147Sxc151355 	return (mfrag);
4333147Sxc151355 }
4343147Sxc151355 
4353147Sxc151355 /*
4363147Sxc151355  * Install received rate set information in the node's state block.
4373147Sxc151355  */
4383147Sxc151355 int
4393147Sxc151355 ieee80211_setup_rates(struct ieee80211_node *in, const uint8_t *rates,
4403147Sxc151355     const uint8_t *xrates, int flags)
4413147Sxc151355 {
4423147Sxc151355 	struct ieee80211_rateset *rs = &in->in_rates;
4433147Sxc151355 
4443147Sxc151355 	bzero(rs, sizeof (*rs));
4453147Sxc151355 	rs->ir_nrates = rates[1];
4463147Sxc151355 	/* skip 1 byte element ID and 1 byte length */
4473147Sxc151355 	bcopy(rates + 2, rs->ir_rates, rs->ir_nrates);
4483147Sxc151355 	if (xrates != NULL) {
4493147Sxc151355 		uint8_t nxrates;
4503147Sxc151355 
4513147Sxc151355 		/*
4523147Sxc151355 		 * Tack on 11g extended supported rate element.
4533147Sxc151355 		 */
4543147Sxc151355 		nxrates = xrates[1];
4553147Sxc151355 		if (rs->ir_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
4563147Sxc151355 			nxrates = IEEE80211_RATE_MAXSIZE - rs->ir_nrates;
4573147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_XRATE,
4587249Sff224033 			    "ieee80211_setup_rates: %s",
4597249Sff224033 			    "[%s] extended rate set too large;"
4607249Sff224033 			    " only using %u of %u rates\n",
4617249Sff224033 			    ieee80211_macaddr_sprintf(in->in_macaddr),
4627249Sff224033 			    nxrates, xrates[1]);
4633147Sxc151355 		}
4643147Sxc151355 		bcopy(xrates + 2, rs->ir_rates + rs->ir_nrates, nxrates);
4653147Sxc151355 		rs->ir_nrates += nxrates;
4663147Sxc151355 	}
4673147Sxc151355 	return (ieee80211_fix_rate(in, flags));
4683147Sxc151355 }
4693147Sxc151355 
4703147Sxc151355 /*
4713147Sxc151355  * Process open-system authentication response frame and start
4723147Sxc151355  * association if the authentication request is accepted.
4733147Sxc151355  */
4743147Sxc151355 static void
4753147Sxc151355 ieee80211_auth_open(ieee80211com_t *ic, struct ieee80211_frame *wh,
4763147Sxc151355     struct ieee80211_node *in, uint16_t seq, uint16_t status)
4773147Sxc151355 {
4783147Sxc151355 	IEEE80211_LOCK_ASSERT(ic);
4793147Sxc151355 	if (in->in_authmode == IEEE80211_AUTH_SHARED) {
4803147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH,
4817249Sff224033 		    "open auth: bad sta auth mode %u", in->in_authmode);
4823147Sxc151355 		return;
4833147Sxc151355 	}
4843147Sxc151355 	if (ic->ic_opmode == IEEE80211_M_STA) {
4853147Sxc151355 		if (ic->ic_state != IEEE80211_S_AUTH ||
4863147Sxc151355 		    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
4873147Sxc151355 			return;
4883147Sxc151355 		}
4893147Sxc151355 		IEEE80211_UNLOCK(ic);
4903147Sxc151355 		if (status != 0) {
4913147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
4927249Sff224033 			    "open auth failed (reason %d)\n", status);
4933147Sxc151355 			if (in != ic->ic_bss)
4943147Sxc151355 				in->in_fails++;
4953147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
4963147Sxc151355 		} else {
4973147Sxc151355 			/* i_fc[0] - frame control's type & subtype field */
4983147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
4997249Sff224033 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
5003147Sxc151355 		}
5013147Sxc151355 		IEEE80211_LOCK(ic);
5023147Sxc151355 	} else {
5033147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_auth_open: "
5047249Sff224033 		    "bad operating mode %u", ic->ic_opmode);
5053147Sxc151355 	}
5063147Sxc151355 }
5073147Sxc151355 
5083147Sxc151355 /*
5093147Sxc151355  * Allocate challenge text for use by shared-key authentication
5103147Sxc151355  * Return B_TRUE on success, B_FALST otherwise.
5113147Sxc151355  */
5123147Sxc151355 static boolean_t
5133147Sxc151355 ieee80211_alloc_challenge(struct ieee80211_node *in)
5143147Sxc151355 {
5153147Sxc151355 	if (in->in_challenge == NULL) {
5163147Sxc151355 		in->in_challenge = kmem_alloc(IEEE80211_CHALLENGE_LEN,
5177249Sff224033 		    KM_NOSLEEP);
5183147Sxc151355 	}
5193147Sxc151355 	if (in->in_challenge == NULL) {
5203147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
5217249Sff224033 		    "[%s] shared key challenge alloc failed\n",
5227249Sff224033 		    ieee80211_macaddr_sprintf(in->in_macaddr));
5233147Sxc151355 	}
5243147Sxc151355 	return (in->in_challenge != NULL);
5253147Sxc151355 }
5263147Sxc151355 
5273147Sxc151355 /*
5283147Sxc151355  * Process shared-key authentication response frames. If authentication
5293147Sxc151355  * succeeds, start association; otherwise, restart scan.
5303147Sxc151355  */
5313147Sxc151355 static void
5323147Sxc151355 ieee80211_auth_shared(ieee80211com_t *ic, struct ieee80211_frame *wh,
5333147Sxc151355     uint8_t *frm, uint8_t *efrm, struct ieee80211_node *in, uint16_t seq,
5343147Sxc151355     uint16_t status)
5353147Sxc151355 {
5363147Sxc151355 	uint8_t *challenge;
5373147Sxc151355 
5383147Sxc151355 	/*
5393147Sxc151355 	 * Pre-shared key authentication is evil; accept
5403147Sxc151355 	 * it only if explicitly configured (it is supported
5413147Sxc151355 	 * mainly for compatibility with clients like OS X).
5423147Sxc151355 	 */
5433147Sxc151355 	IEEE80211_LOCK_ASSERT(ic);
5443147Sxc151355 	if (in->in_authmode != IEEE80211_AUTH_AUTO &&
5453147Sxc151355 	    in->in_authmode != IEEE80211_AUTH_SHARED) {
5463147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_auth_shared: "
5477249Sff224033 		    "bad sta auth mode %u", in->in_authmode);
5483147Sxc151355 		goto bad;
5493147Sxc151355 	}
5503147Sxc151355 
5513147Sxc151355 	challenge = NULL;
5523147Sxc151355 	if (frm + 1 < efrm) {
5533147Sxc151355 		/*
5543147Sxc151355 		 * Challenge text information element
5553147Sxc151355 		 * frm[0] - element ID
5563147Sxc151355 		 * frm[1] - length
5573147Sxc151355 		 * frm[2]... - challenge text
5583147Sxc151355 		 */
5597249Sff224033 		if ((frm[1] + 2) > (_PTRDIFF(efrm, frm))) {
5603147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH,
5617249Sff224033 			    "ieee80211_auth_shared: ie %d%d too long\n",
5627249Sff224033 			    frm[0], (frm[1] + 2) - (_PTRDIFF(efrm, frm)));
5633147Sxc151355 			goto bad;
5643147Sxc151355 		}
5653147Sxc151355 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
5663147Sxc151355 			challenge = frm;
5673147Sxc151355 		frm += frm[1] + 2;
5683147Sxc151355 	}
5693147Sxc151355 	switch (seq) {
5703147Sxc151355 	case IEEE80211_AUTH_SHARED_CHALLENGE:
5713147Sxc151355 	case IEEE80211_AUTH_SHARED_RESPONSE:
5723147Sxc151355 		if (challenge == NULL) {
5733147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH,
5747249Sff224033 			    "ieee80211_auth_shared: no challenge\n");
5753147Sxc151355 			goto bad;
5763147Sxc151355 		}
5773147Sxc151355 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
5783147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH,
5797249Sff224033 			    "ieee80211_auth_shared: bad challenge len %d\n",
5807249Sff224033 			    challenge[1]);
5813147Sxc151355 			goto bad;
5823147Sxc151355 		}
5833147Sxc151355 	default:
5843147Sxc151355 		break;
5853147Sxc151355 	}
5863147Sxc151355 	switch (ic->ic_opmode) {
5873147Sxc151355 	case IEEE80211_M_STA:
5883147Sxc151355 		if (ic->ic_state != IEEE80211_S_AUTH)
5893147Sxc151355 			return;
5903147Sxc151355 		switch (seq) {
5913147Sxc151355 		case IEEE80211_AUTH_SHARED_PASS:
5923147Sxc151355 			if (in->in_challenge != NULL) {
5933147Sxc151355 				kmem_free(in->in_challenge,
5943147Sxc151355 				    IEEE80211_CHALLENGE_LEN);
5953147Sxc151355 				in->in_challenge = NULL;
5963147Sxc151355 			}
5973147Sxc151355 			if (status != 0) {
5983147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_DEBUG |
5997249Sff224033 				    IEEE80211_MSG_AUTH,
6007249Sff224033 				    "shared key auth failed (reason %d)\n",
6017249Sff224033 				    status);
6023147Sxc151355 				if (in != ic->ic_bss)
6033147Sxc151355 					in->in_fails++;
6043147Sxc151355 				return;
6053147Sxc151355 			}
6063147Sxc151355 			IEEE80211_UNLOCK(ic);
6073147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
6087249Sff224033 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
6093147Sxc151355 			IEEE80211_LOCK(ic);
6103147Sxc151355 			break;
6113147Sxc151355 		case IEEE80211_AUTH_SHARED_CHALLENGE:
6123147Sxc151355 			if (!ieee80211_alloc_challenge(in))
6133147Sxc151355 				return;
6143147Sxc151355 			bcopy(&challenge[2], in->in_challenge, challenge[1]);
6153147Sxc151355 			IEEE80211_UNLOCK(ic);
6163147Sxc151355 			IEEE80211_SEND_MGMT(ic, in, IEEE80211_FC0_SUBTYPE_AUTH,
6177249Sff224033 			    seq + 1);
6183147Sxc151355 			IEEE80211_LOCK(ic);
6193147Sxc151355 			break;
6203147Sxc151355 		default:
6213147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH, "80211_auth_shared: "
6227249Sff224033 			    "shared key auth: bad seq %d", seq);
6233147Sxc151355 			return;
6243147Sxc151355 		}
6253147Sxc151355 		break;
6263147Sxc151355 
6273147Sxc151355 	default:
6283147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH,
6297249Sff224033 		    "ieee80211_auth_shared: bad opmode %u\n",
6307249Sff224033 		    ic->ic_opmode);
6313147Sxc151355 		break;
6323147Sxc151355 	}
6333147Sxc151355 	return;
6343147Sxc151355 bad:
6353147Sxc151355 	if (ic->ic_opmode == IEEE80211_M_STA) {
6363147Sxc151355 		/*
6373147Sxc151355 		 * Kick the state machine.  This short-circuits
6383147Sxc151355 		 * using the mgt frame timeout to trigger the
6393147Sxc151355 		 * state transition.
6403147Sxc151355 		 */
6413147Sxc151355 		if (ic->ic_state == IEEE80211_S_AUTH) {
6423147Sxc151355 			IEEE80211_UNLOCK(ic);
6433147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
6443147Sxc151355 			IEEE80211_LOCK(ic);
6453147Sxc151355 		}
6463147Sxc151355 	}
6473147Sxc151355 }
6483147Sxc151355 
6494126Szf162725 static int
6504126Szf162725 iswpaoui(const uint8_t *frm)
6514126Szf162725 {
6524126Szf162725 	uint32_t c = *(uint32_t *)(frm + 2);
6534126Szf162725 	return (frm[1] > 3 && c == ((WPA_OUI_TYPE << 24) | WPA_OUI));
6544126Szf162725 }
6554126Szf162725 
6563147Sxc151355 /*
6573147Sxc151355  * Process a beacon/probe response frame.
6583147Sxc151355  * When the device is in station mode, create a node and add it
6593147Sxc151355  * to the node database for a new ESS or update node info if it's
6603147Sxc151355  * already there.
6613147Sxc151355  */
6623147Sxc151355 static void
6633147Sxc151355 ieee80211_recv_beacon(ieee80211com_t *ic, mblk_t *mp, struct ieee80211_node *in,
6643147Sxc151355     int subtype, int rssi, uint32_t rstamp)
6653147Sxc151355 {
6663147Sxc151355 	ieee80211_impl_t *im = ic->ic_private;
6673147Sxc151355 	struct ieee80211_frame *wh;
6683147Sxc151355 	uint8_t *frm;
6693147Sxc151355 	uint8_t *efrm;	/* end of frame body */
6703147Sxc151355 	struct ieee80211_scanparams scan;
6713147Sxc151355 
6723147Sxc151355 	wh = (struct ieee80211_frame *)mp->b_rptr;
6733147Sxc151355 	frm = (uint8_t *)&wh[1];
6743147Sxc151355 	efrm = (uint8_t *)mp->b_wptr;
6753147Sxc151355 
6763147Sxc151355 	/*
6773147Sxc151355 	 * We process beacon/probe response frames:
6783147Sxc151355 	 *    o when scanning, or
6793147Sxc151355 	 *    o station mode when associated (to collect state
6803147Sxc151355 	 *	updates such as 802.11g slot time), or
6813147Sxc151355 	 *    o adhoc mode (to discover neighbors)
6823147Sxc151355 	 * Frames otherwise received are discarded.
6833147Sxc151355 	 */
6843147Sxc151355 	if (!((ic->ic_flags & IEEE80211_F_SCAN) ||
6853147Sxc151355 	    (ic->ic_opmode == IEEE80211_M_STA && in->in_associd != 0) ||
6863147Sxc151355 	    ic->ic_opmode == IEEE80211_M_IBSS)) {
6873147Sxc151355 		return;
6883147Sxc151355 	}
6893147Sxc151355 
6903147Sxc151355 	/*
6913147Sxc151355 	 * beacon/probe response frame format
6923147Sxc151355 	 *	[8] time stamp
6933147Sxc151355 	 *	[2] beacon interval
6943147Sxc151355 	 *	[2] capability information
6953147Sxc151355 	 *	[tlv] ssid
6963147Sxc151355 	 *	[tlv] supported rates
6973147Sxc151355 	 *	[tlv] country information
6983147Sxc151355 	 *	[tlv] parameter set (FH/DS)
6993147Sxc151355 	 *	[tlv] erp information
7003147Sxc151355 	 *	[tlv] extended supported rates
7013147Sxc151355 	 *	[tlv] WME
7023147Sxc151355 	 *	[tlv] WPA or RSN
7033147Sxc151355 	 */
7047249Sff224033 	IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
7057249Sff224033 	    IEEE80211_BEACON_ELEM_MIN, return);
7063147Sxc151355 	bzero(&scan, sizeof (scan));
7073147Sxc151355 	scan.tstamp  = frm;
7083147Sxc151355 	frm += 8;
7093147Sxc151355 	scan.bintval = (*(uint16_t *)frm);
7103147Sxc151355 	frm += 2;
7113147Sxc151355 	scan.capinfo = (*(uint16_t *)frm);
7123147Sxc151355 	frm += 2;
7133147Sxc151355 	scan.bchan = ieee80211_chan2ieee(ic, ic->ic_curchan);
7143147Sxc151355 	scan.chan = scan.bchan;
7153147Sxc151355 
7163147Sxc151355 	while (frm < efrm) {
7173147Sxc151355 		/* Agere element in beacon */
7183147Sxc151355 		if ((*frm == IEEE80211_ELEMID_AGERE1) ||
7193147Sxc151355 		    (*frm == IEEE80211_ELEMID_AGERE2)) {
7203147Sxc151355 			frm = efrm;
7213147Sxc151355 			break;
7223147Sxc151355 		}
7233147Sxc151355 
7247249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm), frm[1], return);
7253147Sxc151355 		switch (*frm) {
7263147Sxc151355 		case IEEE80211_ELEMID_SSID:
7273147Sxc151355 			scan.ssid = frm;
7283147Sxc151355 			break;
7293147Sxc151355 		case IEEE80211_ELEMID_RATES:
7303147Sxc151355 			scan.rates = frm;
7313147Sxc151355 			break;
7323147Sxc151355 		case IEEE80211_ELEMID_COUNTRY:
7333147Sxc151355 			scan.country = frm;
7343147Sxc151355 			break;
7353147Sxc151355 		case IEEE80211_ELEMID_FHPARMS:
7363147Sxc151355 			if (ic->ic_phytype == IEEE80211_T_FH) {
7373147Sxc151355 				scan.fhdwell = LE_16(*(uint16_t *)(frm + 2));
7383147Sxc151355 				scan.chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
7393147Sxc151355 				scan.fhindex = frm[6];
7403147Sxc151355 				scan.phytype = IEEE80211_T_FH;
7413147Sxc151355 			}
7423147Sxc151355 			break;
7433147Sxc151355 		case IEEE80211_ELEMID_DSPARMS:
7443147Sxc151355 			if (ic->ic_phytype != IEEE80211_T_FH) {
7453147Sxc151355 				scan.chan = frm[2];
7463147Sxc151355 				scan.phytype = IEEE80211_T_DS;
7473147Sxc151355 			}
7483147Sxc151355 			break;
7493147Sxc151355 		case IEEE80211_ELEMID_TIM:
7503147Sxc151355 			scan.tim = frm;
7517249Sff224033 			scan.timoff = _PTRDIFF(frm, mp->b_rptr);
7523147Sxc151355 			break;
7533147Sxc151355 		case IEEE80211_ELEMID_IBSSPARMS:
7543147Sxc151355 			break;
7553147Sxc151355 		case IEEE80211_ELEMID_XRATES:
7563147Sxc151355 			scan.xrates = frm;
7573147Sxc151355 			break;
7583147Sxc151355 		case IEEE80211_ELEMID_ERP:
7593147Sxc151355 			if (frm[1] != 1) {
7603147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_ELEMID,
7617249Sff224033 				    "ieee80211_recv_mgmt: ignore %s, "
7627249Sff224033 				    "invalid ERP element; "
7637249Sff224033 				    "length %u, expecting 1\n",
7647249Sff224033 				    IEEE80211_SUBTYPE_NAME(subtype),
7657249Sff224033 				    frm[1]);
7663147Sxc151355 				break;
7673147Sxc151355 			}
7683147Sxc151355 			scan.erp = frm[2];
7693147Sxc151355 			scan.phytype = IEEE80211_T_OFDM;
7703147Sxc151355 			break;
7713147Sxc151355 		case IEEE80211_ELEMID_RSN:
7723147Sxc151355 			scan.wpa = frm;
7733147Sxc151355 			break;
7744126Szf162725 		case IEEE80211_ELEMID_VENDOR:
7754126Szf162725 			if (iswpaoui(frm))
7764126Szf162725 				scan.wpa = frm;		/* IEEE802.11i D3.0 */
7774126Szf162725 			break;
7783147Sxc151355 		default:
7793147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ELEMID,
7807249Sff224033 			    "ieee80211_recv_mgmt: ignore %s,"
7817249Sff224033 			    "unhandled id %u, len %u, totallen %u",
7827249Sff224033 			    IEEE80211_SUBTYPE_NAME(subtype),
7837249Sff224033 			    *frm, frm[1],
7847249Sff224033 			    MBLKL(mp));
7853147Sxc151355 			break;
7863147Sxc151355 		}
7873147Sxc151355 		/* frm[1] - component length */
7883147Sxc151355 		frm += IEEE80211_ELEM_LEN(frm[1]);
7893147Sxc151355 	}
7903147Sxc151355 	IEEE80211_VERIFY_ELEMENT(scan.rates, IEEE80211_RATE_MAXSIZE, return);
7913147Sxc151355 	IEEE80211_VERIFY_ELEMENT(scan.ssid, IEEE80211_NWID_LEN, return);
7923147Sxc151355 	if (ieee80211_isclr(ic->ic_chan_active, scan.chan)) {
7933147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
7947249Sff224033 		    "ieee80211_recv_mgmt: ignore %s ,"
7957249Sff224033 		    "invalid channel %u\n",
7967249Sff224033 		    IEEE80211_SUBTYPE_NAME(subtype), scan.chan);
7973147Sxc151355 		return;
7983147Sxc151355 	}
7993147Sxc151355 	if (scan.chan != scan.bchan &&
8003147Sxc151355 	    ic->ic_phytype != IEEE80211_T_FH) {
8013147Sxc151355 		/*
8023147Sxc151355 		 * Frame was received on a channel different from the
8033147Sxc151355 		 * one indicated in the DS params element id;
8043147Sxc151355 		 * silently discard it.
8053147Sxc151355 		 *
8063147Sxc151355 		 * NB:	this can happen due to signal leakage.
8073147Sxc151355 		 *	But we should take it for FH phy because
8083147Sxc151355 		 *	the rssi value should be correct even for
8093147Sxc151355 		 *	different hop pattern in FH.
8103147Sxc151355 		 */
8113147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ELEMID,
8127249Sff224033 		    "ieee80211_recv_mgmt: ignore %s ,"
8137249Sff224033 		    "phytype %u channel %u marked for %u\n",
8147249Sff224033 		    IEEE80211_SUBTYPE_NAME(subtype),
8157249Sff224033 		    ic->ic_phytype, scan.bchan, scan.chan);
8163147Sxc151355 		return;
8173147Sxc151355 	}
8183147Sxc151355 	if (!(IEEE80211_BINTVAL_MIN <= scan.bintval &&
8193147Sxc151355 	    scan.bintval <= IEEE80211_BINTVAL_MAX)) {
8203147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
8217249Sff224033 		    "ieee80211_recv_mgmt: ignore %s ,"
8227249Sff224033 		    "bogus beacon interval %u\n",
8237249Sff224033 		    IEEE80211_SUBTYPE_NAME(subtype), scan.bintval);
8243147Sxc151355 		return;
8253147Sxc151355 	}
8263147Sxc151355 
8273147Sxc151355 	/*
8283147Sxc151355 	 * When operating in station mode, check for state updates.
8293147Sxc151355 	 * Be careful to ignore beacons received while doing a
8303147Sxc151355 	 * background scan.  We consider only 11g/WMM stuff right now.
8313147Sxc151355 	 */
8323147Sxc151355 	if (ic->ic_opmode == IEEE80211_M_STA &&
8333147Sxc151355 	    in->in_associd != 0 &&
8343147Sxc151355 	    (!(ic->ic_flags & IEEE80211_F_SCAN) ||
8353147Sxc151355 	    IEEE80211_ADDR_EQ(wh->i_addr2, in->in_bssid))) {
8363147Sxc151355 		/* record tsf of last beacon */
8373147Sxc151355 		bcopy(scan.tstamp, in->in_tstamp.data,
8383147Sxc151355 		    sizeof (in->in_tstamp));
8393147Sxc151355 		/* count beacon frame for s/w bmiss handling */
8403147Sxc151355 		im->im_swbmiss_count++;
8413147Sxc151355 		im->im_bmiss_count = 0;
8423147Sxc151355 
8433147Sxc151355 		if ((in->in_capinfo ^ scan.capinfo) &
8443147Sxc151355 		    IEEE80211_CAPINFO_SHORT_SLOTTIME) {
8453147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
8467249Sff224033 			    "ieee80211_recv_mgmt: "
8477249Sff224033 			    "[%s] cap change: before 0x%x, now 0x%x\n",
8487249Sff224033 			    ieee80211_macaddr_sprintf(wh->i_addr2),
8497249Sff224033 			    in->in_capinfo, scan.capinfo);
8503147Sxc151355 			/*
8513147Sxc151355 			 * NB:	we assume short preamble doesn't
8523147Sxc151355 			 *	change dynamically
8533147Sxc151355 			 */
8543147Sxc151355 			ieee80211_set_shortslottime(ic,
8557249Sff224033 			    ic->ic_curmode == IEEE80211_MODE_11A ||
8567249Sff224033 			    (scan.capinfo &
8577249Sff224033 			    IEEE80211_CAPINFO_SHORT_SLOTTIME));
8583147Sxc151355 			in->in_capinfo = scan.capinfo;
8593147Sxc151355 		}
8603147Sxc151355 
8613147Sxc151355 		if (scan.tim != NULL) {
8623147Sxc151355 			struct ieee80211_tim_ie *ie;
8633147Sxc151355 
8643147Sxc151355 			ie = (struct ieee80211_tim_ie *)scan.tim;
8653147Sxc151355 			in->in_dtim_count = ie->tim_count;
8663147Sxc151355 			in->in_dtim_period = ie->tim_period;
8673147Sxc151355 		}
8683147Sxc151355 		if (ic->ic_flags & IEEE80211_F_SCAN) {
8693147Sxc151355 			ieee80211_add_scan(ic, &scan, wh, subtype, rssi,
8707249Sff224033 			    rstamp);
8713147Sxc151355 		}
8723147Sxc151355 		return;
8733147Sxc151355 	}
8743147Sxc151355 	/*
8753147Sxc151355 	 * If scanning, just pass information to the scan module.
8763147Sxc151355 	 */
8773147Sxc151355 	if (ic->ic_flags & IEEE80211_F_SCAN) {
8783147Sxc151355 		ieee80211_add_scan(ic, &scan, wh, subtype, rssi, rstamp);
8793147Sxc151355 		return;
8803147Sxc151355 	}
8813147Sxc151355 
882*8594SFei.Feng@Sun.COM 	if (ic->ic_opmode == IEEE80211_M_IBSS &&
883*8594SFei.Feng@Sun.COM 	    scan.capinfo & IEEE80211_CAPINFO_IBSS) {
8843147Sxc151355 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, in->in_macaddr)) {
8853147Sxc151355 			/*
8863147Sxc151355 			 * Create a new entry in the neighbor table.
8873147Sxc151355 			 */
8883147Sxc151355 			in = ieee80211_add_neighbor(ic, wh, &scan);
889*8594SFei.Feng@Sun.COM 		} else {
8903147Sxc151355 			/*
891*8594SFei.Feng@Sun.COM 			 * Copy data from beacon to neighbor table.
892*8594SFei.Feng@Sun.COM 			 * Some of this information might change after
893*8594SFei.Feng@Sun.COM 			 * ieee80211_add_neighbor(), so we just copy
894*8594SFei.Feng@Sun.COM 			 * everything over to be safe.
8953147Sxc151355 			 */
8963147Sxc151355 			ieee80211_init_neighbor(in, wh, &scan);
8973147Sxc151355 		}
8983147Sxc151355 		if (in != NULL) {
8993147Sxc151355 			in->in_rssi = (uint8_t)rssi;
9003147Sxc151355 			in->in_rstamp = rstamp;
9013147Sxc151355 		}
9023147Sxc151355 	}
9033147Sxc151355 }
9043147Sxc151355 
9053147Sxc151355 /*
9063147Sxc151355  * Perform input processing for 802.11 management frames.
9073147Sxc151355  * It's the default ic_recv_mgmt callback function for the interface
9083147Sxc151355  * softc, ic. Tipically ic_recv_mgmt is called within ieee80211_input()
9093147Sxc151355  */
9103147Sxc151355 void
9113147Sxc151355 ieee80211_recv_mgmt(ieee80211com_t *ic, mblk_t *mp, struct ieee80211_node *in,
9123147Sxc151355     int subtype, int rssi, uint32_t rstamp)
9133147Sxc151355 {
9143147Sxc151355 	struct ieee80211_frame *wh;
9153147Sxc151355 	uint8_t *frm;		/* pointer to start of the frame */
9163147Sxc151355 	uint8_t *efrm;		/* pointer to end of the frame */
9173147Sxc151355 	uint8_t *ssid;
9183147Sxc151355 	uint8_t *rates;
9193147Sxc151355 	uint8_t *xrates;	/* extended rates */
9203147Sxc151355 	boolean_t allocbs = B_FALSE;
9213147Sxc151355 	uint8_t rate;
9223147Sxc151355 	uint16_t algo;		/* authentication algorithm */
9233147Sxc151355 	uint16_t seq;		/* sequence no */
9243147Sxc151355 	uint16_t status;
9253147Sxc151355 	uint16_t capinfo;
9263147Sxc151355 	uint16_t associd;	/* association ID */
9273147Sxc151355 
9283147Sxc151355 	IEEE80211_LOCK(ic);
9293147Sxc151355 	wh = (struct ieee80211_frame *)mp->b_rptr;
9303147Sxc151355 	frm = (uint8_t *)&wh[1];
9313147Sxc151355 	efrm = (uint8_t *)mp->b_wptr;
9323147Sxc151355 	switch (subtype) {
9333147Sxc151355 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
9343147Sxc151355 	case IEEE80211_FC0_SUBTYPE_BEACON:
9353147Sxc151355 		ieee80211_recv_beacon(ic, mp, in, subtype, rssi, rstamp);
9363147Sxc151355 		break;
9373147Sxc151355 
9383147Sxc151355 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
9393147Sxc151355 		if (ic->ic_opmode == IEEE80211_M_STA ||
9403147Sxc151355 		    ic->ic_state != IEEE80211_S_RUN ||
9413147Sxc151355 		    IEEE80211_IS_MULTICAST(wh->i_addr2)) {
9423147Sxc151355 			break;
9433147Sxc151355 		}
9443147Sxc151355 
9453147Sxc151355 		/*
9463147Sxc151355 		 * prreq frame format
9473147Sxc151355 		 *	[tlv] ssid
9483147Sxc151355 		 *	[tlv] supported rates
9493147Sxc151355 		 *	[tlv] extended supported rates
9503147Sxc151355 		 */
9513147Sxc151355 		ssid = rates = xrates = NULL;
9523147Sxc151355 		while (frm < efrm) {
9537249Sff224033 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
9547249Sff224033 			    frm[1], goto out);
9553147Sxc151355 			switch (*frm) {
9563147Sxc151355 			case IEEE80211_ELEMID_SSID:
9573147Sxc151355 				ssid = frm;
9583147Sxc151355 				break;
9593147Sxc151355 			case IEEE80211_ELEMID_RATES:
9603147Sxc151355 				rates = frm;
9613147Sxc151355 				break;
9623147Sxc151355 			case IEEE80211_ELEMID_XRATES:
9633147Sxc151355 				xrates = frm;
9643147Sxc151355 				break;
9653147Sxc151355 			}
9663147Sxc151355 			frm += frm[1] + 2;
9673147Sxc151355 		}
9683147Sxc151355 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, break);
969*8594SFei.Feng@Sun.COM 		if (xrates != NULL) {
970*8594SFei.Feng@Sun.COM 			IEEE80211_VERIFY_ELEMENT(xrates,
971*8594SFei.Feng@Sun.COM 			    IEEE80211_RATE_MAXSIZE - rates[1], break);
972*8594SFei.Feng@Sun.COM 		}
9733147Sxc151355 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, break);
9743147Sxc151355 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid, break);
975*8594SFei.Feng@Sun.COM 		if (ic->ic_flags & IEEE80211_F_HIDESSID) {
976*8594SFei.Feng@Sun.COM 			if (ssid == NULL || ssid[1] == 0) {
977*8594SFei.Feng@Sun.COM 				ieee80211_dbg(IEEE80211_MSG_INPUT,
978*8594SFei.Feng@Sun.COM 				    "ieee80211_recv_mgmt: ignore %s, "
979*8594SFei.Feng@Sun.COM 				    "no ssid with ssid suppression enabled",
980*8594SFei.Feng@Sun.COM 				    IEEE80211_SUBTYPE_NAME(subtype));
981*8594SFei.Feng@Sun.COM 				break;
982*8594SFei.Feng@Sun.COM 			}
9833147Sxc151355 		}
9843147Sxc151355 
9853147Sxc151355 		if (in == ic->ic_bss) {
9863147Sxc151355 			if (ic->ic_opmode != IEEE80211_M_IBSS) {
9873147Sxc151355 				in = ieee80211_tmp_node(ic, wh->i_addr2);
9883147Sxc151355 				allocbs = B_TRUE;
9893147Sxc151355 			} else if (!IEEE80211_ADDR_EQ(wh->i_addr2,
9903147Sxc151355 			    in->in_macaddr)) {
9913147Sxc151355 				/*
9923147Sxc151355 				 * Cannot tell if the sender is operating
9933147Sxc151355 				 * in ibss mode.  But we need a new node to
9943147Sxc151355 				 * send the response so blindly add them to the
9953147Sxc151355 				 * neighbor table.
9963147Sxc151355 				 */
9973147Sxc151355 				in = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
9987249Sff224033 				    wh->i_addr2);
9993147Sxc151355 			}
10003147Sxc151355 			if (in == NULL)
10013147Sxc151355 				break;
10023147Sxc151355 		}
10033147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ASSOC, "ieee80211_recv_mgmt: "
10047249Sff224033 		    "[%s] recv probe req\n",
10057249Sff224033 		    ieee80211_macaddr_sprintf(wh->i_addr2));
10063147Sxc151355 		in->in_rssi = (uint8_t)rssi;
10073147Sxc151355 		in->in_rstamp = rstamp;
10083147Sxc151355 		/*
10093147Sxc151355 		 * Adjust and check station's rate list with device's
10103147Sxc151355 		 * supported rate.  Send back response if there is at
10113147Sxc151355 		 * least one rate or the fixed rate(if being set) is
10123147Sxc151355 		 * supported by both station and the device
10133147Sxc151355 		 */
10143147Sxc151355 		rate = ieee80211_setup_rates(in, rates, xrates,
10157249Sff224033 		    IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
10167249Sff224033 		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
10173147Sxc151355 		if (rate & IEEE80211_RATE_BASIC) {
10183147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_XRATE, "ieee80211_recv_mgmt"
10197249Sff224033 			    "%s recv'd rate set invalid",
10207249Sff224033 			    IEEE80211_SUBTYPE_NAME(subtype));
10213147Sxc151355 		} else {
10223147Sxc151355 			IEEE80211_UNLOCK(ic);
10233147Sxc151355 			IEEE80211_SEND_MGMT(ic, in,
10247249Sff224033 			    IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
10253147Sxc151355 			IEEE80211_LOCK(ic);
10263147Sxc151355 		}
10273147Sxc151355 		if (allocbs) {
10283147Sxc151355 			/*
10293147Sxc151355 			 * Temporary node created just to send a
10303147Sxc151355 			 * response, reclaim immediately.
10313147Sxc151355 			 */
10323147Sxc151355 			ieee80211_free_node(in);
10333147Sxc151355 		}
10343147Sxc151355 		break;
10353147Sxc151355 
10363147Sxc151355 	case IEEE80211_FC0_SUBTYPE_AUTH:
10373147Sxc151355 		/*
10383147Sxc151355 		 * auth frame format
10393147Sxc151355 		 *	[2] algorithm
10403147Sxc151355 		 *	[2] sequence
10413147Sxc151355 		 *	[2] status
10423147Sxc151355 		 *	[tlv*] challenge
10433147Sxc151355 		 */
10447249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
10457249Sff224033 		    IEEE80211_AUTH_ELEM_MIN, break);
10463147Sxc151355 		algo   = (*(uint16_t *)frm);
10473147Sxc151355 		seq    = (*(uint16_t *)(frm + 2));
10483147Sxc151355 		status = (*(uint16_t *)(frm + 4));
10493147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_recv_mgmt: "
10507249Sff224033 		    "[%s] recv auth frame with algorithm %d seq %d\n",
10517249Sff224033 		    ieee80211_macaddr_sprintf(wh->i_addr2), algo, seq);
10523147Sxc151355 
10533147Sxc151355 		if (ic->ic_flags & IEEE80211_F_COUNTERM) {
10543147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
10557249Sff224033 			    "ieee80211_recv_mgmt: ignore auth, %s\n",
10567249Sff224033 			    "TKIP countermeasures enabled");
10573147Sxc151355 			break;
10583147Sxc151355 		}
10593147Sxc151355 		switch (algo) {
10603147Sxc151355 		case IEEE80211_AUTH_ALG_SHARED:
10613147Sxc151355 			ieee80211_auth_shared(ic, wh, frm + 6, efrm, in,
10627249Sff224033 			    seq, status);
10633147Sxc151355 			break;
10643147Sxc151355 		case IEEE80211_AUTH_ALG_OPEN:
10653147Sxc151355 			ieee80211_auth_open(ic, wh, in, seq, status);
10663147Sxc151355 			break;
10673147Sxc151355 		default:
10683147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_recv_mgmt: "
10697249Sff224033 			    "ignore auth, unsupported alg %d", algo);
10703147Sxc151355 			break;
10713147Sxc151355 		}
10723147Sxc151355 		break;
10733147Sxc151355 
10743147Sxc151355 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
10753147Sxc151355 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
10763147Sxc151355 		if (ic->ic_opmode != IEEE80211_M_STA ||
10773147Sxc151355 		    ic->ic_state != IEEE80211_S_ASSOC)
10783147Sxc151355 			break;
10793147Sxc151355 
10803147Sxc151355 		/*
10813147Sxc151355 		 * asresp frame format
10823147Sxc151355 		 *	[2] capability information
10833147Sxc151355 		 *	[2] status
10843147Sxc151355 		 *	[2] association ID
10853147Sxc151355 		 *	[tlv] supported rates
10863147Sxc151355 		 *	[tlv] extended supported rates
10873147Sxc151355 		 *	[tlv] WME
10883147Sxc151355 		 */
10897249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
10907249Sff224033 		    IEEE80211_ASSOC_RESP_ELEM_MIN, break);
10913147Sxc151355 		in = ic->ic_bss;
10923147Sxc151355 		capinfo = (*(uint16_t *)frm);
10933147Sxc151355 		frm += 2;
10943147Sxc151355 		status = (*(uint16_t *)frm);
10953147Sxc151355 		frm += 2;
10963147Sxc151355 		if (status != 0) {
10973147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
10987249Sff224033 			    "assoc failed (reason %d)\n", status);
10993147Sxc151355 			in = ieee80211_find_node(&ic->ic_scan, wh->i_addr2);
11003147Sxc151355 			if (in != NULL) {
11013147Sxc151355 				in->in_fails++;
11023147Sxc151355 				ieee80211_free_node(in);
11033147Sxc151355 			}
11043147Sxc151355 			break;
11053147Sxc151355 		}
11063147Sxc151355 		associd = (*(uint16_t *)frm);
11073147Sxc151355 		frm += 2;
11083147Sxc151355 
11093147Sxc151355 		rates = xrates = NULL;
11103147Sxc151355 		while (frm < efrm) {
11113147Sxc151355 			/*
11123147Sxc151355 			 * Do not discard frames containing proprietary Agere
11133147Sxc151355 			 * elements 128 and 129, as the reported element length
11143147Sxc151355 			 * is often wrong. Skip rest of the frame, since we can
11153147Sxc151355 			 * not rely on the given element length making it
11163147Sxc151355 			 * impossible to know where the next element starts
11173147Sxc151355 			 */
11183147Sxc151355 			if ((*frm == IEEE80211_ELEMID_AGERE1) ||
11193147Sxc151355 			    (*frm == IEEE80211_ELEMID_AGERE2)) {
11203147Sxc151355 				frm = efrm;
11213147Sxc151355 				break;
11223147Sxc151355 			}
11233147Sxc151355 
11247249Sff224033 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
11257249Sff224033 			    frm[1], goto out);
11263147Sxc151355 			switch (*frm) {
11273147Sxc151355 			case IEEE80211_ELEMID_RATES:
11283147Sxc151355 				rates = frm;
11293147Sxc151355 				break;
11303147Sxc151355 			case IEEE80211_ELEMID_XRATES:
11313147Sxc151355 				xrates = frm;
11323147Sxc151355 				break;
11333147Sxc151355 			}
11343147Sxc151355 			frm += frm[1] + 2;
11353147Sxc151355 		}
11363147Sxc151355 
11373147Sxc151355 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, break);
11383147Sxc151355 		/*
11393147Sxc151355 		 * Adjust and check AP's rate list with device's
11403147Sxc151355 		 * supported rate. Re-start scan if no rate is or the
11413147Sxc151355 		 * fixed rate(if being set) cannot be supported by
11423147Sxc151355 		 * either AP or the device.
11433147Sxc151355 		 */
11443147Sxc151355 		rate = ieee80211_setup_rates(in, rates, xrates,
11457249Sff224033 		    IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
11467249Sff224033 		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
11473147Sxc151355 		if (rate & IEEE80211_RATE_BASIC) {
11483147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
11497249Sff224033 			    "assoc failed (rate set mismatch)\n");
11503147Sxc151355 			if (in != ic->ic_bss)
11513147Sxc151355 				in->in_fails++;
11523147Sxc151355 			IEEE80211_UNLOCK(ic);
11533147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
11543147Sxc151355 			return;
11553147Sxc151355 		}
11563147Sxc151355 
11573147Sxc151355 		in->in_capinfo = capinfo;
11583147Sxc151355 		in->in_associd = associd;
11593147Sxc151355 		in->in_flags &= ~IEEE80211_NODE_QOS;
11603147Sxc151355 		/*
11613147Sxc151355 		 * Configure state now that we are associated.
11623147Sxc151355 		 */
11633147Sxc151355 		if (ic->ic_curmode == IEEE80211_MODE_11A ||
11643147Sxc151355 		    (in->in_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
11653147Sxc151355 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
11663147Sxc151355 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
11673147Sxc151355 		} else {
11683147Sxc151355 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
11693147Sxc151355 			ic->ic_flags |= IEEE80211_F_USEBARKER;
11703147Sxc151355 		}
11713147Sxc151355 		ieee80211_set_shortslottime(ic,
11727249Sff224033 		    ic->ic_curmode == IEEE80211_MODE_11A ||
11737249Sff224033 		    (in->in_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
11743147Sxc151355 		/*
11753147Sxc151355 		 * Honor ERP protection.
11763147Sxc151355 		 *
11773147Sxc151355 		 * NB:	in_erp should zero for non-11g operation.
11783147Sxc151355 		 *	check ic_curmode anyway
11793147Sxc151355 		 */
11803147Sxc151355 		if (ic->ic_curmode == IEEE80211_MODE_11G &&
11813147Sxc151355 		    (in->in_erp & IEEE80211_ERP_USE_PROTECTION))
11823147Sxc151355 			ic->ic_flags |= IEEE80211_F_USEPROT;
11833147Sxc151355 		else
11843147Sxc151355 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
11853147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ASSOC,
11867249Sff224033 		    "assoc success: %s preamble, %s slot time%s%s\n",
11877249Sff224033 		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
11887249Sff224033 		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
11897249Sff224033 		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
11907249Sff224033 		    in->in_flags & IEEE80211_NODE_QOS ? ", QoS" : "");
11913147Sxc151355 		IEEE80211_UNLOCK(ic);
11923147Sxc151355 		ieee80211_new_state(ic, IEEE80211_S_RUN, subtype);
11933147Sxc151355 		return;
11943147Sxc151355 
11953147Sxc151355 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
11963147Sxc151355 		if (ic->ic_state == IEEE80211_S_SCAN)
11973147Sxc151355 			break;
11983147Sxc151355 
11993147Sxc151355 		/*
12003147Sxc151355 		 * deauth frame format
12013147Sxc151355 		 *	[2] reason
12023147Sxc151355 		 */
12037249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm), 2, break);
12043147Sxc151355 		status = (*(uint16_t *)frm);
12053147Sxc151355 
12063147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH,
12077249Sff224033 		    "recv deauthenticate (reason %d)\n", status);
12083147Sxc151355 		switch (ic->ic_opmode) {
12093147Sxc151355 		case IEEE80211_M_STA:
12103147Sxc151355 			IEEE80211_UNLOCK(ic);
12113147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
12127249Sff224033 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
12133147Sxc151355 			return;
12143147Sxc151355 		default:
12153147Sxc151355 			break;
12163147Sxc151355 		}
12173147Sxc151355 		break;
12183147Sxc151355 
12193147Sxc151355 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
12203147Sxc151355 		if (ic->ic_state != IEEE80211_S_RUN &&
12213147Sxc151355 		    ic->ic_state != IEEE80211_S_ASSOC &&
12223147Sxc151355 		    ic->ic_state != IEEE80211_S_AUTH)
12233147Sxc151355 			break;
12243147Sxc151355 		/*
12253147Sxc151355 		 * disassoc frame format
12263147Sxc151355 		 *	[2] reason
12273147Sxc151355 		 */
12287249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm), 2, break);
12293147Sxc151355 		status = (*(uint16_t *)frm);
12303147Sxc151355 
12313147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ASSOC,
12327249Sff224033 		    "recv disassociate (reason %d)\n", status);
12333147Sxc151355 		switch (ic->ic_opmode) {
12343147Sxc151355 		case IEEE80211_M_STA:
12353147Sxc151355 			IEEE80211_UNLOCK(ic);
12363147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
12377249Sff224033 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
12383147Sxc151355 			return;
12393147Sxc151355 		default:
12403147Sxc151355 			break;
12413147Sxc151355 		}
12423147Sxc151355 		break;
12433147Sxc151355 
12443147Sxc151355 	default:
12453147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_recv_mgmt: "
12467249Sff224033 		    "subtype 0x%x not handled\n", subtype);
12473147Sxc151355 		break;
12483147Sxc151355 	} /* switch subtype */
12493147Sxc151355 out:
12503147Sxc151355 	IEEE80211_UNLOCK(ic);
12513147Sxc151355 }
1252