dlt_plugins.c 12 KB

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