pppserial.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2006-2007 Aaron Turner.
  4. * Copyright (c) 2013-2018 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 if
  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 _U_ *ctx)
  110. {
  111. /* FIXME: Only needs to do something if we're using a sub-plugin
  112. * See the jnpr_ether_plugin for an example of this
  113. pppserial_config_t *config;
  114. // do nothing if we're not the decoder
  115. if (ctx->decoder->dlt != dlt_value)
  116. return TCPEDIT_OK;
  117. // init our subcontext & decoder
  118. config = (pppserial_config_t *)ctx->encoder->config;
  119. config->subctx = tcpedit_dlt_init(ctx->tcpedit, SUB_PLUGIN_DLT_TYPE);
  120. */
  121. return TCPEDIT_OK;
  122. }
  123. /*
  124. * Since this is used in a library, we should manually clean up after ourselves
  125. * Unless you allocated some memory in dlt_pppserial_init(), this is just an stub.
  126. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  127. */
  128. int
  129. dlt_pppserial_cleanup(tcpeditdlt_t *ctx)
  130. {
  131. tcpeditdlt_plugin_t *plugin;
  132. assert(ctx);
  133. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  134. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  135. return TCPEDIT_ERROR;
  136. }
  137. safe_free(plugin->name);
  138. plugin->name = NULL;
  139. if (plugin->config != NULL) {
  140. /* clean up the en10mb plugin */
  141. /* FIXME: Only needs to do something if we're using a sub-plugin
  142. pppserial_config_t *config;
  143. config = (pppserial_config_t *)ctx->encoder->config;
  144. tcpedit_dlt_cleanup(config->subctx);
  145. safe_free(config->subctx);
  146. */
  147. safe_free(plugin->config);
  148. plugin->config = NULL;
  149. plugin->config_size = 0;
  150. }
  151. return TCPEDIT_OK; /* success */
  152. }
  153. /*
  154. * This is where you should define all your AutoGen AutoOpts option parsing.
  155. * Any user specified option should have it's bit turned on in the 'provides'
  156. * bit mask.
  157. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  158. */
  159. int
  160. dlt_pppserial_parse_opts(tcpeditdlt_t *ctx)
  161. {
  162. assert(ctx);
  163. /* no options! nothing to do here :) */
  164. return TCPEDIT_OK; /* success */
  165. }
  166. /*
  167. * Function to decode the layer 2 header in the packet.
  168. * You need to fill out:
  169. * - ctx->l2len
  170. * - ctx->srcaddr
  171. * - ctx->dstaddr
  172. * - ctx->proto
  173. * - ctx->decoded_extra
  174. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  175. */
  176. int
  177. dlt_pppserial_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  178. {
  179. struct tcpr_pppserial_hdr *ppp = NULL;
  180. assert(ctx);
  181. assert(packet);
  182. if (pktlen < 4)
  183. return TCPEDIT_ERROR;
  184. /*
  185. * PPP has three fields: address, control and protocol
  186. * address should always be 0xff, and control seems pretty meaningless.
  187. * protocol field informs you of the following header, but alas does not
  188. * use standard IEEE 802.11 values (IPv4 is not 0x0800, but is 0x0021)
  189. */
  190. ppp = (struct tcpr_pppserial_hdr *)packet;
  191. switch (ntohs(ppp->protocol)) {
  192. case 0x0021: /* IPv4 */
  193. ctx->proto = htons(ETHERTYPE_IP);
  194. ctx->l2len = 4;
  195. break;
  196. default:
  197. /*
  198. * PPP Seems to be using different protocol values then IEEE/802.x
  199. * but Wireshark seems to know how to decode them, so rather then
  200. * returning TCPEDIT_SOFT_ERROR and skipping rewrite completely,
  201. * I just copy the packet payload over and let Wireshark figure it out
  202. */
  203. ctx->l2len = 4;
  204. ctx->proto = ppp->protocol;
  205. }
  206. return TCPEDIT_OK; /* success */
  207. }
  208. /*
  209. * Function to encode the layer 2 header back into the packet.
  210. * Returns: total packet len or TCPEDIT_ERROR
  211. */
  212. int
  213. dlt_pppserial_encode(tcpeditdlt_t *ctx, u_char *packet, int pktlen, _U_ tcpr_dir_t dir)
  214. {
  215. assert(ctx);
  216. assert(packet);
  217. if (pktlen < 4)
  218. return TCPEDIT_ERROR;
  219. /* FIXME: make this function work */
  220. return pktlen; /* success */
  221. }
  222. /*
  223. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  224. * Make sure you return this value in NETWORK byte order!
  225. */
  226. int
  227. dlt_pppserial_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  228. {
  229. struct tcpr_pppserial_hdr *ppp = NULL;
  230. int protocol = 0;
  231. assert(ctx);
  232. assert(packet);
  233. if (pktlen < (int)sizeof(*ppp))
  234. return TCPEDIT_ERROR;
  235. ppp = (struct tcpr_pppserial_hdr *)packet;
  236. switch (ntohs(ppp->protocol)) {
  237. case 0x0021: /* IPv4 */
  238. protocol = ETHERTYPE_IP;
  239. break;
  240. default:
  241. tcpedit_seterr(ctx->tcpedit, "Packet " COUNTER_SPEC
  242. " isn't IP. Skipping packet",
  243. ctx->tcpedit->runtime.packetnum);
  244. return TCPEDIT_SOFT_ERROR;
  245. }
  246. return protocol;
  247. }
  248. /*
  249. * Function returns a pointer to the layer 3 protocol header or NULL on error
  250. */
  251. u_char *
  252. dlt_pppserial_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  253. {
  254. int l2len;
  255. assert(ctx);
  256. assert(packet);
  257. /* FIXME: Is there anything else we need to do?? */
  258. l2len = dlt_pppserial_l2len(ctx, packet, pktlen);
  259. if (l2len == -1 || pktlen < l2len)
  260. return NULL;
  261. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  262. }
  263. /*
  264. * function merges the packet (containing L2 and old L3) with the l3data buffer
  265. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  266. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  267. * like SPARC
  268. */
  269. u_char *
  270. dlt_pppserial_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  271. {
  272. int l2len;
  273. assert(ctx);
  274. assert(packet);
  275. assert(l3data);
  276. /* FIXME: Is there anything else we need to do?? */
  277. l2len = dlt_pppserial_l2len(ctx, packet, pktlen);
  278. if (l2len == -1 || pktlen < l2len)
  279. return NULL;
  280. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  281. }
  282. /*
  283. * return a static pointer to the source/destination MAC address
  284. * return NULL on error/address doesn't exist
  285. */
  286. u_char *
  287. dlt_pppserial_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t UNUSED(mac),
  288. const u_char *packet, _U_ const int pktlen)
  289. {
  290. assert(ctx);
  291. assert(packet);
  292. return NULL;
  293. }
  294. /*
  295. * return the length of the L2 header of the current packet
  296. */
  297. int
  298. dlt_pppserial_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  299. {
  300. assert(ctx);
  301. assert(packet);
  302. if (pktlen < 4)
  303. return -1;
  304. return 4;
  305. }
  306. tcpeditdlt_l2addr_type_t
  307. dlt_pppserial_l2addr_type(void)
  308. {
  309. return NONE;
  310. }