hdlc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* $Id: hdlc.c 1911 2007-10-23 21:53:34Z 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 "hdlc.h"
  36. #include "tcpedit.h"
  37. #include "common.h"
  38. #include "tcpr.h"
  39. static char dlt_name[] = "hdlc";
  40. static char _U_ 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. plugin->plugin_get_mac = dlt_hdlc_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_hdlc_init(tcpeditdlt_t *ctx)
  93. {
  94. tcpeditdlt_plugin_t *plugin;
  95. hdlc_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(hdlc_extra_t) > 0)
  103. ctx->decoded_extra = safe_malloc(sizeof(hdlc_extra_t));
  104. /* allocate memory for our config data */
  105. if (sizeof(hdlc_config_t) > 0)
  106. plugin->config = safe_malloc(sizeof(hdlc_config_t));
  107. config = (hdlc_config_t *)plugin->config;
  108. /* default to unset */
  109. config->address = 65535;
  110. config->control = 65535;
  111. return TCPEDIT_OK; /* success */
  112. }
  113. /*
  114. * Since this is used in a library, we should manually clean up after ourselves
  115. * Unless you allocated some memory in dlt_hdlc_init(), this is just an stub.
  116. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  117. */
  118. int
  119. dlt_hdlc_cleanup(tcpeditdlt_t *ctx)
  120. {
  121. tcpeditdlt_plugin_t *plugin;
  122. assert(ctx);
  123. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  124. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  125. return TCPEDIT_ERROR;
  126. }
  127. if (ctx->decoded_extra != NULL) {
  128. safe_free(ctx->decoded_extra);
  129. ctx->decoded_extra = NULL;
  130. }
  131. if (plugin->config != NULL) {
  132. safe_free(plugin->config);
  133. plugin->config = NULL;
  134. }
  135. return TCPEDIT_OK; /* success */
  136. }
  137. /*
  138. * This is where you should define all your AutoGen AutoOpts option parsing.
  139. * Any user specified option should have it's bit turned on in the 'provides'
  140. * bit mask.
  141. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  142. */
  143. int
  144. dlt_hdlc_parse_opts(tcpeditdlt_t *ctx)
  145. {
  146. tcpeditdlt_plugin_t *plugin;
  147. hdlc_config_t *config;
  148. assert(ctx);
  149. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  150. config = plugin->config;
  151. if (HAVE_OPT(HDLC_CONTROL)) {
  152. config->control = (u_int16_t)OPT_VALUE_HDLC_CONTROL;
  153. }
  154. if (HAVE_OPT(HDLC_ADDRESS)) {
  155. config->address = (u_int16_t)OPT_VALUE_HDLC_ADDRESS;
  156. }
  157. return TCPEDIT_OK; /* success */
  158. }
  159. /*
  160. * Function to decode the layer 2 header in the packet.
  161. * You need to fill out:
  162. * - ctx->l2len
  163. * - ctx->srcaddr
  164. * - ctx->dstaddr
  165. * - ctx->proto
  166. * - ctx->decoded_extra
  167. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  168. */
  169. int
  170. dlt_hdlc_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  171. {
  172. cisco_hdlc_t *hdlc;
  173. hdlc_extra_t *extra;
  174. assert(ctx);
  175. assert(packet);
  176. assert(pktlen >= 4);
  177. extra = (hdlc_extra_t *)ctx->decoded_extra;
  178. hdlc = (cisco_hdlc_t *)packet;
  179. ctx->proto = hdlc->protocol;
  180. ctx->l2len = 4;
  181. extra->address = hdlc->address;
  182. extra->control = hdlc->control;
  183. return TCPEDIT_OK; /* success */
  184. }
  185. /*
  186. * Function to encode the layer 2 header back into the packet.
  187. * Returns: total packet len or TCPEDIT_ERROR
  188. */
  189. int
  190. dlt_hdlc_encode(tcpeditdlt_t *ctx, u_char **packet_ex, int pktlen, _U_ tcpr_dir_t dir)
  191. {
  192. cisco_hdlc_t *hdlc;
  193. hdlc_config_t *config = NULL;
  194. hdlc_extra_t *extra = NULL;
  195. tcpeditdlt_plugin_t *plugin = NULL;
  196. u_char *packet;
  197. u_char tmpbuff[MAXPACKET];
  198. assert(ctx);
  199. assert(packet_ex);
  200. assert(pktlen >= 4);
  201. packet = *packet_ex;
  202. assert(packet);
  203. /* Make room for our new l2 header if old l2len != 4 */
  204. if (ctx->l2len > 4) {
  205. memmove(packet + 4, packet + ctx->l2len, pktlen - ctx->l2len);
  206. } else if (ctx->l2len < 4) {
  207. memcpy(tmpbuff, packet, pktlen);
  208. memcpy(packet + 4, (tmpbuff + ctx->l2len), pktlen - ctx->l2len);
  209. }
  210. /* update the total packet length */
  211. pktlen += 4 - ctx->l2len;
  212. /*
  213. * HDLC doesn't support direction, since we have no real src/dst addresses
  214. * to deal with, so we just use the original packet data or option data
  215. */
  216. hdlc = (cisco_hdlc_t *)packet;
  217. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  218. config = plugin->config;
  219. extra = (hdlc_extra_t *)ctx->decoded_extra;
  220. /* set the address field */
  221. if (config->address < 65535) {
  222. hdlc->address = (u_int8_t)config->address;
  223. } else if (extra->hdlc) {
  224. hdlc->address = extra->hdlc;
  225. } else {
  226. tcpedit_seterr(ctx->tcpedit, "%s", "Non-HDLC packet requires --hdlc-address");
  227. return TCPEDIT_ERROR;
  228. }
  229. /* set the control field */
  230. if (config->control < 65535) {
  231. hdlc->control = (u_int8_t)config->control;
  232. } else if (extra->hdlc) {
  233. hdlc->control = extra->hdlc;
  234. } else {
  235. tcpedit_seterr(ctx->tcpedit, "%s", "Non-HDLC packet requires --hdlc-control");
  236. return TCPEDIT_ERROR;
  237. }
  238. /* copy over our protocol */
  239. hdlc->protocol = ctx->proto;
  240. return pktlen; /* success */
  241. }
  242. /*
  243. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  244. */
  245. int
  246. dlt_hdlc_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  247. {
  248. cisco_hdlc_t *hdlc;
  249. assert(ctx);
  250. assert(packet);
  251. assert(pktlen >= 4);
  252. hdlc = (cisco_hdlc_t *)packet;
  253. return hdlc->protocol;
  254. }
  255. /*
  256. * Function returns a pointer to the layer 3 protocol header or NULL on error
  257. */
  258. u_char *
  259. dlt_hdlc_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  260. {
  261. int l2len;
  262. assert(ctx);
  263. assert(packet);
  264. /* FIXME: Is there anything else we need to do?? */
  265. l2len = dlt_hdlc_l2len(ctx, packet, pktlen);
  266. assert(pktlen >= l2len);
  267. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  268. }
  269. /*
  270. * function merges the packet (containing L2 and old L3) with the l3data buffer
  271. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  272. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  273. * like SPARC
  274. */
  275. u_char *
  276. dlt_hdlc_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  277. {
  278. int l2len;
  279. assert(ctx);
  280. assert(packet);
  281. assert(l3data);
  282. /* FIXME: Is there anything else we need to do?? */
  283. l2len = dlt_hdlc_l2len(ctx, packet, pktlen);
  284. assert(pktlen >= l2len);
  285. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  286. }
  287. /*
  288. * return the length of the L2 header of the current packet
  289. */
  290. int
  291. dlt_hdlc_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  292. {
  293. assert(ctx);
  294. assert(packet);
  295. assert(pktlen);
  296. /* HDLC is a static 4 bytes */
  297. return 4;
  298. }
  299. /*
  300. * return a static pointer to the source/destination MAC address
  301. * return NULL on error/address doesn't exist
  302. */
  303. u_char *
  304. dlt_hdlc_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  305. {
  306. assert(ctx);
  307. assert(packet);
  308. assert(pktlen);
  309. /* FIXME: return a ptr to the source or dest mac address. */
  310. switch(mac) {
  311. case SRC_MAC:
  312. return(NULL);
  313. break;
  314. case DST_MAC:
  315. memcpy(ctx->dstmac, packet, 2);
  316. return(ctx->dstmac);
  317. break;
  318. default:
  319. errx(1, "Invalid tcpeditdlt_mac_type_t: %d", mac);
  320. }
  321. return(NULL);
  322. }
  323. tcpeditdlt_l2addr_type_t
  324. dlt_hdlc_l2addr_type(void)
  325. {
  326. return C_HDLC;
  327. }