1
0

flownode.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* $Id: flownode.c 1195 2005-03-10 17:00:51Z 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 "flowreplay.h"
  35. #include "flowreplay_opts.h"
  36. #include "flownode.h"
  37. #include "flowkey.h"
  38. #include "flowstate.h"
  39. extern struct session_tree tcproot, udproot;
  40. extern int nfds;
  41. extern flowreplay_opt_t options;
  42. /* prepare the RB trees for tcp and udp sessions */
  43. RB_PROTOTYPE(session_tree, session_t, node, rbsession_comp)
  44. RB_GENERATE(session_tree, session_t, node, rbsession_comp)
  45. /*
  46. * returns the session_t structure
  47. * based upon the key given for the RB root (one root per
  48. * protocol). If the key doesn't exist, it will return NULL
  49. *
  50. * NOTE: This function is broken! key's are not guaranteed
  51. * to be unique for all combinations of sessions. What we
  52. * really should be doing is using a rbtree using a 32bit
  53. * key and then solving for collisions via a linked list.
  54. * this would probably be faster for the common case and still
  55. * provide adequate speed for collisions rather then ignoring
  56. * the collsion problem all together.
  57. */
  58. struct session_t *
  59. getnodebykey(char proto, u_char * key)
  60. {
  61. struct session_t *node = NULL;
  62. struct session_t like;
  63. like.socket = -1;
  64. memcpy(like.key, key, RBKEYLEN);
  65. if (proto == IPPROTO_TCP) {
  66. if ((node = RB_FIND(session_tree, &tcproot, &like)) == NULL) {
  67. dbg(3, "Couldn't find TCP key: 0x%llx", pkeygen(key));
  68. return (NULL);
  69. }
  70. }
  71. else if (proto == IPPROTO_UDP) {
  72. if ((node = RB_FIND(session_tree, &udproot, &like)) == NULL) {
  73. dbg(3, "Couldn't find UDP key: 0x%llx", pkeygen(key));
  74. return (NULL);
  75. }
  76. }
  77. else {
  78. warnx("Invalid tree protocol: 0x%x", proto);
  79. return (NULL);
  80. }
  81. dbg(3, "Found 0x%llx in the tree", pkeygen(key));
  82. return (node);
  83. }
  84. /*
  85. * inserts a node into a tree.
  86. * we fill out the node and create a new open socket
  87. * we then return the node or NULL on error
  88. */
  89. struct session_t *
  90. newnode(char proto, u_char * key, ip_hdr_t * ip_hdr, void *l4)
  91. {
  92. struct sockaddr_in sa;
  93. struct session_t *newnode = NULL;
  94. const int on = 1;
  95. tcp_hdr_t *tcp_hdr = NULL;
  96. udp_hdr_t *udp_hdr = NULL;
  97. dbg(2, "Adding new node: 0x%llx", pkeygen(key));
  98. newnode = (struct session_t *)safe_malloc(sizeof(struct session_t));
  99. memcpy(newnode->key, key, RBKEYLEN);
  100. newnode->proto = ip_hdr->ip_p;
  101. /* create a TCP or UDP socket & insert it in the tree */
  102. if (newnode->proto == IPPROTO_TCP) {
  103. /* is this a Syn packet? */
  104. tcp_hdr = (tcp_hdr_t *) l4;
  105. /* No new flows for non-Syn packets, unless NoSyn is set */
  106. if ((tcp_hdr->th_flags != TH_SYN) && (options.nosyn == 0)) {
  107. free(newnode);
  108. warnx("We won't connect (%s:%d -> %s:%d) on non-Syn packets",
  109. libnet_addr2name4(ip_hdr->ip_src.s_addr, LIBNET_DONT_RESOLVE),
  110. ntohs(tcp_hdr->th_sport),
  111. libnet_addr2name4(ip_hdr->ip_dst.s_addr, LIBNET_DONT_RESOLVE),
  112. ntohs(tcp_hdr->th_dport));
  113. return (NULL);
  114. }
  115. /* otherwise, continue on our merry way */
  116. newnode->server_ip = ip_hdr->ip_dst.s_addr;
  117. newnode->server_port = tcp_hdr->th_dport;
  118. /* figure out what we should set the state to */
  119. tcp_state(tcp_hdr, newnode);
  120. newnode->direction = C2S;
  121. newnode->wait = DONT_WAIT;
  122. if ((newnode->socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  123. free(newnode);
  124. warnx("Unable to create new TCP socket: %s", strerror(errno));
  125. return (NULL);
  126. }
  127. /* make our socket reusable */
  128. setsockopt(newnode->socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  129. RB_INSERT(session_tree, &tcproot, newnode);
  130. sa.sin_port = tcp_hdr->th_dport;
  131. }
  132. else if (newnode->proto == IPPROTO_UDP) {
  133. udp_hdr = (udp_hdr_t *) l4;
  134. /*
  135. * we're not as smart about UDP as TCP so we just assume
  136. * the first UDP packet is client->server unless we're
  137. * told otherwise
  138. */
  139. if ((options.clients != NULL)
  140. && (check_ip_cidr(options.clients, ip_hdr->ip_src.s_addr))) {
  141. /* source IP is client */
  142. dbg(3, "UDP match client CIDR. Server is destination IP: %s",
  143. libnet_addr2name4(ip_hdr->ip_dst.s_addr, LIBNET_DONT_RESOLVE));
  144. newnode->server_ip = ip_hdr->ip_dst.s_addr;
  145. }
  146. else if ((options.servers != NULL)
  147. && (check_ip_cidr(options.servers, ip_hdr->ip_src.s_addr))) {
  148. /* source IP is server */
  149. dbg(3, "UDP match server CIDR. Server is source IP: %s",
  150. libnet_addr2name4(ip_hdr->ip_src.s_addr, LIBNET_DONT_RESOLVE));
  151. newnode->server_ip = ip_hdr->ip_src.s_addr;
  152. }
  153. else {
  154. /* first packet is client */
  155. dbg(3, "UDP client is first sender. Server is: %s",
  156. libnet_addr2name4(ip_hdr->ip_src.s_addr, LIBNET_DONT_RESOLVE));
  157. newnode->server_ip = ip_hdr->ip_dst.s_addr;
  158. }
  159. newnode->server_port = udp_hdr->uh_dport;
  160. newnode->direction = C2S;
  161. newnode->wait = DONT_WAIT;
  162. if ((newnode->socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  163. free(newnode);
  164. warnx("Unable to create new UDP socket: %s", strerror(errno));
  165. return (NULL);
  166. }
  167. /* make our socket reusable */
  168. setsockopt(newnode->socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  169. RB_INSERT(session_tree, &udproot, newnode);
  170. sa.sin_port = udp_hdr->uh_dport;
  171. }
  172. /* connect to socket */
  173. sa.sin_family = AF_INET;
  174. /* set the appropriate destination IP */
  175. if (options.targetaddr.s_addr != 0) {
  176. sa.sin_addr = options.targetaddr;
  177. }
  178. else {
  179. sa.sin_addr = ip_hdr->ip_dst;
  180. }
  181. if (connect
  182. (newnode->socket, (struct sockaddr *)&sa,
  183. sizeof(struct sockaddr_in)) < 0) {
  184. free(newnode);
  185. warnx("Unable to connect to %s:%hu: %s", inet_ntoa(sa.sin_addr),
  186. ntohs(sa.sin_port), strerror(errno));
  187. return (NULL);
  188. }
  189. dbg(2, "Connected to %s:%hu as socketID: %d", inet_ntoa(sa.sin_addr),
  190. ntohs(sa.sin_port), newnode->socket);
  191. /* increment nfds so our select() works */
  192. if (nfds <= newnode->socket)
  193. nfds = newnode->socket + 1;
  194. return (newnode);
  195. }
  196. /*
  197. * compare two session_t structs for the RB_TREE compare
  198. */
  199. int
  200. rbsession_comp(struct session_t *a, struct session_t *b)
  201. {
  202. return (memcmp(a->key, b->key, RBKEYLEN));
  203. }
  204. /*
  205. * A wrapper around RB_REMOVE to delete a node from a tree
  206. */
  207. void
  208. delete_node(struct session_tree *root, struct session_t *node)
  209. {
  210. dbg(2, "Deleting node 0x%llx", pkeygen(node->key));
  211. RB_REMOVE(session_tree, root, node);
  212. }
  213. void
  214. close_sockets(void)
  215. {
  216. int tcpcount = 0, udpcount = 0;
  217. struct session_t *node = NULL;
  218. /* close the TCP sockets */
  219. RB_FOREACH(node, session_tree, &tcproot) {
  220. close(node->socket);
  221. tcpcount++;
  222. }
  223. /* close the UDP sockets */
  224. RB_FOREACH(node, session_tree, &udproot) {
  225. close(node->socket);
  226. udpcount++;
  227. }
  228. dbg(1, "Closed %d tcp and %d udp socket(s)", tcpcount, udpcount);
  229. }
  230. /*
  231. Local Variables:
  232. mode:c
  233. indent-tabs-mode:nil
  234. c-basic-offset:4
  235. End:
  236. */