raw.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* $Id: raw.c 1921 2007-10-25 18:18: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 <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 _U_ 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. plugin->plugin_get_mac = dlt_raw_get_mac;
  85. /* add it to the available plugin list */
  86. return tcpedit_dlt_addplugin(ctx, plugin);
  87. }
  88. /*
  89. * Initializer function. This function is called only once, if and only iif
  90. * this plugin will be utilized. Remember, if you need to keep track of any state,
  91. * store it in your plugin->config, not a global!
  92. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  93. */
  94. int
  95. dlt_raw_init(tcpeditdlt_t *ctx)
  96. {
  97. tcpeditdlt_plugin_t *plugin;
  98. raw_config_t *config;
  99. assert(ctx);
  100. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  101. tcpedit_seterr(ctx->tcpedit, "Unable to initalize unregistered plugin %s", dlt_name);
  102. return TCPEDIT_ERROR;
  103. }
  104. /* allocate memory for our deocde extra data */
  105. if (sizeof(raw_extra_t) > 0)
  106. ctx->decoded_extra = safe_malloc(sizeof(raw_extra_t));
  107. /* allocate memory for our config data */
  108. if (sizeof(raw_config_t) > 0)
  109. plugin->config = safe_malloc(sizeof(raw_config_t));
  110. config = (raw_config_t *)plugin->config;
  111. return TCPEDIT_OK; /* success */
  112. }
  113. /*
  114. * Since this is used in a library, we should manually clean up after ourselves
  115. * Unless you allocated some memory in dlt_raw_init(), this is just an stub.
  116. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  117. */
  118. int
  119. dlt_raw_cleanup(tcpeditdlt_t *ctx)
  120. {
  121. tcpeditdlt_plugin_t *plugin;
  122. assert(ctx);
  123. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  124. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  125. return TCPEDIT_ERROR;
  126. }
  127. /* FIXME: make this function do something if necessary */
  128. if (ctx->decoded_extra != NULL) {
  129. safe_free(ctx->decoded_extra);
  130. ctx->decoded_extra = NULL;
  131. }
  132. if (plugin->config != NULL) {
  133. safe_free(plugin->config);
  134. plugin->config = NULL;
  135. }
  136. return TCPEDIT_OK; /* success */
  137. }
  138. /*
  139. * This is where you should define all your AutoGen AutoOpts option parsing.
  140. * Any user specified option should have it's bit turned on in the 'provides'
  141. * bit mask.
  142. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  143. */
  144. int
  145. dlt_raw_parse_opts(tcpeditdlt_t *ctx)
  146. {
  147. assert(ctx);
  148. /* no op */
  149. return TCPEDIT_OK; /* success */
  150. }
  151. /*
  152. * Function to decode the layer 2 header in the packet.
  153. * You need to fill out:
  154. * - ctx->l2len
  155. * - ctx->srcaddr
  156. * - ctx->dstaddr
  157. * - ctx->proto
  158. * - ctx->decoded_extra
  159. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  160. */
  161. int
  162. dlt_raw_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  163. {
  164. int proto;
  165. assert(ctx);
  166. assert(packet);
  167. assert(pktlen > 0);
  168. if ((proto = dlt_raw_proto(ctx, packet, pktlen)) == TCPEDIT_ERROR)
  169. return TCPEDIT_ERROR;
  170. ctx->proto = (u_int16_t)proto;
  171. ctx->l2len = 0;
  172. return TCPEDIT_OK; /* success */
  173. }
  174. /*
  175. * Function to encode the layer 2 header back into the packet.
  176. * Returns: total packet len or TCPEDIT_ERROR
  177. */
  178. int
  179. dlt_raw_encode(tcpeditdlt_t *ctx, u_char **packet_ex, int pktlen, _U_ tcpr_dir_t dir)
  180. {
  181. u_char *packet;
  182. assert(ctx);
  183. assert(packet_ex);
  184. assert(pktlen > 0);
  185. packet = *packet_ex;
  186. assert(packet);
  187. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_RAW plugin does not support packet encoding");
  188. return TCPEDIT_ERROR;
  189. }
  190. /*
  191. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  192. */
  193. int
  194. dlt_raw_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  195. {
  196. struct tcpr_ipv4_hdr *iphdr;
  197. assert(ctx);
  198. assert(packet);
  199. assert(pktlen > 0);
  200. int protocol = 0;
  201. iphdr = (struct tcpr_ipv4_hdr *)packet;
  202. if (iphdr->ip_v == 0x04) {
  203. protocol = ETHERTYPE_IP;
  204. } else if (iphdr->ip_v == 0x06) {
  205. protocol = ETHERTYPE_IP6;
  206. } else {
  207. tcpedit_seterr(ctx->tcpedit, "%s", "Unsupported DLT_RAW packet: doesn't look like IPv4 or IPv6");
  208. return TCPEDIT_ERROR;
  209. }
  210. return htons(protocol);
  211. }
  212. /*
  213. * Function returns a pointer to the layer 3 protocol header or NULL on error
  214. */
  215. u_char *
  216. dlt_raw_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  217. {
  218. assert(ctx);
  219. assert(packet);
  220. assert(pktlen);
  221. /* raw has a zero byte header, so this is basically a non-op */
  222. return packet;
  223. }
  224. /*
  225. * function merges the packet (containing L2 and old L3) with the l3data buffer
  226. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  227. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  228. * like SPARC
  229. */
  230. u_char *
  231. dlt_raw_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  232. {
  233. assert(ctx);
  234. assert(packet);
  235. assert(l3data);
  236. assert(pktlen);
  237. /* raw has a zero byte header, so this is basically a non-op */
  238. return packet;
  239. }
  240. /*
  241. * return the length of the L2 header of the current packet
  242. */
  243. int
  244. dlt_raw_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  245. {
  246. assert(ctx);
  247. assert(packet);
  248. assert(pktlen);
  249. return 0;
  250. }
  251. /*
  252. * return a static pointer to the source/destination MAC address
  253. * return NULL on error/address doesn't exist
  254. */
  255. u_char *
  256. dlt_raw_get_mac(tcpeditdlt_t *ctx, _U_ tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  257. {
  258. assert(ctx);
  259. assert(packet);
  260. assert(pktlen);
  261. return(NULL);
  262. }
  263. tcpeditdlt_l2addr_type_t
  264. dlt_raw_l2addr_type(void)
  265. {
  266. return NONE;
  267. }