dlt_utils.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 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 <string.h>
  20. #include "tcpedit.h"
  21. #include "dlt_utils.h"
  22. #include "common.h"
  23. /* from dlt_plugins.c */
  24. extern const uint32_t tcpeditdlt_bit_map[];
  25. extern const char *tcpeditdlt_bit_info[];
  26. /*
  27. * Call parse args on src & dst plugins
  28. */
  29. int
  30. tcpedit_dlt_parse_opts(tcpeditdlt_t *ctx)
  31. {
  32. assert(ctx);
  33. if (ctx->decoder->plugin_parse_opts(ctx) != TCPEDIT_OK)
  34. return TCPEDIT_ERROR;
  35. if (ctx->decoder->dlt != ctx->encoder->dlt) {
  36. if (ctx->encoder->plugin_parse_opts(ctx) != TCPEDIT_OK)
  37. return TCPEDIT_ERROR;
  38. }
  39. return TCPEDIT_OK;
  40. }
  41. /*
  42. * find a given plugin struct in the context for a given DLT. Returns NULL on failure
  43. */
  44. tcpeditdlt_plugin_t *
  45. tcpedit_dlt_getplugin(tcpeditdlt_t *ctx, int dlt)
  46. {
  47. tcpeditdlt_plugin_t *ptr;
  48. assert(ctx);
  49. ptr = ctx->plugins;
  50. if (ptr == NULL)
  51. return NULL;
  52. while (ptr->dlt != dlt && ptr->next != NULL) {
  53. ptr = ptr->next;
  54. }
  55. if (ptr->dlt == dlt)
  56. return ptr;
  57. return NULL;
  58. }
  59. /*
  60. * find a given plugin struct in the context for a given DLT. Returns NULL on failure
  61. */
  62. tcpeditdlt_plugin_t *
  63. tcpedit_dlt_getplugin_byname(tcpeditdlt_t *ctx, const char *name)
  64. {
  65. tcpeditdlt_plugin_t *ptr;
  66. assert(ctx);
  67. assert(name);
  68. ptr = ctx->plugins;
  69. if (ptr == NULL)
  70. return NULL;
  71. while ((strcmp(ptr->name, name) != 0) && ptr->next != NULL) {
  72. ptr = ptr->next;
  73. }
  74. if (strcmp(ptr->name, name) == 0)
  75. return ptr;
  76. return NULL;
  77. }
  78. /*
  79. * Create a new plugin struct. WILL NOT RETURN ON FAILURE! (out of memory is not recoverable)
  80. */
  81. tcpeditdlt_plugin_t *
  82. tcpedit_dlt_newplugin(void)
  83. {
  84. tcpeditdlt_plugin_t *plugin;
  85. plugin = (tcpeditdlt_plugin_t *)safe_malloc(sizeof(tcpeditdlt_plugin_t));
  86. plugin->dlt = 0xffff; /* zero is a valid plugin, so use 0xffff */
  87. return plugin;
  88. }
  89. /*
  90. * Add a plugin to the plugin chain for the given context. Return 0 on success,
  91. * -1 on failure
  92. */
  93. int
  94. tcpedit_dlt_addplugin(tcpeditdlt_t *ctx, tcpeditdlt_plugin_t *new)
  95. {
  96. tcpeditdlt_plugin_t *ptr;
  97. assert(ctx);
  98. assert(new);
  99. /* look for a dupe by DLT */
  100. if ((ptr = tcpedit_dlt_getplugin(ctx, new->dlt)) != NULL) {
  101. tcpedit_seterr(ctx->tcpedit, "Can only have one DLT plugin registered per-DLT: 0x%x", new->dlt);
  102. return TCPEDIT_ERROR;
  103. }
  104. /* dupe by name? */
  105. if ((ptr = tcpedit_dlt_getplugin_byname(ctx, new->name)) != NULL) {
  106. tcpedit_seterr(ctx->tcpedit, "Can only have one DLT plugin registered per-name: %s", new->name);
  107. return TCPEDIT_ERROR;
  108. }
  109. /*
  110. * check that the plugin is properly constructed, note that the encoder
  111. * and decoder are optional!
  112. */
  113. assert(new->dlt < 0xffff);
  114. assert(new->plugin_init);
  115. assert(new->plugin_cleanup);
  116. assert(new->plugin_parse_opts);
  117. assert(new->plugin_proto);
  118. assert(new->plugin_l2addr_type);
  119. assert(new->plugin_l2len);
  120. assert(new->plugin_get_layer3);
  121. assert(new->plugin_merge_layer3);
  122. /* add it to the end of the chain */
  123. if (ctx->plugins == NULL) {
  124. ctx->plugins = new;
  125. } else {
  126. ptr = ctx->plugins;
  127. while (ptr->next != NULL)
  128. ptr = ptr->next;
  129. ptr->next = new;
  130. }
  131. /* we're done */
  132. return 0;
  133. }
  134. /*
  135. * validates that the decoder plugin provides all the fields that are required
  136. * by then encoding plugin. Returns TCPEDIT_OK | TCPEDIT_ERROR
  137. */
  138. int
  139. tcpedit_dlt_validate(tcpeditdlt_t *ctx)
  140. {
  141. uint32_t bit;
  142. /* loops from 1 -> UINT32_MAX by powers of 2 */
  143. for (bit = 1; bit != 0; bit = bit << 2) {
  144. if (ctx->encoder->requires & bit && !(ctx->decoder->provides & bit)) {
  145. tcpedit_seterr(ctx->tcpedit, "%s", tcpeditdlt_bit_info[tcpeditdlt_bit_map[bit]]);
  146. return TCPEDIT_ERROR;
  147. }
  148. }
  149. dbgx(1, "Input linktype is %s",
  150. pcap_datalink_val_to_description(ctx->decoder->dlt));
  151. dbgx(1, "Output linktype is %s",
  152. pcap_datalink_val_to_description(ctx->encoder->dlt));
  153. return TCPEDIT_OK;
  154. }
  155. /*
  156. * Utility function to extract the Layer 3 header and beyond in a single buffer
  157. * Since some CPU's like UltraSPARC are strictly aligned, they really don't like
  158. * it when you jump to an offset which isn't on a word boundary (like ethernet)
  159. */
  160. u_char *
  161. tcpedit_dlt_l3data_copy(tcpeditdlt_t *ctx, u_char *packet, int pktlen, int l2len)
  162. {
  163. u_char *ptr;
  164. assert(ctx);
  165. assert(packet);
  166. if (pktlen <= l2len)
  167. return NULL;
  168. #ifdef FORCE_ALIGN
  169. /*
  170. * copy layer 3 and up to our temp packet buffer
  171. * for now on, we have to edit the packetbuff because
  172. * just before we send the packet, we copy the packetbuff
  173. * back onto the pkt.data + l2len buffer
  174. * we do all this work to prevent byte alignment issues
  175. */
  176. if (l2len % 4 == 0) {
  177. ptr = (&(packet)[l2len]);
  178. } else {
  179. ptr = ctx->l3buff;
  180. memcpy(ptr, (&(packet)[l2len]), pktlen - l2len);
  181. }
  182. #else
  183. /*
  184. * on non-strict byte align systems, don't need to memcpy(),
  185. * just point to 14 bytes into the existing buffer
  186. */
  187. ptr = (&(packet)[l2len]);
  188. #endif
  189. return ptr;
  190. }
  191. /*
  192. * reverse of tcpedit_dlt_l3data_copy
  193. */
  194. u_char *
  195. tcpedit_dlt_l3data_merge(tcpeditdlt_t *ctx, u_char *packet, int pktlen, const u_char *l3data, const int l2len)
  196. {
  197. assert(ctx);
  198. assert(packet);
  199. assert(pktlen >= 0);
  200. assert(l3data);
  201. assert(l2len >= 0);
  202. #ifdef FORCE_ALIGN
  203. /*
  204. * put back the layer 3 and above back in the pkt.data buffer
  205. * we can't edit the packet at layer 3 or above beyond this point
  206. */
  207. if (l2len % 4 != 0)
  208. memcpy((&(packet)[l2len]), l3data, pktlen - l2len);
  209. #endif
  210. return packet;
  211. }
  212. /**
  213. * When using subdecoders, we need to transfer the sub decoder state
  214. * to our state so that our primary encoder has it available.
  215. */
  216. int
  217. tcpedit_dlt_copy_decoder_state(tcpeditdlt_t *ctx, tcpeditdlt_t *subctx)
  218. {
  219. assert(ctx);
  220. assert(subctx);
  221. memcpy(&ctx->srcaddr, &subctx->srcaddr, sizeof(ctx->srcaddr));
  222. memcpy(&ctx->dstaddr, &subctx->dstaddr, sizeof(ctx->dstaddr));
  223. ctx->proto = subctx->proto;
  224. memcpy(&ctx->srcmac, &subctx->srcmac, MAX_MAC_LEN);
  225. memcpy(&ctx->dstmac, &subctx->dstmac, MAX_MAC_LEN);
  226. /* just need to copy the ptr */
  227. ctx->decoded_extra = subctx->decoded_extra;
  228. /*
  229. * the first decoder should of alraedy specified it's l2len, so we need to
  230. * add to it the l2len determined by the sub-plugin
  231. */
  232. ctx->l2len += subctx->l2len;
  233. return TCPEDIT_OK;
  234. }