xref: /openwifi/user_space/inject_80211/radiotap.c (revision b1dd94e38780b1645f1f7894c71908d8218f3d7f)
1 /*
2  * Radiotap parser
3  *
4  * Copyright 2007		Andy Green <[email protected]>
5  */
6 
7 #include "inject_80211.h"
8 #include "radiotap.h"
9 
10 /**
11  * ieee80211_radiotap_iterator_init - radiotap parser iterator initialization
12  * @iterator: radiotap_iterator to initialize
13  * @radiotap_header: radiotap header to parse
14  * @max_length: total length we can parse into (eg, whole packet length)
15  *
16  * Returns: 0 or a negative error code if there is a problem.
17  *
18  * This function initializes an opaque iterator struct which can then
19  * be passed to ieee80211_radiotap_iterator_next() to visit every radiotap
20  * argument which is present in the header.  It knows about extended
21  * present headers and handles them.
22  *
23  * How to use:
24  * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
25  * struct ieee80211_radiotap_iterator (no need to init the struct beforehand)
26  * checking for a good 0 return code.  Then loop calling
27  * __ieee80211_radiotap_iterator_next()... it returns either 0,
28  * -ENOENT if there are no more args to parse, or -EINVAL if there is a problem.
29  * The iterator's @this_arg member points to the start of the argument
30  * associated with the current argument index that is present, which can be
31  * found in the iterator's @this_arg_index member.  This arg index corresponds
32  * to the IEEE80211_RADIOTAP_... defines.
33  *
34  * Radiotap header length:
35  * You can find the CPU-endian total radiotap header length in
36  * iterator->max_length after executing ieee80211_radiotap_iterator_init()
37  * successfully.
38  *
39  * Example code:
40  * See Documentation/networking/radiotap-headers.txt
41  */
42 
43 int ieee80211_radiotap_iterator_init(
44     struct ieee80211_radiotap_iterator *iterator,
45     struct ieee80211_radiotap_header *radiotap_header,
46     int max_length)
47 {
48 	/* Linux only supports version 0 radiotap format */
49 	if (radiotap_header->it_version)
50 		return -EINVAL;
51 
52 	/* sanity check for allowed length and radiotap length field */
53 	if (max_length < le16_to_cpu(radiotap_header->it_len))
54 		return -EINVAL;
55 
56 	iterator->rtheader = radiotap_header;
57 	iterator->max_length = le16_to_cpu(radiotap_header->it_len);
58 	iterator->arg_index = 0;
59 	iterator->bitmap_shifter = le32_to_cpu(radiotap_header->it_present);
60 	iterator->arg = (u8 *)radiotap_header + sizeof(*radiotap_header);
61 	iterator->this_arg = 0;
62 
63 	/* find payload start allowing for extended bitmap(s) */
64 
65 	if (unlikely(iterator->bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT))) {
66 		while (le32_to_cpu(*((u32 *)iterator->arg)) &
67 				   (1<<IEEE80211_RADIOTAP_EXT)) {
68 			iterator->arg += sizeof(u32);
69 
70 			/*
71 			 * check for insanity where the present bitmaps
72 			 * keep claiming to extend up to or even beyond the
73 			 * stated radiotap header length
74 			 */
75 
76 			if (((ulong)iterator->arg -
77 			     (ulong)iterator->rtheader) > iterator->max_length)
78 				return -EINVAL;
79 		}
80 
81 		iterator->arg += sizeof(u32);
82 
83 		/*
84 		 * no need to check again for blowing past stated radiotap
85 		 * header length, because ieee80211_radiotap_iterator_next
86 		 * checks it before it is dereferenced
87 		 */
88 	}
89 
90 	/* we are all initialized happily */
91 
92 	return 0;
93 }
94 
95 
96 /**
97  * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
98  * @iterator: radiotap_iterator to move to next arg (if any)
99  *
100  * Returns: 0 if there is an argument to handle,
101  * -ENOENT if there are no more args or -EINVAL
102  * if there is something else wrong.
103  *
104  * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
105  * in @this_arg_index and sets @this_arg to point to the
106  * payload for the field.  It takes care of alignment handling and extended
107  * present fields.  @this_arg can be changed by the caller (eg,
108  * incremented to move inside a compound argument like
109  * IEEE80211_RADIOTAP_CHANNEL).  The args pointed to are in
110  * little-endian format whatever the endianness of your CPU.
111  */
112 
113 int ieee80211_radiotap_iterator_next(
114     struct ieee80211_radiotap_iterator *iterator)
115 {
116 
117 	/*
118 	 * small length lookup table for all radiotap types we heard of
119 	 * starting from b0 in the bitmap, so we can walk the payload
120 	 * area of the radiotap header
121 	 *
122 	 * There is a requirement to pad args, so that args
123 	 * of a given length must begin at a boundary of that length
124 	 * -- but note that compound args are allowed (eg, 2 x u16
125 	 * for IEEE80211_RADIOTAP_CHANNEL) so total arg length is not
126 	 * a reliable indicator of alignment requirement.
127 	 *
128 	 * upper nybble: content alignment for arg
129 	 * lower nybble: content length for arg
130 	 */
131 
132 	static const u8 rt_sizes[] = {
133 		[IEEE80211_RADIOTAP_TSFT] = 0x88,
134 		[IEEE80211_RADIOTAP_FLAGS] = 0x11,
135 		[IEEE80211_RADIOTAP_RATE] = 0x11,
136 		[IEEE80211_RADIOTAP_CHANNEL] = 0x24,
137 		[IEEE80211_RADIOTAP_FHSS] = 0x22,
138 		[IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = 0x11,
139 		[IEEE80211_RADIOTAP_DBM_ANTNOISE] = 0x11,
140 		[IEEE80211_RADIOTAP_LOCK_QUALITY] = 0x22,
141 		[IEEE80211_RADIOTAP_TX_ATTENUATION] = 0x22,
142 		[IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = 0x22,
143 		[IEEE80211_RADIOTAP_DBM_TX_POWER] = 0x11,
144 		[IEEE80211_RADIOTAP_ANTENNA] = 0x11,
145 		[IEEE80211_RADIOTAP_DB_ANTSIGNAL] = 0x11,
146 		[IEEE80211_RADIOTAP_DB_ANTNOISE] = 0x11,
147 		[IEEE80211_RADIOTAP_RX_FLAGS] = 0x22,
148 		[IEEE80211_RADIOTAP_TX_FLAGS] = 0x22,
149 		[IEEE80211_RADIOTAP_RTS_RETRIES] = 0x11,
150 		[IEEE80211_RADIOTAP_DATA_RETRIES] = 0x11,
151 		[IEEE80211_RADIOTAP_MCS] = 0x13,
152 		[IEEE80211_RADIOTAP_AMPDU_STATUS] = 0x48
153 		/*
154 		 * add more here as they are defined in
155 		 * include/net/ieee80211_radiotap.h
156 		 */
157 	};
158 
159 	/*
160 	 * for every radiotap entry we can at
161 	 * least skip (by knowing the length)...
162 	 */
163 
164 	while (iterator->arg_index < sizeof(rt_sizes)) {
165 		int hit = 0;
166 		int pad;
167 
168 		if (!(iterator->bitmap_shifter & 1))
169 			goto next_entry; /* arg not present */
170 
171 		/*
172 		 * arg is present, account for alignment padding
173 		 *  8-bit args can be at any alignment
174 		 * 16-bit args must start on 16-bit boundary
175 		 * 32-bit args must start on 32-bit boundary
176 		 * 64-bit args must start on 64-bit boundary
177 		 *
178 		 * note that total arg size can differ from alignment of
179 		 * elements inside arg, so we use upper nybble of length
180 		 * table to base alignment on
181 		 *
182 		 * also note: these alignments are ** relative to the
183 		 * start of the radiotap header **.  There is no guarantee
184 		 * that the radiotap header itself is aligned on any
185 		 * kind of boundary.
186 		 */
187 
188 		pad = (((ulong)iterator->arg) -
189 			((ulong)iterator->rtheader)) &
190 			((rt_sizes[iterator->arg_index] >> 4) - 1);
191 
192 		if (pad)
193 			iterator->arg +=
194 				(rt_sizes[iterator->arg_index] >> 4) - pad;
195 
196 		/*
197 		 * this is what we will return to user, but we need to
198 		 * move on first so next call has something fresh to test
199 		 */
200 		iterator->this_arg_index = iterator->arg_index;
201 		iterator->this_arg = iterator->arg;
202 		hit = 1;
203 
204 		/* internally move on the size of this arg */
205 		iterator->arg += rt_sizes[iterator->arg_index] & 0x0f;
206 
207 		/*
208 		 * check for insanity where we are given a bitmap that
209 		 * claims to have more arg content than the length of the
210 		 * radiotap section.  We will normally end up equalling this
211 		 * max_length on the last arg, never exceeding it.
212 		 */
213 
214 		if (((ulong)iterator->arg - (ulong)iterator->rtheader) >
215 		    iterator->max_length)
216 			return -EINVAL;
217 
218 	next_entry:
219 		iterator->arg_index++;
220 		if (unlikely((iterator->arg_index & 31) == 0)) {
221 			/* completed current u32 bitmap */
222 			if (iterator->bitmap_shifter & 1) {
223 				/* b31 was set, there is more */
224 				/* move to next u32 bitmap */
225 				iterator->bitmap_shifter =
226 				    le32_to_cpu(*iterator->next_bitmap);
227 				iterator->next_bitmap++;
228 			} else {
229 				/* no more bitmaps: end */
230 				iterator->arg_index = sizeof(rt_sizes);
231 			}
232 		} else { /* just try the next bit */
233 			iterator->bitmap_shifter >>= 1;
234 		}
235 
236 		/* if we found a valid arg earlier, return it now */
237 		if (hit)
238 			return 0;
239 	}
240 
241 	/* we don't know how to handle any more args, we're done */
242 	return -ENOENT;
243 }
244 
245