blob: a271c27fac774ce987c0db6f1330ffbfca6dc7f7 [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, },
Johannes Berg48613ec2012-07-05 11:32:16 +020044 [IEEE80211_RADIOTAP_MCS] = { .align = 1, .size = 3, },
45 [IEEE80211_RADIOTAP_AMPDU_STATUS] = { .align = 4, .size = 8, },
Johannes Berg33e5a2f2010-02-03 10:24:30 +010046 /*
47 * add more here as they are defined in radiotap.h
48 */
49};
50
51static const struct ieee80211_radiotap_namespace radiotap_ns = {
Nikitas Angelinas94262312010-09-08 22:29:53 +010052 .n_bits = ARRAY_SIZE(rtap_namespace_sizes),
Johannes Berg33e5a2f2010-02-03 10:24:30 +010053 .align_size = rtap_namespace_sizes,
54};
55
Andy Green179f8312007-07-10 19:29:38 +020056/**
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
95int ieee80211_radiotap_iterator_init(
Johannes Berg33e5a2f2010-02-03 10:24:30 +010096 struct ieee80211_radiotap_iterator *iterator,
97 struct ieee80211_radiotap_header *radiotap_header,
98 int max_length, const struct ieee80211_radiotap_vendor_namespaces *vns)
Andy Green179f8312007-07-10 19:29:38 +020099{
Johannes Bergf5563312013-10-11 14:47:05 +0200100 /* check the radiotap header can actually be present */
101 if (max_length < sizeof(struct ieee80211_radiotap_header))
102 return -EINVAL;
103
Andy Green179f8312007-07-10 19:29:38 +0200104 /* 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 */
Harvey Harrisonae7245c2008-05-01 22:19:33 -0700109 if (max_length < get_unaligned_le16(&radiotap_header->it_len))
Andy Green179f8312007-07-10 19:29:38 +0200110 return -EINVAL;
111
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100112 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;
Andy Green179f8312007-07-10 19:29:38 +0200123
124 /* find payload start allowing for extended bitmap(s) */
125
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100126 if (iterator->_bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT)) {
127 while (get_unaligned_le32(iterator->_arg) &
128 (1 << IEEE80211_RADIOTAP_EXT)) {
129 iterator->_arg += sizeof(uint32_t);
Andy Green179f8312007-07-10 19:29:38 +0200130
131 /*
132 * check for insanity where the present bitmaps
133 * keep claiming to extend up to or even beyond the
134 * stated radiotap header length
135 */
136
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100137 if ((unsigned long)iterator->_arg -
Johannes Bergf5563312013-10-11 14:47:05 +0200138 (unsigned long)iterator->_rtheader +
139 sizeof(uint32_t) >
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100140 (unsigned long)iterator->_max_length)
Andy Green179f8312007-07-10 19:29:38 +0200141 return -EINVAL;
142 }
143
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100144 iterator->_arg += sizeof(uint32_t);
Andy Green179f8312007-07-10 19:29:38 +0200145
146 /*
147 * no need to check again for blowing past stated radiotap
148 * header length, because ieee80211_radiotap_iterator_next
149 * checks it before it is dereferenced
150 */
151 }
152
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100153 iterator->this_arg = iterator->_arg;
154
Andy Green179f8312007-07-10 19:29:38 +0200155 /* we are all initialized happily */
156
157 return 0;
158}
159EXPORT_SYMBOL(ieee80211_radiotap_iterator_init);
160
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100161static void find_ns(struct ieee80211_radiotap_iterator *iterator,
162 uint32_t oui, uint8_t subns)
163{
164 int i;
165
166 iterator->current_namespace = NULL;
167
168 if (!iterator->_vns)
169 return;
170
171 for (i = 0; i < iterator->_vns->n_ns; i++) {
172 if (iterator->_vns->ns[i].oui != oui)
173 continue;
174 if (iterator->_vns->ns[i].subns != subns)
175 continue;
176
177 iterator->current_namespace = &iterator->_vns->ns[i];
178 break;
179 }
180}
181
182
Andy Green179f8312007-07-10 19:29:38 +0200183
184/**
185 * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
186 * @iterator: radiotap_iterator to move to next arg (if any)
187 *
188 * Returns: 0 if there is an argument to handle,
189 * -ENOENT if there are no more args or -EINVAL
190 * if there is something else wrong.
191 *
192 * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
193 * in @this_arg_index and sets @this_arg to point to the
194 * payload for the field. It takes care of alignment handling and extended
195 * present fields. @this_arg can be changed by the caller (eg,
196 * incremented to move inside a compound argument like
197 * IEEE80211_RADIOTAP_CHANNEL). The args pointed to are in
198 * little-endian format whatever the endianess of your CPU.
199 *
200 * Alignment Gotcha:
201 * You must take care when dereferencing iterator.this_arg
202 * for multibyte types... the pointer is not aligned. Use
203 * get_unaligned((type *)iterator.this_arg) to dereference
204 * iterator.this_arg for type "type" safely on all arches.
205 */
206
207int ieee80211_radiotap_iterator_next(
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100208 struct ieee80211_radiotap_iterator *iterator)
Andy Green179f8312007-07-10 19:29:38 +0200209{
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100210 while (1) {
Andy Green179f8312007-07-10 19:29:38 +0200211 int hit = 0;
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200212 int pad, align, size, subns;
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100213 uint32_t oui;
Andy Green179f8312007-07-10 19:29:38 +0200214
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100215 /* if no more EXT bits, that's it */
216 if ((iterator->_arg_index % 32) == IEEE80211_RADIOTAP_EXT &&
217 !(iterator->_bitmap_shifter & 1))
218 return -ENOENT;
219
220 if (!(iterator->_bitmap_shifter & 1))
Andy Green179f8312007-07-10 19:29:38 +0200221 goto next_entry; /* arg not present */
222
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100223 /* get alignment/size of data */
224 switch (iterator->_arg_index % 32) {
225 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
226 case IEEE80211_RADIOTAP_EXT:
227 align = 1;
228 size = 0;
229 break;
230 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
231 align = 2;
232 size = 6;
233 break;
234 default:
235 if (!iterator->current_namespace ||
236 iterator->_arg_index >= iterator->current_namespace->n_bits) {
237 if (iterator->current_namespace == &radiotap_ns)
238 return -ENOENT;
239 align = 0;
240 } else {
241 align = iterator->current_namespace->align_size[iterator->_arg_index].align;
242 size = iterator->current_namespace->align_size[iterator->_arg_index].size;
243 }
244 if (!align) {
245 /* skip all subsequent data */
246 iterator->_arg = iterator->_next_ns_data;
247 /* give up on this namespace */
248 iterator->current_namespace = NULL;
249 goto next_entry;
250 }
251 break;
252 }
253
Andy Green179f8312007-07-10 19:29:38 +0200254 /*
255 * arg is present, account for alignment padding
Andy Green179f8312007-07-10 19:29:38 +0200256 *
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100257 * Note that these alignments are relative to the start
258 * of the radiotap header. There is no guarantee
Andy Green179f8312007-07-10 19:29:38 +0200259 * that the radiotap header itself is aligned on any
260 * kind of boundary.
261 *
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100262 * The above is why get_unaligned() is used to dereference
263 * multibyte elements from the radiotap area.
Andy Green179f8312007-07-10 19:29:38 +0200264 */
265
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100266 pad = ((unsigned long)iterator->_arg -
267 (unsigned long)iterator->_rtheader) & (align - 1);
Andy Green179f8312007-07-10 19:29:38 +0200268
269 if (pad)
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100270 iterator->_arg += align - pad;
Andy Green179f8312007-07-10 19:29:38 +0200271
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200272 if (iterator->_arg_index % 32 == IEEE80211_RADIOTAP_VENDOR_NAMESPACE) {
273 int vnslen;
274
275 if ((unsigned long)iterator->_arg + size -
276 (unsigned long)iterator->_rtheader >
277 (unsigned long)iterator->_max_length)
278 return -EINVAL;
279
280 oui = (*iterator->_arg << 16) |
281 (*(iterator->_arg + 1) << 8) |
282 *(iterator->_arg + 2);
283 subns = *(iterator->_arg + 3);
284
285 find_ns(iterator, oui, subns);
286
287 vnslen = get_unaligned_le16(iterator->_arg + 4);
288 iterator->_next_ns_data = iterator->_arg + size + vnslen;
289 if (!iterator->current_namespace)
290 size += vnslen;
291 }
292
Andy Green179f8312007-07-10 19:29:38 +0200293 /*
294 * this is what we will return to user, but we need to
295 * move on first so next call has something fresh to test
296 */
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100297 iterator->this_arg_index = iterator->_arg_index;
298 iterator->this_arg = iterator->_arg;
299 iterator->this_arg_size = size;
Andy Green179f8312007-07-10 19:29:38 +0200300
301 /* internally move on the size of this arg */
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100302 iterator->_arg += size;
Andy Green179f8312007-07-10 19:29:38 +0200303
304 /*
305 * check for insanity where we are given a bitmap that
306 * claims to have more arg content than the length of the
307 * radiotap section. We will normally end up equalling this
308 * max_length on the last arg, never exceeding it.
309 */
310
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100311 if ((unsigned long)iterator->_arg -
312 (unsigned long)iterator->_rtheader >
313 (unsigned long)iterator->_max_length)
Andy Green179f8312007-07-10 19:29:38 +0200314 return -EINVAL;
315
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100316 /* these special ones are valid in each bitmap word */
317 switch (iterator->_arg_index % 32) {
318 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100319 iterator->_reset_on_ext = 1;
320
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100321 iterator->is_radiotap_ns = 0;
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200322 /*
323 * If parser didn't register this vendor
324 * namespace with us, allow it to show it
325 * as 'raw. Do do that, set argument index
326 * to vendor namespace.
327 */
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100328 iterator->this_arg_index =
329 IEEE80211_RADIOTAP_VENDOR_NAMESPACE;
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200330 if (!iterator->current_namespace)
331 hit = 1;
332 goto next_entry;
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100333 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100334 iterator->_reset_on_ext = 1;
335 iterator->current_namespace = &radiotap_ns;
336 iterator->is_radiotap_ns = 1;
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200337 goto next_entry;
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100338 case IEEE80211_RADIOTAP_EXT:
339 /*
340 * bit 31 was set, there is more
341 * -- move to next u32 bitmap
342 */
343 iterator->_bitmap_shifter =
344 get_unaligned_le32(iterator->_next_bitmap);
345 iterator->_next_bitmap++;
346 if (iterator->_reset_on_ext)
347 iterator->_arg_index = 0;
348 else
349 iterator->_arg_index++;
350 iterator->_reset_on_ext = 0;
351 break;
352 default:
353 /* we've got a hit! */
354 hit = 1;
355 next_entry:
356 iterator->_bitmap_shifter >>= 1;
357 iterator->_arg_index++;
358 }
Andy Green179f8312007-07-10 19:29:38 +0200359
360 /* if we found a valid arg earlier, return it now */
361 if (hit)
362 return 0;
363 }
Andy Green179f8312007-07-10 19:29:38 +0200364}
365EXPORT_SYMBOL(ieee80211_radiotap_iterator_next);