13147Sxc151355 /*
2*7249Sff224033  * Copyright 2008 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>
45*7249Sff224033 #include <sys/strsun.h>
463147Sxc151355 #include "net80211_impl.h"
473147Sxc151355 
483147Sxc151355 static mblk_t *ieee80211_defrag(ieee80211com_t *, ieee80211_node_t *,
493147Sxc151355     mblk_t *, int);
503147Sxc151355 
513147Sxc151355 /*
523147Sxc151355  * Process a received frame.  The node associated with the sender
533147Sxc151355  * should be supplied.  If nothing was found in the node table then
543147Sxc151355  * the caller is assumed to supply a reference to ic_bss instead.
553147Sxc151355  * The RSSI and a timestamp are also supplied.  The RSSI data is used
563147Sxc151355  * during AP scanning to select a AP to associate with; it can have
573147Sxc151355  * any units so long as values have consistent units and higher values
583147Sxc151355  * mean ``better signal''.  The receive timestamp is currently not used
593147Sxc151355  * by the 802.11 layer.
603147Sxc151355  */
613147Sxc151355 int
623147Sxc151355 ieee80211_input(ieee80211com_t *ic, mblk_t *mp, struct ieee80211_node *in,
633147Sxc151355     int32_t rssi, uint32_t rstamp)
643147Sxc151355 {
653147Sxc151355 	struct ieee80211_frame *wh;
663147Sxc151355 	struct ieee80211_key *key;
673147Sxc151355 	uint8_t *bssid;
683147Sxc151355 	int hdrspace;
693147Sxc151355 	int len;
703147Sxc151355 	uint16_t rxseq;
713147Sxc151355 	uint8_t dir;
723147Sxc151355 	uint8_t type;
733147Sxc151355 	uint8_t subtype;
743147Sxc151355 	uint8_t tid;
753147Sxc151355 
763147Sxc151355 	ASSERT(in != NULL);
773147Sxc151355 	type = (uint8_t)-1;		/* undefined */
78*7249Sff224033 	len = MBLKL(mp);
793147Sxc151355 	if (len < sizeof (struct ieee80211_frame_min)) {
803147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
81*7249Sff224033 		    "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: "
94*7249Sff224033 		    "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,
119*7249Sff224033 					    "ieee80211_input: too short(2):"
120*7249Sff224033 					    "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,
134*7249Sff224033 				    "ieee80211_input: not to bss %s\n",
135*7249Sff224033 				    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,
150*7249Sff224033 				    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,
169*7249Sff224033 				    "ieee80211_input: duplicate",
170*7249Sff224033 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
171*7249Sff224033 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
172*7249Sff224033 				    in->in_rxseqs[tid] >>
173*7249Sff224033 				    IEEE80211_SEQ_SEQ_SHIFT,
174*7249Sff224033 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
175*7249Sff224033 				    in->in_rxseqs[tid] &
176*7249Sff224033 				    IEEE80211_SEQ_FRAG_MASK,
177*7249Sff224033 				    tid);
1783147Sxc151355 				ic->ic_stats.is_rx_dups++;
1793147Sxc151355 				goto out_exit_mutex;
1803147Sxc151355 			}
1813147Sxc151355 			in->in_rxseqs[tid] = rxseq;
1823147Sxc151355 		}
1833147Sxc151355 		in->in_inact = 0;
1843147Sxc151355 	}
1853147Sxc151355 
1863147Sxc151355 	hdrspace = ieee80211_hdrspace(wh);
1873147Sxc151355 	switch (type) {
1883147Sxc151355 	case IEEE80211_FC0_TYPE_DATA:
1893147Sxc151355 		if (len < hdrspace) {
1903147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
191*7249Sff224033 			    "data too short: expecting %u", hdrspace);
1923147Sxc151355 			goto out_exit_mutex;
1933147Sxc151355 		}
1943147Sxc151355 		switch (ic->ic_opmode) {
1953147Sxc151355 		case IEEE80211_M_STA:
1963147Sxc151355 			if (dir != IEEE80211_FC1_DIR_FROMDS) {
1973147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
198*7249Sff224033 				    "ieee80211_input: data ",
199*7249Sff224033 				    "unknown dir 0x%x", dir);
2003147Sxc151355 				goto out_exit_mutex;
2013147Sxc151355 			}
2023147Sxc151355 			if (IEEE80211_IS_MULTICAST(wh->i_addr1) &&
2033147Sxc151355 			    IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_macaddr)) {
2043147Sxc151355 				/*
2053147Sxc151355 				 * In IEEE802.11 network, multicast packet
2063147Sxc151355 				 * sent from me is broadcasted from AP.
2073147Sxc151355 				 * It should be silently discarded for
2083147Sxc151355 				 * SIMPLEX interface.
2093147Sxc151355 				 */
2103147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
211*7249Sff224033 				    "ieee80211_input: multicast echo\n");
2123147Sxc151355 				goto out_exit_mutex;
2133147Sxc151355 			}
2143147Sxc151355 			break;
2153147Sxc151355 		case IEEE80211_M_IBSS:
2163147Sxc151355 		case IEEE80211_M_AHDEMO:
2173147Sxc151355 			if (dir != IEEE80211_FC1_DIR_NODS) {
2183147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
219*7249Sff224033 				    "ieee80211_input: unknown dir 0x%x",
220*7249Sff224033 				    dir);
2213147Sxc151355 				goto out_exit_mutex;
2223147Sxc151355 			}
2233147Sxc151355 			break;
2243147Sxc151355 		default:
2253147Sxc151355 			ieee80211_err("ieee80211_input: "
226*7249Sff224033 			    "receive data, unknown opmode %u, skip\n",
227*7249Sff224033 			    ic->ic_opmode);
2283147Sxc151355 			goto out_exit_mutex;
2293147Sxc151355 		}
2303147Sxc151355 
2313147Sxc151355 		/*
2323147Sxc151355 		 * Handle privacy requirements.
2333147Sxc151355 		 */
2343147Sxc151355 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2353147Sxc151355 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
2363147Sxc151355 				/*
2373147Sxc151355 				 * Discard encrypted frames when privacy off.
2383147Sxc151355 				 */
2393147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
240*7249Sff224033 				    "ieee80211_input: ""WEP PRIVACY off");
2413147Sxc151355 				ic->ic_stats.is_wep_errors++;
2423147Sxc151355 				goto out_exit_mutex;
2433147Sxc151355 			}
2443147Sxc151355 			key = ieee80211_crypto_decap(ic, mp, hdrspace);
2453147Sxc151355 			if (key == NULL) {
2463147Sxc151355 				/* NB: stats+msgs handled in crypto_decap */
2473147Sxc151355 				ic->ic_stats.is_wep_errors++;
2483147Sxc151355 				goto out_exit_mutex;
2493147Sxc151355 			}
2503147Sxc151355 			wh = (struct ieee80211_frame *)mp->b_rptr;
2513147Sxc151355 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
2523147Sxc151355 		} else {
2533147Sxc151355 			key = NULL;
2543147Sxc151355 		}
2553147Sxc151355 
2563147Sxc151355 		/*
2573147Sxc151355 		 * Next up, any fragmentation
2583147Sxc151355 		 */
2593147Sxc151355 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2603147Sxc151355 			mp = ieee80211_defrag(ic, in, mp, hdrspace);
2613147Sxc151355 			if (mp == NULL) {
2623147Sxc151355 				/* Fragment dropped or frame not complete yet */
2633147Sxc151355 				goto out_exit_mutex;
2643147Sxc151355 			}
2653147Sxc151355 		}
2663147Sxc151355 		wh = NULL;	/* no longer valid, catch any uses */
2673147Sxc151355 
2683147Sxc151355 		/*
2693147Sxc151355 		 * Next strip any MSDU crypto bits.
2703147Sxc151355 		 */
2713147Sxc151355 		if (key != NULL && !ieee80211_crypto_demic(ic, key, mp, 0)) {
2723147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_INPUT, "ieee80211_input: "
273*7249Sff224033 			    "data demic error\n");
2743147Sxc151355 			goto out_exit_mutex;
2753147Sxc151355 		}
2763147Sxc151355 
2773147Sxc151355 		ic->ic_stats.is_rx_frags++;
2783147Sxc151355 		ic->ic_stats.is_rx_bytes += len;
2793147Sxc151355 		IEEE80211_UNLOCK(ic);
2803147Sxc151355 		mac_rx(ic->ic_mach, NULL, mp);
2813147Sxc151355 		return (IEEE80211_FC0_TYPE_DATA);
2823147Sxc151355 
2833147Sxc151355 	case IEEE80211_FC0_TYPE_MGT:
2843147Sxc151355 		if (dir != IEEE80211_FC1_DIR_NODS)
2853147Sxc151355 			goto out_exit_mutex;
2863147Sxc151355 		if (len < sizeof (struct ieee80211_frame))
2873147Sxc151355 			goto out_exit_mutex;
2883147Sxc151355 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2893147Sxc151355 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
2903147Sxc151355 				/*
2913147Sxc151355 				 * Only shared key auth frames with a challenge
2923147Sxc151355 				 * should be encrypted, discard all others.
2933147Sxc151355 				 */
2943147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
295*7249Sff224033 				    "ieee80211_input: "
296*7249Sff224033 				    "%s WEP set but not permitted",
297*7249Sff224033 				    IEEE80211_SUBTYPE_NAME(subtype));
2983147Sxc151355 				ic->ic_stats.is_wep_errors++;
2993147Sxc151355 				goto out_exit_mutex;
3003147Sxc151355 			}
3013147Sxc151355 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
3023147Sxc151355 				/*
3033147Sxc151355 				 * Discard encrypted frames when privacy off.
3043147Sxc151355 				 */
3053147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
306*7249Sff224033 				    "ieee80211_input: "
307*7249Sff224033 				    "mgt WEP set but PRIVACY off");
3083147Sxc151355 				ic->ic_stats.is_wep_errors++;
3093147Sxc151355 				goto out_exit_mutex;
3103147Sxc151355 			}
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: "
326*7249Sff224033 		    "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,
458*7249Sff224033 			    "ieee80211_setup_rates: %s",
459*7249Sff224033 			    "[%s] extended rate set too large;"
460*7249Sff224033 			    " only using %u of %u rates\n",
461*7249Sff224033 			    ieee80211_macaddr_sprintf(in->in_macaddr),
462*7249Sff224033 			    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,
481*7249Sff224033 		    "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,
492*7249Sff224033 			    "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,
499*7249Sff224033 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
5003147Sxc151355 		}
5013147Sxc151355 		IEEE80211_LOCK(ic);
5023147Sxc151355 	} else {
5033147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_auth_open: "
504*7249Sff224033 		    "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,
517*7249Sff224033 		    KM_NOSLEEP);
5183147Sxc151355 	}
5193147Sxc151355 	if (in->in_challenge == NULL) {
5203147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
521*7249Sff224033 		    "[%s] shared key challenge alloc failed\n",
522*7249Sff224033 		    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: "
547*7249Sff224033 		    "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 		 */
559*7249Sff224033 		if ((frm[1] + 2) > (_PTRDIFF(efrm, frm))) {
5603147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH,
561*7249Sff224033 			    "ieee80211_auth_shared: ie %d%d too long\n",
562*7249Sff224033 			    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,
574*7249Sff224033 			    "ieee80211_auth_shared: no challenge\n");
5753147Sxc151355 			goto bad;
5763147Sxc151355 		}
5773147Sxc151355 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
5783147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH,
579*7249Sff224033 			    "ieee80211_auth_shared: bad challenge len %d\n",
580*7249Sff224033 			    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 |
599*7249Sff224033 				    IEEE80211_MSG_AUTH,
600*7249Sff224033 				    "shared key auth failed (reason %d)\n",
601*7249Sff224033 				    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,
608*7249Sff224033 			    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,
617*7249Sff224033 			    seq + 1);
6183147Sxc151355 			IEEE80211_LOCK(ic);
6193147Sxc151355 			break;
6203147Sxc151355 		default:
6213147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH, "80211_auth_shared: "
622*7249Sff224033 			    "shared key auth: bad seq %d", seq);
6233147Sxc151355 			return;
6243147Sxc151355 		}
6253147Sxc151355 		break;
6263147Sxc151355 
6273147Sxc151355 	default:
6283147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH,
629*7249Sff224033 		    "ieee80211_auth_shared: bad opmode %u\n",
630*7249Sff224033 		    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 	 */
704*7249Sff224033 	IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
705*7249Sff224033 	    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 
724*7249Sff224033 		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;
751*7249Sff224033 			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,
761*7249Sff224033 				    "ieee80211_recv_mgmt: ignore %s, "
762*7249Sff224033 				    "invalid ERP element; "
763*7249Sff224033 				    "length %u, expecting 1\n",
764*7249Sff224033 				    IEEE80211_SUBTYPE_NAME(subtype),
765*7249Sff224033 				    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,
780*7249Sff224033 			    "ieee80211_recv_mgmt: ignore %s,"
781*7249Sff224033 			    "unhandled id %u, len %u, totallen %u",
782*7249Sff224033 			    IEEE80211_SUBTYPE_NAME(subtype),
783*7249Sff224033 			    *frm, frm[1],
784*7249Sff224033 			    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,
794*7249Sff224033 		    "ieee80211_recv_mgmt: ignore %s ,"
795*7249Sff224033 		    "invalid channel %u\n",
796*7249Sff224033 		    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,
812*7249Sff224033 		    "ieee80211_recv_mgmt: ignore %s ,"
813*7249Sff224033 		    "phytype %u channel %u marked for %u\n",
814*7249Sff224033 		    IEEE80211_SUBTYPE_NAME(subtype),
815*7249Sff224033 		    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,
821*7249Sff224033 		    "ieee80211_recv_mgmt: ignore %s ,"
822*7249Sff224033 		    "bogus beacon interval %u\n",
823*7249Sff224033 		    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,
846*7249Sff224033 			    "ieee80211_recv_mgmt: "
847*7249Sff224033 			    "[%s] cap change: before 0x%x, now 0x%x\n",
848*7249Sff224033 			    ieee80211_macaddr_sprintf(wh->i_addr2),
849*7249Sff224033 			    in->in_capinfo, scan.capinfo);
8503147Sxc151355 			/*
8513147Sxc151355 			 * NB:	we assume short preamble doesn't
8523147Sxc151355 			 *	change dynamically
8533147Sxc151355 			 */
8543147Sxc151355 			ieee80211_set_shortslottime(ic,
855*7249Sff224033 			    ic->ic_curmode == IEEE80211_MODE_11A ||
856*7249Sff224033 			    (scan.capinfo &
857*7249Sff224033 			    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,
870*7249Sff224033 			    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 
8823147Sxc151355 	if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
8833147Sxc151355 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, in->in_macaddr)) {
8843147Sxc151355 			/*
8853147Sxc151355 			 * Create a new entry in the neighbor table.
8863147Sxc151355 			 */
8873147Sxc151355 			in = ieee80211_add_neighbor(ic, wh, &scan);
8883147Sxc151355 		} else if (in->in_capinfo == 0) {
8893147Sxc151355 			/*
8903147Sxc151355 			 * Update faked node created on transmit.
8913147Sxc151355 			 * Note this also updates the tsf.
8923147Sxc151355 			 */
8933147Sxc151355 			ieee80211_init_neighbor(in, wh, &scan);
8943147Sxc151355 		} else {
8953147Sxc151355 			/*
8963147Sxc151355 			 * Record tsf for potential resync.
8973147Sxc151355 			 */
8983147Sxc151355 			bcopy(scan.tstamp, in->in_tstamp.data,
8993147Sxc151355 			    sizeof (in->in_tstamp));
9003147Sxc151355 		}
9013147Sxc151355 		if (in != NULL) {
9023147Sxc151355 			in->in_rssi = (uint8_t)rssi;
9033147Sxc151355 			in->in_rstamp = rstamp;
9043147Sxc151355 		}
9053147Sxc151355 	}
9063147Sxc151355 }
9073147Sxc151355 
9083147Sxc151355 /*
9093147Sxc151355  * Perform input processing for 802.11 management frames.
9103147Sxc151355  * It's the default ic_recv_mgmt callback function for the interface
9113147Sxc151355  * softc, ic. Tipically ic_recv_mgmt is called within ieee80211_input()
9123147Sxc151355  */
9133147Sxc151355 void
9143147Sxc151355 ieee80211_recv_mgmt(ieee80211com_t *ic, mblk_t *mp, struct ieee80211_node *in,
9153147Sxc151355     int subtype, int rssi, uint32_t rstamp)
9163147Sxc151355 {
9173147Sxc151355 	struct ieee80211_frame *wh;
9183147Sxc151355 	uint8_t *frm;		/* pointer to start of the frame */
9193147Sxc151355 	uint8_t *efrm;		/* pointer to end of the frame */
9203147Sxc151355 	uint8_t *ssid;
9213147Sxc151355 	uint8_t *rates;
9223147Sxc151355 	uint8_t *xrates;	/* extended rates */
9233147Sxc151355 	boolean_t allocbs = B_FALSE;
9243147Sxc151355 	uint8_t rate;
9253147Sxc151355 	uint16_t algo;		/* authentication algorithm */
9263147Sxc151355 	uint16_t seq;		/* sequence no */
9273147Sxc151355 	uint16_t status;
9283147Sxc151355 	uint16_t capinfo;
9293147Sxc151355 	uint16_t associd;	/* association ID */
9303147Sxc151355 
9313147Sxc151355 	IEEE80211_LOCK(ic);
9323147Sxc151355 	wh = (struct ieee80211_frame *)mp->b_rptr;
9333147Sxc151355 	frm = (uint8_t *)&wh[1];
9343147Sxc151355 	efrm = (uint8_t *)mp->b_wptr;
9353147Sxc151355 	switch (subtype) {
9363147Sxc151355 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
9373147Sxc151355 	case IEEE80211_FC0_SUBTYPE_BEACON:
9383147Sxc151355 		ieee80211_recv_beacon(ic, mp, in, subtype, rssi, rstamp);
9393147Sxc151355 		break;
9403147Sxc151355 
9413147Sxc151355 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
9423147Sxc151355 		if (ic->ic_opmode == IEEE80211_M_STA ||
9433147Sxc151355 		    ic->ic_state != IEEE80211_S_RUN ||
9443147Sxc151355 		    IEEE80211_IS_MULTICAST(wh->i_addr2)) {
9453147Sxc151355 			break;
9463147Sxc151355 		}
9473147Sxc151355 
9483147Sxc151355 		/*
9493147Sxc151355 		 * prreq frame format
9503147Sxc151355 		 *	[tlv] ssid
9513147Sxc151355 		 *	[tlv] supported rates
9523147Sxc151355 		 *	[tlv] extended supported rates
9533147Sxc151355 		 */
9543147Sxc151355 		ssid = rates = xrates = NULL;
9553147Sxc151355 		while (frm < efrm) {
956*7249Sff224033 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
957*7249Sff224033 			    frm[1], goto out);
9583147Sxc151355 			switch (*frm) {
9593147Sxc151355 			case IEEE80211_ELEMID_SSID:
9603147Sxc151355 				ssid = frm;
9613147Sxc151355 				break;
9623147Sxc151355 			case IEEE80211_ELEMID_RATES:
9633147Sxc151355 				rates = frm;
9643147Sxc151355 				break;
9653147Sxc151355 			case IEEE80211_ELEMID_XRATES:
9663147Sxc151355 				xrates = frm;
9673147Sxc151355 				break;
9683147Sxc151355 			}
9693147Sxc151355 			frm += frm[1] + 2;
9703147Sxc151355 		}
9713147Sxc151355 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, break);
9723147Sxc151355 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, break);
9733147Sxc151355 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid, break);
9743147Sxc151355 		if ((ic->ic_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
9753147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_INPUT,
976*7249Sff224033 			    "ieee80211_recv_mgmt: ignore %s, "
977*7249Sff224033 			    "no ssid with ssid suppression enabled",
978*7249Sff224033 			    IEEE80211_SUBTYPE_NAME(subtype));
9793147Sxc151355 			break;
9803147Sxc151355 		}
9813147Sxc151355 
9823147Sxc151355 		if (in == ic->ic_bss) {
9833147Sxc151355 			if (ic->ic_opmode != IEEE80211_M_IBSS) {
9843147Sxc151355 				in = ieee80211_tmp_node(ic, wh->i_addr2);
9853147Sxc151355 				allocbs = B_TRUE;
9863147Sxc151355 			} else if (!IEEE80211_ADDR_EQ(wh->i_addr2,
9873147Sxc151355 			    in->in_macaddr)) {
9883147Sxc151355 				/*
9893147Sxc151355 				 * Cannot tell if the sender is operating
9903147Sxc151355 				 * in ibss mode.  But we need a new node to
9913147Sxc151355 				 * send the response so blindly add them to the
9923147Sxc151355 				 * neighbor table.
9933147Sxc151355 				 */
9943147Sxc151355 				in = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
995*7249Sff224033 				    wh->i_addr2);
9963147Sxc151355 			}
9973147Sxc151355 			if (in == NULL)
9983147Sxc151355 				break;
9993147Sxc151355 		}
10003147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ASSOC, "ieee80211_recv_mgmt: "
1001*7249Sff224033 		    "[%s] recv probe req\n",
1002*7249Sff224033 		    ieee80211_macaddr_sprintf(wh->i_addr2));
10033147Sxc151355 		in->in_rssi = (uint8_t)rssi;
10043147Sxc151355 		in->in_rstamp = rstamp;
10053147Sxc151355 		/*
10063147Sxc151355 		 * Adjust and check station's rate list with device's
10073147Sxc151355 		 * supported rate.  Send back response if there is at
10083147Sxc151355 		 * least one rate or the fixed rate(if being set) is
10093147Sxc151355 		 * supported by both station and the device
10103147Sxc151355 		 */
10113147Sxc151355 		rate = ieee80211_setup_rates(in, rates, xrates,
1012*7249Sff224033 		    IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1013*7249Sff224033 		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
10143147Sxc151355 		if (rate & IEEE80211_RATE_BASIC) {
10153147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_XRATE, "ieee80211_recv_mgmt"
1016*7249Sff224033 			    "%s recv'd rate set invalid",
1017*7249Sff224033 			    IEEE80211_SUBTYPE_NAME(subtype));
10183147Sxc151355 		} else {
10193147Sxc151355 			IEEE80211_UNLOCK(ic);
10203147Sxc151355 			IEEE80211_SEND_MGMT(ic, in,
1021*7249Sff224033 			    IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
10223147Sxc151355 			IEEE80211_LOCK(ic);
10233147Sxc151355 		}
10243147Sxc151355 		if (allocbs) {
10253147Sxc151355 			/*
10263147Sxc151355 			 * Temporary node created just to send a
10273147Sxc151355 			 * response, reclaim immediately.
10283147Sxc151355 			 */
10293147Sxc151355 			ieee80211_free_node(in);
10303147Sxc151355 		}
10313147Sxc151355 		break;
10323147Sxc151355 
10333147Sxc151355 	case IEEE80211_FC0_SUBTYPE_AUTH:
10343147Sxc151355 		/*
10353147Sxc151355 		 * auth frame format
10363147Sxc151355 		 *	[2] algorithm
10373147Sxc151355 		 *	[2] sequence
10383147Sxc151355 		 *	[2] status
10393147Sxc151355 		 *	[tlv*] challenge
10403147Sxc151355 		 */
1041*7249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1042*7249Sff224033 		    IEEE80211_AUTH_ELEM_MIN, break);
10433147Sxc151355 		algo   = (*(uint16_t *)frm);
10443147Sxc151355 		seq    = (*(uint16_t *)(frm + 2));
10453147Sxc151355 		status = (*(uint16_t *)(frm + 4));
10463147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_recv_mgmt: "
1047*7249Sff224033 		    "[%s] recv auth frame with algorithm %d seq %d\n",
1048*7249Sff224033 		    ieee80211_macaddr_sprintf(wh->i_addr2), algo, seq);
10493147Sxc151355 
10503147Sxc151355 		if (ic->ic_flags & IEEE80211_F_COUNTERM) {
10513147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1052*7249Sff224033 			    "ieee80211_recv_mgmt: ignore auth, %s\n",
1053*7249Sff224033 			    "TKIP countermeasures enabled");
10543147Sxc151355 			break;
10553147Sxc151355 		}
10563147Sxc151355 		switch (algo) {
10573147Sxc151355 		case IEEE80211_AUTH_ALG_SHARED:
10583147Sxc151355 			ieee80211_auth_shared(ic, wh, frm + 6, efrm, in,
1059*7249Sff224033 			    seq, status);
10603147Sxc151355 			break;
10613147Sxc151355 		case IEEE80211_AUTH_ALG_OPEN:
10623147Sxc151355 			ieee80211_auth_open(ic, wh, in, seq, status);
10633147Sxc151355 			break;
10643147Sxc151355 		default:
10653147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_recv_mgmt: "
1066*7249Sff224033 			    "ignore auth, unsupported alg %d", algo);
10673147Sxc151355 			break;
10683147Sxc151355 		}
10693147Sxc151355 		break;
10703147Sxc151355 
10713147Sxc151355 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
10723147Sxc151355 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
10733147Sxc151355 		if (ic->ic_opmode != IEEE80211_M_STA ||
10743147Sxc151355 		    ic->ic_state != IEEE80211_S_ASSOC)
10753147Sxc151355 			break;
10763147Sxc151355 
10773147Sxc151355 		/*
10783147Sxc151355 		 * asresp frame format
10793147Sxc151355 		 *	[2] capability information
10803147Sxc151355 		 *	[2] status
10813147Sxc151355 		 *	[2] association ID
10823147Sxc151355 		 *	[tlv] supported rates
10833147Sxc151355 		 *	[tlv] extended supported rates
10843147Sxc151355 		 *	[tlv] WME
10853147Sxc151355 		 */
1086*7249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1087*7249Sff224033 		    IEEE80211_ASSOC_RESP_ELEM_MIN, break);
10883147Sxc151355 		in = ic->ic_bss;
10893147Sxc151355 		capinfo = (*(uint16_t *)frm);
10903147Sxc151355 		frm += 2;
10913147Sxc151355 		status = (*(uint16_t *)frm);
10923147Sxc151355 		frm += 2;
10933147Sxc151355 		if (status != 0) {
10943147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
1095*7249Sff224033 			    "assoc failed (reason %d)\n", status);
10963147Sxc151355 			in = ieee80211_find_node(&ic->ic_scan, wh->i_addr2);
10973147Sxc151355 			if (in != NULL) {
10983147Sxc151355 				in->in_fails++;
10993147Sxc151355 				ieee80211_free_node(in);
11003147Sxc151355 			}
11013147Sxc151355 			break;
11023147Sxc151355 		}
11033147Sxc151355 		associd = (*(uint16_t *)frm);
11043147Sxc151355 		frm += 2;
11053147Sxc151355 
11063147Sxc151355 		rates = xrates = NULL;
11073147Sxc151355 		while (frm < efrm) {
11083147Sxc151355 			/*
11093147Sxc151355 			 * Do not discard frames containing proprietary Agere
11103147Sxc151355 			 * elements 128 and 129, as the reported element length
11113147Sxc151355 			 * is often wrong. Skip rest of the frame, since we can
11123147Sxc151355 			 * not rely on the given element length making it
11133147Sxc151355 			 * impossible to know where the next element starts
11143147Sxc151355 			 */
11153147Sxc151355 			if ((*frm == IEEE80211_ELEMID_AGERE1) ||
11163147Sxc151355 			    (*frm == IEEE80211_ELEMID_AGERE2)) {
11173147Sxc151355 				frm = efrm;
11183147Sxc151355 				break;
11193147Sxc151355 			}
11203147Sxc151355 
1121*7249Sff224033 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1122*7249Sff224033 			    frm[1], goto out);
11233147Sxc151355 			switch (*frm) {
11243147Sxc151355 			case IEEE80211_ELEMID_RATES:
11253147Sxc151355 				rates = frm;
11263147Sxc151355 				break;
11273147Sxc151355 			case IEEE80211_ELEMID_XRATES:
11283147Sxc151355 				xrates = frm;
11293147Sxc151355 				break;
11303147Sxc151355 			}
11313147Sxc151355 			frm += frm[1] + 2;
11323147Sxc151355 		}
11333147Sxc151355 
11343147Sxc151355 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, break);
11353147Sxc151355 		/*
11363147Sxc151355 		 * Adjust and check AP's rate list with device's
11373147Sxc151355 		 * supported rate. Re-start scan if no rate is or the
11383147Sxc151355 		 * fixed rate(if being set) cannot be supported by
11393147Sxc151355 		 * either AP or the device.
11403147Sxc151355 		 */
11413147Sxc151355 		rate = ieee80211_setup_rates(in, rates, xrates,
1142*7249Sff224033 		    IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1143*7249Sff224033 		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
11443147Sxc151355 		if (rate & IEEE80211_RATE_BASIC) {
11453147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
1146*7249Sff224033 			    "assoc failed (rate set mismatch)\n");
11473147Sxc151355 			if (in != ic->ic_bss)
11483147Sxc151355 				in->in_fails++;
11493147Sxc151355 			IEEE80211_UNLOCK(ic);
11503147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
11513147Sxc151355 			return;
11523147Sxc151355 		}
11533147Sxc151355 
11543147Sxc151355 		in->in_capinfo = capinfo;
11553147Sxc151355 		in->in_associd = associd;
11563147Sxc151355 		in->in_flags &= ~IEEE80211_NODE_QOS;
11573147Sxc151355 		/*
11583147Sxc151355 		 * Configure state now that we are associated.
11593147Sxc151355 		 */
11603147Sxc151355 		if (ic->ic_curmode == IEEE80211_MODE_11A ||
11613147Sxc151355 		    (in->in_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
11623147Sxc151355 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
11633147Sxc151355 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
11643147Sxc151355 		} else {
11653147Sxc151355 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
11663147Sxc151355 			ic->ic_flags |= IEEE80211_F_USEBARKER;
11673147Sxc151355 		}
11683147Sxc151355 		ieee80211_set_shortslottime(ic,
1169*7249Sff224033 		    ic->ic_curmode == IEEE80211_MODE_11A ||
1170*7249Sff224033 		    (in->in_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
11713147Sxc151355 		/*
11723147Sxc151355 		 * Honor ERP protection.
11733147Sxc151355 		 *
11743147Sxc151355 		 * NB:	in_erp should zero for non-11g operation.
11753147Sxc151355 		 *	check ic_curmode anyway
11763147Sxc151355 		 */
11773147Sxc151355 		if (ic->ic_curmode == IEEE80211_MODE_11G &&
11783147Sxc151355 		    (in->in_erp & IEEE80211_ERP_USE_PROTECTION))
11793147Sxc151355 			ic->ic_flags |= IEEE80211_F_USEPROT;
11803147Sxc151355 		else
11813147Sxc151355 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
11823147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ASSOC,
1183*7249Sff224033 		    "assoc success: %s preamble, %s slot time%s%s\n",
1184*7249Sff224033 		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1185*7249Sff224033 		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1186*7249Sff224033 		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1187*7249Sff224033 		    in->in_flags & IEEE80211_NODE_QOS ? ", QoS" : "");
11883147Sxc151355 		IEEE80211_UNLOCK(ic);
11893147Sxc151355 		ieee80211_new_state(ic, IEEE80211_S_RUN, subtype);
11903147Sxc151355 		return;
11913147Sxc151355 
11923147Sxc151355 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
11933147Sxc151355 		if (ic->ic_state == IEEE80211_S_SCAN)
11943147Sxc151355 			break;
11953147Sxc151355 
11963147Sxc151355 		/*
11973147Sxc151355 		 * deauth frame format
11983147Sxc151355 		 *	[2] reason
11993147Sxc151355 		 */
1200*7249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm), 2, break);
12013147Sxc151355 		status = (*(uint16_t *)frm);
12023147Sxc151355 
12033147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH,
1204*7249Sff224033 		    "recv deauthenticate (reason %d)\n", status);
12053147Sxc151355 		switch (ic->ic_opmode) {
12063147Sxc151355 		case IEEE80211_M_STA:
12073147Sxc151355 			IEEE80211_UNLOCK(ic);
12083147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
1209*7249Sff224033 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
12103147Sxc151355 			return;
12113147Sxc151355 		default:
12123147Sxc151355 			break;
12133147Sxc151355 		}
12143147Sxc151355 		break;
12153147Sxc151355 
12163147Sxc151355 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
12173147Sxc151355 		if (ic->ic_state != IEEE80211_S_RUN &&
12183147Sxc151355 		    ic->ic_state != IEEE80211_S_ASSOC &&
12193147Sxc151355 		    ic->ic_state != IEEE80211_S_AUTH)
12203147Sxc151355 			break;
12213147Sxc151355 		/*
12223147Sxc151355 		 * disassoc frame format
12233147Sxc151355 		 *	[2] reason
12243147Sxc151355 		 */
1225*7249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm), 2, break);
12263147Sxc151355 		status = (*(uint16_t *)frm);
12273147Sxc151355 
12283147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ASSOC,
1229*7249Sff224033 		    "recv disassociate (reason %d)\n", status);
12303147Sxc151355 		switch (ic->ic_opmode) {
12313147Sxc151355 		case IEEE80211_M_STA:
12323147Sxc151355 			IEEE80211_UNLOCK(ic);
12333147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
1234*7249Sff224033 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
12353147Sxc151355 			return;
12363147Sxc151355 		default:
12373147Sxc151355 			break;
12383147Sxc151355 		}
12393147Sxc151355 		break;
12403147Sxc151355 
12413147Sxc151355 	default:
12423147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_recv_mgmt: "
1243*7249Sff224033 		    "subtype 0x%x not handled\n", subtype);
12443147Sxc151355 		break;
12453147Sxc151355 	} /* switch subtype */
12463147Sxc151355 out:
12473147Sxc151355 	IEEE80211_UNLOCK(ic);
12483147Sxc151355 }
1249