xref: /freebsd/sys/net80211/ieee80211_input.c (revision c104cff26ea3910047fb9c08ca58dc20539e8522)
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 
60b032f27cSSam Leffler int
615463c4a4SSam Leffler ieee80211_input_all(struct ieee80211com *ic, struct mbuf *m, int rssi, int nf)
62b032f27cSSam Leffler {
63b032f27cSSam Leffler 	struct ieee80211vap *vap;
64b032f27cSSam Leffler 	int type = -1;
65b032f27cSSam Leffler 
66e1cfcbcbSSam Leffler 	m->m_flags |= M_BCAST;		/* NB: mark for bpf tap'ing */
67e1cfcbcbSSam Leffler 
68b032f27cSSam Leffler 	/* XXX locking */
69b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
70b032f27cSSam Leffler 		struct ieee80211_node *ni;
71b032f27cSSam Leffler 		struct mbuf *mcopy;
72b032f27cSSam Leffler 
73c3655fa4SSam Leffler 		/* NB: could check for IFF_UP but this is cheaper */
74c3655fa4SSam Leffler 		if (vap->iv_state == IEEE80211_S_INIT)
75c3655fa4SSam Leffler 			continue;
76b032f27cSSam Leffler 		/*
77b032f27cSSam Leffler 		 * WDS vap's only receive directed traffic from the
78b032f27cSSam Leffler 		 * station at the ``far end''.  That traffic should
79b032f27cSSam Leffler 		 * be passed through the AP vap the station is associated
80b032f27cSSam Leffler 		 * to--so don't spam them with mcast frames.
81b032f27cSSam Leffler 		 */
82b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_WDS)
83b032f27cSSam Leffler 			continue;
84b032f27cSSam Leffler 		if (TAILQ_NEXT(vap, iv_next) != NULL) {
85b032f27cSSam Leffler 			/*
86b032f27cSSam Leffler 			 * Packet contents are changed by ieee80211_decap
87b032f27cSSam Leffler 			 * so do a deep copy of the packet.
88b032f27cSSam Leffler 			 */
89b032f27cSSam Leffler 			mcopy = m_dup(m, M_DONTWAIT);
90b032f27cSSam Leffler 			if (mcopy == NULL) {
91b032f27cSSam Leffler 				/* XXX stat+msg */
92b032f27cSSam Leffler 				continue;
938a1b9b6aSSam Leffler 			}
94b032f27cSSam Leffler 		} else {
95b032f27cSSam Leffler 			mcopy = m;
96b032f27cSSam Leffler 			m = NULL;
978a1b9b6aSSam Leffler 		}
98b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
995463c4a4SSam Leffler 		type = ieee80211_input(ni, mcopy, rssi, nf);
100b032f27cSSam Leffler 		ieee80211_free_node(ni);
1011a1e1d21SSam Leffler 	}
102b032f27cSSam Leffler 	if (m != NULL)			/* no vaps, reclaim mbuf */
1031a1e1d21SSam Leffler 		m_freem(m);
1041f298879SSam Leffler 	return type;
1051a1e1d21SSam Leffler }
1061a1e1d21SSam Leffler 
1078a1b9b6aSSam Leffler /*
108df0d214aSRui Paulo  * This function reassembles fragments.
109b032f27cSSam Leffler  *
110b032f27cSSam Leffler  * XXX should handle 3 concurrent reassemblies per-spec.
1118a1b9b6aSSam Leffler  */
112b032f27cSSam Leffler struct mbuf *
113b032f27cSSam Leffler ieee80211_defrag(struct ieee80211_node *ni, struct mbuf *m, int hdrspace)
1141a1e1d21SSam Leffler {
115b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1168a1b9b6aSSam Leffler 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
1178a1b9b6aSSam Leffler 	struct ieee80211_frame *lwh;
11868e8e04eSSam Leffler 	uint16_t rxseq;
11968e8e04eSSam Leffler 	uint8_t fragno;
12068e8e04eSSam Leffler 	uint8_t more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG;
1218a1b9b6aSSam Leffler 	struct mbuf *mfrag;
1228a1b9b6aSSam Leffler 
1238a1b9b6aSSam Leffler 	KASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1), ("multicast fragm?"));
1248a1b9b6aSSam Leffler 
12568e8e04eSSam Leffler 	rxseq = le16toh(*(uint16_t *)wh->i_seq);
1268a1b9b6aSSam Leffler 	fragno = rxseq & IEEE80211_SEQ_FRAG_MASK;
1278a1b9b6aSSam Leffler 
1288a1b9b6aSSam Leffler 	/* Quick way out, if there's nothing to defragment */
1298a1b9b6aSSam Leffler 	if (!more_frag && fragno == 0 && ni->ni_rxfrag[0] == NULL)
1308a1b9b6aSSam Leffler 		return m;
1318a1b9b6aSSam Leffler 
1328a1b9b6aSSam Leffler 	/*
1338a1b9b6aSSam Leffler 	 * Remove frag to insure it doesn't get reaped by timer.
1348a1b9b6aSSam Leffler 	 */
1358a1b9b6aSSam Leffler 	if (ni->ni_table == NULL) {
1368a1b9b6aSSam Leffler 		/*
1378a1b9b6aSSam Leffler 		 * Should never happen.  If the node is orphaned (not in
1388a1b9b6aSSam Leffler 		 * the table) then input packets should not reach here.
1398a1b9b6aSSam Leffler 		 * Otherwise, a concurrent request that yanks the table
1408a1b9b6aSSam Leffler 		 * should be blocked by other interlocking and/or by first
1418a1b9b6aSSam Leffler 		 * shutting the driver down.  Regardless, be defensive
1428a1b9b6aSSam Leffler 		 * here and just bail
1438a1b9b6aSSam Leffler 		 */
1448a1b9b6aSSam Leffler 		/* XXX need msg+stat */
1458a1b9b6aSSam Leffler 		m_freem(m);
1468a1b9b6aSSam Leffler 		return NULL;
1478a1b9b6aSSam Leffler 	}
1488a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(ni->ni_table);
1498a1b9b6aSSam Leffler 	mfrag = ni->ni_rxfrag[0];
1508a1b9b6aSSam Leffler 	ni->ni_rxfrag[0] = NULL;
1518a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(ni->ni_table);
1528a1b9b6aSSam Leffler 
1538a1b9b6aSSam Leffler 	/*
1548a1b9b6aSSam Leffler 	 * Validate new fragment is in order and
1558a1b9b6aSSam Leffler 	 * related to the previous ones.
1568a1b9b6aSSam Leffler 	 */
1578a1b9b6aSSam Leffler 	if (mfrag != NULL) {
15868e8e04eSSam Leffler 		uint16_t last_rxseq;
1598a1b9b6aSSam Leffler 
1608a1b9b6aSSam Leffler 		lwh = mtod(mfrag, struct ieee80211_frame *);
16168e8e04eSSam Leffler 		last_rxseq = le16toh(*(uint16_t *)lwh->i_seq);
1628a1b9b6aSSam Leffler 		/* NB: check seq # and frag together */
1638a1b9b6aSSam Leffler 		if (rxseq != last_rxseq+1 ||
1648a1b9b6aSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) ||
1658a1b9b6aSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) {
1668a1b9b6aSSam Leffler 			/*
1678a1b9b6aSSam Leffler 			 * Unrelated fragment or no space for it,
1688a1b9b6aSSam Leffler 			 * clear current fragments.
1698a1b9b6aSSam Leffler 			 */
1708a1b9b6aSSam Leffler 			m_freem(mfrag);
1718a1b9b6aSSam Leffler 			mfrag = NULL;
1728a1b9b6aSSam Leffler 		}
1738a1b9b6aSSam Leffler 	}
1748a1b9b6aSSam Leffler 
1758a1b9b6aSSam Leffler  	if (mfrag == NULL) {
1768a1b9b6aSSam Leffler 		if (fragno != 0) {		/* !first fragment, discard */
177b032f27cSSam Leffler 			vap->iv_stats.is_rx_defrag++;
1788a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_defrag);
1798a1b9b6aSSam Leffler 			m_freem(m);
1808a1b9b6aSSam Leffler 			return NULL;
1818a1b9b6aSSam Leffler 		}
1828a1b9b6aSSam Leffler 		mfrag = m;
1838a1b9b6aSSam Leffler 	} else {				/* concatenate */
1842cc12adeSSam Leffler 		m_adj(m, hdrspace);		/* strip header */
1858a1b9b6aSSam Leffler 		m_cat(mfrag, m);
1868a1b9b6aSSam Leffler 		/* NB: m_cat doesn't update the packet header */
1878a1b9b6aSSam Leffler 		mfrag->m_pkthdr.len += m->m_pkthdr.len;
1888a1b9b6aSSam Leffler 		/* track last seqnum and fragno */
1898a1b9b6aSSam Leffler 		lwh = mtod(mfrag, struct ieee80211_frame *);
19068e8e04eSSam Leffler 		*(uint16_t *) lwh->i_seq = *(uint16_t *) wh->i_seq;
1918a1b9b6aSSam Leffler 	}
1928a1b9b6aSSam Leffler 	if (more_frag) {			/* more to come, save */
193a0cc3f85SSam Leffler 		ni->ni_rxfragstamp = ticks;
1948a1b9b6aSSam Leffler 		ni->ni_rxfrag[0] = mfrag;
1958a1b9b6aSSam Leffler 		mfrag = NULL;
1968a1b9b6aSSam Leffler 	}
1978a1b9b6aSSam Leffler 	return mfrag;
1988a1b9b6aSSam Leffler }
1998a1b9b6aSSam Leffler 
20068e8e04eSSam Leffler void
201b032f27cSSam Leffler ieee80211_deliver_data(struct ieee80211vap *vap,
2021bd482efSSam Leffler 	struct ieee80211_node *ni, struct mbuf *m)
2031bd482efSSam Leffler {
2041bd482efSSam Leffler 	struct ether_header *eh = mtod(m, struct ether_header *);
205b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
2061bd482efSSam Leffler 
207e1cfcbcbSSam Leffler 	/* clear driver/net80211 flags before passing up */
208e1cfcbcbSSam Leffler 	m->m_flags &= ~(M_80211_RX | M_MCAST | M_BCAST);
209e1cfcbcbSSam Leffler 
210b032f27cSSam Leffler 	/* NB: see hostap_deliver_data, this path doesn't handle hostap */
211b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_HOSTAP, ("gack, hostap"));
21206efa2f0SSam Leffler 	/*
21306efa2f0SSam Leffler 	 * Do accounting.
21406efa2f0SSam Leffler 	 */
21506efa2f0SSam Leffler 	ifp->if_ipackets++;
21606efa2f0SSam Leffler 	IEEE80211_NODE_STAT(ni, rx_data);
21706efa2f0SSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
21806efa2f0SSam Leffler 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
21906efa2f0SSam Leffler 		m->m_flags |= M_MCAST;		/* XXX M_BCAST? */
22006efa2f0SSam Leffler 		IEEE80211_NODE_STAT(ni, rx_mcast);
22106efa2f0SSam Leffler 	} else
22206efa2f0SSam Leffler 		IEEE80211_NODE_STAT(ni, rx_ucast);
223b032f27cSSam Leffler 	m->m_pkthdr.rcvif = ifp;
22406efa2f0SSam Leffler 
2251bd482efSSam Leffler 	if (ni->ni_vlan != 0) {
2261bd482efSSam Leffler 		/* attach vlan tag */
22778ba57b9SAndre Oppermann 		m->m_pkthdr.ether_vtag = ni->ni_vlan;
22878ba57b9SAndre Oppermann 		m->m_flags |= M_VLANTAG;
2291bd482efSSam Leffler 	}
230b032f27cSSam Leffler 	ifp->if_input(ifp, m);
2311bd482efSSam Leffler }
2321bd482efSSam Leffler 
233b032f27cSSam Leffler struct mbuf *
234b032f27cSSam Leffler ieee80211_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen)
2358a1b9b6aSSam Leffler {
23659aa14a9SRui Paulo 	struct ieee80211_qosframe_addr4 wh;
2371a1e1d21SSam Leffler 	struct ether_header *eh;
2381a1e1d21SSam Leffler 	struct llc *llc;
2391a1e1d21SSam Leffler 
240c104cff2SRui Paulo 	KASSERT(hdrlen <= sizeof(wh),
241c104cff2SRui Paulo 	    ("hdrlen %d > max %zd", hdrlen, sizeof(wh)));
242c104cff2SRui Paulo 
2432cc12adeSSam Leffler 	if (m->m_len < hdrlen + sizeof(*llc) &&
2442cc12adeSSam Leffler 	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
245c104cff2SRui Paulo 		vap->iv_stats.is_rx_tooshort++;
246c104cff2SRui Paulo 		/* XXX msg */
2471a1e1d21SSam Leffler 		return NULL;
2481a1e1d21SSam Leffler 	}
2492cc12adeSSam Leffler 	memcpy(&wh, mtod(m, caddr_t), hdrlen);
2502cc12adeSSam Leffler 	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
2511a1e1d21SSam Leffler 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
2521a1e1d21SSam Leffler 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
2536bbdc701SSam Leffler 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
2546bbdc701SSam Leffler 	    /* NB: preserve AppleTalk frames that have a native SNAP hdr */
2556bbdc701SSam Leffler 	    !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
2566bbdc701SSam Leffler 	      llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
257ab96db10SSam Leffler 		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
2581a1e1d21SSam Leffler 		llc = NULL;
2591a1e1d21SSam Leffler 	} else {
2602cc12adeSSam Leffler 		m_adj(m, hdrlen - sizeof(*eh));
2611a1e1d21SSam Leffler 	}
2621a1e1d21SSam Leffler 	eh = mtod(m, struct ether_header *);
2631a1e1d21SSam Leffler 	switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
2641a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_NODS:
2651a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
2661a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
2671a1e1d21SSam Leffler 		break;
2681a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_TODS:
2691a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
2701a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
2711a1e1d21SSam Leffler 		break;
2721a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
2731a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
2741a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3);
2751a1e1d21SSam Leffler 		break;
2761a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
2772cc12adeSSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
2782cc12adeSSam Leffler 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr4);
2792cc12adeSSam Leffler 		break;
2801a1e1d21SSam Leffler 	}
2811a1e1d21SSam Leffler #ifdef ALIGNED_POINTER
28268e8e04eSSam Leffler 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
283519f677aSSam Leffler 		m = ieee80211_realign(vap, m, sizeof(*eh));
284519f677aSSam Leffler 		if (m == NULL)
2851a1e1d21SSam Leffler 			return NULL;
2861a1e1d21SSam Leffler 	}
2871a1e1d21SSam Leffler #endif /* ALIGNED_POINTER */
2881a1e1d21SSam Leffler 	if (llc != NULL) {
2891a1e1d21SSam Leffler 		eh = mtod(m, struct ether_header *);
2901a1e1d21SSam Leffler 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
2911a1e1d21SSam Leffler 	}
2921a1e1d21SSam Leffler 	return m;
2931a1e1d21SSam Leffler }
2941a1e1d21SSam Leffler 
2951a1e1d21SSam Leffler /*
29668e8e04eSSam Leffler  * Decap a frame encapsulated in a fast-frame/A-MSDU.
29768e8e04eSSam Leffler  */
29868e8e04eSSam Leffler struct mbuf *
29968e8e04eSSam Leffler ieee80211_decap1(struct mbuf *m, int *framelen)
30068e8e04eSSam Leffler {
30168e8e04eSSam Leffler #define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
30268e8e04eSSam Leffler 	struct ether_header *eh;
30368e8e04eSSam Leffler 	struct llc *llc;
30468e8e04eSSam Leffler 
30568e8e04eSSam Leffler 	/*
30668e8e04eSSam Leffler 	 * The frame has an 802.3 header followed by an 802.2
30768e8e04eSSam Leffler 	 * LLC header.  The encapsulated frame length is in the
30868e8e04eSSam Leffler 	 * first header type field; save that and overwrite it
30968e8e04eSSam Leffler 	 * with the true type field found in the second.  Then
31068e8e04eSSam Leffler 	 * copy the 802.3 header up to where it belongs and
31168e8e04eSSam Leffler 	 * adjust the mbuf contents to remove the void.
31268e8e04eSSam Leffler 	 */
31368e8e04eSSam Leffler 	if (m->m_len < FF_LLC_SIZE && (m = m_pullup(m, FF_LLC_SIZE)) == NULL)
31468e8e04eSSam Leffler 		return NULL;
31568e8e04eSSam Leffler 	eh = mtod(m, struct ether_header *);	/* 802.3 header is first */
31668e8e04eSSam Leffler 	llc = (struct llc *)&eh[1];		/* 802.2 header follows */
31768e8e04eSSam Leffler 	*framelen = ntohs(eh->ether_type)	/* encap'd frame size */
31868e8e04eSSam Leffler 		  + sizeof(struct ether_header) - sizeof(struct llc);
31968e8e04eSSam Leffler 	eh->ether_type = llc->llc_un.type_snap.ether_type;
32068e8e04eSSam Leffler 	ovbcopy(eh, mtod(m, uint8_t *) + sizeof(struct llc),
32168e8e04eSSam Leffler 		sizeof(struct ether_header));
32268e8e04eSSam Leffler 	m_adj(m, sizeof(struct llc));
32368e8e04eSSam Leffler 	return m;
32468e8e04eSSam Leffler #undef FF_LLC_SIZE
32568e8e04eSSam Leffler }
32668e8e04eSSam Leffler 
32768e8e04eSSam Leffler /*
3281a1e1d21SSam Leffler  * Install received rate set information in the node's state block.
3291a1e1d21SSam Leffler  */
3307d77cd53SSam Leffler int
3317d77cd53SSam Leffler ieee80211_setup_rates(struct ieee80211_node *ni,
33268e8e04eSSam Leffler 	const uint8_t *rates, const uint8_t *xrates, int flags)
3331a1e1d21SSam Leffler {
334b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3351a1e1d21SSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
3361a1e1d21SSam Leffler 
3371a1e1d21SSam Leffler 	memset(rs, 0, sizeof(*rs));
3381a1e1d21SSam Leffler 	rs->rs_nrates = rates[1];
3391a1e1d21SSam Leffler 	memcpy(rs->rs_rates, rates + 2, rs->rs_nrates);
3401a1e1d21SSam Leffler 	if (xrates != NULL) {
34168e8e04eSSam Leffler 		uint8_t nxrates;
3421a1e1d21SSam Leffler 		/*
3431a1e1d21SSam Leffler 		 * Tack on 11g extended supported rate element.
3441a1e1d21SSam Leffler 		 */
3451a1e1d21SSam Leffler 		nxrates = xrates[1];
3461a1e1d21SSam Leffler 		if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
3471a1e1d21SSam Leffler 			nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates;
348b032f27cSSam Leffler 			IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE, ni,
349b032f27cSSam Leffler 			    "extended rate set too large; only using "
350b032f27cSSam Leffler 			    "%u of %u rates", nxrates, xrates[1]);
351b032f27cSSam Leffler 			vap->iv_stats.is_rx_rstoobig++;
3521a1e1d21SSam Leffler 		}
3531a1e1d21SSam Leffler 		memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates);
3541a1e1d21SSam Leffler 		rs->rs_nrates += nxrates;
3551a1e1d21SSam Leffler 	}
35670e28b9aSSam Leffler 	return ieee80211_fix_rate(ni, rs, flags);
3571a1e1d21SSam Leffler }
3581a1e1d21SSam Leffler 
35984eb84c4SSam Leffler /*
36084eb84c4SSam Leffler  * Send a management frame error response to the specified
36184eb84c4SSam Leffler  * station.  If ni is associated with the station then use
36284eb84c4SSam Leffler  * it; otherwise allocate a temporary node suitable for
36384eb84c4SSam Leffler  * transmitting the frame and then free the reference so
36484eb84c4SSam Leffler  * it will go away as soon as the frame has been transmitted.
36584eb84c4SSam Leffler  */
366b032f27cSSam Leffler void
367b032f27cSSam Leffler ieee80211_send_error(struct ieee80211_node *ni,
368b032f27cSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN], int subtype, int arg)
36984eb84c4SSam Leffler {
370b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
37184eb84c4SSam Leffler 	int istmp;
37284eb84c4SSam Leffler 
373b032f27cSSam Leffler 	if (ni == vap->iv_bss) {
374b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN) {
375b032f27cSSam Leffler 			/*
376b032f27cSSam Leffler 			 * XXX hack until we get rid of this routine.
377b032f27cSSam Leffler 			 * We can be called prior to the vap reaching
378b032f27cSSam Leffler 			 * run state under certain conditions in which
379b032f27cSSam Leffler 			 * case iv_bss->ni_chan will not be setup.
380b032f27cSSam Leffler 			 * Check for this explicitly and and just ignore
381b032f27cSSam Leffler 			 * the request.
382b032f27cSSam Leffler 			 */
383b032f27cSSam Leffler 			return;
384b032f27cSSam Leffler 		}
385b032f27cSSam Leffler 		ni = ieee80211_tmp_node(vap, mac);
38684eb84c4SSam Leffler 		if (ni == NULL) {
38784eb84c4SSam Leffler 			/* XXX msg */
38884eb84c4SSam Leffler 			return;
38984eb84c4SSam Leffler 		}
39084eb84c4SSam Leffler 		istmp = 1;
39184eb84c4SSam Leffler 	} else
39284eb84c4SSam Leffler 		istmp = 0;
393b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, subtype, arg);
39484eb84c4SSam Leffler 	if (istmp)
39584eb84c4SSam Leffler 		ieee80211_free_node(ni);
39684eb84c4SSam Leffler }
39784eb84c4SSam Leffler 
398b032f27cSSam Leffler int
399b032f27cSSam Leffler ieee80211_alloc_challenge(struct ieee80211_node *ni)
4008a1b9b6aSSam Leffler {
4018a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL)
402e2126decSSam Leffler 		ni->ni_challenge = (uint32_t *) malloc(IEEE80211_CHALLENGE_LEN,
40368e8e04eSSam Leffler 		    M_80211_NODE, M_NOWAIT);
4048a1b9b6aSSam Leffler 	if (ni->ni_challenge == NULL) {
405b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap,
406b032f27cSSam Leffler 		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni,
407b032f27cSSam Leffler 		    "%s", "shared key challenge alloc failed");
4088a1b9b6aSSam Leffler 		/* XXX statistic */
4098a1b9b6aSSam Leffler 	}
4108a1b9b6aSSam Leffler 	return (ni->ni_challenge != NULL);
4118a1b9b6aSSam Leffler }
4128a1b9b6aSSam Leffler 
41366ef3969SSam Leffler /*
414b032f27cSSam Leffler  * Parse a Beacon or ProbeResponse frame and return the
415b032f27cSSam Leffler  * useful information in an ieee80211_scanparams structure.
416b032f27cSSam Leffler  * Status is set to 0 if no problems were found; otherwise
417b032f27cSSam Leffler  * a bitmask of IEEE80211_BPARSE_* items is returned that
418b032f27cSSam Leffler  * describes the problems detected.
41966ef3969SSam Leffler  */
420b032f27cSSam Leffler int
421b032f27cSSam Leffler ieee80211_parse_beacon(struct ieee80211_node *ni, struct mbuf *m,
422b032f27cSSam Leffler 	struct ieee80211_scanparams *scan)
42366ef3969SSam Leffler {
424b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
42566ef3969SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
4261a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
42768e8e04eSSam Leffler 	uint8_t *frm, *efrm;
4281a1e1d21SSam Leffler 
429b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
43068e8e04eSSam Leffler 	frm = (uint8_t *)&wh[1];
431b032f27cSSam Leffler 	efrm = mtod(m, uint8_t *) + m->m_len;
432b032f27cSSam Leffler 	scan->status = 0;
4331a1e1d21SSam Leffler 	/*
4341a1e1d21SSam Leffler 	 * beacon/probe response frame format
4351a1e1d21SSam Leffler 	 *	[8] time stamp
4361a1e1d21SSam Leffler 	 *	[2] beacon interval
4371a1e1d21SSam Leffler 	 *	[2] capability information
4381a1e1d21SSam Leffler 	 *	[tlv] ssid
4391a1e1d21SSam Leffler 	 *	[tlv] supported rates
4401a1e1d21SSam Leffler 	 *	[tlv] country information
441c70761e6SSam Leffler 	 *	[tlv] channel switch announcement (CSA)
4421a1e1d21SSam Leffler 	 *	[tlv] parameter set (FH/DS)
4431a1e1d21SSam Leffler 	 *	[tlv] erp information
4441a1e1d21SSam Leffler 	 *	[tlv] extended supported rates
4458a1b9b6aSSam Leffler 	 *	[tlv] WME
4468a1b9b6aSSam Leffler 	 *	[tlv] WPA or RSN
44768e8e04eSSam Leffler 	 *	[tlv] HT capabilities
44868e8e04eSSam Leffler 	 *	[tlv] HT information
44968e8e04eSSam Leffler 	 *	[tlv] Atheros capabilities
45059aa14a9SRui Paulo 	 *	[tlv] Mesh ID
45159aa14a9SRui Paulo 	 *	[tlv] Mesh Configuration
4521a1e1d21SSam Leffler 	 */
453b032f27cSSam Leffler 	IEEE80211_VERIFY_LENGTH(efrm - frm, 12,
454b032f27cSSam Leffler 	    return (scan->status = IEEE80211_BPARSE_BADIELEN));
455b032f27cSSam Leffler 	memset(scan, 0, sizeof(*scan));
456b032f27cSSam Leffler 	scan->tstamp  = frm;				frm += 8;
457b032f27cSSam Leffler 	scan->bintval = le16toh(*(uint16_t *)frm);	frm += 2;
458b032f27cSSam Leffler 	scan->capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
459b032f27cSSam Leffler 	scan->bchan = ieee80211_chan2ieee(ic, ic->ic_curchan);
460b032f27cSSam Leffler 	scan->chan = scan->bchan;
461b032f27cSSam Leffler 	scan->ies = frm;
462b032f27cSSam Leffler 	scan->ies_len = efrm - frm;
463b5c99415SSam Leffler 
46470326a6eSSam Leffler 	while (efrm - frm > 1) {
465b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2,
466b032f27cSSam Leffler 		    return (scan->status = IEEE80211_BPARSE_BADIELEN));
4671a1e1d21SSam Leffler 		switch (*frm) {
4681a1e1d21SSam Leffler 		case IEEE80211_ELEMID_SSID:
469b032f27cSSam Leffler 			scan->ssid = frm;
4701a1e1d21SSam Leffler 			break;
4711a1e1d21SSam Leffler 		case IEEE80211_ELEMID_RATES:
472b032f27cSSam Leffler 			scan->rates = frm;
4731a1e1d21SSam Leffler 			break;
4741a1e1d21SSam Leffler 		case IEEE80211_ELEMID_COUNTRY:
475b032f27cSSam Leffler 			scan->country = frm;
4761a1e1d21SSam Leffler 			break;
477c70761e6SSam Leffler 		case IEEE80211_ELEMID_CSA:
478c70761e6SSam Leffler 			scan->csa = frm;
479c70761e6SSam Leffler 			break;
4801a1e1d21SSam Leffler 		case IEEE80211_ELEMID_FHPARMS:
4811a1e1d21SSam Leffler 			if (ic->ic_phytype == IEEE80211_T_FH) {
482b032f27cSSam Leffler 				scan->fhdwell = LE_READ_2(&frm[2]);
483b032f27cSSam Leffler 				scan->chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
484b032f27cSSam Leffler 				scan->fhindex = frm[6];
4851a1e1d21SSam Leffler 			}
4861a1e1d21SSam Leffler 			break;
4871a1e1d21SSam Leffler 		case IEEE80211_ELEMID_DSPARMS:
4881a1e1d21SSam Leffler 			/*
4891a1e1d21SSam Leffler 			 * XXX hack this since depending on phytype
4901a1e1d21SSam Leffler 			 * is problematic for multi-mode devices.
4911a1e1d21SSam Leffler 			 */
4921a1e1d21SSam Leffler 			if (ic->ic_phytype != IEEE80211_T_FH)
493b032f27cSSam Leffler 				scan->chan = frm[2];
4941a1e1d21SSam Leffler 			break;
4951a1e1d21SSam Leffler 		case IEEE80211_ELEMID_TIM:
4968a1b9b6aSSam Leffler 			/* XXX ATIM? */
497b032f27cSSam Leffler 			scan->tim = frm;
498b032f27cSSam Leffler 			scan->timoff = frm - mtod(m, uint8_t *);
4991a1e1d21SSam Leffler 			break;
5004bd067c5SSam Leffler 		case IEEE80211_ELEMID_IBSSPARMS:
501b032f27cSSam Leffler 		case IEEE80211_ELEMID_CFPARMS:
502643024a2SSam Leffler 		case IEEE80211_ELEMID_PWRCNSTR:
503b032f27cSSam Leffler 			/* NB: avoid debugging complaints */
5044bd067c5SSam Leffler 			break;
5051a1e1d21SSam Leffler 		case IEEE80211_ELEMID_XRATES:
506b032f27cSSam Leffler 			scan->xrates = frm;
5071a1e1d21SSam Leffler 			break;
5081a1e1d21SSam Leffler 		case IEEE80211_ELEMID_ERP:
5091a1e1d21SSam Leffler 			if (frm[1] != 1) {
510b032f27cSSam Leffler 				IEEE80211_DISCARD_IE(vap,
5118a1b9b6aSSam Leffler 				    IEEE80211_MSG_ELEMID, wh, "ERP",
5128a1b9b6aSSam Leffler 				    "bad len %u", frm[1]);
513b032f27cSSam Leffler 				vap->iv_stats.is_rx_elem_toobig++;
5141a1e1d21SSam Leffler 				break;
5151a1e1d21SSam Leffler 			}
516b032f27cSSam Leffler 			scan->erp = frm[2] | 0x100;
5171a1e1d21SSam Leffler 			break;
51868e8e04eSSam Leffler 		case IEEE80211_ELEMID_HTCAP:
519b032f27cSSam Leffler 			scan->htcap = frm;
52068e8e04eSSam Leffler 			break;
5218a1b9b6aSSam Leffler 		case IEEE80211_ELEMID_RSN:
522b032f27cSSam Leffler 			scan->rsn = frm;
52368e8e04eSSam Leffler 			break;
52468e8e04eSSam Leffler 		case IEEE80211_ELEMID_HTINFO:
525b032f27cSSam Leffler 			scan->htinfo = frm;
5268a1b9b6aSSam Leffler 			break;
52759aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_TDMA
52859aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHID:
52959aa14a9SRui Paulo 			scan->meshid = frm;
53059aa14a9SRui Paulo 			break;
53159aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHCONF:
53259aa14a9SRui Paulo 			scan->meshconf = frm;
53359aa14a9SRui Paulo 			break;
53459aa14a9SRui Paulo #endif
5358a1b9b6aSSam Leffler 		case IEEE80211_ELEMID_VENDOR:
5368a1b9b6aSSam Leffler 			if (iswpaoui(frm))
537b032f27cSSam Leffler 				scan->wpa = frm;
5388a1b9b6aSSam Leffler 			else if (iswmeparam(frm) || iswmeinfo(frm))
539b032f27cSSam Leffler 				scan->wme = frm;
540616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
54168e8e04eSSam Leffler 			else if (isatherosoui(frm))
542b032f27cSSam Leffler 				scan->ath = frm;
543616190d0SSam Leffler #endif
54410ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
54510ad9a77SSam Leffler 			else if (istdmaoui(frm))
54610ad9a77SSam Leffler 				scan->tdma = frm;
54710ad9a77SSam Leffler #endif
5482bfc8a91SSam Leffler 			else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
54968e8e04eSSam Leffler 				/*
55068e8e04eSSam Leffler 				 * Accept pre-draft HT ie's if the
55168e8e04eSSam Leffler 				 * standard ones have not been seen.
55268e8e04eSSam Leffler 				 */
55368e8e04eSSam Leffler 				if (ishtcapoui(frm)) {
554b032f27cSSam Leffler 					if (scan->htcap == NULL)
555b032f27cSSam Leffler 						scan->htcap = frm;
55668e8e04eSSam Leffler 				} else if (ishtinfooui(frm)) {
557b032f27cSSam Leffler 					if (scan->htinfo == NULL)
558b032f27cSSam Leffler 						scan->htcap = frm;
55968e8e04eSSam Leffler 				}
56068e8e04eSSam Leffler 			}
5618a1b9b6aSSam Leffler 			break;
5621a1e1d21SSam Leffler 		default:
563b032f27cSSam Leffler 			IEEE80211_DISCARD_IE(vap, IEEE80211_MSG_ELEMID,
5648a1b9b6aSSam Leffler 			    wh, "unhandled",
5658a1b9b6aSSam Leffler 			    "id %u, len %u", *frm, frm[1]);
566b032f27cSSam Leffler 			vap->iv_stats.is_rx_elem_unknown++;
5671a1e1d21SSam Leffler 			break;
5681a1e1d21SSam Leffler 		}
5691a1e1d21SSam Leffler 		frm += frm[1] + 2;
5701a1e1d21SSam Leffler 	}
571b032f27cSSam Leffler 	IEEE80211_VERIFY_ELEMENT(scan->rates, IEEE80211_RATE_MAXSIZE,
572b032f27cSSam Leffler 	    scan->status |= IEEE80211_BPARSE_RATES_INVALID);
573b032f27cSSam Leffler 	if (scan->rates != NULL && scan->xrates != NULL) {
574b032f27cSSam Leffler 		/*
575b032f27cSSam Leffler 		 * NB: don't process XRATES if RATES is missing.  This
576b032f27cSSam Leffler 		 * avoids a potential null ptr deref and should be ok
577b032f27cSSam Leffler 		 * as the return code will already note RATES is missing
578b032f27cSSam Leffler 		 * (so callers shouldn't otherwise process the frame).
579b032f27cSSam Leffler 		 */
580b032f27cSSam Leffler 		IEEE80211_VERIFY_ELEMENT(scan->xrates,
581b032f27cSSam Leffler 		    IEEE80211_RATE_MAXSIZE - scan->rates[1],
582b032f27cSSam Leffler 		    scan->status |= IEEE80211_BPARSE_XRATES_INVALID);
583b032f27cSSam Leffler 	}
584b032f27cSSam Leffler 	IEEE80211_VERIFY_ELEMENT(scan->ssid, IEEE80211_NWID_LEN,
585b032f27cSSam Leffler 	    scan->status |= IEEE80211_BPARSE_SSID_INVALID);
586b032f27cSSam Leffler 	if (scan->chan != scan->bchan && ic->ic_phytype != IEEE80211_T_FH) {
5871a1e1d21SSam Leffler 		/*
5881a1e1d21SSam Leffler 		 * Frame was received on a channel different from the
5894844aa7dSAtsushi Onoe 		 * one indicated in the DS params element id;
5901a1e1d21SSam Leffler 		 * silently discard it.
5911a1e1d21SSam Leffler 		 *
5921a1e1d21SSam Leffler 		 * NB: this can happen due to signal leakage.
5934844aa7dSAtsushi Onoe 		 *     But we should take it for FH phy because
5944844aa7dSAtsushi Onoe 		 *     the rssi value should be correct even for
5954844aa7dSAtsushi Onoe 		 *     different hop pattern in FH.
5961a1e1d21SSam Leffler 		 */
597b032f27cSSam Leffler 		IEEE80211_DISCARD(vap,
598d365f9c7SSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
599b032f27cSSam Leffler 		    wh, NULL, "for off-channel %u", scan->chan);
600b032f27cSSam Leffler 		vap->iv_stats.is_rx_chanmismatch++;
601b032f27cSSam Leffler 		scan->status |= IEEE80211_BPARSE_OFFCHAN;
602b5c99415SSam Leffler 	}
603b032f27cSSam Leffler 	if (!(IEEE80211_BINTVAL_MIN <= scan->bintval &&
604b032f27cSSam Leffler 	      scan->bintval <= IEEE80211_BINTVAL_MAX)) {
605b032f27cSSam Leffler 		IEEE80211_DISCARD(vap,
606b5c99415SSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
607b032f27cSSam Leffler 		    wh, NULL, "bogus beacon interval", scan->bintval);
608b032f27cSSam Leffler 		vap->iv_stats.is_rx_badbintval++;
609b032f27cSSam Leffler 		scan->status |= IEEE80211_BPARSE_BINTVAL_INVALID;
610b032f27cSSam Leffler 	}
611b032f27cSSam Leffler 	if (scan->country != NULL) {
612b032f27cSSam Leffler 		/*
613b032f27cSSam Leffler 		 * Validate we have at least enough data to extract
614b032f27cSSam Leffler 		 * the country code.  Not sure if we should return an
615b032f27cSSam Leffler 		 * error instead of discarding the IE; consider this
616b032f27cSSam Leffler 		 * being lenient as we don't depend on the data for
617b032f27cSSam Leffler 		 * correct operation.
618b032f27cSSam Leffler 		 */
619b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(scan->country[1], 3 * sizeof(uint8_t),
620b032f27cSSam Leffler 		    scan->country = NULL);
621d365f9c7SSam Leffler 	}
622c70761e6SSam Leffler 	if (scan->csa != NULL) {
623c70761e6SSam Leffler 		/*
624c70761e6SSam Leffler 		 * Validate Channel Switch Announcement; this must
625c70761e6SSam Leffler 		 * be the correct length or we toss the frame.
626c70761e6SSam Leffler 		 */
627c70761e6SSam Leffler 		IEEE80211_VERIFY_LENGTH(scan->csa[1], 3 * sizeof(uint8_t),
628c70761e6SSam Leffler 		    scan->status |= IEEE80211_BPARSE_CSA_INVALID);
629c70761e6SSam Leffler 	}
63068e8e04eSSam Leffler 	/*
63168e8e04eSSam Leffler 	 * Process HT ie's.  This is complicated by our
63268e8e04eSSam Leffler 	 * accepting both the standard ie's and the pre-draft
63368e8e04eSSam Leffler 	 * vendor OUI ie's that some vendors still use/require.
63468e8e04eSSam Leffler 	 */
635b032f27cSSam Leffler 	if (scan->htcap != NULL) {
636b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(scan->htcap[1],
637b032f27cSSam Leffler 		     scan->htcap[0] == IEEE80211_ELEMID_VENDOR ?
63868e8e04eSSam Leffler 			 4 + sizeof(struct ieee80211_ie_htcap)-2 :
63968e8e04eSSam Leffler 			 sizeof(struct ieee80211_ie_htcap)-2,
640b032f27cSSam Leffler 		     scan->htcap = NULL);
64168e8e04eSSam Leffler 	}
642b032f27cSSam Leffler 	if (scan->htinfo != NULL) {
643b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(scan->htinfo[1],
644b032f27cSSam Leffler 		     scan->htinfo[0] == IEEE80211_ELEMID_VENDOR ?
64568e8e04eSSam Leffler 			 4 + sizeof(struct ieee80211_ie_htinfo)-2 :
64668e8e04eSSam Leffler 			 sizeof(struct ieee80211_ie_htinfo)-2,
647b032f27cSSam Leffler 		     scan->htinfo = NULL);
648b032f27cSSam Leffler 	}
649b032f27cSSam Leffler 	return scan->status;
65068e8e04eSSam Leffler }
6511a1e1d21SSam Leffler 
6521a1e1d21SSam Leffler /*
653b032f27cSSam Leffler  * Parse an Action frame.  Return 0 on success, non-zero on failure.
65444c72e42SSam Leffler  */
655b032f27cSSam Leffler int
656b032f27cSSam Leffler ieee80211_parse_action(struct ieee80211_node *ni, struct mbuf *m)
657b032f27cSSam Leffler {
658b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
65968e8e04eSSam Leffler 	const struct ieee80211_action *ia;
660b032f27cSSam Leffler 	struct ieee80211_frame *wh;
661b032f27cSSam Leffler 	uint8_t *frm, *efrm;
66268e8e04eSSam Leffler 
66368e8e04eSSam Leffler 	/*
66468e8e04eSSam Leffler 	 * action frame format:
66568e8e04eSSam Leffler 	 *	[1] category
66668e8e04eSSam Leffler 	 *	[1] action
66768e8e04eSSam Leffler 	 *	[tlv] parameters
66868e8e04eSSam Leffler 	 */
669b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
670b032f27cSSam Leffler 	frm = (u_int8_t *)&wh[1];
671b032f27cSSam Leffler 	efrm = mtod(m, u_int8_t *) + m->m_len;
67268e8e04eSSam Leffler 	IEEE80211_VERIFY_LENGTH(efrm - frm,
673b032f27cSSam Leffler 		sizeof(struct ieee80211_action), return EINVAL);
67468e8e04eSSam Leffler 	ia = (const struct ieee80211_action *) frm;
67568e8e04eSSam Leffler 
676b032f27cSSam Leffler 	vap->iv_stats.is_rx_action++;
67768e8e04eSSam Leffler 	IEEE80211_NODE_STAT(ni, rx_action);
67868e8e04eSSam Leffler 
67968e8e04eSSam Leffler 	/* verify frame payloads but defer processing */
68068e8e04eSSam Leffler 	/* XXX maybe push this to method */
68168e8e04eSSam Leffler 	switch (ia->ia_category) {
68268e8e04eSSam Leffler 	case IEEE80211_ACTION_CAT_BA:
68368e8e04eSSam Leffler 		switch (ia->ia_action) {
68468e8e04eSSam Leffler 		case IEEE80211_ACTION_BA_ADDBA_REQUEST:
68568e8e04eSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
68668e8e04eSSam Leffler 			    sizeof(struct ieee80211_action_ba_addbarequest),
687b032f27cSSam Leffler 			    return EINVAL);
68868e8e04eSSam Leffler 			break;
68968e8e04eSSam Leffler 		case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
69068e8e04eSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
69168e8e04eSSam Leffler 			    sizeof(struct ieee80211_action_ba_addbaresponse),
692b032f27cSSam Leffler 			    return EINVAL);
69368e8e04eSSam Leffler 			break;
69468e8e04eSSam Leffler 		case IEEE80211_ACTION_BA_DELBA:
69568e8e04eSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
69668e8e04eSSam Leffler 			    sizeof(struct ieee80211_action_ba_delba),
697b032f27cSSam Leffler 			    return EINVAL);
69868e8e04eSSam Leffler 			break;
69968e8e04eSSam Leffler 		}
70068e8e04eSSam Leffler 		break;
70168e8e04eSSam Leffler 	case IEEE80211_ACTION_CAT_HT:
70268e8e04eSSam Leffler 		switch (ia->ia_action) {
70368e8e04eSSam Leffler 		case IEEE80211_ACTION_HT_TXCHWIDTH:
70468e8e04eSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
70568e8e04eSSam Leffler 			    sizeof(struct ieee80211_action_ht_txchwidth),
706b032f27cSSam Leffler 			    return EINVAL);
707b032f27cSSam Leffler 			break;
708b032f27cSSam Leffler 		case IEEE80211_ACTION_HT_MIMOPWRSAVE:
709b032f27cSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm,
710b032f27cSSam Leffler 			    sizeof(struct ieee80211_action_ht_mimopowersave),
711b032f27cSSam Leffler 			    return EINVAL);
71268e8e04eSSam Leffler 			break;
71368e8e04eSSam Leffler 		}
71468e8e04eSSam Leffler 		break;
71568e8e04eSSam Leffler 	}
716b032f27cSSam Leffler 	return 0;
7178a1b9b6aSSam Leffler }
7188a1b9b6aSSam Leffler 
7198a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
7208a1b9b6aSSam Leffler /*
7218a1b9b6aSSam Leffler  * Debugging support.
7228a1b9b6aSSam Leffler  */
723b032f27cSSam Leffler void
724b032f27cSSam Leffler ieee80211_ssid_mismatch(struct ieee80211vap *vap, const char *tag,
725b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN], uint8_t *ssid)
726b032f27cSSam Leffler {
727b032f27cSSam Leffler 	printf("[%s] discard %s frame, ssid mismatch: ",
728b032f27cSSam Leffler 		ether_sprintf(mac), tag);
729b032f27cSSam Leffler 	ieee80211_print_essid(ssid + 2, ssid[1]);
730b032f27cSSam Leffler 	printf("\n");
731b032f27cSSam Leffler }
7328a1b9b6aSSam Leffler 
7338a1b9b6aSSam Leffler /*
7348a1b9b6aSSam Leffler  * Return the bssid of a frame.
7358a1b9b6aSSam Leffler  */
73668e8e04eSSam Leffler static const uint8_t *
737b032f27cSSam Leffler ieee80211_getbssid(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
7388a1b9b6aSSam Leffler {
739b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA)
7408a1b9b6aSSam Leffler 		return wh->i_addr2;
7418a1b9b6aSSam Leffler 	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) != IEEE80211_FC1_DIR_NODS)
7428a1b9b6aSSam Leffler 		return wh->i_addr1;
7438a1b9b6aSSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
7448a1b9b6aSSam Leffler 		return wh->i_addr1;
7458a1b9b6aSSam Leffler 	return wh->i_addr3;
7468a1b9b6aSSam Leffler }
7478a1b9b6aSSam Leffler 
748b032f27cSSam Leffler #include <machine/stdarg.h>
749b032f27cSSam Leffler 
750f6df3191SSam Leffler void
751b032f27cSSam Leffler ieee80211_note(struct ieee80211vap *vap, const char *fmt, ...)
752f6df3191SSam Leffler {
753f6df3191SSam Leffler 	char buf[128];		/* XXX */
754f6df3191SSam Leffler 	va_list ap;
755f6df3191SSam Leffler 
756f6df3191SSam Leffler 	va_start(ap, fmt);
757f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
758f6df3191SSam Leffler 	va_end(ap);
759f6df3191SSam Leffler 
760b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "%s", buf);	/* NB: no \n */
761f6df3191SSam Leffler }
762f6df3191SSam Leffler 
763f6df3191SSam Leffler void
764b032f27cSSam Leffler ieee80211_note_frame(struct ieee80211vap *vap,
765f6df3191SSam Leffler 	const struct ieee80211_frame *wh,
766f6df3191SSam Leffler 	const char *fmt, ...)
767f6df3191SSam Leffler {
768f6df3191SSam Leffler 	char buf[128];		/* XXX */
769f6df3191SSam Leffler 	va_list ap;
770f6df3191SSam Leffler 
771f6df3191SSam Leffler 	va_start(ap, fmt);
772f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
773f6df3191SSam Leffler 	va_end(ap);
774b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] %s\n",
775b032f27cSSam Leffler 		ether_sprintf(ieee80211_getbssid(vap, wh)), buf);
776f6df3191SSam Leffler }
777f6df3191SSam Leffler 
778f6df3191SSam Leffler void
779b032f27cSSam Leffler ieee80211_note_mac(struct ieee80211vap *vap,
78068e8e04eSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN],
781f6df3191SSam Leffler 	const char *fmt, ...)
782f6df3191SSam Leffler {
783f6df3191SSam Leffler 	char buf[128];		/* XXX */
784f6df3191SSam Leffler 	va_list ap;
785f6df3191SSam Leffler 
786f6df3191SSam Leffler 	va_start(ap, fmt);
787f6df3191SSam Leffler 	vsnprintf(buf, sizeof(buf), fmt, ap);
788f6df3191SSam Leffler 	va_end(ap);
789b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] %s\n", ether_sprintf(mac), buf);
790f6df3191SSam Leffler }
791f6df3191SSam Leffler 
792a000d7c2SSam Leffler void
793b032f27cSSam Leffler ieee80211_discard_frame(struct ieee80211vap *vap,
7948a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
7958a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
7968a1b9b6aSSam Leffler {
7978a1b9b6aSSam Leffler 	va_list ap;
7988a1b9b6aSSam Leffler 
799b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] discard ",
800b032f27cSSam Leffler 		ether_sprintf(ieee80211_getbssid(vap, wh)));
801b032f27cSSam Leffler 	if (type == NULL) {
802b032f27cSSam Leffler 		printf("%s frame, ", ieee80211_mgt_subtype_name[
803b032f27cSSam Leffler 			(wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) >>
804b032f27cSSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT]);
805b032f27cSSam Leffler 	} else
8068a1b9b6aSSam Leffler 		printf("%s frame, ", type);
8078a1b9b6aSSam Leffler 	va_start(ap, fmt);
8088a1b9b6aSSam Leffler 	vprintf(fmt, ap);
8098a1b9b6aSSam Leffler 	va_end(ap);
8108a1b9b6aSSam Leffler 	printf("\n");
8118a1b9b6aSSam Leffler }
8128a1b9b6aSSam Leffler 
813a000d7c2SSam Leffler void
814b032f27cSSam Leffler ieee80211_discard_ie(struct ieee80211vap *vap,
8158a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh,
8168a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
8178a1b9b6aSSam Leffler {
8188a1b9b6aSSam Leffler 	va_list ap;
8198a1b9b6aSSam Leffler 
820b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] discard ",
821b032f27cSSam Leffler 		ether_sprintf(ieee80211_getbssid(vap, wh)));
8228a1b9b6aSSam Leffler 	if (type != NULL)
8238a1b9b6aSSam Leffler 		printf("%s information element, ", type);
8248a1b9b6aSSam Leffler 	else
8258a1b9b6aSSam Leffler 		printf("information element, ");
8268a1b9b6aSSam Leffler 	va_start(ap, fmt);
8278a1b9b6aSSam Leffler 	vprintf(fmt, ap);
8288a1b9b6aSSam Leffler 	va_end(ap);
8298a1b9b6aSSam Leffler 	printf("\n");
8308a1b9b6aSSam Leffler }
8318a1b9b6aSSam Leffler 
832a000d7c2SSam Leffler void
833b032f27cSSam Leffler ieee80211_discard_mac(struct ieee80211vap *vap,
83468e8e04eSSam Leffler 	const uint8_t mac[IEEE80211_ADDR_LEN],
8358a1b9b6aSSam Leffler 	const char *type, const char *fmt, ...)
8368a1b9b6aSSam Leffler {
8378a1b9b6aSSam Leffler 	va_list ap;
8388a1b9b6aSSam Leffler 
839b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "[%s] discard ", ether_sprintf(mac));
8408a1b9b6aSSam Leffler 	if (type != NULL)
8418a1b9b6aSSam Leffler 		printf("%s frame, ", type);
8428a1b9b6aSSam Leffler 	else
8438a1b9b6aSSam Leffler 		printf("frame, ");
8448a1b9b6aSSam Leffler 	va_start(ap, fmt);
8458a1b9b6aSSam Leffler 	vprintf(fmt, ap);
8468a1b9b6aSSam Leffler 	va_end(ap);
8478a1b9b6aSSam Leffler 	printf("\n");
8488a1b9b6aSSam Leffler }
8498a1b9b6aSSam Leffler #endif /* IEEE80211_DEBUG */
850