user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* $Id: user.c 1893 2007-08-10 04:24:50Z 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 <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 _U_ 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. plugin->plugin_get_mac = dlt_user_get_mac;
  82. /* add it to the available plugin list */
  83. return tcpedit_dlt_addplugin(ctx, plugin);
  84. }
  85. /*
  86. * Initializer function. This function is called only once, if and only iif
  87. * this plugin will be utilized. Remember, if you need to keep track of any state,
  88. * store it in your plugin->config, not a global!
  89. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  90. */
  91. int
  92. dlt_user_init(tcpeditdlt_t *ctx)
  93. {
  94. tcpeditdlt_plugin_t *plugin;
  95. user_config_t *config;
  96. assert(ctx);
  97. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  98. tcpedit_seterr(ctx->tcpedit, "Unable to initalize unregistered plugin %s", dlt_name);
  99. return TCPEDIT_ERROR;
  100. }
  101. /* allocate memory for our deocde extra data */
  102. if (sizeof(user_extra_t) > 0)
  103. ctx->decoded_extra = safe_malloc(sizeof(user_extra_t));
  104. /* allocate memory for our config data */
  105. if (sizeof(user_config_t) > 0)
  106. plugin->config = safe_malloc(sizeof(user_config_t));
  107. config = (user_config_t *)plugin->config;
  108. /* do nothing */
  109. return TCPEDIT_OK; /* success */
  110. }
  111. /*
  112. * Since this is used in a library, we should manually clean up after ourselves
  113. * Unless you allocated some memory in dlt_user_init(), this is just an stub.
  114. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  115. */
  116. int
  117. dlt_user_cleanup(tcpeditdlt_t *ctx)
  118. {
  119. tcpeditdlt_plugin_t *plugin;
  120. assert(ctx);
  121. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  122. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  123. return TCPEDIT_ERROR;
  124. }
  125. /* FIXME: make this function do something if necessary */
  126. if (ctx->decoded_extra != NULL) {
  127. safe_free(ctx->decoded_extra);
  128. ctx->decoded_extra = NULL;
  129. }
  130. if (plugin->config != NULL) {
  131. safe_free(plugin->config);
  132. plugin->config = NULL;
  133. }
  134. return TCPEDIT_OK; /* success */
  135. }
  136. /*
  137. * This is where you should define all your AutoGen AutoOpts option parsing.
  138. * Any user specified option should have it's bit turned on in the 'provides'
  139. * bit mask.
  140. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  141. */
  142. int
  143. dlt_user_parse_opts(tcpeditdlt_t *ctx)
  144. {
  145. tcpeditdlt_plugin_t *plugin;
  146. user_config_t *config;
  147. assert(ctx);
  148. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  149. config = plugin->config;
  150. /*
  151. * --user-dlt will override the output DLT type, otherwise we'll use
  152. * the DLT of the decoder
  153. */
  154. if (HAVE_OPT(USER_DLT)) {
  155. config->dlt = OPT_VALUE_USER_DLT;
  156. } else {
  157. config->dlt = ctx->decoder->dlt;
  158. }
  159. /* --user-dlink */
  160. if (HAVE_OPT(USER_DLINK)) {
  161. int ct = STACKCT_OPT(USER_DLINK);
  162. char **list = STACKLST_OPT(USER_DLINK);
  163. int first = 1;
  164. do {
  165. char *p = *list++;
  166. if (first) {
  167. config->length = read_hexstring(p, config->l2server, USER_L2MAXLEN);
  168. memcpy(config->l2client, config->l2server, config->length);
  169. } else {
  170. if (config->length != read_hexstring(p, config->l2client, USER_L2MAXLEN)) {
  171. tcpedit_seterr(ctx->tcpedit, "%s",
  172. "both --dlink's must contain the same number of bytes");
  173. return TCPEDIT_ERROR;
  174. }
  175. }
  176. first = 0;
  177. } while (--ct > 0);
  178. }
  179. return TCPEDIT_OK; /* success */
  180. }
  181. /* you should never decode packets with this plugin! */
  182. int
  183. dlt_user_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  184. {
  185. assert(ctx);
  186. assert(packet);
  187. assert(pktlen);
  188. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_USER0 plugin does not support packet decoding");
  189. return TCPEDIT_ERROR;
  190. }
  191. /*
  192. * Function to encode the layer 2 header back into the packet.
  193. * Returns: total packet len or TCPEDIT_ERROR
  194. */
  195. int
  196. dlt_user_encode(tcpeditdlt_t *ctx, u_char **packet_ex, int pktlen, tcpr_dir_t dir)
  197. {
  198. u_char *packet;
  199. int l2len;
  200. user_config_t *config;
  201. tcpeditdlt_plugin_t *plugin;
  202. u_char tmpbuff[MAXPACKET];
  203. assert(ctx);
  204. assert(packet_ex);
  205. assert(pktlen > 0);
  206. packet = *packet_ex;
  207. assert(packet);
  208. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  209. config = plugin->config;
  210. /* Make room for our new l2 header if l2len != config->length */
  211. if (l2len > config->length) {
  212. memmove(packet + config->length, packet + ctx->l2len, pktlen - ctx->l2len);
  213. } else if (l2len < config->length) {
  214. memcpy(tmpbuff, packet, pktlen);
  215. memcpy(packet + config->length, (tmpbuff + ctx->l2len), pktlen - ctx->l2len);
  216. }
  217. /* update the total packet length */
  218. pktlen += config->length - ctx->l2len;
  219. if (dir == TCPR_DIR_C2S) {
  220. memcpy(packet, config->l2client, config->length);
  221. } else if (dir == TCPR_DIR_S2C) {
  222. memcpy(packet, config->l2server, config->length);
  223. } else {
  224. tcpedit_seterr(ctx->tcpedit, "%s", "Encoders only support C2S or C2S!");
  225. return TCPEDIT_ERROR;
  226. }
  227. return pktlen; /* success */
  228. }
  229. /*
  230. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  231. */
  232. int
  233. dlt_user_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  234. {
  235. assert(ctx);
  236. assert(packet);
  237. assert(pktlen);
  238. /* calling this for DLT_USER0 is broken */
  239. tcpedit_seterr(ctx->tcpedit, "%s", "Nonsensical calling of dlt_user_proto()");
  240. return TCPEDIT_ERROR;
  241. }
  242. /*
  243. * Function returns a pointer to the layer 3 protocol header or NULL on error
  244. */
  245. u_char *
  246. dlt_user_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  247. {
  248. int l2len;
  249. assert(ctx);
  250. assert(packet);
  251. /* FIXME: Is there anything else we need to do?? */
  252. l2len = dlt_user_l2len(ctx, packet, pktlen);
  253. assert(pktlen >= l2len);
  254. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  255. }
  256. /*
  257. * function merges the packet (containing L2 and old L3) with the l3data buffer
  258. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  259. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  260. * like SPARC
  261. */
  262. u_char *
  263. dlt_user_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  264. {
  265. int l2len;
  266. assert(ctx);
  267. assert(packet);
  268. assert(l3data);
  269. /* FIXME: Is there anything else we need to do?? */
  270. l2len = dlt_user_l2len(ctx, packet, pktlen);
  271. assert(pktlen >= l2len);
  272. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  273. }
  274. /*
  275. * return the length of the L2 header of the current packet
  276. */
  277. int
  278. dlt_user_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  279. {
  280. tcpeditdlt_plugin_t *plugin;
  281. user_config_t *config;
  282. assert(ctx);
  283. assert(packet);
  284. assert(pktlen);
  285. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  286. config = plugin->config;
  287. return config->length;
  288. }
  289. /*
  290. * return a static pointer to the source/destination MAC address
  291. * return NULL on error/address doesn't exist
  292. */
  293. u_char *
  294. dlt_user_get_mac(tcpeditdlt_t *ctx, _U_ tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  295. {
  296. assert(ctx);
  297. assert(packet);
  298. assert(pktlen);
  299. /* we don't know the format of USER DLT, hence always return NULL */
  300. return(NULL);
  301. }
  302. tcpeditdlt_l2addr_type_t
  303. dlt_user_l2addr_type(void)
  304. {
  305. return NONE;
  306. }
  307. /*
  308. * Need this special function for dlt_plugins.c:tcpedit_dlt_output_dlt()
  309. */
  310. u_int16_t
  311. dlt_user_get_output_dlt(tcpeditdlt_t *ctx)
  312. {
  313. tcpeditdlt_plugin_t *plugin;
  314. user_config_t *config;
  315. assert(ctx);
  316. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  317. config = plugin->config;
  318. return config->dlt;
  319. }