xref: /freebsd/sys/net80211/ieee80211_input.c (revision b105165323e798c7238818d0413dbce4e67e6ab0)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
310ad9a77SSam Leffler  * Copyright (c) 2002-2009 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.
141a1e1d21SSam Leffler  *
157535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251a1e1d21SSam Leffler  */
261a1e1d21SSam Leffler 
271a1e1d21SSam Leffler #include <sys/cdefs.h>
281a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
291a1e1d21SSam Leffler 
30b032f27cSSam Leffler #include "opt_wlan.h"
31b032f27cSSam Leffler 
321a1e1d21SSam Leffler #include <sys/param.h>
331a1e1d21SSam Leffler #include <sys/systm.h>
341a1e1d21SSam Leffler #include <sys/mbuf.h>
351a1e1d21SSam Leffler #include <sys/malloc.h>
361a1e1d21SSam Leffler #include <sys/endian.h>
37a0cc3f85SSam Leffler #include <sys/kernel.h>
381a1e1d21SSam Leffler 
398a1b9b6aSSam Leffler #include <sys/socket.h>
401a1e1d21SSam Leffler 
411a1e1d21SSam Leffler #include <net/ethernet.h>
42b032f27cSSam Leffler #include <net/if.h>
431a1e1d21SSam Leffler #include <net/if_llc.h>
44b032f27cSSam Leffler #include <net/if_media.h>
458a1b9b6aSSam Leffler #include <net/if_vlan_var.h>
461a1e1d21SSam Leffler 
471a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
48b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
4959aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
5059aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
5159aa14a9SRui Paulo #endif
521a1e1d21SSam Leffler 
531a1e1d21SSam Leffler #include <net/bpf.h>
541a1e1d21SSam Leffler 
55b032f27cSSam Leffler #ifdef INET
56b032f27cSSam Leffler #include <netinet/in.h>
57b032f27cSSam Leffler #include <net/ethernet.h>
588a1b9b6aSSam Leffler #endif
591a1e1d21SSam Leffler 
60864ab114SAdrian Chadd static void
61864ab114SAdrian Chadd ieee80211_process_mimo(struct ieee80211_node *ni, struct ieee80211_rx_stats *rx)
62864ab114SAdrian Chadd {
63864ab114SAdrian Chadd 	int i;
64864ab114SAdrian Chadd 
65864ab114SAdrian Chadd 	/* Verify the required MIMO bits are set */
66864ab114SAdrian Chadd 	if ((rx->r_flags & (IEEE80211_R_C_CHAIN | IEEE80211_R_C_NF | IEEE80211_R_C_RSSI)) !=
67864ab114SAdrian Chadd 	    (IEEE80211_R_C_CHAIN | IEEE80211_R_C_NF | IEEE80211_R_C_RSSI))
68864ab114SAdrian Chadd 		return;
69864ab114SAdrian Chadd 
70864ab114SAdrian Chadd 	/* XXX This assumes the MIMO radios have both ctl and ext chains */
71864ab114SAdrian Chadd 	for (i = 0; i < MIN(rx->c_chain, IEEE80211_MAX_CHAINS); i++) {
72864ab114SAdrian Chadd 		IEEE80211_RSSI_LPF(ni->ni_mimo_rssi_ctl[i], rx->c_rssi_ctl[i]);
73864ab114SAdrian Chadd 		IEEE80211_RSSI_LPF(ni->ni_mimo_rssi_ext[i], rx->c_rssi_ext[i]);
74864ab114SAdrian Chadd 	}
75864ab114SAdrian Chadd 
76864ab114SAdrian Chadd 	/* XXX This also assumes the MIMO radios have both ctl and ext chains */
77864ab114SAdrian Chadd 	for(i = 0; i < MIN(rx->c_chain, IEEE80211_MAX_CHAINS); i++) {
78864ab114SAdrian Chadd 		ni->ni_mimo_noise_ctl[i] = rx->c_nf_ctl[i];
79864ab114SAdrian Chadd 		ni->ni_mimo_noise_ext[i] = rx->c_nf_ext[i];
80864ab114SAdrian Chadd 	}
81864ab114SAdrian Chadd 	ni->ni_mimo_chains = rx->c_chain;
82864ab114SAdrian Chadd }
83864ab114SAdrian Chadd 
84864ab114SAdrian Chadd int
85864ab114SAdrian Chadd ieee80211_input_mimo(struct ieee80211_node *ni, struct mbuf *m,
86864ab114SAdrian Chadd     struct ieee80211_rx_stats *rx)
87864ab114SAdrian Chadd {
88864ab114SAdrian Chadd 	/* XXX should assert IEEE80211_R_NF and IEEE80211_R_RSSI are set */
89864ab114SAdrian Chadd 	ieee80211_process_mimo(ni, rx);
90864ab114SAdrian Chadd 	return ieee80211_input(ni, m, rx->rssi, rx->nf);
91864ab114SAdrian Chadd }
92864ab114SAdrian Chadd 
93b032f27cSSam Leffler int
945463c4a4SSam Leffler ieee80211_input_all(struct ieee80211com *ic, struct mbuf *m, int rssi, int nf)
95b032f27cSSam Leffler {
96864ab114SAdrian Chadd 	struct ieee80211_rx_stats rx;
97864ab114SAdrian Chadd 
98864ab114SAdrian Chadd 	rx.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
99864ab114SAdrian Chadd 	rx.nf = nf;
100864ab114SAdrian Chadd 	rx.rssi = rssi;
101864ab114SAdrian Chadd 	return ieee80211_input_mimo_all(ic, m, &rx);
102864ab114SAdrian Chadd }
103864ab114SAdrian Chadd 
104864ab114SAdrian Chadd int
105864ab114SAdrian Chadd ieee80211_input_mimo_all(struct ieee80211com *ic, struct mbuf *m,
106864ab114SAdrian Chadd     struct ieee80211_rx_stats *rx)
107864ab114SAdrian Chadd {
108b032f27cSSam Leffler 	struct ieee80211vap *vap;
109b032f27cSSam Leffler 	int type = -1;
110b032f27cSSam Leffler 
111e1cfcbcbSSam Leffler 	m->m_flags |= M_BCAST;		/* NB: mark for bpf tap'ing */
112e1cfcbcbSSam Leffler 
113b032f27cSSam Leffler 	/* XXX locking */
114b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
115b032f27cSSam Leffler 		struct ieee80211_node *ni;
116b032f27cSSam Leffler 		struct mbuf *mcopy;
117b032f27cSSam Leffler 
118c3655fa4SSam Leffler 		/* NB: could check for IFF_UP but this is cheaper */
119c3655fa4SSam Leffler 		if (vap->iv_state == IEEE80211_S_INIT)
120c3655fa4SSam Leffler 			continue;
121b032f27cSSam Leffler 		/*
122b032f27cSSam Leffler 		 * WDS vap's only receive directed traffic from the
123b032f27cSSam Leffler 		 * station at the ``far end''.  That traffic should
124b032f27cSSam Leffler 		 * be passed through the AP vap the station is associated
125b032f27cSSam Leffler 		 * to--so don't spam them with mcast frames.
126b032f27cSSam Leffler 		 */
127b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_WDS)
128b032f27cSSam Leffler 			continue;
129b032f27cSSam Leffler 		if (TAILQ_NEXT(vap, iv_next) != NULL) {
130b032f27cSSam Leffler 			/*
131b032f27cSSam Leffler 			 * Packet contents are changed by ieee80211_decap
132b032f27cSSam Leffler 			 * so do a deep copy of the packet.
133b032f27cSSam Leffler 			 */
134eb1b1807SGleb Smirnoff 			mcopy = m_dup(m, M_NOWAIT);
135b032f27cSSam Leffler 			if (mcopy == NULL) {
136b032f27cSSam Leffler 				/* XXX stat+msg */
137b032f27cSSam Leffler 				continue;
1388a1b9b6aSSam Leffler 			}
139b032f27cSSam Leffler 		} else {
140b032f27cSSam Leffler 			mcopy = m;
141b032f27cSSam Leffler 			m = NULL;
1428a1b9b6aSSam Leffler 		}
143b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
144864ab114SAdrian Chadd 		type = ieee80211_input_mimo(ni, mcopy, rx);
145b032f27cSSam Leffler 		ieee80211_free_node(ni);
1461a1e1d21SSam Leffler 	}
147b032f27cSSam Leffler 	if (m != NULL)			/* no vaps, reclaim mbuf */
1481a1e1d21SSam Leffler 		m_freem(m);
1491f298879SSam Leffler 	return type;
1501a1e1d21SSam Leffler }
1511a1e1d21SSam Leffler 
1528a1b9b6aSSam Leffler /*
153df0d214aSRui Paulo  * This function reassembles fragments.
154b032f27cSSam Leffler  *
155b032f27cSSam Leffler  * XXX should handle 3 concurrent reassemblies per-spec.
1568a1b9b6aSSam Leffler  */
157b032f27cSSam Leffler struct mbuf *
158b032f27cSSam Leffler ieee80211_defrag(struct ieee80211_node *ni, struct mbuf *m, int hdrspace)
1591a1e1d21SSam Leffler {
160b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1618a1b9b6aSSam Leffler 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
1628a1b9b6aSSam Leffler 	struct ieee80211_frame *lwh;
16368e8e04eSSam Leffler 	uint16_t rxseq;
16468e8e04eSSam Leffler 	uint8_t fragno;
16568e8e04eSSam Leffler 	uint8_t more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG;
1668a1b9b6aSSam Leffler 	struct mbuf *mfrag;
1678a1b9b6aSSam Leffler 
1688a1b9b6aSSam Leffler 	KASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1), ("multicast fragm?"));
1698a1b9b6aSSam Leffler 
17068e8e04eSSam Leffler 	rxseq = le16toh(*(uint16_t *)wh->i_seq);
1718a1b9b6aSSam Leffler 	fragno = rxseq & IEEE80211_SEQ_FRAG_MASK;
1728a1b9b6aSSam Leffler 
1738a1b9b6aSSam Leffler 	/* Quick way out, if there's nothing to defragment */
1748a1b9b6aSSam Leffler 	if (!more_frag && fragno == 0 && ni->ni_rxfrag[0] == NULL)
1758a1b9b6aSSam Leffler 		return m;
1768a1b9b6aSSam Leffler 
1778a1b9b6aSSam Leffler 	/*
1788a1b9b6aSSam Leffler 	 * Remove frag to insure it doesn't get reaped by timer.
1798a1b9b6aSSam Leffler 	 */
1808a1b9b6aSSam Leffler 	if (ni->ni_table == NULL) {
1818a1b9b6aSSam Leffler 		/*
1828a1b9b6aSSam Leffler 		 * Should never happen.  If the node is orphaned (not in
1838a1b9b6aSSam Leffler 		 * the table) then input packets should not reach here.
1848a1b9b6aSSam Leffler 		 * Otherwise, a concurrent request that yanks the table
1858a1b9b6aSSam Leffler 		 * should be blocked by other interlocking and/or by first
1868a1b9b6aSSam Leffler 		 * shutting the driver down.  Regardless, be defensive
1878a1b9b6aSSam Leffler 		 * here and just bail
1888a1b9b6aSSam Leffler 		 */
1898a1b9b6aSSam Leffler 		/* XXX need msg+stat */
1908a1b9b6aSSam Leffler 		m_freem(m);
1918a1b9b6aSSam Leffler 		return NULL;
1928a1b9b6aSSam Leffler 	}
1938a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(ni->ni_table);
1948a1b9b6aSSam Leffler 	mfrag = ni->ni_rxfrag[0];
1958a1b9b6aSSam Leffler 	ni->ni_rxfrag[0] = NULL;
1968a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(ni->ni_table);
1978a1b9b6aSSam Leffler 
1988a1b9b6aSSam Leffler 	/*
1998a1b9b6aSSam Leffler 	 * Validate new fragment is in order and
2008a1b9b6aSSam Leffler 	 * related to the previous ones.
2018a1b9b6aSSam Leffler 	 */
2028a1b9b6aSSam Leffler 	if (mfrag != NULL) {
20368e8e04eSSam Leffler 		uint16_t last_rxseq;
2048a1b9b6aSSam Leffler 
2058a1b9b6aSSam Leffler 		lwh = mtod(mfrag, struct ieee80211_frame *);
20668e8e04eSSam Leffler 		last_rxseq = le16toh(*(uint16_t *)lwh->i_seq);
2078a1b9b6aSSam Leffler 		/* NB: check seq # and frag together */
2088a1b9b6aSSam Leffler 		if (rxseq != last_rxseq+1 ||
2098a1b9b6aSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) ||
2108a1b9b6aSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) {
2118a1b9b6aSSam Leffler 			/*
2128a1b9b6aSSam Leffler 			 * Unrelated fragment or no space for it,
2138a1b9b6aSSam Leffler 			 * clear current fragments.
2148a1b9b6aSSam Leffler 			 */
2158a1b9b6aSSam Leffler 			m_freem(mfrag);
2168a1b9b6aSSam Leffler 			mfrag = NULL;
2178a1b9b6aSSam Leffler 		}
2188a1b9b6aSSam Leffler 	}
2198a1b9b6aSSam Leffler 
2208a1b9b6aSSam Leffler  	if (mfrag == NULL) {
2218a1b9b6aSSam Leffler 		if (fragno != 0) {		/* !first fragment, discard */
222b032f27cSSam Leffler 			vap->iv_stats.is_rx_defrag++;
2238a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_defrag);
2248a1b9b6aSSam Leffler 			m_freem(m);
2258a1b9b6aSSam Leffler 			return NULL;
2268a1b9b6aSSam Leffler 		}
2278a1b9b6aSSam Leffler 		mfrag = m;
2288a1b9b6aSSam Leffler 	} else {				/* concatenate */
2292cc12adeSSam Leffler 		m_adj(m, hdrspace);		/* strip header */
2308a1b9b6aSSam Leffler 		m_cat(mfrag, m);
2318a1b9b6aSSam Leffler 		/* NB: m_cat doesn't update the packet header */
2328a1b9b6aSSam Leffler 		mfrag->m_pkthdr.len += m->m_pkthdr.len;
2338a1b9b6aSSam Leffler 		/* track last seqnum and fragno */
2348a1b9b6aSSam Leffler 		lwh = mtod(mfrag, struct ieee80211_frame *);
23568e8e04eSSam Leffler 		*(uint16_t *) lwh->i_seq = *(uint16_t *) wh->i_seq;
2368a1b9b6aSSam Leffler 	}
2378a1b9b6aSSam Leffler 	if (more_frag) {			/* more to come, save */
238a0cc3f85SSam Leffler 		ni->ni_rxfragstamp = ticks;
2398a1b9b6aSSam Leffler 		ni->ni_rxfrag[0] = mfrag;
2408a1b9b6aSSam Leffler 		mfrag = NULL;
2418a1b9b6aSSam Leffler 	}
2428a1b9b6aSSam Leffler 	return mfrag;
2438a1b9b6aSSam Leffler }
2448a1b9b6aSSam Leffler 
24568e8e04eSSam Leffler void
246b032f27cSSam Leffler ieee80211_deliver_data(struct ieee80211vap *vap,
2471bd482efSSam Leffler 	struct ieee80211_node *ni, struct mbuf *m)
2481bd482efSSam Leffler {
2491bd482efSSam Leffler 	struct ether_header *eh = mtod(m, struct ether_header *);
250b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
2511bd482efSSam Leffler 
252e1cfcbcbSSam Leffler 	/* clear driver/net80211 flags before passing up */
25386bd0491SAndre Oppermann 	m->m_flags &= ~(M_MCAST | M_BCAST);
254*b1051653SAdrian Chadd #if __FreeBSD_version >= 1000046
25586bd0491SAndre Oppermann 	m_clrprotoflags(m);
256*b1051653SAdrian Chadd #endif
257e1cfcbcbSSam Leffler 
258b032f27cSSam Leffler 	/* NB: see hostap_deliver_data, this path doesn't handle hostap */
259b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_HOSTAP, ("gack, hostap"));
26006efa2f0SSam Leffler 	/*
26106efa2f0SSam Leffler 	 * Do accounting.
26206efa2f0SSam Leffler 	 */
26306efa2f0SSam Leffler 	ifp->if_ipackets++;
26406efa2f0SSam Leffler 	IEEE80211_NODE_STAT(ni, rx_data);
26506efa2f0SSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
26606efa2f0SSam Leffler 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
26706efa2f0SSam Leffler 		m->m_flags |= M_MCAST;		/* XXX M_BCAST? */
26806efa2f0SSam Leffler 		IEEE80211_NODE_STAT(ni, rx_mcast);
26906efa2f0SSam Leffler 	} else
27006efa2f0SSam Leffler 		IEEE80211_NODE_STAT(ni, rx_ucast);
271b032f27cSSam Leffler 	m->m_pkthdr.rcvif = ifp;
27206efa2f0SSam Leffler 
2731bd482efSSam Leffler 	if (ni->ni_vlan != 0) {
2741bd482efSSam Leffler 		/* attach vlan tag */
27578ba57b9SAndre Oppermann 		m->m_pkthdr.ether_vtag = ni->ni_vlan;
27678ba57b9SAndre Oppermann 		m->m_flags |= M_VLANTAG;
2771bd482efSSam Leffler 	}
278b032f27cSSam Leffler 	ifp->if_input(ifp, m);
2791bd482efSSam Leffler }
2801bd482efSSam Leffler 
281b032f27cSSam Leffler struct mbuf *
282b032f27cSSam Leffler ieee80211_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen)
2838a1b9b6aSSam Leffler {
28459aa14a9SRui Paulo 	struct ieee80211_qosframe_addr4 wh;
2851a1e1d21SSam Leffler 	struct ether_header *eh;
2861a1e1d21SSam Leffler 	struct llc *llc;
2871a1e1d21SSam Leffler 
288c104cff2SRui Paulo 	KASSERT(hdrlen <= sizeof(wh),
289c104cff2SRui Paulo 	    ("hdrlen %d > max %zd", hdrlen, sizeof(wh)));
290c104cff2SRui Paulo 
2912cc12adeSSam Leffler 	if (m->m_len < hdrlen + sizeof(*llc) &&
2922cc12adeSSam Leffler 	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
293c104cff2SRui Paulo 		vap->iv_stats.is_rx_tooshort++;
294c104cff2SRui Paulo 		/* XXX msg */
2951a1e1d21SSam Leffler 		return NULL;
2961a1e1d21SSam Leffler 	}
2972cc12adeSSam Leffler 	memcpy(&wh, mtod(m, caddr_t), hdrlen);
2982cc12adeSSam Leffler 	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
2991a1e1d21SSam Leffler 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
3001a1e1d21SSam Leffler 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
3016bbdc701SSam Leffler 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
3026bbdc701SSam Leffler 	    /* NB: preserve AppleTalk frames that have a native SNAP hdr */
3036bbdc701SSam Leffler 	    !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
3046bbdc701SSam Leffler 	      llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
305ab96db10SSam Leffler 		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
3061a1e1d21SSam Leffler 		llc = NULL;
3071a1e1d21SSam Leffler 	} else {
3082cc12adeSSam Leffler 		m_adj(m, hdrlen - sizeof(*eh));
3091a1e1d21SSam Leffler 	}
3101a1e1d21SSam Leffler 	eh = mtod(m, struct ether_header *);
3111a1e1d21SSam Leffler 	switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
3121a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_NODS:
3131a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
3141a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
3151a1e1d21SSam Leffler 		break;
3161a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_TODS:
3171a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
3181a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
3191a1e1d21SSam Leffler 		break;
3201a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
3211a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
3221a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3);
3231a1e1d21SSam Leffler 		break;
3241a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
3252cc12adeSSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
3262cc12adeSSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr4);
3272cc12adeSSam Leffler 		break;
3281a1e1d21SSam Leffler 	}
32923f4fd6dSGleb Smirnoff #ifndef __NO_STRICT_ALIGNMENT
33068e8e04eSSam Leffler 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
331519f677aSSam Leffler 		m = ieee80211_realign(vap, m, sizeof(*eh));
332519f677aSSam Leffler 		if (m == NULL)
3331a1e1d21SSam Leffler 			return NULL;
3341a1e1d21SSam Leffler 	}
33523f4fd6dSGleb Smirnoff #endif /* !__NO_STRICT_ALIGNMENT */
3361a1e1d21SSam Leffler 	if (llc != NULL) {
3371a1e1d21SSam Leffler 		eh = mtod(m, struct ether_header *);
3381a1e1d21SSam Leffler 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
3391a1e1d21SSam Leffler 	}
3401a1e1d21SSam Leffler 	return m;
3411a1e1d21SSam Leffler }
3421a1e1d21SSam Leffler 
3431a1e1d21SSam Leffler /*
34468e8e04eSSam Leffler  * Decap a frame encapsulated in a fast-frame/A-MSDU.
34568e8e04eSSam Leffler  */
34668e8e04eSSam Leffler struct mbuf *
34768e8e04eSSam Leffler ieee80211_decap1(struct mbuf *m, int *framelen)
34868e8e04eSSam Leffler {
34968e8e04eSSam Leffler #define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
35068e8e04eSSam Leffler 	struct ether_header *eh;
35168e8e04eSSam Leffler 	struct llc *llc;
35268e8e04eSSam Leffler 
35368e8e04eSSam Leffler 	/*
35468e8e04eSSam Leffler 	 * The frame has an 802.3 header followed by an 802.2
35568e8e04eSSam Leffler 	 * LLC header.  The encapsulated frame length is in the
35668e8e04eSSam Leffler 	 * first header type field; save that and overwrite it
35768e8e04eSSam Leffler 	 * with the true type field found in the second.  Then
35868e8e04eSSam Leffler 	 * copy the 802.3 header up to where it belongs and
35968e8e04eSSam Leffler 	 * adjust the mbuf contents to remove the void.
36068e8e04eSSam Leffler 	 */
36168e8e04eSSam Leffler 	if (m->m_len < FF_LLC_SIZE && (m = m_pullup(m, FF_LLC_SIZE)) == NULL)
36268e8e04eSSam Leffler 		return NULL;
36368e8e04eSSam Leffler 	eh = mtod(m, struct ether_header *);	/* 802.3 header is first */
36468e8e04eSSam Leffler 	llc = (struct llc *)&eh[1];		/* 802.2 header follows */
36568e8e04eSSam Leffler 	*framelen = ntohs(eh->ether_type)	/* encap'd frame size */
36668e8e04eSSam Leffler 		  + sizeof(struct ether_header) - sizeof(struct llc);
36768e8e04eSSam Leffler 	eh->ether_type = llc->llc_un.type_snap.ether_type;
36868e8e04eSSam Leffler 	ovbcopy(eh, mtod(m, uint8_t *) + sizeof(struct llc),
36968e8e04eSSam Leffler 		sizeof(struct ether_header));
37068e8e04eSSam Leffler 	m_adj(m, sizeof(struct llc));
37168e8e04eSSam Leffler 	return m;
37268e8e04eSSam Leffler #undef FF_LLC_SIZE
37368e8e04eSSam Leffler }
37468e8e04eSSam Leffler 
37568e8e04eSSam Leffler /*
3761a1e1d21SSam Leffler  * Install received rate set information in the node's state block.
3771a1e1d21SSam Leffler  */
3787d77cd53SSam Leffler int
3797d77cd53SSam Leffler ieee80211_setup_rates(struct ieee80211_node *ni,
38068e8e04eSSam Leffler 	const uint8_t *rates, const uint8_t *xrates, int flags)
3811a1e1d21SSam Leffler {
382b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3831a1e1d21SSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
3841a1e1d21SSam Leffler 
3851a1e1d21SSam Leffler 	memset(rs, 0, sizeof(*rs));
3861a1e1d21SSam Leffler 	rs->rs_nrates = rates[1];
3871a1e1d21SSam Leffler 	memcpy(rs->rs_rates, rates + 2, rs->rs_nrates);
3881a1e1d21SSam Leffler 	if (xrates != NULL) {
38968e8e04eSSam Leffler 		uint8_t nxrates;
3901a1e1d21SSam Leffler 		/*
3911a1e1d21SSam Leffler 		 * Tack on 11g extended supported rate element.
3921a1e1d21SSam Leffler 		 */
3931a1e1d21SSam Leffler 		nxrates = xrates[1];
3941a1e1d21SSam Leffler 		if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
3951a1e1d21SSam Leffler 			nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates;
396b032f27cSSam Leffler 			IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE, ni,
397b032f27cSSam Leffler 			    "extended rate set too large; only using "
398b032f27cSSam Leffler 			    "%u of %u rates", nxrates, xrates[1]);
399b032f27cSSam Leffler 			vap->iv_stats.is_rx_rstoobig++;
4001a1e1d21SSam Leffler 		}
4011a1e1d21SSam Leffler 		memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates);
4021a1e1d21SSam Leffler 		rs->rs_nrates += nxrates;
4031a1e1d21SSam Leffler 	}
40470e28b9aSSam Leffler 	return ieee80211_fix_rate(ni, rs, flags);
4051a1e1d21SSam Leffler }
4061a1e1d21SSam Leffler 
40784eb84c4SSam Leffler /*
40884eb84c4SSam Leffler  * Send a management frame error response to the specified
40984eb84c4SSam Leffler  * station.  If ni is associated with the station then use
41084eb84c4SSam Leffler  * it; otherwise allocate a temporary node suitable for
41184eb84c4SSam Leffler  * transmitting the frame and then free the reference so
41284eb84c4SSam Leffler  * it will go away as soon as the frame has been transmitted.
41384eb84c4SSam Leffler  */
414b032f27cSSam Leffler void
415b032f27cSSam Leffler ieee80211_send_error(struct ieee80211_node *ni,
416b032f27cSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN], int subtype, int arg)
41784eb84c4SSam Leffler {
418b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
41984eb84c4SSam Leffler 	int istmp;
42084eb84c4SSam Leffler 
421b032f27cSSam Leffler 	if (ni == vap->iv_bss) {
422b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN) {
423b032f27cSSam Leffler 			/*
424b032f27cSSam Leffler 			 * XXX hack until we get rid of this routine.
425b032f27cSSam Leffler 			 * We can be called prior to the vap reaching
426b032f27cSSam Leffler 			 * run state under certain conditions in which
427b032f27cSSam Leffler 			 * case iv_bss->ni_chan will not be setup.
428b032f27cSSam Leffler 			 * Check for this explicitly and and just ignore
429b032f27cSSam Leffler 			 * the request.
430b032f27cSSam Leffler 			 */
431b032f27cSSam Leffler 			return;
432b032f27cSSam Leffler 		}
433b032f27cSSam Leffler 		ni = ieee80211_tmp_node(vap, mac);
43484eb84c4SSam Leffler 		if (ni == NULL) {
43584eb84c4SSam Leffler 			/* XXX msg */
43684eb84c4SSam Leffler 			return;
43784eb84c4SSam Leffler 		}
43884eb84c4SSam Leffler 		istmp = 1;
43984eb84c4SSam Leffler 	} else
44084eb84c4SSam Leffler 		istmp = 0;
441b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, subtype, arg);
44284eb84c4SSam Leffler 	if (istmp)
44384eb84c4SSam Leffler 		ieee80211_free_node(ni);
44484eb84c4SSam Leffler }
44584eb84c4SSam Leffler 
446b032f27cSSam Leffler int
447b032f27cSSam Leffler ieee80211_alloc_challenge(struct ieee80211_node *ni)
4488a1b9b6aSSam Leffler {
4498a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL)
450e2126decSSam Leffler 		ni->ni_challenge = (uint32_t *) malloc(IEEE80211_CHALLENGE_LEN,
45168e8e04eSSam Leffler 		    M_80211_NODE, M_NOWAIT);
4528a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL) {
453b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap,
454b032f27cSSam Leffler 		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni,
455b032f27cSSam Leffler 		    "%s", "shared key challenge alloc failed");
4568a1b9b6aSSam Leffler 		/* XXX statistic */
4578a1b9b6aSSam Leffler 	}
4588a1b9b6aSSam Leffler 	return (ni->ni_challenge != NULL);
4598a1b9b6aSSam Leffler }
4608a1b9b6aSSam Leffler 
46166ef3969SSam Leffler /*
462b032f27cSSam Leffler  * Parse a Beacon or ProbeResponse frame and return the
463b032f27cSSam Leffler  * useful information in an ieee80211_scanparams structure.
464b032f27cSSam Leffler  * Status is set to 0 if no problems were found; otherwise
465b032f27cSSam Leffler  * a bitmask of IEEE80211_BPARSE_* items is returned that
466b032f27cSSam Leffler  * describes the problems detected.
46766ef3969SSam Leffler  */
468b032f27cSSam Leffler int
469b032f27cSSam Leffler ieee80211_parse_beacon(struct ieee80211_node *ni, struct mbuf *m,
470b032f27cSSam Leffler 	struct ieee80211_scanparams *scan)
47166ef3969SSam Leffler {
472b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
47366ef3969SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
4741a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
47568e8e04eSSam Leffler 	uint8_t *frm, *efrm;
4761a1e1d21SSam Leffler 
477b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
47868e8e04eSSam Leffler 	frm = (uint8_t *)&wh[1];
479b032f27cSSam Leffler 	efrm = mtod(m, uint8_t *) + m->m_len;
480b032f27cSSam Leffler 	scan->status = 0;
4811a1e1d21SSam Leffler 	/*
4821a1e1d21SSam Leffler 	 * beacon/probe response frame format
4831a1e1d21SSam Leffler 	 *	[8] time stamp
4841a1e1d21SSam Leffler 	 *	[2] beacon interval
4851a1e1d21SSam Leffler 	 *	[2] capability information
4861a1e1d21SSam Leffler 	 *	[tlv] ssid
4871a1e1d21SSam Leffler 	 *	[tlv] supported rates
4881a1e1d21SSam Leffler 	 *	[tlv] country information
489c70761e6SSam Leffler 	 *	[tlv] channel switch announcement (CSA)
4901a1e1d21SSam Leffler 	 *	[tlv] parameter set (FH/DS)
4911a1e1d21SSam Leffler 	 *	[tlv] erp information
4921a1e1d21SSam Leffler 	 *	[tlv] extended supported rates
4938a1b9b6aSSam Leffler 	 *	[tlv] WME
4948a1b9b6aSSam Leffler 	 *	[tlv] WPA or RSN
49568e8e04eSSam Leffler 	 *	[tlv] HT capabilities
49668e8e04eSSam Leffler 	 *	[tlv] HT information
49768e8e04eSSam Leffler 	 *	[tlv] Atheros capabilities
49859aa14a9SRui Paulo 	 *	[tlv] Mesh ID
49959aa14a9SRui Paulo 	 *	[tlv] Mesh Configuration
5001a1e1d21SSam Leffler 	 */
501b032f27cSSam Leffler 	IEEE80211_VERIFY_LENGTH(efrm - frm, 12,
502b032f27cSSam Leffler 	    return (scan->status = IEEE80211_BPARSE_BADIELEN));
503b032f27cSSam Leffler 	memset(scan, 0, sizeof(*scan));
504b032f27cSSam Leffler 	scan->tstamp  = frm;				frm += 8;
505b032f27cSSam Leffler 	scan->bintval = le16toh(*(uint16_t *)frm);	frm += 2;
506b032f27cSSam Leffler 	scan->capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
507b032f27cSSam Leffler 	scan->bchan = ieee80211_chan2ieee(ic, ic->ic_curchan);
508b032f27cSSam Leffler 	scan->chan = scan->bchan;
509b032f27cSSam Leffler 	scan->ies = frm;
510b032f27cSSam Leffler 	scan->ies_len = efrm - frm;
511b5c99415SSam Leffler 
51270326a6eSSam Leffler 	while (efrm - frm > 1) {
513b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2,
514b032f27cSSam Leffler 		    return (scan->status = IEEE80211_BPARSE_BADIELEN));
5151a1e1d21SSam Leffler 		switch (*frm) {
5161a1e1d21SSam Leffler 		case IEEE80211_ELEMID_SSID:
517b032f27cSSam Leffler 			scan->ssid = frm;
5181a1e1d21SSam Leffler 			break;
5191a1e1d21SSam Leffler 		case IEEE80211_ELEMID_RATES:
520b032f27cSSam Leffler 			scan->rates = frm;
5211a1e1d21SSam Leffler 			break;
5221a1e1d21SSam Leffler 		case IEEE80211_ELEMID_COUNTRY:
523b032f27cSSam Leffler 			scan->country = frm;
5241a1e1d21SSam Leffler 			break;
525c70761e6SSam Leffler 		case IEEE80211_ELEMID_CSA:
526c70761e6SSam Leffler 			scan->csa = frm;
527c70761e6SSam Leffler 			break;
52832b0e64bSAdrian Chadd 		case IEEE80211_ELEMID_QUIET:
52932b0e64bSAdrian Chadd 			scan->quiet = frm;
53032b0e64bSAdrian Chadd 			break;
5311a1e1d21SSam Leffler 		case IEEE80211_ELEMID_FHPARMS:
5321a1e1d21SSam Leffler 			if (ic->ic_phytype == IEEE80211_T_FH) {
533b032f27cSSam Leffler 				scan->fhdwell = LE_READ_2(&frm[2]);
534b032f27cSSam Leffler 				scan->chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
535b032f27cSSam Leffler 				scan->fhindex = frm[6];
5361a1e1d21SSam Leffler 			}
5371a1e1d21SSam Leffler 			break;
5381a1e1d21SSam Leffler 		case IEEE80211_ELEMID_DSPARMS:
5391a1e1d21SSam Leffler 			/*
5401a1e1d21SSam Leffler 			 * XXX hack this since depending on phytype
5411a1e1d21SSam Leffler 			 * is problematic for multi-mode devices.
5421a1e1d21SSam Leffler 			 */
5431a1e1d21SSam Leffler 			if (ic->ic_phytype != IEEE80211_T_FH)
544b032f27cSSam Leffler 				scan->chan = frm[2];
5451a1e1d21SSam Leffler 			break;
5461a1e1d21SSam Leffler 		case IEEE80211_ELEMID_TIM:
5478a1b9b6aSSam Leffler 			/* XXX ATIM? */
548b032f27cSSam Leffler 			scan->tim = frm;
549b032f27cSSam Leffler 			scan->timoff = frm - mtod(m, uint8_t *);
5501a1e1d21SSam Leffler 			break;
5514bd067c5SSam Leffler 		case IEEE80211_ELEMID_IBSSPARMS:
552b032f27cSSam Leffler 		case IEEE80211_ELEMID_CFPARMS:
553643024a2SSam Leffler 		case IEEE80211_ELEMID_PWRCNSTR:
554b032f27cSSam Leffler 			/* NB: avoid debugging complaints */
5554bd067c5SSam Leffler 			break;
5561a1e1d21SSam Leffler 		case IEEE80211_ELEMID_XRATES:
557b032f27cSSam Leffler 			scan->xrates = frm;
5581a1e1d21SSam Leffler 			break;
5591a1e1d21SSam Leffler 		case IEEE80211_ELEMID_ERP:
5601a1e1d21SSam Leffler 			if (frm[1] != 1) {
561b032f27cSSam Leffler 				IEEE80211_DISCARD_IE(vap,
5628a1b9b6aSSam Leffler 				    IEEE80211_MSG_ELEMID, wh, "ERP",
5638a1b9b6aSSam Leffler 				    "bad len %u", frm[1]);
564b032f27cSSam Leffler 				vap->iv_stats.is_rx_elem_toobig++;
5651a1e1d21SSam Leffler 				break;
5661a1e1d21SSam Leffler 			}
567b032f27cSSam Leffler 			scan->erp = frm[2] | 0x100;
5681a1e1d21SSam Leffler 			break;
56968e8e04eSSam Leffler 		case IEEE80211_ELEMID_HTCAP:
570b032f27cSSam Leffler 			scan->htcap = frm;
57168e8e04eSSam Leffler 			break;
5728a1b9b6aSSam Leffler 		case IEEE80211_ELEMID_RSN:
573b032f27cSSam Leffler 			scan->rsn = frm;
57468e8e04eSSam Leffler 			break;
57568e8e04eSSam Leffler 		case IEEE80211_ELEMID_HTINFO:
576b032f27cSSam Leffler 			scan->htinfo = frm;
5778a1b9b6aSSam Leffler 			break;
578fbe007daSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
57959aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHID:
58059aa14a9SRui Paulo 			scan->meshid = frm;
58159aa14a9SRui Paulo 			break;
58259aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHCONF:
58359aa14a9SRui Paulo 			scan->meshconf = frm;
58459aa14a9SRui Paulo 			break;
58559aa14a9SRui Paulo #endif
5868a1b9b6aSSam Leffler 		case IEEE80211_ELEMID_VENDOR:
5878a1b9b6aSSam Leffler 			if (iswpaoui(frm))
588b032f27cSSam Leffler 				scan->wpa = frm;
5898a1b9b6aSSam Leffler 			else if (iswmeparam(frm) || iswmeinfo(frm))
590b032f27cSSam Leffler 				scan->wme = frm;
591616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
59268e8e04eSSam Leffler 			else if (isatherosoui(frm))
593b032f27cSSam Leffler 				scan->ath = frm;
594616190d0SSam Leffler #endif
59510ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
59610ad9a77SSam Leffler 			else if (istdmaoui(frm))
59710ad9a77SSam Leffler 				scan->tdma = frm;
59810ad9a77SSam Leffler #endif
5992bfc8a91SSam Leffler 			else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
60068e8e04eSSam Leffler 				/*
60168e8e04eSSam Leffler 				 * Accept pre-draft HT ie's if the
60268e8e04eSSam Leffler 				 * standard ones have not been seen.
60368e8e04eSSam Leffler 				 */
60468e8e04eSSam Leffler 				if (ishtcapoui(frm)) {
605b032f27cSSam Leffler 					if (scan->htcap == NULL)
606b032f27cSSam Leffler 						scan->htcap = frm;
60768e8e04eSSam Leffler 				} else if (ishtinfooui(frm)) {
608b032f27cSSam Leffler 					if (scan->htinfo == NULL)
609b032f27cSSam Leffler 						scan->htcap = frm;
61068e8e04eSSam Leffler 				}
61168e8e04eSSam Leffler 			}
6128a1b9b6aSSam Leffler 			break;
6131a1e1d21SSam Leffler 		default:
614b032f27cSSam Leffler 			IEEE80211_DISCARD_IE(vap, IEEE80211_MSG_ELEMID,
6158a1b9b6aSSam Leffler 			    wh, "unhandled",
6168a1b9b6aSSam Leffler 			    "id %u, len %u", *frm, frm[1]);
617b032f27cSSam Leffler 			vap->iv_stats.is_rx_elem_unknown++;
6181a1e1d21SSam Leffler 			break;
6191a1e1d21SSam Leffler 		}
6201a1e1d21SSam Leffler 		frm += frm[1] + 2;
6211a1e1d21SSam Leffler 	}
622b032f27cSSam Leffler 	IEEE80211_VERIFY_ELEMENT(scan->rates, IEEE80211_RATE_MAXSIZE,
623b032f27cSSam Leffler 	    scan->status |= IEEE80211_BPARSE_RATES_INVALID);
624b032f27cSSam Leffler 	if (scan->rates != NULL && scan->xrates != NULL) {
625b032f27cSSam Leffler 		/*
626b032f27cSSam Leffler 		 * NB: don't process XRATES if RATES is missing.  This
627b032f27cSSam Leffler 		 * avoids a potential null ptr deref and should be ok
628b032f27cSSam Leffler 		 * as the return code will already note RATES is missing
629b032f27cSSam Leffler 		 * (so callers shouldn't otherwise process the frame).
630b032f27cSSam Leffler 		 */
631b032f27cSSam Leffler 		IEEE80211_VERIFY_ELEMENT(scan->xrates,
632b032f27cSSam Leffler 		    IEEE80211_RATE_MAXSIZE - scan->rates[1],
633b032f27cSSam Leffler 		    scan->status |= IEEE80211_BPARSE_XRATES_INVALID);
634b032f27cSSam Leffler 	}
635b032f27cSSam Leffler 	IEEE80211_VERIFY_ELEMENT(scan->ssid, IEEE80211_NWID_LEN,
636b032f27cSSam Leffler 	    scan->status |= IEEE80211_BPARSE_SSID_INVALID);
637b032f27cSSam Leffler 	if (scan->chan != scan->bchan && ic->ic_phytype != IEEE80211_T_FH) {
6381a1e1d21SSam Leffler 		/*
6391a1e1d21SSam Leffler 		 * Frame was received on a channel different from the
6404844aa7dSAtsushi Onoe 		 * one indicated in the DS params element id;
6411a1e1d21SSam Leffler 		 * silently discard it.
6421a1e1d21SSam Leffler 		 *
6431a1e1d21SSam Leffler 		 * NB: this can happen due to signal leakage.
6444844aa7dSAtsushi Onoe 		 *     But we should take it for FH phy because
6454844aa7dSAtsushi Onoe 		 *     the rssi value should be correct even for
6464844aa7dSAtsushi Onoe 		 *     different hop pattern in FH.
6471a1e1d21SSam Leffler 		 */
648b032f27cSSam Leffler 		IEEE80211_DISCARD(vap,
649d365f9c7SSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
650b032f27cSSam Leffler 		    wh, NULL, "for off-channel %u", scan->chan);
651b032f27cSSam Leffler 		vap->iv_stats.is_rx_chanmismatch++;
652b032f27cSSam Leffler 		scan->status |= IEEE80211_BPARSE_OFFCHAN;
653b5c99415SSam Leffler 	}
654b032f27cSSam Leffler 	if (!(IEEE80211_BINTVAL_MIN <= scan->bintval &&
655b032f27cSSam Leffler 	      scan->bintval <= IEEE80211_BINTVAL_MAX)) {
656b032f27cSSam Leffler 		IEEE80211_DISCARD(vap,
657b5c99415SSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
658de681822SAdrian Chadd 		    wh, NULL, "bogus beacon interval (%d TU)",
659de681822SAdrian Chadd 		    (int) scan->bintval);
660b032f27cSSam Leffler 		vap->iv_stats.is_rx_badbintval++;
661b032f27cSSam Leffler 		scan->status |= IEEE80211_BPARSE_BINTVAL_INVALID;
662b032f27cSSam Leffler 	}
663b032f27cSSam Leffler 	if (scan->country != NULL) {
664b032f27cSSam Leffler 		/*
665b032f27cSSam Leffler 		 * Validate we have at least enough data to extract
666b032f27cSSam Leffler 		 * the country code.  Not sure if we should return an
667b032f27cSSam Leffler 		 * error instead of discarding the IE; consider this
668b032f27cSSam Leffler 		 * being lenient as we don't depend on the data for
669b032f27cSSam Leffler 		 * correct operation.
670b032f27cSSam Leffler 		 */
671b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(scan->country[1], 3 * sizeof(uint8_t),
672b032f27cSSam Leffler 		    scan->country = NULL);
673d365f9c7SSam Leffler 	}
674c70761e6SSam Leffler 	if (scan->csa != NULL) {
675c70761e6SSam Leffler 		/*
676c70761e6SSam Leffler 		 * Validate Channel Switch Announcement; this must
677c70761e6SSam Leffler 		 * be the correct length or we toss the frame.
678c70761e6SSam Leffler 		 */
679c70761e6SSam Leffler 		IEEE80211_VERIFY_LENGTH(scan->csa[1], 3 * sizeof(uint8_t),
680c70761e6SSam Leffler 		    scan->status |= IEEE80211_BPARSE_CSA_INVALID);
681c70761e6SSam Leffler 	}
68268e8e04eSSam Leffler 	/*
68368e8e04eSSam Leffler 	 * Process HT ie's.  This is complicated by our
68468e8e04eSSam Leffler 	 * accepting both the standard ie's and the pre-draft
68568e8e04eSSam Leffler 	 * vendor OUI ie's that some vendors still use/require.
68668e8e04eSSam Leffler 	 */
687b032f27cSSam Leffler 	if (scan->htcap != NULL) {
688b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(scan->htcap[1],
689b032f27cSSam Leffler 		     scan->htcap[0] == IEEE80211_ELEMID_VENDOR ?
69068e8e04eSSam Leffler 			 4 + sizeof(struct ieee80211_ie_htcap)-2 :
69168e8e04eSSam Leffler 			 sizeof(struct ieee80211_ie_htcap)-2,
692b032f27cSSam Leffler 		     scan->htcap = NULL);
69368e8e04eSSam Leffler 	}
694b032f27cSSam Leffler 	if (scan->htinfo != NULL) {
695b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(scan->htinfo[1],
696b032f27cSSam Leffler 		     scan->htinfo[0] == IEEE80211_ELEMID_VENDOR ?
69768e8e04eSSam Leffler 			 4 + sizeof(struct ieee80211_ie_htinfo)-2 :
69868e8e04eSSam Leffler 			 sizeof(struct ieee80211_ie_htinfo)-2,
699b032f27cSSam Leffler 		     scan->htinfo = NULL);
700b032f27cSSam Leffler 	}
701b032f27cSSam Leffler 	return scan->status;
70268e8e04eSSam Leffler }
7031a1e1d21SSam Leffler 
7041a1e1d21SSam Leffler /*
705b032f27cSSam Leffler  * Parse an Action frame.  Return 0 on success, non-zero on failure.
70644c72e42SSam Leffler  */
707b032f27cSSam Leffler int
708b032f27cSSam Leffler ieee80211_parse_action(struct ieee80211_node *ni, struct mbuf *m)
709b032f27cSSam Leffler {
710b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
71168e8e04eSSam Leffler 	const struct ieee80211_action *ia;
712b032f27cSSam Leffler 	struct ieee80211_frame *wh;
713b032f27cSSam Leffler 	uint8_t *frm, *efrm;
71468e8e04eSSam Leffler 
71568e8e04eSSam Leffler 	/*
71668e8e04eSSam Leffler 	 * action frame format:
71768e8e04eSSam Leffler 	 *	[1] category
71868e8e04eSSam Leffler 	 *	[1] action
71968e8e04eSSam Leffler 	 *	[tlv] parameters
72068e8e04eSSam Leffler 	 */
721b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
722b032f27cSSam Leffler 	frm = (u_int8_t *)&wh[1];
723b032f27cSSam Leffler 	efrm = mtod(m, u_int8_t *) + m->m_len;
72468e8e04eSSam Leffler 	IEEE80211_VERIFY_LENGTH(efrm - frm,
725b032f27cSSam Leffler 		sizeof(struct ieee80211_action), return EINVAL);
72668e8e04eSSam Leffler 	ia = (const struct ieee80211_action *) frm;
72768e8e04eSSam Leffler 
728b032f27cSSam Leffler 	vap->iv_stats.is_rx_action++;
72968e8e04eSSam Leffler 	IEEE80211_NODE_STAT(ni, rx_action);
73068e8e04eSSam Leffler 
73168e8e04eSSam Leffler 	/* verify frame payloads but defer processing */
73268e8e04eSSam Leffler 	switch (ia->ia_category) {
73368e8e04eSSam Leffler 	case IEEE80211_ACTION_CAT_BA:
73468e8e04eSSam Leffler 		switch (ia->ia_action) {
73568e8e04eSSam Leffler 		case IEEE80211_ACTION_BA_ADDBA_REQUEST:
73668e8e04eSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
73768e8e04eSSam Leffler 			    sizeof(struct ieee80211_action_ba_addbarequest),
738b032f27cSSam Leffler 			    return EINVAL);
73968e8e04eSSam Leffler 			break;
74068e8e04eSSam Leffler 		case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
74168e8e04eSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
74268e8e04eSSam Leffler 			    sizeof(struct ieee80211_action_ba_addbaresponse),
743b032f27cSSam Leffler 			    return EINVAL);
74468e8e04eSSam Leffler 			break;
74568e8e04eSSam Leffler 		case IEEE80211_ACTION_BA_DELBA:
74668e8e04eSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
74768e8e04eSSam Leffler 			    sizeof(struct ieee80211_action_ba_delba),
748b032f27cSSam Leffler 			    return EINVAL);
74968e8e04eSSam Leffler 			break;
75068e8e04eSSam Leffler 		}
75168e8e04eSSam Leffler 		break;
75268e8e04eSSam Leffler 	case IEEE80211_ACTION_CAT_HT:
75368e8e04eSSam Leffler 		switch (ia->ia_action) {
75468e8e04eSSam Leffler 		case IEEE80211_ACTION_HT_TXCHWIDTH:
75568e8e04eSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
75668e8e04eSSam Leffler 			    sizeof(struct ieee80211_action_ht_txchwidth),
757b032f27cSSam Leffler 			    return EINVAL);
758b032f27cSSam Leffler 			break;
759b032f27cSSam Leffler 		case IEEE80211_ACTION_HT_MIMOPWRSAVE:
760b032f27cSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
761b032f27cSSam Leffler 			    sizeof(struct ieee80211_action_ht_mimopowersave),
762b032f27cSSam Leffler 			    return EINVAL);
76368e8e04eSSam Leffler 			break;
76468e8e04eSSam Leffler 		}
76568e8e04eSSam Leffler 		break;
766dbab732dSGleb Smirnoff #ifdef IEEE80211_SUPPORT_MESH
767bdd2a076SAdrian Chadd 	case IEEE80211_ACTION_CAT_MESH:
768bdd2a076SAdrian Chadd 		switch (ia->ia_action) {
769bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_LMETRIC:
770bdd2a076SAdrian Chadd 			/*
771bdd2a076SAdrian Chadd 			 * XXX: verification is true only if we are using
772bdd2a076SAdrian Chadd 			 * Airtime link metric (default)
773bdd2a076SAdrian Chadd 			 */
774bdd2a076SAdrian Chadd 			IEEE80211_VERIFY_LENGTH(efrm - frm,
775bdd2a076SAdrian Chadd 			    sizeof(struct ieee80211_meshlmetric_ie),
776bdd2a076SAdrian Chadd 			    return EINVAL);
777bdd2a076SAdrian Chadd 			break;
778bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_HWMP:
779bdd2a076SAdrian Chadd 			/* verify something */
780bdd2a076SAdrian Chadd 			break;
781bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_GANN:
782c81ceff7SMonthadar Al Jaberi 			IEEE80211_VERIFY_LENGTH(efrm - frm,
783c81ceff7SMonthadar Al Jaberi 			    sizeof(struct ieee80211_meshgann_ie),
784c81ceff7SMonthadar Al Jaberi 			    return EINVAL);
785c81ceff7SMonthadar Al Jaberi 			break;
786bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_CC:
787bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_MCCA_SREQ:
788bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_MCCA_SREP:
789bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_MCCA_AREQ:
790bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_MCCA_ADVER:
791bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_MCCA_TRDOWN:
792bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_TBTT_REQ:
793bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_TBTT_RES:
794bdd2a076SAdrian Chadd 			/* reject these early on, not implemented */
795bdd2a076SAdrian Chadd 			IEEE80211_DISCARD(vap,
796bdd2a076SAdrian Chadd 			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
797bdd2a076SAdrian Chadd 			    wh, NULL, "not implemented yet, act=0x%02X",
798bdd2a076SAdrian Chadd 			    ia->ia_action);
799bdd2a076SAdrian Chadd 			return EINVAL;
800bdd2a076SAdrian Chadd 		}
801bdd2a076SAdrian Chadd 		break;
802ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_CAT_SELF_PROT:
803ebeaa1adSMonthadar Al Jaberi 		/* If TA or RA group address discard silently */
804ebeaa1adSMonthadar Al Jaberi 		if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
805ebeaa1adSMonthadar Al Jaberi 			IEEE80211_IS_MULTICAST(wh->i_addr2))
806ebeaa1adSMonthadar Al Jaberi 			return EINVAL;
807ebeaa1adSMonthadar Al Jaberi 		/*
808ebeaa1adSMonthadar Al Jaberi 		 * XXX: Should we verify complete length now or it is
809ebeaa1adSMonthadar Al Jaberi 		 * to varying in sizes?
810ebeaa1adSMonthadar Al Jaberi 		 */
811ebeaa1adSMonthadar Al Jaberi 		switch (ia->ia_action) {
812ebeaa1adSMonthadar Al Jaberi 		case IEEE80211_ACTION_MESHPEERING_CONFIRM:
813ebeaa1adSMonthadar Al Jaberi 		case IEEE80211_ACTION_MESHPEERING_CLOSE:
814ebeaa1adSMonthadar Al Jaberi 			/* is not a peering candidate (yet) */
815ebeaa1adSMonthadar Al Jaberi 			if (ni == vap->iv_bss)
816ebeaa1adSMonthadar Al Jaberi 				return EINVAL;
817ebeaa1adSMonthadar Al Jaberi 			break;
818ebeaa1adSMonthadar Al Jaberi 		}
819ebeaa1adSMonthadar Al Jaberi 		break;
820dbab732dSGleb Smirnoff #endif
82168e8e04eSSam Leffler 	}
822b032f27cSSam Leffler 	return 0;
8238a1b9b6aSSam Leffler }
8248a1b9b6aSSam Leffler 
8258a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
8268a1b9b6aSSam Leffler /*
8278a1b9b6aSSam Leffler  * Debugging support.
8288a1b9b6aSSam Leffler  */
829b032f27cSSam Leffler void
830b032f27cSSam Leffler ieee80211_ssid_mismatch(struct ieee80211vap *vap, const char *tag,
831b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN], uint8_t *ssid)
832b032f27cSSam Leffler {
833b032f27cSSam Leffler 	printf("[%s] discard %s frame, ssid mismatch: ",
834b032f27cSSam Leffler 		ether_sprintf(mac), tag);
835b032f27cSSam Leffler 	ieee80211_print_essid(ssid + 2, ssid[1]);
836b032f27cSSam Leffler 	printf("\n");
837b032f27cSSam Leffler }
8388a1b9b6aSSam Leffler 
8398a1b9b6aSSam Leffler /*
8408a1b9b6aSSam Leffler  * Return the bssid of a frame.
8418a1b9b6aSSam Leffler  */
84268e8e04eSSam Leffler static const uint8_t *
843d2bc4bf6SRui Paulo ieee80211_getbssid(const struct ieee80211vap *vap,
844d2bc4bf6SRui Paulo 	const struct ieee80211_frame *wh)
8458a1b9b6aSSam Leffler {
846b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA)
8478a1b9b6aSSam Leffler 		return wh->i_addr2;
8488a1b9b6aSSam Leffler 	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) != IEEE80211_FC1_DIR_NODS)
8498a1b9b6aSSam Leffler 		return wh->i_addr1;
8508a1b9b6aSSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
8518a1b9b6aSSam Leffler 		return wh->i_addr1;
8528a1b9b6aSSam Leffler 	return wh->i_addr3;
8538a1b9b6aSSam Leffler }
8548a1b9b6aSSam Leffler 
855b032f27cSSam Leffler #include <machine/stdarg.h>
856b032f27cSSam Leffler 
857f6df3191SSam Leffler void
858d2bc4bf6SRui Paulo ieee80211_note(const struct ieee80211vap *vap, const char *fmt, ...)
859f6df3191SSam Leffler {
860f6df3191SSam Leffler 	char buf[128];		/* XXX */
861f6df3191SSam Leffler 	va_list ap;
862f6df3191SSam Leffler 
863f6df3191SSam Leffler 	va_start(ap, fmt);
864f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
865f6df3191SSam Leffler 	va_end(ap);
866f6df3191SSam Leffler 
867b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "%s", buf);	/* NB: no \n */
868f6df3191SSam Leffler }
869f6df3191SSam Leffler 
870f6df3191SSam Leffler void
871d2bc4bf6SRui Paulo ieee80211_note_frame(const struct ieee80211vap *vap,
872f6df3191SSam Leffler 	const struct ieee80211_frame *wh,
873f6df3191SSam Leffler 	const char *fmt, ...)
874f6df3191SSam Leffler {
875f6df3191SSam Leffler 	char buf[128];		/* XXX */
876f6df3191SSam Leffler 	va_list ap;
877f6df3191SSam Leffler 
878f6df3191SSam Leffler 	va_start(ap, fmt);
879f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
880f6df3191SSam Leffler 	va_end(ap);
881b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] %s\n",
882b032f27cSSam Leffler 		ether_sprintf(ieee80211_getbssid(vap, wh)), buf);
883f6df3191SSam Leffler }
884f6df3191SSam Leffler 
885f6df3191SSam Leffler void
886d2bc4bf6SRui Paulo ieee80211_note_mac(const struct ieee80211vap *vap,
88768e8e04eSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN],
888f6df3191SSam Leffler 	const char *fmt, ...)
889f6df3191SSam Leffler {
890f6df3191SSam Leffler 	char buf[128];		/* XXX */
891f6df3191SSam Leffler 	va_list ap;
892f6df3191SSam Leffler 
893f6df3191SSam Leffler 	va_start(ap, fmt);
894f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
895f6df3191SSam Leffler 	va_end(ap);
896b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] %s\n", ether_sprintf(mac), buf);
897f6df3191SSam Leffler }
898f6df3191SSam Leffler 
899a000d7c2SSam Leffler void
900d2bc4bf6SRui Paulo ieee80211_discard_frame(const struct ieee80211vap *vap,
9018a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
9028a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
9038a1b9b6aSSam Leffler {
9048a1b9b6aSSam Leffler 	va_list ap;
9058a1b9b6aSSam Leffler 
906b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] discard ",
907b032f27cSSam Leffler 		ether_sprintf(ieee80211_getbssid(vap, wh)));
908b032f27cSSam Leffler 	if (type == NULL) {
909b032f27cSSam Leffler 		printf("%s frame, ", ieee80211_mgt_subtype_name[
910b032f27cSSam Leffler 			(wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) >>
911b032f27cSSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT]);
912b032f27cSSam Leffler 	} else
9138a1b9b6aSSam Leffler 		printf("%s frame, ", type);
9148a1b9b6aSSam Leffler 	va_start(ap, fmt);
9158a1b9b6aSSam Leffler 	vprintf(fmt, ap);
9168a1b9b6aSSam Leffler 	va_end(ap);
9178a1b9b6aSSam Leffler 	printf("\n");
9188a1b9b6aSSam Leffler }
9198a1b9b6aSSam Leffler 
920a000d7c2SSam Leffler void
921d2bc4bf6SRui Paulo ieee80211_discard_ie(const struct ieee80211vap *vap,
9228a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
9238a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
9248a1b9b6aSSam Leffler {
9258a1b9b6aSSam Leffler 	va_list ap;
9268a1b9b6aSSam Leffler 
927b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] discard ",
928b032f27cSSam Leffler 		ether_sprintf(ieee80211_getbssid(vap, wh)));
9298a1b9b6aSSam Leffler 	if (type != NULL)
9308a1b9b6aSSam Leffler 		printf("%s information element, ", type);
9318a1b9b6aSSam Leffler 	else
9328a1b9b6aSSam Leffler 		printf("information element, ");
9338a1b9b6aSSam Leffler 	va_start(ap, fmt);
9348a1b9b6aSSam Leffler 	vprintf(fmt, ap);
9358a1b9b6aSSam Leffler 	va_end(ap);
9368a1b9b6aSSam Leffler 	printf("\n");
9378a1b9b6aSSam Leffler }
9388a1b9b6aSSam Leffler 
939a000d7c2SSam Leffler void
940d2bc4bf6SRui Paulo ieee80211_discard_mac(const struct ieee80211vap *vap,
94168e8e04eSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN],
9428a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
9438a1b9b6aSSam Leffler {
9448a1b9b6aSSam Leffler 	va_list ap;
9458a1b9b6aSSam Leffler 
946b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] discard ", ether_sprintf(mac));
9478a1b9b6aSSam Leffler 	if (type != NULL)
9488a1b9b6aSSam Leffler 		printf("%s frame, ", type);
9498a1b9b6aSSam Leffler 	else
9508a1b9b6aSSam Leffler 		printf("frame, ");
9518a1b9b6aSSam Leffler 	va_start(ap, fmt);
9528a1b9b6aSSam Leffler 	vprintf(fmt, ap);
9538a1b9b6aSSam Leffler 	va_end(ap);
9548a1b9b6aSSam Leffler 	printf("\n");
9558a1b9b6aSSam Leffler }
9568a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
957