dlt_plugins.c 12 KB

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