linuxsll.c 8.9 KB

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