dlt_plugins.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2022 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. /* copy our tcpedit context */
  113. ctx->tcpedit = tcpedit;
  114. /* register all our plugins */
  115. if (tcpedit_dlt_register(ctx) != TCPEDIT_OK) {
  116. tcpedit_dlt_cleanup(ctx);
  117. return NULL;
  118. }
  119. /* Choose decode plugin */
  120. if ((ctx->decoder = tcpedit_dlt_getplugin(ctx, srcdlt)) == NULL) {
  121. tcpedit_seterr(tcpedit, "No DLT plugin available for source DLT: 0x%x", srcdlt);
  122. tcpedit_dlt_cleanup(ctx);
  123. return NULL;
  124. }
  125. /* set our dlt type */
  126. ctx->dlt = srcdlt;
  127. /* set our address type */
  128. ctx->addr_type = ctx->decoder->plugin_l2addr_type();
  129. /* initialize decoder plugin */
  130. rcode = ctx->decoder->plugin_init(ctx);
  131. if (tcpedit_checkerror(ctx->tcpedit, rcode, NULL) != TCPEDIT_OK) {
  132. tcpedit_dlt_cleanup(ctx);
  133. return NULL;
  134. }
  135. /* we're OK */
  136. return ctx;
  137. }
  138. /**
  139. * \brief Call this to parse AutoOpts arguments for the DLT encoder plugin
  140. *
  141. * Was previously part of tcpedit_dlt_init(), but moved into it's own function
  142. * to allow a full programtic API. Basically, if you're not using this function
  143. * you'll need to roll your own!
  144. * Returns 0 on success, -1 on error
  145. */
  146. int
  147. tcpedit_dlt_post_args(tcpedit_t *tcpedit)
  148. {
  149. tcpeditdlt_t *ctx;
  150. const char *dst_dlt_name;
  151. int rcode;
  152. assert(tcpedit);
  153. ctx = tcpedit->dlt_ctx;
  154. assert(ctx);
  155. /* Select the encoder plugin */
  156. dst_dlt_name = OPT_ARG(DLT) ? OPT_ARG(DLT) : ctx->decoder->name;
  157. if ((ctx->encoder = tcpedit_dlt_getplugin_byname(ctx, dst_dlt_name)) == NULL) {
  158. tcpedit_seterr(tcpedit, "No output DLT plugin available for: %s", dst_dlt_name);
  159. return TCPEDIT_ERROR;
  160. }
  161. /* Figure out if we're skipping braodcast & multicast */
  162. if (HAVE_OPT(SKIPL2BROADCAST))
  163. ctx->skip_broadcast = 1;
  164. /* init encoder plugin if it's not the decoder plugin */
  165. if (ctx->encoder->dlt != ctx->decoder->dlt) {
  166. if ((rcode = ctx->encoder->plugin_init(ctx)) != TCPEDIT_OK) {
  167. /* plugin should generate the error */
  168. return TCPEDIT_ERROR;
  169. }
  170. }
  171. /* parse the DLT specific options */
  172. if ((rcode = tcpedit_dlt_parse_opts(ctx)) != TCPEDIT_OK) {
  173. /* parser should generate the error */
  174. return TCPEDIT_ERROR;
  175. }
  176. /* we're OK */
  177. return tcpedit_dlt_post_init(ctx);
  178. }
  179. /**
  180. * This is the recommended method to edit a packet. Returns (new) total packet length
  181. */
  182. int
  183. tcpedit_dlt_process(tcpeditdlt_t *ctx, u_char **packet, int pktlen, tcpr_dir_t direction)
  184. {
  185. int rcode;
  186. assert(ctx);
  187. assert(packet);
  188. assert(direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C || direction == TCPR_DIR_NOSEND);
  189. /* nothing to do here */
  190. if (direction == TCPR_DIR_NOSEND)
  191. return pktlen;
  192. /* decode packet */
  193. if ((rcode = tcpedit_dlt_decode(ctx, *packet, pktlen)) == TCPEDIT_ERROR) {
  194. return TCPEDIT_ERROR;
  195. } else if (rcode == TCPEDIT_WARN) {
  196. warnx("Warning decoding packet: %s", tcpedit_getwarn(ctx->tcpedit));
  197. } else if (rcode == TCPEDIT_SOFT_ERROR) {
  198. return rcode; /* can't edit the packet */
  199. }
  200. /* encode packet */
  201. if ((rcode = tcpedit_dlt_encode(ctx, *packet, pktlen, direction)) == TCPEDIT_ERROR) {
  202. return TCPEDIT_ERROR;
  203. } else if (rcode == TCPEDIT_WARN) {
  204. warnx("Warning encoding packet: %s", tcpedit_getwarn(ctx->tcpedit));
  205. }
  206. return rcode;
  207. }
  208. /**
  209. * \brief Call after tcpedit_dlt_post_args() to allow plugins to do special things
  210. *
  211. * Useful for plugins to initialize sub-plugins and what not.
  212. * Returns the standard TCPEDIT_OK|ERROR|WARN
  213. */
  214. int
  215. tcpedit_dlt_post_init(tcpeditdlt_t *tcpedit)
  216. {
  217. /* first init decoder */
  218. if (tcpedit->decoder->plugin_post_init != NULL)
  219. if (tcpedit->decoder->plugin_post_init(tcpedit) == TCPEDIT_ERROR)
  220. return TCPEDIT_ERROR;
  221. /* the encoder */
  222. if (tcpedit->encoder->plugin_post_init != NULL)
  223. if (tcpedit->encoder->plugin_post_init(tcpedit) == TCPEDIT_ERROR)
  224. return TCPEDIT_ERROR;
  225. return TCPEDIT_OK;
  226. }
  227. /**
  228. * What is the output DLT type???
  229. */
  230. int
  231. tcpedit_dlt_output_dlt(tcpeditdlt_t *ctx)
  232. {
  233. uint16_t dlt;
  234. assert(ctx);
  235. /*
  236. * usually we just return the DLT value of the decoder, but for DLT_USER0
  237. * we return a user-specified value via --user-dlt
  238. */
  239. if (ctx->encoder->dlt == DLT_USER0) {
  240. dlt = dlt_user_get_output_dlt(ctx);
  241. } else {
  242. dlt = ctx->encoder->dlt;
  243. }
  244. return dlt;
  245. }
  246. /**
  247. * Get the layer 2 length of the packet using the DLT plugin currently in
  248. * place
  249. */
  250. int
  251. tcpedit_dlt_l2len(tcpeditdlt_t *ctx, int dlt, const u_char *packet, const int pktlen)
  252. {
  253. tcpeditdlt_plugin_t *plugin;
  254. int res;
  255. assert(ctx);
  256. assert(dlt >= 0);
  257. assert(packet);
  258. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  259. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  260. return -1;
  261. }
  262. res = plugin->plugin_l2len(ctx, packet, pktlen);
  263. if (res == -1) {
  264. tcpedit_seterr(ctx->tcpedit, "Packet length %d is to short to contain a layer 2 header for DLT 0x%04x",
  265. pktlen, dlt);
  266. return -1;
  267. }
  268. return res;
  269. }
  270. /**
  271. * Get the L3 type. Returns -1 on error. Get error via tcpedit->geterr()
  272. */
  273. int
  274. tcpedit_dlt_proto(tcpeditdlt_t *ctx, int dlt, const u_char *packet, const int pktlen)
  275. {
  276. tcpeditdlt_plugin_t *plugin;
  277. assert(ctx);
  278. assert(dlt >= 0);
  279. assert(packet);
  280. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  281. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  282. return -1;
  283. }
  284. return plugin->plugin_proto(ctx, packet, pktlen);
  285. }
  286. /**
  287. * Get the L3 data. Returns NULL on error. Get error via tcpedit->geterr()
  288. */
  289. u_char *
  290. tcpedit_dlt_l3data(tcpeditdlt_t *ctx, int dlt, u_char *packet, const int pktlen)
  291. {
  292. tcpeditdlt_plugin_t *plugin;
  293. u_char *res;
  294. assert(ctx);
  295. assert(dlt >= 0);
  296. assert(packet);
  297. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  298. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  299. return NULL;
  300. }
  301. res = plugin->plugin_get_layer3(ctx, packet, pktlen);
  302. if (res == NULL)
  303. tcpedit_seterr(ctx->tcpedit, "Packet length %d is to short to contain a layer 3 header for DLT 0x%04x",
  304. pktlen, dlt);
  305. return res;
  306. }
  307. /**
  308. * \brief Merge the Layer 3 data back onto the mainbuffer so it's immediately
  309. * after the layer 2 header
  310. *
  311. * Since some L2 headers aren't strictly aligned, we need to "merge" the packet w/ L2 data
  312. * and the L3 buffer. This is basically a NO-OP for things like vlan tagged ethernet (16 byte) header
  313. * or Cisco HDLC (4 byte header) but is critical for std ethernet (12 byte header)
  314. */
  315. u_char *
  316. tcpedit_dlt_merge_l3data(tcpeditdlt_t *ctx,
  317. int dlt,
  318. u_char *packet,
  319. const int pktlen,
  320. u_char *ipv4_data,
  321. u_char *ipv6_data)
  322. {
  323. tcpeditdlt_plugin_t *plugin;
  324. u_char *res;
  325. assert(ctx);
  326. assert(dlt >= 0);
  327. assert(packet);
  328. if (ipv4_data == NULL && ipv6_data == NULL)
  329. return packet;
  330. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  331. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  332. return NULL;
  333. }
  334. res = plugin->plugin_merge_layer3(ctx, packet, pktlen, ipv4_data, ipv6_data);
  335. if (res == NULL)
  336. tcpedit_seterr(ctx->tcpedit, "Packet length %d is to short for layer 3 merge for DLT 0x%04x",
  337. pktlen, dlt);
  338. return res;
  339. }
  340. /**
  341. * Call the specific plugin decode() method
  342. */
  343. int
  344. tcpedit_dlt_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  345. {
  346. return ctx->decoder->plugin_decode(ctx, packet, pktlen);
  347. }
  348. /**
  349. * Call the specific plugin encode() method
  350. */
  351. int
  352. tcpedit_dlt_encode(tcpeditdlt_t* ctx, u_char *packet, int pktlen, tcpr_dir_t direction)
  353. {
  354. return ctx->encoder->plugin_encode(ctx, packet, pktlen, direction);
  355. }
  356. /**
  357. * what is the source (decoder) DLT type?
  358. */
  359. int
  360. tcpedit_dlt_src(tcpeditdlt_t *ctx)
  361. {
  362. assert(ctx);
  363. return ctx->decoder->dlt;
  364. }
  365. /**
  366. * What is the destination (encoder) DLT type
  367. */
  368. int
  369. tcpedit_dlt_dst(tcpeditdlt_t *ctx)
  370. {
  371. assert(ctx);
  372. return ctx->encoder->dlt;
  373. }
  374. /**
  375. * cleanup after ourselves: destroys our context and all plugin data
  376. */
  377. void
  378. tcpedit_dlt_cleanup(tcpeditdlt_t *ctx)
  379. {
  380. tcpeditdlt_plugin_t *plugin;
  381. assert(ctx);
  382. plugin = ctx->plugins;
  383. while (plugin != NULL) {
  384. plugin->plugin_cleanup(ctx);
  385. plugin = plugin->next;
  386. }
  387. plugin = ctx->plugins;
  388. while (plugin != NULL) {
  389. tcpeditdlt_plugin_t *plugin_next = plugin->next;
  390. safe_free(plugin);
  391. plugin = plugin_next;
  392. }
  393. if (ctx->decoded_extra != NULL) {
  394. safe_free(ctx->decoded_extra);
  395. ctx->decoded_extra = NULL;
  396. ctx->decoded_extra_size = 0;
  397. }
  398. safe_free(ctx);
  399. }