1
0

bridge.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #include "bridge.h"
  20. #include "config.h"
  21. #include "common.h"
  22. #include "tcpbridge.h"
  23. #include <errno.h>
  24. #include <signal.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/time.h>
  28. extern tcpreplay_stats_t stats;
  29. volatile bool didsig;
  30. static void live_callback(u_char *, const struct pcap_pkthdr *, const u_char *);
  31. static void signal_catcher(int signo);
  32. /**
  33. * First, prep our RB Tree which tracks where each (source)
  34. * MAC really lives so we don't create really nasty network
  35. * storms.
  36. */
  37. static struct macsrc_t *new_node(void);
  38. RB_HEAD(macsrc_tree, macsrc_t) macsrc_root;
  39. static int
  40. rbmacsrc_comp(struct macsrc_t *a, struct macsrc_t *b)
  41. {
  42. return (memcmp(a->key, b->key, ETHER_ADDR_LEN));
  43. }
  44. RB_PROTOTYPE(macsrc_tree, macsrc_t, node, rbmacsrc_comp)
  45. RB_GENERATE(macsrc_tree, macsrc_t, node, rbmacsrc_comp)
  46. /**
  47. * create a new node... Malloc's memory
  48. */
  49. struct macsrc_t *
  50. new_node(void)
  51. {
  52. struct macsrc_t *node;
  53. node = (struct macsrc_t *)safe_malloc(sizeof(struct macsrc_t));
  54. memset(node, '\0', sizeof(struct macsrc_t));
  55. return (node);
  56. }
  57. /**
  58. * main loop for bridging in only one direction
  59. * optimized to not use poll(), but rather libpcap's builtin pcap_loop()
  60. */
  61. static void
  62. do_bridge_unidirectional(tcpbridge_opt_t *options, tcpedit_t *tcpedit)
  63. {
  64. struct live_data_t livedata;
  65. int retcode;
  66. assert(options);
  67. assert(tcpedit);
  68. livedata.tcpedit = tcpedit;
  69. livedata.source = PCAP_INT1;
  70. livedata.pcap = options->pcap1;
  71. livedata.options = options;
  72. if ((retcode = pcap_loop(options->pcap1, (int)options->limit_send, live_callback, (u_char *)&livedata)) < 0) {
  73. warnx("Error in %d pcap_loop(): %s", retcode, pcap_geterr(options->pcap1));
  74. }
  75. }
  76. /**
  77. * main loop for bridging in both directions. Since we dealing with two handles
  78. * we need to poll() on them which isn't the most efficient
  79. */
  80. static void
  81. do_bridge_bidirectional(tcpbridge_opt_t *options, tcpedit_t *tcpedit)
  82. {
  83. struct pollfd polls[2]; /* one for left & right pcap */
  84. int pollresult, pollcount, timeout;
  85. struct live_data_t livedata;
  86. assert(options);
  87. assert(tcpedit);
  88. livedata.tcpedit = tcpedit;
  89. livedata.options = options;
  90. /*
  91. * loop until ctrl-C or we've sent enough packets
  92. * note that if -L wasn't specified, limit_send is
  93. * set to 0 so this will loop infinately
  94. */
  95. while ((options->limit_send == 0) || (options->limit_send > stats.pkts_sent)) {
  96. if (didsig)
  97. break;
  98. dbgx(3, "limit_send: " COUNTER_SPEC " \t pkts_sent: " COUNTER_SPEC, options->limit_send, stats.pkts_sent);
  99. /* reset the result codes */
  100. polls[PCAP_INT1].revents = 0;
  101. polls[PCAP_INT1].events = POLLIN;
  102. polls[PCAP_INT1].fd = pcap_fileno(options->pcap1);
  103. polls[PCAP_INT2].revents = 0;
  104. polls[PCAP_INT2].events = POLLIN;
  105. polls[PCAP_INT2].fd = pcap_fileno(options->pcap2);
  106. timeout = options->poll_timeout;
  107. pollcount = 2;
  108. /* poll for a packet on the two interfaces */
  109. pollresult = poll(polls, pollcount, timeout);
  110. /* poll has returned, process the result */
  111. if (pollresult > 0) {
  112. dbgx(3, "pollresult: %d", pollresult);
  113. /* success, got one or more packets */
  114. if (polls[PCAP_INT1].revents > 0) {
  115. dbg(5, "Processing first interface");
  116. livedata.source = PCAP_INT1;
  117. livedata.pcap = options->pcap1;
  118. pcap_dispatch(options->pcap1, -1, (pcap_handler)live_callback, (u_char *)&livedata);
  119. }
  120. /* check the other interface?? */
  121. if (polls[PCAP_INT2].revents > 0) {
  122. dbg(5, "Processing second interface");
  123. livedata.source = PCAP_INT2;
  124. livedata.pcap = options->pcap2;
  125. pcap_dispatch(options->pcap2, -1, (pcap_handler)live_callback, (u_char *)&livedata);
  126. }
  127. } else if (pollresult == 0) {
  128. dbg(3, "poll timeout exceeded...");
  129. /* do something here? */
  130. } else {
  131. /* poll error, probably a Ctrl-C */
  132. warnx("poll() error: %s", strerror(errno));
  133. }
  134. /* go back to the top of the loop */
  135. }
  136. } /* do_bridge_bidirectional() */
  137. /**
  138. * Main entry point to bridging. Does some initial setup and then calls the
  139. * correct loop (unidirectional or bidirectional)
  140. */
  141. void
  142. do_bridge(tcpbridge_opt_t *options, tcpedit_t *tcpedit)
  143. {
  144. /* do we apply a bpf filter? */
  145. if (options->bpf.filter != NULL) {
  146. /* compile filter */
  147. dbgx(2, "Try to compile pcap bpf filter: %s", options->bpf.filter);
  148. if (pcap_compile(options->pcap1, &options->bpf.program, options->bpf.filter, options->bpf.optimize, 0) != 0) {
  149. errx(-1, "Error compiling BPF filter: %s", pcap_geterr(options->pcap1));
  150. }
  151. /* apply filter */
  152. pcap_setfilter(options->pcap1, &options->bpf.program);
  153. pcap_freecode(&options->bpf.program);
  154. /* same for other interface if applicable */
  155. if (options->unidir == 0) {
  156. /* compile filter */
  157. dbgx(2, "Try to compile pcap bpf filter: %s", options->bpf.filter);
  158. if (pcap_compile(options->pcap2, &options->bpf.program, options->bpf.filter, options->bpf.optimize, 0) !=
  159. 0) {
  160. errx(-1, "Error compiling BPF filter: %s", pcap_geterr(options->pcap2));
  161. }
  162. /* apply filter */
  163. pcap_setfilter(options->pcap2, &options->bpf.program);
  164. pcap_freecode(&options->bpf.program);
  165. }
  166. }
  167. /* register signals */
  168. didsig = 0;
  169. (void)signal(SIGINT, signal_catcher);
  170. if (options->unidir == 1) {
  171. do_bridge_unidirectional(options, tcpedit);
  172. } else {
  173. do_bridge_bidirectional(options, tcpedit);
  174. }
  175. if (get_current_time(&stats.end_time) < 0)
  176. errx(-1, "get_current_time() failed: %s", strerror(errno));
  177. packet_stats(&stats);
  178. }
  179. /**
  180. * This is the callback we use with pcap_dispatch to process
  181. * each packet received by libpcap on the two interfaces.
  182. * Need to return > 0 to denote success
  183. */
  184. static void
  185. live_callback(u_char *usr_data, const struct pcap_pkthdr *const_pkthdr, const u_char *nextpkt)
  186. {
  187. struct live_data_t *livedata = (struct live_data_t *)usr_data;
  188. struct pcap_pkthdr pkthdr_buf = *const_pkthdr;
  189. struct pcap_pkthdr *pkthdr = &pkthdr_buf;
  190. ipv4_hdr_t *ip_hdr = NULL;
  191. ipv6_hdr_t *ip6_hdr = NULL;
  192. pcap_t *send = NULL;
  193. static u_char *pktdata = NULL; /* full packet buffer */
  194. int cache_mode;
  195. static unsigned long packetnum = 0;
  196. struct macsrc_t *node, finder; /* rb tree nodes */
  197. #ifdef DEBUG
  198. u_char dstmac[ETHER_ADDR_LEN];
  199. #endif
  200. u_int16_t l2proto;
  201. packetnum++;
  202. dbgx(2, "packet %lu caplen %d", packetnum, pkthdr->caplen);
  203. /* only malloc the first time */
  204. if (pktdata == NULL) {
  205. /* create packet buffers */
  206. pktdata = (u_char *)safe_malloc(MAXPACKET);
  207. } else {
  208. /* zero out the old packet info */
  209. memset(pktdata, '\0', MAXPACKET);
  210. }
  211. /* copy the packet to our buffer */
  212. memcpy(pktdata, nextpkt, pkthdr->caplen);
  213. #ifdef ENABLE_VERBOSE
  214. /* decode packet? */
  215. if (livedata->options->verbose)
  216. tcpdump_print(livedata->options->tcpdump, pkthdr, nextpkt);
  217. #endif
  218. /* lookup our source MAC in the tree */
  219. memcpy(&finder.key, &pktdata[ETHER_ADDR_LEN], ETHER_ADDR_LEN);
  220. #ifdef DEBUG
  221. memcpy(&dstmac, pktdata, ETHER_ADDR_LEN);
  222. dbgx(1, "SRC MAC: " MAC_FORMAT "\tDST MAC: " MAC_FORMAT, MAC_STR(finder.key), MAC_STR(dstmac));
  223. #endif
  224. /* first, is this a packet sent locally? If so, ignore it */
  225. if ((memcmp(livedata->options->intf1_mac, &finder.key, ETHER_ADDR_LEN)) == 0) {
  226. dbgx(1, "Packet matches the MAC of %s, skipping.", livedata->options->intf1);
  227. return;
  228. } else if ((memcmp(livedata->options->intf2_mac, &finder.key, ETHER_ADDR_LEN)) == 0) {
  229. dbgx(1, "Packet matches the MAC of %s, skipping.", livedata->options->intf2);
  230. return;
  231. }
  232. node = RB_FIND(macsrc_tree, &macsrc_root, &finder);
  233. /* if we can't find the node, build a new one */
  234. if (node == NULL) {
  235. dbg(1, "Unable to find MAC in the tree");
  236. node = new_node();
  237. node->source = livedata->source;
  238. memcpy(&node->key, &finder.key, ETHER_ADDR_LEN);
  239. RB_INSERT(macsrc_tree, &macsrc_root, node);
  240. }
  241. /* otherwise compare sources */
  242. else if (node->source != livedata->source) {
  243. dbg(1, "Found the dest MAC in the tree and it doesn't match this source NIC... skipping packet");
  244. /*
  245. * IMPORTANT!!!
  246. * Never send a packet out the same interface we sourced it on!
  247. */
  248. return;
  249. }
  250. /* what is our cache mode? */
  251. cache_mode = livedata->source == PCAP_INT1 ? TCPR_DIR_C2S : TCPR_DIR_S2C;
  252. l2proto = tcpedit_l3proto(livedata->tcpedit, BEFORE_PROCESS, pktdata, (int)pkthdr->len);
  253. dbgx(2, "Packet protocol: %04hx", l2proto);
  254. /* should we skip this packet based on CIDR match? */
  255. if (l2proto == ETHERTYPE_IP) {
  256. dbg(3, "Packet is IPv4");
  257. ip_hdr = (ipv4_hdr_t *)tcpedit_l3data(livedata->tcpedit, BEFORE_PROCESS, pktdata, (int)pkthdr->len);
  258. /* look for include or exclude CIDR match */
  259. if (livedata->options->xX.cidr != NULL) {
  260. if (!ip_hdr || !process_xX_by_cidr_ipv4(livedata->options->xX.mode, livedata->options->xX.cidr, ip_hdr)) {
  261. dbg(2, "Skipping IPv4 packet due to CIDR match");
  262. return;
  263. }
  264. }
  265. } else if (l2proto == ETHERTYPE_IP6) {
  266. dbg(3, "Packet is IPv6");
  267. ip6_hdr = (ipv6_hdr_t *)tcpedit_l3data(livedata->tcpedit, BEFORE_PROCESS, pktdata, (int)pkthdr->len);
  268. /* look for include or exclude CIDR match */
  269. if (livedata->options->xX.cidr != NULL) {
  270. if (!process_xX_by_cidr_ipv6(livedata->options->xX.mode, livedata->options->xX.cidr, ip6_hdr)) {
  271. dbg(2, "Skipping IPv6 packet due to CIDR match");
  272. return;
  273. }
  274. }
  275. }
  276. if (tcpedit_packet(livedata->tcpedit, &pkthdr, &pktdata, cache_mode) < 0)
  277. return;
  278. /*
  279. * send packets out the OTHER interface
  280. * and update the dst mac if necessary
  281. */
  282. switch (node->source) {
  283. case PCAP_INT1:
  284. dbgx(2, "Packet source was %s... sending out on %s", livedata->options->intf1, livedata->options->intf2);
  285. send = livedata->options->pcap2;
  286. break;
  287. case PCAP_INT2:
  288. dbgx(2, "Packet source was %s... sending out on %s", livedata->options->intf2, livedata->options->intf1);
  289. send = livedata->options->pcap1;
  290. break;
  291. default:
  292. errx(-1, "wtf? our node->source != PCAP_INT1 and != PCAP_INT2: %c", node->source);
  293. }
  294. /*
  295. * write packet out on the network
  296. */
  297. if (pcap_sendpacket(send, pktdata, (int)pkthdr->caplen) < 0)
  298. errx(-1,
  299. "Unable to send packet out %s: %s",
  300. send == livedata->options->pcap1 ? livedata->options->intf1 : livedata->options->intf2,
  301. pcap_geterr(send));
  302. stats.bytes_sent += pkthdr->caplen;
  303. stats.pkts_sent++;
  304. dbgx(1, "Sent packet " COUNTER_SPEC, stats.pkts_sent);
  305. } /* live_callback() */
  306. static void
  307. signal_catcher(int signo)
  308. {
  309. /* stdio in signal handlers causes a race condition, instead set a flag */
  310. if (signo == SIGINT)
  311. didsig = true;
  312. }