null.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* $Id: null.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 "null.h"
  36. #include "tcpedit.h"
  37. #include "common.h"
  38. #include "tcpr.h"
  39. #include <sys/socket.h> // PF_* values
  40. static char dlt_name[] = "null";
  41. static char _U_ dlt_prefix[] = "null";
  42. static u_int16_t dlt_value = DLT_NULL;
  43. /*
  44. * From the libpcap man page:
  45. * DLT_NULL aka BSD loopback encapsulation; the link layer header is a 4-byte
  46. * field, in host byte order, containing a PF_ value from
  47. * socket.h for the network-layer protocol of the packet.
  48. *
  49. * Note that ``host byte order'' is the byte order of the
  50. * machine on which the packets are captured, and the PF_ values
  51. * are for the OS of the machine on which the packets are captured;
  52. * if a live capture is being done, ``host byte order''
  53. * is the byte order of the machine capturing the packets, and
  54. * the PF_ values are those of the OS of the machine capturing
  55. * the packets, but if a ``savefile'' is being read, the byte
  56. * order and PF_ values are not necessarily those of the machine
  57. * reading the capture file.
  58. */
  59. /*
  60. * Function to register ourselves. This function is always called, regardless
  61. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  62. * or anything like that (use the dlt_null_init() function below for that).
  63. * Tasks:
  64. * - Create a new plugin struct
  65. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  66. * actually in the header.
  67. * - Add the plugin to the context's plugin chain
  68. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  69. */
  70. int
  71. dlt_null_register(tcpeditdlt_t *ctx)
  72. {
  73. tcpeditdlt_plugin_t *plugin;
  74. assert(ctx);
  75. /* create a new plugin structure */
  76. plugin = tcpedit_dlt_newplugin();
  77. /* set what we provide & require */
  78. plugin->provides += PLUGIN_MASK_PROTO;
  79. plugin->requires += 0;
  80. /* what is our DLT value? */
  81. plugin->dlt = dlt_value;
  82. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  83. plugin->name = safe_strdup(dlt_prefix);
  84. /*
  85. * Point to our functions, note, you need a function for EVERY method.
  86. * Even if it is only an empty stub returning success.
  87. */
  88. plugin->plugin_init = dlt_null_init;
  89. plugin->plugin_cleanup = dlt_null_cleanup;
  90. plugin->plugin_parse_opts = dlt_null_parse_opts;
  91. plugin->plugin_decode = dlt_null_decode;
  92. plugin->plugin_encode = dlt_null_encode;
  93. plugin->plugin_proto = dlt_null_proto;
  94. plugin->plugin_l2addr_type = dlt_null_l2addr_type;
  95. plugin->plugin_l2len = dlt_null_l2len;
  96. plugin->plugin_get_layer3 = dlt_null_get_layer3;
  97. plugin->plugin_merge_layer3 = dlt_null_merge_layer3;
  98. /* add it to the available plugin list */
  99. return tcpedit_dlt_addplugin(ctx, plugin);
  100. }
  101. /*
  102. * Initializer function. This function is called only once, if and only if
  103. * this plugin will be utilized. Remember, if you need to keep track of any state,
  104. * store it in your plugin->config, not a global!
  105. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  106. */
  107. int
  108. dlt_null_init(tcpeditdlt_t *ctx)
  109. {
  110. tcpeditdlt_plugin_t *plugin;
  111. null_config_t *config;
  112. assert(ctx);
  113. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  114. tcpedit_seterr(ctx->tcpedit, "Unable to initalize unregistered plugin %s", dlt_name);
  115. return TCPEDIT_ERROR;
  116. }
  117. /* allocate memory for our deocde extra data */
  118. if (sizeof(null_extra_t) > 0)
  119. ctx->decoded_extra = safe_malloc(sizeof(null_extra_t));
  120. /* allocate memory for our config data */
  121. if (sizeof(null_config_t) > 0)
  122. plugin->config = safe_malloc(sizeof(null_config_t));
  123. config = (null_config_t *)plugin->config;
  124. return TCPEDIT_OK; /* success */
  125. }
  126. /*
  127. * Since this is used in a library, we should manually clean up after ourselves
  128. * Unless you allocated some memory in dlt_null_init(), this is just an stub.
  129. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  130. */
  131. int
  132. dlt_null_cleanup(tcpeditdlt_t *ctx)
  133. {
  134. tcpeditdlt_plugin_t *plugin;
  135. assert(ctx);
  136. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  137. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  138. return TCPEDIT_ERROR;
  139. }
  140. if (ctx->decoded_extra != NULL) {
  141. safe_free(ctx->decoded_extra);
  142. ctx->decoded_extra = NULL;
  143. }
  144. if (plugin->config != NULL) {
  145. safe_free(plugin->config);
  146. plugin->config = NULL;
  147. }
  148. return TCPEDIT_OK; /* success */
  149. }
  150. /*
  151. * This is where you should define all your AutoGen AutoOpts option parsing.
  152. * Any user specified option should have it's bit turned on in the 'provides'
  153. * bit mask.
  154. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  155. */
  156. int
  157. dlt_null_parse_opts(tcpeditdlt_t *ctx)
  158. {
  159. assert(ctx);
  160. /* nothing to parse here, move along */
  161. return TCPEDIT_OK; /* success */
  162. }
  163. /*
  164. * Function to decode the layer 2 header in the packet.
  165. * You need to fill out:
  166. * - ctx->l2len
  167. * - ctx->srcaddr
  168. * - ctx->dstaddr
  169. * - ctx->proto
  170. * - ctx->decoded_extra
  171. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  172. */
  173. int
  174. dlt_null_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  175. {
  176. int proto;
  177. assert(ctx);
  178. assert(packet);
  179. assert(pktlen > 0);
  180. if ((proto = dlt_null_proto(ctx, packet, pktlen)) == TCPEDIT_ERROR)
  181. return TCPEDIT_ERROR;
  182. ctx->proto = (u_int16_t)proto;
  183. ctx->l2len = 4;
  184. return TCPEDIT_OK; /* success */
  185. }
  186. /*
  187. * Function to encode the layer 2 header back into the packet.
  188. * Returns: total packet len or TCPEDIT_ERROR
  189. */
  190. int
  191. dlt_null_encode(tcpeditdlt_t *ctx, u_char **packet_ex, int pktlen, _U_ tcpr_dir_t dir)
  192. {
  193. u_char *packet;
  194. assert(ctx);
  195. assert(packet_ex);
  196. assert(pktlen > 0);
  197. packet = *packet_ex;
  198. assert(packet);
  199. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_NULL and DLT_LOOP plugins do not support packet encoding");
  200. return TCPEDIT_ERROR;
  201. }
  202. /*
  203. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  204. */
  205. int
  206. dlt_null_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  207. {
  208. assert(ctx);
  209. assert(packet);
  210. assert(pktlen > 0);
  211. u_int32_t *af_type;
  212. int protocol = 0;
  213. af_type = (u_int32_t *)packet;
  214. if (*af_type == PF_INET || SWAPLONG(*af_type) == PF_INET) {
  215. protocol = ETHERTYPE_IP;
  216. } else if (*af_type == PF_INET6 || SWAPLONG(*af_type) == PF_INET6) {
  217. protocol = ETHERTYPE_IP6;
  218. } else {
  219. tcpedit_seterr(ctx->tcpedit, "Unsupported DLT_NULL/DLT_LOOP PF_ type: 0x%04x", *af_type);
  220. return TCPEDIT_ERROR;
  221. }
  222. return protocol;
  223. }
  224. /*
  225. * Function returns a pointer to the layer 3 protocol header or NULL on error
  226. */
  227. u_char *
  228. dlt_null_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  229. {
  230. int l2len;
  231. assert(ctx);
  232. assert(packet);
  233. l2len = dlt_null_l2len(ctx, packet, pktlen);
  234. assert(pktlen >= l2len);
  235. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  236. }
  237. /*
  238. * function merges the packet (containing L2 and old L3) with the l3data buffer
  239. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  240. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  241. * like SPARC
  242. */
  243. u_char *
  244. dlt_null_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  245. {
  246. int l2len;
  247. assert(ctx);
  248. assert(packet);
  249. assert(l3data);
  250. l2len = dlt_null_l2len(ctx, packet, pktlen);
  251. assert(pktlen >= l2len);
  252. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  253. }
  254. /*
  255. * return the length of the L2 header of the current packet
  256. */
  257. int
  258. dlt_null_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  259. {
  260. assert(ctx);
  261. assert(packet);
  262. assert(pktlen);
  263. /* always is 4 */
  264. return 4;
  265. }
  266. /*
  267. * return a static pointer to the source/destination MAC address
  268. * return NULL on error/address doesn't exist
  269. */
  270. u_char *
  271. dlt_null_get_mac(tcpeditdlt_t *ctx, _U_ tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  272. {
  273. assert(ctx);
  274. assert(packet);
  275. assert(pktlen);
  276. return(NULL);
  277. }
  278. tcpeditdlt_l2addr_type_t
  279. dlt_null_l2addr_type(void)
  280. {
  281. return NONE;
  282. }