user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 "user.h"
  27. static char dlt_name[] = "user";
  28. static char _U_ dlt_prefix[] = "user";
  29. static uint16_t dlt_value = DLT_USER0;
  30. /*
  31. * Function to register ourselves. This function is always called, regardless
  32. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  33. * or anything like that (use the dlt_user_init() function below for that).
  34. * Tasks:
  35. * - Create a new plugin struct
  36. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  37. * actually in the header.
  38. * - Add the plugin to the context's plugin chain
  39. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  40. */
  41. int
  42. dlt_user_register(tcpeditdlt_t *ctx)
  43. {
  44. tcpeditdlt_plugin_t *plugin;
  45. assert(ctx);
  46. /* create a new plugin structure */
  47. plugin = tcpedit_dlt_newplugin();
  48. /* FIXME: set what we provide & require */
  49. plugin->provides += PLUGIN_MASK_PROTO + PLUGIN_MASK_SRCADDR + PLUGIN_MASK_DSTADDR;
  50. plugin->requires += 0; // PLUGIN_MASK_PROTO + PLUGIN_MASK_SRCADDR + PLUGIN_MASK_DSTADDR;
  51. /* what is our DLT value? */
  52. plugin->dlt = dlt_value;
  53. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  54. plugin->name = safe_strdup(dlt_prefix);
  55. /*
  56. * Point to our functions, note, you need a function for EVERY method.
  57. * Even if it is only an empty stub returning success.
  58. */
  59. plugin->plugin_init = dlt_user_init;
  60. plugin->plugin_cleanup = dlt_user_cleanup;
  61. plugin->plugin_parse_opts = dlt_user_parse_opts;
  62. plugin->plugin_decode = dlt_user_decode;
  63. plugin->plugin_encode = dlt_user_encode;
  64. plugin->plugin_proto = dlt_user_proto;
  65. plugin->plugin_l2addr_type = dlt_user_l2addr_type;
  66. plugin->plugin_l2len = dlt_user_l2len;
  67. plugin->plugin_get_layer3 = dlt_user_get_layer3;
  68. plugin->plugin_merge_layer3 = dlt_user_merge_layer3;
  69. plugin->plugin_get_mac = dlt_user_get_mac;
  70. /* add it to the available plugin list */
  71. return tcpedit_dlt_addplugin(ctx, plugin);
  72. }
  73. /*
  74. * Initializer function. This function is called only once, if and only if
  75. * this plugin will be utilized. Remember, if you need to keep track of any state,
  76. * store it in your plugin->config, not a global!
  77. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  78. */
  79. int
  80. dlt_user_init(tcpeditdlt_t *ctx)
  81. {
  82. tcpeditdlt_plugin_t *plugin;
  83. user_config_t *config;
  84. assert(ctx);
  85. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  86. tcpedit_seterr(ctx->tcpedit, "Unable to initialize unregistered plugin %s", dlt_name);
  87. return TCPEDIT_ERROR;
  88. }
  89. /* allocate memory for our decode extra data - plus some space for
  90. * other DLT decodes
  91. */
  92. ctx->decoded_extra_size = USER_L2MAXLEN;
  93. ctx->decoded_extra = safe_malloc(ctx->decoded_extra_size);
  94. /* allocate memory for our config data */
  95. plugin->config_size = sizeof(user_config_t);
  96. plugin->config = safe_malloc(plugin->config_size);
  97. config = (user_config_t *)plugin->config;
  98. config->length = -1;
  99. /* do nothing */
  100. return TCPEDIT_OK; /* success */
  101. }
  102. /*
  103. * Since this is used in a library, we should manually clean up after ourselves
  104. * Unless you allocated some memory in dlt_user_init(), this is just an stub.
  105. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  106. */
  107. int
  108. dlt_user_cleanup(tcpeditdlt_t *ctx)
  109. {
  110. tcpeditdlt_plugin_t *plugin;
  111. assert(ctx);
  112. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  113. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  114. return TCPEDIT_ERROR;
  115. }
  116. /* FIXME: make this function do something if necessary */
  117. if (ctx->decoded_extra != NULL) {
  118. safe_free(ctx->decoded_extra);
  119. ctx->decoded_extra = NULL;
  120. ctx->decoded_extra_size = 0;
  121. }
  122. if (plugin->config != NULL) {
  123. safe_free(plugin->config);
  124. plugin->config = NULL;
  125. plugin->config_size = 0;
  126. }
  127. return TCPEDIT_OK; /* success */
  128. }
  129. /*
  130. * This is where you should define all your AutoGen AutoOpts option parsing.
  131. * Any user specified option should have it's bit turned on in the 'provides'
  132. * bit mask.
  133. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  134. */
  135. int
  136. dlt_user_parse_opts(tcpeditdlt_t *ctx)
  137. {
  138. tcpeditdlt_plugin_t *plugin;
  139. user_config_t *config;
  140. assert(ctx);
  141. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  142. if (!plugin)
  143. return TCPEDIT_ERROR;
  144. config = plugin->config;
  145. if (plugin->config_size < sizeof(*config))
  146. return TCPEDIT_ERROR;
  147. /*
  148. * --user-dlt will override the output DLT type, otherwise we'll use
  149. * the DLT of the decoder
  150. */
  151. if (HAVE_OPT(USER_DLT)) {
  152. config->dlt = OPT_VALUE_USER_DLT;
  153. } else {
  154. config->dlt = ctx->decoder->dlt;
  155. }
  156. /* --user-dlink */
  157. if (HAVE_OPT(USER_DLINK)) {
  158. int ct = STACKCT_OPT(USER_DLINK);
  159. char **list = (char**)STACKLST_OPT(USER_DLINK);
  160. int first = 1;
  161. do {
  162. char *p = *list++;
  163. if (first) {
  164. config->length = read_hexstring(p, config->l2server, USER_L2MAXLEN);
  165. memcpy(config->l2client, config->l2server, config->length);
  166. } else {
  167. if (config->length != read_hexstring(p, config->l2client, USER_L2MAXLEN)) {
  168. tcpedit_seterr(ctx->tcpedit, "%s",
  169. "both --dlink's must contain the same number of bytes");
  170. return TCPEDIT_ERROR;
  171. }
  172. }
  173. first = 0;
  174. } while (--ct > 0);
  175. }
  176. return TCPEDIT_OK; /* success */
  177. }
  178. /* you should never decode packets with this plugin! */
  179. int
  180. dlt_user_decode(tcpeditdlt_t *ctx, const u_char *packet, const int _U_ pktlen)
  181. {
  182. assert(ctx);
  183. assert(packet);
  184. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_USER0 plugin does not support packet decoding");
  185. return TCPEDIT_ERROR;
  186. }
  187. /*
  188. * Function to encode the layer 2 header back into the packet.
  189. * Returns: total packet len or TCPEDIT_ERROR
  190. */
  191. int
  192. dlt_user_encode(tcpeditdlt_t *ctx, u_char *packet, int pktlen, tcpr_dir_t dir)
  193. {
  194. user_config_t *config;
  195. tcpeditdlt_plugin_t *plugin;
  196. u_char tmpbuff[MAXPACKET];
  197. assert(ctx);
  198. assert(packet);
  199. if (pktlen == 0)
  200. return TCPEDIT_ERROR;
  201. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  202. if (!plugin)
  203. return TCPEDIT_ERROR;
  204. config = plugin->config;
  205. if (plugin->config_size < sizeof(*config))
  206. return TCPEDIT_ERROR;
  207. /* Make room for our new l2 header if l2len != config->length */
  208. if (ctx->l2len > config->length) {
  209. memmove(packet + config->length, packet + ctx->l2len, pktlen - ctx->l2len);
  210. } else if (ctx->l2len < config->length) {
  211. memcpy(tmpbuff, packet, pktlen);
  212. memcpy(packet + config->length, (tmpbuff + ctx->l2len), pktlen - ctx->l2len);
  213. }
  214. /* update the total packet length */
  215. pktlen += config->length - ctx->l2len;
  216. if (dir == TCPR_DIR_C2S) {
  217. memcpy(packet, config->l2client, config->length);
  218. } else if (dir == TCPR_DIR_S2C) {
  219. memcpy(packet, config->l2server, config->length);
  220. } else {
  221. tcpedit_seterr(ctx->tcpedit, "%s", "Encoders only support C2S or C2S!");
  222. return TCPEDIT_ERROR;
  223. }
  224. return pktlen; /* success */
  225. }
  226. /*
  227. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  228. */
  229. int
  230. dlt_user_proto(tcpeditdlt_t *ctx, const u_char *packet, const int _U_ pktlen)
  231. {
  232. assert(ctx);
  233. assert(packet);
  234. /* calling this for DLT_USER0 is broken */
  235. tcpedit_seterr(ctx->tcpedit, "%s", "Nonsensical calling of dlt_user_proto()");
  236. return TCPEDIT_ERROR;
  237. }
  238. /*
  239. * Function returns a pointer to the layer 3 protocol header or NULL on error
  240. */
  241. u_char *
  242. dlt_user_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  243. {
  244. int l2len;
  245. assert(ctx);
  246. assert(packet);
  247. /* FIXME: Is there anything else we need to do?? */
  248. l2len = dlt_user_l2len(ctx, packet, pktlen);
  249. if (l2len == -1 || pktlen < l2len)
  250. return NULL;
  251. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  252. }
  253. /*
  254. * function merges the packet (containing L2 and old L3) with the l3data buffer
  255. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  256. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  257. * like SPARC
  258. */
  259. u_char *
  260. dlt_user_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  261. {
  262. int l2len;
  263. assert(ctx);
  264. assert(packet);
  265. assert(l3data);
  266. /* FIXME: Is there anything else we need to do?? */
  267. l2len = dlt_user_l2len(ctx, packet, pktlen);
  268. if (l2len == TCPEDIT_ERROR || pktlen < l2len)
  269. return NULL;
  270. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  271. }
  272. /*
  273. * return the length of the L2 header of the current packet
  274. */
  275. int
  276. dlt_user_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int _U_ pktlen)
  277. {
  278. tcpeditdlt_plugin_t *plugin;
  279. user_config_t *config;
  280. assert(ctx);
  281. assert(packet);
  282. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  283. if (!plugin)
  284. return TCPEDIT_ERROR;
  285. config = plugin->config;
  286. if (plugin->config_size < sizeof(*config))
  287. return TCPEDIT_ERROR;
  288. return config->length;
  289. }
  290. /*
  291. * return a static pointer to the source/destination MAC address
  292. * return NULL on error/address doesn't exist
  293. */
  294. u_char *
  295. dlt_user_get_mac(tcpeditdlt_t *ctx, _U_ tcpeditdlt_mac_type_t mac,
  296. const u_char *packet, const int _U_ pktlen)
  297. {
  298. assert(ctx);
  299. assert(packet);
  300. /* we don't know the format of USER DLT, hence always return NULL */
  301. return(NULL);
  302. }
  303. tcpeditdlt_l2addr_type_t
  304. dlt_user_l2addr_type(void)
  305. {
  306. return NONE;
  307. }
  308. /*
  309. * Need this special function for dlt_plugins.c:tcpedit_dlt_output_dlt()
  310. */
  311. uint16_t
  312. dlt_user_get_output_dlt(tcpeditdlt_t *ctx)
  313. {
  314. tcpeditdlt_plugin_t *plugin;
  315. user_config_t *config;
  316. assert(ctx);
  317. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  318. config = plugin->config;
  319. return config->dlt;
  320. }