tcpbridge.c 7.3 KB

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