radiotap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 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 if
  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 decode extra data */
  97. if (ctx->decoded_extra_size > 0) {
  98. if (ctx->decoded_extra_size < sizeof(radiotap_extra_t)) {
  99. ctx->decoded_extra_size = sizeof(radiotap_extra_t);
  100. ctx->decoded_extra = safe_realloc(ctx->decoded_extra,
  101. ctx->decoded_extra_size);
  102. }
  103. } else {
  104. ctx->decoded_extra_size = sizeof(radiotap_extra_t);
  105. ctx->decoded_extra = safe_malloc(ctx->decoded_extra_size);
  106. }
  107. /* allocate memory for our config data */
  108. plugin->config_size = sizeof(radiotap_config_t);
  109. plugin->config = safe_malloc(plugin->config_size);
  110. return TCPEDIT_OK; /* success */
  111. }
  112. /*
  113. * Since this is used in a library, we should manually clean up after ourselves
  114. * Unless you allocated some memory in dlt_radiotap_init(), this is just an stub.
  115. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  116. */
  117. int
  118. dlt_radiotap_cleanup(tcpeditdlt_t *ctx)
  119. {
  120. tcpeditdlt_plugin_t *plugin;
  121. assert(ctx);
  122. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  123. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  124. return TCPEDIT_ERROR;
  125. }
  126. safe_free(plugin->name);
  127. plugin->name = NULL;
  128. safe_free(plugin->config);
  129. plugin->config = NULL;
  130. plugin->config_size = 0;
  131. return TCPEDIT_OK; /* success */
  132. }
  133. /*
  134. * This is where you should define all your AutoGen AutoOpts option parsing.
  135. * Any user specified option should have it's bit turned on in the 'provides'
  136. * bit mask.
  137. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  138. */
  139. int
  140. dlt_radiotap_parse_opts(tcpeditdlt_t *ctx)
  141. {
  142. assert(ctx);
  143. /* we have none */
  144. return TCPEDIT_OK; /* success */
  145. }
  146. /*
  147. * Function to decode the layer 2 header in the packet.
  148. * You need to fill out:
  149. * - ctx->l2len
  150. * - ctx->srcaddr
  151. * - ctx->dstaddr
  152. * - ctx->proto
  153. * - ctx->decoded_extra
  154. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  155. */
  156. int
  157. dlt_radiotap_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  158. {
  159. int radiolen, rcode;
  160. u_char *data;
  161. assert(ctx);
  162. assert(packet);
  163. if (pktlen < (int)sizeof(radiotap_hdr_t))
  164. return TCPEDIT_ERROR;
  165. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  166. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  167. if (!data)
  168. return TCPEDIT_ERROR;
  169. /* ieee80211 decoder fills out everything */
  170. rcode = dlt_ieee80211_decode(ctx, data, pktlen - radiolen);
  171. /* need to override the ieee802.11 l2 length result */
  172. ctx->l2len = dlt_radiotap_80211_l2len(ctx, packet, pktlen);
  173. return (ctx->l2len > 0) ? rcode : TCPEDIT_ERROR;
  174. }
  175. /*
  176. * Function to encode the layer 2 header back into the packet.
  177. * Returns: total packet len or TCPEDIT_ERROR
  178. */
  179. int
  180. dlt_radiotap_encode(tcpeditdlt_t *ctx, u_char *packet, _U_ int pktlen,
  181. _U_ tcpr_dir_t dir)
  182. {
  183. assert(ctx);
  184. assert(packet);
  185. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_IEEE802_11_RADIO plugin does not support packet encoding");
  186. return TCPEDIT_ERROR;
  187. }
  188. /*
  189. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  190. * Make sure you return this in host byte order since all the comparisons will be
  191. * against the ETHERTYPE_* values which are oddly in host byte order.
  192. */
  193. int
  194. dlt_radiotap_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  195. {
  196. int radiolen;
  197. u_char *data;
  198. assert(ctx);
  199. assert(packet);
  200. if (pktlen < (int)sizeof(radiotap_hdr_t))
  201. return TCPEDIT_ERROR;
  202. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  203. if (radiolen < 0 || radiolen > pktlen)
  204. return TCPEDIT_ERROR;
  205. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  206. return dlt_ieee80211_proto(ctx, data, pktlen - radiolen);
  207. }
  208. /*
  209. * Function returns a pointer to the layer 3 protocol header or NULL on error
  210. */
  211. u_char *
  212. dlt_radiotap_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  213. {
  214. int radiolen, l2len;
  215. u_char *data;
  216. assert(ctx);
  217. assert(packet);
  218. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  219. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  220. l2len = dlt_ieee80211_l2len(ctx, data, pktlen - radiolen);
  221. return tcpedit_dlt_l3data_copy(ctx, data, pktlen - radiolen, l2len);
  222. }
  223. /*
  224. * function merges the packet (containing L2 and old L3) with the l3data buffer
  225. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  226. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  227. * like SPARC
  228. */
  229. u_char *
  230. dlt_radiotap_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  231. {
  232. int radiolen, l2len;
  233. u_char *data;
  234. assert(ctx);
  235. assert(packet);
  236. assert(l3data);
  237. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  238. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  239. l2len = dlt_ieee80211_l2len(ctx, data, pktlen);
  240. return tcpedit_dlt_l3data_merge(ctx, data, pktlen - radiolen, l3data, l2len);
  241. }
  242. /*
  243. * return a static pointer to the source/destination MAC address
  244. * return NULL on error/address doesn't exist
  245. */
  246. u_char *
  247. dlt_radiotap_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  248. {
  249. int radiolen;
  250. u_char *data;
  251. assert(ctx);
  252. assert(packet);
  253. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  254. if (pktlen < radiolen)
  255. return NULL;
  256. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  257. return dlt_ieee80211_get_mac(ctx, mac, data, pktlen - radiolen);
  258. }
  259. /*
  260. * return the length of the L2 header of the current packet
  261. */
  262. int
  263. dlt_radiotap_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  264. {
  265. uint16_t radiolen;
  266. assert(ctx);
  267. assert(packet);
  268. if (pktlen < 4)
  269. return -1;
  270. memcpy(&radiolen, &packet[2], 2);
  271. /* little endian to host */
  272. radiolen = ntohs(SWAPSHORT(radiolen));
  273. return (int)radiolen;
  274. }
  275. /*
  276. * return the length of the L2 header w/ 802.11 header of the current packet
  277. */
  278. int
  279. dlt_radiotap_80211_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  280. {
  281. int radiolen, res;
  282. u_char *data;
  283. radiolen = dlt_radiotap_l2len(ctx, packet, pktlen);
  284. if (radiolen == -1)
  285. return TCPEDIT_ERROR;
  286. data = dlt_radiotap_get_80211(ctx, packet, pktlen, radiolen);
  287. res = dlt_ieee80211_l2len(ctx, data, pktlen - radiolen);
  288. if (res == -1)
  289. return TCPEDIT_ERROR;
  290. radiolen += res;
  291. return radiolen;
  292. }
  293. tcpeditdlt_l2addr_type_t
  294. dlt_radiotap_l2addr_type(void)
  295. {
  296. /* FIXME: return the tcpeditdlt_l2addr_type_t value that this DLT uses */
  297. return ETHERNET;
  298. }
  299. /*
  300. * returns a buffer to the 802.11 header in the packet.
  301. * This does an optimization of only doing a memcpy() once per packet
  302. * since we track which was the last packet # we copied.
  303. */
  304. static u_char *
  305. dlt_radiotap_get_80211(tcpeditdlt_t *ctx, const u_char *packet,
  306. const int pktlen, const int radiolen)
  307. {
  308. radiotap_extra_t *extra;
  309. static COUNTER lastpacket = 0;
  310. if (ctx->decoded_extra_size < sizeof(*extra))
  311. return NULL;
  312. extra = (radiotap_extra_t *)(ctx->decoded_extra);
  313. if (pktlen >= radiolen &&
  314. (size_t)(pktlen - radiolen) >= sizeof(extra->packet) &&
  315. lastpacket != ctx->tcpedit->runtime.packetnum) {
  316. memcpy(extra->packet, &packet[radiolen], pktlen - radiolen);
  317. lastpacket = ctx->tcpedit->runtime.packetnum;
  318. }
  319. return extra->packet;
  320. }