flowreplay.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* $Id: flowreplay.c 1398 2005-07-29 04:34:10Z 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 "defines.h"
  33. #include "common.h"
  34. #include <unistd.h> /* getopt() */
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <sys/types.h> /* socket */
  38. #include <sys/socket.h>
  39. #include <sys/select.h> /* select() */
  40. #include <netinet/in.h> /* inet_aton() */
  41. #include <arpa/inet.h>
  42. #include <string.h> /* strtok() */
  43. #include <strings.h> /* strcasecmp() */
  44. #include <nids.h> /* libnids */
  45. #include "flowreplay.h"
  46. #include "flowreplay_opts.h"
  47. #include "flownode.h"
  48. #include "flowkey.h"
  49. #include "flowstate.h"
  50. #include "flowbuff.h"
  51. #include "tree.h"
  52. #ifdef DEBUG
  53. int debug = 0;
  54. #endif
  55. /* libnids external vars */
  56. extern struct nids_prm nids_params;
  57. extern char nids_errbuf[];
  58. static void cleanup(void);
  59. static void init(void);
  60. static void post_args(int argc, char *argv[]);
  61. int main_loop(pcap_t *);
  62. int process_packet(struct session_t *, ip_hdr_t *, void *);
  63. /*
  64. * Global options
  65. */
  66. flowreplay_opt_t options;
  67. struct session_tree tcproot, udproot;
  68. /* file descriptor stuff */
  69. fd_set fds;
  70. int nfds = 0;
  71. int
  72. main(int argc, char *argv[])
  73. {
  74. int optct, i;
  75. char ebuf[PCAP_ERRBUF_SIZE];
  76. pcap_t *pcap = NULL;
  77. int first_run = 0;
  78. init();
  79. /* call autoopts to process args */
  80. optct = optionProcess(&flowreplayOptions, argc, argv);
  81. argc -= optct;
  82. argv += optct;
  83. post_args(argc, argv);
  84. /* loop through the input file(s) */
  85. for (i = 0; i < argc; i++) {
  86. /* set the libnids filename to our file */
  87. nids_params.filename = argv[i];
  88. /* init libnids */
  89. if (!nids_init())
  90. errx(1, "libnids error: %s", nids_errbuf);
  91. if (! first_run) {
  92. first_run = 1;
  93. /*
  94. nids_register_tcp(tcp_callback);
  95. nids_register_udp(udp_callback);
  96. */
  97. }
  98. /* play the pcap */
  99. nids_dispatch(-1);
  100. /* Close the pcap file */
  101. // pcap_close(nids_params.desc);
  102. }
  103. /* close our tcp sockets, etc */
  104. cleanup();
  105. return (0);
  106. }
  107. /*
  108. * main_loop()
  109. */
  110. int
  111. main_loop(pcap_t * pcap)
  112. {
  113. eth_hdr_t *eth_hdr = NULL;
  114. ip_hdr_t *ip_hdr = NULL;
  115. tcp_hdr_t *tcp_hdr = NULL;
  116. udp_hdr_t *udp_hdr = NULL;
  117. u_char pktdata[MAXPACKET];
  118. u_int32_t count = 0;
  119. u_int32_t send_count = 0;
  120. u_char key[12] = "";
  121. struct pcap_pkthdr header;
  122. const u_char *packet = NULL;
  123. struct session_t *node = NULL;
  124. /* process each packet */
  125. while ((packet = pcap_next(pcap, &header)) != NULL) {
  126. count++;
  127. /* we only process IP packets */
  128. eth_hdr = (eth_hdr_t *) packet;
  129. if (ntohs(eth_hdr->ether_type) != ETHERTYPE_IP) {
  130. dbg(2, "************ Skipping non-IP packet #%u ************",
  131. count);
  132. continue; /* next packet */
  133. }
  134. /* zero out old packet info */
  135. memset(&pktdata, '\0', sizeof(pktdata));
  136. /*
  137. * copy over everything except the eth hdr. This byte-aligns
  138. * everything up nicely for us
  139. */
  140. memcpy(&pktdata, (packet + sizeof(eth_hdr_t)),
  141. (header.caplen - sizeof(eth_hdr_t)));
  142. ip_hdr = (ip_hdr_t *) & pktdata;
  143. /* TCP */
  144. if ((options.proto == 0x0 || options.proto == IPPROTO_TCP)
  145. && (ip_hdr->ip_p == IPPROTO_TCP)) {
  146. tcp_hdr = (tcp_hdr_t *) get_layer4(ip_hdr);
  147. /* skip if port is set and not our port */
  148. if ((options.port) && (tcp_hdr->th_sport != options.port &&
  149. tcp_hdr->th_dport != options.port)) {
  150. dbg(3, "Skipping packet #%u based on port not matching", count);
  151. continue; /* next packet */
  152. }
  153. dbg(2, "************ Processing packet #%u ************", count);
  154. if (!rbkeygen(ip_hdr, IPPROTO_TCP, (void *)tcp_hdr, key))
  155. continue; /* next packet */
  156. /* find an existing sockfd or create a new one! */
  157. if ((node = getnodebykey(IPPROTO_TCP, key)) == NULL) {
  158. if ((node = newnode(IPPROTO_TCP, key, ip_hdr, tcp_hdr)) == NULL) {
  159. /* skip if newnode() doesn't create a new node for us */
  160. continue; /* next packet */
  161. }
  162. }
  163. else {
  164. /* calculate the new TCP state */
  165. if (tcp_state(tcp_hdr, node) == TCP_CLOSE) {
  166. dbg(2, "Closing socket #%u on second Fin", node->socket);
  167. close(node->socket);
  168. /* destroy our node */
  169. delete_node(&tcproot, node);
  170. continue; /* next packet */
  171. }
  172. /* send the packet? */
  173. if (process_packet(node, ip_hdr, tcp_hdr))
  174. send_count++; /* number of packets we've actually sent */
  175. }
  176. }
  177. /* UDP */
  178. else if ((options.proto == 0x0 || options.proto == IPPROTO_UDP)
  179. && (ip_hdr->ip_p == IPPROTO_UDP)) {
  180. udp_hdr = (udp_hdr_t *) get_layer4(ip_hdr);
  181. /* skip if port is set and not our port */
  182. if ((options.port) && (udp_hdr->uh_sport != options.port &&
  183. udp_hdr->uh_dport != options.port)) {
  184. dbg(2, "Skipping packet #%u based on port not matching", count);
  185. continue; /* next packet */
  186. }
  187. dbg(2, "************ Processing packet #%u ************", count);
  188. if (!rbkeygen(ip_hdr, IPPROTO_UDP, (void *)udp_hdr, key))
  189. continue; /* next packet */
  190. /* find an existing socket or create a new one! */
  191. if ((node = getnodebykey(IPPROTO_UDP, key)) == NULL) {
  192. if ((node = newnode(IPPROTO_UDP, key, ip_hdr, udp_hdr)) == NULL) {
  193. /* skip if newnode() doesn't create a new node for us */
  194. continue; /* next packet */
  195. }
  196. }
  197. if (process_packet(node, ip_hdr, udp_hdr))
  198. send_count++; /* number of packets we've actually sent */
  199. }
  200. /* non-TCP/UDP */
  201. else {
  202. dbg(2, "Skipping non-TCP/UDP packet #%u (0x%x)", count,
  203. ip_hdr->ip_p);
  204. }
  205. /* add a packet to our counter */
  206. node->count++;
  207. }
  208. /* print number of packets we actually sent */
  209. dbg(1, "Sent %d packets containing data", send_count);
  210. return (count);
  211. }
  212. /*
  213. * actually decides wether or not to send the packet and does the work
  214. */
  215. int
  216. process_packet(struct session_t *node, ip_hdr_t * ip_hdr, void *l4)
  217. {
  218. tcp_hdr_t *tcp_hdr = NULL;
  219. udp_hdr_t *udp_hdr = NULL;
  220. u_char data[MAXPACKET];
  221. int len = 0;
  222. struct sockaddr_in sa;
  223. memset(data, '\0', MAXPACKET);
  224. if (node->proto == IPPROTO_TCP) {
  225. /* packet is TCP */
  226. tcp_hdr = (tcp_hdr_t *) l4;
  227. len =
  228. ntohs(ip_hdr->ip_len) - (ip_hdr->ip_hl * 4) - (tcp_hdr->th_off * 4);
  229. /* check client to server */
  230. if ((ip_hdr->ip_dst.s_addr == node->server_ip) &&
  231. (tcp_hdr->th_dport == node->server_port)) {
  232. dbg(4, "Packet is client -> server");
  233. /* properly deal with TCP options */
  234. memcpy(data, (void *)((u_int32_t *) tcp_hdr + tcp_hdr->th_off),
  235. len);
  236. /* reset direction if client has something to send */
  237. if (len) {
  238. node->direction = C2S;
  239. }
  240. }
  241. /* check server to client */
  242. else if ((ip_hdr->ip_src.s_addr == node->server_ip) &&
  243. (tcp_hdr->th_sport == node->server_port)) {
  244. dbg(4, "Packet is server -> client");
  245. /* reset direction and add server_data len */
  246. if (node->direction == C2S) {
  247. node->direction = S2C;
  248. node->data_expected = len;
  249. }
  250. else {
  251. node->data_expected += len;
  252. }
  253. dbg(4, "Server data = %lu", node->data_expected);
  254. return (0);
  255. }
  256. }
  257. else if (node->proto == IPPROTO_UDP) {
  258. /* packet is UDP */
  259. udp_hdr = (udp_hdr_t *) l4;
  260. len = ntohs(ip_hdr->ip_len) - (ip_hdr->ip_hl * 4) - sizeof(udp_hdr_t);
  261. /* check client to server */
  262. if ((ip_hdr->ip_dst.s_addr == node->server_ip) &&
  263. (udp_hdr->uh_dport == node->server_port)) {
  264. dbg(4, "Packet is client -> server");
  265. memcpy(data, (udp_hdr + 1), len);
  266. /* reset direction if client has something to send */
  267. if (len) {
  268. node->direction = C2S;
  269. }
  270. }
  271. /* check server to client */
  272. else if ((ip_hdr->ip_src.s_addr == node->server_ip) &&
  273. (udp_hdr->uh_sport == node->server_port)) {
  274. dbg(4, "Packet is server -> client");
  275. if (node->direction == C2S) {
  276. node->direction = S2C;
  277. node->data_expected = len;
  278. }
  279. else {
  280. node->data_expected += len;
  281. }
  282. dbg(4, "Server data = %lu", node->data_expected);
  283. return (0);
  284. }
  285. }
  286. else {
  287. warnx("process_packet() doesn't know how to deal with proto: 0x%x",
  288. node->proto);
  289. return (0);
  290. }
  291. if (!len) {
  292. dbg(4, "Skipping packet. len = 0");
  293. return (0);
  294. }
  295. dbg(4, "Sending %d bytes of data");
  296. if (node->proto == IPPROTO_TCP) {
  297. if (send(node->socket, data, len, 0) != len) {
  298. warnx("Error sending data on socket %d (0x%llx)\n%s", node->socket,
  299. pkeygen(node->key), strerror(errno));
  300. }
  301. }
  302. else {
  303. sa.sin_family = AF_INET;
  304. sa.sin_port = node->server_port;
  305. sa.sin_addr.s_addr = node->server_ip;
  306. if (sendto
  307. (node->socket, data, len, 0, (struct sockaddr *)&sa,
  308. sizeof(sa)) != len) {
  309. warnx("Error sending data on socket %d (0x%llx)\n%s", node->socket,
  310. pkeygen(node->key), strerror(errno));
  311. }
  312. }
  313. return (len);
  314. }
  315. static void
  316. init(void)
  317. {
  318. /* init stuff */
  319. FD_ZERO(&fds);
  320. RB_INIT(&tcproot);
  321. RB_INIT(&udproot);
  322. memset(&options.targetaddr, '\0', sizeof(struct in_addr));
  323. memset(&options, '\0', sizeof(flowreplay_opt_t));
  324. options.sendmode = MODE_SEND;
  325. options.pernodebufflim = PER_NODE_BUFF_LIMIT;
  326. options.totalbufflim = TOTAL_BUFF_LIMIT;
  327. }
  328. static void
  329. post_args(int argc, char *argv[])
  330. {
  331. int i;
  332. char filter[PCAP_FILTER_LEN];
  333. /*
  334. * Verify input
  335. */
  336. #ifdef DEBUG
  337. if (HAVE_OPT(DBUG))
  338. debug = OPT_VALUE_DBUG;
  339. #else
  340. if (HAVE_OPT(DBUG))
  341. warn("not configured with --enable-debug. Debugging disabled.");
  342. #endif
  343. /* if -m wait, then must use -w */
  344. if ((options.sendmode == MODE_WAIT) && (!timerisset(&options.timeout)))
  345. err(1, "You must specify a wait period with -m wait");
  346. /* Can't specify client & server CIDR */
  347. if ((options.clients != NULL) && (options.servers != NULL))
  348. err(1, "You can't specify both a client and server cidr block");
  349. /* check for valid stdin */
  350. if (argc > 1)
  351. for (i = 0; i < argc; i++)
  352. if (!strcmp("-", argv[i]))
  353. err(1, "stdin must be the only file specified");
  354. /* apply our pcap filter, with the necessary stuff to handle IP frags */
  355. if (HAVE_OPT(FILTER)) {
  356. strlcpy(filter, OPT_ARG(FILTER), PCAP_FILTER_LEN);
  357. strlcat(filter, " or (ip[6:2] & 0x1fff != 0)", PCAP_FILTER_LEN);
  358. nids_params.pcap_filter = safe_strdup(filter);
  359. }
  360. }
  361. /*
  362. * cleanup after ourselves
  363. */
  364. static void
  365. cleanup(void)
  366. {
  367. dbg(1, "cleanup()");
  368. close_sockets();
  369. }
  370. /*
  371. Local Variables:
  372. mode:c
  373. indent-tabs-mode:nil
  374. c-basic-offset:4
  375. End:
  376. */