dlt_plugins.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 "config.h"
  20. #include <stdlib.h>
  21. #include "tcpedit.h"
  22. #include "plugins.h"
  23. #include "tcpedit_stub.h"
  24. #include "dlt_utils.h"
  25. #include "common.h"
  26. /**
  27. * Include plugin header files here...
  28. */
  29. #include "dlt_en10mb/en10mb.h"
  30. #include "dlt_user/user.h"
  31. #include "dlt_hdlc/hdlc.h"
  32. #include "dlt_raw/raw.h"
  33. #include "dlt_null/null.h"
  34. #include "dlt_loop/loop.h"
  35. #include "dlt_linuxsll/linuxsll.h"
  36. #include "dlt_ieee80211/ieee80211.h"
  37. #include "dlt_radiotap/radiotap.h"
  38. #include "dlt_jnpr_ether/jnpr_ether.h"
  39. #include "dlt_pppserial/pppserial.h"
  40. /**
  41. * Everyone writing a DLT plugin, must add their registration function
  42. * here.
  43. */
  44. int
  45. tcpedit_dlt_register(tcpeditdlt_t *ctx)
  46. {
  47. int retcode = 0;
  48. assert(ctx);
  49. retcode += dlt_en10mb_register(ctx);
  50. retcode += dlt_hdlc_register(ctx);
  51. retcode += dlt_user_register(ctx);
  52. retcode += dlt_raw_register(ctx);
  53. retcode += dlt_null_register(ctx);
  54. retcode += dlt_loop_register(ctx);
  55. retcode += dlt_linuxsll_register(ctx);
  56. retcode += dlt_ieee80211_register(ctx);
  57. retcode += dlt_radiotap_register(ctx);
  58. retcode += dlt_jnpr_ether_register(ctx);
  59. retcode += dlt_pppserial_register(ctx);
  60. if (retcode < 0)
  61. return TCPEDIT_ERROR;
  62. return TCPEDIT_OK;
  63. }
  64. /********************************************************************
  65. * People writing DLT plugins should stop editing here!
  66. *
  67. * Well actually, that's true most of the time, but feel free to take
  68. * a look!
  69. ********************************************************************/
  70. /*
  71. * mapping for bit_mask to bit_info. If you're making changes here
  72. * then you almost certainly need to modify tcpeditdlt_t in dlt_plugins-int.h
  73. */
  74. const uint32_t tcpeditdlt_bit_map[] = {
  75. PLUGIN_MASK_PROTO,
  76. PLUGIN_MASK_SRCADDR,
  77. PLUGIN_MASK_DSTADDR
  78. };
  79. /* Meanings of the above map */
  80. const char *tcpeditdlt_bit_info[] = {
  81. "Missing required Layer 3 protocol.",
  82. "Missing required Layer 2 source address.",
  83. "Missing required Layer 2 destination address."
  84. };
  85. /*********************************************************************
  86. * Internal functions
  87. ********************************************************************/
  88. /*********************************************************************
  89. * Public functions
  90. ********************************************************************/
  91. /**
  92. * initialize our plugin library. Pass the DLT of the source pcap handle.
  93. * Actions:
  94. * - Create new tcpeditdlt_t context
  95. * - Link tcpedit to new context
  96. * - Register plugins
  97. * - Select decoder plugin using srcdlt
  98. * - Select encoder plugin using destination name
  99. * - Initialize decoder/encoder plugins
  100. * - Parse options for encoder plugin
  101. * - Validate provides/reqiures + user options
  102. */
  103. tcpeditdlt_t *
  104. tcpedit_dlt_init(tcpedit_t *tcpedit, const int srcdlt)
  105. {
  106. tcpeditdlt_t *ctx;
  107. int rcode;
  108. assert(tcpedit);
  109. assert(srcdlt >= 0);
  110. ctx = (tcpeditdlt_t *)safe_malloc(sizeof(tcpeditdlt_t));
  111. /* do we need a side buffer for L3 data? */
  112. #ifdef FORCE_ALIGN
  113. ctx->l3buff = (u_char *)safe_malloc(MAXPACKET);
  114. #endif
  115. /* copy our tcpedit context */
  116. ctx->tcpedit = tcpedit;
  117. /* register all our plugins */
  118. if (tcpedit_dlt_register(ctx) != TCPEDIT_OK) {
  119. tcpedit_dlt_cleanup(ctx);
  120. return NULL;
  121. }
  122. /* Choose decode plugin */
  123. if ((ctx->decoder = tcpedit_dlt_getplugin(ctx, srcdlt)) == NULL) {
  124. tcpedit_seterr(tcpedit, "No DLT plugin available for source DLT: 0x%x", srcdlt);
  125. tcpedit_dlt_cleanup(ctx);
  126. return NULL;
  127. }
  128. /* set our dlt type */
  129. ctx->dlt = srcdlt;
  130. /* set our address type */
  131. ctx->addr_type = ctx->decoder->plugin_l2addr_type();
  132. /* initialize decoder plugin */
  133. rcode = ctx->decoder->plugin_init(ctx);
  134. if (tcpedit_checkerror(ctx->tcpedit, rcode, NULL) != TCPEDIT_OK) {
  135. tcpedit_dlt_cleanup(ctx);
  136. return NULL;
  137. }
  138. /* we're OK */
  139. return ctx;
  140. }
  141. /**
  142. * \brief Call this to parse AutoOpts arguments for the DLT encoder plugin
  143. *
  144. * Was previously part of tcpedit_dlt_init(), but moved into it's own function
  145. * to allow a full programtic API. Basically, if you're not using this function
  146. * you'll need to roll your own!
  147. * Returns 0 on success, -1 on error
  148. */
  149. int
  150. tcpedit_dlt_post_args(tcpedit_t *tcpedit)
  151. {
  152. tcpeditdlt_t *ctx;
  153. const char *dst_dlt_name = NULL;
  154. int rcode;
  155. assert(tcpedit);
  156. ctx = tcpedit->dlt_ctx;
  157. assert(ctx);
  158. /* Select the encoder plugin */
  159. dst_dlt_name = OPT_ARG(DLT) ? OPT_ARG(DLT) : ctx->decoder->name;
  160. if ((ctx->encoder = tcpedit_dlt_getplugin_byname(ctx, dst_dlt_name)) == NULL) {
  161. tcpedit_seterr(tcpedit, "No output DLT plugin available for: %s", dst_dlt_name);
  162. return TCPEDIT_ERROR;
  163. }
  164. /* Figure out if we're skipping braodcast & multicast */
  165. if (HAVE_OPT(SKIPL2BROADCAST))
  166. ctx->skip_broadcast = 1;
  167. /* init encoder plugin if it's not the decoder plugin */
  168. if (ctx->encoder->dlt != ctx->decoder->dlt) {
  169. if ((rcode = ctx->encoder->plugin_init(ctx)) != TCPEDIT_OK) {
  170. /* plugin should generate the error */
  171. return TCPEDIT_ERROR;
  172. }
  173. }
  174. /* parse the DLT specific options */
  175. if ((rcode = tcpedit_dlt_parse_opts(ctx)) != TCPEDIT_OK) {
  176. /* parser should generate the error */
  177. return TCPEDIT_ERROR;
  178. }
  179. /* we're OK */
  180. return tcpedit_dlt_post_init(ctx);
  181. }
  182. /**
  183. * This is the recommended method to edit a packet. Returns (new) total packet length
  184. */
  185. int
  186. tcpedit_dlt_process(tcpeditdlt_t *ctx, u_char **packet, int pktlen, tcpr_dir_t direction)
  187. {
  188. int rcode;
  189. assert(ctx);
  190. assert(packet);
  191. assert(direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C || direction == TCPR_DIR_NOSEND);
  192. /* nothing to do here */
  193. if (direction == TCPR_DIR_NOSEND)
  194. return pktlen;
  195. /* decode packet */
  196. if ((rcode = tcpedit_dlt_decode(ctx, *packet, pktlen)) == TCPEDIT_ERROR) {
  197. return TCPEDIT_ERROR;
  198. } else if (rcode == TCPEDIT_WARN) {
  199. warnx("Warning decoding packet: %s", tcpedit_getwarn(ctx->tcpedit));
  200. } else if (rcode == TCPEDIT_SOFT_ERROR) {
  201. return rcode; /* can't edit the packet */
  202. }
  203. /* encode packet */
  204. if ((rcode = tcpedit_dlt_encode(ctx, *packet, pktlen, direction)) == TCPEDIT_ERROR) {
  205. return TCPEDIT_ERROR;
  206. } else if (rcode == TCPEDIT_WARN) {
  207. warnx("Warning encoding packet: %s", tcpedit_getwarn(ctx->tcpedit));
  208. }
  209. return rcode;
  210. }
  211. /**
  212. * \brief Call after tcpedit_dlt_post_args() to allow plugins to do special things
  213. *
  214. * Useful for plugins to initialize sub-plugins and what not.
  215. * Returns the standard TCPEDIT_OK|ERROR|WARN
  216. */
  217. int
  218. tcpedit_dlt_post_init(tcpeditdlt_t *tcpedit)
  219. {
  220. /* first init decoder */
  221. if (tcpedit->decoder->plugin_post_init != NULL)
  222. if (tcpedit->decoder->plugin_post_init(tcpedit) == TCPEDIT_ERROR)
  223. return TCPEDIT_ERROR;
  224. /* the encoder */
  225. if (tcpedit->encoder->plugin_post_init != NULL)
  226. if (tcpedit->encoder->plugin_post_init(tcpedit) == TCPEDIT_ERROR)
  227. return TCPEDIT_ERROR;
  228. return TCPEDIT_OK;
  229. }
  230. /**
  231. * What is the output DLT type???
  232. */
  233. int
  234. tcpedit_dlt_output_dlt(tcpeditdlt_t *ctx)
  235. {
  236. uint16_t dlt;
  237. assert(ctx);
  238. /*
  239. * usually we just return the DLT value of the decoder, but for DLT_USER0
  240. * we return a user-specified value via --user-dlt
  241. */
  242. if (ctx->encoder->dlt == DLT_USER0) {
  243. dlt = dlt_user_get_output_dlt(ctx);
  244. } else {
  245. dlt = ctx->encoder->dlt;
  246. }
  247. return dlt;
  248. }
  249. /**
  250. * Get the layer 2 length of the packet using the DLT plugin currently in
  251. * place
  252. */
  253. int
  254. tcpedit_dlt_l2len(tcpeditdlt_t *ctx, int dlt, const u_char *packet, const int pktlen)
  255. {
  256. tcpeditdlt_plugin_t *plugin;
  257. assert(ctx);
  258. assert(dlt >= 0);
  259. assert(packet);
  260. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  261. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  262. return -1;
  263. }
  264. return plugin->plugin_l2len(ctx, packet, pktlen);
  265. }
  266. /**
  267. * Get the L3 type. Returns -1 on error. Get error via tcpedit->geterr()
  268. */
  269. int
  270. tcpedit_dlt_proto(tcpeditdlt_t *ctx, int dlt, const u_char *packet, const int pktlen)
  271. {
  272. tcpeditdlt_plugin_t *plugin;
  273. assert(ctx);
  274. assert(dlt >= 0);
  275. assert(packet);
  276. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  277. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  278. return -1;
  279. }
  280. return plugin->plugin_proto(ctx, packet, pktlen);
  281. }
  282. /**
  283. * Get the L3 data. Returns NULL on error. Get error via tcpedit->geterr()
  284. */
  285. u_char *
  286. tcpedit_dlt_l3data(tcpeditdlt_t *ctx, int dlt, u_char *packet, const int pktlen)
  287. {
  288. tcpeditdlt_plugin_t *plugin;
  289. assert(ctx);
  290. assert(dlt >= 0);
  291. assert(packet);
  292. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  293. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  294. return NULL;
  295. }
  296. return plugin->plugin_get_layer3(ctx, packet, pktlen);
  297. }
  298. /**
  299. * \brief Merge the Layer 3 data back onto the mainbuffer so it's immediately
  300. * after the layer 2 header
  301. *
  302. * Since some L2 headers aren't strictly aligned, we need to "merge" the packet w/ L2 data
  303. * and the L3 buffer. This is basically a NO-OP for things like vlan tagged ethernet (16 byte) header
  304. * or Cisco HDLC (4 byte header) but is critical for std ethernet (12 byte header)
  305. */
  306. u_char *
  307. tcpedit_dlt_merge_l3data(tcpeditdlt_t *ctx, int dlt, u_char *packet, const int pktlen, u_char *l3data)
  308. {
  309. tcpeditdlt_plugin_t *plugin;
  310. assert(ctx);
  311. assert(dlt >= 0);
  312. assert(packet);
  313. if (l3data == NULL)
  314. return packet;
  315. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  316. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  317. return NULL;
  318. }
  319. return plugin->plugin_merge_layer3(ctx, packet, pktlen, l3data);
  320. }
  321. /**
  322. * Call the specific plugin decode() method
  323. */
  324. int
  325. tcpedit_dlt_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  326. {
  327. return ctx->decoder->plugin_decode(ctx, packet, pktlen);
  328. }
  329. /**
  330. * Call the specific plugin encode() method
  331. */
  332. int
  333. tcpedit_dlt_encode(tcpeditdlt_t* ctx, u_char *packet, int pktlen, tcpr_dir_t direction)
  334. {
  335. return ctx->encoder->plugin_encode(ctx, packet, pktlen, direction);
  336. }
  337. /**
  338. * what is the source (decoder) DLT type?
  339. */
  340. int
  341. tcpedit_dlt_src(tcpeditdlt_t *ctx)
  342. {
  343. assert(ctx);
  344. return ctx->decoder->dlt;
  345. }
  346. /**
  347. * What is the destination (encoder) DLT type
  348. */
  349. int
  350. tcpedit_dlt_dst(tcpeditdlt_t *ctx)
  351. {
  352. assert(ctx);
  353. return ctx->encoder->dlt;
  354. }
  355. /**
  356. * cleanup after ourselves: destroys our context and all plugin data
  357. */
  358. void
  359. tcpedit_dlt_cleanup(tcpeditdlt_t *ctx)
  360. {
  361. assert(ctx);
  362. if (ctx->encoder != NULL)
  363. ctx->encoder->plugin_cleanup(ctx);
  364. if (ctx->decoder != NULL)
  365. ctx->decoder->plugin_cleanup(ctx);
  366. #ifdef FORCE_ALIGN
  367. safe_free(ctx->l3buff);
  368. #endif
  369. if (ctx->decoded_extra != NULL)
  370. safe_free(ctx->decoded_extra);
  371. safe_free(ctx);
  372. }