tcpbridge.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* $Id: tcpbridge.c 2127 2009-01-10 06:06:46Z aturner $ */
  2. /*
  3. * Copyright (c) 2004-2005 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 "tcpbridge.h"
  48. #include "tcpbridge_opts.h"
  49. #include "bridge.h"
  50. #include "tcpedit/tcpedit.h"
  51. #include "send_packets.h"
  52. #ifdef DEBUG
  53. int debug;
  54. #endif
  55. COUNTER bytes_sent, total_bytes, failed, pkts_sent, cache_packets;
  56. struct timeval begin, end;
  57. volatile int didsig;
  58. tcpbridge_opt_t options;
  59. tcpedit_t *tcpedit;
  60. /* local functions */
  61. void init(void);
  62. void post_args(int argc, char *argv[]);
  63. int
  64. main(int argc, char *argv[])
  65. {
  66. int optct, rcode;
  67. init();
  68. /* call autoopts to process arguments */
  69. optct = optionProcess(&tcpbridgeOptions, argc, argv);
  70. argc -= optct;
  71. argv += optct;
  72. post_args(argc, argv);
  73. /* init tcpedit context */
  74. if (tcpedit_init(&tcpedit, pcap_datalink(options.pcap1)) < 0) {
  75. errx(-1, "Error initializing tcpedit: %s", tcpedit_geterr(tcpedit));
  76. }
  77. /* parse the tcpedit args */
  78. rcode = tcpedit_post_args(&tcpedit);
  79. if (rcode < 0) {
  80. errx(-1, "Unable to parse args: %s", tcpedit_geterr(tcpedit));
  81. } else if (rcode == 1) {
  82. warnx("%s", tcpedit_geterr(tcpedit));
  83. }
  84. if (tcpedit_validate(tcpedit) < 0) {
  85. errx(-1, "Unable to edit packets given options:\n%s",
  86. tcpedit_geterr(tcpedit));
  87. }
  88. #ifdef ENABLE_VERBOSE
  89. if (options.verbose) {
  90. options.tcpdump = (tcpdump_t*)safe_malloc(sizeof(tcpdump_t));
  91. tcpdump_open(options.tcpdump, options.pcap1);
  92. }
  93. #endif
  94. if (gettimeofday(&begin, NULL) < 0)
  95. err(-1, "gettimeofday() failed");
  96. /* process packets */
  97. do_bridge(&options, tcpedit);
  98. /* clean up after ourselves */
  99. pcap_close(options.pcap1);
  100. if (options.unidir) {
  101. pcap_close(options.pcap2);
  102. }
  103. #ifdef ENABLE_VERBOSE
  104. tcpdump_close(options.tcpdump);
  105. #endif
  106. return 0;
  107. }
  108. void
  109. init(void)
  110. {
  111. bytes_sent = total_bytes = failed = pkts_sent = cache_packets = 0;
  112. memset(&options, 0, sizeof(options));
  113. options.snaplen = 65535;
  114. options.promisc = 1;
  115. options.to_ms = 1;
  116. total_bytes = 0;
  117. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  118. warnx("Unable to set STDERR to non-blocking: %s", strerror(errno));
  119. }
  120. void
  121. post_args(_U_ int argc, _U_ char *argv[])
  122. {
  123. char ebuf[SENDPACKET_ERRBUF_SIZE];
  124. struct tcpr_ether_addr *eth_buff;
  125. char *intname;
  126. sendpacket_t *sp;
  127. #ifdef ENABLE_PCAP_FINDALLDEVS
  128. interface_list_t *intlist = get_interface_list();
  129. #else
  130. interface_list_t *intlist = NULL;
  131. #endif
  132. #ifdef DEBUG
  133. if (HAVE_OPT(DBUG))
  134. debug = OPT_VALUE_DBUG;
  135. #else
  136. if (HAVE_OPT(DBUG))
  137. warn("not configured with --enable-debug. Debugging disabled.");
  138. #endif
  139. #ifdef ENABLE_VERBOSE
  140. if (HAVE_OPT(VERBOSE))
  141. options.verbose = 1;
  142. if (HAVE_OPT(DECODE))
  143. options.tcpdump->args = safe_strdup(OPT_ARG(DECODE));
  144. #endif
  145. if (HAVE_OPT(UNIDIR))
  146. options.unidir = 1;
  147. if (HAVE_OPT(LIMIT))
  148. options.limit_send = OPT_VALUE_LIMIT; /* default is -1 */
  149. if ((intname = get_interface(intlist, OPT_ARG(INTF1))) == NULL)
  150. errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF1));
  151. options.intf1 = safe_strdup(intname);
  152. if (HAVE_OPT(INTF2)) {
  153. if ((intname = get_interface(intlist, OPT_ARG(INTF2))) == NULL)
  154. errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF2));
  155. options.intf2 = safe_strdup(intname);
  156. }
  157. if (HAVE_OPT(MAC)) {
  158. int ct = STACKCT_OPT(MAC);
  159. char **list = STACKLST_OPT(MAC);
  160. int first = 1;
  161. do {
  162. char *p = *list++;
  163. if (first)
  164. mac2hex(p, (u_char *)options.intf1_mac, ETHER_ADDR_LEN);
  165. else
  166. mac2hex(p, (u_char *)options.intf2_mac, ETHER_ADDR_LEN);
  167. first = 0;
  168. } while (--ct > 0);
  169. }
  170. /*
  171. * Figure out MAC addresses of sending interface(s)
  172. * if user doesn't specify MAC address on CLI, query for it
  173. */
  174. if (memcmp(options.intf1_mac, "\00\00\00\00\00\00", ETHER_ADDR_LEN) == 0) {
  175. if ((sp = sendpacket_open(options.intf1, ebuf, TCPR_DIR_C2S)) == NULL)
  176. errx(-1, "Unable to open interface %s: %s", options.intf1, ebuf);
  177. if ((eth_buff = sendpacket_get_hwaddr(sp)) == NULL) {
  178. warnx("Unable to get MAC address: %s", sendpacket_geterr(sp));
  179. err(-1, "Please consult the man page for using the -M option.");
  180. }
  181. sendpacket_close(sp);
  182. memcpy(options.intf1_mac, eth_buff, ETHER_ADDR_LEN);
  183. }
  184. if (memcmp(options.intf2_mac, "\00\00\00\00\00\00", ETHER_ADDR_LEN) == 0) {
  185. if ((sp = sendpacket_open(options.intf2, ebuf, TCPR_DIR_S2C)) == NULL)
  186. errx(-1, "Unable to open interface %s: %s", options.intf2, ebuf);
  187. if ((eth_buff = sendpacket_get_hwaddr(sp)) == NULL) {
  188. warnx("Unable to get MAC address: %s", sendpacket_geterr(sp));
  189. err(-1, "Please consult the man page for using the -M option.");
  190. }
  191. sendpacket_close(sp);
  192. memcpy(options.intf2_mac, eth_buff, ETHER_ADDR_LEN);
  193. }
  194. /*
  195. * Open interfaces for sending & receiving
  196. */
  197. if ((options.pcap1 = pcap_open_live(options.intf1, options.snaplen,
  198. options.promisc, options.to_ms, ebuf)) == NULL)
  199. errx(-1, "Unable to open interface %s: %s", options.intf1, ebuf);
  200. if (strcmp(options.intf1, options.intf2) == 0)
  201. errx(-1, "Whoa tiger! You don't want to use %s twice!", options.intf1);
  202. /* we always have to open the other pcap handle to send, but we may not listen */
  203. if ((options.pcap2 = pcap_open_live(options.intf2, options.snaplen,
  204. options.promisc, options.to_ms, ebuf)) == NULL)
  205. errx(-1, "Unable to open interface %s: %s", options.intf2, ebuf);
  206. /* poll should be -1 to wait indefinitely */
  207. options.poll_timeout = -1;
  208. }