1
0

dlt_plugins.c 13 KB

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