bridge.c 13 KB

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