tcprewrite.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* $Id: tcprewrite.c 2001 2008-04-27 06:31:29Z aturner $ */
  2. /*
  3. * Copyright (c) 2004-2007 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * Purpose: Modify packets in a pcap file based on rules provided by the
  33. * user to offload work from tcpreplay and provide a easier means of
  34. * reproducing traffic for testing purposes.
  35. */
  36. #include "config.h"
  37. #include "defines.h"
  38. #include "common.h"
  39. #include <ctype.h>
  40. #include <fcntl.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <sys/types.h>
  45. #include <unistd.h>
  46. #include <errno.h>
  47. #include "tcprewrite.h"
  48. #include "tcprewrite_opts.h"
  49. #include "tcpedit/tcpedit.h"
  50. #ifdef DEBUG
  51. int debug;
  52. #endif
  53. #ifdef ENABLE_VERBOSE
  54. /* tcpdump handle */
  55. tcpdump_t tcpdump;
  56. #endif
  57. tcprewrite_opt_t options;
  58. tcpedit_t *tcpedit;
  59. /* local functions */
  60. void tcprewrite_init(void);
  61. void post_args(int argc, char *argv[]);
  62. void verify_input_pcap(pcap_t *pcap);
  63. int rewrite_packets(tcpedit_t *tcpedit, pcap_t *pin, pcap_dumper_t *pout);
  64. int
  65. main(int argc, char *argv[])
  66. {
  67. int optct, rcode;
  68. pcap_t *dlt_pcap;
  69. #ifdef ENABLE_FRAGROUTE
  70. char ebuf[FRAGROUTE_ERRBUF_LEN];
  71. #endif
  72. tcprewrite_init();
  73. /* call autoopts to process arguments */
  74. optct = optionProcess(&tcprewriteOptions, argc, argv);
  75. argc -= optct;
  76. argv += optct;
  77. /* parse the tcprewrite args */
  78. post_args(argc, argv);
  79. /* init tcpedit context */
  80. if (tcpedit_init(&tcpedit, pcap_datalink(options.pin)) < 0) {
  81. errx(1, "Error initializing tcpedit: %s", tcpedit_geterr(tcpedit));
  82. }
  83. /* parse the tcpedit args */
  84. rcode = tcpedit_post_args(&tcpedit);
  85. if (rcode < 0) {
  86. errx(1, "Unable to parse args: %s", tcpedit_geterr(tcpedit));
  87. } else if (rcode == 1) {
  88. warnx("%s", tcpedit_geterr(tcpedit));
  89. }
  90. if (tcpedit_validate(tcpedit) < 0) {
  91. errx(1, "Unable to edit packets given options:\n%s",
  92. tcpedit_geterr(tcpedit));
  93. }
  94. /* open up the output file */
  95. options.outfile = safe_strdup(OPT_ARG(OUTFILE));
  96. dbgx(1, "Rewriting DLT to %s",
  97. pcap_datalink_val_to_name(tcpedit_get_output_dlt(tcpedit)));
  98. if ((dlt_pcap = pcap_open_dead(tcpedit_get_output_dlt(tcpedit), 65535)) == NULL)
  99. err(1, "Unable to open dead pcap handle.");
  100. dbgx(1, "DLT of dlt_pcap is %s",
  101. pcap_datalink_val_to_name(pcap_datalink(dlt_pcap)));
  102. #ifdef ENABLE_FRAGROUTE
  103. if (options.fragroute_args) {
  104. if ((options.frag_ctx = fragroute_init(65535, pcap_datalink(dlt_pcap), options.fragroute_args, ebuf)) == NULL)
  105. errx(1, "%s", ebuf);
  106. }
  107. #endif
  108. #ifdef ENABLE_VERBOSE
  109. if (options.verbose) {
  110. tcpdump_open(&tcpdump, dlt_pcap);
  111. }
  112. #endif
  113. if ((options.pout = pcap_dump_open(dlt_pcap, options.outfile)) == NULL)
  114. errx(1, "Unable to open output pcap file: %s", pcap_geterr(dlt_pcap));
  115. pcap_close(dlt_pcap);
  116. /* rewrite packets */
  117. if (rewrite_packets(tcpedit, options.pin, options.pout) != 0)
  118. errx(1, "Error rewriting packets: %s", tcpedit_geterr(tcpedit));
  119. /* clean up after ourselves */
  120. pcap_dump_close(options.pout);
  121. pcap_close(options.pin);
  122. #ifdef ENABLE_VERBOSE
  123. tcpdump_close(&tcpdump);
  124. #endif
  125. #ifdef ENABLE_DMALLOC
  126. dmalloc_shutdown();
  127. #endif
  128. return 0;
  129. }
  130. void
  131. tcprewrite_init(void)
  132. {
  133. memset(&options, 0, sizeof(options));
  134. #ifdef ENABLE_VERBOSE
  135. /* clear out tcpdump struct */
  136. memset(&tcpdump, '\0', sizeof(tcpdump_t));
  137. #endif
  138. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  139. warnx("Unable to set STDERR to non-blocking: %s", strerror(errno));
  140. }
  141. /**
  142. * post AutoGen argument processing
  143. */
  144. void
  145. post_args(_U_ int argc, _U_ char *argv[])
  146. {
  147. char ebuf[PCAP_ERRBUF_SIZE];
  148. #ifdef DEBUG
  149. if (HAVE_OPT(DBUG))
  150. debug = OPT_VALUE_DBUG;
  151. #else
  152. if (HAVE_OPT(DBUG))
  153. warn("not configured with --enable-debug. Debugging disabled.");
  154. #endif
  155. #ifdef ENABLE_VERBOSE
  156. if (HAVE_OPT(VERBOSE))
  157. options.verbose = 1;
  158. if (HAVE_OPT(DECODE))
  159. tcpdump.args = safe_strdup(OPT_ARG(DECODE));
  160. #endif
  161. #ifdef ENABLE_FRAGROUTE
  162. if (HAVE_OPT(FRAGROUTE))
  163. options.fragroute_args = safe_strdup(OPT_ARG(FRAGROUTE));
  164. options.fragroute_dir = FRAGROUTE_DIR_BOTH;
  165. if (HAVE_OPT(FRAGDIR)) {
  166. if (strcmp(OPT_ARG(FRAGDIR), "c2s") == 0) {
  167. options.fragroute_dir = FRAGROUTE_DIR_C2S;
  168. } else if (strcmp(OPT_ARG(FRAGDIR), "s2c") == 0) {
  169. options.fragroute_dir = FRAGROUTE_DIR_S2C;
  170. } else if (strcmp(OPT_ARG(FRAGDIR), "both") == 0) {
  171. options.fragroute_dir = FRAGROUTE_DIR_BOTH;
  172. } else {
  173. errx(1, "Unknown --fragdir value: %s", OPT_ARG(FRAGDIR));
  174. }
  175. }
  176. #endif
  177. /* open up the input file */
  178. options.infile = safe_strdup(OPT_ARG(INFILE));
  179. if ((options.pin = pcap_open_offline(options.infile, ebuf)) == NULL)
  180. errx(1, "Unable to open input pcap file: %s", ebuf);
  181. }
  182. /**
  183. * Main loop to rewrite packets
  184. */
  185. int
  186. rewrite_packets(tcpedit_t *tcpedit, pcap_t *pin, pcap_dumper_t *pout)
  187. {
  188. tcpr_dir_t cache_result = TCPR_DIR_C2S; /* default to primary */
  189. struct pcap_pkthdr pkthdr, *pkthdr_ptr; /* packet header */
  190. const u_char *pktdata = NULL; /* packet from libpcap */
  191. u_char **packet = NULL; /* packet from tcpedit */
  192. static char *frag = NULL;
  193. COUNTER packetnum = 0;
  194. int rcode, frag_len, i;
  195. pkthdr_ptr = &pkthdr;
  196. if (frag == NULL)
  197. frag = (char *)safe_malloc(65535); /* mtu size */
  198. /* MAIN LOOP
  199. * Keep sending while we have packets or until
  200. * we've sent enough packets
  201. */
  202. while ((pktdata = pcap_next(pin, pkthdr_ptr)) != NULL) {
  203. packetnum++;
  204. dbgx(2, "packet " COUNTER_SPEC " caplen %d", packetnum, pkthdr.caplen);
  205. packet = (u_char **)&pktdata;
  206. #ifdef ENABLE_VERBOSE
  207. if (options.verbose)
  208. tcpdump_print(&tcpdump, pkthdr_ptr, pktdata);
  209. #endif
  210. /* Dual nic processing? */
  211. if (options.cachedata != NULL) {
  212. cache_result = check_cache(options.cachedata, packetnum);
  213. }
  214. /* sometimes we should not send the packet, in such cases
  215. * no point in editing this packet at all, just write it to the
  216. * output file (note, we can't just remove it, or the tcpprep cache
  217. * file will loose it's indexing
  218. */
  219. if (cache_result == TCPR_DIR_NOSEND)
  220. goto WRITE_PACKET; /* still need to write it so cache stays in sync */
  221. if ((rcode = tcpedit_packet(tcpedit, &pkthdr_ptr, packet, cache_result)) == TCPEDIT_ERROR) {
  222. return -1;
  223. } else if ((rcode == TCPEDIT_SOFT_ERROR) && HAVE_OPT(SKIP_SOFT_ERRORS)) {
  224. /* don't write packet */
  225. dbgx(1, "Packet " COUNTER_SPEC " is suppressed from being written due to soft errors", packetnum);
  226. continue;
  227. }
  228. WRITE_PACKET:
  229. #ifdef ENABLE_FRAGROUTE
  230. if (options.frag_ctx == NULL) {
  231. /* write the packet when there's no fragrouting to be done */
  232. pcap_dump((u_char *)pout, pkthdr_ptr, *packet);
  233. } else {
  234. /* packet needs to be fragmented */
  235. if ((options.fragroute_dir == FRAGROUTE_DIR_BOTH) ||
  236. (cache_result == TCPR_DIR_C2S && options.fragroute_dir == FRAGROUTE_DIR_C2S) ||
  237. (cache_result == TCPR_DIR_S2C && options.fragroute_dir == FRAGROUTE_DIR_S2C)) {
  238. if (fragroute_process(options.frag_ctx, *packet, pkthdr_ptr->caplen) < 0)
  239. errx(1, "Error processing packet via fragroute: %s", options.frag_ctx->errbuf);
  240. i = 0;
  241. while ((frag_len = fragroute_getfragment(options.frag_ctx, &frag)) > 0) {
  242. /* frags get the same timestamp as the original packet */
  243. dbgx(1, "processing packet " COUNTER_SPEC " frag: %u (%d)", packetnum, i++, frag_len);
  244. pkthdr_ptr->caplen = frag_len;
  245. pkthdr_ptr->len = frag_len;
  246. pcap_dump((u_char *)pout, pkthdr_ptr, (u_char *)frag);
  247. }
  248. } else {
  249. /* write the packet without fragroute */
  250. pcap_dump((u_char *)pout, pkthdr_ptr, *packet);
  251. }
  252. }
  253. #else
  254. /* write the packet when there's no fragrouting to be done */
  255. pcap_dump((u_char *)pout, pkthdr_ptr, *packet);
  256. #endif
  257. } /* while() */
  258. return 0;
  259. }
  260. /*
  261. Local Variables:
  262. mode:c
  263. indent-tabs-mode:nil
  264. c-basic-offset:4
  265. End:
  266. */