jnpr_ether.c 14 KB

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