user.c 11 KB

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