bridge.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* $Id: bridge.c 2040 2008-05-20 22:37:40Z 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 <errno.h>
  40. #include <stdlib.h>
  41. #include "tcpbridge.h"
  42. #include "bridge.h"
  43. #include "send_packets.h"
  44. #include "tcpedit/tcpedit.h"
  45. extern tcpbridge_opt_t options;
  46. extern struct timeval begin, end;
  47. extern COUNTER bytes_sent, failed, pkts_sent;
  48. extern volatile int didsig;
  49. #ifdef DEBUG
  50. extern int debug;
  51. #endif
  52. static int live_callback(struct live_data_t *,
  53. struct pcap_pkthdr *, const u_char *);
  54. /**
  55. * First, prep our RB Tree which tracks where each (source)
  56. * MAC really lives so we don't create really nasty network
  57. * storms.
  58. */
  59. static struct macsrc_t *new_node(void);
  60. RB_HEAD(macsrc_tree, macsrc_t) macsrc_root;
  61. static int
  62. rbmacsrc_comp(struct macsrc_t *a, struct macsrc_t *b)
  63. {
  64. return (memcmp(a->key, b->key, ETHER_ADDR_LEN));
  65. }
  66. RB_PROTOTYPE(macsrc_tree, macsrc_t, node, rbmacsrc_comp)
  67. RB_GENERATE(macsrc_tree, macsrc_t, node, rbmacsrc_comp)
  68. /**
  69. * redblack init
  70. */
  71. void
  72. rbinit(void)
  73. {
  74. RB_INIT(&macsrc_root);
  75. }
  76. /**
  77. * create a new node... Malloc's memory
  78. */
  79. struct macsrc_t *
  80. new_node(void)
  81. {
  82. struct macsrc_t *node;
  83. node = (struct macsrc_t *)safe_malloc(sizeof(struct macsrc_t));
  84. memset(node, '\0', sizeof(struct macsrc_t));
  85. return (node);
  86. }
  87. /**
  88. * main loop for bridging mode or unidir
  89. */
  90. void
  91. do_bridge(tcpedit_t *tcpedit, pcap_t * pcap1, pcap_t * pcap2)
  92. {
  93. struct pollfd polls[2]; /* one for left & right pcap */
  94. int pollresult = 0;
  95. u_char source1 = PCAP_INT1;
  96. u_char source2 = PCAP_INT2;
  97. struct live_data_t livedata;
  98. int pollcount = 1; /* default to unidir mode */
  99. assert(pcap1); /* must be set */
  100. /* define polls */
  101. polls[PCAP_INT1].fd = pcap_fileno(pcap1);
  102. polls[PCAP_INT1].events = POLLIN | POLLPRI;
  103. polls[PCAP_INT1].revents = 0;
  104. if (! options.unidir) {
  105. assert(pcap2);
  106. polls[PCAP_INT2].fd = pcap_fileno(pcap2);
  107. polls[PCAP_INT2].events = POLLIN | POLLPRI;
  108. polls[PCAP_INT2].revents = 0;
  109. pollcount = 2;
  110. }
  111. /* register signals */
  112. didsig = 0;
  113. (void)signal(SIGINT, catcher);
  114. livedata.tcpedit = tcpedit;
  115. /*
  116. * loop until ctrl-C or we've sent enough packets
  117. * note that if -L wasn't specified, limit_send is
  118. * set to 0 so this will loop infinately
  119. */
  120. while ((options.limit_send == 0) || (options.limit_send != pkts_sent)) {
  121. if (didsig) {
  122. packet_stats(&begin, &end, bytes_sent, pkts_sent, failed);
  123. exit(1);
  124. }
  125. dbgx(3, "limit_send: " COUNTER_SPEC " \t pkts_sent: " COUNTER_SPEC,
  126. options.limit_send, pkts_sent);
  127. /* poll for a packet on the two interfaces */
  128. pollresult = poll(polls, pollcount, options.poll_timeout);
  129. /* poll has returned, process the result */
  130. if (pollresult > 0) {
  131. /* success, got one or more packets */
  132. if (polls[PCAP_INT1].revents > 0) {
  133. dbg(5, "Processing first interface");
  134. livedata.source = source1;
  135. livedata.pcap = pcap1;
  136. pcap_dispatch(pcap1, -1, (pcap_handler) live_callback,
  137. (u_char *) & livedata);
  138. }
  139. /* check the other interface?? */
  140. if (! options.unidir && polls[PCAP_INT2].revents > 0) {
  141. dbg(5, "Processing second interface");
  142. livedata.source = source2;
  143. livedata.pcap = pcap2;
  144. pcap_dispatch(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. /* reset the result codes */
  157. polls[PCAP_INT1].revents = 0;
  158. if (! options.unidir)
  159. polls[PCAP_INT2].revents = 0;
  160. /* go back to the top of the loop */
  161. }
  162. } /* do_bridge() */
  163. /**
  164. * This is the callback we use with pcap_dispatch to process
  165. * each packet recieved by libpcap on the two interfaces.
  166. */
  167. static int
  168. live_callback(struct live_data_t *livedata, struct pcap_pkthdr *pkthdr,
  169. const u_char * nextpkt)
  170. {
  171. ipv4_hdr_t *ip_hdr = NULL;
  172. sendpacket_t *sp = NULL;
  173. static u_char *pktdata = NULL; /* full packet buffer */
  174. u_char **packet;
  175. int cache_mode;
  176. static unsigned long packetnum = 0;
  177. struct macsrc_t *node, finder; /* rb tree nodes */
  178. #ifdef DEBUG
  179. u_char dstmac[ETHER_ADDR_LEN];
  180. #endif
  181. u_int16_t l2proto;
  182. packetnum++;
  183. dbgx(2, "packet %d caplen %d", packetnum, pkthdr->caplen);
  184. /* only malloc the first time */
  185. if (pktdata == NULL) {
  186. /* create packet buffers */
  187. pktdata = (u_char *)safe_malloc(MAXPACKET);
  188. } else {
  189. /* zero out the old packet info */
  190. memset(pktdata, '\0', MAXPACKET);
  191. }
  192. /* copy the packet to our buffer */
  193. memcpy(pktdata, nextpkt, pkthdr->caplen);
  194. #ifdef ENABLE_VERBOSE
  195. /* decode packet? */
  196. if (options.verbose)
  197. tcpdump_print(options.tcpdump, pkthdr, nextpkt);
  198. #endif
  199. /* lookup our source MAC in the tree */
  200. memcpy(&finder.key, &pktdata[ETHER_ADDR_LEN], ETHER_ADDR_LEN);
  201. #ifdef DEBUG
  202. memcpy(&dstmac, pktdata, ETHER_ADDR_LEN);
  203. dbgx(1, "SRC MAC: " MAC_FORMAT "\tDST MAC: " MAC_FORMAT,
  204. MAC_STR(finder.key), MAC_STR(dstmac));
  205. #endif
  206. /* first, is this a packet sent locally? If so, ignore it */
  207. if ((memcmp(sendpacket_get_hwaddr(options.sp1), &finder.key,
  208. ETHER_ADDR_LEN)) == 0) {
  209. dbgx(1, "Packet matches the MAC of %s, skipping.", options.intf1);
  210. return (1);
  211. }
  212. else if ((memcmp(sendpacket_get_hwaddr(options.sp2), &finder.key,
  213. ETHER_ADDR_LEN)) == 0) {
  214. dbgx(1, "Packet matches the MAC of %s, skipping.", options.intf2);
  215. return (1);
  216. }
  217. node = RB_FIND(macsrc_tree, &macsrc_root, &finder);
  218. /* if we can't find the node, build a new one */
  219. if (node == NULL) {
  220. dbg(1, "Unable to find MAC in the tree");
  221. node = new_node();
  222. node->source = livedata->source;
  223. memcpy(&node->key, &finder.key, ETHER_ADDR_LEN);
  224. RB_INSERT(macsrc_tree, &macsrc_root, node);
  225. } /* otherwise compare sources */
  226. else if (node->source != livedata->source) {
  227. dbg(1,
  228. "Found the MAC and we had a source missmatch... skipping packet");
  229. /*
  230. * IMPORTANT!!!
  231. * Never send a packet out the same interface we sourced it on!
  232. */
  233. return (1);
  234. }
  235. /* what is our cache mode? */
  236. cache_mode = livedata->source == PCAP_INT1 ? TCPR_DIR_C2S : TCPR_DIR_S2C;
  237. l2proto = tcpedit_l3proto(livedata->tcpedit, BEFORE_PROCESS, pktdata, pkthdr->len);
  238. dbgx(2, "Packet protocol: %04hx", l2proto);
  239. /* should we skip this packet based on CIDR match? */
  240. if (l2proto == ETHERTYPE_IP) {
  241. dbg(3, "Packet is IP");
  242. ip_hdr = (ipv4_hdr_t *)tcpedit_l3data(livedata->tcpedit, BEFORE_PROCESS, pktdata, pkthdr->len);
  243. /* look for include or exclude CIDR match */
  244. if (options.xX.cidr != NULL) {
  245. if (!process_xX_by_cidr(options.xX.mode, options.xX.cidr, ip_hdr)) {
  246. dbg(2, "Skipping packet due to CIDR match");
  247. return (1);
  248. }
  249. }
  250. }
  251. packet = &pktdata;
  252. if (tcpedit_packet(livedata->tcpedit, &pkthdr, packet, cache_mode) == -1) {
  253. return -1;
  254. }
  255. /*
  256. * send packets out the OTHER interface
  257. * and update the dst mac if necessary
  258. */
  259. if (node->source == PCAP_INT1) {
  260. dbgx(2, "Packet source was %s... sending out on %s", options.intf1,
  261. options.intf2);
  262. sp = options.sp2;
  263. }
  264. else if (node->source == PCAP_INT2) {
  265. dbgx(2, "Packet source was %s... sending out on %s", options.intf2,
  266. options.intf1);
  267. sp = options.sp1;
  268. } else {
  269. errx(1, "wtf? our node->source != PCAP_INT1 and != PCAP_INT2: %c",
  270. node->source);
  271. }
  272. /*
  273. * write packet out on the network
  274. */
  275. if (sendpacket(sp, pktdata, pkthdr->caplen) < (int)pkthdr->caplen) {
  276. errx(1, "Unable to send packet out %s: %s", sp->device, sendpacket_geterr(sp));
  277. }
  278. bytes_sent += pkthdr->caplen;
  279. pkts_sent++;
  280. dbgx(1, "Sent packet " COUNTER_SPEC, pkts_sent);
  281. return (1);
  282. } /* live_callback() */
  283. /*
  284. Local Variables:
  285. mode:c
  286. indent-tabs-mode:nil
  287. c-basic-offset:4
  288. End:
  289. */