4 * Copyright 2007 Andy Green <andy@warmcat.com>
5 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
7 * SPDX-License-Identifier: (ISC OR GPL-2.0-only)
14 #include <epan/packet.h>
16 #include <wsutil/pint.h>
17 #include <wsutil/array.h>
19 #define le16_to_cpu GINT16_FROM_LE
20 #define le32_to_cpu GINT32_FROM_LE
21 #define get_unaligned_le16 pletoh16
22 #define get_unaligned_le32 pletoh32
24 #include "packet-ieee80211-radiotap-iter.h"
26 /* function prototypes and related defs are in radiotap_iter.h */
28 static const struct radiotap_align_size rtap_namespace_sizes
[] = {
29 /* [IEEE80211_RADIOTAP_TSFT] = 0 */ { 8, 8 },
30 /* [IEEE80211_RADIOTAP_FLAGS] = 1 */ { 1, 1 },
31 /* [IEEE80211_RADIOTAP_RATE] = 2 */ { 1, 1 },
32 /* [IEEE80211_RADIOTAP_CHANNEL] = 3 */ { 2, 4 },
33 /* [IEEE80211_RADIOTAP_FHSS] = 4 */ { 2, 2 },
34 /* [IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = 5 */ { 1, 1 },
35 /* [IEEE80211_RADIOTAP_DBM_ANTNOISE] = 6 */ { 1, 1 },
36 /* [IEEE80211_RADIOTAP_LOCK_QUALITY] = 7 */ { 2, 2 },
37 /* [IEEE80211_RADIOTAP_TX_ATTENUATION] = 8 */ { 2, 2 },
38 /* [IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = 9 */ { 2, 2 },
39 /* [IEEE80211_RADIOTAP_DBM_TX_POWER] = 10 */ { 1, 1 },
40 /* [IEEE80211_RADIOTAP_ANTENNA] = 11 */ { 1, 1 },
41 /* [IEEE80211_RADIOTAP_DB_ANTSIGNAL] = 12 */ { 1, 1 },
42 /* [IEEE80211_RADIOTAP_DB_ANTNOISE] = 13 */ { 1, 1 },
43 /* [IEEE80211_RADIOTAP_RX_FLAGS] = 14 */ { 2, 2 },
44 /* [IEEE80211_RADIOTAP_TX_FLAGS] = 15 */ { 2, 2 },
45 /* [IEEE80211_RADIOTAP_RTS_RETRIES] = 16 */ { 1, 1 },
46 /* [IEEE80211_RADIOTAP_DATA_RETRIES] = 17 */ { 1, 1 },
47 /* [IEEE80211_RADIOTAP_XCHANNEL] = 18 */ { 0, 0 }, /* Unofficial, used by FreeBSD */
48 /* [IEEE80211_RADIOTAP_MCS] = 19 */ { 1, 3 },
49 /* [IEEE80211_RADIOTAP_AMPDU_STATUS] = 20 */ { 4, 8 },
50 /* [IEEE80211_RADIOTAP_VHT] = 21 */ { 2, 12 },
51 /* [IEEE80211_RADIOTAP_TIMESTAMP] = 22 */ { 8, 12 },
52 /* [IEEE80211_RADIOTAP_HE] = 23 */ { 2, 12 },
53 /* [IEEE80211_RADIOTAP_HE_MU] = 24 */ { 2, 12 },
54 /* [IEEE80211_RADIOTAP_HE_MU_USER = 25 notdef */ { 0, 0 },
55 /* [IEEE80211_RADIOTAP_0_LENGTH_PSDU = 26 */ { 1, 1 },
56 /* [IEEE80211_RADIOTAP_L_SIG = 27 */ { 2, 4 },
57 /* [IEEE80211_RADIOTAP_TLV = 28 */ { 4, 10 },
59 * add more here as they are defined in
60 * include/net/ieee80211_radiotap.h
64 static const struct ieee80211_radiotap_namespace radiotap_ns
= {
66 (int)array_length(rtap_namespace_sizes
),
74 #define ITERATOR_VALID(iterator, size) \
75 (((iterator)->_arg + (size) - (unsigned char *)((iterator)->_rtheader)) <= \
76 (ptrdiff_t)(iterator)->_max_length)
79 * ieee80211_radiotap_iterator_init - radiotap parser iterator initialization
80 * @iterator: radiotap_iterator to initialize
81 * @radiotap_header: radiotap header to parse
82 * @max_length: total length we can parse into (eg, whole packet length)
84 * Returns: 0 or a negative error code if there is a problem.
86 * This function initializes an opaque iterator struct which can then
87 * be passed to ieee80211_radiotap_iterator_next() to visit every radiotap
88 * argument which is present in the header. It knows about extended
89 * present headers and handles them.
92 * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
93 * struct ieee80211_radiotap_iterator (no need to init the struct beforehand)
94 * checking for a good 0 return code. Then loop calling
95 * __ieee80211_radiotap_iterator_next()... it returns either 0,
96 * -ENOENT if there are no more args to parse, or -EINVAL if there is a problem.
97 * The iterator's @this_arg member points to the start of the argument
98 * associated with the current argument index that is present, which can be
99 * found in the iterator's @this_arg_index member. This arg index corresponds
100 * to the IEEE80211_RADIOTAP_... defines.
102 * Radiotap header length:
103 * You can find the CPU-endian total radiotap header length in
104 * iterator->max_length after executing ieee80211_radiotap_iterator_init()
108 * You must take care when dereferencing iterator.this_arg
109 * for multibyte types... the pointer is not aligned. Use
110 * get_unaligned((type *)iterator.this_arg) to dereference
111 * iterator.this_arg for type "type" safely on all arches.
114 * See https://www.kernel.org/doc/Documentation/networking/radiotap-headers.txt
117 int ieee80211_radiotap_iterator_init(
118 struct ieee80211_radiotap_iterator
*iterator
,
119 struct ieee80211_radiotap_header
*radiotap_header
,
120 int max_length
, const struct ieee80211_radiotap_vendor_namespaces
*vns
)
122 /* XXX - in Wireshark, we've already checked for this */
123 if (max_length
< (int)sizeof(struct ieee80211_radiotap_header
))
126 /* Linux only supports version 0 radiotap format */
127 /* XXX - this is Wireshark, not Linux, and we should report an expert info */
128 if (radiotap_header
->it_version
)
131 /* sanity check for allowed length and radiotap length field */
132 /* XXX - in Wireshark, this compares the length against itself. */
133 if (max_length
< get_unaligned_le16(&radiotap_header
->it_len
))
136 iterator
->_rtheader
= radiotap_header
;
137 iterator
->_max_length
= get_unaligned_le16(&radiotap_header
->it_len
);
138 iterator
->_arg_index
= 0;
139 iterator
->_bitmap_shifter
= get_unaligned_le32(&radiotap_header
->it_present
);
140 iterator
->_arg
= (uint8_t *)radiotap_header
+ sizeof(*radiotap_header
);
141 iterator
->_reset_on_ext
= 0;
142 iterator
->_next_ns_data
= NULL
;
143 iterator
->_next_bitmap
= &radiotap_header
->it_present
;
144 iterator
->_next_bitmap
++;
145 iterator
->_vns
= vns
;
146 iterator
->current_namespace
= &radiotap_ns
;
147 iterator
->is_radiotap_ns
= 1;
148 iterator
->tlv_mode
= 0;
149 #ifdef RADIOTAP_SUPPORT_OVERRIDES
150 iterator
->n_overrides
= 0;
151 iterator
->overrides
= NULL
;
154 /* find payload start allowing for extended bitmap(s) */
155 if (iterator
->_bitmap_shifter
& (1U << IEEE80211_RADIOTAP_EXT
)) {
156 /* XXX - we should report an expert info here */
157 if (!ITERATOR_VALID(iterator
, sizeof(uint32_t)))
159 while (get_unaligned_le32(iterator
->_arg
) &
160 (1U << IEEE80211_RADIOTAP_EXT
)) {
161 iterator
->_arg
+= sizeof(uint32_t);
164 * check for insanity where the present bitmaps
165 * keep claiming to extend up to or even beyond the
166 * stated radiotap header length
168 /* XXX - we should report an expert info here */
169 if (!ITERATOR_VALID(iterator
, sizeof(uint32_t)))
172 /* XXX - we should report an expert info here */
173 if ((get_unaligned_le32(iterator
->_arg
) &
174 (1U << IEEE80211_RADIOTAP_TLVS
)) &&
175 (get_unaligned_le32(iterator
->_arg
) &
176 (1U << IEEE80211_RADIOTAP_EXT
)))
180 iterator
->_arg
+= sizeof(uint32_t);
183 * no need to check again for blowing past stated radiotap
184 * header length, because ieee80211_radiotap_iterator_next
185 * checks it before it is dereferenced
189 iterator
->this_arg
= iterator
->_arg
;
191 /* we are all initialized happily */
196 static void find_ns(struct ieee80211_radiotap_iterator
*iterator
,
197 uint32_t oui
, uint8_t subns
)
201 iterator
->current_namespace
= NULL
;
206 for (i
= 0; i
< iterator
->_vns
->n_ns
; i
++) {
207 if (iterator
->_vns
->ns
[i
].oui
!= oui
)
209 if (iterator
->_vns
->ns
[i
].subns
!= subns
)
212 iterator
->current_namespace
= &iterator
->_vns
->ns
[i
];
217 #ifdef RADIOTAP_SUPPORT_OVERRIDES
218 static int find_override(struct ieee80211_radiotap_iterator
*iterator
,
219 int *align
, int *size
)
223 if (!iterator
->overrides
)
226 for (i
= 0; i
< iterator
->n_overrides
; i
++) {
227 if (iterator
->_arg_index
== iterator
->overrides
[i
].field
) {
228 *align
= iterator
->overrides
[i
].align
;
229 *size
= iterator
->overrides
[i
].size
;
230 if (!*align
) /* erroneous override */
242 * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
243 * @iterator: radiotap_iterator to move to next arg (if any)
245 * Returns: 0 if there is an argument to handle,
246 * -ENOENT if there are no more args or -EINVAL
247 * if there is something else wrong.
249 * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
250 * in @this_arg_index and sets @this_arg to point to the
251 * payload for the field. It takes care of alignment handling and extended
252 * present fields. @this_arg can be changed by the caller (eg,
253 * incremented to move inside a compound argument like
254 * IEEE80211_RADIOTAP_CHANNEL). The args pointed to are in
255 * little-endian format whatever the endianess of your CPU.
258 * You must take care when dereferencing iterator.this_arg
259 * for multibyte types... the pointer is not aligned. Use
260 * get_unaligned((type *)iterator.this_arg) to dereference
261 * iterator.this_arg for type "type" safely on all arches.
264 int ieee80211_radiotap_iterator_next(
265 struct ieee80211_radiotap_iterator
*iterator
)
267 if (iterator
->tlv_mode
) {
268 struct ieee80211_radiotap_tlv
*tlv
;
271 #define TLV_LEN_ALIGN(x) ((x + 3) & ~3)
272 size
= sizeof(*tlv
) + TLV_LEN_ALIGN(iterator
->this_arg_size
);
275 * We know that without the alignment padding it was valid, so
276 * ignore arbitrary padding and return that we finished if no
277 * further TLV could fit.
279 if (!ITERATOR_VALID(iterator
, size
))
282 /* move to next entry */
283 iterator
->_arg
+= sizeof(*tlv
) + TLV_LEN_ALIGN(iterator
->this_arg_size
);
286 /* and check again if we reached the end */
287 if (!ITERATOR_VALID(iterator
, 1))
290 /* if it's not the end but a new TLV won't fit - error out */
291 if (!ITERATOR_VALID(iterator
, sizeof(*tlv
)))
294 tlv
= (struct ieee80211_radiotap_tlv
*)iterator
->_arg
;
296 iterator
->this_arg_index
= get_unaligned_le16(&tlv
->type
);
297 iterator
->this_arg_size
= get_unaligned_le16(&tlv
->datalen
);
298 iterator
->this_arg
= tlv
->data
;
299 iterator
->is_radiotap_ns
=
300 iterator
->this_arg_index
!= IEEE80211_RADIOTAP_VENDOR_NAMESPACE
;
302 if (!ITERATOR_VALID(iterator
, sizeof(*tlv
) + iterator
->this_arg_size
))
309 int pad
, align
, size
, subns
;
312 /* if no more EXT bits, that's it */
313 if ((iterator
->_arg_index
% 32) == IEEE80211_RADIOTAP_EXT
&&
314 !(iterator
->_bitmap_shifter
& 1))
317 if (!(iterator
->_bitmap_shifter
& 1))
318 goto next_entry
; /* arg not present */
320 /* get alignment/size of data */
321 switch (iterator
->_arg_index
% 32) {
322 case IEEE80211_RADIOTAP_TLVS
:
326 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE
:
327 case IEEE80211_RADIOTAP_EXT
:
331 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE
:
336 #ifdef RADIOTAP_SUPPORT_OVERRIDES
337 if (find_override(iterator
, &align
, &size
)) {
341 if (!iterator
->current_namespace
||
342 iterator
->_arg_index
>= iterator
->current_namespace
->n_bits
) {
343 if (iterator
->current_namespace
== &radiotap_ns
)
347 align
= iterator
->current_namespace
->align_size
[iterator
->_arg_index
].align
;
348 size
= iterator
->current_namespace
->align_size
[iterator
->_arg_index
].size
;
351 /* skip all subsequent data */
352 int skip_size
= IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE
- (iterator
->_arg_index
% 32);
353 /* XXX - we should report an expert info here */
354 if (!iterator
->_next_ns_data
)
356 iterator
->_arg
= iterator
->_next_ns_data
;
357 /* give up on this namespace */
358 iterator
->current_namespace
= NULL
;
359 iterator
->_next_ns_data
= NULL
;
360 // Remove 1 because jump to next_entry will also shift bitmap by 1
361 iterator
->_bitmap_shifter
>>= skip_size
- 1;
362 iterator
->_arg_index
+= skip_size
- 1;
363 /* XXX - we should report an expert info here */
364 if (!ITERATOR_VALID(iterator
, 0))
372 * arg is present, account for alignment padding
374 * Note that these alignments are relative to the start
375 * of the radiotap header. There is no guarantee
376 * that the radiotap header itself is aligned on any
379 * The above is why get_unaligned() is used to dereference
380 * multibyte elements from the radiotap area.
383 pad
= (int)((iterator
->_arg
- (unsigned char *)iterator
->_rtheader
) & (align
- 1));
386 iterator
->_arg
+= align
- pad
;
388 if (iterator
->_arg_index
% 32 == IEEE80211_RADIOTAP_VENDOR_NAMESPACE
) {
391 /* XXX - we should report an expert info here */
392 if (!ITERATOR_VALID(iterator
, size
))
395 oui
= (*iterator
->_arg
<< 16) |
396 (*(iterator
->_arg
+ 1) << 8) |
397 *(iterator
->_arg
+ 2);
398 subns
= *(iterator
->_arg
+ 3);
400 find_ns(iterator
, oui
, subns
);
402 vnslen
= get_unaligned_le16(iterator
->_arg
+ 4);
403 iterator
->_next_ns_data
= iterator
->_arg
+ size
+ vnslen
;
404 if (!iterator
->current_namespace
)
406 } else if (iterator
->_arg_index
% 32 == IEEE80211_RADIOTAP_TLVS
) {
407 iterator
->tlv_mode
= 1;
412 * this is what we will return to user, but we need to
413 * move on first so next call has something fresh to test
415 iterator
->this_arg_index
= iterator
->_arg_index
;
416 iterator
->this_arg
= iterator
->_arg
;
417 iterator
->this_arg_size
= size
;
419 /* internally move on the size of this arg */
420 iterator
->_arg
+= size
;
423 * check for insanity where we are given a bitmap that
424 * claims to have more arg content than the length of the
425 * radiotap section. We will normally end up equalling this
426 * max_length on the last arg, never exceeding it.
428 /* XXX - we should report an expert info here */
429 if (!ITERATOR_VALID(iterator
, 0))
432 /* these special ones are valid in each bitmap word */
433 switch (iterator
->_arg_index
% 32) {
434 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE
:
435 iterator
->_reset_on_ext
= 1;
437 iterator
->is_radiotap_ns
= 0;
439 * If parser didn't register this vendor
440 * namespace with us, allow it to show it
441 * as 'raw. Do do that, set argument index
442 * to vendor namespace.
444 iterator
->this_arg_index
=
445 IEEE80211_RADIOTAP_VENDOR_NAMESPACE
;
446 if (!iterator
->current_namespace
)
449 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE
:
450 iterator
->_reset_on_ext
= 1;
451 iterator
->current_namespace
= &radiotap_ns
;
452 iterator
->is_radiotap_ns
= 1;
454 case IEEE80211_RADIOTAP_EXT
:
456 * bit 31 was set, there is more
457 * -- move to next u32 bitmap
459 iterator
->_bitmap_shifter
=
460 get_unaligned_le32(iterator
->_next_bitmap
);
461 iterator
->_next_bitmap
++;
462 if (iterator
->_reset_on_ext
)
463 iterator
->_arg_index
= 0;
465 iterator
->_arg_index
++;
466 iterator
->_reset_on_ext
= 0;
469 /* we've got a hit! */
472 iterator
->_bitmap_shifter
>>= 1;
473 iterator
->_arg_index
++;
476 /* if we found a valid arg earlier, return it now */
483 * Editor modelines - https://www.wireshark.org/tools/modelines.html
488 * indent-tabs-mode: t
491 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
492 * :indentSize=8:tabSize=8:noTabs=false: