xref: /freebsd/sys/net80211/ieee80211_input.c (revision c0fa32ce0b22ee8730aa85c841474b83667a6130)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
38a1b9b6aSSam Leffler  * Copyright (c) 2002-2004 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>
411a1e1d21SSam Leffler 
428a1b9b6aSSam Leffler #include <sys/socket.h>
431a1e1d21SSam Leffler 
441a1e1d21SSam Leffler #include <net/if.h>
451a1e1d21SSam Leffler #include <net/if_media.h>
461a1e1d21SSam Leffler #include <net/ethernet.h>
471a1e1d21SSam Leffler #include <net/if_llc.h>
488a1b9b6aSSam Leffler #include <net/if_vlan_var.h>
491a1e1d21SSam Leffler 
501a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
511a1e1d21SSam Leffler 
521a1e1d21SSam Leffler #include <net/bpf.h>
531a1e1d21SSam Leffler 
548a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
558a1b9b6aSSam Leffler #include <machine/stdarg.h>
568a1b9b6aSSam Leffler 
578a1b9b6aSSam Leffler /*
588a1b9b6aSSam Leffler  * Decide if a received management frame should be
598a1b9b6aSSam Leffler  * printed when debugging is enabled.  This filters some
608a1b9b6aSSam Leffler  * of the less interesting frames that come frequently
618a1b9b6aSSam Leffler  * (e.g. beacons).
628a1b9b6aSSam Leffler  */
638a1b9b6aSSam Leffler static __inline int
648a1b9b6aSSam Leffler doprint(struct ieee80211com *ic, int subtype)
658a1b9b6aSSam Leffler {
668a1b9b6aSSam Leffler 	switch (subtype) {
678a1b9b6aSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
688a1b9b6aSSam Leffler 		return (ic->ic_flags & IEEE80211_F_SCAN);
698a1b9b6aSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
708a1b9b6aSSam Leffler 		return (ic->ic_opmode == IEEE80211_M_IBSS);
718a1b9b6aSSam Leffler 	}
728a1b9b6aSSam Leffler 	return 1;
738a1b9b6aSSam Leffler }
748a1b9b6aSSam Leffler 
758a1b9b6aSSam Leffler /*
768a1b9b6aSSam Leffler  * Emit a debug message about discarding a frame or information
778a1b9b6aSSam Leffler  * element.  One format is for extracting the mac address from
788a1b9b6aSSam Leffler  * the frame header; the other is for when a header is not
798a1b9b6aSSam Leffler  * available or otherwise appropriate.
808a1b9b6aSSam Leffler  */
818a1b9b6aSSam Leffler #define	IEEE80211_DISCARD(_ic, _m, _wh, _type, _fmt, ...) do {		\
828a1b9b6aSSam Leffler 	if ((_ic)->ic_debug & (_m))					\
838a1b9b6aSSam Leffler 		ieee80211_discard_frame(_ic, _wh, _type, _fmt, __VA_ARGS__);\
848a1b9b6aSSam Leffler } while (0)
858a1b9b6aSSam Leffler #define	IEEE80211_DISCARD_IE(_ic, _m, _wh, _type, _fmt, ...) do {	\
868a1b9b6aSSam Leffler 	if ((_ic)->ic_debug & (_m))					\
878a1b9b6aSSam Leffler 		ieee80211_discard_ie(_ic, _wh, _type, _fmt, __VA_ARGS__);\
888a1b9b6aSSam Leffler } while (0)
898a1b9b6aSSam Leffler #define	IEEE80211_DISCARD_MAC(_ic, _m, _mac, _type, _fmt, ...) do {	\
908a1b9b6aSSam Leffler 	if ((_ic)->ic_debug & (_m))					\
918a1b9b6aSSam Leffler 		ieee80211_discard_mac(_ic, _mac, _type, _fmt, __VA_ARGS__);\
928a1b9b6aSSam Leffler } while (0)
938a1b9b6aSSam Leffler 
948a1b9b6aSSam Leffler static const u_int8_t *ieee80211_getbssid(struct ieee80211com *,
958a1b9b6aSSam Leffler 	const struct ieee80211_frame *);
968a1b9b6aSSam Leffler static void ieee80211_discard_frame(struct ieee80211com *,
978a1b9b6aSSam Leffler 	const struct ieee80211_frame *, const char *type, const char *fmt, ...);
988a1b9b6aSSam Leffler static void ieee80211_discard_ie(struct ieee80211com *,
998a1b9b6aSSam Leffler 	const struct ieee80211_frame *, const char *type, const char *fmt, ...);
1008a1b9b6aSSam Leffler static void ieee80211_discard_mac(struct ieee80211com *,
1018a1b9b6aSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN], const char *type,
1028a1b9b6aSSam Leffler 	const char *fmt, ...);
1038a1b9b6aSSam Leffler #else
1048a1b9b6aSSam Leffler #define	IEEE80211_DISCARD(_ic, _m, _wh, _type, _fmt, ...)
1058a1b9b6aSSam Leffler #define	IEEE80211_DISCARD_IE(_ic, _m, _wh, _type, _fmt, ...)
1068a1b9b6aSSam Leffler #define	IEEE80211_DISCARD_MAC(_ic, _m, _mac, _type, _fmt, ...)
1078a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
1088a1b9b6aSSam Leffler 
1098a1b9b6aSSam Leffler static struct mbuf *ieee80211_defrag(struct ieee80211com *,
1108a1b9b6aSSam Leffler 	struct ieee80211_node *, struct mbuf *);
1118a1b9b6aSSam Leffler static struct mbuf *ieee80211_decap(struct ieee80211com *, struct mbuf *);
1128a1b9b6aSSam Leffler static void ieee80211_node_pwrsave(struct ieee80211_node *, int enable);
1138a1b9b6aSSam Leffler static void ieee80211_recv_pspoll(struct ieee80211com *,
1148a1b9b6aSSam Leffler 	struct ieee80211_node *, struct mbuf *);
1151a1e1d21SSam Leffler 
1160a915fadSSam Leffler /*
1170a915fadSSam Leffler  * Process a received frame.  The node associated with the sender
1180a915fadSSam Leffler  * should be supplied.  If nothing was found in the node table then
1190a915fadSSam Leffler  * the caller is assumed to supply a reference to ic_bss instead.
1200a915fadSSam Leffler  * The RSSI and a timestamp are also supplied.  The RSSI data is used
1210a915fadSSam Leffler  * during AP scanning to select a AP to associate with; it can have
1220a915fadSSam Leffler  * any units so long as values have consistent units and higher values
1230a915fadSSam Leffler  * mean ``better signal''.  The receive timestamp is currently not used
1240a915fadSSam Leffler  * by the 802.11 layer.
1250a915fadSSam Leffler  */
1261a1e1d21SSam Leffler void
1278a1b9b6aSSam Leffler ieee80211_input(struct ieee80211com *ic, struct mbuf *m,
1288a1b9b6aSSam Leffler 	struct ieee80211_node *ni, int rssi, u_int32_t rstamp)
1291a1e1d21SSam Leffler {
1308a1b9b6aSSam Leffler #define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
1318a1b9b6aSSam Leffler #define	HAS_SEQ(type)	((type & 0x4) == 0)
1328a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1331a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
1348a1b9b6aSSam Leffler 	struct ieee80211_key *key;
1351a1e1d21SSam Leffler 	struct ether_header *eh;
1368a1b9b6aSSam Leffler 	int len, hdrsize, off;
13718f897abSSam Leffler 	u_int8_t dir, type, subtype;
1381a1e1d21SSam Leffler 	u_int8_t *bssid;
1391a1e1d21SSam Leffler 	u_int16_t rxseq;
1401a1e1d21SSam Leffler 
1410a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
1428a1b9b6aSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
1430a915fadSSam Leffler 
14418f897abSSam Leffler 	/* trim CRC here so WEP can find its own CRC at the end of packet. */
1451a1e1d21SSam Leffler 	if (m->m_flags & M_HASFCS) {
1461a1e1d21SSam Leffler 		m_adj(m, -IEEE80211_CRC_LEN);
1471a1e1d21SSam Leffler 		m->m_flags &= ~M_HASFCS;
1481a1e1d21SSam Leffler 	}
14918f897abSSam Leffler 	KASSERT(m->m_pkthdr.len >= sizeof(struct ieee80211_frame_min),
15018f897abSSam Leffler 		("frame length too short: %u", m->m_pkthdr.len));
15118f897abSSam Leffler 
15218f897abSSam Leffler 	/*
15318f897abSSam Leffler 	 * In monitor mode, send everything directly to bpf.
15418f897abSSam Leffler 	 * XXX may want to include the CRC
15518f897abSSam Leffler 	 */
15618f897abSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
15718f897abSSam Leffler 		goto out;
1581a1e1d21SSam Leffler 
1598a1b9b6aSSam Leffler 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1608a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
1618a1b9b6aSSam Leffler 		    ni->ni_macaddr, NULL,
1628a1b9b6aSSam Leffler 		    "too short (1): len %u", m->m_pkthdr.len);
1638a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_tooshort++;
1648a1b9b6aSSam Leffler 		goto out;
1658a1b9b6aSSam Leffler 	}
1668a1b9b6aSSam Leffler 	/*
1678a1b9b6aSSam Leffler 	 * Bit of a cheat here, we use a pointer for a 3-address
1688a1b9b6aSSam Leffler 	 * frame format but don't reference fields past outside
1698a1b9b6aSSam Leffler 	 * ieee80211_frame_min w/o first validating the data is
1708a1b9b6aSSam Leffler 	 * present.
1718a1b9b6aSSam Leffler 	 */
1721a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
1738a1b9b6aSSam Leffler 
1741a1e1d21SSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1751a1e1d21SSam Leffler 	    IEEE80211_FC0_VERSION_0) {
1768a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
1778a1b9b6aSSam Leffler 		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1781be50176SSam Leffler 		ic->ic_stats.is_rx_badversion++;
1791a1e1d21SSam Leffler 		goto err;
1801a1e1d21SSam Leffler 	}
1811a1e1d21SSam Leffler 
1821a1e1d21SSam Leffler 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
18318f897abSSam Leffler 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1848a1b9b6aSSam Leffler 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1858a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1861a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
1871a1e1d21SSam Leffler 		case IEEE80211_M_STA:
1888a1b9b6aSSam Leffler 			bssid = wh->i_addr2;
1898a1b9b6aSSam Leffler 			if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
1901be50176SSam Leffler 				/* not interested in */
1918a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
1928a1b9b6aSSam Leffler 				    bssid, NULL, "%s", "not to bss");
1931be50176SSam Leffler 				ic->ic_stats.is_rx_wrongbss++;
1941a1e1d21SSam Leffler 				goto out;
1951a1e1d21SSam Leffler 			}
1961a1e1d21SSam Leffler 			break;
1971a1e1d21SSam Leffler 		case IEEE80211_M_IBSS:
1981a1e1d21SSam Leffler 		case IEEE80211_M_AHDEMO:
1991a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
2008a1b9b6aSSam Leffler 			if (dir != IEEE80211_FC1_DIR_NODS)
2011a1e1d21SSam Leffler 				bssid = wh->i_addr1;
2028a1b9b6aSSam Leffler 			else if (type == IEEE80211_FC0_TYPE_CTL)
2038a1b9b6aSSam Leffler 				bssid = wh->i_addr1;
2048a1b9b6aSSam Leffler 			else {
2058a1b9b6aSSam Leffler 				if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
2068a1b9b6aSSam Leffler 					IEEE80211_DISCARD_MAC(ic,
2078a1b9b6aSSam Leffler 					    IEEE80211_MSG_ANY, ni->ni_macaddr,
2088a1b9b6aSSam Leffler 					    NULL, "too short (2): len %u",
2098a1b9b6aSSam Leffler 					    m->m_pkthdr.len);
2108a1b9b6aSSam Leffler 					ic->ic_stats.is_rx_tooshort++;
2118a1b9b6aSSam Leffler 					goto out;
2128a1b9b6aSSam Leffler 				}
2138a1b9b6aSSam Leffler 				bssid = wh->i_addr3;
2148a1b9b6aSSam Leffler 			}
2158a1b9b6aSSam Leffler 			if (type != IEEE80211_FC0_TYPE_DATA)
2168a1b9b6aSSam Leffler 				break;
2178a1b9b6aSSam Leffler 			/*
2188a1b9b6aSSam Leffler 			 * Data frame, validate the bssid.
2198a1b9b6aSSam Leffler 			 */
2201a1e1d21SSam Leffler 			if (!IEEE80211_ADDR_EQ(bssid, ic->ic_bss->ni_bssid) &&
2211a1e1d21SSam Leffler 			    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
2221a1e1d21SSam Leffler 				/* not interested in */
2238a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
2248a1b9b6aSSam Leffler 				    bssid, NULL, "%s", "not to bss");
2251be50176SSam Leffler 				ic->ic_stats.is_rx_wrongbss++;
2261a1e1d21SSam Leffler 				goto out;
2271a1e1d21SSam Leffler 			}
2288a1b9b6aSSam Leffler 			/*
2298a1b9b6aSSam Leffler 			 * For adhoc mode we cons up a node when it doesn't
2308a1b9b6aSSam Leffler 			 * exist. This should probably done after an ACL check.
2318a1b9b6aSSam Leffler 			 */
2328a1b9b6aSSam Leffler 			if (ni == ic->ic_bss &&
2338a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_HOSTAP) {
2348a1b9b6aSSam Leffler 				/*
2358a1b9b6aSSam Leffler 				 * Fake up a node for this newly
2368a1b9b6aSSam Leffler 				 * discovered member of the IBSS.
2378a1b9b6aSSam Leffler 				 */
2388a1b9b6aSSam Leffler 				ni = ieee80211_fakeup_adhoc_node(ic->ic_sta,
2398a1b9b6aSSam Leffler 						type == IEEE80211_FC0_TYPE_CTL ?
2408a1b9b6aSSam Leffler 						    wh->i_addr1 : wh->i_addr2);
2418a1b9b6aSSam Leffler 				if (ni == NULL) {
2428a1b9b6aSSam Leffler 					/* NB: stat kept for alloc failure */
2438a1b9b6aSSam Leffler 					goto err;
2448a1b9b6aSSam Leffler 				}
2458a1b9b6aSSam Leffler 			}
2461a1e1d21SSam Leffler 			break;
2471a1e1d21SSam Leffler 		default:
2488a1b9b6aSSam Leffler 			goto out;
2491a1e1d21SSam Leffler 		}
2501a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
2511a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
2528a1b9b6aSSam Leffler 		if (HAS_SEQ(type)) {
2538a1b9b6aSSam Leffler 			u_int8_t tid;
2548a1b9b6aSSam Leffler 			if (IEEE80211_QOS_HAS_SEQ(wh)) {
2558a1b9b6aSSam Leffler 				tid = ((struct ieee80211_qosframe *)wh)->
2568a1b9b6aSSam Leffler 					i_qos[0] & IEEE80211_QOS_TID;
2578a1b9b6aSSam Leffler 				if (tid >= WME_AC_VI)
2588a1b9b6aSSam Leffler 					ic->ic_wme.wme_hipri_traffic++;
2598a1b9b6aSSam Leffler 				tid++;
2608a1b9b6aSSam Leffler 			} else
2618a1b9b6aSSam Leffler 				tid = 0;
2628a1b9b6aSSam Leffler 			rxseq = le16toh(*(u_int16_t *)wh->i_seq);
2631a1e1d21SSam Leffler 			if ((wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
2648a1b9b6aSSam Leffler 			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
2658a1b9b6aSSam Leffler 				/* duplicate, discard */
2668a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
2678a1b9b6aSSam Leffler 				    bssid, "duplicate",
2688a1b9b6aSSam Leffler 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
2698a1b9b6aSSam Leffler 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
2708a1b9b6aSSam Leffler 				    ni->ni_rxseqs[tid] >>
2718a1b9b6aSSam Leffler 					IEEE80211_SEQ_SEQ_SHIFT,
2728a1b9b6aSSam Leffler 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
2738a1b9b6aSSam Leffler 				    ni->ni_rxseqs[tid] &
2748a1b9b6aSSam Leffler 					IEEE80211_SEQ_FRAG_MASK,
2758a1b9b6aSSam Leffler 				    tid);
2768a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_dup++;
2778a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_dup);
2781a1e1d21SSam Leffler 				goto out;
2791a1e1d21SSam Leffler 			}
2808a1b9b6aSSam Leffler 			ni->ni_rxseqs[tid] = rxseq;
2818a1b9b6aSSam Leffler 		}
2821a1e1d21SSam Leffler 	}
2831a1e1d21SSam Leffler 
28418f897abSSam Leffler 	switch (type) {
2851a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
2868a1b9b6aSSam Leffler 		hdrsize = ieee80211_hdrsize(wh);
2878a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_DATAPAD)
2888a1b9b6aSSam Leffler 			hdrsize = roundup(hdrsize, sizeof(u_int32_t));
2898a1b9b6aSSam Leffler 		if (m->m_len < hdrsize &&
2908a1b9b6aSSam Leffler 		    (m = m_pullup(m, hdrsize)) == NULL) {
2918a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
2928a1b9b6aSSam Leffler 			    wh, "data", "too short: len %u, expecting %u",
2938a1b9b6aSSam Leffler 			    m->m_pkthdr.len, hdrsize);
2948a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_tooshort++;
2958a1b9b6aSSam Leffler 			goto out;		/* XXX */
2968a1b9b6aSSam Leffler 		}
2978a1b9b6aSSam Leffler 		if (subtype & IEEE80211_FC0_SUBTYPE_QOS) {
2988a1b9b6aSSam Leffler 			/* XXX discard if node w/o IEEE80211_NODE_QOS? */
2998a1b9b6aSSam Leffler 			/*
3008a1b9b6aSSam Leffler 			 * Strip QoS control and any padding so only a
3018a1b9b6aSSam Leffler 			 * stock 802.11 header is at the front.
3028a1b9b6aSSam Leffler 			 */
3038a1b9b6aSSam Leffler 			/* XXX 4-address QoS frame */
3048a1b9b6aSSam Leffler 			off = hdrsize - sizeof(struct ieee80211_frame);
3058a1b9b6aSSam Leffler 			ovbcopy(mtod(m, u_int8_t *), mtod(m, u_int8_t *) + off,
3068a1b9b6aSSam Leffler 				hdrsize - off);
3078a1b9b6aSSam Leffler 			m_adj(m, off);
3088a1b9b6aSSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
3098a1b9b6aSSam Leffler 			wh->i_fc[0] &= ~IEEE80211_FC0_SUBTYPE_QOS;
3108a1b9b6aSSam Leffler 		} else {
3118a1b9b6aSSam Leffler 			/* XXX copy up for 4-address frames w/ padding */
3128a1b9b6aSSam Leffler 		}
3131a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
3141a1e1d21SSam Leffler 		case IEEE80211_M_STA:
3151be50176SSam Leffler 			if (dir != IEEE80211_FC1_DIR_FROMDS) {
3161be50176SSam Leffler 				ic->ic_stats.is_rx_wrongdir++;
3171a1e1d21SSam Leffler 				goto out;
3181be50176SSam Leffler 			}
3191a1e1d21SSam Leffler 			if ((ifp->if_flags & IFF_SIMPLEX) &&
3201a1e1d21SSam Leffler 			    IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3211a1e1d21SSam Leffler 			    IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_myaddr)) {
3221a1e1d21SSam Leffler 				/*
3231a1e1d21SSam Leffler 				 * In IEEE802.11 network, multicast packet
3241a1e1d21SSam Leffler 				 * sent from me is broadcasted from AP.
3251a1e1d21SSam Leffler 				 * It should be silently discarded for
3261a1e1d21SSam Leffler 				 * SIMPLEX interface.
3271a1e1d21SSam Leffler 				 */
3288a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3298a1b9b6aSSam Leffler 				    wh, NULL, "%s", "multicast echo");
3301be50176SSam Leffler 				ic->ic_stats.is_rx_mcastecho++;
3311a1e1d21SSam Leffler 				goto out;
3321a1e1d21SSam Leffler 			}
3331a1e1d21SSam Leffler 			break;
3341a1e1d21SSam Leffler 		case IEEE80211_M_IBSS:
3351a1e1d21SSam Leffler 		case IEEE80211_M_AHDEMO:
3361be50176SSam Leffler 			if (dir != IEEE80211_FC1_DIR_NODS) {
3371be50176SSam Leffler 				ic->ic_stats.is_rx_wrongdir++;
3381a1e1d21SSam Leffler 				goto out;
3391be50176SSam Leffler 			}
3408a1b9b6aSSam Leffler 			/* XXX no power-save support */
3411a1e1d21SSam Leffler 			break;
3421a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
3431be50176SSam Leffler 			if (dir != IEEE80211_FC1_DIR_TODS) {
3441be50176SSam Leffler 				ic->ic_stats.is_rx_wrongdir++;
3451a1e1d21SSam Leffler 				goto out;
3461be50176SSam Leffler 			}
3471a1e1d21SSam Leffler 			/* check if source STA is associated */
3480a915fadSSam Leffler 			if (ni == ic->ic_bss) {
3498a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3508a1b9b6aSSam Leffler 				    wh, "data", "%s", "unknown src");
3510a915fadSSam Leffler 				/* NB: caller deals with reference */
3528a1b9b6aSSam Leffler 				ni = ieee80211_dup_bss(ic->ic_sta, wh->i_addr2);
3531a1e1d21SSam Leffler 				if (ni != NULL) {
3541a1e1d21SSam Leffler 					IEEE80211_SEND_MGMT(ic, ni,
3551a1e1d21SSam Leffler 					    IEEE80211_FC0_SUBTYPE_DEAUTH,
3561a1e1d21SSam Leffler 					    IEEE80211_REASON_NOT_AUTHED);
3578a1b9b6aSSam Leffler 					ieee80211_free_node(ni);
3581a1e1d21SSam Leffler 				}
3591be50176SSam Leffler 				ic->ic_stats.is_rx_notassoc++;
3601a1e1d21SSam Leffler 				goto err;
3611a1e1d21SSam Leffler 			}
3621a1e1d21SSam Leffler 			if (ni->ni_associd == 0) {
3638a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3648a1b9b6aSSam Leffler 				    wh, "data", "%s", "unassoc src");
3651a1e1d21SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
3661a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
3671a1e1d21SSam Leffler 				    IEEE80211_REASON_NOT_ASSOCED);
3681be50176SSam Leffler 				ic->ic_stats.is_rx_notassoc++;
3691a1e1d21SSam Leffler 				goto err;
3701a1e1d21SSam Leffler 			}
3718a1b9b6aSSam Leffler 
3728a1b9b6aSSam Leffler 			/*
3738a1b9b6aSSam Leffler 			 * Check for power save state change.
3748a1b9b6aSSam Leffler 			 */
3758a1b9b6aSSam Leffler 			if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
3768a1b9b6aSSam Leffler 			    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
3778a1b9b6aSSam Leffler 				ieee80211_node_pwrsave(ni,
3788a1b9b6aSSam Leffler 					wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
3791a1e1d21SSam Leffler 			break;
3808a1b9b6aSSam Leffler 		default:
3818a1b9b6aSSam Leffler 			/* XXX here to keep compiler happy */
3828a1b9b6aSSam Leffler 			goto out;
3831a1e1d21SSam Leffler 		}
3848a1b9b6aSSam Leffler 
3858a1b9b6aSSam Leffler 		/*
3868a1b9b6aSSam Leffler 		 * Handle privacy requirements.  Note that we
3878a1b9b6aSSam Leffler 		 * must not be preempted from here until after
3888a1b9b6aSSam Leffler 		 * we (potentially) call ieee80211_crypto_demic;
3898a1b9b6aSSam Leffler 		 * otherwise we may violate assumptions in the
3908a1b9b6aSSam Leffler 		 * crypto cipher modules used to do delayed update
3918a1b9b6aSSam Leffler 		 * of replay sequence numbers.
3928a1b9b6aSSam Leffler 		 */
3931a1e1d21SSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
3948a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
3958a1b9b6aSSam Leffler 				/*
3968a1b9b6aSSam Leffler 				 * Discard encrypted frames when privacy is off.
3978a1b9b6aSSam Leffler 				 */
3988a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3998a1b9b6aSSam Leffler 				    wh, "WEP", "%s", "PRIVACY off");
4008a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_noprivacy++;
4018a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
4028a1b9b6aSSam Leffler 				goto out;
4038a1b9b6aSSam Leffler 			}
4048a1b9b6aSSam Leffler 			key = ieee80211_crypto_decap(ic, ni, m);
4058a1b9b6aSSam Leffler 			if (key == NULL) {
4068a1b9b6aSSam Leffler 				/* NB: stats+msgs handled in crypto_decap */
4078a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_wepfail);
4088a1b9b6aSSam Leffler 				goto out;
4091be50176SSam Leffler 			}
4101a1e1d21SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
4118116d318SSam Leffler 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4121be50176SSam Leffler 		} else {
4138a1b9b6aSSam Leffler 			key = NULL;
4148a1b9b6aSSam Leffler 		}
4158a1b9b6aSSam Leffler 
4168a1b9b6aSSam Leffler 		/*
4178a1b9b6aSSam Leffler 		 * Next up, any fragmentation.
4188a1b9b6aSSam Leffler 		 */
4198a1b9b6aSSam Leffler 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
4208a1b9b6aSSam Leffler 			m = ieee80211_defrag(ic, ni, m);
4218a1b9b6aSSam Leffler 			if (m == NULL) {
4228a1b9b6aSSam Leffler 				/* Fragment dropped or frame not complete yet */
4231a1e1d21SSam Leffler 				goto out;
4241a1e1d21SSam Leffler 			}
4251be50176SSam Leffler 		}
4268a1b9b6aSSam Leffler 		wh = NULL;		/* no longer valid, catch any uses */
4278a1b9b6aSSam Leffler 
4288a1b9b6aSSam Leffler 		/*
4298a1b9b6aSSam Leffler 		 * Next strip any MSDU crypto bits.
4308a1b9b6aSSam Leffler 		 */
4318a1b9b6aSSam Leffler 		if (key != NULL && !ieee80211_crypto_demic(ic, key, m)) {
4328a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
4338a1b9b6aSSam Leffler 			    ni->ni_macaddr, "data", "%s", "demic error");
4348a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_demicfail);
4358a1b9b6aSSam Leffler 			goto out;
4368a1b9b6aSSam Leffler 		}
4378a1b9b6aSSam Leffler 
4381a1e1d21SSam Leffler 		/* copy to listener after decrypt */
4391a1e1d21SSam Leffler 		if (ic->ic_rawbpf)
4401a1e1d21SSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
4418a1b9b6aSSam Leffler 
4428a1b9b6aSSam Leffler 		/*
4438a1b9b6aSSam Leffler 		 * Finally, strip the 802.11 header.
4448a1b9b6aSSam Leffler 		 */
4458a1b9b6aSSam Leffler 		m = ieee80211_decap(ic, m);
4461be50176SSam Leffler 		if (m == NULL) {
4478a1b9b6aSSam Leffler 			/* don't count Null data frames as errors */
4488a1b9b6aSSam Leffler 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA)
4498a1b9b6aSSam Leffler 				goto out;
4508a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
4518a1b9b6aSSam Leffler 			    ni->ni_macaddr, "data", "%s", "decap error");
4521be50176SSam Leffler 			ic->ic_stats.is_rx_decap++;
4538a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_decap);
4541a1e1d21SSam Leffler 			goto err;
4551be50176SSam Leffler 		}
4568a1b9b6aSSam Leffler 		eh = mtod(m, struct ether_header *);
4578a1b9b6aSSam Leffler 		if (!ieee80211_node_is_authorized(ni)) {
4588a1b9b6aSSam Leffler 			/*
4598a1b9b6aSSam Leffler 			 * Deny any non-PAE frames received prior to
4608a1b9b6aSSam Leffler 			 * authorization.  For open/shared-key
4618a1b9b6aSSam Leffler 			 * authentication the port is mark authorized
4628a1b9b6aSSam Leffler 			 * after authentication completes.  For 802.1x
4638a1b9b6aSSam Leffler 			 * the port is not marked authorized by the
4648a1b9b6aSSam Leffler 			 * authenticator until the handshake has completed.
4658a1b9b6aSSam Leffler 			 */
4668a1b9b6aSSam Leffler 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
4678a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
4688a1b9b6aSSam Leffler 				    eh->ether_shost, "data",
4698a1b9b6aSSam Leffler 				    "unauthorized port: ether type 0x%x len %u",
4708a1b9b6aSSam Leffler 				    eh->ether_type, m->m_pkthdr.len);
4718a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_unauth++;
4728a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unauth);
4738a1b9b6aSSam Leffler 				goto err;
4748a1b9b6aSSam Leffler 			}
4758a1b9b6aSSam Leffler 		} else {
4768a1b9b6aSSam Leffler 			/*
4778a1b9b6aSSam Leffler 			 * When denying unencrypted frames, discard
4788a1b9b6aSSam Leffler 			 * any non-PAE frames received without encryption.
4798a1b9b6aSSam Leffler 			 */
4808a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_DROPUNENC) &&
4818a1b9b6aSSam Leffler 			    key == NULL &&
4828a1b9b6aSSam Leffler 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
4838a1b9b6aSSam Leffler 				/*
4848a1b9b6aSSam Leffler 				 * Drop unencrypted frames.
4858a1b9b6aSSam Leffler 				 */
4868a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_unencrypted++;
4878a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
4888a1b9b6aSSam Leffler 				goto out;
4898a1b9b6aSSam Leffler 			}
4908a1b9b6aSSam Leffler 		}
4911a1e1d21SSam Leffler 		ifp->if_ipackets++;
4928a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_data);
4938a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
4941a1e1d21SSam Leffler 
4951a1e1d21SSam Leffler 		/* perform as a bridge within the AP */
4968a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
4978a1b9b6aSSam Leffler 		    (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0) {
4988a1b9b6aSSam Leffler 			struct mbuf *m1 = NULL;
4998a1b9b6aSSam Leffler 
5001a1e1d21SSam Leffler 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
5010a915fadSSam Leffler 				m1 = m_copypacket(m, M_DONTWAIT);
5021a1e1d21SSam Leffler 				if (m1 == NULL)
5031a1e1d21SSam Leffler 					ifp->if_oerrors++;
5041a1e1d21SSam Leffler 				else
5051a1e1d21SSam Leffler 					m1->m_flags |= M_MCAST;
5061a1e1d21SSam Leffler 			} else {
5078a1b9b6aSSam Leffler 				/* XXX this dups work done in ieee80211_encap */
5088a1b9b6aSSam Leffler 				/* check if destination is associated */
5098a1b9b6aSSam Leffler 				struct ieee80211_node *ni1 =
5108a1b9b6aSSam Leffler 				    ieee80211_find_node(ic->ic_sta,
5118a1b9b6aSSam Leffler 							eh->ether_dhost);
5128a1b9b6aSSam Leffler 				if (ni1 != NULL) {
5138a1b9b6aSSam Leffler 					/* XXX check if authorized */
5148a1b9b6aSSam Leffler 					if (ni1->ni_associd != 0) {
5151a1e1d21SSam Leffler 						m1 = m;
5161a1e1d21SSam Leffler 						m = NULL;
5171a1e1d21SSam Leffler 					}
5188a1b9b6aSSam Leffler 					/* XXX statistic? */
5198a1b9b6aSSam Leffler 					ieee80211_free_node(ni1);
5201a1e1d21SSam Leffler 				}
5211a1e1d21SSam Leffler 			}
5221a1e1d21SSam Leffler 			if (m1 != NULL) {
5231a1e1d21SSam Leffler 				len = m1->m_pkthdr.len;
5241a1e1d21SSam Leffler 				IF_ENQUEUE(&ifp->if_snd, m1);
5251a1e1d21SSam Leffler 				if (m != NULL)
5261a1e1d21SSam Leffler 					ifp->if_omcasts++;
5271a1e1d21SSam Leffler 				ifp->if_obytes += len;
5281a1e1d21SSam Leffler 			}
5291a1e1d21SSam Leffler 		}
5308a1b9b6aSSam Leffler 		if (m != NULL) {
5318a1b9b6aSSam Leffler 			if (ni->ni_vlan != 0) {
5328a1b9b6aSSam Leffler 				/* attach vlan tag */
5338a1b9b6aSSam Leffler 				/* XXX goto err? */
5348a1b9b6aSSam Leffler 				VLAN_INPUT_TAG(ifp, m, ni->ni_vlan, goto out);
5358a1b9b6aSSam Leffler 			}
5361a1e1d21SSam Leffler 			(*ifp->if_input)(ifp, m);
5378a1b9b6aSSam Leffler 		}
5381a1e1d21SSam Leffler 		return;
5391a1e1d21SSam Leffler 
5401a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
5418a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_mgmt);
5421be50176SSam Leffler 		if (dir != IEEE80211_FC1_DIR_NODS) {
5431be50176SSam Leffler 			ic->ic_stats.is_rx_wrongdir++;
5441a1e1d21SSam Leffler 			goto err;
5451be50176SSam Leffler 		}
5468a1b9b6aSSam Leffler 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
5478a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
5488a1b9b6aSSam Leffler 			    ni->ni_macaddr, "mgt", "too short: len %u",
5498a1b9b6aSSam Leffler 			    m->m_pkthdr.len);
5508a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_tooshort++;
5511a1e1d21SSam Leffler 			goto out;
5521be50176SSam Leffler 		}
5531a1e1d21SSam Leffler #ifdef IEEE80211_DEBUG
5548a1b9b6aSSam Leffler 		if ((ieee80211_msg_debug(ic) && doprint(ic, subtype)) ||
5558a1b9b6aSSam Leffler 		    ieee80211_msg_dumppkts(ic)) {
5568a1b9b6aSSam Leffler 			if_printf(ic->ic_ifp, "received %s from %s rssi %d\n",
5578a1b9b6aSSam Leffler 			    ieee80211_mgt_subtype_name[subtype >>
5588a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
5591a1e1d21SSam Leffler 			    ether_sprintf(wh->i_addr2), rssi);
5601a1e1d21SSam Leffler 		}
5618a1b9b6aSSam Leffler #endif
5628a1b9b6aSSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
5638a1b9b6aSSam Leffler 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
5648a1b9b6aSSam Leffler 				/*
5658a1b9b6aSSam Leffler 				 * Only shared key auth frames with a challenge
5668a1b9b6aSSam Leffler 				 * should be encrypted, discard all others.
5678a1b9b6aSSam Leffler 				 */
5688a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
5698a1b9b6aSSam Leffler 				    wh, ieee80211_mgt_subtype_name[subtype >>
5708a1b9b6aSSam Leffler 					IEEE80211_FC0_SUBTYPE_SHIFT],
5718a1b9b6aSSam Leffler 				    "%s", "WEP set but not permitted");
5728a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_mgtdiscard++; /* XXX */
5738a1b9b6aSSam Leffler 				goto out;
5748a1b9b6aSSam Leffler 			}
5758a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
5768a1b9b6aSSam Leffler 				/*
5778a1b9b6aSSam Leffler 				 * Discard encrypted frames when privacy is off.
5788a1b9b6aSSam Leffler 				 */
5798a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
5808a1b9b6aSSam Leffler 				    wh, "mgt", "%s", "WEP set but PRIVACY off");
5818a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_noprivacy++;
5828a1b9b6aSSam Leffler 				goto out;
5838a1b9b6aSSam Leffler 			}
5848a1b9b6aSSam Leffler 			key = ieee80211_crypto_decap(ic, ni, m);
5858a1b9b6aSSam Leffler 			if (key == NULL) {
5868a1b9b6aSSam Leffler 				/* NB: stats+msgs handled in crypto_decap */
5878a1b9b6aSSam Leffler 				goto out;
5888a1b9b6aSSam Leffler 			}
5898116d318SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
5908116d318SSam Leffler 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
5918a1b9b6aSSam Leffler 		}
5921a1e1d21SSam Leffler 		if (ic->ic_rawbpf)
5931a1e1d21SSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
5940a915fadSSam Leffler 		(*ic->ic_recv_mgmt)(ic, m, ni, subtype, rssi, rstamp);
5951a1e1d21SSam Leffler 		m_freem(m);
5961a1e1d21SSam Leffler 		return;
5971a1e1d21SSam Leffler 
5981a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_CTL:
5998a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_ctrl);
6001be50176SSam Leffler 		ic->ic_stats.is_rx_ctl++;
6018a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
6028a1b9b6aSSam Leffler 			switch (subtype) {
6038a1b9b6aSSam Leffler 			case IEEE80211_FC0_SUBTYPE_PS_POLL:
6048a1b9b6aSSam Leffler 				ieee80211_recv_pspoll(ic, ni, m);
6058a1b9b6aSSam Leffler 				break;
6068a1b9b6aSSam Leffler 			}
6078a1b9b6aSSam Leffler 		}
60818f897abSSam Leffler 		goto out;
6091a1e1d21SSam Leffler 	default:
6108a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
6118a1b9b6aSSam Leffler 		    wh, NULL, "bad frame type 0x%x", type);
6121a1e1d21SSam Leffler 		/* should not come here */
6131a1e1d21SSam Leffler 		break;
6141a1e1d21SSam Leffler 	}
6151a1e1d21SSam Leffler   err:
6161a1e1d21SSam Leffler 	ifp->if_ierrors++;
6171a1e1d21SSam Leffler   out:
6181a1e1d21SSam Leffler 	if (m != NULL) {
6191a1e1d21SSam Leffler 		if (ic->ic_rawbpf)
6201a1e1d21SSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
6211a1e1d21SSam Leffler 		m_freem(m);
6221a1e1d21SSam Leffler 	}
6238a1b9b6aSSam Leffler #undef SEQ_LEQ
6241a1e1d21SSam Leffler }
6251a1e1d21SSam Leffler 
6268a1b9b6aSSam Leffler /*
6278a1b9b6aSSam Leffler  * This function reassemble fragments.
6288a1b9b6aSSam Leffler  */
6298a1b9b6aSSam Leffler static struct mbuf *
6308a1b9b6aSSam Leffler ieee80211_defrag(struct ieee80211com *ic, struct ieee80211_node *ni,
6318a1b9b6aSSam Leffler 	struct mbuf *m)
6321a1e1d21SSam Leffler {
6338a1b9b6aSSam Leffler 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
6348a1b9b6aSSam Leffler 	struct ieee80211_frame *lwh;
6358a1b9b6aSSam Leffler 	u_int16_t rxseq;
6368a1b9b6aSSam Leffler 	u_int8_t fragno;
6378a1b9b6aSSam Leffler 	u_int8_t more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG;
6388a1b9b6aSSam Leffler 	struct mbuf *mfrag;
6398a1b9b6aSSam Leffler 
6408a1b9b6aSSam Leffler 	KASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1), ("multicast fragm?"));
6418a1b9b6aSSam Leffler 
6428a1b9b6aSSam Leffler 	rxseq = le16toh(*(u_int16_t *)wh->i_seq);
6438a1b9b6aSSam Leffler 	fragno = rxseq & IEEE80211_SEQ_FRAG_MASK;
6448a1b9b6aSSam Leffler 
6458a1b9b6aSSam Leffler 	/* Quick way out, if there's nothing to defragment */
6468a1b9b6aSSam Leffler 	if (!more_frag && fragno == 0 && ni->ni_rxfrag[0] == NULL)
6478a1b9b6aSSam Leffler 		return m;
6488a1b9b6aSSam Leffler 
6498a1b9b6aSSam Leffler 	/*
6508a1b9b6aSSam Leffler 	 * Remove frag to insure it doesn't get reaped by timer.
6518a1b9b6aSSam Leffler 	 */
6528a1b9b6aSSam Leffler 	if (ni->ni_table == NULL) {
6538a1b9b6aSSam Leffler 		/*
6548a1b9b6aSSam Leffler 		 * Should never happen.  If the node is orphaned (not in
6558a1b9b6aSSam Leffler 		 * the table) then input packets should not reach here.
6568a1b9b6aSSam Leffler 		 * Otherwise, a concurrent request that yanks the table
6578a1b9b6aSSam Leffler 		 * should be blocked by other interlocking and/or by first
6588a1b9b6aSSam Leffler 		 * shutting the driver down.  Regardless, be defensive
6598a1b9b6aSSam Leffler 		 * here and just bail
6608a1b9b6aSSam Leffler 		 */
6618a1b9b6aSSam Leffler 		/* XXX need msg+stat */
6628a1b9b6aSSam Leffler 		m_freem(m);
6638a1b9b6aSSam Leffler 		return NULL;
6648a1b9b6aSSam Leffler 	}
6658a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(ni->ni_table);
6668a1b9b6aSSam Leffler 	mfrag = ni->ni_rxfrag[0];
6678a1b9b6aSSam Leffler 	ni->ni_rxfrag[0] = NULL;
6688a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(ni->ni_table);
6698a1b9b6aSSam Leffler 
6708a1b9b6aSSam Leffler 	/*
6718a1b9b6aSSam Leffler 	 * Validate new fragment is in order and
6728a1b9b6aSSam Leffler 	 * related to the previous ones.
6738a1b9b6aSSam Leffler 	 */
6748a1b9b6aSSam Leffler 	if (mfrag != NULL) {
6758a1b9b6aSSam Leffler 		u_int16_t last_rxseq;
6768a1b9b6aSSam Leffler 
6778a1b9b6aSSam Leffler 		lwh = mtod(mfrag, struct ieee80211_frame *);
6788a1b9b6aSSam Leffler 		last_rxseq = le16toh(*(u_int16_t *)lwh->i_seq);
6798a1b9b6aSSam Leffler 		/* NB: check seq # and frag together */
6808a1b9b6aSSam Leffler 		if (rxseq != last_rxseq+1 ||
6818a1b9b6aSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) ||
6828a1b9b6aSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) {
6838a1b9b6aSSam Leffler 			/*
6848a1b9b6aSSam Leffler 			 * Unrelated fragment or no space for it,
6858a1b9b6aSSam Leffler 			 * clear current fragments.
6868a1b9b6aSSam Leffler 			 */
6878a1b9b6aSSam Leffler 			m_freem(mfrag);
6888a1b9b6aSSam Leffler 			mfrag = NULL;
6898a1b9b6aSSam Leffler 		}
6908a1b9b6aSSam Leffler 	}
6918a1b9b6aSSam Leffler 
6928a1b9b6aSSam Leffler  	if (mfrag == NULL) {
6938a1b9b6aSSam Leffler 		if (fragno != 0) {		/* !first fragment, discard */
6948a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_defrag);
6958a1b9b6aSSam Leffler 			m_freem(m);
6968a1b9b6aSSam Leffler 			return NULL;
6978a1b9b6aSSam Leffler 		}
6988a1b9b6aSSam Leffler 		mfrag = m;
6998a1b9b6aSSam Leffler 	} else {				/* concatenate */
7008a1b9b6aSSam Leffler 		m_cat(mfrag, m);
7018a1b9b6aSSam Leffler 		/* NB: m_cat doesn't update the packet header */
7028a1b9b6aSSam Leffler 		mfrag->m_pkthdr.len += m->m_pkthdr.len;
7038a1b9b6aSSam Leffler 		/* track last seqnum and fragno */
7048a1b9b6aSSam Leffler 		lwh = mtod(mfrag, struct ieee80211_frame *);
7058a1b9b6aSSam Leffler 		*(u_int16_t *) lwh->i_seq = *(u_int16_t *) wh->i_seq;
7068a1b9b6aSSam Leffler 	}
7078a1b9b6aSSam Leffler 	if (more_frag) {			/* more to come, save */
7088a1b9b6aSSam Leffler 		ni->ni_rxfrag[0] = mfrag;
7098a1b9b6aSSam Leffler 		mfrag = NULL;
7108a1b9b6aSSam Leffler 	}
7118a1b9b6aSSam Leffler 	return mfrag;
7128a1b9b6aSSam Leffler }
7138a1b9b6aSSam Leffler 
7148a1b9b6aSSam Leffler static struct mbuf *
7158a1b9b6aSSam Leffler ieee80211_decap(struct ieee80211com *ic, struct mbuf *m)
7168a1b9b6aSSam Leffler {
7178a1b9b6aSSam Leffler 	struct ieee80211_frame wh;	/* NB: QoS stripped above */
7181a1e1d21SSam Leffler 	struct ether_header *eh;
7191a1e1d21SSam Leffler 	struct llc *llc;
7201a1e1d21SSam Leffler 
7218a1b9b6aSSam Leffler 	if (m->m_len < sizeof(wh) + sizeof(*llc) &&
7228a1b9b6aSSam Leffler 	    (m = m_pullup(m, sizeof(wh) + sizeof(*llc))) == NULL) {
7238a1b9b6aSSam Leffler 		/* XXX stat, msg */
7241a1e1d21SSam Leffler 		return NULL;
7251a1e1d21SSam Leffler 	}
7261a1e1d21SSam Leffler 	memcpy(&wh, mtod(m, caddr_t), sizeof(wh));
7271a1e1d21SSam Leffler 	llc = (struct llc *)(mtod(m, caddr_t) + sizeof(wh));
7281a1e1d21SSam Leffler 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
7291a1e1d21SSam Leffler 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
7301a1e1d21SSam Leffler 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0) {
7311a1e1d21SSam Leffler 		m_adj(m, sizeof(wh) + sizeof(struct llc) - sizeof(*eh));
7321a1e1d21SSam Leffler 		llc = NULL;
7331a1e1d21SSam Leffler 	} else {
7341a1e1d21SSam Leffler 		m_adj(m, sizeof(wh) - sizeof(*eh));
7351a1e1d21SSam Leffler 	}
7361a1e1d21SSam Leffler 	eh = mtod(m, struct ether_header *);
7371a1e1d21SSam Leffler 	switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
7381a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_NODS:
7391a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
7401a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
7411a1e1d21SSam Leffler 		break;
7421a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_TODS:
7431a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
7441a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
7451a1e1d21SSam Leffler 		break;
7461a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
7471a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
7481a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3);
7491a1e1d21SSam Leffler 		break;
7501a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
7511a1e1d21SSam Leffler 		/* not yet supported */
7528a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
7538a1b9b6aSSam Leffler 		    &wh, "data", "%s", "DS to DS not supported");
7541a1e1d21SSam Leffler 		m_freem(m);
7551a1e1d21SSam Leffler 		return NULL;
7561a1e1d21SSam Leffler 	}
7571a1e1d21SSam Leffler #ifdef ALIGNED_POINTER
7581a1e1d21SSam Leffler 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), u_int32_t)) {
7591a1e1d21SSam Leffler 		struct mbuf *n, *n0, **np;
7601a1e1d21SSam Leffler 		caddr_t newdata;
7611a1e1d21SSam Leffler 		int off, pktlen;
7621a1e1d21SSam Leffler 
7631a1e1d21SSam Leffler 		n0 = NULL;
7641a1e1d21SSam Leffler 		np = &n0;
7651a1e1d21SSam Leffler 		off = 0;
7661a1e1d21SSam Leffler 		pktlen = m->m_pkthdr.len;
7671a1e1d21SSam Leffler 		while (pktlen > off) {
7681a1e1d21SSam Leffler 			if (n0 == NULL) {
7691a1e1d21SSam Leffler 				MGETHDR(n, M_DONTWAIT, MT_DATA);
7701a1e1d21SSam Leffler 				if (n == NULL) {
7711a1e1d21SSam Leffler 					m_freem(m);
7721a1e1d21SSam Leffler 					return NULL;
7731a1e1d21SSam Leffler 				}
7741a1e1d21SSam Leffler 				M_MOVE_PKTHDR(n, m);
7751a1e1d21SSam Leffler 				n->m_len = MHLEN;
7761a1e1d21SSam Leffler 			} else {
7771a1e1d21SSam Leffler 				MGET(n, M_DONTWAIT, MT_DATA);
7781a1e1d21SSam Leffler 				if (n == NULL) {
7791a1e1d21SSam Leffler 					m_freem(m);
7801a1e1d21SSam Leffler 					m_freem(n0);
7811a1e1d21SSam Leffler 					return NULL;
7821a1e1d21SSam Leffler 				}
7831a1e1d21SSam Leffler 				n->m_len = MLEN;
7841a1e1d21SSam Leffler 			}
7851a1e1d21SSam Leffler 			if (pktlen - off >= MINCLSIZE) {
7861a1e1d21SSam Leffler 				MCLGET(n, M_DONTWAIT);
7871a1e1d21SSam Leffler 				if (n->m_flags & M_EXT)
7881a1e1d21SSam Leffler 					n->m_len = n->m_ext.ext_size;
7891a1e1d21SSam Leffler 			}
7901a1e1d21SSam Leffler 			if (n0 == NULL) {
7911a1e1d21SSam Leffler 				newdata =
7921a1e1d21SSam Leffler 				    (caddr_t)ALIGN(n->m_data + sizeof(*eh)) -
7931a1e1d21SSam Leffler 				    sizeof(*eh);
7941a1e1d21SSam Leffler 				n->m_len -= newdata - n->m_data;
7951a1e1d21SSam Leffler 				n->m_data = newdata;
7961a1e1d21SSam Leffler 			}
7971a1e1d21SSam Leffler 			if (n->m_len > pktlen - off)
7981a1e1d21SSam Leffler 				n->m_len = pktlen - off;
7991a1e1d21SSam Leffler 			m_copydata(m, off, n->m_len, mtod(n, caddr_t));
8001a1e1d21SSam Leffler 			off += n->m_len;
8011a1e1d21SSam Leffler 			*np = n;
8021a1e1d21SSam Leffler 			np = &n->m_next;
8031a1e1d21SSam Leffler 		}
8041a1e1d21SSam Leffler 		m_freem(m);
8051a1e1d21SSam Leffler 		m = n0;
8061a1e1d21SSam Leffler 	}
8071a1e1d21SSam Leffler #endif /* ALIGNED_POINTER */
8081a1e1d21SSam Leffler 	if (llc != NULL) {
8091a1e1d21SSam Leffler 		eh = mtod(m, struct ether_header *);
8101a1e1d21SSam Leffler 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
8111a1e1d21SSam Leffler 	}
8121a1e1d21SSam Leffler 	return m;
8131a1e1d21SSam Leffler }
8141a1e1d21SSam Leffler 
8151a1e1d21SSam Leffler /*
8161a1e1d21SSam Leffler  * Install received rate set information in the node's state block.
8171a1e1d21SSam Leffler  */
8181a1e1d21SSam Leffler static int
8191a1e1d21SSam Leffler ieee80211_setup_rates(struct ieee80211com *ic, struct ieee80211_node *ni,
8201a1e1d21SSam Leffler 	u_int8_t *rates, u_int8_t *xrates, int flags)
8211a1e1d21SSam Leffler {
8221a1e1d21SSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
8231a1e1d21SSam Leffler 
8241a1e1d21SSam Leffler 	memset(rs, 0, sizeof(*rs));
8251a1e1d21SSam Leffler 	rs->rs_nrates = rates[1];
8261a1e1d21SSam Leffler 	memcpy(rs->rs_rates, rates + 2, rs->rs_nrates);
8271a1e1d21SSam Leffler 	if (xrates != NULL) {
8281a1e1d21SSam Leffler 		u_int8_t nxrates;
8291a1e1d21SSam Leffler 		/*
8301a1e1d21SSam Leffler 		 * Tack on 11g extended supported rate element.
8311a1e1d21SSam Leffler 		 */
8321a1e1d21SSam Leffler 		nxrates = xrates[1];
8331a1e1d21SSam Leffler 		if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
8341a1e1d21SSam Leffler 			nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates;
8358a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_XRATE,
8368a1b9b6aSSam Leffler 			     "[%s] extended rate set too large;"
8371a1e1d21SSam Leffler 			     " only using %u of %u rates\n",
8388a1b9b6aSSam Leffler 			     ether_sprintf(ni->ni_macaddr), nxrates, xrates[1]);
8391be50176SSam Leffler 			ic->ic_stats.is_rx_rstoobig++;
8401a1e1d21SSam Leffler 		}
8411a1e1d21SSam Leffler 		memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates);
8421a1e1d21SSam Leffler 		rs->rs_nrates += nxrates;
8431a1e1d21SSam Leffler 	}
8441a1e1d21SSam Leffler 	return ieee80211_fix_rate(ic, ni, flags);
8451a1e1d21SSam Leffler }
8461a1e1d21SSam Leffler 
8478a1b9b6aSSam Leffler static void
8488a1b9b6aSSam Leffler ieee80211_auth_open(struct ieee80211com *ic, struct ieee80211_frame *wh,
8498a1b9b6aSSam Leffler     struct ieee80211_node *ni, int rssi, u_int32_t rstamp, u_int16_t seq,
8508a1b9b6aSSam Leffler     u_int16_t status)
8518a1b9b6aSSam Leffler {
8528a1b9b6aSSam Leffler 
8538a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
8548a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:
8558a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN ||
8568a1b9b6aSSam Leffler 		    seq != IEEE80211_AUTH_OPEN_REQUEST) {
8578a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
8588a1b9b6aSSam Leffler 			return;
8598a1b9b6aSSam Leffler 		}
8608a1b9b6aSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_AUTH,
8618a1b9b6aSSam Leffler 		    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
8628a1b9b6aSSam Leffler 		break;
8638a1b9b6aSSam Leffler 
8648a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:
8658a1b9b6aSSam Leffler 		/* should not come here */
8668a1b9b6aSSam Leffler 		break;
8678a1b9b6aSSam Leffler 
8688a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:
8698a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN ||
8708a1b9b6aSSam Leffler 		    seq != IEEE80211_AUTH_OPEN_REQUEST) {
8718a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
8728a1b9b6aSSam Leffler 			return;
8738a1b9b6aSSam Leffler 		}
8748a1b9b6aSSam Leffler 		/* always accept open authentication requests */
8758a1b9b6aSSam Leffler 		if (ni == ic->ic_bss) {
8768a1b9b6aSSam Leffler 			ni = ieee80211_dup_bss(ic->ic_sta, wh->i_addr2);
8778a1b9b6aSSam Leffler 			if (ni == NULL)
8788a1b9b6aSSam Leffler 				return;
8798a1b9b6aSSam Leffler 		} else
8808a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
8818a1b9b6aSSam Leffler 		ni->ni_inact_reload = ic->ic_inact_auth;
8828a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni,
8838a1b9b6aSSam Leffler 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
8848a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
8858a1b9b6aSSam Leffler 		    "[%s] station authenticated (open)\n",
8868a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
8878a1b9b6aSSam Leffler 		break;
8888a1b9b6aSSam Leffler 
8898a1b9b6aSSam Leffler 	case IEEE80211_M_STA:
8908a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_AUTH ||
8918a1b9b6aSSam Leffler 		    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
8928a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
8938a1b9b6aSSam Leffler 			return;
8948a1b9b6aSSam Leffler 		}
8958a1b9b6aSSam Leffler 		if (status != 0) {
8968a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
8978a1b9b6aSSam Leffler 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
8988a1b9b6aSSam Leffler 			    "[%s] open auth failed (reason %d)\n",
8998a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr), status);
9008a1b9b6aSSam Leffler 			/* XXX can this happen? */
9018a1b9b6aSSam Leffler 			if (ni != ic->ic_bss)
9028a1b9b6aSSam Leffler 				ni->ni_fails++;
9038a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_fail++;
9048a1b9b6aSSam Leffler 			return;
9058a1b9b6aSSam Leffler 		}
9068a1b9b6aSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_ASSOC,
9078a1b9b6aSSam Leffler 		    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
9088a1b9b6aSSam Leffler 		break;
9098a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:
9108a1b9b6aSSam Leffler 		break;
9118a1b9b6aSSam Leffler 	}
9128a1b9b6aSSam Leffler }
9138a1b9b6aSSam Leffler 
9148a1b9b6aSSam Leffler static int
9158a1b9b6aSSam Leffler alloc_challenge(struct ieee80211com *ic, struct ieee80211_node *ni)
9168a1b9b6aSSam Leffler {
9178a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL)
9188a1b9b6aSSam Leffler 		MALLOC(ni->ni_challenge, u_int32_t*, IEEE80211_CHALLENGE_LEN,
9198a1b9b6aSSam Leffler 		    M_DEVBUF, M_NOWAIT);
9208a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL) {
9218a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
9228a1b9b6aSSam Leffler 		    "[%s] shared key challenge alloc failed\n",
9238a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
9248a1b9b6aSSam Leffler 		/* XXX statistic */
9258a1b9b6aSSam Leffler 	}
9268a1b9b6aSSam Leffler 	return (ni->ni_challenge != NULL);
9278a1b9b6aSSam Leffler }
9288a1b9b6aSSam Leffler 
9298a1b9b6aSSam Leffler /* XXX TODO: add statistics */
9308a1b9b6aSSam Leffler static void
9318a1b9b6aSSam Leffler ieee80211_auth_shared(struct ieee80211com *ic, struct ieee80211_frame *wh,
9328a1b9b6aSSam Leffler     u_int8_t *frm, u_int8_t *efrm, struct ieee80211_node *ni, int rssi,
9338a1b9b6aSSam Leffler     u_int32_t rstamp, u_int16_t seq, u_int16_t status)
9348a1b9b6aSSam Leffler {
9358a1b9b6aSSam Leffler 	u_int8_t *challenge;
9368a1b9b6aSSam Leffler 	int allocbs, estatus;
9378a1b9b6aSSam Leffler 
9388a1b9b6aSSam Leffler 	/*
9398a1b9b6aSSam Leffler 	 * NB: this can happen as we allow pre-shared key
9408a1b9b6aSSam Leffler 	 * authentication to be enabled w/o wep being turned
9418a1b9b6aSSam Leffler 	 * on so that configuration of these can be done
9428a1b9b6aSSam Leffler 	 * in any order.  It may be better to enforce the
9438a1b9b6aSSam Leffler 	 * ordering in which case this check would just be
9448a1b9b6aSSam Leffler 	 * for sanity/consistency.
9458a1b9b6aSSam Leffler 	 */
9468a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
9478a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
9488a1b9b6aSSam Leffler 		    ni->ni_macaddr, "shared key auth",
9498a1b9b6aSSam Leffler 		    "%s", " PRIVACY is disabled");
9508a1b9b6aSSam Leffler 		estatus = IEEE80211_STATUS_ALG;
9518a1b9b6aSSam Leffler 		goto bad;
9528a1b9b6aSSam Leffler 	}
9538a1b9b6aSSam Leffler 	/*
9548a1b9b6aSSam Leffler 	 * Pre-shared key authentication is evil; accept
9558a1b9b6aSSam Leffler 	 * it only if explicitly configured (it is supported
9568a1b9b6aSSam Leffler 	 * mainly for compatibility with clients like OS X).
9578a1b9b6aSSam Leffler 	 */
9588a1b9b6aSSam Leffler 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
9598a1b9b6aSSam Leffler 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
9608a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
9618a1b9b6aSSam Leffler 		    ni->ni_macaddr, "shared key auth",
9628a1b9b6aSSam Leffler 		    "bad sta auth mode %u", ni->ni_authmode);
9638a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
9648a1b9b6aSSam Leffler 		estatus = IEEE80211_STATUS_ALG;
9658a1b9b6aSSam Leffler 		goto bad;
9668a1b9b6aSSam Leffler 	}
9678a1b9b6aSSam Leffler 
9688a1b9b6aSSam Leffler 	challenge = NULL;
9698a1b9b6aSSam Leffler 	if (frm + 1 < efrm) {
9708a1b9b6aSSam Leffler 		if ((frm[1] + 2) > (efrm - frm)) {
9718a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
9728a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
9738a1b9b6aSSam Leffler 			    "ie %d/%d too long",
9748a1b9b6aSSam Leffler 			    frm[0], (frm[1] + 2) - (efrm - frm));
9758a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
9768a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
9778a1b9b6aSSam Leffler 			goto bad;
9788a1b9b6aSSam Leffler 		}
9798a1b9b6aSSam Leffler 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
9808a1b9b6aSSam Leffler 			challenge = frm;
9818a1b9b6aSSam Leffler 		frm += frm[1] + 2;
9828a1b9b6aSSam Leffler 	}
9838a1b9b6aSSam Leffler 	switch (seq) {
9848a1b9b6aSSam Leffler 	case IEEE80211_AUTH_SHARED_CHALLENGE:
9858a1b9b6aSSam Leffler 	case IEEE80211_AUTH_SHARED_RESPONSE:
9868a1b9b6aSSam Leffler 		if (challenge == NULL) {
9878a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
9888a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
9898a1b9b6aSSam Leffler 			    "%s", "no challenge");
9908a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
9918a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
9928a1b9b6aSSam Leffler 			goto bad;
9938a1b9b6aSSam Leffler 		}
9948a1b9b6aSSam Leffler 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
9958a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
9968a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
9978a1b9b6aSSam Leffler 			    "bad challenge len %d", challenge[1]);
9988a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
9998a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
10008a1b9b6aSSam Leffler 			goto bad;
10018a1b9b6aSSam Leffler 		}
10028a1b9b6aSSam Leffler 	default:
10038a1b9b6aSSam Leffler 		break;
10048a1b9b6aSSam Leffler 	}
10058a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
10068a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:
10078a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:
10088a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:
10098a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10108a1b9b6aSSam Leffler 		    ni->ni_macaddr, "shared key auth",
10118a1b9b6aSSam Leffler 		    "bad operating mode %u", ic->ic_opmode);
10128a1b9b6aSSam Leffler 		return;
10138a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:
10148a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN) {
10158a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10168a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10178a1b9b6aSSam Leffler 			    "bad state %u", ic->ic_state);
10188a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_ALG;	/* XXX */
10198a1b9b6aSSam Leffler 			goto bad;
10208a1b9b6aSSam Leffler 		}
10218a1b9b6aSSam Leffler 		switch (seq) {
10228a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_REQUEST:
10238a1b9b6aSSam Leffler 			if (ni == ic->ic_bss) {
10248a1b9b6aSSam Leffler 				ni = ieee80211_dup_bss(ic->ic_sta, wh->i_addr2);
10258a1b9b6aSSam Leffler 				if (ni == NULL) {
10268a1b9b6aSSam Leffler 					/* NB: no way to return an error */
10278a1b9b6aSSam Leffler 					return;
10288a1b9b6aSSam Leffler 				}
10298a1b9b6aSSam Leffler 				allocbs = 1;
10308a1b9b6aSSam Leffler 			} else {
10318a1b9b6aSSam Leffler 				(void) ieee80211_ref_node(ni);
10328a1b9b6aSSam Leffler 				allocbs = 0;
10338a1b9b6aSSam Leffler 			}
10348a1b9b6aSSam Leffler 			ni->ni_rssi = rssi;
10358a1b9b6aSSam Leffler 			ni->ni_rstamp = rstamp;
10368a1b9b6aSSam Leffler 			if (!alloc_challenge(ic, ni)) {
10378a1b9b6aSSam Leffler 				/* NB: don't return error so they rexmit */
10388a1b9b6aSSam Leffler 				return;
10398a1b9b6aSSam Leffler 			}
10408a1b9b6aSSam Leffler 			get_random_bytes(ni->ni_challenge,
10418a1b9b6aSSam Leffler 				IEEE80211_CHALLENGE_LEN);
10428a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
10438a1b9b6aSSam Leffler 				IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
10448a1b9b6aSSam Leffler 				"[%s] shared key %sauth request\n",
10458a1b9b6aSSam Leffler 				ether_sprintf(ni->ni_macaddr),
10468a1b9b6aSSam Leffler 				allocbs ? "" : "re");
10478a1b9b6aSSam Leffler 			break;
10488a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_RESPONSE:
10498a1b9b6aSSam Leffler 			if (ni == ic->ic_bss) {
10508a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10518a1b9b6aSSam Leffler 				    ni->ni_macaddr, "shared key response",
10528a1b9b6aSSam Leffler 				    "%s", "unknown station");
10538a1b9b6aSSam Leffler 				/* NB: don't send a response */
10548a1b9b6aSSam Leffler 				return;
10558a1b9b6aSSam Leffler 			}
10568a1b9b6aSSam Leffler 			if (ni->ni_challenge == NULL) {
10578a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10588a1b9b6aSSam Leffler 				    ni->ni_macaddr, "shared key response",
10598a1b9b6aSSam Leffler 				    "%s", "no challenge recorded");
10608a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_bad_auth++;
10618a1b9b6aSSam Leffler 				estatus = IEEE80211_STATUS_CHALLENGE;
10628a1b9b6aSSam Leffler 				goto bad;
10638a1b9b6aSSam Leffler 			}
10648a1b9b6aSSam Leffler 			if (memcmp(ni->ni_challenge, &challenge[2],
10658a1b9b6aSSam Leffler 			           challenge[1]) != 0) {
10668a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10678a1b9b6aSSam Leffler 				    ni->ni_macaddr, "shared key response",
10688a1b9b6aSSam Leffler 				    "%s", "challenge mismatch");
10698a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_auth_fail++;
10708a1b9b6aSSam Leffler 				estatus = IEEE80211_STATUS_CHALLENGE;
10718a1b9b6aSSam Leffler 				goto bad;
10728a1b9b6aSSam Leffler 			}
10738a1b9b6aSSam Leffler 			ni->ni_inact_reload = ic->ic_inact_auth;
10748a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
10758a1b9b6aSSam Leffler 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
10768a1b9b6aSSam Leffler 			    "[%s] station authenticated (shared key)\n",
10778a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr));
10788a1b9b6aSSam Leffler 			break;
10798a1b9b6aSSam Leffler 		default:
10808a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10818a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10828a1b9b6aSSam Leffler 			    "bad seq %d", seq);
10838a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
10848a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_SEQUENCE;
10858a1b9b6aSSam Leffler 			goto bad;
10868a1b9b6aSSam Leffler 		}
10878a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni,
10888a1b9b6aSSam Leffler 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
10898a1b9b6aSSam Leffler 		break;
10908a1b9b6aSSam Leffler 
10918a1b9b6aSSam Leffler 	case IEEE80211_M_STA:
10928a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_AUTH)
10938a1b9b6aSSam Leffler 			return;
10948a1b9b6aSSam Leffler 		switch (seq) {
10958a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_PASS:
10968a1b9b6aSSam Leffler 			if (ni->ni_challenge != NULL) {
10978a1b9b6aSSam Leffler 				FREE(ni->ni_challenge, M_DEVBUF);
10988a1b9b6aSSam Leffler 				ni->ni_challenge = NULL;
10998a1b9b6aSSam Leffler 			}
11008a1b9b6aSSam Leffler 			if (status != 0) {
11018a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic,
11028a1b9b6aSSam Leffler 				    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
11038a1b9b6aSSam Leffler 				    "[%s] shared key auth failed (reason %d)\n",
11048a1b9b6aSSam Leffler 				    ether_sprintf(ieee80211_getbssid(ic, wh)),
11058a1b9b6aSSam Leffler 				    status);
11068a1b9b6aSSam Leffler 				/* XXX can this happen? */
11078a1b9b6aSSam Leffler 				if (ni != ic->ic_bss)
11088a1b9b6aSSam Leffler 					ni->ni_fails++;
11098a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_auth_fail++;
11108a1b9b6aSSam Leffler 				return;
11118a1b9b6aSSam Leffler 			}
11128a1b9b6aSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
11138a1b9b6aSSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
11148a1b9b6aSSam Leffler 			break;
11158a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_CHALLENGE:
11168a1b9b6aSSam Leffler 			if (!alloc_challenge(ic, ni))
11178a1b9b6aSSam Leffler 				return;
11188a1b9b6aSSam Leffler 			/* XXX could optimize by passing recvd challenge */
11198a1b9b6aSSam Leffler 			memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
11208a1b9b6aSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni,
11218a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
11228a1b9b6aSSam Leffler 			break;
11238a1b9b6aSSam Leffler 		default:
11248a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_AUTH,
11258a1b9b6aSSam Leffler 			    wh, "shared key auth", "bad seq %d", seq);
11268a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
11278a1b9b6aSSam Leffler 			return;
11288a1b9b6aSSam Leffler 		}
11298a1b9b6aSSam Leffler 		break;
11308a1b9b6aSSam Leffler 	}
11318a1b9b6aSSam Leffler 	return;
11328a1b9b6aSSam Leffler bad:
11338a1b9b6aSSam Leffler 	/*
11348a1b9b6aSSam Leffler 	 * Send an error response; but only when operating as an AP.
11358a1b9b6aSSam Leffler 	 */
11368a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
11378a1b9b6aSSam Leffler 		/* XXX hack to workaround calling convention */
11388a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni,
11398a1b9b6aSSam Leffler 			IEEE80211_FC0_SUBTYPE_AUTH,
11408a1b9b6aSSam Leffler 			(seq + 1) | (estatus<<16));
11418a1b9b6aSSam Leffler 	}
11428a1b9b6aSSam Leffler }
11438a1b9b6aSSam Leffler 
11441a1e1d21SSam Leffler /* Verify the existence and length of __elem or get out. */
11451a1e1d21SSam Leffler #define IEEE80211_VERIFY_ELEMENT(__elem, __maxlen) do {			\
11461a1e1d21SSam Leffler 	if ((__elem) == NULL) {						\
11478a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
11488a1b9b6aSSam Leffler 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
11498a1b9b6aSSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
11508a1b9b6aSSam Leffler 		    "%s", "no " #__elem );				\
11511be50176SSam Leffler 		ic->ic_stats.is_rx_elem_missing++;			\
11521a1e1d21SSam Leffler 		return;							\
11531a1e1d21SSam Leffler 	}								\
11541a1e1d21SSam Leffler 	if ((__elem)[1] > (__maxlen)) {					\
11558a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
11568a1b9b6aSSam Leffler 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
11571a1e1d21SSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
11588a1b9b6aSSam Leffler 		    "bad " #__elem " len %d", (__elem)[1]);		\
11591be50176SSam Leffler 		ic->ic_stats.is_rx_elem_toobig++;			\
11601a1e1d21SSam Leffler 		return;							\
11611a1e1d21SSam Leffler 	}								\
11621a1e1d21SSam Leffler } while (0)
11631a1e1d21SSam Leffler 
11641a1e1d21SSam Leffler #define	IEEE80211_VERIFY_LENGTH(_len, _minlen) do {			\
11651a1e1d21SSam Leffler 	if ((_len) < (_minlen)) {					\
11668a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
11678a1b9b6aSSam Leffler 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
11681a1e1d21SSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
11698a1b9b6aSSam Leffler 		    "%s", "ie too short");				\
11701be50176SSam Leffler 		ic->ic_stats.is_rx_elem_toosmall++;			\
11711a1e1d21SSam Leffler 		return;							\
11721a1e1d21SSam Leffler 	}								\
11731a1e1d21SSam Leffler } while (0)
11741a1e1d21SSam Leffler 
11758a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
11768a1b9b6aSSam Leffler static void
11778a1b9b6aSSam Leffler ieee80211_ssid_mismatch(struct ieee80211com *ic, const char *tag,
11788a1b9b6aSSam Leffler 	u_int8_t mac[IEEE80211_ADDR_LEN], u_int8_t *ssid)
11798a1b9b6aSSam Leffler {
11808a1b9b6aSSam Leffler 	printf("[%s] discard %s frame, ssid mismatch: ",
11818a1b9b6aSSam Leffler 		ether_sprintf(mac), tag);
11828a1b9b6aSSam Leffler 	ieee80211_print_essid(ssid + 2, ssid[1]);
11838a1b9b6aSSam Leffler 	printf("\n");
11848a1b9b6aSSam Leffler }
11858a1b9b6aSSam Leffler 
11868a1b9b6aSSam Leffler #define	IEEE80211_VERIFY_SSID(_ni, _ssid) do {				\
11878a1b9b6aSSam Leffler 	if ((_ssid)[1] != 0 &&						\
11888a1b9b6aSSam Leffler 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
11898a1b9b6aSSam Leffler 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
11908a1b9b6aSSam Leffler 		if (ieee80211_msg_input(ic))				\
11918a1b9b6aSSam Leffler 			ieee80211_ssid_mismatch(ic, 			\
11928a1b9b6aSSam Leffler 			    ieee80211_mgt_subtype_name[subtype >>	\
11938a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],		\
11948a1b9b6aSSam Leffler 				wh->i_addr2, _ssid);			\
11958a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_ssidmismatch++;			\
11968a1b9b6aSSam Leffler 		return;							\
11978a1b9b6aSSam Leffler 	}								\
11988a1b9b6aSSam Leffler } while (0)
11998a1b9b6aSSam Leffler #else /* !IEEE80211_DEBUG */
12008a1b9b6aSSam Leffler #define	IEEE80211_VERIFY_SSID(_ni, _ssid) do {				\
12018a1b9b6aSSam Leffler 	if ((_ssid)[1] != 0 &&						\
12028a1b9b6aSSam Leffler 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
12038a1b9b6aSSam Leffler 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
12048a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_ssidmismatch++;			\
12058a1b9b6aSSam Leffler 		return;							\
12068a1b9b6aSSam Leffler 	}								\
12078a1b9b6aSSam Leffler } while (0)
12088a1b9b6aSSam Leffler #endif /* !IEEE80211_DEBUG */
12098a1b9b6aSSam Leffler 
12108a1b9b6aSSam Leffler /* unalligned little endian access */
12118a1b9b6aSSam Leffler #define LE_READ_2(p)					\
12128a1b9b6aSSam Leffler 	((u_int16_t)					\
12138a1b9b6aSSam Leffler 	 ((((const u_int8_t *)(p))[0]      ) |		\
12148a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[1] <<  8)))
12158a1b9b6aSSam Leffler #define LE_READ_4(p)					\
12168a1b9b6aSSam Leffler 	((u_int32_t)					\
12178a1b9b6aSSam Leffler 	 ((((const u_int8_t *)(p))[0]      ) |		\
12188a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[1] <<  8) |		\
12198a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[2] << 16) |		\
12208a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[3] << 24)))
12218a1b9b6aSSam Leffler 
12228a1b9b6aSSam Leffler static int __inline
12238a1b9b6aSSam Leffler iswpaoui(const u_int8_t *frm)
12248a1b9b6aSSam Leffler {
12258a1b9b6aSSam Leffler 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
12268a1b9b6aSSam Leffler }
12278a1b9b6aSSam Leffler 
12288a1b9b6aSSam Leffler static int __inline
12298a1b9b6aSSam Leffler iswmeoui(const u_int8_t *frm)
12308a1b9b6aSSam Leffler {
12318a1b9b6aSSam Leffler 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
12328a1b9b6aSSam Leffler }
12338a1b9b6aSSam Leffler 
12348a1b9b6aSSam Leffler static int __inline
12358a1b9b6aSSam Leffler iswmeparam(const u_int8_t *frm)
12368a1b9b6aSSam Leffler {
12378a1b9b6aSSam Leffler 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
12388a1b9b6aSSam Leffler 		frm[6] == WME_PARAM_OUI_SUBTYPE;
12398a1b9b6aSSam Leffler }
12408a1b9b6aSSam Leffler 
12418a1b9b6aSSam Leffler static int __inline
12428a1b9b6aSSam Leffler iswmeinfo(const u_int8_t *frm)
12438a1b9b6aSSam Leffler {
12448a1b9b6aSSam Leffler 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
12458a1b9b6aSSam Leffler 		frm[6] == WME_INFO_OUI_SUBTYPE;
12468a1b9b6aSSam Leffler }
12478a1b9b6aSSam Leffler 
12488a1b9b6aSSam Leffler static int __inline
12498a1b9b6aSSam Leffler isatherosoui(const u_int8_t *frm)
12508a1b9b6aSSam Leffler {
12518a1b9b6aSSam Leffler 	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
12528a1b9b6aSSam Leffler }
12538a1b9b6aSSam Leffler 
12548a1b9b6aSSam Leffler /*
12558a1b9b6aSSam Leffler  * Convert a WPA cipher selector OUI to an internal
12568a1b9b6aSSam Leffler  * cipher algorithm.  Where appropriate we also
12578a1b9b6aSSam Leffler  * record any key length.
12588a1b9b6aSSam Leffler  */
12598a1b9b6aSSam Leffler static int
12608a1b9b6aSSam Leffler wpa_cipher(u_int8_t *sel, u_int8_t *keylen)
12618a1b9b6aSSam Leffler {
12628a1b9b6aSSam Leffler #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
12638a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
12648a1b9b6aSSam Leffler 
12658a1b9b6aSSam Leffler 	switch (w) {
12668a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_NULL):
12678a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_NONE;
12688a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_WEP40):
12698a1b9b6aSSam Leffler 		if (keylen)
12708a1b9b6aSSam Leffler 			*keylen = 40 / NBBY;
12718a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
12728a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_WEP104):
12738a1b9b6aSSam Leffler 		if (keylen)
12748a1b9b6aSSam Leffler 			*keylen = 104 / NBBY;
12758a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
12768a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_TKIP):
12778a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_TKIP;
12788a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_CCMP):
12798a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_AES_CCM;
12808a1b9b6aSSam Leffler 	}
12818a1b9b6aSSam Leffler 	return 32;		/* NB: so 1<< is discarded */
12828a1b9b6aSSam Leffler #undef WPA_SEL
12838a1b9b6aSSam Leffler }
12848a1b9b6aSSam Leffler 
12858a1b9b6aSSam Leffler /*
12868a1b9b6aSSam Leffler  * Convert a WPA key management/authentication algorithm
12878a1b9b6aSSam Leffler  * to an internal code.
12888a1b9b6aSSam Leffler  */
12898a1b9b6aSSam Leffler static int
12908a1b9b6aSSam Leffler wpa_keymgmt(u_int8_t *sel)
12918a1b9b6aSSam Leffler {
12928a1b9b6aSSam Leffler #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
12938a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
12948a1b9b6aSSam Leffler 
12958a1b9b6aSSam Leffler 	switch (w) {
12968a1b9b6aSSam Leffler 	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
12978a1b9b6aSSam Leffler 		return WPA_ASE_8021X_UNSPEC;
12988a1b9b6aSSam Leffler 	case WPA_SEL(WPA_ASE_8021X_PSK):
12998a1b9b6aSSam Leffler 		return WPA_ASE_8021X_PSK;
13008a1b9b6aSSam Leffler 	case WPA_SEL(WPA_ASE_NONE):
13018a1b9b6aSSam Leffler 		return WPA_ASE_NONE;
13028a1b9b6aSSam Leffler 	}
13038a1b9b6aSSam Leffler 	return 0;		/* NB: so is discarded */
13048a1b9b6aSSam Leffler #undef WPA_SEL
13058a1b9b6aSSam Leffler }
13068a1b9b6aSSam Leffler 
13078a1b9b6aSSam Leffler /*
13088a1b9b6aSSam Leffler  * Parse a WPA information element to collect parameters
13098a1b9b6aSSam Leffler  * and validate the parameters against what has been
13108a1b9b6aSSam Leffler  * configured for the system.
13118a1b9b6aSSam Leffler  */
13128a1b9b6aSSam Leffler static int
13138a1b9b6aSSam Leffler ieee80211_parse_wpa(struct ieee80211com *ic, u_int8_t *frm,
13148a1b9b6aSSam Leffler 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
13158a1b9b6aSSam Leffler {
13168a1b9b6aSSam Leffler 	u_int8_t len = frm[1];
13178a1b9b6aSSam Leffler 	u_int32_t w;
13188a1b9b6aSSam Leffler 	int n;
13198a1b9b6aSSam Leffler 
13208a1b9b6aSSam Leffler 	/*
13218a1b9b6aSSam Leffler 	 * Check the length once for fixed parts: OUI, type,
13228a1b9b6aSSam Leffler 	 * version, mcast cipher, and 2 selector counts.
13238a1b9b6aSSam Leffler 	 * Other, variable-length data, must be checked separately.
13248a1b9b6aSSam Leffler 	 */
13258a1b9b6aSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_WPA1,
13268a1b9b6aSSam Leffler 		("not WPA, flags 0x%x", ic->ic_flags));
13278a1b9b6aSSam Leffler 	if (len < 14) {
13288a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
13298a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
13308a1b9b6aSSam Leffler 		    wh, "WPA", "too short, len %u", len);
13318a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
13328a1b9b6aSSam Leffler 	}
13338a1b9b6aSSam Leffler 	frm += 6, len -= 4;		/* NB: len is payload only */
13348a1b9b6aSSam Leffler 	/* NB: iswapoui already validated the OUI and type */
13358a1b9b6aSSam Leffler 	w = LE_READ_2(frm);
13368a1b9b6aSSam Leffler 	if (w != WPA_VERSION) {
13378a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
13388a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
13398a1b9b6aSSam Leffler 		    wh, "WPA", "bad version %u", w);
13408a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
13418a1b9b6aSSam Leffler 	}
13428a1b9b6aSSam Leffler 	frm += 2, len -= 2;
13438a1b9b6aSSam Leffler 
13448a1b9b6aSSam Leffler 	/* multicast/group cipher */
13458a1b9b6aSSam Leffler 	w = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
13468a1b9b6aSSam Leffler 	if (w != rsn->rsn_mcastcipher) {
13478a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
13488a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
13498a1b9b6aSSam Leffler 		    wh, "WPA", "mcast cipher mismatch; got %u, expected %u",
13508a1b9b6aSSam Leffler 		    w, rsn->rsn_mcastcipher);
13518a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
13528a1b9b6aSSam Leffler 	}
13538a1b9b6aSSam Leffler 	frm += 4, len -= 4;
13548a1b9b6aSSam Leffler 
13558a1b9b6aSSam Leffler 	/* unicast ciphers */
13568a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
13578a1b9b6aSSam Leffler 	frm += 2, len -= 2;
13588a1b9b6aSSam Leffler 	if (len < n*4+2) {
13598a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
13608a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
13618a1b9b6aSSam Leffler 		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
13628a1b9b6aSSam Leffler 		    len, n);
13638a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
13648a1b9b6aSSam Leffler 	}
13658a1b9b6aSSam Leffler 	w = 0;
13668a1b9b6aSSam Leffler 	for (; n > 0; n--) {
13678a1b9b6aSSam Leffler 		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
13688a1b9b6aSSam Leffler 		frm += 4, len -= 4;
13698a1b9b6aSSam Leffler 	}
13708a1b9b6aSSam Leffler 	w &= rsn->rsn_ucastcipherset;
13718a1b9b6aSSam Leffler 	if (w == 0) {
13728a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
13738a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
13748a1b9b6aSSam Leffler 		    wh, "WPA", "%s", "ucast cipher set empty");
13758a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
13768a1b9b6aSSam Leffler 	}
13778a1b9b6aSSam Leffler 	if (w & (1<<IEEE80211_CIPHER_TKIP))
13788a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
13798a1b9b6aSSam Leffler 	else
13808a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
13818a1b9b6aSSam Leffler 
13828a1b9b6aSSam Leffler 	/* key management algorithms */
13838a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
13848a1b9b6aSSam Leffler 	frm += 2, len -= 2;
13858a1b9b6aSSam Leffler 	if (len < n*4) {
13868a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
13878a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
13888a1b9b6aSSam Leffler 		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
13898a1b9b6aSSam Leffler 		    len, n);
13908a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
13918a1b9b6aSSam Leffler 	}
13928a1b9b6aSSam Leffler 	w = 0;
13938a1b9b6aSSam Leffler 	for (; n > 0; n--) {
13948a1b9b6aSSam Leffler 		w |= wpa_keymgmt(frm);
13958a1b9b6aSSam Leffler 		frm += 4, len -= 4;
13968a1b9b6aSSam Leffler 	}
13978a1b9b6aSSam Leffler 	w &= rsn->rsn_keymgmtset;
13988a1b9b6aSSam Leffler 	if (w == 0) {
13998a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14008a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14018a1b9b6aSSam Leffler 		    wh, "WPA", "%s", "no acceptable key mgmt alg");
14028a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14038a1b9b6aSSam Leffler 	}
14048a1b9b6aSSam Leffler 	if (w & WPA_ASE_8021X_UNSPEC)
14058a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
14068a1b9b6aSSam Leffler 	else
14078a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
14088a1b9b6aSSam Leffler 
14098a1b9b6aSSam Leffler 	if (len > 2)		/* optional capabilities */
14108a1b9b6aSSam Leffler 		rsn->rsn_caps = LE_READ_2(frm);
14118a1b9b6aSSam Leffler 
14128a1b9b6aSSam Leffler 	return 0;
14138a1b9b6aSSam Leffler }
14148a1b9b6aSSam Leffler 
14158a1b9b6aSSam Leffler /*
14168a1b9b6aSSam Leffler  * Convert an RSN cipher selector OUI to an internal
14178a1b9b6aSSam Leffler  * cipher algorithm.  Where appropriate we also
14188a1b9b6aSSam Leffler  * record any key length.
14198a1b9b6aSSam Leffler  */
14208a1b9b6aSSam Leffler static int
14218a1b9b6aSSam Leffler rsn_cipher(u_int8_t *sel, u_int8_t *keylen)
14228a1b9b6aSSam Leffler {
14238a1b9b6aSSam Leffler #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
14248a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
14258a1b9b6aSSam Leffler 
14268a1b9b6aSSam Leffler 	switch (w) {
14278a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_NULL):
14288a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_NONE;
14298a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_WEP40):
14308a1b9b6aSSam Leffler 		if (keylen)
14318a1b9b6aSSam Leffler 			*keylen = 40 / NBBY;
14328a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
14338a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_WEP104):
14348a1b9b6aSSam Leffler 		if (keylen)
14358a1b9b6aSSam Leffler 			*keylen = 104 / NBBY;
14368a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
14378a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_TKIP):
14388a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_TKIP;
14398a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_CCMP):
14408a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_AES_CCM;
14418a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_WRAP):
14428a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_AES_OCB;
14438a1b9b6aSSam Leffler 	}
14448a1b9b6aSSam Leffler 	return 32;		/* NB: so 1<< is discarded */
14458a1b9b6aSSam Leffler #undef WPA_SEL
14468a1b9b6aSSam Leffler }
14478a1b9b6aSSam Leffler 
14488a1b9b6aSSam Leffler /*
14498a1b9b6aSSam Leffler  * Convert an RSN key management/authentication algorithm
14508a1b9b6aSSam Leffler  * to an internal code.
14518a1b9b6aSSam Leffler  */
14528a1b9b6aSSam Leffler static int
14538a1b9b6aSSam Leffler rsn_keymgmt(u_int8_t *sel)
14548a1b9b6aSSam Leffler {
14558a1b9b6aSSam Leffler #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
14568a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
14578a1b9b6aSSam Leffler 
14588a1b9b6aSSam Leffler 	switch (w) {
14598a1b9b6aSSam Leffler 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
14608a1b9b6aSSam Leffler 		return RSN_ASE_8021X_UNSPEC;
14618a1b9b6aSSam Leffler 	case RSN_SEL(RSN_ASE_8021X_PSK):
14628a1b9b6aSSam Leffler 		return RSN_ASE_8021X_PSK;
14638a1b9b6aSSam Leffler 	case RSN_SEL(RSN_ASE_NONE):
14648a1b9b6aSSam Leffler 		return RSN_ASE_NONE;
14658a1b9b6aSSam Leffler 	}
14668a1b9b6aSSam Leffler 	return 0;		/* NB: so is discarded */
14678a1b9b6aSSam Leffler #undef RSN_SEL
14688a1b9b6aSSam Leffler }
14698a1b9b6aSSam Leffler 
14708a1b9b6aSSam Leffler /*
14718a1b9b6aSSam Leffler  * Parse a WPA/RSN information element to collect parameters
14728a1b9b6aSSam Leffler  * and validate the parameters against what has been
14738a1b9b6aSSam Leffler  * configured for the system.
14748a1b9b6aSSam Leffler  */
14758a1b9b6aSSam Leffler static int
14768a1b9b6aSSam Leffler ieee80211_parse_rsn(struct ieee80211com *ic, u_int8_t *frm,
14778a1b9b6aSSam Leffler 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
14788a1b9b6aSSam Leffler {
14798a1b9b6aSSam Leffler 	u_int8_t len = frm[1];
14808a1b9b6aSSam Leffler 	u_int32_t w;
14818a1b9b6aSSam Leffler 	int n;
14828a1b9b6aSSam Leffler 
14838a1b9b6aSSam Leffler 	/*
14848a1b9b6aSSam Leffler 	 * Check the length once for fixed parts:
14858a1b9b6aSSam Leffler 	 * version, mcast cipher, and 2 selector counts.
14868a1b9b6aSSam Leffler 	 * Other, variable-length data, must be checked separately.
14878a1b9b6aSSam Leffler 	 */
14888a1b9b6aSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_WPA2,
14898a1b9b6aSSam Leffler 		("not RSN, flags 0x%x", ic->ic_flags));
14908a1b9b6aSSam Leffler 	if (len < 10) {
14918a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14928a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14938a1b9b6aSSam Leffler 		    wh, "RSN", "too short, len %u", len);
14948a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14958a1b9b6aSSam Leffler 	}
14968a1b9b6aSSam Leffler 	frm += 2;
14978a1b9b6aSSam Leffler 	w = LE_READ_2(frm);
14988a1b9b6aSSam Leffler 	if (w != RSN_VERSION) {
14998a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15008a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15018a1b9b6aSSam Leffler 		    wh, "RSN", "bad version %u", w);
15028a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15038a1b9b6aSSam Leffler 	}
15048a1b9b6aSSam Leffler 	frm += 2, len -= 2;
15058a1b9b6aSSam Leffler 
15068a1b9b6aSSam Leffler 	/* multicast/group cipher */
15078a1b9b6aSSam Leffler 	w = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
15088a1b9b6aSSam Leffler 	if (w != rsn->rsn_mcastcipher) {
15098a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15108a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15118a1b9b6aSSam Leffler 		    wh, "RSN", "mcast cipher mismatch; got %u, expected %u",
15128a1b9b6aSSam Leffler 		    w, rsn->rsn_mcastcipher);
15138a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15148a1b9b6aSSam Leffler 	}
15158a1b9b6aSSam Leffler 	frm += 4, len -= 4;
15168a1b9b6aSSam Leffler 
15178a1b9b6aSSam Leffler 	/* unicast ciphers */
15188a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
15198a1b9b6aSSam Leffler 	frm += 2, len -= 2;
15208a1b9b6aSSam Leffler 	if (len < n*4+2) {
15218a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15228a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15238a1b9b6aSSam Leffler 		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
15248a1b9b6aSSam Leffler 		    len, n);
15258a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15268a1b9b6aSSam Leffler 	}
15278a1b9b6aSSam Leffler 	w = 0;
15288a1b9b6aSSam Leffler 	for (; n > 0; n--) {
15298a1b9b6aSSam Leffler 		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
15308a1b9b6aSSam Leffler 		frm += 4, len -= 4;
15318a1b9b6aSSam Leffler 	}
15328a1b9b6aSSam Leffler 	w &= rsn->rsn_ucastcipherset;
15338a1b9b6aSSam Leffler 	if (w == 0) {
15348a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15358a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15368a1b9b6aSSam Leffler 		    wh, "RSN", "%s", "ucast cipher set empty");
15378a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15388a1b9b6aSSam Leffler 	}
15398a1b9b6aSSam Leffler 	if (w & (1<<IEEE80211_CIPHER_TKIP))
15408a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
15418a1b9b6aSSam Leffler 	else
15428a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
15438a1b9b6aSSam Leffler 
15448a1b9b6aSSam Leffler 	/* key management algorithms */
15458a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
15468a1b9b6aSSam Leffler 	frm += 2, len -= 2;
15478a1b9b6aSSam Leffler 	if (len < n*4) {
15488a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15498a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15508a1b9b6aSSam Leffler 		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
15518a1b9b6aSSam Leffler 		    len, n);
15528a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15538a1b9b6aSSam Leffler 	}
15548a1b9b6aSSam Leffler 	w = 0;
15558a1b9b6aSSam Leffler 	for (; n > 0; n--) {
15568a1b9b6aSSam Leffler 		w |= rsn_keymgmt(frm);
15578a1b9b6aSSam Leffler 		frm += 4, len -= 4;
15588a1b9b6aSSam Leffler 	}
15598a1b9b6aSSam Leffler 	w &= rsn->rsn_keymgmtset;
15608a1b9b6aSSam Leffler 	if (w == 0) {
15618a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15628a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15638a1b9b6aSSam Leffler 		    wh, "RSN", "%s", "no acceptable key mgmt alg");
15648a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15658a1b9b6aSSam Leffler 	}
15668a1b9b6aSSam Leffler 	if (w & RSN_ASE_8021X_UNSPEC)
15678a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
15688a1b9b6aSSam Leffler 	else
15698a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
15708a1b9b6aSSam Leffler 
15718a1b9b6aSSam Leffler 	/* optional RSN capabilities */
15728a1b9b6aSSam Leffler 	if (len > 2)
15738a1b9b6aSSam Leffler 		rsn->rsn_caps = LE_READ_2(frm);
15748a1b9b6aSSam Leffler 	/* XXXPMKID */
15758a1b9b6aSSam Leffler 
15768a1b9b6aSSam Leffler 	return 0;
15778a1b9b6aSSam Leffler }
15788a1b9b6aSSam Leffler 
15798a1b9b6aSSam Leffler static int
15808a1b9b6aSSam Leffler ieee80211_parse_wmeparams(struct ieee80211com *ic, u_int8_t *frm,
15818a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh)
15828a1b9b6aSSam Leffler {
15838a1b9b6aSSam Leffler #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
15848a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
15858a1b9b6aSSam Leffler 	u_int len = frm[1], qosinfo;
15868a1b9b6aSSam Leffler 	int i;
15878a1b9b6aSSam Leffler 
15888a1b9b6aSSam Leffler 	if (len < sizeof(struct ieee80211_wme_param)-2) {
15898a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15908a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
15918a1b9b6aSSam Leffler 		    wh, "WME", "too short, len %u", len);
1592c0fa32ceSSam Leffler 		return -1;
15938a1b9b6aSSam Leffler 	}
15948a1b9b6aSSam Leffler 	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
15958a1b9b6aSSam Leffler 	qosinfo &= WME_QOSINFO_COUNT;
15968a1b9b6aSSam Leffler 	/* XXX do proper check for wraparound */
15978a1b9b6aSSam Leffler 	if (qosinfo == wme->wme_wmeChanParams.cap_info)
15988a1b9b6aSSam Leffler 		return 0;
15998a1b9b6aSSam Leffler 	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
16008a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
16018a1b9b6aSSam Leffler 		struct wmeParams *wmep =
16028a1b9b6aSSam Leffler 			&wme->wme_wmeChanParams.cap_wmeParams[i];
16038a1b9b6aSSam Leffler 		/* NB: ACI not used */
16048a1b9b6aSSam Leffler 		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
16058a1b9b6aSSam Leffler 		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
16068a1b9b6aSSam Leffler 		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
16078a1b9b6aSSam Leffler 		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
16088a1b9b6aSSam Leffler 		wmep->wmep_txopLimit = LE_READ_2(frm+2);
16098a1b9b6aSSam Leffler 		frm += 4;
16108a1b9b6aSSam Leffler 	}
16118a1b9b6aSSam Leffler 	wme->wme_wmeChanParams.cap_info = qosinfo;
16128a1b9b6aSSam Leffler 	return 1;
16138a1b9b6aSSam Leffler #undef MS
16148a1b9b6aSSam Leffler }
16158a1b9b6aSSam Leffler 
16168a1b9b6aSSam Leffler static void
16178a1b9b6aSSam Leffler ieee80211_saveie(u_int8_t **iep, const u_int8_t *ie)
16188a1b9b6aSSam Leffler {
16198a1b9b6aSSam Leffler 	u_int ielen = ie[1]+2;
16208a1b9b6aSSam Leffler 	/*
16218a1b9b6aSSam Leffler 	 * Record information element for later use.
16228a1b9b6aSSam Leffler 	 */
16238a1b9b6aSSam Leffler 	if (*iep == NULL || (*iep)[1] != ie[1]) {
16248a1b9b6aSSam Leffler 		if (*iep != NULL)
16258a1b9b6aSSam Leffler 			FREE(*iep, M_DEVBUF);
16268a1b9b6aSSam Leffler 		MALLOC(*iep, void*, ielen, M_DEVBUF, M_NOWAIT);
16278a1b9b6aSSam Leffler 	}
16288a1b9b6aSSam Leffler 	if (*iep != NULL)
16298a1b9b6aSSam Leffler 		memcpy(*iep, ie, ielen);
16308a1b9b6aSSam Leffler 	/* XXX note failure */
16318a1b9b6aSSam Leffler }
16328a1b9b6aSSam Leffler 
16338a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
16348a1b9b6aSSam Leffler static void
16358a1b9b6aSSam Leffler dump_probe_beacon(u_int8_t subtype, int isnew,
16368a1b9b6aSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN],
16378a1b9b6aSSam Leffler 	u_int8_t chan, u_int8_t bchan, u_int16_t capinfo, u_int16_t bintval,
16388a1b9b6aSSam Leffler 	u_int8_t erp, u_int8_t *ssid, u_int8_t *country)
16398a1b9b6aSSam Leffler {
16408a1b9b6aSSam Leffler 	printf("[%s] %s%s on chan %u (bss chan %u) ",
16418a1b9b6aSSam Leffler 	    ether_sprintf(mac), isnew ? "new " : "",
16428a1b9b6aSSam Leffler 	    ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
16438a1b9b6aSSam Leffler 	    chan, bchan);
16448a1b9b6aSSam Leffler 	ieee80211_print_essid(ssid + 2, ssid[1]);
16458a1b9b6aSSam Leffler 	printf("\n");
16468a1b9b6aSSam Leffler 
16478a1b9b6aSSam Leffler 	if (isnew) {
16488a1b9b6aSSam Leffler 		printf("[%s] caps 0x%x bintval %u erp 0x%x",
16498a1b9b6aSSam Leffler 			ether_sprintf(mac), capinfo, bintval, erp);
16508a1b9b6aSSam Leffler 		if (country) {
16518a1b9b6aSSam Leffler #ifdef __FreeBSD__
16528a1b9b6aSSam Leffler 			printf(" country info %*D", country[1], country+2, " ");
16538a1b9b6aSSam Leffler #else
16548a1b9b6aSSam Leffler 			int i;
16558a1b9b6aSSam Leffler 			printf(" country info");
16568a1b9b6aSSam Leffler 			for (i = 0; i < country[1]; i++)
16578a1b9b6aSSam Leffler 				printf(" %02x", country[i+2]);
16588a1b9b6aSSam Leffler #endif
16598a1b9b6aSSam Leffler 		}
16608a1b9b6aSSam Leffler 		printf("\n");
16618a1b9b6aSSam Leffler 	}
16628a1b9b6aSSam Leffler }
16638a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
16648a1b9b6aSSam Leffler 
16651a1e1d21SSam Leffler void
16660a915fadSSam Leffler ieee80211_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
16670a915fadSSam Leffler 	struct ieee80211_node *ni,
16680a915fadSSam Leffler 	int subtype, int rssi, u_int32_t rstamp)
16691a1e1d21SSam Leffler {
16708a1b9b6aSSam Leffler #define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
16718a1b9b6aSSam Leffler #define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
16721a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
16731a1e1d21SSam Leffler 	u_int8_t *frm, *efrm;
16748a1b9b6aSSam Leffler 	u_int8_t *ssid, *rates, *xrates, *wpa, *wme;
16758a1b9b6aSSam Leffler 	int reassoc, resp, allocbs;
16761a1e1d21SSam Leffler 
16771a1e1d21SSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
16781a1e1d21SSam Leffler 	frm = (u_int8_t *)&wh[1];
16791a1e1d21SSam Leffler 	efrm = mtod(m0, u_int8_t *) + m0->m_len;
16801a1e1d21SSam Leffler 	switch (subtype) {
16811a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
16821a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON: {
16838a1b9b6aSSam Leffler 		u_int8_t *tstamp, *country;
16841a1e1d21SSam Leffler 		u_int8_t chan, bchan, fhindex, erp;
16858a1b9b6aSSam Leffler 		u_int16_t capinfo, bintval, timoff;
16861a1e1d21SSam Leffler 		u_int16_t fhdwell;
16871a1e1d21SSam Leffler 
16888a1b9b6aSSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
16898a1b9b6aSSam Leffler 			/*
16908a1b9b6aSSam Leffler 			 * Count beacon frames specially, some drivers
16918a1b9b6aSSam Leffler 			 * use this info to do things like update LED's.
16928a1b9b6aSSam Leffler 			 */
16938a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_beacon++;
16948a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_beacons);
16958a1b9b6aSSam Leffler 		} else
16968a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_proberesp);
16978a1b9b6aSSam Leffler 		/*
16988a1b9b6aSSam Leffler 		 * We process beacon/probe response frames:
16998a1b9b6aSSam Leffler 		 *    o when scanning, or
17008a1b9b6aSSam Leffler 		 *    o station mode when associated (to collect state
17018a1b9b6aSSam Leffler 		 *      updates such as 802.11g slot time), or
17028a1b9b6aSSam Leffler 		 *    o adhoc mode (to discover neighbors)
17038a1b9b6aSSam Leffler 		 * Frames otherwise received are discarded.
17048a1b9b6aSSam Leffler 		 */
17058a1b9b6aSSam Leffler 		if (!((ic->ic_flags & IEEE80211_F_SCAN) ||
17068a1b9b6aSSam Leffler 		      (ic->ic_opmode == IEEE80211_M_STA && ni->ni_associd) ||
17078a1b9b6aSSam Leffler 		       ic->ic_opmode != IEEE80211_M_IBSS)) {
17088a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
17091a1e1d21SSam Leffler 			return;
17101a1e1d21SSam Leffler 		}
17111a1e1d21SSam Leffler 		/*
17121a1e1d21SSam Leffler 		 * beacon/probe response frame format
17131a1e1d21SSam Leffler 		 *	[8] time stamp
17141a1e1d21SSam Leffler 		 *	[2] beacon interval
17151a1e1d21SSam Leffler 		 *	[2] capability information
17161a1e1d21SSam Leffler 		 *	[tlv] ssid
17171a1e1d21SSam Leffler 		 *	[tlv] supported rates
17181a1e1d21SSam Leffler 		 *	[tlv] country information
17191a1e1d21SSam Leffler 		 *	[tlv] parameter set (FH/DS)
17201a1e1d21SSam Leffler 		 *	[tlv] erp information
17211a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
17228a1b9b6aSSam Leffler 		 *	[tlv] WME
17238a1b9b6aSSam Leffler 		 *	[tlv] WPA or RSN
17241a1e1d21SSam Leffler 		 */
17251a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 12);
17261a1e1d21SSam Leffler 		tstamp  = frm;				frm += 8;
17278a1b9b6aSSam Leffler 		bintval = le16toh(*(u_int16_t *)frm);	frm += 2;
17288a1b9b6aSSam Leffler 		capinfo = le16toh(*(u_int16_t *)frm);	frm += 2;
17298a1b9b6aSSam Leffler 		ssid = rates = xrates = country = wpa = wme = NULL;
17301a1e1d21SSam Leffler 		bchan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
17311a1e1d21SSam Leffler 		chan = bchan;
17321a1e1d21SSam Leffler 		fhdwell = 0;
17331a1e1d21SSam Leffler 		fhindex = 0;
17341a1e1d21SSam Leffler 		erp = 0;
17358a1b9b6aSSam Leffler 		timoff = 0;
17361a1e1d21SSam Leffler 		while (frm < efrm) {
17371a1e1d21SSam Leffler 			switch (*frm) {
17381a1e1d21SSam Leffler 			case IEEE80211_ELEMID_SSID:
17391a1e1d21SSam Leffler 				ssid = frm;
17401a1e1d21SSam Leffler 				break;
17411a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
17421a1e1d21SSam Leffler 				rates = frm;
17431a1e1d21SSam Leffler 				break;
17441a1e1d21SSam Leffler 			case IEEE80211_ELEMID_COUNTRY:
17451a1e1d21SSam Leffler 				country = frm;
17461a1e1d21SSam Leffler 				break;
17471a1e1d21SSam Leffler 			case IEEE80211_ELEMID_FHPARMS:
17481a1e1d21SSam Leffler 				if (ic->ic_phytype == IEEE80211_T_FH) {
17498a1b9b6aSSam Leffler 					fhdwell = LE_READ_2(&frm[2]);
17501a1e1d21SSam Leffler 					chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
17511a1e1d21SSam Leffler 					fhindex = frm[6];
17521a1e1d21SSam Leffler 				}
17531a1e1d21SSam Leffler 				break;
17541a1e1d21SSam Leffler 			case IEEE80211_ELEMID_DSPARMS:
17551a1e1d21SSam Leffler 				/*
17561a1e1d21SSam Leffler 				 * XXX hack this since depending on phytype
17571a1e1d21SSam Leffler 				 * is problematic for multi-mode devices.
17581a1e1d21SSam Leffler 				 */
17591a1e1d21SSam Leffler 				if (ic->ic_phytype != IEEE80211_T_FH)
17601a1e1d21SSam Leffler 					chan = frm[2];
17611a1e1d21SSam Leffler 				break;
17621a1e1d21SSam Leffler 			case IEEE80211_ELEMID_TIM:
17638a1b9b6aSSam Leffler 				/* XXX ATIM? */
17648a1b9b6aSSam Leffler 				timoff = frm - mtod(m0, u_int8_t *);
17651a1e1d21SSam Leffler 				break;
17664bd067c5SSam Leffler 			case IEEE80211_ELEMID_IBSSPARMS:
17674bd067c5SSam Leffler 				break;
17681a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
17691a1e1d21SSam Leffler 				xrates = frm;
17701a1e1d21SSam Leffler 				break;
17711a1e1d21SSam Leffler 			case IEEE80211_ELEMID_ERP:
17721a1e1d21SSam Leffler 				if (frm[1] != 1) {
17738a1b9b6aSSam Leffler 					IEEE80211_DISCARD_IE(ic,
17748a1b9b6aSSam Leffler 					    IEEE80211_MSG_ELEMID, wh, "ERP",
17758a1b9b6aSSam Leffler 					    "bad len %u", frm[1]);
17761be50176SSam Leffler 					ic->ic_stats.is_rx_elem_toobig++;
17771a1e1d21SSam Leffler 					break;
17781a1e1d21SSam Leffler 				}
17791a1e1d21SSam Leffler 				erp = frm[2];
17801a1e1d21SSam Leffler 				break;
17818a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_RSN:
17828a1b9b6aSSam Leffler 				wpa = frm;
17838a1b9b6aSSam Leffler 				break;
17848a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
17858a1b9b6aSSam Leffler 				if (iswpaoui(frm))
17868a1b9b6aSSam Leffler 					wpa = frm;
17878a1b9b6aSSam Leffler 				else if (iswmeparam(frm) || iswmeinfo(frm))
17888a1b9b6aSSam Leffler 					wme = frm;
17898a1b9b6aSSam Leffler 				/* XXX Atheros OUI support */
17908a1b9b6aSSam Leffler 				break;
17911a1e1d21SSam Leffler 			default:
17928a1b9b6aSSam Leffler 				IEEE80211_DISCARD_IE(ic, IEEE80211_MSG_ELEMID,
17938a1b9b6aSSam Leffler 				    wh, "unhandled",
17948a1b9b6aSSam Leffler 				    "id %u, len %u", *frm, frm[1]);
17951be50176SSam Leffler 				ic->ic_stats.is_rx_elem_unknown++;
17961a1e1d21SSam Leffler 				break;
17971a1e1d21SSam Leffler 			}
17981a1e1d21SSam Leffler 			frm += frm[1] + 2;
17991a1e1d21SSam Leffler 		}
1800dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
18011a1e1d21SSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
1802a11c9a5cSSam Leffler 		if (
1803a11c9a5cSSam Leffler #if IEEE80211_CHAN_MAX < 255
1804a11c9a5cSSam Leffler 		    chan > IEEE80211_CHAN_MAX ||
1805a11c9a5cSSam Leffler #endif
1806a11c9a5cSSam Leffler 		    isclr(ic->ic_chan_active, chan)) {
18078a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,
18088a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
18098a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
18108a1b9b6aSSam Leffler 			    "invalid channel %u", chan);
18111be50176SSam Leffler 			ic->ic_stats.is_rx_badchan++;
18121a1e1d21SSam Leffler 			return;
18131a1e1d21SSam Leffler 		}
18144844aa7dSAtsushi Onoe 		if (chan != bchan && ic->ic_phytype != IEEE80211_T_FH) {
18151a1e1d21SSam Leffler 			/*
18161a1e1d21SSam Leffler 			 * Frame was received on a channel different from the
18174844aa7dSAtsushi Onoe 			 * one indicated in the DS params element id;
18181a1e1d21SSam Leffler 			 * silently discard it.
18191a1e1d21SSam Leffler 			 *
18201a1e1d21SSam Leffler 			 * NB: this can happen due to signal leakage.
18214844aa7dSAtsushi Onoe 			 *     But we should take it for FH phy because
18224844aa7dSAtsushi Onoe 			 *     the rssi value should be correct even for
18234844aa7dSAtsushi Onoe 			 *     different hop pattern in FH.
18241a1e1d21SSam Leffler 			 */
18258a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,
18268a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
18278a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
18286b312781SSam Leffler 			    "for off-channel %u", chan);
18291be50176SSam Leffler 			ic->ic_stats.is_rx_chanmismatch++;
18301a1e1d21SSam Leffler 			return;
18311a1e1d21SSam Leffler 		}
18321a1e1d21SSam Leffler 
18331a1e1d21SSam Leffler 		/*
18348a1b9b6aSSam Leffler 		 * When operating in station mode, check for state updates.
18358a1b9b6aSSam Leffler 		 * Be careful to ignore beacons received while doing a
18368a1b9b6aSSam Leffler 		 * background scan.  We consider only 11g/WMM stuff right now.
18371a1e1d21SSam Leffler 		 */
18388a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA &&
18398a1b9b6aSSam Leffler 		    ni->ni_associd != 0 &&
18408a1b9b6aSSam Leffler 		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
18418a1b9b6aSSam Leffler 		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
18428a1b9b6aSSam Leffler 			if (ni->ni_erp != erp) {
18438a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
18448a1b9b6aSSam Leffler 				    "[%s] erp change: was 0x%x, now 0x%x\n",
18458a1b9b6aSSam Leffler 				    ether_sprintf(wh->i_addr2),
18468a1b9b6aSSam Leffler 				    ni->ni_erp, erp);
18478a1b9b6aSSam Leffler 				if (erp & IEEE80211_ERP_USE_PROTECTION)
18488a1b9b6aSSam Leffler 					ic->ic_flags |= IEEE80211_F_USEPROT;
18498a1b9b6aSSam Leffler 				else
18508a1b9b6aSSam Leffler 					ic->ic_flags &= ~IEEE80211_F_USEPROT;
18518a1b9b6aSSam Leffler 				ni->ni_erp = erp;
18528a1b9b6aSSam Leffler 				/* XXX statistic */
18531a1e1d21SSam Leffler 			}
18548a1b9b6aSSam Leffler 			if ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
18558a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
18568a1b9b6aSSam Leffler 				    "[%s] capabilities change: before 0x%x,"
18578a1b9b6aSSam Leffler 				     " now 0x%x\n",
18588a1b9b6aSSam Leffler 				     ether_sprintf(wh->i_addr2),
18598a1b9b6aSSam Leffler 				     ni->ni_capinfo, capinfo);
18608a1b9b6aSSam Leffler 				/*
18618a1b9b6aSSam Leffler 				 * NB: we assume short preamble doesn't
18628a1b9b6aSSam Leffler 				 *     change dynamically
18638a1b9b6aSSam Leffler 				 */
18648a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic,
18658a1b9b6aSSam Leffler 					ic->ic_curmode == IEEE80211_MODE_11A ||
18668a1b9b6aSSam Leffler 					(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
18678a1b9b6aSSam Leffler 				ni->ni_capinfo = capinfo;
18688a1b9b6aSSam Leffler 				/* XXX statistic */
18698a1b9b6aSSam Leffler 			}
18708a1b9b6aSSam Leffler 			if (wme != NULL &&
1871c0fa32ceSSam Leffler 			    ieee80211_parse_wmeparams(ic, wme, wh) > 0)
18728a1b9b6aSSam Leffler 				ieee80211_wme_updateparams(ic);
18738a1b9b6aSSam Leffler 			/* NB: don't need the rest of this */
18748a1b9b6aSSam Leffler 			return;
18758a1b9b6aSSam Leffler 		}
18768a1b9b6aSSam Leffler 
18778a1b9b6aSSam Leffler 		if (ni == ic->ic_bss) {
18788a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
18798a1b9b6aSSam Leffler 			if (ieee80211_msg_scan(ic))
18808a1b9b6aSSam Leffler 				dump_probe_beacon(subtype, 1,
18818a1b9b6aSSam Leffler 				    wh->i_addr2, chan, bchan, capinfo,
18828a1b9b6aSSam Leffler 				    bintval, erp, ssid, country);
18831a1e1d21SSam Leffler #endif
18848a1b9b6aSSam Leffler 			/*
18858a1b9b6aSSam Leffler 			 * Create a new entry.  If scanning the entry goes
18868a1b9b6aSSam Leffler 			 * in the scan cache.  Otherwise, be particular when
18878a1b9b6aSSam Leffler 			 * operating in adhoc mode--only take nodes marked
18888a1b9b6aSSam Leffler 			 * as ibss participants so we don't populate our
18898a1b9b6aSSam Leffler 			 * neighbor table with unintersting sta's.
18908a1b9b6aSSam Leffler 			 */
18918a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
18928a1b9b6aSSam Leffler 				if ((capinfo & IEEE80211_CAPINFO_IBSS) == 0)
18938a1b9b6aSSam Leffler 					return;
18948a1b9b6aSSam Leffler 				ni = ieee80211_fakeup_adhoc_node(ic->ic_sta,
18958a1b9b6aSSam Leffler 						wh->i_addr2);
18968a1b9b6aSSam Leffler 			} else
18978a1b9b6aSSam Leffler 				ni = ieee80211_dup_bss(&ic->ic_scan, wh->i_addr2);
1898c64bfa0fSSam Leffler 			if (ni == NULL)
18991a1e1d21SSam Leffler 				return;
19001a1e1d21SSam Leffler 			ni->ni_esslen = ssid[1];
19011a1e1d21SSam Leffler 			memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
19021a1e1d21SSam Leffler 			memcpy(ni->ni_essid, ssid + 2, ssid[1]);
19038a1b9b6aSSam Leffler 		} else if (ssid[1] != 0 &&
19048a1b9b6aSSam Leffler 		    (ISPROBE(subtype) || ni->ni_esslen == 0)) {
19051a1e1d21SSam Leffler 			/*
19068a1b9b6aSSam Leffler 			 * Update ESSID at probe response to adopt
19078a1b9b6aSSam Leffler 			 * hidden AP by Lucent/Cisco, which announces
19088a1b9b6aSSam Leffler 			 * null ESSID in beacon.
19091a1e1d21SSam Leffler 			 */
19108a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
19118a1b9b6aSSam Leffler 			if (ieee80211_msg_scan(ic) ||
19128a1b9b6aSSam Leffler 			    ieee80211_msg_debug(ic))
19138a1b9b6aSSam Leffler 				dump_probe_beacon(subtype, 0,
19148a1b9b6aSSam Leffler 				    wh->i_addr2, chan, bchan, capinfo,
19158a1b9b6aSSam Leffler 				    bintval, erp, ssid, country);
19168a1b9b6aSSam Leffler #endif
19171a1e1d21SSam Leffler 			ni->ni_esslen = ssid[1];
19181a1e1d21SSam Leffler 			memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
19191a1e1d21SSam Leffler 			memcpy(ni->ni_essid, ssid + 2, ssid[1]);
19208a1b9b6aSSam Leffler 		}
19218a1b9b6aSSam Leffler 		ni->ni_scangen = ic->ic_scan.nt_scangen;
19221a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
19231a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
19241a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
19258a1b9b6aSSam Leffler 		memcpy(ni->ni_tstamp.data, tstamp, sizeof(ni->ni_tstamp));
19268a1b9b6aSSam Leffler 		ni->ni_intval = bintval;
19278a1b9b6aSSam Leffler 		ni->ni_capinfo = capinfo;
19281a1e1d21SSam Leffler 		ni->ni_chan = &ic->ic_channels[chan];
19291a1e1d21SSam Leffler 		ni->ni_fhdwell = fhdwell;
19301a1e1d21SSam Leffler 		ni->ni_fhindex = fhindex;
19311a1e1d21SSam Leffler 		ni->ni_erp = erp;
19328a1b9b6aSSam Leffler 		/*
19338a1b9b6aSSam Leffler 		 * Record the byte offset from the mac header to
19348a1b9b6aSSam Leffler 		 * the start of the TIM information element for
19358a1b9b6aSSam Leffler 		 * use by hardware and/or to speedup software
19368a1b9b6aSSam Leffler 		 * processing of beacon frames.
19378a1b9b6aSSam Leffler 		 */
19388a1b9b6aSSam Leffler 		ni->ni_timoff = timoff;
19398a1b9b6aSSam Leffler 		/*
19408a1b9b6aSSam Leffler 		 * Record optional information elements that might be
19418a1b9b6aSSam Leffler 		 * used by applications or drivers.
19428a1b9b6aSSam Leffler 		 */
19438a1b9b6aSSam Leffler 		if (wme != NULL)
19448a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wme_ie, wme);
19458a1b9b6aSSam Leffler 		if (wpa != NULL)
19468a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wpa_ie, wpa);
19471a1e1d21SSam Leffler 		/* NB: must be after ni_chan is setup */
19481a1e1d21SSam Leffler 		ieee80211_setup_rates(ic, ni, rates, xrates, IEEE80211_F_DOSORT);
19491a1e1d21SSam Leffler 		break;
19501a1e1d21SSam Leffler 	}
19511a1e1d21SSam Leffler 
19521a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ: {
19531a1e1d21SSam Leffler 		u_int8_t rate;
19541a1e1d21SSam Leffler 
19558a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA ||
19568a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_RUN) {
19578a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
19581a1e1d21SSam Leffler 			return;
19598a1b9b6aSSam Leffler 		}
19608a1b9b6aSSam Leffler 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
19618a1b9b6aSSam Leffler 			/* frame must be directed */
19628a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;	/* XXX stat */
19631a1e1d21SSam Leffler 			return;
19648a1b9b6aSSam Leffler 		}
19651a1e1d21SSam Leffler 
19661a1e1d21SSam Leffler 		/*
19671a1e1d21SSam Leffler 		 * prreq frame format
19681a1e1d21SSam Leffler 		 *	[tlv] ssid
19691a1e1d21SSam Leffler 		 *	[tlv] supported rates
19701a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
19711a1e1d21SSam Leffler 		 */
19721a1e1d21SSam Leffler 		ssid = rates = xrates = NULL;
19731a1e1d21SSam Leffler 		while (frm < efrm) {
19741a1e1d21SSam Leffler 			switch (*frm) {
19751a1e1d21SSam Leffler 			case IEEE80211_ELEMID_SSID:
19761a1e1d21SSam Leffler 				ssid = frm;
19771a1e1d21SSam Leffler 				break;
19781a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
19791a1e1d21SSam Leffler 				rates = frm;
19801a1e1d21SSam Leffler 				break;
19811a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
19821a1e1d21SSam Leffler 				xrates = frm;
19831a1e1d21SSam Leffler 				break;
19841a1e1d21SSam Leffler 			}
19851a1e1d21SSam Leffler 			frm += frm[1] + 2;
19861a1e1d21SSam Leffler 		}
1987dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
19881a1e1d21SSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
19898a1b9b6aSSam Leffler 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid);
19901a1e1d21SSam Leffler 
19910a915fadSSam Leffler 		if (ni == ic->ic_bss) {
19928a1b9b6aSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_IBSS) {
19938a1b9b6aSSam Leffler 				/*
19948a1b9b6aSSam Leffler 				 * XXX Cannot tell if the sender is operating
19958a1b9b6aSSam Leffler 				 * in ibss mode.  But we need a new node to
19968a1b9b6aSSam Leffler 				 * send the response so blindly add them to the
19978a1b9b6aSSam Leffler 				 * neighbor table.
19988a1b9b6aSSam Leffler 				 */
19998a1b9b6aSSam Leffler 				ni = ieee80211_fakeup_adhoc_node(ic->ic_sta,
20008a1b9b6aSSam Leffler 					wh->i_addr2);
20018a1b9b6aSSam Leffler 			} else
20028a1b9b6aSSam Leffler 				ni = ieee80211_dup_bss(ic->ic_sta, wh->i_addr2);
2003c64bfa0fSSam Leffler 			if (ni == NULL)
20041a1e1d21SSam Leffler 				return;
20051a1e1d21SSam Leffler 			allocbs = 1;
20061a1e1d21SSam Leffler 		} else
20071a1e1d21SSam Leffler 			allocbs = 0;
20088a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
20098a1b9b6aSSam Leffler 		    "[%s] recv probe req\n", ether_sprintf(wh->i_addr2));
20101a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
20111a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
20121a1e1d21SSam Leffler 		rate = ieee80211_setup_rates(ic, ni, rates, xrates,
20131a1e1d21SSam Leffler 			  IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE
20141a1e1d21SSam Leffler 			| IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
20151a1e1d21SSam Leffler 		if (rate & IEEE80211_RATE_BASIC) {
20168a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_XRATE,
20178a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
20188a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
20198a1b9b6aSSam Leffler 			    "%s", "recv'd rate set invalid");
20201a1e1d21SSam Leffler 		} else {
20210a915fadSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni,
20220a915fadSSam Leffler 				IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
20231a1e1d21SSam Leffler 		}
20248a1b9b6aSSam Leffler 		if (allocbs && ic->ic_opmode != IEEE80211_M_IBSS) {
20258a1b9b6aSSam Leffler 			/* reclaim immediately */
20268a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
20278a1b9b6aSSam Leffler 		}
20281a1e1d21SSam Leffler 		break;
20291a1e1d21SSam Leffler 	}
20301a1e1d21SSam Leffler 
20311a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH: {
20321a1e1d21SSam Leffler 		u_int16_t algo, seq, status;
20331a1e1d21SSam Leffler 		/*
20341a1e1d21SSam Leffler 		 * auth frame format
20351a1e1d21SSam Leffler 		 *	[2] algorithm
20361a1e1d21SSam Leffler 		 *	[2] sequence
20371a1e1d21SSam Leffler 		 *	[2] status
20381a1e1d21SSam Leffler 		 *	[tlv*] challenge
20391a1e1d21SSam Leffler 		 */
20401a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
20411a1e1d21SSam Leffler 		algo   = le16toh(*(u_int16_t *)frm);
20421a1e1d21SSam Leffler 		seq    = le16toh(*(u_int16_t *)(frm + 2));
20431a1e1d21SSam Leffler 		status = le16toh(*(u_int16_t *)(frm + 4));
20448a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
20458a1b9b6aSSam Leffler 		    "[%s] recv auth frame with algorithm %d seq %d\n",
20468a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr2), algo, seq);
20478a1b9b6aSSam Leffler 		/*
20488a1b9b6aSSam Leffler 		 * Consult the ACL policy module if setup.
20498a1b9b6aSSam Leffler 		 */
20508a1b9b6aSSam Leffler 		if (ic->ic_acl != NULL &&
20518a1b9b6aSSam Leffler 		    !ic->ic_acl->iac_check(ic, wh->i_addr2)) {
20528a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ACL,
20538a1b9b6aSSam Leffler 			    wh, "auth", "%s", "disallowed by ACL");
20548a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_acl++;
20551a1e1d21SSam Leffler 			return;
20561a1e1d21SSam Leffler 		}
20578a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_COUNTERM) {
20588a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic,
20598a1b9b6aSSam Leffler 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
20608a1b9b6aSSam Leffler 			    wh, "auth", "%s", "TKIP countermeasures enabled");
20618a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_countermeasures++;
20628a1b9b6aSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
20630a915fadSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
20648a1b9b6aSSam Leffler 					IEEE80211_FC0_SUBTYPE_AUTH,
20658a1b9b6aSSam Leffler 					IEEE80211_REASON_MIC_FAILURE);
206659571d2bSSam Leffler 			}
20671a1e1d21SSam Leffler 			return;
20681a1e1d21SSam Leffler 		}
20698a1b9b6aSSam Leffler 		if (algo == IEEE80211_AUTH_ALG_SHARED)
20708a1b9b6aSSam Leffler 			ieee80211_auth_shared(ic, wh, frm + 6, efrm, ni, rssi,
20718a1b9b6aSSam Leffler 			    rstamp, seq, status);
20728a1b9b6aSSam Leffler 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
20738a1b9b6aSSam Leffler 			ieee80211_auth_open(ic, wh, ni, rssi, rstamp, seq,
20748a1b9b6aSSam Leffler 			    status);
20758a1b9b6aSSam Leffler 		else {
20768a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
20778a1b9b6aSSam Leffler 			    wh, "auth", "unsupported alg %d", algo);
20788a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_unsupported++;
20798a1b9b6aSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
20808a1b9b6aSSam Leffler 				/* XXX not right */
20818a1b9b6aSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
20828a1b9b6aSSam Leffler 					IEEE80211_FC0_SUBTYPE_AUTH,
20838a1b9b6aSSam Leffler 					(seq+1) | (IEEE80211_STATUS_ALG<<16));
20848a1b9b6aSSam Leffler 			}
20858a1b9b6aSSam Leffler 			return;
20861a1e1d21SSam Leffler 		}
20871a1e1d21SSam Leffler 		break;
20881a1e1d21SSam Leffler 	}
20891a1e1d21SSam Leffler 
20901a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
20911a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
20921a1e1d21SSam Leffler 		u_int16_t capinfo, bintval;
20938a1b9b6aSSam Leffler 		struct ieee80211_rsnparms rsn;
20948a1b9b6aSSam Leffler 		u_int8_t reason;
20951a1e1d21SSam Leffler 
20961a1e1d21SSam Leffler 		if (ic->ic_opmode != IEEE80211_M_HOSTAP ||
20978a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_RUN) {
20988a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
20991a1e1d21SSam Leffler 			return;
21008a1b9b6aSSam Leffler 		}
21011a1e1d21SSam Leffler 
21021a1e1d21SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
21031a1e1d21SSam Leffler 			reassoc = 1;
21041a1e1d21SSam Leffler 			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
21051a1e1d21SSam Leffler 		} else {
21061a1e1d21SSam Leffler 			reassoc = 0;
21071a1e1d21SSam Leffler 			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
21081a1e1d21SSam Leffler 		}
21091a1e1d21SSam Leffler 		/*
21101a1e1d21SSam Leffler 		 * asreq frame format
21111a1e1d21SSam Leffler 		 *	[2] capability information
21121a1e1d21SSam Leffler 		 *	[2] listen interval
21131a1e1d21SSam Leffler 		 *	[6*] current AP address (reassoc only)
21141a1e1d21SSam Leffler 		 *	[tlv] ssid
21151a1e1d21SSam Leffler 		 *	[tlv] supported rates
21161a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
21178a1b9b6aSSam Leffler 		 *	[tlv] WPA or RSN
21181a1e1d21SSam Leffler 		 */
21191a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4));
21201a1e1d21SSam Leffler 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_bss->ni_bssid)) {
21218a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
21228a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
21238a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
21248a1b9b6aSSam Leffler 			    "%s", "wrong bssid");
21251be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_bss++;
21261a1e1d21SSam Leffler 			return;
21271a1e1d21SSam Leffler 		}
21281a1e1d21SSam Leffler 		capinfo = le16toh(*(u_int16_t *)frm);	frm += 2;
21291a1e1d21SSam Leffler 		bintval = le16toh(*(u_int16_t *)frm);	frm += 2;
21301a1e1d21SSam Leffler 		if (reassoc)
21311a1e1d21SSam Leffler 			frm += 6;	/* ignore current AP info */
21328a1b9b6aSSam Leffler 		ssid = rates = xrates = wpa = wme = NULL;
21331a1e1d21SSam Leffler 		while (frm < efrm) {
21341a1e1d21SSam Leffler 			switch (*frm) {
21351a1e1d21SSam Leffler 			case IEEE80211_ELEMID_SSID:
21361a1e1d21SSam Leffler 				ssid = frm;
21371a1e1d21SSam Leffler 				break;
21381a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
21391a1e1d21SSam Leffler 				rates = frm;
21401a1e1d21SSam Leffler 				break;
21411a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
21421a1e1d21SSam Leffler 				xrates = frm;
21431a1e1d21SSam Leffler 				break;
21448a1b9b6aSSam Leffler 			/* XXX verify only one of RSN and WPA ie's? */
21458a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_RSN:
21468a1b9b6aSSam Leffler 				wpa = frm;
21478a1b9b6aSSam Leffler 				break;
21488a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
21498a1b9b6aSSam Leffler 				if (iswpaoui(frm)) {
21508a1b9b6aSSam Leffler 					if (ic->ic_flags & IEEE80211_F_WPA1)
21518a1b9b6aSSam Leffler 						wpa = frm;
21528a1b9b6aSSam Leffler 				} else if (iswmeinfo(frm))
21538a1b9b6aSSam Leffler 					wme = frm;
21548a1b9b6aSSam Leffler 				/* XXX Atheros OUI support */
21558a1b9b6aSSam Leffler 				break;
21561a1e1d21SSam Leffler 			}
21571a1e1d21SSam Leffler 			frm += frm[1] + 2;
21581a1e1d21SSam Leffler 		}
2159dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
21601a1e1d21SSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
21618a1b9b6aSSam Leffler 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid);
21628a1b9b6aSSam Leffler 
21630a915fadSSam Leffler 		if (ni == ic->ic_bss) {
21648a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
21658a1b9b6aSSam Leffler 			    "[%s] deny %s request, sta not authenticated\n",
21668a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
21678a1b9b6aSSam Leffler 			    reassoc ? "reassoc" : "assoc");
21688a1b9b6aSSam Leffler 			ni = ieee80211_dup_bss(ic->ic_sta, wh->i_addr2);
21690a915fadSSam Leffler 			if (ni != NULL) {
21700a915fadSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
21710a915fadSSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
21721a1e1d21SSam Leffler 				    IEEE80211_REASON_ASSOC_NOT_AUTHED);
21738a1b9b6aSSam Leffler 				ieee80211_free_node(ni);
21740a915fadSSam Leffler 			}
21751be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_notauth++;
21761a1e1d21SSam Leffler 			return;
21771a1e1d21SSam Leffler 		}
21788a1b9b6aSSam Leffler 		if (wpa != NULL) {
21798a1b9b6aSSam Leffler 			/*
21808a1b9b6aSSam Leffler 			 * Parse WPA information element.  Note that
21818a1b9b6aSSam Leffler 			 * we initialize the param block from the node
21828a1b9b6aSSam Leffler 			 * state so that information in the IE overrides
21838a1b9b6aSSam Leffler 			 * our defaults.  The resulting parameters are
21848a1b9b6aSSam Leffler 			 * installed below after the association is assured.
21858a1b9b6aSSam Leffler 			 */
21868a1b9b6aSSam Leffler 			rsn = ni->ni_rsn;
21878a1b9b6aSSam Leffler 			if (wpa[0] != IEEE80211_ELEMID_RSN)
21888a1b9b6aSSam Leffler 				reason = ieee80211_parse_wpa(ic, wpa, &rsn, wh);
21898a1b9b6aSSam Leffler 			else
21908a1b9b6aSSam Leffler 				reason = ieee80211_parse_rsn(ic, wpa, &rsn, wh);
21918a1b9b6aSSam Leffler 			if (reason != 0) {
21928a1b9b6aSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
21938a1b9b6aSSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
21948a1b9b6aSSam Leffler 				ieee80211_node_leave(ic, ni);
21958a1b9b6aSSam Leffler 				/* XXX distinguish WPA/RSN? */
21968a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_assoc_badwpaie++;
21978a1b9b6aSSam Leffler 				return;
21988a1b9b6aSSam Leffler 			}
21998a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
22008a1b9b6aSSam Leffler 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
22018a1b9b6aSSam Leffler 			    "[%s] %s ie: mc %u/%u uc %u/%u key %u caps 0x%x\n",
22028a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
22038a1b9b6aSSam Leffler 			    wpa[0] != IEEE80211_ELEMID_RSN ?  "WPA" : "RSN",
22048a1b9b6aSSam Leffler 			    rsn.rsn_mcastcipher, rsn.rsn_mcastkeylen,
22058a1b9b6aSSam Leffler 			    rsn.rsn_ucastcipher, rsn.rsn_ucastkeylen,
22068a1b9b6aSSam Leffler 			    rsn.rsn_keymgmt, rsn.rsn_caps);
22078a1b9b6aSSam Leffler 		}
22088a1b9b6aSSam Leffler 		/* discard challenge after association */
22098a1b9b6aSSam Leffler 		if (ni->ni_challenge != NULL) {
22108a1b9b6aSSam Leffler 			FREE(ni->ni_challenge, M_DEVBUF);
22118a1b9b6aSSam Leffler 			ni->ni_challenge = NULL;
22128a1b9b6aSSam Leffler 		}
22131a1e1d21SSam Leffler 		/* XXX some stations use the privacy bit for handling APs
22141a1e1d21SSam Leffler 		       that suport both encrypted and unencrypted traffic */
22158a1b9b6aSSam Leffler 		/* NB: PRIVACY flag bits are assumed to match */
22161a1e1d21SSam Leffler 		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0 ||
22178a1b9b6aSSam Leffler 		    (capinfo & IEEE80211_CAPINFO_PRIVACY) ^
22188a1b9b6aSSam Leffler 		    (ic->ic_flags & IEEE80211_F_PRIVACY)) {
22198a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
22208a1b9b6aSSam Leffler 			    "[%s] deny %s request, capability mismatch 0x%x\n",
22218a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
22228a1b9b6aSSam Leffler 			    reassoc ? "reassoc" : "assoc", capinfo);
22230a915fadSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
22240a915fadSSam Leffler 				IEEE80211_STATUS_CAPINFO);
22258a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
22261be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_capmismatch++;
22271a1e1d21SSam Leffler 			return;
22281a1e1d21SSam Leffler 		}
22291a1e1d21SSam Leffler 		ieee80211_setup_rates(ic, ni, rates, xrates,
22301a1e1d21SSam Leffler 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
22311a1e1d21SSam Leffler 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
22321a1e1d21SSam Leffler 		if (ni->ni_rates.rs_nrates == 0) {
22338a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
22348a1b9b6aSSam Leffler 			    "[%s] deny %s request, rate set mismatch\n",
22358a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
22368a1b9b6aSSam Leffler 			    reassoc ? "reassoc" : "assoc");
22370a915fadSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
22380a915fadSSam Leffler 				IEEE80211_STATUS_BASIC_RATE);
22398a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
22401be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_norate++;
22411a1e1d21SSam Leffler 			return;
22421a1e1d21SSam Leffler 		}
22431a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
22441a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
22451a1e1d21SSam Leffler 		ni->ni_intval = bintval;
22461a1e1d21SSam Leffler 		ni->ni_capinfo = capinfo;
22471a1e1d21SSam Leffler 		ni->ni_chan = ic->ic_bss->ni_chan;
22481a1e1d21SSam Leffler 		ni->ni_fhdwell = ic->ic_bss->ni_fhdwell;
22491a1e1d21SSam Leffler 		ni->ni_fhindex = ic->ic_bss->ni_fhindex;
22508a1b9b6aSSam Leffler 		if (wpa != NULL) {
22518a1b9b6aSSam Leffler 			/*
22528a1b9b6aSSam Leffler 			 * Record WPA/RSN parameters for station, mark
22538a1b9b6aSSam Leffler 			 * node as using WPA and record information element
22548a1b9b6aSSam Leffler 			 * for applications that require it.
22558a1b9b6aSSam Leffler 			 */
22568a1b9b6aSSam Leffler 			ni->ni_rsn = rsn;
22578a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wpa_ie, wpa);
22588a1b9b6aSSam Leffler 		} else if (ni->ni_wpa_ie != NULL) {
22598a1b9b6aSSam Leffler 			/*
22608a1b9b6aSSam Leffler 			 * Flush any state from a previous association.
22618a1b9b6aSSam Leffler 			 */
22628a1b9b6aSSam Leffler 			FREE(ni->ni_wpa_ie, M_DEVBUF);
22638a1b9b6aSSam Leffler 			ni->ni_wpa_ie = NULL;
22648a1b9b6aSSam Leffler 		}
22658a1b9b6aSSam Leffler 		if (wme != NULL) {
22668a1b9b6aSSam Leffler 			/*
22678a1b9b6aSSam Leffler 			 * Record WME parameters for station, mark node
22688a1b9b6aSSam Leffler 			 * as capable of QoS and record information
22698a1b9b6aSSam Leffler 			 * element for applications that require it.
22708a1b9b6aSSam Leffler 			 */
22718a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wme_ie, wme);
22728a1b9b6aSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
22738a1b9b6aSSam Leffler 		} else if (ni->ni_wme_ie != NULL) {
22748a1b9b6aSSam Leffler 			/*
22758a1b9b6aSSam Leffler 			 * Flush any state from a previous association.
22768a1b9b6aSSam Leffler 			 */
22778a1b9b6aSSam Leffler 			FREE(ni->ni_wme_ie, M_DEVBUF);
22788a1b9b6aSSam Leffler 			ni->ni_wme_ie = NULL;
22798a1b9b6aSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
22808a1b9b6aSSam Leffler 		}
22818a1b9b6aSSam Leffler 		ieee80211_node_join(ic, ni, resp);
22821a1e1d21SSam Leffler 		break;
22831a1e1d21SSam Leffler 	}
22841a1e1d21SSam Leffler 
22851a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
22861a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
22878a1b9b6aSSam Leffler 		u_int16_t capinfo, associd;
22881a1e1d21SSam Leffler 		u_int16_t status;
22891a1e1d21SSam Leffler 
22901a1e1d21SSam Leffler 		if (ic->ic_opmode != IEEE80211_M_STA ||
22918a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_ASSOC) {
22928a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
22931a1e1d21SSam Leffler 			return;
22948a1b9b6aSSam Leffler 		}
22951a1e1d21SSam Leffler 
22961a1e1d21SSam Leffler 		/*
22971a1e1d21SSam Leffler 		 * asresp frame format
22981a1e1d21SSam Leffler 		 *	[2] capability information
22991a1e1d21SSam Leffler 		 *	[2] status
23001a1e1d21SSam Leffler 		 *	[2] association ID
23011a1e1d21SSam Leffler 		 *	[tlv] supported rates
23021a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
23038a1b9b6aSSam Leffler 		 *	[tlv] WME
23041a1e1d21SSam Leffler 		 */
23051a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
23061a1e1d21SSam Leffler 		ni = ic->ic_bss;
23078a1b9b6aSSam Leffler 		capinfo = le16toh(*(u_int16_t *)frm);
23081a1e1d21SSam Leffler 		frm += 2;
23091a1e1d21SSam Leffler 		status = le16toh(*(u_int16_t *)frm);
23101a1e1d21SSam Leffler 		frm += 2;
23111a1e1d21SSam Leffler 		if (status != 0) {
23128a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
23138a1b9b6aSSam Leffler 			    "[%s] %sassoc failed (reason %d)\n",
23148a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
23158a1b9b6aSSam Leffler 			    ISREASSOC(subtype) ?  "re" : "", status);
23168a1b9b6aSSam Leffler 			if (ni != ic->ic_bss)	/* XXX never true? */
23171a1e1d21SSam Leffler 				ni->ni_fails++;
23188a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_fail++;	/* XXX */
23191a1e1d21SSam Leffler 			return;
23201a1e1d21SSam Leffler 		}
23218a1b9b6aSSam Leffler 		associd = le16toh(*(u_int16_t *)frm);
23221a1e1d21SSam Leffler 		frm += 2;
23231a1e1d21SSam Leffler 
23248a1b9b6aSSam Leffler 		rates = xrates = wpa = wme = NULL;
23251a1e1d21SSam Leffler 		while (frm < efrm) {
23261a1e1d21SSam Leffler 			switch (*frm) {
23271a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
23281a1e1d21SSam Leffler 				rates = frm;
23291a1e1d21SSam Leffler 				break;
23301a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
23311a1e1d21SSam Leffler 				xrates = frm;
23321a1e1d21SSam Leffler 				break;
23338a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
23348a1b9b6aSSam Leffler 				if (iswmeoui(frm))
23358a1b9b6aSSam Leffler 					wme = frm;
23368a1b9b6aSSam Leffler 				/* XXX Atheros OUI support */
23378a1b9b6aSSam Leffler 				break;
23381a1e1d21SSam Leffler 			}
23391a1e1d21SSam Leffler 			frm += frm[1] + 2;
23401a1e1d21SSam Leffler 		}
23411a1e1d21SSam Leffler 
2342dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
23431a1e1d21SSam Leffler 		ieee80211_setup_rates(ic, ni, rates, xrates,
23441a1e1d21SSam Leffler 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
23451a1e1d21SSam Leffler 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
23468a1b9b6aSSam Leffler 		if (ni->ni_rates.rs_nrates == 0) {
23478a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
23488a1b9b6aSSam Leffler 			    "[%s] %sassoc failed (rate set mismatch)\n",
23498a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
23508a1b9b6aSSam Leffler 			    ISREASSOC(subtype) ?  "re" : "");
23518a1b9b6aSSam Leffler 			if (ni != ic->ic_bss)	/* XXX never true? */
23528a1b9b6aSSam Leffler 				ni->ni_fails++;
23538a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_assoc_norate++;
23548a1b9b6aSSam Leffler 			return;
23558a1b9b6aSSam Leffler 		}
23568a1b9b6aSSam Leffler 
23578a1b9b6aSSam Leffler 		ni->ni_capinfo = capinfo;
23588a1b9b6aSSam Leffler 		ni->ni_associd = associd;
2359c0fa32ceSSam Leffler 		if (wme != NULL &&
2360c0fa32ceSSam Leffler 		    ieee80211_parse_wmeparams(ic, wme, wh) >= 0) {
23618a1b9b6aSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
23628a1b9b6aSSam Leffler 			ieee80211_wme_updateparams(ic);
23638a1b9b6aSSam Leffler 		} else
23648a1b9b6aSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
23658a1b9b6aSSam Leffler 		/*
23668a1b9b6aSSam Leffler 		 * Configure state now that we are associated.
23678a1b9b6aSSam Leffler 		 *
23688a1b9b6aSSam Leffler 		 * XXX may need different/additional driver callbacks?
23698a1b9b6aSSam Leffler 		 */
23708a1b9b6aSSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11A ||
23718a1b9b6aSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
23728a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
23738a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
23748a1b9b6aSSam Leffler 		} else {
23758a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
23768a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
23778a1b9b6aSSam Leffler 		}
23788a1b9b6aSSam Leffler 		ieee80211_set_shortslottime(ic,
23798a1b9b6aSSam Leffler 			ic->ic_curmode == IEEE80211_MODE_11A ||
23808a1b9b6aSSam Leffler 			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
23818a1b9b6aSSam Leffler 		/*
23828a1b9b6aSSam Leffler 		 * Honor ERP protection.
23838a1b9b6aSSam Leffler 		 *
23848a1b9b6aSSam Leffler 		 * NB: ni_erp should zero for non-11g operation.
23858a1b9b6aSSam Leffler 		 * XXX check ic_curmode anyway?
23868a1b9b6aSSam Leffler 		 */
23878a1b9b6aSSam Leffler 		if (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION)
23888a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
23898a1b9b6aSSam Leffler 		else
23908a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
23918a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
23928a1b9b6aSSam Leffler 		    "[%s] %sassoc success: %s preamble, %s slot time%s%s\n",
23938a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr2),
23948a1b9b6aSSam Leffler 		    ISREASSOC(subtype) ? "re" : "",
23958a1b9b6aSSam Leffler 		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
23968a1b9b6aSSam Leffler 		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
23978a1b9b6aSSam Leffler 		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
23988a1b9b6aSSam Leffler 		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
23998a1b9b6aSSam Leffler 		);
24008a1b9b6aSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, subtype);
24011a1e1d21SSam Leffler 		break;
24021a1e1d21SSam Leffler 	}
24031a1e1d21SSam Leffler 
24041a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
24051a1e1d21SSam Leffler 		u_int16_t reason;
24068a1b9b6aSSam Leffler 
24078a1b9b6aSSam Leffler 		if (ic->ic_state == IEEE80211_S_SCAN) {
24088a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
24098a1b9b6aSSam Leffler 			return;
24108a1b9b6aSSam Leffler 		}
24111a1e1d21SSam Leffler 		/*
24121a1e1d21SSam Leffler 		 * deauth frame format
24131a1e1d21SSam Leffler 		 *	[2] reason
24141a1e1d21SSam Leffler 		 */
24151a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
24161a1e1d21SSam Leffler 		reason = le16toh(*(u_int16_t *)frm);
24171be50176SSam Leffler 		ic->ic_stats.is_rx_deauth++;
24188a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_deauth);
24191a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
24201a1e1d21SSam Leffler 		case IEEE80211_M_STA:
2421a11c9a5cSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
24221a1e1d21SSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
24231a1e1d21SSam Leffler 			break;
24241a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
24250a915fadSSam Leffler 			if (ni != ic->ic_bss) {
24268a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
24278a1b9b6aSSam Leffler 				    "station %s deauthenticated by peer "
24288a1b9b6aSSam Leffler 				    "(reason %d)\n",
24291a1e1d21SSam Leffler 				    ether_sprintf(ni->ni_macaddr), reason);
24308a1b9b6aSSam Leffler 				ieee80211_node_leave(ic, ni);
24311a1e1d21SSam Leffler 			}
24321a1e1d21SSam Leffler 			break;
24331a1e1d21SSam Leffler 		default:
24348a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
24351a1e1d21SSam Leffler 			break;
24361a1e1d21SSam Leffler 		}
24371a1e1d21SSam Leffler 		break;
24381a1e1d21SSam Leffler 	}
24391a1e1d21SSam Leffler 
24401a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
24411a1e1d21SSam Leffler 		u_int16_t reason;
24428a1b9b6aSSam Leffler 
24438a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN &&
24448a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_AUTH) {
24458a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
24468a1b9b6aSSam Leffler 			return;
24478a1b9b6aSSam Leffler 		}
24481a1e1d21SSam Leffler 		/*
24491a1e1d21SSam Leffler 		 * disassoc frame format
24501a1e1d21SSam Leffler 		 *	[2] reason
24511a1e1d21SSam Leffler 		 */
24521a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
24531a1e1d21SSam Leffler 		reason = le16toh(*(u_int16_t *)frm);
24541be50176SSam Leffler 		ic->ic_stats.is_rx_disassoc++;
24558a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_disassoc);
24561a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
24571a1e1d21SSam Leffler 		case IEEE80211_M_STA:
2458a11c9a5cSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
24591a1e1d21SSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
24601a1e1d21SSam Leffler 			break;
24611a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
24620a915fadSSam Leffler 			if (ni != ic->ic_bss) {
24638a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
24648a1b9b6aSSam Leffler 				    "[%s] sta disassociated by peer (reason %d)\n",
24651a1e1d21SSam Leffler 				    ether_sprintf(ni->ni_macaddr), reason);
24668a1b9b6aSSam Leffler 				ieee80211_node_leave(ic, ni);
24671a1e1d21SSam Leffler 			}
24681a1e1d21SSam Leffler 			break;
24691a1e1d21SSam Leffler 		default:
24708a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
24711a1e1d21SSam Leffler 			break;
24721a1e1d21SSam Leffler 		}
24731a1e1d21SSam Leffler 		break;
24741a1e1d21SSam Leffler 	}
24751a1e1d21SSam Leffler 	default:
24768a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
24778a1b9b6aSSam Leffler 		     wh, "mgt", "subtype 0x%x not handled", subtype);
24781be50176SSam Leffler 		ic->ic_stats.is_rx_badsubtype++;
24791a1e1d21SSam Leffler 		break;
24801a1e1d21SSam Leffler 	}
24818a1b9b6aSSam Leffler #undef ISREASSOC
24828a1b9b6aSSam Leffler #undef ISPROBE
24831a1e1d21SSam Leffler }
24841a1e1d21SSam Leffler #undef IEEE80211_VERIFY_LENGTH
24851a1e1d21SSam Leffler #undef IEEE80211_VERIFY_ELEMENT
24868a1b9b6aSSam Leffler 
24878a1b9b6aSSam Leffler /*
24888a1b9b6aSSam Leffler  * Handle station power-save state change.
24898a1b9b6aSSam Leffler  */
24908a1b9b6aSSam Leffler static void
24918a1b9b6aSSam Leffler ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
24928a1b9b6aSSam Leffler {
24938a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
24948a1b9b6aSSam Leffler 	struct mbuf *m;
24958a1b9b6aSSam Leffler 
24968a1b9b6aSSam Leffler 	if (enable) {
24978a1b9b6aSSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0)
24988a1b9b6aSSam Leffler 			ic->ic_ps_sta++;
24998a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
25008a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
25018a1b9b6aSSam Leffler 		    "[%s] power save mode on, %u sta's in ps mode\n",
25028a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
25038a1b9b6aSSam Leffler 		return;
25048a1b9b6aSSam Leffler 	}
25058a1b9b6aSSam Leffler 
25068a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT)
25078a1b9b6aSSam Leffler 		ic->ic_ps_sta--;
25088a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
25098a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
25108a1b9b6aSSam Leffler 	    "[%s] power save mode off, %u sta's in ps mode\n",
25118a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
25128a1b9b6aSSam Leffler 	/* XXX if no stations in ps mode, flush mc frames */
25138a1b9b6aSSam Leffler 
25148a1b9b6aSSam Leffler 	/*
25158a1b9b6aSSam Leffler 	 * Flush queued unicast frames.
25168a1b9b6aSSam Leffler 	 */
25178a1b9b6aSSam Leffler 	if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) {
25188a1b9b6aSSam Leffler 		ic->ic_set_tim(ic, ni, 0);	/* just in case */
25198a1b9b6aSSam Leffler 		return;
25208a1b9b6aSSam Leffler 	}
25218a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
25228a1b9b6aSSam Leffler 	    "[%s] flush ps queue, %u packets queued\n",
25238a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_SAVEQ_QLEN(ni));
25248a1b9b6aSSam Leffler 	for (;;) {
25258a1b9b6aSSam Leffler 		int qlen;
25268a1b9b6aSSam Leffler 
25278a1b9b6aSSam Leffler 		IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen);
25288a1b9b6aSSam Leffler 		if (m == NULL)
25298a1b9b6aSSam Leffler 			break;
25308a1b9b6aSSam Leffler 		/*
25318a1b9b6aSSam Leffler 		 * If this is the last packet, turn off the TIM bit.
25328a1b9b6aSSam Leffler 		 * If there are more packets, set the more packets bit
25338a1b9b6aSSam Leffler 		 * in the packet dispatched to the station.
25348a1b9b6aSSam Leffler 		 */
25358a1b9b6aSSam Leffler 		if (qlen != 0) {
25368a1b9b6aSSam Leffler 			struct ieee80211_frame_min *wh =
25378a1b9b6aSSam Leffler 				mtod(m, struct ieee80211_frame_min *);
25388a1b9b6aSSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
25398a1b9b6aSSam Leffler 		}
25408a1b9b6aSSam Leffler 		/* XXX need different driver interface */
25418a1b9b6aSSam Leffler 		/* XXX bypasses q max */
25428a1b9b6aSSam Leffler 		IF_ENQUEUE(&ic->ic_ifp->if_snd, m);
25438a1b9b6aSSam Leffler 	}
25448a1b9b6aSSam Leffler }
25458a1b9b6aSSam Leffler 
25468a1b9b6aSSam Leffler /*
25478a1b9b6aSSam Leffler  * Process a received ps-poll frame.
25488a1b9b6aSSam Leffler  */
25498a1b9b6aSSam Leffler static void
25508a1b9b6aSSam Leffler ieee80211_recv_pspoll(struct ieee80211com *ic,
25518a1b9b6aSSam Leffler 	struct ieee80211_node *ni, struct mbuf *m0)
25528a1b9b6aSSam Leffler {
25538a1b9b6aSSam Leffler 	struct ieee80211_frame_min *wh;
25548a1b9b6aSSam Leffler 	struct mbuf *m;
25558a1b9b6aSSam Leffler 	u_int16_t aid;
25568a1b9b6aSSam Leffler 	int qlen;
25578a1b9b6aSSam Leffler 
25588a1b9b6aSSam Leffler 	wh = mtod(m0, struct ieee80211_frame_min *);
25598a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
25608a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
25618a1b9b6aSSam Leffler 		    (struct ieee80211_frame *) wh, "ps-poll",
25628a1b9b6aSSam Leffler 		    "%s", "unassociated station");
25638a1b9b6aSSam Leffler 		ic->ic_stats.is_ps_unassoc++;
25648a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
25658a1b9b6aSSam Leffler 			IEEE80211_REASON_NOT_ASSOCED);
25668a1b9b6aSSam Leffler 		return;
25678a1b9b6aSSam Leffler 	}
25688a1b9b6aSSam Leffler 
25698a1b9b6aSSam Leffler 	aid = le16toh(*(u_int16_t *)wh->i_dur);
25708a1b9b6aSSam Leffler 	if (aid != ni->ni_associd) {
25718a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
25728a1b9b6aSSam Leffler 		    (struct ieee80211_frame *) wh, "ps-poll",
25738a1b9b6aSSam Leffler 		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
25748a1b9b6aSSam Leffler 		    ni->ni_associd, aid);
25758a1b9b6aSSam Leffler 		ic->ic_stats.is_ps_badaid++;
25768a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
25778a1b9b6aSSam Leffler 			IEEE80211_REASON_NOT_ASSOCED);
25788a1b9b6aSSam Leffler 		return;
25798a1b9b6aSSam Leffler 	}
25808a1b9b6aSSam Leffler 
25818a1b9b6aSSam Leffler 	/* Okay, take the first queued packet and put it out... */
25828a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen);
25838a1b9b6aSSam Leffler 	if (m == NULL) {
25848a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
25858a1b9b6aSSam Leffler 		    "[%s] recv ps-poll, but queue empty\n",
25868a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr2));
25878a1b9b6aSSam Leffler 		ieee80211_send_nulldata(ic, ni);
25888a1b9b6aSSam Leffler 		ic->ic_stats.is_ps_qempty++;	/* XXX node stat */
25898a1b9b6aSSam Leffler 		ic->ic_set_tim(ic, ni, 0);	/* just in case */
25908a1b9b6aSSam Leffler 		return;
25918a1b9b6aSSam Leffler 	}
25928a1b9b6aSSam Leffler 	/*
25938a1b9b6aSSam Leffler 	 * If there are more packets, set the more packets bit
25948a1b9b6aSSam Leffler 	 * in the packet dispatched to the station; otherwise
25958a1b9b6aSSam Leffler 	 * turn off the TIM bit.
25968a1b9b6aSSam Leffler 	 */
25978a1b9b6aSSam Leffler 	if (qlen != 0) {
25988a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
25998a1b9b6aSSam Leffler 		    "[%s] recv ps-poll, send packet, %u still queued\n",
26008a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), qlen);
26018a1b9b6aSSam Leffler 		wh = mtod(m, struct ieee80211_frame_min *);
26028a1b9b6aSSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
26038a1b9b6aSSam Leffler 	} else {
26048a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
26058a1b9b6aSSam Leffler 		    "[%s] recv ps-poll, send packet, queue empty\n",
26068a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
26078a1b9b6aSSam Leffler 		ic->ic_set_tim(ic, ni, 0);
26088a1b9b6aSSam Leffler 	}
26098a1b9b6aSSam Leffler 	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
26108a1b9b6aSSam Leffler 	IF_ENQUEUE(&ic->ic_ifp->if_snd, m);
26118a1b9b6aSSam Leffler }
26128a1b9b6aSSam Leffler 
26138a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
26148a1b9b6aSSam Leffler /*
26158a1b9b6aSSam Leffler  * Debugging support.
26168a1b9b6aSSam Leffler  */
26178a1b9b6aSSam Leffler 
26188a1b9b6aSSam Leffler /*
26198a1b9b6aSSam Leffler  * Return the bssid of a frame.
26208a1b9b6aSSam Leffler  */
26218a1b9b6aSSam Leffler static const u_int8_t *
26228a1b9b6aSSam Leffler ieee80211_getbssid(struct ieee80211com *ic, const struct ieee80211_frame *wh)
26238a1b9b6aSSam Leffler {
26248a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA)
26258a1b9b6aSSam Leffler 		return wh->i_addr2;
26268a1b9b6aSSam Leffler 	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) != IEEE80211_FC1_DIR_NODS)
26278a1b9b6aSSam Leffler 		return wh->i_addr1;
26288a1b9b6aSSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
26298a1b9b6aSSam Leffler 		return wh->i_addr1;
26308a1b9b6aSSam Leffler 	return wh->i_addr3;
26318a1b9b6aSSam Leffler }
26328a1b9b6aSSam Leffler 
26338a1b9b6aSSam Leffler static void
26348a1b9b6aSSam Leffler ieee80211_discard_frame(struct ieee80211com *ic,
26358a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
26368a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
26378a1b9b6aSSam Leffler {
26388a1b9b6aSSam Leffler 	va_list ap;
26398a1b9b6aSSam Leffler 
26408a1b9b6aSSam Leffler 	printf("[%s] discard ", ether_sprintf(ieee80211_getbssid(ic, wh)));
26418a1b9b6aSSam Leffler 	if (type != NULL)
26428a1b9b6aSSam Leffler 		printf(" %s frame, ", type);
26438a1b9b6aSSam Leffler 	else
26448a1b9b6aSSam Leffler 		printf(" frame, ");
26458a1b9b6aSSam Leffler 	va_start(ap, fmt);
26468a1b9b6aSSam Leffler 	vprintf(fmt, ap);
26478a1b9b6aSSam Leffler 	va_end(ap);
26488a1b9b6aSSam Leffler 	printf("\n");
26498a1b9b6aSSam Leffler }
26508a1b9b6aSSam Leffler 
26518a1b9b6aSSam Leffler static void
26528a1b9b6aSSam Leffler ieee80211_discard_ie(struct ieee80211com *ic,
26538a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
26548a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
26558a1b9b6aSSam Leffler {
26568a1b9b6aSSam Leffler 	va_list ap;
26578a1b9b6aSSam Leffler 
26588a1b9b6aSSam Leffler 	printf("[%s] discard ", ether_sprintf(ieee80211_getbssid(ic, wh)));
26598a1b9b6aSSam Leffler 	if (type != NULL)
26608a1b9b6aSSam Leffler 		printf(" %s information element, ", type);
26618a1b9b6aSSam Leffler 	else
26628a1b9b6aSSam Leffler 		printf(" information element, ");
26638a1b9b6aSSam Leffler 	va_start(ap, fmt);
26648a1b9b6aSSam Leffler 	vprintf(fmt, ap);
26658a1b9b6aSSam Leffler 	va_end(ap);
26668a1b9b6aSSam Leffler 	printf("\n");
26678a1b9b6aSSam Leffler }
26688a1b9b6aSSam Leffler 
26698a1b9b6aSSam Leffler static void
26708a1b9b6aSSam Leffler ieee80211_discard_mac(struct ieee80211com *ic,
26718a1b9b6aSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN],
26728a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
26738a1b9b6aSSam Leffler {
26748a1b9b6aSSam Leffler 	va_list ap;
26758a1b9b6aSSam Leffler 
26768a1b9b6aSSam Leffler 	printf("[%s] discard ", ether_sprintf(mac));
26778a1b9b6aSSam Leffler 	if (type != NULL)
26788a1b9b6aSSam Leffler 		printf(" %s frame, ", type);
26798a1b9b6aSSam Leffler 	else
26808a1b9b6aSSam Leffler 		printf(" frame, ");
26818a1b9b6aSSam Leffler 	va_start(ap, fmt);
26828a1b9b6aSSam Leffler 	vprintf(fmt, ap);
26838a1b9b6aSSam Leffler 	va_end(ap);
26848a1b9b6aSSam Leffler 	printf("\n");
26858a1b9b6aSSam Leffler }
26868a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2687