raw.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "tcpedit.h"
  22. #include "common.h"
  23. #include "tcpr.h"
  24. #include "dlt_utils.h"
  25. #include "tcpedit_stub.h"
  26. #include "raw.h"
  27. static char dlt_name[] = "raw";
  28. static char _U_ dlt_prefix[] = "raw";
  29. static uint16_t dlt_value = DLT_RAW;
  30. /*
  31. * DLT_RAW is basically a zero length L2 header for IPv4 & IPv6 packets
  32. */
  33. /*
  34. * Function to register ourselves. This function is always called, regardless
  35. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  36. * or anything like that (use the dlt_raw_init() function below for that).
  37. * Tasks:
  38. * - Create a new plugin struct
  39. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  40. * actually in the header.
  41. * - Add the plugin to the context's plugin chain
  42. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  43. */
  44. int
  45. dlt_raw_register(tcpeditdlt_t *ctx)
  46. {
  47. tcpeditdlt_plugin_t *plugin;
  48. assert(ctx);
  49. /* create a new plugin structure */
  50. plugin = tcpedit_dlt_newplugin();
  51. /* set what we provide & require */
  52. plugin->provides += PLUGIN_MASK_PROTO;
  53. /* what is our DLT value? */
  54. plugin->dlt = dlt_value;
  55. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  56. plugin->name = safe_strdup(dlt_prefix);
  57. /*
  58. * Point to our functions, note, you need a function for EVERY method.
  59. * Even if it is only an empty stub returning success.
  60. */
  61. plugin->plugin_init = dlt_raw_init;
  62. plugin->plugin_cleanup = dlt_raw_cleanup;
  63. plugin->plugin_parse_opts = dlt_raw_parse_opts;
  64. plugin->plugin_decode = dlt_raw_decode;
  65. plugin->plugin_encode = dlt_raw_encode;
  66. plugin->plugin_proto = dlt_raw_proto;
  67. plugin->plugin_l2addr_type = dlt_raw_l2addr_type;
  68. plugin->plugin_l2len = dlt_raw_l2len;
  69. plugin->plugin_get_layer3 = dlt_raw_get_layer3;
  70. plugin->plugin_merge_layer3 = dlt_raw_merge_layer3;
  71. plugin->plugin_get_mac = dlt_raw_get_mac;
  72. /* add it to the available plugin list */
  73. return tcpedit_dlt_addplugin(ctx, plugin);
  74. }
  75. /*
  76. * Initializer function. This function is called only once, if and only if
  77. * this plugin will be utilized. Remember, if you need to keep track of any state,
  78. * store it in your plugin->config, not a global!
  79. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  80. */
  81. int
  82. dlt_raw_init(tcpeditdlt_t *ctx)
  83. {
  84. tcpeditdlt_plugin_t *plugin;
  85. assert(ctx);
  86. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  87. tcpedit_seterr(ctx->tcpedit, "Unable to initialize unregistered plugin %s", dlt_name);
  88. return TCPEDIT_ERROR;
  89. }
  90. /* allocate memory for our config data */
  91. if (ctx->decoded_extra_size > 0) {
  92. if (ctx->decoded_extra_size < sizeof(raw_extra_t)) {
  93. ctx->decoded_extra_size = sizeof(raw_extra_t);
  94. ctx->decoded_extra = safe_realloc(ctx->decoded_extra,
  95. ctx->decoded_extra_size);
  96. }
  97. } else {
  98. ctx->decoded_extra_size = sizeof(raw_extra_t);
  99. ctx->decoded_extra = safe_malloc(ctx->decoded_extra_size);
  100. }
  101. /* allocate memory for our config data */
  102. plugin->config_size = sizeof(raw_config_t);
  103. plugin->config = safe_malloc(plugin->config_size);
  104. return TCPEDIT_OK; /* success */
  105. }
  106. /*
  107. * Since this is used in a library, we should manually clean up after ourselves
  108. * Unless you allocated some memory in dlt_raw_init(), this is just an stub.
  109. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  110. */
  111. int
  112. dlt_raw_cleanup(tcpeditdlt_t *ctx)
  113. {
  114. tcpeditdlt_plugin_t *plugin;
  115. assert(ctx);
  116. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  117. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  118. return TCPEDIT_ERROR;
  119. }
  120. safe_free(plugin->name);
  121. plugin->name = NULL;
  122. safe_free(plugin->config);
  123. plugin->config = NULL;
  124. plugin->config_size = 0;
  125. return TCPEDIT_OK; /* success */
  126. }
  127. /*
  128. * This is where you should define all your AutoGen AutoOpts option parsing.
  129. * Any user specified option should have it's bit turned on in the 'provides'
  130. * bit mask.
  131. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  132. */
  133. int
  134. dlt_raw_parse_opts(tcpeditdlt_t *ctx)
  135. {
  136. assert(ctx);
  137. /* no op */
  138. return TCPEDIT_OK; /* success */
  139. }
  140. /*
  141. * Function to decode the layer 2 header in the packet.
  142. * You need to fill out:
  143. * - ctx->l2len
  144. * - ctx->srcaddr
  145. * - ctx->dstaddr
  146. * - ctx->proto
  147. * - ctx->decoded_extra
  148. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  149. */
  150. int
  151. dlt_raw_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  152. {
  153. int proto;
  154. assert(ctx);
  155. assert(packet);
  156. if (pktlen == 0)
  157. return TCPEDIT_ERROR;
  158. if ((proto = dlt_raw_proto(ctx, packet, pktlen)) == TCPEDIT_ERROR)
  159. return TCPEDIT_ERROR;
  160. ctx->proto = (uint16_t)proto;
  161. ctx->l2len = 0;
  162. return TCPEDIT_OK; /* success */
  163. }
  164. /*
  165. * Function to encode the layer 2 header back into the packet.
  166. * Returns: total packet len or TCPEDIT_ERROR
  167. */
  168. int
  169. dlt_raw_encode(tcpeditdlt_t *ctx, u_char *packet, _U_ int pktlen,
  170. _U_ tcpr_dir_t dir)
  171. {
  172. assert(ctx);
  173. assert(packet);
  174. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_RAW plugin does not support packet encoding");
  175. return TCPEDIT_ERROR;
  176. }
  177. /*
  178. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  179. */
  180. int
  181. dlt_raw_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  182. {
  183. struct tcpr_ipv4_hdr *iphdr;
  184. assert(ctx);
  185. assert(packet);
  186. int protocol = 0;
  187. if (pktlen < (int)sizeof(*iphdr))
  188. return TCPEDIT_ERROR;
  189. iphdr = (struct tcpr_ipv4_hdr *)packet;
  190. if (iphdr->ip_v == 0x04) {
  191. protocol = ETHERTYPE_IP;
  192. } else if (iphdr->ip_v == 0x06) {
  193. protocol = ETHERTYPE_IP6;
  194. } else {
  195. tcpedit_seterr(ctx->tcpedit, "%s", "Unsupported DLT_RAW packet: doesn't look like IPv4 or IPv6");
  196. return TCPEDIT_ERROR;
  197. }
  198. return htons(protocol);
  199. }
  200. /*
  201. * Function returns a pointer to the layer 3 protocol header or NULL on error
  202. */
  203. u_char *
  204. dlt_raw_get_layer3(tcpeditdlt_t *ctx, u_char *packet, _U_ const int pktlen)
  205. {
  206. assert(ctx);
  207. assert(packet);
  208. /* raw has a zero byte header, so this is basically a non-op */
  209. return packet;
  210. }
  211. /*
  212. * function merges the packet (containing L2 and old L3) with the l3data buffer
  213. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  214. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  215. * like SPARC
  216. */
  217. u_char *
  218. dlt_raw_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, _U_ const int pktlen,
  219. u_char *l3data)
  220. {
  221. assert(ctx);
  222. assert(packet);
  223. assert(l3data);
  224. /* raw has a zero byte header, so this is basically a non-op */
  225. return packet;
  226. }
  227. /*
  228. * return the length of the L2 header of the current packet
  229. */
  230. int
  231. dlt_raw_l2len(tcpeditdlt_t *ctx, const u_char *packet, _U_ const int pktlen)
  232. {
  233. assert(ctx);
  234. assert(packet);
  235. return 0;
  236. }
  237. /*
  238. * return a static pointer to the source/destination MAC address
  239. * return NULL on error/address doesn't exist
  240. */
  241. u_char *
  242. dlt_raw_get_mac(tcpeditdlt_t *ctx, _U_ tcpeditdlt_mac_type_t mac,
  243. const u_char *packet, _U_ const int pktlen)
  244. {
  245. assert(ctx);
  246. assert(packet);
  247. return(NULL);
  248. }
  249. tcpeditdlt_l2addr_type_t
  250. dlt_raw_l2addr_type(void)
  251. {
  252. return NONE;
  253. }