Linux 3.2.102
[pandora-kernel.git] / net / wireless / 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
17 #include <linux/kernel.h>
18 #include <linux/export.h>
19 #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
25 static 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, },
44         /*
45          * add more here as they are defined in radiotap.h
46          */
47 };
48
49 static const struct ieee80211_radiotap_namespace radiotap_ns = {
50         .n_bits = ARRAY_SIZE(rtap_namespace_sizes),
51         .align_size = rtap_namespace_sizes,
52 };
53
54 /**
55  * ieee80211_radiotap_iterator_init - radiotap parser iterator initialization
56  * @iterator: radiotap_iterator to initialize
57  * @radiotap_header: radiotap header to parse
58  * @max_length: total length we can parse into (eg, whole packet length)
59  *
60  * Returns: 0 or a negative error code if there is a problem.
61  *
62  * This function initializes an opaque iterator struct which can then
63  * be passed to ieee80211_radiotap_iterator_next() to visit every radiotap
64  * argument which is present in the header.  It knows about extended
65  * present headers and handles them.
66  *
67  * How to use:
68  * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
69  * struct ieee80211_radiotap_iterator (no need to init the struct beforehand)
70  * checking for a good 0 return code.  Then loop calling
71  * __ieee80211_radiotap_iterator_next()... it returns either 0,
72  * -ENOENT if there are no more args to parse, or -EINVAL if there is a problem.
73  * The iterator's @this_arg member points to the start of the argument
74  * associated with the current argument index that is present, which can be
75  * found in the iterator's @this_arg_index member.  This arg index corresponds
76  * to the IEEE80211_RADIOTAP_... defines.
77  *
78  * Radiotap header length:
79  * You can find the CPU-endian total radiotap header length in
80  * iterator->max_length after executing ieee80211_radiotap_iterator_init()
81  * successfully.
82  *
83  * Alignment Gotcha:
84  * You must take care when dereferencing iterator.this_arg
85  * for multibyte types... the pointer is not aligned.  Use
86  * get_unaligned((type *)iterator.this_arg) to dereference
87  * iterator.this_arg for type "type" safely on all arches.
88  *
89  * Example code:
90  * See Documentation/networking/radiotap-headers.txt
91  */
92
93 int ieee80211_radiotap_iterator_init(
94         struct ieee80211_radiotap_iterator *iterator,
95         struct ieee80211_radiotap_header *radiotap_header,
96         int max_length, const struct ieee80211_radiotap_vendor_namespaces *vns)
97 {
98         /* check the radiotap header can actually be present */
99         if (max_length < sizeof(struct ieee80211_radiotap_header))
100                 return -EINVAL;
101
102         /* Linux only supports version 0 radiotap format */
103         if (radiotap_header->it_version)
104                 return -EINVAL;
105
106         /* sanity check for allowed length and radiotap length field */
107         if (max_length < get_unaligned_le16(&radiotap_header->it_len))
108                 return -EINVAL;
109
110         iterator->_rtheader = radiotap_header;
111         iterator->_max_length = get_unaligned_le16(&radiotap_header->it_len);
112         iterator->_arg_index = 0;
113         iterator->_bitmap_shifter = get_unaligned_le32(&radiotap_header->it_present);
114         iterator->_arg = (uint8_t *)radiotap_header + sizeof(*radiotap_header);
115         iterator->_reset_on_ext = 0;
116         iterator->_next_bitmap = &radiotap_header->it_present;
117         iterator->_next_bitmap++;
118         iterator->_vns = vns;
119         iterator->current_namespace = &radiotap_ns;
120         iterator->is_radiotap_ns = 1;
121
122         /* find payload start allowing for extended bitmap(s) */
123
124         if (iterator->_bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT)) {
125                 if ((unsigned long)iterator->_arg -
126                     (unsigned long)iterator->_rtheader + sizeof(uint32_t) >
127                     (unsigned long)iterator->_max_length)
128                         return -EINVAL;
129                 while (get_unaligned_le32(iterator->_arg) &
130                                         (1 << IEEE80211_RADIOTAP_EXT)) {
131                         iterator->_arg += sizeof(uint32_t);
132
133                         /*
134                          * check for insanity where the present bitmaps
135                          * keep claiming to extend up to or even beyond the
136                          * stated radiotap header length
137                          */
138
139                         if ((unsigned long)iterator->_arg -
140                             (unsigned long)iterator->_rtheader +
141                             sizeof(uint32_t) >
142                             (unsigned long)iterator->_max_length)
143                                 return -EINVAL;
144                 }
145
146                 iterator->_arg += sizeof(uint32_t);
147
148                 /*
149                  * no need to check again for blowing past stated radiotap
150                  * header length, because ieee80211_radiotap_iterator_next
151                  * checks it before it is dereferenced
152                  */
153         }
154
155         iterator->this_arg = iterator->_arg;
156
157         /* we are all initialized happily */
158
159         return 0;
160 }
161 EXPORT_SYMBOL(ieee80211_radiotap_iterator_init);
162
163 static void find_ns(struct ieee80211_radiotap_iterator *iterator,
164                     uint32_t oui, uint8_t subns)
165 {
166         int i;
167
168         iterator->current_namespace = NULL;
169
170         if (!iterator->_vns)
171                 return;
172
173         for (i = 0; i < iterator->_vns->n_ns; i++) {
174                 if (iterator->_vns->ns[i].oui != oui)
175                         continue;
176                 if (iterator->_vns->ns[i].subns != subns)
177                         continue;
178
179                 iterator->current_namespace = &iterator->_vns->ns[i];
180                 break;
181         }
182 }
183
184
185
186 /**
187  * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
188  * @iterator: radiotap_iterator to move to next arg (if any)
189  *
190  * Returns: 0 if there is an argument to handle,
191  * -ENOENT if there are no more args or -EINVAL
192  * if there is something else wrong.
193  *
194  * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
195  * in @this_arg_index and sets @this_arg to point to the
196  * payload for the field.  It takes care of alignment handling and extended
197  * present fields.  @this_arg can be changed by the caller (eg,
198  * incremented to move inside a compound argument like
199  * IEEE80211_RADIOTAP_CHANNEL).  The args pointed to are in
200  * little-endian format whatever the endianess of your CPU.
201  *
202  * Alignment Gotcha:
203  * You must take care when dereferencing iterator.this_arg
204  * for multibyte types... the pointer is not aligned.  Use
205  * get_unaligned((type *)iterator.this_arg) to dereference
206  * iterator.this_arg for type "type" safely on all arches.
207  */
208
209 int ieee80211_radiotap_iterator_next(
210         struct ieee80211_radiotap_iterator *iterator)
211 {
212         while (1) {
213                 int hit = 0;
214                 int pad, align, size, subns;
215                 uint32_t oui;
216
217                 /* if no more EXT bits, that's it */
218                 if ((iterator->_arg_index % 32) == IEEE80211_RADIOTAP_EXT &&
219                     !(iterator->_bitmap_shifter & 1))
220                         return -ENOENT;
221
222                 if (!(iterator->_bitmap_shifter & 1))
223                         goto next_entry; /* arg not present */
224
225                 /* get alignment/size of data */
226                 switch (iterator->_arg_index % 32) {
227                 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
228                 case IEEE80211_RADIOTAP_EXT:
229                         align = 1;
230                         size = 0;
231                         break;
232                 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
233                         align = 2;
234                         size = 6;
235                         break;
236                 default:
237                         if (!iterator->current_namespace ||
238                             iterator->_arg_index >= iterator->current_namespace->n_bits) {
239                                 if (iterator->current_namespace == &radiotap_ns)
240                                         return -ENOENT;
241                                 align = 0;
242                         } else {
243                                 align = iterator->current_namespace->align_size[iterator->_arg_index].align;
244                                 size = iterator->current_namespace->align_size[iterator->_arg_index].size;
245                         }
246                         if (!align) {
247                                 /* skip all subsequent data */
248                                 iterator->_arg = iterator->_next_ns_data;
249                                 /* give up on this namespace */
250                                 iterator->current_namespace = NULL;
251                                 goto next_entry;
252                         }
253                         break;
254                 }
255
256                 /*
257                  * arg is present, account for alignment padding
258                  *
259                  * Note that these alignments are relative to the start
260                  * of the radiotap header.  There is no guarantee
261                  * that the radiotap header itself is aligned on any
262                  * kind of boundary.
263                  *
264                  * The above is why get_unaligned() is used to dereference
265                  * multibyte elements from the radiotap area.
266                  */
267
268                 pad = ((unsigned long)iterator->_arg -
269                        (unsigned long)iterator->_rtheader) & (align - 1);
270
271                 if (pad)
272                         iterator->_arg += align - pad;
273
274                 if (iterator->_arg_index % 32 == IEEE80211_RADIOTAP_VENDOR_NAMESPACE) {
275                         int vnslen;
276
277                         if ((unsigned long)iterator->_arg + size -
278                             (unsigned long)iterator->_rtheader >
279                             (unsigned long)iterator->_max_length)
280                                 return -EINVAL;
281
282                         oui = (*iterator->_arg << 16) |
283                                 (*(iterator->_arg + 1) << 8) |
284                                 *(iterator->_arg + 2);
285                         subns = *(iterator->_arg + 3);
286
287                         find_ns(iterator, oui, subns);
288
289                         vnslen = get_unaligned_le16(iterator->_arg + 4);
290                         iterator->_next_ns_data = iterator->_arg + size + vnslen;
291                         if (!iterator->current_namespace)
292                                 size += vnslen;
293                 }
294
295                 /*
296                  * this is what we will return to user, but we need to
297                  * move on first so next call has something fresh to test
298                  */
299                 iterator->this_arg_index = iterator->_arg_index;
300                 iterator->this_arg = iterator->_arg;
301                 iterator->this_arg_size = size;
302
303                 /* internally move on the size of this arg */
304                 iterator->_arg += size;
305
306                 /*
307                  * check for insanity where we are given a bitmap that
308                  * claims to have more arg content than the length of the
309                  * radiotap section.  We will normally end up equalling this
310                  * max_length on the last arg, never exceeding it.
311                  */
312
313                 if ((unsigned long)iterator->_arg -
314                     (unsigned long)iterator->_rtheader >
315                     (unsigned long)iterator->_max_length)
316                         return -EINVAL;
317
318                 /* these special ones are valid in each bitmap word */
319                 switch (iterator->_arg_index % 32) {
320                 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
321                         iterator->_reset_on_ext = 1;
322
323                         iterator->is_radiotap_ns = 0;
324                         /*
325                          * If parser didn't register this vendor
326                          * namespace with us, allow it to show it
327                          * as 'raw. Do do that, set argument index
328                          * to vendor namespace.
329                          */
330                         iterator->this_arg_index =
331                                 IEEE80211_RADIOTAP_VENDOR_NAMESPACE;
332                         if (!iterator->current_namespace)
333                                 hit = 1;
334                         goto next_entry;
335                 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
336                         iterator->_reset_on_ext = 1;
337                         iterator->current_namespace = &radiotap_ns;
338                         iterator->is_radiotap_ns = 1;
339                         goto next_entry;
340                 case IEEE80211_RADIOTAP_EXT:
341                         /*
342                          * bit 31 was set, there is more
343                          * -- move to next u32 bitmap
344                          */
345                         iterator->_bitmap_shifter =
346                                 get_unaligned_le32(iterator->_next_bitmap);
347                         iterator->_next_bitmap++;
348                         if (iterator->_reset_on_ext)
349                                 iterator->_arg_index = 0;
350                         else
351                                 iterator->_arg_index++;
352                         iterator->_reset_on_ext = 0;
353                         break;
354                 default:
355                         /* we've got a hit! */
356                         hit = 1;
357  next_entry:
358                         iterator->_bitmap_shifter >>= 1;
359                         iterator->_arg_index++;
360                 }
361
362                 /* if we found a valid arg earlier, return it now */
363                 if (hit)
364                         return 0;
365         }
366 }
367 EXPORT_SYMBOL(ieee80211_radiotap_iterator_next);