xref: /openbmc/linux/net/wireless/radiotap.c (revision 9b9c2cd4)
1 /*
2  * Radiotap parser
3  *
4  * Copyright 2007		Andy Green <andy@warmcat.com>
5  * Copyright 2009		Johannes Berg <johannes@sipsolutions.net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * Alternatively, this software may be distributed under the terms of BSD
12  * license.
13  *
14  * See COPYING for more details.
15  */
16 
17 #include <linux/kernel.h>
18 #include <linux/export.h>
19 #include <net/cfg80211.h>
20 #include <net/ieee80211_radiotap.h>
21 #include <asm/unaligned.h>
22 
23 /* function prototypes and related defs are in include/net/cfg80211.h */
24 
25 static const struct radiotap_align_size rtap_namespace_sizes[] = {
26 	[IEEE80211_RADIOTAP_TSFT] = { .align = 8, .size = 8, },
27 	[IEEE80211_RADIOTAP_FLAGS] = { .align = 1, .size = 1, },
28 	[IEEE80211_RADIOTAP_RATE] = { .align = 1, .size = 1, },
29 	[IEEE80211_RADIOTAP_CHANNEL] = { .align = 2, .size = 4, },
30 	[IEEE80211_RADIOTAP_FHSS] = { .align = 2, .size = 2, },
31 	[IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = { .align = 1, .size = 1, },
32 	[IEEE80211_RADIOTAP_DBM_ANTNOISE] = { .align = 1, .size = 1, },
33 	[IEEE80211_RADIOTAP_LOCK_QUALITY] = { .align = 2, .size = 2, },
34 	[IEEE80211_RADIOTAP_TX_ATTENUATION] = { .align = 2, .size = 2, },
35 	[IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = { .align = 2, .size = 2, },
36 	[IEEE80211_RADIOTAP_DBM_TX_POWER] = { .align = 1, .size = 1, },
37 	[IEEE80211_RADIOTAP_ANTENNA] = { .align = 1, .size = 1, },
38 	[IEEE80211_RADIOTAP_DB_ANTSIGNAL] = { .align = 1, .size = 1, },
39 	[IEEE80211_RADIOTAP_DB_ANTNOISE] = { .align = 1, .size = 1, },
40 	[IEEE80211_RADIOTAP_RX_FLAGS] = { .align = 2, .size = 2, },
41 	[IEEE80211_RADIOTAP_TX_FLAGS] = { .align = 2, .size = 2, },
42 	[IEEE80211_RADIOTAP_RTS_RETRIES] = { .align = 1, .size = 1, },
43 	[IEEE80211_RADIOTAP_DATA_RETRIES] = { .align = 1, .size = 1, },
44 	[IEEE80211_RADIOTAP_MCS] = { .align = 1, .size = 3, },
45 	[IEEE80211_RADIOTAP_AMPDU_STATUS] = { .align = 4, .size = 8, },
46 	/*
47 	 * add more here as they are defined in radiotap.h
48 	 */
49 };
50 
51 static const struct ieee80211_radiotap_namespace radiotap_ns = {
52 	.n_bits = ARRAY_SIZE(rtap_namespace_sizes),
53 	.align_size = rtap_namespace_sizes,
54 };
55 
56 /**
57  * ieee80211_radiotap_iterator_init - radiotap parser iterator initialization
58  * @iterator: radiotap_iterator to initialize
59  * @radiotap_header: radiotap header to parse
60  * @max_length: total length we can parse into (eg, whole packet length)
61  *
62  * Returns: 0 or a negative error code if there is a problem.
63  *
64  * This function initializes an opaque iterator struct which can then
65  * be passed to ieee80211_radiotap_iterator_next() to visit every radiotap
66  * argument which is present in the header.  It knows about extended
67  * present headers and handles them.
68  *
69  * How to use:
70  * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
71  * struct ieee80211_radiotap_iterator (no need to init the struct beforehand)
72  * checking for a good 0 return code.  Then loop calling
73  * __ieee80211_radiotap_iterator_next()... it returns either 0,
74  * -ENOENT if there are no more args to parse, or -EINVAL if there is a problem.
75  * The iterator's @this_arg member points to the start of the argument
76  * associated with the current argument index that is present, which can be
77  * found in the iterator's @this_arg_index member.  This arg index corresponds
78  * to the IEEE80211_RADIOTAP_... defines.
79  *
80  * Radiotap header length:
81  * You can find the CPU-endian total radiotap header length in
82  * iterator->max_length after executing ieee80211_radiotap_iterator_init()
83  * successfully.
84  *
85  * Alignment Gotcha:
86  * You must take care when dereferencing iterator.this_arg
87  * for multibyte types... the pointer is not aligned.  Use
88  * get_unaligned((type *)iterator.this_arg) to dereference
89  * iterator.this_arg for type "type" safely on all arches.
90  *
91  * Example code:
92  * See Documentation/networking/radiotap-headers.txt
93  */
94 
95 int ieee80211_radiotap_iterator_init(
96 	struct ieee80211_radiotap_iterator *iterator,
97 	struct ieee80211_radiotap_header *radiotap_header,
98 	int max_length, const struct ieee80211_radiotap_vendor_namespaces *vns)
99 {
100 	/* check the radiotap header can actually be present */
101 	if (max_length < sizeof(struct ieee80211_radiotap_header))
102 		return -EINVAL;
103 
104 	/* Linux only supports version 0 radiotap format */
105 	if (radiotap_header->it_version)
106 		return -EINVAL;
107 
108 	/* sanity check for allowed length and radiotap length field */
109 	if (max_length < get_unaligned_le16(&radiotap_header->it_len))
110 		return -EINVAL;
111 
112 	iterator->_rtheader = radiotap_header;
113 	iterator->_max_length = get_unaligned_le16(&radiotap_header->it_len);
114 	iterator->_arg_index = 0;
115 	iterator->_bitmap_shifter = get_unaligned_le32(&radiotap_header->it_present);
116 	iterator->_arg = (uint8_t *)radiotap_header + sizeof(*radiotap_header);
117 	iterator->_reset_on_ext = 0;
118 	iterator->_next_bitmap = &radiotap_header->it_present;
119 	iterator->_next_bitmap++;
120 	iterator->_vns = vns;
121 	iterator->current_namespace = &radiotap_ns;
122 	iterator->is_radiotap_ns = 1;
123 
124 	/* find payload start allowing for extended bitmap(s) */
125 
126 	if (iterator->_bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT)) {
127 		if ((unsigned long)iterator->_arg -
128 		    (unsigned long)iterator->_rtheader + sizeof(uint32_t) >
129 		    (unsigned long)iterator->_max_length)
130 			return -EINVAL;
131 		while (get_unaligned_le32(iterator->_arg) &
132 					(1 << IEEE80211_RADIOTAP_EXT)) {
133 			iterator->_arg += sizeof(uint32_t);
134 
135 			/*
136 			 * check for insanity where the present bitmaps
137 			 * keep claiming to extend up to or even beyond the
138 			 * stated radiotap header length
139 			 */
140 
141 			if ((unsigned long)iterator->_arg -
142 			    (unsigned long)iterator->_rtheader +
143 			    sizeof(uint32_t) >
144 			    (unsigned long)iterator->_max_length)
145 				return -EINVAL;
146 		}
147 
148 		iterator->_arg += sizeof(uint32_t);
149 
150 		/*
151 		 * no need to check again for blowing past stated radiotap
152 		 * header length, because ieee80211_radiotap_iterator_next
153 		 * checks it before it is dereferenced
154 		 */
155 	}
156 
157 	iterator->this_arg = iterator->_arg;
158 
159 	/* we are all initialized happily */
160 
161 	return 0;
162 }
163 EXPORT_SYMBOL(ieee80211_radiotap_iterator_init);
164 
165 static void find_ns(struct ieee80211_radiotap_iterator *iterator,
166 		    uint32_t oui, uint8_t subns)
167 {
168 	int i;
169 
170 	iterator->current_namespace = NULL;
171 
172 	if (!iterator->_vns)
173 		return;
174 
175 	for (i = 0; i < iterator->_vns->n_ns; i++) {
176 		if (iterator->_vns->ns[i].oui != oui)
177 			continue;
178 		if (iterator->_vns->ns[i].subns != subns)
179 			continue;
180 
181 		iterator->current_namespace = &iterator->_vns->ns[i];
182 		break;
183 	}
184 }
185 
186 
187 
188 /**
189  * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
190  * @iterator: radiotap_iterator to move to next arg (if any)
191  *
192  * Returns: 0 if there is an argument to handle,
193  * -ENOENT if there are no more args or -EINVAL
194  * if there is something else wrong.
195  *
196  * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
197  * in @this_arg_index and sets @this_arg to point to the
198  * payload for the field.  It takes care of alignment handling and extended
199  * present fields.  @this_arg can be changed by the caller (eg,
200  * incremented to move inside a compound argument like
201  * IEEE80211_RADIOTAP_CHANNEL).  The args pointed to are in
202  * little-endian format whatever the endianess of your CPU.
203  *
204  * Alignment Gotcha:
205  * You must take care when dereferencing iterator.this_arg
206  * for multibyte types... the pointer is not aligned.  Use
207  * get_unaligned((type *)iterator.this_arg) to dereference
208  * iterator.this_arg for type "type" safely on all arches.
209  */
210 
211 int ieee80211_radiotap_iterator_next(
212 	struct ieee80211_radiotap_iterator *iterator)
213 {
214 	while (1) {
215 		int hit = 0;
216 		int pad, align, size, subns;
217 		uint32_t oui;
218 
219 		/* if no more EXT bits, that's it */
220 		if ((iterator->_arg_index % 32) == IEEE80211_RADIOTAP_EXT &&
221 		    !(iterator->_bitmap_shifter & 1))
222 			return -ENOENT;
223 
224 		if (!(iterator->_bitmap_shifter & 1))
225 			goto next_entry; /* arg not present */
226 
227 		/* get alignment/size of data */
228 		switch (iterator->_arg_index % 32) {
229 		case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
230 		case IEEE80211_RADIOTAP_EXT:
231 			align = 1;
232 			size = 0;
233 			break;
234 		case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
235 			align = 2;
236 			size = 6;
237 			break;
238 		default:
239 			if (!iterator->current_namespace ||
240 			    iterator->_arg_index >= iterator->current_namespace->n_bits) {
241 				if (iterator->current_namespace == &radiotap_ns)
242 					return -ENOENT;
243 				align = 0;
244 			} else {
245 				align = iterator->current_namespace->align_size[iterator->_arg_index].align;
246 				size = iterator->current_namespace->align_size[iterator->_arg_index].size;
247 			}
248 			if (!align) {
249 				/* skip all subsequent data */
250 				iterator->_arg = iterator->_next_ns_data;
251 				/* give up on this namespace */
252 				iterator->current_namespace = NULL;
253 				goto next_entry;
254 			}
255 			break;
256 		}
257 
258 		/*
259 		 * arg is present, account for alignment padding
260 		 *
261 		 * Note that these alignments are relative to the start
262 		 * of the radiotap header.  There is no guarantee
263 		 * that the radiotap header itself is aligned on any
264 		 * kind of boundary.
265 		 *
266 		 * The above is why get_unaligned() is used to dereference
267 		 * multibyte elements from the radiotap area.
268 		 */
269 
270 		pad = ((unsigned long)iterator->_arg -
271 		       (unsigned long)iterator->_rtheader) & (align - 1);
272 
273 		if (pad)
274 			iterator->_arg += align - pad;
275 
276 		if (iterator->_arg_index % 32 == IEEE80211_RADIOTAP_VENDOR_NAMESPACE) {
277 			int vnslen;
278 
279 			if ((unsigned long)iterator->_arg + size -
280 			    (unsigned long)iterator->_rtheader >
281 			    (unsigned long)iterator->_max_length)
282 				return -EINVAL;
283 
284 			oui = (*iterator->_arg << 16) |
285 				(*(iterator->_arg + 1) << 8) |
286 				*(iterator->_arg + 2);
287 			subns = *(iterator->_arg + 3);
288 
289 			find_ns(iterator, oui, subns);
290 
291 			vnslen = get_unaligned_le16(iterator->_arg + 4);
292 			iterator->_next_ns_data = iterator->_arg + size + vnslen;
293 			if (!iterator->current_namespace)
294 				size += vnslen;
295 		}
296 
297 		/*
298 		 * this is what we will return to user, but we need to
299 		 * move on first so next call has something fresh to test
300 		 */
301 		iterator->this_arg_index = iterator->_arg_index;
302 		iterator->this_arg = iterator->_arg;
303 		iterator->this_arg_size = size;
304 
305 		/* internally move on the size of this arg */
306 		iterator->_arg += size;
307 
308 		/*
309 		 * check for insanity where we are given a bitmap that
310 		 * claims to have more arg content than the length of the
311 		 * radiotap section.  We will normally end up equalling this
312 		 * max_length on the last arg, never exceeding it.
313 		 */
314 
315 		if ((unsigned long)iterator->_arg -
316 		    (unsigned long)iterator->_rtheader >
317 		    (unsigned long)iterator->_max_length)
318 			return -EINVAL;
319 
320 		/* these special ones are valid in each bitmap word */
321 		switch (iterator->_arg_index % 32) {
322 		case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
323 			iterator->_reset_on_ext = 1;
324 
325 			iterator->is_radiotap_ns = 0;
326 			/*
327 			 * If parser didn't register this vendor
328 			 * namespace with us, allow it to show it
329 			 * as 'raw. Do do that, set argument index
330 			 * to vendor namespace.
331 			 */
332 			iterator->this_arg_index =
333 				IEEE80211_RADIOTAP_VENDOR_NAMESPACE;
334 			if (!iterator->current_namespace)
335 				hit = 1;
336 			goto next_entry;
337 		case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
338 			iterator->_reset_on_ext = 1;
339 			iterator->current_namespace = &radiotap_ns;
340 			iterator->is_radiotap_ns = 1;
341 			goto next_entry;
342 		case IEEE80211_RADIOTAP_EXT:
343 			/*
344 			 * bit 31 was set, there is more
345 			 * -- move to next u32 bitmap
346 			 */
347 			iterator->_bitmap_shifter =
348 				get_unaligned_le32(iterator->_next_bitmap);
349 			iterator->_next_bitmap++;
350 			if (iterator->_reset_on_ext)
351 				iterator->_arg_index = 0;
352 			else
353 				iterator->_arg_index++;
354 			iterator->_reset_on_ext = 0;
355 			break;
356 		default:
357 			/* we've got a hit! */
358 			hit = 1;
359  next_entry:
360 			iterator->_bitmap_shifter >>= 1;
361 			iterator->_arg_index++;
362 		}
363 
364 		/* if we found a valid arg earlier, return it now */
365 		if (hit)
366 			return 0;
367 	}
368 }
369 EXPORT_SYMBOL(ieee80211_radiotap_iterator_next);
370