radiotap.c 12 KB

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