1
0

tcprewrite.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2025 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. /*
  20. * Purpose: Modify packets in a pcap file based on rules provided by the
  21. * user to offload work from tcpreplay and provide an easier means of
  22. * reproducing traffic for testing purposes.
  23. */
  24. #include "tcprewrite.h"
  25. #include "config.h"
  26. #include "common.h"
  27. #include "tcpedit/fuzzing.h"
  28. #include "tcpedit/tcpedit.h"
  29. #include "tcprewrite_opts.h"
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <sys/types.h>
  36. #include <unistd.h>
  37. #ifdef HAVE_SYS_STAT_H
  38. #include <sys/stat.h>
  39. #endif
  40. #ifdef DEBUG
  41. int debug;
  42. #endif
  43. #ifdef ENABLE_VERBOSE
  44. /* tcpdump handle */
  45. tcpdump_t tcpdump;
  46. #endif
  47. tcprewrite_opt_t options;
  48. tcpedit_t *tcpedit;
  49. /* local functions */
  50. void tcprewrite_init(void);
  51. void post_args(int argc, char *argv[]);
  52. int rewrite_packets(tcpedit_t *tcpedit_ctx, pcap_t *pin, pcap_dumper_t *pout);
  53. int
  54. main(int argc, char *argv[])
  55. {
  56. int optct, rcode;
  57. pcap_t *dlt_pcap;
  58. #ifdef ENABLE_FRAGROUTE
  59. char ebuf[FRAGROUTE_ERRBUF_LEN];
  60. #endif
  61. tcprewrite_init();
  62. /* call autoopts to process arguments */
  63. optct = optionProcess(&tcprewriteOptions, argc, argv);
  64. argc -= optct;
  65. argv += optct;
  66. /* parse the tcprewrite args */
  67. post_args(argc, argv);
  68. /* init tcpedit context */
  69. if (tcpedit_init(&tcpedit, pcap_datalink(options.pin)) < 0) {
  70. err_no_exitx("Error initializing tcpedit: %s", tcpedit_geterr(tcpedit));
  71. tcpedit_close(&tcpedit);
  72. exit(-1);
  73. }
  74. /* parse the tcpedit args */
  75. rcode = tcpedit_post_args(tcpedit);
  76. if (rcode < 0) {
  77. err_no_exitx("Unable to parse args: %s", tcpedit_geterr(tcpedit));
  78. tcpedit_close(&tcpedit);
  79. exit(-1);
  80. } else if (rcode == 1) {
  81. warnx("%s", tcpedit_geterr(tcpedit));
  82. }
  83. if (tcpedit_validate(tcpedit) < 0) {
  84. err_no_exitx("Unable to edit packets given options:\n%s", tcpedit_geterr(tcpedit));
  85. tcpedit_close(&tcpedit);
  86. exit(-1);
  87. }
  88. /* fuzzing init */
  89. fuzzing_init(tcpedit->fuzz_seed, tcpedit->fuzz_factor);
  90. /* open up the output file */
  91. options.outfile = safe_strdup(OPT_ARG(OUTFILE));
  92. dbgx(1, "Rewriting DLT to %s", pcap_datalink_val_to_name(tcpedit_get_output_dlt(tcpedit)));
  93. #ifdef HAVE_SYS_STAT_H
  94. /* if the output file exists, make sure it isn't also the input file */
  95. struct stat outfile_stat;
  96. if (stat(options.outfile, &outfile_stat) == 0) {
  97. struct stat infile_stat;
  98. if (stat(options.infile, &infile_stat) == 0) {
  99. if (outfile_stat.st_ino == infile_stat.st_ino) {
  100. /* they are the same file */
  101. tcpedit_close(&tcpedit);
  102. err(-1, "--infile and --outfile cannot be the same file");
  103. }
  104. }
  105. }
  106. #endif
  107. if ((dlt_pcap = pcap_open_dead(tcpedit_get_output_dlt(tcpedit), 65535)) == NULL) {
  108. tcpedit_close(&tcpedit);
  109. err(-1, "Unable to open dead pcap handle.");
  110. }
  111. dbgx(1, "DLT of dlt_pcap is %s", pcap_datalink_val_to_name(pcap_datalink(dlt_pcap)));
  112. #ifdef ENABLE_FRAGROUTE
  113. if (options.fragroute_args) {
  114. if ((options.frag_ctx = fragroute_init(65535, pcap_datalink(dlt_pcap), options.fragroute_args, ebuf)) == NULL) {
  115. err_no_exitx("%s", ebuf);
  116. tcpedit_close(&tcpedit);
  117. exit(-1);
  118. }
  119. }
  120. #endif
  121. #ifdef ENABLE_VERBOSE
  122. if (options.verbose) {
  123. tcpdump_open(&tcpdump, dlt_pcap);
  124. }
  125. #endif
  126. if ((options.pout = pcap_dump_open(dlt_pcap, options.outfile)) == NULL) {
  127. err_no_exitx("Unable to open output pcap file: %s", pcap_geterr(dlt_pcap));
  128. tcpedit_close(&tcpedit);
  129. exit(-1);
  130. }
  131. pcap_close(dlt_pcap);
  132. /* rewrite packets */
  133. if (rewrite_packets(tcpedit, options.pin, options.pout) == TCPEDIT_ERROR) {
  134. err_no_exitx("Error rewriting packets: %s", tcpedit_geterr(tcpedit));
  135. tcpedit_close(&tcpedit);
  136. exit(-1);
  137. }
  138. /* clean up after ourselves */
  139. pcap_dump_close(options.pout);
  140. pcap_close(options.pin);
  141. tcpedit_close(&tcpedit);
  142. #ifdef ENABLE_VERBOSE
  143. tcpdump_close(&tcpdump);
  144. #endif
  145. #ifdef ENABLE_FRAGROUTE
  146. if (options.frag_ctx) {
  147. fragroute_close(options.frag_ctx);
  148. }
  149. #endif
  150. #ifdef ENABLE_DMALLOC
  151. dmalloc_shutdown();
  152. #endif
  153. restore_stdin();
  154. return 0;
  155. }
  156. void
  157. tcprewrite_init(void)
  158. {
  159. memset(&options, 0, sizeof(options));
  160. #ifdef ENABLE_VERBOSE
  161. /* clear out tcpdump struct */
  162. memset(&tcpdump, '\0', sizeof(tcpdump_t));
  163. #endif
  164. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  165. warnx("Unable to set STDERR to non-blocking: %s", strerror(errno));
  166. }
  167. /**
  168. * post AutoGen argument processing
  169. */
  170. void
  171. post_args(_U_ int argc, _U_ char *argv[])
  172. {
  173. char ebuf[PCAP_ERRBUF_SIZE];
  174. #ifdef DEBUG
  175. if (HAVE_OPT(DBUG))
  176. debug = OPT_VALUE_DBUG;
  177. #else
  178. if (HAVE_OPT(DBUG))
  179. warn("not configured with --enable-debug. Debugging disabled.");
  180. #endif
  181. if (HAVE_OPT(SUPPRESS_WARNINGS))
  182. print_warnings = 0;
  183. #ifdef ENABLE_VERBOSE
  184. if (HAVE_OPT(VERBOSE))
  185. options.verbose = 1;
  186. if (HAVE_OPT(DECODE))
  187. tcpdump.args = safe_strdup(OPT_ARG(DECODE));
  188. #endif
  189. #ifdef ENABLE_FRAGROUTE
  190. if (HAVE_OPT(FRAGROUTE))
  191. options.fragroute_args = safe_strdup(OPT_ARG(FRAGROUTE));
  192. options.fragroute_dir = FRAGROUTE_DIR_BOTH;
  193. if (HAVE_OPT(FRAGDIR)) {
  194. if (strcmp(OPT_ARG(FRAGDIR), "c2s") == 0) {
  195. options.fragroute_dir = FRAGROUTE_DIR_C2S;
  196. } else if (strcmp(OPT_ARG(FRAGDIR), "s2c") == 0) {
  197. options.fragroute_dir = FRAGROUTE_DIR_S2C;
  198. } else if (strcmp(OPT_ARG(FRAGDIR), "both") == 0) {
  199. options.fragroute_dir = FRAGROUTE_DIR_BOTH;
  200. } else {
  201. errx(-1, "Unknown --fragdir value: %s", OPT_ARG(FRAGDIR));
  202. }
  203. }
  204. #endif
  205. /* open up the input file */
  206. options.infile = safe_strdup(OPT_ARG(INFILE));
  207. if ((options.pin = pcap_open_offline(options.infile, ebuf)) == NULL)
  208. errx(-1, "Unable to open input pcap file: %s", ebuf);
  209. #ifdef HAVE_PCAP_SNAPSHOT
  210. if (pcap_snapshot(options.pin) < 65535)
  211. warnx("%s was captured using a snaplen of %d bytes. This may mean you have truncated packets.",
  212. options.infile,
  213. pcap_snapshot(options.pin));
  214. #endif
  215. }
  216. /**
  217. * Main loop to rewrite packets
  218. */
  219. int
  220. rewrite_packets(tcpedit_t *tcpedit_ctx, pcap_t *pin, pcap_dumper_t *pout)
  221. {
  222. tcpr_dir_t cache_result = TCPR_DIR_C2S; /* default to primary */
  223. struct pcap_pkthdr pkthdr, *pkthdr_ptr; /* packet header */
  224. const u_char *pktconst = NULL; /* packet from libpcap */
  225. u_char **pktdata = NULL;
  226. static u_char *pktdata_buff;
  227. static char *frag = NULL;
  228. COUNTER packetnum = 0;
  229. int rcode;
  230. #ifdef ENABLE_FRAGROUTE
  231. int frag_len, proto;
  232. #endif
  233. pkthdr_ptr = &pkthdr;
  234. if (pktdata_buff == NULL)
  235. pktdata_buff = (u_char *)safe_malloc(MAXPACKET);
  236. pktdata = &pktdata_buff;
  237. if (frag == NULL)
  238. frag = (char *)safe_malloc(MAXPACKET);
  239. /* MAIN LOOP
  240. * Keep sending while we have packets or until
  241. * we've sent enough packets
  242. */
  243. while ((pktconst = safe_pcap_next(pin, pkthdr_ptr)) != NULL) {
  244. packetnum++;
  245. dbgx(2, "packet " COUNTER_SPEC " caplen %d", packetnum, pkthdr.caplen);
  246. if (pkthdr.caplen > MAX_SNAPLEN)
  247. errx(-1, "Frame too big, caplen %d exceeds %d", pkthdr.caplen, MAX_SNAPLEN);
  248. if (pkthdr.len > MAX_SNAPLEN)
  249. errx(-1, "Frame too big, len %d exceeds %d", pkthdr.len, MAX_SNAPLEN);
  250. /*
  251. * copy over the packet so we can pad it out if necessary and
  252. * because pcap_next() returns a const ptr
  253. */
  254. memcpy(*pktdata, pktconst, pkthdr.caplen);
  255. /* Dual nic processing? */
  256. if (options.cachedata != NULL) {
  257. cache_result = check_cache(options.cachedata, packetnum);
  258. }
  259. /* sometimes we should not send the packet, in such cases
  260. * no point in editing this packet at all, just write it to the
  261. * output file (note, we can't just remove it, or the tcpprep cache
  262. * file will lose it's indexing
  263. */
  264. if (cache_result == TCPR_DIR_NOSEND)
  265. goto WRITE_PACKET; /* still need to write it so cache stays in sync */
  266. if ((rcode = tcpedit_packet(tcpedit_ctx, &pkthdr_ptr, pktdata, cache_result)) == TCPEDIT_ERROR) {
  267. return rcode;
  268. } else if ((rcode == TCPEDIT_SOFT_ERROR) && HAVE_OPT(SKIP_SOFT_ERRORS)) {
  269. /* don't write packet */
  270. dbgx(1, "Packet " COUNTER_SPEC " is suppressed from being written due to soft errors", packetnum);
  271. continue;
  272. }
  273. #ifdef ENABLE_VERBOSE
  274. if (options.verbose)
  275. tcpdump_print(&tcpdump, pkthdr_ptr, *pktdata);
  276. #endif
  277. WRITE_PACKET:
  278. #ifdef ENABLE_FRAGROUTE
  279. if (options.frag_ctx == NULL) {
  280. /* write the packet when there's no fragrouting to be done */
  281. if (pkthdr_ptr->caplen)
  282. pcap_dump((u_char *)pout, pkthdr_ptr, *pktdata);
  283. } else {
  284. /* get the L3 protocol of the packet */
  285. proto = tcpedit_l3proto(tcpedit_ctx, AFTER_PROCESS, *pktdata, pkthdr_ptr->caplen);
  286. /* packet is IPv4/IPv6 AND needs to be fragmented */
  287. if ((proto == ETHERTYPE_IP || proto == ETHERTYPE_IP6) &&
  288. ((options.fragroute_dir == FRAGROUTE_DIR_BOTH) ||
  289. (cache_result == TCPR_DIR_C2S && options.fragroute_dir == FRAGROUTE_DIR_C2S) ||
  290. (cache_result == TCPR_DIR_S2C && options.fragroute_dir == FRAGROUTE_DIR_S2C))) {
  291. #ifdef DEBUG
  292. int i = 0;
  293. #endif
  294. if (fragroute_process(options.frag_ctx, *pktdata, pkthdr_ptr->caplen) < 0)
  295. errx(-1, "Error processing packet via fragroute: %s", options.frag_ctx->errbuf);
  296. while ((frag_len = fragroute_getfragment(options.frag_ctx, &frag)) > 0) {
  297. /* frags get the same timestamp as the original packet */
  298. dbgx(1, "processing packet " COUNTER_SPEC " frag: %u (%d)", packetnum, i++, frag_len);
  299. pkthdr_ptr->caplen = frag_len;
  300. pkthdr_ptr->len = frag_len;
  301. if (pkthdr_ptr->caplen)
  302. pcap_dump((u_char *)pout, pkthdr_ptr, (u_char *)frag);
  303. }
  304. } else {
  305. /* write the packet without fragroute */
  306. if (pkthdr_ptr->caplen)
  307. pcap_dump((u_char *)pout, pkthdr_ptr, *pktdata);
  308. }
  309. }
  310. #else
  311. /* write the packet when there's no fragrouting to be done */
  312. if (pkthdr_ptr->caplen)
  313. pcap_dump((u_char *)pout, pkthdr_ptr, *pktdata);
  314. #endif
  315. } /* while() */
  316. return 0;
  317. }