tcprewrite.c 11 KB

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