radiotap.c 8.5 KB

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