tcpbridge.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 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. restore_stdin();
  93. return 0;
  94. }
  95. void
  96. init(void)
  97. {
  98. memset(&stats, 0, sizeof(stats));
  99. memset(&options, 0, sizeof(options));
  100. options.snaplen = 65535;
  101. options.promisc = 1;
  102. options.to_ms = 1;
  103. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  104. warnx("Unable to set STDERR to non-blocking: %s", strerror(errno));
  105. }
  106. void
  107. post_args(_U_ int argc, _U_ char *argv[])
  108. {
  109. char ebuf[SENDPACKET_ERRBUF_SIZE];
  110. struct tcpr_ether_addr *eth_buff;
  111. char *intname;
  112. sendpacket_t *sp;
  113. #ifdef ENABLE_PCAP_FINDALLDEVS
  114. interface_list_t *intlist = get_interface_list();
  115. #else
  116. interface_list_t *intlist = NULL;
  117. #endif
  118. #ifdef DEBUG
  119. if (HAVE_OPT(DBUG))
  120. debug = OPT_VALUE_DBUG;
  121. #else
  122. if (HAVE_OPT(DBUG))
  123. warn("not configured with --enable-debug. Debugging disabled.");
  124. #endif
  125. #ifdef ENABLE_VERBOSE
  126. if (HAVE_OPT(VERBOSE))
  127. options.verbose = 1;
  128. if (HAVE_OPT(DECODE))
  129. options.tcpdump->args = safe_strdup(OPT_ARG(DECODE));
  130. #endif
  131. if (HAVE_OPT(UNIDIR))
  132. options.unidir = 1;
  133. if (HAVE_OPT(LIMIT))
  134. options.limit_send = OPT_VALUE_LIMIT; /* default is -1 */
  135. if ((intname = get_interface(intlist, OPT_ARG(INTF1))) == NULL) {
  136. if (!strncmp(OPT_ARG(INTF1), "netmap:", 7) || !strncmp(OPT_ARG(INTF1), "vale", 4))
  137. errx(-1, "Unable to connect to netmap interface %s. Ensure netmap module is installed (see INSTALL).",
  138. OPT_ARG(INTF1));
  139. else
  140. errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF1));
  141. }
  142. options.intf1 = safe_strdup(intname);
  143. if (HAVE_OPT(INTF2)) {
  144. if ((intname = get_interface(intlist, OPT_ARG(INTF2))) == NULL)
  145. errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF2));
  146. options.intf2 = safe_strdup(intname);
  147. }
  148. if (HAVE_OPT(MAC)) {
  149. int ct = STACKCT_OPT(MAC);
  150. char **list = (char**)STACKLST_OPT(MAC);
  151. int first = 1;
  152. do {
  153. char *p = *list++;
  154. if (first)
  155. mac2hex(p, (u_char *)options.intf1_mac, ETHER_ADDR_LEN);
  156. else
  157. mac2hex(p, (u_char *)options.intf2_mac, ETHER_ADDR_LEN);
  158. first = 0;
  159. } while (--ct > 0);
  160. }
  161. /*
  162. * Figure out MAC addresses of sending interface(s)
  163. * if user doesn't specify MAC address on CLI, query for it
  164. */
  165. if (memcmp(options.intf1_mac, "\00\00\00\00\00\00", ETHER_ADDR_LEN) == 0) {
  166. if ((sp = sendpacket_open(options.intf1, ebuf, TCPR_DIR_C2S, SP_TYPE_NONE, NULL)) == NULL)
  167. errx(-1, "Unable to open interface %s: %s", options.intf1, ebuf);
  168. if ((eth_buff = sendpacket_get_hwaddr(sp)) == NULL) {
  169. warnx("Unable to get MAC address: %s", sendpacket_geterr(sp));
  170. err(-1, "Please consult the man page for using the -M option.");
  171. }
  172. memcpy(options.intf1_mac, eth_buff, ETHER_ADDR_LEN);
  173. sendpacket_close(sp);
  174. }
  175. if (memcmp(options.intf2_mac, "\00\00\00\00\00\00", ETHER_ADDR_LEN) == 0) {
  176. if ((sp = sendpacket_open(options.intf2, ebuf, TCPR_DIR_S2C, SP_TYPE_NONE, NULL)) == NULL)
  177. errx(-1, "Unable to open interface %s: %s", options.intf2, ebuf);
  178. if ((eth_buff = sendpacket_get_hwaddr(sp)) == NULL) {
  179. warnx("Unable to get MAC address: %s", sendpacket_geterr(sp));
  180. err(-1, "Please consult the man page for using the -M option.");
  181. }
  182. memcpy(options.intf2_mac, eth_buff, ETHER_ADDR_LEN);
  183. sendpacket_close(sp);
  184. }
  185. /*
  186. * Open interfaces for sending & receiving
  187. */
  188. if ((options.pcap1 = pcap_open_live(options.intf1, options.snaplen,
  189. options.promisc, options.to_ms, ebuf)) == NULL)
  190. errx(-1, "Unable to open interface %s: %s", options.intf1, ebuf);
  191. if (strcmp(options.intf1, options.intf2) == 0)
  192. errx(-1, "Whoa tiger! You don't want to use %s twice!", options.intf1);
  193. /* we always have to open the other pcap handle to send, but we may not listen */
  194. if ((options.pcap2 = pcap_open_live(options.intf2, options.snaplen,
  195. options.promisc, options.to_ms, ebuf)) == NULL)
  196. errx(-1, "Unable to open interface %s: %s", options.intf2, ebuf);
  197. /* poll should be -1 to wait indefinitely */
  198. options.poll_timeout = -1;
  199. safe_free(intlist);
  200. }