en10mb.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 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 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 if
  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. if (ctx->decoded_extra_size > 0) {
  92. if (ctx->decoded_extra_size < sizeof(en10mb_extra_t)) {
  93. ctx->decoded_extra_size = sizeof(en10mb_extra_t);
  94. ctx->decoded_extra = safe_realloc(ctx->decoded_extra,
  95. ctx->decoded_extra_size);
  96. }
  97. } else {
  98. ctx->decoded_extra_size = sizeof(en10mb_extra_t);
  99. ctx->decoded_extra = safe_malloc(ctx->decoded_extra_size);
  100. }
  101. plugin->config_size = sizeof(en10mb_config_t);
  102. plugin->config = safe_malloc(plugin->config_size);
  103. config = (en10mb_config_t *)plugin->config;
  104. /* init vlan user values to -1 to indicate not set */
  105. config->vlan_tag = 65535;
  106. config->vlan_pri = 255;
  107. config->vlan_cfi = 255;
  108. return TCPEDIT_OK; /* success */
  109. }
  110. /*
  111. * Since this is used in a library, we should manually clean up after ourselves
  112. * Unless you allocated some memory in dlt_en10mb_init(), this is just an stub.
  113. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  114. */
  115. int
  116. dlt_en10mb_cleanup(tcpeditdlt_t *ctx)
  117. {
  118. tcpeditdlt_plugin_t *plugin;
  119. assert(ctx);
  120. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  121. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s",
  122. dlt_name);
  123. return TCPEDIT_ERROR;
  124. }
  125. safe_free(plugin->name);
  126. plugin->name = NULL;
  127. if (plugin->config != NULL) {
  128. en10mb_config_t *config = (en10mb_config_t*)plugin->config;
  129. safe_free(config->subs.entries);
  130. safe_free(plugin->config);
  131. plugin->config = NULL;
  132. plugin->config_size = 0;
  133. }
  134. return TCPEDIT_OK; /* success */
  135. }
  136. int
  137. dlt_en10mb_parse_subsmac_entry(const char *raw, en10mb_sub_entry_t *entry)
  138. {
  139. char *candidate = safe_strdup(raw);
  140. int parse_result = dualmac2hex(candidate, entry->target, entry->rewrite, SUBSMAC_ENTRY_LEN);
  141. free(candidate);
  142. return parse_result;
  143. }
  144. en10mb_sub_entry_t *
  145. dlt_en10mb_realloc_merge(en10mb_sub_conf_t config, en10mb_sub_entry_t *new_entries, int entries_count)
  146. {
  147. int i;
  148. config.entries = safe_realloc(config.entries,
  149. (config.count + entries_count)
  150. * sizeof(en10mb_sub_entry_t));
  151. for (i = 0; i < entries_count; i++) {
  152. config.entries[config.count + i] = new_entries[i];
  153. }
  154. return config.entries;
  155. }
  156. int
  157. dlt_en10mb_parse_subsmac(tcpeditdlt_t *ctx, en10mb_config_t *config, const char *input)
  158. {
  159. int input_len = strlen(input);
  160. int possible_entries_number = (input_len / (SUBSMAC_ENTRY_LEN + 1)) + 1;
  161. int entry = 0;
  162. en10mb_sub_entry_t *entries = safe_malloc(possible_entries_number * sizeof(en10mb_sub_entry_t));
  163. for (entry = 0; entry < possible_entries_number; entry++) {
  164. const int read_offset = entry + entry * SUBSMAC_ENTRY_LEN;
  165. if (input_len - read_offset < SUBSMAC_ENTRY_LEN) {
  166. free(entries);
  167. tcpedit_seterr(ctx->tcpedit, "Unable to parse --enet-subsmac=%s", input);
  168. return TCPEDIT_ERROR;
  169. }
  170. switch(dlt_en10mb_parse_subsmac_entry(input + read_offset, &entries[entry])) {
  171. case 3:
  172. /* Both read; This is what we want */
  173. break;
  174. default:
  175. free(entries);
  176. tcpedit_seterr(ctx->tcpedit, "Unable to parse --enet-subsmac=%s", input);
  177. return TCPEDIT_ERROR;
  178. }
  179. }
  180. config->subs.entries = dlt_en10mb_realloc_merge(config->subs, entries, possible_entries_number);
  181. config->subs.count += possible_entries_number;
  182. free(entries);
  183. return TCPEDIT_OK;
  184. }
  185. /*
  186. * This is where you should define all your AutoGen AutoOpts option parsing.
  187. * Any user specified option should have it's bit turned on in the 'provides'
  188. * bit mask.
  189. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  190. */
  191. int
  192. dlt_en10mb_parse_opts(tcpeditdlt_t *ctx)
  193. {
  194. tcpeditdlt_plugin_t *plugin;
  195. en10mb_config_t *config;
  196. assert(ctx);
  197. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  198. if (!plugin)
  199. return TCPEDIT_ERROR;
  200. config = (en10mb_config_t *)plugin->config;
  201. if (plugin->config_size < sizeof(*config))
  202. return TCPEDIT_ERROR;
  203. /* --subsmacs */
  204. if (HAVE_OPT(ENET_SUBSMAC)) {
  205. int i, count = STACKCT_OPT(ENET_SUBSMAC);
  206. char **list = (char**) STACKLST_OPT(ENET_SUBSMAC);
  207. for (i = 0; i < count; i++) {
  208. int parse_result = dlt_en10mb_parse_subsmac(ctx, config, list[i]);
  209. if (parse_result == TCPEDIT_ERROR) {
  210. return TCPEDIT_ERROR;
  211. }
  212. }
  213. }
  214. /* --mac-seed */
  215. if (HAVE_OPT(ENET_MAC_SEED)) {
  216. int i,j;
  217. config->random.set = OPT_VALUE_ENET_MAC_SEED;
  218. for (i = 0; i < 6; i++) {
  219. config->random.mask[i] = (u_char)tcpr_random(&config->random.set) % 256;
  220. /* only unique numbers */
  221. for (j = 0; j < i; j++) {
  222. if (config->random.mask[i] == config->random.mask[j]) {
  223. i--;
  224. break;
  225. }
  226. }
  227. }
  228. if (HAVE_OPT(ENET_MAC_SEED_KEEP_BYTES)) {
  229. config->random.keep = OPT_VALUE_ENET_MAC_SEED_KEEP_BYTES;
  230. }
  231. }
  232. /* --dmac */
  233. if (HAVE_OPT(ENET_DMAC)) {
  234. int macparse;
  235. macparse = dualmac2hex(OPT_ARG(ENET_DMAC), config->intf1_dmac,
  236. config->intf2_dmac, strlen(OPT_ARG(ENET_DMAC)));
  237. switch (macparse) {
  238. case 1:
  239. config->mac_mask += TCPEDIT_MAC_MASK_DMAC1;
  240. break;
  241. case 2:
  242. config->mac_mask += TCPEDIT_MAC_MASK_DMAC2;
  243. break;
  244. case 3:
  245. config->mac_mask += TCPEDIT_MAC_MASK_DMAC1;
  246. config->mac_mask += TCPEDIT_MAC_MASK_DMAC2;
  247. break;
  248. case 0:
  249. /* nothing to do */
  250. break;
  251. default:
  252. tcpedit_seterr(ctx->tcpedit,
  253. "Unable to parse --enet-dmac=%s", OPT_ARG(ENET_DMAC));
  254. return TCPEDIT_ERROR;
  255. break;
  256. }
  257. plugin->requires -= PLUGIN_MASK_DSTADDR;
  258. }
  259. /* --smac */
  260. if (HAVE_OPT(ENET_SMAC)) {
  261. int macparse;
  262. macparse = dualmac2hex(OPT_ARG(ENET_SMAC), config->intf1_smac,
  263. config->intf2_smac, strlen(OPT_ARG(ENET_SMAC)));
  264. switch (macparse) {
  265. case 1:
  266. config->mac_mask += TCPEDIT_MAC_MASK_SMAC1;
  267. break;
  268. case 2:
  269. config->mac_mask += TCPEDIT_MAC_MASK_SMAC2;
  270. break;
  271. case 3:
  272. config->mac_mask += TCPEDIT_MAC_MASK_SMAC1;
  273. config->mac_mask += TCPEDIT_MAC_MASK_SMAC2;
  274. break;
  275. case 0:
  276. /* nothing to do */
  277. break;
  278. default:
  279. tcpedit_seterr(ctx->tcpedit,
  280. "Unable to parse --enet-smac=%s", OPT_ARG(ENET_SMAC));
  281. return TCPEDIT_ERROR;
  282. break;
  283. }
  284. plugin->requires -= PLUGIN_MASK_SRCADDR;
  285. }
  286. /*
  287. * Validate 802.1q vlan args and populate tcpedit->vlan_record
  288. */
  289. if (HAVE_OPT(ENET_VLAN)) {
  290. if (strcmp(OPT_ARG(ENET_VLAN), "add") == 0) { // add or change
  291. config->vlan = TCPEDIT_VLAN_ADD;
  292. } else if (strcmp(OPT_ARG(ENET_VLAN), "del") == 0) {
  293. config->vlan = TCPEDIT_VLAN_DEL;
  294. } else {
  295. tcpedit_seterr(ctx->tcpedit, "Invalid --enet-vlan=%s", OPT_ARG(ENET_VLAN));
  296. return -1;
  297. }
  298. if (config->vlan != TCPEDIT_VLAN_OFF) {
  299. if (config->vlan == TCPEDIT_VLAN_ADD) {
  300. if (! HAVE_OPT(ENET_VLAN_TAG)) {
  301. tcpedit_seterr(ctx->tcpedit, "%s",
  302. "Must specify a new 802.1 VLAN tag if vlan "
  303. "mode is add");
  304. return TCPEDIT_ERROR;
  305. }
  306. /*
  307. * fill out the 802.1q header
  308. */
  309. config->vlan_tag = OPT_VALUE_ENET_VLAN_TAG;
  310. dbgx(1, "We will %s 802.1q headers",
  311. config->vlan == TCPEDIT_VLAN_DEL ? "delete" : "add/modify");
  312. if (HAVE_OPT(ENET_VLAN_PRI))
  313. config->vlan_pri = OPT_VALUE_ENET_VLAN_PRI;
  314. if (HAVE_OPT(ENET_VLAN_CFI))
  315. config->vlan_cfi = OPT_VALUE_ENET_VLAN_CFI;
  316. }
  317. }
  318. }
  319. return TCPEDIT_OK; /* success */
  320. }
  321. /*
  322. * Function to decode the layer 2 header in the packet
  323. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  324. */
  325. int
  326. dlt_en10mb_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  327. {
  328. struct tcpr_ethernet_hdr *eth = NULL;
  329. struct tcpr_802_1q_hdr *vlan = NULL;
  330. en10mb_extra_t *extra = NULL;
  331. assert(ctx);
  332. assert(packet);
  333. if (pktlen < TCPR_802_3_H)
  334. return TCPEDIT_ERROR;
  335. /* get our src & dst address */
  336. eth = (struct tcpr_ethernet_hdr *)packet;
  337. memcpy(&(ctx->dstaddr.ethernet), eth, ETHER_ADDR_LEN);
  338. memcpy(&(ctx->srcaddr.ethernet), &(eth->ether_shost), ETHER_ADDR_LEN);
  339. extra = (en10mb_extra_t *)ctx->decoded_extra;
  340. if (ctx->decoded_extra_size < sizeof(*extra))
  341. return TCPEDIT_ERROR;
  342. extra->vlan = 0;
  343. /* get the L3 protocol type & L2 len*/
  344. switch (ntohs(eth->ether_type)) {
  345. case ETHERTYPE_VLAN:
  346. if (pktlen < TCPR_802_1Q_H)
  347. return TCPEDIT_ERROR;
  348. vlan = (struct tcpr_802_1q_hdr *)packet;
  349. ctx->proto = vlan->vlan_len;
  350. /* Get VLAN tag info */
  351. extra->vlan = 1;
  352. /* must use these mask values, rather then what's in the tcpr.h since it assumes you're shifting */
  353. extra->vlan_tag = vlan->vlan_priority_c_vid & 0x0FFF;
  354. extra->vlan_pri = vlan->vlan_priority_c_vid & 0xE000;
  355. extra->vlan_cfi = vlan->vlan_priority_c_vid & 0x1000;
  356. ctx->l2len = TCPR_802_1Q_H;
  357. break;
  358. /* we don't properly handle SNAP encoding */
  359. default:
  360. ctx->proto = eth->ether_type;
  361. ctx->l2len = TCPR_802_3_H;
  362. }
  363. return TCPEDIT_OK; /* success */
  364. }
  365. /*
  366. * Function to encode the layer 2 header back into the packet.
  367. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  368. */
  369. int
  370. dlt_en10mb_encode(tcpeditdlt_t *ctx, u_char *packet, int pktlen, tcpr_dir_t dir)
  371. {
  372. tcpeditdlt_plugin_t *plugin = NULL;
  373. struct tcpr_ethernet_hdr *eth = NULL;
  374. struct tcpr_802_1q_hdr *vlan = NULL;
  375. en10mb_config_t *config = NULL;
  376. en10mb_extra_t *extra = NULL;
  377. int newl2len = 0;
  378. assert(ctx);
  379. assert(packet);
  380. if (pktlen < TCPR_802_1Q_H) {
  381. tcpedit_seterr(ctx->tcpedit,
  382. "Unable to process packet #" COUNTER_SPEC " since it is less then 14 bytes.",
  383. ctx->tcpedit->runtime.packetnum);
  384. return TCPEDIT_ERROR;
  385. }
  386. plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
  387. if (!plugin)
  388. return TCPEDIT_ERROR;
  389. config = plugin->config;
  390. if (plugin->config_size < sizeof(*config))
  391. return TCPEDIT_ERROR;
  392. extra = (en10mb_extra_t *)ctx->decoded_extra;
  393. if (ctx->decoded_extra_size < sizeof(*extra))
  394. return TCPEDIT_ERROR;
  395. /* figure out the new layer2 length, first for the case: ethernet -> ethernet? */
  396. if (ctx->decoder->dlt == dlt_value) {
  397. if ((ctx->l2len == TCPR_802_1Q_H && config->vlan == TCPEDIT_VLAN_OFF) ||
  398. (config->vlan == TCPEDIT_VLAN_ADD)) {
  399. newl2len = TCPR_802_1Q_H;
  400. } else if ((ctx->l2len == TCPR_802_3_H && config->vlan == TCPEDIT_VLAN_OFF) ||
  401. (config->vlan == TCPEDIT_VLAN_DEL)) {
  402. newl2len = TCPR_802_3_H;
  403. }
  404. }
  405. /* newl2len for some other DLT -> ethernet */
  406. else {
  407. /* if add a vlan then 18, else 14 bytes */
  408. newl2len = config->vlan == TCPEDIT_VLAN_ADD ? TCPR_802_1Q_H : TCPR_802_3_H;
  409. }
  410. if (pktlen < newl2len) {
  411. tcpedit_seterr(ctx->tcpedit,
  412. "Unable to process packet #" COUNTER_SPEC " since its new length less then %d bytes.",
  413. ctx->tcpedit->runtime.packetnum, newl2len);
  414. return TCPEDIT_ERROR;
  415. }
  416. if (pktlen < ctx->l2len) {
  417. tcpedit_seterr(ctx->tcpedit,
  418. "Unable to process packet #" COUNTER_SPEC " since its new length less then %d L2 bytes.",
  419. ctx->tcpedit->runtime.packetnum, ctx->l2len);
  420. return TCPEDIT_ERROR;
  421. }
  422. /* Make space for our new L2 header */
  423. if (newl2len != ctx->l2len) {
  424. if (pktlen + (newl2len - ctx->l2len) > MAXPACKET)
  425. errx(-1, "New frame too big, new length %d exceeds %d",
  426. pktlen + (newl2len - ctx->l2len), MAXPACKET);
  427. memmove(packet + newl2len, packet + ctx->l2len, pktlen - ctx->l2len);
  428. }
  429. /* update the total packet length */
  430. pktlen += newl2len - ctx->l2len;
  431. /* always set the src & dst address as the first 12 bytes */
  432. eth = (struct tcpr_ethernet_hdr *)packet;
  433. if (dir == TCPR_DIR_C2S) {
  434. /* copy user supplied SRC MAC if provided or from original packet */
  435. if (config->mac_mask & TCPEDIT_MAC_MASK_SMAC1) {
  436. if ((ctx->addr_type == ETHERNET &&
  437. ((ctx->skip_broadcast &&
  438. is_unicast_ethernet(ctx, ctx->srcaddr.ethernet)) || !ctx->skip_broadcast))
  439. || ctx->addr_type != ETHERNET) {
  440. memcpy(eth->ether_shost, config->intf1_smac, ETHER_ADDR_LEN);
  441. } else {
  442. memcpy(eth->ether_shost, ctx->srcaddr.ethernet, ETHER_ADDR_LEN);
  443. }
  444. } else if (ctx->addr_type == ETHERNET) {
  445. memcpy(eth->ether_shost, ctx->srcaddr.ethernet, ETHER_ADDR_LEN);
  446. } else {
  447. tcpedit_seterr(ctx->tcpedit, "%s", "Please provide a source address");
  448. return TCPEDIT_ERROR;
  449. }
  450. /* copy user supplied DMAC MAC if provided or from original packet */
  451. if (config->mac_mask & TCPEDIT_MAC_MASK_DMAC1) {
  452. if ((ctx->addr_type == ETHERNET &&
  453. ((ctx->skip_broadcast && is_unicast_ethernet(ctx, ctx->dstaddr.ethernet)) || !ctx->skip_broadcast))
  454. || ctx->addr_type != ETHERNET) {
  455. memcpy(eth->ether_dhost, config->intf1_dmac, ETHER_ADDR_LEN);
  456. } else {
  457. memcpy(eth->ether_dhost, ctx->dstaddr.ethernet, ETHER_ADDR_LEN);
  458. }
  459. } else if (ctx->addr_type == ETHERNET) {
  460. memcpy(eth->ether_dhost, ctx->dstaddr.ethernet, ETHER_ADDR_LEN);
  461. } else {
  462. tcpedit_seterr(ctx->tcpedit, "%s", "Please provide a destination address");
  463. return TCPEDIT_ERROR;
  464. }
  465. } else if (dir == TCPR_DIR_S2C) {
  466. /* copy user supplied SRC MAC if provided or from original packet */
  467. if (config->mac_mask & TCPEDIT_MAC_MASK_SMAC2) {
  468. if ((ctx->addr_type == ETHERNET &&
  469. ((ctx->skip_broadcast && is_unicast_ethernet(ctx, ctx->srcaddr.ethernet)) || !ctx->skip_broadcast))
  470. || ctx->addr_type != ETHERNET) {
  471. memcpy(eth->ether_shost, config->intf2_smac, ETHER_ADDR_LEN);
  472. } else {
  473. memcpy(eth->ether_shost, ctx->srcaddr.ethernet, ETHER_ADDR_LEN);
  474. }
  475. } else if (ctx->addr_type == ETHERNET) {
  476. memcpy(eth->ether_shost, ctx->srcaddr.ethernet, ETHER_ADDR_LEN);
  477. } else {
  478. tcpedit_seterr(ctx->tcpedit, "%s", "Please provide a source address");
  479. return TCPEDIT_ERROR;
  480. }
  481. /* copy user supplied DMAC MAC if provided or from original packet */
  482. if (config->mac_mask & TCPEDIT_MAC_MASK_DMAC2) {
  483. if ((ctx->addr_type == ETHERNET &&
  484. ((ctx->skip_broadcast && is_unicast_ethernet(ctx, ctx->dstaddr.ethernet)) || !ctx->skip_broadcast))
  485. || ctx->addr_type != ETHERNET) {
  486. memcpy(eth->ether_dhost, config->intf2_dmac, ETHER_ADDR_LEN);
  487. } else {
  488. memcpy(eth->ether_dhost, ctx->dstaddr.ethernet, ETHER_ADDR_LEN);
  489. }
  490. } else if (ctx->addr_type == ETHERNET) {
  491. memcpy(eth->ether_dhost, ctx->dstaddr.ethernet, ETHER_ADDR_LEN);
  492. } else {
  493. tcpedit_seterr(ctx->tcpedit, "%s", "Please provide a destination address");
  494. return TCPEDIT_ERROR;
  495. }
  496. } else {
  497. tcpedit_seterr(ctx->tcpedit, "%s", "Encoders only support C2S or C2S!");
  498. return TCPEDIT_ERROR;
  499. }
  500. if (config->subs.entries) {
  501. int entry = 0;
  502. for (entry = 0 ; entry < config->subs.count; entry++) {
  503. en10mb_sub_entry_t *current = &config->subs.entries[entry];
  504. if (!memcmp(eth->ether_dhost, current->target, ETHER_ADDR_LEN)) {
  505. memcpy(eth->ether_dhost, current->rewrite, ETHER_ADDR_LEN);
  506. }
  507. if (!memcmp(eth->ether_shost, current->target, ETHER_ADDR_LEN)) {
  508. memcpy(eth->ether_shost, current->rewrite, ETHER_ADDR_LEN);
  509. }
  510. }
  511. }
  512. if (config->random.set) {
  513. int unicast_src = is_unicast_ethernet(ctx, eth->ether_shost);
  514. int unicast_dst = is_unicast_ethernet(ctx, eth->ether_dhost);
  515. int i = config->random.keep;
  516. for ( ; i < ETHER_ADDR_LEN; i++) {
  517. eth->ether_shost[i] = MAC_MASK_APPLY(eth->ether_shost[i], config->random.mask[i], unicast_src);
  518. eth->ether_dhost[i] = MAC_MASK_APPLY(eth->ether_dhost[i], config->random.mask[i], unicast_dst);
  519. }
  520. /* avoid making unicast packets multicast */
  521. if (!config->random.keep) {
  522. eth->ether_shost[0] &= ~(0x01 * unicast_src);
  523. eth->ether_dhost[0] &= ~(0x01 * unicast_dst);
  524. }
  525. }
  526. if (newl2len == TCPR_802_3_H) {
  527. /* all we need for 802.3 is the proto */
  528. eth->ether_type = ctx->proto;
  529. } else if (newl2len == TCPR_802_1Q_H) {
  530. /* VLAN tags need a bit more */
  531. vlan = (struct tcpr_802_1q_hdr *)packet;
  532. vlan->vlan_len = ctx->proto;
  533. vlan->vlan_tpi = htons(ETHERTYPE_VLAN);
  534. /* are we changing VLAN info? */
  535. if (config->vlan_tag < 65535) {
  536. vlan->vlan_priority_c_vid =
  537. htons((uint16_t)config->vlan_tag & TCPR_802_1Q_VIDMASK);
  538. } else if (extra->vlan) {
  539. vlan->vlan_priority_c_vid = extra->vlan_tag;
  540. } else {
  541. tcpedit_seterr(ctx->tcpedit, "%s", "Non-VLAN tagged packet requires --enet-vlan-tag");
  542. return TCPEDIT_ERROR;
  543. }
  544. if (config->vlan_pri < 255) {
  545. vlan->vlan_priority_c_vid += htons((uint16_t)config->vlan_pri << 13);
  546. } else if (extra->vlan) {
  547. vlan->vlan_priority_c_vid += extra->vlan_pri;
  548. } else {
  549. tcpedit_seterr(ctx->tcpedit, "%s", "Non-VLAN tagged packet requires --enet-vlan-pri");
  550. return TCPEDIT_ERROR;
  551. }
  552. if (config->vlan_cfi < 255) {
  553. vlan->vlan_priority_c_vid += htons((uint16_t)config->vlan_cfi << 12);
  554. } else if (extra->vlan) {
  555. vlan->vlan_priority_c_vid += extra->vlan_cfi;
  556. } else {
  557. tcpedit_seterr(ctx->tcpedit, "%s", "Non-VLAN tagged packet requires --enet-vlan-cfi");
  558. return TCPEDIT_ERROR;
  559. }
  560. } else {
  561. tcpedit_seterr(ctx->tcpedit, "Unsupported new layer 2 length: %d", newl2len);
  562. return TCPEDIT_ERROR;
  563. }
  564. return pktlen;
  565. }
  566. /*
  567. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  568. */
  569. int
  570. dlt_en10mb_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  571. {
  572. struct tcpr_ethernet_hdr *eth = NULL;
  573. struct tcpr_802_1q_hdr *vlan = NULL;
  574. assert(ctx);
  575. assert(packet);
  576. if (pktlen < (int) sizeof(*eth)) {
  577. tcpedit_seterr(ctx->tcpedit, "Ethernet packet length too short: %d",
  578. pktlen);
  579. return TCPEDIT_ERROR;
  580. }
  581. eth = (struct tcpr_ethernet_hdr *)packet;
  582. switch (ntohs(eth->ether_type)) {
  583. case ETHERTYPE_VLAN:
  584. vlan = (struct tcpr_802_1q_hdr *)packet;
  585. return vlan->vlan_len;
  586. break;
  587. default:
  588. return eth->ether_type;
  589. break;
  590. }
  591. return TCPEDIT_ERROR;
  592. }
  593. /*
  594. * Function returns a pointer to the layer 3 protocol header or NULL on error
  595. */
  596. u_char *
  597. dlt_en10mb_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  598. {
  599. int l2len;
  600. assert(ctx);
  601. assert(packet);
  602. l2len = dlt_en10mb_l2len(ctx, packet, pktlen);
  603. if (l2len == -1 || pktlen < l2len)
  604. return NULL;
  605. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  606. }
  607. /*
  608. * function merges the packet (containing L2 and old L3) with the l3data buffer
  609. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  610. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  611. * like SPARC
  612. */
  613. u_char *
  614. dlt_en10mb_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  615. {
  616. int l2len;
  617. assert(ctx);
  618. assert(packet);
  619. assert(l3data);
  620. l2len = dlt_en10mb_l2len(ctx, packet, pktlen);
  621. if (l2len == -1 || pktlen < l2len)
  622. return NULL;
  623. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  624. }
  625. /*
  626. * return a static pointer to the source/destination MAC address
  627. * return NULL on error/address doesn't exist
  628. */
  629. u_char *
  630. dlt_en10mb_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  631. {
  632. assert(ctx);
  633. assert(packet);
  634. if (pktlen < 14)
  635. return NULL;
  636. /* FIXME: return a ptr to the source or dest mac address. */
  637. switch(mac) {
  638. case SRC_MAC:
  639. memcpy(ctx->srcmac, &packet[6], ETHER_ADDR_LEN);
  640. return(ctx->srcmac);
  641. break;
  642. case DST_MAC:
  643. memcpy(ctx->dstmac, packet, ETHER_ADDR_LEN);
  644. return(ctx->dstmac);
  645. break;
  646. default:
  647. errx(1, "Invalid tcpeditdlt_mac_type_t: %d", mac);
  648. }
  649. return(NULL);
  650. }
  651. /*
  652. * return the length of the L2 header of the current packet
  653. */
  654. int
  655. dlt_en10mb_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  656. {
  657. int l2len;
  658. uint16_t ether_type;
  659. assert(ctx);
  660. assert(packet);
  661. l2len = sizeof(eth_hdr_t);
  662. if (pktlen < l2len)
  663. return -1;
  664. ether_type = ntohs(((eth_hdr_t*)packet)->ether_type);
  665. while (ether_type == ETHERTYPE_VLAN) {
  666. if (pktlen < l2len + (int)sizeof(vlan_hdr_t))
  667. return -1;
  668. vlan_hdr_t *vlan_hdr = (vlan_hdr_t*)(packet + l2len);
  669. ether_type = ntohs(vlan_hdr->vlan_tpid);
  670. l2len += 4;
  671. }
  672. if (l2len > 0) {
  673. if (pktlen < l2len) {
  674. /* can happen if fuzzing is enabled */
  675. tcpedit_seterr(ctx->tcpedit, "dlt_en10mb_l2len: pktlen=%u is less than l2len=%u",
  676. pktlen, l2len);
  677. return -1;
  678. }
  679. return l2len;
  680. }
  681. tcpedit_seterr(ctx->tcpedit, "dlt_en10mb_l2len: %s", "Whoops! Bug in my code!");
  682. return TCPEDIT_ERROR;
  683. }
  684. tcpeditdlt_l2addr_type_t
  685. dlt_en10mb_l2addr_type(void)
  686. {
  687. return ETHERNET;
  688. }