13147Sxc151355 /*
28594SFei.Feng@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
33147Sxc151355  * Use is subject to license terms.
43147Sxc151355  */
53147Sxc151355 
63147Sxc151355 /*
73147Sxc151355  * Copyright (c) 2001 Atsushi Onoe
88594SFei.Feng@Sun.COM  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
93147Sxc151355  * All rights reserved.
103147Sxc151355  *
113147Sxc151355  * Redistribution and use in source and binary forms, with or without
123147Sxc151355  * modification, are permitted provided that the following conditions
133147Sxc151355  * are met:
143147Sxc151355  * 1. Redistributions of source code must retain the above copyright
153147Sxc151355  *    notice, this list of conditions and the following disclaimer.
163147Sxc151355  * 2. Redistributions in binary form must reproduce the above copyright
173147Sxc151355  *    notice, this list of conditions and the following disclaimer in the
183147Sxc151355  *    documentation and/or other materials provided with the distribution.
193147Sxc151355  * 3. The name of the author may not be used to endorse or promote products
203147Sxc151355  *    derived from this software without specific prior written permission.
213147Sxc151355  *
223147Sxc151355  * Alternatively, this software may be distributed under the terms of the
233147Sxc151355  * GNU General Public License ("GPL") version 2 as published by the Free
243147Sxc151355  * Software Foundation.
253147Sxc151355  *
263147Sxc151355  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
273147Sxc151355  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
283147Sxc151355  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
293147Sxc151355  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
303147Sxc151355  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
313147Sxc151355  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
323147Sxc151355  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
333147Sxc151355  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
343147Sxc151355  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
353147Sxc151355  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
363147Sxc151355  */
373147Sxc151355 
383147Sxc151355 /*
393147Sxc151355  * Process received frame
403147Sxc151355  */
413147Sxc151355 
428275SEric Cheng #include <sys/mac_provider.h>
433147Sxc151355 #include <sys/byteorder.h>
447249Sff224033 #include <sys/strsun.h>
453147Sxc151355 #include "net80211_impl.h"
463147Sxc151355 
473147Sxc151355 static mblk_t *ieee80211_defrag(ieee80211com_t *, ieee80211_node_t *,
483147Sxc151355     mblk_t *, int);
493147Sxc151355 
503147Sxc151355 /*
513147Sxc151355  * Process a received frame.  The node associated with the sender
523147Sxc151355  * should be supplied.  If nothing was found in the node table then
533147Sxc151355  * the caller is assumed to supply a reference to ic_bss instead.
543147Sxc151355  * The RSSI and a timestamp are also supplied.  The RSSI data is used
553147Sxc151355  * during AP scanning to select a AP to associate with; it can have
563147Sxc151355  * any units so long as values have consistent units and higher values
573147Sxc151355  * mean ``better signal''.  The receive timestamp is currently not used
583147Sxc151355  * by the 802.11 layer.
593147Sxc151355  */
603147Sxc151355 int
613147Sxc151355 ieee80211_input(ieee80211com_t *ic, mblk_t *mp, struct ieee80211_node *in,
623147Sxc151355     int32_t rssi, uint32_t rstamp)
633147Sxc151355 {
643147Sxc151355 	struct ieee80211_frame *wh;
653147Sxc151355 	struct ieee80211_key *key;
663147Sxc151355 	uint8_t *bssid;
673147Sxc151355 	int hdrspace;
683147Sxc151355 	int len;
693147Sxc151355 	uint16_t rxseq;
703147Sxc151355 	uint8_t dir;
713147Sxc151355 	uint8_t type;
723147Sxc151355 	uint8_t subtype;
733147Sxc151355 	uint8_t tid;
74*10266SQuaker.Fang@Sun.COM 	uint8_t qos;
75*10266SQuaker.Fang@Sun.COM 
76*10266SQuaker.Fang@Sun.COM 	if (mp->b_flag & M_AMPDU) {
77*10266SQuaker.Fang@Sun.COM 		/*
78*10266SQuaker.Fang@Sun.COM 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
79*10266SQuaker.Fang@Sun.COM 		 * w/ M_AMPDU marked have already passed through here
80*10266SQuaker.Fang@Sun.COM 		 * but were received out of order and been held on the
81*10266SQuaker.Fang@Sun.COM 		 * reorder queue.  When resubmitted they are marked
82*10266SQuaker.Fang@Sun.COM 		 * with the M_AMPDU flag and we can bypass most of the
83*10266SQuaker.Fang@Sun.COM 		 * normal processing.
84*10266SQuaker.Fang@Sun.COM 		 */
85*10266SQuaker.Fang@Sun.COM 		IEEE80211_LOCK(ic);
86*10266SQuaker.Fang@Sun.COM 		wh = (struct ieee80211_frame *)mp->b_rptr;
87*10266SQuaker.Fang@Sun.COM 		type = IEEE80211_FC0_TYPE_DATA;
88*10266SQuaker.Fang@Sun.COM 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
89*10266SQuaker.Fang@Sun.COM 		subtype = IEEE80211_FC0_SUBTYPE_QOS;
90*10266SQuaker.Fang@Sun.COM 		hdrspace = ieee80211_hdrspace(ic, wh);	/* optimize */
91*10266SQuaker.Fang@Sun.COM 		/* clear driver/net80211 flags before passing up */
92*10266SQuaker.Fang@Sun.COM 		mp->b_flag &= ~M_AMPDU;
93*10266SQuaker.Fang@Sun.COM 		goto resubmit_ampdu;
94*10266SQuaker.Fang@Sun.COM 	}
953147Sxc151355 
963147Sxc151355 	ASSERT(in != NULL);
978594SFei.Feng@Sun.COM 	in->in_inact = in->in_inact_reload;
983147Sxc151355 	type = (uint8_t)-1;		/* undefined */
997249Sff224033 	len = MBLKL(mp);
1003147Sxc151355 	if (len < sizeof (struct ieee80211_frame_min)) {
1013147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
1027249Sff224033 		    "too short (1): len %u", len);
1033147Sxc151355 		goto out;
1043147Sxc151355 	}
1053147Sxc151355 	/*
1063147Sxc151355 	 * Bit of a cheat here, we use a pointer for a 3-address
1073147Sxc151355 	 * frame format but don't reference fields past outside
1083147Sxc151355 	 * ieee80211_frame_min w/o first validating the data is
1093147Sxc151355 	 * present.
1103147Sxc151355 	 */
1113147Sxc151355 	wh = (struct ieee80211_frame *)mp->b_rptr;
1123147Sxc151355 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1133147Sxc151355 	    IEEE80211_FC0_VERSION_0) {
1143147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
1157249Sff224033 		    "discard pkt with wrong version %x", wh->i_fc[0]);
1163147Sxc151355 		goto out;
1173147Sxc151355 	}
1183147Sxc151355 
1193147Sxc151355 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1203147Sxc151355 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1213147Sxc151355 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1223147Sxc151355 
1233147Sxc151355 	IEEE80211_LOCK(ic);
1243147Sxc151355 	if (!(ic->ic_flags & IEEE80211_F_SCAN)) {
1253147Sxc151355 		switch (ic->ic_opmode) {
1263147Sxc151355 		case IEEE80211_M_STA:
1273147Sxc151355 			bssid = wh->i_addr2;
1283147Sxc151355 			if (!IEEE80211_ADDR_EQ(bssid, in->in_bssid))
1293147Sxc151355 				goto out_exit_mutex;
1303147Sxc151355 			break;
1313147Sxc151355 		case IEEE80211_M_IBSS:
1323147Sxc151355 		case IEEE80211_M_AHDEMO:
1333147Sxc151355 			if (dir != IEEE80211_FC1_DIR_NODS) {
1343147Sxc151355 				bssid = wh->i_addr1;
1353147Sxc151355 			} else if (type == IEEE80211_FC0_TYPE_CTL) {
1363147Sxc151355 				bssid = wh->i_addr1;
1373147Sxc151355 			} else {
1383147Sxc151355 				if (len < sizeof (struct ieee80211_frame)) {
1393147Sxc151355 					ieee80211_dbg(IEEE80211_MSG_ANY,
1407249Sff224033 					    "ieee80211_input: too short(2):"
1417249Sff224033 					    "len %u\n", len);
1423147Sxc151355 					goto out_exit_mutex;
1433147Sxc151355 				}
1443147Sxc151355 				bssid = wh->i_addr3;
1453147Sxc151355 			}
1463147Sxc151355 			if (type != IEEE80211_FC0_TYPE_DATA)
1473147Sxc151355 				break;
1483147Sxc151355 			/*
1493147Sxc151355 			 * Data frame, validate the bssid.
1503147Sxc151355 			 */
1513147Sxc151355 			if (!IEEE80211_ADDR_EQ(bssid, ic->ic_bss->in_bssid) &&
1523147Sxc151355 			    !IEEE80211_ADDR_EQ(bssid, wifi_bcastaddr)) {
1533147Sxc151355 				/* not interested in */
1543147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
1557249Sff224033 				    "ieee80211_input: not to bss %s\n",
1567249Sff224033 				    ieee80211_macaddr_sprintf(bssid));
1573147Sxc151355 				goto out_exit_mutex;
1583147Sxc151355 			}
1593147Sxc151355 			/*
1603147Sxc151355 			 * For adhoc mode we cons up a node when it doesn't
1613147Sxc151355 			 * exist. This should probably done after an ACL check.
1623147Sxc151355 			 */
1633147Sxc151355 			if (in == ic->ic_bss &&
1643147Sxc151355 			    ic->ic_opmode != IEEE80211_M_HOSTAP &&
1653147Sxc151355 			    !IEEE80211_ADDR_EQ(wh->i_addr2, in->in_macaddr)) {
1663147Sxc151355 				/*
1673147Sxc151355 				 * Fake up a node for this newly
1683147Sxc151355 				 * discovered member of the IBSS.
1693147Sxc151355 				 */
1703147Sxc151355 				in = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
1717249Sff224033 				    wh->i_addr2);
1723147Sxc151355 				if (in == NULL) {
1733147Sxc151355 					/* NB: stat kept for alloc failure */
1743147Sxc151355 					goto out_exit_mutex;
1753147Sxc151355 				}
1763147Sxc151355 			}
1773147Sxc151355 			break;
1783147Sxc151355 		default:
1793147Sxc151355 			goto out_exit_mutex;
1803147Sxc151355 		}
1813147Sxc151355 		in->in_rssi = (uint8_t)rssi;
1823147Sxc151355 		in->in_rstamp = rstamp;
1833147Sxc151355 		if (!(type & IEEE80211_FC0_TYPE_CTL)) {
184*10266SQuaker.Fang@Sun.COM 			if (IEEE80211_QOS_HAS_SEQ(wh)) {
185*10266SQuaker.Fang@Sun.COM 				tid = ((struct ieee80211_qosframe *)wh)->
186*10266SQuaker.Fang@Sun.COM 				    i_qos[0] & IEEE80211_QOS_TID;
187*10266SQuaker.Fang@Sun.COM 				if (TID_TO_WME_AC(tid) >= WME_AC_VI)
188*10266SQuaker.Fang@Sun.COM 					ic->ic_wme.wme_hipri_traffic++;
189*10266SQuaker.Fang@Sun.COM 				tid++;
190*10266SQuaker.Fang@Sun.COM 			} else {
191*10266SQuaker.Fang@Sun.COM 				tid = IEEE80211_NONQOS_TID;
192*10266SQuaker.Fang@Sun.COM 			}
1939327SFei.Feng@Sun.COM 			rxseq = LE_16(*(uint16_t *)wh->i_seq);
194*10266SQuaker.Fang@Sun.COM 			if ((in->in_flags & IEEE80211_NODE_HT) == 0 &&
195*10266SQuaker.Fang@Sun.COM 			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
1963147Sxc151355 			    (rxseq - in->in_rxseqs[tid]) <= 0) {
1973147Sxc151355 				/* duplicate, discard */
1983147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
1997249Sff224033 				    "ieee80211_input: duplicate",
2007249Sff224033 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
2017249Sff224033 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
2027249Sff224033 				    in->in_rxseqs[tid] >>
2037249Sff224033 				    IEEE80211_SEQ_SEQ_SHIFT,
2047249Sff224033 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
2057249Sff224033 				    in->in_rxseqs[tid] &
2067249Sff224033 				    IEEE80211_SEQ_FRAG_MASK,
2077249Sff224033 				    tid);
2083147Sxc151355 				ic->ic_stats.is_rx_dups++;
2093147Sxc151355 				goto out_exit_mutex;
2103147Sxc151355 			}
2113147Sxc151355 			in->in_rxseqs[tid] = rxseq;
2123147Sxc151355 		}
2133147Sxc151355 	}
2143147Sxc151355 
2153147Sxc151355 	switch (type) {
2163147Sxc151355 	case IEEE80211_FC0_TYPE_DATA:
217*10266SQuaker.Fang@Sun.COM 		hdrspace = ieee80211_hdrspace(ic, wh);
2183147Sxc151355 		if (len < hdrspace) {
2193147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
2207249Sff224033 			    "data too short: expecting %u", hdrspace);
2213147Sxc151355 			goto out_exit_mutex;
2223147Sxc151355 		}
2233147Sxc151355 		switch (ic->ic_opmode) {
2243147Sxc151355 		case IEEE80211_M_STA:
2253147Sxc151355 			if (dir != IEEE80211_FC1_DIR_FROMDS) {
2263147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
2277249Sff224033 				    "ieee80211_input: data ",
2287249Sff224033 				    "unknown dir 0x%x", dir);
2293147Sxc151355 				goto out_exit_mutex;
2303147Sxc151355 			}
2313147Sxc151355 			if (IEEE80211_IS_MULTICAST(wh->i_addr1) &&
2323147Sxc151355 			    IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_macaddr)) {
2333147Sxc151355 				/*
2343147Sxc151355 				 * In IEEE802.11 network, multicast packet
2353147Sxc151355 				 * sent from me is broadcasted from AP.
2363147Sxc151355 				 * It should be silently discarded for
2373147Sxc151355 				 * SIMPLEX interface.
2383147Sxc151355 				 */
2393147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
2407249Sff224033 				    "ieee80211_input: multicast echo\n");
2413147Sxc151355 				goto out_exit_mutex;
2423147Sxc151355 			}
2433147Sxc151355 			break;
2443147Sxc151355 		case IEEE80211_M_IBSS:
2453147Sxc151355 		case IEEE80211_M_AHDEMO:
2463147Sxc151355 			if (dir != IEEE80211_FC1_DIR_NODS) {
2473147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
2487249Sff224033 				    "ieee80211_input: unknown dir 0x%x",
2497249Sff224033 				    dir);
2503147Sxc151355 				goto out_exit_mutex;
2513147Sxc151355 			}
2523147Sxc151355 			break;
2533147Sxc151355 		default:
2543147Sxc151355 			ieee80211_err("ieee80211_input: "
2557249Sff224033 			    "receive data, unknown opmode %u, skip\n",
2567249Sff224033 			    ic->ic_opmode);
2573147Sxc151355 			goto out_exit_mutex;
2583147Sxc151355 		}
2593147Sxc151355 
2603147Sxc151355 		/*
261*10266SQuaker.Fang@Sun.COM 		 * Handle A-MPDU re-ordering.  The station must be
262*10266SQuaker.Fang@Sun.COM 		 * associated and negotiated HT.  The frame must be
263*10266SQuaker.Fang@Sun.COM 		 * a QoS frame (not QoS null data) and not previously
264*10266SQuaker.Fang@Sun.COM 		 * processed for A-MPDU re-ordering.  If the frame is
265*10266SQuaker.Fang@Sun.COM 		 * to be processed directly then ieee80211_ampdu_reorder
266*10266SQuaker.Fang@Sun.COM 		 * will return 0; otherwise it has consumed the mbuf
267*10266SQuaker.Fang@Sun.COM 		 * and we should do nothing more with it.
268*10266SQuaker.Fang@Sun.COM 		 */
269*10266SQuaker.Fang@Sun.COM 		if ((in->in_flags & IEEE80211_NODE_HT) &&
270*10266SQuaker.Fang@Sun.COM 		    (subtype == IEEE80211_FC0_SUBTYPE_QOS)) {
271*10266SQuaker.Fang@Sun.COM 			IEEE80211_UNLOCK(ic);
272*10266SQuaker.Fang@Sun.COM 			if (ieee80211_ampdu_reorder(in, mp) != 0) {
273*10266SQuaker.Fang@Sun.COM 				mp = NULL;	/* CONSUMED */
274*10266SQuaker.Fang@Sun.COM 				goto out;
275*10266SQuaker.Fang@Sun.COM 			}
276*10266SQuaker.Fang@Sun.COM 			IEEE80211_LOCK(ic);
277*10266SQuaker.Fang@Sun.COM 		}
278*10266SQuaker.Fang@Sun.COM 	resubmit_ampdu:
279*10266SQuaker.Fang@Sun.COM 
280*10266SQuaker.Fang@Sun.COM 		/*
2813147Sxc151355 		 * Handle privacy requirements.
2823147Sxc151355 		 */
2833147Sxc151355 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2843147Sxc151355 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
2853147Sxc151355 				/*
2863147Sxc151355 				 * Discard encrypted frames when privacy off.
2873147Sxc151355 				 */
2883147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
2897249Sff224033 				    "ieee80211_input: ""WEP PRIVACY off");
2903147Sxc151355 				ic->ic_stats.is_wep_errors++;
2913147Sxc151355 				goto out_exit_mutex;
2923147Sxc151355 			}
2933147Sxc151355 			key = ieee80211_crypto_decap(ic, mp, hdrspace);
2943147Sxc151355 			if (key == NULL) {
2953147Sxc151355 				/* NB: stats+msgs handled in crypto_decap */
2963147Sxc151355 				ic->ic_stats.is_wep_errors++;
2973147Sxc151355 				goto out_exit_mutex;
2983147Sxc151355 			}
2993147Sxc151355 			wh = (struct ieee80211_frame *)mp->b_rptr;
3003147Sxc151355 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3013147Sxc151355 		} else {
3023147Sxc151355 			key = NULL;
3033147Sxc151355 		}
3043147Sxc151355 
3053147Sxc151355 		/*
306*10266SQuaker.Fang@Sun.COM 		 * Save QoS bits for use below--before we strip the header.
307*10266SQuaker.Fang@Sun.COM 		 */
308*10266SQuaker.Fang@Sun.COM 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
309*10266SQuaker.Fang@Sun.COM 			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
310*10266SQuaker.Fang@Sun.COM 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
311*10266SQuaker.Fang@Sun.COM 			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
312*10266SQuaker.Fang@Sun.COM 		} else {
313*10266SQuaker.Fang@Sun.COM 			qos = 0;
314*10266SQuaker.Fang@Sun.COM 		}
315*10266SQuaker.Fang@Sun.COM 
316*10266SQuaker.Fang@Sun.COM 		/*
3173147Sxc151355 		 * Next up, any fragmentation
3183147Sxc151355 		 */
3193147Sxc151355 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3203147Sxc151355 			mp = ieee80211_defrag(ic, in, mp, hdrspace);
3213147Sxc151355 			if (mp == NULL) {
3223147Sxc151355 				/* Fragment dropped or frame not complete yet */
3233147Sxc151355 				goto out_exit_mutex;
3243147Sxc151355 			}
3253147Sxc151355 		}
3263147Sxc151355 		wh = NULL;	/* no longer valid, catch any uses */
3273147Sxc151355 
3283147Sxc151355 		/*
3293147Sxc151355 		 * Next strip any MSDU crypto bits.
3303147Sxc151355 		 */
3313147Sxc151355 		if (key != NULL && !ieee80211_crypto_demic(ic, key, mp, 0)) {
3323147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_INPUT, "ieee80211_input: "
3337249Sff224033 			    "data demic error\n");
3343147Sxc151355 			goto out_exit_mutex;
3353147Sxc151355 		}
3363147Sxc151355 
337*10266SQuaker.Fang@Sun.COM 		if (qos & IEEE80211_QOS_AMSDU) {
338*10266SQuaker.Fang@Sun.COM 			ieee80211_dbg(IEEE80211_MSG_INPUT | IEEE80211_MSG_HT,
339*10266SQuaker.Fang@Sun.COM 			    "ieee80211_input: QOS_AMSDU (%x)\n", qos);
340*10266SQuaker.Fang@Sun.COM 
341*10266SQuaker.Fang@Sun.COM 			mp = ieee80211_decap_amsdu(in, mp);
342*10266SQuaker.Fang@Sun.COM 			if (mp == NULL)		/* MSDU processed by HT */
343*10266SQuaker.Fang@Sun.COM 				goto out_exit_mutex;
344*10266SQuaker.Fang@Sun.COM 		}
345*10266SQuaker.Fang@Sun.COM 
3463147Sxc151355 		ic->ic_stats.is_rx_frags++;
3473147Sxc151355 		ic->ic_stats.is_rx_bytes += len;
3483147Sxc151355 		IEEE80211_UNLOCK(ic);
3493147Sxc151355 		mac_rx(ic->ic_mach, NULL, mp);
3503147Sxc151355 		return (IEEE80211_FC0_TYPE_DATA);
3513147Sxc151355 
3523147Sxc151355 	case IEEE80211_FC0_TYPE_MGT:
3533147Sxc151355 		if (dir != IEEE80211_FC1_DIR_NODS)
3543147Sxc151355 			goto out_exit_mutex;
3553147Sxc151355 		if (len < sizeof (struct ieee80211_frame))
3563147Sxc151355 			goto out_exit_mutex;
3573147Sxc151355 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
3583147Sxc151355 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
3593147Sxc151355 				/*
3603147Sxc151355 				 * Only shared key auth frames with a challenge
3613147Sxc151355 				 * should be encrypted, discard all others.
3623147Sxc151355 				 */
3633147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
3647249Sff224033 				    "ieee80211_input: "
3657249Sff224033 				    "%s WEP set but not permitted",
3667249Sff224033 				    IEEE80211_SUBTYPE_NAME(subtype));
3673147Sxc151355 				ic->ic_stats.is_wep_errors++;
3683147Sxc151355 				goto out_exit_mutex;
3693147Sxc151355 			}
3703147Sxc151355 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
3713147Sxc151355 				/*
3723147Sxc151355 				 * Discard encrypted frames when privacy off.
3733147Sxc151355 				 */
3743147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_INPUT,
3757249Sff224033 				    "ieee80211_input: "
3767249Sff224033 				    "mgt WEP set but PRIVACY off");
3773147Sxc151355 				ic->ic_stats.is_wep_errors++;
3783147Sxc151355 				goto out_exit_mutex;
3793147Sxc151355 			}
380*10266SQuaker.Fang@Sun.COM 			hdrspace = ieee80211_hdrspace(ic, wh);
3813147Sxc151355 			key = ieee80211_crypto_decap(ic, mp, hdrspace);
3823147Sxc151355 			if (key == NULL) {
3833147Sxc151355 				/* NB: stats+msgs handled in crypto_decap */
3843147Sxc151355 				goto out_exit_mutex;
3853147Sxc151355 			}
3863147Sxc151355 			wh = (struct ieee80211_frame *)mp->b_rptr;
3873147Sxc151355 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3883147Sxc151355 		}
3893147Sxc151355 		IEEE80211_UNLOCK(ic);
3903147Sxc151355 		ic->ic_recv_mgmt(ic, mp, in, subtype, rssi, rstamp);
3913147Sxc151355 		goto out;
3923147Sxc151355 
3933147Sxc151355 	case IEEE80211_FC0_TYPE_CTL:
394*10266SQuaker.Fang@Sun.COM 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
395*10266SQuaker.Fang@Sun.COM 			switch (subtype) {
396*10266SQuaker.Fang@Sun.COM 			case IEEE80211_FC0_SUBTYPE_BAR:
397*10266SQuaker.Fang@Sun.COM 				ieee80211_recv_bar(in, mp);
398*10266SQuaker.Fang@Sun.COM 				break;
399*10266SQuaker.Fang@Sun.COM 			}
400*10266SQuaker.Fang@Sun.COM 		}
401*10266SQuaker.Fang@Sun.COM 		break;
402*10266SQuaker.Fang@Sun.COM 
4033147Sxc151355 	default:
4043147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
4057249Sff224033 		    "bad frame type 0x%x", type);
4063147Sxc151355 		/* should not come here */
4073147Sxc151355 		break;
4083147Sxc151355 	}
4093147Sxc151355 out_exit_mutex:
4103147Sxc151355 	IEEE80211_UNLOCK(ic);
4113147Sxc151355 out:
4123147Sxc151355 	if (mp != NULL)
4133147Sxc151355 		freemsg(mp);
4143147Sxc151355 
4153147Sxc151355 	return (type);
4163147Sxc151355 }
4173147Sxc151355 
4183147Sxc151355 /*
4193147Sxc151355  * This function reassemble fragments.
4203147Sxc151355  * More fragments bit in the frame control means the packet is fragmented.
4213147Sxc151355  * While the sequence control field consists of 4-bit fragment number
4223147Sxc151355  * field and a 12-bit sequence number field.
4233147Sxc151355  */
4243147Sxc151355 /* ARGSUSED */
4253147Sxc151355 static mblk_t *
4263147Sxc151355 ieee80211_defrag(ieee80211com_t *ic, struct ieee80211_node *in, mblk_t *mp,
4273147Sxc151355     int hdrspace)
4283147Sxc151355 {
4293147Sxc151355 	struct ieee80211_frame *wh = (struct ieee80211_frame *)mp->b_rptr;
4303147Sxc151355 	struct ieee80211_frame *lwh;
4313147Sxc151355 	mblk_t *mfrag;
4323147Sxc151355 	uint16_t rxseq;
4333147Sxc151355 	uint8_t fragno;
4343147Sxc151355 	uint8_t more_frag;
4353147Sxc151355 
4363147Sxc151355 	ASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1));
4373147Sxc151355 	more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG;
4383147Sxc151355 	rxseq = LE_16(*(uint16_t *)wh->i_seq);
4393147Sxc151355 	fragno = rxseq & IEEE80211_SEQ_FRAG_MASK;
4403147Sxc151355 
4413147Sxc151355 	/* Quick way out, if there's nothing to defragment */
4423147Sxc151355 	if (!more_frag && fragno == 0 && in->in_rxfrag == NULL)
4433147Sxc151355 		return (mp);
4443147Sxc151355 
4453147Sxc151355 	/*
4463147Sxc151355 	 * Remove frag to insure it doesn't get reaped by timer.
4473147Sxc151355 	 */
4483147Sxc151355 	if (in->in_table == NULL) {
4493147Sxc151355 		/*
4503147Sxc151355 		 * Should never happen.  If the node is orphaned (not in
4513147Sxc151355 		 * the table) then input packets should not reach here.
4523147Sxc151355 		 * Otherwise, a concurrent request that yanks the table
4533147Sxc151355 		 * should be blocked by other interlocking and/or by first
4543147Sxc151355 		 * shutting the driver down.  Regardless, be defensive
4553147Sxc151355 		 * here and just bail
4563147Sxc151355 		 */
4573147Sxc151355 		freemsg(mp);
4583147Sxc151355 		return (NULL);
4593147Sxc151355 	}
4603147Sxc151355 	IEEE80211_NODE_LOCK(in->in_table);
4613147Sxc151355 	mfrag = in->in_rxfrag;
4623147Sxc151355 	in->in_rxfrag = NULL;
4633147Sxc151355 	IEEE80211_NODE_UNLOCK(in->in_table);
4643147Sxc151355 
4653147Sxc151355 	/*
4663147Sxc151355 	 * Validate new fragment is in order and
4673147Sxc151355 	 * related to the previous ones.
4683147Sxc151355 	 */
4693147Sxc151355 	if (mfrag != NULL) {
4703147Sxc151355 		uint16_t last_rxseq;
4713147Sxc151355 
4723147Sxc151355 		lwh = (struct ieee80211_frame *)mfrag->b_rptr;
4733147Sxc151355 		last_rxseq = LE_16(*(uint16_t *)lwh->i_seq);
4743147Sxc151355 		/*
4753147Sxc151355 		 * Sequence control field contains 12-bit sequence no
4763147Sxc151355 		 * and 4-bit fragment number. For fragemnts, the
4773147Sxc151355 		 * sequence no is not changed.
4783147Sxc151355 		 * NB: check seq # and frag together
4793147Sxc151355 		 */
4803147Sxc151355 		if (rxseq != last_rxseq + 1 ||
4813147Sxc151355 		    !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) ||
4823147Sxc151355 		    !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) {
4833147Sxc151355 			/*
4843147Sxc151355 			 * Unrelated fragment or no space for it,
4853147Sxc151355 			 * clear current fragments.
4863147Sxc151355 			 */
4873147Sxc151355 			freemsg(mfrag);
4883147Sxc151355 			mfrag = NULL;
4893147Sxc151355 		}
4903147Sxc151355 	}
4913147Sxc151355 
4923147Sxc151355 	if (mfrag == NULL) {
4933147Sxc151355 		if (fragno != 0) {	/* !first fragment, discard */
4943147Sxc151355 			freemsg(mp);
4953147Sxc151355 			return (NULL);
4963147Sxc151355 		}
4973147Sxc151355 		mfrag = mp;
4983147Sxc151355 	} else {			/* concatenate */
4993147Sxc151355 		(void) adjmsg(mp, hdrspace);
5003147Sxc151355 		linkb(mfrag, mp);
5013147Sxc151355 		/* track last seqnum and fragno */
5023147Sxc151355 		lwh = (struct ieee80211_frame *)mfrag->b_rptr;
5033147Sxc151355 		*(uint16_t *)lwh->i_seq = *(uint16_t *)wh->i_seq;
5043147Sxc151355 	}
5053147Sxc151355 	if (more_frag != 0) {		/* more to come, save */
5063147Sxc151355 		in->in_rxfragstamp = ddi_get_lbolt();
5073147Sxc151355 		in->in_rxfrag = mfrag;
5083147Sxc151355 		mfrag = NULL;
5093147Sxc151355 	}
5103147Sxc151355 
5113147Sxc151355 	return (mfrag);
5123147Sxc151355 }
5133147Sxc151355 
5143147Sxc151355 /*
5153147Sxc151355  * Install received rate set information in the node's state block.
5163147Sxc151355  */
5173147Sxc151355 int
5183147Sxc151355 ieee80211_setup_rates(struct ieee80211_node *in, const uint8_t *rates,
5193147Sxc151355     const uint8_t *xrates, int flags)
5203147Sxc151355 {
5213147Sxc151355 	struct ieee80211_rateset *rs = &in->in_rates;
5223147Sxc151355 
5233147Sxc151355 	bzero(rs, sizeof (*rs));
5243147Sxc151355 	rs->ir_nrates = rates[1];
5253147Sxc151355 	/* skip 1 byte element ID and 1 byte length */
5263147Sxc151355 	bcopy(rates + 2, rs->ir_rates, rs->ir_nrates);
5273147Sxc151355 	if (xrates != NULL) {
5283147Sxc151355 		uint8_t nxrates;
5293147Sxc151355 
5303147Sxc151355 		/*
5313147Sxc151355 		 * Tack on 11g extended supported rate element.
5323147Sxc151355 		 */
5333147Sxc151355 		nxrates = xrates[1];
5343147Sxc151355 		if (rs->ir_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
5353147Sxc151355 			nxrates = IEEE80211_RATE_MAXSIZE - rs->ir_nrates;
5363147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_XRATE,
5377249Sff224033 			    "ieee80211_setup_rates: %s",
5387249Sff224033 			    "[%s] extended rate set too large;"
5397249Sff224033 			    " only using %u of %u rates\n",
5407249Sff224033 			    ieee80211_macaddr_sprintf(in->in_macaddr),
5417249Sff224033 			    nxrates, xrates[1]);
5423147Sxc151355 		}
5433147Sxc151355 		bcopy(xrates + 2, rs->ir_rates + rs->ir_nrates, nxrates);
5443147Sxc151355 		rs->ir_nrates += nxrates;
5453147Sxc151355 	}
546*10266SQuaker.Fang@Sun.COM 	return (ieee80211_fix_rate(in, &in->in_rates, flags));
5473147Sxc151355 }
5483147Sxc151355 
5493147Sxc151355 /*
5503147Sxc151355  * Process open-system authentication response frame and start
5513147Sxc151355  * association if the authentication request is accepted.
5523147Sxc151355  */
5533147Sxc151355 static void
5543147Sxc151355 ieee80211_auth_open(ieee80211com_t *ic, struct ieee80211_frame *wh,
5553147Sxc151355     struct ieee80211_node *in, uint16_t seq, uint16_t status)
5563147Sxc151355 {
5573147Sxc151355 	IEEE80211_LOCK_ASSERT(ic);
5583147Sxc151355 	if (in->in_authmode == IEEE80211_AUTH_SHARED) {
5593147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH,
5607249Sff224033 		    "open auth: bad sta auth mode %u", in->in_authmode);
5613147Sxc151355 		return;
5623147Sxc151355 	}
5633147Sxc151355 	if (ic->ic_opmode == IEEE80211_M_STA) {
5643147Sxc151355 		if (ic->ic_state != IEEE80211_S_AUTH ||
5653147Sxc151355 		    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
5663147Sxc151355 			return;
5673147Sxc151355 		}
5683147Sxc151355 		IEEE80211_UNLOCK(ic);
5693147Sxc151355 		if (status != 0) {
5703147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
5717249Sff224033 			    "open auth failed (reason %d)\n", status);
5723147Sxc151355 			if (in != ic->ic_bss)
5733147Sxc151355 				in->in_fails++;
5743147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
5753147Sxc151355 		} else {
5763147Sxc151355 			/* i_fc[0] - frame control's type & subtype field */
5773147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
5787249Sff224033 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
5793147Sxc151355 		}
5803147Sxc151355 		IEEE80211_LOCK(ic);
5813147Sxc151355 	} else {
5823147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_auth_open: "
5837249Sff224033 		    "bad operating mode %u", ic->ic_opmode);
5843147Sxc151355 	}
5853147Sxc151355 }
5863147Sxc151355 
5873147Sxc151355 /*
5883147Sxc151355  * Allocate challenge text for use by shared-key authentication
5893147Sxc151355  * Return B_TRUE on success, B_FALST otherwise.
5903147Sxc151355  */
5913147Sxc151355 static boolean_t
5923147Sxc151355 ieee80211_alloc_challenge(struct ieee80211_node *in)
5933147Sxc151355 {
5943147Sxc151355 	if (in->in_challenge == NULL) {
5953147Sxc151355 		in->in_challenge = kmem_alloc(IEEE80211_CHALLENGE_LEN,
5967249Sff224033 		    KM_NOSLEEP);
5973147Sxc151355 	}
5983147Sxc151355 	if (in->in_challenge == NULL) {
5993147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
6007249Sff224033 		    "[%s] shared key challenge alloc failed\n",
6017249Sff224033 		    ieee80211_macaddr_sprintf(in->in_macaddr));
6023147Sxc151355 	}
6033147Sxc151355 	return (in->in_challenge != NULL);
6043147Sxc151355 }
6053147Sxc151355 
6063147Sxc151355 /*
6073147Sxc151355  * Process shared-key authentication response frames. If authentication
6083147Sxc151355  * succeeds, start association; otherwise, restart scan.
6093147Sxc151355  */
6103147Sxc151355 static void
6113147Sxc151355 ieee80211_auth_shared(ieee80211com_t *ic, struct ieee80211_frame *wh,
6123147Sxc151355     uint8_t *frm, uint8_t *efrm, struct ieee80211_node *in, uint16_t seq,
6133147Sxc151355     uint16_t status)
6143147Sxc151355 {
6153147Sxc151355 	uint8_t *challenge;
6163147Sxc151355 
6173147Sxc151355 	/*
6183147Sxc151355 	 * Pre-shared key authentication is evil; accept
6193147Sxc151355 	 * it only if explicitly configured (it is supported
6203147Sxc151355 	 * mainly for compatibility with clients like OS X).
6213147Sxc151355 	 */
6223147Sxc151355 	IEEE80211_LOCK_ASSERT(ic);
6233147Sxc151355 	if (in->in_authmode != IEEE80211_AUTH_AUTO &&
6243147Sxc151355 	    in->in_authmode != IEEE80211_AUTH_SHARED) {
6253147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_auth_shared: "
6267249Sff224033 		    "bad sta auth mode %u", in->in_authmode);
6273147Sxc151355 		goto bad;
6283147Sxc151355 	}
6293147Sxc151355 
6303147Sxc151355 	challenge = NULL;
6313147Sxc151355 	if (frm + 1 < efrm) {
6323147Sxc151355 		/*
6333147Sxc151355 		 * Challenge text information element
6343147Sxc151355 		 * frm[0] - element ID
6353147Sxc151355 		 * frm[1] - length
6363147Sxc151355 		 * frm[2]... - challenge text
6373147Sxc151355 		 */
6387249Sff224033 		if ((frm[1] + 2) > (_PTRDIFF(efrm, frm))) {
6393147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH,
6407249Sff224033 			    "ieee80211_auth_shared: ie %d%d too long\n",
6417249Sff224033 			    frm[0], (frm[1] + 2) - (_PTRDIFF(efrm, frm)));
6423147Sxc151355 			goto bad;
6433147Sxc151355 		}
6443147Sxc151355 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
6453147Sxc151355 			challenge = frm;
6463147Sxc151355 		frm += frm[1] + 2;
6473147Sxc151355 	}
6483147Sxc151355 	switch (seq) {
6493147Sxc151355 	case IEEE80211_AUTH_SHARED_CHALLENGE:
6503147Sxc151355 	case IEEE80211_AUTH_SHARED_RESPONSE:
6513147Sxc151355 		if (challenge == NULL) {
6523147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH,
6537249Sff224033 			    "ieee80211_auth_shared: no challenge\n");
6543147Sxc151355 			goto bad;
6553147Sxc151355 		}
6563147Sxc151355 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
6573147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH,
6587249Sff224033 			    "ieee80211_auth_shared: bad challenge len %d\n",
6597249Sff224033 			    challenge[1]);
6603147Sxc151355 			goto bad;
6613147Sxc151355 		}
6623147Sxc151355 	default:
6633147Sxc151355 		break;
6643147Sxc151355 	}
6653147Sxc151355 	switch (ic->ic_opmode) {
6663147Sxc151355 	case IEEE80211_M_STA:
6673147Sxc151355 		if (ic->ic_state != IEEE80211_S_AUTH)
6683147Sxc151355 			return;
6693147Sxc151355 		switch (seq) {
6703147Sxc151355 		case IEEE80211_AUTH_SHARED_PASS:
6713147Sxc151355 			if (in->in_challenge != NULL) {
6723147Sxc151355 				kmem_free(in->in_challenge,
6733147Sxc151355 				    IEEE80211_CHALLENGE_LEN);
6743147Sxc151355 				in->in_challenge = NULL;
6753147Sxc151355 			}
6763147Sxc151355 			if (status != 0) {
6773147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_DEBUG |
6787249Sff224033 				    IEEE80211_MSG_AUTH,
6797249Sff224033 				    "shared key auth failed (reason %d)\n",
6807249Sff224033 				    status);
6813147Sxc151355 				if (in != ic->ic_bss)
6823147Sxc151355 					in->in_fails++;
6833147Sxc151355 				return;
6843147Sxc151355 			}
6853147Sxc151355 			IEEE80211_UNLOCK(ic);
6863147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
6877249Sff224033 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
6883147Sxc151355 			IEEE80211_LOCK(ic);
6893147Sxc151355 			break;
6903147Sxc151355 		case IEEE80211_AUTH_SHARED_CHALLENGE:
6913147Sxc151355 			if (!ieee80211_alloc_challenge(in))
6923147Sxc151355 				return;
6933147Sxc151355 			bcopy(&challenge[2], in->in_challenge, challenge[1]);
6943147Sxc151355 			IEEE80211_UNLOCK(ic);
6953147Sxc151355 			IEEE80211_SEND_MGMT(ic, in, IEEE80211_FC0_SUBTYPE_AUTH,
6967249Sff224033 			    seq + 1);
6973147Sxc151355 			IEEE80211_LOCK(ic);
6983147Sxc151355 			break;
6993147Sxc151355 		default:
7003147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH, "80211_auth_shared: "
7017249Sff224033 			    "shared key auth: bad seq %d", seq);
7023147Sxc151355 			return;
7033147Sxc151355 		}
7043147Sxc151355 		break;
7053147Sxc151355 
7063147Sxc151355 	default:
7073147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH,
7087249Sff224033 		    "ieee80211_auth_shared: bad opmode %u\n",
7097249Sff224033 		    ic->ic_opmode);
7103147Sxc151355 		break;
7113147Sxc151355 	}
7123147Sxc151355 	return;
7133147Sxc151355 bad:
7143147Sxc151355 	if (ic->ic_opmode == IEEE80211_M_STA) {
7153147Sxc151355 		/*
7163147Sxc151355 		 * Kick the state machine.  This short-circuits
7173147Sxc151355 		 * using the mgt frame timeout to trigger the
7183147Sxc151355 		 * state transition.
7193147Sxc151355 		 */
7203147Sxc151355 		if (ic->ic_state == IEEE80211_S_AUTH) {
7213147Sxc151355 			IEEE80211_UNLOCK(ic);
7223147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
7233147Sxc151355 			IEEE80211_LOCK(ic);
7243147Sxc151355 		}
7253147Sxc151355 	}
7263147Sxc151355 }
7273147Sxc151355 
7284126Szf162725 static int
7294126Szf162725 iswpaoui(const uint8_t *frm)
7304126Szf162725 {
7319327SFei.Feng@Sun.COM 	uint32_t c;
7329327SFei.Feng@Sun.COM 	bcopy(frm + 2, &c, 4);
7339327SFei.Feng@Sun.COM 	return (frm[1] > 3 && LE_32(c) == ((WPA_OUI_TYPE << 24) | WPA_OUI));
7344126Szf162725 }
7354126Szf162725 
736*10266SQuaker.Fang@Sun.COM #define	LE_READ_4(p)							\
737*10266SQuaker.Fang@Sun.COM 	((uint32_t)							\
738*10266SQuaker.Fang@Sun.COM 	((((uint8_t *)(p))[0]) | (((uint8_t *)(p))[1] <<  8) |		\
739*10266SQuaker.Fang@Sun.COM 	(((uint8_t *)(p))[2] << 16) | (((uint8_t *)(p))[3] << 24)))
740*10266SQuaker.Fang@Sun.COM 
741*10266SQuaker.Fang@Sun.COM #define	LE_READ_2(p)							\
742*10266SQuaker.Fang@Sun.COM 	((uint16_t)							\
743*10266SQuaker.Fang@Sun.COM 	(((uint8_t *)(p))[0]) | (((uint8_t *)(p))[1] <<  8))
744*10266SQuaker.Fang@Sun.COM 
745*10266SQuaker.Fang@Sun.COM static int
746*10266SQuaker.Fang@Sun.COM iswmeoui(const uint8_t *frm)
747*10266SQuaker.Fang@Sun.COM {
748*10266SQuaker.Fang@Sun.COM 	return (frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI));
749*10266SQuaker.Fang@Sun.COM }
750*10266SQuaker.Fang@Sun.COM 
751*10266SQuaker.Fang@Sun.COM static int
752*10266SQuaker.Fang@Sun.COM iswmeparam(const uint8_t *frm)
753*10266SQuaker.Fang@Sun.COM {
754*10266SQuaker.Fang@Sun.COM 	return (frm[1] > 5 &&
755*10266SQuaker.Fang@Sun.COM 	    LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
756*10266SQuaker.Fang@Sun.COM 	    frm[6] == WME_PARAM_OUI_SUBTYPE);
757*10266SQuaker.Fang@Sun.COM }
758*10266SQuaker.Fang@Sun.COM 
759*10266SQuaker.Fang@Sun.COM static int
760*10266SQuaker.Fang@Sun.COM iswmeinfo(const uint8_t *frm)
761*10266SQuaker.Fang@Sun.COM {
762*10266SQuaker.Fang@Sun.COM 	return (frm[1] > 5 &&
763*10266SQuaker.Fang@Sun.COM 	    LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
764*10266SQuaker.Fang@Sun.COM 	    frm[6] == WME_INFO_OUI_SUBTYPE);
765*10266SQuaker.Fang@Sun.COM }
766*10266SQuaker.Fang@Sun.COM 
767*10266SQuaker.Fang@Sun.COM static int
768*10266SQuaker.Fang@Sun.COM ishtcapoui(const uint8_t *frm)
769*10266SQuaker.Fang@Sun.COM {
770*10266SQuaker.Fang@Sun.COM 	return (frm[1] > 3 &&
771*10266SQuaker.Fang@Sun.COM 	    LE_READ_4(frm+2) == ((BCM_OUI_HTCAP<<24)|BCM_OUI));
772*10266SQuaker.Fang@Sun.COM }
773*10266SQuaker.Fang@Sun.COM 
774*10266SQuaker.Fang@Sun.COM static int
775*10266SQuaker.Fang@Sun.COM ishtinfooui(const uint8_t *frm)
776*10266SQuaker.Fang@Sun.COM {
777*10266SQuaker.Fang@Sun.COM 	return (frm[1] > 3 &&
778*10266SQuaker.Fang@Sun.COM 	    LE_READ_4(frm+2) == ((BCM_OUI_HTINFO<<24)|BCM_OUI));
779*10266SQuaker.Fang@Sun.COM }
780*10266SQuaker.Fang@Sun.COM 
781*10266SQuaker.Fang@Sun.COM /* ARGSUSED */
782*10266SQuaker.Fang@Sun.COM static int
783*10266SQuaker.Fang@Sun.COM ieee80211_parse_wmeparams(struct ieee80211com *ic, uint8_t *frm,
784*10266SQuaker.Fang@Sun.COM 	const struct ieee80211_frame *wh)
785*10266SQuaker.Fang@Sun.COM {
786*10266SQuaker.Fang@Sun.COM #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
787*10266SQuaker.Fang@Sun.COM 	struct ieee80211_wme_state *wme = &ic->ic_wme;
788*10266SQuaker.Fang@Sun.COM 	uint_t len = frm[1];
789*10266SQuaker.Fang@Sun.COM 	uint8_t qosinfo;
790*10266SQuaker.Fang@Sun.COM 	int i;
791*10266SQuaker.Fang@Sun.COM 
792*10266SQuaker.Fang@Sun.COM 	if (len < sizeof (struct ieee80211_wme_param) - 2) {
793*10266SQuaker.Fang@Sun.COM 		ieee80211_dbg(IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
794*10266SQuaker.Fang@Sun.COM 		    "WME too short, len %u", len);
795*10266SQuaker.Fang@Sun.COM 		return (-1);
796*10266SQuaker.Fang@Sun.COM 	}
797*10266SQuaker.Fang@Sun.COM 	qosinfo = frm[offsetof(struct ieee80211_wme_param, wme_qosInfo)];
798*10266SQuaker.Fang@Sun.COM 	qosinfo &= WME_QOSINFO_COUNT;
799*10266SQuaker.Fang@Sun.COM 	/* do proper check for wraparound */
800*10266SQuaker.Fang@Sun.COM 	if (qosinfo == wme->wme_wmeChanParams.cap_info)
801*10266SQuaker.Fang@Sun.COM 		return (0);
802*10266SQuaker.Fang@Sun.COM 	frm += offsetof(struct ieee80211_wme_param, wme_acParams);
803*10266SQuaker.Fang@Sun.COM 	for (i = 0; i < WME_NUM_AC; i++) {
804*10266SQuaker.Fang@Sun.COM 		struct wmeParams *wmep =
805*10266SQuaker.Fang@Sun.COM 		    &wme->wme_wmeChanParams.cap_wmeParams[i];
806*10266SQuaker.Fang@Sun.COM 		/* NB: ACI not used */
807*10266SQuaker.Fang@Sun.COM 		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
808*10266SQuaker.Fang@Sun.COM 		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
809*10266SQuaker.Fang@Sun.COM 		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
810*10266SQuaker.Fang@Sun.COM 		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
811*10266SQuaker.Fang@Sun.COM 		wmep->wmep_txopLimit = LE_READ_2(frm+2);
812*10266SQuaker.Fang@Sun.COM 		frm += 4;
813*10266SQuaker.Fang@Sun.COM 	}
814*10266SQuaker.Fang@Sun.COM 	wme->wme_wmeChanParams.cap_info = qosinfo;
815*10266SQuaker.Fang@Sun.COM 	return (1);
816*10266SQuaker.Fang@Sun.COM #undef MS
817*10266SQuaker.Fang@Sun.COM }
818*10266SQuaker.Fang@Sun.COM 
8193147Sxc151355 /*
8203147Sxc151355  * Process a beacon/probe response frame.
8213147Sxc151355  * When the device is in station mode, create a node and add it
8223147Sxc151355  * to the node database for a new ESS or update node info if it's
8233147Sxc151355  * already there.
8243147Sxc151355  */
8253147Sxc151355 static void
8263147Sxc151355 ieee80211_recv_beacon(ieee80211com_t *ic, mblk_t *mp, struct ieee80211_node *in,
8273147Sxc151355     int subtype, int rssi, uint32_t rstamp)
8283147Sxc151355 {
8293147Sxc151355 	ieee80211_impl_t *im = ic->ic_private;
8303147Sxc151355 	struct ieee80211_frame *wh;
8313147Sxc151355 	uint8_t *frm;
8323147Sxc151355 	uint8_t *efrm;	/* end of frame body */
8333147Sxc151355 	struct ieee80211_scanparams scan;
8343147Sxc151355 
8353147Sxc151355 	wh = (struct ieee80211_frame *)mp->b_rptr;
8363147Sxc151355 	frm = (uint8_t *)&wh[1];
8373147Sxc151355 	efrm = (uint8_t *)mp->b_wptr;
8383147Sxc151355 
8393147Sxc151355 	/*
8403147Sxc151355 	 * We process beacon/probe response frames:
8413147Sxc151355 	 *    o when scanning, or
8423147Sxc151355 	 *    o station mode when associated (to collect state
8433147Sxc151355 	 *	updates such as 802.11g slot time), or
8443147Sxc151355 	 *    o adhoc mode (to discover neighbors)
8453147Sxc151355 	 * Frames otherwise received are discarded.
8463147Sxc151355 	 */
8473147Sxc151355 	if (!((ic->ic_flags & IEEE80211_F_SCAN) ||
8483147Sxc151355 	    (ic->ic_opmode == IEEE80211_M_STA && in->in_associd != 0) ||
8493147Sxc151355 	    ic->ic_opmode == IEEE80211_M_IBSS)) {
8503147Sxc151355 		return;
8513147Sxc151355 	}
8523147Sxc151355 
8533147Sxc151355 	/*
8543147Sxc151355 	 * beacon/probe response frame format
8553147Sxc151355 	 *	[8] time stamp
8563147Sxc151355 	 *	[2] beacon interval
8573147Sxc151355 	 *	[2] capability information
8583147Sxc151355 	 *	[tlv] ssid
8593147Sxc151355 	 *	[tlv] supported rates
8603147Sxc151355 	 *	[tlv] country information
8613147Sxc151355 	 *	[tlv] parameter set (FH/DS)
8623147Sxc151355 	 *	[tlv] erp information
8633147Sxc151355 	 *	[tlv] extended supported rates
8643147Sxc151355 	 *	[tlv] WME
8653147Sxc151355 	 *	[tlv] WPA or RSN
866*10266SQuaker.Fang@Sun.COM 	 *	[tlv] HT capabilities
867*10266SQuaker.Fang@Sun.COM 	 *	[tlv] HT information
8683147Sxc151355 	 */
8697249Sff224033 	IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
8707249Sff224033 	    IEEE80211_BEACON_ELEM_MIN, return);
8713147Sxc151355 	bzero(&scan, sizeof (scan));
8723147Sxc151355 	scan.tstamp  = frm;
8733147Sxc151355 	frm += 8;
8749327SFei.Feng@Sun.COM 	scan.bintval = LE_16(*(uint16_t *)frm);
8753147Sxc151355 	frm += 2;
8769327SFei.Feng@Sun.COM 	scan.capinfo = LE_16(*(uint16_t *)frm);
8773147Sxc151355 	frm += 2;
8783147Sxc151355 	scan.bchan = ieee80211_chan2ieee(ic, ic->ic_curchan);
8793147Sxc151355 	scan.chan = scan.bchan;
8803147Sxc151355 
8813147Sxc151355 	while (frm < efrm) {
8823147Sxc151355 		/* Agere element in beacon */
8833147Sxc151355 		if ((*frm == IEEE80211_ELEMID_AGERE1) ||
8843147Sxc151355 		    (*frm == IEEE80211_ELEMID_AGERE2)) {
8853147Sxc151355 			frm = efrm;
8863147Sxc151355 			break;
8873147Sxc151355 		}
8883147Sxc151355 
8897249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm), frm[1], return);
8903147Sxc151355 		switch (*frm) {
8913147Sxc151355 		case IEEE80211_ELEMID_SSID:
8923147Sxc151355 			scan.ssid = frm;
8933147Sxc151355 			break;
8943147Sxc151355 		case IEEE80211_ELEMID_RATES:
8953147Sxc151355 			scan.rates = frm;
8963147Sxc151355 			break;
8973147Sxc151355 		case IEEE80211_ELEMID_COUNTRY:
8983147Sxc151355 			scan.country = frm;
8993147Sxc151355 			break;
9003147Sxc151355 		case IEEE80211_ELEMID_FHPARMS:
9013147Sxc151355 			if (ic->ic_phytype == IEEE80211_T_FH) {
9023147Sxc151355 				scan.fhdwell = LE_16(*(uint16_t *)(frm + 2));
9033147Sxc151355 				scan.chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
9043147Sxc151355 				scan.fhindex = frm[6];
9053147Sxc151355 				scan.phytype = IEEE80211_T_FH;
9063147Sxc151355 			}
9073147Sxc151355 			break;
9083147Sxc151355 		case IEEE80211_ELEMID_DSPARMS:
9093147Sxc151355 			if (ic->ic_phytype != IEEE80211_T_FH) {
9103147Sxc151355 				scan.chan = frm[2];
9113147Sxc151355 				scan.phytype = IEEE80211_T_DS;
9123147Sxc151355 			}
9133147Sxc151355 			break;
9143147Sxc151355 		case IEEE80211_ELEMID_TIM:
9153147Sxc151355 			scan.tim = frm;
9167249Sff224033 			scan.timoff = _PTRDIFF(frm, mp->b_rptr);
9173147Sxc151355 			break;
9183147Sxc151355 		case IEEE80211_ELEMID_IBSSPARMS:
9193147Sxc151355 			break;
9203147Sxc151355 		case IEEE80211_ELEMID_XRATES:
9213147Sxc151355 			scan.xrates = frm;
9223147Sxc151355 			break;
9233147Sxc151355 		case IEEE80211_ELEMID_ERP:
9243147Sxc151355 			if (frm[1] != 1) {
9253147Sxc151355 				ieee80211_dbg(IEEE80211_MSG_ELEMID,
9267249Sff224033 				    "ieee80211_recv_mgmt: ignore %s, "
9277249Sff224033 				    "invalid ERP element; "
9287249Sff224033 				    "length %u, expecting 1\n",
9297249Sff224033 				    IEEE80211_SUBTYPE_NAME(subtype),
9307249Sff224033 				    frm[1]);
9313147Sxc151355 				break;
9323147Sxc151355 			}
9333147Sxc151355 			scan.erp = frm[2];
9343147Sxc151355 			scan.phytype = IEEE80211_T_OFDM;
9353147Sxc151355 			break;
936*10266SQuaker.Fang@Sun.COM 		case IEEE80211_ELEMID_HTCAP:
937*10266SQuaker.Fang@Sun.COM 			scan.htcap = frm;
938*10266SQuaker.Fang@Sun.COM 			break;
9393147Sxc151355 		case IEEE80211_ELEMID_RSN:
9403147Sxc151355 			scan.wpa = frm;
9413147Sxc151355 			break;
942*10266SQuaker.Fang@Sun.COM 		case IEEE80211_ELEMID_HTINFO:
943*10266SQuaker.Fang@Sun.COM 			scan.htinfo = frm;
944*10266SQuaker.Fang@Sun.COM 			break;
9454126Szf162725 		case IEEE80211_ELEMID_VENDOR:
9464126Szf162725 			if (iswpaoui(frm))
9474126Szf162725 				scan.wpa = frm;		/* IEEE802.11i D3.0 */
948*10266SQuaker.Fang@Sun.COM 			else if (iswmeparam(frm) || iswmeinfo(frm))
949*10266SQuaker.Fang@Sun.COM 				scan.wme = frm;
950*10266SQuaker.Fang@Sun.COM 			else if (ic->ic_flags_ext & IEEE80211_FEXT_HTCOMPAT) {
951*10266SQuaker.Fang@Sun.COM 				/*
952*10266SQuaker.Fang@Sun.COM 				 * Accept pre-draft HT ie's if the
953*10266SQuaker.Fang@Sun.COM 				 * standard ones have not been seen.
954*10266SQuaker.Fang@Sun.COM 				 */
955*10266SQuaker.Fang@Sun.COM 				if (ishtcapoui(frm)) {
956*10266SQuaker.Fang@Sun.COM 					if (scan.htcap == NULL)
957*10266SQuaker.Fang@Sun.COM 						scan.htcap = frm;
958*10266SQuaker.Fang@Sun.COM 				} else if (ishtinfooui(frm)) {
959*10266SQuaker.Fang@Sun.COM 					if (scan.htinfo == NULL)
960*10266SQuaker.Fang@Sun.COM 						scan.htinfo = frm;
961*10266SQuaker.Fang@Sun.COM 				}
962*10266SQuaker.Fang@Sun.COM 			}
9634126Szf162725 			break;
9643147Sxc151355 		default:
9653147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ELEMID,
9667249Sff224033 			    "ieee80211_recv_mgmt: ignore %s,"
9677249Sff224033 			    "unhandled id %u, len %u, totallen %u",
9687249Sff224033 			    IEEE80211_SUBTYPE_NAME(subtype),
9697249Sff224033 			    *frm, frm[1],
9707249Sff224033 			    MBLKL(mp));
9713147Sxc151355 			break;
9723147Sxc151355 		}
9733147Sxc151355 		/* frm[1] - component length */
9743147Sxc151355 		frm += IEEE80211_ELEM_LEN(frm[1]);
9753147Sxc151355 	}
9763147Sxc151355 	IEEE80211_VERIFY_ELEMENT(scan.rates, IEEE80211_RATE_MAXSIZE, return);
9773147Sxc151355 	IEEE80211_VERIFY_ELEMENT(scan.ssid, IEEE80211_NWID_LEN, return);
9783147Sxc151355 	if (ieee80211_isclr(ic->ic_chan_active, scan.chan)) {
9793147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
9807249Sff224033 		    "ieee80211_recv_mgmt: ignore %s ,"
9817249Sff224033 		    "invalid channel %u\n",
9827249Sff224033 		    IEEE80211_SUBTYPE_NAME(subtype), scan.chan);
9833147Sxc151355 		return;
9843147Sxc151355 	}
9853147Sxc151355 	if (scan.chan != scan.bchan &&
9863147Sxc151355 	    ic->ic_phytype != IEEE80211_T_FH) {
9873147Sxc151355 		/*
9883147Sxc151355 		 * Frame was received on a channel different from the
9893147Sxc151355 		 * one indicated in the DS params element id;
9903147Sxc151355 		 * silently discard it.
9913147Sxc151355 		 *
9923147Sxc151355 		 * NB:	this can happen due to signal leakage.
9933147Sxc151355 		 *	But we should take it for FH phy because
9943147Sxc151355 		 *	the rssi value should be correct even for
9953147Sxc151355 		 *	different hop pattern in FH.
9963147Sxc151355 		 */
9973147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ELEMID,
9987249Sff224033 		    "ieee80211_recv_mgmt: ignore %s ,"
9997249Sff224033 		    "phytype %u channel %u marked for %u\n",
10007249Sff224033 		    IEEE80211_SUBTYPE_NAME(subtype),
10017249Sff224033 		    ic->ic_phytype, scan.bchan, scan.chan);
10023147Sxc151355 		return;
10033147Sxc151355 	}
10043147Sxc151355 	if (!(IEEE80211_BINTVAL_MIN <= scan.bintval &&
10053147Sxc151355 	    scan.bintval <= IEEE80211_BINTVAL_MAX)) {
10063147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
10077249Sff224033 		    "ieee80211_recv_mgmt: ignore %s ,"
10087249Sff224033 		    "bogus beacon interval %u\n",
10097249Sff224033 		    IEEE80211_SUBTYPE_NAME(subtype), scan.bintval);
10103147Sxc151355 		return;
10113147Sxc151355 	}
1012*10266SQuaker.Fang@Sun.COM 	/*
1013*10266SQuaker.Fang@Sun.COM 	 * Process HT ie's.  This is complicated by our
1014*10266SQuaker.Fang@Sun.COM 	 * accepting both the standard ie's and the pre-draft
1015*10266SQuaker.Fang@Sun.COM 	 * vendor OUI ie's that some vendors still use/require.
1016*10266SQuaker.Fang@Sun.COM 	 */
1017*10266SQuaker.Fang@Sun.COM 	if (scan.htcap != NULL) {
1018*10266SQuaker.Fang@Sun.COM 		IEEE80211_VERIFY_LENGTH(scan.htcap[1],
1019*10266SQuaker.Fang@Sun.COM 		    scan.htcap[0] == IEEE80211_ELEMID_VENDOR ?
1020*10266SQuaker.Fang@Sun.COM 		    4 + sizeof (struct ieee80211_ie_htcap) - 2 :
1021*10266SQuaker.Fang@Sun.COM 		    sizeof (struct ieee80211_ie_htcap) - 2,
1022*10266SQuaker.Fang@Sun.COM 		    scan.htcap = NULL);
1023*10266SQuaker.Fang@Sun.COM 	}
1024*10266SQuaker.Fang@Sun.COM 	if (scan.htinfo != NULL) {
1025*10266SQuaker.Fang@Sun.COM 		IEEE80211_VERIFY_LENGTH(scan.htinfo[1],
1026*10266SQuaker.Fang@Sun.COM 		    scan.htinfo[0] == IEEE80211_ELEMID_VENDOR ?
1027*10266SQuaker.Fang@Sun.COM 		    4 + sizeof (struct ieee80211_ie_htinfo) - 2 :
1028*10266SQuaker.Fang@Sun.COM 		    sizeof (struct ieee80211_ie_htinfo) - 2,
1029*10266SQuaker.Fang@Sun.COM 		    scan.htinfo = NULL);
1030*10266SQuaker.Fang@Sun.COM 	}
10313147Sxc151355 
10323147Sxc151355 	/*
10333147Sxc151355 	 * When operating in station mode, check for state updates.
10343147Sxc151355 	 * Be careful to ignore beacons received while doing a
10353147Sxc151355 	 * background scan.  We consider only 11g/WMM stuff right now.
10363147Sxc151355 	 */
10373147Sxc151355 	if (ic->ic_opmode == IEEE80211_M_STA &&
10383147Sxc151355 	    in->in_associd != 0 &&
10393147Sxc151355 	    (!(ic->ic_flags & IEEE80211_F_SCAN) ||
10403147Sxc151355 	    IEEE80211_ADDR_EQ(wh->i_addr2, in->in_bssid))) {
10413147Sxc151355 		/* record tsf of last beacon */
10423147Sxc151355 		bcopy(scan.tstamp, in->in_tstamp.data,
10433147Sxc151355 		    sizeof (in->in_tstamp));
10443147Sxc151355 		/* count beacon frame for s/w bmiss handling */
10453147Sxc151355 		im->im_swbmiss_count++;
10463147Sxc151355 		im->im_bmiss_count = 0;
10473147Sxc151355 
10483147Sxc151355 		if ((in->in_capinfo ^ scan.capinfo) &
10493147Sxc151355 		    IEEE80211_CAPINFO_SHORT_SLOTTIME) {
10503147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
10517249Sff224033 			    "ieee80211_recv_mgmt: "
10527249Sff224033 			    "[%s] cap change: before 0x%x, now 0x%x\n",
10537249Sff224033 			    ieee80211_macaddr_sprintf(wh->i_addr2),
10547249Sff224033 			    in->in_capinfo, scan.capinfo);
10553147Sxc151355 			/*
10563147Sxc151355 			 * NB:	we assume short preamble doesn't
10573147Sxc151355 			 *	change dynamically
10583147Sxc151355 			 */
10593147Sxc151355 			ieee80211_set_shortslottime(ic,
10607249Sff224033 			    ic->ic_curmode == IEEE80211_MODE_11A ||
10617249Sff224033 			    (scan.capinfo &
10627249Sff224033 			    IEEE80211_CAPINFO_SHORT_SLOTTIME));
10633147Sxc151355 			in->in_capinfo = scan.capinfo;
10643147Sxc151355 		}
1065*10266SQuaker.Fang@Sun.COM 		if (scan.wme != NULL &&
1066*10266SQuaker.Fang@Sun.COM 		    (in->in_flags & IEEE80211_NODE_QOS) &&
1067*10266SQuaker.Fang@Sun.COM 		    ieee80211_parse_wmeparams(ic, scan.wme, wh) > 0) {
1068*10266SQuaker.Fang@Sun.COM 			ieee80211_wme_updateparams(ic);
1069*10266SQuaker.Fang@Sun.COM 		}
1070*10266SQuaker.Fang@Sun.COM 		if (scan.htcap != NULL)
1071*10266SQuaker.Fang@Sun.COM 			ieee80211_parse_htcap(in, scan.htcap);
1072*10266SQuaker.Fang@Sun.COM 		if (scan.htinfo != NULL) {
1073*10266SQuaker.Fang@Sun.COM 			ieee80211_parse_htinfo(in, scan.htinfo);
1074*10266SQuaker.Fang@Sun.COM 			if (in->in_chan != ic->ic_curchan) {
1075*10266SQuaker.Fang@Sun.COM 				/*
1076*10266SQuaker.Fang@Sun.COM 				 * Channel has been adjusted based on
1077*10266SQuaker.Fang@Sun.COM 				 * negotiated HT parameters; force the
1078*10266SQuaker.Fang@Sun.COM 				 * channel state to follow.
1079*10266SQuaker.Fang@Sun.COM 				 */
1080*10266SQuaker.Fang@Sun.COM 				ieee80211_setcurchan(ic, in->in_chan);
1081*10266SQuaker.Fang@Sun.COM 			}
1082*10266SQuaker.Fang@Sun.COM 		}
10833147Sxc151355 		if (scan.tim != NULL) {
10843147Sxc151355 			struct ieee80211_tim_ie *ie;
10853147Sxc151355 
10863147Sxc151355 			ie = (struct ieee80211_tim_ie *)scan.tim;
10873147Sxc151355 			in->in_dtim_count = ie->tim_count;
10883147Sxc151355 			in->in_dtim_period = ie->tim_period;
10893147Sxc151355 		}
10903147Sxc151355 		if (ic->ic_flags & IEEE80211_F_SCAN) {
10913147Sxc151355 			ieee80211_add_scan(ic, &scan, wh, subtype, rssi,
10927249Sff224033 			    rstamp);
10933147Sxc151355 		}
10943147Sxc151355 		return;
10953147Sxc151355 	}
10963147Sxc151355 	/*
10973147Sxc151355 	 * If scanning, just pass information to the scan module.
10983147Sxc151355 	 */
10993147Sxc151355 	if (ic->ic_flags & IEEE80211_F_SCAN) {
11003147Sxc151355 		ieee80211_add_scan(ic, &scan, wh, subtype, rssi, rstamp);
11013147Sxc151355 		return;
11023147Sxc151355 	}
11033147Sxc151355 
11048594SFei.Feng@Sun.COM 	if (ic->ic_opmode == IEEE80211_M_IBSS &&
11058594SFei.Feng@Sun.COM 	    scan.capinfo & IEEE80211_CAPINFO_IBSS) {
11063147Sxc151355 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, in->in_macaddr)) {
11073147Sxc151355 			/*
11083147Sxc151355 			 * Create a new entry in the neighbor table.
11093147Sxc151355 			 */
11103147Sxc151355 			in = ieee80211_add_neighbor(ic, wh, &scan);
11118594SFei.Feng@Sun.COM 		} else {
11123147Sxc151355 			/*
11138594SFei.Feng@Sun.COM 			 * Copy data from beacon to neighbor table.
11148594SFei.Feng@Sun.COM 			 * Some of this information might change after
11158594SFei.Feng@Sun.COM 			 * ieee80211_add_neighbor(), so we just copy
11168594SFei.Feng@Sun.COM 			 * everything over to be safe.
11173147Sxc151355 			 */
11183147Sxc151355 			ieee80211_init_neighbor(in, wh, &scan);
11193147Sxc151355 		}
11203147Sxc151355 		if (in != NULL) {
11213147Sxc151355 			in->in_rssi = (uint8_t)rssi;
11223147Sxc151355 			in->in_rstamp = rstamp;
11233147Sxc151355 		}
11243147Sxc151355 	}
11253147Sxc151355 }
11263147Sxc151355 
11273147Sxc151355 /*
11283147Sxc151355  * Perform input processing for 802.11 management frames.
11293147Sxc151355  * It's the default ic_recv_mgmt callback function for the interface
11303147Sxc151355  * softc, ic. Tipically ic_recv_mgmt is called within ieee80211_input()
11313147Sxc151355  */
11323147Sxc151355 void
11333147Sxc151355 ieee80211_recv_mgmt(ieee80211com_t *ic, mblk_t *mp, struct ieee80211_node *in,
11343147Sxc151355     int subtype, int rssi, uint32_t rstamp)
11353147Sxc151355 {
11363147Sxc151355 	struct ieee80211_frame *wh;
11373147Sxc151355 	uint8_t *frm;		/* pointer to start of the frame */
11383147Sxc151355 	uint8_t *efrm;		/* pointer to end of the frame */
11393147Sxc151355 	uint8_t *ssid;
11403147Sxc151355 	uint8_t *rates;
11413147Sxc151355 	uint8_t *xrates;	/* extended rates */
1142*10266SQuaker.Fang@Sun.COM 	uint8_t	*wme;
1143*10266SQuaker.Fang@Sun.COM 	uint8_t *htcap, *htinfo;
11443147Sxc151355 	boolean_t allocbs = B_FALSE;
11453147Sxc151355 	uint8_t rate;
11463147Sxc151355 	uint16_t algo;		/* authentication algorithm */
11473147Sxc151355 	uint16_t seq;		/* sequence no */
11483147Sxc151355 	uint16_t status;
11493147Sxc151355 	uint16_t capinfo;
11503147Sxc151355 	uint16_t associd;	/* association ID */
1151*10266SQuaker.Fang@Sun.COM 	const struct ieee80211_action *ia;
11523147Sxc151355 
11533147Sxc151355 	IEEE80211_LOCK(ic);
11543147Sxc151355 	wh = (struct ieee80211_frame *)mp->b_rptr;
11553147Sxc151355 	frm = (uint8_t *)&wh[1];
11563147Sxc151355 	efrm = (uint8_t *)mp->b_wptr;
11573147Sxc151355 	switch (subtype) {
11583147Sxc151355 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
11593147Sxc151355 	case IEEE80211_FC0_SUBTYPE_BEACON:
11603147Sxc151355 		ieee80211_recv_beacon(ic, mp, in, subtype, rssi, rstamp);
11613147Sxc151355 		break;
11623147Sxc151355 
11633147Sxc151355 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
11643147Sxc151355 		if (ic->ic_opmode == IEEE80211_M_STA ||
11653147Sxc151355 		    ic->ic_state != IEEE80211_S_RUN ||
11663147Sxc151355 		    IEEE80211_IS_MULTICAST(wh->i_addr2)) {
11673147Sxc151355 			break;
11683147Sxc151355 		}
11693147Sxc151355 
11703147Sxc151355 		/*
11713147Sxc151355 		 * prreq frame format
11723147Sxc151355 		 *	[tlv] ssid
11733147Sxc151355 		 *	[tlv] supported rates
11743147Sxc151355 		 *	[tlv] extended supported rates
11753147Sxc151355 		 */
11763147Sxc151355 		ssid = rates = xrates = NULL;
11773147Sxc151355 		while (frm < efrm) {
11787249Sff224033 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
11797249Sff224033 			    frm[1], goto out);
11803147Sxc151355 			switch (*frm) {
11813147Sxc151355 			case IEEE80211_ELEMID_SSID:
11823147Sxc151355 				ssid = frm;
11833147Sxc151355 				break;
11843147Sxc151355 			case IEEE80211_ELEMID_RATES:
11853147Sxc151355 				rates = frm;
11863147Sxc151355 				break;
11873147Sxc151355 			case IEEE80211_ELEMID_XRATES:
11883147Sxc151355 				xrates = frm;
11893147Sxc151355 				break;
11903147Sxc151355 			}
11913147Sxc151355 			frm += frm[1] + 2;
11923147Sxc151355 		}
11933147Sxc151355 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, break);
11948594SFei.Feng@Sun.COM 		if (xrates != NULL) {
11958594SFei.Feng@Sun.COM 			IEEE80211_VERIFY_ELEMENT(xrates,
11968594SFei.Feng@Sun.COM 			    IEEE80211_RATE_MAXSIZE - rates[1], break);
11978594SFei.Feng@Sun.COM 		}
11983147Sxc151355 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, break);
11993147Sxc151355 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid, break);
12008594SFei.Feng@Sun.COM 		if (ic->ic_flags & IEEE80211_F_HIDESSID) {
12018594SFei.Feng@Sun.COM 			if (ssid == NULL || ssid[1] == 0) {
12028594SFei.Feng@Sun.COM 				ieee80211_dbg(IEEE80211_MSG_INPUT,
12038594SFei.Feng@Sun.COM 				    "ieee80211_recv_mgmt: ignore %s, "
12048594SFei.Feng@Sun.COM 				    "no ssid with ssid suppression enabled",
12058594SFei.Feng@Sun.COM 				    IEEE80211_SUBTYPE_NAME(subtype));
12068594SFei.Feng@Sun.COM 				break;
12078594SFei.Feng@Sun.COM 			}
12083147Sxc151355 		}
12093147Sxc151355 
12103147Sxc151355 		if (in == ic->ic_bss) {
12113147Sxc151355 			if (ic->ic_opmode != IEEE80211_M_IBSS) {
12123147Sxc151355 				in = ieee80211_tmp_node(ic, wh->i_addr2);
12133147Sxc151355 				allocbs = B_TRUE;
12143147Sxc151355 			} else if (!IEEE80211_ADDR_EQ(wh->i_addr2,
12153147Sxc151355 			    in->in_macaddr)) {
12163147Sxc151355 				/*
12173147Sxc151355 				 * Cannot tell if the sender is operating
12183147Sxc151355 				 * in ibss mode.  But we need a new node to
12193147Sxc151355 				 * send the response so blindly add them to the
12203147Sxc151355 				 * neighbor table.
12213147Sxc151355 				 */
12223147Sxc151355 				in = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
12237249Sff224033 				    wh->i_addr2);
12243147Sxc151355 			}
12253147Sxc151355 			if (in == NULL)
12263147Sxc151355 				break;
12273147Sxc151355 		}
12283147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ASSOC, "ieee80211_recv_mgmt: "
12297249Sff224033 		    "[%s] recv probe req\n",
12307249Sff224033 		    ieee80211_macaddr_sprintf(wh->i_addr2));
12313147Sxc151355 		in->in_rssi = (uint8_t)rssi;
12323147Sxc151355 		in->in_rstamp = rstamp;
12333147Sxc151355 		/*
12343147Sxc151355 		 * Adjust and check station's rate list with device's
12353147Sxc151355 		 * supported rate.  Send back response if there is at
12363147Sxc151355 		 * least one rate or the fixed rate(if being set) is
12373147Sxc151355 		 * supported by both station and the device
12383147Sxc151355 		 */
12393147Sxc151355 		rate = ieee80211_setup_rates(in, rates, xrates,
12407249Sff224033 		    IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
12417249Sff224033 		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
12423147Sxc151355 		if (rate & IEEE80211_RATE_BASIC) {
12433147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_XRATE, "ieee80211_recv_mgmt"
12447249Sff224033 			    "%s recv'd rate set invalid",
12457249Sff224033 			    IEEE80211_SUBTYPE_NAME(subtype));
12463147Sxc151355 		} else {
12473147Sxc151355 			IEEE80211_UNLOCK(ic);
12483147Sxc151355 			IEEE80211_SEND_MGMT(ic, in,
12497249Sff224033 			    IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
12503147Sxc151355 			IEEE80211_LOCK(ic);
12513147Sxc151355 		}
12523147Sxc151355 		if (allocbs) {
12533147Sxc151355 			/*
12543147Sxc151355 			 * Temporary node created just to send a
12553147Sxc151355 			 * response, reclaim immediately.
12563147Sxc151355 			 */
12573147Sxc151355 			ieee80211_free_node(in);
12583147Sxc151355 		}
12593147Sxc151355 		break;
12603147Sxc151355 
12613147Sxc151355 	case IEEE80211_FC0_SUBTYPE_AUTH:
12623147Sxc151355 		/*
12633147Sxc151355 		 * auth frame format
12643147Sxc151355 		 *	[2] algorithm
12653147Sxc151355 		 *	[2] sequence
12663147Sxc151355 		 *	[2] status
12673147Sxc151355 		 *	[tlv*] challenge
12683147Sxc151355 		 */
12697249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
12707249Sff224033 		    IEEE80211_AUTH_ELEM_MIN, break);
12719327SFei.Feng@Sun.COM 		algo   = LE_16(*(uint16_t *)frm);
12729327SFei.Feng@Sun.COM 		seq    = LE_16(*(uint16_t *)(frm + 2));
12739327SFei.Feng@Sun.COM 		status = LE_16(*(uint16_t *)(frm + 4));
12743147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_recv_mgmt: "
12757249Sff224033 		    "[%s] recv auth frame with algorithm %d seq %d\n",
12767249Sff224033 		    ieee80211_macaddr_sprintf(wh->i_addr2), algo, seq);
12773147Sxc151355 
12783147Sxc151355 		if (ic->ic_flags & IEEE80211_F_COUNTERM) {
12793147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
12807249Sff224033 			    "ieee80211_recv_mgmt: ignore auth, %s\n",
12817249Sff224033 			    "TKIP countermeasures enabled");
12823147Sxc151355 			break;
12833147Sxc151355 		}
12843147Sxc151355 		switch (algo) {
12853147Sxc151355 		case IEEE80211_AUTH_ALG_SHARED:
12863147Sxc151355 			ieee80211_auth_shared(ic, wh, frm + 6, efrm, in,
12877249Sff224033 			    seq, status);
12883147Sxc151355 			break;
12893147Sxc151355 		case IEEE80211_AUTH_ALG_OPEN:
12903147Sxc151355 			ieee80211_auth_open(ic, wh, in, seq, status);
12913147Sxc151355 			break;
12923147Sxc151355 		default:
12933147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_recv_mgmt: "
12947249Sff224033 			    "ignore auth, unsupported alg %d", algo);
12953147Sxc151355 			break;
12963147Sxc151355 		}
12973147Sxc151355 		break;
12983147Sxc151355 
12993147Sxc151355 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
13003147Sxc151355 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
13013147Sxc151355 		if (ic->ic_opmode != IEEE80211_M_STA ||
13023147Sxc151355 		    ic->ic_state != IEEE80211_S_ASSOC)
13033147Sxc151355 			break;
13043147Sxc151355 
13053147Sxc151355 		/*
13063147Sxc151355 		 * asresp frame format
13073147Sxc151355 		 *	[2] capability information
13083147Sxc151355 		 *	[2] status
13093147Sxc151355 		 *	[2] association ID
13103147Sxc151355 		 *	[tlv] supported rates
13113147Sxc151355 		 *	[tlv] extended supported rates
13123147Sxc151355 		 *	[tlv] WME
1313*10266SQuaker.Fang@Sun.COM 		 *	[tlv] HT capabilities
1314*10266SQuaker.Fang@Sun.COM 		 *	[tlv] HT info
13153147Sxc151355 		 */
13167249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
13177249Sff224033 		    IEEE80211_ASSOC_RESP_ELEM_MIN, break);
13183147Sxc151355 		in = ic->ic_bss;
13199327SFei.Feng@Sun.COM 		capinfo = LE_16(*(uint16_t *)frm);
13203147Sxc151355 		frm += 2;
13219327SFei.Feng@Sun.COM 		status = LE_16(*(uint16_t *)frm);
13223147Sxc151355 		frm += 2;
13233147Sxc151355 		if (status != 0) {
13243147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
13257249Sff224033 			    "assoc failed (reason %d)\n", status);
13263147Sxc151355 			in = ieee80211_find_node(&ic->ic_scan, wh->i_addr2);
13273147Sxc151355 			if (in != NULL) {
13283147Sxc151355 				in->in_fails++;
13293147Sxc151355 				ieee80211_free_node(in);
13303147Sxc151355 			}
13313147Sxc151355 			break;
13323147Sxc151355 		}
13339327SFei.Feng@Sun.COM 		associd = LE_16(*(uint16_t *)frm);
13343147Sxc151355 		frm += 2;
13353147Sxc151355 
1336*10266SQuaker.Fang@Sun.COM 		rates = xrates = wme = htcap = htinfo = NULL;
13373147Sxc151355 		while (frm < efrm) {
13383147Sxc151355 			/*
13393147Sxc151355 			 * Do not discard frames containing proprietary Agere
13403147Sxc151355 			 * elements 128 and 129, as the reported element length
13413147Sxc151355 			 * is often wrong. Skip rest of the frame, since we can
13423147Sxc151355 			 * not rely on the given element length making it
13433147Sxc151355 			 * impossible to know where the next element starts
13443147Sxc151355 			 */
13453147Sxc151355 			if ((*frm == IEEE80211_ELEMID_AGERE1) ||
13463147Sxc151355 			    (*frm == IEEE80211_ELEMID_AGERE2)) {
13473147Sxc151355 				frm = efrm;
13483147Sxc151355 				break;
13493147Sxc151355 			}
13503147Sxc151355 
13517249Sff224033 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
13527249Sff224033 			    frm[1], goto out);
13533147Sxc151355 			switch (*frm) {
13543147Sxc151355 			case IEEE80211_ELEMID_RATES:
13553147Sxc151355 				rates = frm;
13563147Sxc151355 				break;
13573147Sxc151355 			case IEEE80211_ELEMID_XRATES:
13583147Sxc151355 				xrates = frm;
13593147Sxc151355 				break;
1360*10266SQuaker.Fang@Sun.COM 			case IEEE80211_ELEMID_HTCAP:
1361*10266SQuaker.Fang@Sun.COM 				htcap = frm;
1362*10266SQuaker.Fang@Sun.COM 				break;
1363*10266SQuaker.Fang@Sun.COM 			case IEEE80211_ELEMID_HTINFO:
1364*10266SQuaker.Fang@Sun.COM 				htinfo = frm;
1365*10266SQuaker.Fang@Sun.COM 				break;
1366*10266SQuaker.Fang@Sun.COM 			case IEEE80211_ELEMID_VENDOR:
1367*10266SQuaker.Fang@Sun.COM 				if (iswmeoui(frm))
1368*10266SQuaker.Fang@Sun.COM 					wme = frm;
1369*10266SQuaker.Fang@Sun.COM 				else if (ic->ic_flags_ext &
1370*10266SQuaker.Fang@Sun.COM 				    IEEE80211_FEXT_HTCOMPAT) {
1371*10266SQuaker.Fang@Sun.COM 					/*
1372*10266SQuaker.Fang@Sun.COM 					 * Accept pre-draft HT ie's if the
1373*10266SQuaker.Fang@Sun.COM 					 * standard ones have not been seen.
1374*10266SQuaker.Fang@Sun.COM 					 */
1375*10266SQuaker.Fang@Sun.COM 					if (ishtcapoui(frm)) {
1376*10266SQuaker.Fang@Sun.COM 						if (htcap == NULL)
1377*10266SQuaker.Fang@Sun.COM 							htcap = frm;
1378*10266SQuaker.Fang@Sun.COM 					} else if (ishtinfooui(frm)) {
1379*10266SQuaker.Fang@Sun.COM 						if (htinfo == NULL)
1380*10266SQuaker.Fang@Sun.COM 							htinfo = frm;
1381*10266SQuaker.Fang@Sun.COM 					}
1382*10266SQuaker.Fang@Sun.COM 				}
1383*10266SQuaker.Fang@Sun.COM 				break;
13843147Sxc151355 			}
13853147Sxc151355 			frm += frm[1] + 2;
13863147Sxc151355 		}
13873147Sxc151355 
13883147Sxc151355 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, break);
13893147Sxc151355 		/*
13903147Sxc151355 		 * Adjust and check AP's rate list with device's
13913147Sxc151355 		 * supported rate. Re-start scan if no rate is or the
13923147Sxc151355 		 * fixed rate(if being set) cannot be supported by
13933147Sxc151355 		 * either AP or the device.
13943147Sxc151355 		 */
13953147Sxc151355 		rate = ieee80211_setup_rates(in, rates, xrates,
13967249Sff224033 		    IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
13977249Sff224033 		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
13983147Sxc151355 		if (rate & IEEE80211_RATE_BASIC) {
13993147Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
14007249Sff224033 			    "assoc failed (rate set mismatch)\n");
14013147Sxc151355 			if (in != ic->ic_bss)
14023147Sxc151355 				in->in_fails++;
14033147Sxc151355 			IEEE80211_UNLOCK(ic);
14043147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
14053147Sxc151355 			return;
14063147Sxc151355 		}
14073147Sxc151355 
14083147Sxc151355 		in->in_capinfo = capinfo;
14093147Sxc151355 		in->in_associd = associd;
1410*10266SQuaker.Fang@Sun.COM 		if (wme != NULL &&
1411*10266SQuaker.Fang@Sun.COM 		    ieee80211_parse_wmeparams(ic, wme, wh) >= 0) {
1412*10266SQuaker.Fang@Sun.COM 			in->in_flags |= IEEE80211_NODE_QOS;
1413*10266SQuaker.Fang@Sun.COM 			ieee80211_wme_updateparams(ic);
1414*10266SQuaker.Fang@Sun.COM 		} else {
1415*10266SQuaker.Fang@Sun.COM 			in->in_flags &= ~IEEE80211_NODE_QOS;
1416*10266SQuaker.Fang@Sun.COM 		}
1417*10266SQuaker.Fang@Sun.COM 		/*
1418*10266SQuaker.Fang@Sun.COM 		 * Setup HT state according to the negotiation.
1419*10266SQuaker.Fang@Sun.COM 		 */
1420*10266SQuaker.Fang@Sun.COM 		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1421*10266SQuaker.Fang@Sun.COM 		    htcap != NULL && htinfo != NULL) {
1422*10266SQuaker.Fang@Sun.COM 			ieee80211_ht_node_init(in, htcap);
1423*10266SQuaker.Fang@Sun.COM 			ieee80211_parse_htinfo(in, htinfo);
1424*10266SQuaker.Fang@Sun.COM 			(void) ieee80211_setup_htrates(in,
1425*10266SQuaker.Fang@Sun.COM 			    htcap, IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1426*10266SQuaker.Fang@Sun.COM 			ieee80211_setup_basic_htrates(in, htinfo);
1427*10266SQuaker.Fang@Sun.COM 			if (in->in_chan != ic->ic_curchan) {
1428*10266SQuaker.Fang@Sun.COM 				/*
1429*10266SQuaker.Fang@Sun.COM 				 * Channel has been adjusted based on
1430*10266SQuaker.Fang@Sun.COM 				 * negotiated HT parameters; force the
1431*10266SQuaker.Fang@Sun.COM 				 * channel state to follow.
1432*10266SQuaker.Fang@Sun.COM 				 */
1433*10266SQuaker.Fang@Sun.COM 				ieee80211_setcurchan(ic, in->in_chan);
1434*10266SQuaker.Fang@Sun.COM 			}
1435*10266SQuaker.Fang@Sun.COM 		}
14363147Sxc151355 		/*
14373147Sxc151355 		 * Configure state now that we are associated.
14383147Sxc151355 		 */
14393147Sxc151355 		if (ic->ic_curmode == IEEE80211_MODE_11A ||
14403147Sxc151355 		    (in->in_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
14413147Sxc151355 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
14423147Sxc151355 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
14433147Sxc151355 		} else {
14443147Sxc151355 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
14453147Sxc151355 			ic->ic_flags |= IEEE80211_F_USEBARKER;
14463147Sxc151355 		}
14473147Sxc151355 		ieee80211_set_shortslottime(ic,
14487249Sff224033 		    ic->ic_curmode == IEEE80211_MODE_11A ||
14497249Sff224033 		    (in->in_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
14503147Sxc151355 		/*
14513147Sxc151355 		 * Honor ERP protection.
14523147Sxc151355 		 *
14533147Sxc151355 		 * NB:	in_erp should zero for non-11g operation.
14543147Sxc151355 		 *	check ic_curmode anyway
14553147Sxc151355 		 */
14563147Sxc151355 		if (ic->ic_curmode == IEEE80211_MODE_11G &&
14573147Sxc151355 		    (in->in_erp & IEEE80211_ERP_USE_PROTECTION))
14583147Sxc151355 			ic->ic_flags |= IEEE80211_F_USEPROT;
14593147Sxc151355 		else
14603147Sxc151355 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
14613147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ASSOC,
14627249Sff224033 		    "assoc success: %s preamble, %s slot time%s%s\n",
14637249Sff224033 		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
14647249Sff224033 		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
14657249Sff224033 		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
14667249Sff224033 		    in->in_flags & IEEE80211_NODE_QOS ? ", QoS" : "");
14673147Sxc151355 		IEEE80211_UNLOCK(ic);
14683147Sxc151355 		ieee80211_new_state(ic, IEEE80211_S_RUN, subtype);
14693147Sxc151355 		return;
14703147Sxc151355 
14713147Sxc151355 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
14723147Sxc151355 		if (ic->ic_state == IEEE80211_S_SCAN)
14733147Sxc151355 			break;
14743147Sxc151355 
14753147Sxc151355 		/*
14763147Sxc151355 		 * deauth frame format
14773147Sxc151355 		 *	[2] reason
14783147Sxc151355 		 */
14797249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm), 2, break);
14809327SFei.Feng@Sun.COM 		status = LE_16(*(uint16_t *)frm);
14813147Sxc151355 
14823147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_AUTH,
14837249Sff224033 		    "recv deauthenticate (reason %d)\n", status);
14843147Sxc151355 		switch (ic->ic_opmode) {
14853147Sxc151355 		case IEEE80211_M_STA:
14863147Sxc151355 			IEEE80211_UNLOCK(ic);
14873147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
14887249Sff224033 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
14893147Sxc151355 			return;
14903147Sxc151355 		default:
14913147Sxc151355 			break;
14923147Sxc151355 		}
14933147Sxc151355 		break;
14943147Sxc151355 
14953147Sxc151355 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
14963147Sxc151355 		if (ic->ic_state != IEEE80211_S_RUN &&
14973147Sxc151355 		    ic->ic_state != IEEE80211_S_ASSOC &&
14983147Sxc151355 		    ic->ic_state != IEEE80211_S_AUTH)
14993147Sxc151355 			break;
15003147Sxc151355 		/*
15013147Sxc151355 		 * disassoc frame format
15023147Sxc151355 		 *	[2] reason
15033147Sxc151355 		 */
15047249Sff224033 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm), 2, break);
15059327SFei.Feng@Sun.COM 		status = LE_16(*(uint16_t *)frm);
15063147Sxc151355 
15073147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ASSOC,
15087249Sff224033 		    "recv disassociate (reason %d)\n", status);
15093147Sxc151355 		switch (ic->ic_opmode) {
15103147Sxc151355 		case IEEE80211_M_STA:
15113147Sxc151355 			IEEE80211_UNLOCK(ic);
15123147Sxc151355 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
15137249Sff224033 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
15143147Sxc151355 			return;
15153147Sxc151355 		default:
15163147Sxc151355 			break;
15173147Sxc151355 		}
15183147Sxc151355 		break;
15193147Sxc151355 
1520*10266SQuaker.Fang@Sun.COM 	case IEEE80211_FC0_SUBTYPE_ACTION:
1521*10266SQuaker.Fang@Sun.COM 		if (ic->ic_state != IEEE80211_S_RUN &&
1522*10266SQuaker.Fang@Sun.COM 		    ic->ic_state != IEEE80211_S_ASSOC &&
1523*10266SQuaker.Fang@Sun.COM 		    ic->ic_state != IEEE80211_S_AUTH)
1524*10266SQuaker.Fang@Sun.COM 			break;
1525*10266SQuaker.Fang@Sun.COM 
1526*10266SQuaker.Fang@Sun.COM 		/*
1527*10266SQuaker.Fang@Sun.COM 		 * action frame format:
1528*10266SQuaker.Fang@Sun.COM 		 *	[1] category
1529*10266SQuaker.Fang@Sun.COM 		 *	[1] action
1530*10266SQuaker.Fang@Sun.COM 		 *	[tlv] parameters
1531*10266SQuaker.Fang@Sun.COM 		 */
1532*10266SQuaker.Fang@Sun.COM 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1533*10266SQuaker.Fang@Sun.COM 		    sizeof (struct ieee80211_action), break);
1534*10266SQuaker.Fang@Sun.COM 		ia = (const struct ieee80211_action *) frm;
1535*10266SQuaker.Fang@Sun.COM 
1536*10266SQuaker.Fang@Sun.COM 		/* verify frame payloads but defer processing */
1537*10266SQuaker.Fang@Sun.COM 		/* maybe push this to method */
1538*10266SQuaker.Fang@Sun.COM 		switch (ia->ia_category) {
1539*10266SQuaker.Fang@Sun.COM 		case IEEE80211_ACTION_CAT_BA:
1540*10266SQuaker.Fang@Sun.COM 			switch (ia->ia_action) {
1541*10266SQuaker.Fang@Sun.COM 			case IEEE80211_ACTION_BA_ADDBA_REQUEST:
1542*10266SQuaker.Fang@Sun.COM 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1543*10266SQuaker.Fang@Sun.COM 			    sizeof (struct ieee80211_action_ba_addbarequest),
1544*10266SQuaker.Fang@Sun.COM 			    break);
1545*10266SQuaker.Fang@Sun.COM 			break;
1546*10266SQuaker.Fang@Sun.COM 			case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
1547*10266SQuaker.Fang@Sun.COM 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1548*10266SQuaker.Fang@Sun.COM 			    sizeof (struct ieee80211_action_ba_addbaresponse),
1549*10266SQuaker.Fang@Sun.COM 			    break);
1550*10266SQuaker.Fang@Sun.COM 			break;
1551*10266SQuaker.Fang@Sun.COM 			case IEEE80211_ACTION_BA_DELBA:
1552*10266SQuaker.Fang@Sun.COM 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1553*10266SQuaker.Fang@Sun.COM 			    sizeof (struct ieee80211_action_ba_delba),
1554*10266SQuaker.Fang@Sun.COM 			    break);
1555*10266SQuaker.Fang@Sun.COM 			break;
1556*10266SQuaker.Fang@Sun.COM 			}
1557*10266SQuaker.Fang@Sun.COM 			break;
1558*10266SQuaker.Fang@Sun.COM 		case IEEE80211_ACTION_CAT_HT:
1559*10266SQuaker.Fang@Sun.COM 			switch (ia->ia_action) {
1560*10266SQuaker.Fang@Sun.COM 			case IEEE80211_ACTION_HT_TXCHWIDTH:
1561*10266SQuaker.Fang@Sun.COM 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1562*10266SQuaker.Fang@Sun.COM 			    sizeof (struct ieee80211_action_ht_txchwidth),
1563*10266SQuaker.Fang@Sun.COM 			    break);
1564*10266SQuaker.Fang@Sun.COM 			break;
1565*10266SQuaker.Fang@Sun.COM 			}
1566*10266SQuaker.Fang@Sun.COM 			break;
1567*10266SQuaker.Fang@Sun.COM 		}
1568*10266SQuaker.Fang@Sun.COM 		ic->ic_recv_action(in, frm, efrm);
1569*10266SQuaker.Fang@Sun.COM 		break;
1570*10266SQuaker.Fang@Sun.COM 
15713147Sxc151355 	default:
15723147Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_recv_mgmt: "
15737249Sff224033 		    "subtype 0x%x not handled\n", subtype);
15743147Sxc151355 		break;
15753147Sxc151355 	} /* switch subtype */
15763147Sxc151355 out:
15773147Sxc151355 	IEEE80211_UNLOCK(ic);
15783147Sxc151355 }
1579