radiotap.c 11 KB

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