libwifi 0.0.3
An 802.11 Frame Parsing and Generation library in C
radiotap.c
Go to the documentation of this file.
1/*
2 * Radiotap parser
3 *
4 * Copyright 2007 Andy Green <andy@warmcat.com>
5 * 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 ISC
12 * license, see COPYING for more details.
13 */
14#include "platform.h"
15#include "radiotap_iter.h"
16
17/* function prototypes and related defs are in radiotap_iter.h */
18
21 {
22 .align = 8,
23 .size = 8,
24 },
26 {
27 .align = 1,
28 .size = 1,
29 },
31 {
32 .align = 1,
33 .size = 1,
34 },
36 {
37 .align = 2,
38 .size = 4,
39 },
41 {
42 .align = 2,
43 .size = 2,
44 },
46 {
47 .align = 1,
48 .size = 1,
49 },
51 {
52 .align = 1,
53 .size = 1,
54 },
56 {
57 .align = 2,
58 .size = 2,
59 },
61 {
62 .align = 2,
63 .size = 2,
64 },
66 {
67 .align = 2,
68 .size = 2,
69 },
71 {
72 .align = 1,
73 .size = 1,
74 },
76 {
77 .align = 1,
78 .size = 1,
79 },
81 {
82 .align = 1,
83 .size = 1,
84 },
86 {
87 .align = 1,
88 .size = 1,
89 },
91 {
92 .align = 2,
93 .size = 2,
94 },
96 {
97 .align = 2,
98 .size = 2,
99 },
101 {
102 .align = 1,
103 .size = 1,
104 },
106 {
107 .align = 1,
108 .size = 1,
109 },
111 {
112 .align = 1,
113 .size = 3,
114 },
116 {
117 .align = 4,
118 .size = 8,
119 },
121 {
122 .align = 2,
123 .size = 12,
124 },
126 {
127 .align = 8,
128 .size = 12,
129 },
130 /*
131 * add more here as they are defined in radiotap.h
132 */
133};
134
136 .n_bits = sizeof(rtap_namespace_sizes) / sizeof(rtap_namespace_sizes[0]),
137 .align_size = rtap_namespace_sizes,
138};
139
178EXPORT
180 struct ieee80211_radiotap_header *radiotap_header, int max_length,
181 const struct ieee80211_radiotap_vendor_namespaces *vns) {
182 /* must at least have the radiotap header */
183 if (max_length < (int) sizeof(struct ieee80211_radiotap_header))
184 return -EINVAL;
185
186 /* Linux only supports version 0 radiotap format */
187 if (radiotap_header->it_version)
188 return -EINVAL;
189
190 /* sanity check for allowed length and radiotap length field */
191 if (max_length < get_unaligned_le16(UNALIGNED_ADDRESS(&radiotap_header->it_len)))
192 return -EINVAL;
193
194 iterator->_rtheader = radiotap_header;
195 iterator->_max_length = get_unaligned_le16(UNALIGNED_ADDRESS(&radiotap_header->it_len));
196 iterator->_arg_index = 0;
197 iterator->_bitmap_shifter = get_unaligned_le32(UNALIGNED_ADDRESS(&radiotap_header->it_present));
198 iterator->_arg = (uint8_t *) radiotap_header + sizeof(*radiotap_header);
199 iterator->_reset_on_ext = 0;
200#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 9
201#pragma GCC diagnostic push
202#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
203#endif
204 iterator->_next_bitmap = UNALIGNED_ADDRESS(&radiotap_header->it_present);
205#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 9
206#pragma GCC diagnostic pop
207#endif
208 iterator->_next_bitmap++;
209 iterator->_vns = vns;
210 iterator->current_namespace = &radiotap_ns;
211 iterator->is_radiotap_ns = 1;
212#ifdef RADIOTAP_SUPPORT_OVERRIDES
213 iterator->n_overrides = 0;
214 iterator->overrides = NULL;
215#endif
216
217 /* find payload start allowing for extended bitmap(s) */
218
219 if (iterator->_bitmap_shifter & (1 << IEEE80211_RADIOTAP_EXT)) {
220 if ((unsigned long) iterator->_arg - (unsigned long) iterator->_rtheader + sizeof(uint32_t) >
221 (unsigned long) iterator->_max_length)
222 return -EINVAL;
223 while (get_unaligned_le32(iterator->_arg) & (1 << IEEE80211_RADIOTAP_EXT)) {
224 iterator->_arg += sizeof(uint32_t);
225
226 /*
227 * check for insanity where the present bitmaps
228 * keep claiming to extend up to or even beyond the
229 * stated radiotap header length
230 */
231
232 if ((unsigned long) iterator->_arg - (unsigned long) iterator->_rtheader + sizeof(uint32_t) >
233 (unsigned long) iterator->_max_length)
234 return -EINVAL;
235 }
236
237 iterator->_arg += sizeof(uint32_t);
238
239 /*
240 * no need to check again for blowing past stated radiotap
241 * header length, because ieee80211_radiotap_iterator_next
242 * checks it before it is dereferenced
243 */
244 }
245
246 iterator->this_arg = iterator->_arg;
247
248 /* we are all initialized happily */
249
250 return 0;
251}
252
253static void find_ns(struct ieee80211_radiotap_iterator *iterator, uint32_t oui, uint8_t subns) {
254 int i;
255
256 iterator->current_namespace = NULL;
257
258 if (!iterator->_vns)
259 return;
260
261 for (i = 0; i < iterator->_vns->n_ns; i++) {
262 if (iterator->_vns->ns[i].oui != oui)
263 continue;
264 if (iterator->_vns->ns[i].subns != subns)
265 continue;
266
267 iterator->current_namespace = &iterator->_vns->ns[i];
268 break;
269 }
270}
271
272#ifdef RADIOTAP_SUPPORT_OVERRIDES
273static int find_override(struct ieee80211_radiotap_iterator *iterator, int *align, int *size) {
274 int i;
275
276 if (!iterator->overrides)
277 return 0;
278
279 for (i = 0; i < iterator->n_overrides; i++) {
280 if (iterator->_arg_index == iterator->overrides[i].field) {
281 *align = iterator->overrides[i].align;
282 *size = iterator->overrides[i].size;
283 if (!*align) /* erroneous override */
284 return 0;
285 return 1;
286 }
287 }
288
289 return 0;
290}
291#endif
292
316EXPORT
318 while (1) {
319 int hit = 0;
320 int pad, align, size, subns;
321 uint32_t oui;
322
323 /* if no more EXT bits, that's it */
324 if ((iterator->_arg_index % 32) == IEEE80211_RADIOTAP_EXT && !(iterator->_bitmap_shifter & 1))
325 return -ENOENT;
326
327 if (!(iterator->_bitmap_shifter & 1))
328 goto next_entry; /* arg not present */
329
330 /* get alignment/size of data */
331 switch (iterator->_arg_index % 32) {
334 align = 1;
335 size = 0;
336 break;
338 align = 2;
339 size = 6;
340 break;
341 default:
342#ifdef RADIOTAP_SUPPORT_OVERRIDES
343 if (find_override(iterator, &align, &size)) {
344 /* all set */
345 } else
346#endif
347 if (!iterator->current_namespace ||
348 iterator->_arg_index >= iterator->current_namespace->n_bits) {
349 if (iterator->current_namespace == &radiotap_ns)
350 return -ENOENT;
351 align = 0;
352 } else {
353 align = iterator->current_namespace->align_size[iterator->_arg_index].align;
354 size = iterator->current_namespace->align_size[iterator->_arg_index].size;
355 }
356 if (!align) {
357 /* skip all subsequent data */
358 iterator->_arg = iterator->_next_ns_data;
359 /* give up on this namespace */
360 iterator->current_namespace = NULL;
361 goto next_entry;
362 }
363 break;
364 }
365
366 /*
367 * arg is present, account for alignment padding
368 *
369 * Note that these alignments are relative to the start
370 * of the radiotap header. There is no guarantee
371 * that the radiotap header itself is aligned on any
372 * kind of boundary.
373 *
374 * The above is why get_unaligned() is used to dereference
375 * multibyte elements from the radiotap area.
376 */
377
378 pad = ((unsigned long) iterator->_arg - (unsigned long) iterator->_rtheader) & (align - 1);
379
380 if (pad)
381 iterator->_arg += align - pad;
382
383 if (iterator->_arg_index % 32 == IEEE80211_RADIOTAP_VENDOR_NAMESPACE) {
384 int vnslen;
385
386 if ((unsigned long) iterator->_arg + size - (unsigned long) iterator->_rtheader >
387 (unsigned long) iterator->_max_length)
388 return -EINVAL;
389
390 oui = (*iterator->_arg << 16) | (*(iterator->_arg + 1) << 8) | *(iterator->_arg + 2);
391 subns = *(iterator->_arg + 3);
392
393 find_ns(iterator, oui, subns);
394
395 vnslen = get_unaligned_le16(iterator->_arg + 4);
396 iterator->_next_ns_data = iterator->_arg + size + vnslen;
397 if (!iterator->current_namespace)
398 size += vnslen;
399 }
400
401 /*
402 * this is what we will return to user, but we need to
403 * move on first so next call has something fresh to test
404 */
405 iterator->this_arg_index = iterator->_arg_index;
406 iterator->this_arg = iterator->_arg;
407 iterator->this_arg_size = size;
408
409 /* internally move on the size of this arg */
410 iterator->_arg += size;
411
412 /*
413 * check for insanity where we are given a bitmap that
414 * claims to have more arg content than the length of the
415 * radiotap section. We will normally end up equalling this
416 * max_length on the last arg, never exceeding it.
417 */
418
419 if ((unsigned long) iterator->_arg - (unsigned long) iterator->_rtheader >
420 (unsigned long) iterator->_max_length)
421 return -EINVAL;
422
423 /* these special ones are valid in each bitmap word */
424 switch (iterator->_arg_index % 32) {
426 iterator->_reset_on_ext = 1;
427
428 iterator->is_radiotap_ns = 0;
429 /*
430 * If parser didn't register this vendor
431 * namespace with us, allow it to show it
432 * as 'raw. Do do that, set argument index
433 * to vendor namespace.
434 */
436 if (!iterator->current_namespace)
437 hit = 1;
438 goto next_entry;
440 iterator->_reset_on_ext = 1;
441 iterator->current_namespace = &radiotap_ns;
442 iterator->is_radiotap_ns = 1;
443 goto next_entry;
445 /*
446 * bit 31 was set, there is more
447 * -- move to next u32 bitmap
448 */
449 iterator->_bitmap_shifter = get_unaligned_le32(iterator->_next_bitmap);
450 iterator->_next_bitmap++;
451 if (iterator->_reset_on_ext)
452 iterator->_arg_index = 0;
453 else
454 iterator->_arg_index++;
455 iterator->_reset_on_ext = 0;
456 break;
457 default:
458 /* we've got a hit! */
459 hit = 1;
460 next_entry:
461 iterator->_bitmap_shifter >>= 1;
462 iterator->_arg_index++;
463 }
464
465 /* if we found a valid arg earlier, return it now */
466 if (hit)
467 return 0;
468 }
469}
static void find_ns(struct ieee80211_radiotap_iterator *iterator, uint32_t oui, uint8_t subns)
Definition: radiotap.c:253
const struct ieee80211_radiotap_namespace radiotap_ns
Definition: radiotap.c:135
static const struct radiotap_align_size rtap_namespace_sizes[]
Definition: radiotap.c:19
EXPORT int ieee80211_radiotap_iterator_next(struct ieee80211_radiotap_iterator *iterator)
ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg @iterator: radiotap_itera...
Definition: radiotap.c:317
EXPORT int ieee80211_radiotap_iterator_init(struct ieee80211_radiotap_iterator *iterator, struct ieee80211_radiotap_header *radiotap_header, int max_length, const struct ieee80211_radiotap_vendor_namespaces *vns)
ieee80211_radiotap_iterator_init - radiotap parser iterator initialization @iterator: radiotap_iterat...
Definition: radiotap.c:179
@ IEEE80211_RADIOTAP_DB_TX_ATTENUATION
Definition: radiotap.h:79
@ IEEE80211_RADIOTAP_EXT
Definition: radiotap.h:97
@ IEEE80211_RADIOTAP_DATA_RETRIES
Definition: radiotap.h:87
@ IEEE80211_RADIOTAP_DBM_TX_POWER
Definition: radiotap.h:80
@ IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE
Definition: radiotap.h:95
@ IEEE80211_RADIOTAP_AMPDU_STATUS
Definition: radiotap.h:90
@ IEEE80211_RADIOTAP_TX_FLAGS
Definition: radiotap.h:85
@ IEEE80211_RADIOTAP_RTS_RETRIES
Definition: radiotap.h:86
@ IEEE80211_RADIOTAP_MCS
Definition: radiotap.h:89
@ IEEE80211_RADIOTAP_LOCK_QUALITY
Definition: radiotap.h:77
@ IEEE80211_RADIOTAP_VENDOR_NAMESPACE
Definition: radiotap.h:96
@ IEEE80211_RADIOTAP_CHANNEL
Definition: radiotap.h:73
@ IEEE80211_RADIOTAP_VHT
Definition: radiotap.h:91
@ IEEE80211_RADIOTAP_FLAGS
Definition: radiotap.h:71
@ IEEE80211_RADIOTAP_TX_ATTENUATION
Definition: radiotap.h:78
@ IEEE80211_RADIOTAP_DB_ANTNOISE
Definition: radiotap.h:83
@ IEEE80211_RADIOTAP_DB_ANTSIGNAL
Definition: radiotap.h:82
@ IEEE80211_RADIOTAP_TSFT
Definition: radiotap.h:70
@ IEEE80211_RADIOTAP_TIMESTAMP
Definition: radiotap.h:92
@ IEEE80211_RADIOTAP_RATE
Definition: radiotap.h:72
@ IEEE80211_RADIOTAP_FHSS
Definition: radiotap.h:74
@ IEEE80211_RADIOTAP_DBM_ANTNOISE
Definition: radiotap.h:76
@ IEEE80211_RADIOTAP_ANTENNA
Definition: radiotap.h:81
@ IEEE80211_RADIOTAP_DBM_ANTSIGNAL
Definition: radiotap.h:75
@ IEEE80211_RADIOTAP_RX_FLAGS
Definition: radiotap.h:84
#define EXPORT
Definition: platform.h:55
#define get_unaligned_le16(p)
Definition: platform.h:78
#define get_unaligned_le32(p)
Definition: platform.h:79
#define UNALIGNED_ADDRESS(x)
Definition: platform.h:81
struct ieee82011_radiotap_header - base radiotap header
Definition: radiotap.h:41
uint16_t it_len
@it_len: overall radiotap header length
Definition: radiotap.h:55
uint8_t it_version
@it_version: radiotap version, always 0
Definition: radiotap.h:45
uint32_t it_present
@it_present: (first) present word
Definition: radiotap.h:60
struct ieee80211_radiotap_iterator - tracks walk thru present radiotap args @this_arg_index: index of...
Definition: radiotap_iter.h:65
unsigned char * _next_ns_data
Definition: radiotap_iter.h:70
const struct ieee80211_radiotap_vendor_namespaces * _vns
Definition: radiotap_iter.h:67
const struct radiotap_override * overrides
Definition: radiotap_iter.h:74
struct ieee80211_radiotap_header * _rtheader
Definition: radiotap_iter.h:66
const struct ieee80211_radiotap_namespace * current_namespace
Definition: radiotap_iter.h:68
const struct radiotap_align_size * align_size
Definition: radiotap_iter.h:22
const struct ieee80211_radiotap_namespace * ns
Definition: radiotap_iter.h:29
unsigned char oui[3]
Definition: tag.h:0