1 /*-
2  * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 #ifndef _NET80211_IEEE80211_INPUT_H_
28 #define _NET80211_IEEE80211_INPUT_H_
29 
30 /* Verify the existence and length of __elem or get out. */
31 #define IEEE80211_VERIFY_ELEMENT(__elem, __maxlen, _action) do {	\
32 	if ((__elem) == NULL) {						\
33 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ELEMID,		\
34 		    wh, NULL, "%s", "no " #__elem );			\
35 		vap->iv_stats.is_rx_elem_missing++;			\
36 		_action;						\
37 	} else if ((__elem)[1] > (__maxlen)) {				\
38 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ELEMID,		\
39 		    wh, NULL, "bad " #__elem " len %d", (__elem)[1]);	\
40 		vap->iv_stats.is_rx_elem_toobig++;			\
41 		_action;						\
42 	}								\
43 } while (0)
44 
45 #define	IEEE80211_VERIFY_LENGTH(_len, _minlen, _action) do {		\
46 	if ((_len) < (_minlen)) {					\
47 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ELEMID,		\
48 		    wh, NULL, "ie too short, got %ld, expected %ld",	\
49 		    (long)(_len), (long)(_minlen));			\
50 		vap->iv_stats.is_rx_elem_toosmall++;			\
51 		_action;						\
52 	}								\
53 } while (0)
54 
55 #ifdef IEEE80211_DEBUG
56 void	ieee80211_ssid_mismatch(struct ieee80211vap *, const char *tag,
57 	uint8_t mac[IEEE80211_ADDR_LEN], uint8_t *ssid);
58 
59 #define	IEEE80211_VERIFY_SSID(_ni, _ssid, _action) do {			\
60 	if ((_ssid)[1] != 0 &&						\
61 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
62 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
63 		if (ieee80211_msg_input(vap))				\
64 			ieee80211_ssid_mismatch(vap, 			\
65 			    ieee80211_mgt_subtype_name[subtype >>	\
66 				IEEE80211_FC0_SUBTYPE_SHIFT],		\
67 				wh->i_addr2, _ssid);			\
68 		vap->iv_stats.is_rx_ssidmismatch++;			\
69 		_action;						\
70 	}								\
71 } while (0)
72 #else /* !IEEE80211_DEBUG */
73 #define	IEEE80211_VERIFY_SSID(_ni, _ssid, _action) do {			\
74 	if ((_ssid)[1] != 0 &&						\
75 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
76 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
77 		vap->iv_stats.is_rx_ssidmismatch++;			\
78 		_action;						\
79 	}								\
80 } while (0)
81 #endif /* !IEEE80211_DEBUG */
82 
83 /* unalligned little endian access */
84 #define LE_READ_2(p)					\
85 	((uint16_t)					\
86 	 ((((const uint8_t *)(p))[0]      ) |		\
87 	  (((const uint8_t *)(p))[1] <<  8)))
88 #define LE_READ_4(p)					\
89 	((uint32_t)					\
90 	 ((((const uint8_t *)(p))[0]      ) |		\
91 	  (((const uint8_t *)(p))[1] <<  8) |		\
92 	  (((const uint8_t *)(p))[2] << 16) |		\
93 	  (((const uint8_t *)(p))[3] << 24)))
94 
95 static __inline int
96 iswpaoui(const uint8_t *frm)
97 {
98 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
99 }
100 
101 static __inline int
102 iswmeoui(const uint8_t *frm)
103 {
104 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
105 }
106 
107 static __inline int
108 iswmeparam(const uint8_t *frm)
109 {
110 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
111 		frm[6] == WME_PARAM_OUI_SUBTYPE;
112 }
113 
114 static __inline int
115 iswmeinfo(const uint8_t *frm)
116 {
117 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
118 		frm[6] == WME_INFO_OUI_SUBTYPE;
119 }
120 
121 static __inline int
122 isatherosoui(const uint8_t *frm)
123 {
124 	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
125 }
126 
127 static __inline int
128 istdmaoui(const uint8_t *frm)
129 {
130 	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
131 }
132 
133 static __inline int
134 ishtcapoui(const uint8_t *frm)
135 {
136 	return frm[1] > 3 && LE_READ_4(frm+2) == ((BCM_OUI_HTCAP<<24)|BCM_OUI);
137 }
138 
139 static __inline int
140 ishtinfooui(const uint8_t *frm)
141 {
142 	return frm[1] > 3 && LE_READ_4(frm+2) == ((BCM_OUI_HTINFO<<24)|BCM_OUI);
143 }
144 
145 #include <sys/endian.h>		/* For le16toh() */
146 
147 /*
148  * Check the current frame sequence number against the current TID
149  * state and return whether it's in sequence or should be dropped.
150  *
151  * Since out of order packet and duplicate packet eliminations should
152  * be done by the AMPDU RX code, this routine blindly accepts all
153  * frames from a HT station w/ a TID that is currently doing AMPDU-RX.
154  * HT stations without WME or where the TID is not doing AMPDU-RX
155  * are checked like non-HT stations.
156  *
157  * The routine only eliminates packets whose sequence/fragment
158  * match or are less than the last seen sequence/fragment number
159  * AND are retransmits It doesn't try to eliminate out of order packets.
160  *
161  * Since all frames after sequence number 4095 will be less than 4095
162  * (as the seqnum wraps), handle that special case so packets aren't
163  * incorrectly dropped - ie, if the next packet is sequence number 0
164  * but a retransmit since the initial packet didn't make it.
165  */
166 static __inline int
167 ieee80211_check_rxseq(struct ieee80211_node *ni, struct ieee80211_frame *wh)
168 {
169 #define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
170 #define	SEQ_EQ(a,b)	((int)((a)-(b)) == 0)
171 #define	HAS_SEQ(type)	((type & 0x4) == 0)
172 #define	SEQNO(a)	((a) >> IEEE80211_SEQ_SEQ_SHIFT)
173 #define	FRAGNO(a)	((a) & IEEE80211_SEQ_FRAG_MASK)
174 	uint16_t rxseq;
175 	uint8_t type;
176 	uint8_t tid;
177 	struct ieee80211_rx_ampdu *rap;
178 
179 	rxseq = le16toh(*(uint16_t *)wh->i_seq);
180 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
181 
182 	/* Types with no sequence number are always treated valid */
183 	if (! HAS_SEQ(type))
184 		return 1;
185 
186 	tid = ieee80211_gettid(wh);
187 
188 	/*
189 	 * Only do the HT AMPDU check for WME stations; non-WME HT stations
190 	 * shouldn't exist outside of debugging. We should at least
191 	 * handle that.
192 	 */
193 	if (tid < WME_NUM_TID) {
194 		rap = &ni->ni_rx_ampdu[tid];
195 		/* HT nodes currently doing RX AMPDU are always valid */
196 		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
197 		    (rap->rxa_flags & IEEE80211_AGGR_RUNNING))
198 			return 1;
199 	}
200 
201 	/*
202 	 * Otherwise, retries for packets below or equal to the last
203 	 * seen sequence number should be dropped.
204 	 */
205 
206 	/*
207 	 * Treat frame seqnum 4095 as special due to boundary
208 	 * wrapping conditions.
209 	 */
210 	if (SEQNO(ni->ni_rxseqs[tid]) == 4095) {
211 		/*
212 		 * Drop retransmits on seqnum 4095/current fragment for itself.
213 		 */
214 		if (SEQ_EQ(rxseq, ni->ni_rxseqs[tid]) &&
215 		    (wh->i_fc[1] & IEEE80211_FC1_RETRY))
216 			return 0;
217 		/*
218 		 * Treat any subsequent frame as fine if the last seen frame
219 		 * is 4095 and it's not a retransmit for the same sequence
220 		 * number. However, this doesn't capture incorrectly ordered
221 		 * fragments w/ sequence number 4095. It shouldn't be seen
222 		 * in practice, but see the comment above for further info.
223 		 */
224 		return 1;
225 	}
226 
227 	/*
228 	 * At this point we assume that retransmitted seq/frag numbers below
229 	 * the current can simply be eliminated.
230 	 */
231 	if ((wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
232 	    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid]))
233 		return 0;
234 
235 	return 1;
236 #undef	SEQ_LEQ
237 #undef	SEQ_EQ
238 #undef	HAS_SEQ
239 #undef	SEQNO
240 #undef	FRAGNO
241 }
242 
243 void	ieee80211_deliver_data(struct ieee80211vap *,
244 		struct ieee80211_node *, struct mbuf *);
245 struct mbuf *ieee80211_defrag(struct ieee80211_node *,
246 		struct mbuf *, int);
247 struct mbuf *ieee80211_realign(struct ieee80211vap *, struct mbuf *, size_t);
248 struct mbuf *ieee80211_decap(struct ieee80211vap *, struct mbuf *, int);
249 struct mbuf *ieee80211_decap1(struct mbuf *, int *);
250 int	ieee80211_setup_rates(struct ieee80211_node *ni,
251 		const uint8_t *rates, const uint8_t *xrates, int flags);
252 void ieee80211_send_error(struct ieee80211_node *,
253 		const uint8_t mac[IEEE80211_ADDR_LEN], int subtype, int arg);
254 int	ieee80211_alloc_challenge(struct ieee80211_node *);
255 int	ieee80211_parse_beacon(struct ieee80211_node *, struct mbuf *,
256 		struct ieee80211_scanparams *);
257 int	ieee80211_parse_action(struct ieee80211_node *, struct mbuf *);
258 #endif /* _NET80211_IEEE80211_INPUT_H_ */
259