tcpbridge.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "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. tcpedit_close(&tcpedit);
  67. errx(-1, "Unable to parse args: %s", tcpedit_geterr(tcpedit));
  68. } else if (rcode == 1) {
  69. warnx("%s", tcpedit_geterr(tcpedit));
  70. }
  71. if (tcpedit_validate(tcpedit) < 0) {
  72. tcpedit_close(&tcpedit);
  73. errx(-1, "Unable to edit packets given options:\n%s",
  74. tcpedit_geterr(tcpedit));
  75. }
  76. #ifdef ENABLE_VERBOSE
  77. if (options.verbose) {
  78. options.tcpdump = (tcpdump_t*)safe_malloc(sizeof(tcpdump_t));
  79. tcpdump_open(options.tcpdump, options.pcap1);
  80. }
  81. #endif
  82. if (gettimeofday(&stats.start_time, NULL) < 0) {
  83. tcpedit_close(&tcpedit);
  84. err(-1, "gettimeofday() failed");
  85. }
  86. /* process packets */
  87. do_bridge(&options, tcpedit);
  88. /* clean up after ourselves */
  89. pcap_close(options.pcap1);
  90. if (options.unidir) {
  91. pcap_close(options.pcap2);
  92. }
  93. tcpedit_close(&tcpedit);
  94. #ifdef ENABLE_VERBOSE
  95. tcpdump_close(options.tcpdump);
  96. #endif
  97. restore_stdin();
  98. return 0;
  99. }
  100. void
  101. init(void)
  102. {
  103. memset(&stats, 0, sizeof(stats));
  104. memset(&options, 0, sizeof(options));
  105. options.snaplen = 65535;
  106. options.promisc = 1;
  107. options.to_ms = 1;
  108. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  109. warnx("Unable to set STDERR to non-blocking: %s", strerror(errno));
  110. }
  111. void
  112. post_args(_U_ int argc, _U_ char *argv[])
  113. {
  114. char ebuf[SENDPACKET_ERRBUF_SIZE];
  115. struct tcpr_ether_addr *eth_buff;
  116. char *intname;
  117. sendpacket_t *sp;
  118. #ifdef ENABLE_PCAP_FINDALLDEVS
  119. interface_list_t *intlist = get_interface_list();
  120. #else
  121. interface_list_t *intlist = NULL;
  122. #endif
  123. #ifdef DEBUG
  124. if (HAVE_OPT(DBUG))
  125. debug = OPT_VALUE_DBUG;
  126. #else
  127. if (HAVE_OPT(DBUG))
  128. warn("not configured with --enable-debug. Debugging disabled.");
  129. #endif
  130. #ifdef ENABLE_VERBOSE
  131. if (HAVE_OPT(VERBOSE))
  132. options.verbose = 1;
  133. if (HAVE_OPT(DECODE))
  134. options.tcpdump->args = safe_strdup(OPT_ARG(DECODE));
  135. #endif
  136. if (HAVE_OPT(UNIDIR))
  137. options.unidir = 1;
  138. if (HAVE_OPT(LIMIT))
  139. options.limit_send = OPT_VALUE_LIMIT; /* default is -1 */
  140. if ((intname = get_interface(intlist, OPT_ARG(INTF1))) == NULL) {
  141. if (!strncmp(OPT_ARG(INTF1), "netmap:", 7) || !strncmp(OPT_ARG(INTF1), "vale", 4))
  142. errx(-1, "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,
  194. options.promisc, options.to_ms, ebuf)) == NULL)
  195. errx(-1, "Unable to open interface %s: %s", options.intf1, ebuf);
  196. if (strcmp(options.intf1, options.intf2) == 0)
  197. errx(-1, "Whoa tiger! You don't want to use %s twice!", options.intf1);
  198. /* we always have to open the other pcap handle to send, but we may not listen */
  199. if ((options.pcap2 = pcap_open_live(options.intf2, options.snaplen,
  200. options.promisc, options.to_ms, ebuf)) == NULL)
  201. errx(-1, "Unable to open interface %s: %s", options.intf2, ebuf);
  202. /* poll should be -1 to wait indefinitely */
  203. options.poll_timeout = -1;
  204. safe_free(intlist);
  205. }