blob: 6582d155e2fce5417bc498a438ae9a19a3eca35f [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, },
Lorenzo Bianconi7837a772016-02-19 11:43:04 +010046 [IEEE80211_RADIOTAP_VHT] = { .align = 2, .size = 12, },
Johannes Berg33e5a2f2010-02-03 10:24:30 +010047 /*
48 * add more here as they are defined in radiotap.h
49 */
50};
51
52static const struct ieee80211_radiotap_namespace radiotap_ns = {
Nikitas Angelinas94262312010-09-08 22:29:53 +010053 .n_bits = ARRAY_SIZE(rtap_namespace_sizes),
Johannes Berg33e5a2f2010-02-03 10:24:30 +010054 .align_size = rtap_namespace_sizes,
55};
56
Andy Green179f8312007-07-10 19:29:38 +020057/**
58 * ieee80211_radiotap_iterator_init - radiotap parser iterator initialization
59 * @iterator: radiotap_iterator to initialize
60 * @radiotap_header: radiotap header to parse
61 * @max_length: total length we can parse into (eg, whole packet length)
62 *
63 * Returns: 0 or a negative error code if there is a problem.
64 *
65 * This function initializes an opaque iterator struct which can then
66 * be passed to ieee80211_radiotap_iterator_next() to visit every radiotap
67 * argument which is present in the header. It knows about extended
68 * present headers and handles them.
69 *
70 * How to use:
71 * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
72 * struct ieee80211_radiotap_iterator (no need to init the struct beforehand)
73 * checking for a good 0 return code. Then loop calling
74 * __ieee80211_radiotap_iterator_next()... it returns either 0,
75 * -ENOENT if there are no more args to parse, or -EINVAL if there is a problem.
76 * The iterator's @this_arg member points to the start of the argument
77 * associated with the current argument index that is present, which can be
78 * found in the iterator's @this_arg_index member. This arg index corresponds
79 * to the IEEE80211_RADIOTAP_... defines.
80 *
81 * Radiotap header length:
82 * You can find the CPU-endian total radiotap header length in
83 * iterator->max_length after executing ieee80211_radiotap_iterator_init()
84 * successfully.
85 *
86 * Alignment Gotcha:
87 * You must take care when dereferencing iterator.this_arg
88 * for multibyte types... the pointer is not aligned. Use
89 * get_unaligned((type *)iterator.this_arg) to dereference
90 * iterator.this_arg for type "type" safely on all arches.
91 *
92 * Example code:
93 * See Documentation/networking/radiotap-headers.txt
94 */
95
96int ieee80211_radiotap_iterator_init(
Johannes Berg33e5a2f2010-02-03 10:24:30 +010097 struct ieee80211_radiotap_iterator *iterator,
98 struct ieee80211_radiotap_header *radiotap_header,
99 int max_length, const struct ieee80211_radiotap_vendor_namespaces *vns)
Andy Green179f8312007-07-10 19:29:38 +0200100{
Johannes Bergf5563312013-10-11 14:47:05 +0200101 /* check the radiotap header can actually be present */
102 if (max_length < sizeof(struct ieee80211_radiotap_header))
103 return -EINVAL;
104
Andy Green179f8312007-07-10 19:29:38 +0200105 /* Linux only supports version 0 radiotap format */
106 if (radiotap_header->it_version)
107 return -EINVAL;
108
109 /* sanity check for allowed length and radiotap length field */
Harvey Harrisonae7245c2008-05-01 22:19:33 -0700110 if (max_length < get_unaligned_le16(&radiotap_header->it_len))
Andy Green179f8312007-07-10 19:29:38 +0200111 return -EINVAL;
112
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100113 iterator->_rtheader = radiotap_header;
114 iterator->_max_length = get_unaligned_le16(&radiotap_header->it_len);
115 iterator->_arg_index = 0;
116 iterator->_bitmap_shifter = get_unaligned_le32(&radiotap_header->it_present);
117 iterator->_arg = (uint8_t *)radiotap_header + sizeof(*radiotap_header);
118 iterator->_reset_on_ext = 0;
119 iterator->_next_bitmap = &radiotap_header->it_present;
120 iterator->_next_bitmap++;
121 iterator->_vns = vns;
122 iterator->current_namespace = &radiotap_ns;
123 iterator->is_radiotap_ns = 1;
Andy Green179f8312007-07-10 19:29:38 +0200124
125 /* find payload start allowing for extended bitmap(s) */
126
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100127 if (iterator->_bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT)) {
Johannes Bergbd02cd22013-12-16 12:04:36 +0100128 if ((unsigned long)iterator->_arg -
129 (unsigned long)iterator->_rtheader + sizeof(uint32_t) >
130 (unsigned long)iterator->_max_length)
131 return -EINVAL;
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100132 while (get_unaligned_le32(iterator->_arg) &
133 (1 << IEEE80211_RADIOTAP_EXT)) {
134 iterator->_arg += sizeof(uint32_t);
Andy Green179f8312007-07-10 19:29:38 +0200135
136 /*
137 * check for insanity where the present bitmaps
138 * keep claiming to extend up to or even beyond the
139 * stated radiotap header length
140 */
141
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100142 if ((unsigned long)iterator->_arg -
Johannes Bergf5563312013-10-11 14:47:05 +0200143 (unsigned long)iterator->_rtheader +
144 sizeof(uint32_t) >
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100145 (unsigned long)iterator->_max_length)
Andy Green179f8312007-07-10 19:29:38 +0200146 return -EINVAL;
147 }
148
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100149 iterator->_arg += sizeof(uint32_t);
Andy Green179f8312007-07-10 19:29:38 +0200150
151 /*
152 * no need to check again for blowing past stated radiotap
153 * header length, because ieee80211_radiotap_iterator_next
154 * checks it before it is dereferenced
155 */
156 }
157
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100158 iterator->this_arg = iterator->_arg;
159
Andy Green179f8312007-07-10 19:29:38 +0200160 /* we are all initialized happily */
161
162 return 0;
163}
164EXPORT_SYMBOL(ieee80211_radiotap_iterator_init);
165
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100166static void find_ns(struct ieee80211_radiotap_iterator *iterator,
167 uint32_t oui, uint8_t subns)
168{
169 int i;
170
171 iterator->current_namespace = NULL;
172
173 if (!iterator->_vns)
174 return;
175
176 for (i = 0; i < iterator->_vns->n_ns; i++) {
177 if (iterator->_vns->ns[i].oui != oui)
178 continue;
179 if (iterator->_vns->ns[i].subns != subns)
180 continue;
181
182 iterator->current_namespace = &iterator->_vns->ns[i];
183 break;
184 }
185}
186
187
Andy Green179f8312007-07-10 19:29:38 +0200188
189/**
190 * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
191 * @iterator: radiotap_iterator to move to next arg (if any)
192 *
193 * Returns: 0 if there is an argument to handle,
194 * -ENOENT if there are no more args or -EINVAL
195 * if there is something else wrong.
196 *
197 * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
198 * in @this_arg_index and sets @this_arg to point to the
199 * payload for the field. It takes care of alignment handling and extended
200 * present fields. @this_arg can be changed by the caller (eg,
201 * incremented to move inside a compound argument like
202 * IEEE80211_RADIOTAP_CHANNEL). The args pointed to are in
203 * little-endian format whatever the endianess of your CPU.
204 *
205 * Alignment Gotcha:
206 * You must take care when dereferencing iterator.this_arg
207 * for multibyte types... the pointer is not aligned. Use
208 * get_unaligned((type *)iterator.this_arg) to dereference
209 * iterator.this_arg for type "type" safely on all arches.
210 */
211
212int ieee80211_radiotap_iterator_next(
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100213 struct ieee80211_radiotap_iterator *iterator)
Andy Green179f8312007-07-10 19:29:38 +0200214{
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100215 while (1) {
Andy Green179f8312007-07-10 19:29:38 +0200216 int hit = 0;
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200217 int pad, align, size, subns;
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100218 uint32_t oui;
Andy Green179f8312007-07-10 19:29:38 +0200219
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100220 /* if no more EXT bits, that's it */
221 if ((iterator->_arg_index % 32) == IEEE80211_RADIOTAP_EXT &&
222 !(iterator->_bitmap_shifter & 1))
223 return -ENOENT;
224
225 if (!(iterator->_bitmap_shifter & 1))
Andy Green179f8312007-07-10 19:29:38 +0200226 goto next_entry; /* arg not present */
227
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100228 /* get alignment/size of data */
229 switch (iterator->_arg_index % 32) {
230 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
231 case IEEE80211_RADIOTAP_EXT:
232 align = 1;
233 size = 0;
234 break;
235 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
236 align = 2;
237 size = 6;
238 break;
239 default:
240 if (!iterator->current_namespace ||
241 iterator->_arg_index >= iterator->current_namespace->n_bits) {
242 if (iterator->current_namespace == &radiotap_ns)
243 return -ENOENT;
244 align = 0;
245 } else {
246 align = iterator->current_namespace->align_size[iterator->_arg_index].align;
247 size = iterator->current_namespace->align_size[iterator->_arg_index].size;
248 }
249 if (!align) {
250 /* skip all subsequent data */
251 iterator->_arg = iterator->_next_ns_data;
252 /* give up on this namespace */
253 iterator->current_namespace = NULL;
254 goto next_entry;
255 }
256 break;
257 }
258
Andy Green179f8312007-07-10 19:29:38 +0200259 /*
260 * arg is present, account for alignment padding
Andy Green179f8312007-07-10 19:29:38 +0200261 *
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100262 * Note that these alignments are relative to the start
263 * of the radiotap header. There is no guarantee
Andy Green179f8312007-07-10 19:29:38 +0200264 * that the radiotap header itself is aligned on any
265 * kind of boundary.
266 *
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100267 * The above is why get_unaligned() is used to dereference
268 * multibyte elements from the radiotap area.
Andy Green179f8312007-07-10 19:29:38 +0200269 */
270
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100271 pad = ((unsigned long)iterator->_arg -
272 (unsigned long)iterator->_rtheader) & (align - 1);
Andy Green179f8312007-07-10 19:29:38 +0200273
274 if (pad)
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100275 iterator->_arg += align - pad;
Andy Green179f8312007-07-10 19:29:38 +0200276
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200277 if (iterator->_arg_index % 32 == IEEE80211_RADIOTAP_VENDOR_NAMESPACE) {
278 int vnslen;
279
280 if ((unsigned long)iterator->_arg + size -
281 (unsigned long)iterator->_rtheader >
282 (unsigned long)iterator->_max_length)
283 return -EINVAL;
284
285 oui = (*iterator->_arg << 16) |
286 (*(iterator->_arg + 1) << 8) |
287 *(iterator->_arg + 2);
288 subns = *(iterator->_arg + 3);
289
290 find_ns(iterator, oui, subns);
291
292 vnslen = get_unaligned_le16(iterator->_arg + 4);
293 iterator->_next_ns_data = iterator->_arg + size + vnslen;
294 if (!iterator->current_namespace)
295 size += vnslen;
296 }
297
Andy Green179f8312007-07-10 19:29:38 +0200298 /*
299 * this is what we will return to user, but we need to
300 * move on first so next call has something fresh to test
301 */
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100302 iterator->this_arg_index = iterator->_arg_index;
303 iterator->this_arg = iterator->_arg;
304 iterator->this_arg_size = size;
Andy Green179f8312007-07-10 19:29:38 +0200305
306 /* internally move on the size of this arg */
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100307 iterator->_arg += size;
Andy Green179f8312007-07-10 19:29:38 +0200308
309 /*
310 * check for insanity where we are given a bitmap that
311 * claims to have more arg content than the length of the
312 * radiotap section. We will normally end up equalling this
313 * max_length on the last arg, never exceeding it.
314 */
315
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100316 if ((unsigned long)iterator->_arg -
317 (unsigned long)iterator->_rtheader >
318 (unsigned long)iterator->_max_length)
Andy Green179f8312007-07-10 19:29:38 +0200319 return -EINVAL;
320
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100321 /* these special ones are valid in each bitmap word */
322 switch (iterator->_arg_index % 32) {
323 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100324 iterator->_reset_on_ext = 1;
325
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100326 iterator->is_radiotap_ns = 0;
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200327 /*
328 * If parser didn't register this vendor
329 * namespace with us, allow it to show it
330 * as 'raw. Do do that, set argument index
331 * to vendor namespace.
332 */
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100333 iterator->this_arg_index =
334 IEEE80211_RADIOTAP_VENDOR_NAMESPACE;
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200335 if (!iterator->current_namespace)
336 hit = 1;
337 goto next_entry;
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100338 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100339 iterator->_reset_on_ext = 1;
340 iterator->current_namespace = &radiotap_ns;
341 iterator->is_radiotap_ns = 1;
Johannes Berg9ebad4a2010-10-14 13:41:35 +0200342 goto next_entry;
Johannes Berg33e5a2f2010-02-03 10:24:30 +0100343 case IEEE80211_RADIOTAP_EXT:
344 /*
345 * bit 31 was set, there is more
346 * -- move to next u32 bitmap
347 */
348 iterator->_bitmap_shifter =
349 get_unaligned_le32(iterator->_next_bitmap);
350 iterator->_next_bitmap++;
351 if (iterator->_reset_on_ext)
352 iterator->_arg_index = 0;
353 else
354 iterator->_arg_index++;
355 iterator->_reset_on_ext = 0;
356 break;
357 default:
358 /* we've got a hit! */
359 hit = 1;
360 next_entry:
361 iterator->_bitmap_shifter >>= 1;
362 iterator->_arg_index++;
363 }
Andy Green179f8312007-07-10 19:29:38 +0200364
365 /* if we found a valid arg earlier, return it now */
366 if (hit)
367 return 0;
368 }
Andy Green179f8312007-07-10 19:29:38 +0200369}
370EXPORT_SYMBOL(ieee80211_radiotap_iterator_next);