tcprewrite.c 10 KB

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