xref: /freebsd/sys/net80211/ieee80211_input.c (revision 097131fffe766b8e52cbbcfe201ed496cea6f297)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
31f1d7810SSam Leffler  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
147535e66aSSam Leffler  * 3. The name of the author may not be used to endorse or promote products
157535e66aSSam Leffler  *    derived from this software without specific prior written permission.
161a1e1d21SSam Leffler  *
171a1e1d21SSam Leffler  * Alternatively, this software may be distributed under the terms of the
181a1e1d21SSam Leffler  * GNU General Public License ("GPL") version 2 as published by the Free
191a1e1d21SSam Leffler  * Software Foundation.
201a1e1d21SSam Leffler  *
217535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
227535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
237535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
247535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
257535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
267535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
277535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
287535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
297535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
307535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311a1e1d21SSam Leffler  */
321a1e1d21SSam Leffler 
331a1e1d21SSam Leffler #include <sys/cdefs.h>
341a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
351a1e1d21SSam Leffler 
361a1e1d21SSam Leffler #include <sys/param.h>
371a1e1d21SSam Leffler #include <sys/systm.h>
381a1e1d21SSam Leffler #include <sys/mbuf.h>
391a1e1d21SSam Leffler #include <sys/malloc.h>
401a1e1d21SSam Leffler #include <sys/endian.h>
41a0cc3f85SSam Leffler #include <sys/kernel.h>
421a1e1d21SSam Leffler 
438a1b9b6aSSam Leffler #include <sys/socket.h>
441a1e1d21SSam Leffler 
451a1e1d21SSam Leffler #include <net/if.h>
461a1e1d21SSam Leffler #include <net/if_media.h>
471a1e1d21SSam Leffler #include <net/ethernet.h>
481a1e1d21SSam Leffler #include <net/if_llc.h>
498a1b9b6aSSam Leffler #include <net/if_vlan_var.h>
501a1e1d21SSam Leffler 
511a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
521a1e1d21SSam Leffler 
531a1e1d21SSam Leffler #include <net/bpf.h>
541a1e1d21SSam Leffler 
558a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
568a1b9b6aSSam Leffler #include <machine/stdarg.h>
578a1b9b6aSSam Leffler 
588a1b9b6aSSam Leffler /*
598a1b9b6aSSam Leffler  * Decide if a received management frame should be
608a1b9b6aSSam Leffler  * printed when debugging is enabled.  This filters some
618a1b9b6aSSam Leffler  * of the less interesting frames that come frequently
628a1b9b6aSSam Leffler  * (e.g. beacons).
638a1b9b6aSSam Leffler  */
648a1b9b6aSSam Leffler static __inline int
658a1b9b6aSSam Leffler doprint(struct ieee80211com *ic, int subtype)
668a1b9b6aSSam Leffler {
678a1b9b6aSSam Leffler 	switch (subtype) {
688a1b9b6aSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
698a1b9b6aSSam Leffler 		return (ic->ic_flags & IEEE80211_F_SCAN);
708a1b9b6aSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
718a1b9b6aSSam Leffler 		return (ic->ic_opmode == IEEE80211_M_IBSS);
728a1b9b6aSSam Leffler 	}
738a1b9b6aSSam Leffler 	return 1;
748a1b9b6aSSam Leffler }
758a1b9b6aSSam Leffler 
768a1b9b6aSSam Leffler /*
778a1b9b6aSSam Leffler  * Emit a debug message about discarding a frame or information
788a1b9b6aSSam Leffler  * element.  One format is for extracting the mac address from
798a1b9b6aSSam Leffler  * the frame header; the other is for when a header is not
808a1b9b6aSSam Leffler  * available or otherwise appropriate.
818a1b9b6aSSam Leffler  */
828a1b9b6aSSam Leffler #define	IEEE80211_DISCARD(_ic, _m, _wh, _type, _fmt, ...) do {		\
838a1b9b6aSSam Leffler 	if ((_ic)->ic_debug & (_m))					\
848a1b9b6aSSam Leffler 		ieee80211_discard_frame(_ic, _wh, _type, _fmt, __VA_ARGS__);\
858a1b9b6aSSam Leffler } while (0)
868a1b9b6aSSam Leffler #define	IEEE80211_DISCARD_IE(_ic, _m, _wh, _type, _fmt, ...) do {	\
878a1b9b6aSSam Leffler 	if ((_ic)->ic_debug & (_m))					\
888a1b9b6aSSam Leffler 		ieee80211_discard_ie(_ic, _wh, _type, _fmt, __VA_ARGS__);\
898a1b9b6aSSam Leffler } while (0)
908a1b9b6aSSam Leffler #define	IEEE80211_DISCARD_MAC(_ic, _m, _mac, _type, _fmt, ...) do {	\
918a1b9b6aSSam Leffler 	if ((_ic)->ic_debug & (_m))					\
928a1b9b6aSSam Leffler 		ieee80211_discard_mac(_ic, _mac, _type, _fmt, __VA_ARGS__);\
938a1b9b6aSSam Leffler } while (0)
948a1b9b6aSSam Leffler 
958a1b9b6aSSam Leffler static const u_int8_t *ieee80211_getbssid(struct ieee80211com *,
968a1b9b6aSSam Leffler 	const struct ieee80211_frame *);
978a1b9b6aSSam Leffler static void ieee80211_discard_frame(struct ieee80211com *,
988a1b9b6aSSam Leffler 	const struct ieee80211_frame *, const char *type, const char *fmt, ...);
998a1b9b6aSSam Leffler static void ieee80211_discard_ie(struct ieee80211com *,
1008a1b9b6aSSam Leffler 	const struct ieee80211_frame *, const char *type, const char *fmt, ...);
1018a1b9b6aSSam Leffler static void ieee80211_discard_mac(struct ieee80211com *,
1028a1b9b6aSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN], const char *type,
1038a1b9b6aSSam Leffler 	const char *fmt, ...);
1048a1b9b6aSSam Leffler #else
1058a1b9b6aSSam Leffler #define	IEEE80211_DISCARD(_ic, _m, _wh, _type, _fmt, ...)
1068a1b9b6aSSam Leffler #define	IEEE80211_DISCARD_IE(_ic, _m, _wh, _type, _fmt, ...)
1078a1b9b6aSSam Leffler #define	IEEE80211_DISCARD_MAC(_ic, _m, _mac, _type, _fmt, ...)
1088a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
1098a1b9b6aSSam Leffler 
1108a1b9b6aSSam Leffler static struct mbuf *ieee80211_defrag(struct ieee80211com *,
1112cc12adeSSam Leffler 	struct ieee80211_node *, struct mbuf *, int);
1122cc12adeSSam Leffler static struct mbuf *ieee80211_decap(struct ieee80211com *, struct mbuf *, int);
11384eb84c4SSam Leffler static void ieee80211_send_error(struct ieee80211com *, struct ieee80211_node *,
11484eb84c4SSam Leffler 		const u_int8_t *mac, int subtype, int arg);
1151bd482efSSam Leffler static void ieee80211_deliver_data(struct ieee80211com *,
1161bd482efSSam Leffler 	struct ieee80211_node *, struct mbuf *);
1178a1b9b6aSSam Leffler static void ieee80211_node_pwrsave(struct ieee80211_node *, int enable);
1188a1b9b6aSSam Leffler static void ieee80211_recv_pspoll(struct ieee80211com *,
1198a1b9b6aSSam Leffler 	struct ieee80211_node *, struct mbuf *);
1201a1e1d21SSam Leffler 
1210a915fadSSam Leffler /*
1220a915fadSSam Leffler  * Process a received frame.  The node associated with the sender
1230a915fadSSam Leffler  * should be supplied.  If nothing was found in the node table then
1240a915fadSSam Leffler  * the caller is assumed to supply a reference to ic_bss instead.
1250a915fadSSam Leffler  * The RSSI and a timestamp are also supplied.  The RSSI data is used
1260a915fadSSam Leffler  * during AP scanning to select a AP to associate with; it can have
1270a915fadSSam Leffler  * any units so long as values have consistent units and higher values
1280a915fadSSam Leffler  * mean ``better signal''.  The receive timestamp is currently not used
1290a915fadSSam Leffler  * by the 802.11 layer.
1300a915fadSSam Leffler  */
1311f298879SSam Leffler int
1328a1b9b6aSSam Leffler ieee80211_input(struct ieee80211com *ic, struct mbuf *m,
1338a1b9b6aSSam Leffler 	struct ieee80211_node *ni, int rssi, u_int32_t rstamp)
1341a1e1d21SSam Leffler {
1358a1b9b6aSSam Leffler #define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
1368a1b9b6aSSam Leffler #define	HAS_SEQ(type)	((type & 0x4) == 0)
1378a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1381a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
1398a1b9b6aSSam Leffler 	struct ieee80211_key *key;
1401a1e1d21SSam Leffler 	struct ether_header *eh;
141b138c150SSam Leffler 	int hdrspace;
14218f897abSSam Leffler 	u_int8_t dir, type, subtype;
1431a1e1d21SSam Leffler 	u_int8_t *bssid;
1441a1e1d21SSam Leffler 	u_int16_t rxseq;
1451a1e1d21SSam Leffler 
1460a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
1478a1b9b6aSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
1480a915fadSSam Leffler 
14918f897abSSam Leffler 	/* trim CRC here so WEP can find its own CRC at the end of packet. */
1501a1e1d21SSam Leffler 	if (m->m_flags & M_HASFCS) {
1511a1e1d21SSam Leffler 		m_adj(m, -IEEE80211_CRC_LEN);
1521a1e1d21SSam Leffler 		m->m_flags &= ~M_HASFCS;
1531a1e1d21SSam Leffler 	}
1541f298879SSam Leffler 	type = -1;			/* undefined */
15518f897abSSam Leffler 	/*
15618f897abSSam Leffler 	 * In monitor mode, send everything directly to bpf.
15718f897abSSam Leffler 	 * XXX may want to include the CRC
15818f897abSSam Leffler 	 */
15918f897abSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
16018f897abSSam Leffler 		goto out;
1611a1e1d21SSam Leffler 
1628a1b9b6aSSam Leffler 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1638a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
1648a1b9b6aSSam Leffler 		    ni->ni_macaddr, NULL,
1658a1b9b6aSSam Leffler 		    "too short (1): len %u", m->m_pkthdr.len);
1668a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_tooshort++;
1678a1b9b6aSSam Leffler 		goto out;
1688a1b9b6aSSam Leffler 	}
1698a1b9b6aSSam Leffler 	/*
1708a1b9b6aSSam Leffler 	 * Bit of a cheat here, we use a pointer for a 3-address
1718a1b9b6aSSam Leffler 	 * frame format but don't reference fields past outside
1728a1b9b6aSSam Leffler 	 * ieee80211_frame_min w/o first validating the data is
1738a1b9b6aSSam Leffler 	 * present.
1748a1b9b6aSSam Leffler 	 */
1751a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
1768a1b9b6aSSam Leffler 
1771a1e1d21SSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1781a1e1d21SSam Leffler 	    IEEE80211_FC0_VERSION_0) {
1798a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
1808a1b9b6aSSam Leffler 		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1811be50176SSam Leffler 		ic->ic_stats.is_rx_badversion++;
1821a1e1d21SSam Leffler 		goto err;
1831a1e1d21SSam Leffler 	}
1841a1e1d21SSam Leffler 
1851a1e1d21SSam Leffler 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
18618f897abSSam Leffler 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1878a1b9b6aSSam Leffler 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1888a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1891a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
1901a1e1d21SSam Leffler 		case IEEE80211_M_STA:
1918a1b9b6aSSam Leffler 			bssid = wh->i_addr2;
1928a1b9b6aSSam Leffler 			if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
1931be50176SSam Leffler 				/* not interested in */
1948a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
1958a1b9b6aSSam Leffler 				    bssid, NULL, "%s", "not to bss");
1961be50176SSam Leffler 				ic->ic_stats.is_rx_wrongbss++;
1971a1e1d21SSam Leffler 				goto out;
1981a1e1d21SSam Leffler 			}
1991a1e1d21SSam Leffler 			break;
2001a1e1d21SSam Leffler 		case IEEE80211_M_IBSS:
2011a1e1d21SSam Leffler 		case IEEE80211_M_AHDEMO:
2021a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
2038a1b9b6aSSam Leffler 			if (dir != IEEE80211_FC1_DIR_NODS)
2041a1e1d21SSam Leffler 				bssid = wh->i_addr1;
2058a1b9b6aSSam Leffler 			else if (type == IEEE80211_FC0_TYPE_CTL)
2068a1b9b6aSSam Leffler 				bssid = wh->i_addr1;
2078a1b9b6aSSam Leffler 			else {
2088a1b9b6aSSam Leffler 				if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
2098a1b9b6aSSam Leffler 					IEEE80211_DISCARD_MAC(ic,
2108a1b9b6aSSam Leffler 					    IEEE80211_MSG_ANY, ni->ni_macaddr,
2118a1b9b6aSSam Leffler 					    NULL, "too short (2): len %u",
2128a1b9b6aSSam Leffler 					    m->m_pkthdr.len);
2138a1b9b6aSSam Leffler 					ic->ic_stats.is_rx_tooshort++;
2148a1b9b6aSSam Leffler 					goto out;
2158a1b9b6aSSam Leffler 				}
2168a1b9b6aSSam Leffler 				bssid = wh->i_addr3;
2178a1b9b6aSSam Leffler 			}
218bc031652SSam Leffler 			if (type != IEEE80211_FC0_TYPE_DATA)
219bc031652SSam Leffler 				break;
2208a1b9b6aSSam Leffler 			/*
2218a1b9b6aSSam Leffler 			 * Data frame, validate the bssid.
2228a1b9b6aSSam Leffler 			 */
2231a1e1d21SSam Leffler 			if (!IEEE80211_ADDR_EQ(bssid, ic->ic_bss->ni_bssid) &&
2241a1e1d21SSam Leffler 			    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
2251a1e1d21SSam Leffler 				/* not interested in */
2268a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
2278a1b9b6aSSam Leffler 				    bssid, NULL, "%s", "not to bss");
2281be50176SSam Leffler 				ic->ic_stats.is_rx_wrongbss++;
2291a1e1d21SSam Leffler 				goto out;
2301a1e1d21SSam Leffler 			}
2318a1b9b6aSSam Leffler 			/*
2328a1b9b6aSSam Leffler 			 * For adhoc mode we cons up a node when it doesn't
2338a1b9b6aSSam Leffler 			 * exist. This should probably done after an ACL check.
2348a1b9b6aSSam Leffler 			 */
2358a1b9b6aSSam Leffler 			if (ni == ic->ic_bss &&
236be425a0fSSam Leffler 			    ic->ic_opmode != IEEE80211_M_HOSTAP &&
237be425a0fSSam Leffler 			    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
2388a1b9b6aSSam Leffler 				/*
2398a1b9b6aSSam Leffler 				 * Fake up a node for this newly
2408a1b9b6aSSam Leffler 				 * discovered member of the IBSS.
2418a1b9b6aSSam Leffler 				 */
242acc4f7f5SSam Leffler 				ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
2430796482dSSam Leffler 						    wh->i_addr2);
2448a1b9b6aSSam Leffler 				if (ni == NULL) {
2458a1b9b6aSSam Leffler 					/* NB: stat kept for alloc failure */
2468a1b9b6aSSam Leffler 					goto err;
2478a1b9b6aSSam Leffler 				}
2488a1b9b6aSSam Leffler 			}
2491a1e1d21SSam Leffler 			break;
2501a1e1d21SSam Leffler 		default:
2518a1b9b6aSSam Leffler 			goto out;
2521a1e1d21SSam Leffler 		}
2531a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
2541a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
2558a1b9b6aSSam Leffler 		if (HAS_SEQ(type)) {
2568a1b9b6aSSam Leffler 			u_int8_t tid;
2578a1b9b6aSSam Leffler 			if (IEEE80211_QOS_HAS_SEQ(wh)) {
2588a1b9b6aSSam Leffler 				tid = ((struct ieee80211_qosframe *)wh)->
2598a1b9b6aSSam Leffler 					i_qos[0] & IEEE80211_QOS_TID;
2601a08800dSSam Leffler 				if (TID_TO_WME_AC(tid) >= WME_AC_VI)
2618a1b9b6aSSam Leffler 					ic->ic_wme.wme_hipri_traffic++;
2628a1b9b6aSSam Leffler 				tid++;
2638a1b9b6aSSam Leffler 			} else
2648a1b9b6aSSam Leffler 				tid = 0;
2658a1b9b6aSSam Leffler 			rxseq = le16toh(*(u_int16_t *)wh->i_seq);
2661a1e1d21SSam Leffler 			if ((wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
2678a1b9b6aSSam Leffler 			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
2688a1b9b6aSSam Leffler 				/* duplicate, discard */
2698a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
2708a1b9b6aSSam Leffler 				    bssid, "duplicate",
2718a1b9b6aSSam Leffler 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
2728a1b9b6aSSam Leffler 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
2738a1b9b6aSSam Leffler 				    ni->ni_rxseqs[tid] >>
2748a1b9b6aSSam Leffler 					IEEE80211_SEQ_SEQ_SHIFT,
2758a1b9b6aSSam Leffler 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
2768a1b9b6aSSam Leffler 				    ni->ni_rxseqs[tid] &
2778a1b9b6aSSam Leffler 					IEEE80211_SEQ_FRAG_MASK,
2788a1b9b6aSSam Leffler 				    tid);
2798a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_dup++;
2808a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_dup);
2811a1e1d21SSam Leffler 				goto out;
2821a1e1d21SSam Leffler 			}
2838a1b9b6aSSam Leffler 			ni->ni_rxseqs[tid] = rxseq;
2848a1b9b6aSSam Leffler 		}
2851a1e1d21SSam Leffler 	}
2861a1e1d21SSam Leffler 
28718f897abSSam Leffler 	switch (type) {
2881a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
2892cc12adeSSam Leffler 		hdrspace = ieee80211_hdrspace(ic, wh);
2902cc12adeSSam Leffler 		if (m->m_len < hdrspace &&
2912cc12adeSSam Leffler 		    (m = m_pullup(m, hdrspace)) == NULL) {
29220098591SSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
29320098591SSam Leffler 			    ni->ni_macaddr, NULL,
2942cc12adeSSam Leffler 			    "data too short: expecting %u", hdrspace);
2958a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_tooshort++;
2968a1b9b6aSSam Leffler 			goto out;		/* XXX */
2978a1b9b6aSSam Leffler 		}
2981a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
2991a1e1d21SSam Leffler 		case IEEE80211_M_STA:
3001be50176SSam Leffler 			if (dir != IEEE80211_FC1_DIR_FROMDS) {
301bd6f09d9SSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
302bd6f09d9SSam Leffler 				    wh, "data", "%s", "unknown dir 0x%x", dir);
3031be50176SSam Leffler 				ic->ic_stats.is_rx_wrongdir++;
3041a1e1d21SSam Leffler 				goto out;
3051be50176SSam Leffler 			}
3061a1e1d21SSam Leffler 			if ((ifp->if_flags & IFF_SIMPLEX) &&
3071a1e1d21SSam Leffler 			    IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3081a1e1d21SSam Leffler 			    IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_myaddr)) {
3091a1e1d21SSam Leffler 				/*
3101a1e1d21SSam Leffler 				 * In IEEE802.11 network, multicast packet
3111a1e1d21SSam Leffler 				 * sent from me is broadcasted from AP.
3121a1e1d21SSam Leffler 				 * It should be silently discarded for
3131a1e1d21SSam Leffler 				 * SIMPLEX interface.
3141a1e1d21SSam Leffler 				 */
3158a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3168a1b9b6aSSam Leffler 				    wh, NULL, "%s", "multicast echo");
3171be50176SSam Leffler 				ic->ic_stats.is_rx_mcastecho++;
3181a1e1d21SSam Leffler 				goto out;
3191a1e1d21SSam Leffler 			}
3201a1e1d21SSam Leffler 			break;
3211a1e1d21SSam Leffler 		case IEEE80211_M_IBSS:
3221a1e1d21SSam Leffler 		case IEEE80211_M_AHDEMO:
3231be50176SSam Leffler 			if (dir != IEEE80211_FC1_DIR_NODS) {
324bd6f09d9SSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
325bd6f09d9SSam Leffler 				    wh, "data", "%s", "unknown dir 0x%x", dir);
3261be50176SSam Leffler 				ic->ic_stats.is_rx_wrongdir++;
3271a1e1d21SSam Leffler 				goto out;
3281be50176SSam Leffler 			}
3298a1b9b6aSSam Leffler 			/* XXX no power-save support */
3301a1e1d21SSam Leffler 			break;
3311a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
3321be50176SSam Leffler 			if (dir != IEEE80211_FC1_DIR_TODS) {
333bd6f09d9SSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
334bd6f09d9SSam Leffler 				    wh, "data", "%s", "unknown dir 0x%x", dir);
3351be50176SSam Leffler 				ic->ic_stats.is_rx_wrongdir++;
3361a1e1d21SSam Leffler 				goto out;
3371be50176SSam Leffler 			}
3381a1e1d21SSam Leffler 			/* check if source STA is associated */
3390a915fadSSam Leffler 			if (ni == ic->ic_bss) {
3408a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3418a1b9b6aSSam Leffler 				    wh, "data", "%s", "unknown src");
34284eb84c4SSam Leffler 				ieee80211_send_error(ic, ni, wh->i_addr2,
3431a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
3441a1e1d21SSam Leffler 				    IEEE80211_REASON_NOT_AUTHED);
3451be50176SSam Leffler 				ic->ic_stats.is_rx_notassoc++;
3461a1e1d21SSam Leffler 				goto err;
3471a1e1d21SSam Leffler 			}
3481a1e1d21SSam Leffler 			if (ni->ni_associd == 0) {
3498a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3508a1b9b6aSSam Leffler 				    wh, "data", "%s", "unassoc src");
3511a1e1d21SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
3521a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
3531a1e1d21SSam Leffler 				    IEEE80211_REASON_NOT_ASSOCED);
3541be50176SSam Leffler 				ic->ic_stats.is_rx_notassoc++;
3551a1e1d21SSam Leffler 				goto err;
3561a1e1d21SSam Leffler 			}
3578a1b9b6aSSam Leffler 
3588a1b9b6aSSam Leffler 			/*
3598a1b9b6aSSam Leffler 			 * Check for power save state change.
3608a1b9b6aSSam Leffler 			 */
3618a1b9b6aSSam Leffler 			if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
3628a1b9b6aSSam Leffler 			    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
3638a1b9b6aSSam Leffler 				ieee80211_node_pwrsave(ni,
3648a1b9b6aSSam Leffler 					wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
3651a1e1d21SSam Leffler 			break;
3668a1b9b6aSSam Leffler 		default:
3678a1b9b6aSSam Leffler 			/* XXX here to keep compiler happy */
3688a1b9b6aSSam Leffler 			goto out;
3691a1e1d21SSam Leffler 		}
3708a1b9b6aSSam Leffler 
3718a1b9b6aSSam Leffler 		/*
3728a1b9b6aSSam Leffler 		 * Handle privacy requirements.  Note that we
3738a1b9b6aSSam Leffler 		 * must not be preempted from here until after
3748a1b9b6aSSam Leffler 		 * we (potentially) call ieee80211_crypto_demic;
3758a1b9b6aSSam Leffler 		 * otherwise we may violate assumptions in the
3768a1b9b6aSSam Leffler 		 * crypto cipher modules used to do delayed update
3778a1b9b6aSSam Leffler 		 * of replay sequence numbers.
3788a1b9b6aSSam Leffler 		 */
3791a1e1d21SSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
3808a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
3818a1b9b6aSSam Leffler 				/*
3828a1b9b6aSSam Leffler 				 * Discard encrypted frames when privacy is off.
3838a1b9b6aSSam Leffler 				 */
3848a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3858a1b9b6aSSam Leffler 				    wh, "WEP", "%s", "PRIVACY off");
3868a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_noprivacy++;
3878a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
3888a1b9b6aSSam Leffler 				goto out;
3898a1b9b6aSSam Leffler 			}
3902cc12adeSSam Leffler 			key = ieee80211_crypto_decap(ic, ni, m, hdrspace);
3918a1b9b6aSSam Leffler 			if (key == NULL) {
3928a1b9b6aSSam Leffler 				/* NB: stats+msgs handled in crypto_decap */
3938a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_wepfail);
3948a1b9b6aSSam Leffler 				goto out;
3951be50176SSam Leffler 			}
3961a1e1d21SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
3978116d318SSam Leffler 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3981be50176SSam Leffler 		} else {
3998a1b9b6aSSam Leffler 			key = NULL;
4008a1b9b6aSSam Leffler 		}
4018a1b9b6aSSam Leffler 
4028a1b9b6aSSam Leffler 		/*
4038a1b9b6aSSam Leffler 		 * Next up, any fragmentation.
4048a1b9b6aSSam Leffler 		 */
4058a1b9b6aSSam Leffler 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
4062cc12adeSSam Leffler 			m = ieee80211_defrag(ic, ni, m, hdrspace);
4078a1b9b6aSSam Leffler 			if (m == NULL) {
4088a1b9b6aSSam Leffler 				/* Fragment dropped or frame not complete yet */
4091a1e1d21SSam Leffler 				goto out;
4101a1e1d21SSam Leffler 			}
4111be50176SSam Leffler 		}
4128a1b9b6aSSam Leffler 		wh = NULL;		/* no longer valid, catch any uses */
4138a1b9b6aSSam Leffler 
4148a1b9b6aSSam Leffler 		/*
4158a1b9b6aSSam Leffler 		 * Next strip any MSDU crypto bits.
4168a1b9b6aSSam Leffler 		 */
41796d88463SSam Leffler 		if (key != NULL && !ieee80211_crypto_demic(ic, key, m, 0)) {
4188a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
4198a1b9b6aSSam Leffler 			    ni->ni_macaddr, "data", "%s", "demic error");
4208a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_demicfail);
4218a1b9b6aSSam Leffler 			goto out;
4228a1b9b6aSSam Leffler 		}
4238a1b9b6aSSam Leffler 
4241a1e1d21SSam Leffler 		/* copy to listener after decrypt */
4251a1e1d21SSam Leffler 		if (ic->ic_rawbpf)
4261a1e1d21SSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
4278a1b9b6aSSam Leffler 
4288a1b9b6aSSam Leffler 		/*
4298a1b9b6aSSam Leffler 		 * Finally, strip the 802.11 header.
4308a1b9b6aSSam Leffler 		 */
4312cc12adeSSam Leffler 		m = ieee80211_decap(ic, m, hdrspace);
4321be50176SSam Leffler 		if (m == NULL) {
4338a1b9b6aSSam Leffler 			/* don't count Null data frames as errors */
4348a1b9b6aSSam Leffler 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA)
4358a1b9b6aSSam Leffler 				goto out;
4368a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
4378a1b9b6aSSam Leffler 			    ni->ni_macaddr, "data", "%s", "decap error");
4381be50176SSam Leffler 			ic->ic_stats.is_rx_decap++;
4398a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_decap);
4401a1e1d21SSam Leffler 			goto err;
4411be50176SSam Leffler 		}
4428a1b9b6aSSam Leffler 		eh = mtod(m, struct ether_header *);
4438a1b9b6aSSam Leffler 		if (!ieee80211_node_is_authorized(ni)) {
4448a1b9b6aSSam Leffler 			/*
4458a1b9b6aSSam Leffler 			 * Deny any non-PAE frames received prior to
4468a1b9b6aSSam Leffler 			 * authorization.  For open/shared-key
4478a1b9b6aSSam Leffler 			 * authentication the port is mark authorized
4488a1b9b6aSSam Leffler 			 * after authentication completes.  For 802.1x
4498a1b9b6aSSam Leffler 			 * the port is not marked authorized by the
4508a1b9b6aSSam Leffler 			 * authenticator until the handshake has completed.
4518a1b9b6aSSam Leffler 			 */
4528a1b9b6aSSam Leffler 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
4538a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
4548a1b9b6aSSam Leffler 				    eh->ether_shost, "data",
4558a1b9b6aSSam Leffler 				    "unauthorized port: ether type 0x%x len %u",
4568a1b9b6aSSam Leffler 				    eh->ether_type, m->m_pkthdr.len);
4578a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_unauth++;
4588a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unauth);
4598a1b9b6aSSam Leffler 				goto err;
4608a1b9b6aSSam Leffler 			}
4618a1b9b6aSSam Leffler 		} else {
4628a1b9b6aSSam Leffler 			/*
4638a1b9b6aSSam Leffler 			 * When denying unencrypted frames, discard
4648a1b9b6aSSam Leffler 			 * any non-PAE frames received without encryption.
4658a1b9b6aSSam Leffler 			 */
4668a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_DROPUNENC) &&
4678a1b9b6aSSam Leffler 			    key == NULL &&
4688a1b9b6aSSam Leffler 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
4698a1b9b6aSSam Leffler 				/*
4708a1b9b6aSSam Leffler 				 * Drop unencrypted frames.
4718a1b9b6aSSam Leffler 				 */
4728a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_unencrypted++;
4738a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
4748a1b9b6aSSam Leffler 				goto out;
4758a1b9b6aSSam Leffler 			}
4768a1b9b6aSSam Leffler 		}
4771a1e1d21SSam Leffler 		ifp->if_ipackets++;
4788a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_data);
4798a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
4801a1e1d21SSam Leffler 
4811bd482efSSam Leffler 		ieee80211_deliver_data(ic, ni, m);
4821f298879SSam Leffler 		return IEEE80211_FC0_TYPE_DATA;
4831a1e1d21SSam Leffler 
4841a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
4858a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_mgmt);
4861be50176SSam Leffler 		if (dir != IEEE80211_FC1_DIR_NODS) {
487bd6f09d9SSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
488bd6f09d9SSam Leffler 			    wh, "data", "%s", "unknown dir 0x%x", dir);
4891be50176SSam Leffler 			ic->ic_stats.is_rx_wrongdir++;
4901a1e1d21SSam Leffler 			goto err;
4911be50176SSam Leffler 		}
4928a1b9b6aSSam Leffler 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
4938a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
4948a1b9b6aSSam Leffler 			    ni->ni_macaddr, "mgt", "too short: len %u",
4958a1b9b6aSSam Leffler 			    m->m_pkthdr.len);
4968a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_tooshort++;
4971a1e1d21SSam Leffler 			goto out;
4981be50176SSam Leffler 		}
4991a1e1d21SSam Leffler #ifdef IEEE80211_DEBUG
5008a1b9b6aSSam Leffler 		if ((ieee80211_msg_debug(ic) && doprint(ic, subtype)) ||
5018a1b9b6aSSam Leffler 		    ieee80211_msg_dumppkts(ic)) {
5028a1b9b6aSSam Leffler 			if_printf(ic->ic_ifp, "received %s from %s rssi %d\n",
5038a1b9b6aSSam Leffler 			    ieee80211_mgt_subtype_name[subtype >>
5048a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
5051a1e1d21SSam Leffler 			    ether_sprintf(wh->i_addr2), rssi);
5061a1e1d21SSam Leffler 		}
5078a1b9b6aSSam Leffler #endif
5088a1b9b6aSSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
5098a1b9b6aSSam Leffler 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
5108a1b9b6aSSam Leffler 				/*
5118a1b9b6aSSam Leffler 				 * Only shared key auth frames with a challenge
5128a1b9b6aSSam Leffler 				 * should be encrypted, discard all others.
5138a1b9b6aSSam Leffler 				 */
5148a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
5158a1b9b6aSSam Leffler 				    wh, ieee80211_mgt_subtype_name[subtype >>
5168a1b9b6aSSam Leffler 					IEEE80211_FC0_SUBTYPE_SHIFT],
5178a1b9b6aSSam Leffler 				    "%s", "WEP set but not permitted");
5188a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_mgtdiscard++; /* XXX */
5198a1b9b6aSSam Leffler 				goto out;
5208a1b9b6aSSam Leffler 			}
5218a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
5228a1b9b6aSSam Leffler 				/*
5238a1b9b6aSSam Leffler 				 * Discard encrypted frames when privacy is off.
5248a1b9b6aSSam Leffler 				 */
5258a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
5268a1b9b6aSSam Leffler 				    wh, "mgt", "%s", "WEP set but PRIVACY off");
5278a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_noprivacy++;
5288a1b9b6aSSam Leffler 				goto out;
5298a1b9b6aSSam Leffler 			}
5302cc12adeSSam Leffler 			hdrspace = ieee80211_hdrspace(ic, wh);
5312cc12adeSSam Leffler 			key = ieee80211_crypto_decap(ic, ni, m, hdrspace);
5328a1b9b6aSSam Leffler 			if (key == NULL) {
5338a1b9b6aSSam Leffler 				/* NB: stats+msgs handled in crypto_decap */
5348a1b9b6aSSam Leffler 				goto out;
5358a1b9b6aSSam Leffler 			}
5368116d318SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
5378116d318SSam Leffler 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
5388a1b9b6aSSam Leffler 		}
5391a1e1d21SSam Leffler 		if (ic->ic_rawbpf)
5401a1e1d21SSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
5410a915fadSSam Leffler 		(*ic->ic_recv_mgmt)(ic, m, ni, subtype, rssi, rstamp);
5421a1e1d21SSam Leffler 		m_freem(m);
5431f298879SSam Leffler 		return type;
5441a1e1d21SSam Leffler 
5451a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_CTL:
5468a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_ctrl);
5471be50176SSam Leffler 		ic->ic_stats.is_rx_ctl++;
5488a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
5498a1b9b6aSSam Leffler 			switch (subtype) {
5508a1b9b6aSSam Leffler 			case IEEE80211_FC0_SUBTYPE_PS_POLL:
5518a1b9b6aSSam Leffler 				ieee80211_recv_pspoll(ic, ni, m);
5528a1b9b6aSSam Leffler 				break;
5538a1b9b6aSSam Leffler 			}
5548a1b9b6aSSam Leffler 		}
55518f897abSSam Leffler 		goto out;
5561a1e1d21SSam Leffler 	default:
5578a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
5588a1b9b6aSSam Leffler 		    wh, NULL, "bad frame type 0x%x", type);
5591a1e1d21SSam Leffler 		/* should not come here */
5601a1e1d21SSam Leffler 		break;
5611a1e1d21SSam Leffler 	}
5621a1e1d21SSam Leffler err:
5631a1e1d21SSam Leffler 	ifp->if_ierrors++;
5641a1e1d21SSam Leffler out:
5651a1e1d21SSam Leffler 	if (m != NULL) {
5661a1e1d21SSam Leffler 		if (ic->ic_rawbpf)
5671a1e1d21SSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
5681a1e1d21SSam Leffler 		m_freem(m);
5691a1e1d21SSam Leffler 	}
5701f298879SSam Leffler 	return type;
5718a1b9b6aSSam Leffler #undef SEQ_LEQ
5721a1e1d21SSam Leffler }
5731a1e1d21SSam Leffler 
5748a1b9b6aSSam Leffler /*
5758a1b9b6aSSam Leffler  * This function reassemble fragments.
5768a1b9b6aSSam Leffler  */
5778a1b9b6aSSam Leffler static struct mbuf *
5788a1b9b6aSSam Leffler ieee80211_defrag(struct ieee80211com *ic, struct ieee80211_node *ni,
5792cc12adeSSam Leffler 	struct mbuf *m, int hdrspace)
5801a1e1d21SSam Leffler {
5818a1b9b6aSSam Leffler 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
5828a1b9b6aSSam Leffler 	struct ieee80211_frame *lwh;
5838a1b9b6aSSam Leffler 	u_int16_t rxseq;
5848a1b9b6aSSam Leffler 	u_int8_t fragno;
5858a1b9b6aSSam Leffler 	u_int8_t more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG;
5868a1b9b6aSSam Leffler 	struct mbuf *mfrag;
5878a1b9b6aSSam Leffler 
5888a1b9b6aSSam Leffler 	KASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1), ("multicast fragm?"));
5898a1b9b6aSSam Leffler 
5908a1b9b6aSSam Leffler 	rxseq = le16toh(*(u_int16_t *)wh->i_seq);
5918a1b9b6aSSam Leffler 	fragno = rxseq & IEEE80211_SEQ_FRAG_MASK;
5928a1b9b6aSSam Leffler 
5938a1b9b6aSSam Leffler 	/* Quick way out, if there's nothing to defragment */
5948a1b9b6aSSam Leffler 	if (!more_frag && fragno == 0 && ni->ni_rxfrag[0] == NULL)
5958a1b9b6aSSam Leffler 		return m;
5968a1b9b6aSSam Leffler 
5978a1b9b6aSSam Leffler 	/*
5988a1b9b6aSSam Leffler 	 * Remove frag to insure it doesn't get reaped by timer.
5998a1b9b6aSSam Leffler 	 */
6008a1b9b6aSSam Leffler 	if (ni->ni_table == NULL) {
6018a1b9b6aSSam Leffler 		/*
6028a1b9b6aSSam Leffler 		 * Should never happen.  If the node is orphaned (not in
6038a1b9b6aSSam Leffler 		 * the table) then input packets should not reach here.
6048a1b9b6aSSam Leffler 		 * Otherwise, a concurrent request that yanks the table
6058a1b9b6aSSam Leffler 		 * should be blocked by other interlocking and/or by first
6068a1b9b6aSSam Leffler 		 * shutting the driver down.  Regardless, be defensive
6078a1b9b6aSSam Leffler 		 * here and just bail
6088a1b9b6aSSam Leffler 		 */
6098a1b9b6aSSam Leffler 		/* XXX need msg+stat */
6108a1b9b6aSSam Leffler 		m_freem(m);
6118a1b9b6aSSam Leffler 		return NULL;
6128a1b9b6aSSam Leffler 	}
6138a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(ni->ni_table);
6148a1b9b6aSSam Leffler 	mfrag = ni->ni_rxfrag[0];
6158a1b9b6aSSam Leffler 	ni->ni_rxfrag[0] = NULL;
6168a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(ni->ni_table);
6178a1b9b6aSSam Leffler 
6188a1b9b6aSSam Leffler 	/*
6198a1b9b6aSSam Leffler 	 * Validate new fragment is in order and
6208a1b9b6aSSam Leffler 	 * related to the previous ones.
6218a1b9b6aSSam Leffler 	 */
6228a1b9b6aSSam Leffler 	if (mfrag != NULL) {
6238a1b9b6aSSam Leffler 		u_int16_t last_rxseq;
6248a1b9b6aSSam Leffler 
6258a1b9b6aSSam Leffler 		lwh = mtod(mfrag, struct ieee80211_frame *);
6268a1b9b6aSSam Leffler 		last_rxseq = le16toh(*(u_int16_t *)lwh->i_seq);
6278a1b9b6aSSam Leffler 		/* NB: check seq # and frag together */
6288a1b9b6aSSam Leffler 		if (rxseq != last_rxseq+1 ||
6298a1b9b6aSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) ||
6308a1b9b6aSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) {
6318a1b9b6aSSam Leffler 			/*
6328a1b9b6aSSam Leffler 			 * Unrelated fragment or no space for it,
6338a1b9b6aSSam Leffler 			 * clear current fragments.
6348a1b9b6aSSam Leffler 			 */
6358a1b9b6aSSam Leffler 			m_freem(mfrag);
6368a1b9b6aSSam Leffler 			mfrag = NULL;
6378a1b9b6aSSam Leffler 		}
6388a1b9b6aSSam Leffler 	}
6398a1b9b6aSSam Leffler 
6408a1b9b6aSSam Leffler  	if (mfrag == NULL) {
6418a1b9b6aSSam Leffler 		if (fragno != 0) {		/* !first fragment, discard */
6428a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_defrag);
6438a1b9b6aSSam Leffler 			m_freem(m);
6448a1b9b6aSSam Leffler 			return NULL;
6458a1b9b6aSSam Leffler 		}
6468a1b9b6aSSam Leffler 		mfrag = m;
6478a1b9b6aSSam Leffler 	} else {				/* concatenate */
6482cc12adeSSam Leffler 		m_adj(m, hdrspace);		/* strip header */
6498a1b9b6aSSam Leffler 		m_cat(mfrag, m);
6508a1b9b6aSSam Leffler 		/* NB: m_cat doesn't update the packet header */
6518a1b9b6aSSam Leffler 		mfrag->m_pkthdr.len += m->m_pkthdr.len;
6528a1b9b6aSSam Leffler 		/* track last seqnum and fragno */
6538a1b9b6aSSam Leffler 		lwh = mtod(mfrag, struct ieee80211_frame *);
6548a1b9b6aSSam Leffler 		*(u_int16_t *) lwh->i_seq = *(u_int16_t *) wh->i_seq;
6558a1b9b6aSSam Leffler 	}
6568a1b9b6aSSam Leffler 	if (more_frag) {			/* more to come, save */
657a0cc3f85SSam Leffler 		ni->ni_rxfragstamp = ticks;
6588a1b9b6aSSam Leffler 		ni->ni_rxfrag[0] = mfrag;
6598a1b9b6aSSam Leffler 		mfrag = NULL;
6608a1b9b6aSSam Leffler 	}
6618a1b9b6aSSam Leffler 	return mfrag;
6628a1b9b6aSSam Leffler }
6638a1b9b6aSSam Leffler 
6641bd482efSSam Leffler static void
6651bd482efSSam Leffler ieee80211_deliver_data(struct ieee80211com *ic,
6661bd482efSSam Leffler 	struct ieee80211_node *ni, struct mbuf *m)
6671bd482efSSam Leffler {
6681bd482efSSam Leffler 	struct ether_header *eh = mtod(m, struct ether_header *);
6691bd482efSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
6701bd482efSSam Leffler 
6711bd482efSSam Leffler 	/* perform as a bridge within the AP */
6721bd482efSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
6731bd482efSSam Leffler 	    (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0) {
6741bd482efSSam Leffler 		struct mbuf *m1 = NULL;
6751bd482efSSam Leffler 
6761bd482efSSam Leffler 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
6771bd482efSSam Leffler 			m1 = m_copypacket(m, M_DONTWAIT);
6781bd482efSSam Leffler 			if (m1 == NULL)
6791bd482efSSam Leffler 				ifp->if_oerrors++;
6801bd482efSSam Leffler 			else
6811bd482efSSam Leffler 				m1->m_flags |= M_MCAST;
6821bd482efSSam Leffler 		} else {
683767cee4fSSam Leffler 			/*
684767cee4fSSam Leffler 			 * Check if the destination is known; if so
685767cee4fSSam Leffler 			 * and the port is authorized dispatch directly.
686767cee4fSSam Leffler 			 */
687767cee4fSSam Leffler 			struct ieee80211_node *sta =
688767cee4fSSam Leffler 			    ieee80211_find_node(&ic->ic_sta, eh->ether_dhost);
689767cee4fSSam Leffler 			if (sta != NULL) {
690767cee4fSSam Leffler 				if (ieee80211_node_is_authorized(sta)) {
691767cee4fSSam Leffler 					/*
692767cee4fSSam Leffler 					 * Beware of sending to ourself; this
693767cee4fSSam Leffler 					 * needs to happen via the normal
694767cee4fSSam Leffler 					 * input path.
695767cee4fSSam Leffler 					 */
696767cee4fSSam Leffler 					if (sta != ic->ic_bss) {
6971bd482efSSam Leffler 						m1 = m;
6981bd482efSSam Leffler 						m = NULL;
6991bd482efSSam Leffler 					}
700767cee4fSSam Leffler 				} else {
701767cee4fSSam Leffler 					ic->ic_stats.is_rx_unauth++;
702767cee4fSSam Leffler 					IEEE80211_NODE_STAT(sta, rx_unauth);
703767cee4fSSam Leffler 				}
704767cee4fSSam Leffler 				ieee80211_free_node(sta);
7051bd482efSSam Leffler 			}
7061bd482efSSam Leffler 		}
7071bd482efSSam Leffler 		if (m1 != NULL)
7081bd482efSSam Leffler 			IF_HANDOFF(&ifp->if_snd, m1, ifp);
7091bd482efSSam Leffler 	}
7101bd482efSSam Leffler 	if (m != NULL) {
7111bd482efSSam Leffler 		if (ni->ni_vlan != 0) {
7121bd482efSSam Leffler 			/* attach vlan tag */
713d147662cSGleb Smirnoff 			VLAN_INPUT_TAG(ifp, m, ni->ni_vlan);
714d147662cSGleb Smirnoff 			if (m == NULL)
715d147662cSGleb Smirnoff 				goto out;	/* XXX goto err? */
7161bd482efSSam Leffler 		}
7171bd482efSSam Leffler 		(*ifp->if_input)(ifp, m);
7181bd482efSSam Leffler 	}
7191bd482efSSam Leffler 	return;
7201bd482efSSam Leffler   out:
7211bd482efSSam Leffler 	if (m != NULL) {
7221bd482efSSam Leffler 		if (ic->ic_rawbpf)
7231bd482efSSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
7241bd482efSSam Leffler 		m_freem(m);
7251bd482efSSam Leffler 	}
7261bd482efSSam Leffler }
7271bd482efSSam Leffler 
7288a1b9b6aSSam Leffler static struct mbuf *
7292cc12adeSSam Leffler ieee80211_decap(struct ieee80211com *ic, struct mbuf *m, int hdrlen)
7308a1b9b6aSSam Leffler {
7312cc12adeSSam Leffler 	struct ieee80211_qosframe_addr4 wh;	/* Max size address frames */
7321a1e1d21SSam Leffler 	struct ether_header *eh;
7331a1e1d21SSam Leffler 	struct llc *llc;
7341a1e1d21SSam Leffler 
7352cc12adeSSam Leffler 	if (m->m_len < hdrlen + sizeof(*llc) &&
7362cc12adeSSam Leffler 	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
7378a1b9b6aSSam Leffler 		/* XXX stat, msg */
7381a1e1d21SSam Leffler 		return NULL;
7391a1e1d21SSam Leffler 	}
7402cc12adeSSam Leffler 	memcpy(&wh, mtod(m, caddr_t), hdrlen);
7412cc12adeSSam Leffler 	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
7421a1e1d21SSam Leffler 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
7431a1e1d21SSam Leffler 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
7441a1e1d21SSam Leffler 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0) {
7452cc12adeSSam Leffler 		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
7461a1e1d21SSam Leffler 		llc = NULL;
7471a1e1d21SSam Leffler 	} else {
7482cc12adeSSam Leffler 		m_adj(m, hdrlen - sizeof(*eh));
7491a1e1d21SSam Leffler 	}
7501a1e1d21SSam Leffler 	eh = mtod(m, struct ether_header *);
7511a1e1d21SSam Leffler 	switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
7521a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_NODS:
7531a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
7541a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
7551a1e1d21SSam Leffler 		break;
7561a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_TODS:
7571a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
7581a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
7591a1e1d21SSam Leffler 		break;
7601a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
7611a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
7621a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3);
7631a1e1d21SSam Leffler 		break;
7641a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
7652cc12adeSSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
7662cc12adeSSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr4);
7672cc12adeSSam Leffler 		break;
7681a1e1d21SSam Leffler 	}
7691a1e1d21SSam Leffler #ifdef ALIGNED_POINTER
7701a1e1d21SSam Leffler 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), u_int32_t)) {
7711a1e1d21SSam Leffler 		struct mbuf *n, *n0, **np;
7721a1e1d21SSam Leffler 		caddr_t newdata;
7731a1e1d21SSam Leffler 		int off, pktlen;
7741a1e1d21SSam Leffler 
7751a1e1d21SSam Leffler 		n0 = NULL;
7761a1e1d21SSam Leffler 		np = &n0;
7771a1e1d21SSam Leffler 		off = 0;
7781a1e1d21SSam Leffler 		pktlen = m->m_pkthdr.len;
7791a1e1d21SSam Leffler 		while (pktlen > off) {
7801a1e1d21SSam Leffler 			if (n0 == NULL) {
7811a1e1d21SSam Leffler 				MGETHDR(n, M_DONTWAIT, MT_DATA);
7821a1e1d21SSam Leffler 				if (n == NULL) {
7831a1e1d21SSam Leffler 					m_freem(m);
7841a1e1d21SSam Leffler 					return NULL;
7851a1e1d21SSam Leffler 				}
7861a1e1d21SSam Leffler 				M_MOVE_PKTHDR(n, m);
7871a1e1d21SSam Leffler 				n->m_len = MHLEN;
7881a1e1d21SSam Leffler 			} else {
7891a1e1d21SSam Leffler 				MGET(n, M_DONTWAIT, MT_DATA);
7901a1e1d21SSam Leffler 				if (n == NULL) {
7911a1e1d21SSam Leffler 					m_freem(m);
7921a1e1d21SSam Leffler 					m_freem(n0);
7931a1e1d21SSam Leffler 					return NULL;
7941a1e1d21SSam Leffler 				}
7951a1e1d21SSam Leffler 				n->m_len = MLEN;
7961a1e1d21SSam Leffler 			}
7971a1e1d21SSam Leffler 			if (pktlen - off >= MINCLSIZE) {
7981a1e1d21SSam Leffler 				MCLGET(n, M_DONTWAIT);
7991a1e1d21SSam Leffler 				if (n->m_flags & M_EXT)
8001a1e1d21SSam Leffler 					n->m_len = n->m_ext.ext_size;
8011a1e1d21SSam Leffler 			}
8021a1e1d21SSam Leffler 			if (n0 == NULL) {
8031a1e1d21SSam Leffler 				newdata =
8041a1e1d21SSam Leffler 				    (caddr_t)ALIGN(n->m_data + sizeof(*eh)) -
8051a1e1d21SSam Leffler 				    sizeof(*eh);
8061a1e1d21SSam Leffler 				n->m_len -= newdata - n->m_data;
8071a1e1d21SSam Leffler 				n->m_data = newdata;
8081a1e1d21SSam Leffler 			}
8091a1e1d21SSam Leffler 			if (n->m_len > pktlen - off)
8101a1e1d21SSam Leffler 				n->m_len = pktlen - off;
8111a1e1d21SSam Leffler 			m_copydata(m, off, n->m_len, mtod(n, caddr_t));
8121a1e1d21SSam Leffler 			off += n->m_len;
8131a1e1d21SSam Leffler 			*np = n;
8141a1e1d21SSam Leffler 			np = &n->m_next;
8151a1e1d21SSam Leffler 		}
8161a1e1d21SSam Leffler 		m_freem(m);
8171a1e1d21SSam Leffler 		m = n0;
8181a1e1d21SSam Leffler 	}
8191a1e1d21SSam Leffler #endif /* ALIGNED_POINTER */
8201a1e1d21SSam Leffler 	if (llc != NULL) {
8211a1e1d21SSam Leffler 		eh = mtod(m, struct ether_header *);
8221a1e1d21SSam Leffler 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
8231a1e1d21SSam Leffler 	}
8241a1e1d21SSam Leffler 	return m;
8251a1e1d21SSam Leffler }
8261a1e1d21SSam Leffler 
8271a1e1d21SSam Leffler /*
8281a1e1d21SSam Leffler  * Install received rate set information in the node's state block.
8291a1e1d21SSam Leffler  */
8307d77cd53SSam Leffler int
8317d77cd53SSam Leffler ieee80211_setup_rates(struct ieee80211_node *ni,
8327d77cd53SSam Leffler 	const u_int8_t *rates, const u_int8_t *xrates, int flags)
8331a1e1d21SSam Leffler {
8347d77cd53SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
8351a1e1d21SSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
8361a1e1d21SSam Leffler 
8371a1e1d21SSam Leffler 	memset(rs, 0, sizeof(*rs));
8381a1e1d21SSam Leffler 	rs->rs_nrates = rates[1];
8391a1e1d21SSam Leffler 	memcpy(rs->rs_rates, rates + 2, rs->rs_nrates);
8401a1e1d21SSam Leffler 	if (xrates != NULL) {
8411a1e1d21SSam Leffler 		u_int8_t nxrates;
8421a1e1d21SSam Leffler 		/*
8431a1e1d21SSam Leffler 		 * Tack on 11g extended supported rate element.
8441a1e1d21SSam Leffler 		 */
8451a1e1d21SSam Leffler 		nxrates = xrates[1];
8461a1e1d21SSam Leffler 		if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
8471a1e1d21SSam Leffler 			nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates;
8488a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_XRATE,
8498a1b9b6aSSam Leffler 			     "[%s] extended rate set too large;"
8501a1e1d21SSam Leffler 			     " only using %u of %u rates\n",
8518a1b9b6aSSam Leffler 			     ether_sprintf(ni->ni_macaddr), nxrates, xrates[1]);
8521be50176SSam Leffler 			ic->ic_stats.is_rx_rstoobig++;
8531a1e1d21SSam Leffler 		}
8541a1e1d21SSam Leffler 		memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates);
8551a1e1d21SSam Leffler 		rs->rs_nrates += nxrates;
8561a1e1d21SSam Leffler 	}
8577d77cd53SSam Leffler 	return ieee80211_fix_rate(ni, flags);
8581a1e1d21SSam Leffler }
8591a1e1d21SSam Leffler 
8608a1b9b6aSSam Leffler static void
8618a1b9b6aSSam Leffler ieee80211_auth_open(struct ieee80211com *ic, struct ieee80211_frame *wh,
8628a1b9b6aSSam Leffler     struct ieee80211_node *ni, int rssi, u_int32_t rstamp, u_int16_t seq,
8638a1b9b6aSSam Leffler     u_int16_t status)
8648a1b9b6aSSam Leffler {
8658a1b9b6aSSam Leffler 
866b8d05d3cSSam Leffler 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
867b8d05d3cSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
868b8d05d3cSSam Leffler 		    ni->ni_macaddr, "open auth",
869b8d05d3cSSam Leffler 		    "bad sta auth mode %u", ni->ni_authmode);
870b8d05d3cSSam Leffler 		ic->ic_stats.is_rx_bad_auth++;	/* XXX */
871b8d05d3cSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
872b8d05d3cSSam Leffler 			/* XXX hack to workaround calling convention */
873b8d05d3cSSam Leffler 			ieee80211_send_error(ic, ni, wh->i_addr2,
874b8d05d3cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_AUTH,
875b8d05d3cSSam Leffler 			    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
876b8d05d3cSSam Leffler 		}
877b8d05d3cSSam Leffler 		return;
878b8d05d3cSSam Leffler 	}
8798a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
8808a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:
8818a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:
8824fd1a57dSSam Leffler 	case IEEE80211_M_MONITOR:
8838a1b9b6aSSam Leffler 		/* should not come here */
8844fd1a57dSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
8854fd1a57dSSam Leffler 		    ni->ni_macaddr, "open auth",
8864fd1a57dSSam Leffler 		    "bad operating mode %u", ic->ic_opmode);
8878a1b9b6aSSam Leffler 		break;
8888a1b9b6aSSam Leffler 
8898a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:
8908a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN ||
8918a1b9b6aSSam Leffler 		    seq != IEEE80211_AUTH_OPEN_REQUEST) {
8928a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
8938a1b9b6aSSam Leffler 			return;
8948a1b9b6aSSam Leffler 		}
8958a1b9b6aSSam Leffler 		/* always accept open authentication requests */
8968a1b9b6aSSam Leffler 		if (ni == ic->ic_bss) {
897acc4f7f5SSam Leffler 			ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
8988a1b9b6aSSam Leffler 			if (ni == NULL)
8998a1b9b6aSSam Leffler 				return;
900ebdda46cSSam Leffler 		} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
9018a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
902ebdda46cSSam Leffler 		/*
903ebdda46cSSam Leffler 		 * Mark the node as referenced to reflect that it's
904ebdda46cSSam Leffler 		 * reference count has been bumped to insure it remains
905ebdda46cSSam Leffler 		 * after the transaction completes.
906ebdda46cSSam Leffler 		 */
907ebdda46cSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_AREF;
908ebdda46cSSam Leffler 
9098a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni,
9108a1b9b6aSSam Leffler 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
9118a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
9128a1b9b6aSSam Leffler 		    "[%s] station authenticated (open)\n",
9138a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
914da17aba1SSam Leffler 		/*
915da17aba1SSam Leffler 		 * When 802.1x is not in use mark the port
916da17aba1SSam Leffler 		 * authorized at this point so traffic can flow.
917da17aba1SSam Leffler 		 */
918da17aba1SSam Leffler 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
919e4918ecdSSam Leffler 			ieee80211_node_authorize(ni);
9208a1b9b6aSSam Leffler 		break;
9218a1b9b6aSSam Leffler 
9228a1b9b6aSSam Leffler 	case IEEE80211_M_STA:
9238a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_AUTH ||
9248a1b9b6aSSam Leffler 		    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
9258a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
9268a1b9b6aSSam Leffler 			return;
9278a1b9b6aSSam Leffler 		}
9288a1b9b6aSSam Leffler 		if (status != 0) {
9298a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
9308a1b9b6aSSam Leffler 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
9318a1b9b6aSSam Leffler 			    "[%s] open auth failed (reason %d)\n",
9328a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr), status);
9338a1b9b6aSSam Leffler 			/* XXX can this happen? */
9348a1b9b6aSSam Leffler 			if (ni != ic->ic_bss)
9358a1b9b6aSSam Leffler 				ni->ni_fails++;
9368a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_fail++;
937181181acSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
938181181acSSam Leffler 		} else
9398a1b9b6aSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
9408a1b9b6aSSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
9418a1b9b6aSSam Leffler 		break;
9428a1b9b6aSSam Leffler 	}
9438a1b9b6aSSam Leffler }
9448a1b9b6aSSam Leffler 
94584eb84c4SSam Leffler /*
94684eb84c4SSam Leffler  * Send a management frame error response to the specified
94784eb84c4SSam Leffler  * station.  If ni is associated with the station then use
94884eb84c4SSam Leffler  * it; otherwise allocate a temporary node suitable for
94984eb84c4SSam Leffler  * transmitting the frame and then free the reference so
95084eb84c4SSam Leffler  * it will go away as soon as the frame has been transmitted.
95184eb84c4SSam Leffler  */
95284eb84c4SSam Leffler static void
95384eb84c4SSam Leffler ieee80211_send_error(struct ieee80211com *ic, struct ieee80211_node *ni,
95484eb84c4SSam Leffler 	const u_int8_t *mac, int subtype, int arg)
95584eb84c4SSam Leffler {
95684eb84c4SSam Leffler 	int istmp;
95784eb84c4SSam Leffler 
95884eb84c4SSam Leffler 	if (ni == ic->ic_bss) {
95997c973adSSam Leffler 		ni = ieee80211_tmp_node(ic, mac);
96084eb84c4SSam Leffler 		if (ni == NULL) {
96184eb84c4SSam Leffler 			/* XXX msg */
96284eb84c4SSam Leffler 			return;
96384eb84c4SSam Leffler 		}
96484eb84c4SSam Leffler 		istmp = 1;
96584eb84c4SSam Leffler 	} else
96684eb84c4SSam Leffler 		istmp = 0;
96784eb84c4SSam Leffler 	IEEE80211_SEND_MGMT(ic, ni, subtype, arg);
96884eb84c4SSam Leffler 	if (istmp)
96984eb84c4SSam Leffler 		ieee80211_free_node(ni);
97084eb84c4SSam Leffler }
97184eb84c4SSam Leffler 
9728a1b9b6aSSam Leffler static int
9738a1b9b6aSSam Leffler alloc_challenge(struct ieee80211com *ic, struct ieee80211_node *ni)
9748a1b9b6aSSam Leffler {
9758a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL)
9768a1b9b6aSSam Leffler 		MALLOC(ni->ni_challenge, u_int32_t*, IEEE80211_CHALLENGE_LEN,
9778a1b9b6aSSam Leffler 		    M_DEVBUF, M_NOWAIT);
9788a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL) {
9798a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
9808a1b9b6aSSam Leffler 		    "[%s] shared key challenge alloc failed\n",
9818a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
9828a1b9b6aSSam Leffler 		/* XXX statistic */
9838a1b9b6aSSam Leffler 	}
9848a1b9b6aSSam Leffler 	return (ni->ni_challenge != NULL);
9858a1b9b6aSSam Leffler }
9868a1b9b6aSSam Leffler 
9878a1b9b6aSSam Leffler /* XXX TODO: add statistics */
9888a1b9b6aSSam Leffler static void
9898a1b9b6aSSam Leffler ieee80211_auth_shared(struct ieee80211com *ic, struct ieee80211_frame *wh,
9908a1b9b6aSSam Leffler     u_int8_t *frm, u_int8_t *efrm, struct ieee80211_node *ni, int rssi,
9918a1b9b6aSSam Leffler     u_int32_t rstamp, u_int16_t seq, u_int16_t status)
9928a1b9b6aSSam Leffler {
9938a1b9b6aSSam Leffler 	u_int8_t *challenge;
9948a1b9b6aSSam Leffler 	int allocbs, estatus;
9958a1b9b6aSSam Leffler 
9968a1b9b6aSSam Leffler 	/*
9978a1b9b6aSSam Leffler 	 * NB: this can happen as we allow pre-shared key
9988a1b9b6aSSam Leffler 	 * authentication to be enabled w/o wep being turned
9998a1b9b6aSSam Leffler 	 * on so that configuration of these can be done
10008a1b9b6aSSam Leffler 	 * in any order.  It may be better to enforce the
10018a1b9b6aSSam Leffler 	 * ordering in which case this check would just be
10028a1b9b6aSSam Leffler 	 * for sanity/consistency.
10038a1b9b6aSSam Leffler 	 */
10048a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
10058a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10068a1b9b6aSSam Leffler 		    ni->ni_macaddr, "shared key auth",
10078a1b9b6aSSam Leffler 		    "%s", " PRIVACY is disabled");
10088a1b9b6aSSam Leffler 		estatus = IEEE80211_STATUS_ALG;
10098a1b9b6aSSam Leffler 		goto bad;
10108a1b9b6aSSam Leffler 	}
10118a1b9b6aSSam Leffler 	/*
10128a1b9b6aSSam Leffler 	 * Pre-shared key authentication is evil; accept
10138a1b9b6aSSam Leffler 	 * it only if explicitly configured (it is supported
10148a1b9b6aSSam Leffler 	 * mainly for compatibility with clients like OS X).
10158a1b9b6aSSam Leffler 	 */
10168a1b9b6aSSam Leffler 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
10178a1b9b6aSSam Leffler 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
10188a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10198a1b9b6aSSam Leffler 		    ni->ni_macaddr, "shared key auth",
10208a1b9b6aSSam Leffler 		    "bad sta auth mode %u", ni->ni_authmode);
10218a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
10228a1b9b6aSSam Leffler 		estatus = IEEE80211_STATUS_ALG;
10238a1b9b6aSSam Leffler 		goto bad;
10248a1b9b6aSSam Leffler 	}
10258a1b9b6aSSam Leffler 
10268a1b9b6aSSam Leffler 	challenge = NULL;
10278a1b9b6aSSam Leffler 	if (frm + 1 < efrm) {
10288a1b9b6aSSam Leffler 		if ((frm[1] + 2) > (efrm - frm)) {
10298a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10308a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10318a1b9b6aSSam Leffler 			    "ie %d/%d too long",
10328a1b9b6aSSam Leffler 			    frm[0], (frm[1] + 2) - (efrm - frm));
10338a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
10348a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
10358a1b9b6aSSam Leffler 			goto bad;
10368a1b9b6aSSam Leffler 		}
10378a1b9b6aSSam Leffler 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
10388a1b9b6aSSam Leffler 			challenge = frm;
10398a1b9b6aSSam Leffler 		frm += frm[1] + 2;
10408a1b9b6aSSam Leffler 	}
10418a1b9b6aSSam Leffler 	switch (seq) {
10428a1b9b6aSSam Leffler 	case IEEE80211_AUTH_SHARED_CHALLENGE:
10438a1b9b6aSSam Leffler 	case IEEE80211_AUTH_SHARED_RESPONSE:
10448a1b9b6aSSam Leffler 		if (challenge == NULL) {
10458a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10468a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10478a1b9b6aSSam Leffler 			    "%s", "no challenge");
10488a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
10498a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
10508a1b9b6aSSam Leffler 			goto bad;
10518a1b9b6aSSam Leffler 		}
10528a1b9b6aSSam Leffler 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
10538a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10548a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10558a1b9b6aSSam Leffler 			    "bad challenge len %d", challenge[1]);
10568a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
10578a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
10588a1b9b6aSSam Leffler 			goto bad;
10598a1b9b6aSSam Leffler 		}
10608a1b9b6aSSam Leffler 	default:
10618a1b9b6aSSam Leffler 		break;
10628a1b9b6aSSam Leffler 	}
10638a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
10648a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:
10658a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:
10668a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:
10678a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10688a1b9b6aSSam Leffler 		    ni->ni_macaddr, "shared key auth",
10698a1b9b6aSSam Leffler 		    "bad operating mode %u", ic->ic_opmode);
10708a1b9b6aSSam Leffler 		return;
10718a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:
10728a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN) {
10738a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10748a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10758a1b9b6aSSam Leffler 			    "bad state %u", ic->ic_state);
10768a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_ALG;	/* XXX */
10778a1b9b6aSSam Leffler 			goto bad;
10788a1b9b6aSSam Leffler 		}
10798a1b9b6aSSam Leffler 		switch (seq) {
10808a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_REQUEST:
10818a1b9b6aSSam Leffler 			if (ni == ic->ic_bss) {
1082acc4f7f5SSam Leffler 				ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
10838a1b9b6aSSam Leffler 				if (ni == NULL) {
10848a1b9b6aSSam Leffler 					/* NB: no way to return an error */
10858a1b9b6aSSam Leffler 					return;
10868a1b9b6aSSam Leffler 				}
10878a1b9b6aSSam Leffler 				allocbs = 1;
10888a1b9b6aSSam Leffler 			} else {
1089ebdda46cSSam Leffler 				if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
10908a1b9b6aSSam Leffler 					(void) ieee80211_ref_node(ni);
10918a1b9b6aSSam Leffler 				allocbs = 0;
10928a1b9b6aSSam Leffler 			}
1093ebdda46cSSam Leffler 			/*
1094ebdda46cSSam Leffler 			 * Mark the node as referenced to reflect that it's
1095ebdda46cSSam Leffler 			 * reference count has been bumped to insure it remains
1096ebdda46cSSam Leffler 			 * after the transaction completes.
1097ebdda46cSSam Leffler 			 */
1098ebdda46cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_AREF;
10998a1b9b6aSSam Leffler 			ni->ni_rssi = rssi;
11008a1b9b6aSSam Leffler 			ni->ni_rstamp = rstamp;
11018a1b9b6aSSam Leffler 			if (!alloc_challenge(ic, ni)) {
11028a1b9b6aSSam Leffler 				/* NB: don't return error so they rexmit */
11038a1b9b6aSSam Leffler 				return;
11048a1b9b6aSSam Leffler 			}
11058a1b9b6aSSam Leffler 			get_random_bytes(ni->ni_challenge,
11068a1b9b6aSSam Leffler 				IEEE80211_CHALLENGE_LEN);
11078a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
11088a1b9b6aSSam Leffler 				IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
11098a1b9b6aSSam Leffler 				"[%s] shared key %sauth request\n",
11108a1b9b6aSSam Leffler 				ether_sprintf(ni->ni_macaddr),
11118a1b9b6aSSam Leffler 				allocbs ? "" : "re");
11128a1b9b6aSSam Leffler 			break;
11138a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_RESPONSE:
11148a1b9b6aSSam Leffler 			if (ni == ic->ic_bss) {
11158a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
11168a1b9b6aSSam Leffler 				    ni->ni_macaddr, "shared key response",
11178a1b9b6aSSam Leffler 				    "%s", "unknown station");
11188a1b9b6aSSam Leffler 				/* NB: don't send a response */
11198a1b9b6aSSam Leffler 				return;
11208a1b9b6aSSam Leffler 			}
11218a1b9b6aSSam Leffler 			if (ni->ni_challenge == NULL) {
11228a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
11238a1b9b6aSSam Leffler 				    ni->ni_macaddr, "shared key response",
11248a1b9b6aSSam Leffler 				    "%s", "no challenge recorded");
11258a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_bad_auth++;
11268a1b9b6aSSam Leffler 				estatus = IEEE80211_STATUS_CHALLENGE;
11278a1b9b6aSSam Leffler 				goto bad;
11288a1b9b6aSSam Leffler 			}
11298a1b9b6aSSam Leffler 			if (memcmp(ni->ni_challenge, &challenge[2],
11308a1b9b6aSSam Leffler 			           challenge[1]) != 0) {
11318a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
11328a1b9b6aSSam Leffler 				    ni->ni_macaddr, "shared key response",
11338a1b9b6aSSam Leffler 				    "%s", "challenge mismatch");
11348a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_auth_fail++;
11358a1b9b6aSSam Leffler 				estatus = IEEE80211_STATUS_CHALLENGE;
11368a1b9b6aSSam Leffler 				goto bad;
11378a1b9b6aSSam Leffler 			}
11388a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
11398a1b9b6aSSam Leffler 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
11408a1b9b6aSSam Leffler 			    "[%s] station authenticated (shared key)\n",
11418a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr));
1142e4918ecdSSam Leffler 			ieee80211_node_authorize(ni);
11438a1b9b6aSSam Leffler 			break;
11448a1b9b6aSSam Leffler 		default:
11458a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
11468a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
11478a1b9b6aSSam Leffler 			    "bad seq %d", seq);
11488a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
11498a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_SEQUENCE;
11508a1b9b6aSSam Leffler 			goto bad;
11518a1b9b6aSSam Leffler 		}
11528a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni,
11538a1b9b6aSSam Leffler 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
11548a1b9b6aSSam Leffler 		break;
11558a1b9b6aSSam Leffler 
11568a1b9b6aSSam Leffler 	case IEEE80211_M_STA:
11578a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_AUTH)
11588a1b9b6aSSam Leffler 			return;
11598a1b9b6aSSam Leffler 		switch (seq) {
11608a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_PASS:
11618a1b9b6aSSam Leffler 			if (ni->ni_challenge != NULL) {
11628a1b9b6aSSam Leffler 				FREE(ni->ni_challenge, M_DEVBUF);
11638a1b9b6aSSam Leffler 				ni->ni_challenge = NULL;
11648a1b9b6aSSam Leffler 			}
11658a1b9b6aSSam Leffler 			if (status != 0) {
11668a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic,
11678a1b9b6aSSam Leffler 				    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
11688a1b9b6aSSam Leffler 				    "[%s] shared key auth failed (reason %d)\n",
11698a1b9b6aSSam Leffler 				    ether_sprintf(ieee80211_getbssid(ic, wh)),
11708a1b9b6aSSam Leffler 				    status);
11718a1b9b6aSSam Leffler 				/* XXX can this happen? */
11728a1b9b6aSSam Leffler 				if (ni != ic->ic_bss)
11738a1b9b6aSSam Leffler 					ni->ni_fails++;
11748a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_auth_fail++;
11758a1b9b6aSSam Leffler 				return;
11768a1b9b6aSSam Leffler 			}
11778a1b9b6aSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
11788a1b9b6aSSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
11798a1b9b6aSSam Leffler 			break;
11808a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_CHALLENGE:
11818a1b9b6aSSam Leffler 			if (!alloc_challenge(ic, ni))
11828a1b9b6aSSam Leffler 				return;
11838a1b9b6aSSam Leffler 			/* XXX could optimize by passing recvd challenge */
11848a1b9b6aSSam Leffler 			memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
11858a1b9b6aSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni,
11868a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
11878a1b9b6aSSam Leffler 			break;
11888a1b9b6aSSam Leffler 		default:
11898a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_AUTH,
11908a1b9b6aSSam Leffler 			    wh, "shared key auth", "bad seq %d", seq);
11918a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
11928a1b9b6aSSam Leffler 			return;
11938a1b9b6aSSam Leffler 		}
11948a1b9b6aSSam Leffler 		break;
11958a1b9b6aSSam Leffler 	}
11968a1b9b6aSSam Leffler 	return;
11978a1b9b6aSSam Leffler bad:
11988a1b9b6aSSam Leffler 	/*
11998a1b9b6aSSam Leffler 	 * Send an error response; but only when operating as an AP.
12008a1b9b6aSSam Leffler 	 */
12018a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
12028a1b9b6aSSam Leffler 		/* XXX hack to workaround calling convention */
120384eb84c4SSam Leffler 		ieee80211_send_error(ic, ni, wh->i_addr2,
12048a1b9b6aSSam Leffler 		    IEEE80211_FC0_SUBTYPE_AUTH,
12058a1b9b6aSSam Leffler 		    (seq + 1) | (estatus<<16));
1206181181acSSam Leffler 	} else if (ic->ic_opmode == IEEE80211_M_STA) {
1207181181acSSam Leffler 		/*
1208181181acSSam Leffler 		 * Kick the state machine.  This short-circuits
1209181181acSSam Leffler 		 * using the mgt frame timeout to trigger the
1210181181acSSam Leffler 		 * state transition.
1211181181acSSam Leffler 		 */
1212181181acSSam Leffler 		if (ic->ic_state == IEEE80211_S_AUTH)
1213181181acSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
12148a1b9b6aSSam Leffler 	}
12158a1b9b6aSSam Leffler }
12168a1b9b6aSSam Leffler 
12171a1e1d21SSam Leffler /* Verify the existence and length of __elem or get out. */
12181a1e1d21SSam Leffler #define IEEE80211_VERIFY_ELEMENT(__elem, __maxlen) do {			\
12191a1e1d21SSam Leffler 	if ((__elem) == NULL) {						\
12208a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
12218a1b9b6aSSam Leffler 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
12228a1b9b6aSSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
12238a1b9b6aSSam Leffler 		    "%s", "no " #__elem );				\
12241be50176SSam Leffler 		ic->ic_stats.is_rx_elem_missing++;			\
12251a1e1d21SSam Leffler 		return;							\
12261a1e1d21SSam Leffler 	}								\
12271a1e1d21SSam Leffler 	if ((__elem)[1] > (__maxlen)) {					\
12288a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
12298a1b9b6aSSam Leffler 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
12301a1e1d21SSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
12318a1b9b6aSSam Leffler 		    "bad " #__elem " len %d", (__elem)[1]);		\
12321be50176SSam Leffler 		ic->ic_stats.is_rx_elem_toobig++;			\
12331a1e1d21SSam Leffler 		return;							\
12341a1e1d21SSam Leffler 	}								\
12351a1e1d21SSam Leffler } while (0)
12361a1e1d21SSam Leffler 
12371a1e1d21SSam Leffler #define	IEEE80211_VERIFY_LENGTH(_len, _minlen) do {			\
12381a1e1d21SSam Leffler 	if ((_len) < (_minlen)) {					\
12398a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
12408a1b9b6aSSam Leffler 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
12411a1e1d21SSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
12428a1b9b6aSSam Leffler 		    "%s", "ie too short");				\
12431be50176SSam Leffler 		ic->ic_stats.is_rx_elem_toosmall++;			\
12441a1e1d21SSam Leffler 		return;							\
12451a1e1d21SSam Leffler 	}								\
12461a1e1d21SSam Leffler } while (0)
12471a1e1d21SSam Leffler 
12488a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
12498a1b9b6aSSam Leffler static void
12508a1b9b6aSSam Leffler ieee80211_ssid_mismatch(struct ieee80211com *ic, const char *tag,
12518a1b9b6aSSam Leffler 	u_int8_t mac[IEEE80211_ADDR_LEN], u_int8_t *ssid)
12528a1b9b6aSSam Leffler {
12538a1b9b6aSSam Leffler 	printf("[%s] discard %s frame, ssid mismatch: ",
12548a1b9b6aSSam Leffler 		ether_sprintf(mac), tag);
12558a1b9b6aSSam Leffler 	ieee80211_print_essid(ssid + 2, ssid[1]);
12568a1b9b6aSSam Leffler 	printf("\n");
12578a1b9b6aSSam Leffler }
12588a1b9b6aSSam Leffler 
12598a1b9b6aSSam Leffler #define	IEEE80211_VERIFY_SSID(_ni, _ssid) do {				\
12608a1b9b6aSSam Leffler 	if ((_ssid)[1] != 0 &&						\
12618a1b9b6aSSam Leffler 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
12628a1b9b6aSSam Leffler 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
12638a1b9b6aSSam Leffler 		if (ieee80211_msg_input(ic))				\
12648a1b9b6aSSam Leffler 			ieee80211_ssid_mismatch(ic, 			\
12658a1b9b6aSSam Leffler 			    ieee80211_mgt_subtype_name[subtype >>	\
12668a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],		\
12678a1b9b6aSSam Leffler 				wh->i_addr2, _ssid);			\
12688a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_ssidmismatch++;			\
12698a1b9b6aSSam Leffler 		return;							\
12708a1b9b6aSSam Leffler 	}								\
12718a1b9b6aSSam Leffler } while (0)
12728a1b9b6aSSam Leffler #else /* !IEEE80211_DEBUG */
12738a1b9b6aSSam Leffler #define	IEEE80211_VERIFY_SSID(_ni, _ssid) do {				\
12748a1b9b6aSSam Leffler 	if ((_ssid)[1] != 0 &&						\
12758a1b9b6aSSam Leffler 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
12768a1b9b6aSSam Leffler 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
12778a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_ssidmismatch++;			\
12788a1b9b6aSSam Leffler 		return;							\
12798a1b9b6aSSam Leffler 	}								\
12808a1b9b6aSSam Leffler } while (0)
12818a1b9b6aSSam Leffler #endif /* !IEEE80211_DEBUG */
12828a1b9b6aSSam Leffler 
12838a1b9b6aSSam Leffler /* unalligned little endian access */
12848a1b9b6aSSam Leffler #define LE_READ_2(p)					\
12858a1b9b6aSSam Leffler 	((u_int16_t)					\
12868a1b9b6aSSam Leffler 	 ((((const u_int8_t *)(p))[0]      ) |		\
12878a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[1] <<  8)))
12888a1b9b6aSSam Leffler #define LE_READ_4(p)					\
12898a1b9b6aSSam Leffler 	((u_int32_t)					\
12908a1b9b6aSSam Leffler 	 ((((const u_int8_t *)(p))[0]      ) |		\
12918a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[1] <<  8) |		\
12928a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[2] << 16) |		\
12938a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[3] << 24)))
12948a1b9b6aSSam Leffler 
12958a1b9b6aSSam Leffler static int __inline
12968a1b9b6aSSam Leffler iswpaoui(const u_int8_t *frm)
12978a1b9b6aSSam Leffler {
12988a1b9b6aSSam Leffler 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
12998a1b9b6aSSam Leffler }
13008a1b9b6aSSam Leffler 
13018a1b9b6aSSam Leffler static int __inline
13028a1b9b6aSSam Leffler iswmeoui(const u_int8_t *frm)
13038a1b9b6aSSam Leffler {
13048a1b9b6aSSam Leffler 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
13058a1b9b6aSSam Leffler }
13068a1b9b6aSSam Leffler 
13078a1b9b6aSSam Leffler static int __inline
13088a1b9b6aSSam Leffler iswmeparam(const u_int8_t *frm)
13098a1b9b6aSSam Leffler {
13108a1b9b6aSSam Leffler 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
13118a1b9b6aSSam Leffler 		frm[6] == WME_PARAM_OUI_SUBTYPE;
13128a1b9b6aSSam Leffler }
13138a1b9b6aSSam Leffler 
13148a1b9b6aSSam Leffler static int __inline
13158a1b9b6aSSam Leffler iswmeinfo(const u_int8_t *frm)
13168a1b9b6aSSam Leffler {
13178a1b9b6aSSam Leffler 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
13188a1b9b6aSSam Leffler 		frm[6] == WME_INFO_OUI_SUBTYPE;
13198a1b9b6aSSam Leffler }
13208a1b9b6aSSam Leffler 
13218a1b9b6aSSam Leffler static int __inline
13228a1b9b6aSSam Leffler isatherosoui(const u_int8_t *frm)
13238a1b9b6aSSam Leffler {
13248a1b9b6aSSam Leffler 	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
13258a1b9b6aSSam Leffler }
13268a1b9b6aSSam Leffler 
13278a1b9b6aSSam Leffler /*
13288a1b9b6aSSam Leffler  * Convert a WPA cipher selector OUI to an internal
13298a1b9b6aSSam Leffler  * cipher algorithm.  Where appropriate we also
13308a1b9b6aSSam Leffler  * record any key length.
13318a1b9b6aSSam Leffler  */
13328a1b9b6aSSam Leffler static int
13338a1b9b6aSSam Leffler wpa_cipher(u_int8_t *sel, u_int8_t *keylen)
13348a1b9b6aSSam Leffler {
13358a1b9b6aSSam Leffler #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
13368a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
13378a1b9b6aSSam Leffler 
13388a1b9b6aSSam Leffler 	switch (w) {
13398a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_NULL):
13408a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_NONE;
13418a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_WEP40):
13428a1b9b6aSSam Leffler 		if (keylen)
13438a1b9b6aSSam Leffler 			*keylen = 40 / NBBY;
13448a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
13458a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_WEP104):
13468a1b9b6aSSam Leffler 		if (keylen)
13478a1b9b6aSSam Leffler 			*keylen = 104 / NBBY;
13488a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
13498a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_TKIP):
13508a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_TKIP;
13518a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_CCMP):
13528a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_AES_CCM;
13538a1b9b6aSSam Leffler 	}
13548a1b9b6aSSam Leffler 	return 32;		/* NB: so 1<< is discarded */
13558a1b9b6aSSam Leffler #undef WPA_SEL
13568a1b9b6aSSam Leffler }
13578a1b9b6aSSam Leffler 
13588a1b9b6aSSam Leffler /*
13598a1b9b6aSSam Leffler  * Convert a WPA key management/authentication algorithm
13608a1b9b6aSSam Leffler  * to an internal code.
13618a1b9b6aSSam Leffler  */
13628a1b9b6aSSam Leffler static int
13638a1b9b6aSSam Leffler wpa_keymgmt(u_int8_t *sel)
13648a1b9b6aSSam Leffler {
13658a1b9b6aSSam Leffler #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
13668a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
13678a1b9b6aSSam Leffler 
13688a1b9b6aSSam Leffler 	switch (w) {
13698a1b9b6aSSam Leffler 	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
13708a1b9b6aSSam Leffler 		return WPA_ASE_8021X_UNSPEC;
13718a1b9b6aSSam Leffler 	case WPA_SEL(WPA_ASE_8021X_PSK):
13728a1b9b6aSSam Leffler 		return WPA_ASE_8021X_PSK;
13738a1b9b6aSSam Leffler 	case WPA_SEL(WPA_ASE_NONE):
13748a1b9b6aSSam Leffler 		return WPA_ASE_NONE;
13758a1b9b6aSSam Leffler 	}
13768a1b9b6aSSam Leffler 	return 0;		/* NB: so is discarded */
13778a1b9b6aSSam Leffler #undef WPA_SEL
13788a1b9b6aSSam Leffler }
13798a1b9b6aSSam Leffler 
13808a1b9b6aSSam Leffler /*
13818a1b9b6aSSam Leffler  * Parse a WPA information element to collect parameters
13828a1b9b6aSSam Leffler  * and validate the parameters against what has been
13838a1b9b6aSSam Leffler  * configured for the system.
13848a1b9b6aSSam Leffler  */
13858a1b9b6aSSam Leffler static int
13868a1b9b6aSSam Leffler ieee80211_parse_wpa(struct ieee80211com *ic, u_int8_t *frm,
13878a1b9b6aSSam Leffler 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
13888a1b9b6aSSam Leffler {
13898a1b9b6aSSam Leffler 	u_int8_t len = frm[1];
13908a1b9b6aSSam Leffler 	u_int32_t w;
13918a1b9b6aSSam Leffler 	int n;
13928a1b9b6aSSam Leffler 
13938a1b9b6aSSam Leffler 	/*
13948a1b9b6aSSam Leffler 	 * Check the length once for fixed parts: OUI, type,
13958a1b9b6aSSam Leffler 	 * version, mcast cipher, and 2 selector counts.
13968a1b9b6aSSam Leffler 	 * Other, variable-length data, must be checked separately.
13978a1b9b6aSSam Leffler 	 */
1398bdad3a10SSam Leffler 	if ((ic->ic_flags & IEEE80211_F_WPA1) == 0) {
1399bdad3a10SSam Leffler 		IEEE80211_DISCARD_IE(ic,
1400bdad3a10SSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1401bdad3a10SSam Leffler 		    wh, "WPA", "not WPA, flags 0x%x", ic->ic_flags);
1402bdad3a10SSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1403bdad3a10SSam Leffler 	}
14048a1b9b6aSSam Leffler 	if (len < 14) {
14058a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14068a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14078a1b9b6aSSam Leffler 		    wh, "WPA", "too short, len %u", len);
14088a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14098a1b9b6aSSam Leffler 	}
14108a1b9b6aSSam Leffler 	frm += 6, len -= 4;		/* NB: len is payload only */
14118a1b9b6aSSam Leffler 	/* NB: iswapoui already validated the OUI and type */
14128a1b9b6aSSam Leffler 	w = LE_READ_2(frm);
14138a1b9b6aSSam Leffler 	if (w != WPA_VERSION) {
14148a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14158a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14168a1b9b6aSSam Leffler 		    wh, "WPA", "bad version %u", w);
14178a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14188a1b9b6aSSam Leffler 	}
14198a1b9b6aSSam Leffler 	frm += 2, len -= 2;
14208a1b9b6aSSam Leffler 
14218a1b9b6aSSam Leffler 	/* multicast/group cipher */
14228a1b9b6aSSam Leffler 	w = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
14238a1b9b6aSSam Leffler 	if (w != rsn->rsn_mcastcipher) {
14248a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14258a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14268a1b9b6aSSam Leffler 		    wh, "WPA", "mcast cipher mismatch; got %u, expected %u",
14278a1b9b6aSSam Leffler 		    w, rsn->rsn_mcastcipher);
14288a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14298a1b9b6aSSam Leffler 	}
14308a1b9b6aSSam Leffler 	frm += 4, len -= 4;
14318a1b9b6aSSam Leffler 
14328a1b9b6aSSam Leffler 	/* unicast ciphers */
14338a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
14348a1b9b6aSSam Leffler 	frm += 2, len -= 2;
14358a1b9b6aSSam Leffler 	if (len < n*4+2) {
14368a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14378a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14388a1b9b6aSSam Leffler 		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
14398a1b9b6aSSam Leffler 		    len, n);
14408a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14418a1b9b6aSSam Leffler 	}
14428a1b9b6aSSam Leffler 	w = 0;
14438a1b9b6aSSam Leffler 	for (; n > 0; n--) {
14448a1b9b6aSSam Leffler 		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
14458a1b9b6aSSam Leffler 		frm += 4, len -= 4;
14468a1b9b6aSSam Leffler 	}
14478a1b9b6aSSam Leffler 	w &= rsn->rsn_ucastcipherset;
14488a1b9b6aSSam Leffler 	if (w == 0) {
14498a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14508a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14518a1b9b6aSSam Leffler 		    wh, "WPA", "%s", "ucast cipher set empty");
14528a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14538a1b9b6aSSam Leffler 	}
14548a1b9b6aSSam Leffler 	if (w & (1<<IEEE80211_CIPHER_TKIP))
14558a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
14568a1b9b6aSSam Leffler 	else
14578a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
14588a1b9b6aSSam Leffler 
14598a1b9b6aSSam Leffler 	/* key management algorithms */
14608a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
14618a1b9b6aSSam Leffler 	frm += 2, len -= 2;
14628a1b9b6aSSam Leffler 	if (len < n*4) {
14638a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14648a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14658a1b9b6aSSam Leffler 		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
14668a1b9b6aSSam Leffler 		    len, n);
14678a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14688a1b9b6aSSam Leffler 	}
14698a1b9b6aSSam Leffler 	w = 0;
14708a1b9b6aSSam Leffler 	for (; n > 0; n--) {
14718a1b9b6aSSam Leffler 		w |= wpa_keymgmt(frm);
14728a1b9b6aSSam Leffler 		frm += 4, len -= 4;
14738a1b9b6aSSam Leffler 	}
14748a1b9b6aSSam Leffler 	w &= rsn->rsn_keymgmtset;
14758a1b9b6aSSam Leffler 	if (w == 0) {
14768a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14778a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14788a1b9b6aSSam Leffler 		    wh, "WPA", "%s", "no acceptable key mgmt alg");
14798a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14808a1b9b6aSSam Leffler 	}
14818a1b9b6aSSam Leffler 	if (w & WPA_ASE_8021X_UNSPEC)
14828a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
14838a1b9b6aSSam Leffler 	else
14848a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
14858a1b9b6aSSam Leffler 
14868a1b9b6aSSam Leffler 	if (len > 2)		/* optional capabilities */
14878a1b9b6aSSam Leffler 		rsn->rsn_caps = LE_READ_2(frm);
14888a1b9b6aSSam Leffler 
14898a1b9b6aSSam Leffler 	return 0;
14908a1b9b6aSSam Leffler }
14918a1b9b6aSSam Leffler 
14928a1b9b6aSSam Leffler /*
14938a1b9b6aSSam Leffler  * Convert an RSN cipher selector OUI to an internal
14948a1b9b6aSSam Leffler  * cipher algorithm.  Where appropriate we also
14958a1b9b6aSSam Leffler  * record any key length.
14968a1b9b6aSSam Leffler  */
14978a1b9b6aSSam Leffler static int
14988a1b9b6aSSam Leffler rsn_cipher(u_int8_t *sel, u_int8_t *keylen)
14998a1b9b6aSSam Leffler {
15008a1b9b6aSSam Leffler #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
15018a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
15028a1b9b6aSSam Leffler 
15038a1b9b6aSSam Leffler 	switch (w) {
15048a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_NULL):
15058a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_NONE;
15068a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_WEP40):
15078a1b9b6aSSam Leffler 		if (keylen)
15088a1b9b6aSSam Leffler 			*keylen = 40 / NBBY;
15098a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
15108a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_WEP104):
15118a1b9b6aSSam Leffler 		if (keylen)
15128a1b9b6aSSam Leffler 			*keylen = 104 / NBBY;
15138a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
15148a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_TKIP):
15158a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_TKIP;
15168a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_CCMP):
15178a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_AES_CCM;
15188a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_WRAP):
15198a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_AES_OCB;
15208a1b9b6aSSam Leffler 	}
15218a1b9b6aSSam Leffler 	return 32;		/* NB: so 1<< is discarded */
15228a1b9b6aSSam Leffler #undef WPA_SEL
15238a1b9b6aSSam Leffler }
15248a1b9b6aSSam Leffler 
15258a1b9b6aSSam Leffler /*
15268a1b9b6aSSam Leffler  * Convert an RSN key management/authentication algorithm
15278a1b9b6aSSam Leffler  * to an internal code.
15288a1b9b6aSSam Leffler  */
15298a1b9b6aSSam Leffler static int
15308a1b9b6aSSam Leffler rsn_keymgmt(u_int8_t *sel)
15318a1b9b6aSSam Leffler {
15328a1b9b6aSSam Leffler #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
15338a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
15348a1b9b6aSSam Leffler 
15358a1b9b6aSSam Leffler 	switch (w) {
15368a1b9b6aSSam Leffler 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
15378a1b9b6aSSam Leffler 		return RSN_ASE_8021X_UNSPEC;
15388a1b9b6aSSam Leffler 	case RSN_SEL(RSN_ASE_8021X_PSK):
15398a1b9b6aSSam Leffler 		return RSN_ASE_8021X_PSK;
15408a1b9b6aSSam Leffler 	case RSN_SEL(RSN_ASE_NONE):
15418a1b9b6aSSam Leffler 		return RSN_ASE_NONE;
15428a1b9b6aSSam Leffler 	}
15438a1b9b6aSSam Leffler 	return 0;		/* NB: so is discarded */
15448a1b9b6aSSam Leffler #undef RSN_SEL
15458a1b9b6aSSam Leffler }
15468a1b9b6aSSam Leffler 
15478a1b9b6aSSam Leffler /*
15488a1b9b6aSSam Leffler  * Parse a WPA/RSN information element to collect parameters
15498a1b9b6aSSam Leffler  * and validate the parameters against what has been
15508a1b9b6aSSam Leffler  * configured for the system.
15518a1b9b6aSSam Leffler  */
15528a1b9b6aSSam Leffler static int
15538a1b9b6aSSam Leffler ieee80211_parse_rsn(struct ieee80211com *ic, u_int8_t *frm,
15548a1b9b6aSSam Leffler 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
15558a1b9b6aSSam Leffler {
15568a1b9b6aSSam Leffler 	u_int8_t len = frm[1];
15578a1b9b6aSSam Leffler 	u_int32_t w;
15588a1b9b6aSSam Leffler 	int n;
15598a1b9b6aSSam Leffler 
15608a1b9b6aSSam Leffler 	/*
15618a1b9b6aSSam Leffler 	 * Check the length once for fixed parts:
15628a1b9b6aSSam Leffler 	 * version, mcast cipher, and 2 selector counts.
15638a1b9b6aSSam Leffler 	 * Other, variable-length data, must be checked separately.
15648a1b9b6aSSam Leffler 	 */
1565bdad3a10SSam Leffler 	if ((ic->ic_flags & IEEE80211_F_WPA2) == 0) {
1566bdad3a10SSam Leffler 		IEEE80211_DISCARD_IE(ic,
1567bdad3a10SSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1568bdad3a10SSam Leffler 		    wh, "WPA", "not RSN, flags 0x%x", ic->ic_flags);
1569bdad3a10SSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1570bdad3a10SSam Leffler 	}
15718a1b9b6aSSam Leffler 	if (len < 10) {
15728a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15738a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15748a1b9b6aSSam Leffler 		    wh, "RSN", "too short, len %u", len);
15758a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15768a1b9b6aSSam Leffler 	}
15778a1b9b6aSSam Leffler 	frm += 2;
15788a1b9b6aSSam Leffler 	w = LE_READ_2(frm);
15798a1b9b6aSSam Leffler 	if (w != RSN_VERSION) {
15808a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15818a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15828a1b9b6aSSam Leffler 		    wh, "RSN", "bad version %u", w);
15838a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15848a1b9b6aSSam Leffler 	}
15858a1b9b6aSSam Leffler 	frm += 2, len -= 2;
15868a1b9b6aSSam Leffler 
15878a1b9b6aSSam Leffler 	/* multicast/group cipher */
15888a1b9b6aSSam Leffler 	w = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
15898a1b9b6aSSam Leffler 	if (w != rsn->rsn_mcastcipher) {
15908a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15918a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15928a1b9b6aSSam Leffler 		    wh, "RSN", "mcast cipher mismatch; got %u, expected %u",
15938a1b9b6aSSam Leffler 		    w, rsn->rsn_mcastcipher);
15948a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15958a1b9b6aSSam Leffler 	}
15968a1b9b6aSSam Leffler 	frm += 4, len -= 4;
15978a1b9b6aSSam Leffler 
15988a1b9b6aSSam Leffler 	/* unicast ciphers */
15998a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
16008a1b9b6aSSam Leffler 	frm += 2, len -= 2;
16018a1b9b6aSSam Leffler 	if (len < n*4+2) {
16028a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
16038a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
16048a1b9b6aSSam Leffler 		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
16058a1b9b6aSSam Leffler 		    len, n);
16068a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
16078a1b9b6aSSam Leffler 	}
16088a1b9b6aSSam Leffler 	w = 0;
16098a1b9b6aSSam Leffler 	for (; n > 0; n--) {
16108a1b9b6aSSam Leffler 		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
16118a1b9b6aSSam Leffler 		frm += 4, len -= 4;
16128a1b9b6aSSam Leffler 	}
16138a1b9b6aSSam Leffler 	w &= rsn->rsn_ucastcipherset;
16148a1b9b6aSSam Leffler 	if (w == 0) {
16158a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
16168a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
16178a1b9b6aSSam Leffler 		    wh, "RSN", "%s", "ucast cipher set empty");
16188a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
16198a1b9b6aSSam Leffler 	}
16208a1b9b6aSSam Leffler 	if (w & (1<<IEEE80211_CIPHER_TKIP))
16218a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
16228a1b9b6aSSam Leffler 	else
16238a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
16248a1b9b6aSSam Leffler 
16258a1b9b6aSSam Leffler 	/* key management algorithms */
16268a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
16278a1b9b6aSSam Leffler 	frm += 2, len -= 2;
16288a1b9b6aSSam Leffler 	if (len < n*4) {
16298a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
16308a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
16318a1b9b6aSSam Leffler 		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
16328a1b9b6aSSam Leffler 		    len, n);
16338a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
16348a1b9b6aSSam Leffler 	}
16358a1b9b6aSSam Leffler 	w = 0;
16368a1b9b6aSSam Leffler 	for (; n > 0; n--) {
16378a1b9b6aSSam Leffler 		w |= rsn_keymgmt(frm);
16388a1b9b6aSSam Leffler 		frm += 4, len -= 4;
16398a1b9b6aSSam Leffler 	}
16408a1b9b6aSSam Leffler 	w &= rsn->rsn_keymgmtset;
16418a1b9b6aSSam Leffler 	if (w == 0) {
16428a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
16438a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
16448a1b9b6aSSam Leffler 		    wh, "RSN", "%s", "no acceptable key mgmt alg");
16458a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
16468a1b9b6aSSam Leffler 	}
16478a1b9b6aSSam Leffler 	if (w & RSN_ASE_8021X_UNSPEC)
16488a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
16498a1b9b6aSSam Leffler 	else
16508a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
16518a1b9b6aSSam Leffler 
16528a1b9b6aSSam Leffler 	/* optional RSN capabilities */
16538a1b9b6aSSam Leffler 	if (len > 2)
16548a1b9b6aSSam Leffler 		rsn->rsn_caps = LE_READ_2(frm);
16558a1b9b6aSSam Leffler 	/* XXXPMKID */
16568a1b9b6aSSam Leffler 
16578a1b9b6aSSam Leffler 	return 0;
16588a1b9b6aSSam Leffler }
16598a1b9b6aSSam Leffler 
16608a1b9b6aSSam Leffler static int
16618a1b9b6aSSam Leffler ieee80211_parse_wmeparams(struct ieee80211com *ic, u_int8_t *frm,
16628a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh)
16638a1b9b6aSSam Leffler {
16648a1b9b6aSSam Leffler #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
16658a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
16668a1b9b6aSSam Leffler 	u_int len = frm[1], qosinfo;
16678a1b9b6aSSam Leffler 	int i;
16688a1b9b6aSSam Leffler 
16698a1b9b6aSSam Leffler 	if (len < sizeof(struct ieee80211_wme_param)-2) {
16708a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
16718a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
16728a1b9b6aSSam Leffler 		    wh, "WME", "too short, len %u", len);
1673c0fa32ceSSam Leffler 		return -1;
16748a1b9b6aSSam Leffler 	}
16758a1b9b6aSSam Leffler 	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
16768a1b9b6aSSam Leffler 	qosinfo &= WME_QOSINFO_COUNT;
16778a1b9b6aSSam Leffler 	/* XXX do proper check for wraparound */
16788a1b9b6aSSam Leffler 	if (qosinfo == wme->wme_wmeChanParams.cap_info)
16798a1b9b6aSSam Leffler 		return 0;
16808a1b9b6aSSam Leffler 	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
16818a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
16828a1b9b6aSSam Leffler 		struct wmeParams *wmep =
16838a1b9b6aSSam Leffler 			&wme->wme_wmeChanParams.cap_wmeParams[i];
16848a1b9b6aSSam Leffler 		/* NB: ACI not used */
16858a1b9b6aSSam Leffler 		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
16868a1b9b6aSSam Leffler 		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
16878a1b9b6aSSam Leffler 		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
16888a1b9b6aSSam Leffler 		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
16898a1b9b6aSSam Leffler 		wmep->wmep_txopLimit = LE_READ_2(frm+2);
16908a1b9b6aSSam Leffler 		frm += 4;
16918a1b9b6aSSam Leffler 	}
16928a1b9b6aSSam Leffler 	wme->wme_wmeChanParams.cap_info = qosinfo;
16938a1b9b6aSSam Leffler 	return 1;
16948a1b9b6aSSam Leffler #undef MS
16958a1b9b6aSSam Leffler }
16968a1b9b6aSSam Leffler 
1697b5c99415SSam Leffler void
16988a1b9b6aSSam Leffler ieee80211_saveie(u_int8_t **iep, const u_int8_t *ie)
16998a1b9b6aSSam Leffler {
17008a1b9b6aSSam Leffler 	u_int ielen = ie[1]+2;
17018a1b9b6aSSam Leffler 	/*
17028a1b9b6aSSam Leffler 	 * Record information element for later use.
17038a1b9b6aSSam Leffler 	 */
17048a1b9b6aSSam Leffler 	if (*iep == NULL || (*iep)[1] != ie[1]) {
17058a1b9b6aSSam Leffler 		if (*iep != NULL)
17068a1b9b6aSSam Leffler 			FREE(*iep, M_DEVBUF);
17078a1b9b6aSSam Leffler 		MALLOC(*iep, void*, ielen, M_DEVBUF, M_NOWAIT);
17088a1b9b6aSSam Leffler 	}
17098a1b9b6aSSam Leffler 	if (*iep != NULL)
17108a1b9b6aSSam Leffler 		memcpy(*iep, ie, ielen);
17118a1b9b6aSSam Leffler 	/* XXX note failure */
17128a1b9b6aSSam Leffler }
17138a1b9b6aSSam Leffler 
17141a1e1d21SSam Leffler void
17150a915fadSSam Leffler ieee80211_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
17160a915fadSSam Leffler 	struct ieee80211_node *ni,
17170a915fadSSam Leffler 	int subtype, int rssi, u_int32_t rstamp)
17181a1e1d21SSam Leffler {
17198a1b9b6aSSam Leffler #define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
17208a1b9b6aSSam Leffler #define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
17211a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
17221a1e1d21SSam Leffler 	u_int8_t *frm, *efrm;
17238a1b9b6aSSam Leffler 	u_int8_t *ssid, *rates, *xrates, *wpa, *wme;
17248a1b9b6aSSam Leffler 	int reassoc, resp, allocbs;
172549d4c02fSSam Leffler 	u_int8_t rate;
17261a1e1d21SSam Leffler 
17271a1e1d21SSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
17281a1e1d21SSam Leffler 	frm = (u_int8_t *)&wh[1];
17291a1e1d21SSam Leffler 	efrm = mtod(m0, u_int8_t *) + m0->m_len;
17301a1e1d21SSam Leffler 	switch (subtype) {
17311a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
17321a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON: {
1733b5c99415SSam Leffler 		struct ieee80211_scanparams scan;
17341a1e1d21SSam Leffler 
17358a1b9b6aSSam Leffler 		/*
17368a1b9b6aSSam Leffler 		 * We process beacon/probe response frames:
17378a1b9b6aSSam Leffler 		 *    o when scanning, or
17388a1b9b6aSSam Leffler 		 *    o station mode when associated (to collect state
17398a1b9b6aSSam Leffler 		 *      updates such as 802.11g slot time), or
17408a1b9b6aSSam Leffler 		 *    o adhoc mode (to discover neighbors)
17418a1b9b6aSSam Leffler 		 * Frames otherwise received are discarded.
17428a1b9b6aSSam Leffler 		 */
17438a1b9b6aSSam Leffler 		if (!((ic->ic_flags & IEEE80211_F_SCAN) ||
17448a1b9b6aSSam Leffler 		      (ic->ic_opmode == IEEE80211_M_STA && ni->ni_associd) ||
17455784a371SSam Leffler 		       ic->ic_opmode == IEEE80211_M_IBSS)) {
17468a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
17471a1e1d21SSam Leffler 			return;
17481a1e1d21SSam Leffler 		}
17491a1e1d21SSam Leffler 		/*
17501a1e1d21SSam Leffler 		 * beacon/probe response frame format
17511a1e1d21SSam Leffler 		 *	[8] time stamp
17521a1e1d21SSam Leffler 		 *	[2] beacon interval
17531a1e1d21SSam Leffler 		 *	[2] capability information
17541a1e1d21SSam Leffler 		 *	[tlv] ssid
17551a1e1d21SSam Leffler 		 *	[tlv] supported rates
17561a1e1d21SSam Leffler 		 *	[tlv] country information
17571a1e1d21SSam Leffler 		 *	[tlv] parameter set (FH/DS)
17581a1e1d21SSam Leffler 		 *	[tlv] erp information
17591a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
17608a1b9b6aSSam Leffler 		 *	[tlv] WME
17618a1b9b6aSSam Leffler 		 *	[tlv] WPA or RSN
17621a1e1d21SSam Leffler 		 */
17631a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 12);
1764b5c99415SSam Leffler 		memset(&scan, 0, sizeof(scan));
1765b5c99415SSam Leffler 		scan.tstamp  = frm;				frm += 8;
1766b5c99415SSam Leffler 		scan.bintval = le16toh(*(u_int16_t *)frm);	frm += 2;
1767b5c99415SSam Leffler 		scan.capinfo = le16toh(*(u_int16_t *)frm);	frm += 2;
1768b5c99415SSam Leffler 		scan.bchan = ieee80211_chan2ieee(ic, ic->ic_curchan);
1769b5c99415SSam Leffler 		scan.chan = scan.bchan;
1770b5c99415SSam Leffler 
17711a1e1d21SSam Leffler 		while (frm < efrm) {
1772336ec6a1SSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1]);
17731a1e1d21SSam Leffler 			switch (*frm) {
17741a1e1d21SSam Leffler 			case IEEE80211_ELEMID_SSID:
1775b5c99415SSam Leffler 				scan.ssid = frm;
17761a1e1d21SSam Leffler 				break;
17771a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
1778b5c99415SSam Leffler 				scan.rates = frm;
17791a1e1d21SSam Leffler 				break;
17801a1e1d21SSam Leffler 			case IEEE80211_ELEMID_COUNTRY:
1781b5c99415SSam Leffler 				scan.country = frm;
17821a1e1d21SSam Leffler 				break;
17831a1e1d21SSam Leffler 			case IEEE80211_ELEMID_FHPARMS:
17841a1e1d21SSam Leffler 				if (ic->ic_phytype == IEEE80211_T_FH) {
1785b5c99415SSam Leffler 					scan.fhdwell = LE_READ_2(&frm[2]);
1786b5c99415SSam Leffler 					scan.chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
1787b5c99415SSam Leffler 					scan.fhindex = frm[6];
17881a1e1d21SSam Leffler 				}
17891a1e1d21SSam Leffler 				break;
17901a1e1d21SSam Leffler 			case IEEE80211_ELEMID_DSPARMS:
17911a1e1d21SSam Leffler 				/*
17921a1e1d21SSam Leffler 				 * XXX hack this since depending on phytype
17931a1e1d21SSam Leffler 				 * is problematic for multi-mode devices.
17941a1e1d21SSam Leffler 				 */
17951a1e1d21SSam Leffler 				if (ic->ic_phytype != IEEE80211_T_FH)
1796b5c99415SSam Leffler 					scan.chan = frm[2];
17971a1e1d21SSam Leffler 				break;
17981a1e1d21SSam Leffler 			case IEEE80211_ELEMID_TIM:
17998a1b9b6aSSam Leffler 				/* XXX ATIM? */
1800b5c99415SSam Leffler 				scan.tim = frm;
1801b5c99415SSam Leffler 				scan.timoff = frm - mtod(m0, u_int8_t *);
18021a1e1d21SSam Leffler 				break;
18034bd067c5SSam Leffler 			case IEEE80211_ELEMID_IBSSPARMS:
18044bd067c5SSam Leffler 				break;
18051a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
1806b5c99415SSam Leffler 				scan.xrates = frm;
18071a1e1d21SSam Leffler 				break;
18081a1e1d21SSam Leffler 			case IEEE80211_ELEMID_ERP:
18091a1e1d21SSam Leffler 				if (frm[1] != 1) {
18108a1b9b6aSSam Leffler 					IEEE80211_DISCARD_IE(ic,
18118a1b9b6aSSam Leffler 					    IEEE80211_MSG_ELEMID, wh, "ERP",
18128a1b9b6aSSam Leffler 					    "bad len %u", frm[1]);
18131be50176SSam Leffler 					ic->ic_stats.is_rx_elem_toobig++;
18141a1e1d21SSam Leffler 					break;
18151a1e1d21SSam Leffler 				}
1816b5c99415SSam Leffler 				scan.erp = frm[2];
18171a1e1d21SSam Leffler 				break;
18188a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_RSN:
1819b5c99415SSam Leffler 				scan.wpa = frm;
18208a1b9b6aSSam Leffler 				break;
18218a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
18228a1b9b6aSSam Leffler 				if (iswpaoui(frm))
1823b5c99415SSam Leffler 					scan.wpa = frm;
18248a1b9b6aSSam Leffler 				else if (iswmeparam(frm) || iswmeinfo(frm))
1825b5c99415SSam Leffler 					scan.wme = frm;
18268a1b9b6aSSam Leffler 				/* XXX Atheros OUI support */
18278a1b9b6aSSam Leffler 				break;
18281a1e1d21SSam Leffler 			default:
18298a1b9b6aSSam Leffler 				IEEE80211_DISCARD_IE(ic, IEEE80211_MSG_ELEMID,
18308a1b9b6aSSam Leffler 				    wh, "unhandled",
18318a1b9b6aSSam Leffler 				    "id %u, len %u", *frm, frm[1]);
18321be50176SSam Leffler 				ic->ic_stats.is_rx_elem_unknown++;
18331a1e1d21SSam Leffler 				break;
18341a1e1d21SSam Leffler 			}
18351a1e1d21SSam Leffler 			frm += frm[1] + 2;
18361a1e1d21SSam Leffler 		}
1837b5c99415SSam Leffler 		IEEE80211_VERIFY_ELEMENT(scan.rates, IEEE80211_RATE_MAXSIZE);
1838b5c99415SSam Leffler 		IEEE80211_VERIFY_ELEMENT(scan.ssid, IEEE80211_NWID_LEN);
1839a11c9a5cSSam Leffler 		if (
1840a11c9a5cSSam Leffler #if IEEE80211_CHAN_MAX < 255
1841b5c99415SSam Leffler 		    scan.chan > IEEE80211_CHAN_MAX ||
1842a11c9a5cSSam Leffler #endif
1843b5c99415SSam Leffler 		    isclr(ic->ic_chan_active, scan.chan)) {
1844b5c99415SSam Leffler 			IEEE80211_DISCARD(ic,
1845b5c99415SSam Leffler 			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
18468a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
18478a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
1848b5c99415SSam Leffler 			    "invalid channel %u", scan.chan);
18491be50176SSam Leffler 			ic->ic_stats.is_rx_badchan++;
18501a1e1d21SSam Leffler 			return;
18511a1e1d21SSam Leffler 		}
1852b5c99415SSam Leffler 		if (scan.chan != scan.bchan &&
1853b5c99415SSam Leffler 		    ic->ic_phytype != IEEE80211_T_FH) {
18541a1e1d21SSam Leffler 			/*
18551a1e1d21SSam Leffler 			 * Frame was received on a channel different from the
18564844aa7dSAtsushi Onoe 			 * one indicated in the DS params element id;
18571a1e1d21SSam Leffler 			 * silently discard it.
18581a1e1d21SSam Leffler 			 *
18591a1e1d21SSam Leffler 			 * NB: this can happen due to signal leakage.
18604844aa7dSAtsushi Onoe 			 *     But we should take it for FH phy because
18614844aa7dSAtsushi Onoe 			 *     the rssi value should be correct even for
18624844aa7dSAtsushi Onoe 			 *     different hop pattern in FH.
18631a1e1d21SSam Leffler 			 */
1864d365f9c7SSam Leffler 			IEEE80211_DISCARD(ic,
1865d365f9c7SSam Leffler 			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
1866d365f9c7SSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
1867d365f9c7SSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
1868b5c99415SSam Leffler 			    "for off-channel %u", scan.chan);
1869b5c99415SSam Leffler 			ic->ic_stats.is_rx_chanmismatch++;
1870b5c99415SSam Leffler 			return;
1871b5c99415SSam Leffler 		}
1872b5c99415SSam Leffler 		if (!(IEEE80211_BINTVAL_MIN <= scan.bintval &&
1873b5c99415SSam Leffler 		      scan.bintval <= IEEE80211_BINTVAL_MAX)) {
1874b5c99415SSam Leffler 			IEEE80211_DISCARD(ic,
1875b5c99415SSam Leffler 			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
1876b5c99415SSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
1877b5c99415SSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
1878b5c99415SSam Leffler 			    "bogus beacon interval", scan.bintval);
1879d365f9c7SSam Leffler 			ic->ic_stats.is_rx_badbintval++;
1880d365f9c7SSam Leffler 			return;
1881d365f9c7SSam Leffler 		}
18821a1e1d21SSam Leffler 
18831a1e1d21SSam Leffler 		/*
188444c72e42SSam Leffler 		 * Count frame now that we know it's to be processed.
188544c72e42SSam Leffler 		 */
188644c72e42SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
188744c72e42SSam Leffler 			ic->ic_stats.is_rx_beacon++;		/* XXX remove */
188844c72e42SSam Leffler 			IEEE80211_NODE_STAT(ni, rx_beacons);
188944c72e42SSam Leffler 		} else
189044c72e42SSam Leffler 			IEEE80211_NODE_STAT(ni, rx_proberesp);
189144c72e42SSam Leffler 
189244c72e42SSam Leffler 		/*
18938a1b9b6aSSam Leffler 		 * When operating in station mode, check for state updates.
18948a1b9b6aSSam Leffler 		 * Be careful to ignore beacons received while doing a
18958a1b9b6aSSam Leffler 		 * background scan.  We consider only 11g/WMM stuff right now.
18961a1e1d21SSam Leffler 		 */
18978a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA &&
18988a1b9b6aSSam Leffler 		    ni->ni_associd != 0 &&
18998a1b9b6aSSam Leffler 		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
19008a1b9b6aSSam Leffler 		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1901f1e95a23SSam Leffler 			/* record tsf of last beacon */
1902b5c99415SSam Leffler 			memcpy(ni->ni_tstamp.data, scan.tstamp,
1903f1e95a23SSam Leffler 				sizeof(ni->ni_tstamp));
1904e99662a6SSam Leffler 			/* count beacon frame for s/w bmiss handling */
1905e99662a6SSam Leffler 			ic->ic_swbmiss_count++;
1906e99662a6SSam Leffler 			ic->ic_bmiss_count = 0;
1907b5c99415SSam Leffler 			if (ni->ni_erp != scan.erp) {
19088a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
19098a1b9b6aSSam Leffler 				    "[%s] erp change: was 0x%x, now 0x%x\n",
19108a1b9b6aSSam Leffler 				    ether_sprintf(wh->i_addr2),
1911b5c99415SSam Leffler 				    ni->ni_erp, scan.erp);
1912ec425115SSam Leffler 				if (ic->ic_curmode == IEEE80211_MODE_11G &&
1913ec425115SSam Leffler 				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
19148a1b9b6aSSam Leffler 					ic->ic_flags |= IEEE80211_F_USEPROT;
19158a1b9b6aSSam Leffler 				else
19168a1b9b6aSSam Leffler 					ic->ic_flags &= ~IEEE80211_F_USEPROT;
1917b5c99415SSam Leffler 				ni->ni_erp = scan.erp;
19188a1b9b6aSSam Leffler 				/* XXX statistic */
19191a1e1d21SSam Leffler 			}
1920b5c99415SSam Leffler 			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
19218a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
19228a1b9b6aSSam Leffler 				    "[%s] capabilities change: before 0x%x,"
19238a1b9b6aSSam Leffler 				     " now 0x%x\n",
19248a1b9b6aSSam Leffler 				     ether_sprintf(wh->i_addr2),
1925b5c99415SSam Leffler 				     ni->ni_capinfo, scan.capinfo);
19268a1b9b6aSSam Leffler 				/*
19278a1b9b6aSSam Leffler 				 * NB: we assume short preamble doesn't
19288a1b9b6aSSam Leffler 				 *     change dynamically
19298a1b9b6aSSam Leffler 				 */
19308a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic,
19318a1b9b6aSSam Leffler 					ic->ic_curmode == IEEE80211_MODE_11A ||
1932f219c9d3SSam Leffler 					(scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1933b5c99415SSam Leffler 				ni->ni_capinfo = scan.capinfo;
19348a1b9b6aSSam Leffler 				/* XXX statistic */
19358a1b9b6aSSam Leffler 			}
1936b5c99415SSam Leffler 			if (scan.wme != NULL &&
19373fd5a5aaSSam Leffler 			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
1938b5c99415SSam Leffler 			    ieee80211_parse_wmeparams(ic, scan.wme, wh) > 0)
19398a1b9b6aSSam Leffler 				ieee80211_wme_updateparams(ic);
1940b5c99415SSam Leffler 			if (scan.tim != NULL) {
1941a634d6a7SSam Leffler 				struct ieee80211_tim_ie *ie =
1942b5c99415SSam Leffler 				    (struct ieee80211_tim_ie *) scan.tim;
1943a634d6a7SSam Leffler 
1944a634d6a7SSam Leffler 				ni->ni_dtim_count = ie->tim_count;
1945a634d6a7SSam Leffler 				ni->ni_dtim_period = ie->tim_period;
1946a634d6a7SSam Leffler 			}
1947b5c99415SSam Leffler 			if (ic->ic_flags & IEEE80211_F_SCAN)
1948b5c99415SSam Leffler 				ieee80211_add_scan(ic, &scan, wh,
1949b5c99415SSam Leffler 					subtype, rssi, rstamp);
19508a1b9b6aSSam Leffler 			return;
19518a1b9b6aSSam Leffler 		}
19528a1b9b6aSSam Leffler 		/*
1953b5c99415SSam Leffler 		 * If scanning, just pass information to the scan module.
19548a1b9b6aSSam Leffler 		 */
1955b5c99415SSam Leffler 		if (ic->ic_flags & IEEE80211_F_SCAN) {
1956097131ffSSam Leffler 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1957097131ffSSam Leffler 				/*
1958097131ffSSam Leffler 				 * Actively scanning a channel marked passive;
1959097131ffSSam Leffler 				 * send a probe request now that we know there
1960097131ffSSam Leffler 				 * is 802.11 traffic present.
1961097131ffSSam Leffler 				 *
1962097131ffSSam Leffler 				 * XXX check if the beacon we recv'd gives
1963097131ffSSam Leffler 				 * us what we need and suppress the probe req
1964097131ffSSam Leffler 				 */
1965097131ffSSam Leffler 				ieee80211_probe_curchan(ic, 1);
1966097131ffSSam Leffler 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1967097131ffSSam Leffler 			}
1968b5c99415SSam Leffler 			ieee80211_add_scan(ic, &scan, wh,
1969b5c99415SSam Leffler 				subtype, rssi, rstamp);
19708a1b9b6aSSam Leffler 			return;
19718a1b9b6aSSam Leffler 		}
1972b5c99415SSam Leffler 		if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
1973b5c99415SSam Leffler 			if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1974b5c99415SSam Leffler 				/*
1975b5c99415SSam Leffler 				 * Create a new entry in the neighbor table.
1976b5c99415SSam Leffler 				 */
1977b5c99415SSam Leffler 				ni = ieee80211_add_neighbor(ic, wh, &scan);
1978be425a0fSSam Leffler 			} else if (ni->ni_capinfo == 0) {
1979be425a0fSSam Leffler 				/*
1980be425a0fSSam Leffler 				 * Update faked node created on transmit.
1981be425a0fSSam Leffler 				 * Note this also updates the tsf.
1982be425a0fSSam Leffler 				 */
1983be425a0fSSam Leffler 				ieee80211_init_neighbor(ni, wh, &scan);
1984b5c99415SSam Leffler 			} else {
1985b5c99415SSam Leffler 				/*
1986b5c99415SSam Leffler 				 * Record tsf for potential resync.
1987b5c99415SSam Leffler 				 */
1988b5c99415SSam Leffler 				memcpy(ni->ni_tstamp.data, scan.tstamp,
1989b5c99415SSam Leffler 					sizeof(ni->ni_tstamp));
1990b5c99415SSam Leffler 			}
1991b5c99415SSam Leffler 			if (ni != NULL) {
19921a1e1d21SSam Leffler 				ni->ni_rssi = rssi;
19931a1e1d21SSam Leffler 				ni->ni_rstamp = rstamp;
1994a634d6a7SSam Leffler 			}
1995b5c99415SSam Leffler 		}
19961a1e1d21SSam Leffler 		break;
19971a1e1d21SSam Leffler 	}
19981a1e1d21SSam Leffler 
199949d4c02fSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
20008a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA ||
20018a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_RUN) {
20028a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
20031a1e1d21SSam Leffler 			return;
20048a1b9b6aSSam Leffler 		}
20058a1b9b6aSSam Leffler 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
20068a1b9b6aSSam Leffler 			/* frame must be directed */
20078a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;	/* XXX stat */
20081a1e1d21SSam Leffler 			return;
20098a1b9b6aSSam Leffler 		}
20101a1e1d21SSam Leffler 
20111a1e1d21SSam Leffler 		/*
20121a1e1d21SSam Leffler 		 * prreq frame format
20131a1e1d21SSam Leffler 		 *	[tlv] ssid
20141a1e1d21SSam Leffler 		 *	[tlv] supported rates
20151a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
20161a1e1d21SSam Leffler 		 */
20171a1e1d21SSam Leffler 		ssid = rates = xrates = NULL;
20181a1e1d21SSam Leffler 		while (frm < efrm) {
2019336ec6a1SSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1]);
20201a1e1d21SSam Leffler 			switch (*frm) {
20211a1e1d21SSam Leffler 			case IEEE80211_ELEMID_SSID:
20221a1e1d21SSam Leffler 				ssid = frm;
20231a1e1d21SSam Leffler 				break;
20241a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
20251a1e1d21SSam Leffler 				rates = frm;
20261a1e1d21SSam Leffler 				break;
20271a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
20281a1e1d21SSam Leffler 				xrates = frm;
20291a1e1d21SSam Leffler 				break;
20301a1e1d21SSam Leffler 			}
20311a1e1d21SSam Leffler 			frm += frm[1] + 2;
20321a1e1d21SSam Leffler 		}
2033dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
20341a1e1d21SSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
20358a1b9b6aSSam Leffler 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid);
20364ef04d32SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
20374ef04d32SSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
20384ef04d32SSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
20394ef04d32SSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
20404ef04d32SSam Leffler 			    "%s", "no ssid with ssid suppression enabled");
20414ef04d32SSam Leffler 			ic->ic_stats.is_rx_ssidmismatch++; /*XXX*/
20424ef04d32SSam Leffler 			return;
20434ef04d32SSam Leffler 		}
20441a1e1d21SSam Leffler 
2045be425a0fSSam Leffler 		allocbs = 0;
20460a915fadSSam Leffler 		if (ni == ic->ic_bss) {
2047be425a0fSSam Leffler 			if (ic->ic_opmode != IEEE80211_M_IBSS) {
2048be425a0fSSam Leffler 				ni = ieee80211_tmp_node(ic, wh->i_addr2);
2049be425a0fSSam Leffler 				allocbs = 1;
2050be425a0fSSam Leffler 			} else if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
20518a1b9b6aSSam Leffler 				/*
20528a1b9b6aSSam Leffler 				 * XXX Cannot tell if the sender is operating
20538a1b9b6aSSam Leffler 				 * in ibss mode.  But we need a new node to
20548a1b9b6aSSam Leffler 				 * send the response so blindly add them to the
20558a1b9b6aSSam Leffler 				 * neighbor table.
20568a1b9b6aSSam Leffler 				 */
2057acc4f7f5SSam Leffler 				ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
20588a1b9b6aSSam Leffler 					wh->i_addr2);
2059be425a0fSSam Leffler 			}
2060c64bfa0fSSam Leffler 			if (ni == NULL)
20611a1e1d21SSam Leffler 				return;
2062be425a0fSSam Leffler 		}
20638a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
20648a1b9b6aSSam Leffler 		    "[%s] recv probe req\n", ether_sprintf(wh->i_addr2));
20651a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
20661a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
20677d77cd53SSam Leffler 		rate = ieee80211_setup_rates(ni, rates, xrates,
20681a1e1d21SSam Leffler 			  IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE
20691a1e1d21SSam Leffler 			| IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
20701a1e1d21SSam Leffler 		if (rate & IEEE80211_RATE_BASIC) {
20718a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_XRATE,
20728a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
20738a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
20748a1b9b6aSSam Leffler 			    "%s", "recv'd rate set invalid");
20751a1e1d21SSam Leffler 		} else {
20760a915fadSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni,
20770a915fadSSam Leffler 				IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
20781a1e1d21SSam Leffler 		}
2079be425a0fSSam Leffler 		if (allocbs) {
2080be425a0fSSam Leffler 			/*
2081be425a0fSSam Leffler 			 * Temporary node created just to send a
2082be425a0fSSam Leffler 			 * response, reclaim immediately.
2083be425a0fSSam Leffler 			 */
20848a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
20858a1b9b6aSSam Leffler 		}
20861a1e1d21SSam Leffler 		break;
20871a1e1d21SSam Leffler 
20881a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH: {
20891a1e1d21SSam Leffler 		u_int16_t algo, seq, status;
20901a1e1d21SSam Leffler 		/*
20911a1e1d21SSam Leffler 		 * auth frame format
20921a1e1d21SSam Leffler 		 *	[2] algorithm
20931a1e1d21SSam Leffler 		 *	[2] sequence
20941a1e1d21SSam Leffler 		 *	[2] status
20951a1e1d21SSam Leffler 		 *	[tlv*] challenge
20961a1e1d21SSam Leffler 		 */
20971a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
20981a1e1d21SSam Leffler 		algo   = le16toh(*(u_int16_t *)frm);
20991a1e1d21SSam Leffler 		seq    = le16toh(*(u_int16_t *)(frm + 2));
21001a1e1d21SSam Leffler 		status = le16toh(*(u_int16_t *)(frm + 4));
21018a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
21028a1b9b6aSSam Leffler 		    "[%s] recv auth frame with algorithm %d seq %d\n",
21038a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr2), algo, seq);
21048a1b9b6aSSam Leffler 		/*
21058a1b9b6aSSam Leffler 		 * Consult the ACL policy module if setup.
21068a1b9b6aSSam Leffler 		 */
21078a1b9b6aSSam Leffler 		if (ic->ic_acl != NULL &&
21088a1b9b6aSSam Leffler 		    !ic->ic_acl->iac_check(ic, wh->i_addr2)) {
21098a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ACL,
21108a1b9b6aSSam Leffler 			    wh, "auth", "%s", "disallowed by ACL");
21118a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_acl++;
2112a3d1edc2SSam Leffler 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2113a3d1edc2SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
2114a3d1edc2SSam Leffler 				    IEEE80211_FC0_SUBTYPE_AUTH,
2115a3d1edc2SSam Leffler 				    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
2116a3d1edc2SSam Leffler 			}
21171a1e1d21SSam Leffler 			return;
21181a1e1d21SSam Leffler 		}
21198a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_COUNTERM) {
21208a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic,
21218a1b9b6aSSam Leffler 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
21228a1b9b6aSSam Leffler 			    wh, "auth", "%s", "TKIP countermeasures enabled");
21238a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_countermeasures++;
21248a1b9b6aSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
21250a915fadSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
21268a1b9b6aSSam Leffler 					IEEE80211_FC0_SUBTYPE_AUTH,
21278a1b9b6aSSam Leffler 					IEEE80211_REASON_MIC_FAILURE);
212859571d2bSSam Leffler 			}
21291a1e1d21SSam Leffler 			return;
21301a1e1d21SSam Leffler 		}
21318a1b9b6aSSam Leffler 		if (algo == IEEE80211_AUTH_ALG_SHARED)
21328a1b9b6aSSam Leffler 			ieee80211_auth_shared(ic, wh, frm + 6, efrm, ni, rssi,
21338a1b9b6aSSam Leffler 			    rstamp, seq, status);
21348a1b9b6aSSam Leffler 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
21358a1b9b6aSSam Leffler 			ieee80211_auth_open(ic, wh, ni, rssi, rstamp, seq,
21368a1b9b6aSSam Leffler 			    status);
21378a1b9b6aSSam Leffler 		else {
21388a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
21398a1b9b6aSSam Leffler 			    wh, "auth", "unsupported alg %d", algo);
21408a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_unsupported++;
21418a1b9b6aSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
21428a1b9b6aSSam Leffler 				/* XXX not right */
21438a1b9b6aSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
21448a1b9b6aSSam Leffler 					IEEE80211_FC0_SUBTYPE_AUTH,
21458a1b9b6aSSam Leffler 					(seq+1) | (IEEE80211_STATUS_ALG<<16));
21468a1b9b6aSSam Leffler 			}
21478a1b9b6aSSam Leffler 			return;
21481a1e1d21SSam Leffler 		}
21491a1e1d21SSam Leffler 		break;
21501a1e1d21SSam Leffler 	}
21511a1e1d21SSam Leffler 
21521a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
21531a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
2154d365f9c7SSam Leffler 		u_int16_t capinfo, lintval;
21558a1b9b6aSSam Leffler 		struct ieee80211_rsnparms rsn;
21568a1b9b6aSSam Leffler 		u_int8_t reason;
21571a1e1d21SSam Leffler 
21581a1e1d21SSam Leffler 		if (ic->ic_opmode != IEEE80211_M_HOSTAP ||
21598a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_RUN) {
21608a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
21611a1e1d21SSam Leffler 			return;
21628a1b9b6aSSam Leffler 		}
21631a1e1d21SSam Leffler 
21641a1e1d21SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
21651a1e1d21SSam Leffler 			reassoc = 1;
21661a1e1d21SSam Leffler 			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
21671a1e1d21SSam Leffler 		} else {
21681a1e1d21SSam Leffler 			reassoc = 0;
21691a1e1d21SSam Leffler 			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
21701a1e1d21SSam Leffler 		}
21711a1e1d21SSam Leffler 		/*
21721a1e1d21SSam Leffler 		 * asreq frame format
21731a1e1d21SSam Leffler 		 *	[2] capability information
21741a1e1d21SSam Leffler 		 *	[2] listen interval
21751a1e1d21SSam Leffler 		 *	[6*] current AP address (reassoc only)
21761a1e1d21SSam Leffler 		 *	[tlv] ssid
21771a1e1d21SSam Leffler 		 *	[tlv] supported rates
21781a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
21798a1b9b6aSSam Leffler 		 *	[tlv] WPA or RSN
21801a1e1d21SSam Leffler 		 */
21811a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4));
21821a1e1d21SSam Leffler 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_bss->ni_bssid)) {
21838a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
21848a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
21858a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
21868a1b9b6aSSam Leffler 			    "%s", "wrong bssid");
21871be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_bss++;
21881a1e1d21SSam Leffler 			return;
21891a1e1d21SSam Leffler 		}
21901a1e1d21SSam Leffler 		capinfo = le16toh(*(u_int16_t *)frm);	frm += 2;
2191d365f9c7SSam Leffler 		lintval = le16toh(*(u_int16_t *)frm);	frm += 2;
21921a1e1d21SSam Leffler 		if (reassoc)
21931a1e1d21SSam Leffler 			frm += 6;	/* ignore current AP info */
21948a1b9b6aSSam Leffler 		ssid = rates = xrates = wpa = wme = NULL;
21951a1e1d21SSam Leffler 		while (frm < efrm) {
2196336ec6a1SSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1]);
21971a1e1d21SSam Leffler 			switch (*frm) {
21981a1e1d21SSam Leffler 			case IEEE80211_ELEMID_SSID:
21991a1e1d21SSam Leffler 				ssid = frm;
22001a1e1d21SSam Leffler 				break;
22011a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
22021a1e1d21SSam Leffler 				rates = frm;
22031a1e1d21SSam Leffler 				break;
22041a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
22051a1e1d21SSam Leffler 				xrates = frm;
22061a1e1d21SSam Leffler 				break;
22078a1b9b6aSSam Leffler 			/* XXX verify only one of RSN and WPA ie's? */
22088a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_RSN:
22098a1b9b6aSSam Leffler 				wpa = frm;
22108a1b9b6aSSam Leffler 				break;
22118a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
2212bdad3a10SSam Leffler 				if (iswpaoui(frm))
22138a1b9b6aSSam Leffler 					wpa = frm;
2214bdad3a10SSam Leffler 				else if (iswmeinfo(frm))
22158a1b9b6aSSam Leffler 					wme = frm;
22168a1b9b6aSSam Leffler 				/* XXX Atheros OUI support */
22178a1b9b6aSSam Leffler 				break;
22181a1e1d21SSam Leffler 			}
22191a1e1d21SSam Leffler 			frm += frm[1] + 2;
22201a1e1d21SSam Leffler 		}
2221dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
22221a1e1d21SSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
22238a1b9b6aSSam Leffler 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid);
22248a1b9b6aSSam Leffler 
22250a915fadSSam Leffler 		if (ni == ic->ic_bss) {
22268a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
22278a1b9b6aSSam Leffler 			    "[%s] deny %s request, sta not authenticated\n",
22288a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
22298a1b9b6aSSam Leffler 			    reassoc ? "reassoc" : "assoc");
223084eb84c4SSam Leffler 			ieee80211_send_error(ic, ni, wh->i_addr2,
22310a915fadSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
22321a1e1d21SSam Leffler 			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
22331be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_notauth++;
22341a1e1d21SSam Leffler 			return;
22351a1e1d21SSam Leffler 		}
2236c9a4bb99SSam Leffler 		/* assert right associstion security credentials */
2237c9a4bb99SSam Leffler 		if (wpa == NULL && (ic->ic_flags & IEEE80211_F_WPA)) {
2238c9a4bb99SSam Leffler 			IEEE80211_DPRINTF(ic,
2239c9a4bb99SSam Leffler 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
2240c9a4bb99SSam Leffler 			    "[%s] no WPA/RSN IE in association request\n",
2241c9a4bb99SSam Leffler 			    ether_sprintf(wh->i_addr2));
2242c9a4bb99SSam Leffler 			IEEE80211_SEND_MGMT(ic, ni,
2243c9a4bb99SSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
2244c9a4bb99SSam Leffler 			    IEEE80211_REASON_RSN_REQUIRED);
2245c9a4bb99SSam Leffler 			ieee80211_node_leave(ic, ni);
2246c9a4bb99SSam Leffler 			/* XXX distinguish WPA/RSN? */
2247c9a4bb99SSam Leffler 			ic->ic_stats.is_rx_assoc_badwpaie++;
2248c9a4bb99SSam Leffler 			return;
2249c9a4bb99SSam Leffler 		}
22508a1b9b6aSSam Leffler 		if (wpa != NULL) {
22518a1b9b6aSSam Leffler 			/*
22528a1b9b6aSSam Leffler 			 * Parse WPA information element.  Note that
22538a1b9b6aSSam Leffler 			 * we initialize the param block from the node
22548a1b9b6aSSam Leffler 			 * state so that information in the IE overrides
22558a1b9b6aSSam Leffler 			 * our defaults.  The resulting parameters are
22568a1b9b6aSSam Leffler 			 * installed below after the association is assured.
22578a1b9b6aSSam Leffler 			 */
22588a1b9b6aSSam Leffler 			rsn = ni->ni_rsn;
22598a1b9b6aSSam Leffler 			if (wpa[0] != IEEE80211_ELEMID_RSN)
22608a1b9b6aSSam Leffler 				reason = ieee80211_parse_wpa(ic, wpa, &rsn, wh);
22618a1b9b6aSSam Leffler 			else
22628a1b9b6aSSam Leffler 				reason = ieee80211_parse_rsn(ic, wpa, &rsn, wh);
22638a1b9b6aSSam Leffler 			if (reason != 0) {
22648a1b9b6aSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
22658a1b9b6aSSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
22668a1b9b6aSSam Leffler 				ieee80211_node_leave(ic, ni);
22678a1b9b6aSSam Leffler 				/* XXX distinguish WPA/RSN? */
22688a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_assoc_badwpaie++;
22698a1b9b6aSSam Leffler 				return;
22708a1b9b6aSSam Leffler 			}
22718a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
22728a1b9b6aSSam Leffler 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
22738a1b9b6aSSam Leffler 			    "[%s] %s ie: mc %u/%u uc %u/%u key %u caps 0x%x\n",
22748a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
22758a1b9b6aSSam Leffler 			    wpa[0] != IEEE80211_ELEMID_RSN ?  "WPA" : "RSN",
22768a1b9b6aSSam Leffler 			    rsn.rsn_mcastcipher, rsn.rsn_mcastkeylen,
22778a1b9b6aSSam Leffler 			    rsn.rsn_ucastcipher, rsn.rsn_ucastkeylen,
22788a1b9b6aSSam Leffler 			    rsn.rsn_keymgmt, rsn.rsn_caps);
22798a1b9b6aSSam Leffler 		}
22808a1b9b6aSSam Leffler 		/* discard challenge after association */
22818a1b9b6aSSam Leffler 		if (ni->ni_challenge != NULL) {
22828a1b9b6aSSam Leffler 			FREE(ni->ni_challenge, M_DEVBUF);
22838a1b9b6aSSam Leffler 			ni->ni_challenge = NULL;
22848a1b9b6aSSam Leffler 		}
22859b4db829SSam Leffler 		/* NB: 802.11 spec says to ignore station's privacy bit */
22869b4db829SSam Leffler 		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
22878a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
22888a1b9b6aSSam Leffler 			    "[%s] deny %s request, capability mismatch 0x%x\n",
22898a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
22908a1b9b6aSSam Leffler 			    reassoc ? "reassoc" : "assoc", capinfo);
22910a915fadSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
22920a915fadSSam Leffler 				IEEE80211_STATUS_CAPINFO);
22938a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
22941be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_capmismatch++;
22951a1e1d21SSam Leffler 			return;
22961a1e1d21SSam Leffler 		}
22977d77cd53SSam Leffler 		rate = ieee80211_setup_rates(ni, rates, xrates,
22981a1e1d21SSam Leffler 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
22991a1e1d21SSam Leffler 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2300c4f040c3SSam Leffler 		/*
2301c4f040c3SSam Leffler 		 * If constrained to 11g-only stations reject an
2302c4f040c3SSam Leffler 		 * 11b-only station.  We cheat a bit here by looking
2303c4f040c3SSam Leffler 		 * at the max negotiated xmit rate and assuming anyone
2304c4f040c3SSam Leffler 		 * with a best rate <24Mb/s is an 11b station.
2305c4f040c3SSam Leffler 		 */
2306c4f040c3SSam Leffler 		if ((rate & IEEE80211_RATE_BASIC) ||
2307c4f040c3SSam Leffler 		    ((ic->ic_flags & IEEE80211_F_PUREG) && rate < 48)) {
23088a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
23098a1b9b6aSSam Leffler 			    "[%s] deny %s request, rate set mismatch\n",
23108a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
23118a1b9b6aSSam Leffler 			    reassoc ? "reassoc" : "assoc");
23120a915fadSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
23130a915fadSSam Leffler 				IEEE80211_STATUS_BASIC_RATE);
23148a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
23151be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_norate++;
23161a1e1d21SSam Leffler 			return;
23171a1e1d21SSam Leffler 		}
23181a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
23191a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
2320d365f9c7SSam Leffler 		ni->ni_intval = lintval;
23211a1e1d21SSam Leffler 		ni->ni_capinfo = capinfo;
23221a1e1d21SSam Leffler 		ni->ni_chan = ic->ic_bss->ni_chan;
23231a1e1d21SSam Leffler 		ni->ni_fhdwell = ic->ic_bss->ni_fhdwell;
23241a1e1d21SSam Leffler 		ni->ni_fhindex = ic->ic_bss->ni_fhindex;
23258a1b9b6aSSam Leffler 		if (wpa != NULL) {
23268a1b9b6aSSam Leffler 			/*
23278a1b9b6aSSam Leffler 			 * Record WPA/RSN parameters for station, mark
23288a1b9b6aSSam Leffler 			 * node as using WPA and record information element
23298a1b9b6aSSam Leffler 			 * for applications that require it.
23308a1b9b6aSSam Leffler 			 */
23318a1b9b6aSSam Leffler 			ni->ni_rsn = rsn;
23328a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wpa_ie, wpa);
23338a1b9b6aSSam Leffler 		} else if (ni->ni_wpa_ie != NULL) {
23348a1b9b6aSSam Leffler 			/*
23358a1b9b6aSSam Leffler 			 * Flush any state from a previous association.
23368a1b9b6aSSam Leffler 			 */
23378a1b9b6aSSam Leffler 			FREE(ni->ni_wpa_ie, M_DEVBUF);
23388a1b9b6aSSam Leffler 			ni->ni_wpa_ie = NULL;
23398a1b9b6aSSam Leffler 		}
23408a1b9b6aSSam Leffler 		if (wme != NULL) {
23418a1b9b6aSSam Leffler 			/*
23428a1b9b6aSSam Leffler 			 * Record WME parameters for station, mark node
23438a1b9b6aSSam Leffler 			 * as capable of QoS and record information
23448a1b9b6aSSam Leffler 			 * element for applications that require it.
23458a1b9b6aSSam Leffler 			 */
23468a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wme_ie, wme);
23478a1b9b6aSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
23488a1b9b6aSSam Leffler 		} else if (ni->ni_wme_ie != NULL) {
23498a1b9b6aSSam Leffler 			/*
23508a1b9b6aSSam Leffler 			 * Flush any state from a previous association.
23518a1b9b6aSSam Leffler 			 */
23528a1b9b6aSSam Leffler 			FREE(ni->ni_wme_ie, M_DEVBUF);
23538a1b9b6aSSam Leffler 			ni->ni_wme_ie = NULL;
23548a1b9b6aSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
23558a1b9b6aSSam Leffler 		}
23568a1b9b6aSSam Leffler 		ieee80211_node_join(ic, ni, resp);
23571a1e1d21SSam Leffler 		break;
23581a1e1d21SSam Leffler 	}
23591a1e1d21SSam Leffler 
23601a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
23611a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
23628a1b9b6aSSam Leffler 		u_int16_t capinfo, associd;
23631a1e1d21SSam Leffler 		u_int16_t status;
23641a1e1d21SSam Leffler 
23651a1e1d21SSam Leffler 		if (ic->ic_opmode != IEEE80211_M_STA ||
23668a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_ASSOC) {
23678a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
23681a1e1d21SSam Leffler 			return;
23698a1b9b6aSSam Leffler 		}
23701a1e1d21SSam Leffler 
23711a1e1d21SSam Leffler 		/*
23721a1e1d21SSam Leffler 		 * asresp frame format
23731a1e1d21SSam Leffler 		 *	[2] capability information
23741a1e1d21SSam Leffler 		 *	[2] status
23751a1e1d21SSam Leffler 		 *	[2] association ID
23761a1e1d21SSam Leffler 		 *	[tlv] supported rates
23771a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
23788a1b9b6aSSam Leffler 		 *	[tlv] WME
23791a1e1d21SSam Leffler 		 */
23801a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
23811a1e1d21SSam Leffler 		ni = ic->ic_bss;
23828a1b9b6aSSam Leffler 		capinfo = le16toh(*(u_int16_t *)frm);
23831a1e1d21SSam Leffler 		frm += 2;
23841a1e1d21SSam Leffler 		status = le16toh(*(u_int16_t *)frm);
23851a1e1d21SSam Leffler 		frm += 2;
23861a1e1d21SSam Leffler 		if (status != 0) {
23878a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
23888a1b9b6aSSam Leffler 			    "[%s] %sassoc failed (reason %d)\n",
23898a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
23908a1b9b6aSSam Leffler 			    ISREASSOC(subtype) ?  "re" : "", status);
23918a1b9b6aSSam Leffler 			if (ni != ic->ic_bss)	/* XXX never true? */
23921a1e1d21SSam Leffler 				ni->ni_fails++;
23938a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_fail++;	/* XXX */
23941a1e1d21SSam Leffler 			return;
23951a1e1d21SSam Leffler 		}
23968a1b9b6aSSam Leffler 		associd = le16toh(*(u_int16_t *)frm);
23971a1e1d21SSam Leffler 		frm += 2;
23981a1e1d21SSam Leffler 
23998a1b9b6aSSam Leffler 		rates = xrates = wpa = wme = NULL;
24001a1e1d21SSam Leffler 		while (frm < efrm) {
2401336ec6a1SSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1]);
24021a1e1d21SSam Leffler 			switch (*frm) {
24031a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
24041a1e1d21SSam Leffler 				rates = frm;
24051a1e1d21SSam Leffler 				break;
24061a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
24071a1e1d21SSam Leffler 				xrates = frm;
24081a1e1d21SSam Leffler 				break;
24098a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
24108a1b9b6aSSam Leffler 				if (iswmeoui(frm))
24118a1b9b6aSSam Leffler 					wme = frm;
24128a1b9b6aSSam Leffler 				/* XXX Atheros OUI support */
24138a1b9b6aSSam Leffler 				break;
24141a1e1d21SSam Leffler 			}
24151a1e1d21SSam Leffler 			frm += frm[1] + 2;
24161a1e1d21SSam Leffler 		}
24171a1e1d21SSam Leffler 
2418dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
24197d77cd53SSam Leffler 		rate = ieee80211_setup_rates(ni, rates, xrates,
24201a1e1d21SSam Leffler 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
24211a1e1d21SSam Leffler 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
242249d4c02fSSam Leffler 		if (rate & IEEE80211_RATE_BASIC) {
24238a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
24248a1b9b6aSSam Leffler 			    "[%s] %sassoc failed (rate set mismatch)\n",
24258a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
24268a1b9b6aSSam Leffler 			    ISREASSOC(subtype) ?  "re" : "");
24278a1b9b6aSSam Leffler 			if (ni != ic->ic_bss)	/* XXX never true? */
24288a1b9b6aSSam Leffler 				ni->ni_fails++;
24298a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_assoc_norate++;
2430181181acSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
24318a1b9b6aSSam Leffler 			return;
24328a1b9b6aSSam Leffler 		}
24338a1b9b6aSSam Leffler 
24348a1b9b6aSSam Leffler 		ni->ni_capinfo = capinfo;
24358a1b9b6aSSam Leffler 		ni->ni_associd = associd;
2436c0fa32ceSSam Leffler 		if (wme != NULL &&
2437c0fa32ceSSam Leffler 		    ieee80211_parse_wmeparams(ic, wme, wh) >= 0) {
24388a1b9b6aSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
24398a1b9b6aSSam Leffler 			ieee80211_wme_updateparams(ic);
24408a1b9b6aSSam Leffler 		} else
24418a1b9b6aSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
24428a1b9b6aSSam Leffler 		/*
24438a1b9b6aSSam Leffler 		 * Configure state now that we are associated.
24448a1b9b6aSSam Leffler 		 *
24458a1b9b6aSSam Leffler 		 * XXX may need different/additional driver callbacks?
24468a1b9b6aSSam Leffler 		 */
24478a1b9b6aSSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11A ||
24488a1b9b6aSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
24498a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
24508a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
24518a1b9b6aSSam Leffler 		} else {
24528a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
24538a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
24548a1b9b6aSSam Leffler 		}
24558a1b9b6aSSam Leffler 		ieee80211_set_shortslottime(ic,
24568a1b9b6aSSam Leffler 			ic->ic_curmode == IEEE80211_MODE_11A ||
24578a1b9b6aSSam Leffler 			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
24588a1b9b6aSSam Leffler 		/*
24598a1b9b6aSSam Leffler 		 * Honor ERP protection.
24608a1b9b6aSSam Leffler 		 *
24618a1b9b6aSSam Leffler 		 * NB: ni_erp should zero for non-11g operation.
24628a1b9b6aSSam Leffler 		 * XXX check ic_curmode anyway?
24638a1b9b6aSSam Leffler 		 */
2464ec425115SSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G &&
2465ec425115SSam Leffler 		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
24668a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
24678a1b9b6aSSam Leffler 		else
24688a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
24698a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
24708a1b9b6aSSam Leffler 		    "[%s] %sassoc success: %s preamble, %s slot time%s%s\n",
24718a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr2),
24728a1b9b6aSSam Leffler 		    ISREASSOC(subtype) ? "re" : "",
24738a1b9b6aSSam Leffler 		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
24748a1b9b6aSSam Leffler 		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
24758a1b9b6aSSam Leffler 		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
24768a1b9b6aSSam Leffler 		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
24778a1b9b6aSSam Leffler 		);
24788a1b9b6aSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, subtype);
24791a1e1d21SSam Leffler 		break;
24801a1e1d21SSam Leffler 	}
24811a1e1d21SSam Leffler 
24821a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
24831a1e1d21SSam Leffler 		u_int16_t reason;
24848a1b9b6aSSam Leffler 
24858a1b9b6aSSam Leffler 		if (ic->ic_state == IEEE80211_S_SCAN) {
24868a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
24878a1b9b6aSSam Leffler 			return;
24888a1b9b6aSSam Leffler 		}
24891a1e1d21SSam Leffler 		/*
24901a1e1d21SSam Leffler 		 * deauth frame format
24911a1e1d21SSam Leffler 		 *	[2] reason
24921a1e1d21SSam Leffler 		 */
24931a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
24941a1e1d21SSam Leffler 		reason = le16toh(*(u_int16_t *)frm);
24951be50176SSam Leffler 		ic->ic_stats.is_rx_deauth++;
24968a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_deauth);
24974720ec19SSam Leffler 
24984720ec19SSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
24994720ec19SSam Leffler 		    "[%s] recv deauthenticate (reason %d)\n",
25004720ec19SSam Leffler 		    ether_sprintf(ni->ni_macaddr), reason);
25011a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
25021a1e1d21SSam Leffler 		case IEEE80211_M_STA:
2503a11c9a5cSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
25041a1e1d21SSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
25051a1e1d21SSam Leffler 			break;
25061a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
25074720ec19SSam Leffler 			if (ni != ic->ic_bss)
25088a1b9b6aSSam Leffler 				ieee80211_node_leave(ic, ni);
25091a1e1d21SSam Leffler 			break;
25101a1e1d21SSam Leffler 		default:
25118a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
25121a1e1d21SSam Leffler 			break;
25131a1e1d21SSam Leffler 		}
25141a1e1d21SSam Leffler 		break;
25151a1e1d21SSam Leffler 	}
25161a1e1d21SSam Leffler 
25171a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
25181a1e1d21SSam Leffler 		u_int16_t reason;
25198a1b9b6aSSam Leffler 
25208a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN &&
2521404265d4SSam Leffler 		    ic->ic_state != IEEE80211_S_ASSOC &&
25228a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_AUTH) {
25238a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
25248a1b9b6aSSam Leffler 			return;
25258a1b9b6aSSam Leffler 		}
25261a1e1d21SSam Leffler 		/*
25271a1e1d21SSam Leffler 		 * disassoc frame format
25281a1e1d21SSam Leffler 		 *	[2] reason
25291a1e1d21SSam Leffler 		 */
25301a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
25311a1e1d21SSam Leffler 		reason = le16toh(*(u_int16_t *)frm);
25321be50176SSam Leffler 		ic->ic_stats.is_rx_disassoc++;
25338a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_disassoc);
25344720ec19SSam Leffler 
25354720ec19SSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2536e35ac6b9SSam Leffler 		    "[%s] recv disassociate (reason %d)\n",
25374720ec19SSam Leffler 		    ether_sprintf(ni->ni_macaddr), reason);
25381a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
25391a1e1d21SSam Leffler 		case IEEE80211_M_STA:
2540a11c9a5cSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
25411a1e1d21SSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
25421a1e1d21SSam Leffler 			break;
25431a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
25444720ec19SSam Leffler 			if (ni != ic->ic_bss)
25458a1b9b6aSSam Leffler 				ieee80211_node_leave(ic, ni);
25461a1e1d21SSam Leffler 			break;
25471a1e1d21SSam Leffler 		default:
25488a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
25491a1e1d21SSam Leffler 			break;
25501a1e1d21SSam Leffler 		}
25511a1e1d21SSam Leffler 		break;
25521a1e1d21SSam Leffler 	}
25531a1e1d21SSam Leffler 	default:
25548a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
25558a1b9b6aSSam Leffler 		     wh, "mgt", "subtype 0x%x not handled", subtype);
25561be50176SSam Leffler 		ic->ic_stats.is_rx_badsubtype++;
25571a1e1d21SSam Leffler 		break;
25581a1e1d21SSam Leffler 	}
25598a1b9b6aSSam Leffler #undef ISREASSOC
25608a1b9b6aSSam Leffler #undef ISPROBE
25611a1e1d21SSam Leffler }
25621a1e1d21SSam Leffler #undef IEEE80211_VERIFY_LENGTH
25631a1e1d21SSam Leffler #undef IEEE80211_VERIFY_ELEMENT
25648a1b9b6aSSam Leffler 
25658a1b9b6aSSam Leffler /*
25668a1b9b6aSSam Leffler  * Handle station power-save state change.
25678a1b9b6aSSam Leffler  */
25688a1b9b6aSSam Leffler static void
25698a1b9b6aSSam Leffler ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
25708a1b9b6aSSam Leffler {
25718a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
25728a1b9b6aSSam Leffler 	struct mbuf *m;
25738a1b9b6aSSam Leffler 
25748a1b9b6aSSam Leffler 	if (enable) {
25758a1b9b6aSSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0)
25768a1b9b6aSSam Leffler 			ic->ic_ps_sta++;
25778a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
25788a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
25798a1b9b6aSSam Leffler 		    "[%s] power save mode on, %u sta's in ps mode\n",
25808a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
25818a1b9b6aSSam Leffler 		return;
25828a1b9b6aSSam Leffler 	}
25838a1b9b6aSSam Leffler 
25848a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT)
25858a1b9b6aSSam Leffler 		ic->ic_ps_sta--;
25868a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
25878a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
25888a1b9b6aSSam Leffler 	    "[%s] power save mode off, %u sta's in ps mode\n",
25898a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
25908a1b9b6aSSam Leffler 	/* XXX if no stations in ps mode, flush mc frames */
25918a1b9b6aSSam Leffler 
25928a1b9b6aSSam Leffler 	/*
25938a1b9b6aSSam Leffler 	 * Flush queued unicast frames.
25948a1b9b6aSSam Leffler 	 */
25958a1b9b6aSSam Leffler 	if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) {
2596c789ea8bSSam Leffler 		if (ic->ic_set_tim != NULL)
2597edfa57d0SSam Leffler 			ic->ic_set_tim(ni, 0);		/* just in case */
25988a1b9b6aSSam Leffler 		return;
25998a1b9b6aSSam Leffler 	}
26008a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
26018a1b9b6aSSam Leffler 	    "[%s] flush ps queue, %u packets queued\n",
26028a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_SAVEQ_QLEN(ni));
26038a1b9b6aSSam Leffler 	for (;;) {
26048a1b9b6aSSam Leffler 		int qlen;
26058a1b9b6aSSam Leffler 
26068a1b9b6aSSam Leffler 		IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen);
26078a1b9b6aSSam Leffler 		if (m == NULL)
26088a1b9b6aSSam Leffler 			break;
26098a1b9b6aSSam Leffler 		/*
26108a1b9b6aSSam Leffler 		 * If this is the last packet, turn off the TIM bit.
26118a1b9b6aSSam Leffler 		 * If there are more packets, set the more packets bit
2612bc5627d9SSam Leffler 		 * in the mbuf so ieee80211_encap will mark the 802.11
2613bc5627d9SSam Leffler 		 * head to indicate more data frames will follow.
26148a1b9b6aSSam Leffler 		 */
2615bc5627d9SSam Leffler 		if (qlen != 0)
2616bc5627d9SSam Leffler 			m->m_flags |= M_MORE_DATA;
26178a1b9b6aSSam Leffler 		/* XXX need different driver interface */
26188a1b9b6aSSam Leffler 		/* XXX bypasses q max */
26198a1b9b6aSSam Leffler 		IF_ENQUEUE(&ic->ic_ifp->if_snd, m);
26208a1b9b6aSSam Leffler 	}
2621c789ea8bSSam Leffler 	if (ic->ic_set_tim != NULL)
2622edfa57d0SSam Leffler 		ic->ic_set_tim(ni, 0);
26238a1b9b6aSSam Leffler }
26248a1b9b6aSSam Leffler 
26258a1b9b6aSSam Leffler /*
26268a1b9b6aSSam Leffler  * Process a received ps-poll frame.
26278a1b9b6aSSam Leffler  */
26288a1b9b6aSSam Leffler static void
26298a1b9b6aSSam Leffler ieee80211_recv_pspoll(struct ieee80211com *ic,
26308a1b9b6aSSam Leffler 	struct ieee80211_node *ni, struct mbuf *m0)
26318a1b9b6aSSam Leffler {
26328a1b9b6aSSam Leffler 	struct ieee80211_frame_min *wh;
26338a1b9b6aSSam Leffler 	struct mbuf *m;
26348a1b9b6aSSam Leffler 	u_int16_t aid;
26358a1b9b6aSSam Leffler 	int qlen;
26368a1b9b6aSSam Leffler 
26378a1b9b6aSSam Leffler 	wh = mtod(m0, struct ieee80211_frame_min *);
26388a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
26398a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
26408a1b9b6aSSam Leffler 		    (struct ieee80211_frame *) wh, "ps-poll",
26418a1b9b6aSSam Leffler 		    "%s", "unassociated station");
26428a1b9b6aSSam Leffler 		ic->ic_stats.is_ps_unassoc++;
26438a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
26448a1b9b6aSSam Leffler 			IEEE80211_REASON_NOT_ASSOCED);
26458a1b9b6aSSam Leffler 		return;
26468a1b9b6aSSam Leffler 	}
26478a1b9b6aSSam Leffler 
26488a1b9b6aSSam Leffler 	aid = le16toh(*(u_int16_t *)wh->i_dur);
26498a1b9b6aSSam Leffler 	if (aid != ni->ni_associd) {
26508a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
26518a1b9b6aSSam Leffler 		    (struct ieee80211_frame *) wh, "ps-poll",
26528a1b9b6aSSam Leffler 		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
26538a1b9b6aSSam Leffler 		    ni->ni_associd, aid);
26548a1b9b6aSSam Leffler 		ic->ic_stats.is_ps_badaid++;
26558a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
26568a1b9b6aSSam Leffler 			IEEE80211_REASON_NOT_ASSOCED);
26578a1b9b6aSSam Leffler 		return;
26588a1b9b6aSSam Leffler 	}
26598a1b9b6aSSam Leffler 
26608a1b9b6aSSam Leffler 	/* Okay, take the first queued packet and put it out... */
26618a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen);
26628a1b9b6aSSam Leffler 	if (m == NULL) {
26638a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
26648a1b9b6aSSam Leffler 		    "[%s] recv ps-poll, but queue empty\n",
26658a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr2));
266619ad2dd7SSam Leffler 		ieee80211_send_nulldata(ieee80211_ref_node(ni));
26678a1b9b6aSSam Leffler 		ic->ic_stats.is_ps_qempty++;	/* XXX node stat */
2668c789ea8bSSam Leffler 		if (ic->ic_set_tim != NULL)
2669edfa57d0SSam Leffler 			ic->ic_set_tim(ni, 0);	/* just in case */
26708a1b9b6aSSam Leffler 		return;
26718a1b9b6aSSam Leffler 	}
26728a1b9b6aSSam Leffler 	/*
26738a1b9b6aSSam Leffler 	 * If there are more packets, set the more packets bit
26748a1b9b6aSSam Leffler 	 * in the packet dispatched to the station; otherwise
26758a1b9b6aSSam Leffler 	 * turn off the TIM bit.
26768a1b9b6aSSam Leffler 	 */
26778a1b9b6aSSam Leffler 	if (qlen != 0) {
26788a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
26798a1b9b6aSSam Leffler 		    "[%s] recv ps-poll, send packet, %u still queued\n",
26808a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), qlen);
2681c52dab62SSam Leffler 		m->m_flags |= M_MORE_DATA;
26828a1b9b6aSSam Leffler 	} else {
26838a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
26848a1b9b6aSSam Leffler 		    "[%s] recv ps-poll, send packet, queue empty\n",
26858a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
2686c789ea8bSSam Leffler 		if (ic->ic_set_tim != NULL)
2687edfa57d0SSam Leffler 			ic->ic_set_tim(ni, 0);
26888a1b9b6aSSam Leffler 	}
26898a1b9b6aSSam Leffler 	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
26908a1b9b6aSSam Leffler 	IF_ENQUEUE(&ic->ic_ifp->if_snd, m);
26918a1b9b6aSSam Leffler }
26928a1b9b6aSSam Leffler 
26938a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
26948a1b9b6aSSam Leffler /*
26958a1b9b6aSSam Leffler  * Debugging support.
26968a1b9b6aSSam Leffler  */
26978a1b9b6aSSam Leffler 
26988a1b9b6aSSam Leffler /*
26998a1b9b6aSSam Leffler  * Return the bssid of a frame.
27008a1b9b6aSSam Leffler  */
27018a1b9b6aSSam Leffler static const u_int8_t *
27028a1b9b6aSSam Leffler ieee80211_getbssid(struct ieee80211com *ic, const struct ieee80211_frame *wh)
27038a1b9b6aSSam Leffler {
27048a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA)
27058a1b9b6aSSam Leffler 		return wh->i_addr2;
27068a1b9b6aSSam Leffler 	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) != IEEE80211_FC1_DIR_NODS)
27078a1b9b6aSSam Leffler 		return wh->i_addr1;
27088a1b9b6aSSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
27098a1b9b6aSSam Leffler 		return wh->i_addr1;
27108a1b9b6aSSam Leffler 	return wh->i_addr3;
27118a1b9b6aSSam Leffler }
27128a1b9b6aSSam Leffler 
2713f6df3191SSam Leffler void
2714f6df3191SSam Leffler ieee80211_note(struct ieee80211com *ic, const char *fmt, ...)
2715f6df3191SSam Leffler {
2716f6df3191SSam Leffler 	char buf[128];		/* XXX */
2717f6df3191SSam Leffler 	va_list ap;
2718f6df3191SSam Leffler 
2719f6df3191SSam Leffler 	va_start(ap, fmt);
2720f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
2721f6df3191SSam Leffler 	va_end(ap);
2722f6df3191SSam Leffler 
2723f6df3191SSam Leffler 	if_printf(ic->ic_ifp, "%s", buf);	/* NB: no \n */
2724f6df3191SSam Leffler }
2725f6df3191SSam Leffler 
2726f6df3191SSam Leffler void
2727f6df3191SSam Leffler ieee80211_note_frame(struct ieee80211com *ic,
2728f6df3191SSam Leffler 	const struct ieee80211_frame *wh,
2729f6df3191SSam Leffler 	const char *fmt, ...)
2730f6df3191SSam Leffler {
2731f6df3191SSam Leffler 	char buf[128];		/* XXX */
2732f6df3191SSam Leffler 	va_list ap;
2733f6df3191SSam Leffler 
2734f6df3191SSam Leffler 	va_start(ap, fmt);
2735f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
2736f6df3191SSam Leffler 	va_end(ap);
2737f6df3191SSam Leffler 	if_printf(ic->ic_ifp, "[%s] %s\n",
2738f6df3191SSam Leffler 		ether_sprintf(ieee80211_getbssid(ic, wh)), buf);
2739f6df3191SSam Leffler }
2740f6df3191SSam Leffler 
2741f6df3191SSam Leffler void
2742f6df3191SSam Leffler ieee80211_note_mac(struct ieee80211com *ic,
2743f6df3191SSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN],
2744f6df3191SSam Leffler 	const char *fmt, ...)
2745f6df3191SSam Leffler {
2746f6df3191SSam Leffler 	char buf[128];		/* XXX */
2747f6df3191SSam Leffler 	va_list ap;
2748f6df3191SSam Leffler 
2749f6df3191SSam Leffler 	va_start(ap, fmt);
2750f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
2751f6df3191SSam Leffler 	va_end(ap);
2752f6df3191SSam Leffler 	if_printf(ic->ic_ifp, "[%s] %s\n", ether_sprintf(mac), buf);
2753f6df3191SSam Leffler }
2754f6df3191SSam Leffler 
27558a1b9b6aSSam Leffler static void
27568a1b9b6aSSam Leffler ieee80211_discard_frame(struct ieee80211com *ic,
27578a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
27588a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
27598a1b9b6aSSam Leffler {
27608a1b9b6aSSam Leffler 	va_list ap;
27618a1b9b6aSSam Leffler 
2762497c84aeSSam Leffler 	printf("[%s:%s] discard ", ic->ic_ifp->if_xname,
2763497c84aeSSam Leffler 		ether_sprintf(ieee80211_getbssid(ic, wh)));
27648a1b9b6aSSam Leffler 	if (type != NULL)
27658a1b9b6aSSam Leffler 		printf("%s frame, ", type);
27668a1b9b6aSSam Leffler 	else
27678a1b9b6aSSam Leffler 		printf("frame, ");
27688a1b9b6aSSam Leffler 	va_start(ap, fmt);
27698a1b9b6aSSam Leffler 	vprintf(fmt, ap);
27708a1b9b6aSSam Leffler 	va_end(ap);
27718a1b9b6aSSam Leffler 	printf("\n");
27728a1b9b6aSSam Leffler }
27738a1b9b6aSSam Leffler 
27748a1b9b6aSSam Leffler static void
27758a1b9b6aSSam Leffler ieee80211_discard_ie(struct ieee80211com *ic,
27768a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
27778a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
27788a1b9b6aSSam Leffler {
27798a1b9b6aSSam Leffler 	va_list ap;
27808a1b9b6aSSam Leffler 
2781497c84aeSSam Leffler 	printf("[%s:%s] discard ", ic->ic_ifp->if_xname,
2782497c84aeSSam Leffler 		ether_sprintf(ieee80211_getbssid(ic, wh)));
27838a1b9b6aSSam Leffler 	if (type != NULL)
27848a1b9b6aSSam Leffler 		printf("%s information element, ", type);
27858a1b9b6aSSam Leffler 	else
27868a1b9b6aSSam Leffler 		printf("information element, ");
27878a1b9b6aSSam Leffler 	va_start(ap, fmt);
27888a1b9b6aSSam Leffler 	vprintf(fmt, ap);
27898a1b9b6aSSam Leffler 	va_end(ap);
27908a1b9b6aSSam Leffler 	printf("\n");
27918a1b9b6aSSam Leffler }
27928a1b9b6aSSam Leffler 
27938a1b9b6aSSam Leffler static void
27948a1b9b6aSSam Leffler ieee80211_discard_mac(struct ieee80211com *ic,
27958a1b9b6aSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN],
27968a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
27978a1b9b6aSSam Leffler {
27988a1b9b6aSSam Leffler 	va_list ap;
27998a1b9b6aSSam Leffler 
2800aa8c14c4SSam Leffler 	printf("[%s:%s] discard ", ic->ic_ifp->if_xname, ether_sprintf(mac));
28018a1b9b6aSSam Leffler 	if (type != NULL)
28028a1b9b6aSSam Leffler 		printf("%s frame, ", type);
28038a1b9b6aSSam Leffler 	else
28048a1b9b6aSSam Leffler 		printf("frame, ");
28058a1b9b6aSSam Leffler 	va_start(ap, fmt);
28068a1b9b6aSSam Leffler 	vprintf(fmt, ap);
28078a1b9b6aSSam Leffler 	va_end(ap);
28088a1b9b6aSSam Leffler 	printf("\n");
28098a1b9b6aSSam Leffler }
28108a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2811