hdlc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 "hdlc.h"
  36. #include "tcpedit.h"
  37. #include "common.h"
  38. #include "tcpr.h"
  39. static char dlt_name[] = "hdlc";
  40. static char __attribute__((unused)) dlt_prefix[] = "hdlc";
  41. static u_int16_t dlt_value = DLT_C_HDLC;
  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_hdlc_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_hdlc_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;
  62. plugin->requires += PLUGIN_MASK_PROTO;
  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_hdlc_init;
  72. plugin->plugin_cleanup = dlt_hdlc_cleanup;
  73. plugin->plugin_parse_opts = dlt_hdlc_parse_opts;
  74. plugin->plugin_decode = dlt_hdlc_decode;
  75. plugin->plugin_encode = dlt_hdlc_encode;
  76. plugin->plugin_proto = dlt_hdlc_proto;
  77. plugin->plugin_l2addr_type = dlt_hdlc_l2addr_type;
  78. plugin->plugin_l2len = dlt_hdlc_l2len;
  79. plugin->plugin_get_layer3 = dlt_hdlc_get_layer3;
  80. plugin->plugin_merge_layer3 = dlt_hdlc_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_hdlc_init(tcpeditdlt_t *ctx)
  92. {
  93. tcpeditdlt_plugin_t *plugin;
  94. hdlc_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(hdlc_extra_t) > 0)
  102. ctx->decoded_extra = safe_malloc(sizeof(hdlc_extra_t));
  103. /* allocate memory for our config data */
  104. if (sizeof(hdlc_config_t) > 0)
  105. plugin->config = safe_malloc(sizeof(hdlc_config_t));
  106. config = (hdlc_config_t *)plugin->config;
  107. /* default to unset */
  108. config->address = 65535;
  109. config->control = 65535;
  110. return TCPEDIT_OK; /* success */
  111. }
  112. /*
  113. * Since this is used in a library, we should manually clean up after ourselves
  114. * Unless you allocated some memory in dlt_hdlc_init(), this is just an stub.
  115. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  116. */
  117. int
  118. dlt_hdlc_cleanup(tcpeditdlt_t *ctx)
  119. {
  120. tcpeditdlt_plugin_t *plugin;
  121. assert(ctx);
  122. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  123. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  124. return TCPEDIT_ERROR;
  125. }
  126. if (ctx->decoded_extra != NULL) {
  127. free(ctx->decoded_extra);
  128. ctx->decoded_extra = NULL;
  129. }
  130. if (plugin->config != NULL) {
  131. 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_hdlc_parse_opts(tcpeditdlt_t *ctx)
  144. {
  145. tcpeditdlt_plugin_t *plugin;
  146. hdlc_config_t *config;
  147. assert(ctx);
  148. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  149. config = plugin->config;
  150. if (HAVE_OPT(HDLC_CONTROL)) {
  151. config->control = (u_int16_t)OPT_VALUE_HDLC_CONTROL;
  152. }
  153. if (HAVE_OPT(HDLC_ADDRESS)) {
  154. config->address = (u_int16_t)OPT_VALUE_HDLC_ADDRESS;
  155. }
  156. return TCPEDIT_OK; /* success */
  157. }
  158. /*
  159. * Function to decode the layer 2 header in the packet.
  160. * You need to fill out:
  161. * - ctx->l2len
  162. * - ctx->srcaddr
  163. * - ctx->dstaddr
  164. * - ctx->proto
  165. * - ctx->decoded_extra
  166. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  167. */
  168. int
  169. dlt_hdlc_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  170. {
  171. cisco_hdlc_t *hdlc;
  172. hdlc_extra_t *extra;
  173. assert(ctx);
  174. assert(packet);
  175. assert(pktlen >= 4);
  176. extra = (hdlc_extra_t *)ctx->decoded_extra;
  177. hdlc = (cisco_hdlc_t *)packet;
  178. ctx->proto = hdlc->protocol;
  179. ctx->l2len = 4;
  180. extra->address = hdlc->address;
  181. extra->control = hdlc->control;
  182. return TCPEDIT_OK; /* success */
  183. }
  184. /*
  185. * Function to encode the layer 2 header back into the packet.
  186. * Returns: total packet len or TCPEDIT_ERROR
  187. */
  188. int
  189. dlt_hdlc_encode(tcpeditdlt_t *ctx, u_char **packet_ex, int pktlen, __attribute__((unused)) tcpr_dir_t dir)
  190. {
  191. cisco_hdlc_t *hdlc;
  192. hdlc_config_t *config = NULL;
  193. hdlc_extra_t *extra = NULL;
  194. tcpeditdlt_plugin_t *plugin = NULL;
  195. u_char *packet;
  196. u_char tmpbuff[MAXPACKET];
  197. assert(ctx);
  198. assert(packet_ex);
  199. assert(pktlen >= 4);
  200. packet = *packet_ex;
  201. assert(packet);
  202. /* Make room for our new l2 header if old l2len != 4 */
  203. if (ctx->l2len > 4) {
  204. memmove(packet + 4, packet + ctx->l2len, pktlen - ctx->l2len);
  205. } else if (ctx->l2len < 4) {
  206. memcpy(tmpbuff, packet, pktlen);
  207. memcpy(packet + 4, (tmpbuff + ctx->l2len), pktlen - ctx->l2len);
  208. }
  209. /* update the total packet length */
  210. pktlen += 4 - ctx->l2len;
  211. /*
  212. * HDLC doesn't support direction, since we have no real src/dst addresses
  213. * to deal with, so we just use the original packet data or option data
  214. */
  215. hdlc = (cisco_hdlc_t *)packet;
  216. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  217. config = plugin->config;
  218. extra = (hdlc_extra_t *)ctx->decoded_extra;
  219. /* set the address field */
  220. if (config->address < 65535) {
  221. hdlc->address = (u_int8_t)config->address;
  222. } else if (extra->hdlc) {
  223. hdlc->address = extra->hdlc;
  224. } else {
  225. tcpedit_seterr(ctx->tcpedit, "%s", "Non-HDLC packet requires --hdlc-address");
  226. return TCPEDIT_ERROR;
  227. }
  228. /* set the control field */
  229. if (config->control < 65535) {
  230. hdlc->control = (u_int8_t)config->control;
  231. } else if (extra->hdlc) {
  232. hdlc->control = extra->hdlc;
  233. } else {
  234. tcpedit_seterr(ctx->tcpedit, "%s", "Non-HDLC packet requires --hdlc-control");
  235. return TCPEDIT_ERROR;
  236. }
  237. /* copy over our protocol */
  238. hdlc->protocol = ctx->proto;
  239. return pktlen; /* success */
  240. }
  241. /*
  242. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  243. */
  244. int
  245. dlt_hdlc_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  246. {
  247. cisco_hdlc_t *hdlc;
  248. assert(ctx);
  249. assert(packet);
  250. assert(pktlen >= 4);
  251. hdlc = (cisco_hdlc_t *)packet;
  252. return ntohs(hdlc->protocol);
  253. }
  254. /*
  255. * Function returns a pointer to the layer 3 protocol header or NULL on error
  256. */
  257. u_char *
  258. dlt_hdlc_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  259. {
  260. int l2len;
  261. assert(ctx);
  262. assert(packet);
  263. /* FIXME: Is there anything else we need to do?? */
  264. l2len = dlt_hdlc_l2len(ctx, packet, pktlen);
  265. assert(pktlen >= l2len);
  266. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  267. }
  268. /*
  269. * function merges the packet (containing L2 and old L3) with the l3data buffer
  270. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  271. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  272. * like SPARC
  273. */
  274. u_char *
  275. dlt_hdlc_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  276. {
  277. int l2len;
  278. assert(ctx);
  279. assert(packet);
  280. assert(l3data);
  281. /* FIXME: Is there anything else we need to do?? */
  282. l2len = dlt_hdlc_l2len(ctx, packet, pktlen);
  283. assert(pktlen >= l2len);
  284. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  285. }
  286. /*
  287. * return the length of the L2 header of the current packet
  288. */
  289. int
  290. dlt_hdlc_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  291. {
  292. assert(ctx);
  293. assert(packet);
  294. assert(pktlen);
  295. /* HDLC is a static 4 bytes */
  296. return 4;
  297. }
  298. tcpeditdlt_l2addr_type_t
  299. dlt_hdlc_l2addr_type(void)
  300. {
  301. return C_HDLC;
  302. }