bridge.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* $Id: bridge.c 1462 2006-04-13 05:10:27Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2005 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "config.h"
  32. #include "defines.h"
  33. #include "common.h"
  34. #include <sys/time.h>
  35. #include <signal.h>
  36. #include <string.h>
  37. #include <netinet/in.h>
  38. #include <time.h>
  39. #include "tcpbridge.h"
  40. #include "bridge.h"
  41. #include "send_packets.h"
  42. #include "tcpedit/tcpedit.h"
  43. extern tcpbridge_opt_t options;
  44. extern struct timeval begin, end;
  45. extern COUNTER bytes_sent, failed, pkts_sent;
  46. extern volatile int didsig;
  47. #ifdef DEBUG
  48. extern int debug;
  49. #endif
  50. static int live_callback(struct live_data_t *,
  51. struct pcap_pkthdr *, const u_char *);
  52. /*
  53. * First, prep our RB Tree which tracks where each (source)
  54. * MAC really lives so we don't create really nasty network
  55. * storms.
  56. */
  57. static struct macsrc_t *new_node(void);
  58. RB_HEAD(macsrc_tree, macsrc_t) macsrc_root;
  59. static int
  60. rbmacsrc_comp(struct macsrc_t *a, struct macsrc_t *b)
  61. {
  62. return (memcmp(a->key, b->key, ETHER_ADDR_LEN));
  63. }
  64. RB_PROTOTYPE(macsrc_tree, macsrc_t, node, rbmacsrc_comp)
  65. RB_GENERATE(macsrc_tree, macsrc_t, node, rbmacsrc_comp)
  66. void
  67. rbinit(void)
  68. {
  69. RB_INIT(&macsrc_root);
  70. }
  71. struct macsrc_t *
  72. new_node(void)
  73. {
  74. struct macsrc_t *node;
  75. node = (struct macsrc_t *)safe_malloc(sizeof(struct macsrc_t));
  76. memset(node, '\0', sizeof(struct macsrc_t));
  77. return (node);
  78. }
  79. /*
  80. * main loop for bridging mode or unidir
  81. */
  82. void
  83. do_bridge(pcap_t * pcap1, pcap_t * pcap2)
  84. {
  85. struct pollfd polls[2]; /* one for left & right pcap */
  86. int pollresult = 0;
  87. u_char source1 = PCAP_INT1;
  88. u_char source2 = PCAP_INT2;
  89. struct live_data_t livedata;
  90. int pollcount = 1; /* default to unidir mode */
  91. assert(pcap1); /* must be set */
  92. /* define polls */
  93. polls[PCAP_INT1].fd = pcap_fileno(pcap1);
  94. polls[PCAP_INT1].events = POLLIN | POLLPRI;
  95. polls[PCAP_INT1].revents = 0;
  96. if (! options.unidir) {
  97. assert(pcap2);
  98. polls[PCAP_INT2].fd = pcap_fileno(pcap2);
  99. polls[PCAP_INT2].events = POLLIN | POLLPRI;
  100. polls[PCAP_INT2].revents = 0;
  101. pollcount = 2;
  102. }
  103. /* register signals */
  104. didsig = 0;
  105. (void)signal(SIGINT, catcher);
  106. /*
  107. * loop until ctrl-C or we've sent enough packets
  108. * note that if -L wasn't specified, limit_send is
  109. * set to 0 so this will loop infinately
  110. */
  111. while ((options.limit_send == 0) || (options.limit_send != pkts_sent)) {
  112. if (didsig) {
  113. packet_stats(&begin, &end, bytes_sent, pkts_sent, failed);
  114. exit(1);
  115. }
  116. dbgx(1, "limit_send: " COUNTER_SPEC " \t pkts_sent: " COUNTER_SPEC,
  117. options.limit_send, pkts_sent);
  118. /* poll for a packet on the two interfaces */
  119. pollresult = poll(polls, pollcount, options.poll_timeout);
  120. /* poll has returned, process the result */
  121. if (pollresult > 0) {
  122. /* success, got one or more packets */
  123. if (polls[PCAP_INT1].revents > 0) {
  124. dbg(2, "Processing first interface");
  125. livedata.source = source1;
  126. livedata.pcap = pcap1;
  127. pcap_dispatch(pcap1, -1, (pcap_handler) live_callback,
  128. (u_char *) & livedata);
  129. }
  130. /* check the other interface?? */
  131. if (! options.unidir && polls[PCAP_INT2].revents > 0) {
  132. dbg(2, "Processing second interface");
  133. livedata.source = source2;
  134. livedata.pcap = pcap2;
  135. pcap_dispatch(pcap2, -1, (pcap_handler) live_callback,
  136. (u_char *) & livedata);
  137. }
  138. }
  139. else if (pollresult == 0) {
  140. dbg(3, "poll timeout exceeded...");
  141. /* do something here? */
  142. }
  143. else {
  144. /* poll error, probably a Ctrl-C */
  145. warnx("poll() error: %s", strerror(errno));
  146. }
  147. /* reset the result codes */
  148. polls[PCAP_INT1].revents = 0;
  149. if (! options.unidir)
  150. polls[PCAP_INT2].revents = 0;
  151. /* go back to the top of the loop */
  152. }
  153. } /* do_bridge() */
  154. /*
  155. * This is the callback we use with pcap_dispatch to process
  156. * each packet recieved by libpcap on the two interfaces.
  157. */
  158. static int
  159. live_callback(struct live_data_t *livedata, struct pcap_pkthdr *pkthdr,
  160. const u_char * nextpkt)
  161. {
  162. ip_hdr_t *ip_hdr = NULL;
  163. libnet_t *l = NULL;
  164. static u_char *pktdata = NULL; /* full packet buffer */
  165. #ifdef FORCE_ALIGN
  166. u_char *ipbuff = NULL; /* IP header and above buffer */
  167. #endif
  168. static int first_time = 1;
  169. int ret, newl2len, cache_mode;
  170. static unsigned long packetnum = 0;
  171. struct macsrc_t *node, finder; /* rb tree nodes */
  172. #ifdef DEBUG
  173. u_char dstmac[ETHER_ADDR_LEN];
  174. #endif
  175. packetnum++;
  176. dbgx(2, "packet %d caplen %d", packetnum, pkthdr->caplen);
  177. /* only malloc the first time */
  178. if (first_time) {
  179. /* create packet buffers */
  180. pktdata = (u_char *)safe_malloc(MAXPACKET);
  181. #ifdef FORCE_ALIGN
  182. ipbuff = (u_char *)safe_malloc(MAXPACKET);
  183. #endif
  184. first_time = 0;
  185. } else {
  186. /* zero out the old packet info */
  187. memset(pktdata, '\0', MAXPACKET);
  188. #ifdef FORCE_ALIGN
  189. memset(ipbuff, '\0', MAXPACKET);
  190. #endif
  191. }
  192. #ifdef HAVE_TCPDUMP
  193. /* decode packet? */
  194. if (options.verbose)
  195. tcpdump_print(options.tcpdump, pkthdr, nextpkt);
  196. #endif
  197. /* lookup our source MAC in the tree */
  198. memcpy(&finder.key, &pktdata[ETHER_ADDR_LEN], ETHER_ADDR_LEN);
  199. #ifdef DEBUG
  200. memcpy(&dstmac, pktdata, ETHER_ADDR_LEN);
  201. dbgx(1, "Source MAC: " MAC_FORMAT "\tDestin MAC: " MAC_FORMAT,
  202. MAC_STR(finder.key), MAC_STR(dstmac));
  203. #endif
  204. /* first, is this a packet sent locally? If so, ignore it */
  205. if ((memcmp(libnet_get_hwaddr(options.send1), &finder.key,
  206. ETHER_ADDR_LEN)) == 0) {
  207. dbgx(1, "Packet matches the MAC of %s, skipping.", options.intf1);
  208. return (1);
  209. }
  210. else if ((memcmp(libnet_get_hwaddr(options.send2), &finder.key,
  211. ETHER_ADDR_LEN)) == 0) {
  212. dbgx(1, "Packet matches the MAC of %s, skipping.", options.intf2);
  213. return (1);
  214. }
  215. node = RB_FIND(macsrc_tree, &macsrc_root, &finder);
  216. /* if we can't find the node, build a new one */
  217. if (node == NULL) {
  218. dbg(1, "Unable to find MAC in the tree");
  219. node = new_node();
  220. node->source = livedata->source;
  221. memcpy(&node->key, &finder.key, ETHER_ADDR_LEN);
  222. RB_INSERT(macsrc_tree, &macsrc_root, node);
  223. } /* otherwise compare sources */
  224. else if (node->source != livedata->source) {
  225. dbg(1,
  226. "Found the MAC and we had a source missmatch... skipping packet");
  227. /*
  228. * IMPORTANT!!!
  229. * Never send a packet out the same interface we sourced it on!
  230. */
  231. return (1);
  232. }
  233. /* what is our cache mode? */
  234. cache_mode = livedata->source == PCAP_INT1 ? CACHE_PRIMARY : CACHE_SECONDARY;
  235. /* Rewrite any Layer 2 data and copy the data to our local buffer */
  236. if ((newl2len = rewrite_l2(livedata->pcap, &pkthdr, pktdata, cache_mode))
  237. == 0) {
  238. warnx("Error rewriting layer 2 data... skipping packet %d", packetnum);
  239. return (1);
  240. }
  241. /*
  242. * send packets out the OTHER interface
  243. * and update the dst mac if necessary
  244. */
  245. if (node->source == PCAP_INT1) {
  246. dbgx(2, "Packet source was %s... sending out on %s", options.intf1,
  247. options.intf2);
  248. l = options.send2;
  249. }
  250. else if (node->source == PCAP_INT2) {
  251. dbgx(2, "Packet source was %s... sending out on %s", options.intf2,
  252. options.intf1);
  253. l = options.send1;
  254. } else {
  255. errx(1, "wtf? our node->source != PCAP_INT1 and != PCAP_INT2: %c",
  256. node->source);
  257. }
  258. if (get_l2protocol(nextpkt, pkthdr->caplen, livedata->linktype)
  259. == ETHERTYPE_IP) {
  260. dbg(3, "Packet is IP");
  261. #ifdef FORCE_ALIGN
  262. /*
  263. * copy layer 3 and up to our temp packet buffer
  264. * for now on, we have to edit the packetbuff because
  265. * just before we send the packet, we copy the packetbuff
  266. * back onto the pkt.data + newl2len buffer
  267. * we do all this work to prevent byte alignment issues
  268. */
  269. ip_hdr = (ip_hdr_t *) ipbuff;
  270. memcpy(ip_hdr, (&pktdata[newl2len]), pkthdr->caplen - newl2len);
  271. #else
  272. /*
  273. * on non-strict byte align systems, don't need to memcpy(),
  274. * just point to 14 bytes into the existing buffer
  275. */
  276. ip_hdr = (ip_hdr_t *) (&pktdata[newl2len]);
  277. #endif
  278. /* look for include or exclude CIDR match */
  279. if (options.xX.cidr != NULL) {
  280. if (!process_xX_by_cidr(options.xX.mode, options.xX.cidr, ip_hdr)) {
  281. return (1);
  282. }
  283. }
  284. }
  285. else {
  286. dbg(3, "Packet is not IP");
  287. /* non-IP packets have a NULL ip_hdr struct */
  288. ip_hdr = NULL;
  289. }
  290. #ifdef STRICT_ALIGN
  291. /*
  292. * put back the layer 3 and above back in the pkt.data buffer
  293. * we can't edit the packet at layer 3 or above beyond this point
  294. */
  295. memcpy(&pktdata[newl2len], ip_hdr, pkthdr->caplen - newl2len);
  296. #endif
  297. /*
  298. * write packet out on the network
  299. */
  300. do {
  301. ret = libnet_adv_write_link(l, pktdata, pkthdr->caplen);
  302. if (ret == -1) {
  303. /* Make note of failed writes due to full buffers */
  304. if (errno == ENOBUFS) {
  305. failed++;
  306. }
  307. else {
  308. errx(1, "libnet_adv_write_link(): %s", strerror(errno));
  309. }
  310. }
  311. } while (ret == -1 && !didsig);
  312. bytes_sent += pkthdr->caplen;
  313. pkts_sent++;
  314. dbgx(1, "Sent packet " COUNTER_SPEC, pkts_sent);
  315. return (1);
  316. } /* live_callback() */
  317. /*
  318. Local Variables:
  319. mode:c
  320. indent-tabs-mode:nil
  321. c-basic-offset:4
  322. End:
  323. */