jnpr_ether.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 "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 if
  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. plugin->config_size = sizeof(jnpr_ether_config_t);
  106. plugin->config = safe_malloc(plugin->config_size);
  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. assert(ctx);
  135. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  136. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  137. return TCPEDIT_ERROR;
  138. }
  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. /* clean up the en10mb plugin */
  146. jnpr_ether_config_t * config;
  147. config = (jnpr_ether_config_t *)ctx->encoder->config;
  148. tcpedit_dlt_cleanup(config->subctx);
  149. plugin->config = NULL;
  150. plugin->config_size = 0;
  151. }
  152. return TCPEDIT_OK; /* success */
  153. }
  154. /*
  155. * This is where you should define all your AutoGen AutoOpts option parsing.
  156. * Any user specified option should have it's bit turned on in the 'provides'
  157. * bit mask.
  158. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  159. */
  160. int
  161. dlt_jnpr_ether_parse_opts(tcpeditdlt_t *ctx)
  162. {
  163. assert(ctx);
  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_jnpr_ether_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  178. {
  179. int jnpr_header_len = 0;
  180. const u_char *ethernet = NULL;
  181. jnpr_ether_config_t *config;
  182. assert(ctx);
  183. assert(packet);
  184. /* MAGIC + Static fields + Extension Length */
  185. if (pktlen < JUNIPER_ETHER_HEADER_LEN)
  186. return TCPEDIT_ERROR;
  187. config = (jnpr_ether_config_t *)ctx->encoder->config;
  188. /* first, verify magic */
  189. if (memcmp(packet, JUNIPER_ETHER_MAGIC, JUNIPER_ETHER_MAGIC_LEN) != 0) {
  190. tcpedit_seterr(ctx->tcpedit, "Invalid magic 0x%02X%02X%02X",
  191. packet[0], packet[1], packet[2]);
  192. return TCPEDIT_ERROR;
  193. }
  194. /* next make sure the L2 header is present */
  195. if ((packet[JUNIPER_ETHER_OPTIONS_OFFSET] & JUNIPER_ETHER_L2PRESENT)
  196. != JUNIPER_ETHER_L2PRESENT) {
  197. tcpedit_seterr(ctx->tcpedit, "Frame is missing L2 Header: %x",
  198. packet[JUNIPER_ETHER_OPTIONS_OFFSET]);
  199. return TCPEDIT_ERROR;
  200. }
  201. /* then get the Juniper header length */
  202. memcpy(&jnpr_header_len, &packet[JUNIPER_ETHER_EXTLEN_OFFSET], 2);
  203. jnpr_header_len = ntohs(jnpr_header_len) + JUNIPER_ETHER_HEADER_LEN;
  204. dbgx(1, "jnpr header len: %d", jnpr_header_len);
  205. /* make sure the packet is big enough to find the Ethernet Header */
  206. if (pktlen < jnpr_header_len + TCPR_ETH_H) {
  207. tcpedit_seterr(ctx->tcpedit, "Frame is too short! %d < %d",
  208. pktlen, (jnpr_header_len + TCPR_ETH_H));
  209. return TCPEDIT_ERROR;
  210. }
  211. ctx->l2len = jnpr_header_len;
  212. /* jump to the appropriate offset */
  213. ethernet = packet + jnpr_header_len;
  214. /* let the en10mb plugin decode the rest */
  215. if (tcpedit_dlt_decode(config->subctx, ethernet, (pktlen - jnpr_header_len)) == TCPEDIT_ERROR)
  216. return TCPEDIT_ERROR;
  217. /* copy the subdecoder state to our encoder state */
  218. if (tcpedit_dlt_copy_decoder_state(ctx, config->subctx) == TCPEDIT_ERROR)
  219. return TCPEDIT_ERROR;
  220. return TCPEDIT_OK;
  221. }
  222. /*
  223. * Function to encode the layer 2 header back into the packet.
  224. * Returns: total packet len or TCPEDIT_ERROR
  225. */
  226. int
  227. dlt_jnpr_ether_encode(tcpeditdlt_t *ctx, u_char *packet, int pktlen, _U_ tcpr_dir_t dir)
  228. {
  229. assert(ctx);
  230. assert(packet);
  231. /* MAGIC + Static fields + Extension Length */
  232. if (pktlen < JUNIPER_ETHER_HEADER_LEN)
  233. return TCPEDIT_ERROR;
  234. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_JUNIPER_ETHER plugin does not support packet encoding");
  235. return TCPEDIT_ERROR;
  236. }
  237. /*
  238. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  239. * Make sure you return this value in NETWORK byte order!
  240. */
  241. int
  242. dlt_jnpr_ether_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  243. {
  244. int jnpr_hdr_len;
  245. const u_char *ethernet;
  246. jnpr_ether_config_t *config;
  247. assert(ctx);
  248. assert(packet);
  249. /* MAGIC + Static fields + Extension Length */
  250. if (pktlen < JUNIPER_ETHER_HEADER_LEN)
  251. return TCPEDIT_ERROR;
  252. config = (jnpr_ether_config_t *)ctx->encoder->config;
  253. /* next make sure the L2 header is present */
  254. if ((packet[JUNIPER_ETHER_OPTIONS_OFFSET] & JUNIPER_ETHER_L2PRESENT)
  255. != JUNIPER_ETHER_L2PRESENT) {
  256. tcpedit_seterr(ctx->tcpedit, "Frame is missing L2 Header: %x",
  257. packet[JUNIPER_ETHER_OPTIONS_OFFSET]);
  258. return TCPEDIT_ERROR;
  259. }
  260. /* then get the Juniper header length */
  261. memcpy(&jnpr_hdr_len, &packet[JUNIPER_ETHER_EXTLEN_OFFSET], 2);
  262. jnpr_hdr_len = ntohs(jnpr_hdr_len) + JUNIPER_ETHER_HEADER_LEN;
  263. ethernet = packet + jnpr_hdr_len;
  264. /* let the en10mb plugin do the rest of the work */
  265. return tcpedit_dlt_proto(config->subctx, DLT_EN10MB, ethernet, (pktlen - jnpr_hdr_len));
  266. }
  267. /*
  268. * Function returns a pointer to the layer 3 protocol header or NULL on error
  269. */
  270. u_char *
  271. dlt_jnpr_ether_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  272. {
  273. int l2len;
  274. assert(ctx);
  275. assert(packet);
  276. /* next make sure the L2 header is present */
  277. if ((packet[JUNIPER_ETHER_OPTIONS_OFFSET] & JUNIPER_ETHER_L2PRESENT)
  278. != JUNIPER_ETHER_L2PRESENT) {
  279. tcpedit_seterr(ctx->tcpedit, "Frame is missing L2 Header: %x",
  280. packet[JUNIPER_ETHER_OPTIONS_OFFSET]);
  281. return NULL;
  282. }
  283. l2len = dlt_jnpr_ether_l2len(ctx, packet, pktlen);
  284. if (l2len == -1 || pktlen < l2len)
  285. return NULL;
  286. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  287. }
  288. /*
  289. * function merges the packet (containing L2 and old L3) with the l3data buffer
  290. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  291. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  292. * like SPARC
  293. */
  294. u_char *
  295. dlt_jnpr_ether_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  296. {
  297. int l2len;
  298. assert(ctx);
  299. assert(packet);
  300. assert(l3data);
  301. l2len = dlt_jnpr_ether_l2len(ctx, packet, pktlen);
  302. if (l2len == -1 || pktlen < l2len)
  303. return NULL;
  304. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  305. }
  306. /*
  307. * return a static pointer to the source/destination MAC address
  308. * return NULL on error/address doesn't exist
  309. */
  310. u_char *
  311. dlt_jnpr_ether_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  312. {
  313. const u_char *ethernet = NULL;
  314. jnpr_ether_config_t *config;
  315. int jnpr_hdr_len = 0;
  316. assert(ctx);
  317. assert(packet);
  318. if (pktlen < JUNIPER_ETHER_EXTLEN_OFFSET + 2)
  319. return NULL;
  320. config = (jnpr_ether_config_t *)ctx->encoder->config;
  321. /* first get the Juniper header length */
  322. memcpy(&jnpr_hdr_len, &packet[JUNIPER_ETHER_EXTLEN_OFFSET], 2);
  323. jnpr_hdr_len = ntohs(jnpr_hdr_len) + JUNIPER_ETHER_HEADER_LEN;
  324. ethernet = packet + jnpr_hdr_len;
  325. return dlt_en10mb_get_mac(config->subctx, mac, ethernet, (pktlen - jnpr_hdr_len));
  326. }
  327. /*
  328. * return the length of the L2 header of the current packet
  329. */
  330. int
  331. dlt_jnpr_ether_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  332. {
  333. int len, res;
  334. jnpr_ether_config_t *config;
  335. assert(ctx);
  336. assert(packet);
  337. if (pktlen < JUNIPER_ETHER_EXTLEN_OFFSET + 2)
  338. return -1;
  339. config = (jnpr_ether_config_t *)ctx->encoder->config;
  340. /* first get the Juniper header length */
  341. memcpy(&len, &packet[JUNIPER_ETHER_EXTLEN_OFFSET], 2);
  342. len = ntohs(len) + JUNIPER_ETHER_HEADER_LEN;
  343. dbgx(3, "juniper header len: %u", len);
  344. /* add the 802.3 length */
  345. res = tcpedit_dlt_l2len(config->subctx, DLT_EN10MB, (packet + len), (pktlen - len));
  346. if (res == -1)
  347. return TCPEDIT_ERROR;
  348. len += res;
  349. dbgx(3, "total l2len: %u", len);
  350. /* and return that */
  351. return len;
  352. }
  353. tcpeditdlt_l2addr_type_t
  354. dlt_jnpr_ether_l2addr_type(void)
  355. {
  356. return ETHERNET;
  357. }