add updated mac80211 - this no longer relies on patching includes in the kernel trees...
[openwrt/openwrt.git] / package / mac80211 / src / wireless / radiotap.c
1 /*
2 * Radiotap parser
3 *
4 * Copyright 2007 Andy Green <andy@warmcat.com>
5 */
6
7 #include <net/cfg80211.h>
8 #include <net/ieee80211_radiotap.h>
9 #include <asm/unaligned.h>
10
11 /* function prototypes and related defs are in include/net/cfg80211.h */
12
13 /**
14 * ieee80211_radiotap_iterator_init - radiotap parser iterator initialization
15 * @iterator: radiotap_iterator to initialize
16 * @radiotap_header: radiotap header to parse
17 * @max_length: total length we can parse into (eg, whole packet length)
18 *
19 * Returns: 0 or a negative error code if there is a problem.
20 *
21 * This function initializes an opaque iterator struct which can then
22 * be passed to ieee80211_radiotap_iterator_next() to visit every radiotap
23 * argument which is present in the header. It knows about extended
24 * present headers and handles them.
25 *
26 * How to use:
27 * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
28 * struct ieee80211_radiotap_iterator (no need to init the struct beforehand)
29 * checking for a good 0 return code. Then loop calling
30 * __ieee80211_radiotap_iterator_next()... it returns either 0,
31 * -ENOENT if there are no more args to parse, or -EINVAL if there is a problem.
32 * The iterator's @this_arg member points to the start of the argument
33 * associated with the current argument index that is present, which can be
34 * found in the iterator's @this_arg_index member. This arg index corresponds
35 * to the IEEE80211_RADIOTAP_... defines.
36 *
37 * Radiotap header length:
38 * You can find the CPU-endian total radiotap header length in
39 * iterator->max_length after executing ieee80211_radiotap_iterator_init()
40 * successfully.
41 *
42 * Alignment Gotcha:
43 * You must take care when dereferencing iterator.this_arg
44 * for multibyte types... the pointer is not aligned. Use
45 * get_unaligned((type *)iterator.this_arg) to dereference
46 * iterator.this_arg for type "type" safely on all arches.
47 *
48 * Example code:
49 * See Documentation/networking/radiotap-headers.txt
50 */
51
52 int ieee80211_radiotap_iterator_init(
53 struct ieee80211_radiotap_iterator *iterator,
54 struct ieee80211_radiotap_header *radiotap_header,
55 int max_length)
56 {
57 /* Linux only supports version 0 radiotap format */
58 if (radiotap_header->it_version)
59 return -EINVAL;
60
61 /* sanity check for allowed length and radiotap length field */
62 if (max_length < le16_to_cpu(get_unaligned(&radiotap_header->it_len)))
63 return -EINVAL;
64
65 iterator->rtheader = radiotap_header;
66 iterator->max_length = le16_to_cpu(get_unaligned(
67 &radiotap_header->it_len));
68 iterator->arg_index = 0;
69 iterator->bitmap_shifter = le32_to_cpu(get_unaligned(
70 &radiotap_header->it_present));
71 iterator->arg = (u8 *)radiotap_header + sizeof(*radiotap_header);
72 iterator->this_arg = NULL;
73
74 /* find payload start allowing for extended bitmap(s) */
75
76 if (unlikely(iterator->bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT))) {
77 while (le32_to_cpu(get_unaligned((__le32 *)iterator->arg)) &
78 (1<<IEEE80211_RADIOTAP_EXT)) {
79 iterator->arg += sizeof(u32);
80
81 /*
82 * check for insanity where the present bitmaps
83 * keep claiming to extend up to or even beyond the
84 * stated radiotap header length
85 */
86
87 if (((ulong)iterator->arg -
88 (ulong)iterator->rtheader) > iterator->max_length)
89 return -EINVAL;
90 }
91
92 iterator->arg += sizeof(u32);
93
94 /*
95 * no need to check again for blowing past stated radiotap
96 * header length, because ieee80211_radiotap_iterator_next
97 * checks it before it is dereferenced
98 */
99 }
100
101 /* we are all initialized happily */
102
103 return 0;
104 }
105 EXPORT_SYMBOL(ieee80211_radiotap_iterator_init);
106
107
108 /**
109 * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
110 * @iterator: radiotap_iterator to move to next arg (if any)
111 *
112 * Returns: 0 if there is an argument to handle,
113 * -ENOENT if there are no more args or -EINVAL
114 * if there is something else wrong.
115 *
116 * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
117 * in @this_arg_index and sets @this_arg to point to the
118 * payload for the field. It takes care of alignment handling and extended
119 * present fields. @this_arg can be changed by the caller (eg,
120 * incremented to move inside a compound argument like
121 * IEEE80211_RADIOTAP_CHANNEL). The args pointed to are in
122 * little-endian format whatever the endianess of your CPU.
123 *
124 * Alignment Gotcha:
125 * You must take care when dereferencing iterator.this_arg
126 * for multibyte types... the pointer is not aligned. Use
127 * get_unaligned((type *)iterator.this_arg) to dereference
128 * iterator.this_arg for type "type" safely on all arches.
129 */
130
131 int ieee80211_radiotap_iterator_next(
132 struct ieee80211_radiotap_iterator *iterator)
133 {
134
135 /*
136 * small length lookup table for all radiotap types we heard of
137 * starting from b0 in the bitmap, so we can walk the payload
138 * area of the radiotap header
139 *
140 * There is a requirement to pad args, so that args
141 * of a given length must begin at a boundary of that length
142 * -- but note that compound args are allowed (eg, 2 x u16
143 * for IEEE80211_RADIOTAP_CHANNEL) so total arg length is not
144 * a reliable indicator of alignment requirement.
145 *
146 * upper nybble: content alignment for arg
147 * lower nybble: content length for arg
148 */
149
150 static const u8 rt_sizes[] = {
151 [IEEE80211_RADIOTAP_TSFT] = 0x88,
152 [IEEE80211_RADIOTAP_FLAGS] = 0x11,
153 [IEEE80211_RADIOTAP_RATE] = 0x11,
154 [IEEE80211_RADIOTAP_CHANNEL] = 0x24,
155 [IEEE80211_RADIOTAP_FHSS] = 0x22,
156 [IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = 0x11,
157 [IEEE80211_RADIOTAP_DBM_ANTNOISE] = 0x11,
158 [IEEE80211_RADIOTAP_LOCK_QUALITY] = 0x22,
159 [IEEE80211_RADIOTAP_TX_ATTENUATION] = 0x22,
160 [IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = 0x22,
161 [IEEE80211_RADIOTAP_DBM_TX_POWER] = 0x11,
162 [IEEE80211_RADIOTAP_ANTENNA] = 0x11,
163 [IEEE80211_RADIOTAP_DB_ANTSIGNAL] = 0x11,
164 [IEEE80211_RADIOTAP_DB_ANTNOISE] = 0x11,
165 [IEEE80211_RADIOTAP_RX_FLAGS] = 0x22,
166 [IEEE80211_RADIOTAP_TX_FLAGS] = 0x22,
167 [IEEE80211_RADIOTAP_RTS_RETRIES] = 0x11,
168 [IEEE80211_RADIOTAP_DATA_RETRIES] = 0x11,
169 /*
170 * add more here as they are defined in
171 * include/net/ieee80211_radiotap.h
172 */
173 };
174
175 /*
176 * for every radiotap entry we can at
177 * least skip (by knowing the length)...
178 */
179
180 while (iterator->arg_index < sizeof(rt_sizes)) {
181 int hit = 0;
182 int pad;
183
184 if (!(iterator->bitmap_shifter & 1))
185 goto next_entry; /* arg not present */
186
187 /*
188 * arg is present, account for alignment padding
189 * 8-bit args can be at any alignment
190 * 16-bit args must start on 16-bit boundary
191 * 32-bit args must start on 32-bit boundary
192 * 64-bit args must start on 64-bit boundary
193 *
194 * note that total arg size can differ from alignment of
195 * elements inside arg, so we use upper nybble of length
196 * table to base alignment on
197 *
198 * also note: these alignments are ** relative to the
199 * start of the radiotap header **. There is no guarantee
200 * that the radiotap header itself is aligned on any
201 * kind of boundary.
202 *
203 * the above is why get_unaligned() is used to dereference
204 * multibyte elements from the radiotap area
205 */
206
207 pad = (((ulong)iterator->arg) -
208 ((ulong)iterator->rtheader)) &
209 ((rt_sizes[iterator->arg_index] >> 4) - 1);
210
211 if (pad)
212 iterator->arg +=
213 (rt_sizes[iterator->arg_index] >> 4) - pad;
214
215 /*
216 * this is what we will return to user, but we need to
217 * move on first so next call has something fresh to test
218 */
219 iterator->this_arg_index = iterator->arg_index;
220 iterator->this_arg = iterator->arg;
221 hit = 1;
222
223 /* internally move on the size of this arg */
224 iterator->arg += rt_sizes[iterator->arg_index] & 0x0f;
225
226 /*
227 * check for insanity where we are given a bitmap that
228 * claims to have more arg content than the length of the
229 * radiotap section. We will normally end up equalling this
230 * max_length on the last arg, never exceeding it.
231 */
232
233 if (((ulong)iterator->arg - (ulong)iterator->rtheader) >
234 iterator->max_length)
235 return -EINVAL;
236
237 next_entry:
238 iterator->arg_index++;
239 if (unlikely((iterator->arg_index & 31) == 0)) {
240 /* completed current u32 bitmap */
241 if (iterator->bitmap_shifter & 1) {
242 /* b31 was set, there is more */
243 /* move to next u32 bitmap */
244 iterator->bitmap_shifter = le32_to_cpu(
245 get_unaligned(iterator->next_bitmap));
246 iterator->next_bitmap++;
247 } else
248 /* no more bitmaps: end */
249 iterator->arg_index = sizeof(rt_sizes);
250 } else /* just try the next bit */
251 iterator->bitmap_shifter >>= 1;
252
253 /* if we found a valid arg earlier, return it now */
254 if (hit)
255 return 0;
256 }
257
258 /* we don't know how to handle any more args, we're done */
259 return -ENOENT;
260 }
261 EXPORT_SYMBOL(ieee80211_radiotap_iterator_next);