blob: 617a310025b1aa92d5eb166b5e9457494ae2a023 [file] [log] [blame]
Andy Green179f8312007-07-10 19:29:38 +02001/*
2 * Radiotap parser
3 *
4 * Copyright 2007 Andy Green <andy@warmcat.com>
Johannes Berg33e5a2f2010-02-03 10:24:30 +01005 * 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.
Andy Green179f8312007-07-10 19:29:38 +020015 */
16
Nikitas Angelinas94262312010-09-08 22:29:53 +010017#include <linux/kernel.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040018#include <linux/export.h>
Andy Green179f8312007-07-10 19:29:38 +020019#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
Johannes Berg33e5a2f2010-02-03 10:24:30 +010025static 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 /*
45 * add more here as they are defined in radiotap.h
46 */
47};
48
49static const struct ieee80211_radiotap_namespace radiotap_ns = {
Nikitas Angelinas94262312010-09-08 22:29:53 +010050 .n_bits = ARRAY_SIZE(rtap_namespace_sizes),
Johannes Berg33e5a2f2010-02-03 10:24:30 +010051 .align_size = rtap_namespace_sizes,
52};
53
Andy Green179f8312007-07-10 19:29:38 +020054/**
55 * ieee80211_radiotap_iterator_init - radiotap parser iterator initialization
56 * @iterator: radiotap_iterator to initialize
57 * @radiotap_header: radiotap header to parse
58 * @max_length: total length we can parse into (eg, whole packet length)
59 *
60 * Returns: 0 or a negative error code if there is a problem.
61 *
62 * This function initializes an opaque iterator struct which can then
63 * be passed to ieee80211_radiotap_iterator_next() to visit every radiotap
64 * argument which is present in the header. It knows about extended
65 * present headers and handles them.
66 *
67 * How to use:
68 * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
69 * struct ieee80211_radiotap_iterator (no need to init the struct beforehand)
70 * checking for a good 0 return code. Then loop calling
71 * __ieee80211_radiotap_iterator_next()... it returns either 0,
72 * -ENOENT if there are no more args to parse, or -EINVAL if there is a problem.
73 * The iterator's @this_arg member points to the start of the argument
74 * associated with the current argument index that is present, which can be
75 * found in the iterator's @this_arg_index member. This arg index corresponds
76 * to the IEEE80211_RADIOTAP_... defines.
77 *
78 * Radiotap header length:
79 * You can find the CPU-endian total radiotap header length in
80 * iterator->max_length after executing ieee80211_radiotap_iterator_init()
81 * successfully.
82 *
83 * Alignment Gotcha:
84 * You must take care when dereferencing iterator.this_arg
85 * for multibyte types... the pointer is not aligned. Use
86 * get_unaligned((type *)iterator.this_arg) to dereference
87 * iterator.this_arg for type "type" safely on all arches.
88 *
89 * Example code:
90 * See Documentation/networking/radiotap-headers.txt
91 */
92
93int ieee80211_radiotap_iterator_init(
Johannes Berg33e5a2f2010-02-03 10:24:30 +010094 struct ieee80211_radiotap_iterator *iterator,
95 struct ieee80211_radiotap_header *radiotap_header,
96 int max_length, const struct ieee80211_radiotap_vendor_namespaces *vns)
Andy Green179f8312007-07-10 19:29:38 +020097{
Johannes Berg5b81d472013-10-11 14:47:05 +020098 /* check the radiotap header can actually be present */
99 if (max_length < sizeof(struct ieee80211_radiotap_header))
100 return -EINVAL;
101
Andy Green179f8312007-07-10 19:29:38 +0200102 /* Linux only supports version 0 radiotap format */
103 if (radiotap_header->it_version)
104 return -EINVAL;
105
106 /* sanity check for allowed length and radiotap length field */
Harvey Harrisonae7245c2008-05-01 22:19:33 -0700107 if (max_length < get_unaligned_le16(&radiotap_header->it_len))
Andy Green179f8312007-07-10 19:29:38 +0200108 return -EINVAL;
109
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100110 iterator->_rtheader = radiotap_header;
111 iterator->_max_length = get_unaligned_le16(&radiotap_header->it_len);
112 iterator->_arg_index = 0;
113 iterator->_bitmap_shifter = get_unaligned_le32(&radiotap_header->it_present);
114 iterator->_arg = (uint8_t *)radiotap_header + sizeof(*radiotap_header);
115 iterator->_reset_on_ext = 0;
116 iterator->_next_bitmap = &radiotap_header->it_present;
117 iterator->_next_bitmap++;
118 iterator->_vns = vns;
119 iterator->current_namespace = &radiotap_ns;
120 iterator->is_radiotap_ns = 1;
Andy Green179f8312007-07-10 19:29:38 +0200121
122 /* find payload start allowing for extended bitmap(s) */
123
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100124 if (iterator->_bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT)) {
125 while (get_unaligned_le32(iterator->_arg) &
126 (1 << IEEE80211_RADIOTAP_EXT)) {
127 iterator->_arg += sizeof(uint32_t);
Andy Green179f8312007-07-10 19:29:38 +0200128
129 /*
130 * check for insanity where the present bitmaps
131 * keep claiming to extend up to or even beyond the
132 * stated radiotap header length
133 */
134
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100135 if ((unsigned long)iterator->_arg -
Johannes Berg5b81d472013-10-11 14:47:05 +0200136 (unsigned long)iterator->_rtheader +
137 sizeof(uint32_t) >
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100138 (unsigned long)iterator->_max_length)
Andy Green179f8312007-07-10 19:29:38 +0200139 return -EINVAL;
140 }
141
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100142 iterator->_arg += sizeof(uint32_t);
Andy Green179f8312007-07-10 19:29:38 +0200143
144 /*
145 * no need to check again for blowing past stated radiotap
146 * header length, because ieee80211_radiotap_iterator_next
147 * checks it before it is dereferenced
148 */
149 }
150
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100151 iterator->this_arg = iterator->_arg;
152
Andy Green179f8312007-07-10 19:29:38 +0200153 /* we are all initialized happily */
154
155 return 0;
156}
157EXPORT_SYMBOL(ieee80211_radiotap_iterator_init);
158
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100159static void find_ns(struct ieee80211_radiotap_iterator *iterator,
160 uint32_t oui, uint8_t subns)
161{
162 int i;
163
164 iterator->current_namespace = NULL;
165
166 if (!iterator->_vns)
167 return;
168
169 for (i = 0; i < iterator->_vns->n_ns; i++) {
170 if (iterator->_vns->ns[i].oui != oui)
171 continue;
172 if (iterator->_vns->ns[i].subns != subns)
173 continue;
174
175 iterator->current_namespace = &iterator->_vns->ns[i];
176 break;
177 }
178}
179
180
Andy Green179f8312007-07-10 19:29:38 +0200181
182/**
183 * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
184 * @iterator: radiotap_iterator to move to next arg (if any)
185 *
186 * Returns: 0 if there is an argument to handle,
187 * -ENOENT if there are no more args or -EINVAL
188 * if there is something else wrong.
189 *
190 * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
191 * in @this_arg_index and sets @this_arg to point to the
192 * payload for the field. It takes care of alignment handling and extended
193 * present fields. @this_arg can be changed by the caller (eg,
194 * incremented to move inside a compound argument like
195 * IEEE80211_RADIOTAP_CHANNEL). The args pointed to are in
196 * little-endian format whatever the endianess of your CPU.
197 *
198 * Alignment Gotcha:
199 * You must take care when dereferencing iterator.this_arg
200 * for multibyte types... the pointer is not aligned. Use
201 * get_unaligned((type *)iterator.this_arg) to dereference
202 * iterator.this_arg for type "type" safely on all arches.
203 */
204
205int ieee80211_radiotap_iterator_next(
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100206 struct ieee80211_radiotap_iterator *iterator)
Andy Green179f8312007-07-10 19:29:38 +0200207{
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100208 while (1) {
Andy Green179f8312007-07-10 19:29:38 +0200209 int hit = 0;
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200210 int pad, align, size, subns;
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100211 uint32_t oui;
Andy Green179f8312007-07-10 19:29:38 +0200212
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100213 /* if no more EXT bits, that's it */
214 if ((iterator->_arg_index % 32) == IEEE80211_RADIOTAP_EXT &&
215 !(iterator->_bitmap_shifter & 1))
216 return -ENOENT;
217
218 if (!(iterator->_bitmap_shifter & 1))
Andy Green179f8312007-07-10 19:29:38 +0200219 goto next_entry; /* arg not present */
220
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100221 /* get alignment/size of data */
222 switch (iterator->_arg_index % 32) {
223 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
224 case IEEE80211_RADIOTAP_EXT:
225 align = 1;
226 size = 0;
227 break;
228 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
229 align = 2;
230 size = 6;
231 break;
232 default:
233 if (!iterator->current_namespace ||
234 iterator->_arg_index >= iterator->current_namespace->n_bits) {
235 if (iterator->current_namespace == &radiotap_ns)
236 return -ENOENT;
237 align = 0;
238 } else {
239 align = iterator->current_namespace->align_size[iterator->_arg_index].align;
240 size = iterator->current_namespace->align_size[iterator->_arg_index].size;
241 }
242 if (!align) {
243 /* skip all subsequent data */
244 iterator->_arg = iterator->_next_ns_data;
245 /* give up on this namespace */
246 iterator->current_namespace = NULL;
247 goto next_entry;
248 }
249 break;
250 }
251
Andy Green179f8312007-07-10 19:29:38 +0200252 /*
253 * arg is present, account for alignment padding
Andy Green179f8312007-07-10 19:29:38 +0200254 *
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100255 * Note that these alignments are relative to the start
256 * of the radiotap header. There is no guarantee
Andy Green179f8312007-07-10 19:29:38 +0200257 * that the radiotap header itself is aligned on any
258 * kind of boundary.
259 *
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100260 * The above is why get_unaligned() is used to dereference
261 * multibyte elements from the radiotap area.
Andy Green179f8312007-07-10 19:29:38 +0200262 */
263
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100264 pad = ((unsigned long)iterator->_arg -
265 (unsigned long)iterator->_rtheader) & (align - 1);
Andy Green179f8312007-07-10 19:29:38 +0200266
267 if (pad)
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100268 iterator->_arg += align - pad;
Andy Green179f8312007-07-10 19:29:38 +0200269
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200270 if (iterator->_arg_index % 32 == IEEE80211_RADIOTAP_VENDOR_NAMESPACE) {
271 int vnslen;
272
273 if ((unsigned long)iterator->_arg + size -
274 (unsigned long)iterator->_rtheader >
275 (unsigned long)iterator->_max_length)
276 return -EINVAL;
277
278 oui = (*iterator->_arg << 16) |
279 (*(iterator->_arg + 1) << 8) |
280 *(iterator->_arg + 2);
281 subns = *(iterator->_arg + 3);
282
283 find_ns(iterator, oui, subns);
284
285 vnslen = get_unaligned_le16(iterator->_arg + 4);
286 iterator->_next_ns_data = iterator->_arg + size + vnslen;
287 if (!iterator->current_namespace)
288 size += vnslen;
289 }
290
Andy Green179f8312007-07-10 19:29:38 +0200291 /*
292 * this is what we will return to user, but we need to
293 * move on first so next call has something fresh to test
294 */
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100295 iterator->this_arg_index = iterator->_arg_index;
296 iterator->this_arg = iterator->_arg;
297 iterator->this_arg_size = size;
Andy Green179f8312007-07-10 19:29:38 +0200298
299 /* internally move on the size of this arg */
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100300 iterator->_arg += size;
Andy Green179f8312007-07-10 19:29:38 +0200301
302 /*
303 * check for insanity where we are given a bitmap that
304 * claims to have more arg content than the length of the
305 * radiotap section. We will normally end up equalling this
306 * max_length on the last arg, never exceeding it.
307 */
308
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100309 if ((unsigned long)iterator->_arg -
310 (unsigned long)iterator->_rtheader >
311 (unsigned long)iterator->_max_length)
Andy Green179f8312007-07-10 19:29:38 +0200312 return -EINVAL;
313
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100314 /* these special ones are valid in each bitmap word */
315 switch (iterator->_arg_index % 32) {
316 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100317 iterator->_reset_on_ext = 1;
318
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100319 iterator->is_radiotap_ns = 0;
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200320 /*
321 * If parser didn't register this vendor
322 * namespace with us, allow it to show it
323 * as 'raw. Do do that, set argument index
324 * to vendor namespace.
325 */
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100326 iterator->this_arg_index =
327 IEEE80211_RADIOTAP_VENDOR_NAMESPACE;
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200328 if (!iterator->current_namespace)
329 hit = 1;
330 goto next_entry;
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100331 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100332 iterator->_reset_on_ext = 1;
333 iterator->current_namespace = &radiotap_ns;
334 iterator->is_radiotap_ns = 1;
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200335 goto next_entry;
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100336 case IEEE80211_RADIOTAP_EXT:
337 /*
338 * bit 31 was set, there is more
339 * -- move to next u32 bitmap
340 */
341 iterator->_bitmap_shifter =
342 get_unaligned_le32(iterator->_next_bitmap);
343 iterator->_next_bitmap++;
344 if (iterator->_reset_on_ext)
345 iterator->_arg_index = 0;
346 else
347 iterator->_arg_index++;
348 iterator->_reset_on_ext = 0;
349 break;
350 default:
351 /* we've got a hit! */
352 hit = 1;
353 next_entry:
354 iterator->_bitmap_shifter >>= 1;
355 iterator->_arg_index++;
356 }
Andy Green179f8312007-07-10 19:29:38 +0200357
358 /* if we found a valid arg earlier, return it now */
359 if (hit)
360 return 0;
361 }
Andy Green179f8312007-07-10 19:29:38 +0200362}
363EXPORT_SYMBOL(ieee80211_radiotap_iterator_next);