en10mb.c 26 KB

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