13147Sxc151355 /*
2*4126Szf162725  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
33147Sxc151355  * Use is subject to license terms.
43147Sxc151355  */
53147Sxc151355 
63147Sxc151355 /*
73147Sxc151355  * Copyright (c) 2001 Atsushi Onoe
83147Sxc151355  * Copyright (c) 2002-2005 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
393147Sxc151355 
403147Sxc151355 /*
413147Sxc151355  * Process received frame
423147Sxc151355  */
433147Sxc151355 
443147Sxc151355 #include <sys/byteorder.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);
763147Sxc151355 	type = (uint8_t)-1;		/* undefined */
773147Sxc151355 	len = mp->b_wptr - mp->b_rptr;
783147Sxc151355 	if (len < sizeof (struct ieee80211_frame_min)) {
793147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
803147Sxc151355 			"too short (1): len %u", len);
813147Sxc151355 		goto out;
823147Sxc151355 	}
833147Sxc151355 	/*
843147Sxc151355 	 * Bit of a cheat here, we use a pointer for a 3-address
853147Sxc151355 	 * frame format but don't reference fields past outside
863147Sxc151355 	 * ieee80211_frame_min w/o first validating the data is
873147Sxc151355 	 * present.
883147Sxc151355 	 */
893147Sxc151355 	wh = (struct ieee80211_frame *)mp->b_rptr;
903147Sxc151355 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
913147Sxc151355 	    IEEE80211_FC0_VERSION_0) {
923147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
933147Sxc151355 			"discard pkt with wrong version %x", wh->i_fc[0]);
943147Sxc151355 		goto out;
953147Sxc151355 	}
963147Sxc151355 
973147Sxc151355 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
983147Sxc151355 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
993147Sxc151355 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1003147Sxc151355 
1013147Sxc151355 	IEEE80211_LOCK(ic);
1023147Sxc151355 	if (!(ic->ic_flags & IEEE80211_F_SCAN)) {
1033147Sxc151355 		switch (ic->ic_opmode) {
1043147Sxc151355 		case IEEE80211_M_STA:
1053147Sxc151355 			bssid = wh->i_addr2;
1063147Sxc151355 			if (!IEEE80211_ADDR_EQ(bssid, in->in_bssid))
1073147Sxc151355 				goto out_exit_mutex;
1083147Sxc151355 			break;
1093147Sxc151355 		case IEEE80211_M_IBSS:
1103147Sxc151355 		case IEEE80211_M_AHDEMO:
1113147Sxc151355 			if (dir != IEEE80211_FC1_DIR_NODS) {
1123147Sxc151355 				bssid = wh->i_addr1;
1133147Sxc151355 			} else if (type == IEEE80211_FC0_TYPE_CTL) {
1143147Sxc151355 				bssid = wh->i_addr1;
1153147Sxc151355 			} else {
1163147Sxc151355 				if (len < sizeof (struct ieee80211_frame)) {
1173147Sxc151355 					ieee80211_dbg(IEEE80211_MSG_ANY,
1183147Sxc151355 						"ieee80211_input: too short(2):"
1193147Sxc151355 						"len %u\n", len);
1203147Sxc151355 					goto out_exit_mutex;
1213147Sxc151355 				}
1223147Sxc151355 				bssid = wh->i_addr3;
1233147Sxc151355 			}
1243147Sxc151355 			if (type != IEEE80211_FC0_TYPE_DATA)
1253147Sxc151355 				break;
1263147Sxc151355 			/*
1273147Sxc151355 			 * Data frame, validate the bssid.
1283147Sxc151355 			 */
1293147Sxc151355 			if (!IEEE80211_ADDR_EQ(bssid, ic->ic_bss->in_bssid) &&
1303147Sxc151355 			    !IEEE80211_ADDR_EQ(bssid, wifi_bcastaddr)) {
1313147Sxc151355 				/* not interested in */
1323147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
1333147Sxc151355 					"ieee80211_input: not to bss %s\n",
1343147Sxc151355 					ieee80211_macaddr_sprintf(bssid));
1353147Sxc151355 				goto out_exit_mutex;
1363147Sxc151355 			}
1373147Sxc151355 			/*
1383147Sxc151355 			 * For adhoc mode we cons up a node when it doesn't
1393147Sxc151355 			 * exist. This should probably done after an ACL check.
1403147Sxc151355 			 */
1413147Sxc151355 			if (in == ic->ic_bss &&
1423147Sxc151355 			    ic->ic_opmode != IEEE80211_M_HOSTAP &&
1433147Sxc151355 			    !IEEE80211_ADDR_EQ(wh->i_addr2, in->in_macaddr)) {
1443147Sxc151355 				/*
1453147Sxc151355 				 * Fake up a node for this newly
1463147Sxc151355 				 * discovered member of the IBSS.
1473147Sxc151355 				 */
1483147Sxc151355 				in = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
1493147Sxc151355 					wh->i_addr2);
1503147Sxc151355 				if (in == NULL) {
1513147Sxc151355 					/* NB: stat kept for alloc failure */
1523147Sxc151355 					goto out_exit_mutex;
1533147Sxc151355 				}
1543147Sxc151355 			}
1553147Sxc151355 			break;
1563147Sxc151355 		default:
1573147Sxc151355 			goto out_exit_mutex;
1583147Sxc151355 		}
1593147Sxc151355 		in->in_rssi = (uint8_t)rssi;
1603147Sxc151355 		in->in_rstamp = rstamp;
1613147Sxc151355 		if (!(type & IEEE80211_FC0_TYPE_CTL)) {
1623147Sxc151355 			tid = 0;
1633147Sxc151355 			rxseq = (*(uint16_t *)wh->i_seq);
1643147Sxc151355 			if ((wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
1653147Sxc151355 			    (rxseq - in->in_rxseqs[tid]) <= 0) {
1663147Sxc151355 				/* duplicate, discard */
1673147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
1683147Sxc151355 					"ieee80211_input: duplicate",
1693147Sxc151355 					"seqno <%u,%u> fragno <%u,%u> tid %u",
1703147Sxc151355 					rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
1713147Sxc151355 					in->in_rxseqs[tid] >>
1723147Sxc151355 						IEEE80211_SEQ_SEQ_SHIFT,
1733147Sxc151355 					rxseq & IEEE80211_SEQ_FRAG_MASK,
1743147Sxc151355 					in->in_rxseqs[tid] &
1753147Sxc151355 						IEEE80211_SEQ_FRAG_MASK,
1763147Sxc151355 					tid);
1773147Sxc151355 				ic->ic_stats.is_rx_dups++;
1783147Sxc151355 				goto out_exit_mutex;
1793147Sxc151355 			}
1803147Sxc151355 			in->in_rxseqs[tid] = rxseq;
1813147Sxc151355 		}
1823147Sxc151355 		in->in_inact = 0;
1833147Sxc151355 	}
1843147Sxc151355 
1853147Sxc151355 	hdrspace = ieee80211_hdrspace(wh);
1863147Sxc151355 	switch (type) {
1873147Sxc151355 	case IEEE80211_FC0_TYPE_DATA:
1883147Sxc151355 		if (len < hdrspace) {
1893147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
1903147Sxc151355 				"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,
1973147Sxc151355 					"ieee80211_input: data ",
1983147Sxc151355 					"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,
2103147Sxc151355 					"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,
2183147Sxc151355 					"ieee80211_input: unknown dir 0x%x",
2193147Sxc151355 					dir);
2203147Sxc151355 				goto out_exit_mutex;
2213147Sxc151355 			}
2223147Sxc151355 			break;
2233147Sxc151355 		default:
2243147Sxc151355 			ieee80211_err("ieee80211_input: "
2253147Sxc151355 				"receive data, unknown opmode %u, skip\n",
2263147Sxc151355 				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,
2393147Sxc151355 					"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: "
2723147Sxc151355 				"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,
2943147Sxc151355 					"ieee80211_input: "
2953147Sxc151355 					"%s WEP set but not permitted",
2963147Sxc151355 					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,
3053147Sxc151355 					"ieee80211_input: "
3063147Sxc151355 					"mgt WEP set but PRIVACY off");
3073147Sxc151355 				ic->ic_stats.is_wep_errors++;
3083147Sxc151355 				goto out_exit_mutex;
3093147Sxc151355 			}
3103147Sxc151355 			key = ieee80211_crypto_decap(ic, mp, hdrspace);
3113147Sxc151355 			if (key == NULL) {
3123147Sxc151355 				/* NB: stats+msgs handled in crypto_decap */
3133147Sxc151355 				goto out_exit_mutex;
3143147Sxc151355 			}
3153147Sxc151355 			wh = (struct ieee80211_frame *)mp->b_rptr;
3163147Sxc151355 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3173147Sxc151355 		}
3183147Sxc151355 		IEEE80211_UNLOCK(ic);
3193147Sxc151355 		ic->ic_recv_mgmt(ic, mp, in, subtype, rssi, rstamp);
3203147Sxc151355 		goto out;
3213147Sxc151355 
3223147Sxc151355 	case IEEE80211_FC0_TYPE_CTL:
3233147Sxc151355 	default:
3243147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
3253147Sxc151355 			"bad frame type 0x%x", type);
3263147Sxc151355 		/* should not come here */
3273147Sxc151355 		break;
3283147Sxc151355 	}
3293147Sxc151355 out_exit_mutex:
3303147Sxc151355 	IEEE80211_UNLOCK(ic);
3313147Sxc151355 out:
3323147Sxc151355 	if (mp != NULL)
3333147Sxc151355 		freemsg(mp);
3343147Sxc151355 
3353147Sxc151355 	return (type);
3363147Sxc151355 }
3373147Sxc151355 
3383147Sxc151355 /*
3393147Sxc151355  * This function reassemble fragments.
3403147Sxc151355  * More fragments bit in the frame control means the packet is fragmented.
3413147Sxc151355  * While the sequence control field consists of 4-bit fragment number
3423147Sxc151355  * field and a 12-bit sequence number field.
3433147Sxc151355  */
3443147Sxc151355 /* ARGSUSED */
3453147Sxc151355 static mblk_t *
3463147Sxc151355 ieee80211_defrag(ieee80211com_t *ic, struct ieee80211_node *in, mblk_t *mp,
3473147Sxc151355     int hdrspace)
3483147Sxc151355 {
3493147Sxc151355 	struct ieee80211_frame *wh = (struct ieee80211_frame *)mp->b_rptr;
3503147Sxc151355 	struct ieee80211_frame *lwh;
3513147Sxc151355 	mblk_t *mfrag;
3523147Sxc151355 	uint16_t rxseq;
3533147Sxc151355 	uint8_t fragno;
3543147Sxc151355 	uint8_t more_frag;
3553147Sxc151355 
3563147Sxc151355 	ASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1));
3573147Sxc151355 	more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG;
3583147Sxc151355 	rxseq = LE_16(*(uint16_t *)wh->i_seq);
3593147Sxc151355 	fragno = rxseq & IEEE80211_SEQ_FRAG_MASK;
3603147Sxc151355 
3613147Sxc151355 	/* Quick way out, if there's nothing to defragment */
3623147Sxc151355 	if (!more_frag && fragno == 0 && in->in_rxfrag == NULL)
3633147Sxc151355 		return (mp);
3643147Sxc151355 
3653147Sxc151355 	/*
3663147Sxc151355 	 * Remove frag to insure it doesn't get reaped by timer.
3673147Sxc151355 	 */
3683147Sxc151355 	if (in->in_table == NULL) {
3693147Sxc151355 		/*
3703147Sxc151355 		 * Should never happen.  If the node is orphaned (not in
3713147Sxc151355 		 * the table) then input packets should not reach here.
3723147Sxc151355 		 * Otherwise, a concurrent request that yanks the table
3733147Sxc151355 		 * should be blocked by other interlocking and/or by first
3743147Sxc151355 		 * shutting the driver down.  Regardless, be defensive
3753147Sxc151355 		 * here and just bail
3763147Sxc151355 		 */
3773147Sxc151355 		freemsg(mp);
3783147Sxc151355 		return (NULL);
3793147Sxc151355 	}
3803147Sxc151355 	IEEE80211_NODE_LOCK(in->in_table);
3813147Sxc151355 	mfrag = in->in_rxfrag;
3823147Sxc151355 	in->in_rxfrag = NULL;
3833147Sxc151355 	IEEE80211_NODE_UNLOCK(in->in_table);
3843147Sxc151355 
3853147Sxc151355 	/*
3863147Sxc151355 	 * Validate new fragment is in order and
3873147Sxc151355 	 * related to the previous ones.
3883147Sxc151355 	 */
3893147Sxc151355 	if (mfrag != NULL) {
3903147Sxc151355 		uint16_t last_rxseq;
3913147Sxc151355 
3923147Sxc151355 		lwh = (struct ieee80211_frame *)mfrag->b_rptr;
3933147Sxc151355 		last_rxseq = LE_16(*(uint16_t *)lwh->i_seq);
3943147Sxc151355 		/*
3953147Sxc151355 		 * Sequence control field contains 12-bit sequence no
3963147Sxc151355 		 * and 4-bit fragment number. For fragemnts, the
3973147Sxc151355 		 * sequence no is not changed.
3983147Sxc151355 		 * NB: check seq # and frag together
3993147Sxc151355 		 */
4003147Sxc151355 		if (rxseq != last_rxseq + 1 ||
4013147Sxc151355 		    !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) ||
4023147Sxc151355 		    !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) {
4033147Sxc151355 			/*
4043147Sxc151355 			 * Unrelated fragment or no space for it,
4053147Sxc151355 			 * clear current fragments.
4063147Sxc151355 			 */
4073147Sxc151355 			freemsg(mfrag);
4083147Sxc151355 			mfrag = NULL;
4093147Sxc151355 		}
4103147Sxc151355 	}
4113147Sxc151355 
4123147Sxc151355 	if (mfrag == NULL) {
4133147Sxc151355 		if (fragno != 0) {	/* !first fragment, discard */
4143147Sxc151355 			freemsg(mp);
4153147Sxc151355 			return (NULL);
4163147Sxc151355 		}
4173147Sxc151355 		mfrag = mp;
4183147Sxc151355 	} else {			/* concatenate */
4193147Sxc151355 		(void) adjmsg(mp, hdrspace);
4203147Sxc151355 		linkb(mfrag, mp);
4213147Sxc151355 		/* track last seqnum and fragno */
4223147Sxc151355 		lwh = (struct ieee80211_frame *)mfrag->b_rptr;
4233147Sxc151355 		*(uint16_t *)lwh->i_seq = *(uint16_t *)wh->i_seq;
4243147Sxc151355 	}
4253147Sxc151355 	if (more_frag != 0) {		/* more to come, save */
4263147Sxc151355 		in->in_rxfragstamp = ddi_get_lbolt();
4273147Sxc151355 		in->in_rxfrag = mfrag;
4283147Sxc151355 		mfrag = NULL;
4293147Sxc151355 	}
4303147Sxc151355 
4313147Sxc151355 	return (mfrag);
4323147Sxc151355 }
4333147Sxc151355 
4343147Sxc151355 /*
4353147Sxc151355  * Install received rate set information in the node's state block.
4363147Sxc151355  */
4373147Sxc151355 int
4383147Sxc151355 ieee80211_setup_rates(struct ieee80211_node *in, const uint8_t *rates,
4393147Sxc151355     const uint8_t *xrates, int flags)
4403147Sxc151355 {
4413147Sxc151355 	struct ieee80211_rateset *rs = &in->in_rates;
4423147Sxc151355 
4433147Sxc151355 	bzero(rs, sizeof (*rs));
4443147Sxc151355 	rs->ir_nrates = rates[1];
4453147Sxc151355 	/* skip 1 byte element ID and 1 byte length */
4463147Sxc151355 	bcopy(rates + 2, rs->ir_rates, rs->ir_nrates);
4473147Sxc151355 	if (xrates != NULL) {
4483147Sxc151355 		uint8_t nxrates;
4493147Sxc151355 
4503147Sxc151355 		/*
4513147Sxc151355 		 * Tack on 11g extended supported rate element.
4523147Sxc151355 		 */
4533147Sxc151355 		nxrates = xrates[1];
4543147Sxc151355 		if (rs->ir_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
4553147Sxc151355 			nxrates = IEEE80211_RATE_MAXSIZE - rs->ir_nrates;
4563147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_XRATE,
4573147Sxc151355 				"ieee80211_setup_rates: %s",
4583147Sxc151355 				"[%s] extended rate set too large;"
4593147Sxc151355 				" only using %u of %u rates\n",
4603147Sxc151355 				ieee80211_macaddr_sprintf(in->in_macaddr),
4613147Sxc151355 				nxrates, xrates[1]);
4623147Sxc151355 		}
4633147Sxc151355 		bcopy(xrates + 2, rs->ir_rates + rs->ir_nrates, nxrates);
4643147Sxc151355 		rs->ir_nrates += nxrates;
4653147Sxc151355 	}
4663147Sxc151355 	return (ieee80211_fix_rate(in, flags));
4673147Sxc151355 }
4683147Sxc151355 
4693147Sxc151355 /*
4703147Sxc151355  * Process open-system authentication response frame and start
4713147Sxc151355  * association if the authentication request is accepted.
4723147Sxc151355  */
4733147Sxc151355 static void
4743147Sxc151355 ieee80211_auth_open(ieee80211com_t *ic, struct ieee80211_frame *wh,
4753147Sxc151355     struct ieee80211_node *in, uint16_t seq, uint16_t status)
4763147Sxc151355 {
4773147Sxc151355 	IEEE80211_LOCK_ASSERT(ic);
4783147Sxc151355 	if (in->in_authmode == IEEE80211_AUTH_SHARED) {
4793147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH,
4803147Sxc151355 			"open auth: bad sta auth mode %u", in->in_authmode);
4813147Sxc151355 		return;
4823147Sxc151355 	}
4833147Sxc151355 	if (ic->ic_opmode == IEEE80211_M_STA) {
4843147Sxc151355 		if (ic->ic_state != IEEE80211_S_AUTH ||
4853147Sxc151355 		    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
4863147Sxc151355 			return;
4873147Sxc151355 		}
4883147Sxc151355 		IEEE80211_UNLOCK(ic);
4893147Sxc151355 		if (status != 0) {
4903147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
4913147Sxc151355 				"open auth failed (reason %d)\n", status);
4923147Sxc151355 			if (in != ic->ic_bss)
4933147Sxc151355 				in->in_fails++;
4943147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
4953147Sxc151355 		} else {
4963147Sxc151355 			/* i_fc[0] - frame control's type & subtype field */
4973147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
4983147Sxc151355 				wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
4993147Sxc151355 		}
5003147Sxc151355 		IEEE80211_LOCK(ic);
5013147Sxc151355 	} else {
5023147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_auth_open: "
5033147Sxc151355 			"bad operating mode %u", ic->ic_opmode);
5043147Sxc151355 	}
5053147Sxc151355 }
5063147Sxc151355 
5073147Sxc151355 /*
5083147Sxc151355  * Allocate challenge text for use by shared-key authentication
5093147Sxc151355  * Return B_TRUE on success, B_FALST otherwise.
5103147Sxc151355  */
5113147Sxc151355 static boolean_t
5123147Sxc151355 ieee80211_alloc_challenge(struct ieee80211_node *in)
5133147Sxc151355 {
5143147Sxc151355 	if (in->in_challenge == NULL) {
5153147Sxc151355 		in->in_challenge = kmem_alloc(IEEE80211_CHALLENGE_LEN,
5163147Sxc151355 			KM_NOSLEEP);
5173147Sxc151355 	}
5183147Sxc151355 	if (in->in_challenge == NULL) {
5193147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
5203147Sxc151355 			"[%s] shared key challenge alloc failed\n",
5213147Sxc151355 			ieee80211_macaddr_sprintf(in->in_macaddr));
5223147Sxc151355 	}
5233147Sxc151355 	return (in->in_challenge != NULL);
5243147Sxc151355 }
5253147Sxc151355 
5263147Sxc151355 /*
5273147Sxc151355  * Process shared-key authentication response frames. If authentication
5283147Sxc151355  * succeeds, start association; otherwise, restart scan.
5293147Sxc151355  */
5303147Sxc151355 static void
5313147Sxc151355 ieee80211_auth_shared(ieee80211com_t *ic, struct ieee80211_frame *wh,
5323147Sxc151355     uint8_t *frm, uint8_t *efrm, struct ieee80211_node *in, uint16_t seq,
5333147Sxc151355     uint16_t status)
5343147Sxc151355 {
5353147Sxc151355 	uint8_t *challenge;
5363147Sxc151355 
5373147Sxc151355 	/*
5383147Sxc151355 	 * Pre-shared key authentication is evil; accept
5393147Sxc151355 	 * it only if explicitly configured (it is supported
5403147Sxc151355 	 * mainly for compatibility with clients like OS X).
5413147Sxc151355 	 */
5423147Sxc151355 	IEEE80211_LOCK_ASSERT(ic);
5433147Sxc151355 	if (in->in_authmode != IEEE80211_AUTH_AUTO &&
5443147Sxc151355 	    in->in_authmode != IEEE80211_AUTH_SHARED) {
5453147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_auth_shared: "
5463147Sxc151355 			"bad sta auth mode %u", in->in_authmode);
5473147Sxc151355 		goto bad;
5483147Sxc151355 	}
5493147Sxc151355 
5503147Sxc151355 	challenge = NULL;
5513147Sxc151355 	if (frm + 1 < efrm) {
5523147Sxc151355 		/*
5533147Sxc151355 		 * Challenge text information element
5543147Sxc151355 		 * frm[0] - element ID
5553147Sxc151355 		 * frm[1] - length
5563147Sxc151355 		 * frm[2]... - challenge text
5573147Sxc151355 		 */
5583147Sxc151355 		if ((frm[1] + 2) > (efrm - frm)) {
5593147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH,
5603147Sxc151355 				"ieee80211_auth_shared: ie %d%d too long\n",
5613147Sxc151355 				frm[0], (frm[1] + 2) - (efrm - frm));
5623147Sxc151355 			goto bad;
5633147Sxc151355 		}
5643147Sxc151355 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
5653147Sxc151355 			challenge = frm;
5663147Sxc151355 		frm += frm[1] + 2;
5673147Sxc151355 	}
5683147Sxc151355 	switch (seq) {
5693147Sxc151355 	case IEEE80211_AUTH_SHARED_CHALLENGE:
5703147Sxc151355 	case IEEE80211_AUTH_SHARED_RESPONSE:
5713147Sxc151355 		if (challenge == NULL) {
5723147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH,
5733147Sxc151355 				"ieee80211_auth_shared: no challenge\n");
5743147Sxc151355 			goto bad;
5753147Sxc151355 		}
5763147Sxc151355 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
5773147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH,
5783147Sxc151355 				"ieee80211_auth_shared: bad challenge len %d\n",
5793147Sxc151355 				challenge[1]);
5803147Sxc151355 			goto bad;
5813147Sxc151355 		}
5823147Sxc151355 	default:
5833147Sxc151355 		break;
5843147Sxc151355 	}
5853147Sxc151355 	switch (ic->ic_opmode) {
5863147Sxc151355 	case IEEE80211_M_STA:
5873147Sxc151355 		if (ic->ic_state != IEEE80211_S_AUTH)
5883147Sxc151355 			return;
5893147Sxc151355 		switch (seq) {
5903147Sxc151355 		case IEEE80211_AUTH_SHARED_PASS:
5913147Sxc151355 			if (in->in_challenge != NULL) {
5923147Sxc151355 				kmem_free(in->in_challenge,
5933147Sxc151355 				    IEEE80211_CHALLENGE_LEN);
5943147Sxc151355 				in->in_challenge = NULL;
5953147Sxc151355 			}
5963147Sxc151355 			if (status != 0) {
5973147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_DEBUG |
5983147Sxc151355 					IEEE80211_MSG_AUTH,
5993147Sxc151355 					"shared key auth failed (reason %d)\n",
6003147Sxc151355 					status);
6013147Sxc151355 				if (in != ic->ic_bss)
6023147Sxc151355 					in->in_fails++;
6033147Sxc151355 				return;
6043147Sxc151355 			}
6053147Sxc151355 			IEEE80211_UNLOCK(ic);
6063147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
6073147Sxc151355 				wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
6083147Sxc151355 			IEEE80211_LOCK(ic);
6093147Sxc151355 			break;
6103147Sxc151355 		case IEEE80211_AUTH_SHARED_CHALLENGE:
6113147Sxc151355 			if (!ieee80211_alloc_challenge(in))
6123147Sxc151355 				return;
6133147Sxc151355 			bcopy(&challenge[2], in->in_challenge, challenge[1]);
6143147Sxc151355 			IEEE80211_UNLOCK(ic);
6153147Sxc151355 			IEEE80211_SEND_MGMT(ic, in, IEEE80211_FC0_SUBTYPE_AUTH,
6163147Sxc151355 				seq + 1);
6173147Sxc151355 			IEEE80211_LOCK(ic);
6183147Sxc151355 			break;
6193147Sxc151355 		default:
6203147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH, "80211_auth_shared: "
6213147Sxc151355 				"shared key auth: bad seq %d", seq);
6223147Sxc151355 			return;
6233147Sxc151355 		}
6243147Sxc151355 		break;
6253147Sxc151355 
6263147Sxc151355 	default:
6273147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH,
6283147Sxc151355 			"ieee80211_auth_shared: bad opmode %u\n",
6293147Sxc151355 			ic->ic_opmode);
6303147Sxc151355 		break;
6313147Sxc151355 	}
6323147Sxc151355 	return;
6333147Sxc151355 bad:
6343147Sxc151355 	if (ic->ic_opmode == IEEE80211_M_STA) {
6353147Sxc151355 		/*
6363147Sxc151355 		 * Kick the state machine.  This short-circuits
6373147Sxc151355 		 * using the mgt frame timeout to trigger the
6383147Sxc151355 		 * state transition.
6393147Sxc151355 		 */
6403147Sxc151355 		if (ic->ic_state == IEEE80211_S_AUTH) {
6413147Sxc151355 			IEEE80211_UNLOCK(ic);
6423147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
6433147Sxc151355 			IEEE80211_LOCK(ic);
6443147Sxc151355 		}
6453147Sxc151355 	}
6463147Sxc151355 }
6473147Sxc151355 
648*4126Szf162725 static int
649*4126Szf162725 iswpaoui(const uint8_t *frm)
650*4126Szf162725 {
651*4126Szf162725 	uint32_t c = *(uint32_t *)(frm + 2);
652*4126Szf162725 	return (frm[1] > 3 && c == ((WPA_OUI_TYPE << 24) | WPA_OUI));
653*4126Szf162725 }
654*4126Szf162725 
6553147Sxc151355 /*
6563147Sxc151355  * Process a beacon/probe response frame.
6573147Sxc151355  * When the device is in station mode, create a node and add it
6583147Sxc151355  * to the node database for a new ESS or update node info if it's
6593147Sxc151355  * already there.
6603147Sxc151355  */
6613147Sxc151355 static void
6623147Sxc151355 ieee80211_recv_beacon(ieee80211com_t *ic, mblk_t *mp, struct ieee80211_node *in,
6633147Sxc151355     int subtype, int rssi, uint32_t rstamp)
6643147Sxc151355 {
6653147Sxc151355 	ieee80211_impl_t *im = ic->ic_private;
6663147Sxc151355 	struct ieee80211_frame *wh;
6673147Sxc151355 	uint8_t *frm;
6683147Sxc151355 	uint8_t *efrm;	/* end of frame body */
6693147Sxc151355 	struct ieee80211_scanparams scan;
6703147Sxc151355 
6713147Sxc151355 	wh = (struct ieee80211_frame *)mp->b_rptr;
6723147Sxc151355 	frm = (uint8_t *)&wh[1];
6733147Sxc151355 	efrm = (uint8_t *)mp->b_wptr;
6743147Sxc151355 
6753147Sxc151355 	/*
6763147Sxc151355 	 * We process beacon/probe response frames:
6773147Sxc151355 	 *    o when scanning, or
6783147Sxc151355 	 *    o station mode when associated (to collect state
6793147Sxc151355 	 *	updates such as 802.11g slot time), or
6803147Sxc151355 	 *    o adhoc mode (to discover neighbors)
6813147Sxc151355 	 * Frames otherwise received are discarded.
6823147Sxc151355 	 */
6833147Sxc151355 	if (!((ic->ic_flags & IEEE80211_F_SCAN) ||
6843147Sxc151355 	    (ic->ic_opmode == IEEE80211_M_STA && in->in_associd != 0) ||
6853147Sxc151355 	    ic->ic_opmode == IEEE80211_M_IBSS)) {
6863147Sxc151355 		return;
6873147Sxc151355 	}
6883147Sxc151355 
6893147Sxc151355 	/*
6903147Sxc151355 	 * beacon/probe response frame format
6913147Sxc151355 	 *	[8] time stamp
6923147Sxc151355 	 *	[2] beacon interval
6933147Sxc151355 	 *	[2] capability information
6943147Sxc151355 	 *	[tlv] ssid
6953147Sxc151355 	 *	[tlv] supported rates
6963147Sxc151355 	 *	[tlv] country information
6973147Sxc151355 	 *	[tlv] parameter set (FH/DS)
6983147Sxc151355 	 *	[tlv] erp information
6993147Sxc151355 	 *	[tlv] extended supported rates
7003147Sxc151355 	 *	[tlv] WME
7013147Sxc151355 	 *	[tlv] WPA or RSN
7023147Sxc151355 	 */
7033147Sxc151355 	IEEE80211_VERIFY_LENGTH(efrm - frm, IEEE80211_BEACON_ELEM_MIN, return);
7043147Sxc151355 	bzero(&scan, sizeof (scan));
7053147Sxc151355 	scan.tstamp  = frm;
7063147Sxc151355 	frm += 8;
7073147Sxc151355 	scan.bintval = (*(uint16_t *)frm);
7083147Sxc151355 	frm += 2;
7093147Sxc151355 	scan.capinfo = (*(uint16_t *)frm);
7103147Sxc151355 	frm += 2;
7113147Sxc151355 	scan.bchan = ieee80211_chan2ieee(ic, ic->ic_curchan);
7123147Sxc151355 	scan.chan = scan.bchan;
7133147Sxc151355 
7143147Sxc151355 	while (frm < efrm) {
7153147Sxc151355 		/* Agere element in beacon */
7163147Sxc151355 		if ((*frm == IEEE80211_ELEMID_AGERE1) ||
7173147Sxc151355 		    (*frm == IEEE80211_ELEMID_AGERE2)) {
7183147Sxc151355 			frm = efrm;
7193147Sxc151355 			break;
7203147Sxc151355 		}
7213147Sxc151355 
7223147Sxc151355 		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1], return);
7233147Sxc151355 		switch (*frm) {
7243147Sxc151355 		case IEEE80211_ELEMID_SSID:
7253147Sxc151355 			scan.ssid = frm;
7263147Sxc151355 			break;
7273147Sxc151355 		case IEEE80211_ELEMID_RATES:
7283147Sxc151355 			scan.rates = frm;
7293147Sxc151355 			break;
7303147Sxc151355 		case IEEE80211_ELEMID_COUNTRY:
7313147Sxc151355 			scan.country = frm;
7323147Sxc151355 			break;
7333147Sxc151355 		case IEEE80211_ELEMID_FHPARMS:
7343147Sxc151355 			if (ic->ic_phytype == IEEE80211_T_FH) {
7353147Sxc151355 				scan.fhdwell = LE_16(*(uint16_t *)(frm + 2));
7363147Sxc151355 				scan.chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
7373147Sxc151355 				scan.fhindex = frm[6];
7383147Sxc151355 				scan.phytype = IEEE80211_T_FH;
7393147Sxc151355 			}
7403147Sxc151355 			break;
7413147Sxc151355 		case IEEE80211_ELEMID_DSPARMS:
7423147Sxc151355 			if (ic->ic_phytype != IEEE80211_T_FH) {
7433147Sxc151355 				scan.chan = frm[2];
7443147Sxc151355 				scan.phytype = IEEE80211_T_DS;
7453147Sxc151355 			}
7463147Sxc151355 			break;
7473147Sxc151355 		case IEEE80211_ELEMID_TIM:
7483147Sxc151355 			scan.tim = frm;
7493147Sxc151355 			scan.timoff = frm - mp->b_rptr;
7503147Sxc151355 			break;
7513147Sxc151355 		case IEEE80211_ELEMID_IBSSPARMS:
7523147Sxc151355 			break;
7533147Sxc151355 		case IEEE80211_ELEMID_XRATES:
7543147Sxc151355 			scan.xrates = frm;
7553147Sxc151355 			break;
7563147Sxc151355 		case IEEE80211_ELEMID_ERP:
7573147Sxc151355 			if (frm[1] != 1) {
7583147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_ELEMID,
7593147Sxc151355 					"ieee80211_recv_mgmt: ignore %s, "
7603147Sxc151355 					"invalid ERP element; "
7613147Sxc151355 					"length %u, expecting 1\n",
7623147Sxc151355 					IEEE80211_SUBTYPE_NAME(subtype),
7633147Sxc151355 					frm[1]);
7643147Sxc151355 				break;
7653147Sxc151355 			}
7663147Sxc151355 			scan.erp = frm[2];
7673147Sxc151355 			scan.phytype = IEEE80211_T_OFDM;
7683147Sxc151355 			break;
7693147Sxc151355 		case IEEE80211_ELEMID_RSN:
7703147Sxc151355 			scan.wpa = frm;
7713147Sxc151355 			break;
772*4126Szf162725 		case IEEE80211_ELEMID_VENDOR:
773*4126Szf162725 			if (iswpaoui(frm))
774*4126Szf162725 				scan.wpa = frm;		/* IEEE802.11i D3.0 */
775*4126Szf162725 			break;
7763147Sxc151355 		default:
7773147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ELEMID,
7783147Sxc151355 				"ieee80211_recv_mgmt: ignore %s,"
7793147Sxc151355 				"unhandled id %u, len %u, totallen %u",
7803147Sxc151355 				IEEE80211_SUBTYPE_NAME(subtype),
7813147Sxc151355 				*frm, frm[1],
7823147Sxc151355 				mp->b_wptr - mp->b_rptr);
7833147Sxc151355 			break;
7843147Sxc151355 		}
7853147Sxc151355 		/* frm[1] - component length */
7863147Sxc151355 		frm += IEEE80211_ELEM_LEN(frm[1]);
7873147Sxc151355 	}
7883147Sxc151355 	IEEE80211_VERIFY_ELEMENT(scan.rates, IEEE80211_RATE_MAXSIZE, return);
7893147Sxc151355 	IEEE80211_VERIFY_ELEMENT(scan.ssid, IEEE80211_NWID_LEN, return);
7903147Sxc151355 	if (ieee80211_isclr(ic->ic_chan_active, scan.chan)) {
7913147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
7923147Sxc151355 			"ieee80211_recv_mgmt: ignore %s ,"
7933147Sxc151355 			"invalid channel %u\n",
7943147Sxc151355 			IEEE80211_SUBTYPE_NAME(subtype), scan.chan);
7953147Sxc151355 		return;
7963147Sxc151355 	}
7973147Sxc151355 	if (scan.chan != scan.bchan &&
7983147Sxc151355 	    ic->ic_phytype != IEEE80211_T_FH) {
7993147Sxc151355 		/*
8003147Sxc151355 		 * Frame was received on a channel different from the
8013147Sxc151355 		 * one indicated in the DS params element id;
8023147Sxc151355 		 * silently discard it.
8033147Sxc151355 		 *
8043147Sxc151355 		 * NB:	this can happen due to signal leakage.
8053147Sxc151355 		 *	But we should take it for FH phy because
8063147Sxc151355 		 *	the rssi value should be correct even for
8073147Sxc151355 		 *	different hop pattern in FH.
8083147Sxc151355 		 */
8093147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ELEMID,
8103147Sxc151355 			"ieee80211_recv_mgmt: ignore %s ,"
8113147Sxc151355 			"phytype %u channel %u marked for %u\n",
8123147Sxc151355 			IEEE80211_SUBTYPE_NAME(subtype),
8133147Sxc151355 			ic->ic_phytype, scan.bchan, scan.chan);
8143147Sxc151355 		return;
8153147Sxc151355 	}
8163147Sxc151355 	if (!(IEEE80211_BINTVAL_MIN <= scan.bintval &&
8173147Sxc151355 	    scan.bintval <= IEEE80211_BINTVAL_MAX)) {
8183147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
8193147Sxc151355 			"ieee80211_recv_mgmt: ignore %s ,"
8203147Sxc151355 			"bogus beacon interval %u\n",
8213147Sxc151355 			IEEE80211_SUBTYPE_NAME(subtype), scan.bintval);
8223147Sxc151355 		return;
8233147Sxc151355 	}
8243147Sxc151355 
8253147Sxc151355 	/*
8263147Sxc151355 	 * When operating in station mode, check for state updates.
8273147Sxc151355 	 * Be careful to ignore beacons received while doing a
8283147Sxc151355 	 * background scan.  We consider only 11g/WMM stuff right now.
8293147Sxc151355 	 */
8303147Sxc151355 	if (ic->ic_opmode == IEEE80211_M_STA &&
8313147Sxc151355 	    in->in_associd != 0 &&
8323147Sxc151355 	    (!(ic->ic_flags & IEEE80211_F_SCAN) ||
8333147Sxc151355 	    IEEE80211_ADDR_EQ(wh->i_addr2, in->in_bssid))) {
8343147Sxc151355 		/* record tsf of last beacon */
8353147Sxc151355 		bcopy(scan.tstamp, in->in_tstamp.data,
8363147Sxc151355 		    sizeof (in->in_tstamp));
8373147Sxc151355 		/* count beacon frame for s/w bmiss handling */
8383147Sxc151355 		im->im_swbmiss_count++;
8393147Sxc151355 		im->im_bmiss_count = 0;
8403147Sxc151355 
8413147Sxc151355 		if ((in->in_capinfo ^ scan.capinfo) &
8423147Sxc151355 		    IEEE80211_CAPINFO_SHORT_SLOTTIME) {
8433147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
8443147Sxc151355 				"ieee80211_recv_mgmt: "
8453147Sxc151355 				"[%s] cap change: before 0x%x, now 0x%x\n",
8463147Sxc151355 				ieee80211_macaddr_sprintf(wh->i_addr2),
8473147Sxc151355 				in->in_capinfo, scan.capinfo);
8483147Sxc151355 			/*
8493147Sxc151355 			 * NB:	we assume short preamble doesn't
8503147Sxc151355 			 *	change dynamically
8513147Sxc151355 			 */
8523147Sxc151355 			ieee80211_set_shortslottime(ic,
8533147Sxc151355 				ic->ic_curmode == IEEE80211_MODE_11A ||
8543147Sxc151355 					(scan.capinfo &
8553147Sxc151355 					IEEE80211_CAPINFO_SHORT_SLOTTIME));
8563147Sxc151355 			in->in_capinfo = scan.capinfo;
8573147Sxc151355 		}
8583147Sxc151355 
8593147Sxc151355 		if (scan.tim != NULL) {
8603147Sxc151355 			struct ieee80211_tim_ie *ie;
8613147Sxc151355 
8623147Sxc151355 			ie = (struct ieee80211_tim_ie *)scan.tim;
8633147Sxc151355 			in->in_dtim_count = ie->tim_count;
8643147Sxc151355 			in->in_dtim_period = ie->tim_period;
8653147Sxc151355 		}
8663147Sxc151355 		if (ic->ic_flags & IEEE80211_F_SCAN) {
8673147Sxc151355 			ieee80211_add_scan(ic, &scan, wh, subtype, rssi,
8683147Sxc151355 				rstamp);
8693147Sxc151355 		}
8703147Sxc151355 		return;
8713147Sxc151355 	}
8723147Sxc151355 	/*
8733147Sxc151355 	 * If scanning, just pass information to the scan module.
8743147Sxc151355 	 */
8753147Sxc151355 	if (ic->ic_flags & IEEE80211_F_SCAN) {
8763147Sxc151355 		ieee80211_add_scan(ic, &scan, wh, subtype, rssi, rstamp);
8773147Sxc151355 		return;
8783147Sxc151355 	}
8793147Sxc151355 
8803147Sxc151355 	if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
8813147Sxc151355 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, in->in_macaddr)) {
8823147Sxc151355 			/*
8833147Sxc151355 			 * Create a new entry in the neighbor table.
8843147Sxc151355 			 */
8853147Sxc151355 			in = ieee80211_add_neighbor(ic, wh, &scan);
8863147Sxc151355 		} else if (in->in_capinfo == 0) {
8873147Sxc151355 			/*
8883147Sxc151355 			 * Update faked node created on transmit.
8893147Sxc151355 			 * Note this also updates the tsf.
8903147Sxc151355 			 */
8913147Sxc151355 			ieee80211_init_neighbor(in, wh, &scan);
8923147Sxc151355 		} else {
8933147Sxc151355 			/*
8943147Sxc151355 			 * Record tsf for potential resync.
8953147Sxc151355 			 */
8963147Sxc151355 			bcopy(scan.tstamp, in->in_tstamp.data,
8973147Sxc151355 			    sizeof (in->in_tstamp));
8983147Sxc151355 		}
8993147Sxc151355 		if (in != NULL) {
9003147Sxc151355 			in->in_rssi = (uint8_t)rssi;
9013147Sxc151355 			in->in_rstamp = rstamp;
9023147Sxc151355 		}
9033147Sxc151355 	}
9043147Sxc151355 }
9053147Sxc151355 
9063147Sxc151355 /*
9073147Sxc151355  * Perform input processing for 802.11 management frames.
9083147Sxc151355  * It's the default ic_recv_mgmt callback function for the interface
9093147Sxc151355  * softc, ic. Tipically ic_recv_mgmt is called within ieee80211_input()
9103147Sxc151355  */
9113147Sxc151355 void
9123147Sxc151355 ieee80211_recv_mgmt(ieee80211com_t *ic, mblk_t *mp, struct ieee80211_node *in,
9133147Sxc151355     int subtype, int rssi, uint32_t rstamp)
9143147Sxc151355 {
9153147Sxc151355 	struct ieee80211_frame *wh;
9163147Sxc151355 	uint8_t *frm;		/* pointer to start of the frame */
9173147Sxc151355 	uint8_t *efrm;		/* pointer to end of the frame */
9183147Sxc151355 	uint8_t *ssid;
9193147Sxc151355 	uint8_t *rates;
9203147Sxc151355 	uint8_t *xrates;	/* extended rates */
9213147Sxc151355 	boolean_t allocbs = B_FALSE;
9223147Sxc151355 	uint8_t rate;
9233147Sxc151355 	uint16_t algo;		/* authentication algorithm */
9243147Sxc151355 	uint16_t seq;		/* sequence no */
9253147Sxc151355 	uint16_t status;
9263147Sxc151355 	uint16_t capinfo;
9273147Sxc151355 	uint16_t associd;	/* association ID */
9283147Sxc151355 
9293147Sxc151355 	IEEE80211_LOCK(ic);
9303147Sxc151355 	wh = (struct ieee80211_frame *)mp->b_rptr;
9313147Sxc151355 	frm = (uint8_t *)&wh[1];
9323147Sxc151355 	efrm = (uint8_t *)mp->b_wptr;
9333147Sxc151355 	switch (subtype) {
9343147Sxc151355 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
9353147Sxc151355 	case IEEE80211_FC0_SUBTYPE_BEACON:
9363147Sxc151355 		ieee80211_recv_beacon(ic, mp, in, subtype, rssi, rstamp);
9373147Sxc151355 		break;
9383147Sxc151355 
9393147Sxc151355 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
9403147Sxc151355 		if (ic->ic_opmode == IEEE80211_M_STA ||
9413147Sxc151355 		    ic->ic_state != IEEE80211_S_RUN ||
9423147Sxc151355 		    IEEE80211_IS_MULTICAST(wh->i_addr2)) {
9433147Sxc151355 			break;
9443147Sxc151355 		}
9453147Sxc151355 
9463147Sxc151355 		/*
9473147Sxc151355 		 * prreq frame format
9483147Sxc151355 		 *	[tlv] ssid
9493147Sxc151355 		 *	[tlv] supported rates
9503147Sxc151355 		 *	[tlv] extended supported rates
9513147Sxc151355 		 */
9523147Sxc151355 		ssid = rates = xrates = NULL;
9533147Sxc151355 		while (frm < efrm) {
9543147Sxc151355 			IEEE80211_VERIFY_LENGTH(efrm - frm, 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);
9693147Sxc151355 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, break);
9703147Sxc151355 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid, break);
9713147Sxc151355 		if ((ic->ic_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
9723147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_INPUT,
9733147Sxc151355 				"ieee80211_recv_mgmt: ignore %s, "
9743147Sxc151355 				"no ssid with ssid suppression enabled",
9753147Sxc151355 				IEEE80211_SUBTYPE_NAME(subtype));
9763147Sxc151355 			break;
9773147Sxc151355 		}
9783147Sxc151355 
9793147Sxc151355 		if (in == ic->ic_bss) {
9803147Sxc151355 			if (ic->ic_opmode != IEEE80211_M_IBSS) {
9813147Sxc151355 				in = ieee80211_tmp_node(ic, wh->i_addr2);
9823147Sxc151355 				allocbs = B_TRUE;
9833147Sxc151355 			} else if (!IEEE80211_ADDR_EQ(wh->i_addr2,
9843147Sxc151355 			    in->in_macaddr)) {
9853147Sxc151355 				/*
9863147Sxc151355 				 * Cannot tell if the sender is operating
9873147Sxc151355 				 * in ibss mode.  But we need a new node to
9883147Sxc151355 				 * send the response so blindly add them to the
9893147Sxc151355 				 * neighbor table.
9903147Sxc151355 				 */
9913147Sxc151355 				in = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
9923147Sxc151355 					wh->i_addr2);
9933147Sxc151355 			}
9943147Sxc151355 			if (in == NULL)
9953147Sxc151355 				break;
9963147Sxc151355 		}
9973147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ASSOC, "ieee80211_recv_mgmt: "
9983147Sxc151355 			"[%s] recv probe req\n",
9993147Sxc151355 			ieee80211_macaddr_sprintf(wh->i_addr2));
10003147Sxc151355 		in->in_rssi = (uint8_t)rssi;
10013147Sxc151355 		in->in_rstamp = rstamp;
10023147Sxc151355 		/*
10033147Sxc151355 		 * Adjust and check station's rate list with device's
10043147Sxc151355 		 * supported rate.  Send back response if there is at
10053147Sxc151355 		 * least one rate or the fixed rate(if being set) is
10063147Sxc151355 		 * supported by both station and the device
10073147Sxc151355 		 */
10083147Sxc151355 		rate = ieee80211_setup_rates(in, rates, xrates,
10093147Sxc151355 			IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
10103147Sxc151355 			IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
10113147Sxc151355 		if (rate & IEEE80211_RATE_BASIC) {
10123147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_XRATE, "ieee80211_recv_mgmt"
10133147Sxc151355 				"%s recv'd rate set invalid",
10143147Sxc151355 				IEEE80211_SUBTYPE_NAME(subtype));
10153147Sxc151355 		} else {
10163147Sxc151355 			IEEE80211_UNLOCK(ic);
10173147Sxc151355 			IEEE80211_SEND_MGMT(ic, in,
10183147Sxc151355 				IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
10193147Sxc151355 			IEEE80211_LOCK(ic);
10203147Sxc151355 		}
10213147Sxc151355 		if (allocbs) {
10223147Sxc151355 			/*
10233147Sxc151355 			 * Temporary node created just to send a
10243147Sxc151355 			 * response, reclaim immediately.
10253147Sxc151355 			 */
10263147Sxc151355 			ieee80211_free_node(in);
10273147Sxc151355 		}
10283147Sxc151355 		break;
10293147Sxc151355 
10303147Sxc151355 	case IEEE80211_FC0_SUBTYPE_AUTH:
10313147Sxc151355 		/*
10323147Sxc151355 		 * auth frame format
10333147Sxc151355 		 *	[2] algorithm
10343147Sxc151355 		 *	[2] sequence
10353147Sxc151355 		 *	[2] status
10363147Sxc151355 		 *	[tlv*] challenge
10373147Sxc151355 		 */
10383147Sxc151355 		IEEE80211_VERIFY_LENGTH(efrm - frm, IEEE80211_AUTH_ELEM_MIN,
10393147Sxc151355 			break);
10403147Sxc151355 		algo   = (*(uint16_t *)frm);
10413147Sxc151355 		seq    = (*(uint16_t *)(frm + 2));
10423147Sxc151355 		status = (*(uint16_t *)(frm + 4));
10433147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_recv_mgmt: "
10443147Sxc151355 			"[%s] recv auth frame with algorithm %d seq %d\n",
10453147Sxc151355 			ieee80211_macaddr_sprintf(wh->i_addr2), algo, seq);
10463147Sxc151355 
10473147Sxc151355 		if (ic->ic_flags & IEEE80211_F_COUNTERM) {
10483147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
10493147Sxc151355 				"ieee80211_recv_mgmt: ignore auth, %s\n",
10503147Sxc151355 				"TKIP countermeasures enabled");
10513147Sxc151355 			break;
10523147Sxc151355 		}
10533147Sxc151355 		switch (algo) {
10543147Sxc151355 		case IEEE80211_AUTH_ALG_SHARED:
10553147Sxc151355 			ieee80211_auth_shared(ic, wh, frm + 6, efrm, in,
10563147Sxc151355 				seq, status);
10573147Sxc151355 			break;
10583147Sxc151355 		case IEEE80211_AUTH_ALG_OPEN:
10593147Sxc151355 			ieee80211_auth_open(ic, wh, in, seq, status);
10603147Sxc151355 			break;
10613147Sxc151355 		default:
10623147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_recv_mgmt: "
10633147Sxc151355 				"ignore auth, unsupported alg %d", algo);
10643147Sxc151355 			break;
10653147Sxc151355 		}
10663147Sxc151355 		break;
10673147Sxc151355 
10683147Sxc151355 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
10693147Sxc151355 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
10703147Sxc151355 		if (ic->ic_opmode != IEEE80211_M_STA ||
10713147Sxc151355 		    ic->ic_state != IEEE80211_S_ASSOC)
10723147Sxc151355 			break;
10733147Sxc151355 
10743147Sxc151355 		/*
10753147Sxc151355 		 * asresp frame format
10763147Sxc151355 		 *	[2] capability information
10773147Sxc151355 		 *	[2] status
10783147Sxc151355 		 *	[2] association ID
10793147Sxc151355 		 *	[tlv] supported rates
10803147Sxc151355 		 *	[tlv] extended supported rates
10813147Sxc151355 		 *	[tlv] WME
10823147Sxc151355 		 */
10833147Sxc151355 		IEEE80211_VERIFY_LENGTH(efrm - frm,
10843147Sxc151355 			IEEE80211_ASSOC_RESP_ELEM_MIN, break);
10853147Sxc151355 		in = ic->ic_bss;
10863147Sxc151355 		capinfo = (*(uint16_t *)frm);
10873147Sxc151355 		frm += 2;
10883147Sxc151355 		status = (*(uint16_t *)frm);
10893147Sxc151355 		frm += 2;
10903147Sxc151355 		if (status != 0) {
10913147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
10923147Sxc151355 				"assoc failed (reason %d)\n", status);
10933147Sxc151355 			in = ieee80211_find_node(&ic->ic_scan, wh->i_addr2);
10943147Sxc151355 			if (in != NULL) {
10953147Sxc151355 				in->in_fails++;
10963147Sxc151355 				ieee80211_free_node(in);
10973147Sxc151355 			}
10983147Sxc151355 			break;
10993147Sxc151355 		}
11003147Sxc151355 		associd = (*(uint16_t *)frm);
11013147Sxc151355 		frm += 2;
11023147Sxc151355 
11033147Sxc151355 		rates = xrates = NULL;
11043147Sxc151355 		while (frm < efrm) {
11053147Sxc151355 			/*
11063147Sxc151355 			 * Do not discard frames containing proprietary Agere
11073147Sxc151355 			 * elements 128 and 129, as the reported element length
11083147Sxc151355 			 * is often wrong. Skip rest of the frame, since we can
11093147Sxc151355 			 * not rely on the given element length making it
11103147Sxc151355 			 * impossible to know where the next element starts
11113147Sxc151355 			 */
11123147Sxc151355 			if ((*frm == IEEE80211_ELEMID_AGERE1) ||
11133147Sxc151355 			    (*frm == IEEE80211_ELEMID_AGERE2)) {
11143147Sxc151355 				frm = efrm;
11153147Sxc151355 				break;
11163147Sxc151355 			}
11173147Sxc151355 
11183147Sxc151355 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1], goto out);
11193147Sxc151355 			switch (*frm) {
11203147Sxc151355 			case IEEE80211_ELEMID_RATES:
11213147Sxc151355 				rates = frm;
11223147Sxc151355 				break;
11233147Sxc151355 			case IEEE80211_ELEMID_XRATES:
11243147Sxc151355 				xrates = frm;
11253147Sxc151355 				break;
11263147Sxc151355 			}
11273147Sxc151355 			frm += frm[1] + 2;
11283147Sxc151355 		}
11293147Sxc151355 
11303147Sxc151355 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, break);
11313147Sxc151355 		/*
11323147Sxc151355 		 * Adjust and check AP's rate list with device's
11333147Sxc151355 		 * supported rate. Re-start scan if no rate is or the
11343147Sxc151355 		 * fixed rate(if being set) cannot be supported by
11353147Sxc151355 		 * either AP or the device.
11363147Sxc151355 		 */
11373147Sxc151355 		rate = ieee80211_setup_rates(in, rates, xrates,
11383147Sxc151355 			IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
11393147Sxc151355 			IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
11403147Sxc151355 		if (rate & IEEE80211_RATE_BASIC) {
11413147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
11423147Sxc151355 				"assoc failed (rate set mismatch)\n");
11433147Sxc151355 			if (in != ic->ic_bss)
11443147Sxc151355 				in->in_fails++;
11453147Sxc151355 			IEEE80211_UNLOCK(ic);
11463147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
11473147Sxc151355 			return;
11483147Sxc151355 		}
11493147Sxc151355 
11503147Sxc151355 		in->in_capinfo = capinfo;
11513147Sxc151355 		in->in_associd = associd;
11523147Sxc151355 		in->in_flags &= ~IEEE80211_NODE_QOS;
11533147Sxc151355 		/*
11543147Sxc151355 		 * Configure state now that we are associated.
11553147Sxc151355 		 */
11563147Sxc151355 		if (ic->ic_curmode == IEEE80211_MODE_11A ||
11573147Sxc151355 		    (in->in_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
11583147Sxc151355 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
11593147Sxc151355 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
11603147Sxc151355 		} else {
11613147Sxc151355 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
11623147Sxc151355 			ic->ic_flags |= IEEE80211_F_USEBARKER;
11633147Sxc151355 		}
11643147Sxc151355 		ieee80211_set_shortslottime(ic,
11653147Sxc151355 			ic->ic_curmode == IEEE80211_MODE_11A ||
11663147Sxc151355 			(in->in_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
11673147Sxc151355 		/*
11683147Sxc151355 		 * Honor ERP protection.
11693147Sxc151355 		 *
11703147Sxc151355 		 * NB:	in_erp should zero for non-11g operation.
11713147Sxc151355 		 *	check ic_curmode anyway
11723147Sxc151355 		 */
11733147Sxc151355 		if (ic->ic_curmode == IEEE80211_MODE_11G &&
11743147Sxc151355 		    (in->in_erp & IEEE80211_ERP_USE_PROTECTION))
11753147Sxc151355 			ic->ic_flags |= IEEE80211_F_USEPROT;
11763147Sxc151355 		else
11773147Sxc151355 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
11783147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ASSOC,
11793147Sxc151355 			"assoc success: %s preamble, %s slot time%s%s\n",
11803147Sxc151355 			ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
11813147Sxc151355 			ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
11823147Sxc151355 			ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
11833147Sxc151355 			in->in_flags & IEEE80211_NODE_QOS ? ", QoS" : "");
11843147Sxc151355 		IEEE80211_UNLOCK(ic);
11853147Sxc151355 		ieee80211_new_state(ic, IEEE80211_S_RUN, subtype);
11863147Sxc151355 		return;
11873147Sxc151355 
11883147Sxc151355 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
11893147Sxc151355 		if (ic->ic_state == IEEE80211_S_SCAN)
11903147Sxc151355 			break;
11913147Sxc151355 
11923147Sxc151355 		/*
11933147Sxc151355 		 * deauth frame format
11943147Sxc151355 		 *	[2] reason
11953147Sxc151355 		 */
11963147Sxc151355 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, break);
11973147Sxc151355 		status = (*(uint16_t *)frm);
11983147Sxc151355 
11993147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH,
12003147Sxc151355 			"recv deauthenticate (reason %d)\n", status);
12013147Sxc151355 		switch (ic->ic_opmode) {
12023147Sxc151355 		case IEEE80211_M_STA:
12033147Sxc151355 			IEEE80211_UNLOCK(ic);
12043147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
12053147Sxc151355 				wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
12063147Sxc151355 			return;
12073147Sxc151355 		default:
12083147Sxc151355 			break;
12093147Sxc151355 		}
12103147Sxc151355 		break;
12113147Sxc151355 
12123147Sxc151355 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
12133147Sxc151355 		if (ic->ic_state != IEEE80211_S_RUN &&
12143147Sxc151355 		    ic->ic_state != IEEE80211_S_ASSOC &&
12153147Sxc151355 		    ic->ic_state != IEEE80211_S_AUTH)
12163147Sxc151355 			break;
12173147Sxc151355 		/*
12183147Sxc151355 		 * disassoc frame format
12193147Sxc151355 		 *	[2] reason
12203147Sxc151355 		 */
12213147Sxc151355 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, break);
12223147Sxc151355 		status = (*(uint16_t *)frm);
12233147Sxc151355 
12243147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ASSOC,
12253147Sxc151355 			"recv disassociate (reason %d)\n", status);
12263147Sxc151355 		switch (ic->ic_opmode) {
12273147Sxc151355 		case IEEE80211_M_STA:
12283147Sxc151355 			IEEE80211_UNLOCK(ic);
12293147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
12303147Sxc151355 				wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
12313147Sxc151355 			return;
12323147Sxc151355 		default:
12333147Sxc151355 			break;
12343147Sxc151355 		}
12353147Sxc151355 		break;
12363147Sxc151355 
12373147Sxc151355 	default:
12383147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_recv_mgmt: "
12393147Sxc151355 			"subtype 0x%x not handled\n", subtype);
12403147Sxc151355 		break;
12413147Sxc151355 	} /* switch subtype */
12423147Sxc151355 out:
12433147Sxc151355 	IEEE80211_UNLOCK(ic);
12443147Sxc151355 }
1245