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