dlt_plugins.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 "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;
  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. int res;
  258. assert(ctx);
  259. assert(dlt >= 0);
  260. assert(packet);
  261. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  262. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  263. return -1;
  264. }
  265. res = plugin->plugin_l2len(ctx, packet, pktlen);
  266. if (res == -1) {
  267. tcpedit_seterr(ctx->tcpedit, "Packet length %d is to short to contain a layer 2 header for DLT 0x%04x",
  268. pktlen, dlt);
  269. return -1;
  270. }
  271. return res;
  272. }
  273. /**
  274. * Get the L3 type. Returns -1 on error. Get error via tcpedit->geterr()
  275. */
  276. int
  277. tcpedit_dlt_proto(tcpeditdlt_t *ctx, int dlt, const u_char *packet, const int pktlen)
  278. {
  279. tcpeditdlt_plugin_t *plugin;
  280. assert(ctx);
  281. assert(dlt >= 0);
  282. assert(packet);
  283. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  284. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  285. return -1;
  286. }
  287. return plugin->plugin_proto(ctx, packet, pktlen);
  288. }
  289. /**
  290. * Get the L3 data. Returns NULL on error. Get error via tcpedit->geterr()
  291. */
  292. u_char *
  293. tcpedit_dlt_l3data(tcpeditdlt_t *ctx, int dlt, u_char *packet, const int pktlen)
  294. {
  295. tcpeditdlt_plugin_t *plugin;
  296. u_char *res;
  297. assert(ctx);
  298. assert(dlt >= 0);
  299. assert(packet);
  300. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  301. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  302. return NULL;
  303. }
  304. res = plugin->plugin_get_layer3(ctx, packet, pktlen);
  305. if (res == NULL)
  306. tcpedit_seterr(ctx->tcpedit, "Packet length %d is to short to contain a layer 3 header for DLT 0x%04x",
  307. pktlen, dlt);
  308. return res;
  309. }
  310. /**
  311. * \brief Merge the Layer 3 data back onto the mainbuffer so it's immediately
  312. * after the layer 2 header
  313. *
  314. * Since some L2 headers aren't strictly aligned, we need to "merge" the packet w/ L2 data
  315. * and the L3 buffer. This is basically a NO-OP for things like vlan tagged ethernet (16 byte) header
  316. * or Cisco HDLC (4 byte header) but is critical for std ethernet (12 byte header)
  317. */
  318. u_char *
  319. tcpedit_dlt_merge_l3data(tcpeditdlt_t *ctx, int dlt, u_char *packet, const int pktlen, u_char *l3data)
  320. {
  321. tcpeditdlt_plugin_t *plugin;
  322. u_char *res;
  323. assert(ctx);
  324. assert(dlt >= 0);
  325. assert(packet);
  326. if (l3data == NULL)
  327. return packet;
  328. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  329. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  330. return NULL;
  331. }
  332. res = plugin->plugin_merge_layer3(ctx, packet, pktlen, l3data);
  333. if (res == NULL)
  334. tcpedit_seterr(ctx->tcpedit, "Packet length %d is to short for layer 3 merge for DLT 0x%04x",
  335. pktlen, dlt);
  336. return res;
  337. }
  338. /**
  339. * Call the specific plugin decode() method
  340. */
  341. int
  342. tcpedit_dlt_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  343. {
  344. return ctx->decoder->plugin_decode(ctx, packet, pktlen);
  345. }
  346. /**
  347. * Call the specific plugin encode() method
  348. */
  349. int
  350. tcpedit_dlt_encode(tcpeditdlt_t* ctx, u_char *packet, int pktlen, tcpr_dir_t direction)
  351. {
  352. return ctx->encoder->plugin_encode(ctx, packet, pktlen, direction);
  353. }
  354. /**
  355. * what is the source (decoder) DLT type?
  356. */
  357. int
  358. tcpedit_dlt_src(tcpeditdlt_t *ctx)
  359. {
  360. assert(ctx);
  361. return ctx->decoder->dlt;
  362. }
  363. /**
  364. * What is the destination (encoder) DLT type
  365. */
  366. int
  367. tcpedit_dlt_dst(tcpeditdlt_t *ctx)
  368. {
  369. assert(ctx);
  370. return ctx->encoder->dlt;
  371. }
  372. /**
  373. * cleanup after ourselves: destroys our context and all plugin data
  374. */
  375. void
  376. tcpedit_dlt_cleanup(tcpeditdlt_t *ctx)
  377. {
  378. assert(ctx);
  379. if (ctx->encoder != NULL)
  380. ctx->encoder->plugin_cleanup(ctx);
  381. if (ctx->decoder != NULL)
  382. ctx->decoder->plugin_cleanup(ctx);
  383. #ifdef FORCE_ALIGN
  384. safe_free(ctx->l3buff);
  385. #endif
  386. if (ctx->decoded_extra != NULL)
  387. safe_free(ctx->decoded_extra);
  388. safe_free(ctx);
  389. }