jnpr_ether.c 13 KB

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