xref: /freebsd/sys/net80211/ieee80211_input.c (revision 23f4fd6da409f9bca1ce839776e72e85baeee3e9)
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 */
253e1cfcbcbSSam Leffler 	m->m_flags &= ~(M_80211_RX | M_MCAST | M_BCAST);
254e1cfcbcbSSam Leffler 
255b032f27cSSam Leffler 	/* NB: see hostap_deliver_data, this path doesn't handle hostap */
256b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_HOSTAP, ("gack, hostap"));
25706efa2f0SSam Leffler 	/*
25806efa2f0SSam Leffler 	 * Do accounting.
25906efa2f0SSam Leffler 	 */
26006efa2f0SSam Leffler 	ifp->if_ipackets++;
26106efa2f0SSam Leffler 	IEEE80211_NODE_STAT(ni, rx_data);
26206efa2f0SSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
26306efa2f0SSam Leffler 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
26406efa2f0SSam Leffler 		m->m_flags |= M_MCAST;		/* XXX M_BCAST? */
26506efa2f0SSam Leffler 		IEEE80211_NODE_STAT(ni, rx_mcast);
26606efa2f0SSam Leffler 	} else
26706efa2f0SSam Leffler 		IEEE80211_NODE_STAT(ni, rx_ucast);
268b032f27cSSam Leffler 	m->m_pkthdr.rcvif = ifp;
26906efa2f0SSam Leffler 
2701bd482efSSam Leffler 	if (ni->ni_vlan != 0) {
2711bd482efSSam Leffler 		/* attach vlan tag */
27278ba57b9SAndre Oppermann 		m->m_pkthdr.ether_vtag = ni->ni_vlan;
27378ba57b9SAndre Oppermann 		m->m_flags |= M_VLANTAG;
2741bd482efSSam Leffler 	}
275b032f27cSSam Leffler 	ifp->if_input(ifp, m);
2761bd482efSSam Leffler }
2771bd482efSSam Leffler 
278b032f27cSSam Leffler struct mbuf *
279b032f27cSSam Leffler ieee80211_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen)
2808a1b9b6aSSam Leffler {
28159aa14a9SRui Paulo 	struct ieee80211_qosframe_addr4 wh;
2821a1e1d21SSam Leffler 	struct ether_header *eh;
2831a1e1d21SSam Leffler 	struct llc *llc;
2841a1e1d21SSam Leffler 
285c104cff2SRui Paulo 	KASSERT(hdrlen <= sizeof(wh),
286c104cff2SRui Paulo 	    ("hdrlen %d > max %zd", hdrlen, sizeof(wh)));
287c104cff2SRui Paulo 
2882cc12adeSSam Leffler 	if (m->m_len < hdrlen + sizeof(*llc) &&
2892cc12adeSSam Leffler 	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
290c104cff2SRui Paulo 		vap->iv_stats.is_rx_tooshort++;
291c104cff2SRui Paulo 		/* XXX msg */
2921a1e1d21SSam Leffler 		return NULL;
2931a1e1d21SSam Leffler 	}
2942cc12adeSSam Leffler 	memcpy(&wh, mtod(m, caddr_t), hdrlen);
2952cc12adeSSam Leffler 	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
2961a1e1d21SSam Leffler 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
2971a1e1d21SSam Leffler 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
2986bbdc701SSam Leffler 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
2996bbdc701SSam Leffler 	    /* NB: preserve AppleTalk frames that have a native SNAP hdr */
3006bbdc701SSam Leffler 	    !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
3016bbdc701SSam Leffler 	      llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
302ab96db10SSam Leffler 		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
3031a1e1d21SSam Leffler 		llc = NULL;
3041a1e1d21SSam Leffler 	} else {
3052cc12adeSSam Leffler 		m_adj(m, hdrlen - sizeof(*eh));
3061a1e1d21SSam Leffler 	}
3071a1e1d21SSam Leffler 	eh = mtod(m, struct ether_header *);
3081a1e1d21SSam Leffler 	switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
3091a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_NODS:
3101a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
3111a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
3121a1e1d21SSam Leffler 		break;
3131a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_TODS:
3141a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
3151a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
3161a1e1d21SSam Leffler 		break;
3171a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
3181a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
3191a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3);
3201a1e1d21SSam Leffler 		break;
3211a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
3222cc12adeSSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
3232cc12adeSSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr4);
3242cc12adeSSam Leffler 		break;
3251a1e1d21SSam Leffler 	}
326*23f4fd6dSGleb Smirnoff #ifndef __NO_STRICT_ALIGNMENT
32768e8e04eSSam Leffler 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
328519f677aSSam Leffler 		m = ieee80211_realign(vap, m, sizeof(*eh));
329519f677aSSam Leffler 		if (m == NULL)
3301a1e1d21SSam Leffler 			return NULL;
3311a1e1d21SSam Leffler 	}
332*23f4fd6dSGleb Smirnoff #endif /* !__NO_STRICT_ALIGNMENT */
3331a1e1d21SSam Leffler 	if (llc != NULL) {
3341a1e1d21SSam Leffler 		eh = mtod(m, struct ether_header *);
3351a1e1d21SSam Leffler 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
3361a1e1d21SSam Leffler 	}
3371a1e1d21SSam Leffler 	return m;
3381a1e1d21SSam Leffler }
3391a1e1d21SSam Leffler 
3401a1e1d21SSam Leffler /*
34168e8e04eSSam Leffler  * Decap a frame encapsulated in a fast-frame/A-MSDU.
34268e8e04eSSam Leffler  */
34368e8e04eSSam Leffler struct mbuf *
34468e8e04eSSam Leffler ieee80211_decap1(struct mbuf *m, int *framelen)
34568e8e04eSSam Leffler {
34668e8e04eSSam Leffler #define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
34768e8e04eSSam Leffler 	struct ether_header *eh;
34868e8e04eSSam Leffler 	struct llc *llc;
34968e8e04eSSam Leffler 
35068e8e04eSSam Leffler 	/*
35168e8e04eSSam Leffler 	 * The frame has an 802.3 header followed by an 802.2
35268e8e04eSSam Leffler 	 * LLC header.  The encapsulated frame length is in the
35368e8e04eSSam Leffler 	 * first header type field; save that and overwrite it
35468e8e04eSSam Leffler 	 * with the true type field found in the second.  Then
35568e8e04eSSam Leffler 	 * copy the 802.3 header up to where it belongs and
35668e8e04eSSam Leffler 	 * adjust the mbuf contents to remove the void.
35768e8e04eSSam Leffler 	 */
35868e8e04eSSam Leffler 	if (m->m_len < FF_LLC_SIZE && (m = m_pullup(m, FF_LLC_SIZE)) == NULL)
35968e8e04eSSam Leffler 		return NULL;
36068e8e04eSSam Leffler 	eh = mtod(m, struct ether_header *);	/* 802.3 header is first */
36168e8e04eSSam Leffler 	llc = (struct llc *)&eh[1];		/* 802.2 header follows */
36268e8e04eSSam Leffler 	*framelen = ntohs(eh->ether_type)	/* encap'd frame size */
36368e8e04eSSam Leffler 		  + sizeof(struct ether_header) - sizeof(struct llc);
36468e8e04eSSam Leffler 	eh->ether_type = llc->llc_un.type_snap.ether_type;
36568e8e04eSSam Leffler 	ovbcopy(eh, mtod(m, uint8_t *) + sizeof(struct llc),
36668e8e04eSSam Leffler 		sizeof(struct ether_header));
36768e8e04eSSam Leffler 	m_adj(m, sizeof(struct llc));
36868e8e04eSSam Leffler 	return m;
36968e8e04eSSam Leffler #undef FF_LLC_SIZE
37068e8e04eSSam Leffler }
37168e8e04eSSam Leffler 
37268e8e04eSSam Leffler /*
3731a1e1d21SSam Leffler  * Install received rate set information in the node's state block.
3741a1e1d21SSam Leffler  */
3757d77cd53SSam Leffler int
3767d77cd53SSam Leffler ieee80211_setup_rates(struct ieee80211_node *ni,
37768e8e04eSSam Leffler 	const uint8_t *rates, const uint8_t *xrates, int flags)
3781a1e1d21SSam Leffler {
379b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3801a1e1d21SSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
3811a1e1d21SSam Leffler 
3821a1e1d21SSam Leffler 	memset(rs, 0, sizeof(*rs));
3831a1e1d21SSam Leffler 	rs->rs_nrates = rates[1];
3841a1e1d21SSam Leffler 	memcpy(rs->rs_rates, rates + 2, rs->rs_nrates);
3851a1e1d21SSam Leffler 	if (xrates != NULL) {
38668e8e04eSSam Leffler 		uint8_t nxrates;
3871a1e1d21SSam Leffler 		/*
3881a1e1d21SSam Leffler 		 * Tack on 11g extended supported rate element.
3891a1e1d21SSam Leffler 		 */
3901a1e1d21SSam Leffler 		nxrates = xrates[1];
3911a1e1d21SSam Leffler 		if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
3921a1e1d21SSam Leffler 			nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates;
393b032f27cSSam Leffler 			IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE, ni,
394b032f27cSSam Leffler 			    "extended rate set too large; only using "
395b032f27cSSam Leffler 			    "%u of %u rates", nxrates, xrates[1]);
396b032f27cSSam Leffler 			vap->iv_stats.is_rx_rstoobig++;
3971a1e1d21SSam Leffler 		}
3981a1e1d21SSam Leffler 		memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates);
3991a1e1d21SSam Leffler 		rs->rs_nrates += nxrates;
4001a1e1d21SSam Leffler 	}
40170e28b9aSSam Leffler 	return ieee80211_fix_rate(ni, rs, flags);
4021a1e1d21SSam Leffler }
4031a1e1d21SSam Leffler 
40484eb84c4SSam Leffler /*
40584eb84c4SSam Leffler  * Send a management frame error response to the specified
40684eb84c4SSam Leffler  * station.  If ni is associated with the station then use
40784eb84c4SSam Leffler  * it; otherwise allocate a temporary node suitable for
40884eb84c4SSam Leffler  * transmitting the frame and then free the reference so
40984eb84c4SSam Leffler  * it will go away as soon as the frame has been transmitted.
41084eb84c4SSam Leffler  */
411b032f27cSSam Leffler void
412b032f27cSSam Leffler ieee80211_send_error(struct ieee80211_node *ni,
413b032f27cSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN], int subtype, int arg)
41484eb84c4SSam Leffler {
415b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
41684eb84c4SSam Leffler 	int istmp;
41784eb84c4SSam Leffler 
418b032f27cSSam Leffler 	if (ni == vap->iv_bss) {
419b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN) {
420b032f27cSSam Leffler 			/*
421b032f27cSSam Leffler 			 * XXX hack until we get rid of this routine.
422b032f27cSSam Leffler 			 * We can be called prior to the vap reaching
423b032f27cSSam Leffler 			 * run state under certain conditions in which
424b032f27cSSam Leffler 			 * case iv_bss->ni_chan will not be setup.
425b032f27cSSam Leffler 			 * Check for this explicitly and and just ignore
426b032f27cSSam Leffler 			 * the request.
427b032f27cSSam Leffler 			 */
428b032f27cSSam Leffler 			return;
429b032f27cSSam Leffler 		}
430b032f27cSSam Leffler 		ni = ieee80211_tmp_node(vap, mac);
43184eb84c4SSam Leffler 		if (ni == NULL) {
43284eb84c4SSam Leffler 			/* XXX msg */
43384eb84c4SSam Leffler 			return;
43484eb84c4SSam Leffler 		}
43584eb84c4SSam Leffler 		istmp = 1;
43684eb84c4SSam Leffler 	} else
43784eb84c4SSam Leffler 		istmp = 0;
438b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, subtype, arg);
43984eb84c4SSam Leffler 	if (istmp)
44084eb84c4SSam Leffler 		ieee80211_free_node(ni);
44184eb84c4SSam Leffler }
44284eb84c4SSam Leffler 
443b032f27cSSam Leffler int
444b032f27cSSam Leffler ieee80211_alloc_challenge(struct ieee80211_node *ni)
4458a1b9b6aSSam Leffler {
4468a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL)
447e2126decSSam Leffler 		ni->ni_challenge = (uint32_t *) malloc(IEEE80211_CHALLENGE_LEN,
44868e8e04eSSam Leffler 		    M_80211_NODE, M_NOWAIT);
4498a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL) {
450b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap,
451b032f27cSSam Leffler 		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni,
452b032f27cSSam Leffler 		    "%s", "shared key challenge alloc failed");
4538a1b9b6aSSam Leffler 		/* XXX statistic */
4548a1b9b6aSSam Leffler 	}
4558a1b9b6aSSam Leffler 	return (ni->ni_challenge != NULL);
4568a1b9b6aSSam Leffler }
4578a1b9b6aSSam Leffler 
45866ef3969SSam Leffler /*
459b032f27cSSam Leffler  * Parse a Beacon or ProbeResponse frame and return the
460b032f27cSSam Leffler  * useful information in an ieee80211_scanparams structure.
461b032f27cSSam Leffler  * Status is set to 0 if no problems were found; otherwise
462b032f27cSSam Leffler  * a bitmask of IEEE80211_BPARSE_* items is returned that
463b032f27cSSam Leffler  * describes the problems detected.
46466ef3969SSam Leffler  */
465b032f27cSSam Leffler int
466b032f27cSSam Leffler ieee80211_parse_beacon(struct ieee80211_node *ni, struct mbuf *m,
467b032f27cSSam Leffler 	struct ieee80211_scanparams *scan)
46866ef3969SSam Leffler {
469b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
47066ef3969SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
4711a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
47268e8e04eSSam Leffler 	uint8_t *frm, *efrm;
4731a1e1d21SSam Leffler 
474b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
47568e8e04eSSam Leffler 	frm = (uint8_t *)&wh[1];
476b032f27cSSam Leffler 	efrm = mtod(m, uint8_t *) + m->m_len;
477b032f27cSSam Leffler 	scan->status = 0;
4781a1e1d21SSam Leffler 	/*
4791a1e1d21SSam Leffler 	 * beacon/probe response frame format
4801a1e1d21SSam Leffler 	 *	[8] time stamp
4811a1e1d21SSam Leffler 	 *	[2] beacon interval
4821a1e1d21SSam Leffler 	 *	[2] capability information
4831a1e1d21SSam Leffler 	 *	[tlv] ssid
4841a1e1d21SSam Leffler 	 *	[tlv] supported rates
4851a1e1d21SSam Leffler 	 *	[tlv] country information
486c70761e6SSam Leffler 	 *	[tlv] channel switch announcement (CSA)
4871a1e1d21SSam Leffler 	 *	[tlv] parameter set (FH/DS)
4881a1e1d21SSam Leffler 	 *	[tlv] erp information
4891a1e1d21SSam Leffler 	 *	[tlv] extended supported rates
4908a1b9b6aSSam Leffler 	 *	[tlv] WME
4918a1b9b6aSSam Leffler 	 *	[tlv] WPA or RSN
49268e8e04eSSam Leffler 	 *	[tlv] HT capabilities
49368e8e04eSSam Leffler 	 *	[tlv] HT information
49468e8e04eSSam Leffler 	 *	[tlv] Atheros capabilities
49559aa14a9SRui Paulo 	 *	[tlv] Mesh ID
49659aa14a9SRui Paulo 	 *	[tlv] Mesh Configuration
4971a1e1d21SSam Leffler 	 */
498b032f27cSSam Leffler 	IEEE80211_VERIFY_LENGTH(efrm - frm, 12,
499b032f27cSSam Leffler 	    return (scan->status = IEEE80211_BPARSE_BADIELEN));
500b032f27cSSam Leffler 	memset(scan, 0, sizeof(*scan));
501b032f27cSSam Leffler 	scan->tstamp  = frm;				frm += 8;
502b032f27cSSam Leffler 	scan->bintval = le16toh(*(uint16_t *)frm);	frm += 2;
503b032f27cSSam Leffler 	scan->capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
504b032f27cSSam Leffler 	scan->bchan = ieee80211_chan2ieee(ic, ic->ic_curchan);
505b032f27cSSam Leffler 	scan->chan = scan->bchan;
506b032f27cSSam Leffler 	scan->ies = frm;
507b032f27cSSam Leffler 	scan->ies_len = efrm - frm;
508b5c99415SSam Leffler 
50970326a6eSSam Leffler 	while (efrm - frm > 1) {
510b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2,
511b032f27cSSam Leffler 		    return (scan->status = IEEE80211_BPARSE_BADIELEN));
5121a1e1d21SSam Leffler 		switch (*frm) {
5131a1e1d21SSam Leffler 		case IEEE80211_ELEMID_SSID:
514b032f27cSSam Leffler 			scan->ssid = frm;
5151a1e1d21SSam Leffler 			break;
5161a1e1d21SSam Leffler 		case IEEE80211_ELEMID_RATES:
517b032f27cSSam Leffler 			scan->rates = frm;
5181a1e1d21SSam Leffler 			break;
5191a1e1d21SSam Leffler 		case IEEE80211_ELEMID_COUNTRY:
520b032f27cSSam Leffler 			scan->country = frm;
5211a1e1d21SSam Leffler 			break;
522c70761e6SSam Leffler 		case IEEE80211_ELEMID_CSA:
523c70761e6SSam Leffler 			scan->csa = frm;
524c70761e6SSam Leffler 			break;
52532b0e64bSAdrian Chadd 		case IEEE80211_ELEMID_QUIET:
52632b0e64bSAdrian Chadd 			scan->quiet = frm;
52732b0e64bSAdrian Chadd 			break;
5281a1e1d21SSam Leffler 		case IEEE80211_ELEMID_FHPARMS:
5291a1e1d21SSam Leffler 			if (ic->ic_phytype == IEEE80211_T_FH) {
530b032f27cSSam Leffler 				scan->fhdwell = LE_READ_2(&frm[2]);
531b032f27cSSam Leffler 				scan->chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
532b032f27cSSam Leffler 				scan->fhindex = frm[6];
5331a1e1d21SSam Leffler 			}
5341a1e1d21SSam Leffler 			break;
5351a1e1d21SSam Leffler 		case IEEE80211_ELEMID_DSPARMS:
5361a1e1d21SSam Leffler 			/*
5371a1e1d21SSam Leffler 			 * XXX hack this since depending on phytype
5381a1e1d21SSam Leffler 			 * is problematic for multi-mode devices.
5391a1e1d21SSam Leffler 			 */
5401a1e1d21SSam Leffler 			if (ic->ic_phytype != IEEE80211_T_FH)
541b032f27cSSam Leffler 				scan->chan = frm[2];
5421a1e1d21SSam Leffler 			break;
5431a1e1d21SSam Leffler 		case IEEE80211_ELEMID_TIM:
5448a1b9b6aSSam Leffler 			/* XXX ATIM? */
545b032f27cSSam Leffler 			scan->tim = frm;
546b032f27cSSam Leffler 			scan->timoff = frm - mtod(m, uint8_t *);
5471a1e1d21SSam Leffler 			break;
5484bd067c5SSam Leffler 		case IEEE80211_ELEMID_IBSSPARMS:
549b032f27cSSam Leffler 		case IEEE80211_ELEMID_CFPARMS:
550643024a2SSam Leffler 		case IEEE80211_ELEMID_PWRCNSTR:
551b032f27cSSam Leffler 			/* NB: avoid debugging complaints */
5524bd067c5SSam Leffler 			break;
5531a1e1d21SSam Leffler 		case IEEE80211_ELEMID_XRATES:
554b032f27cSSam Leffler 			scan->xrates = frm;
5551a1e1d21SSam Leffler 			break;
5561a1e1d21SSam Leffler 		case IEEE80211_ELEMID_ERP:
5571a1e1d21SSam Leffler 			if (frm[1] != 1) {
558b032f27cSSam Leffler 				IEEE80211_DISCARD_IE(vap,
5598a1b9b6aSSam Leffler 				    IEEE80211_MSG_ELEMID, wh, "ERP",
5608a1b9b6aSSam Leffler 				    "bad len %u", frm[1]);
561b032f27cSSam Leffler 				vap->iv_stats.is_rx_elem_toobig++;
5621a1e1d21SSam Leffler 				break;
5631a1e1d21SSam Leffler 			}
564b032f27cSSam Leffler 			scan->erp = frm[2] | 0x100;
5651a1e1d21SSam Leffler 			break;
56668e8e04eSSam Leffler 		case IEEE80211_ELEMID_HTCAP:
567b032f27cSSam Leffler 			scan->htcap = frm;
56868e8e04eSSam Leffler 			break;
5698a1b9b6aSSam Leffler 		case IEEE80211_ELEMID_RSN:
570b032f27cSSam Leffler 			scan->rsn = frm;
57168e8e04eSSam Leffler 			break;
57268e8e04eSSam Leffler 		case IEEE80211_ELEMID_HTINFO:
573b032f27cSSam Leffler 			scan->htinfo = frm;
5748a1b9b6aSSam Leffler 			break;
575fbe007daSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
57659aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHID:
57759aa14a9SRui Paulo 			scan->meshid = frm;
57859aa14a9SRui Paulo 			break;
57959aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHCONF:
58059aa14a9SRui Paulo 			scan->meshconf = frm;
58159aa14a9SRui Paulo 			break;
58259aa14a9SRui Paulo #endif
5838a1b9b6aSSam Leffler 		case IEEE80211_ELEMID_VENDOR:
5848a1b9b6aSSam Leffler 			if (iswpaoui(frm))
585b032f27cSSam Leffler 				scan->wpa = frm;
5868a1b9b6aSSam Leffler 			else if (iswmeparam(frm) || iswmeinfo(frm))
587b032f27cSSam Leffler 				scan->wme = frm;
588616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
58968e8e04eSSam Leffler 			else if (isatherosoui(frm))
590b032f27cSSam Leffler 				scan->ath = frm;
591616190d0SSam Leffler #endif
59210ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
59310ad9a77SSam Leffler 			else if (istdmaoui(frm))
59410ad9a77SSam Leffler 				scan->tdma = frm;
59510ad9a77SSam Leffler #endif
5962bfc8a91SSam Leffler 			else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
59768e8e04eSSam Leffler 				/*
59868e8e04eSSam Leffler 				 * Accept pre-draft HT ie's if the
59968e8e04eSSam Leffler 				 * standard ones have not been seen.
60068e8e04eSSam Leffler 				 */
60168e8e04eSSam Leffler 				if (ishtcapoui(frm)) {
602b032f27cSSam Leffler 					if (scan->htcap == NULL)
603b032f27cSSam Leffler 						scan->htcap = frm;
60468e8e04eSSam Leffler 				} else if (ishtinfooui(frm)) {
605b032f27cSSam Leffler 					if (scan->htinfo == NULL)
606b032f27cSSam Leffler 						scan->htcap = frm;
60768e8e04eSSam Leffler 				}
60868e8e04eSSam Leffler 			}
6098a1b9b6aSSam Leffler 			break;
6101a1e1d21SSam Leffler 		default:
611b032f27cSSam Leffler 			IEEE80211_DISCARD_IE(vap, IEEE80211_MSG_ELEMID,
6128a1b9b6aSSam Leffler 			    wh, "unhandled",
6138a1b9b6aSSam Leffler 			    "id %u, len %u", *frm, frm[1]);
614b032f27cSSam Leffler 			vap->iv_stats.is_rx_elem_unknown++;
6151a1e1d21SSam Leffler 			break;
6161a1e1d21SSam Leffler 		}
6171a1e1d21SSam Leffler 		frm += frm[1] + 2;
6181a1e1d21SSam Leffler 	}
619b032f27cSSam Leffler 	IEEE80211_VERIFY_ELEMENT(scan->rates, IEEE80211_RATE_MAXSIZE,
620b032f27cSSam Leffler 	    scan->status |= IEEE80211_BPARSE_RATES_INVALID);
621b032f27cSSam Leffler 	if (scan->rates != NULL && scan->xrates != NULL) {
622b032f27cSSam Leffler 		/*
623b032f27cSSam Leffler 		 * NB: don't process XRATES if RATES is missing.  This
624b032f27cSSam Leffler 		 * avoids a potential null ptr deref and should be ok
625b032f27cSSam Leffler 		 * as the return code will already note RATES is missing
626b032f27cSSam Leffler 		 * (so callers shouldn't otherwise process the frame).
627b032f27cSSam Leffler 		 */
628b032f27cSSam Leffler 		IEEE80211_VERIFY_ELEMENT(scan->xrates,
629b032f27cSSam Leffler 		    IEEE80211_RATE_MAXSIZE - scan->rates[1],
630b032f27cSSam Leffler 		    scan->status |= IEEE80211_BPARSE_XRATES_INVALID);
631b032f27cSSam Leffler 	}
632b032f27cSSam Leffler 	IEEE80211_VERIFY_ELEMENT(scan->ssid, IEEE80211_NWID_LEN,
633b032f27cSSam Leffler 	    scan->status |= IEEE80211_BPARSE_SSID_INVALID);
634b032f27cSSam Leffler 	if (scan->chan != scan->bchan && ic->ic_phytype != IEEE80211_T_FH) {
6351a1e1d21SSam Leffler 		/*
6361a1e1d21SSam Leffler 		 * Frame was received on a channel different from the
6374844aa7dSAtsushi Onoe 		 * one indicated in the DS params element id;
6381a1e1d21SSam Leffler 		 * silently discard it.
6391a1e1d21SSam Leffler 		 *
6401a1e1d21SSam Leffler 		 * NB: this can happen due to signal leakage.
6414844aa7dSAtsushi Onoe 		 *     But we should take it for FH phy because
6424844aa7dSAtsushi Onoe 		 *     the rssi value should be correct even for
6434844aa7dSAtsushi Onoe 		 *     different hop pattern in FH.
6441a1e1d21SSam Leffler 		 */
645b032f27cSSam Leffler 		IEEE80211_DISCARD(vap,
646d365f9c7SSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
647b032f27cSSam Leffler 		    wh, NULL, "for off-channel %u", scan->chan);
648b032f27cSSam Leffler 		vap->iv_stats.is_rx_chanmismatch++;
649b032f27cSSam Leffler 		scan->status |= IEEE80211_BPARSE_OFFCHAN;
650b5c99415SSam Leffler 	}
651b032f27cSSam Leffler 	if (!(IEEE80211_BINTVAL_MIN <= scan->bintval &&
652b032f27cSSam Leffler 	      scan->bintval <= IEEE80211_BINTVAL_MAX)) {
653b032f27cSSam Leffler 		IEEE80211_DISCARD(vap,
654b5c99415SSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
655de681822SAdrian Chadd 		    wh, NULL, "bogus beacon interval (%d TU)",
656de681822SAdrian Chadd 		    (int) scan->bintval);
657b032f27cSSam Leffler 		vap->iv_stats.is_rx_badbintval++;
658b032f27cSSam Leffler 		scan->status |= IEEE80211_BPARSE_BINTVAL_INVALID;
659b032f27cSSam Leffler 	}
660b032f27cSSam Leffler 	if (scan->country != NULL) {
661b032f27cSSam Leffler 		/*
662b032f27cSSam Leffler 		 * Validate we have at least enough data to extract
663b032f27cSSam Leffler 		 * the country code.  Not sure if we should return an
664b032f27cSSam Leffler 		 * error instead of discarding the IE; consider this
665b032f27cSSam Leffler 		 * being lenient as we don't depend on the data for
666b032f27cSSam Leffler 		 * correct operation.
667b032f27cSSam Leffler 		 */
668b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(scan->country[1], 3 * sizeof(uint8_t),
669b032f27cSSam Leffler 		    scan->country = NULL);
670d365f9c7SSam Leffler 	}
671c70761e6SSam Leffler 	if (scan->csa != NULL) {
672c70761e6SSam Leffler 		/*
673c70761e6SSam Leffler 		 * Validate Channel Switch Announcement; this must
674c70761e6SSam Leffler 		 * be the correct length or we toss the frame.
675c70761e6SSam Leffler 		 */
676c70761e6SSam Leffler 		IEEE80211_VERIFY_LENGTH(scan->csa[1], 3 * sizeof(uint8_t),
677c70761e6SSam Leffler 		    scan->status |= IEEE80211_BPARSE_CSA_INVALID);
678c70761e6SSam Leffler 	}
67968e8e04eSSam Leffler 	/*
68068e8e04eSSam Leffler 	 * Process HT ie's.  This is complicated by our
68168e8e04eSSam Leffler 	 * accepting both the standard ie's and the pre-draft
68268e8e04eSSam Leffler 	 * vendor OUI ie's that some vendors still use/require.
68368e8e04eSSam Leffler 	 */
684b032f27cSSam Leffler 	if (scan->htcap != NULL) {
685b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(scan->htcap[1],
686b032f27cSSam Leffler 		     scan->htcap[0] == IEEE80211_ELEMID_VENDOR ?
68768e8e04eSSam Leffler 			 4 + sizeof(struct ieee80211_ie_htcap)-2 :
68868e8e04eSSam Leffler 			 sizeof(struct ieee80211_ie_htcap)-2,
689b032f27cSSam Leffler 		     scan->htcap = NULL);
69068e8e04eSSam Leffler 	}
691b032f27cSSam Leffler 	if (scan->htinfo != NULL) {
692b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(scan->htinfo[1],
693b032f27cSSam Leffler 		     scan->htinfo[0] == IEEE80211_ELEMID_VENDOR ?
69468e8e04eSSam Leffler 			 4 + sizeof(struct ieee80211_ie_htinfo)-2 :
69568e8e04eSSam Leffler 			 sizeof(struct ieee80211_ie_htinfo)-2,
696b032f27cSSam Leffler 		     scan->htinfo = NULL);
697b032f27cSSam Leffler 	}
698b032f27cSSam Leffler 	return scan->status;
69968e8e04eSSam Leffler }
7001a1e1d21SSam Leffler 
7011a1e1d21SSam Leffler /*
702b032f27cSSam Leffler  * Parse an Action frame.  Return 0 on success, non-zero on failure.
70344c72e42SSam Leffler  */
704b032f27cSSam Leffler int
705b032f27cSSam Leffler ieee80211_parse_action(struct ieee80211_node *ni, struct mbuf *m)
706b032f27cSSam Leffler {
707b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
70868e8e04eSSam Leffler 	const struct ieee80211_action *ia;
709b032f27cSSam Leffler 	struct ieee80211_frame *wh;
710b032f27cSSam Leffler 	uint8_t *frm, *efrm;
71168e8e04eSSam Leffler 
71268e8e04eSSam Leffler 	/*
71368e8e04eSSam Leffler 	 * action frame format:
71468e8e04eSSam Leffler 	 *	[1] category
71568e8e04eSSam Leffler 	 *	[1] action
71668e8e04eSSam Leffler 	 *	[tlv] parameters
71768e8e04eSSam Leffler 	 */
718b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
719b032f27cSSam Leffler 	frm = (u_int8_t *)&wh[1];
720b032f27cSSam Leffler 	efrm = mtod(m, u_int8_t *) + m->m_len;
72168e8e04eSSam Leffler 	IEEE80211_VERIFY_LENGTH(efrm - frm,
722b032f27cSSam Leffler 		sizeof(struct ieee80211_action), return EINVAL);
72368e8e04eSSam Leffler 	ia = (const struct ieee80211_action *) frm;
72468e8e04eSSam Leffler 
725b032f27cSSam Leffler 	vap->iv_stats.is_rx_action++;
72668e8e04eSSam Leffler 	IEEE80211_NODE_STAT(ni, rx_action);
72768e8e04eSSam Leffler 
72868e8e04eSSam Leffler 	/* verify frame payloads but defer processing */
72968e8e04eSSam Leffler 	switch (ia->ia_category) {
73068e8e04eSSam Leffler 	case IEEE80211_ACTION_CAT_BA:
73168e8e04eSSam Leffler 		switch (ia->ia_action) {
73268e8e04eSSam Leffler 		case IEEE80211_ACTION_BA_ADDBA_REQUEST:
73368e8e04eSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
73468e8e04eSSam Leffler 			    sizeof(struct ieee80211_action_ba_addbarequest),
735b032f27cSSam Leffler 			    return EINVAL);
73668e8e04eSSam Leffler 			break;
73768e8e04eSSam Leffler 		case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
73868e8e04eSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
73968e8e04eSSam Leffler 			    sizeof(struct ieee80211_action_ba_addbaresponse),
740b032f27cSSam Leffler 			    return EINVAL);
74168e8e04eSSam Leffler 			break;
74268e8e04eSSam Leffler 		case IEEE80211_ACTION_BA_DELBA:
74368e8e04eSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
74468e8e04eSSam Leffler 			    sizeof(struct ieee80211_action_ba_delba),
745b032f27cSSam Leffler 			    return EINVAL);
74668e8e04eSSam Leffler 			break;
74768e8e04eSSam Leffler 		}
74868e8e04eSSam Leffler 		break;
74968e8e04eSSam Leffler 	case IEEE80211_ACTION_CAT_HT:
75068e8e04eSSam Leffler 		switch (ia->ia_action) {
75168e8e04eSSam Leffler 		case IEEE80211_ACTION_HT_TXCHWIDTH:
75268e8e04eSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
75368e8e04eSSam Leffler 			    sizeof(struct ieee80211_action_ht_txchwidth),
754b032f27cSSam Leffler 			    return EINVAL);
755b032f27cSSam Leffler 			break;
756b032f27cSSam Leffler 		case IEEE80211_ACTION_HT_MIMOPWRSAVE:
757b032f27cSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
758b032f27cSSam Leffler 			    sizeof(struct ieee80211_action_ht_mimopowersave),
759b032f27cSSam Leffler 			    return EINVAL);
76068e8e04eSSam Leffler 			break;
76168e8e04eSSam Leffler 		}
76268e8e04eSSam Leffler 		break;
763dbab732dSGleb Smirnoff #ifdef IEEE80211_SUPPORT_MESH
764bdd2a076SAdrian Chadd 	case IEEE80211_ACTION_CAT_MESH:
765bdd2a076SAdrian Chadd 		switch (ia->ia_action) {
766bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_LMETRIC:
767bdd2a076SAdrian Chadd 			/*
768bdd2a076SAdrian Chadd 			 * XXX: verification is true only if we are using
769bdd2a076SAdrian Chadd 			 * Airtime link metric (default)
770bdd2a076SAdrian Chadd 			 */
771bdd2a076SAdrian Chadd 			IEEE80211_VERIFY_LENGTH(efrm - frm,
772bdd2a076SAdrian Chadd 			    sizeof(struct ieee80211_meshlmetric_ie),
773bdd2a076SAdrian Chadd 			    return EINVAL);
774bdd2a076SAdrian Chadd 			break;
775bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_HWMP:
776bdd2a076SAdrian Chadd 			/* verify something */
777bdd2a076SAdrian Chadd 			break;
778bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_GANN:
779c81ceff7SMonthadar Al Jaberi 			IEEE80211_VERIFY_LENGTH(efrm - frm,
780c81ceff7SMonthadar Al Jaberi 			    sizeof(struct ieee80211_meshgann_ie),
781c81ceff7SMonthadar Al Jaberi 			    return EINVAL);
782c81ceff7SMonthadar Al Jaberi 			break;
783bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_CC:
784bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_MCCA_SREQ:
785bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_MCCA_SREP:
786bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_MCCA_AREQ:
787bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_MCCA_ADVER:
788bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_MCCA_TRDOWN:
789bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_TBTT_REQ:
790bdd2a076SAdrian Chadd 		case IEEE80211_ACTION_MESH_TBTT_RES:
791bdd2a076SAdrian Chadd 			/* reject these early on, not implemented */
792bdd2a076SAdrian Chadd 			IEEE80211_DISCARD(vap,
793bdd2a076SAdrian Chadd 			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
794bdd2a076SAdrian Chadd 			    wh, NULL, "not implemented yet, act=0x%02X",
795bdd2a076SAdrian Chadd 			    ia->ia_action);
796bdd2a076SAdrian Chadd 			return EINVAL;
797bdd2a076SAdrian Chadd 		}
798bdd2a076SAdrian Chadd 		break;
799ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_CAT_SELF_PROT:
800ebeaa1adSMonthadar Al Jaberi 		/* If TA or RA group address discard silently */
801ebeaa1adSMonthadar Al Jaberi 		if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
802ebeaa1adSMonthadar Al Jaberi 			IEEE80211_IS_MULTICAST(wh->i_addr2))
803ebeaa1adSMonthadar Al Jaberi 			return EINVAL;
804ebeaa1adSMonthadar Al Jaberi 		/*
805ebeaa1adSMonthadar Al Jaberi 		 * XXX: Should we verify complete length now or it is
806ebeaa1adSMonthadar Al Jaberi 		 * to varying in sizes?
807ebeaa1adSMonthadar Al Jaberi 		 */
808ebeaa1adSMonthadar Al Jaberi 		switch (ia->ia_action) {
809ebeaa1adSMonthadar Al Jaberi 		case IEEE80211_ACTION_MESHPEERING_CONFIRM:
810ebeaa1adSMonthadar Al Jaberi 		case IEEE80211_ACTION_MESHPEERING_CLOSE:
811ebeaa1adSMonthadar Al Jaberi 			/* is not a peering candidate (yet) */
812ebeaa1adSMonthadar Al Jaberi 			if (ni == vap->iv_bss)
813ebeaa1adSMonthadar Al Jaberi 				return EINVAL;
814ebeaa1adSMonthadar Al Jaberi 			break;
815ebeaa1adSMonthadar Al Jaberi 		}
816ebeaa1adSMonthadar Al Jaberi 		break;
817dbab732dSGleb Smirnoff #endif
81868e8e04eSSam Leffler 	}
819b032f27cSSam Leffler 	return 0;
8208a1b9b6aSSam Leffler }
8218a1b9b6aSSam Leffler 
8228a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
8238a1b9b6aSSam Leffler /*
8248a1b9b6aSSam Leffler  * Debugging support.
8258a1b9b6aSSam Leffler  */
826b032f27cSSam Leffler void
827b032f27cSSam Leffler ieee80211_ssid_mismatch(struct ieee80211vap *vap, const char *tag,
828b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN], uint8_t *ssid)
829b032f27cSSam Leffler {
830b032f27cSSam Leffler 	printf("[%s] discard %s frame, ssid mismatch: ",
831b032f27cSSam Leffler 		ether_sprintf(mac), tag);
832b032f27cSSam Leffler 	ieee80211_print_essid(ssid + 2, ssid[1]);
833b032f27cSSam Leffler 	printf("\n");
834b032f27cSSam Leffler }
8358a1b9b6aSSam Leffler 
8368a1b9b6aSSam Leffler /*
8378a1b9b6aSSam Leffler  * Return the bssid of a frame.
8388a1b9b6aSSam Leffler  */
83968e8e04eSSam Leffler static const uint8_t *
840d2bc4bf6SRui Paulo ieee80211_getbssid(const struct ieee80211vap *vap,
841d2bc4bf6SRui Paulo 	const struct ieee80211_frame *wh)
8428a1b9b6aSSam Leffler {
843b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA)
8448a1b9b6aSSam Leffler 		return wh->i_addr2;
8458a1b9b6aSSam Leffler 	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) != IEEE80211_FC1_DIR_NODS)
8468a1b9b6aSSam Leffler 		return wh->i_addr1;
8478a1b9b6aSSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
8488a1b9b6aSSam Leffler 		return wh->i_addr1;
8498a1b9b6aSSam Leffler 	return wh->i_addr3;
8508a1b9b6aSSam Leffler }
8518a1b9b6aSSam Leffler 
852b032f27cSSam Leffler #include <machine/stdarg.h>
853b032f27cSSam Leffler 
854f6df3191SSam Leffler void
855d2bc4bf6SRui Paulo ieee80211_note(const struct ieee80211vap *vap, const char *fmt, ...)
856f6df3191SSam Leffler {
857f6df3191SSam Leffler 	char buf[128];		/* XXX */
858f6df3191SSam Leffler 	va_list ap;
859f6df3191SSam Leffler 
860f6df3191SSam Leffler 	va_start(ap, fmt);
861f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
862f6df3191SSam Leffler 	va_end(ap);
863f6df3191SSam Leffler 
864b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "%s", buf);	/* NB: no \n */
865f6df3191SSam Leffler }
866f6df3191SSam Leffler 
867f6df3191SSam Leffler void
868d2bc4bf6SRui Paulo ieee80211_note_frame(const struct ieee80211vap *vap,
869f6df3191SSam Leffler 	const struct ieee80211_frame *wh,
870f6df3191SSam Leffler 	const char *fmt, ...)
871f6df3191SSam Leffler {
872f6df3191SSam Leffler 	char buf[128];		/* XXX */
873f6df3191SSam Leffler 	va_list ap;
874f6df3191SSam Leffler 
875f6df3191SSam Leffler 	va_start(ap, fmt);
876f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
877f6df3191SSam Leffler 	va_end(ap);
878b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] %s\n",
879b032f27cSSam Leffler 		ether_sprintf(ieee80211_getbssid(vap, wh)), buf);
880f6df3191SSam Leffler }
881f6df3191SSam Leffler 
882f6df3191SSam Leffler void
883d2bc4bf6SRui Paulo ieee80211_note_mac(const struct ieee80211vap *vap,
88468e8e04eSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN],
885f6df3191SSam Leffler 	const char *fmt, ...)
886f6df3191SSam Leffler {
887f6df3191SSam Leffler 	char buf[128];		/* XXX */
888f6df3191SSam Leffler 	va_list ap;
889f6df3191SSam Leffler 
890f6df3191SSam Leffler 	va_start(ap, fmt);
891f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
892f6df3191SSam Leffler 	va_end(ap);
893b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] %s\n", ether_sprintf(mac), buf);
894f6df3191SSam Leffler }
895f6df3191SSam Leffler 
896a000d7c2SSam Leffler void
897d2bc4bf6SRui Paulo ieee80211_discard_frame(const struct ieee80211vap *vap,
8988a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
8998a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
9008a1b9b6aSSam Leffler {
9018a1b9b6aSSam Leffler 	va_list ap;
9028a1b9b6aSSam Leffler 
903b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] discard ",
904b032f27cSSam Leffler 		ether_sprintf(ieee80211_getbssid(vap, wh)));
905b032f27cSSam Leffler 	if (type == NULL) {
906b032f27cSSam Leffler 		printf("%s frame, ", ieee80211_mgt_subtype_name[
907b032f27cSSam Leffler 			(wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) >>
908b032f27cSSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT]);
909b032f27cSSam Leffler 	} else
9108a1b9b6aSSam Leffler 		printf("%s frame, ", type);
9118a1b9b6aSSam Leffler 	va_start(ap, fmt);
9128a1b9b6aSSam Leffler 	vprintf(fmt, ap);
9138a1b9b6aSSam Leffler 	va_end(ap);
9148a1b9b6aSSam Leffler 	printf("\n");
9158a1b9b6aSSam Leffler }
9168a1b9b6aSSam Leffler 
917a000d7c2SSam Leffler void
918d2bc4bf6SRui Paulo ieee80211_discard_ie(const struct ieee80211vap *vap,
9198a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
9208a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
9218a1b9b6aSSam Leffler {
9228a1b9b6aSSam Leffler 	va_list ap;
9238a1b9b6aSSam Leffler 
924b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] discard ",
925b032f27cSSam Leffler 		ether_sprintf(ieee80211_getbssid(vap, wh)));
9268a1b9b6aSSam Leffler 	if (type != NULL)
9278a1b9b6aSSam Leffler 		printf("%s information element, ", type);
9288a1b9b6aSSam Leffler 	else
9298a1b9b6aSSam Leffler 		printf("information element, ");
9308a1b9b6aSSam Leffler 	va_start(ap, fmt);
9318a1b9b6aSSam Leffler 	vprintf(fmt, ap);
9328a1b9b6aSSam Leffler 	va_end(ap);
9338a1b9b6aSSam Leffler 	printf("\n");
9348a1b9b6aSSam Leffler }
9358a1b9b6aSSam Leffler 
936a000d7c2SSam Leffler void
937d2bc4bf6SRui Paulo ieee80211_discard_mac(const struct ieee80211vap *vap,
93868e8e04eSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN],
9398a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
9408a1b9b6aSSam Leffler {
9418a1b9b6aSSam Leffler 	va_list ap;
9428a1b9b6aSSam Leffler 
943b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] discard ", ether_sprintf(mac));
9448a1b9b6aSSam Leffler 	if (type != NULL)
9458a1b9b6aSSam Leffler 		printf("%s frame, ", type);
9468a1b9b6aSSam Leffler 	else
9478a1b9b6aSSam Leffler 		printf("frame, ");
9488a1b9b6aSSam Leffler 	va_start(ap, fmt);
9498a1b9b6aSSam Leffler 	vprintf(fmt, ap);
9508a1b9b6aSSam Leffler 	va_end(ap);
9518a1b9b6aSSam Leffler 	printf("\n");
9528a1b9b6aSSam Leffler }
9538a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
954