radiotap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* $Id: radiotap.c 1893 2007-08-10 04:24:50Z aturner $ */
  2. /*
  3. * Copyright (c) 2006-2007 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "dlt_plugins-int.h"
  34. #include "dlt_utils.h"
  35. #include "radiotap.h"
  36. #include "../dlt_ieee80211/ieee80211.h"
  37. #include "tcpedit.h"
  38. #include "common.h"
  39. #include "tcpr.h"
  40. /* edit these variables to taste */
  41. static char dlt_name[] = "radiotap";
  42. _U_ static char dlt_prefix[] = "radiotap";
  43. static u_int16_t dlt_value = DLT_IEEE802_11_RADIO;
  44. /*
  45. * The Radiotap header plugin utilizes the 802.11 plugin internally to do all the work
  46. * we just eat the radiotap header itself and pass the resulting buffer to the ieee80211
  47. * plugin.
  48. */
  49. static u_char *dlt_radiotap_get_80211(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen, const int radiolen);
  50. /*
  51. * Function to register ourselves. This function is always called, regardless
  52. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  53. * or anything like that (use the dlt_radiotap_init() function below for that).
  54. * Tasks:
  55. * - Create a new plugin struct
  56. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  57. * actually in the header.
  58. * - Add the plugin to the context's plugin chain
  59. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  60. */
  61. int
  62. dlt_radiotap_register(tcpeditdlt_t *ctx)
  63. {
  64. tcpeditdlt_plugin_t *plugin;
  65. assert(ctx);
  66. /* create a new plugin structure */
  67. plugin = tcpedit_dlt_newplugin();
  68. /* we're a decoder only plugin, copy from ieee802.11 */
  69. plugin->provides += PLUGIN_MASK_PROTO + PLUGIN_MASK_SRCADDR + PLUGIN_MASK_DSTADDR;
  70. plugin->requires += 0;
  71. /* what is our DLT value? */
  72. plugin->dlt = dlt_value;
  73. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  74. plugin->name = safe_strdup(dlt_name);
  75. /*
  76. * Point to our functions, note, you need a function for EVERY method.
  77. * Even if it is only an empty stub returning success.
  78. */
  79. plugin->plugin_init = dlt_radiotap_init;
  80. plugin->plugin_cleanup = dlt_radiotap_cleanup;
  81. plugin->plugin_parse_opts = dlt_radiotap_parse_opts;
  82. plugin->plugin_decode = dlt_radiotap_decode;
  83. plugin->plugin_encode = dlt_radiotap_encode;
  84. plugin->plugin_proto = dlt_radiotap_proto;
  85. plugin->plugin_l2addr_type = dlt_radiotap_l2addr_type;
  86. plugin->plugin_l2len = dlt_radiotap_80211_l2len;
  87. plugin->plugin_get_layer3 = dlt_radiotap_get_layer3;
  88. plugin->plugin_merge_layer3 = dlt_radiotap_merge_layer3;
  89. plugin->plugin_get_mac = dlt_radiotap_get_mac;
  90. /* add it to the available plugin list */
  91. return tcpedit_dlt_addplugin(ctx, plugin);
  92. }
  93. /*
  94. * Initializer function. This function is called only once, if and only iif
  95. * this plugin will be utilized. Remember, if you need to keep track of any state,
  96. * store it in your plugin->config, not a global!
  97. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  98. */
  99. int
  100. dlt_radiotap_init(tcpeditdlt_t *ctx)
  101. {
  102. tcpeditdlt_plugin_t *plugin;
  103. radiotap_config_t *config;
  104. assert(ctx);
  105. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  106. tcpedit_seterr(ctx->tcpedit, "Unable to initalize unregistered plugin %s", dlt_name);
  107. return TCPEDIT_ERROR;
  108. }
  109. /* allocate memory for our deocde extra data */
  110. if (sizeof(radiotap_extra_t) > 0)
  111. ctx->decoded_extra = safe_malloc(sizeof(radiotap_extra_t));
  112. /* allocate memory for our config data */
  113. if (sizeof(radiotap_config_t) > 0)
  114. plugin->config = safe_malloc(sizeof(radiotap_config_t));
  115. config = (radiotap_config_t *)plugin->config;
  116. return TCPEDIT_OK; /* success */
  117. }
  118. /*
  119. * Since this is used in a library, we should manually clean up after ourselves
  120. * Unless you allocated some memory in dlt_radiotap_init(), this is just an stub.
  121. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  122. */
  123. int
  124. dlt_radiotap_cleanup(tcpeditdlt_t *ctx)
  125. {
  126. tcpeditdlt_plugin_t *plugin;
  127. assert(ctx);
  128. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  129. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  130. return TCPEDIT_ERROR;
  131. }
  132. if (ctx->decoded_extra != NULL) {
  133. safe_free(ctx->decoded_extra);
  134. ctx->decoded_extra = NULL;
  135. }
  136. if (plugin->config != NULL) {
  137. safe_free(plugin->config);
  138. plugin->config = NULL;
  139. }
  140. return TCPEDIT_OK; /* success */
  141. }
  142. /*
  143. * This is where you should define all your AutoGen AutoOpts option parsing.
  144. * Any user specified option should have it's bit turned on in the 'provides'
  145. * bit mask.
  146. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  147. */
  148. int
  149. dlt_radiotap_parse_opts(tcpeditdlt_t *ctx)
  150. {
  151. assert(ctx);
  152. /* we have none */
  153. return TCPEDIT_OK; /* success */
  154. }
  155. /*
  156. * Function to decode the layer 2 header in the packet.
  157. * You need to fill out:
  158. * - ctx->l2len
  159. * - ctx->srcaddr
  160. * - ctx->dstaddr
  161. * - ctx->proto
  162. * - ctx->decoded_extra
  163. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  164. */
  165. int
  166. dlt_radiotap_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  167. {
  168. int radiolen, rcode;
  169. u_char *data;
  170. assert(ctx);
  171. assert(packet);
  172. assert(pktlen >= (int)sizeof(radiotap_hdr_t));
  173. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  174. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  175. /* ieee80211 decoder fills out everything */
  176. rcode = dlt_ieee80211_decode(ctx, data, pktlen - radiolen);
  177. /* need to override the ieee802.11 l2 length result */
  178. ctx->l2len = dlt_radiotap_80211_l2len(ctx, packet, pktlen);
  179. return rcode;
  180. }
  181. /*
  182. * Function to encode the layer 2 header back into the packet.
  183. * Returns: total packet len or TCPEDIT_ERROR
  184. */
  185. int
  186. dlt_radiotap_encode(tcpeditdlt_t *ctx, u_char **packet_ex, int pktlen, _U_ tcpr_dir_t dir)
  187. {
  188. assert(ctx);
  189. assert(packet_ex);
  190. assert(pktlen > 0);
  191. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_IEEE802_11_RADIO plugin does not support packet encoding");
  192. return TCPEDIT_ERROR;
  193. }
  194. /*
  195. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  196. * Make sure you return this in host byte order since all the comparisions will be
  197. * against the ETHERTYPE_* values which are oddly in host byte order.
  198. */
  199. int
  200. dlt_radiotap_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  201. {
  202. int radiolen;
  203. u_char *data;
  204. assert(ctx);
  205. assert(packet);
  206. assert(pktlen > (int)sizeof(radiotap_hdr_t));
  207. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  208. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  209. return dlt_ieee80211_proto(ctx, data, pktlen - radiolen);
  210. }
  211. /*
  212. * Function returns a pointer to the layer 3 protocol header or NULL on error
  213. */
  214. u_char *
  215. dlt_radiotap_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  216. {
  217. int radiolen, l2len;
  218. u_char *data;
  219. assert(ctx);
  220. assert(packet);
  221. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  222. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  223. l2len = dlt_ieee80211_l2len(ctx, data, pktlen - radiolen);
  224. return tcpedit_dlt_l3data_copy(ctx, data, pktlen - radiolen, l2len);
  225. }
  226. /*
  227. * function merges the packet (containing L2 and old L3) with the l3data buffer
  228. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  229. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  230. * like SPARC
  231. */
  232. u_char *
  233. dlt_radiotap_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  234. {
  235. int radiolen, l2len;
  236. u_char *data;
  237. assert(ctx);
  238. assert(packet);
  239. assert(l3data);
  240. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  241. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  242. l2len = dlt_ieee80211_l2len(ctx, data, pktlen);
  243. return tcpedit_dlt_l3data_merge(ctx, data, pktlen - radiolen, l3data, l2len);
  244. }
  245. /*
  246. * return a static pointer to the source/destination MAC address
  247. * return NULL on error/address doesn't exist
  248. */
  249. u_char *
  250. dlt_radiotap_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  251. {
  252. int radiolen;
  253. u_char *data;
  254. assert(ctx);
  255. assert(packet);
  256. assert(pktlen);
  257. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  258. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  259. return dlt_ieee80211_get_mac(ctx, mac, data, pktlen - radiolen);
  260. }
  261. /*
  262. * return the length of the L2 header of the current packet
  263. */
  264. int
  265. dlt_radiotap_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  266. {
  267. u_int16_t radiolen;
  268. assert(ctx);
  269. assert(packet);
  270. assert(pktlen);
  271. memcpy(&radiolen, &packet[2], 2);
  272. return (int)radiolen;
  273. }
  274. /*
  275. * return the length of the L2 header w/ 802.11 header of the current packet
  276. */
  277. int
  278. dlt_radiotap_80211_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  279. {
  280. int radiolen;
  281. u_char *data;
  282. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  283. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  284. radiolen += dlt_ieee80211_l2len(ctx, data, pktlen - radiolen);
  285. return radiolen;
  286. }
  287. tcpeditdlt_l2addr_type_t
  288. dlt_radiotap_l2addr_type(void)
  289. {
  290. /* FIXME: return the tcpeditdlt_l2addr_type_t value that this DLT uses */
  291. return ETHERNET;
  292. }
  293. /*
  294. * returns a buffer to the 802.11 header in the packet.
  295. * This does an optimization of only doing a memcpy() once per packet
  296. * since we track which was the last packet # we copied.
  297. */
  298. static u_char *
  299. dlt_radiotap_get_80211(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen, const int radiolen)
  300. {
  301. radiotap_extra_t *extra;
  302. static COUNTER lastpacket = 0;
  303. extra = (radiotap_extra_t *)(ctx->decoded_extra);
  304. if (lastpacket != ctx->tcpedit->runtime.packetnum) {
  305. memcpy(extra->packet, &packet[radiolen], pktlen - radiolen);
  306. lastpacket = ctx->tcpedit->runtime.packetnum;
  307. }
  308. return extra->packet;
  309. }