flownode.c 8.8 KB

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