flowreplay.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* $Id: flowreplay.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 <libnet.h>
  32. #include <pcap.h>
  33. #include <unistd.h> /* getopt() */
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <sys/types.h> /* socket */
  37. #include <sys/socket.h>
  38. #include <sys/select.h> /* select() */
  39. #include <netinet/in.h> /* inet_aton() */
  40. #include <arpa/inet.h>
  41. #include <string.h> /* strtok() */
  42. #include <strings.h> /* strcasecmp() */
  43. #include "config.h"
  44. #include "flowreplay.h"
  45. #include "flownode.h"
  46. #include "flowkey.h"
  47. #include "flowstate.h"
  48. #include "flowbuff.h"
  49. #include "cidr.h"
  50. #include "err.h"
  51. #include "tcpreplay.h"
  52. #include "rbtree.h"
  53. #include "timer.h"
  54. #include "utils.h"
  55. #ifdef DEBUG
  56. int debug = 0;
  57. #endif
  58. static void cleanup(void);
  59. static void init(void);
  60. int main_loop(pcap_t *, u_char, u_int16_t);
  61. int process_packet(struct session_t *, ip_hdr_t *, void *);
  62. struct session_tree tcproot, udproot;
  63. /* getopt */
  64. extern int optind, opterr, optopt;
  65. extern char *optarg;
  66. /* we get this from libpcap */
  67. extern char pcap_version[];
  68. /* send mode */
  69. int SendMode = MODE_SEND;
  70. /* require Syn to start flow? */
  71. int NoSyn = 0;
  72. /* file descriptor stuff */
  73. fd_set fds;
  74. int nfds = 0;
  75. /* target to connect to */
  76. struct in_addr targetaddr;
  77. /* Client/Server CIDR blocks */
  78. CIDR *clients = NULL, *servers = NULL;
  79. /* libnet handle for libnet functions */
  80. libnet_t *l = NULL;
  81. /* limits for buffered packets */
  82. int32_t pernodebufflim = PER_NODE_BUFF_LIMIT;
  83. int32_t totalbufflim = TOTAL_BUFF_LIMIT; /* counts down to zero */
  84. static void
  85. version(void)
  86. {
  87. fprintf(stderr, "flowreplay version: %s", VERSION);
  88. #ifdef DEBUG
  89. fprintf(stderr, " (debug)");
  90. #endif
  91. fprintf(stderr, "\n");
  92. fprintf(stderr, "Compiled against libnet: %s\n", LIBNET_VERSION);
  93. fprintf(stderr, "Compiled against libpcap: %s\n", pcap_version);
  94. exit(0);
  95. }
  96. static void
  97. usage(void)
  98. {
  99. fprintf(stderr, "Usage: flowreplay [args] <file1> <file2> ...\n"
  100. "-c <CIDR1,CIDR2,...>\tClients are on this CIDR block\n");
  101. #ifdef DEBUG
  102. fprintf(stderr, "-d <level>\t\tEnable debug output to STDERR\n");
  103. #endif
  104. fprintf(stderr,
  105. "-f\t\t\tFirst TCP packet starts flow (don't require SYN)\n"
  106. "-h\t\t\tHelp\n"
  107. "-m <mode>\t\tReplay mode (send|wait|bytes)\n"
  108. "-t <ipaddr>\t\tRedirect flows to target ip address\n"
  109. "-p <proto/port>\t\tLimit to protocol and/or port\n"
  110. "-s <CIDR1,CIDR2,...>\tServers are on this CIDR block\n"
  111. "-V\t\t\tVersion\n"
  112. "-w <sec.usec>\t\tWait for server to send data\n");
  113. exit(0);
  114. }
  115. int
  116. main(int argc, char *argv[])
  117. {
  118. int ch, i;
  119. char ebuf[PCAP_ERRBUF_SIZE];
  120. pcap_t *pcap = NULL;
  121. char *p_parse = NULL;
  122. u_char proto = 0x0;
  123. u_int16_t port = 0;
  124. struct timeval timeout = { 0, 0 };
  125. init();
  126. while ((ch = getopt(argc, argv, "c:fhm:p:s:t:Vw:"
  127. #ifdef DEBUG
  128. "d:"
  129. #endif
  130. )) != -1) {
  131. switch (ch) {
  132. case 'c': /* client network */
  133. if (!parse_cidr(&clients, optarg, ","))
  134. usage();
  135. break;
  136. #ifdef DEBUG
  137. case 'd':
  138. debug = atoi(optarg);
  139. break;
  140. #endif
  141. case 'f': /* don't require a Syn packet to start a flow */
  142. NoSyn = 1;
  143. break;
  144. case 'h':
  145. usage();
  146. exit(0);
  147. break;
  148. case 'm': /* mode */
  149. if (strcasecmp(optarg, "send") == 0) {
  150. SendMode = MODE_SEND;
  151. }
  152. else if (strcasecmp(optarg, "wait") == 0) {
  153. SendMode = MODE_WAIT;
  154. }
  155. else if (strcasecmp(optarg, "bytes") == 0) {
  156. SendMode = MODE_BYTES;
  157. }
  158. else {
  159. errx(1, "Invalid mode: -m %s", optarg);
  160. }
  161. break;
  162. case 'p': /* protocol & port */
  163. p_parse = strtok(optarg, "/");
  164. if (strcasecmp(p_parse, "TCP") == 0) {
  165. proto = IPPROTO_TCP;
  166. dbg(1, "Proto: TCP");
  167. }
  168. else if (strcasecmp(p_parse, "UDP") == 0) {
  169. proto = IPPROTO_UDP;
  170. dbg(1, "Proto: UDP");
  171. }
  172. else {
  173. errx(1, "Unknown protocol: %s", p_parse);
  174. }
  175. /* if a port is specifed, set it */
  176. if ((p_parse = strtok(NULL, "/")))
  177. port = atoi(p_parse);
  178. dbg(1, "Port: %u", port);
  179. port = htons(port);
  180. break;
  181. case 's': /* server network */
  182. if (!parse_cidr(&servers, optarg, ","))
  183. usage();
  184. break;
  185. case 't': /* target IP */
  186. #ifdef INET_ATON
  187. if (inet_aton(optarg, &targetaddr) == 0)
  188. errx(1, "Invalid target IP address: %s", optarg);
  189. #elif INET_ADDR
  190. if ((targetaddr.s_addr = inet_addr(optarg)) == -1)
  191. errx(1, "Invalid target IP address: %s", optarg);
  192. #endif
  193. break;
  194. case 'V':
  195. version();
  196. exit(0);
  197. break;
  198. case 'w': /* wait between last server packet */
  199. float2timer(atof(optarg), &timeout);
  200. break;
  201. default:
  202. warnx("Invalid argument: -%c", ch);
  203. usage();
  204. exit(1);
  205. break;
  206. }
  207. } /* getopt() END */
  208. /*
  209. * Verify input
  210. */
  211. /* if -m wait, then must use -w */
  212. if ((SendMode == MODE_WAIT) && (!timerisset(&timeout)))
  213. errx(1, "You must specify a wait period with -m wait");
  214. /* Can't specify client & server CIDR */
  215. if ((clients != NULL) && (servers != NULL))
  216. errx(1, "You can't specify both a client and server cidr block");
  217. /* move over to the input files */
  218. argc -= optind;
  219. argv += optind;
  220. /* we need to replay something */
  221. if (argc == 0) {
  222. usage();
  223. exit(1);
  224. }
  225. /* check for valid stdin */
  226. if (argc > 1)
  227. for (i = 0; i < argc; i++)
  228. if (!strcmp("-", argv[i]))
  229. errx(1, "stdin must be the only file specified");
  230. /* loop through the input file(s) */
  231. for (i = 0; i < argc; i++) {
  232. /* open the pcap file */
  233. if ((pcap = pcap_open_offline(argv[i], ebuf)) == NULL)
  234. errx(1, "Error opening file: %s", ebuf);
  235. /* play the pcap */
  236. main_loop(pcap, proto, port);
  237. /* Close the pcap file */
  238. pcap_close(pcap);
  239. }
  240. /* close our tcp sockets, etc */
  241. cleanup();
  242. return (0);
  243. }
  244. /*
  245. * main_loop()
  246. */
  247. int
  248. main_loop(pcap_t * pcap, u_char proto, u_int16_t port)
  249. {
  250. eth_hdr_t *eth_hdr = NULL;
  251. ip_hdr_t *ip_hdr = NULL;
  252. tcp_hdr_t *tcp_hdr = NULL;
  253. udp_hdr_t *udp_hdr = NULL;
  254. u_char pktdata[MAXPACKET];
  255. u_int32_t count = 0;
  256. u_int32_t send_count = 0;
  257. u_char key[12] = "";
  258. struct pcap_pkthdr header;
  259. const u_char *packet = NULL;
  260. struct session_t *node = NULL;
  261. /* process each packet */
  262. while ((packet = pcap_next(pcap, &header)) != NULL) {
  263. count++;
  264. /* we only process IP packets */
  265. eth_hdr = (eth_hdr_t *) packet;
  266. if (ntohs(eth_hdr->ether_type) != ETHERTYPE_IP) {
  267. dbg(2, "************ Skipping non-IP packet #%u ************",
  268. count);
  269. continue; /* next packet */
  270. }
  271. /* zero out old packet info */
  272. memset(&pktdata, '\0', sizeof(pktdata));
  273. /*
  274. * copy over everything except the eth hdr. This byte-aligns
  275. * everything up nicely for us
  276. */
  277. memcpy(&pktdata, (packet + sizeof(eth_hdr_t)),
  278. (header.caplen - sizeof(eth_hdr_t)));
  279. ip_hdr = (ip_hdr_t *) & pktdata;
  280. /* TCP */
  281. if ((proto == 0x0 || proto == IPPROTO_TCP)
  282. && (ip_hdr->ip_p == IPPROTO_TCP)) {
  283. tcp_hdr = (tcp_hdr_t *) get_layer4(ip_hdr);
  284. /* skip if port is set and not our port */
  285. if ((port) && (tcp_hdr->th_sport != port &&
  286. tcp_hdr->th_dport != port)) {
  287. dbg(3, "Skipping packet #%u based on port not matching", count);
  288. continue; /* next packet */
  289. }
  290. dbg(2, "************ Processing packet #%u ************", count);
  291. if (!rbkeygen(ip_hdr, IPPROTO_TCP, (void *)tcp_hdr, key))
  292. continue; /* next packet */
  293. /* find an existing sockfd or create a new one! */
  294. if ((node = getnodebykey(IPPROTO_TCP, key)) == NULL) {
  295. if ((node = newnode(IPPROTO_TCP, key, ip_hdr, tcp_hdr)) == NULL) {
  296. /* skip if newnode() doesn't create a new node for us */
  297. continue; /* next packet */
  298. }
  299. }
  300. else {
  301. /* calculate the new TCP state */
  302. if (tcp_state(tcp_hdr, node) == TCP_CLOSE) {
  303. dbg(2, "Closing socket #%u on second Fin", node->socket);
  304. close(node->socket);
  305. /* destroy our node */
  306. delete_node(&tcproot, node);
  307. continue; /* next packet */
  308. }
  309. /* send the packet? */
  310. if (process_packet(node, ip_hdr, tcp_hdr))
  311. send_count++; /* number of packets we've actually sent */
  312. }
  313. }
  314. /* UDP */
  315. else if ((proto == 0x0 || proto == IPPROTO_UDP)
  316. && (ip_hdr->ip_p == IPPROTO_UDP)) {
  317. udp_hdr = (udp_hdr_t *) get_layer4(ip_hdr);
  318. /* skip if port is set and not our port */
  319. if ((port) && (udp_hdr->uh_sport != port &&
  320. udp_hdr->uh_dport != port)) {
  321. dbg(2, "Skipping packet #%u based on port not matching", count);
  322. continue; /* next packet */
  323. }
  324. dbg(2, "************ Processing packet #%u ************", count);
  325. if (!rbkeygen(ip_hdr, IPPROTO_UDP, (void *)udp_hdr, key))
  326. continue; /* next packet */
  327. /* find an existing socket or create a new one! */
  328. if ((node = getnodebykey(IPPROTO_UDP, key)) == NULL) {
  329. if ((node = newnode(IPPROTO_UDP, key, ip_hdr, udp_hdr)) == NULL) {
  330. /* skip if newnode() doesn't create a new node for us */
  331. continue; /* next packet */
  332. }
  333. }
  334. if (process_packet(node, ip_hdr, udp_hdr))
  335. send_count++; /* number of packets we've actually sent */
  336. }
  337. /* non-TCP/UDP */
  338. else {
  339. dbg(2, "Skipping non-TCP/UDP packet #%u (0x%x)", count,
  340. ip_hdr->ip_p);
  341. }
  342. /* add a packet to our counter */
  343. node->count++;
  344. }
  345. /* print number of packets we actually sent */
  346. dbg(1, "Sent %d packets containing data", send_count);
  347. return (count);
  348. }
  349. /*
  350. * actually decides wether or not to send the packet and does the work
  351. */
  352. int
  353. process_packet(struct session_t *node, ip_hdr_t * ip_hdr, void *l4)
  354. {
  355. tcp_hdr_t *tcp_hdr = NULL;
  356. udp_hdr_t *udp_hdr = NULL;
  357. u_char data[MAXPACKET];
  358. int len = 0;
  359. struct sockaddr_in sa;
  360. memset(data, '\0', MAXPACKET);
  361. if (node->proto == IPPROTO_TCP) {
  362. /* packet is TCP */
  363. tcp_hdr = (tcp_hdr_t *) l4;
  364. len =
  365. ntohs(ip_hdr->ip_len) - (ip_hdr->ip_hl * 4) - (tcp_hdr->th_off * 4);
  366. /* check client to server */
  367. if ((ip_hdr->ip_dst.s_addr == node->server_ip) &&
  368. (tcp_hdr->th_dport == node->server_port)) {
  369. dbg(4, "Packet is client -> server");
  370. /* properly deal with TCP options */
  371. memcpy(data, (void *)((u_int32_t *) tcp_hdr + tcp_hdr->th_off),
  372. len);
  373. /* reset direction if client has something to send */
  374. if (len) {
  375. node->direction = C2S;
  376. }
  377. }
  378. /* check server to client */
  379. else if ((ip_hdr->ip_src.s_addr == node->server_ip) &&
  380. (tcp_hdr->th_sport == node->server_port)) {
  381. dbg(4, "Packet is server -> client");
  382. /* reset direction and add server_data len */
  383. if (node->direction == C2S) {
  384. node->direction = S2C;
  385. node->data_expected = len;
  386. }
  387. else {
  388. node->data_expected += len;
  389. }
  390. dbg(4, "Server data = %d", node->data_expected);
  391. return (0);
  392. }
  393. }
  394. else if (node->proto == IPPROTO_UDP) {
  395. /* packet is UDP */
  396. udp_hdr = (udp_hdr_t *) l4;
  397. len = ntohs(ip_hdr->ip_len) - (ip_hdr->ip_hl * 4) - sizeof(tcp_hdr_t);
  398. /* check client to server */
  399. if ((ip_hdr->ip_dst.s_addr == node->server_ip) &&
  400. (udp_hdr->uh_dport == node->server_port)) {
  401. dbg(4, "Packet is client -> server");
  402. memcpy(data, (udp_hdr + 1), len);
  403. /* reset direction if client has something to send */
  404. if (len) {
  405. node->direction = C2S;
  406. }
  407. }
  408. /* check server to client */
  409. else if ((ip_hdr->ip_src.s_addr == node->server_ip) &&
  410. (udp_hdr->uh_sport == node->server_port)) {
  411. dbg(4, "Packet is server -> client");
  412. if (node->direction == C2S) {
  413. node->direction = S2C;
  414. node->data_expected = len;
  415. }
  416. else {
  417. node->data_expected += len;
  418. }
  419. dbg(4, "Server data = %d", node->data_expected);
  420. return (0);
  421. }
  422. }
  423. else {
  424. warnx("process_packet() doesn't know how to deal with proto: 0x%x",
  425. node->proto);
  426. return (0);
  427. }
  428. if (!len) {
  429. dbg(4, "Skipping packet. len = 0");
  430. return (0);
  431. }
  432. dbg(4, "Sending %d bytes of data");
  433. if (node->proto == IPPROTO_TCP) {
  434. if (send(node->socket, data, len, 0) != len) {
  435. warnx("Error sending data on socket %d (0x%llx)\n%s", node->socket,
  436. pkeygen(node->key), strerror(errno));
  437. }
  438. }
  439. else {
  440. sa.sin_family = AF_INET;
  441. sa.sin_port = node->server_port;
  442. sa.sin_addr.s_addr = node->server_ip;
  443. if (sendto
  444. (node->socket, data, len, 0, (struct sockaddr *)&sa,
  445. sizeof(sa)) != len) {
  446. warnx("Error sending data on socket %d (0x%llx)\n%s", node->socket,
  447. pkeygen(node->key), strerror(errno));
  448. }
  449. }
  450. return (len);
  451. }
  452. static void
  453. init(void)
  454. {
  455. /* init stuff */
  456. FD_ZERO(&fds);
  457. RB_INIT(&tcproot);
  458. RB_INIT(&udproot);
  459. memset(&targetaddr, '\0', sizeof(struct in_addr));
  460. }
  461. /*
  462. * cleanup after ourselves
  463. */
  464. static void
  465. cleanup(void)
  466. {
  467. dbg(1, "cleanup()");
  468. close_sockets();
  469. }