radiotap.c 12 KB

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