tcpbridge.c 7.2 KB

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