raw.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* $Id:$ */
  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 <string.h>
  33. #include "dlt_plugins-int.h"
  34. #include "dlt_utils.h"
  35. #include "raw.h"
  36. #include "tcpedit.h"
  37. #include "common.h"
  38. #include "tcpr.h"
  39. /* FIXME: edit these variables to taste */
  40. static char dlt_name[] = "raw";
  41. static char __attribute__((unused)) dlt_prefix[] = "raw";
  42. static u_int16_t dlt_value = DLT_RAW;
  43. /*
  44. * DLT_RAW is basically a zero length L2 header for IPv4 & IPv6 packets
  45. */
  46. /*
  47. * Function to register ourselves. This function is always called, regardless
  48. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  49. * or anything like that (use the dlt_raw_init() function below for that).
  50. * Tasks:
  51. * - Create a new plugin struct
  52. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  53. * actually in the header.
  54. * - Add the plugin to the context's plugin chain
  55. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  56. */
  57. int
  58. dlt_raw_register(tcpeditdlt_t *ctx)
  59. {
  60. tcpeditdlt_plugin_t *plugin;
  61. assert(ctx);
  62. /* create a new plugin structure */
  63. plugin = tcpedit_dlt_newplugin();
  64. /* set what we provide & require */
  65. plugin->provides += PLUGIN_MASK_PROTO;
  66. /* what is our DLT value? */
  67. plugin->dlt = dlt_value;
  68. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  69. plugin->name = safe_strdup(dlt_prefix);
  70. /*
  71. * Point to our functions, note, you need a function for EVERY method.
  72. * Even if it is only an empty stub returning success.
  73. */
  74. plugin->plugin_init = dlt_raw_init;
  75. plugin->plugin_cleanup = dlt_raw_cleanup;
  76. plugin->plugin_parse_opts = dlt_raw_parse_opts;
  77. plugin->plugin_decode = dlt_raw_decode;
  78. plugin->plugin_encode = dlt_raw_encode;
  79. plugin->plugin_proto = dlt_raw_proto;
  80. plugin->plugin_l2addr_type = dlt_raw_l2addr_type;
  81. plugin->plugin_l2len = dlt_raw_l2len;
  82. plugin->plugin_get_layer3 = dlt_raw_get_layer3;
  83. plugin->plugin_merge_layer3 = dlt_raw_merge_layer3;
  84. /* add it to the available plugin list */
  85. return tcpedit_dlt_addplugin(ctx, plugin);
  86. }
  87. /*
  88. * Initializer function. This function is called only once, if and only iif
  89. * this plugin will be utilized. Remember, if you need to keep track of any state,
  90. * store it in your plugin->config, not a global!
  91. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  92. */
  93. int
  94. dlt_raw_init(tcpeditdlt_t *ctx)
  95. {
  96. tcpeditdlt_plugin_t *plugin;
  97. raw_config_t *config;
  98. assert(ctx);
  99. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  100. tcpedit_seterr(ctx->tcpedit, "Unable to initalize unregistered plugin %s", dlt_name);
  101. return TCPEDIT_ERROR;
  102. }
  103. /* allocate memory for our deocde extra data */
  104. if (sizeof(raw_extra_t) > 0)
  105. ctx->decoded_extra = safe_malloc(sizeof(raw_extra_t));
  106. /* allocate memory for our config data */
  107. if (sizeof(raw_config_t) > 0)
  108. plugin->config = safe_malloc(sizeof(raw_config_t));
  109. config = (raw_config_t *)plugin->config;
  110. return TCPEDIT_OK; /* success */
  111. }
  112. /*
  113. * Since this is used in a library, we should manually clean up after ourselves
  114. * Unless you allocated some memory in dlt_raw_init(), this is just an stub.
  115. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  116. */
  117. int
  118. dlt_raw_cleanup(tcpeditdlt_t *ctx)
  119. {
  120. tcpeditdlt_plugin_t *plugin;
  121. assert(ctx);
  122. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  123. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  124. return TCPEDIT_ERROR;
  125. }
  126. /* FIXME: make this function do something if necessary */
  127. if (ctx->decoded_extra != NULL) {
  128. free(ctx->decoded_extra);
  129. ctx->decoded_extra = NULL;
  130. }
  131. if (plugin->config != NULL) {
  132. free(plugin->config);
  133. plugin->config = NULL;
  134. }
  135. return TCPEDIT_OK; /* success */
  136. }
  137. /*
  138. * This is where you should define all your AutoGen AutoOpts option parsing.
  139. * Any user specified option should have it's bit turned on in the 'provides'
  140. * bit mask.
  141. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  142. */
  143. int
  144. dlt_raw_parse_opts(tcpeditdlt_t *ctx)
  145. {
  146. assert(ctx);
  147. /* no op */
  148. return TCPEDIT_OK; /* success */
  149. }
  150. /*
  151. * Function to decode the layer 2 header in the packet.
  152. * You need to fill out:
  153. * - ctx->l2len
  154. * - ctx->srcaddr
  155. * - ctx->dstaddr
  156. * - ctx->proto
  157. * - ctx->decoded_extra
  158. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  159. */
  160. int
  161. dlt_raw_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  162. {
  163. int proto;
  164. assert(ctx);
  165. assert(packet);
  166. assert(pktlen > 0);
  167. if ((proto = dlt_raw_proto(ctx, packet, pktlen)) == TCPEDIT_ERROR)
  168. return TCPEDIT_ERROR;
  169. ctx->proto = (u_int16_t)proto;
  170. ctx->l2len = 0;
  171. return TCPEDIT_OK; /* success */
  172. }
  173. /*
  174. * Function to encode the layer 2 header back into the packet.
  175. * Returns: total packet len or TCPEDIT_ERROR
  176. */
  177. int
  178. dlt_raw_encode(tcpeditdlt_t *ctx, u_char **packet_ex, int pktlen, __attribute__((unused))tcpr_dir_t dir)
  179. {
  180. u_char *packet;
  181. assert(ctx);
  182. assert(packet_ex);
  183. assert(pktlen > 0);
  184. packet = *packet_ex;
  185. assert(packet);
  186. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_RAW plugin does not support packet encoding");
  187. return TCPEDIT_ERROR;
  188. }
  189. /*
  190. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  191. */
  192. int
  193. dlt_raw_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  194. {
  195. struct tcpr_ipv4_hdr *iphdr;
  196. assert(ctx);
  197. assert(packet);
  198. assert(pktlen > 0);
  199. int protocol = 0;
  200. iphdr = (struct tcpr_ipv4_hdr *)packet;
  201. if (iphdr->ip_v == 0x04) {
  202. protocol = ETHERTYPE_IP;
  203. } else if (iphdr->ip_v == 0x06) {
  204. protocol = ETHERTYPE_IP6;
  205. } else {
  206. tcpedit_seterr(ctx->tcpedit, "%s", "Unsupported DLT_RAW packet: doesn't look like IPv4 or IPv6");
  207. return TCPEDIT_ERROR;
  208. }
  209. return protocol;
  210. }
  211. /*
  212. * Function returns a pointer to the layer 3 protocol header or NULL on error
  213. */
  214. u_char *
  215. dlt_raw_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  216. {
  217. assert(ctx);
  218. assert(packet);
  219. assert(pktlen);
  220. /* raw has a zero byte header, so this is basically a non-op */
  221. return packet;
  222. }
  223. /*
  224. * function merges the packet (containing L2 and old L3) with the l3data buffer
  225. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  226. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  227. * like SPARC
  228. */
  229. u_char *
  230. dlt_raw_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  231. {
  232. assert(ctx);
  233. assert(packet);
  234. assert(l3data);
  235. assert(pktlen);
  236. /* raw has a zero byte header, so this is basically a non-op */
  237. return packet;
  238. }
  239. /*
  240. * return the length of the L2 header of the current packet
  241. */
  242. int
  243. dlt_raw_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  244. {
  245. assert(ctx);
  246. assert(packet);
  247. assert(pktlen);
  248. return 0;
  249. }
  250. tcpeditdlt_l2addr_type_t
  251. dlt_raw_l2addr_type(void)
  252. {
  253. return NONE;
  254. }