dlt_plugins.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* $Id: dlt_plugins.c 1802 2007-04-14 22:30:11Z aturner $ */
  2. /*
  3. * Copyright (c) 2006-2007 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <stdlib.h>
  32. #include "dlt_plugins-int.h"
  33. #include "dlt_utils.h"
  34. #include "common.h"
  35. /*
  36. * Include plugin header files here...
  37. */
  38. #include "dlt_en10mb/en10mb.h"
  39. #include "dlt_user/user.h"
  40. #include "dlt_hdlc/hdlc.h"
  41. #include "dlt_raw/raw.h"
  42. #include "dlt_null/null.h"
  43. #include "dlt_loop/loop.h"
  44. #include "dlt_linuxsll/linuxsll.h"
  45. /*******************************************************************
  46. * Everyone writing a DLT plugin, must add their registration function
  47. * here.
  48. *******************************************************************/
  49. int
  50. tcpedit_dlt_register(tcpeditdlt_t *ctx)
  51. {
  52. int retcode = 0;
  53. assert(ctx);
  54. retcode += dlt_en10mb_register(ctx);
  55. retcode += dlt_hdlc_register(ctx);
  56. retcode += dlt_user_register(ctx);
  57. retcode += dlt_raw_register(ctx);
  58. retcode += dlt_null_register(ctx);
  59. retcode += dlt_loop_register(ctx);
  60. retcode += dlt_linuxsll_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. /*
  69. * mapping for bit_mask to bit_info. If you're making changes here
  70. * then you almost certainly need to modify tcpeditdlt_t in dlt_plugins-int.h
  71. */
  72. const u_int32_t tcpeditdlt_bit_map[] = {
  73. PLUGIN_MASK_PROTO,
  74. PLUGIN_MASK_SRCADDR,
  75. PLUGIN_MASK_DSTADDR
  76. };
  77. /* Meanings of the above map */
  78. const char *tcpeditdlt_bit_info[] = {
  79. "Missing required Layer 3 protocol.",
  80. "Missing required Layer 2 source address.",
  81. "Missing required Layer 2 destination address."
  82. };
  83. /*********************************************************************
  84. * Internal functions
  85. ********************************************************************/
  86. /*********************************************************************
  87. * Public functions
  88. ********************************************************************/
  89. /*
  90. * initialize our plugin library. Pass the DLT of the source pcap handle.
  91. * Actions:
  92. * - Create new tcpeditdlt_t context
  93. * - Link tcpedit to new context
  94. * - Register plugins
  95. * - Select decoder plugin using srcdlt
  96. * - Select encoder plugin using destination name
  97. * - Initialize decoder/encoder plugins
  98. * - Parse options for encoder plugin
  99. * - Validate provides/reqiures + user options
  100. */
  101. tcpeditdlt_t *
  102. tcpedit_dlt_init(tcpedit_t *tcpedit, const int srcdlt)
  103. {
  104. tcpeditdlt_t *ctx;
  105. int rcode;
  106. const char *dst_dlt_name = NULL;
  107. assert(tcpedit);
  108. assert(srcdlt >= 0);
  109. ctx = (tcpeditdlt_t *)safe_malloc(sizeof(tcpeditdlt_t));
  110. /* do we need a side buffer for L3 data? */
  111. #ifdef FORCE_ALIGN
  112. ctx->l3buff = (u_char *)safe_malloc(MAXPACKET);
  113. #endif
  114. /* copy our tcpedit context */
  115. ctx->tcpedit = tcpedit;
  116. /* register all our plugins */
  117. if (tcpedit_dlt_register(ctx) != TCPEDIT_OK) {
  118. goto INIT_ERROR;
  119. }
  120. /* Choose decode plugin */
  121. if ((ctx->decoder = tcpedit_dlt_getplugin(ctx, srcdlt)) == NULL) {
  122. tcpedit_seterr(tcpedit, "No DLT plugin available for source DLT: 0x%x", srcdlt);
  123. goto INIT_ERROR;
  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. /* initalize decoder plugin */
  130. rcode = ctx->decoder->plugin_init(ctx);
  131. if (tcpedit_checkerror(ctx->tcpedit, rcode, NULL) != TCPEDIT_OK) {
  132. goto INIT_ERROR;
  133. }
  134. /* Select the encoder plugin */
  135. dst_dlt_name = OPT_ARG(DLT) ? OPT_ARG(DLT) : ctx->decoder->name;
  136. if ((ctx->encoder = tcpedit_dlt_getplugin_byname(ctx, dst_dlt_name)) == NULL) {
  137. tcpedit_seterr(tcpedit, "No output DLT plugin available for: %s", dst_dlt_name);
  138. goto INIT_ERROR;
  139. }
  140. /* Figure out if we're skipping braodcast & multicast */
  141. if (HAVE_OPT(SKIPL2BROADCAST))
  142. ctx->skip_broadcast = 1;
  143. /* init encoder plugin if it's not the decoder plugin */
  144. if (ctx->encoder->dlt != ctx->decoder->dlt) {
  145. rcode = ctx->encoder->plugin_init(ctx);
  146. if (tcpedit_checkerror(ctx->tcpedit, rcode, NULL) != TCPEDIT_OK) {
  147. goto INIT_ERROR;
  148. }
  149. }
  150. /* parse the DLT specific options */
  151. rcode = tcpedit_dlt_parse_opts(ctx);
  152. if (tcpedit_checkerror(ctx->tcpedit, rcode, "parsing options") != TCPEDIT_OK) {
  153. goto INIT_ERROR;
  154. }
  155. /* validate that the SRC/DST DLT + options give us enough info */
  156. rcode = tcpedit_dlt_validate(ctx);
  157. if (tcpedit_checkerror(ctx->tcpedit, rcode, "validating options") != TCPEDIT_OK) {
  158. goto INIT_ERROR;
  159. }
  160. /* we're OK */
  161. return ctx;
  162. INIT_ERROR:
  163. tcpedit_dlt_cleanup(ctx);
  164. return NULL;
  165. }
  166. /*
  167. * This is the recommended method to edit a packet. Returns (new) total packet length
  168. */
  169. int
  170. tcpedit_dlt_process(tcpeditdlt_t *ctx, u_char *packet, int pktlen, tcpr_dir_t direction)
  171. {
  172. int rcode;
  173. assert(ctx);
  174. assert(packet);
  175. assert(pktlen);
  176. assert(direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C || direction == TCPR_DIR_NOSEND);
  177. /* nothing to do here */
  178. if (direction == TCPR_DIR_NOSEND)
  179. return pktlen;
  180. /* decode packet */
  181. if ((rcode = tcpedit_dlt_decode(ctx, packet, pktlen)) == TCPEDIT_ERROR) {
  182. return TCPEDIT_ERROR;
  183. } else if (rcode == TCPEDIT_WARN) {
  184. fprintf(stderr, "Warning decoding packet: %s", tcpedit_getwarn(ctx->tcpedit));
  185. }
  186. /* encode packet */
  187. if ((rcode = tcpedit_dlt_encode(ctx, &packet, pktlen, direction)) == TCPEDIT_ERROR) {
  188. return TCPEDIT_ERROR;
  189. } else if (rcode == TCPEDIT_WARN) {
  190. fprintf(stderr, "Warning encoding packet: %s", tcpedit_getwarn(ctx->tcpedit));
  191. }
  192. return rcode;
  193. }
  194. /*
  195. * What is the output DLT type???
  196. */
  197. int
  198. tcpedit_dlt_output_dlt(tcpeditdlt_t *ctx)
  199. {
  200. u_int16_t dlt;
  201. assert(ctx);
  202. /*
  203. * usually we just return the DLT value of the decoder, but for DLT_USER0
  204. * we return a user-specified value via --user-dlt
  205. */
  206. if (ctx->encoder->dlt == DLT_USER0) {
  207. dlt = dlt_user_get_output_dlt(ctx);
  208. } else {
  209. dlt = ctx->encoder->dlt;
  210. }
  211. return dlt;
  212. }
  213. int
  214. tcpedit_dlt_l2len(tcpeditdlt_t *ctx, int dlt, const u_char *packet, const int pktlen)
  215. {
  216. tcpeditdlt_plugin_t *plugin;
  217. assert(ctx);
  218. assert(dlt >= 0);
  219. assert(packet);
  220. assert(pktlen);
  221. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  222. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  223. return -1;
  224. }
  225. return plugin->plugin_l2len(ctx, packet, pktlen);
  226. }
  227. /*
  228. * Get the L3 type. Returns -1 on error. Get error via tcpedit->geterr()
  229. */
  230. int
  231. tcpedit_dlt_proto(tcpeditdlt_t *ctx, int dlt, const u_char *packet, const int pktlen)
  232. {
  233. tcpeditdlt_plugin_t *plugin;
  234. assert(ctx);
  235. assert(dlt >= 0);
  236. assert(packet);
  237. assert(pktlen);
  238. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  239. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  240. return -1;
  241. }
  242. return plugin->plugin_proto(ctx, packet, pktlen);
  243. }
  244. /*
  245. * Get the L3 data. Returns NULL on error. Get error via tcpedit->geterr()
  246. */
  247. u_char *
  248. tcpedit_dlt_l3data(tcpeditdlt_t *ctx, int dlt, u_char *packet, const int pktlen)
  249. {
  250. tcpeditdlt_plugin_t *plugin;
  251. assert(ctx);
  252. assert(dlt >= 0);
  253. assert(packet);
  254. assert(pktlen);
  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 NULL;
  258. }
  259. return plugin->plugin_get_layer3(ctx, packet, pktlen);
  260. }
  261. /*
  262. * Since some L2 headers aren't strictly aligned, we need to "merge" the packet w/ L2 data
  263. * and the L3 buffer. This is basically a NO-OP for things like vlan tagged ethernet (16 byte) header
  264. * or Cisco HDLC (4 byte header) but is critical for std ethernet (12 byte header)
  265. */
  266. u_char *
  267. tcpedit_dlt_merge_l3data(tcpeditdlt_t *ctx, int dlt, u_char *packet, const int pktlen, u_char *l3data)
  268. {
  269. tcpeditdlt_plugin_t *plugin;
  270. assert(ctx);
  271. assert(dlt >= 0);
  272. assert(pktlen >= 0);
  273. assert(packet);
  274. if (l3data == NULL)
  275. return 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 NULL;
  279. }
  280. return plugin->plugin_merge_layer3(ctx, packet, pktlen, l3data);
  281. }
  282. /*
  283. * Call the specific plugin decode() method
  284. */
  285. int
  286. tcpedit_dlt_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  287. {
  288. return ctx->decoder->plugin_decode(ctx, packet, pktlen);
  289. }
  290. /*
  291. * Call the specific plugin encode() method
  292. */
  293. int
  294. tcpedit_dlt_encode(tcpeditdlt_t* ctx, u_char **packet, int pktlen, tcpr_dir_t direction)
  295. {
  296. return ctx->encoder->plugin_encode(ctx, packet, pktlen, direction);
  297. }
  298. /*
  299. * what is the source (decoder) DLT type
  300. */
  301. int
  302. tcpedit_dlt_src(tcpeditdlt_t *ctx)
  303. {
  304. assert(ctx);
  305. return ctx->decoder->dlt;
  306. }
  307. /*
  308. * What is the destination (encoder) DLT type
  309. */
  310. int
  311. tcpedit_dlt_dst(tcpeditdlt_t *ctx)
  312. {
  313. assert(ctx);
  314. return ctx->encoder->dlt;
  315. }
  316. /*
  317. * cleanup after ourselves: destroys our context and all plugin data
  318. */
  319. void
  320. tcpedit_dlt_cleanup(tcpeditdlt_t *ctx)
  321. {
  322. assert(ctx);
  323. if (ctx->encoder != NULL)
  324. ctx->encoder->plugin_cleanup(ctx);
  325. if (ctx->decoder != NULL)
  326. ctx->decoder->plugin_cleanup(ctx);
  327. #ifdef FORCE_ALIGN
  328. free(ctx->l3buff);
  329. #endif
  330. if (ctx->decoded_extra != NULL)
  331. free(ctx->decoded_extra);
  332. free(ctx);
  333. }