dlt_plugins.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* $Id: dlt_plugins.c 1963 2008-01-23 18:21:40Z 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 "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. */
  177. int
  178. tcpedit_dlt_process(tcpeditdlt_t *ctx, u_char *packet, int pktlen, tcpr_dir_t direction)
  179. {
  180. int rcode;
  181. assert(ctx);
  182. assert(packet);
  183. assert(pktlen);
  184. assert(direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C || direction == TCPR_DIR_NOSEND);
  185. /* nothing to do here */
  186. if (direction == TCPR_DIR_NOSEND)
  187. return pktlen;
  188. /* decode packet */
  189. if ((rcode = tcpedit_dlt_decode(ctx, packet, pktlen)) == TCPEDIT_ERROR) {
  190. return TCPEDIT_ERROR;
  191. } else if (rcode == TCPEDIT_WARN) {
  192. warnx("Warning decoding packet: %s", tcpedit_getwarn(ctx->tcpedit));
  193. } else if (rcode == TCPEDIT_SOFT_ERROR) {
  194. return rcode; /* can't edit the packet */
  195. }
  196. /* encode packet */
  197. if ((rcode = tcpedit_dlt_encode(ctx, &packet, pktlen, direction)) == TCPEDIT_ERROR) {
  198. return TCPEDIT_ERROR;
  199. } else if (rcode == TCPEDIT_WARN) {
  200. warnx("Warning encoding packet: %s", tcpedit_getwarn(ctx->tcpedit));
  201. }
  202. return rcode;
  203. }
  204. /**
  205. * What is the output DLT type???
  206. */
  207. int
  208. tcpedit_dlt_output_dlt(tcpeditdlt_t *ctx)
  209. {
  210. u_int16_t dlt;
  211. assert(ctx);
  212. /*
  213. * usually we just return the DLT value of the decoder, but for DLT_USER0
  214. * we return a user-specified value via --user-dlt
  215. */
  216. if (ctx->encoder->dlt == DLT_USER0) {
  217. dlt = dlt_user_get_output_dlt(ctx);
  218. } else {
  219. dlt = ctx->encoder->dlt;
  220. }
  221. return dlt;
  222. }
  223. /**
  224. * Get the layer 2 length of the packet using the DLT plugin currently in
  225. * place
  226. */
  227. int
  228. tcpedit_dlt_l2len(tcpeditdlt_t *ctx, int dlt, const u_char *packet, const int pktlen)
  229. {
  230. tcpeditdlt_plugin_t *plugin;
  231. assert(ctx);
  232. assert(dlt >= 0);
  233. assert(packet);
  234. assert(pktlen);
  235. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  236. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  237. return -1;
  238. }
  239. return plugin->plugin_l2len(ctx, packet, pktlen);
  240. }
  241. /**
  242. * Get the L3 type. Returns -1 on error. Get error via tcpedit->geterr()
  243. */
  244. int
  245. tcpedit_dlt_proto(tcpeditdlt_t *ctx, int dlt, const u_char *packet, const int pktlen)
  246. {
  247. tcpeditdlt_plugin_t *plugin;
  248. assert(ctx);
  249. assert(dlt >= 0);
  250. assert(packet);
  251. assert(pktlen);
  252. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  253. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  254. return -1;
  255. }
  256. return plugin->plugin_proto(ctx, packet, pktlen);
  257. }
  258. /**
  259. * Get the L3 data. Returns NULL on error. Get error via tcpedit->geterr()
  260. */
  261. u_char *
  262. tcpedit_dlt_l3data(tcpeditdlt_t *ctx, int dlt, u_char *packet, const int pktlen)
  263. {
  264. tcpeditdlt_plugin_t *plugin;
  265. assert(ctx);
  266. assert(dlt >= 0);
  267. assert(packet);
  268. assert(pktlen);
  269. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  270. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  271. return NULL;
  272. }
  273. return plugin->plugin_get_layer3(ctx, packet, pktlen);
  274. }
  275. /**
  276. * \brief Merge the Layer 3 data back onto the mainbuffer so it's immediately
  277. * after the layer 2 header
  278. *
  279. * Since some L2 headers aren't strictly aligned, we need to "merge" the packet w/ L2 data
  280. * and the L3 buffer. This is basically a NO-OP for things like vlan tagged ethernet (16 byte) header
  281. * or Cisco HDLC (4 byte header) but is critical for std ethernet (12 byte header)
  282. */
  283. u_char *
  284. tcpedit_dlt_merge_l3data(tcpeditdlt_t *ctx, int dlt, u_char *packet, const int pktlen, u_char *l3data)
  285. {
  286. tcpeditdlt_plugin_t *plugin;
  287. assert(ctx);
  288. assert(dlt >= 0);
  289. assert(pktlen >= 0);
  290. assert(packet);
  291. if (l3data == NULL)
  292. return packet;
  293. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
  294. tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
  295. return NULL;
  296. }
  297. return plugin->plugin_merge_layer3(ctx, packet, pktlen, l3data);
  298. }
  299. /**
  300. * Call the specific plugin decode() method
  301. */
  302. int
  303. tcpedit_dlt_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  304. {
  305. return ctx->decoder->plugin_decode(ctx, packet, pktlen);
  306. }
  307. /**
  308. * Call the specific plugin encode() method
  309. */
  310. int
  311. tcpedit_dlt_encode(tcpeditdlt_t* ctx, u_char **packet, int pktlen, tcpr_dir_t direction)
  312. {
  313. return ctx->encoder->plugin_encode(ctx, packet, pktlen, direction);
  314. }
  315. /**
  316. * what is the source (decoder) DLT type?
  317. */
  318. int
  319. tcpedit_dlt_src(tcpeditdlt_t *ctx)
  320. {
  321. assert(ctx);
  322. return ctx->decoder->dlt;
  323. }
  324. /**
  325. * What is the destination (encoder) DLT type
  326. */
  327. int
  328. tcpedit_dlt_dst(tcpeditdlt_t *ctx)
  329. {
  330. assert(ctx);
  331. return ctx->encoder->dlt;
  332. }
  333. /**
  334. * cleanup after ourselves: destroys our context and all plugin data
  335. */
  336. void
  337. tcpedit_dlt_cleanup(tcpeditdlt_t *ctx)
  338. {
  339. assert(ctx);
  340. if (ctx->encoder != NULL)
  341. ctx->encoder->plugin_cleanup(ctx);
  342. if (ctx->decoder != NULL)
  343. ctx->decoder->plugin_cleanup(ctx);
  344. #ifdef FORCE_ALIGN
  345. safe_free(ctx->l3buff);
  346. #endif
  347. if (ctx->decoded_extra != NULL)
  348. safe_free(ctx->decoded_extra);
  349. safe_free(ctx);
  350. }