Packets/80211Bundle: Read-only radiotap reimplementation
[senf.git] / senf / Packets / 80211Bundle / radiotap / radiotap.c
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 BSD
12  * license.
13  *
14  * See COPYING for more details.
15  */
16 #include "radiotap_iter.h"
17 #include "platform.h"
18
19 /* function prototypes and related defs are in radiotap_iter.h */
20
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, },
40         /*
41          * add more here as they are defined in radiotap.h
42          */
43 };
44
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,
48 };
49
50 /**
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)
55  *
56  * Returns: 0 or a negative error code if there is a problem.
57  *
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.
62  *
63  * How to use:
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.
73  *
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()
77  * successfully.
78  *
79  * Alignment Gotcha:
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.
84  *
85  * Example code: parse.c
86  */
87
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)
92 {
93         /* Linux only supports version 0 radiotap format */
94         if (radiotap_header->it_version)
95                 return -EINVAL;
96
97         /* sanity check for allowed length and radiotap length field */
98         if (max_length < get_unaligned_le16(&radiotap_header->it_len))
99                 return -EINVAL;
100
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;
115 #endif
116
117         /* find payload start allowing for extended bitmap(s) */
118
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);
123
124                         /*
125                          * check for insanity where the present bitmaps
126                          * keep claiming to extend up to or even beyond the
127                          * stated radiotap header length
128                          */
129
130                         if ((unsigned long)iterator->_arg -
131                             (unsigned long)iterator->_rtheader >
132                             (unsigned long)iterator->_max_length)
133                                 return -EINVAL;
134                 }
135
136                 iterator->_arg += sizeof(uint32_t);
137
138                 /*
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
142                  */
143         }
144
145         iterator->this_arg = iterator->_arg;
146
147         /* we are all initialized happily */
148
149         return 0;
150 }
151
152 static void find_ns(struct ieee80211_radiotap_iterator *iterator,
153                     uint32_t oui, uint8_t subns)
154 {
155         int i;
156
157         iterator->current_namespace = NULL;
158
159         if (!iterator->_vns)
160                 return;
161
162         for (i = 0; i < iterator->_vns->n_ns; i++) {
163                 if (iterator->_vns->ns[i].oui != oui)
164                         continue;
165                 if (iterator->_vns->ns[i].subns != subns)
166                         continue;
167
168                 iterator->current_namespace = &iterator->_vns->ns[i];
169                 break;
170         }
171 }
172
173 #ifdef RADIOTAP_SUPPORT_OVERRIDES
174 static int find_override(struct ieee80211_radiotap_iterator *iterator,
175                          int *align, int *size)
176 {
177         int i;
178
179         if (!iterator->overrides)
180                 return 0;
181
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 */
187                                 return 0;
188                         return 1;
189                 }
190         }
191
192         return 0;
193 }
194 #endif
195
196
197 /**
198  * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
199  * @iterator: radiotap_iterator to move to next arg (if any)
200  *
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.
204  *
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.
212  *
213  * Alignment Gotcha:
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.
218  */
219
220 int ieee80211_radiotap_iterator_next(
221         struct ieee80211_radiotap_iterator *iterator)
222 {
223         while (1) {
224                 int hit = 0;
225                 int pad, align, size, subns, vnslen;
226                 uint32_t oui;
227
228                 /* if no more EXT bits, that's it */
229                 if ((iterator->_arg_index % 32) == IEEE80211_RADIOTAP_EXT &&
230                     !(iterator->_bitmap_shifter & 1))
231                         return -ENOENT;
232
233                 if (!(iterator->_bitmap_shifter & 1))
234                         goto next_entry; /* arg not present */
235
236                 /* get alignment/size of data */
237                 switch (iterator->_arg_index % 32) {
238                 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
239                 case IEEE80211_RADIOTAP_EXT:
240                         align = 1;
241                         size = 0;
242                         break;
243                 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
244                         align = 2;
245                         size = 6;
246                         break;
247                 default:
248 #ifdef RADIOTAP_SUPPORT_OVERRIDES
249                         if (find_override(iterator, &align, &size)) {
250                                 /* all set */
251                         } else
252 #endif
253                         if (!iterator->current_namespace ||
254                             iterator->_arg_index >= iterator->current_namespace->n_bits) {
255                                 if (iterator->current_namespace == &radiotap_ns)
256                                         return -ENOENT;
257                                 align = 0;
258                         } else {
259                                 align = iterator->current_namespace->align_size[iterator->_arg_index].align;
260                                 size = iterator->current_namespace->align_size[iterator->_arg_index].size;
261                         }
262                         if (!align) {
263                                 /* skip all subsequent data */
264                                 iterator->_arg = iterator->_next_ns_data;
265                                 /* give up on this namespace */
266                                 iterator->current_namespace = NULL;
267                                 goto next_entry;
268                         }
269                         break;
270                 }
271
272                 /*
273                  * arg is present, account for alignment padding
274                  *
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
278                  * kind of boundary.
279                  *
280                  * The above is why get_unaligned() is used to dereference
281                  * multibyte elements from the radiotap area.
282                  */
283
284                 pad = ((unsigned long)iterator->_arg -
285                        (unsigned long)iterator->_rtheader) & (align - 1);
286
287                 if (pad)
288                         iterator->_arg += align - pad;
289
290                 /*
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
293                  */
294                 iterator->this_arg_index = iterator->_arg_index;
295                 iterator->this_arg = iterator->_arg;
296                 iterator->this_arg_size = size;
297
298                 /* internally move on the size of this arg */
299                 iterator->_arg += size;
300
301                 /*
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.
306                  */
307
308                 if ((unsigned long)iterator->_arg -
309                     (unsigned long)iterator->_rtheader >
310                     (unsigned long)iterator->_max_length)
311                         return -EINVAL;
312
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++;
318
319                         iterator->_reset_on_ext = 1;
320
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);
327
328                         find_ns(iterator, oui, subns);
329
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)
339                                 return -EINVAL;
340                         hit = 1;
341                         break;
342                 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
343                         iterator->_bitmap_shifter >>= 1;
344                         iterator->_arg_index++;
345
346                         iterator->_reset_on_ext = 1;
347                         iterator->current_namespace = &radiotap_ns;
348                         iterator->is_radiotap_ns = 1;
349                         break;
350                 case IEEE80211_RADIOTAP_EXT:
351                         /*
352                          * bit 31 was set, there is more
353                          * -- move to next u32 bitmap
354                          */
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;
360                         else
361                                 iterator->_arg_index++;
362                         iterator->_reset_on_ext = 0;
363                         break;
364                 default:
365                         /* we've got a hit! */
366                         hit = 1;
367  next_entry:
368                         iterator->_bitmap_shifter >>= 1;
369                         iterator->_arg_index++;
370                 }
371
372                 /* if we found a valid arg earlier, return it now */
373                 if (hit)
374                         return 0;
375         }
376 }