en10mb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "tcpedit.h"
  22. #include "common.h"
  23. #include "tcpr.h"
  24. #include "dlt_utils.h"
  25. #include "tcpedit_stub.h"
  26. #include "../ethernet.h"
  27. #include "en10mb.h"
  28. static char _U_ dlt_name[] = "en10mb";
  29. static char dlt_prefix[] = "enet";
  30. static uint16_t dlt_value = DLT_EN10MB;
  31. /*
  32. * Function to register ourselves. This function is always called, regardless
  33. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  34. * or anything like that (use the dlt_en10mb_init() function below for that).
  35. * Tasks:
  36. * - Create a new plugin struct
  37. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  38. * actually in the header.
  39. * - Add the plugin to the context's plugin chain
  40. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  41. */
  42. int
  43. dlt_en10mb_register(tcpeditdlt_t *ctx)
  44. {
  45. tcpeditdlt_plugin_t *plugin;
  46. assert(ctx);
  47. /* create a new plugin structure */
  48. plugin = tcpedit_dlt_newplugin();
  49. /* set what we provide & require */
  50. plugin->provides += PLUGIN_MASK_PROTO + PLUGIN_MASK_SRCADDR + PLUGIN_MASK_DSTADDR;
  51. plugin->requires += PLUGIN_MASK_PROTO + PLUGIN_MASK_SRCADDR + PLUGIN_MASK_DSTADDR;
  52. /* what is our dlt type? */
  53. plugin->dlt = dlt_value;
  54. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  55. plugin->name = safe_strdup(dlt_prefix);
  56. /*
  57. * Point to our functions, note, you need a function for EVERY method.
  58. * Even if it is only an empty stub returning success.
  59. */
  60. plugin->plugin_init = dlt_en10mb_init;
  61. plugin->plugin_cleanup = dlt_en10mb_cleanup;
  62. plugin->plugin_parse_opts = dlt_en10mb_parse_opts;
  63. plugin->plugin_decode = dlt_en10mb_decode;
  64. plugin->plugin_encode = dlt_en10mb_encode;
  65. plugin->plugin_proto = dlt_en10mb_proto;
  66. plugin->plugin_l2addr_type = dlt_en10mb_l2addr_type;
  67. plugin->plugin_l2len = dlt_en10mb_l2len;
  68. plugin->plugin_get_layer3 = dlt_en10mb_get_layer3;
  69. plugin->plugin_merge_layer3 = dlt_en10mb_merge_layer3;
  70. plugin->plugin_get_mac = dlt_en10mb_get_mac;
  71. /* add it to the available plugin list */
  72. return tcpedit_dlt_addplugin(ctx, plugin);
  73. }
  74. /*
  75. * Initializer function. This function is called only once, if and only iif
  76. * this plugin will be utilized. Remember, if you need to keep track of any state,
  77. * store it in your plugin->config, not a global!
  78. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  79. */
  80. int
  81. dlt_en10mb_init(tcpeditdlt_t *ctx)
  82. {
  83. tcpeditdlt_plugin_t *plugin;
  84. en10mb_config_t *config;
  85. assert(ctx);
  86. /* vlan tags need an additional 4 bytes */
  87. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  88. tcpedit_seterr(ctx->tcpedit, "%s", "Unable to initialize unregistered plugin en10mb");
  89. return TCPEDIT_ERROR;
  90. }
  91. ctx->decoded_extra = safe_malloc(sizeof(en10mb_extra_t));
  92. plugin->config = safe_malloc(sizeof(en10mb_config_t));
  93. config = (en10mb_config_t *)plugin->config;
  94. /* init vlan user values to -1 to indicate not set */
  95. config->vlan_tag = 65535;
  96. config->vlan_pri = 255;
  97. config->vlan_cfi = 255;
  98. return TCPEDIT_OK; /* success */
  99. }
  100. /*
  101. * Since this is used in a library, we should manually clean up after ourselves
  102. * Unless you allocated some memory in dlt_en10mb_init(), this is just an stub.
  103. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  104. */
  105. int
  106. dlt_en10mb_cleanup(tcpeditdlt_t *ctx)
  107. {
  108. tcpeditdlt_plugin_t *plugin;
  109. assert(ctx);
  110. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL)
  111. return TCPEDIT_OK;
  112. if (ctx->decoded_extra != NULL) {
  113. safe_free(ctx->decoded_extra);
  114. ctx->decoded_extra = NULL;
  115. }
  116. if (plugin->config != NULL) {
  117. safe_free(plugin->config);
  118. plugin->config = NULL;
  119. }
  120. return TCPEDIT_OK; /* success */
  121. }
  122. int
  123. dlt_en10mb_parse_subsmac_entry(const char *raw, en10mb_sub_entry_t *entry)
  124. {
  125. char *candidate = safe_strdup(raw);
  126. int parse_result = dualmac2hex(candidate, entry->target, entry->rewrite, SUBSMAC_ENTRY_LEN);
  127. free(candidate);
  128. return parse_result;
  129. }
  130. en10mb_sub_entry_t *
  131. dlt_en10mb_realloc_merge(en10mb_sub_conf_t config, en10mb_sub_entry_t *new_entries, int entries_count)
  132. {
  133. int i;
  134. en10mb_sub_entry_t *merged = safe_realloc(
  135. config.entries, (config.count + entries_count) * sizeof(en10mb_sub_entry_t));
  136. for (i = 0; i < entries_count; i++) {
  137. merged[config.count + i] = new_entries[i];
  138. }
  139. return merged;
  140. }
  141. int
  142. dlt_en10mb_parse_subsmac(tcpeditdlt_t *ctx, en10mb_config_t *config, const char *input)
  143. {
  144. int input_len = strlen(input);
  145. int possible_entries_number = (input_len / (SUBSMAC_ENTRY_LEN + 1)) + 1;
  146. int entry = 0;
  147. en10mb_sub_entry_t *entries = safe_malloc(possible_entries_number * sizeof(en10mb_sub_entry_t));
  148. for (entry = 0; entry < possible_entries_number; entry++) {
  149. const int read_offset = entry + entry * SUBSMAC_ENTRY_LEN;
  150. if (input_len - read_offset < SUBSMAC_ENTRY_LEN) {
  151. free(entries);
  152. tcpedit_seterr(ctx->tcpedit, "Unable to parse --enet-subsmac=%s", input);
  153. return TCPEDIT_ERROR;
  154. }
  155. switch(dlt_en10mb_parse_subsmac_entry(input + read_offset, &entries[entry])) {
  156. case 3:
  157. /* Both read; This is what we want */
  158. break;
  159. default:
  160. free(entries);
  161. tcpedit_seterr(ctx->tcpedit, "Unable to parse --enet-subsmac=%s", input);
  162. return TCPEDIT_ERROR;
  163. }
  164. }
  165. config->subs.entries = dlt_en10mb_realloc_merge(config->subs, entries, possible_entries_number);
  166. config->subs.count += possible_entries_number;
  167. free(entries);
  168. return TCPEDIT_OK;
  169. }
  170. /*
  171. * This is where you should define all your AutoGen AutoOpts option parsing.
  172. * Any user specified option should have it's bit turned on in the 'provides'
  173. * bit mask.
  174. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  175. */
  176. int
  177. dlt_en10mb_parse_opts(tcpeditdlt_t *ctx)
  178. {
  179. tcpeditdlt_plugin_t *plugin;
  180. en10mb_config_t *config;
  181. assert(ctx);
  182. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  183. config = (en10mb_config_t *)plugin->config;
  184. /* --subsmacs */
  185. if (HAVE_OPT(ENET_SUBSMAC)) {
  186. int i, count = STACKCT_OPT(ENET_SUBSMAC);
  187. char **list = (char**) STACKLST_OPT(ENET_SUBSMAC);
  188. for (i = 0; i < count; i++) {
  189. int parse_result = dlt_en10mb_parse_subsmac(ctx, config, list[i]);
  190. if (parse_result == TCPEDIT_ERROR) {
  191. return TCPEDIT_ERROR;
  192. }
  193. }
  194. }
  195. /* --mac-seed */
  196. if (HAVE_OPT(ENET_MAC_SEED)) {
  197. int i,j;
  198. config->random.set = OPT_VALUE_ENET_MAC_SEED;
  199. for (i = 0; i < 6; i++) {
  200. config->random.mask[i] = (u_char)tcpr_random(&config->random.set) % 256;
  201. /* only unique numbers */
  202. for (j = 0; j < i; j++) {
  203. if (config->random.mask[i] == config->random.mask[j]) {
  204. i--;
  205. break;
  206. }
  207. }
  208. }
  209. if (HAVE_OPT(ENET_MAC_SEED_KEEP_BYTES)) {
  210. config->random.keep = OPT_VALUE_ENET_MAC_SEED_KEEP_BYTES;
  211. }
  212. }
  213. /* --dmac */
  214. if (HAVE_OPT(ENET_DMAC)) {
  215. int macparse;
  216. macparse = dualmac2hex(OPT_ARG(ENET_DMAC), config->intf1_dmac,
  217. config->intf2_dmac, strlen(OPT_ARG(ENET_DMAC)));
  218. switch (macparse) {
  219. case 1:
  220. config->mac_mask += TCPEDIT_MAC_MASK_DMAC1;
  221. break;
  222. case 2:
  223. config->mac_mask += TCPEDIT_MAC_MASK_DMAC2;
  224. break;
  225. case 3:
  226. config->mac_mask += TCPEDIT_MAC_MASK_DMAC1;
  227. config->mac_mask += TCPEDIT_MAC_MASK_DMAC2;
  228. break;
  229. case 0:
  230. /* nothing to do */
  231. break;
  232. default:
  233. tcpedit_seterr(ctx->tcpedit,
  234. "Unable to parse --enet-dmac=%s", OPT_ARG(ENET_DMAC));
  235. return TCPEDIT_ERROR;
  236. break;
  237. }
  238. plugin->requires -= PLUGIN_MASK_DSTADDR;
  239. }
  240. /* --smac */
  241. if (HAVE_OPT(ENET_SMAC)) {
  242. int macparse;
  243. macparse = dualmac2hex(OPT_ARG(ENET_SMAC), config->intf1_smac,
  244. config->intf2_smac, strlen(OPT_ARG(ENET_SMAC)));
  245. switch (macparse) {
  246. case 1:
  247. config->mac_mask += TCPEDIT_MAC_MASK_SMAC1;
  248. break;
  249. case 2:
  250. config->mac_mask += TCPEDIT_MAC_MASK_SMAC2;
  251. break;
  252. case 3:
  253. config->mac_mask += TCPEDIT_MAC_MASK_SMAC1;
  254. config->mac_mask += TCPEDIT_MAC_MASK_SMAC2;
  255. break;
  256. case 0:
  257. /* nothing to do */
  258. break;
  259. default:
  260. tcpedit_seterr(ctx->tcpedit,
  261. "Unable to parse --enet-smac=%s", OPT_ARG(ENET_SMAC));
  262. return TCPEDIT_ERROR;
  263. break;
  264. }
  265. plugin->requires -= PLUGIN_MASK_SRCADDR;
  266. }
  267. /*
  268. * Validate 802.1q vlan args and populate tcpedit->vlan_record
  269. */
  270. if (HAVE_OPT(ENET_VLAN)) {
  271. if (strcmp(OPT_ARG(ENET_VLAN), "add") == 0) { // add or change
  272. config->vlan = TCPEDIT_VLAN_ADD;
  273. } else if (strcmp(OPT_ARG(ENET_VLAN), "del") == 0) {
  274. config->vlan = TCPEDIT_VLAN_DEL;
  275. } else {
  276. tcpedit_seterr(ctx->tcpedit, "Invalid --enet-vlan=%s", OPT_ARG(ENET_VLAN));
  277. return -1;
  278. }
  279. if (config->vlan != TCPEDIT_VLAN_OFF) {
  280. if (config->vlan == TCPEDIT_VLAN_ADD) {
  281. if (! HAVE_OPT(ENET_VLAN_TAG)) {
  282. tcpedit_seterr(ctx->tcpedit, "%s",
  283. "Must specify a new 802.1 VLAN tag if vlan "
  284. "mode is add");
  285. return TCPEDIT_ERROR;
  286. }
  287. /*
  288. * fill out the 802.1q header
  289. */
  290. config->vlan_tag = OPT_VALUE_ENET_VLAN_TAG;
  291. dbgx(1, "We will %s 802.1q headers",
  292. config->vlan == TCPEDIT_VLAN_DEL ? "delete" : "add/modify");
  293. if (HAVE_OPT(ENET_VLAN_PRI))
  294. config->vlan_pri = OPT_VALUE_ENET_VLAN_PRI;
  295. if (HAVE_OPT(ENET_VLAN_CFI))
  296. config->vlan_cfi = OPT_VALUE_ENET_VLAN_CFI;
  297. }
  298. }
  299. }
  300. return TCPEDIT_OK; /* success */
  301. }
  302. /*
  303. * Function to decode the layer 2 header in the packet
  304. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  305. */
  306. int
  307. dlt_en10mb_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  308. {
  309. struct tcpr_ethernet_hdr *eth = NULL;
  310. struct tcpr_802_1q_hdr *vlan = NULL;
  311. en10mb_extra_t *extra = NULL;
  312. assert(ctx);
  313. assert(packet);
  314. if (pktlen < 14)
  315. return TCPEDIT_ERROR;
  316. /* get our src & dst address */
  317. eth = (struct tcpr_ethernet_hdr *)packet;
  318. memcpy(&(ctx->dstaddr.ethernet), eth, ETHER_ADDR_LEN);
  319. memcpy(&(ctx->srcaddr.ethernet), &(eth->ether_shost), ETHER_ADDR_LEN);
  320. extra = (en10mb_extra_t *)ctx->decoded_extra;
  321. extra->vlan = 0;
  322. /* get the L3 protocol type & L2 len*/
  323. switch (ntohs(eth->ether_type)) {
  324. case ETHERTYPE_VLAN:
  325. vlan = (struct tcpr_802_1q_hdr *)packet;
  326. ctx->proto = vlan->vlan_len;
  327. /* Get VLAN tag info */
  328. extra->vlan = 1;
  329. /* must use these mask values, rather then what's in the tcpr.h since it assumes you're shifting */
  330. extra->vlan_tag = vlan->vlan_priority_c_vid & 0x0FFF;
  331. extra->vlan_pri = vlan->vlan_priority_c_vid & 0xE000;
  332. extra->vlan_cfi = vlan->vlan_priority_c_vid & 0x1000;
  333. ctx->l2len = TCPR_802_1Q_H;
  334. break;
  335. /* we don't properly handle SNAP encoding */
  336. default:
  337. ctx->proto = eth->ether_type;
  338. ctx->l2len = TCPR_802_3_H;
  339. break;
  340. }
  341. return TCPEDIT_OK; /* success */
  342. }
  343. /*
  344. * Function to encode the layer 2 header back into the packet.
  345. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  346. */
  347. int
  348. dlt_en10mb_encode(tcpeditdlt_t *ctx, u_char *packet, int pktlen, tcpr_dir_t dir)
  349. {
  350. tcpeditdlt_plugin_t *plugin = NULL;
  351. struct tcpr_ethernet_hdr *eth = NULL;
  352. struct tcpr_802_1q_hdr *vlan = NULL;
  353. en10mb_config_t *config = NULL;
  354. en10mb_extra_t *extra = NULL;
  355. int newl2len = 0;
  356. assert(ctx);
  357. assert(packet);
  358. if (pktlen < 14) {
  359. tcpedit_seterr(ctx->tcpedit,
  360. "Unable to process packet #" COUNTER_SPEC " since it is less then 14 bytes.",
  361. ctx->tcpedit->runtime.packetnum);
  362. return TCPEDIT_ERROR;
  363. }
  364. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  365. config = plugin->config;
  366. extra = (en10mb_extra_t *)ctx->decoded_extra;
  367. /* figure out the new layer2 length, first for the case: ethernet -> ethernet? */
  368. if (ctx->decoder->dlt == dlt_value) {
  369. if ((ctx->l2len == TCPR_802_1Q_H && config->vlan == TCPEDIT_VLAN_OFF) ||
  370. (config->vlan == TCPEDIT_VLAN_ADD)) {
  371. newl2len = TCPR_802_1Q_H;
  372. } else if ((ctx->l2len == TCPR_802_3_H && config->vlan == TCPEDIT_VLAN_OFF) ||
  373. (config->vlan == TCPEDIT_VLAN_DEL)) {
  374. newl2len = TCPR_802_3_H;
  375. }
  376. }
  377. /* newl2len for some other DLT -> ethernet */
  378. else {
  379. /* if add a vlan then 18, else 14 bytes */
  380. newl2len = config->vlan == TCPEDIT_VLAN_ADD ? TCPR_802_1Q_H : TCPR_802_3_H;
  381. }
  382. /* Make space for our new L2 header */
  383. if (newl2len != ctx->l2len)
  384. memmove(packet + newl2len, packet + ctx->l2len, pktlen - ctx->l2len);
  385. /* update the total packet length */
  386. pktlen += newl2len - ctx->l2len;
  387. /* always set the src & dst address as the first 12 bytes */
  388. eth = (struct tcpr_ethernet_hdr *)packet;
  389. if (dir == TCPR_DIR_C2S) {
  390. /* copy user supplied SRC MAC if provided or from original packet */
  391. if (config->mac_mask & TCPEDIT_MAC_MASK_SMAC1) {
  392. if ((ctx->addr_type == ETHERNET &&
  393. ((ctx->skip_broadcast &&
  394. is_unicast_ethernet(ctx, ctx->srcaddr.ethernet)) || !ctx->skip_broadcast))
  395. || ctx->addr_type != ETHERNET) {
  396. memcpy(eth->ether_shost, config->intf1_smac, ETHER_ADDR_LEN);
  397. } else {
  398. memcpy(eth->ether_shost, ctx->srcaddr.ethernet, ETHER_ADDR_LEN);
  399. }
  400. } else if (ctx->addr_type == ETHERNET) {
  401. memcpy(eth->ether_shost, ctx->srcaddr.ethernet, ETHER_ADDR_LEN);
  402. } else {
  403. tcpedit_seterr(ctx->tcpedit, "%s", "Please provide a source address");
  404. return TCPEDIT_ERROR;
  405. }
  406. /* copy user supplied DMAC MAC if provided or from original packet */
  407. if (config->mac_mask & TCPEDIT_MAC_MASK_DMAC1) {
  408. if ((ctx->addr_type == ETHERNET &&
  409. ((ctx->skip_broadcast && is_unicast_ethernet(ctx, ctx->dstaddr.ethernet)) || !ctx->skip_broadcast))
  410. || ctx->addr_type != ETHERNET) {
  411. memcpy(eth->ether_dhost, config->intf1_dmac, ETHER_ADDR_LEN);
  412. } else {
  413. memcpy(eth->ether_dhost, ctx->dstaddr.ethernet, ETHER_ADDR_LEN);
  414. }
  415. } else if (ctx->addr_type == ETHERNET) {
  416. memcpy(eth->ether_dhost, ctx->dstaddr.ethernet, ETHER_ADDR_LEN);
  417. } else {
  418. tcpedit_seterr(ctx->tcpedit, "%s", "Please provide a destination address");
  419. return TCPEDIT_ERROR;
  420. }
  421. } else if (dir == TCPR_DIR_S2C) {
  422. /* copy user supplied SRC MAC if provided or from original packet */
  423. if (config->mac_mask & TCPEDIT_MAC_MASK_SMAC2) {
  424. if ((ctx->addr_type == ETHERNET &&
  425. ((ctx->skip_broadcast && is_unicast_ethernet(ctx, ctx->srcaddr.ethernet)) || !ctx->skip_broadcast))
  426. || ctx->addr_type != ETHERNET) {
  427. memcpy(eth->ether_shost, config->intf2_smac, ETHER_ADDR_LEN);
  428. } else {
  429. memcpy(eth->ether_shost, ctx->srcaddr.ethernet, ETHER_ADDR_LEN);
  430. }
  431. } else if (ctx->addr_type == ETHERNET) {
  432. memcpy(eth->ether_shost, ctx->srcaddr.ethernet, ETHER_ADDR_LEN);
  433. } else {
  434. tcpedit_seterr(ctx->tcpedit, "%s", "Please provide a source address");
  435. return TCPEDIT_ERROR;
  436. }
  437. /* copy user supplied DMAC MAC if provided or from original packet */
  438. if (config->mac_mask & TCPEDIT_MAC_MASK_DMAC2) {
  439. if ((ctx->addr_type == ETHERNET &&
  440. ((ctx->skip_broadcast && is_unicast_ethernet(ctx, ctx->dstaddr.ethernet)) || !ctx->skip_broadcast))
  441. || ctx->addr_type != ETHERNET) {
  442. memcpy(eth->ether_dhost, config->intf2_dmac, ETHER_ADDR_LEN);
  443. } else {
  444. memcpy(eth->ether_dhost, ctx->dstaddr.ethernet, ETHER_ADDR_LEN);
  445. }
  446. } else if (ctx->addr_type == ETHERNET) {
  447. memcpy(eth->ether_dhost, ctx->dstaddr.ethernet, ETHER_ADDR_LEN);
  448. } else {
  449. tcpedit_seterr(ctx->tcpedit, "%s", "Please provide a destination address");
  450. return TCPEDIT_ERROR;
  451. }
  452. } else {
  453. tcpedit_seterr(ctx->tcpedit, "%s", "Encoders only support C2S or C2S!");
  454. return TCPEDIT_ERROR;
  455. }
  456. if (config->subs.entries) {
  457. int entry = 0;
  458. for (entry = 0 ; entry < config->subs.count; entry++) {
  459. en10mb_sub_entry_t *current = &config->subs.entries[entry];
  460. if (!memcmp(eth->ether_dhost, current->target, ETHER_ADDR_LEN)) {
  461. memcpy(eth->ether_dhost, current->rewrite, ETHER_ADDR_LEN);
  462. }
  463. if (!memcmp(eth->ether_shost, current->target, ETHER_ADDR_LEN)) {
  464. memcpy(eth->ether_shost, current->rewrite, ETHER_ADDR_LEN);
  465. }
  466. }
  467. }
  468. if (config->random.set) {
  469. int unicast_src = is_unicast_ethernet(ctx, eth->ether_shost);
  470. int unicast_dst = is_unicast_ethernet(ctx, eth->ether_dhost);
  471. int i = config->random.keep;
  472. for ( ; i < ETHER_ADDR_LEN; i++) {
  473. eth->ether_shost[i] = MAC_MASK_APPLY(eth->ether_shost[i], config->random.mask[i], unicast_src);
  474. eth->ether_dhost[i] = MAC_MASK_APPLY(eth->ether_dhost[i], config->random.mask[i], unicast_dst);
  475. }
  476. /* avoid making unicast packets multicast */
  477. if (!config->random.keep) {
  478. eth->ether_shost[0] &= ~(0x01 * unicast_src);
  479. eth->ether_dhost[0] &= ~(0x01 * unicast_dst);
  480. }
  481. }
  482. if (newl2len == TCPR_802_3_H) {
  483. /* all we need for 802.3 is the proto */
  484. eth->ether_type = ctx->proto;
  485. } else if (newl2len == TCPR_802_1Q_H) {
  486. /* VLAN tags need a bit more */
  487. vlan = (struct tcpr_802_1q_hdr *)packet;
  488. vlan->vlan_len = ctx->proto;
  489. vlan->vlan_tpi = htons(ETHERTYPE_VLAN);
  490. /* are we changing VLAN info? */
  491. if (config->vlan_tag < 65535) {
  492. vlan->vlan_priority_c_vid =
  493. htons((uint16_t)config->vlan_tag & TCPR_802_1Q_VIDMASK);
  494. } else if (extra->vlan) {
  495. vlan->vlan_priority_c_vid = extra->vlan_tag;
  496. } else {
  497. tcpedit_seterr(ctx->tcpedit, "%s", "Non-VLAN tagged packet requires --enet-vlan-tag");
  498. return TCPEDIT_ERROR;
  499. }
  500. if (config->vlan_pri < 255) {
  501. vlan->vlan_priority_c_vid += htons((uint16_t)config->vlan_pri << 13);
  502. } else if (extra->vlan) {
  503. vlan->vlan_priority_c_vid += extra->vlan_pri;
  504. } else {
  505. tcpedit_seterr(ctx->tcpedit, "%s", "Non-VLAN tagged packet requires --enet-vlan-pri");
  506. return TCPEDIT_ERROR;
  507. }
  508. if (config->vlan_cfi < 255) {
  509. vlan->vlan_priority_c_vid += htons((uint16_t)config->vlan_cfi << 12);
  510. } else if (extra->vlan) {
  511. vlan->vlan_priority_c_vid += extra->vlan_cfi;
  512. } else {
  513. tcpedit_seterr(ctx->tcpedit, "%s", "Non-VLAN tagged packet requires --enet-vlan-cfi");
  514. return TCPEDIT_ERROR;
  515. }
  516. } else {
  517. tcpedit_seterr(ctx->tcpedit, "Unsupported new layer 2 length: %d", newl2len);
  518. return TCPEDIT_ERROR;
  519. }
  520. return pktlen;
  521. }
  522. /*
  523. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  524. */
  525. int
  526. dlt_en10mb_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  527. {
  528. struct tcpr_ethernet_hdr *eth = NULL;
  529. struct tcpr_802_1q_hdr *vlan = NULL;
  530. assert(ctx);
  531. assert(packet);
  532. if (pktlen < (int) sizeof(*eth))
  533. return TCPEDIT_ERROR;
  534. eth = (struct tcpr_ethernet_hdr *)packet;
  535. switch (ntohs(eth->ether_type)) {
  536. case ETHERTYPE_VLAN:
  537. vlan = (struct tcpr_802_1q_hdr *)packet;
  538. return vlan->vlan_len;
  539. break;
  540. default:
  541. return eth->ether_type;
  542. break;
  543. }
  544. return TCPEDIT_ERROR;
  545. }
  546. /*
  547. * Function returns a pointer to the layer 3 protocol header or NULL on error
  548. */
  549. u_char *
  550. dlt_en10mb_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  551. {
  552. int l2len;
  553. assert(ctx);
  554. assert(packet);
  555. l2len = dlt_en10mb_l2len(ctx, packet, pktlen);
  556. if (pktlen < l2len)
  557. return NULL;
  558. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  559. }
  560. /*
  561. * function merges the packet (containing L2 and old L3) with the l3data buffer
  562. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  563. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  564. * like SPARC
  565. */
  566. u_char *
  567. dlt_en10mb_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  568. {
  569. int l2len;
  570. assert(ctx);
  571. assert(packet);
  572. assert(l3data);
  573. l2len = dlt_en10mb_l2len(ctx, packet, pktlen);
  574. if (pktlen < l2len)
  575. return NULL;
  576. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  577. }
  578. /*
  579. * return a static pointer to the source/destination MAC address
  580. * return NULL on error/address doesn't exist
  581. */
  582. u_char *
  583. dlt_en10mb_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  584. {
  585. assert(ctx);
  586. assert(packet);
  587. if (pktlen < 14)
  588. return NULL;
  589. /* FIXME: return a ptr to the source or dest mac address. */
  590. switch(mac) {
  591. case SRC_MAC:
  592. memcpy(ctx->srcmac, &packet[6], ETHER_ADDR_LEN);
  593. return(ctx->srcmac);
  594. break;
  595. case DST_MAC:
  596. memcpy(ctx->dstmac, packet, ETHER_ADDR_LEN);
  597. return(ctx->dstmac);
  598. break;
  599. default:
  600. errx(1, "Invalid tcpeditdlt_mac_type_t: %d", mac);
  601. }
  602. return(NULL);
  603. }
  604. /*
  605. * return the length of the L2 header of the current packet
  606. */
  607. int
  608. dlt_en10mb_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  609. {
  610. int l2len;
  611. struct tcpr_ethernet_hdr *eth = NULL;
  612. assert(ctx);
  613. assert(packet);
  614. eth = (struct tcpr_ethernet_hdr *)packet;
  615. switch (ntohs(eth->ether_type)) {
  616. case ETHERTYPE_VLAN:
  617. l2len = 18;
  618. break;
  619. default:
  620. l2len = 14;
  621. break;
  622. }
  623. if (l2len > 0) {
  624. if (pktlen < l2len) {
  625. /* can happen if fuzzing is enabled */
  626. return 0;
  627. }
  628. return l2len;
  629. }
  630. tcpedit_seterr(ctx->tcpedit, "%s", "Whoops! Bug in my code!");
  631. return -1;
  632. }
  633. tcpeditdlt_l2addr_type_t
  634. dlt_en10mb_l2addr_type(void)
  635. {
  636. return ETHERNET;
  637. }