xref: /freebsd/sys/net80211/ieee80211_input.c (revision bc5627d91f8cce793deecb3472b8fa27e9b335cd)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
31f1d7810SSam Leffler  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
147535e66aSSam Leffler  * 3. The name of the author may not be used to endorse or promote products
157535e66aSSam Leffler  *    derived from this software without specific prior written permission.
161a1e1d21SSam Leffler  *
171a1e1d21SSam Leffler  * Alternatively, this software may be distributed under the terms of the
181a1e1d21SSam Leffler  * GNU General Public License ("GPL") version 2 as published by the Free
191a1e1d21SSam Leffler  * Software Foundation.
201a1e1d21SSam Leffler  *
217535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
227535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
237535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
247535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
257535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
267535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
277535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
287535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
297535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
307535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311a1e1d21SSam Leffler  */
321a1e1d21SSam Leffler 
331a1e1d21SSam Leffler #include <sys/cdefs.h>
341a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
351a1e1d21SSam Leffler 
361a1e1d21SSam Leffler #include <sys/param.h>
371a1e1d21SSam Leffler #include <sys/systm.h>
381a1e1d21SSam Leffler #include <sys/mbuf.h>
391a1e1d21SSam Leffler #include <sys/malloc.h>
401a1e1d21SSam Leffler #include <sys/endian.h>
41a0cc3f85SSam Leffler #include <sys/kernel.h>
421a1e1d21SSam Leffler 
438a1b9b6aSSam Leffler #include <sys/socket.h>
441a1e1d21SSam Leffler 
451a1e1d21SSam Leffler #include <net/if.h>
461a1e1d21SSam Leffler #include <net/if_media.h>
471a1e1d21SSam Leffler #include <net/ethernet.h>
481a1e1d21SSam Leffler #include <net/if_llc.h>
498a1b9b6aSSam Leffler #include <net/if_vlan_var.h>
501a1e1d21SSam Leffler 
511a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
521a1e1d21SSam Leffler 
531a1e1d21SSam Leffler #include <net/bpf.h>
541a1e1d21SSam Leffler 
558a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
568a1b9b6aSSam Leffler #include <machine/stdarg.h>
578a1b9b6aSSam Leffler 
588a1b9b6aSSam Leffler /*
598a1b9b6aSSam Leffler  * Decide if a received management frame should be
608a1b9b6aSSam Leffler  * printed when debugging is enabled.  This filters some
618a1b9b6aSSam Leffler  * of the less interesting frames that come frequently
628a1b9b6aSSam Leffler  * (e.g. beacons).
638a1b9b6aSSam Leffler  */
648a1b9b6aSSam Leffler static __inline int
658a1b9b6aSSam Leffler doprint(struct ieee80211com *ic, int subtype)
668a1b9b6aSSam Leffler {
678a1b9b6aSSam Leffler 	switch (subtype) {
688a1b9b6aSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
698a1b9b6aSSam Leffler 		return (ic->ic_flags & IEEE80211_F_SCAN);
708a1b9b6aSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
718a1b9b6aSSam Leffler 		return (ic->ic_opmode == IEEE80211_M_IBSS);
728a1b9b6aSSam Leffler 	}
738a1b9b6aSSam Leffler 	return 1;
748a1b9b6aSSam Leffler }
758a1b9b6aSSam Leffler 
768a1b9b6aSSam Leffler /*
778a1b9b6aSSam Leffler  * Emit a debug message about discarding a frame or information
788a1b9b6aSSam Leffler  * element.  One format is for extracting the mac address from
798a1b9b6aSSam Leffler  * the frame header; the other is for when a header is not
808a1b9b6aSSam Leffler  * available or otherwise appropriate.
818a1b9b6aSSam Leffler  */
828a1b9b6aSSam Leffler #define	IEEE80211_DISCARD(_ic, _m, _wh, _type, _fmt, ...) do {		\
838a1b9b6aSSam Leffler 	if ((_ic)->ic_debug & (_m))					\
848a1b9b6aSSam Leffler 		ieee80211_discard_frame(_ic, _wh, _type, _fmt, __VA_ARGS__);\
858a1b9b6aSSam Leffler } while (0)
868a1b9b6aSSam Leffler #define	IEEE80211_DISCARD_IE(_ic, _m, _wh, _type, _fmt, ...) do {	\
878a1b9b6aSSam Leffler 	if ((_ic)->ic_debug & (_m))					\
888a1b9b6aSSam Leffler 		ieee80211_discard_ie(_ic, _wh, _type, _fmt, __VA_ARGS__);\
898a1b9b6aSSam Leffler } while (0)
908a1b9b6aSSam Leffler #define	IEEE80211_DISCARD_MAC(_ic, _m, _mac, _type, _fmt, ...) do {	\
918a1b9b6aSSam Leffler 	if ((_ic)->ic_debug & (_m))					\
928a1b9b6aSSam Leffler 		ieee80211_discard_mac(_ic, _mac, _type, _fmt, __VA_ARGS__);\
938a1b9b6aSSam Leffler } while (0)
948a1b9b6aSSam Leffler 
958a1b9b6aSSam Leffler static const u_int8_t *ieee80211_getbssid(struct ieee80211com *,
968a1b9b6aSSam Leffler 	const struct ieee80211_frame *);
978a1b9b6aSSam Leffler static void ieee80211_discard_frame(struct ieee80211com *,
988a1b9b6aSSam Leffler 	const struct ieee80211_frame *, const char *type, const char *fmt, ...);
998a1b9b6aSSam Leffler static void ieee80211_discard_ie(struct ieee80211com *,
1008a1b9b6aSSam Leffler 	const struct ieee80211_frame *, const char *type, const char *fmt, ...);
1018a1b9b6aSSam Leffler static void ieee80211_discard_mac(struct ieee80211com *,
1028a1b9b6aSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN], const char *type,
1038a1b9b6aSSam Leffler 	const char *fmt, ...);
1048a1b9b6aSSam Leffler #else
1058a1b9b6aSSam Leffler #define	IEEE80211_DISCARD(_ic, _m, _wh, _type, _fmt, ...)
1068a1b9b6aSSam Leffler #define	IEEE80211_DISCARD_IE(_ic, _m, _wh, _type, _fmt, ...)
1078a1b9b6aSSam Leffler #define	IEEE80211_DISCARD_MAC(_ic, _m, _mac, _type, _fmt, ...)
1088a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
1098a1b9b6aSSam Leffler 
1108a1b9b6aSSam Leffler static struct mbuf *ieee80211_defrag(struct ieee80211com *,
1112cc12adeSSam Leffler 	struct ieee80211_node *, struct mbuf *, int);
1122cc12adeSSam Leffler static struct mbuf *ieee80211_decap(struct ieee80211com *, struct mbuf *, int);
11384eb84c4SSam Leffler static void ieee80211_send_error(struct ieee80211com *, struct ieee80211_node *,
11484eb84c4SSam Leffler 		const u_int8_t *mac, int subtype, int arg);
1158a1b9b6aSSam Leffler static void ieee80211_node_pwrsave(struct ieee80211_node *, int enable);
1168a1b9b6aSSam Leffler static void ieee80211_recv_pspoll(struct ieee80211com *,
1178a1b9b6aSSam Leffler 	struct ieee80211_node *, struct mbuf *);
1181a1e1d21SSam Leffler 
1190a915fadSSam Leffler /*
1200a915fadSSam Leffler  * Process a received frame.  The node associated with the sender
1210a915fadSSam Leffler  * should be supplied.  If nothing was found in the node table then
1220a915fadSSam Leffler  * the caller is assumed to supply a reference to ic_bss instead.
1230a915fadSSam Leffler  * The RSSI and a timestamp are also supplied.  The RSSI data is used
1240a915fadSSam Leffler  * during AP scanning to select a AP to associate with; it can have
1250a915fadSSam Leffler  * any units so long as values have consistent units and higher values
1260a915fadSSam Leffler  * mean ``better signal''.  The receive timestamp is currently not used
1270a915fadSSam Leffler  * by the 802.11 layer.
1280a915fadSSam Leffler  */
1291f298879SSam Leffler int
1308a1b9b6aSSam Leffler ieee80211_input(struct ieee80211com *ic, struct mbuf *m,
1318a1b9b6aSSam Leffler 	struct ieee80211_node *ni, int rssi, u_int32_t rstamp)
1321a1e1d21SSam Leffler {
1338a1b9b6aSSam Leffler #define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
1348a1b9b6aSSam Leffler #define	HAS_SEQ(type)	((type & 0x4) == 0)
1358a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1361a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
1378a1b9b6aSSam Leffler 	struct ieee80211_key *key;
1381a1e1d21SSam Leffler 	struct ether_header *eh;
1392cc12adeSSam Leffler 	int len, hdrspace;
14018f897abSSam Leffler 	u_int8_t dir, type, subtype;
1411a1e1d21SSam Leffler 	u_int8_t *bssid;
1421a1e1d21SSam Leffler 	u_int16_t rxseq;
1431a1e1d21SSam Leffler 
1440a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
1458a1b9b6aSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
1460a915fadSSam Leffler 
14718f897abSSam Leffler 	/* trim CRC here so WEP can find its own CRC at the end of packet. */
1481a1e1d21SSam Leffler 	if (m->m_flags & M_HASFCS) {
1491a1e1d21SSam Leffler 		m_adj(m, -IEEE80211_CRC_LEN);
1501a1e1d21SSam Leffler 		m->m_flags &= ~M_HASFCS;
1511a1e1d21SSam Leffler 	}
15218f897abSSam Leffler 	KASSERT(m->m_pkthdr.len >= sizeof(struct ieee80211_frame_min),
15318f897abSSam Leffler 		("frame length too short: %u", m->m_pkthdr.len));
15418f897abSSam Leffler 
1551f298879SSam Leffler 	type = -1;			/* undefined */
15618f897abSSam Leffler 	/*
15718f897abSSam Leffler 	 * In monitor mode, send everything directly to bpf.
15818f897abSSam Leffler 	 * XXX may want to include the CRC
15918f897abSSam Leffler 	 */
16018f897abSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
16118f897abSSam Leffler 		goto out;
1621a1e1d21SSam Leffler 
1638a1b9b6aSSam Leffler 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1648a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
1658a1b9b6aSSam Leffler 		    ni->ni_macaddr, NULL,
1668a1b9b6aSSam Leffler 		    "too short (1): len %u", m->m_pkthdr.len);
1678a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_tooshort++;
1688a1b9b6aSSam Leffler 		goto out;
1698a1b9b6aSSam Leffler 	}
1708a1b9b6aSSam Leffler 	/*
1718a1b9b6aSSam Leffler 	 * Bit of a cheat here, we use a pointer for a 3-address
1728a1b9b6aSSam Leffler 	 * frame format but don't reference fields past outside
1738a1b9b6aSSam Leffler 	 * ieee80211_frame_min w/o first validating the data is
1748a1b9b6aSSam Leffler 	 * present.
1758a1b9b6aSSam Leffler 	 */
1761a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
1778a1b9b6aSSam Leffler 
1781a1e1d21SSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1791a1e1d21SSam Leffler 	    IEEE80211_FC0_VERSION_0) {
1808a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
1818a1b9b6aSSam Leffler 		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1821be50176SSam Leffler 		ic->ic_stats.is_rx_badversion++;
1831a1e1d21SSam Leffler 		goto err;
1841a1e1d21SSam Leffler 	}
1851a1e1d21SSam Leffler 
1861a1e1d21SSam Leffler 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
18718f897abSSam Leffler 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1888a1b9b6aSSam Leffler 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1898a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1901a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
1911a1e1d21SSam Leffler 		case IEEE80211_M_STA:
1928a1b9b6aSSam Leffler 			bssid = wh->i_addr2;
1938a1b9b6aSSam Leffler 			if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
1941be50176SSam Leffler 				/* not interested in */
1958a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
1968a1b9b6aSSam Leffler 				    bssid, NULL, "%s", "not to bss");
1971be50176SSam Leffler 				ic->ic_stats.is_rx_wrongbss++;
1981a1e1d21SSam Leffler 				goto out;
1991a1e1d21SSam Leffler 			}
2001a1e1d21SSam Leffler 			break;
2011a1e1d21SSam Leffler 		case IEEE80211_M_IBSS:
2021a1e1d21SSam Leffler 		case IEEE80211_M_AHDEMO:
2031a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
2048a1b9b6aSSam Leffler 			if (dir != IEEE80211_FC1_DIR_NODS)
2051a1e1d21SSam Leffler 				bssid = wh->i_addr1;
2068a1b9b6aSSam Leffler 			else if (type == IEEE80211_FC0_TYPE_CTL)
2078a1b9b6aSSam Leffler 				bssid = wh->i_addr1;
2088a1b9b6aSSam Leffler 			else {
2098a1b9b6aSSam Leffler 				if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
2108a1b9b6aSSam Leffler 					IEEE80211_DISCARD_MAC(ic,
2118a1b9b6aSSam Leffler 					    IEEE80211_MSG_ANY, ni->ni_macaddr,
2128a1b9b6aSSam Leffler 					    NULL, "too short (2): len %u",
2138a1b9b6aSSam Leffler 					    m->m_pkthdr.len);
2148a1b9b6aSSam Leffler 					ic->ic_stats.is_rx_tooshort++;
2158a1b9b6aSSam Leffler 					goto out;
2168a1b9b6aSSam Leffler 				}
2178a1b9b6aSSam Leffler 				bssid = wh->i_addr3;
2188a1b9b6aSSam Leffler 			}
219bc031652SSam Leffler 			if (type != IEEE80211_FC0_TYPE_DATA)
220bc031652SSam Leffler 				break;
2218a1b9b6aSSam Leffler 			/*
2228a1b9b6aSSam Leffler 			 * Data frame, validate the bssid.
2238a1b9b6aSSam Leffler 			 */
2241a1e1d21SSam Leffler 			if (!IEEE80211_ADDR_EQ(bssid, ic->ic_bss->ni_bssid) &&
2251a1e1d21SSam Leffler 			    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
2261a1e1d21SSam Leffler 				/* not interested in */
2278a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
2288a1b9b6aSSam Leffler 				    bssid, NULL, "%s", "not to bss");
2291be50176SSam Leffler 				ic->ic_stats.is_rx_wrongbss++;
2301a1e1d21SSam Leffler 				goto out;
2311a1e1d21SSam Leffler 			}
2328a1b9b6aSSam Leffler 			/*
2338a1b9b6aSSam Leffler 			 * For adhoc mode we cons up a node when it doesn't
2348a1b9b6aSSam Leffler 			 * exist. This should probably done after an ACL check.
2358a1b9b6aSSam Leffler 			 */
2368a1b9b6aSSam Leffler 			if (ni == ic->ic_bss &&
2378a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_HOSTAP) {
2388a1b9b6aSSam Leffler 				/*
2398a1b9b6aSSam Leffler 				 * Fake up a node for this newly
2408a1b9b6aSSam Leffler 				 * discovered member of the IBSS.
2418a1b9b6aSSam Leffler 				 */
242acc4f7f5SSam Leffler 				ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
2430796482dSSam Leffler 						    wh->i_addr2);
2448a1b9b6aSSam Leffler 				if (ni == NULL) {
2458a1b9b6aSSam Leffler 					/* NB: stat kept for alloc failure */
2468a1b9b6aSSam Leffler 					goto err;
2478a1b9b6aSSam Leffler 				}
2488a1b9b6aSSam Leffler 			}
2491a1e1d21SSam Leffler 			break;
2501a1e1d21SSam Leffler 		default:
2518a1b9b6aSSam Leffler 			goto out;
2521a1e1d21SSam Leffler 		}
2531a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
2541a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
2558a1b9b6aSSam Leffler 		if (HAS_SEQ(type)) {
2568a1b9b6aSSam Leffler 			u_int8_t tid;
2578a1b9b6aSSam Leffler 			if (IEEE80211_QOS_HAS_SEQ(wh)) {
2588a1b9b6aSSam Leffler 				tid = ((struct ieee80211_qosframe *)wh)->
2598a1b9b6aSSam Leffler 					i_qos[0] & IEEE80211_QOS_TID;
2608a1b9b6aSSam Leffler 				if (tid >= WME_AC_VI)
2618a1b9b6aSSam Leffler 					ic->ic_wme.wme_hipri_traffic++;
2628a1b9b6aSSam Leffler 				tid++;
2638a1b9b6aSSam Leffler 			} else
2648a1b9b6aSSam Leffler 				tid = 0;
2658a1b9b6aSSam Leffler 			rxseq = le16toh(*(u_int16_t *)wh->i_seq);
2661a1e1d21SSam Leffler 			if ((wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
2678a1b9b6aSSam Leffler 			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
2688a1b9b6aSSam Leffler 				/* duplicate, discard */
2698a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
2708a1b9b6aSSam Leffler 				    bssid, "duplicate",
2718a1b9b6aSSam Leffler 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
2728a1b9b6aSSam Leffler 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
2738a1b9b6aSSam Leffler 				    ni->ni_rxseqs[tid] >>
2748a1b9b6aSSam Leffler 					IEEE80211_SEQ_SEQ_SHIFT,
2758a1b9b6aSSam Leffler 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
2768a1b9b6aSSam Leffler 				    ni->ni_rxseqs[tid] &
2778a1b9b6aSSam Leffler 					IEEE80211_SEQ_FRAG_MASK,
2788a1b9b6aSSam Leffler 				    tid);
2798a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_dup++;
2808a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_dup);
2811a1e1d21SSam Leffler 				goto out;
2821a1e1d21SSam Leffler 			}
2838a1b9b6aSSam Leffler 			ni->ni_rxseqs[tid] = rxseq;
2848a1b9b6aSSam Leffler 		}
2851a1e1d21SSam Leffler 	}
2861a1e1d21SSam Leffler 
28718f897abSSam Leffler 	switch (type) {
2881a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
2892cc12adeSSam Leffler 		hdrspace = ieee80211_hdrspace(ic, wh);
2902cc12adeSSam Leffler 		if (m->m_len < hdrspace &&
2912cc12adeSSam Leffler 		    (m = m_pullup(m, hdrspace)) == NULL) {
29220098591SSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
29320098591SSam Leffler 			    ni->ni_macaddr, NULL,
2942cc12adeSSam Leffler 			    "data too short: expecting %u", hdrspace);
2958a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_tooshort++;
2968a1b9b6aSSam Leffler 			goto out;		/* XXX */
2978a1b9b6aSSam Leffler 		}
2981a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
2991a1e1d21SSam Leffler 		case IEEE80211_M_STA:
3001be50176SSam Leffler 			if (dir != IEEE80211_FC1_DIR_FROMDS) {
3011be50176SSam Leffler 				ic->ic_stats.is_rx_wrongdir++;
3021a1e1d21SSam Leffler 				goto out;
3031be50176SSam Leffler 			}
3041a1e1d21SSam Leffler 			if ((ifp->if_flags & IFF_SIMPLEX) &&
3051a1e1d21SSam Leffler 			    IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3061a1e1d21SSam Leffler 			    IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_myaddr)) {
3071a1e1d21SSam Leffler 				/*
3081a1e1d21SSam Leffler 				 * In IEEE802.11 network, multicast packet
3091a1e1d21SSam Leffler 				 * sent from me is broadcasted from AP.
3101a1e1d21SSam Leffler 				 * It should be silently discarded for
3111a1e1d21SSam Leffler 				 * SIMPLEX interface.
3121a1e1d21SSam Leffler 				 */
3138a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3148a1b9b6aSSam Leffler 				    wh, NULL, "%s", "multicast echo");
3151be50176SSam Leffler 				ic->ic_stats.is_rx_mcastecho++;
3161a1e1d21SSam Leffler 				goto out;
3171a1e1d21SSam Leffler 			}
3181a1e1d21SSam Leffler 			break;
3191a1e1d21SSam Leffler 		case IEEE80211_M_IBSS:
3201a1e1d21SSam Leffler 		case IEEE80211_M_AHDEMO:
3211be50176SSam Leffler 			if (dir != IEEE80211_FC1_DIR_NODS) {
3221be50176SSam Leffler 				ic->ic_stats.is_rx_wrongdir++;
3231a1e1d21SSam Leffler 				goto out;
3241be50176SSam Leffler 			}
3258a1b9b6aSSam Leffler 			/* XXX no power-save support */
3261a1e1d21SSam Leffler 			break;
3271a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
3281be50176SSam Leffler 			if (dir != IEEE80211_FC1_DIR_TODS) {
3291be50176SSam Leffler 				ic->ic_stats.is_rx_wrongdir++;
3301a1e1d21SSam Leffler 				goto out;
3311be50176SSam Leffler 			}
3321a1e1d21SSam Leffler 			/* check if source STA is associated */
3330a915fadSSam Leffler 			if (ni == ic->ic_bss) {
3348a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3358a1b9b6aSSam Leffler 				    wh, "data", "%s", "unknown src");
33684eb84c4SSam Leffler 				ieee80211_send_error(ic, ni, wh->i_addr2,
3371a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
3381a1e1d21SSam Leffler 				    IEEE80211_REASON_NOT_AUTHED);
3391be50176SSam Leffler 				ic->ic_stats.is_rx_notassoc++;
3401a1e1d21SSam Leffler 				goto err;
3411a1e1d21SSam Leffler 			}
3421a1e1d21SSam Leffler 			if (ni->ni_associd == 0) {
3438a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3448a1b9b6aSSam Leffler 				    wh, "data", "%s", "unassoc src");
3451a1e1d21SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
3461a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
3471a1e1d21SSam Leffler 				    IEEE80211_REASON_NOT_ASSOCED);
3481be50176SSam Leffler 				ic->ic_stats.is_rx_notassoc++;
3491a1e1d21SSam Leffler 				goto err;
3501a1e1d21SSam Leffler 			}
3518a1b9b6aSSam Leffler 
3528a1b9b6aSSam Leffler 			/*
3538a1b9b6aSSam Leffler 			 * Check for power save state change.
3548a1b9b6aSSam Leffler 			 */
3558a1b9b6aSSam Leffler 			if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
3568a1b9b6aSSam Leffler 			    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
3578a1b9b6aSSam Leffler 				ieee80211_node_pwrsave(ni,
3588a1b9b6aSSam Leffler 					wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
3591a1e1d21SSam Leffler 			break;
3608a1b9b6aSSam Leffler 		default:
3618a1b9b6aSSam Leffler 			/* XXX here to keep compiler happy */
3628a1b9b6aSSam Leffler 			goto out;
3631a1e1d21SSam Leffler 		}
3648a1b9b6aSSam Leffler 
3658a1b9b6aSSam Leffler 		/*
3668a1b9b6aSSam Leffler 		 * Handle privacy requirements.  Note that we
3678a1b9b6aSSam Leffler 		 * must not be preempted from here until after
3688a1b9b6aSSam Leffler 		 * we (potentially) call ieee80211_crypto_demic;
3698a1b9b6aSSam Leffler 		 * otherwise we may violate assumptions in the
3708a1b9b6aSSam Leffler 		 * crypto cipher modules used to do delayed update
3718a1b9b6aSSam Leffler 		 * of replay sequence numbers.
3728a1b9b6aSSam Leffler 		 */
3731a1e1d21SSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
3748a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
3758a1b9b6aSSam Leffler 				/*
3768a1b9b6aSSam Leffler 				 * Discard encrypted frames when privacy is off.
3778a1b9b6aSSam Leffler 				 */
3788a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3798a1b9b6aSSam Leffler 				    wh, "WEP", "%s", "PRIVACY off");
3808a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_noprivacy++;
3818a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
3828a1b9b6aSSam Leffler 				goto out;
3838a1b9b6aSSam Leffler 			}
3842cc12adeSSam Leffler 			key = ieee80211_crypto_decap(ic, ni, m, hdrspace);
3858a1b9b6aSSam Leffler 			if (key == NULL) {
3868a1b9b6aSSam Leffler 				/* NB: stats+msgs handled in crypto_decap */
3878a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_wepfail);
3888a1b9b6aSSam Leffler 				goto out;
3891be50176SSam Leffler 			}
3901a1e1d21SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
3918116d318SSam Leffler 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3921be50176SSam Leffler 		} else {
3938a1b9b6aSSam Leffler 			key = NULL;
3948a1b9b6aSSam Leffler 		}
3958a1b9b6aSSam Leffler 
3968a1b9b6aSSam Leffler 		/*
3978a1b9b6aSSam Leffler 		 * Next up, any fragmentation.
3988a1b9b6aSSam Leffler 		 */
3998a1b9b6aSSam Leffler 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
4002cc12adeSSam Leffler 			m = ieee80211_defrag(ic, ni, m, hdrspace);
4018a1b9b6aSSam Leffler 			if (m == NULL) {
4028a1b9b6aSSam Leffler 				/* Fragment dropped or frame not complete yet */
4031a1e1d21SSam Leffler 				goto out;
4041a1e1d21SSam Leffler 			}
4051be50176SSam Leffler 		}
4068a1b9b6aSSam Leffler 		wh = NULL;		/* no longer valid, catch any uses */
4078a1b9b6aSSam Leffler 
4088a1b9b6aSSam Leffler 		/*
4098a1b9b6aSSam Leffler 		 * Next strip any MSDU crypto bits.
4108a1b9b6aSSam Leffler 		 */
41196d88463SSam Leffler 		if (key != NULL && !ieee80211_crypto_demic(ic, key, m, 0)) {
4128a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
4138a1b9b6aSSam Leffler 			    ni->ni_macaddr, "data", "%s", "demic error");
4148a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_demicfail);
4158a1b9b6aSSam Leffler 			goto out;
4168a1b9b6aSSam Leffler 		}
4178a1b9b6aSSam Leffler 
4181a1e1d21SSam Leffler 		/* copy to listener after decrypt */
4191a1e1d21SSam Leffler 		if (ic->ic_rawbpf)
4201a1e1d21SSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
4218a1b9b6aSSam Leffler 
4228a1b9b6aSSam Leffler 		/*
4238a1b9b6aSSam Leffler 		 * Finally, strip the 802.11 header.
4248a1b9b6aSSam Leffler 		 */
4252cc12adeSSam Leffler 		m = ieee80211_decap(ic, m, hdrspace);
4261be50176SSam Leffler 		if (m == NULL) {
4278a1b9b6aSSam Leffler 			/* don't count Null data frames as errors */
4288a1b9b6aSSam Leffler 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA)
4298a1b9b6aSSam Leffler 				goto out;
4308a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
4318a1b9b6aSSam Leffler 			    ni->ni_macaddr, "data", "%s", "decap error");
4321be50176SSam Leffler 			ic->ic_stats.is_rx_decap++;
4338a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_decap);
4341a1e1d21SSam Leffler 			goto err;
4351be50176SSam Leffler 		}
4368a1b9b6aSSam Leffler 		eh = mtod(m, struct ether_header *);
4378a1b9b6aSSam Leffler 		if (!ieee80211_node_is_authorized(ni)) {
4388a1b9b6aSSam Leffler 			/*
4398a1b9b6aSSam Leffler 			 * Deny any non-PAE frames received prior to
4408a1b9b6aSSam Leffler 			 * authorization.  For open/shared-key
4418a1b9b6aSSam Leffler 			 * authentication the port is mark authorized
4428a1b9b6aSSam Leffler 			 * after authentication completes.  For 802.1x
4438a1b9b6aSSam Leffler 			 * the port is not marked authorized by the
4448a1b9b6aSSam Leffler 			 * authenticator until the handshake has completed.
4458a1b9b6aSSam Leffler 			 */
4468a1b9b6aSSam Leffler 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
4478a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
4488a1b9b6aSSam Leffler 				    eh->ether_shost, "data",
4498a1b9b6aSSam Leffler 				    "unauthorized port: ether type 0x%x len %u",
4508a1b9b6aSSam Leffler 				    eh->ether_type, m->m_pkthdr.len);
4518a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_unauth++;
4528a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unauth);
4538a1b9b6aSSam Leffler 				goto err;
4548a1b9b6aSSam Leffler 			}
4558a1b9b6aSSam Leffler 		} else {
4568a1b9b6aSSam Leffler 			/*
4578a1b9b6aSSam Leffler 			 * When denying unencrypted frames, discard
4588a1b9b6aSSam Leffler 			 * any non-PAE frames received without encryption.
4598a1b9b6aSSam Leffler 			 */
4608a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_DROPUNENC) &&
4618a1b9b6aSSam Leffler 			    key == NULL &&
4628a1b9b6aSSam Leffler 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
4638a1b9b6aSSam Leffler 				/*
4648a1b9b6aSSam Leffler 				 * Drop unencrypted frames.
4658a1b9b6aSSam Leffler 				 */
4668a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_unencrypted++;
4678a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
4688a1b9b6aSSam Leffler 				goto out;
4698a1b9b6aSSam Leffler 			}
4708a1b9b6aSSam Leffler 		}
4711a1e1d21SSam Leffler 		ifp->if_ipackets++;
4728a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_data);
4738a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
4741a1e1d21SSam Leffler 
4751a1e1d21SSam Leffler 		/* perform as a bridge within the AP */
4768a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
4778a1b9b6aSSam Leffler 		    (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0) {
4788a1b9b6aSSam Leffler 			struct mbuf *m1 = NULL;
4798a1b9b6aSSam Leffler 
4801a1e1d21SSam Leffler 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
4810a915fadSSam Leffler 				m1 = m_copypacket(m, M_DONTWAIT);
4821a1e1d21SSam Leffler 				if (m1 == NULL)
4831a1e1d21SSam Leffler 					ifp->if_oerrors++;
4841a1e1d21SSam Leffler 				else
4851a1e1d21SSam Leffler 					m1->m_flags |= M_MCAST;
4861a1e1d21SSam Leffler 			} else {
4878a1b9b6aSSam Leffler 				/* XXX this dups work done in ieee80211_encap */
4888a1b9b6aSSam Leffler 				/* check if destination is associated */
4898a1b9b6aSSam Leffler 				struct ieee80211_node *ni1 =
490acc4f7f5SSam Leffler 				    ieee80211_find_node(&ic->ic_sta,
4918a1b9b6aSSam Leffler 							eh->ether_dhost);
4928a1b9b6aSSam Leffler 				if (ni1 != NULL) {
4938a1b9b6aSSam Leffler 					/* XXX check if authorized */
4948a1b9b6aSSam Leffler 					if (ni1->ni_associd != 0) {
4951a1e1d21SSam Leffler 						m1 = m;
4961a1e1d21SSam Leffler 						m = NULL;
4971a1e1d21SSam Leffler 					}
4988a1b9b6aSSam Leffler 					/* XXX statistic? */
4998a1b9b6aSSam Leffler 					ieee80211_free_node(ni1);
5001a1e1d21SSam Leffler 				}
5011a1e1d21SSam Leffler 			}
5021a1e1d21SSam Leffler 			if (m1 != NULL) {
5031a1e1d21SSam Leffler 				len = m1->m_pkthdr.len;
5041a1e1d21SSam Leffler 				IF_ENQUEUE(&ifp->if_snd, m1);
5051a1e1d21SSam Leffler 				if (m != NULL)
5061a1e1d21SSam Leffler 					ifp->if_omcasts++;
5071a1e1d21SSam Leffler 				ifp->if_obytes += len;
5081a1e1d21SSam Leffler 			}
5091a1e1d21SSam Leffler 		}
5108a1b9b6aSSam Leffler 		if (m != NULL) {
5118a1b9b6aSSam Leffler 			if (ni->ni_vlan != 0) {
5128a1b9b6aSSam Leffler 				/* attach vlan tag */
5138a1b9b6aSSam Leffler 				/* XXX goto err? */
5148a1b9b6aSSam Leffler 				VLAN_INPUT_TAG(ifp, m, ni->ni_vlan, goto out);
5158a1b9b6aSSam Leffler 			}
5161a1e1d21SSam Leffler 			(*ifp->if_input)(ifp, m);
5178a1b9b6aSSam Leffler 		}
5181f298879SSam Leffler 		return IEEE80211_FC0_TYPE_DATA;
5191a1e1d21SSam Leffler 
5201a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
5218a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_mgmt);
5221be50176SSam Leffler 		if (dir != IEEE80211_FC1_DIR_NODS) {
5231be50176SSam Leffler 			ic->ic_stats.is_rx_wrongdir++;
5241a1e1d21SSam Leffler 			goto err;
5251be50176SSam Leffler 		}
5268a1b9b6aSSam Leffler 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
5278a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
5288a1b9b6aSSam Leffler 			    ni->ni_macaddr, "mgt", "too short: len %u",
5298a1b9b6aSSam Leffler 			    m->m_pkthdr.len);
5308a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_tooshort++;
5311a1e1d21SSam Leffler 			goto out;
5321be50176SSam Leffler 		}
5331a1e1d21SSam Leffler #ifdef IEEE80211_DEBUG
5348a1b9b6aSSam Leffler 		if ((ieee80211_msg_debug(ic) && doprint(ic, subtype)) ||
5358a1b9b6aSSam Leffler 		    ieee80211_msg_dumppkts(ic)) {
5368a1b9b6aSSam Leffler 			if_printf(ic->ic_ifp, "received %s from %s rssi %d\n",
5378a1b9b6aSSam Leffler 			    ieee80211_mgt_subtype_name[subtype >>
5388a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
5391a1e1d21SSam Leffler 			    ether_sprintf(wh->i_addr2), rssi);
5401a1e1d21SSam Leffler 		}
5418a1b9b6aSSam Leffler #endif
5428a1b9b6aSSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
5438a1b9b6aSSam Leffler 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
5448a1b9b6aSSam Leffler 				/*
5458a1b9b6aSSam Leffler 				 * Only shared key auth frames with a challenge
5468a1b9b6aSSam Leffler 				 * should be encrypted, discard all others.
5478a1b9b6aSSam Leffler 				 */
5488a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
5498a1b9b6aSSam Leffler 				    wh, ieee80211_mgt_subtype_name[subtype >>
5508a1b9b6aSSam Leffler 					IEEE80211_FC0_SUBTYPE_SHIFT],
5518a1b9b6aSSam Leffler 				    "%s", "WEP set but not permitted");
5528a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_mgtdiscard++; /* XXX */
5538a1b9b6aSSam Leffler 				goto out;
5548a1b9b6aSSam Leffler 			}
5558a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
5568a1b9b6aSSam Leffler 				/*
5578a1b9b6aSSam Leffler 				 * Discard encrypted frames when privacy is off.
5588a1b9b6aSSam Leffler 				 */
5598a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
5608a1b9b6aSSam Leffler 				    wh, "mgt", "%s", "WEP set but PRIVACY off");
5618a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_noprivacy++;
5628a1b9b6aSSam Leffler 				goto out;
5638a1b9b6aSSam Leffler 			}
5642cc12adeSSam Leffler 			hdrspace = ieee80211_hdrspace(ic, wh);
5652cc12adeSSam Leffler 			key = ieee80211_crypto_decap(ic, ni, m, hdrspace);
5668a1b9b6aSSam Leffler 			if (key == NULL) {
5678a1b9b6aSSam Leffler 				/* NB: stats+msgs handled in crypto_decap */
5688a1b9b6aSSam Leffler 				goto out;
5698a1b9b6aSSam Leffler 			}
5708116d318SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
5718116d318SSam Leffler 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
5728a1b9b6aSSam Leffler 		}
5731a1e1d21SSam Leffler 		if (ic->ic_rawbpf)
5741a1e1d21SSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
5750a915fadSSam Leffler 		(*ic->ic_recv_mgmt)(ic, m, ni, subtype, rssi, rstamp);
5761a1e1d21SSam Leffler 		m_freem(m);
5771f298879SSam Leffler 		return type;
5781a1e1d21SSam Leffler 
5791a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_CTL:
5808a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_ctrl);
5811be50176SSam Leffler 		ic->ic_stats.is_rx_ctl++;
5828a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
5838a1b9b6aSSam Leffler 			switch (subtype) {
5848a1b9b6aSSam Leffler 			case IEEE80211_FC0_SUBTYPE_PS_POLL:
5858a1b9b6aSSam Leffler 				ieee80211_recv_pspoll(ic, ni, m);
5868a1b9b6aSSam Leffler 				break;
5878a1b9b6aSSam Leffler 			}
5888a1b9b6aSSam Leffler 		}
58918f897abSSam Leffler 		goto out;
5901a1e1d21SSam Leffler 	default:
5918a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
5928a1b9b6aSSam Leffler 		    wh, NULL, "bad frame type 0x%x", type);
5931a1e1d21SSam Leffler 		/* should not come here */
5941a1e1d21SSam Leffler 		break;
5951a1e1d21SSam Leffler 	}
5961a1e1d21SSam Leffler err:
5971a1e1d21SSam Leffler 	ifp->if_ierrors++;
5981a1e1d21SSam Leffler out:
5991a1e1d21SSam Leffler 	if (m != NULL) {
6001a1e1d21SSam Leffler 		if (ic->ic_rawbpf)
6011a1e1d21SSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
6021a1e1d21SSam Leffler 		m_freem(m);
6031a1e1d21SSam Leffler 	}
6041f298879SSam Leffler 	return type;
6058a1b9b6aSSam Leffler #undef SEQ_LEQ
6061a1e1d21SSam Leffler }
6071a1e1d21SSam Leffler 
6088a1b9b6aSSam Leffler /*
6098a1b9b6aSSam Leffler  * This function reassemble fragments.
6108a1b9b6aSSam Leffler  */
6118a1b9b6aSSam Leffler static struct mbuf *
6128a1b9b6aSSam Leffler ieee80211_defrag(struct ieee80211com *ic, struct ieee80211_node *ni,
6132cc12adeSSam Leffler 	struct mbuf *m, int hdrspace)
6141a1e1d21SSam Leffler {
6158a1b9b6aSSam Leffler 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
6168a1b9b6aSSam Leffler 	struct ieee80211_frame *lwh;
6178a1b9b6aSSam Leffler 	u_int16_t rxseq;
6188a1b9b6aSSam Leffler 	u_int8_t fragno;
6198a1b9b6aSSam Leffler 	u_int8_t more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG;
6208a1b9b6aSSam Leffler 	struct mbuf *mfrag;
6218a1b9b6aSSam Leffler 
6228a1b9b6aSSam Leffler 	KASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1), ("multicast fragm?"));
6238a1b9b6aSSam Leffler 
6248a1b9b6aSSam Leffler 	rxseq = le16toh(*(u_int16_t *)wh->i_seq);
6258a1b9b6aSSam Leffler 	fragno = rxseq & IEEE80211_SEQ_FRAG_MASK;
6268a1b9b6aSSam Leffler 
6278a1b9b6aSSam Leffler 	/* Quick way out, if there's nothing to defragment */
6288a1b9b6aSSam Leffler 	if (!more_frag && fragno == 0 && ni->ni_rxfrag[0] == NULL)
6298a1b9b6aSSam Leffler 		return m;
6308a1b9b6aSSam Leffler 
6318a1b9b6aSSam Leffler 	/*
6328a1b9b6aSSam Leffler 	 * Remove frag to insure it doesn't get reaped by timer.
6338a1b9b6aSSam Leffler 	 */
6348a1b9b6aSSam Leffler 	if (ni->ni_table == NULL) {
6358a1b9b6aSSam Leffler 		/*
6368a1b9b6aSSam Leffler 		 * Should never happen.  If the node is orphaned (not in
6378a1b9b6aSSam Leffler 		 * the table) then input packets should not reach here.
6388a1b9b6aSSam Leffler 		 * Otherwise, a concurrent request that yanks the table
6398a1b9b6aSSam Leffler 		 * should be blocked by other interlocking and/or by first
6408a1b9b6aSSam Leffler 		 * shutting the driver down.  Regardless, be defensive
6418a1b9b6aSSam Leffler 		 * here and just bail
6428a1b9b6aSSam Leffler 		 */
6438a1b9b6aSSam Leffler 		/* XXX need msg+stat */
6448a1b9b6aSSam Leffler 		m_freem(m);
6458a1b9b6aSSam Leffler 		return NULL;
6468a1b9b6aSSam Leffler 	}
6478a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(ni->ni_table);
6488a1b9b6aSSam Leffler 	mfrag = ni->ni_rxfrag[0];
6498a1b9b6aSSam Leffler 	ni->ni_rxfrag[0] = NULL;
6508a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(ni->ni_table);
6518a1b9b6aSSam Leffler 
6528a1b9b6aSSam Leffler 	/*
6538a1b9b6aSSam Leffler 	 * Validate new fragment is in order and
6548a1b9b6aSSam Leffler 	 * related to the previous ones.
6558a1b9b6aSSam Leffler 	 */
6568a1b9b6aSSam Leffler 	if (mfrag != NULL) {
6578a1b9b6aSSam Leffler 		u_int16_t last_rxseq;
6588a1b9b6aSSam Leffler 
6598a1b9b6aSSam Leffler 		lwh = mtod(mfrag, struct ieee80211_frame *);
6608a1b9b6aSSam Leffler 		last_rxseq = le16toh(*(u_int16_t *)lwh->i_seq);
6618a1b9b6aSSam Leffler 		/* NB: check seq # and frag together */
6628a1b9b6aSSam Leffler 		if (rxseq != last_rxseq+1 ||
6638a1b9b6aSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) ||
6648a1b9b6aSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) {
6658a1b9b6aSSam Leffler 			/*
6668a1b9b6aSSam Leffler 			 * Unrelated fragment or no space for it,
6678a1b9b6aSSam Leffler 			 * clear current fragments.
6688a1b9b6aSSam Leffler 			 */
6698a1b9b6aSSam Leffler 			m_freem(mfrag);
6708a1b9b6aSSam Leffler 			mfrag = NULL;
6718a1b9b6aSSam Leffler 		}
6728a1b9b6aSSam Leffler 	}
6738a1b9b6aSSam Leffler 
6748a1b9b6aSSam Leffler  	if (mfrag == NULL) {
6758a1b9b6aSSam Leffler 		if (fragno != 0) {		/* !first fragment, discard */
6768a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_defrag);
6778a1b9b6aSSam Leffler 			m_freem(m);
6788a1b9b6aSSam Leffler 			return NULL;
6798a1b9b6aSSam Leffler 		}
6808a1b9b6aSSam Leffler 		mfrag = m;
6818a1b9b6aSSam Leffler 	} else {				/* concatenate */
6822cc12adeSSam Leffler 		m_adj(m, hdrspace);		/* strip header */
6838a1b9b6aSSam Leffler 		m_cat(mfrag, m);
6848a1b9b6aSSam Leffler 		/* NB: m_cat doesn't update the packet header */
6858a1b9b6aSSam Leffler 		mfrag->m_pkthdr.len += m->m_pkthdr.len;
6868a1b9b6aSSam Leffler 		/* track last seqnum and fragno */
6878a1b9b6aSSam Leffler 		lwh = mtod(mfrag, struct ieee80211_frame *);
6888a1b9b6aSSam Leffler 		*(u_int16_t *) lwh->i_seq = *(u_int16_t *) wh->i_seq;
6898a1b9b6aSSam Leffler 	}
6908a1b9b6aSSam Leffler 	if (more_frag) {			/* more to come, save */
691a0cc3f85SSam Leffler 		ni->ni_rxfragstamp = ticks;
6928a1b9b6aSSam Leffler 		ni->ni_rxfrag[0] = mfrag;
6938a1b9b6aSSam Leffler 		mfrag = NULL;
6948a1b9b6aSSam Leffler 	}
6958a1b9b6aSSam Leffler 	return mfrag;
6968a1b9b6aSSam Leffler }
6978a1b9b6aSSam Leffler 
6988a1b9b6aSSam Leffler static struct mbuf *
6992cc12adeSSam Leffler ieee80211_decap(struct ieee80211com *ic, struct mbuf *m, int hdrlen)
7008a1b9b6aSSam Leffler {
7012cc12adeSSam Leffler 	struct ieee80211_qosframe_addr4 wh;	/* Max size address frames */
7021a1e1d21SSam Leffler 	struct ether_header *eh;
7031a1e1d21SSam Leffler 	struct llc *llc;
7041a1e1d21SSam Leffler 
7052cc12adeSSam Leffler 	if (m->m_len < hdrlen + sizeof(*llc) &&
7062cc12adeSSam Leffler 	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
7078a1b9b6aSSam Leffler 		/* XXX stat, msg */
7081a1e1d21SSam Leffler 		return NULL;
7091a1e1d21SSam Leffler 	}
7102cc12adeSSam Leffler 	memcpy(&wh, mtod(m, caddr_t), hdrlen);
7112cc12adeSSam Leffler 	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
7121a1e1d21SSam Leffler 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
7131a1e1d21SSam Leffler 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
7141a1e1d21SSam Leffler 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0) {
7152cc12adeSSam Leffler 		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
7161a1e1d21SSam Leffler 		llc = NULL;
7171a1e1d21SSam Leffler 	} else {
7182cc12adeSSam Leffler 		m_adj(m, hdrlen - sizeof(*eh));
7191a1e1d21SSam Leffler 	}
7201a1e1d21SSam Leffler 	eh = mtod(m, struct ether_header *);
7211a1e1d21SSam Leffler 	switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
7221a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_NODS:
7231a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
7241a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
7251a1e1d21SSam Leffler 		break;
7261a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_TODS:
7271a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
7281a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
7291a1e1d21SSam Leffler 		break;
7301a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
7311a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
7321a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3);
7331a1e1d21SSam Leffler 		break;
7341a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
7352cc12adeSSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
7362cc12adeSSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr4);
7372cc12adeSSam Leffler 		break;
7381a1e1d21SSam Leffler 	}
7391a1e1d21SSam Leffler #ifdef ALIGNED_POINTER
7401a1e1d21SSam Leffler 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), u_int32_t)) {
7411a1e1d21SSam Leffler 		struct mbuf *n, *n0, **np;
7421a1e1d21SSam Leffler 		caddr_t newdata;
7431a1e1d21SSam Leffler 		int off, pktlen;
7441a1e1d21SSam Leffler 
7451a1e1d21SSam Leffler 		n0 = NULL;
7461a1e1d21SSam Leffler 		np = &n0;
7471a1e1d21SSam Leffler 		off = 0;
7481a1e1d21SSam Leffler 		pktlen = m->m_pkthdr.len;
7491a1e1d21SSam Leffler 		while (pktlen > off) {
7501a1e1d21SSam Leffler 			if (n0 == NULL) {
7511a1e1d21SSam Leffler 				MGETHDR(n, M_DONTWAIT, MT_DATA);
7521a1e1d21SSam Leffler 				if (n == NULL) {
7531a1e1d21SSam Leffler 					m_freem(m);
7541a1e1d21SSam Leffler 					return NULL;
7551a1e1d21SSam Leffler 				}
7561a1e1d21SSam Leffler 				M_MOVE_PKTHDR(n, m);
7571a1e1d21SSam Leffler 				n->m_len = MHLEN;
7581a1e1d21SSam Leffler 			} else {
7591a1e1d21SSam Leffler 				MGET(n, M_DONTWAIT, MT_DATA);
7601a1e1d21SSam Leffler 				if (n == NULL) {
7611a1e1d21SSam Leffler 					m_freem(m);
7621a1e1d21SSam Leffler 					m_freem(n0);
7631a1e1d21SSam Leffler 					return NULL;
7641a1e1d21SSam Leffler 				}
7651a1e1d21SSam Leffler 				n->m_len = MLEN;
7661a1e1d21SSam Leffler 			}
7671a1e1d21SSam Leffler 			if (pktlen - off >= MINCLSIZE) {
7681a1e1d21SSam Leffler 				MCLGET(n, M_DONTWAIT);
7691a1e1d21SSam Leffler 				if (n->m_flags & M_EXT)
7701a1e1d21SSam Leffler 					n->m_len = n->m_ext.ext_size;
7711a1e1d21SSam Leffler 			}
7721a1e1d21SSam Leffler 			if (n0 == NULL) {
7731a1e1d21SSam Leffler 				newdata =
7741a1e1d21SSam Leffler 				    (caddr_t)ALIGN(n->m_data + sizeof(*eh)) -
7751a1e1d21SSam Leffler 				    sizeof(*eh);
7761a1e1d21SSam Leffler 				n->m_len -= newdata - n->m_data;
7771a1e1d21SSam Leffler 				n->m_data = newdata;
7781a1e1d21SSam Leffler 			}
7791a1e1d21SSam Leffler 			if (n->m_len > pktlen - off)
7801a1e1d21SSam Leffler 				n->m_len = pktlen - off;
7811a1e1d21SSam Leffler 			m_copydata(m, off, n->m_len, mtod(n, caddr_t));
7821a1e1d21SSam Leffler 			off += n->m_len;
7831a1e1d21SSam Leffler 			*np = n;
7841a1e1d21SSam Leffler 			np = &n->m_next;
7851a1e1d21SSam Leffler 		}
7861a1e1d21SSam Leffler 		m_freem(m);
7871a1e1d21SSam Leffler 		m = n0;
7881a1e1d21SSam Leffler 	}
7891a1e1d21SSam Leffler #endif /* ALIGNED_POINTER */
7901a1e1d21SSam Leffler 	if (llc != NULL) {
7911a1e1d21SSam Leffler 		eh = mtod(m, struct ether_header *);
7921a1e1d21SSam Leffler 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
7931a1e1d21SSam Leffler 	}
7941a1e1d21SSam Leffler 	return m;
7951a1e1d21SSam Leffler }
7961a1e1d21SSam Leffler 
7971a1e1d21SSam Leffler /*
7981a1e1d21SSam Leffler  * Install received rate set information in the node's state block.
7991a1e1d21SSam Leffler  */
8001a1e1d21SSam Leffler static int
8011a1e1d21SSam Leffler ieee80211_setup_rates(struct ieee80211com *ic, struct ieee80211_node *ni,
8021a1e1d21SSam Leffler 	u_int8_t *rates, u_int8_t *xrates, int flags)
8031a1e1d21SSam Leffler {
8041a1e1d21SSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
8051a1e1d21SSam Leffler 
8061a1e1d21SSam Leffler 	memset(rs, 0, sizeof(*rs));
8071a1e1d21SSam Leffler 	rs->rs_nrates = rates[1];
8081a1e1d21SSam Leffler 	memcpy(rs->rs_rates, rates + 2, rs->rs_nrates);
8091a1e1d21SSam Leffler 	if (xrates != NULL) {
8101a1e1d21SSam Leffler 		u_int8_t nxrates;
8111a1e1d21SSam Leffler 		/*
8121a1e1d21SSam Leffler 		 * Tack on 11g extended supported rate element.
8131a1e1d21SSam Leffler 		 */
8141a1e1d21SSam Leffler 		nxrates = xrates[1];
8151a1e1d21SSam Leffler 		if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
8161a1e1d21SSam Leffler 			nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates;
8178a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_XRATE,
8188a1b9b6aSSam Leffler 			     "[%s] extended rate set too large;"
8191a1e1d21SSam Leffler 			     " only using %u of %u rates\n",
8208a1b9b6aSSam Leffler 			     ether_sprintf(ni->ni_macaddr), nxrates, xrates[1]);
8211be50176SSam Leffler 			ic->ic_stats.is_rx_rstoobig++;
8221a1e1d21SSam Leffler 		}
8231a1e1d21SSam Leffler 		memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates);
8241a1e1d21SSam Leffler 		rs->rs_nrates += nxrates;
8251a1e1d21SSam Leffler 	}
8261a1e1d21SSam Leffler 	return ieee80211_fix_rate(ic, ni, flags);
8271a1e1d21SSam Leffler }
8281a1e1d21SSam Leffler 
8298a1b9b6aSSam Leffler static void
8308a1b9b6aSSam Leffler ieee80211_auth_open(struct ieee80211com *ic, struct ieee80211_frame *wh,
8318a1b9b6aSSam Leffler     struct ieee80211_node *ni, int rssi, u_int32_t rstamp, u_int16_t seq,
8328a1b9b6aSSam Leffler     u_int16_t status)
8338a1b9b6aSSam Leffler {
8348a1b9b6aSSam Leffler 
835b8d05d3cSSam Leffler 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
836b8d05d3cSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
837b8d05d3cSSam Leffler 		    ni->ni_macaddr, "open auth",
838b8d05d3cSSam Leffler 		    "bad sta auth mode %u", ni->ni_authmode);
839b8d05d3cSSam Leffler 		ic->ic_stats.is_rx_bad_auth++;	/* XXX */
840b8d05d3cSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
841b8d05d3cSSam Leffler 			/* XXX hack to workaround calling convention */
842b8d05d3cSSam Leffler 			ieee80211_send_error(ic, ni, wh->i_addr2,
843b8d05d3cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_AUTH,
844b8d05d3cSSam Leffler 			    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
845b8d05d3cSSam Leffler 		}
846b8d05d3cSSam Leffler 		return;
847b8d05d3cSSam Leffler 	}
8488a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
8498a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:
8508a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:
8514fd1a57dSSam Leffler 	case IEEE80211_M_MONITOR:
8528a1b9b6aSSam Leffler 		/* should not come here */
8534fd1a57dSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
8544fd1a57dSSam Leffler 		    ni->ni_macaddr, "open auth",
8554fd1a57dSSam Leffler 		    "bad operating mode %u", ic->ic_opmode);
8568a1b9b6aSSam Leffler 		break;
8578a1b9b6aSSam Leffler 
8588a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:
8598a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN ||
8608a1b9b6aSSam Leffler 		    seq != IEEE80211_AUTH_OPEN_REQUEST) {
8618a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
8628a1b9b6aSSam Leffler 			return;
8638a1b9b6aSSam Leffler 		}
8648a1b9b6aSSam Leffler 		/* always accept open authentication requests */
8658a1b9b6aSSam Leffler 		if (ni == ic->ic_bss) {
866acc4f7f5SSam Leffler 			ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
8678a1b9b6aSSam Leffler 			if (ni == NULL)
8688a1b9b6aSSam Leffler 				return;
869ebdda46cSSam Leffler 		} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
8708a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
871ebdda46cSSam Leffler 		/*
872ebdda46cSSam Leffler 		 * Mark the node as referenced to reflect that it's
873ebdda46cSSam Leffler 		 * reference count has been bumped to insure it remains
874ebdda46cSSam Leffler 		 * after the transaction completes.
875ebdda46cSSam Leffler 		 */
876ebdda46cSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_AREF;
877ebdda46cSSam Leffler 
8788a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni,
8798a1b9b6aSSam Leffler 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
8808a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
8818a1b9b6aSSam Leffler 		    "[%s] station authenticated (open)\n",
8828a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
883da17aba1SSam Leffler 		/*
884da17aba1SSam Leffler 		 * When 802.1x is not in use mark the port
885da17aba1SSam Leffler 		 * authorized at this point so traffic can flow.
886da17aba1SSam Leffler 		 */
887da17aba1SSam Leffler 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
888da17aba1SSam Leffler 			ieee80211_node_authorize(ic, ni);
8898a1b9b6aSSam Leffler 		break;
8908a1b9b6aSSam Leffler 
8918a1b9b6aSSam Leffler 	case IEEE80211_M_STA:
8928a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_AUTH ||
8938a1b9b6aSSam Leffler 		    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
8948a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
8958a1b9b6aSSam Leffler 			return;
8968a1b9b6aSSam Leffler 		}
8978a1b9b6aSSam Leffler 		if (status != 0) {
8988a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
8998a1b9b6aSSam Leffler 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
9008a1b9b6aSSam Leffler 			    "[%s] open auth failed (reason %d)\n",
9018a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr), status);
9028a1b9b6aSSam Leffler 			/* XXX can this happen? */
9038a1b9b6aSSam Leffler 			if (ni != ic->ic_bss)
9048a1b9b6aSSam Leffler 				ni->ni_fails++;
9058a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_fail++;
906181181acSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
907181181acSSam Leffler 		} else
9088a1b9b6aSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
9098a1b9b6aSSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
9108a1b9b6aSSam Leffler 		break;
9118a1b9b6aSSam Leffler 	}
9128a1b9b6aSSam Leffler }
9138a1b9b6aSSam Leffler 
91484eb84c4SSam Leffler /*
91584eb84c4SSam Leffler  * Send a management frame error response to the specified
91684eb84c4SSam Leffler  * station.  If ni is associated with the station then use
91784eb84c4SSam Leffler  * it; otherwise allocate a temporary node suitable for
91884eb84c4SSam Leffler  * transmitting the frame and then free the reference so
91984eb84c4SSam Leffler  * it will go away as soon as the frame has been transmitted.
92084eb84c4SSam Leffler  */
92184eb84c4SSam Leffler static void
92284eb84c4SSam Leffler ieee80211_send_error(struct ieee80211com *ic, struct ieee80211_node *ni,
92384eb84c4SSam Leffler 	const u_int8_t *mac, int subtype, int arg)
92484eb84c4SSam Leffler {
92584eb84c4SSam Leffler 	int istmp;
92684eb84c4SSam Leffler 
92784eb84c4SSam Leffler 	if (ni == ic->ic_bss) {
92884eb84c4SSam Leffler 		ni = ieee80211_dup_bss(&ic->ic_sta, mac);
92984eb84c4SSam Leffler 		if (ni == NULL) {
93084eb84c4SSam Leffler 			/* XXX msg */
93184eb84c4SSam Leffler 			return;
93284eb84c4SSam Leffler 		}
93384eb84c4SSam Leffler 		istmp = 1;
93484eb84c4SSam Leffler 	} else
93584eb84c4SSam Leffler 		istmp = 0;
93684eb84c4SSam Leffler 	IEEE80211_SEND_MGMT(ic, ni, subtype, arg);
93784eb84c4SSam Leffler 	if (istmp)
93884eb84c4SSam Leffler 		ieee80211_free_node(ni);
93984eb84c4SSam Leffler }
94084eb84c4SSam Leffler 
9418a1b9b6aSSam Leffler static int
9428a1b9b6aSSam Leffler alloc_challenge(struct ieee80211com *ic, struct ieee80211_node *ni)
9438a1b9b6aSSam Leffler {
9448a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL)
9458a1b9b6aSSam Leffler 		MALLOC(ni->ni_challenge, u_int32_t*, IEEE80211_CHALLENGE_LEN,
9468a1b9b6aSSam Leffler 		    M_DEVBUF, M_NOWAIT);
9478a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL) {
9488a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
9498a1b9b6aSSam Leffler 		    "[%s] shared key challenge alloc failed\n",
9508a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
9518a1b9b6aSSam Leffler 		/* XXX statistic */
9528a1b9b6aSSam Leffler 	}
9538a1b9b6aSSam Leffler 	return (ni->ni_challenge != NULL);
9548a1b9b6aSSam Leffler }
9558a1b9b6aSSam Leffler 
9568a1b9b6aSSam Leffler /* XXX TODO: add statistics */
9578a1b9b6aSSam Leffler static void
9588a1b9b6aSSam Leffler ieee80211_auth_shared(struct ieee80211com *ic, struct ieee80211_frame *wh,
9598a1b9b6aSSam Leffler     u_int8_t *frm, u_int8_t *efrm, struct ieee80211_node *ni, int rssi,
9608a1b9b6aSSam Leffler     u_int32_t rstamp, u_int16_t seq, u_int16_t status)
9618a1b9b6aSSam Leffler {
9628a1b9b6aSSam Leffler 	u_int8_t *challenge;
9638a1b9b6aSSam Leffler 	int allocbs, estatus;
9648a1b9b6aSSam Leffler 
9658a1b9b6aSSam Leffler 	/*
9668a1b9b6aSSam Leffler 	 * NB: this can happen as we allow pre-shared key
9678a1b9b6aSSam Leffler 	 * authentication to be enabled w/o wep being turned
9688a1b9b6aSSam Leffler 	 * on so that configuration of these can be done
9698a1b9b6aSSam Leffler 	 * in any order.  It may be better to enforce the
9708a1b9b6aSSam Leffler 	 * ordering in which case this check would just be
9718a1b9b6aSSam Leffler 	 * for sanity/consistency.
9728a1b9b6aSSam Leffler 	 */
9738a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
9748a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
9758a1b9b6aSSam Leffler 		    ni->ni_macaddr, "shared key auth",
9768a1b9b6aSSam Leffler 		    "%s", " PRIVACY is disabled");
9778a1b9b6aSSam Leffler 		estatus = IEEE80211_STATUS_ALG;
9788a1b9b6aSSam Leffler 		goto bad;
9798a1b9b6aSSam Leffler 	}
9808a1b9b6aSSam Leffler 	/*
9818a1b9b6aSSam Leffler 	 * Pre-shared key authentication is evil; accept
9828a1b9b6aSSam Leffler 	 * it only if explicitly configured (it is supported
9838a1b9b6aSSam Leffler 	 * mainly for compatibility with clients like OS X).
9848a1b9b6aSSam Leffler 	 */
9858a1b9b6aSSam Leffler 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
9868a1b9b6aSSam Leffler 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
9878a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
9888a1b9b6aSSam Leffler 		    ni->ni_macaddr, "shared key auth",
9898a1b9b6aSSam Leffler 		    "bad sta auth mode %u", ni->ni_authmode);
9908a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
9918a1b9b6aSSam Leffler 		estatus = IEEE80211_STATUS_ALG;
9928a1b9b6aSSam Leffler 		goto bad;
9938a1b9b6aSSam Leffler 	}
9948a1b9b6aSSam Leffler 
9958a1b9b6aSSam Leffler 	challenge = NULL;
9968a1b9b6aSSam Leffler 	if (frm + 1 < efrm) {
9978a1b9b6aSSam Leffler 		if ((frm[1] + 2) > (efrm - frm)) {
9988a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
9998a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10008a1b9b6aSSam Leffler 			    "ie %d/%d too long",
10018a1b9b6aSSam Leffler 			    frm[0], (frm[1] + 2) - (efrm - frm));
10028a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
10038a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
10048a1b9b6aSSam Leffler 			goto bad;
10058a1b9b6aSSam Leffler 		}
10068a1b9b6aSSam Leffler 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
10078a1b9b6aSSam Leffler 			challenge = frm;
10088a1b9b6aSSam Leffler 		frm += frm[1] + 2;
10098a1b9b6aSSam Leffler 	}
10108a1b9b6aSSam Leffler 	switch (seq) {
10118a1b9b6aSSam Leffler 	case IEEE80211_AUTH_SHARED_CHALLENGE:
10128a1b9b6aSSam Leffler 	case IEEE80211_AUTH_SHARED_RESPONSE:
10138a1b9b6aSSam Leffler 		if (challenge == NULL) {
10148a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10158a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10168a1b9b6aSSam Leffler 			    "%s", "no challenge");
10178a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
10188a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
10198a1b9b6aSSam Leffler 			goto bad;
10208a1b9b6aSSam Leffler 		}
10218a1b9b6aSSam Leffler 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
10228a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10238a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10248a1b9b6aSSam Leffler 			    "bad challenge len %d", challenge[1]);
10258a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
10268a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
10278a1b9b6aSSam Leffler 			goto bad;
10288a1b9b6aSSam Leffler 		}
10298a1b9b6aSSam Leffler 	default:
10308a1b9b6aSSam Leffler 		break;
10318a1b9b6aSSam Leffler 	}
10328a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
10338a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:
10348a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:
10358a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:
10368a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10378a1b9b6aSSam Leffler 		    ni->ni_macaddr, "shared key auth",
10388a1b9b6aSSam Leffler 		    "bad operating mode %u", ic->ic_opmode);
10398a1b9b6aSSam Leffler 		return;
10408a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:
10418a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN) {
10428a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10438a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10448a1b9b6aSSam Leffler 			    "bad state %u", ic->ic_state);
10458a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_ALG;	/* XXX */
10468a1b9b6aSSam Leffler 			goto bad;
10478a1b9b6aSSam Leffler 		}
10488a1b9b6aSSam Leffler 		switch (seq) {
10498a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_REQUEST:
10508a1b9b6aSSam Leffler 			if (ni == ic->ic_bss) {
1051acc4f7f5SSam Leffler 				ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
10528a1b9b6aSSam Leffler 				if (ni == NULL) {
10538a1b9b6aSSam Leffler 					/* NB: no way to return an error */
10548a1b9b6aSSam Leffler 					return;
10558a1b9b6aSSam Leffler 				}
10568a1b9b6aSSam Leffler 				allocbs = 1;
10578a1b9b6aSSam Leffler 			} else {
1058ebdda46cSSam Leffler 				if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
10598a1b9b6aSSam Leffler 					(void) ieee80211_ref_node(ni);
10608a1b9b6aSSam Leffler 				allocbs = 0;
10618a1b9b6aSSam Leffler 			}
1062ebdda46cSSam Leffler 			/*
1063ebdda46cSSam Leffler 			 * Mark the node as referenced to reflect that it's
1064ebdda46cSSam Leffler 			 * reference count has been bumped to insure it remains
1065ebdda46cSSam Leffler 			 * after the transaction completes.
1066ebdda46cSSam Leffler 			 */
1067ebdda46cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_AREF;
10688a1b9b6aSSam Leffler 			ni->ni_rssi = rssi;
10698a1b9b6aSSam Leffler 			ni->ni_rstamp = rstamp;
10708a1b9b6aSSam Leffler 			if (!alloc_challenge(ic, ni)) {
10718a1b9b6aSSam Leffler 				/* NB: don't return error so they rexmit */
10728a1b9b6aSSam Leffler 				return;
10738a1b9b6aSSam Leffler 			}
10748a1b9b6aSSam Leffler 			get_random_bytes(ni->ni_challenge,
10758a1b9b6aSSam Leffler 				IEEE80211_CHALLENGE_LEN);
10768a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
10778a1b9b6aSSam Leffler 				IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
10788a1b9b6aSSam Leffler 				"[%s] shared key %sauth request\n",
10798a1b9b6aSSam Leffler 				ether_sprintf(ni->ni_macaddr),
10808a1b9b6aSSam Leffler 				allocbs ? "" : "re");
10818a1b9b6aSSam Leffler 			break;
10828a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_RESPONSE:
10838a1b9b6aSSam Leffler 			if (ni == ic->ic_bss) {
10848a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10858a1b9b6aSSam Leffler 				    ni->ni_macaddr, "shared key response",
10868a1b9b6aSSam Leffler 				    "%s", "unknown station");
10878a1b9b6aSSam Leffler 				/* NB: don't send a response */
10888a1b9b6aSSam Leffler 				return;
10898a1b9b6aSSam Leffler 			}
10908a1b9b6aSSam Leffler 			if (ni->ni_challenge == NULL) {
10918a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10928a1b9b6aSSam Leffler 				    ni->ni_macaddr, "shared key response",
10938a1b9b6aSSam Leffler 				    "%s", "no challenge recorded");
10948a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_bad_auth++;
10958a1b9b6aSSam Leffler 				estatus = IEEE80211_STATUS_CHALLENGE;
10968a1b9b6aSSam Leffler 				goto bad;
10978a1b9b6aSSam Leffler 			}
10988a1b9b6aSSam Leffler 			if (memcmp(ni->ni_challenge, &challenge[2],
10998a1b9b6aSSam Leffler 			           challenge[1]) != 0) {
11008a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
11018a1b9b6aSSam Leffler 				    ni->ni_macaddr, "shared key response",
11028a1b9b6aSSam Leffler 				    "%s", "challenge mismatch");
11038a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_auth_fail++;
11048a1b9b6aSSam Leffler 				estatus = IEEE80211_STATUS_CHALLENGE;
11058a1b9b6aSSam Leffler 				goto bad;
11068a1b9b6aSSam Leffler 			}
11078a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
11088a1b9b6aSSam Leffler 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
11098a1b9b6aSSam Leffler 			    "[%s] station authenticated (shared key)\n",
11108a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr));
1111da17aba1SSam Leffler 			ieee80211_node_authorize(ic, ni);
11128a1b9b6aSSam Leffler 			break;
11138a1b9b6aSSam Leffler 		default:
11148a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
11158a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
11168a1b9b6aSSam Leffler 			    "bad seq %d", seq);
11178a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
11188a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_SEQUENCE;
11198a1b9b6aSSam Leffler 			goto bad;
11208a1b9b6aSSam Leffler 		}
11218a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni,
11228a1b9b6aSSam Leffler 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
11238a1b9b6aSSam Leffler 		break;
11248a1b9b6aSSam Leffler 
11258a1b9b6aSSam Leffler 	case IEEE80211_M_STA:
11268a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_AUTH)
11278a1b9b6aSSam Leffler 			return;
11288a1b9b6aSSam Leffler 		switch (seq) {
11298a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_PASS:
11308a1b9b6aSSam Leffler 			if (ni->ni_challenge != NULL) {
11318a1b9b6aSSam Leffler 				FREE(ni->ni_challenge, M_DEVBUF);
11328a1b9b6aSSam Leffler 				ni->ni_challenge = NULL;
11338a1b9b6aSSam Leffler 			}
11348a1b9b6aSSam Leffler 			if (status != 0) {
11358a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic,
11368a1b9b6aSSam Leffler 				    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
11378a1b9b6aSSam Leffler 				    "[%s] shared key auth failed (reason %d)\n",
11388a1b9b6aSSam Leffler 				    ether_sprintf(ieee80211_getbssid(ic, wh)),
11398a1b9b6aSSam Leffler 				    status);
11408a1b9b6aSSam Leffler 				/* XXX can this happen? */
11418a1b9b6aSSam Leffler 				if (ni != ic->ic_bss)
11428a1b9b6aSSam Leffler 					ni->ni_fails++;
11438a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_auth_fail++;
11448a1b9b6aSSam Leffler 				return;
11458a1b9b6aSSam Leffler 			}
11468a1b9b6aSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
11478a1b9b6aSSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
11488a1b9b6aSSam Leffler 			break;
11498a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_CHALLENGE:
11508a1b9b6aSSam Leffler 			if (!alloc_challenge(ic, ni))
11518a1b9b6aSSam Leffler 				return;
11528a1b9b6aSSam Leffler 			/* XXX could optimize by passing recvd challenge */
11538a1b9b6aSSam Leffler 			memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
11548a1b9b6aSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni,
11558a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
11568a1b9b6aSSam Leffler 			break;
11578a1b9b6aSSam Leffler 		default:
11588a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_AUTH,
11598a1b9b6aSSam Leffler 			    wh, "shared key auth", "bad seq %d", seq);
11608a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
11618a1b9b6aSSam Leffler 			return;
11628a1b9b6aSSam Leffler 		}
11638a1b9b6aSSam Leffler 		break;
11648a1b9b6aSSam Leffler 	}
11658a1b9b6aSSam Leffler 	return;
11668a1b9b6aSSam Leffler bad:
11678a1b9b6aSSam Leffler 	/*
11688a1b9b6aSSam Leffler 	 * Send an error response; but only when operating as an AP.
11698a1b9b6aSSam Leffler 	 */
11708a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
11718a1b9b6aSSam Leffler 		/* XXX hack to workaround calling convention */
117284eb84c4SSam Leffler 		ieee80211_send_error(ic, ni, wh->i_addr2,
11738a1b9b6aSSam Leffler 		    IEEE80211_FC0_SUBTYPE_AUTH,
11748a1b9b6aSSam Leffler 		    (seq + 1) | (estatus<<16));
1175181181acSSam Leffler 	} else if (ic->ic_opmode == IEEE80211_M_STA) {
1176181181acSSam Leffler 		/*
1177181181acSSam Leffler 		 * Kick the state machine.  This short-circuits
1178181181acSSam Leffler 		 * using the mgt frame timeout to trigger the
1179181181acSSam Leffler 		 * state transition.
1180181181acSSam Leffler 		 */
1181181181acSSam Leffler 		if (ic->ic_state == IEEE80211_S_AUTH)
1182181181acSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
11838a1b9b6aSSam Leffler 	}
11848a1b9b6aSSam Leffler }
11858a1b9b6aSSam Leffler 
11861a1e1d21SSam Leffler /* Verify the existence and length of __elem or get out. */
11871a1e1d21SSam Leffler #define IEEE80211_VERIFY_ELEMENT(__elem, __maxlen) do {			\
11881a1e1d21SSam Leffler 	if ((__elem) == NULL) {						\
11898a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
11908a1b9b6aSSam Leffler 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
11918a1b9b6aSSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
11928a1b9b6aSSam Leffler 		    "%s", "no " #__elem );				\
11931be50176SSam Leffler 		ic->ic_stats.is_rx_elem_missing++;			\
11941a1e1d21SSam Leffler 		return;							\
11951a1e1d21SSam Leffler 	}								\
11961a1e1d21SSam Leffler 	if ((__elem)[1] > (__maxlen)) {					\
11978a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
11988a1b9b6aSSam Leffler 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
11991a1e1d21SSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
12008a1b9b6aSSam Leffler 		    "bad " #__elem " len %d", (__elem)[1]);		\
12011be50176SSam Leffler 		ic->ic_stats.is_rx_elem_toobig++;			\
12021a1e1d21SSam Leffler 		return;							\
12031a1e1d21SSam Leffler 	}								\
12041a1e1d21SSam Leffler } while (0)
12051a1e1d21SSam Leffler 
12061a1e1d21SSam Leffler #define	IEEE80211_VERIFY_LENGTH(_len, _minlen) do {			\
12071a1e1d21SSam Leffler 	if ((_len) < (_minlen)) {					\
12088a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
12098a1b9b6aSSam Leffler 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
12101a1e1d21SSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
12118a1b9b6aSSam Leffler 		    "%s", "ie too short");				\
12121be50176SSam Leffler 		ic->ic_stats.is_rx_elem_toosmall++;			\
12131a1e1d21SSam Leffler 		return;							\
12141a1e1d21SSam Leffler 	}								\
12151a1e1d21SSam Leffler } while (0)
12161a1e1d21SSam Leffler 
12178a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
12188a1b9b6aSSam Leffler static void
12198a1b9b6aSSam Leffler ieee80211_ssid_mismatch(struct ieee80211com *ic, const char *tag,
12208a1b9b6aSSam Leffler 	u_int8_t mac[IEEE80211_ADDR_LEN], u_int8_t *ssid)
12218a1b9b6aSSam Leffler {
12228a1b9b6aSSam Leffler 	printf("[%s] discard %s frame, ssid mismatch: ",
12238a1b9b6aSSam Leffler 		ether_sprintf(mac), tag);
12248a1b9b6aSSam Leffler 	ieee80211_print_essid(ssid + 2, ssid[1]);
12258a1b9b6aSSam Leffler 	printf("\n");
12268a1b9b6aSSam Leffler }
12278a1b9b6aSSam Leffler 
12288a1b9b6aSSam Leffler #define	IEEE80211_VERIFY_SSID(_ni, _ssid) do {				\
12298a1b9b6aSSam Leffler 	if ((_ssid)[1] != 0 &&						\
12308a1b9b6aSSam Leffler 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
12318a1b9b6aSSam Leffler 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
12328a1b9b6aSSam Leffler 		if (ieee80211_msg_input(ic))				\
12338a1b9b6aSSam Leffler 			ieee80211_ssid_mismatch(ic, 			\
12348a1b9b6aSSam Leffler 			    ieee80211_mgt_subtype_name[subtype >>	\
12358a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],		\
12368a1b9b6aSSam Leffler 				wh->i_addr2, _ssid);			\
12378a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_ssidmismatch++;			\
12388a1b9b6aSSam Leffler 		return;							\
12398a1b9b6aSSam Leffler 	}								\
12408a1b9b6aSSam Leffler } while (0)
12418a1b9b6aSSam Leffler #else /* !IEEE80211_DEBUG */
12428a1b9b6aSSam Leffler #define	IEEE80211_VERIFY_SSID(_ni, _ssid) do {				\
12438a1b9b6aSSam Leffler 	if ((_ssid)[1] != 0 &&						\
12448a1b9b6aSSam Leffler 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
12458a1b9b6aSSam Leffler 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
12468a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_ssidmismatch++;			\
12478a1b9b6aSSam Leffler 		return;							\
12488a1b9b6aSSam Leffler 	}								\
12498a1b9b6aSSam Leffler } while (0)
12508a1b9b6aSSam Leffler #endif /* !IEEE80211_DEBUG */
12518a1b9b6aSSam Leffler 
12528a1b9b6aSSam Leffler /* unalligned little endian access */
12538a1b9b6aSSam Leffler #define LE_READ_2(p)					\
12548a1b9b6aSSam Leffler 	((u_int16_t)					\
12558a1b9b6aSSam Leffler 	 ((((const u_int8_t *)(p))[0]      ) |		\
12568a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[1] <<  8)))
12578a1b9b6aSSam Leffler #define LE_READ_4(p)					\
12588a1b9b6aSSam Leffler 	((u_int32_t)					\
12598a1b9b6aSSam Leffler 	 ((((const u_int8_t *)(p))[0]      ) |		\
12608a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[1] <<  8) |		\
12618a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[2] << 16) |		\
12628a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[3] << 24)))
12638a1b9b6aSSam Leffler 
12648a1b9b6aSSam Leffler static int __inline
12658a1b9b6aSSam Leffler iswpaoui(const u_int8_t *frm)
12668a1b9b6aSSam Leffler {
12678a1b9b6aSSam Leffler 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
12688a1b9b6aSSam Leffler }
12698a1b9b6aSSam Leffler 
12708a1b9b6aSSam Leffler static int __inline
12718a1b9b6aSSam Leffler iswmeoui(const u_int8_t *frm)
12728a1b9b6aSSam Leffler {
12738a1b9b6aSSam Leffler 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
12748a1b9b6aSSam Leffler }
12758a1b9b6aSSam Leffler 
12768a1b9b6aSSam Leffler static int __inline
12778a1b9b6aSSam Leffler iswmeparam(const u_int8_t *frm)
12788a1b9b6aSSam Leffler {
12798a1b9b6aSSam Leffler 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
12808a1b9b6aSSam Leffler 		frm[6] == WME_PARAM_OUI_SUBTYPE;
12818a1b9b6aSSam Leffler }
12828a1b9b6aSSam Leffler 
12838a1b9b6aSSam Leffler static int __inline
12848a1b9b6aSSam Leffler iswmeinfo(const u_int8_t *frm)
12858a1b9b6aSSam Leffler {
12868a1b9b6aSSam Leffler 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
12878a1b9b6aSSam Leffler 		frm[6] == WME_INFO_OUI_SUBTYPE;
12888a1b9b6aSSam Leffler }
12898a1b9b6aSSam Leffler 
12908a1b9b6aSSam Leffler static int __inline
12918a1b9b6aSSam Leffler isatherosoui(const u_int8_t *frm)
12928a1b9b6aSSam Leffler {
12938a1b9b6aSSam Leffler 	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
12948a1b9b6aSSam Leffler }
12958a1b9b6aSSam Leffler 
12968a1b9b6aSSam Leffler /*
12978a1b9b6aSSam Leffler  * Convert a WPA cipher selector OUI to an internal
12988a1b9b6aSSam Leffler  * cipher algorithm.  Where appropriate we also
12998a1b9b6aSSam Leffler  * record any key length.
13008a1b9b6aSSam Leffler  */
13018a1b9b6aSSam Leffler static int
13028a1b9b6aSSam Leffler wpa_cipher(u_int8_t *sel, u_int8_t *keylen)
13038a1b9b6aSSam Leffler {
13048a1b9b6aSSam Leffler #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
13058a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
13068a1b9b6aSSam Leffler 
13078a1b9b6aSSam Leffler 	switch (w) {
13088a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_NULL):
13098a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_NONE;
13108a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_WEP40):
13118a1b9b6aSSam Leffler 		if (keylen)
13128a1b9b6aSSam Leffler 			*keylen = 40 / NBBY;
13138a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
13148a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_WEP104):
13158a1b9b6aSSam Leffler 		if (keylen)
13168a1b9b6aSSam Leffler 			*keylen = 104 / NBBY;
13178a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
13188a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_TKIP):
13198a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_TKIP;
13208a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_CCMP):
13218a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_AES_CCM;
13228a1b9b6aSSam Leffler 	}
13238a1b9b6aSSam Leffler 	return 32;		/* NB: so 1<< is discarded */
13248a1b9b6aSSam Leffler #undef WPA_SEL
13258a1b9b6aSSam Leffler }
13268a1b9b6aSSam Leffler 
13278a1b9b6aSSam Leffler /*
13288a1b9b6aSSam Leffler  * Convert a WPA key management/authentication algorithm
13298a1b9b6aSSam Leffler  * to an internal code.
13308a1b9b6aSSam Leffler  */
13318a1b9b6aSSam Leffler static int
13328a1b9b6aSSam Leffler wpa_keymgmt(u_int8_t *sel)
13338a1b9b6aSSam Leffler {
13348a1b9b6aSSam Leffler #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
13358a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
13368a1b9b6aSSam Leffler 
13378a1b9b6aSSam Leffler 	switch (w) {
13388a1b9b6aSSam Leffler 	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
13398a1b9b6aSSam Leffler 		return WPA_ASE_8021X_UNSPEC;
13408a1b9b6aSSam Leffler 	case WPA_SEL(WPA_ASE_8021X_PSK):
13418a1b9b6aSSam Leffler 		return WPA_ASE_8021X_PSK;
13428a1b9b6aSSam Leffler 	case WPA_SEL(WPA_ASE_NONE):
13438a1b9b6aSSam Leffler 		return WPA_ASE_NONE;
13448a1b9b6aSSam Leffler 	}
13458a1b9b6aSSam Leffler 	return 0;		/* NB: so is discarded */
13468a1b9b6aSSam Leffler #undef WPA_SEL
13478a1b9b6aSSam Leffler }
13488a1b9b6aSSam Leffler 
13498a1b9b6aSSam Leffler /*
13508a1b9b6aSSam Leffler  * Parse a WPA information element to collect parameters
13518a1b9b6aSSam Leffler  * and validate the parameters against what has been
13528a1b9b6aSSam Leffler  * configured for the system.
13538a1b9b6aSSam Leffler  */
13548a1b9b6aSSam Leffler static int
13558a1b9b6aSSam Leffler ieee80211_parse_wpa(struct ieee80211com *ic, u_int8_t *frm,
13568a1b9b6aSSam Leffler 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
13578a1b9b6aSSam Leffler {
13588a1b9b6aSSam Leffler 	u_int8_t len = frm[1];
13598a1b9b6aSSam Leffler 	u_int32_t w;
13608a1b9b6aSSam Leffler 	int n;
13618a1b9b6aSSam Leffler 
13628a1b9b6aSSam Leffler 	/*
13638a1b9b6aSSam Leffler 	 * Check the length once for fixed parts: OUI, type,
13648a1b9b6aSSam Leffler 	 * version, mcast cipher, and 2 selector counts.
13658a1b9b6aSSam Leffler 	 * Other, variable-length data, must be checked separately.
13668a1b9b6aSSam Leffler 	 */
13678a1b9b6aSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_WPA1,
13688a1b9b6aSSam Leffler 		("not WPA, flags 0x%x", ic->ic_flags));
13698a1b9b6aSSam Leffler 	if (len < 14) {
13708a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
13718a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
13728a1b9b6aSSam Leffler 		    wh, "WPA", "too short, len %u", len);
13738a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
13748a1b9b6aSSam Leffler 	}
13758a1b9b6aSSam Leffler 	frm += 6, len -= 4;		/* NB: len is payload only */
13768a1b9b6aSSam Leffler 	/* NB: iswapoui already validated the OUI and type */
13778a1b9b6aSSam Leffler 	w = LE_READ_2(frm);
13788a1b9b6aSSam Leffler 	if (w != WPA_VERSION) {
13798a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
13808a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
13818a1b9b6aSSam Leffler 		    wh, "WPA", "bad version %u", w);
13828a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
13838a1b9b6aSSam Leffler 	}
13848a1b9b6aSSam Leffler 	frm += 2, len -= 2;
13858a1b9b6aSSam Leffler 
13868a1b9b6aSSam Leffler 	/* multicast/group cipher */
13878a1b9b6aSSam Leffler 	w = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
13888a1b9b6aSSam Leffler 	if (w != rsn->rsn_mcastcipher) {
13898a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
13908a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
13918a1b9b6aSSam Leffler 		    wh, "WPA", "mcast cipher mismatch; got %u, expected %u",
13928a1b9b6aSSam Leffler 		    w, rsn->rsn_mcastcipher);
13938a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
13948a1b9b6aSSam Leffler 	}
13958a1b9b6aSSam Leffler 	frm += 4, len -= 4;
13968a1b9b6aSSam Leffler 
13978a1b9b6aSSam Leffler 	/* unicast ciphers */
13988a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
13998a1b9b6aSSam Leffler 	frm += 2, len -= 2;
14008a1b9b6aSSam Leffler 	if (len < n*4+2) {
14018a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14028a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14038a1b9b6aSSam Leffler 		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
14048a1b9b6aSSam Leffler 		    len, n);
14058a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14068a1b9b6aSSam Leffler 	}
14078a1b9b6aSSam Leffler 	w = 0;
14088a1b9b6aSSam Leffler 	for (; n > 0; n--) {
14098a1b9b6aSSam Leffler 		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
14108a1b9b6aSSam Leffler 		frm += 4, len -= 4;
14118a1b9b6aSSam Leffler 	}
14128a1b9b6aSSam Leffler 	w &= rsn->rsn_ucastcipherset;
14138a1b9b6aSSam Leffler 	if (w == 0) {
14148a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14158a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14168a1b9b6aSSam Leffler 		    wh, "WPA", "%s", "ucast cipher set empty");
14178a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14188a1b9b6aSSam Leffler 	}
14198a1b9b6aSSam Leffler 	if (w & (1<<IEEE80211_CIPHER_TKIP))
14208a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
14218a1b9b6aSSam Leffler 	else
14228a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
14238a1b9b6aSSam Leffler 
14248a1b9b6aSSam Leffler 	/* key management algorithms */
14258a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
14268a1b9b6aSSam Leffler 	frm += 2, len -= 2;
14278a1b9b6aSSam Leffler 	if (len < n*4) {
14288a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14298a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14308a1b9b6aSSam Leffler 		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
14318a1b9b6aSSam Leffler 		    len, n);
14328a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14338a1b9b6aSSam Leffler 	}
14348a1b9b6aSSam Leffler 	w = 0;
14358a1b9b6aSSam Leffler 	for (; n > 0; n--) {
14368a1b9b6aSSam Leffler 		w |= wpa_keymgmt(frm);
14378a1b9b6aSSam Leffler 		frm += 4, len -= 4;
14388a1b9b6aSSam Leffler 	}
14398a1b9b6aSSam Leffler 	w &= rsn->rsn_keymgmtset;
14408a1b9b6aSSam Leffler 	if (w == 0) {
14418a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14428a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14438a1b9b6aSSam Leffler 		    wh, "WPA", "%s", "no acceptable key mgmt alg");
14448a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14458a1b9b6aSSam Leffler 	}
14468a1b9b6aSSam Leffler 	if (w & WPA_ASE_8021X_UNSPEC)
14478a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
14488a1b9b6aSSam Leffler 	else
14498a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
14508a1b9b6aSSam Leffler 
14518a1b9b6aSSam Leffler 	if (len > 2)		/* optional capabilities */
14528a1b9b6aSSam Leffler 		rsn->rsn_caps = LE_READ_2(frm);
14538a1b9b6aSSam Leffler 
14548a1b9b6aSSam Leffler 	return 0;
14558a1b9b6aSSam Leffler }
14568a1b9b6aSSam Leffler 
14578a1b9b6aSSam Leffler /*
14588a1b9b6aSSam Leffler  * Convert an RSN cipher selector OUI to an internal
14598a1b9b6aSSam Leffler  * cipher algorithm.  Where appropriate we also
14608a1b9b6aSSam Leffler  * record any key length.
14618a1b9b6aSSam Leffler  */
14628a1b9b6aSSam Leffler static int
14638a1b9b6aSSam Leffler rsn_cipher(u_int8_t *sel, u_int8_t *keylen)
14648a1b9b6aSSam Leffler {
14658a1b9b6aSSam Leffler #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
14668a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
14678a1b9b6aSSam Leffler 
14688a1b9b6aSSam Leffler 	switch (w) {
14698a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_NULL):
14708a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_NONE;
14718a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_WEP40):
14728a1b9b6aSSam Leffler 		if (keylen)
14738a1b9b6aSSam Leffler 			*keylen = 40 / NBBY;
14748a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
14758a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_WEP104):
14768a1b9b6aSSam Leffler 		if (keylen)
14778a1b9b6aSSam Leffler 			*keylen = 104 / NBBY;
14788a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
14798a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_TKIP):
14808a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_TKIP;
14818a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_CCMP):
14828a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_AES_CCM;
14838a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_WRAP):
14848a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_AES_OCB;
14858a1b9b6aSSam Leffler 	}
14868a1b9b6aSSam Leffler 	return 32;		/* NB: so 1<< is discarded */
14878a1b9b6aSSam Leffler #undef WPA_SEL
14888a1b9b6aSSam Leffler }
14898a1b9b6aSSam Leffler 
14908a1b9b6aSSam Leffler /*
14918a1b9b6aSSam Leffler  * Convert an RSN key management/authentication algorithm
14928a1b9b6aSSam Leffler  * to an internal code.
14938a1b9b6aSSam Leffler  */
14948a1b9b6aSSam Leffler static int
14958a1b9b6aSSam Leffler rsn_keymgmt(u_int8_t *sel)
14968a1b9b6aSSam Leffler {
14978a1b9b6aSSam Leffler #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
14988a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
14998a1b9b6aSSam Leffler 
15008a1b9b6aSSam Leffler 	switch (w) {
15018a1b9b6aSSam Leffler 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
15028a1b9b6aSSam Leffler 		return RSN_ASE_8021X_UNSPEC;
15038a1b9b6aSSam Leffler 	case RSN_SEL(RSN_ASE_8021X_PSK):
15048a1b9b6aSSam Leffler 		return RSN_ASE_8021X_PSK;
15058a1b9b6aSSam Leffler 	case RSN_SEL(RSN_ASE_NONE):
15068a1b9b6aSSam Leffler 		return RSN_ASE_NONE;
15078a1b9b6aSSam Leffler 	}
15088a1b9b6aSSam Leffler 	return 0;		/* NB: so is discarded */
15098a1b9b6aSSam Leffler #undef RSN_SEL
15108a1b9b6aSSam Leffler }
15118a1b9b6aSSam Leffler 
15128a1b9b6aSSam Leffler /*
15138a1b9b6aSSam Leffler  * Parse a WPA/RSN information element to collect parameters
15148a1b9b6aSSam Leffler  * and validate the parameters against what has been
15158a1b9b6aSSam Leffler  * configured for the system.
15168a1b9b6aSSam Leffler  */
15178a1b9b6aSSam Leffler static int
15188a1b9b6aSSam Leffler ieee80211_parse_rsn(struct ieee80211com *ic, u_int8_t *frm,
15198a1b9b6aSSam Leffler 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
15208a1b9b6aSSam Leffler {
15218a1b9b6aSSam Leffler 	u_int8_t len = frm[1];
15228a1b9b6aSSam Leffler 	u_int32_t w;
15238a1b9b6aSSam Leffler 	int n;
15248a1b9b6aSSam Leffler 
15258a1b9b6aSSam Leffler 	/*
15268a1b9b6aSSam Leffler 	 * Check the length once for fixed parts:
15278a1b9b6aSSam Leffler 	 * version, mcast cipher, and 2 selector counts.
15288a1b9b6aSSam Leffler 	 * Other, variable-length data, must be checked separately.
15298a1b9b6aSSam Leffler 	 */
15308a1b9b6aSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_WPA2,
15318a1b9b6aSSam Leffler 		("not RSN, flags 0x%x", ic->ic_flags));
15328a1b9b6aSSam Leffler 	if (len < 10) {
15338a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15348a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15358a1b9b6aSSam Leffler 		    wh, "RSN", "too short, len %u", len);
15368a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15378a1b9b6aSSam Leffler 	}
15388a1b9b6aSSam Leffler 	frm += 2;
15398a1b9b6aSSam Leffler 	w = LE_READ_2(frm);
15408a1b9b6aSSam Leffler 	if (w != RSN_VERSION) {
15418a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15428a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15438a1b9b6aSSam Leffler 		    wh, "RSN", "bad version %u", w);
15448a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15458a1b9b6aSSam Leffler 	}
15468a1b9b6aSSam Leffler 	frm += 2, len -= 2;
15478a1b9b6aSSam Leffler 
15488a1b9b6aSSam Leffler 	/* multicast/group cipher */
15498a1b9b6aSSam Leffler 	w = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
15508a1b9b6aSSam Leffler 	if (w != rsn->rsn_mcastcipher) {
15518a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15528a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15538a1b9b6aSSam Leffler 		    wh, "RSN", "mcast cipher mismatch; got %u, expected %u",
15548a1b9b6aSSam Leffler 		    w, rsn->rsn_mcastcipher);
15558a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15568a1b9b6aSSam Leffler 	}
15578a1b9b6aSSam Leffler 	frm += 4, len -= 4;
15588a1b9b6aSSam Leffler 
15598a1b9b6aSSam Leffler 	/* unicast ciphers */
15608a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
15618a1b9b6aSSam Leffler 	frm += 2, len -= 2;
15628a1b9b6aSSam Leffler 	if (len < n*4+2) {
15638a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15648a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15658a1b9b6aSSam Leffler 		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
15668a1b9b6aSSam Leffler 		    len, n);
15678a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15688a1b9b6aSSam Leffler 	}
15698a1b9b6aSSam Leffler 	w = 0;
15708a1b9b6aSSam Leffler 	for (; n > 0; n--) {
15718a1b9b6aSSam Leffler 		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
15728a1b9b6aSSam Leffler 		frm += 4, len -= 4;
15738a1b9b6aSSam Leffler 	}
15748a1b9b6aSSam Leffler 	w &= rsn->rsn_ucastcipherset;
15758a1b9b6aSSam Leffler 	if (w == 0) {
15768a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15778a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15788a1b9b6aSSam Leffler 		    wh, "RSN", "%s", "ucast cipher set empty");
15798a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15808a1b9b6aSSam Leffler 	}
15818a1b9b6aSSam Leffler 	if (w & (1<<IEEE80211_CIPHER_TKIP))
15828a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
15838a1b9b6aSSam Leffler 	else
15848a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
15858a1b9b6aSSam Leffler 
15868a1b9b6aSSam Leffler 	/* key management algorithms */
15878a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
15888a1b9b6aSSam Leffler 	frm += 2, len -= 2;
15898a1b9b6aSSam Leffler 	if (len < n*4) {
15908a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15918a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15928a1b9b6aSSam Leffler 		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
15938a1b9b6aSSam Leffler 		    len, n);
15948a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15958a1b9b6aSSam Leffler 	}
15968a1b9b6aSSam Leffler 	w = 0;
15978a1b9b6aSSam Leffler 	for (; n > 0; n--) {
15988a1b9b6aSSam Leffler 		w |= rsn_keymgmt(frm);
15998a1b9b6aSSam Leffler 		frm += 4, len -= 4;
16008a1b9b6aSSam Leffler 	}
16018a1b9b6aSSam Leffler 	w &= rsn->rsn_keymgmtset;
16028a1b9b6aSSam Leffler 	if (w == 0) {
16038a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
16048a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
16058a1b9b6aSSam Leffler 		    wh, "RSN", "%s", "no acceptable key mgmt alg");
16068a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
16078a1b9b6aSSam Leffler 	}
16088a1b9b6aSSam Leffler 	if (w & RSN_ASE_8021X_UNSPEC)
16098a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
16108a1b9b6aSSam Leffler 	else
16118a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
16128a1b9b6aSSam Leffler 
16138a1b9b6aSSam Leffler 	/* optional RSN capabilities */
16148a1b9b6aSSam Leffler 	if (len > 2)
16158a1b9b6aSSam Leffler 		rsn->rsn_caps = LE_READ_2(frm);
16168a1b9b6aSSam Leffler 	/* XXXPMKID */
16178a1b9b6aSSam Leffler 
16188a1b9b6aSSam Leffler 	return 0;
16198a1b9b6aSSam Leffler }
16208a1b9b6aSSam Leffler 
16218a1b9b6aSSam Leffler static int
16228a1b9b6aSSam Leffler ieee80211_parse_wmeparams(struct ieee80211com *ic, u_int8_t *frm,
16238a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh)
16248a1b9b6aSSam Leffler {
16258a1b9b6aSSam Leffler #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
16268a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
16278a1b9b6aSSam Leffler 	u_int len = frm[1], qosinfo;
16288a1b9b6aSSam Leffler 	int i;
16298a1b9b6aSSam Leffler 
16308a1b9b6aSSam Leffler 	if (len < sizeof(struct ieee80211_wme_param)-2) {
16318a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
16328a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
16338a1b9b6aSSam Leffler 		    wh, "WME", "too short, len %u", len);
1634c0fa32ceSSam Leffler 		return -1;
16358a1b9b6aSSam Leffler 	}
16368a1b9b6aSSam Leffler 	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
16378a1b9b6aSSam Leffler 	qosinfo &= WME_QOSINFO_COUNT;
16388a1b9b6aSSam Leffler 	/* XXX do proper check for wraparound */
16398a1b9b6aSSam Leffler 	if (qosinfo == wme->wme_wmeChanParams.cap_info)
16408a1b9b6aSSam Leffler 		return 0;
16418a1b9b6aSSam Leffler 	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
16428a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
16438a1b9b6aSSam Leffler 		struct wmeParams *wmep =
16448a1b9b6aSSam Leffler 			&wme->wme_wmeChanParams.cap_wmeParams[i];
16458a1b9b6aSSam Leffler 		/* NB: ACI not used */
16468a1b9b6aSSam Leffler 		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
16478a1b9b6aSSam Leffler 		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
16488a1b9b6aSSam Leffler 		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
16498a1b9b6aSSam Leffler 		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
16508a1b9b6aSSam Leffler 		wmep->wmep_txopLimit = LE_READ_2(frm+2);
16518a1b9b6aSSam Leffler 		frm += 4;
16528a1b9b6aSSam Leffler 	}
16538a1b9b6aSSam Leffler 	wme->wme_wmeChanParams.cap_info = qosinfo;
16548a1b9b6aSSam Leffler 	return 1;
16558a1b9b6aSSam Leffler #undef MS
16568a1b9b6aSSam Leffler }
16578a1b9b6aSSam Leffler 
16588a1b9b6aSSam Leffler static void
16598a1b9b6aSSam Leffler ieee80211_saveie(u_int8_t **iep, const u_int8_t *ie)
16608a1b9b6aSSam Leffler {
16618a1b9b6aSSam Leffler 	u_int ielen = ie[1]+2;
16628a1b9b6aSSam Leffler 	/*
16638a1b9b6aSSam Leffler 	 * Record information element for later use.
16648a1b9b6aSSam Leffler 	 */
16658a1b9b6aSSam Leffler 	if (*iep == NULL || (*iep)[1] != ie[1]) {
16668a1b9b6aSSam Leffler 		if (*iep != NULL)
16678a1b9b6aSSam Leffler 			FREE(*iep, M_DEVBUF);
16688a1b9b6aSSam Leffler 		MALLOC(*iep, void*, ielen, M_DEVBUF, M_NOWAIT);
16698a1b9b6aSSam Leffler 	}
16708a1b9b6aSSam Leffler 	if (*iep != NULL)
16718a1b9b6aSSam Leffler 		memcpy(*iep, ie, ielen);
16728a1b9b6aSSam Leffler 	/* XXX note failure */
16738a1b9b6aSSam Leffler }
16748a1b9b6aSSam Leffler 
16758a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
16768a1b9b6aSSam Leffler static void
16778a1b9b6aSSam Leffler dump_probe_beacon(u_int8_t subtype, int isnew,
16788a1b9b6aSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN],
16798a1b9b6aSSam Leffler 	u_int8_t chan, u_int8_t bchan, u_int16_t capinfo, u_int16_t bintval,
16808a1b9b6aSSam Leffler 	u_int8_t erp, u_int8_t *ssid, u_int8_t *country)
16818a1b9b6aSSam Leffler {
16828a1b9b6aSSam Leffler 	printf("[%s] %s%s on chan %u (bss chan %u) ",
16838a1b9b6aSSam Leffler 	    ether_sprintf(mac), isnew ? "new " : "",
16848a1b9b6aSSam Leffler 	    ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
16858a1b9b6aSSam Leffler 	    chan, bchan);
16868a1b9b6aSSam Leffler 	ieee80211_print_essid(ssid + 2, ssid[1]);
16878a1b9b6aSSam Leffler 	printf("\n");
16888a1b9b6aSSam Leffler 
16898a1b9b6aSSam Leffler 	if (isnew) {
16908a1b9b6aSSam Leffler 		printf("[%s] caps 0x%x bintval %u erp 0x%x",
16918a1b9b6aSSam Leffler 			ether_sprintf(mac), capinfo, bintval, erp);
16928a1b9b6aSSam Leffler 		if (country) {
16938a1b9b6aSSam Leffler #ifdef __FreeBSD__
16948a1b9b6aSSam Leffler 			printf(" country info %*D", country[1], country+2, " ");
16958a1b9b6aSSam Leffler #else
16968a1b9b6aSSam Leffler 			int i;
16978a1b9b6aSSam Leffler 			printf(" country info");
16988a1b9b6aSSam Leffler 			for (i = 0; i < country[1]; i++)
16998a1b9b6aSSam Leffler 				printf(" %02x", country[i+2]);
17008a1b9b6aSSam Leffler #endif
17018a1b9b6aSSam Leffler 		}
17028a1b9b6aSSam Leffler 		printf("\n");
17038a1b9b6aSSam Leffler 	}
17048a1b9b6aSSam Leffler }
17058a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
17068a1b9b6aSSam Leffler 
17071a1e1d21SSam Leffler void
17080a915fadSSam Leffler ieee80211_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
17090a915fadSSam Leffler 	struct ieee80211_node *ni,
17100a915fadSSam Leffler 	int subtype, int rssi, u_int32_t rstamp)
17111a1e1d21SSam Leffler {
17128a1b9b6aSSam Leffler #define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
17138a1b9b6aSSam Leffler #define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
17141a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
17151a1e1d21SSam Leffler 	u_int8_t *frm, *efrm;
17168a1b9b6aSSam Leffler 	u_int8_t *ssid, *rates, *xrates, *wpa, *wme;
17178a1b9b6aSSam Leffler 	int reassoc, resp, allocbs;
171849d4c02fSSam Leffler 	u_int8_t rate;
17191a1e1d21SSam Leffler 
17201a1e1d21SSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
17211a1e1d21SSam Leffler 	frm = (u_int8_t *)&wh[1];
17221a1e1d21SSam Leffler 	efrm = mtod(m0, u_int8_t *) + m0->m_len;
17231a1e1d21SSam Leffler 	switch (subtype) {
17241a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
17251a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON: {
1726a634d6a7SSam Leffler 		u_int8_t *tstamp, *country, *tim;
17271a1e1d21SSam Leffler 		u_int8_t chan, bchan, fhindex, erp;
17288a1b9b6aSSam Leffler 		u_int16_t capinfo, bintval, timoff;
17291a1e1d21SSam Leffler 		u_int16_t fhdwell;
17301a1e1d21SSam Leffler 
17318a1b9b6aSSam Leffler 		/*
17328a1b9b6aSSam Leffler 		 * We process beacon/probe response frames:
17338a1b9b6aSSam Leffler 		 *    o when scanning, or
17348a1b9b6aSSam Leffler 		 *    o station mode when associated (to collect state
17358a1b9b6aSSam Leffler 		 *      updates such as 802.11g slot time), or
17368a1b9b6aSSam Leffler 		 *    o adhoc mode (to discover neighbors)
17378a1b9b6aSSam Leffler 		 * Frames otherwise received are discarded.
17388a1b9b6aSSam Leffler 		 */
17398a1b9b6aSSam Leffler 		if (!((ic->ic_flags & IEEE80211_F_SCAN) ||
17408a1b9b6aSSam Leffler 		      (ic->ic_opmode == IEEE80211_M_STA && ni->ni_associd) ||
17415784a371SSam Leffler 		       ic->ic_opmode == IEEE80211_M_IBSS)) {
17428a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
17431a1e1d21SSam Leffler 			return;
17441a1e1d21SSam Leffler 		}
17451a1e1d21SSam Leffler 		/*
17461a1e1d21SSam Leffler 		 * beacon/probe response frame format
17471a1e1d21SSam Leffler 		 *	[8] time stamp
17481a1e1d21SSam Leffler 		 *	[2] beacon interval
17491a1e1d21SSam Leffler 		 *	[2] capability information
17501a1e1d21SSam Leffler 		 *	[tlv] ssid
17511a1e1d21SSam Leffler 		 *	[tlv] supported rates
17521a1e1d21SSam Leffler 		 *	[tlv] country information
17531a1e1d21SSam Leffler 		 *	[tlv] parameter set (FH/DS)
17541a1e1d21SSam Leffler 		 *	[tlv] erp information
17551a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
17568a1b9b6aSSam Leffler 		 *	[tlv] WME
17578a1b9b6aSSam Leffler 		 *	[tlv] WPA or RSN
17581a1e1d21SSam Leffler 		 */
17591a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 12);
17601a1e1d21SSam Leffler 		tstamp  = frm;				frm += 8;
17618a1b9b6aSSam Leffler 		bintval = le16toh(*(u_int16_t *)frm);	frm += 2;
17628a1b9b6aSSam Leffler 		capinfo = le16toh(*(u_int16_t *)frm);	frm += 2;
1763a634d6a7SSam Leffler 		ssid = rates = xrates = country = wpa = wme = tim = NULL;
17641a1e1d21SSam Leffler 		bchan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
17651a1e1d21SSam Leffler 		chan = bchan;
17661a1e1d21SSam Leffler 		fhdwell = 0;
17671a1e1d21SSam Leffler 		fhindex = 0;
17681a1e1d21SSam Leffler 		erp = 0;
17698a1b9b6aSSam Leffler 		timoff = 0;
17701a1e1d21SSam Leffler 		while (frm < efrm) {
17711a1e1d21SSam Leffler 			switch (*frm) {
17721a1e1d21SSam Leffler 			case IEEE80211_ELEMID_SSID:
17731a1e1d21SSam Leffler 				ssid = frm;
17741a1e1d21SSam Leffler 				break;
17751a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
17761a1e1d21SSam Leffler 				rates = frm;
17771a1e1d21SSam Leffler 				break;
17781a1e1d21SSam Leffler 			case IEEE80211_ELEMID_COUNTRY:
17791a1e1d21SSam Leffler 				country = frm;
17801a1e1d21SSam Leffler 				break;
17811a1e1d21SSam Leffler 			case IEEE80211_ELEMID_FHPARMS:
17821a1e1d21SSam Leffler 				if (ic->ic_phytype == IEEE80211_T_FH) {
17838a1b9b6aSSam Leffler 					fhdwell = LE_READ_2(&frm[2]);
17841a1e1d21SSam Leffler 					chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
17851a1e1d21SSam Leffler 					fhindex = frm[6];
17861a1e1d21SSam Leffler 				}
17871a1e1d21SSam Leffler 				break;
17881a1e1d21SSam Leffler 			case IEEE80211_ELEMID_DSPARMS:
17891a1e1d21SSam Leffler 				/*
17901a1e1d21SSam Leffler 				 * XXX hack this since depending on phytype
17911a1e1d21SSam Leffler 				 * is problematic for multi-mode devices.
17921a1e1d21SSam Leffler 				 */
17931a1e1d21SSam Leffler 				if (ic->ic_phytype != IEEE80211_T_FH)
17941a1e1d21SSam Leffler 					chan = frm[2];
17951a1e1d21SSam Leffler 				break;
17961a1e1d21SSam Leffler 			case IEEE80211_ELEMID_TIM:
17978a1b9b6aSSam Leffler 				/* XXX ATIM? */
1798a634d6a7SSam Leffler 				tim = frm;
17998a1b9b6aSSam Leffler 				timoff = frm - mtod(m0, u_int8_t *);
18001a1e1d21SSam Leffler 				break;
18014bd067c5SSam Leffler 			case IEEE80211_ELEMID_IBSSPARMS:
18024bd067c5SSam Leffler 				break;
18031a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
18041a1e1d21SSam Leffler 				xrates = frm;
18051a1e1d21SSam Leffler 				break;
18061a1e1d21SSam Leffler 			case IEEE80211_ELEMID_ERP:
18071a1e1d21SSam Leffler 				if (frm[1] != 1) {
18088a1b9b6aSSam Leffler 					IEEE80211_DISCARD_IE(ic,
18098a1b9b6aSSam Leffler 					    IEEE80211_MSG_ELEMID, wh, "ERP",
18108a1b9b6aSSam Leffler 					    "bad len %u", frm[1]);
18111be50176SSam Leffler 					ic->ic_stats.is_rx_elem_toobig++;
18121a1e1d21SSam Leffler 					break;
18131a1e1d21SSam Leffler 				}
18141a1e1d21SSam Leffler 				erp = frm[2];
18151a1e1d21SSam Leffler 				break;
18168a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_RSN:
18178a1b9b6aSSam Leffler 				wpa = frm;
18188a1b9b6aSSam Leffler 				break;
18198a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
18208a1b9b6aSSam Leffler 				if (iswpaoui(frm))
18218a1b9b6aSSam Leffler 					wpa = frm;
18228a1b9b6aSSam Leffler 				else if (iswmeparam(frm) || iswmeinfo(frm))
18238a1b9b6aSSam Leffler 					wme = frm;
18248a1b9b6aSSam Leffler 				/* XXX Atheros OUI support */
18258a1b9b6aSSam Leffler 				break;
18261a1e1d21SSam Leffler 			default:
18278a1b9b6aSSam Leffler 				IEEE80211_DISCARD_IE(ic, IEEE80211_MSG_ELEMID,
18288a1b9b6aSSam Leffler 				    wh, "unhandled",
18298a1b9b6aSSam Leffler 				    "id %u, len %u", *frm, frm[1]);
18301be50176SSam Leffler 				ic->ic_stats.is_rx_elem_unknown++;
18311a1e1d21SSam Leffler 				break;
18321a1e1d21SSam Leffler 			}
18331a1e1d21SSam Leffler 			frm += frm[1] + 2;
18341a1e1d21SSam Leffler 		}
1835dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
18361a1e1d21SSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
1837a11c9a5cSSam Leffler 		if (
1838a11c9a5cSSam Leffler #if IEEE80211_CHAN_MAX < 255
1839a11c9a5cSSam Leffler 		    chan > IEEE80211_CHAN_MAX ||
1840a11c9a5cSSam Leffler #endif
1841a11c9a5cSSam Leffler 		    isclr(ic->ic_chan_active, chan)) {
18428a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,
18438a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
18448a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
18458a1b9b6aSSam Leffler 			    "invalid channel %u", chan);
18461be50176SSam Leffler 			ic->ic_stats.is_rx_badchan++;
18471a1e1d21SSam Leffler 			return;
18481a1e1d21SSam Leffler 		}
18494844aa7dSAtsushi Onoe 		if (chan != bchan && ic->ic_phytype != IEEE80211_T_FH) {
18501a1e1d21SSam Leffler 			/*
18511a1e1d21SSam Leffler 			 * Frame was received on a channel different from the
18524844aa7dSAtsushi Onoe 			 * one indicated in the DS params element id;
18531a1e1d21SSam Leffler 			 * silently discard it.
18541a1e1d21SSam Leffler 			 *
18551a1e1d21SSam Leffler 			 * NB: this can happen due to signal leakage.
18564844aa7dSAtsushi Onoe 			 *     But we should take it for FH phy because
18574844aa7dSAtsushi Onoe 			 *     the rssi value should be correct even for
18584844aa7dSAtsushi Onoe 			 *     different hop pattern in FH.
18591a1e1d21SSam Leffler 			 */
18608a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,
18618a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
18628a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
18636b312781SSam Leffler 			    "for off-channel %u", chan);
18641be50176SSam Leffler 			ic->ic_stats.is_rx_chanmismatch++;
18651a1e1d21SSam Leffler 			return;
18661a1e1d21SSam Leffler 		}
18671a1e1d21SSam Leffler 
18681a1e1d21SSam Leffler 		/*
186944c72e42SSam Leffler 		 * Count frame now that we know it's to be processed.
187044c72e42SSam Leffler 		 */
187144c72e42SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
187244c72e42SSam Leffler 			ic->ic_stats.is_rx_beacon++;		/* XXX remove */
187344c72e42SSam Leffler 			IEEE80211_NODE_STAT(ni, rx_beacons);
187444c72e42SSam Leffler 		} else
187544c72e42SSam Leffler 			IEEE80211_NODE_STAT(ni, rx_proberesp);
187644c72e42SSam Leffler 
187744c72e42SSam Leffler 		/*
18788a1b9b6aSSam Leffler 		 * When operating in station mode, check for state updates.
18798a1b9b6aSSam Leffler 		 * Be careful to ignore beacons received while doing a
18808a1b9b6aSSam Leffler 		 * background scan.  We consider only 11g/WMM stuff right now.
18811a1e1d21SSam Leffler 		 */
18828a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA &&
18838a1b9b6aSSam Leffler 		    ni->ni_associd != 0 &&
18848a1b9b6aSSam Leffler 		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
18858a1b9b6aSSam Leffler 		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1886f1e95a23SSam Leffler 			/* record tsf of last beacon */
1887f1e95a23SSam Leffler 			memcpy(ni->ni_tstamp.data, tstamp,
1888f1e95a23SSam Leffler 				sizeof(ni->ni_tstamp));
18898a1b9b6aSSam Leffler 			if (ni->ni_erp != erp) {
18908a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
18918a1b9b6aSSam Leffler 				    "[%s] erp change: was 0x%x, now 0x%x\n",
18928a1b9b6aSSam Leffler 				    ether_sprintf(wh->i_addr2),
18938a1b9b6aSSam Leffler 				    ni->ni_erp, erp);
1894ec425115SSam Leffler 				if (ic->ic_curmode == IEEE80211_MODE_11G &&
1895ec425115SSam Leffler 				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
18968a1b9b6aSSam Leffler 					ic->ic_flags |= IEEE80211_F_USEPROT;
18978a1b9b6aSSam Leffler 				else
18988a1b9b6aSSam Leffler 					ic->ic_flags &= ~IEEE80211_F_USEPROT;
18998a1b9b6aSSam Leffler 				ni->ni_erp = erp;
19008a1b9b6aSSam Leffler 				/* XXX statistic */
19011a1e1d21SSam Leffler 			}
19028a1b9b6aSSam Leffler 			if ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
19038a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
19048a1b9b6aSSam Leffler 				    "[%s] capabilities change: before 0x%x,"
19058a1b9b6aSSam Leffler 				     " now 0x%x\n",
19068a1b9b6aSSam Leffler 				     ether_sprintf(wh->i_addr2),
19078a1b9b6aSSam Leffler 				     ni->ni_capinfo, capinfo);
19088a1b9b6aSSam Leffler 				/*
19098a1b9b6aSSam Leffler 				 * NB: we assume short preamble doesn't
19108a1b9b6aSSam Leffler 				 *     change dynamically
19118a1b9b6aSSam Leffler 				 */
19128a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic,
19138a1b9b6aSSam Leffler 					ic->ic_curmode == IEEE80211_MODE_11A ||
19148a1b9b6aSSam Leffler 					(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
19158a1b9b6aSSam Leffler 				ni->ni_capinfo = capinfo;
19168a1b9b6aSSam Leffler 				/* XXX statistic */
19178a1b9b6aSSam Leffler 			}
19188a1b9b6aSSam Leffler 			if (wme != NULL &&
19193fd5a5aaSSam Leffler 			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
1920c0fa32ceSSam Leffler 			    ieee80211_parse_wmeparams(ic, wme, wh) > 0)
19218a1b9b6aSSam Leffler 				ieee80211_wme_updateparams(ic);
1922a634d6a7SSam Leffler 			if (tim != NULL) {
1923a634d6a7SSam Leffler 				struct ieee80211_tim_ie *ie =
1924a634d6a7SSam Leffler 				    (struct ieee80211_tim_ie *) tim;
1925a634d6a7SSam Leffler 
1926a634d6a7SSam Leffler 				ni->ni_dtim_count = ie->tim_count;
1927a634d6a7SSam Leffler 				ni->ni_dtim_period = ie->tim_period;
1928a634d6a7SSam Leffler 			}
19298a1b9b6aSSam Leffler 			/* NB: don't need the rest of this */
193065e278a8SSam Leffler 			if ((ic->ic_flags & IEEE80211_F_SCAN) == 0)
19318a1b9b6aSSam Leffler 				return;
19328a1b9b6aSSam Leffler 		}
19338a1b9b6aSSam Leffler 
1934720bd46eSTai-hwa Liang 		if (ni == ic->ic_bss &&
1935720bd46eSTai-hwa Liang 		    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid)) {
19368a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
19378a1b9b6aSSam Leffler 			if (ieee80211_msg_scan(ic))
19388a1b9b6aSSam Leffler 				dump_probe_beacon(subtype, 1,
19398a1b9b6aSSam Leffler 				    wh->i_addr2, chan, bchan, capinfo,
19408a1b9b6aSSam Leffler 				    bintval, erp, ssid, country);
19411a1e1d21SSam Leffler #endif
19428a1b9b6aSSam Leffler 			/*
19438a1b9b6aSSam Leffler 			 * Create a new entry.  If scanning the entry goes
19448a1b9b6aSSam Leffler 			 * in the scan cache.  Otherwise, be particular when
19458a1b9b6aSSam Leffler 			 * operating in adhoc mode--only take nodes marked
19468a1b9b6aSSam Leffler 			 * as ibss participants so we don't populate our
19478a1b9b6aSSam Leffler 			 * neighbor table with unintersting sta's.
19488a1b9b6aSSam Leffler 			 */
19498a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
19508a1b9b6aSSam Leffler 				if ((capinfo & IEEE80211_CAPINFO_IBSS) == 0)
19518a1b9b6aSSam Leffler 					return;
1952acc4f7f5SSam Leffler 				ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
19538a1b9b6aSSam Leffler 						wh->i_addr2);
19548a1b9b6aSSam Leffler 			} else
19558a1b9b6aSSam Leffler 				ni = ieee80211_dup_bss(&ic->ic_scan, wh->i_addr2);
1956c64bfa0fSSam Leffler 			if (ni == NULL)
19571a1e1d21SSam Leffler 				return;
19581a1e1d21SSam Leffler 			ni->ni_esslen = ssid[1];
19591a1e1d21SSam Leffler 			memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
19601a1e1d21SSam Leffler 			memcpy(ni->ni_essid, ssid + 2, ssid[1]);
19618a1b9b6aSSam Leffler 		} else if (ssid[1] != 0 &&
19628a1b9b6aSSam Leffler 		    (ISPROBE(subtype) || ni->ni_esslen == 0)) {
19631a1e1d21SSam Leffler 			/*
19648a1b9b6aSSam Leffler 			 * Update ESSID at probe response to adopt
19658a1b9b6aSSam Leffler 			 * hidden AP by Lucent/Cisco, which announces
19668a1b9b6aSSam Leffler 			 * null ESSID in beacon.
19671a1e1d21SSam Leffler 			 */
19688a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
19698a1b9b6aSSam Leffler 			if (ieee80211_msg_scan(ic) ||
19708a1b9b6aSSam Leffler 			    ieee80211_msg_debug(ic))
19718a1b9b6aSSam Leffler 				dump_probe_beacon(subtype, 0,
19728a1b9b6aSSam Leffler 				    wh->i_addr2, chan, bchan, capinfo,
19738a1b9b6aSSam Leffler 				    bintval, erp, ssid, country);
19748a1b9b6aSSam Leffler #endif
19751a1e1d21SSam Leffler 			ni->ni_esslen = ssid[1];
19761a1e1d21SSam Leffler 			memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
19771a1e1d21SSam Leffler 			memcpy(ni->ni_essid, ssid + 2, ssid[1]);
19788a1b9b6aSSam Leffler 		}
19798a1b9b6aSSam Leffler 		ni->ni_scangen = ic->ic_scan.nt_scangen;
19801a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
19811a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
19821a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
19838a1b9b6aSSam Leffler 		memcpy(ni->ni_tstamp.data, tstamp, sizeof(ni->ni_tstamp));
19848a1b9b6aSSam Leffler 		ni->ni_intval = bintval;
19858a1b9b6aSSam Leffler 		ni->ni_capinfo = capinfo;
19861a1e1d21SSam Leffler 		ni->ni_chan = &ic->ic_channels[chan];
19871a1e1d21SSam Leffler 		ni->ni_fhdwell = fhdwell;
19881a1e1d21SSam Leffler 		ni->ni_fhindex = fhindex;
19891a1e1d21SSam Leffler 		ni->ni_erp = erp;
1990a634d6a7SSam Leffler 		if (tim != NULL) {
1991a634d6a7SSam Leffler 			struct ieee80211_tim_ie *ie =
1992a634d6a7SSam Leffler 			    (struct ieee80211_tim_ie *) tim;
1993a634d6a7SSam Leffler 
1994a634d6a7SSam Leffler 			ni->ni_dtim_count = ie->tim_count;
1995a634d6a7SSam Leffler 			ni->ni_dtim_period = ie->tim_period;
1996a634d6a7SSam Leffler 		}
19978a1b9b6aSSam Leffler 		/*
19988a1b9b6aSSam Leffler 		 * Record the byte offset from the mac header to
19998a1b9b6aSSam Leffler 		 * the start of the TIM information element for
20008a1b9b6aSSam Leffler 		 * use by hardware and/or to speedup software
20018a1b9b6aSSam Leffler 		 * processing of beacon frames.
20028a1b9b6aSSam Leffler 		 */
20038a1b9b6aSSam Leffler 		ni->ni_timoff = timoff;
20048a1b9b6aSSam Leffler 		/*
20058a1b9b6aSSam Leffler 		 * Record optional information elements that might be
20068a1b9b6aSSam Leffler 		 * used by applications or drivers.
20078a1b9b6aSSam Leffler 		 */
20088a1b9b6aSSam Leffler 		if (wme != NULL)
20098a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wme_ie, wme);
20108a1b9b6aSSam Leffler 		if (wpa != NULL)
20118a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wpa_ie, wpa);
20121a1e1d21SSam Leffler 		/* NB: must be after ni_chan is setup */
20131a1e1d21SSam Leffler 		ieee80211_setup_rates(ic, ni, rates, xrates, IEEE80211_F_DOSORT);
20141a1e1d21SSam Leffler 		break;
20151a1e1d21SSam Leffler 	}
20161a1e1d21SSam Leffler 
201749d4c02fSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
20188a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA ||
20198a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_RUN) {
20208a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
20211a1e1d21SSam Leffler 			return;
20228a1b9b6aSSam Leffler 		}
20238a1b9b6aSSam Leffler 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
20248a1b9b6aSSam Leffler 			/* frame must be directed */
20258a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;	/* XXX stat */
20261a1e1d21SSam Leffler 			return;
20278a1b9b6aSSam Leffler 		}
20281a1e1d21SSam Leffler 
20291a1e1d21SSam Leffler 		/*
20301a1e1d21SSam Leffler 		 * prreq frame format
20311a1e1d21SSam Leffler 		 *	[tlv] ssid
20321a1e1d21SSam Leffler 		 *	[tlv] supported rates
20331a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
20341a1e1d21SSam Leffler 		 */
20351a1e1d21SSam Leffler 		ssid = rates = xrates = NULL;
20361a1e1d21SSam Leffler 		while (frm < efrm) {
20371a1e1d21SSam Leffler 			switch (*frm) {
20381a1e1d21SSam Leffler 			case IEEE80211_ELEMID_SSID:
20391a1e1d21SSam Leffler 				ssid = frm;
20401a1e1d21SSam Leffler 				break;
20411a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
20421a1e1d21SSam Leffler 				rates = frm;
20431a1e1d21SSam Leffler 				break;
20441a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
20451a1e1d21SSam Leffler 				xrates = frm;
20461a1e1d21SSam Leffler 				break;
20471a1e1d21SSam Leffler 			}
20481a1e1d21SSam Leffler 			frm += frm[1] + 2;
20491a1e1d21SSam Leffler 		}
2050dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
20511a1e1d21SSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
20528a1b9b6aSSam Leffler 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid);
20534ef04d32SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
20544ef04d32SSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
20554ef04d32SSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
20564ef04d32SSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
20574ef04d32SSam Leffler 			    "%s", "no ssid with ssid suppression enabled");
20584ef04d32SSam Leffler 			ic->ic_stats.is_rx_ssidmismatch++; /*XXX*/
20594ef04d32SSam Leffler 			return;
20604ef04d32SSam Leffler 		}
20611a1e1d21SSam Leffler 
20620a915fadSSam Leffler 		if (ni == ic->ic_bss) {
20638a1b9b6aSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_IBSS) {
20648a1b9b6aSSam Leffler 				/*
20658a1b9b6aSSam Leffler 				 * XXX Cannot tell if the sender is operating
20668a1b9b6aSSam Leffler 				 * in ibss mode.  But we need a new node to
20678a1b9b6aSSam Leffler 				 * send the response so blindly add them to the
20688a1b9b6aSSam Leffler 				 * neighbor table.
20698a1b9b6aSSam Leffler 				 */
2070acc4f7f5SSam Leffler 				ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
20718a1b9b6aSSam Leffler 					wh->i_addr2);
20728a1b9b6aSSam Leffler 			} else
2073acc4f7f5SSam Leffler 				ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
2074c64bfa0fSSam Leffler 			if (ni == NULL)
20751a1e1d21SSam Leffler 				return;
20761a1e1d21SSam Leffler 			allocbs = 1;
20771a1e1d21SSam Leffler 		} else
20781a1e1d21SSam Leffler 			allocbs = 0;
20798a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
20808a1b9b6aSSam Leffler 		    "[%s] recv probe req\n", ether_sprintf(wh->i_addr2));
20811a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
20821a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
20831a1e1d21SSam Leffler 		rate = ieee80211_setup_rates(ic, ni, rates, xrates,
20841a1e1d21SSam Leffler 			  IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE
20851a1e1d21SSam Leffler 			| IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
20861a1e1d21SSam Leffler 		if (rate & IEEE80211_RATE_BASIC) {
20878a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_XRATE,
20888a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
20898a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
20908a1b9b6aSSam Leffler 			    "%s", "recv'd rate set invalid");
20911a1e1d21SSam Leffler 		} else {
20920a915fadSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni,
20930a915fadSSam Leffler 				IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
20941a1e1d21SSam Leffler 		}
20958a1b9b6aSSam Leffler 		if (allocbs && ic->ic_opmode != IEEE80211_M_IBSS) {
20968a1b9b6aSSam Leffler 			/* reclaim immediately */
20978a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
20988a1b9b6aSSam Leffler 		}
20991a1e1d21SSam Leffler 		break;
21001a1e1d21SSam Leffler 
21011a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH: {
21021a1e1d21SSam Leffler 		u_int16_t algo, seq, status;
21031a1e1d21SSam Leffler 		/*
21041a1e1d21SSam Leffler 		 * auth frame format
21051a1e1d21SSam Leffler 		 *	[2] algorithm
21061a1e1d21SSam Leffler 		 *	[2] sequence
21071a1e1d21SSam Leffler 		 *	[2] status
21081a1e1d21SSam Leffler 		 *	[tlv*] challenge
21091a1e1d21SSam Leffler 		 */
21101a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
21111a1e1d21SSam Leffler 		algo   = le16toh(*(u_int16_t *)frm);
21121a1e1d21SSam Leffler 		seq    = le16toh(*(u_int16_t *)(frm + 2));
21131a1e1d21SSam Leffler 		status = le16toh(*(u_int16_t *)(frm + 4));
21148a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
21158a1b9b6aSSam Leffler 		    "[%s] recv auth frame with algorithm %d seq %d\n",
21168a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr2), algo, seq);
21178a1b9b6aSSam Leffler 		/*
21188a1b9b6aSSam Leffler 		 * Consult the ACL policy module if setup.
21198a1b9b6aSSam Leffler 		 */
21208a1b9b6aSSam Leffler 		if (ic->ic_acl != NULL &&
21218a1b9b6aSSam Leffler 		    !ic->ic_acl->iac_check(ic, wh->i_addr2)) {
21228a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ACL,
21238a1b9b6aSSam Leffler 			    wh, "auth", "%s", "disallowed by ACL");
21248a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_acl++;
21251a1e1d21SSam Leffler 			return;
21261a1e1d21SSam Leffler 		}
21278a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_COUNTERM) {
21288a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic,
21298a1b9b6aSSam Leffler 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
21308a1b9b6aSSam Leffler 			    wh, "auth", "%s", "TKIP countermeasures enabled");
21318a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_countermeasures++;
21328a1b9b6aSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
21330a915fadSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
21348a1b9b6aSSam Leffler 					IEEE80211_FC0_SUBTYPE_AUTH,
21358a1b9b6aSSam Leffler 					IEEE80211_REASON_MIC_FAILURE);
213659571d2bSSam Leffler 			}
21371a1e1d21SSam Leffler 			return;
21381a1e1d21SSam Leffler 		}
21398a1b9b6aSSam Leffler 		if (algo == IEEE80211_AUTH_ALG_SHARED)
21408a1b9b6aSSam Leffler 			ieee80211_auth_shared(ic, wh, frm + 6, efrm, ni, rssi,
21418a1b9b6aSSam Leffler 			    rstamp, seq, status);
21428a1b9b6aSSam Leffler 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
21438a1b9b6aSSam Leffler 			ieee80211_auth_open(ic, wh, ni, rssi, rstamp, seq,
21448a1b9b6aSSam Leffler 			    status);
21458a1b9b6aSSam Leffler 		else {
21468a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
21478a1b9b6aSSam Leffler 			    wh, "auth", "unsupported alg %d", algo);
21488a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_unsupported++;
21498a1b9b6aSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
21508a1b9b6aSSam Leffler 				/* XXX not right */
21518a1b9b6aSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
21528a1b9b6aSSam Leffler 					IEEE80211_FC0_SUBTYPE_AUTH,
21538a1b9b6aSSam Leffler 					(seq+1) | (IEEE80211_STATUS_ALG<<16));
21548a1b9b6aSSam Leffler 			}
21558a1b9b6aSSam Leffler 			return;
21561a1e1d21SSam Leffler 		}
21571a1e1d21SSam Leffler 		break;
21581a1e1d21SSam Leffler 	}
21591a1e1d21SSam Leffler 
21601a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
21611a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
21621a1e1d21SSam Leffler 		u_int16_t capinfo, bintval;
21638a1b9b6aSSam Leffler 		struct ieee80211_rsnparms rsn;
21648a1b9b6aSSam Leffler 		u_int8_t reason;
21651a1e1d21SSam Leffler 
21661a1e1d21SSam Leffler 		if (ic->ic_opmode != IEEE80211_M_HOSTAP ||
21678a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_RUN) {
21688a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
21691a1e1d21SSam Leffler 			return;
21708a1b9b6aSSam Leffler 		}
21711a1e1d21SSam Leffler 
21721a1e1d21SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
21731a1e1d21SSam Leffler 			reassoc = 1;
21741a1e1d21SSam Leffler 			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
21751a1e1d21SSam Leffler 		} else {
21761a1e1d21SSam Leffler 			reassoc = 0;
21771a1e1d21SSam Leffler 			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
21781a1e1d21SSam Leffler 		}
21791a1e1d21SSam Leffler 		/*
21801a1e1d21SSam Leffler 		 * asreq frame format
21811a1e1d21SSam Leffler 		 *	[2] capability information
21821a1e1d21SSam Leffler 		 *	[2] listen interval
21831a1e1d21SSam Leffler 		 *	[6*] current AP address (reassoc only)
21841a1e1d21SSam Leffler 		 *	[tlv] ssid
21851a1e1d21SSam Leffler 		 *	[tlv] supported rates
21861a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
21878a1b9b6aSSam Leffler 		 *	[tlv] WPA or RSN
21881a1e1d21SSam Leffler 		 */
21891a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4));
21901a1e1d21SSam Leffler 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_bss->ni_bssid)) {
21918a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
21928a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
21938a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
21948a1b9b6aSSam Leffler 			    "%s", "wrong bssid");
21951be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_bss++;
21961a1e1d21SSam Leffler 			return;
21971a1e1d21SSam Leffler 		}
21981a1e1d21SSam Leffler 		capinfo = le16toh(*(u_int16_t *)frm);	frm += 2;
21991a1e1d21SSam Leffler 		bintval = le16toh(*(u_int16_t *)frm);	frm += 2;
22001a1e1d21SSam Leffler 		if (reassoc)
22011a1e1d21SSam Leffler 			frm += 6;	/* ignore current AP info */
22028a1b9b6aSSam Leffler 		ssid = rates = xrates = wpa = wme = NULL;
22031a1e1d21SSam Leffler 		while (frm < efrm) {
22041a1e1d21SSam Leffler 			switch (*frm) {
22051a1e1d21SSam Leffler 			case IEEE80211_ELEMID_SSID:
22061a1e1d21SSam Leffler 				ssid = frm;
22071a1e1d21SSam Leffler 				break;
22081a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
22091a1e1d21SSam Leffler 				rates = frm;
22101a1e1d21SSam Leffler 				break;
22111a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
22121a1e1d21SSam Leffler 				xrates = frm;
22131a1e1d21SSam Leffler 				break;
22148a1b9b6aSSam Leffler 			/* XXX verify only one of RSN and WPA ie's? */
22158a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_RSN:
22168a1b9b6aSSam Leffler 				wpa = frm;
22178a1b9b6aSSam Leffler 				break;
22188a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
22198a1b9b6aSSam Leffler 				if (iswpaoui(frm)) {
22208a1b9b6aSSam Leffler 					if (ic->ic_flags & IEEE80211_F_WPA1)
22218a1b9b6aSSam Leffler 						wpa = frm;
22228a1b9b6aSSam Leffler 				} else if (iswmeinfo(frm))
22238a1b9b6aSSam Leffler 					wme = frm;
22248a1b9b6aSSam Leffler 				/* XXX Atheros OUI support */
22258a1b9b6aSSam Leffler 				break;
22261a1e1d21SSam Leffler 			}
22271a1e1d21SSam Leffler 			frm += frm[1] + 2;
22281a1e1d21SSam Leffler 		}
2229dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
22301a1e1d21SSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
22318a1b9b6aSSam Leffler 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid);
22328a1b9b6aSSam Leffler 
22330a915fadSSam Leffler 		if (ni == ic->ic_bss) {
22348a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
22358a1b9b6aSSam Leffler 			    "[%s] deny %s request, sta not authenticated\n",
22368a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
22378a1b9b6aSSam Leffler 			    reassoc ? "reassoc" : "assoc");
223884eb84c4SSam Leffler 			ieee80211_send_error(ic, ni, wh->i_addr2,
22390a915fadSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
22401a1e1d21SSam Leffler 			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
22411be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_notauth++;
22421a1e1d21SSam Leffler 			return;
22431a1e1d21SSam Leffler 		}
2244c9a4bb99SSam Leffler 		/* assert right associstion security credentials */
2245c9a4bb99SSam Leffler 		if (wpa == NULL && (ic->ic_flags & IEEE80211_F_WPA)) {
2246c9a4bb99SSam Leffler 			IEEE80211_DPRINTF(ic,
2247c9a4bb99SSam Leffler 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
2248c9a4bb99SSam Leffler 			    "[%s] no WPA/RSN IE in association request\n",
2249c9a4bb99SSam Leffler 			    ether_sprintf(wh->i_addr2));
2250c9a4bb99SSam Leffler 			IEEE80211_SEND_MGMT(ic, ni,
2251c9a4bb99SSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
2252c9a4bb99SSam Leffler 			    IEEE80211_REASON_RSN_REQUIRED);
2253c9a4bb99SSam Leffler 			ieee80211_node_leave(ic, ni);
2254c9a4bb99SSam Leffler 			/* XXX distinguish WPA/RSN? */
2255c9a4bb99SSam Leffler 			ic->ic_stats.is_rx_assoc_badwpaie++;
2256c9a4bb99SSam Leffler 			return;
2257c9a4bb99SSam Leffler 		}
22588a1b9b6aSSam Leffler 		if (wpa != NULL) {
22598a1b9b6aSSam Leffler 			/*
22608a1b9b6aSSam Leffler 			 * Parse WPA information element.  Note that
22618a1b9b6aSSam Leffler 			 * we initialize the param block from the node
22628a1b9b6aSSam Leffler 			 * state so that information in the IE overrides
22638a1b9b6aSSam Leffler 			 * our defaults.  The resulting parameters are
22648a1b9b6aSSam Leffler 			 * installed below after the association is assured.
22658a1b9b6aSSam Leffler 			 */
22668a1b9b6aSSam Leffler 			rsn = ni->ni_rsn;
22678a1b9b6aSSam Leffler 			if (wpa[0] != IEEE80211_ELEMID_RSN)
22688a1b9b6aSSam Leffler 				reason = ieee80211_parse_wpa(ic, wpa, &rsn, wh);
22698a1b9b6aSSam Leffler 			else
22708a1b9b6aSSam Leffler 				reason = ieee80211_parse_rsn(ic, wpa, &rsn, wh);
22718a1b9b6aSSam Leffler 			if (reason != 0) {
22728a1b9b6aSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
22738a1b9b6aSSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
22748a1b9b6aSSam Leffler 				ieee80211_node_leave(ic, ni);
22758a1b9b6aSSam Leffler 				/* XXX distinguish WPA/RSN? */
22768a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_assoc_badwpaie++;
22778a1b9b6aSSam Leffler 				return;
22788a1b9b6aSSam Leffler 			}
22798a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
22808a1b9b6aSSam Leffler 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
22818a1b9b6aSSam Leffler 			    "[%s] %s ie: mc %u/%u uc %u/%u key %u caps 0x%x\n",
22828a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
22838a1b9b6aSSam Leffler 			    wpa[0] != IEEE80211_ELEMID_RSN ?  "WPA" : "RSN",
22848a1b9b6aSSam Leffler 			    rsn.rsn_mcastcipher, rsn.rsn_mcastkeylen,
22858a1b9b6aSSam Leffler 			    rsn.rsn_ucastcipher, rsn.rsn_ucastkeylen,
22868a1b9b6aSSam Leffler 			    rsn.rsn_keymgmt, rsn.rsn_caps);
22878a1b9b6aSSam Leffler 		}
22888a1b9b6aSSam Leffler 		/* discard challenge after association */
22898a1b9b6aSSam Leffler 		if (ni->ni_challenge != NULL) {
22908a1b9b6aSSam Leffler 			FREE(ni->ni_challenge, M_DEVBUF);
22918a1b9b6aSSam Leffler 			ni->ni_challenge = NULL;
22928a1b9b6aSSam Leffler 		}
22939b4db829SSam Leffler 		/* NB: 802.11 spec says to ignore station's privacy bit */
22949b4db829SSam Leffler 		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
22958a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
22968a1b9b6aSSam Leffler 			    "[%s] deny %s request, capability mismatch 0x%x\n",
22978a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
22988a1b9b6aSSam Leffler 			    reassoc ? "reassoc" : "assoc", capinfo);
22990a915fadSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
23000a915fadSSam Leffler 				IEEE80211_STATUS_CAPINFO);
23018a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
23021be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_capmismatch++;
23031a1e1d21SSam Leffler 			return;
23041a1e1d21SSam Leffler 		}
230549d4c02fSSam Leffler 		rate = ieee80211_setup_rates(ic, ni, rates, xrates,
23061a1e1d21SSam Leffler 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
23071a1e1d21SSam Leffler 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
230849d4c02fSSam Leffler 		if (rate & IEEE80211_RATE_BASIC) {
23098a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
23108a1b9b6aSSam Leffler 			    "[%s] deny %s request, rate set mismatch\n",
23118a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
23128a1b9b6aSSam Leffler 			    reassoc ? "reassoc" : "assoc");
23130a915fadSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
23140a915fadSSam Leffler 				IEEE80211_STATUS_BASIC_RATE);
23158a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
23161be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_norate++;
23171a1e1d21SSam Leffler 			return;
23181a1e1d21SSam Leffler 		}
23191a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
23201a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
23211a1e1d21SSam Leffler 		ni->ni_intval = bintval;
23221a1e1d21SSam Leffler 		ni->ni_capinfo = capinfo;
23231a1e1d21SSam Leffler 		ni->ni_chan = ic->ic_bss->ni_chan;
23241a1e1d21SSam Leffler 		ni->ni_fhdwell = ic->ic_bss->ni_fhdwell;
23251a1e1d21SSam Leffler 		ni->ni_fhindex = ic->ic_bss->ni_fhindex;
23268a1b9b6aSSam Leffler 		if (wpa != NULL) {
23278a1b9b6aSSam Leffler 			/*
23288a1b9b6aSSam Leffler 			 * Record WPA/RSN parameters for station, mark
23298a1b9b6aSSam Leffler 			 * node as using WPA and record information element
23308a1b9b6aSSam Leffler 			 * for applications that require it.
23318a1b9b6aSSam Leffler 			 */
23328a1b9b6aSSam Leffler 			ni->ni_rsn = rsn;
23338a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wpa_ie, wpa);
23348a1b9b6aSSam Leffler 		} else if (ni->ni_wpa_ie != NULL) {
23358a1b9b6aSSam Leffler 			/*
23368a1b9b6aSSam Leffler 			 * Flush any state from a previous association.
23378a1b9b6aSSam Leffler 			 */
23388a1b9b6aSSam Leffler 			FREE(ni->ni_wpa_ie, M_DEVBUF);
23398a1b9b6aSSam Leffler 			ni->ni_wpa_ie = NULL;
23408a1b9b6aSSam Leffler 		}
23418a1b9b6aSSam Leffler 		if (wme != NULL) {
23428a1b9b6aSSam Leffler 			/*
23438a1b9b6aSSam Leffler 			 * Record WME parameters for station, mark node
23448a1b9b6aSSam Leffler 			 * as capable of QoS and record information
23458a1b9b6aSSam Leffler 			 * element for applications that require it.
23468a1b9b6aSSam Leffler 			 */
23478a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wme_ie, wme);
23488a1b9b6aSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
23498a1b9b6aSSam Leffler 		} else if (ni->ni_wme_ie != NULL) {
23508a1b9b6aSSam Leffler 			/*
23518a1b9b6aSSam Leffler 			 * Flush any state from a previous association.
23528a1b9b6aSSam Leffler 			 */
23538a1b9b6aSSam Leffler 			FREE(ni->ni_wme_ie, M_DEVBUF);
23548a1b9b6aSSam Leffler 			ni->ni_wme_ie = NULL;
23558a1b9b6aSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
23568a1b9b6aSSam Leffler 		}
23578a1b9b6aSSam Leffler 		ieee80211_node_join(ic, ni, resp);
23581a1e1d21SSam Leffler 		break;
23591a1e1d21SSam Leffler 	}
23601a1e1d21SSam Leffler 
23611a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
23621a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
23638a1b9b6aSSam Leffler 		u_int16_t capinfo, associd;
23641a1e1d21SSam Leffler 		u_int16_t status;
23651a1e1d21SSam Leffler 
23661a1e1d21SSam Leffler 		if (ic->ic_opmode != IEEE80211_M_STA ||
23678a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_ASSOC) {
23688a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
23691a1e1d21SSam Leffler 			return;
23708a1b9b6aSSam Leffler 		}
23711a1e1d21SSam Leffler 
23721a1e1d21SSam Leffler 		/*
23731a1e1d21SSam Leffler 		 * asresp frame format
23741a1e1d21SSam Leffler 		 *	[2] capability information
23751a1e1d21SSam Leffler 		 *	[2] status
23761a1e1d21SSam Leffler 		 *	[2] association ID
23771a1e1d21SSam Leffler 		 *	[tlv] supported rates
23781a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
23798a1b9b6aSSam Leffler 		 *	[tlv] WME
23801a1e1d21SSam Leffler 		 */
23811a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
23821a1e1d21SSam Leffler 		ni = ic->ic_bss;
23838a1b9b6aSSam Leffler 		capinfo = le16toh(*(u_int16_t *)frm);
23841a1e1d21SSam Leffler 		frm += 2;
23851a1e1d21SSam Leffler 		status = le16toh(*(u_int16_t *)frm);
23861a1e1d21SSam Leffler 		frm += 2;
23871a1e1d21SSam Leffler 		if (status != 0) {
23888a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
23898a1b9b6aSSam Leffler 			    "[%s] %sassoc failed (reason %d)\n",
23908a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
23918a1b9b6aSSam Leffler 			    ISREASSOC(subtype) ?  "re" : "", status);
23928a1b9b6aSSam Leffler 			if (ni != ic->ic_bss)	/* XXX never true? */
23931a1e1d21SSam Leffler 				ni->ni_fails++;
23948a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_fail++;	/* XXX */
23951a1e1d21SSam Leffler 			return;
23961a1e1d21SSam Leffler 		}
23978a1b9b6aSSam Leffler 		associd = le16toh(*(u_int16_t *)frm);
23981a1e1d21SSam Leffler 		frm += 2;
23991a1e1d21SSam Leffler 
24008a1b9b6aSSam Leffler 		rates = xrates = wpa = wme = NULL;
24011a1e1d21SSam Leffler 		while (frm < efrm) {
24021a1e1d21SSam Leffler 			switch (*frm) {
24031a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
24041a1e1d21SSam Leffler 				rates = frm;
24051a1e1d21SSam Leffler 				break;
24061a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
24071a1e1d21SSam Leffler 				xrates = frm;
24081a1e1d21SSam Leffler 				break;
24098a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
24108a1b9b6aSSam Leffler 				if (iswmeoui(frm))
24118a1b9b6aSSam Leffler 					wme = frm;
24128a1b9b6aSSam Leffler 				/* XXX Atheros OUI support */
24138a1b9b6aSSam Leffler 				break;
24141a1e1d21SSam Leffler 			}
24151a1e1d21SSam Leffler 			frm += frm[1] + 2;
24161a1e1d21SSam Leffler 		}
24171a1e1d21SSam Leffler 
2418dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
241949d4c02fSSam Leffler 		rate = ieee80211_setup_rates(ic, ni, rates, xrates,
24201a1e1d21SSam Leffler 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
24211a1e1d21SSam Leffler 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
242249d4c02fSSam Leffler 		if (rate & IEEE80211_RATE_BASIC) {
24238a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
24248a1b9b6aSSam Leffler 			    "[%s] %sassoc failed (rate set mismatch)\n",
24258a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
24268a1b9b6aSSam Leffler 			    ISREASSOC(subtype) ?  "re" : "");
24278a1b9b6aSSam Leffler 			if (ni != ic->ic_bss)	/* XXX never true? */
24288a1b9b6aSSam Leffler 				ni->ni_fails++;
24298a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_assoc_norate++;
2430181181acSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
24318a1b9b6aSSam Leffler 			return;
24328a1b9b6aSSam Leffler 		}
24338a1b9b6aSSam Leffler 
24348a1b9b6aSSam Leffler 		ni->ni_capinfo = capinfo;
24358a1b9b6aSSam Leffler 		ni->ni_associd = associd;
2436c0fa32ceSSam Leffler 		if (wme != NULL &&
2437c0fa32ceSSam Leffler 		    ieee80211_parse_wmeparams(ic, wme, wh) >= 0) {
24388a1b9b6aSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
24398a1b9b6aSSam Leffler 			ieee80211_wme_updateparams(ic);
24408a1b9b6aSSam Leffler 		} else
24418a1b9b6aSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
24428a1b9b6aSSam Leffler 		/*
24438a1b9b6aSSam Leffler 		 * Configure state now that we are associated.
24448a1b9b6aSSam Leffler 		 *
24458a1b9b6aSSam Leffler 		 * XXX may need different/additional driver callbacks?
24468a1b9b6aSSam Leffler 		 */
24478a1b9b6aSSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11A ||
24488a1b9b6aSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
24498a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
24508a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
24518a1b9b6aSSam Leffler 		} else {
24528a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
24538a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
24548a1b9b6aSSam Leffler 		}
24558a1b9b6aSSam Leffler 		ieee80211_set_shortslottime(ic,
24568a1b9b6aSSam Leffler 			ic->ic_curmode == IEEE80211_MODE_11A ||
24578a1b9b6aSSam Leffler 			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
24588a1b9b6aSSam Leffler 		/*
24598a1b9b6aSSam Leffler 		 * Honor ERP protection.
24608a1b9b6aSSam Leffler 		 *
24618a1b9b6aSSam Leffler 		 * NB: ni_erp should zero for non-11g operation.
24628a1b9b6aSSam Leffler 		 * XXX check ic_curmode anyway?
24638a1b9b6aSSam Leffler 		 */
2464ec425115SSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G &&
2465ec425115SSam Leffler 		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
24668a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
24678a1b9b6aSSam Leffler 		else
24688a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
24698a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
24708a1b9b6aSSam Leffler 		    "[%s] %sassoc success: %s preamble, %s slot time%s%s\n",
24718a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr2),
24728a1b9b6aSSam Leffler 		    ISREASSOC(subtype) ? "re" : "",
24738a1b9b6aSSam Leffler 		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
24748a1b9b6aSSam Leffler 		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
24758a1b9b6aSSam Leffler 		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
24768a1b9b6aSSam Leffler 		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
24778a1b9b6aSSam Leffler 		);
24788a1b9b6aSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, subtype);
24791a1e1d21SSam Leffler 		break;
24801a1e1d21SSam Leffler 	}
24811a1e1d21SSam Leffler 
24821a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
24831a1e1d21SSam Leffler 		u_int16_t reason;
24848a1b9b6aSSam Leffler 
24858a1b9b6aSSam Leffler 		if (ic->ic_state == IEEE80211_S_SCAN) {
24868a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
24878a1b9b6aSSam Leffler 			return;
24888a1b9b6aSSam Leffler 		}
24891a1e1d21SSam Leffler 		/*
24901a1e1d21SSam Leffler 		 * deauth frame format
24911a1e1d21SSam Leffler 		 *	[2] reason
24921a1e1d21SSam Leffler 		 */
24931a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
24941a1e1d21SSam Leffler 		reason = le16toh(*(u_int16_t *)frm);
24951be50176SSam Leffler 		ic->ic_stats.is_rx_deauth++;
24968a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_deauth);
24974720ec19SSam Leffler 
24984720ec19SSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
24994720ec19SSam Leffler 		    "[%s] recv deauthenticate (reason %d)\n",
25004720ec19SSam Leffler 		    ether_sprintf(ni->ni_macaddr), reason);
25011a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
25021a1e1d21SSam Leffler 		case IEEE80211_M_STA:
2503a11c9a5cSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
25041a1e1d21SSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
25051a1e1d21SSam Leffler 			break;
25061a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
25074720ec19SSam Leffler 			if (ni != ic->ic_bss)
25088a1b9b6aSSam Leffler 				ieee80211_node_leave(ic, ni);
25091a1e1d21SSam Leffler 			break;
25101a1e1d21SSam Leffler 		default:
25118a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
25121a1e1d21SSam Leffler 			break;
25131a1e1d21SSam Leffler 		}
25141a1e1d21SSam Leffler 		break;
25151a1e1d21SSam Leffler 	}
25161a1e1d21SSam Leffler 
25171a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
25181a1e1d21SSam Leffler 		u_int16_t reason;
25198a1b9b6aSSam Leffler 
25208a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN &&
2521404265d4SSam Leffler 		    ic->ic_state != IEEE80211_S_ASSOC &&
25228a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_AUTH) {
25238a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
25248a1b9b6aSSam Leffler 			return;
25258a1b9b6aSSam Leffler 		}
25261a1e1d21SSam Leffler 		/*
25271a1e1d21SSam Leffler 		 * disassoc frame format
25281a1e1d21SSam Leffler 		 *	[2] reason
25291a1e1d21SSam Leffler 		 */
25301a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
25311a1e1d21SSam Leffler 		reason = le16toh(*(u_int16_t *)frm);
25321be50176SSam Leffler 		ic->ic_stats.is_rx_disassoc++;
25338a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_disassoc);
25344720ec19SSam Leffler 
25354720ec19SSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
25364720ec19SSam Leffler 		    "[%s] recv disassociated (reason %d)\n",
25374720ec19SSam Leffler 		    ether_sprintf(ni->ni_macaddr), reason);
25381a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
25391a1e1d21SSam Leffler 		case IEEE80211_M_STA:
2540a11c9a5cSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
25411a1e1d21SSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
25421a1e1d21SSam Leffler 			break;
25431a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
25444720ec19SSam Leffler 			if (ni != ic->ic_bss)
25458a1b9b6aSSam Leffler 				ieee80211_node_leave(ic, ni);
25461a1e1d21SSam Leffler 			break;
25471a1e1d21SSam Leffler 		default:
25488a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
25491a1e1d21SSam Leffler 			break;
25501a1e1d21SSam Leffler 		}
25511a1e1d21SSam Leffler 		break;
25521a1e1d21SSam Leffler 	}
25531a1e1d21SSam Leffler 	default:
25548a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
25558a1b9b6aSSam Leffler 		     wh, "mgt", "subtype 0x%x not handled", subtype);
25561be50176SSam Leffler 		ic->ic_stats.is_rx_badsubtype++;
25571a1e1d21SSam Leffler 		break;
25581a1e1d21SSam Leffler 	}
25598a1b9b6aSSam Leffler #undef ISREASSOC
25608a1b9b6aSSam Leffler #undef ISPROBE
25611a1e1d21SSam Leffler }
25621a1e1d21SSam Leffler #undef IEEE80211_VERIFY_LENGTH
25631a1e1d21SSam Leffler #undef IEEE80211_VERIFY_ELEMENT
25648a1b9b6aSSam Leffler 
25658a1b9b6aSSam Leffler /*
25668a1b9b6aSSam Leffler  * Handle station power-save state change.
25678a1b9b6aSSam Leffler  */
25688a1b9b6aSSam Leffler static void
25698a1b9b6aSSam Leffler ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
25708a1b9b6aSSam Leffler {
25718a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
25728a1b9b6aSSam Leffler 	struct mbuf *m;
25738a1b9b6aSSam Leffler 
25748a1b9b6aSSam Leffler 	if (enable) {
25758a1b9b6aSSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0)
25768a1b9b6aSSam Leffler 			ic->ic_ps_sta++;
25778a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
25788a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
25798a1b9b6aSSam Leffler 		    "[%s] power save mode on, %u sta's in ps mode\n",
25808a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
25818a1b9b6aSSam Leffler 		return;
25828a1b9b6aSSam Leffler 	}
25838a1b9b6aSSam Leffler 
25848a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT)
25858a1b9b6aSSam Leffler 		ic->ic_ps_sta--;
25868a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
25878a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
25888a1b9b6aSSam Leffler 	    "[%s] power save mode off, %u sta's in ps mode\n",
25898a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
25908a1b9b6aSSam Leffler 	/* XXX if no stations in ps mode, flush mc frames */
25918a1b9b6aSSam Leffler 
25928a1b9b6aSSam Leffler 	/*
25938a1b9b6aSSam Leffler 	 * Flush queued unicast frames.
25948a1b9b6aSSam Leffler 	 */
25958a1b9b6aSSam Leffler 	if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) {
2596c789ea8bSSam Leffler 		if (ic->ic_set_tim != NULL)
25978a1b9b6aSSam Leffler 			ic->ic_set_tim(ic, ni, 0);	/* just in case */
25988a1b9b6aSSam Leffler 		return;
25998a1b9b6aSSam Leffler 	}
26008a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
26018a1b9b6aSSam Leffler 	    "[%s] flush ps queue, %u packets queued\n",
26028a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_SAVEQ_QLEN(ni));
26038a1b9b6aSSam Leffler 	for (;;) {
26048a1b9b6aSSam Leffler 		int qlen;
26058a1b9b6aSSam Leffler 
26068a1b9b6aSSam Leffler 		IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen);
26078a1b9b6aSSam Leffler 		if (m == NULL)
26088a1b9b6aSSam Leffler 			break;
26098a1b9b6aSSam Leffler 		/*
26108a1b9b6aSSam Leffler 		 * If this is the last packet, turn off the TIM bit.
26118a1b9b6aSSam Leffler 		 * If there are more packets, set the more packets bit
2612bc5627d9SSam Leffler 		 * in the mbuf so ieee80211_encap will mark the 802.11
2613bc5627d9SSam Leffler 		 * head to indicate more data frames will follow.
26148a1b9b6aSSam Leffler 		 */
2615bc5627d9SSam Leffler 		if (qlen != 0)
2616bc5627d9SSam Leffler 			m->m_flags |= M_MORE_DATA;
26178a1b9b6aSSam Leffler 		/* XXX need different driver interface */
26188a1b9b6aSSam Leffler 		/* XXX bypasses q max */
26198a1b9b6aSSam Leffler 		IF_ENQUEUE(&ic->ic_ifp->if_snd, m);
26208a1b9b6aSSam Leffler 	}
2621c789ea8bSSam Leffler 	if (ic->ic_set_tim != NULL)
2622c789ea8bSSam Leffler 		ic->ic_set_tim(ic, ni, 0);
26238a1b9b6aSSam Leffler }
26248a1b9b6aSSam Leffler 
26258a1b9b6aSSam Leffler /*
26268a1b9b6aSSam Leffler  * Process a received ps-poll frame.
26278a1b9b6aSSam Leffler  */
26288a1b9b6aSSam Leffler static void
26298a1b9b6aSSam Leffler ieee80211_recv_pspoll(struct ieee80211com *ic,
26308a1b9b6aSSam Leffler 	struct ieee80211_node *ni, struct mbuf *m0)
26318a1b9b6aSSam Leffler {
26328a1b9b6aSSam Leffler 	struct ieee80211_frame_min *wh;
26338a1b9b6aSSam Leffler 	struct mbuf *m;
26348a1b9b6aSSam Leffler 	u_int16_t aid;
26358a1b9b6aSSam Leffler 	int qlen;
26368a1b9b6aSSam Leffler 
26378a1b9b6aSSam Leffler 	wh = mtod(m0, struct ieee80211_frame_min *);
26388a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
26398a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
26408a1b9b6aSSam Leffler 		    (struct ieee80211_frame *) wh, "ps-poll",
26418a1b9b6aSSam Leffler 		    "%s", "unassociated station");
26428a1b9b6aSSam Leffler 		ic->ic_stats.is_ps_unassoc++;
26438a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
26448a1b9b6aSSam Leffler 			IEEE80211_REASON_NOT_ASSOCED);
26458a1b9b6aSSam Leffler 		return;
26468a1b9b6aSSam Leffler 	}
26478a1b9b6aSSam Leffler 
26488a1b9b6aSSam Leffler 	aid = le16toh(*(u_int16_t *)wh->i_dur);
26498a1b9b6aSSam Leffler 	if (aid != ni->ni_associd) {
26508a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
26518a1b9b6aSSam Leffler 		    (struct ieee80211_frame *) wh, "ps-poll",
26528a1b9b6aSSam Leffler 		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
26538a1b9b6aSSam Leffler 		    ni->ni_associd, aid);
26548a1b9b6aSSam Leffler 		ic->ic_stats.is_ps_badaid++;
26558a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
26568a1b9b6aSSam Leffler 			IEEE80211_REASON_NOT_ASSOCED);
26578a1b9b6aSSam Leffler 		return;
26588a1b9b6aSSam Leffler 	}
26598a1b9b6aSSam Leffler 
26608a1b9b6aSSam Leffler 	/* Okay, take the first queued packet and put it out... */
26618a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen);
26628a1b9b6aSSam Leffler 	if (m == NULL) {
26638a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
26648a1b9b6aSSam Leffler 		    "[%s] recv ps-poll, but queue empty\n",
26658a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr2));
26668a1b9b6aSSam Leffler 		ieee80211_send_nulldata(ic, ni);
26678a1b9b6aSSam Leffler 		ic->ic_stats.is_ps_qempty++;	/* XXX node stat */
2668c789ea8bSSam Leffler 		if (ic->ic_set_tim != NULL)
26698a1b9b6aSSam Leffler 			ic->ic_set_tim(ic, ni, 0);	/* just in case */
26708a1b9b6aSSam Leffler 		return;
26718a1b9b6aSSam Leffler 	}
26728a1b9b6aSSam Leffler 	/*
26738a1b9b6aSSam Leffler 	 * If there are more packets, set the more packets bit
26748a1b9b6aSSam Leffler 	 * in the packet dispatched to the station; otherwise
26758a1b9b6aSSam Leffler 	 * turn off the TIM bit.
26768a1b9b6aSSam Leffler 	 */
26778a1b9b6aSSam Leffler 	if (qlen != 0) {
26788a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
26798a1b9b6aSSam Leffler 		    "[%s] recv ps-poll, send packet, %u still queued\n",
26808a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), qlen);
26818a1b9b6aSSam Leffler 		wh = mtod(m, struct ieee80211_frame_min *);
26828a1b9b6aSSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
26838a1b9b6aSSam Leffler 	} else {
26848a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
26858a1b9b6aSSam Leffler 		    "[%s] recv ps-poll, send packet, queue empty\n",
26868a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
2687c789ea8bSSam Leffler 		if (ic->ic_set_tim != NULL)
26888a1b9b6aSSam Leffler 			ic->ic_set_tim(ic, ni, 0);
26898a1b9b6aSSam Leffler 	}
26908a1b9b6aSSam Leffler 	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
26918a1b9b6aSSam Leffler 	IF_ENQUEUE(&ic->ic_ifp->if_snd, m);
26928a1b9b6aSSam Leffler }
26938a1b9b6aSSam Leffler 
26948a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
26958a1b9b6aSSam Leffler /*
26968a1b9b6aSSam Leffler  * Debugging support.
26978a1b9b6aSSam Leffler  */
26988a1b9b6aSSam Leffler 
26998a1b9b6aSSam Leffler /*
27008a1b9b6aSSam Leffler  * Return the bssid of a frame.
27018a1b9b6aSSam Leffler  */
27028a1b9b6aSSam Leffler static const u_int8_t *
27038a1b9b6aSSam Leffler ieee80211_getbssid(struct ieee80211com *ic, const struct ieee80211_frame *wh)
27048a1b9b6aSSam Leffler {
27058a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA)
27068a1b9b6aSSam Leffler 		return wh->i_addr2;
27078a1b9b6aSSam Leffler 	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) != IEEE80211_FC1_DIR_NODS)
27088a1b9b6aSSam Leffler 		return wh->i_addr1;
27098a1b9b6aSSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
27108a1b9b6aSSam Leffler 		return wh->i_addr1;
27118a1b9b6aSSam Leffler 	return wh->i_addr3;
27128a1b9b6aSSam Leffler }
27138a1b9b6aSSam Leffler 
27148a1b9b6aSSam Leffler static void
27158a1b9b6aSSam Leffler ieee80211_discard_frame(struct ieee80211com *ic,
27168a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
27178a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
27188a1b9b6aSSam Leffler {
27198a1b9b6aSSam Leffler 	va_list ap;
27208a1b9b6aSSam Leffler 
27218a1b9b6aSSam Leffler 	printf("[%s] discard ", ether_sprintf(ieee80211_getbssid(ic, wh)));
27228a1b9b6aSSam Leffler 	if (type != NULL)
27238a1b9b6aSSam Leffler 		printf(" %s frame, ", type);
27248a1b9b6aSSam Leffler 	else
27258a1b9b6aSSam Leffler 		printf(" frame, ");
27268a1b9b6aSSam Leffler 	va_start(ap, fmt);
27278a1b9b6aSSam Leffler 	vprintf(fmt, ap);
27288a1b9b6aSSam Leffler 	va_end(ap);
27298a1b9b6aSSam Leffler 	printf("\n");
27308a1b9b6aSSam Leffler }
27318a1b9b6aSSam Leffler 
27328a1b9b6aSSam Leffler static void
27338a1b9b6aSSam Leffler ieee80211_discard_ie(struct ieee80211com *ic,
27348a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
27358a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
27368a1b9b6aSSam Leffler {
27378a1b9b6aSSam Leffler 	va_list ap;
27388a1b9b6aSSam Leffler 
27398a1b9b6aSSam Leffler 	printf("[%s] discard ", ether_sprintf(ieee80211_getbssid(ic, wh)));
27408a1b9b6aSSam Leffler 	if (type != NULL)
27418a1b9b6aSSam Leffler 		printf(" %s information element, ", type);
27428a1b9b6aSSam Leffler 	else
27438a1b9b6aSSam Leffler 		printf(" information element, ");
27448a1b9b6aSSam Leffler 	va_start(ap, fmt);
27458a1b9b6aSSam Leffler 	vprintf(fmt, ap);
27468a1b9b6aSSam Leffler 	va_end(ap);
27478a1b9b6aSSam Leffler 	printf("\n");
27488a1b9b6aSSam Leffler }
27498a1b9b6aSSam Leffler 
27508a1b9b6aSSam Leffler static void
27518a1b9b6aSSam Leffler ieee80211_discard_mac(struct ieee80211com *ic,
27528a1b9b6aSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN],
27538a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
27548a1b9b6aSSam Leffler {
27558a1b9b6aSSam Leffler 	va_list ap;
27568a1b9b6aSSam Leffler 
27578a1b9b6aSSam Leffler 	printf("[%s] discard ", ether_sprintf(mac));
27588a1b9b6aSSam Leffler 	if (type != NULL)
27598a1b9b6aSSam Leffler 		printf(" %s frame, ", type);
27608a1b9b6aSSam Leffler 	else
27618a1b9b6aSSam Leffler 		printf(" frame, ");
27628a1b9b6aSSam Leffler 	va_start(ap, fmt);
27638a1b9b6aSSam Leffler 	vprintf(fmt, ap);
27648a1b9b6aSSam Leffler 	va_end(ap);
27658a1b9b6aSSam Leffler 	printf("\n");
27668a1b9b6aSSam Leffler }
27678a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2768