flownode.c 8.9 KB

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