tcpbridge.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2022 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 (gettimeofday(&stats.start_time, NULL) < 0) {
  80. tcpedit_close(&tcpedit);
  81. err(-1, "gettimeofday() 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. #ifdef ENABLE_VERBOSE
  128. if (HAVE_OPT(VERBOSE))
  129. options.verbose = 1;
  130. if (HAVE_OPT(DECODE))
  131. options.tcpdump->args = safe_strdup(OPT_ARG(DECODE));
  132. #endif
  133. if (HAVE_OPT(UNIDIR))
  134. options.unidir = 1;
  135. if (HAVE_OPT(LIMIT))
  136. options.limit_send = OPT_VALUE_LIMIT; /* default is -1 */
  137. if ((intname = get_interface(intlist, OPT_ARG(INTF1))) == NULL) {
  138. if (!strncmp(OPT_ARG(INTF1), "netmap:", 7) || !strncmp(OPT_ARG(INTF1), "vale", 4))
  139. errx(-1,
  140. "Unable to connect to netmap interface %s. Ensure netmap module is installed (see INSTALL).",
  141. OPT_ARG(INTF1));
  142. else
  143. errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF1));
  144. }
  145. options.intf1 = safe_strdup(intname);
  146. if (HAVE_OPT(INTF2)) {
  147. if ((intname = get_interface(intlist, OPT_ARG(INTF2))) == NULL)
  148. errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF2));
  149. options.intf2 = safe_strdup(intname);
  150. }
  151. if (HAVE_OPT(MAC)) {
  152. int ct = STACKCT_OPT(MAC);
  153. char **list = (char **)STACKLST_OPT(MAC);
  154. int first = 1;
  155. do {
  156. char *p = *list++;
  157. if (first)
  158. mac2hex(p, (u_char *)options.intf1_mac, ETHER_ADDR_LEN);
  159. else
  160. mac2hex(p, (u_char *)options.intf2_mac, ETHER_ADDR_LEN);
  161. first = 0;
  162. } while (--ct > 0);
  163. }
  164. /*
  165. * Figure out MAC addresses of sending interface(s)
  166. * if user doesn't specify MAC address on CLI, query for it
  167. */
  168. if (memcmp(options.intf1_mac, "\00\00\00\00\00\00", ETHER_ADDR_LEN) == 0) {
  169. if ((sp = sendpacket_open(options.intf1, ebuf, TCPR_DIR_C2S, SP_TYPE_NONE, NULL)) == NULL)
  170. errx(-1, "Unable to open interface %s: %s", options.intf1, ebuf);
  171. if ((eth_buff = sendpacket_get_hwaddr(sp)) == NULL) {
  172. warnx("Unable to get MAC address: %s", sendpacket_geterr(sp));
  173. err(-1, "Please consult the man page for using the -M option.");
  174. }
  175. memcpy(options.intf1_mac, eth_buff, ETHER_ADDR_LEN);
  176. sendpacket_close(sp);
  177. }
  178. if (memcmp(options.intf2_mac, "\00\00\00\00\00\00", ETHER_ADDR_LEN) == 0) {
  179. if ((sp = sendpacket_open(options.intf2, ebuf, TCPR_DIR_S2C, SP_TYPE_NONE, NULL)) == NULL)
  180. errx(-1, "Unable to open interface %s: %s", options.intf2, ebuf);
  181. if ((eth_buff = sendpacket_get_hwaddr(sp)) == NULL) {
  182. warnx("Unable to get MAC address: %s", sendpacket_geterr(sp));
  183. err(-1, "Please consult the man page for using the -M option.");
  184. }
  185. memcpy(options.intf2_mac, eth_buff, ETHER_ADDR_LEN);
  186. sendpacket_close(sp);
  187. }
  188. /*
  189. * Open interfaces for sending & receiving
  190. */
  191. if ((options.pcap1 = pcap_open_live(options.intf1, options.snaplen, options.promisc, options.to_ms, ebuf)) == NULL)
  192. errx(-1, "Unable to open interface %s: %s", options.intf1, ebuf);
  193. if (strcmp(options.intf1, options.intf2) == 0)
  194. errx(-1, "Whoa tiger! You don't want to use %s twice!", options.intf1);
  195. /* we always have to open the other pcap handle to send, but we may not listen */
  196. if ((options.pcap2 = pcap_open_live(options.intf2, options.snaplen, options.promisc, options.to_ms, ebuf)) == NULL)
  197. errx(-1, "Unable to open interface %s: %s", options.intf2, ebuf);
  198. /* poll should be -1 to wait indefinitely */
  199. options.poll_timeout = -1;
  200. safe_free(intlist);
  201. }