tcprewrite.c 11 KB

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