tcprewrite.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 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. #ifdef ENABLE_VERBOSE
  165. if (HAVE_OPT(VERBOSE))
  166. options.verbose = 1;
  167. if (HAVE_OPT(DECODE))
  168. tcpdump.args = safe_strdup(OPT_ARG(DECODE));
  169. #endif
  170. #ifdef ENABLE_FRAGROUTE
  171. if (HAVE_OPT(FRAGROUTE))
  172. options.fragroute_args = safe_strdup(OPT_ARG(FRAGROUTE));
  173. options.fragroute_dir = FRAGROUTE_DIR_BOTH;
  174. if (HAVE_OPT(FRAGDIR)) {
  175. if (strcmp(OPT_ARG(FRAGDIR), "c2s") == 0) {
  176. options.fragroute_dir = FRAGROUTE_DIR_C2S;
  177. } else if (strcmp(OPT_ARG(FRAGDIR), "s2c") == 0) {
  178. options.fragroute_dir = FRAGROUTE_DIR_S2C;
  179. } else if (strcmp(OPT_ARG(FRAGDIR), "both") == 0) {
  180. options.fragroute_dir = FRAGROUTE_DIR_BOTH;
  181. } else {
  182. errx(-1, "Unknown --fragdir value: %s", OPT_ARG(FRAGDIR));
  183. }
  184. }
  185. #endif
  186. /* open up the input file */
  187. options.infile = safe_strdup(OPT_ARG(INFILE));
  188. if ((options.pin = pcap_open_offline(options.infile, ebuf)) == NULL)
  189. errx(-1, "Unable to open input pcap file: %s", ebuf);
  190. #ifdef HAVE_PCAP_SNAPSHOT
  191. if (pcap_snapshot(options.pin) < 65535)
  192. warnx("%s was captured using a snaplen of %d bytes. This may mean you have truncated packets.",
  193. options.infile,
  194. pcap_snapshot(options.pin));
  195. #endif
  196. }
  197. /**
  198. * Main loop to rewrite packets
  199. */
  200. int
  201. rewrite_packets(tcpedit_t *tcpedit_ctx, pcap_t *pin, pcap_dumper_t *pout)
  202. {
  203. tcpr_dir_t cache_result = TCPR_DIR_C2S; /* default to primary */
  204. struct pcap_pkthdr pkthdr, *pkthdr_ptr; /* packet header */
  205. const u_char *pktconst = NULL; /* packet from libpcap */
  206. u_char **pktdata = NULL;
  207. static u_char *pktdata_buff;
  208. static char *frag = NULL;
  209. COUNTER packetnum = 0;
  210. int rcode;
  211. #ifdef ENABLE_FRAGROUTE
  212. int frag_len, proto;
  213. #endif
  214. pkthdr_ptr = &pkthdr;
  215. if (pktdata_buff == NULL)
  216. pktdata_buff = (u_char *)safe_malloc(MAXPACKET);
  217. pktdata = &pktdata_buff;
  218. if (frag == NULL)
  219. frag = (char *)safe_malloc(MAXPACKET);
  220. /* MAIN LOOP
  221. * Keep sending while we have packets or until
  222. * we've sent enough packets
  223. */
  224. while ((pktconst = safe_pcap_next(pin, pkthdr_ptr)) != NULL) {
  225. packetnum++;
  226. dbgx(2, "packet " COUNTER_SPEC " caplen %d", packetnum, pkthdr.caplen);
  227. if (pkthdr.caplen > MAX_SNAPLEN)
  228. errx(-1, "Frame too big, caplen %d exceeds %d", pkthdr.caplen, MAX_SNAPLEN);
  229. /*
  230. * copy over the packet so we can pad it out if necessary and
  231. * because pcap_next() returns a const ptr
  232. */
  233. memcpy(*pktdata, pktconst, pkthdr.caplen);
  234. /* Dual nic processing? */
  235. if (options.cachedata != NULL) {
  236. cache_result = check_cache(options.cachedata, packetnum);
  237. }
  238. /* sometimes we should not send the packet, in such cases
  239. * no point in editing this packet at all, just write it to the
  240. * output file (note, we can't just remove it, or the tcpprep cache
  241. * file will lose it's indexing
  242. */
  243. if (cache_result == TCPR_DIR_NOSEND)
  244. goto WRITE_PACKET; /* still need to write it so cache stays in sync */
  245. if ((rcode = tcpedit_packet(tcpedit_ctx, &pkthdr_ptr, pktdata, cache_result)) == TCPEDIT_ERROR) {
  246. return rcode;
  247. } else if ((rcode == TCPEDIT_SOFT_ERROR) && HAVE_OPT(SKIP_SOFT_ERRORS)) {
  248. /* don't write packet */
  249. dbgx(1, "Packet " COUNTER_SPEC " is suppressed from being written due to soft errors", packetnum);
  250. continue;
  251. }
  252. #ifdef ENABLE_VERBOSE
  253. if (options.verbose)
  254. tcpdump_print(&tcpdump, pkthdr_ptr, *pktdata);
  255. #endif
  256. WRITE_PACKET:
  257. #ifdef ENABLE_FRAGROUTE
  258. if (options.frag_ctx == NULL) {
  259. /* write the packet when there's no fragrouting to be done */
  260. if (pkthdr_ptr->caplen)
  261. pcap_dump((u_char *)pout, pkthdr_ptr, *pktdata);
  262. } else {
  263. /* get the L3 protocol of the packet */
  264. proto = tcpedit_l3proto(tcpedit_ctx, AFTER_PROCESS, *pktdata, pkthdr_ptr->caplen);
  265. /* packet is IPv4/IPv6 AND needs to be fragmented */
  266. if ((proto == ETHERTYPE_IP || proto == ETHERTYPE_IP6) &&
  267. ((options.fragroute_dir == FRAGROUTE_DIR_BOTH) ||
  268. (cache_result == TCPR_DIR_C2S && options.fragroute_dir == FRAGROUTE_DIR_C2S) ||
  269. (cache_result == TCPR_DIR_S2C && options.fragroute_dir == FRAGROUTE_DIR_S2C))) {
  270. #ifdef DEBUG
  271. int i = 0;
  272. #endif
  273. if (fragroute_process(options.frag_ctx, *pktdata, pkthdr_ptr->caplen) < 0)
  274. errx(-1, "Error processing packet via fragroute: %s", options.frag_ctx->errbuf);
  275. while ((frag_len = fragroute_getfragment(options.frag_ctx, &frag)) > 0) {
  276. /* frags get the same timestamp as the original packet */
  277. dbgx(1, "processing packet " COUNTER_SPEC " frag: %u (%d)", packetnum, i++, frag_len);
  278. pkthdr_ptr->caplen = frag_len;
  279. pkthdr_ptr->len = frag_len;
  280. if (pkthdr_ptr->caplen)
  281. pcap_dump((u_char *)pout, pkthdr_ptr, (u_char *)frag);
  282. }
  283. } else {
  284. /* write the packet without fragroute */
  285. if (pkthdr_ptr->caplen)
  286. pcap_dump((u_char *)pout, pkthdr_ptr, *pktdata);
  287. }
  288. }
  289. #else
  290. /* write the packet when there's no fragrouting to be done */
  291. if (pkthdr_ptr->caplen)
  292. pcap_dump((u_char *)pout, pkthdr_ptr, *pktdata);
  293. #endif
  294. } /* while() */
  295. return 0;
  296. }