radiotap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <linux/kernel.h>
  17. #include <net/cfg80211.h>
  18. #include <net/ieee80211_radiotap.h>
  19. #include <asm/unaligned.h>
  20. /* function prototypes and related defs are in include/net/cfg80211.h */
  21. static const struct radiotap_align_size rtap_namespace_sizes[] = {
  22. [IEEE80211_RADIOTAP_TSFT] = { .align = 8, .size = 8, },
  23. [IEEE80211_RADIOTAP_FLAGS] = { .align = 1, .size = 1, },
  24. [IEEE80211_RADIOTAP_RATE] = { .align = 1, .size = 1, },
  25. [IEEE80211_RADIOTAP_CHANNEL] = { .align = 2, .size = 4, },
  26. [IEEE80211_RADIOTAP_FHSS] = { .align = 2, .size = 2, },
  27. [IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = { .align = 1, .size = 1, },
  28. [IEEE80211_RADIOTAP_DBM_ANTNOISE] = { .align = 1, .size = 1, },
  29. [IEEE80211_RADIOTAP_LOCK_QUALITY] = { .align = 2, .size = 2, },
  30. [IEEE80211_RADIOTAP_TX_ATTENUATION] = { .align = 2, .size = 2, },
  31. [IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = { .align = 2, .size = 2, },
  32. [IEEE80211_RADIOTAP_DBM_TX_POWER] = { .align = 1, .size = 1, },
  33. [IEEE80211_RADIOTAP_ANTENNA] = { .align = 1, .size = 1, },
  34. [IEEE80211_RADIOTAP_DB_ANTSIGNAL] = { .align = 1, .size = 1, },
  35. [IEEE80211_RADIOTAP_DB_ANTNOISE] = { .align = 1, .size = 1, },
  36. [IEEE80211_RADIOTAP_RX_FLAGS] = { .align = 2, .size = 2, },
  37. [IEEE80211_RADIOTAP_TX_FLAGS] = { .align = 2, .size = 2, },
  38. [IEEE80211_RADIOTAP_RTS_RETRIES] = { .align = 1, .size = 1, },
  39. [IEEE80211_RADIOTAP_DATA_RETRIES] = { .align = 1, .size = 1, },
  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 = ARRAY_SIZE(rtap_namespace_sizes),
  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:
  84. * See Documentation/networking/radiotap-headers.txt
  85. */
  86. int ieee80211_radiotap_iterator_init(
  87. struct ieee80211_radiotap_iterator *iterator,
  88. struct ieee80211_radiotap_header *radiotap_header,
  89. int max_length, const struct ieee80211_radiotap_vendor_namespaces *vns)
  90. {
  91. /* Linux only supports version 0 radiotap format */
  92. if (radiotap_header->it_version)
  93. return -EINVAL;
  94. /* sanity check for allowed length and radiotap length field */
  95. if (max_length < get_unaligned_le16(&radiotap_header->it_len))
  96. return -EINVAL;
  97. iterator->_rtheader = radiotap_header;
  98. iterator->_max_length = get_unaligned_le16(&radiotap_header->it_len);
  99. iterator->_arg_index = 0;
  100. iterator->_bitmap_shifter = get_unaligned_le32(&radiotap_header->it_present);
  101. iterator->_arg = (uint8_t *)radiotap_header + sizeof(*radiotap_header);
  102. iterator->_reset_on_ext = 0;
  103. iterator->_next_bitmap = &radiotap_header->it_present;
  104. iterator->_next_bitmap++;
  105. iterator->_vns = vns;
  106. iterator->current_namespace = &radiotap_ns;
  107. iterator->is_radiotap_ns = 1;
  108. /* find payload start allowing for extended bitmap(s) */
  109. if (iterator->_bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT)) {
  110. while (get_unaligned_le32(iterator->_arg) &
  111. (1 << IEEE80211_RADIOTAP_EXT)) {
  112. iterator->_arg += sizeof(uint32_t);
  113. /*
  114. * check for insanity where the present bitmaps
  115. * keep claiming to extend up to or even beyond the
  116. * stated radiotap header length
  117. */
  118. if ((unsigned long)iterator->_arg -
  119. (unsigned long)iterator->_rtheader >
  120. (unsigned long)iterator->_max_length)
  121. return -EINVAL;
  122. }
  123. iterator->_arg += sizeof(uint32_t);
  124. /*
  125. * no need to check again for blowing past stated radiotap
  126. * header length, because ieee80211_radiotap_iterator_next
  127. * checks it before it is dereferenced
  128. */
  129. }
  130. iterator->this_arg = iterator->_arg;
  131. /* we are all initialized happily */
  132. return 0;
  133. }
  134. EXPORT_SYMBOL(ieee80211_radiotap_iterator_init);
  135. static void find_ns(struct ieee80211_radiotap_iterator *iterator,
  136. uint32_t oui, uint8_t subns)
  137. {
  138. int i;
  139. iterator->current_namespace = NULL;
  140. if (!iterator->_vns)
  141. return;
  142. for (i = 0; i < iterator->_vns->n_ns; i++) {
  143. if (iterator->_vns->ns[i].oui != oui)
  144. continue;
  145. if (iterator->_vns->ns[i].subns != subns)
  146. continue;
  147. iterator->current_namespace = &iterator->_vns->ns[i];
  148. break;
  149. }
  150. }
  151. /**
  152. * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
  153. * @iterator: radiotap_iterator to move to next arg (if any)
  154. *
  155. * Returns: 0 if there is an argument to handle,
  156. * -ENOENT if there are no more args or -EINVAL
  157. * if there is something else wrong.
  158. *
  159. * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
  160. * in @this_arg_index and sets @this_arg to point to the
  161. * payload for the field. It takes care of alignment handling and extended
  162. * present fields. @this_arg can be changed by the caller (eg,
  163. * incremented to move inside a compound argument like
  164. * IEEE80211_RADIOTAP_CHANNEL). The args pointed to are in
  165. * little-endian format whatever the endianess of your CPU.
  166. *
  167. * Alignment Gotcha:
  168. * You must take care when dereferencing iterator.this_arg
  169. * for multibyte types... the pointer is not aligned. Use
  170. * get_unaligned((type *)iterator.this_arg) to dereference
  171. * iterator.this_arg for type "type" safely on all arches.
  172. */
  173. int ieee80211_radiotap_iterator_next(
  174. struct ieee80211_radiotap_iterator *iterator)
  175. {
  176. while (1) {
  177. int hit = 0;
  178. int pad, align, size, subns;
  179. uint32_t oui;
  180. /* if no more EXT bits, that's it */
  181. if ((iterator->_arg_index % 32) == IEEE80211_RADIOTAP_EXT &&
  182. !(iterator->_bitmap_shifter & 1))
  183. return -ENOENT;
  184. if (!(iterator->_bitmap_shifter & 1))
  185. goto next_entry; /* arg not present */
  186. /* get alignment/size of data */
  187. switch (iterator->_arg_index % 32) {
  188. case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
  189. case IEEE80211_RADIOTAP_EXT:
  190. align = 1;
  191. size = 0;
  192. break;
  193. case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
  194. align = 2;
  195. size = 6;
  196. break;
  197. default:
  198. if (!iterator->current_namespace ||
  199. iterator->_arg_index >= iterator->current_namespace->n_bits) {
  200. if (iterator->current_namespace == &radiotap_ns)
  201. return -ENOENT;
  202. align = 0;
  203. } else {
  204. align = iterator->current_namespace->align_size[iterator->_arg_index].align;
  205. size = iterator->current_namespace->align_size[iterator->_arg_index].size;
  206. }
  207. if (!align) {
  208. /* skip all subsequent data */
  209. iterator->_arg = iterator->_next_ns_data;
  210. /* give up on this namespace */
  211. iterator->current_namespace = NULL;
  212. goto next_entry;
  213. }
  214. break;
  215. }
  216. /*
  217. * arg is present, account for alignment padding
  218. *
  219. * Note that these alignments are relative to the start
  220. * of the radiotap header. There is no guarantee
  221. * that the radiotap header itself is aligned on any
  222. * kind of boundary.
  223. *
  224. * The above is why get_unaligned() is used to dereference
  225. * multibyte elements from the radiotap area.
  226. */
  227. pad = ((unsigned long)iterator->_arg -
  228. (unsigned long)iterator->_rtheader) & (align - 1);
  229. if (pad)
  230. iterator->_arg += align - pad;
  231. if (iterator->_arg_index % 32 == IEEE80211_RADIOTAP_VENDOR_NAMESPACE) {
  232. int vnslen;
  233. if ((unsigned long)iterator->_arg + size -
  234. (unsigned long)iterator->_rtheader >
  235. (unsigned long)iterator->_max_length)
  236. return -EINVAL;
  237. oui = (*iterator->_arg << 16) |
  238. (*(iterator->_arg + 1) << 8) |
  239. *(iterator->_arg + 2);
  240. subns = *(iterator->_arg + 3);
  241. find_ns(iterator, oui, subns);
  242. vnslen = get_unaligned_le16(iterator->_arg + 4);
  243. iterator->_next_ns_data = iterator->_arg + size + vnslen;
  244. if (!iterator->current_namespace)
  245. size += vnslen;
  246. }
  247. /*
  248. * this is what we will return to user, but we need to
  249. * move on first so next call has something fresh to test
  250. */
  251. iterator->this_arg_index = iterator->_arg_index;
  252. iterator->this_arg = iterator->_arg;
  253. iterator->this_arg_size = size;
  254. /* internally move on the size of this arg */
  255. iterator->_arg += size;
  256. /*
  257. * check for insanity where we are given a bitmap that
  258. * claims to have more arg content than the length of the
  259. * radiotap section. We will normally end up equalling this
  260. * max_length on the last arg, never exceeding it.
  261. */
  262. if ((unsigned long)iterator->_arg -
  263. (unsigned long)iterator->_rtheader >
  264. (unsigned long)iterator->_max_length)
  265. return -EINVAL;
  266. /* these special ones are valid in each bitmap word */
  267. switch (iterator->_arg_index % 32) {
  268. case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
  269. iterator->_reset_on_ext = 1;
  270. iterator->is_radiotap_ns = 0;
  271. /*
  272. * If parser didn't register this vendor
  273. * namespace with us, allow it to show it
  274. * as 'raw. Do do that, set argument index
  275. * to vendor namespace.
  276. */
  277. iterator->this_arg_index =
  278. IEEE80211_RADIOTAP_VENDOR_NAMESPACE;
  279. if (!iterator->current_namespace)
  280. hit = 1;
  281. goto next_entry;
  282. case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
  283. iterator->_reset_on_ext = 1;
  284. iterator->current_namespace = &radiotap_ns;
  285. iterator->is_radiotap_ns = 1;
  286. goto next_entry;
  287. case IEEE80211_RADIOTAP_EXT:
  288. /*
  289. * bit 31 was set, there is more
  290. * -- move to next u32 bitmap
  291. */
  292. iterator->_bitmap_shifter =
  293. get_unaligned_le32(iterator->_next_bitmap);
  294. iterator->_next_bitmap++;
  295. if (iterator->_reset_on_ext)
  296. iterator->_arg_index = 0;
  297. else
  298. iterator->_arg_index++;
  299. iterator->_reset_on_ext = 0;
  300. break;
  301. default:
  302. /* we've got a hit! */
  303. hit = 1;
  304. next_entry:
  305. iterator->_bitmap_shifter >>= 1;
  306. iterator->_arg_index++;
  307. }
  308. /* if we found a valid arg earlier, return it now */
  309. if (hit)
  310. return 0;
  311. }
  312. }
  313. EXPORT_SYMBOL(ieee80211_radiotap_iterator_next);