flownode.c 9.0 KB

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