linuxsll.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* $Id: linuxsll.c 1893 2007-08-10 04:24: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 "linuxsll.h"
  36. #include "tcpedit.h"
  37. #include "common.h"
  38. #include "tcpr.h"
  39. static char dlt_name[] = "linuxsll";
  40. static char _U_ 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. plugin->plugin_get_mac = dlt_linuxsll_get_mac;
  82. /* add it to the available plugin list */
  83. return tcpedit_dlt_addplugin(ctx, plugin);
  84. }
  85. /*
  86. * Initializer function. This function is called only once, if and only iif
  87. * this plugin will be utilized. Remember, if you need to keep track of any state,
  88. * store it in your plugin->config, not a global!
  89. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  90. */
  91. int
  92. dlt_linuxsll_init(tcpeditdlt_t *ctx)
  93. {
  94. tcpeditdlt_plugin_t *plugin;
  95. linuxsll_config_t *config;
  96. assert(ctx);
  97. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  98. tcpedit_seterr(ctx->tcpedit, "Unable to initalize unregistered plugin %s", dlt_name);
  99. return TCPEDIT_ERROR;
  100. }
  101. /* allocate memory for our deocde extra data */
  102. if (sizeof(linuxsll_extra_t) > 0)
  103. ctx->decoded_extra = safe_malloc(sizeof(linuxsll_extra_t));
  104. /* allocate memory for our config data */
  105. if (sizeof(linuxsll_config_t) > 0)
  106. plugin->config = safe_malloc(sizeof(linuxsll_config_t));
  107. config = (linuxsll_config_t *)plugin->config;
  108. return TCPEDIT_OK; /* success */
  109. }
  110. /*
  111. * Since this is used in a library, we should manually clean up after ourselves
  112. * Unless you allocated some memory in dlt_linuxsll_init(), this is just an stub.
  113. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  114. */
  115. int
  116. dlt_linuxsll_cleanup(tcpeditdlt_t *ctx)
  117. {
  118. tcpeditdlt_plugin_t *plugin;
  119. assert(ctx);
  120. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  121. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  122. return TCPEDIT_ERROR;
  123. }
  124. /* FIXME: make this function do something if necessary */
  125. if (ctx->decoded_extra != NULL) {
  126. safe_free(ctx->decoded_extra);
  127. ctx->decoded_extra = NULL;
  128. }
  129. if (plugin->config != NULL) {
  130. safe_free(plugin->config);
  131. plugin->config = NULL;
  132. }
  133. return TCPEDIT_OK; /* success */
  134. }
  135. /*
  136. * This is where you should define all your AutoGen AutoOpts option parsing.
  137. * Any user specified option should have it's bit turned on in the 'provides'
  138. * bit mask.
  139. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  140. */
  141. int
  142. dlt_linuxsll_parse_opts(tcpeditdlt_t *ctx)
  143. {
  144. assert(ctx);
  145. /* nothing to parse */
  146. return TCPEDIT_OK; /* success */
  147. }
  148. /*
  149. * Function to decode the layer 2 header in the packet.
  150. * You need to fill out:
  151. * - ctx->l2len
  152. * - ctx->srcaddr
  153. * - ctx->dstaddr
  154. * - ctx->proto
  155. * - ctx->decoded_extra
  156. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  157. */
  158. int
  159. dlt_linuxsll_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  160. {
  161. linux_sll_header_t *linux_sll;
  162. assert(ctx);
  163. assert(packet);
  164. assert(pktlen > (int)sizeof(linux_sll_header_t));
  165. linux_sll = (linux_sll_header_t *)packet;
  166. ctx->proto = linux_sll->proto;
  167. ctx->l2len = sizeof(linux_sll_header_t);
  168. if (ntohs(linux_sll->type) == ARPHRD_ETHER) { /* ethernet */
  169. memcpy(&(ctx->srcaddr), linux_sll->address, ETHER_ADDR_LEN);
  170. } else {
  171. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_LINUX_SLL pcap's must contain only ethernet packets");
  172. return TCPEDIT_ERROR;
  173. }
  174. return TCPEDIT_OK; /* success */
  175. }
  176. /*
  177. * Function to encode the layer 2 header back into the packet.
  178. * Returns: total packet len or TCPEDIT_ERROR
  179. */
  180. int
  181. dlt_linuxsll_encode(tcpeditdlt_t *ctx, u_char **packet_ex, int pktlen, _U_ tcpr_dir_t dir)
  182. {
  183. u_char *packet;
  184. assert(ctx);
  185. assert(packet_ex);
  186. assert(pktlen > 0);
  187. packet = *packet_ex;
  188. assert(packet);
  189. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_LINUX_SLL plugin does not support packet encoding");
  190. return TCPEDIT_ERROR;
  191. }
  192. /*
  193. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  194. */
  195. int
  196. dlt_linuxsll_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  197. {
  198. linux_sll_header_t *linux_sll;
  199. assert(ctx);
  200. assert(packet);
  201. assert(pktlen >= (int)sizeof(linux_sll_header_t));
  202. linux_sll = (linux_sll_header_t *)packet;
  203. return ntohs(linux_sll->proto);
  204. }
  205. /*
  206. * Function returns a pointer to the layer 3 protocol header or NULL on error
  207. */
  208. u_char *
  209. dlt_linuxsll_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  210. {
  211. int l2len;
  212. assert(ctx);
  213. assert(packet);
  214. l2len = dlt_linuxsll_l2len(ctx, packet, pktlen);
  215. assert(pktlen >= l2len);
  216. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  217. }
  218. /*
  219. * function merges the packet (containing L2 and old L3) with the l3data buffer
  220. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  221. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  222. * like SPARC
  223. */
  224. u_char *
  225. dlt_linuxsll_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  226. {
  227. int l2len;
  228. assert(ctx);
  229. assert(packet);
  230. assert(l3data);
  231. l2len = dlt_linuxsll_l2len(ctx, packet, pktlen);
  232. assert(pktlen >= l2len);
  233. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  234. }
  235. /*
  236. * return the length of the L2 header of the current packet
  237. */
  238. int
  239. dlt_linuxsll_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  240. {
  241. assert(ctx);
  242. assert(packet);
  243. assert(pktlen);
  244. return sizeof(linux_sll_header_t);
  245. }
  246. /*
  247. * return a static pointer to the source/destination MAC address
  248. * return NULL on error/address doesn't exist
  249. */
  250. u_char *
  251. dlt_linuxsll_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  252. {
  253. assert(ctx);
  254. assert(packet);
  255. assert(pktlen);
  256. /* FIXME: return a ptr to the source or dest mac address. */
  257. switch(mac) {
  258. case SRC_MAC:
  259. memcpy(ctx->srcmac, &packet[6], 8); /* linuxssl defines the src mac field to be 8 bytes, not 6 */
  260. return(ctx->srcmac);
  261. break;
  262. case DST_MAC:
  263. return(NULL);
  264. break;
  265. default:
  266. errx(1, "Invalid tcpeditdlt_mac_type_t: %d", mac);
  267. }
  268. return(NULL);
  269. }
  270. tcpeditdlt_l2addr_type_t
  271. dlt_linuxsll_l2addr_type(void)
  272. {
  273. /* we only support ethernet packets */
  274. return ETHERNET;
  275. }