flowreplay.c 13 KB

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