jnpr_ether.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 "defines.h"
  35. #include "common.h"
  36. #include "tcpr.h"
  37. #include "tcpedit.h"
  38. #include "dlt_utils.h"
  39. #include "tcpedit_stub.h"
  40. #include "jnpr_ether.h"
  41. #include "jnpr_ether_types.h"
  42. #include "../ethernet.h"
  43. #include "../dlt_en10mb/en10mb.h"
  44. static char dlt_name[] = "jnpr_eth";
  45. static uint16_t dlt_value = DLT_JUNIPER_ETHER;
  46. /*
  47. * Function to register ourselves. This function is always called, regardless
  48. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  49. * or anything like that (use the dlt_jnpr_ether_init() function below for that).
  50. * Tasks:
  51. * - Create a new plugin struct
  52. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  53. * actually in the header.
  54. * - Add the plugin to the context's plugin chain
  55. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  56. */
  57. int
  58. dlt_jnpr_ether_register(tcpeditdlt_t *ctx)
  59. {
  60. tcpeditdlt_plugin_t *plugin;
  61. assert(ctx);
  62. /* create a new plugin structure */
  63. plugin = tcpedit_dlt_newplugin();
  64. plugin->provides += PLUGIN_MASK_PROTO + PLUGIN_MASK_SRCADDR + PLUGIN_MASK_DSTADDR;
  65. plugin->requires = 0;
  66. /* what is our DLT value? */
  67. plugin->dlt = dlt_value;
  68. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  69. plugin->name = safe_strdup(dlt_name);
  70. /*
  71. * Point to our functions, note, you need a function for EVERY method.
  72. * Even if it is only an empty stub returning success.
  73. */
  74. plugin->plugin_init = dlt_jnpr_ether_init;
  75. plugin->plugin_post_init = dlt_jnpr_ether_post_init;
  76. plugin->plugin_cleanup = dlt_jnpr_ether_cleanup;
  77. plugin->plugin_parse_opts = dlt_jnpr_ether_parse_opts;
  78. plugin->plugin_decode = dlt_jnpr_ether_decode;
  79. plugin->plugin_encode = dlt_jnpr_ether_encode;
  80. plugin->plugin_proto = dlt_jnpr_ether_proto;
  81. plugin->plugin_l2addr_type = dlt_jnpr_ether_l2addr_type;
  82. plugin->plugin_l2len = dlt_jnpr_ether_l2len;
  83. plugin->plugin_get_layer3 = dlt_jnpr_ether_get_layer3;
  84. plugin->plugin_merge_layer3 = dlt_jnpr_ether_merge_layer3;
  85. plugin->plugin_get_mac = dlt_jnpr_ether_get_mac;
  86. /* add it to the available plugin list */
  87. return tcpedit_dlt_addplugin(ctx, plugin);
  88. }
  89. /*
  90. * Initializer function. This function is called only once, if and only iif
  91. * this plugin will be utilized. Remember, if you need to keep track of any state,
  92. * store it in your plugin->config, not a global!
  93. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  94. */
  95. int
  96. dlt_jnpr_ether_init(tcpeditdlt_t *ctx)
  97. {
  98. tcpeditdlt_plugin_t *plugin;
  99. assert(ctx);
  100. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  101. tcpedit_seterr(ctx->tcpedit, "Unable to initialize unregistered plugin %s", dlt_name);
  102. return TCPEDIT_ERROR;
  103. }
  104. /* allocate memory for our config data */
  105. if (sizeof(jnpr_ether_config_t) > 0)
  106. plugin->config = safe_malloc(sizeof(jnpr_ether_config_t));
  107. return TCPEDIT_OK; /* success */
  108. }
  109. /**
  110. * Post init function. This function is called only once after init() and parse_opts()
  111. * It basically allows decoders to properly initialize sub-plugins.
  112. */
  113. int
  114. dlt_jnpr_ether_post_init(tcpeditdlt_t *ctx)
  115. {
  116. jnpr_ether_config_t *config;
  117. /* do nothing if we're not the decoder */
  118. if (ctx->decoder->dlt != dlt_value)
  119. return TCPEDIT_OK;
  120. /* init our subcontext & decoder of en10mb */
  121. config = (jnpr_ether_config_t *)ctx->encoder->config;
  122. config->subctx = tcpedit_dlt_init(ctx->tcpedit, DLT_EN10MB);
  123. return TCPEDIT_OK;
  124. }
  125. /*
  126. * Since this is used in a library, we should manually clean up after ourselves
  127. * Unless you allocated some memory in dlt_jnpr_ether_init(), this is just an stub.
  128. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  129. */
  130. int
  131. dlt_jnpr_ether_cleanup(tcpeditdlt_t *ctx)
  132. {
  133. tcpeditdlt_plugin_t *plugin;
  134. jnpr_ether_config_t *config;
  135. assert(ctx);
  136. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  137. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  138. return TCPEDIT_ERROR;
  139. }
  140. if (ctx->decoded_extra != NULL) {
  141. safe_free(ctx->decoded_extra);
  142. ctx->decoded_extra = NULL;
  143. }
  144. if (plugin->config != NULL) {
  145. /* clean up the en10mb plugin */
  146. config = (jnpr_ether_config_t *)ctx->encoder->config;
  147. tcpedit_dlt_cleanup(config->subctx);
  148. plugin->config = NULL;
  149. }
  150. return TCPEDIT_OK; /* success */
  151. }
  152. /*
  153. * This is where you should define all your AutoGen AutoOpts option parsing.
  154. * Any user specified option should have it's bit turned on in the 'provides'
  155. * bit mask.
  156. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  157. */
  158. int
  159. dlt_jnpr_ether_parse_opts(tcpeditdlt_t *ctx)
  160. {
  161. assert(ctx);
  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_jnpr_ether_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  176. {
  177. int jnpr_header_len = 0;
  178. const u_char *ethernet = NULL;
  179. jnpr_ether_config_t *config;
  180. assert(ctx);
  181. assert(packet);
  182. /* MAGIC + Static fields + Extension Length */
  183. if (pktlen < JUNIPER_ETHER_HEADER_LEN)
  184. return TCPEDIT_ERROR;
  185. config = (jnpr_ether_config_t *)ctx->encoder->config;
  186. /* first, verify magic */
  187. if (memcmp(packet, JUNIPER_ETHER_MAGIC, JUNIPER_ETHER_MAGIC_LEN) != 0) {
  188. tcpedit_seterr(ctx->tcpedit, "Invalid magic 0x%02X%02X%02X",
  189. packet[0], packet[1], packet[2]);
  190. return TCPEDIT_ERROR;
  191. }
  192. /* next make sure the L2 header is present */
  193. if ((packet[JUNIPER_ETHER_OPTIONS_OFFSET] & JUNIPER_ETHER_L2PRESENT)
  194. != JUNIPER_ETHER_L2PRESENT) {
  195. tcpedit_seterr(ctx->tcpedit, "Frame is missing L2 Header: %x",
  196. packet[JUNIPER_ETHER_OPTIONS_OFFSET]);
  197. return TCPEDIT_ERROR;
  198. }
  199. /* then get the Juniper header length */
  200. memcpy(&jnpr_header_len, &packet[JUNIPER_ETHER_EXTLEN_OFFSET], 2);
  201. jnpr_header_len = ntohs(jnpr_header_len) + JUNIPER_ETHER_HEADER_LEN;
  202. dbgx(1, "jnpr header len: %d", jnpr_header_len);
  203. /* make sure the packet is big enough to find the Ethernet Header */
  204. if (pktlen < jnpr_header_len + TCPR_ETH_H) {
  205. tcpedit_seterr(ctx->tcpedit, "Frame is too short! %d < %d",
  206. pktlen, (jnpr_header_len + TCPR_ETH_H));
  207. return TCPEDIT_ERROR;
  208. }
  209. ctx->l2len = jnpr_header_len;
  210. /* jump to the appropriate offset */
  211. ethernet = packet + jnpr_header_len;
  212. /* let the en10mb plugin decode the rest */
  213. if (tcpedit_dlt_decode(config->subctx, ethernet, (pktlen - jnpr_header_len)) == TCPEDIT_ERROR)
  214. return TCPEDIT_ERROR;
  215. /* copy the subdecoder state to our encoder state */
  216. if (tcpedit_dlt_copy_decoder_state(ctx, config->subctx) == TCPEDIT_ERROR)
  217. return TCPEDIT_ERROR;
  218. return TCPEDIT_OK;
  219. }
  220. /*
  221. * Function to encode the layer 2 header back into the packet.
  222. * Returns: total packet len or TCPEDIT_ERROR
  223. */
  224. int
  225. dlt_jnpr_ether_encode(tcpeditdlt_t *ctx, u_char *packet, int pktlen, _U_ tcpr_dir_t dir)
  226. {
  227. assert(ctx);
  228. assert(packet);
  229. /* MAGIC + Static fields + Extension Length */
  230. if (pktlen < JUNIPER_ETHER_HEADER_LEN)
  231. return TCPEDIT_ERROR;
  232. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_JUNIPER_ETHER plugin does not support packet encoding");
  233. return TCPEDIT_ERROR;
  234. }
  235. /*
  236. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  237. * Make sure you return this value in NETWORK byte order!
  238. */
  239. int
  240. dlt_jnpr_ether_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  241. {
  242. int jnpr_hdr_len;
  243. const u_char *ethernet;
  244. jnpr_ether_config_t *config;
  245. assert(ctx);
  246. assert(packet);
  247. /* MAGIC + Static fields + Extension Length */
  248. if (pktlen < JUNIPER_ETHER_HEADER_LEN)
  249. return TCPEDIT_ERROR;
  250. config = (jnpr_ether_config_t *)ctx->encoder->config;
  251. /* next make sure the L2 header is present */
  252. if ((packet[JUNIPER_ETHER_OPTIONS_OFFSET] & JUNIPER_ETHER_L2PRESENT)
  253. != JUNIPER_ETHER_L2PRESENT) {
  254. tcpedit_seterr(ctx->tcpedit, "Frame is missing L2 Header: %x",
  255. packet[JUNIPER_ETHER_OPTIONS_OFFSET]);
  256. return TCPEDIT_ERROR;
  257. }
  258. /* then get the Juniper header length */
  259. memcpy(&jnpr_hdr_len, &packet[JUNIPER_ETHER_EXTLEN_OFFSET], 2);
  260. jnpr_hdr_len = ntohs(jnpr_hdr_len) + JUNIPER_ETHER_HEADER_LEN;
  261. ethernet = packet + jnpr_hdr_len;
  262. /* let the en10mb plugin do the rest of the work */
  263. return tcpedit_dlt_proto(config->subctx, DLT_EN10MB, ethernet, (pktlen - jnpr_hdr_len));
  264. }
  265. /*
  266. * Function returns a pointer to the layer 3 protocol header or NULL on error
  267. */
  268. u_char *
  269. dlt_jnpr_ether_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  270. {
  271. int l2len;
  272. assert(ctx);
  273. assert(packet);
  274. /* next make sure the L2 header is present */
  275. if ((packet[JUNIPER_ETHER_OPTIONS_OFFSET] & JUNIPER_ETHER_L2PRESENT)
  276. != JUNIPER_ETHER_L2PRESENT) {
  277. tcpedit_seterr(ctx->tcpedit, "Frame is missing L2 Header: %x",
  278. packet[JUNIPER_ETHER_OPTIONS_OFFSET]);
  279. return NULL;
  280. }
  281. l2len = dlt_jnpr_ether_l2len(ctx, packet, pktlen);
  282. if (pktlen < l2len)
  283. return NULL;
  284. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  285. }
  286. /*
  287. * function merges the packet (containing L2 and old L3) with the l3data buffer
  288. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  289. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  290. * like SPARC
  291. */
  292. u_char *
  293. dlt_jnpr_ether_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  294. {
  295. int l2len;
  296. assert(ctx);
  297. assert(packet);
  298. assert(l3data);
  299. l2len = dlt_jnpr_ether_l2len(ctx, packet, pktlen);
  300. if (pktlen < l2len)
  301. return NULL;
  302. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  303. }
  304. /*
  305. * return a static pointer to the source/destination MAC address
  306. * return NULL on error/address doesn't exist
  307. */
  308. u_char *
  309. dlt_jnpr_ether_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  310. {
  311. const u_char *ethernet = NULL;
  312. jnpr_ether_config_t *config;
  313. int jnpr_hdr_len = 0;
  314. assert(ctx);
  315. assert(packet);
  316. if (pktlen < JUNIPER_ETHER_EXTLEN_OFFSET + 2)
  317. return NULL;
  318. config = (jnpr_ether_config_t *)ctx->encoder->config;
  319. /* first get the Juniper header length */
  320. memcpy(&jnpr_hdr_len, &packet[JUNIPER_ETHER_EXTLEN_OFFSET], 2);
  321. jnpr_hdr_len = ntohs(jnpr_hdr_len) + JUNIPER_ETHER_HEADER_LEN;
  322. ethernet = packet + jnpr_hdr_len;
  323. return dlt_en10mb_get_mac(config->subctx, mac, ethernet, (pktlen - jnpr_hdr_len));
  324. }
  325. /*
  326. * return the length of the L2 header of the current packet
  327. */
  328. int
  329. dlt_jnpr_ether_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  330. {
  331. uint16_t len;
  332. jnpr_ether_config_t *config;
  333. assert(ctx);
  334. assert(packet);
  335. if (pktlen < JUNIPER_ETHER_EXTLEN_OFFSET + 2)
  336. return 0;
  337. config = (jnpr_ether_config_t *)ctx->encoder->config;
  338. /* first get the Juniper header length */
  339. memcpy(&len, &packet[JUNIPER_ETHER_EXTLEN_OFFSET], 2);
  340. len = ntohs(len) + JUNIPER_ETHER_HEADER_LEN;
  341. dbgx(3, "juniper header len: %u", len);
  342. /* add the 802.3 length */
  343. len += tcpedit_dlt_l2len(config->subctx, DLT_EN10MB, (packet + len), (pktlen - len));
  344. dbgx(3, "total l2len: %u", len);
  345. /* and return that */
  346. return len;
  347. }
  348. tcpeditdlt_l2addr_type_t
  349. dlt_jnpr_ether_l2addr_type(void)
  350. {
  351. return ETHERNET;
  352. }