radiotap.c 12 KB

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