radiotap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "tcpedit.h"
  22. #include "common.h"
  23. #include "tcpr.h"
  24. #include "dlt_utils.h"
  25. #include "tcpedit_stub.h"
  26. #include "radiotap.h"
  27. #include "../dlt_ieee80211/ieee80211.h"
  28. /* edit these variables to taste */
  29. static char dlt_name[] = "radiotap";
  30. _U_ static char dlt_prefix[] = "radiotap";
  31. static uint16_t dlt_value = DLT_IEEE802_11_RADIO;
  32. /*
  33. * The Radiotap header plugin utilizes the 802.11 plugin internally to do all the work
  34. * we just eat the radiotap header itself and pass the resulting buffer to the ieee80211
  35. * plugin.
  36. */
  37. static u_char *dlt_radiotap_get_80211(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen, const int radiolen);
  38. /*
  39. * Function to register ourselves. This function is always called, regardless
  40. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  41. * or anything like that (use the dlt_radiotap_init() function below for that).
  42. * Tasks:
  43. * - Create a new plugin struct
  44. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  45. * actually in the header.
  46. * - Add the plugin to the context's plugin chain
  47. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  48. */
  49. int
  50. dlt_radiotap_register(tcpeditdlt_t *ctx)
  51. {
  52. tcpeditdlt_plugin_t *plugin;
  53. assert(ctx);
  54. /* create a new plugin structure */
  55. plugin = tcpedit_dlt_newplugin();
  56. /* we're a decoder only plugin, copy from ieee802.11 */
  57. plugin->provides += PLUGIN_MASK_PROTO + PLUGIN_MASK_SRCADDR + PLUGIN_MASK_DSTADDR;
  58. plugin->requires += 0;
  59. /* what is our DLT value? */
  60. plugin->dlt = dlt_value;
  61. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  62. plugin->name = safe_strdup(dlt_name);
  63. /*
  64. * Point to our functions, note, you need a function for EVERY method.
  65. * Even if it is only an empty stub returning success.
  66. */
  67. plugin->plugin_init = dlt_radiotap_init;
  68. plugin->plugin_cleanup = dlt_radiotap_cleanup;
  69. plugin->plugin_parse_opts = dlt_radiotap_parse_opts;
  70. plugin->plugin_decode = dlt_radiotap_decode;
  71. plugin->plugin_encode = dlt_radiotap_encode;
  72. plugin->plugin_proto = dlt_radiotap_proto;
  73. plugin->plugin_l2addr_type = dlt_radiotap_l2addr_type;
  74. plugin->plugin_l2len = dlt_radiotap_80211_l2len;
  75. plugin->plugin_get_layer3 = dlt_radiotap_get_layer3;
  76. plugin->plugin_merge_layer3 = dlt_radiotap_merge_layer3;
  77. plugin->plugin_get_mac = dlt_radiotap_get_mac;
  78. /* add it to the available plugin list */
  79. return tcpedit_dlt_addplugin(ctx, plugin);
  80. }
  81. /*
  82. * Initializer function. This function is called only once, if and only iif
  83. * this plugin will be utilized. Remember, if you need to keep track of any state,
  84. * store it in your plugin->config, not a global!
  85. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  86. */
  87. int
  88. dlt_radiotap_init(tcpeditdlt_t *ctx)
  89. {
  90. tcpeditdlt_plugin_t *plugin;
  91. assert(ctx);
  92. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  93. tcpedit_seterr(ctx->tcpedit, "Unable to initialize unregistered plugin %s", dlt_name);
  94. return TCPEDIT_ERROR;
  95. }
  96. /* allocate memory for our deocde extra data */
  97. if (sizeof(radiotap_extra_t) > 0)
  98. ctx->decoded_extra = safe_malloc(sizeof(radiotap_extra_t));
  99. /* allocate memory for our config data */
  100. if (sizeof(radiotap_config_t) > 0)
  101. plugin->config = safe_malloc(sizeof(radiotap_config_t));
  102. return TCPEDIT_OK; /* success */
  103. }
  104. /*
  105. * Since this is used in a library, we should manually clean up after ourselves
  106. * Unless you allocated some memory in dlt_radiotap_init(), this is just an stub.
  107. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  108. */
  109. int
  110. dlt_radiotap_cleanup(tcpeditdlt_t *ctx)
  111. {
  112. tcpeditdlt_plugin_t *plugin;
  113. assert(ctx);
  114. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  115. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  116. return TCPEDIT_ERROR;
  117. }
  118. if (ctx->decoded_extra != NULL) {
  119. safe_free(ctx->decoded_extra);
  120. ctx->decoded_extra = NULL;
  121. }
  122. if (plugin->config != NULL) {
  123. safe_free(plugin->config);
  124. plugin->config = NULL;
  125. }
  126. return TCPEDIT_OK; /* success */
  127. }
  128. /*
  129. * This is where you should define all your AutoGen AutoOpts option parsing.
  130. * Any user specified option should have it's bit turned on in the 'provides'
  131. * bit mask.
  132. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  133. */
  134. int
  135. dlt_radiotap_parse_opts(tcpeditdlt_t *ctx)
  136. {
  137. assert(ctx);
  138. /* we have none */
  139. return TCPEDIT_OK; /* success */
  140. }
  141. /*
  142. * Function to decode the layer 2 header in the packet.
  143. * You need to fill out:
  144. * - ctx->l2len
  145. * - ctx->srcaddr
  146. * - ctx->dstaddr
  147. * - ctx->proto
  148. * - ctx->decoded_extra
  149. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  150. */
  151. int
  152. dlt_radiotap_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  153. {
  154. int radiolen, rcode;
  155. u_char *data;
  156. assert(ctx);
  157. assert(packet);
  158. if (pktlen < (int)sizeof(radiotap_hdr_t))
  159. return TCPEDIT_ERROR;
  160. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  161. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  162. /* ieee80211 decoder fills out everything */
  163. rcode = dlt_ieee80211_decode(ctx, data, pktlen - radiolen);
  164. /* need to override the ieee802.11 l2 length result */
  165. ctx->l2len = dlt_radiotap_80211_l2len(ctx, packet, pktlen);
  166. return rcode;
  167. }
  168. /*
  169. * Function to encode the layer 2 header back into the packet.
  170. * Returns: total packet len or TCPEDIT_ERROR
  171. */
  172. int
  173. dlt_radiotap_encode(tcpeditdlt_t *ctx, u_char *packet, int pktlen, _U_ tcpr_dir_t dir)
  174. {
  175. assert(ctx);
  176. assert(packet);
  177. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_IEEE802_11_RADIO plugin does not support packet encoding");
  178. return TCPEDIT_ERROR;
  179. }
  180. /*
  181. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  182. * Make sure you return this in host byte order since all the comparisions will be
  183. * against the ETHERTYPE_* values which are oddly in host byte order.
  184. */
  185. int
  186. dlt_radiotap_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  187. {
  188. int radiolen;
  189. u_char *data;
  190. assert(ctx);
  191. assert(packet);
  192. if (pktlen < (int)sizeof(radiotap_hdr_t))
  193. return TCPEDIT_ERROR;
  194. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  195. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  196. return dlt_ieee80211_proto(ctx, data, pktlen - radiolen);
  197. }
  198. /*
  199. * Function returns a pointer to the layer 3 protocol header or NULL on error
  200. */
  201. u_char *
  202. dlt_radiotap_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  203. {
  204. int radiolen, l2len;
  205. u_char *data;
  206. assert(ctx);
  207. assert(packet);
  208. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  209. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  210. l2len = dlt_ieee80211_l2len(ctx, data, pktlen - radiolen);
  211. return tcpedit_dlt_l3data_copy(ctx, data, pktlen - radiolen, l2len);
  212. }
  213. /*
  214. * function merges the packet (containing L2 and old L3) with the l3data buffer
  215. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  216. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  217. * like SPARC
  218. */
  219. u_char *
  220. dlt_radiotap_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  221. {
  222. int radiolen, l2len;
  223. u_char *data;
  224. assert(ctx);
  225. assert(packet);
  226. assert(l3data);
  227. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  228. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  229. l2len = dlt_ieee80211_l2len(ctx, data, pktlen);
  230. return tcpedit_dlt_l3data_merge(ctx, data, pktlen - radiolen, l3data, l2len);
  231. }
  232. /*
  233. * return a static pointer to the source/destination MAC address
  234. * return NULL on error/address doesn't exist
  235. */
  236. u_char *
  237. dlt_radiotap_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  238. {
  239. int radiolen;
  240. u_char *data;
  241. assert(ctx);
  242. assert(packet);
  243. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  244. if (pktlen < radiolen)
  245. return NULL;
  246. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  247. return dlt_ieee80211_get_mac(ctx, mac, data, pktlen - radiolen);
  248. }
  249. /*
  250. * return the length of the L2 header of the current packet
  251. */
  252. int
  253. dlt_radiotap_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  254. {
  255. uint16_t radiolen;
  256. assert(ctx);
  257. assert(packet);
  258. if (pktlen < 4)
  259. return 0;
  260. memcpy(&radiolen, &packet[2], 2);
  261. /* little endian to host */
  262. radiolen = ntohs(SWAPSHORT(radiolen));
  263. return (int)radiolen;
  264. }
  265. /*
  266. * return the length of the L2 header w/ 802.11 header of the current packet
  267. */
  268. int
  269. dlt_radiotap_80211_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  270. {
  271. int radiolen;
  272. u_char *data;
  273. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  274. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  275. radiolen += dlt_ieee80211_l2len(ctx, data, pktlen - radiolen);
  276. return radiolen;
  277. }
  278. tcpeditdlt_l2addr_type_t
  279. dlt_radiotap_l2addr_type(void)
  280. {
  281. /* FIXME: return the tcpeditdlt_l2addr_type_t value that this DLT uses */
  282. return ETHERNET;
  283. }
  284. /*
  285. * returns a buffer to the 802.11 header in the packet.
  286. * This does an optimization of only doing a memcpy() once per packet
  287. * since we track which was the last packet # we copied.
  288. */
  289. static u_char *
  290. dlt_radiotap_get_80211(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen, const int radiolen)
  291. {
  292. radiotap_extra_t *extra;
  293. static COUNTER lastpacket = 0;
  294. extra = (radiotap_extra_t *)(ctx->decoded_extra);
  295. if (lastpacket != ctx->tcpedit->runtime.packetnum) {
  296. memcpy(extra->packet, &packet[radiolen], pktlen - radiolen);
  297. lastpacket = ctx->tcpedit->runtime.packetnum;
  298. }
  299. return extra->packet;
  300. }