Merge branch 'spi/for-3.2' of git://git.pengutronix.de/git/wsa/linux-2.6
[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         /* Linux only supports version 0 radiotap format */
99         if (radiotap_header->it_version)
100                 return -EINVAL;
101
102         /* sanity check for allowed length and radiotap length field */
103         if (max_length < get_unaligned_le16(&radiotap_header->it_len))
104                 return -EINVAL;
105
106         iterator->_rtheader = radiotap_header;
107         iterator->_max_length = get_unaligned_le16(&radiotap_header->it_len);
108         iterator->_arg_index = 0;
109         iterator->_bitmap_shifter = get_unaligned_le32(&radiotap_header->it_present);
110         iterator->_arg = (uint8_t *)radiotap_header + sizeof(*radiotap_header);
111         iterator->_reset_on_ext = 0;
112         iterator->_next_bitmap = &radiotap_header->it_present;
113         iterator->_next_bitmap++;
114         iterator->_vns = vns;
115         iterator->current_namespace = &radiotap_ns;
116         iterator->is_radiotap_ns = 1;
117
118         /* find payload start allowing for extended bitmap(s) */
119
120         if (iterator->_bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT)) {
121                 while (get_unaligned_le32(iterator->_arg) &
122                                         (1 << IEEE80211_RADIOTAP_EXT)) {
123                         iterator->_arg += sizeof(uint32_t);
124
125                         /*
126                          * check for insanity where the present bitmaps
127                          * keep claiming to extend up to or even beyond the
128                          * stated radiotap header length
129                          */
130
131                         if ((unsigned long)iterator->_arg -
132                             (unsigned long)iterator->_rtheader >
133                             (unsigned long)iterator->_max_length)
134                                 return -EINVAL;
135                 }
136
137                 iterator->_arg += sizeof(uint32_t);
138
139                 /*
140                  * no need to check again for blowing past stated radiotap
141                  * header length, because ieee80211_radiotap_iterator_next
142                  * checks it before it is dereferenced
143                  */
144         }
145
146         iterator->this_arg = iterator->_arg;
147
148         /* we are all initialized happily */
149
150         return 0;
151 }
152 EXPORT_SYMBOL(ieee80211_radiotap_iterator_init);
153
154 static void find_ns(struct ieee80211_radiotap_iterator *iterator,
155                     uint32_t oui, uint8_t subns)
156 {
157         int i;
158
159         iterator->current_namespace = NULL;
160
161         if (!iterator->_vns)
162                 return;
163
164         for (i = 0; i < iterator->_vns->n_ns; i++) {
165                 if (iterator->_vns->ns[i].oui != oui)
166                         continue;
167                 if (iterator->_vns->ns[i].subns != subns)
168                         continue;
169
170                 iterator->current_namespace = &iterator->_vns->ns[i];
171                 break;
172         }
173 }
174
175
176
177 /**
178  * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
179  * @iterator: radiotap_iterator to move to next arg (if any)
180  *
181  * Returns: 0 if there is an argument to handle,
182  * -ENOENT if there are no more args or -EINVAL
183  * if there is something else wrong.
184  *
185  * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
186  * in @this_arg_index and sets @this_arg to point to the
187  * payload for the field.  It takes care of alignment handling and extended
188  * present fields.  @this_arg can be changed by the caller (eg,
189  * incremented to move inside a compound argument like
190  * IEEE80211_RADIOTAP_CHANNEL).  The args pointed to are in
191  * little-endian format whatever the endianess of your CPU.
192  *
193  * Alignment Gotcha:
194  * You must take care when dereferencing iterator.this_arg
195  * for multibyte types... the pointer is not aligned.  Use
196  * get_unaligned((type *)iterator.this_arg) to dereference
197  * iterator.this_arg for type "type" safely on all arches.
198  */
199
200 int ieee80211_radiotap_iterator_next(
201         struct ieee80211_radiotap_iterator *iterator)
202 {
203         while (1) {
204                 int hit = 0;
205                 int pad, align, size, subns;
206                 uint32_t oui;
207
208                 /* if no more EXT bits, that's it */
209                 if ((iterator->_arg_index % 32) == IEEE80211_RADIOTAP_EXT &&
210                     !(iterator->_bitmap_shifter & 1))
211                         return -ENOENT;
212
213                 if (!(iterator->_bitmap_shifter & 1))
214                         goto next_entry; /* arg not present */
215
216                 /* get alignment/size of data */
217                 switch (iterator->_arg_index % 32) {
218                 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
219                 case IEEE80211_RADIOTAP_EXT:
220                         align = 1;
221                         size = 0;
222                         break;
223                 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
224                         align = 2;
225                         size = 6;
226                         break;
227                 default:
228                         if (!iterator->current_namespace ||
229                             iterator->_arg_index >= iterator->current_namespace->n_bits) {
230                                 if (iterator->current_namespace == &radiotap_ns)
231                                         return -ENOENT;
232                                 align = 0;
233                         } else {
234                                 align = iterator->current_namespace->align_size[iterator->_arg_index].align;
235                                 size = iterator->current_namespace->align_size[iterator->_arg_index].size;
236                         }
237                         if (!align) {
238                                 /* skip all subsequent data */
239                                 iterator->_arg = iterator->_next_ns_data;
240                                 /* give up on this namespace */
241                                 iterator->current_namespace = NULL;
242                                 goto next_entry;
243                         }
244                         break;
245                 }
246
247                 /*
248                  * arg is present, account for alignment padding
249                  *
250                  * Note that these alignments are relative to the start
251                  * of the radiotap header.  There is no guarantee
252                  * that the radiotap header itself is aligned on any
253                  * kind of boundary.
254                  *
255                  * The above is why get_unaligned() is used to dereference
256                  * multibyte elements from the radiotap area.
257                  */
258
259                 pad = ((unsigned long)iterator->_arg -
260                        (unsigned long)iterator->_rtheader) & (align - 1);
261
262                 if (pad)
263                         iterator->_arg += align - pad;
264
265                 if (iterator->_arg_index % 32 == IEEE80211_RADIOTAP_VENDOR_NAMESPACE) {
266                         int vnslen;
267
268                         if ((unsigned long)iterator->_arg + size -
269                             (unsigned long)iterator->_rtheader >
270                             (unsigned long)iterator->_max_length)
271                                 return -EINVAL;
272
273                         oui = (*iterator->_arg << 16) |
274                                 (*(iterator->_arg + 1) << 8) |
275                                 *(iterator->_arg + 2);
276                         subns = *(iterator->_arg + 3);
277
278                         find_ns(iterator, oui, subns);
279
280                         vnslen = get_unaligned_le16(iterator->_arg + 4);
281                         iterator->_next_ns_data = iterator->_arg + size + vnslen;
282                         if (!iterator->current_namespace)
283                                 size += vnslen;
284                 }
285
286                 /*
287                  * this is what we will return to user, but we need to
288                  * move on first so next call has something fresh to test
289                  */
290                 iterator->this_arg_index = iterator->_arg_index;
291                 iterator->this_arg = iterator->_arg;
292                 iterator->this_arg_size = size;
293
294                 /* internally move on the size of this arg */
295                 iterator->_arg += size;
296
297                 /*
298                  * check for insanity where we are given a bitmap that
299                  * claims to have more arg content than the length of the
300                  * radiotap section.  We will normally end up equalling this
301                  * max_length on the last arg, never exceeding it.
302                  */
303
304                 if ((unsigned long)iterator->_arg -
305                     (unsigned long)iterator->_rtheader >
306                     (unsigned long)iterator->_max_length)
307                         return -EINVAL;
308
309                 /* these special ones are valid in each bitmap word */
310                 switch (iterator->_arg_index % 32) {
311                 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
312                         iterator->_reset_on_ext = 1;
313
314                         iterator->is_radiotap_ns = 0;
315                         /*
316                          * If parser didn't register this vendor
317                          * namespace with us, allow it to show it
318                          * as 'raw. Do do that, set argument index
319                          * to vendor namespace.
320                          */
321                         iterator->this_arg_index =
322                                 IEEE80211_RADIOTAP_VENDOR_NAMESPACE;
323                         if (!iterator->current_namespace)
324                                 hit = 1;
325                         goto next_entry;
326                 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
327                         iterator->_reset_on_ext = 1;
328                         iterator->current_namespace = &radiotap_ns;
329                         iterator->is_radiotap_ns = 1;
330                         goto next_entry;
331                 case IEEE80211_RADIOTAP_EXT:
332                         /*
333                          * bit 31 was set, there is more
334                          * -- move to next u32 bitmap
335                          */
336                         iterator->_bitmap_shifter =
337                                 get_unaligned_le32(iterator->_next_bitmap);
338                         iterator->_next_bitmap++;
339                         if (iterator->_reset_on_ext)
340                                 iterator->_arg_index = 0;
341                         else
342                                 iterator->_arg_index++;
343                         iterator->_reset_on_ext = 0;
344                         break;
345                 default:
346                         /* we've got a hit! */
347                         hit = 1;
348  next_entry:
349                         iterator->_bitmap_shifter >>= 1;
350                         iterator->_arg_index++;
351                 }
352
353                 /* if we found a valid arg earlier, return it now */
354                 if (hit)
355                         return 0;
356         }
357 }
358 EXPORT_SYMBOL(ieee80211_radiotap_iterator_next);