pppserial.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2006-2007 Aaron Turner.
  4. * Copyright (c) 2013-2017 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright owners nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  26. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  28. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "tcpedit.h"
  35. #include "common.h"
  36. #include "tcpr.h"
  37. #include "dlt_utils.h"
  38. #include "tcpedit_stub.h"
  39. #include "pppserial.h"
  40. #include "pppserial_types.h"
  41. static char dlt_name[] = "pppserial";
  42. static u_int16_t dlt_value = 0x0032;
  43. /*
  44. * Function to register ourselves. This function is always called, regardless
  45. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  46. * or anything like that (use the dlt_pppserial_init() function below for that).
  47. * Tasks:
  48. * - Create a new plugin struct
  49. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  50. * actually in the header.
  51. * - Add the plugin to the context's plugin chain
  52. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  53. */
  54. int
  55. dlt_pppserial_register(tcpeditdlt_t *ctx)
  56. {
  57. tcpeditdlt_plugin_t *plugin;
  58. assert(ctx);
  59. /* create a new plugin structure */
  60. plugin = tcpedit_dlt_newplugin();
  61. /* set what we provide & require */
  62. plugin->provides += PLUGIN_MASK_PROTO;
  63. /* plugin->requires += PLUGIN_MASK_PROTO + PLUGIN_MASK_SRCADDR + PLUGIN_MASK_DSTADDR; */
  64. /* what is our DLT value? */
  65. plugin->dlt = dlt_value;
  66. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  67. plugin->name = safe_strdup(dlt_name);
  68. /*
  69. * Point to our functions, note, you need a function for EVERY method.
  70. * Even if it is only an empty stub returning success.
  71. */
  72. plugin->plugin_init = dlt_pppserial_init;
  73. plugin->plugin_post_init = dlt_pppserial_init;
  74. plugin->plugin_cleanup = dlt_pppserial_cleanup;
  75. plugin->plugin_parse_opts = dlt_pppserial_parse_opts;
  76. plugin->plugin_decode = dlt_pppserial_decode;
  77. plugin->plugin_encode = dlt_pppserial_encode;
  78. plugin->plugin_proto = dlt_pppserial_proto;
  79. plugin->plugin_l2addr_type = dlt_pppserial_l2addr_type;
  80. plugin->plugin_l2len = dlt_pppserial_l2len;
  81. plugin->plugin_get_layer3 = dlt_pppserial_get_layer3;
  82. plugin->plugin_merge_layer3 = dlt_pppserial_merge_layer3;
  83. plugin->plugin_get_mac = dlt_pppserial_get_mac;
  84. /* add it to the available plugin list */
  85. return tcpedit_dlt_addplugin(ctx, plugin);
  86. }
  87. /*
  88. * Initializer function. This function is called only once, if and only iif
  89. * this plugin will be utilized. Remember, if you need to keep track of any state,
  90. * store it in your plugin->config, not a global!
  91. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  92. */
  93. int
  94. dlt_pppserial_init(tcpeditdlt_t *ctx)
  95. {
  96. tcpeditdlt_plugin_t *plugin;
  97. assert(ctx);
  98. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  99. tcpedit_seterr(ctx->tcpedit, "Unable to initialize unregistered plugin %s", dlt_name);
  100. return TCPEDIT_ERROR;
  101. }
  102. return TCPEDIT_OK; /* success */
  103. }
  104. /**
  105. * Post init function. This function is called only once after init() and parse_opts()
  106. * It basically allows decoders to properly initialize sub-plugins.
  107. */
  108. int
  109. dlt_pppserial_post_init(tcpeditdlt_t *ctx)
  110. {
  111. assert(ctx);
  112. /* FIXME: Only needs to do something if we're using a sub-plugin
  113. * See the jnpr_ether_plugin for an example of this
  114. pppserial_config_t *config;
  115. // do nothing if we're not the decoder
  116. if (ctx->decoder->dlt != dlt_value)
  117. return TCPEDIT_OK;
  118. // init our subcontext & decoder
  119. config = (pppserial_config_t *)ctx->encoder->config;
  120. config->subctx = tcpedit_dlt_init(ctx->tcpedit, SUB_PLUGIN_DLT_TYPE);
  121. */
  122. return TCPEDIT_OK;
  123. }
  124. /*
  125. * Since this is used in a library, we should manually clean up after ourselves
  126. * Unless you allocated some memory in dlt_pppserial_init(), this is just an stub.
  127. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  128. */
  129. int
  130. dlt_pppserial_cleanup(tcpeditdlt_t *ctx)
  131. {
  132. tcpeditdlt_plugin_t *plugin;
  133. assert(ctx);
  134. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  135. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  136. return TCPEDIT_ERROR;
  137. }
  138. /* FIXME: make this function do something if necessary */
  139. if (ctx->decoded_extra != NULL) {
  140. safe_free(ctx->decoded_extra);
  141. ctx->decoded_extra = NULL;
  142. }
  143. if (plugin->config != NULL) {
  144. safe_free(plugin->config);
  145. plugin->config = NULL;
  146. }
  147. return TCPEDIT_OK; /* success */
  148. }
  149. /*
  150. * This is where you should define all your AutoGen AutoOpts option parsing.
  151. * Any user specified option should have it's bit turned on in the 'provides'
  152. * bit mask.
  153. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  154. */
  155. int
  156. dlt_pppserial_parse_opts(tcpeditdlt_t *ctx)
  157. {
  158. assert(ctx);
  159. /* no options! nothing to do here :) */
  160. return TCPEDIT_OK; /* success */
  161. }
  162. /*
  163. * Function to decode the layer 2 header in the packet.
  164. * You need to fill out:
  165. * - ctx->l2len
  166. * - ctx->srcaddr
  167. * - ctx->dstaddr
  168. * - ctx->proto
  169. * - ctx->decoded_extra
  170. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  171. */
  172. int
  173. dlt_pppserial_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  174. {
  175. struct tcpr_pppserial_hdr *ppp = NULL;
  176. assert(ctx);
  177. assert(packet);
  178. if (pktlen < 4)
  179. return TCPEDIT_ERROR;
  180. /*
  181. * PPP has three fields: address, control and protocol
  182. * address should always be 0xff, and control seems pretty meaningless.
  183. * protocol field informs you of the following header, but alas does not
  184. * use standard IEEE 802.11 values (IPv4 is not 0x0800, but is 0x0021)
  185. */
  186. ppp = (struct tcpr_pppserial_hdr *)packet;
  187. switch (ntohs(ppp->protocol)) {
  188. case 0x0021: /* IPv4 */
  189. ctx->proto = htons(ETHERTYPE_IP);
  190. ctx->l2len = 4;
  191. break;
  192. default:
  193. /*
  194. * PPP Seems to be using different protocol values then IEEE/802.x
  195. * but Wireshark seems to know how to decode them, so rather then
  196. * returning TCPEDIT_SOFT_ERROR and skipping rewrite completely,
  197. * I just copy the packet payload over and let Wireshark figure it out
  198. */
  199. ctx->l2len = 4;
  200. ctx->proto = ppp->protocol;
  201. }
  202. return TCPEDIT_OK; /* success */
  203. }
  204. /*
  205. * Function to encode the layer 2 header back into the packet.
  206. * Returns: total packet len or TCPEDIT_ERROR
  207. */
  208. int
  209. dlt_pppserial_encode(tcpeditdlt_t *ctx, u_char *packet, int pktlen, _U_ tcpr_dir_t dir)
  210. {
  211. assert(ctx);
  212. assert(packet);
  213. if (pktlen < 4)
  214. return TCPEDIT_ERROR;
  215. /* FIXME: make this function work */
  216. return pktlen; /* success */
  217. }
  218. /*
  219. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  220. * Make sure you return this value in NETWORK byte order!
  221. */
  222. int
  223. dlt_pppserial_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  224. {
  225. struct tcpr_pppserial_hdr *ppp = NULL;
  226. int protocol = 0;
  227. assert(ctx);
  228. assert(packet);
  229. if (pktlen < (int)sizeof(*ppp))
  230. return TCPEDIT_ERROR;
  231. ppp = (struct tcpr_pppserial_hdr *)packet;
  232. switch (ntohs(ppp->protocol)) {
  233. case 0x0021: /* IPv4 */
  234. protocol = ETHERTYPE_IP;
  235. break;
  236. default:
  237. tcpedit_seterr(ctx->tcpedit, "Packet " COUNTER_SPEC
  238. " isn't IP. Skipping packet",
  239. ctx->tcpedit->runtime.packetnum);
  240. return TCPEDIT_SOFT_ERROR;
  241. }
  242. return protocol;
  243. }
  244. /*
  245. * Function returns a pointer to the layer 3 protocol header or NULL on error
  246. */
  247. u_char *
  248. dlt_pppserial_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  249. {
  250. int l2len;
  251. assert(ctx);
  252. assert(packet);
  253. /* FIXME: Is there anything else we need to do?? */
  254. l2len = dlt_pppserial_l2len(ctx, packet, pktlen);
  255. if (pktlen < l2len)
  256. return NULL;
  257. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  258. }
  259. /*
  260. * function merges the packet (containing L2 and old L3) with the l3data buffer
  261. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  262. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  263. * like SPARC
  264. */
  265. u_char *
  266. dlt_pppserial_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  267. {
  268. int l2len;
  269. assert(ctx);
  270. assert(packet);
  271. assert(l3data);
  272. /* FIXME: Is there anything else we need to do?? */
  273. l2len = dlt_pppserial_l2len(ctx, packet, pktlen);
  274. if (pktlen < l2len)
  275. return NULL;
  276. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  277. }
  278. /*
  279. * return a static pointer to the source/destination MAC address
  280. * return NULL on error/address doesn't exist
  281. */
  282. u_char *
  283. dlt_pppserial_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t UNUSED(mac), const u_char *packet, const int pktlen)
  284. {
  285. assert(ctx);
  286. assert(packet);
  287. return NULL;
  288. }
  289. /*
  290. * return the length of the L2 header of the current packet
  291. */
  292. int
  293. dlt_pppserial_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  294. {
  295. assert(ctx);
  296. assert(packet);
  297. if (pktlen < 4)
  298. return 0;
  299. return 4;
  300. }
  301. tcpeditdlt_l2addr_type_t
  302. dlt_pppserial_l2addr_type(void)
  303. {
  304. return NONE;
  305. }