4 * Copyright 2007 Andy Green <andy@warmcat.com>
5 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
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.
11 * Alternatively, this software may be distributed under the terms of BSD
14 * See COPYING for more details.
16 #include "radiotap_iter.h"
19 /* function prototypes and related defs are in radiotap_iter.h */
21 static const struct radiotap_align_size rtap_namespace_sizes[] = {
22 [IEEE80211_RADIOTAP_TSFT] = { .align = 8, .size = 8, },
23 [IEEE80211_RADIOTAP_FLAGS] = { .align = 1, .size = 1, },
24 [IEEE80211_RADIOTAP_RATE] = { .align = 1, .size = 1, },
25 [IEEE80211_RADIOTAP_CHANNEL] = { .align = 2, .size = 4, },
26 [IEEE80211_RADIOTAP_FHSS] = { .align = 2, .size = 2, },
27 [IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = { .align = 1, .size = 1, },
28 [IEEE80211_RADIOTAP_DBM_ANTNOISE] = { .align = 1, .size = 1, },
29 [IEEE80211_RADIOTAP_LOCK_QUALITY] = { .align = 2, .size = 2, },
30 [IEEE80211_RADIOTAP_TX_ATTENUATION] = { .align = 2, .size = 2, },
31 [IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = { .align = 2, .size = 2, },
32 [IEEE80211_RADIOTAP_DBM_TX_POWER] = { .align = 1, .size = 1, },
33 [IEEE80211_RADIOTAP_ANTENNA] = { .align = 1, .size = 1, },
34 [IEEE80211_RADIOTAP_DB_ANTSIGNAL] = { .align = 1, .size = 1, },
35 [IEEE80211_RADIOTAP_DB_ANTNOISE] = { .align = 1, .size = 1, },
36 [IEEE80211_RADIOTAP_RX_FLAGS] = { .align = 2, .size = 2, },
37 [IEEE80211_RADIOTAP_TX_FLAGS] = { .align = 2, .size = 2, },
38 [IEEE80211_RADIOTAP_RTS_RETRIES] = { .align = 1, .size = 1, },
39 [IEEE80211_RADIOTAP_DATA_RETRIES] = { .align = 1, .size = 1, },
41 * add more here as they are defined in radiotap.h
45 static const struct ieee80211_radiotap_namespace radiotap_ns = {
46 .n_bits = sizeof(rtap_namespace_sizes) / sizeof(rtap_namespace_sizes[0]),
47 .align_size = rtap_namespace_sizes,
51 * ieee80211_radiotap_iterator_init - radiotap parser iterator initialization
52 * @iterator: radiotap_iterator to initialize
53 * @radiotap_header: radiotap header to parse
54 * @max_length: total length we can parse into (eg, whole packet length)
56 * Returns: 0 or a negative error code if there is a problem.
58 * This function initializes an opaque iterator struct which can then
59 * be passed to ieee80211_radiotap_iterator_next() to visit every radiotap
60 * argument which is present in the header. It knows about extended
61 * present headers and handles them.
64 * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
65 * struct ieee80211_radiotap_iterator (no need to init the struct beforehand)
66 * checking for a good 0 return code. Then loop calling
67 * __ieee80211_radiotap_iterator_next()... it returns either 0,
68 * -ENOENT if there are no more args to parse, or -EINVAL if there is a problem.
69 * The iterator's @this_arg member points to the start of the argument
70 * associated with the current argument index that is present, which can be
71 * found in the iterator's @this_arg_index member. This arg index corresponds
72 * to the IEEE80211_RADIOTAP_... defines.
74 * Radiotap header length:
75 * You can find the CPU-endian total radiotap header length in
76 * iterator->max_length after executing ieee80211_radiotap_iterator_init()
80 * You must take care when dereferencing iterator.this_arg
81 * for multibyte types... the pointer is not aligned. Use
82 * get_unaligned((type *)iterator.this_arg) to dereference
83 * iterator.this_arg for type "type" safely on all arches.
85 * Example code: parse.c
88 int ieee80211_radiotap_iterator_init(
89 struct ieee80211_radiotap_iterator *iterator,
90 struct ieee80211_radiotap_header *radiotap_header,
91 int max_length, const struct ieee80211_radiotap_vendor_namespaces *vns)
93 /* Linux only supports version 0 radiotap format */
94 if (radiotap_header->it_version)
97 /* sanity check for allowed length and radiotap length field */
98 if (max_length < get_unaligned_le16(&radiotap_header->it_len))
101 iterator->_rtheader = radiotap_header;
102 iterator->_max_length = get_unaligned_le16(&radiotap_header->it_len);
103 iterator->_arg_index = 0;
104 iterator->_bitmap_shifter = get_unaligned_le32(&radiotap_header->it_present);
105 iterator->_arg = (uint8_t *)radiotap_header + sizeof(*radiotap_header);
106 iterator->_reset_on_ext = 0;
107 iterator->_next_bitmap = &radiotap_header->it_present;
108 iterator->_next_bitmap++;
109 iterator->_vns = vns;
110 iterator->current_namespace = &radiotap_ns;
111 iterator->is_radiotap_ns = 1;
112 #ifdef RADIOTAP_SUPPORT_OVERRIDES
113 iterator->n_overrides = 0;
114 iterator->overrides = NULL;
117 /* find payload start allowing for extended bitmap(s) */
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);
125 * check for insanity where the present bitmaps
126 * keep claiming to extend up to or even beyond the
127 * stated radiotap header length
130 if ((unsigned long)iterator->_arg -
131 (unsigned long)iterator->_rtheader >
132 (unsigned long)iterator->_max_length)
136 iterator->_arg += sizeof(uint32_t);
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
145 iterator->this_arg = iterator->_arg;
147 /* we are all initialized happily */
152 static void find_ns(struct ieee80211_radiotap_iterator *iterator,
153 uint32_t oui, uint8_t subns)
157 iterator->current_namespace = NULL;
162 for (i = 0; i < iterator->_vns->n_ns; i++) {
163 if (iterator->_vns->ns[i].oui != oui)
165 if (iterator->_vns->ns[i].subns != subns)
168 iterator->current_namespace = &iterator->_vns->ns[i];
173 #ifdef RADIOTAP_SUPPORT_OVERRIDES
174 static int find_override(struct ieee80211_radiotap_iterator *iterator,
175 int *align, int *size)
179 if (!iterator->overrides)
182 for (i = 0; i < iterator->n_overrides; i++) {
183 if (iterator->_arg_index == iterator->overrides[i].field) {
184 *align = iterator->overrides[i].align;
185 *size = iterator->overrides[i].size;
186 if (!*align) /* erroneous override */
198 * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
199 * @iterator: radiotap_iterator to move to next arg (if any)
201 * Returns: 0 if there is an argument to handle,
202 * -ENOENT if there are no more args or -EINVAL
203 * if there is something else wrong.
205 * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
206 * in @this_arg_index and sets @this_arg to point to the
207 * payload for the field. It takes care of alignment handling and extended
208 * present fields. @this_arg can be changed by the caller (eg,
209 * incremented to move inside a compound argument like
210 * IEEE80211_RADIOTAP_CHANNEL). The args pointed to are in
211 * little-endian format whatever the endianess of your CPU.
214 * You must take care when dereferencing iterator.this_arg
215 * for multibyte types... the pointer is not aligned. Use
216 * get_unaligned((type *)iterator.this_arg) to dereference
217 * iterator.this_arg for type "type" safely on all arches.
220 int ieee80211_radiotap_iterator_next(
221 struct ieee80211_radiotap_iterator *iterator)
225 int pad, align, size, subns, vnslen;
228 /* if no more EXT bits, that's it */
229 if ((iterator->_arg_index % 32) == IEEE80211_RADIOTAP_EXT &&
230 !(iterator->_bitmap_shifter & 1))
233 if (!(iterator->_bitmap_shifter & 1))
234 goto next_entry; /* arg not present */
236 /* get alignment/size of data */
237 switch (iterator->_arg_index % 32) {
238 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
239 case IEEE80211_RADIOTAP_EXT:
243 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
248 #ifdef RADIOTAP_SUPPORT_OVERRIDES
249 if (find_override(iterator, &align, &size)) {
253 if (!iterator->current_namespace ||
254 iterator->_arg_index >= iterator->current_namespace->n_bits) {
255 if (iterator->current_namespace == &radiotap_ns)
259 align = iterator->current_namespace->align_size[iterator->_arg_index].align;
260 size = iterator->current_namespace->align_size[iterator->_arg_index].size;
263 /* skip all subsequent data */
264 iterator->_arg = iterator->_next_ns_data;
265 /* give up on this namespace */
266 iterator->current_namespace = NULL;
273 * arg is present, account for alignment padding
275 * Note that these alignments are relative to the start
276 * of the radiotap header. There is no guarantee
277 * that the radiotap header itself is aligned on any
280 * The above is why get_unaligned() is used to dereference
281 * multibyte elements from the radiotap area.
284 pad = ((unsigned long)iterator->_arg -
285 (unsigned long)iterator->_rtheader) & (align - 1);
288 iterator->_arg += align - pad;
291 * this is what we will return to user, but we need to
292 * move on first so next call has something fresh to test
294 iterator->this_arg_index = iterator->_arg_index;
295 iterator->this_arg = iterator->_arg;
296 iterator->this_arg_size = size;
298 /* internally move on the size of this arg */
299 iterator->_arg += size;
302 * check for insanity where we are given a bitmap that
303 * claims to have more arg content than the length of the
304 * radiotap section. We will normally end up equalling this
305 * max_length on the last arg, never exceeding it.
308 if ((unsigned long)iterator->_arg -
309 (unsigned long)iterator->_rtheader >
310 (unsigned long)iterator->_max_length)
313 /* these special ones are valid in each bitmap word */
314 switch (iterator->_arg_index % 32) {
315 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
316 iterator->_bitmap_shifter >>= 1;
317 iterator->_arg_index++;
319 iterator->_reset_on_ext = 1;
321 vnslen = get_unaligned_le16(iterator->this_arg + 4);
322 iterator->_next_ns_data = iterator->_arg + vnslen;
323 oui = (*iterator->this_arg << 16) |
324 (*(iterator->this_arg + 1) << 8) |
325 *(iterator->this_arg + 2);
326 subns = *(iterator->this_arg + 3);
328 find_ns(iterator, oui, subns);
330 iterator->is_radiotap_ns = 0;
331 /* allow parsers to show this information */
332 iterator->this_arg_index =
333 IEEE80211_RADIOTAP_VENDOR_NAMESPACE;
334 iterator->this_arg_size += vnslen;
335 if ((unsigned long)iterator->this_arg +
336 iterator->this_arg_size -
337 (unsigned long)iterator->_rtheader >
338 (unsigned long)(unsigned long)iterator->_max_length)
342 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
343 iterator->_bitmap_shifter >>= 1;
344 iterator->_arg_index++;
346 iterator->_reset_on_ext = 1;
347 iterator->current_namespace = &radiotap_ns;
348 iterator->is_radiotap_ns = 1;
350 case IEEE80211_RADIOTAP_EXT:
352 * bit 31 was set, there is more
353 * -- move to next u32 bitmap
355 iterator->_bitmap_shifter =
356 get_unaligned_le32(iterator->_next_bitmap);
357 iterator->_next_bitmap++;
358 if (iterator->_reset_on_ext)
359 iterator->_arg_index = 0;
361 iterator->_arg_index++;
362 iterator->_reset_on_ext = 0;
365 /* we've got a hit! */
368 iterator->_bitmap_shifter >>= 1;
369 iterator->_arg_index++;
372 /* if we found a valid arg earlier, return it now */