jnpr_ether.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2006-2007 Aaron Turner.
  4. * Copyright (c) 2013-2026 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 extensions_len = 0;
  177. int jnpr_header_len = 0;
  178. const u_char *ethernet = NULL;
  179. const u_char *extension;
  180. u_char dlt = 0;
  181. u_char encapsulation = 0;
  182. jnpr_ether_config_t *config;
  183. assert(ctx);
  184. assert(packet);
  185. /* MAGIC + Static fields + Extension Length */
  186. if (pktlen < JUNIPER_ETHER_HEADER_LEN)
  187. return TCPEDIT_ERROR;
  188. config = (jnpr_ether_config_t *)ctx->encoder->config;
  189. /* first, verify magic */
  190. if (memcmp(packet, JUNIPER_ETHER_MAGIC, JUNIPER_ETHER_MAGIC_LEN) != 0) {
  191. tcpedit_seterr(ctx->tcpedit, "Invalid magic 0x%02X%02X%02X", 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) != JUNIPER_ETHER_L2PRESENT) {
  196. tcpedit_seterr(ctx->tcpedit, "Frame is missing L2 Header: %x", packet[JUNIPER_ETHER_OPTIONS_OFFSET]);
  197. return TCPEDIT_ERROR;
  198. }
  199. /* then get the Juniper header length */
  200. memcpy(&extensions_len, &packet[JUNIPER_ETHER_EXTLEN_OFFSET], 2);
  201. extensions_len = ntohs(extensions_len);
  202. jnpr_header_len = extensions_len + JUNIPER_ETHER_HEADER_LEN;
  203. dbgx(1, "jnpr header len: %d", jnpr_header_len);
  204. /* make sure the packet is big enough to find the Ethernet Header */
  205. if (pktlen < jnpr_header_len + TCPR_ETH_H) {
  206. tcpedit_seterr(ctx->tcpedit, "Frame is too short! %d < %d", 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. /* parse the extension header to ensure this is Ethernet - the only DLT we currently support */
  213. extension = packet + JUNIPER_ETHER_HEADER_LEN;
  214. while (extension < ethernet - 2) {
  215. u_char ext_len = extension[1];
  216. if (extension[0] == JUNIPER_ETHER_EXT_MEDIA_TYPE)
  217. dlt = extension[2];
  218. else if (extension[0] == JUNIPER_ETHER_EXT_ENCAPSULATION)
  219. encapsulation = extension[2];
  220. if (dlt != 0 && encapsulation != 0)
  221. break;
  222. extension += ext_len + 2;
  223. }
  224. if (extension > ethernet) {
  225. tcpedit_seterr(ctx->tcpedit, "Extension to long! %d", extension - ethernet);
  226. return TCPEDIT_ERROR;
  227. }
  228. if (dlt != DLT_EN10MB || encapsulation != 14) {
  229. tcpedit_setwarn(ctx->tcpedit, "packet DLT %d and encapsulation type %u not supported",
  230. dlt, encapsulation);
  231. return TCPEDIT_WARN;
  232. }
  233. /* let the en10mb plugin decode the rest */
  234. if (tcpedit_dlt_decode(config->subctx, ethernet, (pktlen - jnpr_header_len)) == TCPEDIT_ERROR)
  235. return TCPEDIT_ERROR;
  236. /* copy the subdecoder state to our encoder state */
  237. if (tcpedit_dlt_copy_decoder_state(ctx, config->subctx) == TCPEDIT_ERROR)
  238. return TCPEDIT_ERROR;
  239. return TCPEDIT_OK;
  240. }
  241. /*
  242. * Function to encode the layer 2 header back into the packet.
  243. * Returns: total packet len or TCPEDIT_ERROR
  244. */
  245. int
  246. dlt_jnpr_ether_encode(tcpeditdlt_t *ctx, u_char *packet, int pktlen, _U_ tcpr_dir_t dir)
  247. {
  248. assert(ctx);
  249. assert(packet);
  250. /* MAGIC + Static fields + Extension Length */
  251. if (pktlen < JUNIPER_ETHER_HEADER_LEN)
  252. return TCPEDIT_ERROR;
  253. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_JUNIPER_ETHER plugin does not support packet encoding");
  254. return TCPEDIT_ERROR;
  255. }
  256. /*
  257. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  258. * Make sure you return this value in NETWORK byte order!
  259. */
  260. int
  261. dlt_jnpr_ether_proto(tcpeditdlt_t *ctx, const u_char *packet, int pktlen)
  262. {
  263. int jnpr_hdr_len;
  264. const u_char *ethernet;
  265. jnpr_ether_config_t *config;
  266. assert(ctx);
  267. assert(packet);
  268. /* MAGIC + Static fields + Extension Length */
  269. if (pktlen < JUNIPER_ETHER_HEADER_LEN)
  270. return TCPEDIT_ERROR;
  271. config = (jnpr_ether_config_t *)ctx->encoder->config;
  272. /* next make sure the L2 header is present */
  273. if ((packet[JUNIPER_ETHER_OPTIONS_OFFSET] & JUNIPER_ETHER_L2PRESENT) != JUNIPER_ETHER_L2PRESENT) {
  274. tcpedit_seterr(ctx->tcpedit, "Frame is missing L2 Header: %x", packet[JUNIPER_ETHER_OPTIONS_OFFSET]);
  275. return TCPEDIT_ERROR;
  276. }
  277. /* then get the Juniper header length */
  278. memcpy(&jnpr_hdr_len, &packet[JUNIPER_ETHER_EXTLEN_OFFSET], 2);
  279. jnpr_hdr_len = ntohs(jnpr_hdr_len) + JUNIPER_ETHER_HEADER_LEN;
  280. if (jnpr_hdr_len > pktlen) {
  281. tcpedit_seterr(ctx->tcpedit,
  282. "Juniper header length %d invalid: it is greater than packet length %d",
  283. jnpr_hdr_len, pktlen);
  284. return TCPEDIT_ERROR;
  285. }
  286. ethernet = packet + jnpr_hdr_len;
  287. /* let the en10mb plugin do the rest of the work */
  288. return tcpedit_dlt_proto(config->subctx, DLT_EN10MB, ethernet, (pktlen - jnpr_hdr_len));
  289. }
  290. /*
  291. * Function returns a pointer to the layer 3 protocol header or NULL on error
  292. */
  293. u_char *
  294. dlt_jnpr_ether_get_layer3(tcpeditdlt_t *ctx, u_char *packet, int pktlen)
  295. {
  296. int l2len;
  297. assert(ctx);
  298. assert(packet);
  299. /* next make sure the L2 header is present */
  300. if ((packet[JUNIPER_ETHER_OPTIONS_OFFSET] & JUNIPER_ETHER_L2PRESENT) != JUNIPER_ETHER_L2PRESENT) {
  301. tcpedit_seterr(ctx->tcpedit, "Frame is missing L2 Header: %x", packet[JUNIPER_ETHER_OPTIONS_OFFSET]);
  302. return NULL;
  303. }
  304. l2len = dlt_jnpr_ether_l2len(ctx, packet, pktlen);
  305. if (l2len == -1 || pktlen < l2len)
  306. return NULL;
  307. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  308. }
  309. /*
  310. * function merges the packet (containing L2 and old L3) with the l3data buffer
  311. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  312. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  313. * like SPARC
  314. */
  315. u_char *
  316. dlt_jnpr_ether_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, int pktlen, u_char *ipv4_data, u_char *ipv6_data)
  317. {
  318. int l2len;
  319. assert(ctx);
  320. assert(packet);
  321. assert(ipv4_data || ipv6_data);
  322. l2len = dlt_jnpr_ether_l2len(ctx, packet, pktlen);
  323. if (l2len == -1 || pktlen < l2len)
  324. return NULL;
  325. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, ipv4_data ?: ipv6_data, l2len);
  326. }
  327. /*
  328. * return a static pointer to the source/destination MAC address
  329. * return NULL on error/address doesn't exist
  330. */
  331. u_char *
  332. dlt_jnpr_ether_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, int pktlen)
  333. {
  334. const u_char *ethernet = NULL;
  335. jnpr_ether_config_t *config;
  336. int jnpr_hdr_len = 0;
  337. assert(ctx);
  338. assert(packet);
  339. if (pktlen < JUNIPER_ETHER_EXTLEN_OFFSET + 2)
  340. return NULL;
  341. config = (jnpr_ether_config_t *)ctx->encoder->config;
  342. /* first get the Juniper header length */
  343. memcpy(&jnpr_hdr_len, &packet[JUNIPER_ETHER_EXTLEN_OFFSET], 2);
  344. jnpr_hdr_len = ntohs(jnpr_hdr_len) + JUNIPER_ETHER_HEADER_LEN;
  345. ethernet = packet + jnpr_hdr_len;
  346. return dlt_en10mb_get_mac(config->subctx, mac, ethernet, (pktlen - jnpr_hdr_len));
  347. }
  348. /*
  349. * return the length of the L2 header of the current packet
  350. */
  351. int
  352. dlt_jnpr_ether_l2len(tcpeditdlt_t *ctx, const u_char *packet, int pktlen)
  353. {
  354. int len, res;
  355. jnpr_ether_config_t *config;
  356. assert(ctx);
  357. assert(packet);
  358. if (pktlen < JUNIPER_ETHER_EXTLEN_OFFSET + 2)
  359. return -1;
  360. config = (jnpr_ether_config_t *)ctx->encoder->config;
  361. /* first get the Juniper header length */
  362. memcpy(&len, &packet[JUNIPER_ETHER_EXTLEN_OFFSET], 2);
  363. len = ntohs(len) + JUNIPER_ETHER_HEADER_LEN;
  364. dbgx(3, "juniper header len: %u", len);
  365. /* add the 802.3 length */
  366. res = tcpedit_dlt_l2len(config->subctx, DLT_EN10MB, (packet + len), (pktlen - len));
  367. if (res == -1)
  368. return TCPEDIT_ERROR;
  369. len += res;
  370. dbgx(3, "total l2len: %u", len);
  371. /* and return that */
  372. return len;
  373. }
  374. tcpeditdlt_l2addr_type_t
  375. dlt_jnpr_ether_l2addr_type(void)
  376. {
  377. return ETHERNET;
  378. }