radiotap.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 < get_unaligned_le16(&radiotap_header->it_len))
  58. return -EINVAL;
  59. iterator->rtheader = radiotap_header;
  60. iterator->max_length = get_unaligned_le16(&radiotap_header->it_len);
  61. iterator->arg_index = 0;
  62. iterator->bitmap_shifter = get_unaligned_le32(&radiotap_header->it_present);
  63. iterator->arg = (u8 *)radiotap_header + sizeof(*radiotap_header);
  64. iterator->this_arg = NULL;
  65. /* find payload start allowing for extended bitmap(s) */
  66. if (unlikely(iterator->bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT))) {
  67. while (get_unaligned_le32(iterator->arg) &
  68. (1 << IEEE80211_RADIOTAP_EXT)) {
  69. iterator->arg += sizeof(u32);
  70. /*
  71. * check for insanity where the present bitmaps
  72. * keep claiming to extend up to or even beyond the
  73. * stated radiotap header length
  74. */
  75. if (((ulong)iterator->arg -
  76. (ulong)iterator->rtheader) > iterator->max_length)
  77. return -EINVAL;
  78. }
  79. iterator->arg += sizeof(u32);
  80. /*
  81. * no need to check again for blowing past stated radiotap
  82. * header length, because ieee80211_radiotap_iterator_next
  83. * checks it before it is dereferenced
  84. */
  85. }
  86. /* we are all initialized happily */
  87. return 0;
  88. }
  89. EXPORT_SYMBOL(ieee80211_radiotap_iterator_init);
  90. /**
  91. * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
  92. * @iterator: radiotap_iterator to move to next arg (if any)
  93. *
  94. * Returns: 0 if there is an argument to handle,
  95. * -ENOENT if there are no more args or -EINVAL
  96. * if there is something else wrong.
  97. *
  98. * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
  99. * in @this_arg_index and sets @this_arg to point to the
  100. * payload for the field. It takes care of alignment handling and extended
  101. * present fields. @this_arg can be changed by the caller (eg,
  102. * incremented to move inside a compound argument like
  103. * IEEE80211_RADIOTAP_CHANNEL). The args pointed to are in
  104. * little-endian format whatever the endianess of your CPU.
  105. *
  106. * Alignment Gotcha:
  107. * You must take care when dereferencing iterator.this_arg
  108. * for multibyte types... the pointer is not aligned. Use
  109. * get_unaligned((type *)iterator.this_arg) to dereference
  110. * iterator.this_arg for type "type" safely on all arches.
  111. */
  112. int ieee80211_radiotap_iterator_next(
  113. struct ieee80211_radiotap_iterator *iterator)
  114. {
  115. /*
  116. * small length lookup table for all radiotap types we heard of
  117. * starting from b0 in the bitmap, so we can walk the payload
  118. * area of the radiotap header
  119. *
  120. * There is a requirement to pad args, so that args
  121. * of a given length must begin at a boundary of that length
  122. * -- but note that compound args are allowed (eg, 2 x u16
  123. * for IEEE80211_RADIOTAP_CHANNEL) so total arg length is not
  124. * a reliable indicator of alignment requirement.
  125. *
  126. * upper nybble: content alignment for arg
  127. * lower nybble: content length for arg
  128. */
  129. static const u8 rt_sizes[] = {
  130. [IEEE80211_RADIOTAP_TSFT] = 0x88,
  131. [IEEE80211_RADIOTAP_FLAGS] = 0x11,
  132. [IEEE80211_RADIOTAP_RATE] = 0x11,
  133. [IEEE80211_RADIOTAP_CHANNEL] = 0x24,
  134. [IEEE80211_RADIOTAP_FHSS] = 0x22,
  135. [IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = 0x11,
  136. [IEEE80211_RADIOTAP_DBM_ANTNOISE] = 0x11,
  137. [IEEE80211_RADIOTAP_LOCK_QUALITY] = 0x22,
  138. [IEEE80211_RADIOTAP_TX_ATTENUATION] = 0x22,
  139. [IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = 0x22,
  140. [IEEE80211_RADIOTAP_DBM_TX_POWER] = 0x11,
  141. [IEEE80211_RADIOTAP_ANTENNA] = 0x11,
  142. [IEEE80211_RADIOTAP_DB_ANTSIGNAL] = 0x11,
  143. [IEEE80211_RADIOTAP_DB_ANTNOISE] = 0x11,
  144. [IEEE80211_RADIOTAP_RX_FLAGS] = 0x22,
  145. [IEEE80211_RADIOTAP_TX_FLAGS] = 0x22,
  146. [IEEE80211_RADIOTAP_RTS_RETRIES] = 0x11,
  147. [IEEE80211_RADIOTAP_DATA_RETRIES] = 0x11,
  148. /*
  149. * add more here as they are defined in
  150. * include/net/ieee80211_radiotap.h
  151. */
  152. };
  153. /*
  154. * for every radiotap entry we can at
  155. * least skip (by knowing the length)...
  156. */
  157. while (iterator->arg_index < sizeof(rt_sizes)) {
  158. int hit = 0;
  159. int pad;
  160. if (!(iterator->bitmap_shifter & 1))
  161. goto next_entry; /* arg not present */
  162. /*
  163. * arg is present, account for alignment padding
  164. * 8-bit args can be at any alignment
  165. * 16-bit args must start on 16-bit boundary
  166. * 32-bit args must start on 32-bit boundary
  167. * 64-bit args must start on 64-bit boundary
  168. *
  169. * note that total arg size can differ from alignment of
  170. * elements inside arg, so we use upper nybble of length
  171. * table to base alignment on
  172. *
  173. * also note: these alignments are ** relative to the
  174. * start of the radiotap header **. There is no guarantee
  175. * that the radiotap header itself is aligned on any
  176. * kind of boundary.
  177. *
  178. * the above is why get_unaligned() is used to dereference
  179. * multibyte elements from the radiotap area
  180. */
  181. pad = (((ulong)iterator->arg) -
  182. ((ulong)iterator->rtheader)) &
  183. ((rt_sizes[iterator->arg_index] >> 4) - 1);
  184. if (pad)
  185. iterator->arg +=
  186. (rt_sizes[iterator->arg_index] >> 4) - pad;
  187. /*
  188. * this is what we will return to user, but we need to
  189. * move on first so next call has something fresh to test
  190. */
  191. iterator->this_arg_index = iterator->arg_index;
  192. iterator->this_arg = iterator->arg;
  193. hit = 1;
  194. /* internally move on the size of this arg */
  195. iterator->arg += rt_sizes[iterator->arg_index] & 0x0f;
  196. /*
  197. * check for insanity where we are given a bitmap that
  198. * claims to have more arg content than the length of the
  199. * radiotap section. We will normally end up equalling this
  200. * max_length on the last arg, never exceeding it.
  201. */
  202. if (((ulong)iterator->arg - (ulong)iterator->rtheader) >
  203. iterator->max_length)
  204. return -EINVAL;
  205. next_entry:
  206. iterator->arg_index++;
  207. if (unlikely((iterator->arg_index & 31) == 0)) {
  208. /* completed current u32 bitmap */
  209. if (iterator->bitmap_shifter & 1) {
  210. /* b31 was set, there is more */
  211. /* move to next u32 bitmap */
  212. iterator->bitmap_shifter =
  213. get_unaligned_le32(iterator->next_bitmap);
  214. iterator->next_bitmap++;
  215. } else
  216. /* no more bitmaps: end */
  217. iterator->arg_index = sizeof(rt_sizes);
  218. } else /* just try the next bit */
  219. iterator->bitmap_shifter >>= 1;
  220. /* if we found a valid arg earlier, return it now */
  221. if (hit)
  222. return 0;
  223. }
  224. /* we don't know how to handle any more args, we're done */
  225. return -ENOENT;
  226. }
  227. EXPORT_SYMBOL(ieee80211_radiotap_iterator_next);