pppserial.c 11 KB

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