Staging: hv: remove OnChildDeviceRemove vmbus_driver callback
[zen-stable.git] / net / wireless / radiotap.c
blobdbe35e138e945f1bfc63aae4fdcb3a0c1e371b0b
1 /*
2 * Radiotap parser
4 * Copyright 2007 Andy Green <andy@warmcat.com>
5 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
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.
11 * Alternatively, this software may be distributed under the terms of BSD
12 * license.
14 * See COPYING for more details.
17 #include <linux/kernel.h>
18 #include <net/cfg80211.h>
19 #include <net/ieee80211_radiotap.h>
20 #include <asm/unaligned.h>
22 /* function prototypes and related defs are in include/net/cfg80211.h */
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, },
44 * add more here as they are defined in radiotap.h
48 static const struct ieee80211_radiotap_namespace radiotap_ns = {
49 .n_bits = ARRAY_SIZE(rtap_namespace_sizes),
50 .align_size = rtap_namespace_sizes,
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)
59 * Returns: 0 or a negative error code if there is a problem.
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.
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.
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.
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.
88 * Example code:
89 * See Documentation/networking/radiotap-headers.txt
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)
97 /* Linux only supports version 0 radiotap format */
98 if (radiotap_header->it_version)
99 return -EINVAL;
101 /* sanity check for allowed length and radiotap length field */
102 if (max_length < get_unaligned_le16(&radiotap_header->it_len))
103 return -EINVAL;
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;
117 /* find payload start allowing for extended bitmap(s) */
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);
125 * check for insanity where the present bitmaps
126 * keep claiming to extend up to or even beyond the
127 * stated radiotap header length
130 if ((unsigned long)iterator->_arg -
131 (unsigned long)iterator->_rtheader >
132 (unsigned long)iterator->_max_length)
133 return -EINVAL;
136 iterator->_arg += sizeof(uint32_t);
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
145 iterator->this_arg = iterator->_arg;
147 /* we are all initialized happily */
149 return 0;
151 EXPORT_SYMBOL(ieee80211_radiotap_iterator_init);
153 static void find_ns(struct ieee80211_radiotap_iterator *iterator,
154 uint32_t oui, uint8_t subns)
156 int i;
158 iterator->current_namespace = NULL;
160 if (!iterator->_vns)
161 return;
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;
169 iterator->current_namespace = &iterator->_vns->ns[i];
170 break;
177 * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
178 * @iterator: radiotap_iterator to move to next arg (if any)
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.
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.
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.
199 int ieee80211_radiotap_iterator_next(
200 struct ieee80211_radiotap_iterator *iterator)
202 while (1) {
203 int hit = 0;
204 int pad, align, size, subns;
205 uint32_t oui;
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;
212 if (!(iterator->_bitmap_shifter & 1))
213 goto next_entry; /* arg not present */
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;
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;
243 break;
247 * arg is present, account for alignment padding
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.
254 * The above is why get_unaligned() is used to dereference
255 * multibyte elements from the radiotap area.
258 pad = ((unsigned long)iterator->_arg -
259 (unsigned long)iterator->_rtheader) & (align - 1);
261 if (pad)
262 iterator->_arg += align - pad;
264 if (iterator->_arg_index % 32 == IEEE80211_RADIOTAP_VENDOR_NAMESPACE) {
265 int vnslen;
267 if ((unsigned long)iterator->_arg + size -
268 (unsigned long)iterator->_rtheader >
269 (unsigned long)iterator->_max_length)
270 return -EINVAL;
272 oui = (*iterator->_arg << 16) |
273 (*(iterator->_arg + 1) << 8) |
274 *(iterator->_arg + 2);
275 subns = *(iterator->_arg + 3);
277 find_ns(iterator, oui, subns);
279 vnslen = get_unaligned_le16(iterator->_arg + 4);
280 iterator->_next_ns_data = iterator->_arg + size + vnslen;
281 if (!iterator->current_namespace)
282 size += vnslen;
286 * this is what we will return to user, but we need to
287 * move on first so next call has something fresh to test
289 iterator->this_arg_index = iterator->_arg_index;
290 iterator->this_arg = iterator->_arg;
291 iterator->this_arg_size = size;
293 /* internally move on the size of this arg */
294 iterator->_arg += size;
297 * check for insanity where we are given a bitmap that
298 * claims to have more arg content than the length of the
299 * radiotap section. We will normally end up equalling this
300 * max_length on the last arg, never exceeding it.
303 if ((unsigned long)iterator->_arg -
304 (unsigned long)iterator->_rtheader >
305 (unsigned long)iterator->_max_length)
306 return -EINVAL;
308 /* these special ones are valid in each bitmap word */
309 switch (iterator->_arg_index % 32) {
310 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
311 iterator->_reset_on_ext = 1;
313 iterator->is_radiotap_ns = 0;
315 * If parser didn't register this vendor
316 * namespace with us, allow it to show it
317 * as 'raw. Do do that, set argument index
318 * to vendor namespace.
320 iterator->this_arg_index =
321 IEEE80211_RADIOTAP_VENDOR_NAMESPACE;
322 if (!iterator->current_namespace)
323 hit = 1;
324 goto next_entry;
325 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
326 iterator->_reset_on_ext = 1;
327 iterator->current_namespace = &radiotap_ns;
328 iterator->is_radiotap_ns = 1;
329 goto next_entry;
330 case IEEE80211_RADIOTAP_EXT:
332 * bit 31 was set, there is more
333 * -- move to next u32 bitmap
335 iterator->_bitmap_shifter =
336 get_unaligned_le32(iterator->_next_bitmap);
337 iterator->_next_bitmap++;
338 if (iterator->_reset_on_ext)
339 iterator->_arg_index = 0;
340 else
341 iterator->_arg_index++;
342 iterator->_reset_on_ext = 0;
343 break;
344 default:
345 /* we've got a hit! */
346 hit = 1;
347 next_entry:
348 iterator->_bitmap_shifter >>= 1;
349 iterator->_arg_index++;
352 /* if we found a valid arg earlier, return it now */
353 if (hit)
354 return 0;
357 EXPORT_SYMBOL(ieee80211_radiotap_iterator_next);