replay_live.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* $Id: replay_live.c 767 2004-10-06 12:48:49Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2004 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 <libnet.h>
  33. #ifdef HAVE_PCAPNAV
  34. #include <pcapnav.h>
  35. #else
  36. #include "fakepcapnav.h"
  37. #endif
  38. #include <sys/time.h>
  39. #include <signal.h>
  40. #include <string.h>
  41. #include <netinet/in.h>
  42. #include <time.h>
  43. #include "tcpreplay.h"
  44. #include "cidr.h"
  45. #include "cache.h"
  46. #include "edit_packet.h"
  47. #include "err.h"
  48. #include "do_packets.h"
  49. #include "replay_live.h"
  50. #include "list.h"
  51. #include "xX.h"
  52. #ifdef HAVE_SYS_POLL_H
  53. #include <sys/poll.h>
  54. #elif HAVE_POLL_H
  55. #include <poll.h>
  56. #else
  57. #include "fakepoll.h"
  58. #endif
  59. extern struct options options;
  60. extern struct timeval begin, end;
  61. extern u_int64_t bytes_sent, failed, pkts_sent;
  62. extern u_int32_t cache_packets;
  63. extern volatile int didsig;
  64. extern int l2len, maxpacket;
  65. extern char *intf, *intf2;
  66. extern int include_exclude_mode;
  67. extern LIST *xX_list;
  68. extern CIDR *xX_cidr;
  69. #ifdef DEBUG
  70. extern int debug;
  71. #endif
  72. void packet_stats(); /* from tcpreplay.c */
  73. static int live_callback(struct live_data_t *,
  74. struct pcap_pkthdr *, const u_char *);
  75. /*
  76. * First, prep our RB Tree which tracks where each (source)
  77. * MAC really lives so we don't create really nasty network
  78. * storms.
  79. */
  80. static struct macsrc_t *new_node(void);
  81. RB_HEAD(macsrc_tree, macsrc_t) macsrc_root;
  82. static int
  83. rbmacsrc_comp(struct macsrc_t *a, struct macsrc_t *b)
  84. {
  85. return (memcmp(a->key, b->key, ETHER_ADDR_LEN));
  86. }
  87. RB_PROTOTYPE(macsrc_tree, macsrc_t, node, rbmacsrc_comp)
  88. RB_GENERATE(macsrc_tree, macsrc_t, node, rbmacsrc_comp)
  89. void
  90. rbinit(void)
  91. {
  92. RB_INIT(&macsrc_root);
  93. }
  94. struct macsrc_t *
  95. new_node(void)
  96. {
  97. struct macsrc_t *node;
  98. node = (struct macsrc_t *)malloc(sizeof(struct macsrc_t));
  99. if (node == NULL)
  100. errx(1, "Error malloc() in new_node()");
  101. memset(node, '\0', sizeof(struct macsrc_t));
  102. return (node);
  103. }
  104. /*
  105. * main loop for bridging mode
  106. */
  107. void
  108. do_bridge(pcap_t * pcap1, pcap_t * pcap2, int l2enabled, char *l2data,
  109. int l2len)
  110. {
  111. struct pollfd polls[2]; /* one for left & right pcap */
  112. int pollresult = 0;
  113. u_char source1 = PCAP_INT1;
  114. u_char source2 = PCAP_INT2;
  115. struct live_data_t livedata;
  116. /* define polls */
  117. polls[PCAP_INT1].fd = pcap_fileno(pcap1);
  118. polls[PCAP_INT2].fd = pcap_fileno(pcap2);
  119. polls[PCAP_INT1].events = POLLIN | POLLPRI;
  120. polls[PCAP_INT2].events = POLLIN | POLLPRI;
  121. polls[PCAP_INT1].revents = 0;
  122. polls[PCAP_INT2].revents = 0;
  123. /* our live data thingy */
  124. livedata.linktype = pcap_datalink(pcap1);
  125. livedata.l2enabled = l2enabled;
  126. livedata.l2len = l2len;
  127. livedata.l2data = l2data;
  128. /* register signals */
  129. didsig = 0;
  130. (void)signal(SIGINT, catcher);
  131. /*
  132. * loop until ctrl-C or we've sent enough packets
  133. * note that if -L wasn't specified, limit_send is
  134. * set to -1 so this will loop infinately
  135. */
  136. while ((options.limit_send == -1) || (options.limit_send != pkts_sent)) {
  137. if (didsig) {
  138. packet_stats();
  139. exit(1);
  140. }
  141. dbg(1, "limit_send: %llu \t pkts_sent: %llu", options.limit_send,
  142. pkts_sent);
  143. /* poll for a packet on the two interfaces */
  144. pollresult = poll(polls, 2, options.poll_timeout);
  145. /* poll has returned, process the result */
  146. if (pollresult > 0) {
  147. /* success, got one or more packets */
  148. if (polls[PCAP_INT1].revents > 0) {
  149. dbg(2, "Processing first interface");
  150. livedata.source = source1;
  151. pcap_dispatch(pcap1, -1, (pcap_handler) live_callback,
  152. (u_char *) & livedata);
  153. }
  154. /* check the other interface */
  155. if (polls[PCAP_INT2].revents > 0) {
  156. dbg(2, "Processing second interface");
  157. livedata.source = source2;
  158. pcap_dispatch(pcap2, -1, (pcap_handler) live_callback,
  159. (u_char *) & livedata);
  160. }
  161. }
  162. else if (pollresult == 0) {
  163. dbg(3, "poll timeout exceeded...");
  164. /* do something here? */
  165. }
  166. else {
  167. /* poll error, probably a Ctrl-C */
  168. warnx("poll() error: %s", strerror(errno));
  169. }
  170. /* reset the result codes */
  171. polls[PCAP_INT1].revents = 0;
  172. polls[PCAP_INT2].revents = 0;
  173. /* go back to the top of the loop */
  174. }
  175. } /* do_bridge() */
  176. /*
  177. * This is the callback we use with pcap_dispatch to process
  178. * each packet recieved by libpcap on the two interfaces.
  179. */
  180. static int
  181. live_callback(struct live_data_t *livedata, struct pcap_pkthdr *pkthdr,
  182. const u_char * nextpkt)
  183. {
  184. eth_hdr_t *eth_hdr = NULL;
  185. ip_hdr_t *ip_hdr = NULL;
  186. libnet_t *l = NULL;
  187. u_char *pktdata = NULL; /* full packet buffer */
  188. #ifdef FORCE_ALIGN
  189. u_char *ipbuff = NULL; /* IP header and above buffer */
  190. #endif
  191. int ret, newl2len;
  192. static unsigned long packetnum = 0;
  193. struct macsrc_t *node, finder; /* rb tree nodes */
  194. #ifdef DEBUG
  195. u_char dstmac[ETHER_ADDR_LEN];
  196. #endif
  197. packetnum++;
  198. dbg(2, "packet %d caplen %d", packetnum, pkthdr->caplen);
  199. /* create packet buffers */
  200. if ((pktdata = (u_char *) malloc(maxpacket)) == NULL)
  201. errx(1, "Unable to malloc pktdata buffer");
  202. #ifdef FORCE_ALIGN
  203. if ((ipbuff = (u_char *) malloc(maxpacket)) == NULL)
  204. errx(1, "Unable to malloc ipbuff buffer");
  205. #endif
  206. /* zero out the old packet info */
  207. memset(pktdata, '\0', maxpacket);
  208. /* Rewrite any Layer 2 data and copy the data to our local buffer */
  209. if ((newl2len = rewrite_l2(pkthdr, pktdata, nextpkt,
  210. livedata->linktype, livedata->l2enabled,
  211. livedata->l2data, livedata->l2len)) == 0) {
  212. warnx("Error rewriting layer 2 data... skipping packet %d", packetnum);
  213. return (1);
  214. }
  215. /* lookup our source MAC in the tree */
  216. memcpy(&finder.key, &pktdata[ETHER_ADDR_LEN], ETHER_ADDR_LEN);
  217. #ifdef DEBUG
  218. memcpy(&dstmac, pktdata, ETHER_ADDR_LEN);
  219. dbg(1, "Source MAC: " MAC_FORMAT "\tDestin MAC: " MAC_FORMAT,
  220. MAC_STR(finder.key), MAC_STR(dstmac));
  221. #endif
  222. /* first, is this a packet sent locally? If so, ignore it */
  223. if ((memcmp(libnet_get_hwaddr(options.intf1), &finder.key, ETHER_ADDR_LEN))
  224. == 0) {
  225. dbg(1, "Packet matches the MAC of %s, skipping.", intf);
  226. return (1);
  227. }
  228. else if ((memcmp
  229. (libnet_get_hwaddr(options.intf2), &finder.key,
  230. ETHER_ADDR_LEN)) == 0) {
  231. dbg(1, "Packet matches the MAC of %s, skipping.", intf2);
  232. return (1);
  233. }
  234. node = RB_FIND(macsrc_tree, &macsrc_root, &finder);
  235. /* if we can't find the node, build a new one */
  236. if (node == NULL) {
  237. dbg(1, "Unable to find MAC in the tree");
  238. node = new_node();
  239. node->source = livedata->source;
  240. memcpy(&node->key, &finder.key, ETHER_ADDR_LEN);
  241. RB_INSERT(macsrc_tree, &macsrc_root, node);
  242. } /* otherwise compare sources */
  243. else if (node->source != livedata->source) {
  244. dbg(1,
  245. "Found the MAC and we had a source missmatch... skipping packet");
  246. /*
  247. * IMPORTANT!!!
  248. * Never send a packet out the same interface we sourced it on!
  249. */
  250. return (1);
  251. }
  252. /*
  253. * send packets out the OTHER interface
  254. * and update the dst mac if necessary
  255. */
  256. if (node->source == PCAP_INT1) {
  257. dbg(2, "Packet source was %s... sending out on %s", intf, intf2);
  258. l = options.intf2;
  259. if (memcmp(options.intf2_mac, NULL_MAC, 6) != 0) {
  260. dbg(3, "Rewriting destination MAC for %s", intf2);
  261. memcpy(eth_hdr->ether_dhost, options.intf2_mac, ETHER_ADDR_LEN);
  262. }
  263. }
  264. else {
  265. dbg(2, "Packet source was %s... sending out on %s", intf2, intf);
  266. l = options.intf1;
  267. if (memcmp(options.intf1_mac, NULL_MAC, 6) != 0) {
  268. dbg(3, "Rewriting destination MAC for %s", intf);
  269. memcpy(eth_hdr->ether_dhost, options.intf1_mac, ETHER_ADDR_LEN);
  270. }
  271. }
  272. eth_hdr = (eth_hdr_t *) pktdata;
  273. /* does packet have an IP header? if so set our pointer to it */
  274. if (ntohs(eth_hdr->ether_type) == ETHERTYPE_IP) {
  275. dbg(3, "Packet is IP");
  276. #ifdef FORCE_ALIGN
  277. /*
  278. * copy layer 3 and up to our temp packet buffer
  279. * for now on, we have to edit the packetbuff because
  280. * just before we send the packet, we copy the packetbuff
  281. * back onto the pkt.data + newl2len buffer
  282. * we do all this work to prevent byte alignment issues
  283. */
  284. ip_hdr = (ip_hdr_t *) ipbuff;
  285. memcpy(ip_hdr, (&pktdata[newl2len]), pkthdr->caplen - newl2len);
  286. #else
  287. /*
  288. * on non-strict byte align systems, don't need to memcpy(),
  289. * just point to 14 bytes into the existing buffer
  290. */
  291. ip_hdr = (ip_hdr_t *) (&pktdata[newl2len]);
  292. #endif
  293. /* look for include or exclude CIDR match */
  294. if (xX_cidr != NULL) {
  295. if (!process_xX_by_cidr(include_exclude_mode, xX_cidr, ip_hdr)) {
  296. return (1);
  297. }
  298. }
  299. }
  300. else {
  301. dbg(3, "Packet is not IP");
  302. /* non-IP packets have a NULL ip_hdr struct */
  303. ip_hdr = NULL;
  304. }
  305. /* check for martians? */
  306. if (options.no_martians && (ip_hdr != NULL)) {
  307. switch ((ntohl(ip_hdr->ip_dst.s_addr) & 0xff000000) >> 24) {
  308. case 0:
  309. case 127:
  310. case 255:
  311. dbg(1, "Skipping martian. Packet #%d", pkts_sent);
  312. /* then skip the packet */
  313. return (1);
  314. default:
  315. /* continue processing */
  316. break;
  317. }
  318. }
  319. /* Untruncate packet? Only for IP packets */
  320. if ((options.trunc) && (ip_hdr != NULL)) {
  321. untrunc_packet(pkthdr, pktdata, ip_hdr, l, newl2len);
  322. }
  323. /* do we need to spoof the src/dst IP address? */
  324. if ((options.seed) && (ip_hdr != NULL)) {
  325. randomize_ips(pkthdr, pktdata, ip_hdr, l, newl2len);
  326. }
  327. /* do we need to force fixing checksums? */
  328. if ((options.fixchecksums) && (ip_hdr != NULL)) {
  329. fix_checksums(pkthdr, ip_hdr, l);
  330. }
  331. #ifdef STRICT_ALIGN
  332. /*
  333. * put back the layer 3 and above back in the pkt.data buffer
  334. * we can't edit the packet at layer 3 or above beyond this point
  335. */
  336. memcpy(&pktdata[newl2len], ip_hdr, pkthdr->caplen - newl2len);
  337. #endif
  338. /*
  339. * write packet out on the network
  340. */
  341. do {
  342. ret = libnet_adv_write_link(l, pktdata, pkthdr->caplen);
  343. if (ret == -1) {
  344. /* Make note of failed writes due to full buffers */
  345. if (errno == ENOBUFS) {
  346. failed++;
  347. }
  348. else {
  349. errx(1, "libnet_adv_write_link(): %s", strerror(errno));
  350. }
  351. }
  352. } while (ret == -1 && !didsig);
  353. bytes_sent += pkthdr->caplen;
  354. pkts_sent++;
  355. dbg(1, "Sent packet %llu", pkts_sent);
  356. /* free buffers */
  357. free(pktdata);
  358. #ifdef FORCE_ALIGN
  359. free(ipbuff);
  360. #endif
  361. return (1);
  362. } /* live_callback() */