flownode.c 8.8 KB

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