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