dlt_utils.c 7.3 KB

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