xref: /freebsd/sys/net80211/ieee80211_input.c (revision aa8c14c444ea2e685ee80269251049a999923a9e)
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 &&
2368a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_HOSTAP) {
2378a1b9b6aSSam Leffler 				/*
2388a1b9b6aSSam Leffler 				 * Fake up a node for this newly
2398a1b9b6aSSam Leffler 				 * discovered member of the IBSS.
2408a1b9b6aSSam Leffler 				 */
241acc4f7f5SSam Leffler 				ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
2420796482dSSam Leffler 						    wh->i_addr2);
2438a1b9b6aSSam Leffler 				if (ni == NULL) {
2448a1b9b6aSSam Leffler 					/* NB: stat kept for alloc failure */
2458a1b9b6aSSam Leffler 					goto err;
2468a1b9b6aSSam Leffler 				}
2478a1b9b6aSSam Leffler 			}
2481a1e1d21SSam Leffler 			break;
2491a1e1d21SSam Leffler 		default:
2508a1b9b6aSSam Leffler 			goto out;
2511a1e1d21SSam Leffler 		}
2521a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
2531a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
2548a1b9b6aSSam Leffler 		if (HAS_SEQ(type)) {
2558a1b9b6aSSam Leffler 			u_int8_t tid;
2568a1b9b6aSSam Leffler 			if (IEEE80211_QOS_HAS_SEQ(wh)) {
2578a1b9b6aSSam Leffler 				tid = ((struct ieee80211_qosframe *)wh)->
2588a1b9b6aSSam Leffler 					i_qos[0] & IEEE80211_QOS_TID;
2591a08800dSSam Leffler 				if (TID_TO_WME_AC(tid) >= WME_AC_VI)
2608a1b9b6aSSam Leffler 					ic->ic_wme.wme_hipri_traffic++;
2618a1b9b6aSSam Leffler 				tid++;
2628a1b9b6aSSam Leffler 			} else
2638a1b9b6aSSam Leffler 				tid = 0;
2648a1b9b6aSSam Leffler 			rxseq = le16toh(*(u_int16_t *)wh->i_seq);
2651a1e1d21SSam Leffler 			if ((wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
2668a1b9b6aSSam Leffler 			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
2678a1b9b6aSSam Leffler 				/* duplicate, discard */
2688a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
2698a1b9b6aSSam Leffler 				    bssid, "duplicate",
2708a1b9b6aSSam Leffler 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
2718a1b9b6aSSam Leffler 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
2728a1b9b6aSSam Leffler 				    ni->ni_rxseqs[tid] >>
2738a1b9b6aSSam Leffler 					IEEE80211_SEQ_SEQ_SHIFT,
2748a1b9b6aSSam Leffler 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
2758a1b9b6aSSam Leffler 				    ni->ni_rxseqs[tid] &
2768a1b9b6aSSam Leffler 					IEEE80211_SEQ_FRAG_MASK,
2778a1b9b6aSSam Leffler 				    tid);
2788a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_dup++;
2798a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_dup);
2801a1e1d21SSam Leffler 				goto out;
2811a1e1d21SSam Leffler 			}
2828a1b9b6aSSam Leffler 			ni->ni_rxseqs[tid] = rxseq;
2838a1b9b6aSSam Leffler 		}
2841a1e1d21SSam Leffler 	}
2851a1e1d21SSam Leffler 
28618f897abSSam Leffler 	switch (type) {
2871a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
2882cc12adeSSam Leffler 		hdrspace = ieee80211_hdrspace(ic, wh);
2892cc12adeSSam Leffler 		if (m->m_len < hdrspace &&
2902cc12adeSSam Leffler 		    (m = m_pullup(m, hdrspace)) == NULL) {
29120098591SSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
29220098591SSam Leffler 			    ni->ni_macaddr, NULL,
2932cc12adeSSam Leffler 			    "data too short: expecting %u", hdrspace);
2948a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_tooshort++;
2958a1b9b6aSSam Leffler 			goto out;		/* XXX */
2968a1b9b6aSSam Leffler 		}
2971a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
2981a1e1d21SSam Leffler 		case IEEE80211_M_STA:
2991be50176SSam Leffler 			if (dir != IEEE80211_FC1_DIR_FROMDS) {
300bd6f09d9SSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
301bd6f09d9SSam Leffler 				    wh, "data", "%s", "unknown dir 0x%x", dir);
3021be50176SSam Leffler 				ic->ic_stats.is_rx_wrongdir++;
3031a1e1d21SSam Leffler 				goto out;
3041be50176SSam Leffler 			}
3051a1e1d21SSam Leffler 			if ((ifp->if_flags & IFF_SIMPLEX) &&
3061a1e1d21SSam Leffler 			    IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3071a1e1d21SSam Leffler 			    IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_myaddr)) {
3081a1e1d21SSam Leffler 				/*
3091a1e1d21SSam Leffler 				 * In IEEE802.11 network, multicast packet
3101a1e1d21SSam Leffler 				 * sent from me is broadcasted from AP.
3111a1e1d21SSam Leffler 				 * It should be silently discarded for
3121a1e1d21SSam Leffler 				 * SIMPLEX interface.
3131a1e1d21SSam Leffler 				 */
3148a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3158a1b9b6aSSam Leffler 				    wh, NULL, "%s", "multicast echo");
3161be50176SSam Leffler 				ic->ic_stats.is_rx_mcastecho++;
3171a1e1d21SSam Leffler 				goto out;
3181a1e1d21SSam Leffler 			}
3191a1e1d21SSam Leffler 			break;
3201a1e1d21SSam Leffler 		case IEEE80211_M_IBSS:
3211a1e1d21SSam Leffler 		case IEEE80211_M_AHDEMO:
3221be50176SSam Leffler 			if (dir != IEEE80211_FC1_DIR_NODS) {
323bd6f09d9SSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
324bd6f09d9SSam Leffler 				    wh, "data", "%s", "unknown dir 0x%x", dir);
3251be50176SSam Leffler 				ic->ic_stats.is_rx_wrongdir++;
3261a1e1d21SSam Leffler 				goto out;
3271be50176SSam Leffler 			}
3288a1b9b6aSSam Leffler 			/* XXX no power-save support */
3291a1e1d21SSam Leffler 			break;
3301a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
3311be50176SSam Leffler 			if (dir != IEEE80211_FC1_DIR_TODS) {
332bd6f09d9SSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
333bd6f09d9SSam Leffler 				    wh, "data", "%s", "unknown dir 0x%x", dir);
3341be50176SSam Leffler 				ic->ic_stats.is_rx_wrongdir++;
3351a1e1d21SSam Leffler 				goto out;
3361be50176SSam Leffler 			}
3371a1e1d21SSam Leffler 			/* check if source STA is associated */
3380a915fadSSam Leffler 			if (ni == ic->ic_bss) {
3398a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3408a1b9b6aSSam Leffler 				    wh, "data", "%s", "unknown src");
34184eb84c4SSam Leffler 				ieee80211_send_error(ic, ni, wh->i_addr2,
3421a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
3431a1e1d21SSam Leffler 				    IEEE80211_REASON_NOT_AUTHED);
3441be50176SSam Leffler 				ic->ic_stats.is_rx_notassoc++;
3451a1e1d21SSam Leffler 				goto err;
3461a1e1d21SSam Leffler 			}
3471a1e1d21SSam Leffler 			if (ni->ni_associd == 0) {
3488a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3498a1b9b6aSSam Leffler 				    wh, "data", "%s", "unassoc src");
3501a1e1d21SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
3511a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
3521a1e1d21SSam Leffler 				    IEEE80211_REASON_NOT_ASSOCED);
3531be50176SSam Leffler 				ic->ic_stats.is_rx_notassoc++;
3541a1e1d21SSam Leffler 				goto err;
3551a1e1d21SSam Leffler 			}
3568a1b9b6aSSam Leffler 
3578a1b9b6aSSam Leffler 			/*
3588a1b9b6aSSam Leffler 			 * Check for power save state change.
3598a1b9b6aSSam Leffler 			 */
3608a1b9b6aSSam Leffler 			if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
3618a1b9b6aSSam Leffler 			    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
3628a1b9b6aSSam Leffler 				ieee80211_node_pwrsave(ni,
3638a1b9b6aSSam Leffler 					wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
3641a1e1d21SSam Leffler 			break;
3658a1b9b6aSSam Leffler 		default:
3668a1b9b6aSSam Leffler 			/* XXX here to keep compiler happy */
3678a1b9b6aSSam Leffler 			goto out;
3681a1e1d21SSam Leffler 		}
3698a1b9b6aSSam Leffler 
3708a1b9b6aSSam Leffler 		/*
3718a1b9b6aSSam Leffler 		 * Handle privacy requirements.  Note that we
3728a1b9b6aSSam Leffler 		 * must not be preempted from here until after
3738a1b9b6aSSam Leffler 		 * we (potentially) call ieee80211_crypto_demic;
3748a1b9b6aSSam Leffler 		 * otherwise we may violate assumptions in the
3758a1b9b6aSSam Leffler 		 * crypto cipher modules used to do delayed update
3768a1b9b6aSSam Leffler 		 * of replay sequence numbers.
3778a1b9b6aSSam Leffler 		 */
3781a1e1d21SSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
3798a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
3808a1b9b6aSSam Leffler 				/*
3818a1b9b6aSSam Leffler 				 * Discard encrypted frames when privacy is off.
3828a1b9b6aSSam Leffler 				 */
3838a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
3848a1b9b6aSSam Leffler 				    wh, "WEP", "%s", "PRIVACY off");
3858a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_noprivacy++;
3868a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
3878a1b9b6aSSam Leffler 				goto out;
3888a1b9b6aSSam Leffler 			}
3892cc12adeSSam Leffler 			key = ieee80211_crypto_decap(ic, ni, m, hdrspace);
3908a1b9b6aSSam Leffler 			if (key == NULL) {
3918a1b9b6aSSam Leffler 				/* NB: stats+msgs handled in crypto_decap */
3928a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_wepfail);
3938a1b9b6aSSam Leffler 				goto out;
3941be50176SSam Leffler 			}
3951a1e1d21SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
3968116d318SSam Leffler 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3971be50176SSam Leffler 		} else {
3988a1b9b6aSSam Leffler 			key = NULL;
3998a1b9b6aSSam Leffler 		}
4008a1b9b6aSSam Leffler 
4018a1b9b6aSSam Leffler 		/*
4028a1b9b6aSSam Leffler 		 * Next up, any fragmentation.
4038a1b9b6aSSam Leffler 		 */
4048a1b9b6aSSam Leffler 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
4052cc12adeSSam Leffler 			m = ieee80211_defrag(ic, ni, m, hdrspace);
4068a1b9b6aSSam Leffler 			if (m == NULL) {
4078a1b9b6aSSam Leffler 				/* Fragment dropped or frame not complete yet */
4081a1e1d21SSam Leffler 				goto out;
4091a1e1d21SSam Leffler 			}
4101be50176SSam Leffler 		}
4118a1b9b6aSSam Leffler 		wh = NULL;		/* no longer valid, catch any uses */
4128a1b9b6aSSam Leffler 
4138a1b9b6aSSam Leffler 		/*
4148a1b9b6aSSam Leffler 		 * Next strip any MSDU crypto bits.
4158a1b9b6aSSam Leffler 		 */
41696d88463SSam Leffler 		if (key != NULL && !ieee80211_crypto_demic(ic, key, m, 0)) {
4178a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
4188a1b9b6aSSam Leffler 			    ni->ni_macaddr, "data", "%s", "demic error");
4198a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_demicfail);
4208a1b9b6aSSam Leffler 			goto out;
4218a1b9b6aSSam Leffler 		}
4228a1b9b6aSSam Leffler 
4231a1e1d21SSam Leffler 		/* copy to listener after decrypt */
4241a1e1d21SSam Leffler 		if (ic->ic_rawbpf)
4251a1e1d21SSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
4268a1b9b6aSSam Leffler 
4278a1b9b6aSSam Leffler 		/*
4288a1b9b6aSSam Leffler 		 * Finally, strip the 802.11 header.
4298a1b9b6aSSam Leffler 		 */
4302cc12adeSSam Leffler 		m = ieee80211_decap(ic, m, hdrspace);
4311be50176SSam Leffler 		if (m == NULL) {
4328a1b9b6aSSam Leffler 			/* don't count Null data frames as errors */
4338a1b9b6aSSam Leffler 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA)
4348a1b9b6aSSam Leffler 				goto out;
4358a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
4368a1b9b6aSSam Leffler 			    ni->ni_macaddr, "data", "%s", "decap error");
4371be50176SSam Leffler 			ic->ic_stats.is_rx_decap++;
4388a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_decap);
4391a1e1d21SSam Leffler 			goto err;
4401be50176SSam Leffler 		}
4418a1b9b6aSSam Leffler 		eh = mtod(m, struct ether_header *);
4428a1b9b6aSSam Leffler 		if (!ieee80211_node_is_authorized(ni)) {
4438a1b9b6aSSam Leffler 			/*
4448a1b9b6aSSam Leffler 			 * Deny any non-PAE frames received prior to
4458a1b9b6aSSam Leffler 			 * authorization.  For open/shared-key
4468a1b9b6aSSam Leffler 			 * authentication the port is mark authorized
4478a1b9b6aSSam Leffler 			 * after authentication completes.  For 802.1x
4488a1b9b6aSSam Leffler 			 * the port is not marked authorized by the
4498a1b9b6aSSam Leffler 			 * authenticator until the handshake has completed.
4508a1b9b6aSSam Leffler 			 */
4518a1b9b6aSSam Leffler 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
4528a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
4538a1b9b6aSSam Leffler 				    eh->ether_shost, "data",
4548a1b9b6aSSam Leffler 				    "unauthorized port: ether type 0x%x len %u",
4558a1b9b6aSSam Leffler 				    eh->ether_type, m->m_pkthdr.len);
4568a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_unauth++;
4578a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unauth);
4588a1b9b6aSSam Leffler 				goto err;
4598a1b9b6aSSam Leffler 			}
4608a1b9b6aSSam Leffler 		} else {
4618a1b9b6aSSam Leffler 			/*
4628a1b9b6aSSam Leffler 			 * When denying unencrypted frames, discard
4638a1b9b6aSSam Leffler 			 * any non-PAE frames received without encryption.
4648a1b9b6aSSam Leffler 			 */
4658a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_DROPUNENC) &&
4668a1b9b6aSSam Leffler 			    key == NULL &&
4678a1b9b6aSSam Leffler 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
4688a1b9b6aSSam Leffler 				/*
4698a1b9b6aSSam Leffler 				 * Drop unencrypted frames.
4708a1b9b6aSSam Leffler 				 */
4718a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_unencrypted++;
4728a1b9b6aSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
4738a1b9b6aSSam Leffler 				goto out;
4748a1b9b6aSSam Leffler 			}
4758a1b9b6aSSam Leffler 		}
4761a1e1d21SSam Leffler 		ifp->if_ipackets++;
4778a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_data);
4788a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
4791a1e1d21SSam Leffler 
4801bd482efSSam Leffler 		ieee80211_deliver_data(ic, ni, m);
4811f298879SSam Leffler 		return IEEE80211_FC0_TYPE_DATA;
4821a1e1d21SSam Leffler 
4831a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
4848a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_mgmt);
4851be50176SSam Leffler 		if (dir != IEEE80211_FC1_DIR_NODS) {
486bd6f09d9SSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
487bd6f09d9SSam Leffler 			    wh, "data", "%s", "unknown dir 0x%x", dir);
4881be50176SSam Leffler 			ic->ic_stats.is_rx_wrongdir++;
4891a1e1d21SSam Leffler 			goto err;
4901be50176SSam Leffler 		}
4918a1b9b6aSSam Leffler 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
4928a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
4938a1b9b6aSSam Leffler 			    ni->ni_macaddr, "mgt", "too short: len %u",
4948a1b9b6aSSam Leffler 			    m->m_pkthdr.len);
4958a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_tooshort++;
4961a1e1d21SSam Leffler 			goto out;
4971be50176SSam Leffler 		}
4981a1e1d21SSam Leffler #ifdef IEEE80211_DEBUG
4998a1b9b6aSSam Leffler 		if ((ieee80211_msg_debug(ic) && doprint(ic, subtype)) ||
5008a1b9b6aSSam Leffler 		    ieee80211_msg_dumppkts(ic)) {
5018a1b9b6aSSam Leffler 			if_printf(ic->ic_ifp, "received %s from %s rssi %d\n",
5028a1b9b6aSSam Leffler 			    ieee80211_mgt_subtype_name[subtype >>
5038a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
5041a1e1d21SSam Leffler 			    ether_sprintf(wh->i_addr2), rssi);
5051a1e1d21SSam Leffler 		}
5068a1b9b6aSSam Leffler #endif
5078a1b9b6aSSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
5088a1b9b6aSSam Leffler 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
5098a1b9b6aSSam Leffler 				/*
5108a1b9b6aSSam Leffler 				 * Only shared key auth frames with a challenge
5118a1b9b6aSSam Leffler 				 * should be encrypted, discard all others.
5128a1b9b6aSSam Leffler 				 */
5138a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
5148a1b9b6aSSam Leffler 				    wh, ieee80211_mgt_subtype_name[subtype >>
5158a1b9b6aSSam Leffler 					IEEE80211_FC0_SUBTYPE_SHIFT],
5168a1b9b6aSSam Leffler 				    "%s", "WEP set but not permitted");
5178a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_mgtdiscard++; /* XXX */
5188a1b9b6aSSam Leffler 				goto out;
5198a1b9b6aSSam Leffler 			}
5208a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
5218a1b9b6aSSam Leffler 				/*
5228a1b9b6aSSam Leffler 				 * Discard encrypted frames when privacy is off.
5238a1b9b6aSSam Leffler 				 */
5248a1b9b6aSSam Leffler 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
5258a1b9b6aSSam Leffler 				    wh, "mgt", "%s", "WEP set but PRIVACY off");
5268a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_noprivacy++;
5278a1b9b6aSSam Leffler 				goto out;
5288a1b9b6aSSam Leffler 			}
5292cc12adeSSam Leffler 			hdrspace = ieee80211_hdrspace(ic, wh);
5302cc12adeSSam Leffler 			key = ieee80211_crypto_decap(ic, ni, m, hdrspace);
5318a1b9b6aSSam Leffler 			if (key == NULL) {
5328a1b9b6aSSam Leffler 				/* NB: stats+msgs handled in crypto_decap */
5338a1b9b6aSSam Leffler 				goto out;
5348a1b9b6aSSam Leffler 			}
5358116d318SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
5368116d318SSam Leffler 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
5378a1b9b6aSSam Leffler 		}
5381a1e1d21SSam Leffler 		if (ic->ic_rawbpf)
5391a1e1d21SSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
5400a915fadSSam Leffler 		(*ic->ic_recv_mgmt)(ic, m, ni, subtype, rssi, rstamp);
5411a1e1d21SSam Leffler 		m_freem(m);
5421f298879SSam Leffler 		return type;
5431a1e1d21SSam Leffler 
5441a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_CTL:
5458a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_ctrl);
5461be50176SSam Leffler 		ic->ic_stats.is_rx_ctl++;
5478a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
5488a1b9b6aSSam Leffler 			switch (subtype) {
5498a1b9b6aSSam Leffler 			case IEEE80211_FC0_SUBTYPE_PS_POLL:
5508a1b9b6aSSam Leffler 				ieee80211_recv_pspoll(ic, ni, m);
5518a1b9b6aSSam Leffler 				break;
5528a1b9b6aSSam Leffler 			}
5538a1b9b6aSSam Leffler 		}
55418f897abSSam Leffler 		goto out;
5551a1e1d21SSam Leffler 	default:
5568a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
5578a1b9b6aSSam Leffler 		    wh, NULL, "bad frame type 0x%x", type);
5581a1e1d21SSam Leffler 		/* should not come here */
5591a1e1d21SSam Leffler 		break;
5601a1e1d21SSam Leffler 	}
5611a1e1d21SSam Leffler err:
5621a1e1d21SSam Leffler 	ifp->if_ierrors++;
5631a1e1d21SSam Leffler out:
5641a1e1d21SSam Leffler 	if (m != NULL) {
5651a1e1d21SSam Leffler 		if (ic->ic_rawbpf)
5661a1e1d21SSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
5671a1e1d21SSam Leffler 		m_freem(m);
5681a1e1d21SSam Leffler 	}
5691f298879SSam Leffler 	return type;
5708a1b9b6aSSam Leffler #undef SEQ_LEQ
5711a1e1d21SSam Leffler }
5721a1e1d21SSam Leffler 
5738a1b9b6aSSam Leffler /*
5748a1b9b6aSSam Leffler  * This function reassemble fragments.
5758a1b9b6aSSam Leffler  */
5768a1b9b6aSSam Leffler static struct mbuf *
5778a1b9b6aSSam Leffler ieee80211_defrag(struct ieee80211com *ic, struct ieee80211_node *ni,
5782cc12adeSSam Leffler 	struct mbuf *m, int hdrspace)
5791a1e1d21SSam Leffler {
5808a1b9b6aSSam Leffler 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
5818a1b9b6aSSam Leffler 	struct ieee80211_frame *lwh;
5828a1b9b6aSSam Leffler 	u_int16_t rxseq;
5838a1b9b6aSSam Leffler 	u_int8_t fragno;
5848a1b9b6aSSam Leffler 	u_int8_t more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG;
5858a1b9b6aSSam Leffler 	struct mbuf *mfrag;
5868a1b9b6aSSam Leffler 
5878a1b9b6aSSam Leffler 	KASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1), ("multicast fragm?"));
5888a1b9b6aSSam Leffler 
5898a1b9b6aSSam Leffler 	rxseq = le16toh(*(u_int16_t *)wh->i_seq);
5908a1b9b6aSSam Leffler 	fragno = rxseq & IEEE80211_SEQ_FRAG_MASK;
5918a1b9b6aSSam Leffler 
5928a1b9b6aSSam Leffler 	/* Quick way out, if there's nothing to defragment */
5938a1b9b6aSSam Leffler 	if (!more_frag && fragno == 0 && ni->ni_rxfrag[0] == NULL)
5948a1b9b6aSSam Leffler 		return m;
5958a1b9b6aSSam Leffler 
5968a1b9b6aSSam Leffler 	/*
5978a1b9b6aSSam Leffler 	 * Remove frag to insure it doesn't get reaped by timer.
5988a1b9b6aSSam Leffler 	 */
5998a1b9b6aSSam Leffler 	if (ni->ni_table == NULL) {
6008a1b9b6aSSam Leffler 		/*
6018a1b9b6aSSam Leffler 		 * Should never happen.  If the node is orphaned (not in
6028a1b9b6aSSam Leffler 		 * the table) then input packets should not reach here.
6038a1b9b6aSSam Leffler 		 * Otherwise, a concurrent request that yanks the table
6048a1b9b6aSSam Leffler 		 * should be blocked by other interlocking and/or by first
6058a1b9b6aSSam Leffler 		 * shutting the driver down.  Regardless, be defensive
6068a1b9b6aSSam Leffler 		 * here and just bail
6078a1b9b6aSSam Leffler 		 */
6088a1b9b6aSSam Leffler 		/* XXX need msg+stat */
6098a1b9b6aSSam Leffler 		m_freem(m);
6108a1b9b6aSSam Leffler 		return NULL;
6118a1b9b6aSSam Leffler 	}
6128a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(ni->ni_table);
6138a1b9b6aSSam Leffler 	mfrag = ni->ni_rxfrag[0];
6148a1b9b6aSSam Leffler 	ni->ni_rxfrag[0] = NULL;
6158a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(ni->ni_table);
6168a1b9b6aSSam Leffler 
6178a1b9b6aSSam Leffler 	/*
6188a1b9b6aSSam Leffler 	 * Validate new fragment is in order and
6198a1b9b6aSSam Leffler 	 * related to the previous ones.
6208a1b9b6aSSam Leffler 	 */
6218a1b9b6aSSam Leffler 	if (mfrag != NULL) {
6228a1b9b6aSSam Leffler 		u_int16_t last_rxseq;
6238a1b9b6aSSam Leffler 
6248a1b9b6aSSam Leffler 		lwh = mtod(mfrag, struct ieee80211_frame *);
6258a1b9b6aSSam Leffler 		last_rxseq = le16toh(*(u_int16_t *)lwh->i_seq);
6268a1b9b6aSSam Leffler 		/* NB: check seq # and frag together */
6278a1b9b6aSSam Leffler 		if (rxseq != last_rxseq+1 ||
6288a1b9b6aSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) ||
6298a1b9b6aSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) {
6308a1b9b6aSSam Leffler 			/*
6318a1b9b6aSSam Leffler 			 * Unrelated fragment or no space for it,
6328a1b9b6aSSam Leffler 			 * clear current fragments.
6338a1b9b6aSSam Leffler 			 */
6348a1b9b6aSSam Leffler 			m_freem(mfrag);
6358a1b9b6aSSam Leffler 			mfrag = NULL;
6368a1b9b6aSSam Leffler 		}
6378a1b9b6aSSam Leffler 	}
6388a1b9b6aSSam Leffler 
6398a1b9b6aSSam Leffler  	if (mfrag == NULL) {
6408a1b9b6aSSam Leffler 		if (fragno != 0) {		/* !first fragment, discard */
6418a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_defrag);
6428a1b9b6aSSam Leffler 			m_freem(m);
6438a1b9b6aSSam Leffler 			return NULL;
6448a1b9b6aSSam Leffler 		}
6458a1b9b6aSSam Leffler 		mfrag = m;
6468a1b9b6aSSam Leffler 	} else {				/* concatenate */
6472cc12adeSSam Leffler 		m_adj(m, hdrspace);		/* strip header */
6488a1b9b6aSSam Leffler 		m_cat(mfrag, m);
6498a1b9b6aSSam Leffler 		/* NB: m_cat doesn't update the packet header */
6508a1b9b6aSSam Leffler 		mfrag->m_pkthdr.len += m->m_pkthdr.len;
6518a1b9b6aSSam Leffler 		/* track last seqnum and fragno */
6528a1b9b6aSSam Leffler 		lwh = mtod(mfrag, struct ieee80211_frame *);
6538a1b9b6aSSam Leffler 		*(u_int16_t *) lwh->i_seq = *(u_int16_t *) wh->i_seq;
6548a1b9b6aSSam Leffler 	}
6558a1b9b6aSSam Leffler 	if (more_frag) {			/* more to come, save */
656a0cc3f85SSam Leffler 		ni->ni_rxfragstamp = ticks;
6578a1b9b6aSSam Leffler 		ni->ni_rxfrag[0] = mfrag;
6588a1b9b6aSSam Leffler 		mfrag = NULL;
6598a1b9b6aSSam Leffler 	}
6608a1b9b6aSSam Leffler 	return mfrag;
6618a1b9b6aSSam Leffler }
6628a1b9b6aSSam Leffler 
6631bd482efSSam Leffler static void
6641bd482efSSam Leffler ieee80211_deliver_data(struct ieee80211com *ic,
6651bd482efSSam Leffler 	struct ieee80211_node *ni, struct mbuf *m)
6661bd482efSSam Leffler {
6671bd482efSSam Leffler 	struct ether_header *eh = mtod(m, struct ether_header *);
6681bd482efSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
6691bd482efSSam Leffler 
6701bd482efSSam Leffler 	/* perform as a bridge within the AP */
6711bd482efSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
6721bd482efSSam Leffler 	    (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0) {
6731bd482efSSam Leffler 		struct mbuf *m1 = NULL;
6741bd482efSSam Leffler 
6751bd482efSSam Leffler 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
6761bd482efSSam Leffler 			m1 = m_copypacket(m, M_DONTWAIT);
6771bd482efSSam Leffler 			if (m1 == NULL)
6781bd482efSSam Leffler 				ifp->if_oerrors++;
6791bd482efSSam Leffler 			else
6801bd482efSSam Leffler 				m1->m_flags |= M_MCAST;
6811bd482efSSam Leffler 		} else {
6821bd482efSSam Leffler 			/* XXX this dups work done in ieee80211_encap */
6831bd482efSSam Leffler 			/* check if destination is associated */
6841bd482efSSam Leffler 			struct ieee80211_node *ni1 =
6851bd482efSSam Leffler 			    ieee80211_find_node(&ic->ic_sta,
6861bd482efSSam Leffler 						eh->ether_dhost);
6871bd482efSSam Leffler 			if (ni1 != NULL) {
6881bd482efSSam Leffler 				if (ieee80211_node_is_authorized(ni1)) {
6891bd482efSSam Leffler 					m1 = m;
6901bd482efSSam Leffler 					m = NULL;
6911bd482efSSam Leffler 				}
6921bd482efSSam Leffler 				/* XXX statistic? */
6931bd482efSSam Leffler 				ieee80211_free_node(ni1);
6941bd482efSSam Leffler 			}
6951bd482efSSam Leffler 		}
6961bd482efSSam Leffler 		if (m1 != NULL)
6971bd482efSSam Leffler 			IF_HANDOFF(&ifp->if_snd, m1, ifp);
6981bd482efSSam Leffler 	}
6991bd482efSSam Leffler 	if (m != NULL) {
7001bd482efSSam Leffler 		if (ni->ni_vlan != 0) {
7011bd482efSSam Leffler 			/* attach vlan tag */
7021bd482efSSam Leffler 			/* XXX goto err? */
7031bd482efSSam Leffler 			VLAN_INPUT_TAG(ifp, m, ni->ni_vlan, goto out);
7041bd482efSSam Leffler 		}
7051bd482efSSam Leffler 		(*ifp->if_input)(ifp, m);
7061bd482efSSam Leffler 	}
7071bd482efSSam Leffler 	return;
7081bd482efSSam Leffler   out:
7091bd482efSSam Leffler 	if (m != NULL) {
7101bd482efSSam Leffler 		if (ic->ic_rawbpf)
7111bd482efSSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
7121bd482efSSam Leffler 		m_freem(m);
7131bd482efSSam Leffler 	}
7141bd482efSSam Leffler }
7151bd482efSSam Leffler 
7168a1b9b6aSSam Leffler static struct mbuf *
7172cc12adeSSam Leffler ieee80211_decap(struct ieee80211com *ic, struct mbuf *m, int hdrlen)
7188a1b9b6aSSam Leffler {
7192cc12adeSSam Leffler 	struct ieee80211_qosframe_addr4 wh;	/* Max size address frames */
7201a1e1d21SSam Leffler 	struct ether_header *eh;
7211a1e1d21SSam Leffler 	struct llc *llc;
7221a1e1d21SSam Leffler 
7232cc12adeSSam Leffler 	if (m->m_len < hdrlen + sizeof(*llc) &&
7242cc12adeSSam Leffler 	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
7258a1b9b6aSSam Leffler 		/* XXX stat, msg */
7261a1e1d21SSam Leffler 		return NULL;
7271a1e1d21SSam Leffler 	}
7282cc12adeSSam Leffler 	memcpy(&wh, mtod(m, caddr_t), hdrlen);
7292cc12adeSSam Leffler 	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
7301a1e1d21SSam Leffler 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
7311a1e1d21SSam Leffler 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
7321a1e1d21SSam Leffler 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0) {
7332cc12adeSSam Leffler 		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
7341a1e1d21SSam Leffler 		llc = NULL;
7351a1e1d21SSam Leffler 	} else {
7362cc12adeSSam Leffler 		m_adj(m, hdrlen - sizeof(*eh));
7371a1e1d21SSam Leffler 	}
7381a1e1d21SSam Leffler 	eh = mtod(m, struct ether_header *);
7391a1e1d21SSam Leffler 	switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
7401a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_NODS:
7411a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
7421a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
7431a1e1d21SSam Leffler 		break;
7441a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_TODS:
7451a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
7461a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
7471a1e1d21SSam Leffler 		break;
7481a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
7491a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
7501a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3);
7511a1e1d21SSam Leffler 		break;
7521a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
7532cc12adeSSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
7542cc12adeSSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr4);
7552cc12adeSSam Leffler 		break;
7561a1e1d21SSam Leffler 	}
7571a1e1d21SSam Leffler #ifdef ALIGNED_POINTER
7581a1e1d21SSam Leffler 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), u_int32_t)) {
7591a1e1d21SSam Leffler 		struct mbuf *n, *n0, **np;
7601a1e1d21SSam Leffler 		caddr_t newdata;
7611a1e1d21SSam Leffler 		int off, pktlen;
7621a1e1d21SSam Leffler 
7631a1e1d21SSam Leffler 		n0 = NULL;
7641a1e1d21SSam Leffler 		np = &n0;
7651a1e1d21SSam Leffler 		off = 0;
7661a1e1d21SSam Leffler 		pktlen = m->m_pkthdr.len;
7671a1e1d21SSam Leffler 		while (pktlen > off) {
7681a1e1d21SSam Leffler 			if (n0 == NULL) {
7691a1e1d21SSam Leffler 				MGETHDR(n, M_DONTWAIT, MT_DATA);
7701a1e1d21SSam Leffler 				if (n == NULL) {
7711a1e1d21SSam Leffler 					m_freem(m);
7721a1e1d21SSam Leffler 					return NULL;
7731a1e1d21SSam Leffler 				}
7741a1e1d21SSam Leffler 				M_MOVE_PKTHDR(n, m);
7751a1e1d21SSam Leffler 				n->m_len = MHLEN;
7761a1e1d21SSam Leffler 			} else {
7771a1e1d21SSam Leffler 				MGET(n, M_DONTWAIT, MT_DATA);
7781a1e1d21SSam Leffler 				if (n == NULL) {
7791a1e1d21SSam Leffler 					m_freem(m);
7801a1e1d21SSam Leffler 					m_freem(n0);
7811a1e1d21SSam Leffler 					return NULL;
7821a1e1d21SSam Leffler 				}
7831a1e1d21SSam Leffler 				n->m_len = MLEN;
7841a1e1d21SSam Leffler 			}
7851a1e1d21SSam Leffler 			if (pktlen - off >= MINCLSIZE) {
7861a1e1d21SSam Leffler 				MCLGET(n, M_DONTWAIT);
7871a1e1d21SSam Leffler 				if (n->m_flags & M_EXT)
7881a1e1d21SSam Leffler 					n->m_len = n->m_ext.ext_size;
7891a1e1d21SSam Leffler 			}
7901a1e1d21SSam Leffler 			if (n0 == NULL) {
7911a1e1d21SSam Leffler 				newdata =
7921a1e1d21SSam Leffler 				    (caddr_t)ALIGN(n->m_data + sizeof(*eh)) -
7931a1e1d21SSam Leffler 				    sizeof(*eh);
7941a1e1d21SSam Leffler 				n->m_len -= newdata - n->m_data;
7951a1e1d21SSam Leffler 				n->m_data = newdata;
7961a1e1d21SSam Leffler 			}
7971a1e1d21SSam Leffler 			if (n->m_len > pktlen - off)
7981a1e1d21SSam Leffler 				n->m_len = pktlen - off;
7991a1e1d21SSam Leffler 			m_copydata(m, off, n->m_len, mtod(n, caddr_t));
8001a1e1d21SSam Leffler 			off += n->m_len;
8011a1e1d21SSam Leffler 			*np = n;
8021a1e1d21SSam Leffler 			np = &n->m_next;
8031a1e1d21SSam Leffler 		}
8041a1e1d21SSam Leffler 		m_freem(m);
8051a1e1d21SSam Leffler 		m = n0;
8061a1e1d21SSam Leffler 	}
8071a1e1d21SSam Leffler #endif /* ALIGNED_POINTER */
8081a1e1d21SSam Leffler 	if (llc != NULL) {
8091a1e1d21SSam Leffler 		eh = mtod(m, struct ether_header *);
8101a1e1d21SSam Leffler 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
8111a1e1d21SSam Leffler 	}
8121a1e1d21SSam Leffler 	return m;
8131a1e1d21SSam Leffler }
8141a1e1d21SSam Leffler 
8151a1e1d21SSam Leffler /*
8161a1e1d21SSam Leffler  * Install received rate set information in the node's state block.
8171a1e1d21SSam Leffler  */
8181a1e1d21SSam Leffler static int
8191a1e1d21SSam Leffler ieee80211_setup_rates(struct ieee80211com *ic, struct ieee80211_node *ni,
8201a1e1d21SSam Leffler 	u_int8_t *rates, u_int8_t *xrates, int flags)
8211a1e1d21SSam Leffler {
8221a1e1d21SSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
8231a1e1d21SSam Leffler 
8241a1e1d21SSam Leffler 	memset(rs, 0, sizeof(*rs));
8251a1e1d21SSam Leffler 	rs->rs_nrates = rates[1];
8261a1e1d21SSam Leffler 	memcpy(rs->rs_rates, rates + 2, rs->rs_nrates);
8271a1e1d21SSam Leffler 	if (xrates != NULL) {
8281a1e1d21SSam Leffler 		u_int8_t nxrates;
8291a1e1d21SSam Leffler 		/*
8301a1e1d21SSam Leffler 		 * Tack on 11g extended supported rate element.
8311a1e1d21SSam Leffler 		 */
8321a1e1d21SSam Leffler 		nxrates = xrates[1];
8331a1e1d21SSam Leffler 		if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
8341a1e1d21SSam Leffler 			nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates;
8358a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_XRATE,
8368a1b9b6aSSam Leffler 			     "[%s] extended rate set too large;"
8371a1e1d21SSam Leffler 			     " only using %u of %u rates\n",
8388a1b9b6aSSam Leffler 			     ether_sprintf(ni->ni_macaddr), nxrates, xrates[1]);
8391be50176SSam Leffler 			ic->ic_stats.is_rx_rstoobig++;
8401a1e1d21SSam Leffler 		}
8411a1e1d21SSam Leffler 		memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates);
8421a1e1d21SSam Leffler 		rs->rs_nrates += nxrates;
8431a1e1d21SSam Leffler 	}
8441a1e1d21SSam Leffler 	return ieee80211_fix_rate(ic, ni, flags);
8451a1e1d21SSam Leffler }
8461a1e1d21SSam Leffler 
8478a1b9b6aSSam Leffler static void
8488a1b9b6aSSam Leffler ieee80211_auth_open(struct ieee80211com *ic, struct ieee80211_frame *wh,
8498a1b9b6aSSam Leffler     struct ieee80211_node *ni, int rssi, u_int32_t rstamp, u_int16_t seq,
8508a1b9b6aSSam Leffler     u_int16_t status)
8518a1b9b6aSSam Leffler {
8528a1b9b6aSSam Leffler 
853b8d05d3cSSam Leffler 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
854b8d05d3cSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
855b8d05d3cSSam Leffler 		    ni->ni_macaddr, "open auth",
856b8d05d3cSSam Leffler 		    "bad sta auth mode %u", ni->ni_authmode);
857b8d05d3cSSam Leffler 		ic->ic_stats.is_rx_bad_auth++;	/* XXX */
858b8d05d3cSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
859b8d05d3cSSam Leffler 			/* XXX hack to workaround calling convention */
860b8d05d3cSSam Leffler 			ieee80211_send_error(ic, ni, wh->i_addr2,
861b8d05d3cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_AUTH,
862b8d05d3cSSam Leffler 			    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
863b8d05d3cSSam Leffler 		}
864b8d05d3cSSam Leffler 		return;
865b8d05d3cSSam Leffler 	}
8668a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
8678a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:
8688a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:
8694fd1a57dSSam Leffler 	case IEEE80211_M_MONITOR:
8708a1b9b6aSSam Leffler 		/* should not come here */
8714fd1a57dSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
8724fd1a57dSSam Leffler 		    ni->ni_macaddr, "open auth",
8734fd1a57dSSam Leffler 		    "bad operating mode %u", ic->ic_opmode);
8748a1b9b6aSSam Leffler 		break;
8758a1b9b6aSSam Leffler 
8768a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:
8778a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN ||
8788a1b9b6aSSam Leffler 		    seq != IEEE80211_AUTH_OPEN_REQUEST) {
8798a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
8808a1b9b6aSSam Leffler 			return;
8818a1b9b6aSSam Leffler 		}
8828a1b9b6aSSam Leffler 		/* always accept open authentication requests */
8838a1b9b6aSSam Leffler 		if (ni == ic->ic_bss) {
884acc4f7f5SSam Leffler 			ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
8858a1b9b6aSSam Leffler 			if (ni == NULL)
8868a1b9b6aSSam Leffler 				return;
887ebdda46cSSam Leffler 		} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
8888a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
889ebdda46cSSam Leffler 		/*
890ebdda46cSSam Leffler 		 * Mark the node as referenced to reflect that it's
891ebdda46cSSam Leffler 		 * reference count has been bumped to insure it remains
892ebdda46cSSam Leffler 		 * after the transaction completes.
893ebdda46cSSam Leffler 		 */
894ebdda46cSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_AREF;
895ebdda46cSSam Leffler 
8968a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni,
8978a1b9b6aSSam Leffler 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
8988a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
8998a1b9b6aSSam Leffler 		    "[%s] station authenticated (open)\n",
9008a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
901da17aba1SSam Leffler 		/*
902da17aba1SSam Leffler 		 * When 802.1x is not in use mark the port
903da17aba1SSam Leffler 		 * authorized at this point so traffic can flow.
904da17aba1SSam Leffler 		 */
905da17aba1SSam Leffler 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
906da17aba1SSam Leffler 			ieee80211_node_authorize(ic, ni);
9078a1b9b6aSSam Leffler 		break;
9088a1b9b6aSSam Leffler 
9098a1b9b6aSSam Leffler 	case IEEE80211_M_STA:
9108a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_AUTH ||
9118a1b9b6aSSam Leffler 		    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
9128a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
9138a1b9b6aSSam Leffler 			return;
9148a1b9b6aSSam Leffler 		}
9158a1b9b6aSSam Leffler 		if (status != 0) {
9168a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
9178a1b9b6aSSam Leffler 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
9188a1b9b6aSSam Leffler 			    "[%s] open auth failed (reason %d)\n",
9198a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr), status);
9208a1b9b6aSSam Leffler 			/* XXX can this happen? */
9218a1b9b6aSSam Leffler 			if (ni != ic->ic_bss)
9228a1b9b6aSSam Leffler 				ni->ni_fails++;
9238a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_fail++;
924181181acSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
925181181acSSam Leffler 		} else
9268a1b9b6aSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
9278a1b9b6aSSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
9288a1b9b6aSSam Leffler 		break;
9298a1b9b6aSSam Leffler 	}
9308a1b9b6aSSam Leffler }
9318a1b9b6aSSam Leffler 
93284eb84c4SSam Leffler /*
93384eb84c4SSam Leffler  * Send a management frame error response to the specified
93484eb84c4SSam Leffler  * station.  If ni is associated with the station then use
93584eb84c4SSam Leffler  * it; otherwise allocate a temporary node suitable for
93684eb84c4SSam Leffler  * transmitting the frame and then free the reference so
93784eb84c4SSam Leffler  * it will go away as soon as the frame has been transmitted.
93884eb84c4SSam Leffler  */
93984eb84c4SSam Leffler static void
94084eb84c4SSam Leffler ieee80211_send_error(struct ieee80211com *ic, struct ieee80211_node *ni,
94184eb84c4SSam Leffler 	const u_int8_t *mac, int subtype, int arg)
94284eb84c4SSam Leffler {
94384eb84c4SSam Leffler 	int istmp;
94484eb84c4SSam Leffler 
94584eb84c4SSam Leffler 	if (ni == ic->ic_bss) {
94684eb84c4SSam Leffler 		ni = ieee80211_dup_bss(&ic->ic_sta, mac);
94784eb84c4SSam Leffler 		if (ni == NULL) {
94884eb84c4SSam Leffler 			/* XXX msg */
94984eb84c4SSam Leffler 			return;
95084eb84c4SSam Leffler 		}
95184eb84c4SSam Leffler 		istmp = 1;
95284eb84c4SSam Leffler 	} else
95384eb84c4SSam Leffler 		istmp = 0;
95484eb84c4SSam Leffler 	IEEE80211_SEND_MGMT(ic, ni, subtype, arg);
95584eb84c4SSam Leffler 	if (istmp)
95684eb84c4SSam Leffler 		ieee80211_free_node(ni);
95784eb84c4SSam Leffler }
95884eb84c4SSam Leffler 
9598a1b9b6aSSam Leffler static int
9608a1b9b6aSSam Leffler alloc_challenge(struct ieee80211com *ic, struct ieee80211_node *ni)
9618a1b9b6aSSam Leffler {
9628a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL)
9638a1b9b6aSSam Leffler 		MALLOC(ni->ni_challenge, u_int32_t*, IEEE80211_CHALLENGE_LEN,
9648a1b9b6aSSam Leffler 		    M_DEVBUF, M_NOWAIT);
9658a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL) {
9668a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
9678a1b9b6aSSam Leffler 		    "[%s] shared key challenge alloc failed\n",
9688a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
9698a1b9b6aSSam Leffler 		/* XXX statistic */
9708a1b9b6aSSam Leffler 	}
9718a1b9b6aSSam Leffler 	return (ni->ni_challenge != NULL);
9728a1b9b6aSSam Leffler }
9738a1b9b6aSSam Leffler 
9748a1b9b6aSSam Leffler /* XXX TODO: add statistics */
9758a1b9b6aSSam Leffler static void
9768a1b9b6aSSam Leffler ieee80211_auth_shared(struct ieee80211com *ic, struct ieee80211_frame *wh,
9778a1b9b6aSSam Leffler     u_int8_t *frm, u_int8_t *efrm, struct ieee80211_node *ni, int rssi,
9788a1b9b6aSSam Leffler     u_int32_t rstamp, u_int16_t seq, u_int16_t status)
9798a1b9b6aSSam Leffler {
9808a1b9b6aSSam Leffler 	u_int8_t *challenge;
9818a1b9b6aSSam Leffler 	int allocbs, estatus;
9828a1b9b6aSSam Leffler 
9838a1b9b6aSSam Leffler 	/*
9848a1b9b6aSSam Leffler 	 * NB: this can happen as we allow pre-shared key
9858a1b9b6aSSam Leffler 	 * authentication to be enabled w/o wep being turned
9868a1b9b6aSSam Leffler 	 * on so that configuration of these can be done
9878a1b9b6aSSam Leffler 	 * in any order.  It may be better to enforce the
9888a1b9b6aSSam Leffler 	 * ordering in which case this check would just be
9898a1b9b6aSSam Leffler 	 * for sanity/consistency.
9908a1b9b6aSSam Leffler 	 */
9918a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
9928a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
9938a1b9b6aSSam Leffler 		    ni->ni_macaddr, "shared key auth",
9948a1b9b6aSSam Leffler 		    "%s", " PRIVACY is disabled");
9958a1b9b6aSSam Leffler 		estatus = IEEE80211_STATUS_ALG;
9968a1b9b6aSSam Leffler 		goto bad;
9978a1b9b6aSSam Leffler 	}
9988a1b9b6aSSam Leffler 	/*
9998a1b9b6aSSam Leffler 	 * Pre-shared key authentication is evil; accept
10008a1b9b6aSSam Leffler 	 * it only if explicitly configured (it is supported
10018a1b9b6aSSam Leffler 	 * mainly for compatibility with clients like OS X).
10028a1b9b6aSSam Leffler 	 */
10038a1b9b6aSSam Leffler 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
10048a1b9b6aSSam Leffler 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
10058a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10068a1b9b6aSSam Leffler 		    ni->ni_macaddr, "shared key auth",
10078a1b9b6aSSam Leffler 		    "bad sta auth mode %u", ni->ni_authmode);
10088a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
10098a1b9b6aSSam Leffler 		estatus = IEEE80211_STATUS_ALG;
10108a1b9b6aSSam Leffler 		goto bad;
10118a1b9b6aSSam Leffler 	}
10128a1b9b6aSSam Leffler 
10138a1b9b6aSSam Leffler 	challenge = NULL;
10148a1b9b6aSSam Leffler 	if (frm + 1 < efrm) {
10158a1b9b6aSSam Leffler 		if ((frm[1] + 2) > (efrm - frm)) {
10168a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10178a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10188a1b9b6aSSam Leffler 			    "ie %d/%d too long",
10198a1b9b6aSSam Leffler 			    frm[0], (frm[1] + 2) - (efrm - frm));
10208a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
10218a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
10228a1b9b6aSSam Leffler 			goto bad;
10238a1b9b6aSSam Leffler 		}
10248a1b9b6aSSam Leffler 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
10258a1b9b6aSSam Leffler 			challenge = frm;
10268a1b9b6aSSam Leffler 		frm += frm[1] + 2;
10278a1b9b6aSSam Leffler 	}
10288a1b9b6aSSam Leffler 	switch (seq) {
10298a1b9b6aSSam Leffler 	case IEEE80211_AUTH_SHARED_CHALLENGE:
10308a1b9b6aSSam Leffler 	case IEEE80211_AUTH_SHARED_RESPONSE:
10318a1b9b6aSSam Leffler 		if (challenge == NULL) {
10328a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10338a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10348a1b9b6aSSam Leffler 			    "%s", "no challenge");
10358a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
10368a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
10378a1b9b6aSSam Leffler 			goto bad;
10388a1b9b6aSSam Leffler 		}
10398a1b9b6aSSam Leffler 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
10408a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10418a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10428a1b9b6aSSam Leffler 			    "bad challenge len %d", challenge[1]);
10438a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
10448a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
10458a1b9b6aSSam Leffler 			goto bad;
10468a1b9b6aSSam Leffler 		}
10478a1b9b6aSSam Leffler 	default:
10488a1b9b6aSSam Leffler 		break;
10498a1b9b6aSSam Leffler 	}
10508a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
10518a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:
10528a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:
10538a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:
10548a1b9b6aSSam Leffler 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10558a1b9b6aSSam Leffler 		    ni->ni_macaddr, "shared key auth",
10568a1b9b6aSSam Leffler 		    "bad operating mode %u", ic->ic_opmode);
10578a1b9b6aSSam Leffler 		return;
10588a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:
10598a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN) {
10608a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
10618a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
10628a1b9b6aSSam Leffler 			    "bad state %u", ic->ic_state);
10638a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_ALG;	/* XXX */
10648a1b9b6aSSam Leffler 			goto bad;
10658a1b9b6aSSam Leffler 		}
10668a1b9b6aSSam Leffler 		switch (seq) {
10678a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_REQUEST:
10688a1b9b6aSSam Leffler 			if (ni == ic->ic_bss) {
1069acc4f7f5SSam Leffler 				ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
10708a1b9b6aSSam Leffler 				if (ni == NULL) {
10718a1b9b6aSSam Leffler 					/* NB: no way to return an error */
10728a1b9b6aSSam Leffler 					return;
10738a1b9b6aSSam Leffler 				}
10748a1b9b6aSSam Leffler 				allocbs = 1;
10758a1b9b6aSSam Leffler 			} else {
1076ebdda46cSSam Leffler 				if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
10778a1b9b6aSSam Leffler 					(void) ieee80211_ref_node(ni);
10788a1b9b6aSSam Leffler 				allocbs = 0;
10798a1b9b6aSSam Leffler 			}
1080ebdda46cSSam Leffler 			/*
1081ebdda46cSSam Leffler 			 * Mark the node as referenced to reflect that it's
1082ebdda46cSSam Leffler 			 * reference count has been bumped to insure it remains
1083ebdda46cSSam Leffler 			 * after the transaction completes.
1084ebdda46cSSam Leffler 			 */
1085ebdda46cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_AREF;
10868a1b9b6aSSam Leffler 			ni->ni_rssi = rssi;
10878a1b9b6aSSam Leffler 			ni->ni_rstamp = rstamp;
10888a1b9b6aSSam Leffler 			if (!alloc_challenge(ic, ni)) {
10898a1b9b6aSSam Leffler 				/* NB: don't return error so they rexmit */
10908a1b9b6aSSam Leffler 				return;
10918a1b9b6aSSam Leffler 			}
10928a1b9b6aSSam Leffler 			get_random_bytes(ni->ni_challenge,
10938a1b9b6aSSam Leffler 				IEEE80211_CHALLENGE_LEN);
10948a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
10958a1b9b6aSSam Leffler 				IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
10968a1b9b6aSSam Leffler 				"[%s] shared key %sauth request\n",
10978a1b9b6aSSam Leffler 				ether_sprintf(ni->ni_macaddr),
10988a1b9b6aSSam Leffler 				allocbs ? "" : "re");
10998a1b9b6aSSam Leffler 			break;
11008a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_RESPONSE:
11018a1b9b6aSSam Leffler 			if (ni == ic->ic_bss) {
11028a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
11038a1b9b6aSSam Leffler 				    ni->ni_macaddr, "shared key response",
11048a1b9b6aSSam Leffler 				    "%s", "unknown station");
11058a1b9b6aSSam Leffler 				/* NB: don't send a response */
11068a1b9b6aSSam Leffler 				return;
11078a1b9b6aSSam Leffler 			}
11088a1b9b6aSSam Leffler 			if (ni->ni_challenge == NULL) {
11098a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
11108a1b9b6aSSam Leffler 				    ni->ni_macaddr, "shared key response",
11118a1b9b6aSSam Leffler 				    "%s", "no challenge recorded");
11128a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_bad_auth++;
11138a1b9b6aSSam Leffler 				estatus = IEEE80211_STATUS_CHALLENGE;
11148a1b9b6aSSam Leffler 				goto bad;
11158a1b9b6aSSam Leffler 			}
11168a1b9b6aSSam Leffler 			if (memcmp(ni->ni_challenge, &challenge[2],
11178a1b9b6aSSam Leffler 			           challenge[1]) != 0) {
11188a1b9b6aSSam Leffler 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
11198a1b9b6aSSam Leffler 				    ni->ni_macaddr, "shared key response",
11208a1b9b6aSSam Leffler 				    "%s", "challenge mismatch");
11218a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_auth_fail++;
11228a1b9b6aSSam Leffler 				estatus = IEEE80211_STATUS_CHALLENGE;
11238a1b9b6aSSam Leffler 				goto bad;
11248a1b9b6aSSam Leffler 			}
11258a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
11268a1b9b6aSSam Leffler 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
11278a1b9b6aSSam Leffler 			    "[%s] station authenticated (shared key)\n",
11288a1b9b6aSSam Leffler 			    ether_sprintf(ni->ni_macaddr));
1129da17aba1SSam Leffler 			ieee80211_node_authorize(ic, ni);
11308a1b9b6aSSam Leffler 			break;
11318a1b9b6aSSam Leffler 		default:
11328a1b9b6aSSam Leffler 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
11338a1b9b6aSSam Leffler 			    ni->ni_macaddr, "shared key auth",
11348a1b9b6aSSam Leffler 			    "bad seq %d", seq);
11358a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
11368a1b9b6aSSam Leffler 			estatus = IEEE80211_STATUS_SEQUENCE;
11378a1b9b6aSSam Leffler 			goto bad;
11388a1b9b6aSSam Leffler 		}
11398a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni,
11408a1b9b6aSSam Leffler 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
11418a1b9b6aSSam Leffler 		break;
11428a1b9b6aSSam Leffler 
11438a1b9b6aSSam Leffler 	case IEEE80211_M_STA:
11448a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_AUTH)
11458a1b9b6aSSam Leffler 			return;
11468a1b9b6aSSam Leffler 		switch (seq) {
11478a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_PASS:
11488a1b9b6aSSam Leffler 			if (ni->ni_challenge != NULL) {
11498a1b9b6aSSam Leffler 				FREE(ni->ni_challenge, M_DEVBUF);
11508a1b9b6aSSam Leffler 				ni->ni_challenge = NULL;
11518a1b9b6aSSam Leffler 			}
11528a1b9b6aSSam Leffler 			if (status != 0) {
11538a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic,
11548a1b9b6aSSam Leffler 				    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
11558a1b9b6aSSam Leffler 				    "[%s] shared key auth failed (reason %d)\n",
11568a1b9b6aSSam Leffler 				    ether_sprintf(ieee80211_getbssid(ic, wh)),
11578a1b9b6aSSam Leffler 				    status);
11588a1b9b6aSSam Leffler 				/* XXX can this happen? */
11598a1b9b6aSSam Leffler 				if (ni != ic->ic_bss)
11608a1b9b6aSSam Leffler 					ni->ni_fails++;
11618a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_auth_fail++;
11628a1b9b6aSSam Leffler 				return;
11638a1b9b6aSSam Leffler 			}
11648a1b9b6aSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
11658a1b9b6aSSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
11668a1b9b6aSSam Leffler 			break;
11678a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED_CHALLENGE:
11688a1b9b6aSSam Leffler 			if (!alloc_challenge(ic, ni))
11698a1b9b6aSSam Leffler 				return;
11708a1b9b6aSSam Leffler 			/* XXX could optimize by passing recvd challenge */
11718a1b9b6aSSam Leffler 			memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
11728a1b9b6aSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni,
11738a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
11748a1b9b6aSSam Leffler 			break;
11758a1b9b6aSSam Leffler 		default:
11768a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_AUTH,
11778a1b9b6aSSam Leffler 			    wh, "shared key auth", "bad seq %d", seq);
11788a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_bad_auth++;
11798a1b9b6aSSam Leffler 			return;
11808a1b9b6aSSam Leffler 		}
11818a1b9b6aSSam Leffler 		break;
11828a1b9b6aSSam Leffler 	}
11838a1b9b6aSSam Leffler 	return;
11848a1b9b6aSSam Leffler bad:
11858a1b9b6aSSam Leffler 	/*
11868a1b9b6aSSam Leffler 	 * Send an error response; but only when operating as an AP.
11878a1b9b6aSSam Leffler 	 */
11888a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
11898a1b9b6aSSam Leffler 		/* XXX hack to workaround calling convention */
119084eb84c4SSam Leffler 		ieee80211_send_error(ic, ni, wh->i_addr2,
11918a1b9b6aSSam Leffler 		    IEEE80211_FC0_SUBTYPE_AUTH,
11928a1b9b6aSSam Leffler 		    (seq + 1) | (estatus<<16));
1193181181acSSam Leffler 	} else if (ic->ic_opmode == IEEE80211_M_STA) {
1194181181acSSam Leffler 		/*
1195181181acSSam Leffler 		 * Kick the state machine.  This short-circuits
1196181181acSSam Leffler 		 * using the mgt frame timeout to trigger the
1197181181acSSam Leffler 		 * state transition.
1198181181acSSam Leffler 		 */
1199181181acSSam Leffler 		if (ic->ic_state == IEEE80211_S_AUTH)
1200181181acSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
12018a1b9b6aSSam Leffler 	}
12028a1b9b6aSSam Leffler }
12038a1b9b6aSSam Leffler 
12041a1e1d21SSam Leffler /* Verify the existence and length of __elem or get out. */
12051a1e1d21SSam Leffler #define IEEE80211_VERIFY_ELEMENT(__elem, __maxlen) do {			\
12061a1e1d21SSam Leffler 	if ((__elem) == NULL) {						\
12078a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
12088a1b9b6aSSam Leffler 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
12098a1b9b6aSSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
12108a1b9b6aSSam Leffler 		    "%s", "no " #__elem );				\
12111be50176SSam Leffler 		ic->ic_stats.is_rx_elem_missing++;			\
12121a1e1d21SSam Leffler 		return;							\
12131a1e1d21SSam Leffler 	}								\
12141a1e1d21SSam Leffler 	if ((__elem)[1] > (__maxlen)) {					\
12158a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
12168a1b9b6aSSam Leffler 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
12171a1e1d21SSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
12188a1b9b6aSSam Leffler 		    "bad " #__elem " len %d", (__elem)[1]);		\
12191be50176SSam Leffler 		ic->ic_stats.is_rx_elem_toobig++;			\
12201a1e1d21SSam Leffler 		return;							\
12211a1e1d21SSam Leffler 	}								\
12221a1e1d21SSam Leffler } while (0)
12231a1e1d21SSam Leffler 
12241a1e1d21SSam Leffler #define	IEEE80211_VERIFY_LENGTH(_len, _minlen) do {			\
12251a1e1d21SSam Leffler 	if ((_len) < (_minlen)) {					\
12268a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
12278a1b9b6aSSam Leffler 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
12281a1e1d21SSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
12298a1b9b6aSSam Leffler 		    "%s", "ie too short");				\
12301be50176SSam Leffler 		ic->ic_stats.is_rx_elem_toosmall++;			\
12311a1e1d21SSam Leffler 		return;							\
12321a1e1d21SSam Leffler 	}								\
12331a1e1d21SSam Leffler } while (0)
12341a1e1d21SSam Leffler 
12358a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
12368a1b9b6aSSam Leffler static void
12378a1b9b6aSSam Leffler ieee80211_ssid_mismatch(struct ieee80211com *ic, const char *tag,
12388a1b9b6aSSam Leffler 	u_int8_t mac[IEEE80211_ADDR_LEN], u_int8_t *ssid)
12398a1b9b6aSSam Leffler {
12408a1b9b6aSSam Leffler 	printf("[%s] discard %s frame, ssid mismatch: ",
12418a1b9b6aSSam Leffler 		ether_sprintf(mac), tag);
12428a1b9b6aSSam Leffler 	ieee80211_print_essid(ssid + 2, ssid[1]);
12438a1b9b6aSSam Leffler 	printf("\n");
12448a1b9b6aSSam Leffler }
12458a1b9b6aSSam Leffler 
12468a1b9b6aSSam Leffler #define	IEEE80211_VERIFY_SSID(_ni, _ssid) do {				\
12478a1b9b6aSSam Leffler 	if ((_ssid)[1] != 0 &&						\
12488a1b9b6aSSam Leffler 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
12498a1b9b6aSSam Leffler 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
12508a1b9b6aSSam Leffler 		if (ieee80211_msg_input(ic))				\
12518a1b9b6aSSam Leffler 			ieee80211_ssid_mismatch(ic, 			\
12528a1b9b6aSSam Leffler 			    ieee80211_mgt_subtype_name[subtype >>	\
12538a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],		\
12548a1b9b6aSSam Leffler 				wh->i_addr2, _ssid);			\
12558a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_ssidmismatch++;			\
12568a1b9b6aSSam Leffler 		return;							\
12578a1b9b6aSSam Leffler 	}								\
12588a1b9b6aSSam Leffler } while (0)
12598a1b9b6aSSam Leffler #else /* !IEEE80211_DEBUG */
12608a1b9b6aSSam Leffler #define	IEEE80211_VERIFY_SSID(_ni, _ssid) do {				\
12618a1b9b6aSSam Leffler 	if ((_ssid)[1] != 0 &&						\
12628a1b9b6aSSam Leffler 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
12638a1b9b6aSSam Leffler 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
12648a1b9b6aSSam Leffler 		ic->ic_stats.is_rx_ssidmismatch++;			\
12658a1b9b6aSSam Leffler 		return;							\
12668a1b9b6aSSam Leffler 	}								\
12678a1b9b6aSSam Leffler } while (0)
12688a1b9b6aSSam Leffler #endif /* !IEEE80211_DEBUG */
12698a1b9b6aSSam Leffler 
12708a1b9b6aSSam Leffler /* unalligned little endian access */
12718a1b9b6aSSam Leffler #define LE_READ_2(p)					\
12728a1b9b6aSSam Leffler 	((u_int16_t)					\
12738a1b9b6aSSam Leffler 	 ((((const u_int8_t *)(p))[0]      ) |		\
12748a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[1] <<  8)))
12758a1b9b6aSSam Leffler #define LE_READ_4(p)					\
12768a1b9b6aSSam Leffler 	((u_int32_t)					\
12778a1b9b6aSSam Leffler 	 ((((const u_int8_t *)(p))[0]      ) |		\
12788a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[1] <<  8) |		\
12798a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[2] << 16) |		\
12808a1b9b6aSSam Leffler 	  (((const u_int8_t *)(p))[3] << 24)))
12818a1b9b6aSSam Leffler 
12828a1b9b6aSSam Leffler static int __inline
12838a1b9b6aSSam Leffler iswpaoui(const u_int8_t *frm)
12848a1b9b6aSSam Leffler {
12858a1b9b6aSSam Leffler 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
12868a1b9b6aSSam Leffler }
12878a1b9b6aSSam Leffler 
12888a1b9b6aSSam Leffler static int __inline
12898a1b9b6aSSam Leffler iswmeoui(const u_int8_t *frm)
12908a1b9b6aSSam Leffler {
12918a1b9b6aSSam Leffler 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
12928a1b9b6aSSam Leffler }
12938a1b9b6aSSam Leffler 
12948a1b9b6aSSam Leffler static int __inline
12958a1b9b6aSSam Leffler iswmeparam(const u_int8_t *frm)
12968a1b9b6aSSam Leffler {
12978a1b9b6aSSam Leffler 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
12988a1b9b6aSSam Leffler 		frm[6] == WME_PARAM_OUI_SUBTYPE;
12998a1b9b6aSSam Leffler }
13008a1b9b6aSSam Leffler 
13018a1b9b6aSSam Leffler static int __inline
13028a1b9b6aSSam Leffler iswmeinfo(const u_int8_t *frm)
13038a1b9b6aSSam Leffler {
13048a1b9b6aSSam Leffler 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
13058a1b9b6aSSam Leffler 		frm[6] == WME_INFO_OUI_SUBTYPE;
13068a1b9b6aSSam Leffler }
13078a1b9b6aSSam Leffler 
13088a1b9b6aSSam Leffler static int __inline
13098a1b9b6aSSam Leffler isatherosoui(const u_int8_t *frm)
13108a1b9b6aSSam Leffler {
13118a1b9b6aSSam Leffler 	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
13128a1b9b6aSSam Leffler }
13138a1b9b6aSSam Leffler 
13148a1b9b6aSSam Leffler /*
13158a1b9b6aSSam Leffler  * Convert a WPA cipher selector OUI to an internal
13168a1b9b6aSSam Leffler  * cipher algorithm.  Where appropriate we also
13178a1b9b6aSSam Leffler  * record any key length.
13188a1b9b6aSSam Leffler  */
13198a1b9b6aSSam Leffler static int
13208a1b9b6aSSam Leffler wpa_cipher(u_int8_t *sel, u_int8_t *keylen)
13218a1b9b6aSSam Leffler {
13228a1b9b6aSSam Leffler #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
13238a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
13248a1b9b6aSSam Leffler 
13258a1b9b6aSSam Leffler 	switch (w) {
13268a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_NULL):
13278a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_NONE;
13288a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_WEP40):
13298a1b9b6aSSam Leffler 		if (keylen)
13308a1b9b6aSSam Leffler 			*keylen = 40 / NBBY;
13318a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
13328a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_WEP104):
13338a1b9b6aSSam Leffler 		if (keylen)
13348a1b9b6aSSam Leffler 			*keylen = 104 / NBBY;
13358a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
13368a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_TKIP):
13378a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_TKIP;
13388a1b9b6aSSam Leffler 	case WPA_SEL(WPA_CSE_CCMP):
13398a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_AES_CCM;
13408a1b9b6aSSam Leffler 	}
13418a1b9b6aSSam Leffler 	return 32;		/* NB: so 1<< is discarded */
13428a1b9b6aSSam Leffler #undef WPA_SEL
13438a1b9b6aSSam Leffler }
13448a1b9b6aSSam Leffler 
13458a1b9b6aSSam Leffler /*
13468a1b9b6aSSam Leffler  * Convert a WPA key management/authentication algorithm
13478a1b9b6aSSam Leffler  * to an internal code.
13488a1b9b6aSSam Leffler  */
13498a1b9b6aSSam Leffler static int
13508a1b9b6aSSam Leffler wpa_keymgmt(u_int8_t *sel)
13518a1b9b6aSSam Leffler {
13528a1b9b6aSSam Leffler #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
13538a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
13548a1b9b6aSSam Leffler 
13558a1b9b6aSSam Leffler 	switch (w) {
13568a1b9b6aSSam Leffler 	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
13578a1b9b6aSSam Leffler 		return WPA_ASE_8021X_UNSPEC;
13588a1b9b6aSSam Leffler 	case WPA_SEL(WPA_ASE_8021X_PSK):
13598a1b9b6aSSam Leffler 		return WPA_ASE_8021X_PSK;
13608a1b9b6aSSam Leffler 	case WPA_SEL(WPA_ASE_NONE):
13618a1b9b6aSSam Leffler 		return WPA_ASE_NONE;
13628a1b9b6aSSam Leffler 	}
13638a1b9b6aSSam Leffler 	return 0;		/* NB: so is discarded */
13648a1b9b6aSSam Leffler #undef WPA_SEL
13658a1b9b6aSSam Leffler }
13668a1b9b6aSSam Leffler 
13678a1b9b6aSSam Leffler /*
13688a1b9b6aSSam Leffler  * Parse a WPA information element to collect parameters
13698a1b9b6aSSam Leffler  * and validate the parameters against what has been
13708a1b9b6aSSam Leffler  * configured for the system.
13718a1b9b6aSSam Leffler  */
13728a1b9b6aSSam Leffler static int
13738a1b9b6aSSam Leffler ieee80211_parse_wpa(struct ieee80211com *ic, u_int8_t *frm,
13748a1b9b6aSSam Leffler 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
13758a1b9b6aSSam Leffler {
13768a1b9b6aSSam Leffler 	u_int8_t len = frm[1];
13778a1b9b6aSSam Leffler 	u_int32_t w;
13788a1b9b6aSSam Leffler 	int n;
13798a1b9b6aSSam Leffler 
13808a1b9b6aSSam Leffler 	/*
13818a1b9b6aSSam Leffler 	 * Check the length once for fixed parts: OUI, type,
13828a1b9b6aSSam Leffler 	 * version, mcast cipher, and 2 selector counts.
13838a1b9b6aSSam Leffler 	 * Other, variable-length data, must be checked separately.
13848a1b9b6aSSam Leffler 	 */
13858a1b9b6aSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_WPA1,
13868a1b9b6aSSam Leffler 		("not WPA, flags 0x%x", ic->ic_flags));
13878a1b9b6aSSam Leffler 	if (len < 14) {
13888a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
13898a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
13908a1b9b6aSSam Leffler 		    wh, "WPA", "too short, len %u", len);
13918a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
13928a1b9b6aSSam Leffler 	}
13938a1b9b6aSSam Leffler 	frm += 6, len -= 4;		/* NB: len is payload only */
13948a1b9b6aSSam Leffler 	/* NB: iswapoui already validated the OUI and type */
13958a1b9b6aSSam Leffler 	w = LE_READ_2(frm);
13968a1b9b6aSSam Leffler 	if (w != WPA_VERSION) {
13978a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
13988a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
13998a1b9b6aSSam Leffler 		    wh, "WPA", "bad version %u", w);
14008a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14018a1b9b6aSSam Leffler 	}
14028a1b9b6aSSam Leffler 	frm += 2, len -= 2;
14038a1b9b6aSSam Leffler 
14048a1b9b6aSSam Leffler 	/* multicast/group cipher */
14058a1b9b6aSSam Leffler 	w = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
14068a1b9b6aSSam Leffler 	if (w != rsn->rsn_mcastcipher) {
14078a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14088a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14098a1b9b6aSSam Leffler 		    wh, "WPA", "mcast cipher mismatch; got %u, expected %u",
14108a1b9b6aSSam Leffler 		    w, rsn->rsn_mcastcipher);
14118a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14128a1b9b6aSSam Leffler 	}
14138a1b9b6aSSam Leffler 	frm += 4, len -= 4;
14148a1b9b6aSSam Leffler 
14158a1b9b6aSSam Leffler 	/* unicast ciphers */
14168a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
14178a1b9b6aSSam Leffler 	frm += 2, len -= 2;
14188a1b9b6aSSam Leffler 	if (len < n*4+2) {
14198a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14208a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14218a1b9b6aSSam Leffler 		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
14228a1b9b6aSSam Leffler 		    len, n);
14238a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14248a1b9b6aSSam Leffler 	}
14258a1b9b6aSSam Leffler 	w = 0;
14268a1b9b6aSSam Leffler 	for (; n > 0; n--) {
14278a1b9b6aSSam Leffler 		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
14288a1b9b6aSSam Leffler 		frm += 4, len -= 4;
14298a1b9b6aSSam Leffler 	}
14308a1b9b6aSSam Leffler 	w &= rsn->rsn_ucastcipherset;
14318a1b9b6aSSam Leffler 	if (w == 0) {
14328a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14338a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14348a1b9b6aSSam Leffler 		    wh, "WPA", "%s", "ucast cipher set empty");
14358a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14368a1b9b6aSSam Leffler 	}
14378a1b9b6aSSam Leffler 	if (w & (1<<IEEE80211_CIPHER_TKIP))
14388a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
14398a1b9b6aSSam Leffler 	else
14408a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
14418a1b9b6aSSam Leffler 
14428a1b9b6aSSam Leffler 	/* key management algorithms */
14438a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
14448a1b9b6aSSam Leffler 	frm += 2, len -= 2;
14458a1b9b6aSSam Leffler 	if (len < n*4) {
14468a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14478a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14488a1b9b6aSSam Leffler 		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
14498a1b9b6aSSam Leffler 		    len, n);
14508a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14518a1b9b6aSSam Leffler 	}
14528a1b9b6aSSam Leffler 	w = 0;
14538a1b9b6aSSam Leffler 	for (; n > 0; n--) {
14548a1b9b6aSSam Leffler 		w |= wpa_keymgmt(frm);
14558a1b9b6aSSam Leffler 		frm += 4, len -= 4;
14568a1b9b6aSSam Leffler 	}
14578a1b9b6aSSam Leffler 	w &= rsn->rsn_keymgmtset;
14588a1b9b6aSSam Leffler 	if (w == 0) {
14598a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
14608a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
14618a1b9b6aSSam Leffler 		    wh, "WPA", "%s", "no acceptable key mgmt alg");
14628a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
14638a1b9b6aSSam Leffler 	}
14648a1b9b6aSSam Leffler 	if (w & WPA_ASE_8021X_UNSPEC)
14658a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
14668a1b9b6aSSam Leffler 	else
14678a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
14688a1b9b6aSSam Leffler 
14698a1b9b6aSSam Leffler 	if (len > 2)		/* optional capabilities */
14708a1b9b6aSSam Leffler 		rsn->rsn_caps = LE_READ_2(frm);
14718a1b9b6aSSam Leffler 
14728a1b9b6aSSam Leffler 	return 0;
14738a1b9b6aSSam Leffler }
14748a1b9b6aSSam Leffler 
14758a1b9b6aSSam Leffler /*
14768a1b9b6aSSam Leffler  * Convert an RSN cipher selector OUI to an internal
14778a1b9b6aSSam Leffler  * cipher algorithm.  Where appropriate we also
14788a1b9b6aSSam Leffler  * record any key length.
14798a1b9b6aSSam Leffler  */
14808a1b9b6aSSam Leffler static int
14818a1b9b6aSSam Leffler rsn_cipher(u_int8_t *sel, u_int8_t *keylen)
14828a1b9b6aSSam Leffler {
14838a1b9b6aSSam Leffler #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
14848a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
14858a1b9b6aSSam Leffler 
14868a1b9b6aSSam Leffler 	switch (w) {
14878a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_NULL):
14888a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_NONE;
14898a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_WEP40):
14908a1b9b6aSSam Leffler 		if (keylen)
14918a1b9b6aSSam Leffler 			*keylen = 40 / NBBY;
14928a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
14938a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_WEP104):
14948a1b9b6aSSam Leffler 		if (keylen)
14958a1b9b6aSSam Leffler 			*keylen = 104 / NBBY;
14968a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_WEP;
14978a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_TKIP):
14988a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_TKIP;
14998a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_CCMP):
15008a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_AES_CCM;
15018a1b9b6aSSam Leffler 	case RSN_SEL(RSN_CSE_WRAP):
15028a1b9b6aSSam Leffler 		return IEEE80211_CIPHER_AES_OCB;
15038a1b9b6aSSam Leffler 	}
15048a1b9b6aSSam Leffler 	return 32;		/* NB: so 1<< is discarded */
15058a1b9b6aSSam Leffler #undef WPA_SEL
15068a1b9b6aSSam Leffler }
15078a1b9b6aSSam Leffler 
15088a1b9b6aSSam Leffler /*
15098a1b9b6aSSam Leffler  * Convert an RSN key management/authentication algorithm
15108a1b9b6aSSam Leffler  * to an internal code.
15118a1b9b6aSSam Leffler  */
15128a1b9b6aSSam Leffler static int
15138a1b9b6aSSam Leffler rsn_keymgmt(u_int8_t *sel)
15148a1b9b6aSSam Leffler {
15158a1b9b6aSSam Leffler #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
15168a1b9b6aSSam Leffler 	u_int32_t w = LE_READ_4(sel);
15178a1b9b6aSSam Leffler 
15188a1b9b6aSSam Leffler 	switch (w) {
15198a1b9b6aSSam Leffler 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
15208a1b9b6aSSam Leffler 		return RSN_ASE_8021X_UNSPEC;
15218a1b9b6aSSam Leffler 	case RSN_SEL(RSN_ASE_8021X_PSK):
15228a1b9b6aSSam Leffler 		return RSN_ASE_8021X_PSK;
15238a1b9b6aSSam Leffler 	case RSN_SEL(RSN_ASE_NONE):
15248a1b9b6aSSam Leffler 		return RSN_ASE_NONE;
15258a1b9b6aSSam Leffler 	}
15268a1b9b6aSSam Leffler 	return 0;		/* NB: so is discarded */
15278a1b9b6aSSam Leffler #undef RSN_SEL
15288a1b9b6aSSam Leffler }
15298a1b9b6aSSam Leffler 
15308a1b9b6aSSam Leffler /*
15318a1b9b6aSSam Leffler  * Parse a WPA/RSN information element to collect parameters
15328a1b9b6aSSam Leffler  * and validate the parameters against what has been
15338a1b9b6aSSam Leffler  * configured for the system.
15348a1b9b6aSSam Leffler  */
15358a1b9b6aSSam Leffler static int
15368a1b9b6aSSam Leffler ieee80211_parse_rsn(struct ieee80211com *ic, u_int8_t *frm,
15378a1b9b6aSSam Leffler 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
15388a1b9b6aSSam Leffler {
15398a1b9b6aSSam Leffler 	u_int8_t len = frm[1];
15408a1b9b6aSSam Leffler 	u_int32_t w;
15418a1b9b6aSSam Leffler 	int n;
15428a1b9b6aSSam Leffler 
15438a1b9b6aSSam Leffler 	/*
15448a1b9b6aSSam Leffler 	 * Check the length once for fixed parts:
15458a1b9b6aSSam Leffler 	 * version, mcast cipher, and 2 selector counts.
15468a1b9b6aSSam Leffler 	 * Other, variable-length data, must be checked separately.
15478a1b9b6aSSam Leffler 	 */
15488a1b9b6aSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_WPA2,
15498a1b9b6aSSam Leffler 		("not RSN, flags 0x%x", ic->ic_flags));
15508a1b9b6aSSam Leffler 	if (len < 10) {
15518a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15528a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15538a1b9b6aSSam Leffler 		    wh, "RSN", "too short, len %u", len);
15548a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15558a1b9b6aSSam Leffler 	}
15568a1b9b6aSSam Leffler 	frm += 2;
15578a1b9b6aSSam Leffler 	w = LE_READ_2(frm);
15588a1b9b6aSSam Leffler 	if (w != RSN_VERSION) {
15598a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15608a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15618a1b9b6aSSam Leffler 		    wh, "RSN", "bad version %u", w);
15628a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15638a1b9b6aSSam Leffler 	}
15648a1b9b6aSSam Leffler 	frm += 2, len -= 2;
15658a1b9b6aSSam Leffler 
15668a1b9b6aSSam Leffler 	/* multicast/group cipher */
15678a1b9b6aSSam Leffler 	w = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
15688a1b9b6aSSam Leffler 	if (w != rsn->rsn_mcastcipher) {
15698a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15708a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15718a1b9b6aSSam Leffler 		    wh, "RSN", "mcast cipher mismatch; got %u, expected %u",
15728a1b9b6aSSam Leffler 		    w, rsn->rsn_mcastcipher);
15738a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15748a1b9b6aSSam Leffler 	}
15758a1b9b6aSSam Leffler 	frm += 4, len -= 4;
15768a1b9b6aSSam Leffler 
15778a1b9b6aSSam Leffler 	/* unicast ciphers */
15788a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
15798a1b9b6aSSam Leffler 	frm += 2, len -= 2;
15808a1b9b6aSSam Leffler 	if (len < n*4+2) {
15818a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15828a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15838a1b9b6aSSam Leffler 		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
15848a1b9b6aSSam Leffler 		    len, n);
15858a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15868a1b9b6aSSam Leffler 	}
15878a1b9b6aSSam Leffler 	w = 0;
15888a1b9b6aSSam Leffler 	for (; n > 0; n--) {
15898a1b9b6aSSam Leffler 		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
15908a1b9b6aSSam Leffler 		frm += 4, len -= 4;
15918a1b9b6aSSam Leffler 	}
15928a1b9b6aSSam Leffler 	w &= rsn->rsn_ucastcipherset;
15938a1b9b6aSSam Leffler 	if (w == 0) {
15948a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
15958a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
15968a1b9b6aSSam Leffler 		    wh, "RSN", "%s", "ucast cipher set empty");
15978a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
15988a1b9b6aSSam Leffler 	}
15998a1b9b6aSSam Leffler 	if (w & (1<<IEEE80211_CIPHER_TKIP))
16008a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
16018a1b9b6aSSam Leffler 	else
16028a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
16038a1b9b6aSSam Leffler 
16048a1b9b6aSSam Leffler 	/* key management algorithms */
16058a1b9b6aSSam Leffler 	n = LE_READ_2(frm);
16068a1b9b6aSSam Leffler 	frm += 2, len -= 2;
16078a1b9b6aSSam Leffler 	if (len < n*4) {
16088a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
16098a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
16108a1b9b6aSSam Leffler 		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
16118a1b9b6aSSam Leffler 		    len, n);
16128a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
16138a1b9b6aSSam Leffler 	}
16148a1b9b6aSSam Leffler 	w = 0;
16158a1b9b6aSSam Leffler 	for (; n > 0; n--) {
16168a1b9b6aSSam Leffler 		w |= rsn_keymgmt(frm);
16178a1b9b6aSSam Leffler 		frm += 4, len -= 4;
16188a1b9b6aSSam Leffler 	}
16198a1b9b6aSSam Leffler 	w &= rsn->rsn_keymgmtset;
16208a1b9b6aSSam Leffler 	if (w == 0) {
16218a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
16228a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
16238a1b9b6aSSam Leffler 		    wh, "RSN", "%s", "no acceptable key mgmt alg");
16248a1b9b6aSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
16258a1b9b6aSSam Leffler 	}
16268a1b9b6aSSam Leffler 	if (w & RSN_ASE_8021X_UNSPEC)
16278a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
16288a1b9b6aSSam Leffler 	else
16298a1b9b6aSSam Leffler 		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
16308a1b9b6aSSam Leffler 
16318a1b9b6aSSam Leffler 	/* optional RSN capabilities */
16328a1b9b6aSSam Leffler 	if (len > 2)
16338a1b9b6aSSam Leffler 		rsn->rsn_caps = LE_READ_2(frm);
16348a1b9b6aSSam Leffler 	/* XXXPMKID */
16358a1b9b6aSSam Leffler 
16368a1b9b6aSSam Leffler 	return 0;
16378a1b9b6aSSam Leffler }
16388a1b9b6aSSam Leffler 
16398a1b9b6aSSam Leffler static int
16408a1b9b6aSSam Leffler ieee80211_parse_wmeparams(struct ieee80211com *ic, u_int8_t *frm,
16418a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh)
16428a1b9b6aSSam Leffler {
16438a1b9b6aSSam Leffler #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
16448a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
16458a1b9b6aSSam Leffler 	u_int len = frm[1], qosinfo;
16468a1b9b6aSSam Leffler 	int i;
16478a1b9b6aSSam Leffler 
16488a1b9b6aSSam Leffler 	if (len < sizeof(struct ieee80211_wme_param)-2) {
16498a1b9b6aSSam Leffler 		IEEE80211_DISCARD_IE(ic,
16508a1b9b6aSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
16518a1b9b6aSSam Leffler 		    wh, "WME", "too short, len %u", len);
1652c0fa32ceSSam Leffler 		return -1;
16538a1b9b6aSSam Leffler 	}
16548a1b9b6aSSam Leffler 	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
16558a1b9b6aSSam Leffler 	qosinfo &= WME_QOSINFO_COUNT;
16568a1b9b6aSSam Leffler 	/* XXX do proper check for wraparound */
16578a1b9b6aSSam Leffler 	if (qosinfo == wme->wme_wmeChanParams.cap_info)
16588a1b9b6aSSam Leffler 		return 0;
16598a1b9b6aSSam Leffler 	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
16608a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
16618a1b9b6aSSam Leffler 		struct wmeParams *wmep =
16628a1b9b6aSSam Leffler 			&wme->wme_wmeChanParams.cap_wmeParams[i];
16638a1b9b6aSSam Leffler 		/* NB: ACI not used */
16648a1b9b6aSSam Leffler 		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
16658a1b9b6aSSam Leffler 		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
16668a1b9b6aSSam Leffler 		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
16678a1b9b6aSSam Leffler 		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
16688a1b9b6aSSam Leffler 		wmep->wmep_txopLimit = LE_READ_2(frm+2);
16698a1b9b6aSSam Leffler 		frm += 4;
16708a1b9b6aSSam Leffler 	}
16718a1b9b6aSSam Leffler 	wme->wme_wmeChanParams.cap_info = qosinfo;
16728a1b9b6aSSam Leffler 	return 1;
16738a1b9b6aSSam Leffler #undef MS
16748a1b9b6aSSam Leffler }
16758a1b9b6aSSam Leffler 
16768a1b9b6aSSam Leffler static void
16778a1b9b6aSSam Leffler ieee80211_saveie(u_int8_t **iep, const u_int8_t *ie)
16788a1b9b6aSSam Leffler {
16798a1b9b6aSSam Leffler 	u_int ielen = ie[1]+2;
16808a1b9b6aSSam Leffler 	/*
16818a1b9b6aSSam Leffler 	 * Record information element for later use.
16828a1b9b6aSSam Leffler 	 */
16838a1b9b6aSSam Leffler 	if (*iep == NULL || (*iep)[1] != ie[1]) {
16848a1b9b6aSSam Leffler 		if (*iep != NULL)
16858a1b9b6aSSam Leffler 			FREE(*iep, M_DEVBUF);
16868a1b9b6aSSam Leffler 		MALLOC(*iep, void*, ielen, M_DEVBUF, M_NOWAIT);
16878a1b9b6aSSam Leffler 	}
16888a1b9b6aSSam Leffler 	if (*iep != NULL)
16898a1b9b6aSSam Leffler 		memcpy(*iep, ie, ielen);
16908a1b9b6aSSam Leffler 	/* XXX note failure */
16918a1b9b6aSSam Leffler }
16928a1b9b6aSSam Leffler 
16938a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
16948a1b9b6aSSam Leffler static void
16958a1b9b6aSSam Leffler dump_probe_beacon(u_int8_t subtype, int isnew,
16968a1b9b6aSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN],
16978a1b9b6aSSam Leffler 	u_int8_t chan, u_int8_t bchan, u_int16_t capinfo, u_int16_t bintval,
16988a1b9b6aSSam Leffler 	u_int8_t erp, u_int8_t *ssid, u_int8_t *country)
16998a1b9b6aSSam Leffler {
17008a1b9b6aSSam Leffler 	printf("[%s] %s%s on chan %u (bss chan %u) ",
17018a1b9b6aSSam Leffler 	    ether_sprintf(mac), isnew ? "new " : "",
17028a1b9b6aSSam Leffler 	    ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
17038a1b9b6aSSam Leffler 	    chan, bchan);
17048a1b9b6aSSam Leffler 	ieee80211_print_essid(ssid + 2, ssid[1]);
17058a1b9b6aSSam Leffler 	printf("\n");
17068a1b9b6aSSam Leffler 
17078a1b9b6aSSam Leffler 	if (isnew) {
17088a1b9b6aSSam Leffler 		printf("[%s] caps 0x%x bintval %u erp 0x%x",
17098a1b9b6aSSam Leffler 			ether_sprintf(mac), capinfo, bintval, erp);
17108a1b9b6aSSam Leffler 		if (country) {
17118a1b9b6aSSam Leffler #ifdef __FreeBSD__
17128a1b9b6aSSam Leffler 			printf(" country info %*D", country[1], country+2, " ");
17138a1b9b6aSSam Leffler #else
17148a1b9b6aSSam Leffler 			int i;
17158a1b9b6aSSam Leffler 			printf(" country info");
17168a1b9b6aSSam Leffler 			for (i = 0; i < country[1]; i++)
17178a1b9b6aSSam Leffler 				printf(" %02x", country[i+2]);
17188a1b9b6aSSam Leffler #endif
17198a1b9b6aSSam Leffler 		}
17208a1b9b6aSSam Leffler 		printf("\n");
17218a1b9b6aSSam Leffler 	}
17228a1b9b6aSSam Leffler }
17238a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
17248a1b9b6aSSam Leffler 
17251a1e1d21SSam Leffler void
17260a915fadSSam Leffler ieee80211_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
17270a915fadSSam Leffler 	struct ieee80211_node *ni,
17280a915fadSSam Leffler 	int subtype, int rssi, u_int32_t rstamp)
17291a1e1d21SSam Leffler {
17308a1b9b6aSSam Leffler #define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
17318a1b9b6aSSam Leffler #define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
17321a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
17331a1e1d21SSam Leffler 	u_int8_t *frm, *efrm;
17348a1b9b6aSSam Leffler 	u_int8_t *ssid, *rates, *xrates, *wpa, *wme;
17358a1b9b6aSSam Leffler 	int reassoc, resp, allocbs;
173649d4c02fSSam Leffler 	u_int8_t rate;
17371a1e1d21SSam Leffler 
17381a1e1d21SSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
17391a1e1d21SSam Leffler 	frm = (u_int8_t *)&wh[1];
17401a1e1d21SSam Leffler 	efrm = mtod(m0, u_int8_t *) + m0->m_len;
17411a1e1d21SSam Leffler 	switch (subtype) {
17421a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
17431a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON: {
1744a634d6a7SSam Leffler 		u_int8_t *tstamp, *country, *tim;
17451a1e1d21SSam Leffler 		u_int8_t chan, bchan, fhindex, erp;
17468a1b9b6aSSam Leffler 		u_int16_t capinfo, bintval, timoff;
17471a1e1d21SSam Leffler 		u_int16_t fhdwell;
17481a1e1d21SSam Leffler 
17498a1b9b6aSSam Leffler 		/*
17508a1b9b6aSSam Leffler 		 * We process beacon/probe response frames:
17518a1b9b6aSSam Leffler 		 *    o when scanning, or
17528a1b9b6aSSam Leffler 		 *    o station mode when associated (to collect state
17538a1b9b6aSSam Leffler 		 *      updates such as 802.11g slot time), or
17548a1b9b6aSSam Leffler 		 *    o adhoc mode (to discover neighbors)
17558a1b9b6aSSam Leffler 		 * Frames otherwise received are discarded.
17568a1b9b6aSSam Leffler 		 */
17578a1b9b6aSSam Leffler 		if (!((ic->ic_flags & IEEE80211_F_SCAN) ||
17588a1b9b6aSSam Leffler 		      (ic->ic_opmode == IEEE80211_M_STA && ni->ni_associd) ||
17595784a371SSam Leffler 		       ic->ic_opmode == IEEE80211_M_IBSS)) {
17608a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
17611a1e1d21SSam Leffler 			return;
17621a1e1d21SSam Leffler 		}
17631a1e1d21SSam Leffler 		/*
17641a1e1d21SSam Leffler 		 * beacon/probe response frame format
17651a1e1d21SSam Leffler 		 *	[8] time stamp
17661a1e1d21SSam Leffler 		 *	[2] beacon interval
17671a1e1d21SSam Leffler 		 *	[2] capability information
17681a1e1d21SSam Leffler 		 *	[tlv] ssid
17691a1e1d21SSam Leffler 		 *	[tlv] supported rates
17701a1e1d21SSam Leffler 		 *	[tlv] country information
17711a1e1d21SSam Leffler 		 *	[tlv] parameter set (FH/DS)
17721a1e1d21SSam Leffler 		 *	[tlv] erp information
17731a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
17748a1b9b6aSSam Leffler 		 *	[tlv] WME
17758a1b9b6aSSam Leffler 		 *	[tlv] WPA or RSN
17761a1e1d21SSam Leffler 		 */
17771a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 12);
17781a1e1d21SSam Leffler 		tstamp  = frm;				frm += 8;
17798a1b9b6aSSam Leffler 		bintval = le16toh(*(u_int16_t *)frm);	frm += 2;
17808a1b9b6aSSam Leffler 		capinfo = le16toh(*(u_int16_t *)frm);	frm += 2;
1781a634d6a7SSam Leffler 		ssid = rates = xrates = country = wpa = wme = tim = NULL;
17821a1e1d21SSam Leffler 		bchan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
17831a1e1d21SSam Leffler 		chan = bchan;
17841a1e1d21SSam Leffler 		fhdwell = 0;
17851a1e1d21SSam Leffler 		fhindex = 0;
17861a1e1d21SSam Leffler 		erp = 0;
17878a1b9b6aSSam Leffler 		timoff = 0;
17881a1e1d21SSam Leffler 		while (frm < efrm) {
17891a1e1d21SSam Leffler 			switch (*frm) {
17901a1e1d21SSam Leffler 			case IEEE80211_ELEMID_SSID:
17911a1e1d21SSam Leffler 				ssid = frm;
17921a1e1d21SSam Leffler 				break;
17931a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
17941a1e1d21SSam Leffler 				rates = frm;
17951a1e1d21SSam Leffler 				break;
17961a1e1d21SSam Leffler 			case IEEE80211_ELEMID_COUNTRY:
17971a1e1d21SSam Leffler 				country = frm;
17981a1e1d21SSam Leffler 				break;
17991a1e1d21SSam Leffler 			case IEEE80211_ELEMID_FHPARMS:
18001a1e1d21SSam Leffler 				if (ic->ic_phytype == IEEE80211_T_FH) {
18018a1b9b6aSSam Leffler 					fhdwell = LE_READ_2(&frm[2]);
18021a1e1d21SSam Leffler 					chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
18031a1e1d21SSam Leffler 					fhindex = frm[6];
18041a1e1d21SSam Leffler 				}
18051a1e1d21SSam Leffler 				break;
18061a1e1d21SSam Leffler 			case IEEE80211_ELEMID_DSPARMS:
18071a1e1d21SSam Leffler 				/*
18081a1e1d21SSam Leffler 				 * XXX hack this since depending on phytype
18091a1e1d21SSam Leffler 				 * is problematic for multi-mode devices.
18101a1e1d21SSam Leffler 				 */
18111a1e1d21SSam Leffler 				if (ic->ic_phytype != IEEE80211_T_FH)
18121a1e1d21SSam Leffler 					chan = frm[2];
18131a1e1d21SSam Leffler 				break;
18141a1e1d21SSam Leffler 			case IEEE80211_ELEMID_TIM:
18158a1b9b6aSSam Leffler 				/* XXX ATIM? */
1816a634d6a7SSam Leffler 				tim = frm;
18178a1b9b6aSSam Leffler 				timoff = frm - mtod(m0, u_int8_t *);
18181a1e1d21SSam Leffler 				break;
18194bd067c5SSam Leffler 			case IEEE80211_ELEMID_IBSSPARMS:
18204bd067c5SSam Leffler 				break;
18211a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
18221a1e1d21SSam Leffler 				xrates = frm;
18231a1e1d21SSam Leffler 				break;
18241a1e1d21SSam Leffler 			case IEEE80211_ELEMID_ERP:
18251a1e1d21SSam Leffler 				if (frm[1] != 1) {
18268a1b9b6aSSam Leffler 					IEEE80211_DISCARD_IE(ic,
18278a1b9b6aSSam Leffler 					    IEEE80211_MSG_ELEMID, wh, "ERP",
18288a1b9b6aSSam Leffler 					    "bad len %u", frm[1]);
18291be50176SSam Leffler 					ic->ic_stats.is_rx_elem_toobig++;
18301a1e1d21SSam Leffler 					break;
18311a1e1d21SSam Leffler 				}
18321a1e1d21SSam Leffler 				erp = frm[2];
18331a1e1d21SSam Leffler 				break;
18348a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_RSN:
18358a1b9b6aSSam Leffler 				wpa = frm;
18368a1b9b6aSSam Leffler 				break;
18378a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
18388a1b9b6aSSam Leffler 				if (iswpaoui(frm))
18398a1b9b6aSSam Leffler 					wpa = frm;
18408a1b9b6aSSam Leffler 				else if (iswmeparam(frm) || iswmeinfo(frm))
18418a1b9b6aSSam Leffler 					wme = frm;
18428a1b9b6aSSam Leffler 				/* XXX Atheros OUI support */
18438a1b9b6aSSam Leffler 				break;
18441a1e1d21SSam Leffler 			default:
18458a1b9b6aSSam Leffler 				IEEE80211_DISCARD_IE(ic, IEEE80211_MSG_ELEMID,
18468a1b9b6aSSam Leffler 				    wh, "unhandled",
18478a1b9b6aSSam Leffler 				    "id %u, len %u", *frm, frm[1]);
18481be50176SSam Leffler 				ic->ic_stats.is_rx_elem_unknown++;
18491a1e1d21SSam Leffler 				break;
18501a1e1d21SSam Leffler 			}
18511a1e1d21SSam Leffler 			frm += frm[1] + 2;
18521a1e1d21SSam Leffler 		}
1853dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
18541a1e1d21SSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
1855a11c9a5cSSam Leffler 		if (
1856a11c9a5cSSam Leffler #if IEEE80211_CHAN_MAX < 255
1857a11c9a5cSSam Leffler 		    chan > IEEE80211_CHAN_MAX ||
1858a11c9a5cSSam Leffler #endif
1859a11c9a5cSSam Leffler 		    isclr(ic->ic_chan_active, chan)) {
18608a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,
18618a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
18628a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
18638a1b9b6aSSam Leffler 			    "invalid channel %u", chan);
18641be50176SSam Leffler 			ic->ic_stats.is_rx_badchan++;
18651a1e1d21SSam Leffler 			return;
18661a1e1d21SSam Leffler 		}
18674844aa7dSAtsushi Onoe 		if (chan != bchan && ic->ic_phytype != IEEE80211_T_FH) {
18681a1e1d21SSam Leffler 			/*
18691a1e1d21SSam Leffler 			 * Frame was received on a channel different from the
18704844aa7dSAtsushi Onoe 			 * one indicated in the DS params element id;
18711a1e1d21SSam Leffler 			 * silently discard it.
18721a1e1d21SSam Leffler 			 *
18731a1e1d21SSam Leffler 			 * NB: this can happen due to signal leakage.
18744844aa7dSAtsushi Onoe 			 *     But we should take it for FH phy because
18754844aa7dSAtsushi Onoe 			 *     the rssi value should be correct even for
18764844aa7dSAtsushi Onoe 			 *     different hop pattern in FH.
18771a1e1d21SSam Leffler 			 */
18788a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,
18798a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
18808a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
18816b312781SSam Leffler 			    "for off-channel %u", chan);
18821be50176SSam Leffler 			ic->ic_stats.is_rx_chanmismatch++;
18831a1e1d21SSam Leffler 			return;
18841a1e1d21SSam Leffler 		}
18851a1e1d21SSam Leffler 
18861a1e1d21SSam Leffler 		/*
188744c72e42SSam Leffler 		 * Count frame now that we know it's to be processed.
188844c72e42SSam Leffler 		 */
188944c72e42SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
189044c72e42SSam Leffler 			ic->ic_stats.is_rx_beacon++;		/* XXX remove */
189144c72e42SSam Leffler 			IEEE80211_NODE_STAT(ni, rx_beacons);
189244c72e42SSam Leffler 		} else
189344c72e42SSam Leffler 			IEEE80211_NODE_STAT(ni, rx_proberesp);
189444c72e42SSam Leffler 
189544c72e42SSam Leffler 		/*
18968a1b9b6aSSam Leffler 		 * When operating in station mode, check for state updates.
18978a1b9b6aSSam Leffler 		 * Be careful to ignore beacons received while doing a
18988a1b9b6aSSam Leffler 		 * background scan.  We consider only 11g/WMM stuff right now.
18991a1e1d21SSam Leffler 		 */
19008a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA &&
19018a1b9b6aSSam Leffler 		    ni->ni_associd != 0 &&
19028a1b9b6aSSam Leffler 		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
19038a1b9b6aSSam Leffler 		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1904f1e95a23SSam Leffler 			/* record tsf of last beacon */
1905f1e95a23SSam Leffler 			memcpy(ni->ni_tstamp.data, tstamp,
1906f1e95a23SSam Leffler 				sizeof(ni->ni_tstamp));
19078a1b9b6aSSam Leffler 			if (ni->ni_erp != 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),
19118a1b9b6aSSam Leffler 				    ni->ni_erp, 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;
19178a1b9b6aSSam Leffler 				ni->ni_erp = erp;
19188a1b9b6aSSam Leffler 				/* XXX statistic */
19191a1e1d21SSam Leffler 			}
19208a1b9b6aSSam Leffler 			if ((ni->ni_capinfo ^ 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),
19258a1b9b6aSSam Leffler 				     ni->ni_capinfo, 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 ||
19328a1b9b6aSSam Leffler 					(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
19338a1b9b6aSSam Leffler 				ni->ni_capinfo = capinfo;
19348a1b9b6aSSam Leffler 				/* XXX statistic */
19358a1b9b6aSSam Leffler 			}
19368a1b9b6aSSam Leffler 			if (wme != NULL &&
19373fd5a5aaSSam Leffler 			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
1938c0fa32ceSSam Leffler 			    ieee80211_parse_wmeparams(ic, wme, wh) > 0)
19398a1b9b6aSSam Leffler 				ieee80211_wme_updateparams(ic);
1940a634d6a7SSam Leffler 			if (tim != NULL) {
1941a634d6a7SSam Leffler 				struct ieee80211_tim_ie *ie =
1942a634d6a7SSam Leffler 				    (struct ieee80211_tim_ie *) tim;
1943a634d6a7SSam Leffler 
1944a634d6a7SSam Leffler 				ni->ni_dtim_count = ie->tim_count;
1945a634d6a7SSam Leffler 				ni->ni_dtim_period = ie->tim_period;
1946a634d6a7SSam Leffler 			}
19478a1b9b6aSSam Leffler 			/* NB: don't need the rest of this */
194865e278a8SSam Leffler 			if ((ic->ic_flags & IEEE80211_F_SCAN) == 0)
19498a1b9b6aSSam Leffler 				return;
19508a1b9b6aSSam Leffler 		}
19518a1b9b6aSSam Leffler 
1952720bd46eSTai-hwa Liang 		if (ni == ic->ic_bss &&
1953720bd46eSTai-hwa Liang 		    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid)) {
19548a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
19558a1b9b6aSSam Leffler 			if (ieee80211_msg_scan(ic))
19568a1b9b6aSSam Leffler 				dump_probe_beacon(subtype, 1,
19578a1b9b6aSSam Leffler 				    wh->i_addr2, chan, bchan, capinfo,
19588a1b9b6aSSam Leffler 				    bintval, erp, ssid, country);
19591a1e1d21SSam Leffler #endif
19608a1b9b6aSSam Leffler 			/*
19618a1b9b6aSSam Leffler 			 * Create a new entry.  If scanning the entry goes
19628a1b9b6aSSam Leffler 			 * in the scan cache.  Otherwise, be particular when
19638a1b9b6aSSam Leffler 			 * operating in adhoc mode--only take nodes marked
19648a1b9b6aSSam Leffler 			 * as ibss participants so we don't populate our
19658a1b9b6aSSam Leffler 			 * neighbor table with unintersting sta's.
19668a1b9b6aSSam Leffler 			 */
19678a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
19688a1b9b6aSSam Leffler 				if ((capinfo & IEEE80211_CAPINFO_IBSS) == 0)
19698a1b9b6aSSam Leffler 					return;
1970acc4f7f5SSam Leffler 				ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
19718a1b9b6aSSam Leffler 						wh->i_addr2);
19728a1b9b6aSSam Leffler 			} else
19738a1b9b6aSSam Leffler 				ni = ieee80211_dup_bss(&ic->ic_scan, wh->i_addr2);
1974c64bfa0fSSam Leffler 			if (ni == NULL)
19751a1e1d21SSam Leffler 				return;
19761a1e1d21SSam Leffler 			ni->ni_esslen = ssid[1];
19771a1e1d21SSam Leffler 			memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
19781a1e1d21SSam Leffler 			memcpy(ni->ni_essid, ssid + 2, ssid[1]);
19798a1b9b6aSSam Leffler 		} else if (ssid[1] != 0 &&
19808a1b9b6aSSam Leffler 		    (ISPROBE(subtype) || ni->ni_esslen == 0)) {
19811a1e1d21SSam Leffler 			/*
19828a1b9b6aSSam Leffler 			 * Update ESSID at probe response to adopt
19838a1b9b6aSSam Leffler 			 * hidden AP by Lucent/Cisco, which announces
19848a1b9b6aSSam Leffler 			 * null ESSID in beacon.
19851a1e1d21SSam Leffler 			 */
19868a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
19878a1b9b6aSSam Leffler 			if (ieee80211_msg_scan(ic) ||
19888a1b9b6aSSam Leffler 			    ieee80211_msg_debug(ic))
19898a1b9b6aSSam Leffler 				dump_probe_beacon(subtype, 0,
19908a1b9b6aSSam Leffler 				    wh->i_addr2, chan, bchan, capinfo,
19918a1b9b6aSSam Leffler 				    bintval, erp, ssid, country);
19928a1b9b6aSSam Leffler #endif
19931a1e1d21SSam Leffler 			ni->ni_esslen = ssid[1];
19941a1e1d21SSam Leffler 			memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
19951a1e1d21SSam Leffler 			memcpy(ni->ni_essid, ssid + 2, ssid[1]);
19968a1b9b6aSSam Leffler 		}
19978a1b9b6aSSam Leffler 		ni->ni_scangen = ic->ic_scan.nt_scangen;
19981a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
19991a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
20001a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
20018a1b9b6aSSam Leffler 		memcpy(ni->ni_tstamp.data, tstamp, sizeof(ni->ni_tstamp));
20028a1b9b6aSSam Leffler 		ni->ni_intval = bintval;
20038a1b9b6aSSam Leffler 		ni->ni_capinfo = capinfo;
20041a1e1d21SSam Leffler 		ni->ni_chan = &ic->ic_channels[chan];
20051a1e1d21SSam Leffler 		ni->ni_fhdwell = fhdwell;
20061a1e1d21SSam Leffler 		ni->ni_fhindex = fhindex;
20071a1e1d21SSam Leffler 		ni->ni_erp = erp;
2008a634d6a7SSam Leffler 		if (tim != NULL) {
2009a634d6a7SSam Leffler 			struct ieee80211_tim_ie *ie =
2010a634d6a7SSam Leffler 			    (struct ieee80211_tim_ie *) tim;
2011a634d6a7SSam Leffler 
2012a634d6a7SSam Leffler 			ni->ni_dtim_count = ie->tim_count;
2013a634d6a7SSam Leffler 			ni->ni_dtim_period = ie->tim_period;
2014a634d6a7SSam Leffler 		}
20158a1b9b6aSSam Leffler 		/*
20168a1b9b6aSSam Leffler 		 * Record the byte offset from the mac header to
20178a1b9b6aSSam Leffler 		 * the start of the TIM information element for
20188a1b9b6aSSam Leffler 		 * use by hardware and/or to speedup software
20198a1b9b6aSSam Leffler 		 * processing of beacon frames.
20208a1b9b6aSSam Leffler 		 */
20218a1b9b6aSSam Leffler 		ni->ni_timoff = timoff;
20228a1b9b6aSSam Leffler 		/*
20238a1b9b6aSSam Leffler 		 * Record optional information elements that might be
20248a1b9b6aSSam Leffler 		 * used by applications or drivers.
20258a1b9b6aSSam Leffler 		 */
20268a1b9b6aSSam Leffler 		if (wme != NULL)
20278a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wme_ie, wme);
20288a1b9b6aSSam Leffler 		if (wpa != NULL)
20298a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wpa_ie, wpa);
20301a1e1d21SSam Leffler 		/* NB: must be after ni_chan is setup */
20311a1e1d21SSam Leffler 		ieee80211_setup_rates(ic, ni, rates, xrates, IEEE80211_F_DOSORT);
20321a1e1d21SSam Leffler 		break;
20331a1e1d21SSam Leffler 	}
20341a1e1d21SSam Leffler 
203549d4c02fSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
20368a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA ||
20378a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_RUN) {
20388a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
20391a1e1d21SSam Leffler 			return;
20408a1b9b6aSSam Leffler 		}
20418a1b9b6aSSam Leffler 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
20428a1b9b6aSSam Leffler 			/* frame must be directed */
20438a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;	/* XXX stat */
20441a1e1d21SSam Leffler 			return;
20458a1b9b6aSSam Leffler 		}
20461a1e1d21SSam Leffler 
20471a1e1d21SSam Leffler 		/*
20481a1e1d21SSam Leffler 		 * prreq frame format
20491a1e1d21SSam Leffler 		 *	[tlv] ssid
20501a1e1d21SSam Leffler 		 *	[tlv] supported rates
20511a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
20521a1e1d21SSam Leffler 		 */
20531a1e1d21SSam Leffler 		ssid = rates = xrates = NULL;
20541a1e1d21SSam Leffler 		while (frm < efrm) {
20551a1e1d21SSam Leffler 			switch (*frm) {
20561a1e1d21SSam Leffler 			case IEEE80211_ELEMID_SSID:
20571a1e1d21SSam Leffler 				ssid = frm;
20581a1e1d21SSam Leffler 				break;
20591a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
20601a1e1d21SSam Leffler 				rates = frm;
20611a1e1d21SSam Leffler 				break;
20621a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
20631a1e1d21SSam Leffler 				xrates = frm;
20641a1e1d21SSam Leffler 				break;
20651a1e1d21SSam Leffler 			}
20661a1e1d21SSam Leffler 			frm += frm[1] + 2;
20671a1e1d21SSam Leffler 		}
2068dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
20691a1e1d21SSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
20708a1b9b6aSSam Leffler 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid);
20714ef04d32SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
20724ef04d32SSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
20734ef04d32SSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
20744ef04d32SSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
20754ef04d32SSam Leffler 			    "%s", "no ssid with ssid suppression enabled");
20764ef04d32SSam Leffler 			ic->ic_stats.is_rx_ssidmismatch++; /*XXX*/
20774ef04d32SSam Leffler 			return;
20784ef04d32SSam Leffler 		}
20791a1e1d21SSam Leffler 
20800a915fadSSam Leffler 		if (ni == ic->ic_bss) {
20818a1b9b6aSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_IBSS) {
20828a1b9b6aSSam Leffler 				/*
20838a1b9b6aSSam Leffler 				 * XXX Cannot tell if the sender is operating
20848a1b9b6aSSam Leffler 				 * in ibss mode.  But we need a new node to
20858a1b9b6aSSam Leffler 				 * send the response so blindly add them to the
20868a1b9b6aSSam Leffler 				 * neighbor table.
20878a1b9b6aSSam Leffler 				 */
2088acc4f7f5SSam Leffler 				ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
20898a1b9b6aSSam Leffler 					wh->i_addr2);
20908a1b9b6aSSam Leffler 			} else
2091acc4f7f5SSam Leffler 				ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
2092c64bfa0fSSam Leffler 			if (ni == NULL)
20931a1e1d21SSam Leffler 				return;
20941a1e1d21SSam Leffler 			allocbs = 1;
20951a1e1d21SSam Leffler 		} else
20961a1e1d21SSam Leffler 			allocbs = 0;
20978a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
20988a1b9b6aSSam Leffler 		    "[%s] recv probe req\n", ether_sprintf(wh->i_addr2));
20991a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
21001a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
21011a1e1d21SSam Leffler 		rate = ieee80211_setup_rates(ic, ni, rates, xrates,
21021a1e1d21SSam Leffler 			  IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE
21031a1e1d21SSam Leffler 			| IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
21041a1e1d21SSam Leffler 		if (rate & IEEE80211_RATE_BASIC) {
21058a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_XRATE,
21068a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
21078a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
21088a1b9b6aSSam Leffler 			    "%s", "recv'd rate set invalid");
21091a1e1d21SSam Leffler 		} else {
21100a915fadSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni,
21110a915fadSSam Leffler 				IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
21121a1e1d21SSam Leffler 		}
21138a1b9b6aSSam Leffler 		if (allocbs && ic->ic_opmode != IEEE80211_M_IBSS) {
21148a1b9b6aSSam Leffler 			/* reclaim immediately */
21158a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
21168a1b9b6aSSam Leffler 		}
21171a1e1d21SSam Leffler 		break;
21181a1e1d21SSam Leffler 
21191a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH: {
21201a1e1d21SSam Leffler 		u_int16_t algo, seq, status;
21211a1e1d21SSam Leffler 		/*
21221a1e1d21SSam Leffler 		 * auth frame format
21231a1e1d21SSam Leffler 		 *	[2] algorithm
21241a1e1d21SSam Leffler 		 *	[2] sequence
21251a1e1d21SSam Leffler 		 *	[2] status
21261a1e1d21SSam Leffler 		 *	[tlv*] challenge
21271a1e1d21SSam Leffler 		 */
21281a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
21291a1e1d21SSam Leffler 		algo   = le16toh(*(u_int16_t *)frm);
21301a1e1d21SSam Leffler 		seq    = le16toh(*(u_int16_t *)(frm + 2));
21311a1e1d21SSam Leffler 		status = le16toh(*(u_int16_t *)(frm + 4));
21328a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
21338a1b9b6aSSam Leffler 		    "[%s] recv auth frame with algorithm %d seq %d\n",
21348a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr2), algo, seq);
21358a1b9b6aSSam Leffler 		/*
21368a1b9b6aSSam Leffler 		 * Consult the ACL policy module if setup.
21378a1b9b6aSSam Leffler 		 */
21388a1b9b6aSSam Leffler 		if (ic->ic_acl != NULL &&
21398a1b9b6aSSam Leffler 		    !ic->ic_acl->iac_check(ic, wh->i_addr2)) {
21408a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ACL,
21418a1b9b6aSSam Leffler 			    wh, "auth", "%s", "disallowed by ACL");
21428a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_acl++;
2143a3d1edc2SSam Leffler 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2144a3d1edc2SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
2145a3d1edc2SSam Leffler 				    IEEE80211_FC0_SUBTYPE_AUTH,
2146a3d1edc2SSam Leffler 				    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
2147a3d1edc2SSam Leffler 			}
21481a1e1d21SSam Leffler 			return;
21491a1e1d21SSam Leffler 		}
21508a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_COUNTERM) {
21518a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic,
21528a1b9b6aSSam Leffler 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
21538a1b9b6aSSam Leffler 			    wh, "auth", "%s", "TKIP countermeasures enabled");
21548a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_countermeasures++;
21558a1b9b6aSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
21560a915fadSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
21578a1b9b6aSSam Leffler 					IEEE80211_FC0_SUBTYPE_AUTH,
21588a1b9b6aSSam Leffler 					IEEE80211_REASON_MIC_FAILURE);
215959571d2bSSam Leffler 			}
21601a1e1d21SSam Leffler 			return;
21611a1e1d21SSam Leffler 		}
21628a1b9b6aSSam Leffler 		if (algo == IEEE80211_AUTH_ALG_SHARED)
21638a1b9b6aSSam Leffler 			ieee80211_auth_shared(ic, wh, frm + 6, efrm, ni, rssi,
21648a1b9b6aSSam Leffler 			    rstamp, seq, status);
21658a1b9b6aSSam Leffler 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
21668a1b9b6aSSam Leffler 			ieee80211_auth_open(ic, wh, ni, rssi, rstamp, seq,
21678a1b9b6aSSam Leffler 			    status);
21688a1b9b6aSSam Leffler 		else {
21698a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
21708a1b9b6aSSam Leffler 			    wh, "auth", "unsupported alg %d", algo);
21718a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_unsupported++;
21728a1b9b6aSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
21738a1b9b6aSSam Leffler 				/* XXX not right */
21748a1b9b6aSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
21758a1b9b6aSSam Leffler 					IEEE80211_FC0_SUBTYPE_AUTH,
21768a1b9b6aSSam Leffler 					(seq+1) | (IEEE80211_STATUS_ALG<<16));
21778a1b9b6aSSam Leffler 			}
21788a1b9b6aSSam Leffler 			return;
21791a1e1d21SSam Leffler 		}
21801a1e1d21SSam Leffler 		break;
21811a1e1d21SSam Leffler 	}
21821a1e1d21SSam Leffler 
21831a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
21841a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
21851a1e1d21SSam Leffler 		u_int16_t capinfo, bintval;
21868a1b9b6aSSam Leffler 		struct ieee80211_rsnparms rsn;
21878a1b9b6aSSam Leffler 		u_int8_t reason;
21881a1e1d21SSam Leffler 
21891a1e1d21SSam Leffler 		if (ic->ic_opmode != IEEE80211_M_HOSTAP ||
21908a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_RUN) {
21918a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
21921a1e1d21SSam Leffler 			return;
21938a1b9b6aSSam Leffler 		}
21941a1e1d21SSam Leffler 
21951a1e1d21SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
21961a1e1d21SSam Leffler 			reassoc = 1;
21971a1e1d21SSam Leffler 			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
21981a1e1d21SSam Leffler 		} else {
21991a1e1d21SSam Leffler 			reassoc = 0;
22001a1e1d21SSam Leffler 			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
22011a1e1d21SSam Leffler 		}
22021a1e1d21SSam Leffler 		/*
22031a1e1d21SSam Leffler 		 * asreq frame format
22041a1e1d21SSam Leffler 		 *	[2] capability information
22051a1e1d21SSam Leffler 		 *	[2] listen interval
22061a1e1d21SSam Leffler 		 *	[6*] current AP address (reassoc only)
22071a1e1d21SSam Leffler 		 *	[tlv] ssid
22081a1e1d21SSam Leffler 		 *	[tlv] supported rates
22091a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
22108a1b9b6aSSam Leffler 		 *	[tlv] WPA or RSN
22111a1e1d21SSam Leffler 		 */
22121a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4));
22131a1e1d21SSam Leffler 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_bss->ni_bssid)) {
22148a1b9b6aSSam Leffler 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
22158a1b9b6aSSam Leffler 			    wh, ieee80211_mgt_subtype_name[subtype >>
22168a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
22178a1b9b6aSSam Leffler 			    "%s", "wrong bssid");
22181be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_bss++;
22191a1e1d21SSam Leffler 			return;
22201a1e1d21SSam Leffler 		}
22211a1e1d21SSam Leffler 		capinfo = le16toh(*(u_int16_t *)frm);	frm += 2;
22221a1e1d21SSam Leffler 		bintval = le16toh(*(u_int16_t *)frm);	frm += 2;
22231a1e1d21SSam Leffler 		if (reassoc)
22241a1e1d21SSam Leffler 			frm += 6;	/* ignore current AP info */
22258a1b9b6aSSam Leffler 		ssid = rates = xrates = wpa = wme = NULL;
22261a1e1d21SSam Leffler 		while (frm < efrm) {
22271a1e1d21SSam Leffler 			switch (*frm) {
22281a1e1d21SSam Leffler 			case IEEE80211_ELEMID_SSID:
22291a1e1d21SSam Leffler 				ssid = frm;
22301a1e1d21SSam Leffler 				break;
22311a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
22321a1e1d21SSam Leffler 				rates = frm;
22331a1e1d21SSam Leffler 				break;
22341a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
22351a1e1d21SSam Leffler 				xrates = frm;
22361a1e1d21SSam Leffler 				break;
22378a1b9b6aSSam Leffler 			/* XXX verify only one of RSN and WPA ie's? */
22388a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_RSN:
22398a1b9b6aSSam Leffler 				wpa = frm;
22408a1b9b6aSSam Leffler 				break;
22418a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
22428a1b9b6aSSam Leffler 				if (iswpaoui(frm)) {
22438a1b9b6aSSam Leffler 					if (ic->ic_flags & IEEE80211_F_WPA1)
22448a1b9b6aSSam Leffler 						wpa = frm;
22458a1b9b6aSSam Leffler 				} else if (iswmeinfo(frm))
22468a1b9b6aSSam Leffler 					wme = frm;
22478a1b9b6aSSam Leffler 				/* XXX Atheros OUI support */
22488a1b9b6aSSam Leffler 				break;
22491a1e1d21SSam Leffler 			}
22501a1e1d21SSam Leffler 			frm += frm[1] + 2;
22511a1e1d21SSam Leffler 		}
2252dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
22531a1e1d21SSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
22548a1b9b6aSSam Leffler 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid);
22558a1b9b6aSSam Leffler 
22560a915fadSSam Leffler 		if (ni == ic->ic_bss) {
22578a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
22588a1b9b6aSSam Leffler 			    "[%s] deny %s request, sta not authenticated\n",
22598a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
22608a1b9b6aSSam Leffler 			    reassoc ? "reassoc" : "assoc");
226184eb84c4SSam Leffler 			ieee80211_send_error(ic, ni, wh->i_addr2,
22620a915fadSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
22631a1e1d21SSam Leffler 			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
22641be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_notauth++;
22651a1e1d21SSam Leffler 			return;
22661a1e1d21SSam Leffler 		}
2267c9a4bb99SSam Leffler 		/* assert right associstion security credentials */
2268c9a4bb99SSam Leffler 		if (wpa == NULL && (ic->ic_flags & IEEE80211_F_WPA)) {
2269c9a4bb99SSam Leffler 			IEEE80211_DPRINTF(ic,
2270c9a4bb99SSam Leffler 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
2271c9a4bb99SSam Leffler 			    "[%s] no WPA/RSN IE in association request\n",
2272c9a4bb99SSam Leffler 			    ether_sprintf(wh->i_addr2));
2273c9a4bb99SSam Leffler 			IEEE80211_SEND_MGMT(ic, ni,
2274c9a4bb99SSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
2275c9a4bb99SSam Leffler 			    IEEE80211_REASON_RSN_REQUIRED);
2276c9a4bb99SSam Leffler 			ieee80211_node_leave(ic, ni);
2277c9a4bb99SSam Leffler 			/* XXX distinguish WPA/RSN? */
2278c9a4bb99SSam Leffler 			ic->ic_stats.is_rx_assoc_badwpaie++;
2279c9a4bb99SSam Leffler 			return;
2280c9a4bb99SSam Leffler 		}
22818a1b9b6aSSam Leffler 		if (wpa != NULL) {
22828a1b9b6aSSam Leffler 			/*
22838a1b9b6aSSam Leffler 			 * Parse WPA information element.  Note that
22848a1b9b6aSSam Leffler 			 * we initialize the param block from the node
22858a1b9b6aSSam Leffler 			 * state so that information in the IE overrides
22868a1b9b6aSSam Leffler 			 * our defaults.  The resulting parameters are
22878a1b9b6aSSam Leffler 			 * installed below after the association is assured.
22888a1b9b6aSSam Leffler 			 */
22898a1b9b6aSSam Leffler 			rsn = ni->ni_rsn;
22908a1b9b6aSSam Leffler 			if (wpa[0] != IEEE80211_ELEMID_RSN)
22918a1b9b6aSSam Leffler 				reason = ieee80211_parse_wpa(ic, wpa, &rsn, wh);
22928a1b9b6aSSam Leffler 			else
22938a1b9b6aSSam Leffler 				reason = ieee80211_parse_rsn(ic, wpa, &rsn, wh);
22948a1b9b6aSSam Leffler 			if (reason != 0) {
22958a1b9b6aSSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
22968a1b9b6aSSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
22978a1b9b6aSSam Leffler 				ieee80211_node_leave(ic, ni);
22988a1b9b6aSSam Leffler 				/* XXX distinguish WPA/RSN? */
22998a1b9b6aSSam Leffler 				ic->ic_stats.is_rx_assoc_badwpaie++;
23008a1b9b6aSSam Leffler 				return;
23018a1b9b6aSSam Leffler 			}
23028a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic,
23038a1b9b6aSSam Leffler 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
23048a1b9b6aSSam Leffler 			    "[%s] %s ie: mc %u/%u uc %u/%u key %u caps 0x%x\n",
23058a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
23068a1b9b6aSSam Leffler 			    wpa[0] != IEEE80211_ELEMID_RSN ?  "WPA" : "RSN",
23078a1b9b6aSSam Leffler 			    rsn.rsn_mcastcipher, rsn.rsn_mcastkeylen,
23088a1b9b6aSSam Leffler 			    rsn.rsn_ucastcipher, rsn.rsn_ucastkeylen,
23098a1b9b6aSSam Leffler 			    rsn.rsn_keymgmt, rsn.rsn_caps);
23108a1b9b6aSSam Leffler 		}
23118a1b9b6aSSam Leffler 		/* discard challenge after association */
23128a1b9b6aSSam Leffler 		if (ni->ni_challenge != NULL) {
23138a1b9b6aSSam Leffler 			FREE(ni->ni_challenge, M_DEVBUF);
23148a1b9b6aSSam Leffler 			ni->ni_challenge = NULL;
23158a1b9b6aSSam Leffler 		}
23169b4db829SSam Leffler 		/* NB: 802.11 spec says to ignore station's privacy bit */
23179b4db829SSam Leffler 		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
23188a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
23198a1b9b6aSSam Leffler 			    "[%s] deny %s request, capability mismatch 0x%x\n",
23208a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
23218a1b9b6aSSam Leffler 			    reassoc ? "reassoc" : "assoc", capinfo);
23220a915fadSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
23230a915fadSSam Leffler 				IEEE80211_STATUS_CAPINFO);
23248a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
23251be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_capmismatch++;
23261a1e1d21SSam Leffler 			return;
23271a1e1d21SSam Leffler 		}
232849d4c02fSSam Leffler 		rate = ieee80211_setup_rates(ic, ni, rates, xrates,
23291a1e1d21SSam Leffler 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
23301a1e1d21SSam Leffler 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2331c4f040c3SSam Leffler 		/*
2332c4f040c3SSam Leffler 		 * If constrained to 11g-only stations reject an
2333c4f040c3SSam Leffler 		 * 11b-only station.  We cheat a bit here by looking
2334c4f040c3SSam Leffler 		 * at the max negotiated xmit rate and assuming anyone
2335c4f040c3SSam Leffler 		 * with a best rate <24Mb/s is an 11b station.
2336c4f040c3SSam Leffler 		 */
2337c4f040c3SSam Leffler 		if ((rate & IEEE80211_RATE_BASIC) ||
2338c4f040c3SSam Leffler 		    ((ic->ic_flags & IEEE80211_F_PUREG) && rate < 48)) {
23398a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
23408a1b9b6aSSam Leffler 			    "[%s] deny %s request, rate set mismatch\n",
23418a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
23428a1b9b6aSSam Leffler 			    reassoc ? "reassoc" : "assoc");
23430a915fadSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
23440a915fadSSam Leffler 				IEEE80211_STATUS_BASIC_RATE);
23458a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
23461be50176SSam Leffler 			ic->ic_stats.is_rx_assoc_norate++;
23471a1e1d21SSam Leffler 			return;
23481a1e1d21SSam Leffler 		}
23491a1e1d21SSam Leffler 		ni->ni_rssi = rssi;
23501a1e1d21SSam Leffler 		ni->ni_rstamp = rstamp;
23511a1e1d21SSam Leffler 		ni->ni_intval = bintval;
23521a1e1d21SSam Leffler 		ni->ni_capinfo = capinfo;
23531a1e1d21SSam Leffler 		ni->ni_chan = ic->ic_bss->ni_chan;
23541a1e1d21SSam Leffler 		ni->ni_fhdwell = ic->ic_bss->ni_fhdwell;
23551a1e1d21SSam Leffler 		ni->ni_fhindex = ic->ic_bss->ni_fhindex;
23568a1b9b6aSSam Leffler 		if (wpa != NULL) {
23578a1b9b6aSSam Leffler 			/*
23588a1b9b6aSSam Leffler 			 * Record WPA/RSN parameters for station, mark
23598a1b9b6aSSam Leffler 			 * node as using WPA and record information element
23608a1b9b6aSSam Leffler 			 * for applications that require it.
23618a1b9b6aSSam Leffler 			 */
23628a1b9b6aSSam Leffler 			ni->ni_rsn = rsn;
23638a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wpa_ie, wpa);
23648a1b9b6aSSam Leffler 		} else if (ni->ni_wpa_ie != NULL) {
23658a1b9b6aSSam Leffler 			/*
23668a1b9b6aSSam Leffler 			 * Flush any state from a previous association.
23678a1b9b6aSSam Leffler 			 */
23688a1b9b6aSSam Leffler 			FREE(ni->ni_wpa_ie, M_DEVBUF);
23698a1b9b6aSSam Leffler 			ni->ni_wpa_ie = NULL;
23708a1b9b6aSSam Leffler 		}
23718a1b9b6aSSam Leffler 		if (wme != NULL) {
23728a1b9b6aSSam Leffler 			/*
23738a1b9b6aSSam Leffler 			 * Record WME parameters for station, mark node
23748a1b9b6aSSam Leffler 			 * as capable of QoS and record information
23758a1b9b6aSSam Leffler 			 * element for applications that require it.
23768a1b9b6aSSam Leffler 			 */
23778a1b9b6aSSam Leffler 			ieee80211_saveie(&ni->ni_wme_ie, wme);
23788a1b9b6aSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
23798a1b9b6aSSam Leffler 		} else if (ni->ni_wme_ie != NULL) {
23808a1b9b6aSSam Leffler 			/*
23818a1b9b6aSSam Leffler 			 * Flush any state from a previous association.
23828a1b9b6aSSam Leffler 			 */
23838a1b9b6aSSam Leffler 			FREE(ni->ni_wme_ie, M_DEVBUF);
23848a1b9b6aSSam Leffler 			ni->ni_wme_ie = NULL;
23858a1b9b6aSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
23868a1b9b6aSSam Leffler 		}
23878a1b9b6aSSam Leffler 		ieee80211_node_join(ic, ni, resp);
23881a1e1d21SSam Leffler 		break;
23891a1e1d21SSam Leffler 	}
23901a1e1d21SSam Leffler 
23911a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
23921a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
23938a1b9b6aSSam Leffler 		u_int16_t capinfo, associd;
23941a1e1d21SSam Leffler 		u_int16_t status;
23951a1e1d21SSam Leffler 
23961a1e1d21SSam Leffler 		if (ic->ic_opmode != IEEE80211_M_STA ||
23978a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_ASSOC) {
23988a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
23991a1e1d21SSam Leffler 			return;
24008a1b9b6aSSam Leffler 		}
24011a1e1d21SSam Leffler 
24021a1e1d21SSam Leffler 		/*
24031a1e1d21SSam Leffler 		 * asresp frame format
24041a1e1d21SSam Leffler 		 *	[2] capability information
24051a1e1d21SSam Leffler 		 *	[2] status
24061a1e1d21SSam Leffler 		 *	[2] association ID
24071a1e1d21SSam Leffler 		 *	[tlv] supported rates
24081a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
24098a1b9b6aSSam Leffler 		 *	[tlv] WME
24101a1e1d21SSam Leffler 		 */
24111a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
24121a1e1d21SSam Leffler 		ni = ic->ic_bss;
24138a1b9b6aSSam Leffler 		capinfo = le16toh(*(u_int16_t *)frm);
24141a1e1d21SSam Leffler 		frm += 2;
24151a1e1d21SSam Leffler 		status = le16toh(*(u_int16_t *)frm);
24161a1e1d21SSam Leffler 		frm += 2;
24171a1e1d21SSam Leffler 		if (status != 0) {
24188a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
24198a1b9b6aSSam Leffler 			    "[%s] %sassoc failed (reason %d)\n",
24208a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
24218a1b9b6aSSam Leffler 			    ISREASSOC(subtype) ?  "re" : "", status);
24228a1b9b6aSSam Leffler 			if (ni != ic->ic_bss)	/* XXX never true? */
24231a1e1d21SSam Leffler 				ni->ni_fails++;
24248a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_auth_fail++;	/* XXX */
24251a1e1d21SSam Leffler 			return;
24261a1e1d21SSam Leffler 		}
24278a1b9b6aSSam Leffler 		associd = le16toh(*(u_int16_t *)frm);
24281a1e1d21SSam Leffler 		frm += 2;
24291a1e1d21SSam Leffler 
24308a1b9b6aSSam Leffler 		rates = xrates = wpa = wme = NULL;
24311a1e1d21SSam Leffler 		while (frm < efrm) {
24321a1e1d21SSam Leffler 			switch (*frm) {
24331a1e1d21SSam Leffler 			case IEEE80211_ELEMID_RATES:
24341a1e1d21SSam Leffler 				rates = frm;
24351a1e1d21SSam Leffler 				break;
24361a1e1d21SSam Leffler 			case IEEE80211_ELEMID_XRATES:
24371a1e1d21SSam Leffler 				xrates = frm;
24381a1e1d21SSam Leffler 				break;
24398a1b9b6aSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
24408a1b9b6aSSam Leffler 				if (iswmeoui(frm))
24418a1b9b6aSSam Leffler 					wme = frm;
24428a1b9b6aSSam Leffler 				/* XXX Atheros OUI support */
24438a1b9b6aSSam Leffler 				break;
24441a1e1d21SSam Leffler 			}
24451a1e1d21SSam Leffler 			frm += frm[1] + 2;
24461a1e1d21SSam Leffler 		}
24471a1e1d21SSam Leffler 
2448dd0e6ea6SSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
244949d4c02fSSam Leffler 		rate = ieee80211_setup_rates(ic, ni, rates, xrates,
24501a1e1d21SSam Leffler 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
24511a1e1d21SSam Leffler 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
245249d4c02fSSam Leffler 		if (rate & IEEE80211_RATE_BASIC) {
24538a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
24548a1b9b6aSSam Leffler 			    "[%s] %sassoc failed (rate set mismatch)\n",
24558a1b9b6aSSam Leffler 			    ether_sprintf(wh->i_addr2),
24568a1b9b6aSSam Leffler 			    ISREASSOC(subtype) ?  "re" : "");
24578a1b9b6aSSam Leffler 			if (ni != ic->ic_bss)	/* XXX never true? */
24588a1b9b6aSSam Leffler 				ni->ni_fails++;
24598a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_assoc_norate++;
2460181181acSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
24618a1b9b6aSSam Leffler 			return;
24628a1b9b6aSSam Leffler 		}
24638a1b9b6aSSam Leffler 
24648a1b9b6aSSam Leffler 		ni->ni_capinfo = capinfo;
24658a1b9b6aSSam Leffler 		ni->ni_associd = associd;
2466c0fa32ceSSam Leffler 		if (wme != NULL &&
2467c0fa32ceSSam Leffler 		    ieee80211_parse_wmeparams(ic, wme, wh) >= 0) {
24688a1b9b6aSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
24698a1b9b6aSSam Leffler 			ieee80211_wme_updateparams(ic);
24708a1b9b6aSSam Leffler 		} else
24718a1b9b6aSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
24728a1b9b6aSSam Leffler 		/*
24738a1b9b6aSSam Leffler 		 * Configure state now that we are associated.
24748a1b9b6aSSam Leffler 		 *
24758a1b9b6aSSam Leffler 		 * XXX may need different/additional driver callbacks?
24768a1b9b6aSSam Leffler 		 */
24778a1b9b6aSSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11A ||
24788a1b9b6aSSam Leffler 		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
24798a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
24808a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
24818a1b9b6aSSam Leffler 		} else {
24828a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
24838a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
24848a1b9b6aSSam Leffler 		}
24858a1b9b6aSSam Leffler 		ieee80211_set_shortslottime(ic,
24868a1b9b6aSSam Leffler 			ic->ic_curmode == IEEE80211_MODE_11A ||
24878a1b9b6aSSam Leffler 			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
24888a1b9b6aSSam Leffler 		/*
24898a1b9b6aSSam Leffler 		 * Honor ERP protection.
24908a1b9b6aSSam Leffler 		 *
24918a1b9b6aSSam Leffler 		 * NB: ni_erp should zero for non-11g operation.
24928a1b9b6aSSam Leffler 		 * XXX check ic_curmode anyway?
24938a1b9b6aSSam Leffler 		 */
2494ec425115SSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G &&
2495ec425115SSam Leffler 		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
24968a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
24978a1b9b6aSSam Leffler 		else
24988a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
24998a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
25008a1b9b6aSSam Leffler 		    "[%s] %sassoc success: %s preamble, %s slot time%s%s\n",
25018a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr2),
25028a1b9b6aSSam Leffler 		    ISREASSOC(subtype) ? "re" : "",
25038a1b9b6aSSam Leffler 		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
25048a1b9b6aSSam Leffler 		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
25058a1b9b6aSSam Leffler 		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
25068a1b9b6aSSam Leffler 		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
25078a1b9b6aSSam Leffler 		);
25088a1b9b6aSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, subtype);
25091a1e1d21SSam Leffler 		break;
25101a1e1d21SSam Leffler 	}
25111a1e1d21SSam Leffler 
25121a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
25131a1e1d21SSam Leffler 		u_int16_t reason;
25148a1b9b6aSSam Leffler 
25158a1b9b6aSSam Leffler 		if (ic->ic_state == IEEE80211_S_SCAN) {
25168a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
25178a1b9b6aSSam Leffler 			return;
25188a1b9b6aSSam Leffler 		}
25191a1e1d21SSam Leffler 		/*
25201a1e1d21SSam Leffler 		 * deauth frame format
25211a1e1d21SSam Leffler 		 *	[2] reason
25221a1e1d21SSam Leffler 		 */
25231a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
25241a1e1d21SSam Leffler 		reason = le16toh(*(u_int16_t *)frm);
25251be50176SSam Leffler 		ic->ic_stats.is_rx_deauth++;
25268a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_deauth);
25274720ec19SSam Leffler 
25284720ec19SSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
25294720ec19SSam Leffler 		    "[%s] recv deauthenticate (reason %d)\n",
25304720ec19SSam Leffler 		    ether_sprintf(ni->ni_macaddr), reason);
25311a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
25321a1e1d21SSam Leffler 		case IEEE80211_M_STA:
2533a11c9a5cSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
25341a1e1d21SSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
25351a1e1d21SSam Leffler 			break;
25361a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
25374720ec19SSam Leffler 			if (ni != ic->ic_bss)
25388a1b9b6aSSam Leffler 				ieee80211_node_leave(ic, ni);
25391a1e1d21SSam Leffler 			break;
25401a1e1d21SSam Leffler 		default:
25418a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
25421a1e1d21SSam Leffler 			break;
25431a1e1d21SSam Leffler 		}
25441a1e1d21SSam Leffler 		break;
25451a1e1d21SSam Leffler 	}
25461a1e1d21SSam Leffler 
25471a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
25481a1e1d21SSam Leffler 		u_int16_t reason;
25498a1b9b6aSSam Leffler 
25508a1b9b6aSSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN &&
2551404265d4SSam Leffler 		    ic->ic_state != IEEE80211_S_ASSOC &&
25528a1b9b6aSSam Leffler 		    ic->ic_state != IEEE80211_S_AUTH) {
25538a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
25548a1b9b6aSSam Leffler 			return;
25558a1b9b6aSSam Leffler 		}
25561a1e1d21SSam Leffler 		/*
25571a1e1d21SSam Leffler 		 * disassoc frame format
25581a1e1d21SSam Leffler 		 *	[2] reason
25591a1e1d21SSam Leffler 		 */
25601a1e1d21SSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
25611a1e1d21SSam Leffler 		reason = le16toh(*(u_int16_t *)frm);
25621be50176SSam Leffler 		ic->ic_stats.is_rx_disassoc++;
25638a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_disassoc);
25644720ec19SSam Leffler 
25654720ec19SSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
25664720ec19SSam Leffler 		    "[%s] recv disassociated (reason %d)\n",
25674720ec19SSam Leffler 		    ether_sprintf(ni->ni_macaddr), reason);
25681a1e1d21SSam Leffler 		switch (ic->ic_opmode) {
25691a1e1d21SSam Leffler 		case IEEE80211_M_STA:
2570a11c9a5cSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
25711a1e1d21SSam Leffler 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
25721a1e1d21SSam Leffler 			break;
25731a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
25744720ec19SSam Leffler 			if (ni != ic->ic_bss)
25758a1b9b6aSSam Leffler 				ieee80211_node_leave(ic, ni);
25761a1e1d21SSam Leffler 			break;
25771a1e1d21SSam Leffler 		default:
25788a1b9b6aSSam Leffler 			ic->ic_stats.is_rx_mgtdiscard++;
25791a1e1d21SSam Leffler 			break;
25801a1e1d21SSam Leffler 		}
25811a1e1d21SSam Leffler 		break;
25821a1e1d21SSam Leffler 	}
25831a1e1d21SSam Leffler 	default:
25848a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
25858a1b9b6aSSam Leffler 		     wh, "mgt", "subtype 0x%x not handled", subtype);
25861be50176SSam Leffler 		ic->ic_stats.is_rx_badsubtype++;
25871a1e1d21SSam Leffler 		break;
25881a1e1d21SSam Leffler 	}
25898a1b9b6aSSam Leffler #undef ISREASSOC
25908a1b9b6aSSam Leffler #undef ISPROBE
25911a1e1d21SSam Leffler }
25921a1e1d21SSam Leffler #undef IEEE80211_VERIFY_LENGTH
25931a1e1d21SSam Leffler #undef IEEE80211_VERIFY_ELEMENT
25948a1b9b6aSSam Leffler 
25958a1b9b6aSSam Leffler /*
25968a1b9b6aSSam Leffler  * Handle station power-save state change.
25978a1b9b6aSSam Leffler  */
25988a1b9b6aSSam Leffler static void
25998a1b9b6aSSam Leffler ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
26008a1b9b6aSSam Leffler {
26018a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
26028a1b9b6aSSam Leffler 	struct mbuf *m;
26038a1b9b6aSSam Leffler 
26048a1b9b6aSSam Leffler 	if (enable) {
26058a1b9b6aSSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0)
26068a1b9b6aSSam Leffler 			ic->ic_ps_sta++;
26078a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
26088a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
26098a1b9b6aSSam Leffler 		    "[%s] power save mode on, %u sta's in ps mode\n",
26108a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
26118a1b9b6aSSam Leffler 		return;
26128a1b9b6aSSam Leffler 	}
26138a1b9b6aSSam Leffler 
26148a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT)
26158a1b9b6aSSam Leffler 		ic->ic_ps_sta--;
26168a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
26178a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
26188a1b9b6aSSam Leffler 	    "[%s] power save mode off, %u sta's in ps mode\n",
26198a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
26208a1b9b6aSSam Leffler 	/* XXX if no stations in ps mode, flush mc frames */
26218a1b9b6aSSam Leffler 
26228a1b9b6aSSam Leffler 	/*
26238a1b9b6aSSam Leffler 	 * Flush queued unicast frames.
26248a1b9b6aSSam Leffler 	 */
26258a1b9b6aSSam Leffler 	if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) {
2626c789ea8bSSam Leffler 		if (ic->ic_set_tim != NULL)
26278a1b9b6aSSam Leffler 			ic->ic_set_tim(ic, ni, 0);	/* just in case */
26288a1b9b6aSSam Leffler 		return;
26298a1b9b6aSSam Leffler 	}
26308a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
26318a1b9b6aSSam Leffler 	    "[%s] flush ps queue, %u packets queued\n",
26328a1b9b6aSSam Leffler 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_SAVEQ_QLEN(ni));
26338a1b9b6aSSam Leffler 	for (;;) {
26348a1b9b6aSSam Leffler 		int qlen;
26358a1b9b6aSSam Leffler 
26368a1b9b6aSSam Leffler 		IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen);
26378a1b9b6aSSam Leffler 		if (m == NULL)
26388a1b9b6aSSam Leffler 			break;
26398a1b9b6aSSam Leffler 		/*
26408a1b9b6aSSam Leffler 		 * If this is the last packet, turn off the TIM bit.
26418a1b9b6aSSam Leffler 		 * If there are more packets, set the more packets bit
2642bc5627d9SSam Leffler 		 * in the mbuf so ieee80211_encap will mark the 802.11
2643bc5627d9SSam Leffler 		 * head to indicate more data frames will follow.
26448a1b9b6aSSam Leffler 		 */
2645bc5627d9SSam Leffler 		if (qlen != 0)
2646bc5627d9SSam Leffler 			m->m_flags |= M_MORE_DATA;
26478a1b9b6aSSam Leffler 		/* XXX need different driver interface */
26488a1b9b6aSSam Leffler 		/* XXX bypasses q max */
26498a1b9b6aSSam Leffler 		IF_ENQUEUE(&ic->ic_ifp->if_snd, m);
26508a1b9b6aSSam Leffler 	}
2651c789ea8bSSam Leffler 	if (ic->ic_set_tim != NULL)
2652c789ea8bSSam Leffler 		ic->ic_set_tim(ic, ni, 0);
26538a1b9b6aSSam Leffler }
26548a1b9b6aSSam Leffler 
26558a1b9b6aSSam Leffler /*
26568a1b9b6aSSam Leffler  * Process a received ps-poll frame.
26578a1b9b6aSSam Leffler  */
26588a1b9b6aSSam Leffler static void
26598a1b9b6aSSam Leffler ieee80211_recv_pspoll(struct ieee80211com *ic,
26608a1b9b6aSSam Leffler 	struct ieee80211_node *ni, struct mbuf *m0)
26618a1b9b6aSSam Leffler {
26628a1b9b6aSSam Leffler 	struct ieee80211_frame_min *wh;
26638a1b9b6aSSam Leffler 	struct mbuf *m;
26648a1b9b6aSSam Leffler 	u_int16_t aid;
26658a1b9b6aSSam Leffler 	int qlen;
26668a1b9b6aSSam Leffler 
26678a1b9b6aSSam Leffler 	wh = mtod(m0, struct ieee80211_frame_min *);
26688a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
26698a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
26708a1b9b6aSSam Leffler 		    (struct ieee80211_frame *) wh, "ps-poll",
26718a1b9b6aSSam Leffler 		    "%s", "unassociated station");
26728a1b9b6aSSam Leffler 		ic->ic_stats.is_ps_unassoc++;
26738a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
26748a1b9b6aSSam Leffler 			IEEE80211_REASON_NOT_ASSOCED);
26758a1b9b6aSSam Leffler 		return;
26768a1b9b6aSSam Leffler 	}
26778a1b9b6aSSam Leffler 
26788a1b9b6aSSam Leffler 	aid = le16toh(*(u_int16_t *)wh->i_dur);
26798a1b9b6aSSam Leffler 	if (aid != ni->ni_associd) {
26808a1b9b6aSSam Leffler 		IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
26818a1b9b6aSSam Leffler 		    (struct ieee80211_frame *) wh, "ps-poll",
26828a1b9b6aSSam Leffler 		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
26838a1b9b6aSSam Leffler 		    ni->ni_associd, aid);
26848a1b9b6aSSam Leffler 		ic->ic_stats.is_ps_badaid++;
26858a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
26868a1b9b6aSSam Leffler 			IEEE80211_REASON_NOT_ASSOCED);
26878a1b9b6aSSam Leffler 		return;
26888a1b9b6aSSam Leffler 	}
26898a1b9b6aSSam Leffler 
26908a1b9b6aSSam Leffler 	/* Okay, take the first queued packet and put it out... */
26918a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen);
26928a1b9b6aSSam Leffler 	if (m == NULL) {
26938a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
26948a1b9b6aSSam Leffler 		    "[%s] recv ps-poll, but queue empty\n",
26958a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr2));
26968a1b9b6aSSam Leffler 		ieee80211_send_nulldata(ic, ni);
26978a1b9b6aSSam Leffler 		ic->ic_stats.is_ps_qempty++;	/* XXX node stat */
2698c789ea8bSSam Leffler 		if (ic->ic_set_tim != NULL)
26998a1b9b6aSSam Leffler 			ic->ic_set_tim(ic, ni, 0);	/* just in case */
27008a1b9b6aSSam Leffler 		return;
27018a1b9b6aSSam Leffler 	}
27028a1b9b6aSSam Leffler 	/*
27038a1b9b6aSSam Leffler 	 * If there are more packets, set the more packets bit
27048a1b9b6aSSam Leffler 	 * in the packet dispatched to the station; otherwise
27058a1b9b6aSSam Leffler 	 * turn off the TIM bit.
27068a1b9b6aSSam Leffler 	 */
27078a1b9b6aSSam Leffler 	if (qlen != 0) {
27088a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
27098a1b9b6aSSam Leffler 		    "[%s] recv ps-poll, send packet, %u still queued\n",
27108a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), qlen);
2711c52dab62SSam Leffler 		m->m_flags |= M_MORE_DATA;
27128a1b9b6aSSam Leffler 	} else {
27138a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
27148a1b9b6aSSam Leffler 		    "[%s] recv ps-poll, send packet, queue empty\n",
27158a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr));
2716c789ea8bSSam Leffler 		if (ic->ic_set_tim != NULL)
27178a1b9b6aSSam Leffler 			ic->ic_set_tim(ic, ni, 0);
27188a1b9b6aSSam Leffler 	}
27198a1b9b6aSSam Leffler 	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
27208a1b9b6aSSam Leffler 	IF_ENQUEUE(&ic->ic_ifp->if_snd, m);
27218a1b9b6aSSam Leffler }
27228a1b9b6aSSam Leffler 
27238a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
27248a1b9b6aSSam Leffler /*
27258a1b9b6aSSam Leffler  * Debugging support.
27268a1b9b6aSSam Leffler  */
27278a1b9b6aSSam Leffler 
27288a1b9b6aSSam Leffler /*
27298a1b9b6aSSam Leffler  * Return the bssid of a frame.
27308a1b9b6aSSam Leffler  */
27318a1b9b6aSSam Leffler static const u_int8_t *
27328a1b9b6aSSam Leffler ieee80211_getbssid(struct ieee80211com *ic, const struct ieee80211_frame *wh)
27338a1b9b6aSSam Leffler {
27348a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA)
27358a1b9b6aSSam Leffler 		return wh->i_addr2;
27368a1b9b6aSSam Leffler 	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) != IEEE80211_FC1_DIR_NODS)
27378a1b9b6aSSam Leffler 		return wh->i_addr1;
27388a1b9b6aSSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
27398a1b9b6aSSam Leffler 		return wh->i_addr1;
27408a1b9b6aSSam Leffler 	return wh->i_addr3;
27418a1b9b6aSSam Leffler }
27428a1b9b6aSSam Leffler 
2743f6df3191SSam Leffler void
2744f6df3191SSam Leffler ieee80211_note(struct ieee80211com *ic, 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 
2753f6df3191SSam Leffler 	if_printf(ic->ic_ifp, "%s", buf);	/* NB: no \n */
2754f6df3191SSam Leffler }
2755f6df3191SSam Leffler 
2756f6df3191SSam Leffler void
2757f6df3191SSam Leffler ieee80211_note_frame(struct ieee80211com *ic,
2758f6df3191SSam Leffler 	const struct ieee80211_frame *wh,
2759f6df3191SSam Leffler 	const char *fmt, ...)
2760f6df3191SSam Leffler {
2761f6df3191SSam Leffler 	char buf[128];		/* XXX */
2762f6df3191SSam Leffler 	va_list ap;
2763f6df3191SSam Leffler 
2764f6df3191SSam Leffler 	va_start(ap, fmt);
2765f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
2766f6df3191SSam Leffler 	va_end(ap);
2767f6df3191SSam Leffler 	if_printf(ic->ic_ifp, "[%s] %s\n",
2768f6df3191SSam Leffler 		ether_sprintf(ieee80211_getbssid(ic, wh)), buf);
2769f6df3191SSam Leffler }
2770f6df3191SSam Leffler 
2771f6df3191SSam Leffler void
2772f6df3191SSam Leffler ieee80211_note_mac(struct ieee80211com *ic,
2773f6df3191SSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN],
2774f6df3191SSam Leffler 	const char *fmt, ...)
2775f6df3191SSam Leffler {
2776f6df3191SSam Leffler 	char buf[128];		/* XXX */
2777f6df3191SSam Leffler 	va_list ap;
2778f6df3191SSam Leffler 
2779f6df3191SSam Leffler 	va_start(ap, fmt);
2780f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
2781f6df3191SSam Leffler 	va_end(ap);
2782f6df3191SSam Leffler 	if_printf(ic->ic_ifp, "[%s] %s\n", ether_sprintf(mac), buf);
2783f6df3191SSam Leffler }
2784f6df3191SSam Leffler 
27858a1b9b6aSSam Leffler static void
27868a1b9b6aSSam Leffler ieee80211_discard_frame(struct ieee80211com *ic,
27878a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
27888a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
27898a1b9b6aSSam Leffler {
27908a1b9b6aSSam Leffler 	va_list ap;
27918a1b9b6aSSam Leffler 
2792497c84aeSSam Leffler 	printf("[%s:%s] discard ", ic->ic_ifp->if_xname,
2793497c84aeSSam Leffler 		ether_sprintf(ieee80211_getbssid(ic, wh)));
27948a1b9b6aSSam Leffler 	if (type != NULL)
27958a1b9b6aSSam Leffler 		printf("%s frame, ", type);
27968a1b9b6aSSam Leffler 	else
27978a1b9b6aSSam Leffler 		printf("frame, ");
27988a1b9b6aSSam Leffler 	va_start(ap, fmt);
27998a1b9b6aSSam Leffler 	vprintf(fmt, ap);
28008a1b9b6aSSam Leffler 	va_end(ap);
28018a1b9b6aSSam Leffler 	printf("\n");
28028a1b9b6aSSam Leffler }
28038a1b9b6aSSam Leffler 
28048a1b9b6aSSam Leffler static void
28058a1b9b6aSSam Leffler ieee80211_discard_ie(struct ieee80211com *ic,
28068a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
28078a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
28088a1b9b6aSSam Leffler {
28098a1b9b6aSSam Leffler 	va_list ap;
28108a1b9b6aSSam Leffler 
2811497c84aeSSam Leffler 	printf("[%s:%s] discard ", ic->ic_ifp->if_xname,
2812497c84aeSSam Leffler 		ether_sprintf(ieee80211_getbssid(ic, wh)));
28138a1b9b6aSSam Leffler 	if (type != NULL)
28148a1b9b6aSSam Leffler 		printf("%s information element, ", type);
28158a1b9b6aSSam Leffler 	else
28168a1b9b6aSSam Leffler 		printf("information element, ");
28178a1b9b6aSSam Leffler 	va_start(ap, fmt);
28188a1b9b6aSSam Leffler 	vprintf(fmt, ap);
28198a1b9b6aSSam Leffler 	va_end(ap);
28208a1b9b6aSSam Leffler 	printf("\n");
28218a1b9b6aSSam Leffler }
28228a1b9b6aSSam Leffler 
28238a1b9b6aSSam Leffler static void
28248a1b9b6aSSam Leffler ieee80211_discard_mac(struct ieee80211com *ic,
28258a1b9b6aSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN],
28268a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
28278a1b9b6aSSam Leffler {
28288a1b9b6aSSam Leffler 	va_list ap;
28298a1b9b6aSSam Leffler 
2830aa8c14c4SSam Leffler 	printf("[%s:%s] discard ", ic->ic_ifp->if_xname, ether_sprintf(mac));
28318a1b9b6aSSam Leffler 	if (type != NULL)
28328a1b9b6aSSam Leffler 		printf("%s frame, ", type);
28338a1b9b6aSSam Leffler 	else
28348a1b9b6aSSam Leffler 		printf("frame, ");
28358a1b9b6aSSam Leffler 	va_start(ap, fmt);
28368a1b9b6aSSam Leffler 	vprintf(fmt, ap);
28378a1b9b6aSSam Leffler 	va_end(ap);
28388a1b9b6aSSam Leffler 	printf("\n");
28398a1b9b6aSSam Leffler }
28408a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
2841