Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Radiotap parser |
| 3 | * |
| 4 | * Copyright 2007 Andy Green <andy@warmcat.com> |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 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. |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
Nikitas Angelinas | 9426231 | 2010-09-08 22:29:53 +0100 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 18 | #include <linux/export.h> |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 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 | |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 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 = { |
Nikitas Angelinas | 9426231 | 2010-09-08 22:29:53 +0100 | [diff] [blame] | 50 | .n_bits = ARRAY_SIZE(rtap_namespace_sizes), |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 51 | .align_size = rtap_namespace_sizes, |
| 52 | }; |
| 53 | |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 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( |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 94 | struct ieee80211_radiotap_iterator *iterator, |
| 95 | struct ieee80211_radiotap_header *radiotap_header, |
| 96 | int max_length, const struct ieee80211_radiotap_vendor_namespaces *vns) |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 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 */ |
Harvey Harrison | ae7245c | 2008-05-01 22:19:33 -0700 | [diff] [blame] | 103 | if (max_length < get_unaligned_le16(&radiotap_header->it_len)) |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 104 | return -EINVAL; |
| 105 | |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 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; |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 117 | |
| 118 | /* find payload start allowing for extended bitmap(s) */ |
| 119 | |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 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); |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 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 | |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 131 | if ((unsigned long)iterator->_arg - |
| 132 | (unsigned long)iterator->_rtheader > |
| 133 | (unsigned long)iterator->_max_length) |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 134 | return -EINVAL; |
| 135 | } |
| 136 | |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 137 | iterator->_arg += sizeof(uint32_t); |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 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 | |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 146 | iterator->this_arg = iterator->_arg; |
| 147 | |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 148 | /* we are all initialized happily */ |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | EXPORT_SYMBOL(ieee80211_radiotap_iterator_init); |
| 153 | |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 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 | |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 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( |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 201 | struct ieee80211_radiotap_iterator *iterator) |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 202 | { |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 203 | while (1) { |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 204 | int hit = 0; |
Johannes Berg | 9ebad4a | 2010-10-14 13:41:35 +0200 | [diff] [blame] | 205 | int pad, align, size, subns; |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 206 | uint32_t oui; |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 207 | |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 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)) |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 214 | goto next_entry; /* arg not present */ |
| 215 | |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 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 | |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 247 | /* |
| 248 | * arg is present, account for alignment padding |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 249 | * |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 250 | * Note that these alignments are relative to the start |
| 251 | * of the radiotap header. There is no guarantee |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 252 | * that the radiotap header itself is aligned on any |
| 253 | * kind of boundary. |
| 254 | * |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 255 | * The above is why get_unaligned() is used to dereference |
| 256 | * multibyte elements from the radiotap area. |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 257 | */ |
| 258 | |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 259 | pad = ((unsigned long)iterator->_arg - |
| 260 | (unsigned long)iterator->_rtheader) & (align - 1); |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 261 | |
| 262 | if (pad) |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 263 | iterator->_arg += align - pad; |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 264 | |
Johannes Berg | 9ebad4a | 2010-10-14 13:41:35 +0200 | [diff] [blame] | 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 | |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 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 | */ |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 290 | iterator->this_arg_index = iterator->_arg_index; |
| 291 | iterator->this_arg = iterator->_arg; |
| 292 | iterator->this_arg_size = size; |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 293 | |
| 294 | /* internally move on the size of this arg */ |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 295 | iterator->_arg += size; |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 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 | |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 304 | if ((unsigned long)iterator->_arg - |
| 305 | (unsigned long)iterator->_rtheader > |
| 306 | (unsigned long)iterator->_max_length) |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 307 | return -EINVAL; |
| 308 | |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 309 | /* these special ones are valid in each bitmap word */ |
| 310 | switch (iterator->_arg_index % 32) { |
| 311 | case IEEE80211_RADIOTAP_VENDOR_NAMESPACE: |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 312 | iterator->_reset_on_ext = 1; |
| 313 | |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 314 | iterator->is_radiotap_ns = 0; |
Johannes Berg | 9ebad4a | 2010-10-14 13:41:35 +0200 | [diff] [blame] | 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 | */ |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 321 | iterator->this_arg_index = |
| 322 | IEEE80211_RADIOTAP_VENDOR_NAMESPACE; |
Johannes Berg | 9ebad4a | 2010-10-14 13:41:35 +0200 | [diff] [blame] | 323 | if (!iterator->current_namespace) |
| 324 | hit = 1; |
| 325 | goto next_entry; |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 326 | case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE: |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 327 | iterator->_reset_on_ext = 1; |
| 328 | iterator->current_namespace = &radiotap_ns; |
| 329 | iterator->is_radiotap_ns = 1; |
Johannes Berg | 9ebad4a | 2010-10-14 13:41:35 +0200 | [diff] [blame] | 330 | goto next_entry; |
Johannes Berg | 33e5a2f | 2010-02-03 10:24:30 +0100 | [diff] [blame] | 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 | } |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 352 | |
| 353 | /* if we found a valid arg earlier, return it now */ |
| 354 | if (hit) |
| 355 | return 0; |
| 356 | } |
Andy Green | 179f831 | 2007-07-10 19:29:38 +0200 | [diff] [blame] | 357 | } |
| 358 | EXPORT_SYMBOL(ieee80211_radiotap_iterator_next); |