radiotap.c 11 KB

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