tree.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /* $Id: tree.c 1386 2005-07-03 19:29:26Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2005 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 <stdio.h>
  35. #include <stdlib.h>
  36. #include "tree.h"
  37. #include "tcpprep.h"
  38. #include "tcpprep_opts.h"
  39. extern data_tree_t treeroot;
  40. extern tcpprep_opt_t options;
  41. #ifdef DEBUG
  42. extern int debug;
  43. #endif
  44. /* static buffer used by tree_print*() functions */
  45. char tree_print_buff[TREEPRINTBUFFLEN];
  46. static tree_t *new_tree();
  47. static tree_t *packet2tree(const u_char *);
  48. static char *tree_print(data_tree_t *);
  49. static char *tree_printnode(const char *, const tree_t *);
  50. static void tree_buildcidr(data_tree_t *, buildcidr_t *);
  51. static int tree_checkincidr(data_tree_t *, buildcidr_t *);
  52. RB_PROTOTYPE(data_tree_s, tree_s, node, tree_comp)
  53. RB_GENERATE(data_tree_s, tree_s, node, tree_comp)
  54. /*
  55. * used with rbwalk to walk a tree and generate cidr_t * cidrdata.
  56. * is smart enough to prevent dupes. void * arg is cast to bulidcidr_t
  57. */
  58. void
  59. tree_buildcidr(data_tree_t *treeroot, buildcidr_t * bcdata)
  60. {
  61. tree_t *node = NULL;
  62. cidr_t *newcidr = NULL;
  63. unsigned long network = 0;
  64. unsigned long mask = ~0; /* turn on all bits */
  65. dbg(1, "Running: tree_buildcidr()");
  66. RB_FOREACH(node, data_tree_s, treeroot) {
  67. /* we only check types that are vaild */
  68. if (bcdata->type != ANY) /* don't check if we're adding ANY */
  69. if (bcdata->type != node->type) /* no match, exit early */
  70. return;
  71. /*
  72. * in cases of leaves and last visit add to cidrdata if
  73. * necessary
  74. */
  75. dbg(4, "Checking if node exists...");
  76. if (!check_ip_cidr(options.cidrdata, node->ip)) { /* if we exist, abort */
  77. dbg(3, "Node %s doesn't exist... creating.",
  78. libnet_addr2name4(node->ip, RESOLVE));
  79. newcidr = new_cidr();
  80. newcidr->masklen = bcdata->masklen;
  81. network = node->ip & (mask << (32 - bcdata->masklen));
  82. dbg(3, "Using network: %s",
  83. libnet_addr2name4(network, LIBNET_DONT_RESOLVE));
  84. newcidr->network = network;
  85. add_cidr(&options.cidrdata, &newcidr);
  86. }
  87. }
  88. }
  89. /*
  90. * uses rbwalk to check to see if a given ip address of a given type in the
  91. * tree is inside any of the cidrdata
  92. *
  93. */
  94. static int
  95. tree_checkincidr(data_tree_t *treeroot, buildcidr_t * bcdata)
  96. {
  97. tree_t *node = NULL;
  98. RB_FOREACH(node, data_tree_s, treeroot) {
  99. /* we only check types that are vaild */
  100. if (bcdata->type != ANY) /* don't check if we're adding ANY */
  101. if (bcdata->type != node->type) /* no match, exit early */
  102. return 0;
  103. /*
  104. * in cases of leaves and last visit add to cidrdata if
  105. * necessary
  106. */
  107. if (check_ip_cidr(options.cidrdata, node->ip)) { /* if we exist, abort */
  108. return 1;
  109. }
  110. }
  111. return 0;
  112. }
  113. /*
  114. * processes the tree using rbwalk / tree2cidr to generate a CIDR
  115. * used for 2nd pass, router mode
  116. *
  117. * returns > 0 for success (the mask len), 0 for fail
  118. */
  119. int
  120. process_tree()
  121. {
  122. int mymask = 0;
  123. buildcidr_t *bcdata;
  124. dbg(1, "Running: process_tree()");
  125. bcdata = (buildcidr_t *)safe_malloc(sizeof(buildcidr_t));
  126. for (mymask = options.max_mask; mymask <= options.min_mask; mymask++) {
  127. dbg(1, "Current mask: %u", mymask);
  128. /* set starting vals */
  129. bcdata->type = SERVER;
  130. bcdata->masklen = mymask;
  131. /* build cidrdata with servers */
  132. tree_buildcidr(&treeroot, bcdata);
  133. /* calculate types of all IP's */
  134. tree_calculate(&treeroot);
  135. /* try to find clients in cidrdata */
  136. bcdata->type = CLIENT;
  137. if (! tree_checkincidr(&treeroot, bcdata)) { /* didn't find any clients in cidrdata */
  138. return (mymask); /* success! */
  139. }
  140. else {
  141. destroy_cidr(options.cidrdata); /* clean up after our mess */
  142. options.cidrdata = NULL;
  143. }
  144. }
  145. /* we failed to find a vaild cidr list */
  146. return (0);
  147. }
  148. /*
  149. * processes rbdata to bulid cidrdata based upon the
  150. * given type (SERVER, CLIENT, UNKNOWN) using the given masklen
  151. *
  152. * is smart enough to prevent dupes
  153. */
  154. void
  155. tree_to_cidr(const int masklen, const int type)
  156. {
  157. }
  158. /*
  159. * Checks to see if an IP is client or server by finding it in the tree
  160. * returns SERVER or CLIENT.
  161. * if mode = UNKNOWN, then abort on unknowns
  162. * if mode = CLIENT, then unknowns become clients
  163. * if mode = SERVER, then unknowns become servers
  164. */
  165. int
  166. check_ip_tree(const int mode, const unsigned long ip)
  167. {
  168. tree_t *node = NULL, *finder = NULL;
  169. finder = new_tree();
  170. finder->ip = ip;
  171. node = RB_FIND(data_tree_s, &treeroot, finder);
  172. if (node == NULL && mode == UNKNOWN)
  173. errx(1, "%s (%lu) is an unknown system... aborting.!\n"
  174. "Try a different auto mode (-n router|client|server)",
  175. libnet_addr2name4(ip, RESOLVE), ip);
  176. #ifdef DEBUG
  177. if (node->type == SERVER) {
  178. dbg(1, "Server: %s", libnet_addr2name4(ip, RESOLVE));
  179. }
  180. else if (node->type == CLIENT) {
  181. dbg(1, "Client: %s", libnet_addr2name4(ip, RESOLVE));
  182. }
  183. else {
  184. dbg(1, "Unknown: %s", libnet_addr2name4(ip, RESOLVE));
  185. }
  186. #endif
  187. /* return node type if we found the node, else return the default (mode) */
  188. if (node != NULL) {
  189. return (node->type);
  190. }
  191. else {
  192. return mode;
  193. }
  194. }
  195. /*
  196. * adds an entry to the tree (phase 1 of auto mode)
  197. */
  198. void
  199. add_tree(const unsigned long ip, const u_char * data)
  200. {
  201. tree_t *node = NULL, *newnode = NULL;
  202. newnode = packet2tree(data);
  203. if (newnode->type == UNKNOWN) {
  204. /* couldn't figure out if packet was client or server */
  205. dbg(2, "%s (%lu) unknown client/server",
  206. libnet_addr2name4(newnode->ip, RESOLVE), newnode->ip);
  207. }
  208. /* try to find a simular entry in the tree */
  209. node = RB_FIND(data_tree_s, &treeroot, newnode);
  210. #ifdef DEBUG
  211. dbg(3, "%s", tree_printnode("add_tree", node));
  212. #endif
  213. /* new entry required */
  214. if (node == NULL) {
  215. /* increment counters */
  216. if (newnode->type == SERVER) {
  217. newnode->server_cnt++;
  218. }
  219. else if (newnode->type == CLIENT) {
  220. newnode->client_cnt++;
  221. }
  222. /* insert it in */
  223. RB_INSERT(data_tree_s, &treeroot, newnode);
  224. }
  225. else {
  226. /* we found something, so update it */
  227. dbg(2, " node: %p\nnewnode: %p", node, newnode);
  228. #ifdef DEBUG
  229. dbg(3, "%s", tree_printnode("update node", node));
  230. #endif
  231. /* increment counter */
  232. if (newnode->type == SERVER) {
  233. node->server_cnt++;
  234. }
  235. else if (newnode->type == CLIENT) {
  236. /* temp debug code */
  237. node->client_cnt++;
  238. }
  239. /* didn't insert it, so free it */
  240. free(newnode);
  241. }
  242. dbg(2, "------- START NEXT -------");
  243. #ifdef DEBUG
  244. dbg(3, "%s", tree_print(&treeroot));
  245. #endif
  246. }
  247. /*
  248. * calculates wether an IP is a client, server, or unknown for each node in the tree
  249. */
  250. void
  251. tree_calculate(data_tree_t *treeroot)
  252. {
  253. tree_t *node;
  254. dbg(1, "Running tree_calculate()");
  255. RB_FOREACH(node, data_tree_s, treeroot) {
  256. dbg(4, "Processing %s", libnet_addr2name4(node->ip, RESOLVE));
  257. if ((node->server_cnt > 0) || (node->client_cnt > 0)) {
  258. /* type based on: server >= (client*ratio) */
  259. if ((double)node->server_cnt >= (double)node->client_cnt * options.ratio) {
  260. node->type = SERVER;
  261. dbg(3, "Setting %s to server",
  262. libnet_addr2name4(node->ip, RESOLVE));
  263. }
  264. else {
  265. node->type = CLIENT;
  266. dbg(3, "Setting %s to client",
  267. libnet_addr2name4(node->ip, RESOLVE));
  268. }
  269. }
  270. else { /* IP had no client or server connections */
  271. node->type = UNKNOWN;
  272. dbg(3, "Setting %s to unknown",
  273. libnet_addr2name4(node->ip, RESOLVE));
  274. }
  275. }
  276. }
  277. /*
  278. * tree_comp(), called by rbsearch compares two treees and returns:
  279. * 1 = first > second
  280. * -1 = first < second
  281. * 0 = first = second
  282. * based upon the ip address stored
  283. *
  284. */
  285. int
  286. tree_comp(tree_t *t1, tree_t *t2)
  287. {
  288. if (t1->ip > t2->ip) {
  289. dbg(2, "%s > %s", libnet_addr2name4(t1->ip, RESOLVE),
  290. libnet_addr2name4(t2->ip, RESOLVE));
  291. return 1;
  292. }
  293. if (t1->ip < t2->ip) {
  294. dbg(2, "%s < %s", libnet_addr2name4(t1->ip, RESOLVE),
  295. libnet_addr2name4(t2->ip, RESOLVE));
  296. return -1;
  297. }
  298. dbg(2, "%s = %s", libnet_addr2name4(t1->ip, RESOLVE),
  299. libnet_addr2name4(t2->ip, RESOLVE));
  300. return 0;
  301. }
  302. /*
  303. * creates a new TREE * with reasonable defaults
  304. */
  305. static tree_t *
  306. new_tree()
  307. {
  308. tree_t *node;
  309. node = (tree_t *)safe_malloc(sizeof(tree_t));
  310. memset(node, '\0', sizeof(tree_t));
  311. node->server_cnt = 0;
  312. node->client_cnt = 0;
  313. node->type = UNKNOWN;
  314. node->masklen = -1;
  315. node->ip = 0;
  316. return (node);
  317. }
  318. /*
  319. * returns a struct of TREE * from a packet header
  320. * and sets the type to be SERVER or CLIENT or UNKNOWN
  321. * if it's an undefined packet, we return -1 for the type
  322. * the u_char * data should be the data that is passed by pcap_dispatch()
  323. */
  324. tree_t *
  325. packet2tree(const u_char * data)
  326. {
  327. tree_t *node = NULL;
  328. eth_hdr_t *eth_hdr = NULL;
  329. ip_hdr_t ip_hdr;
  330. tcp_hdr_t tcp_hdr;
  331. udp_hdr_t udp_hdr;
  332. icmp_hdr_t icmp_hdr;
  333. dns_hdr_t dns_hdr;
  334. node = new_tree();
  335. eth_hdr = (eth_hdr_t *) (data);
  336. /* prevent issues with byte alignment, must memcpy */
  337. memcpy(&ip_hdr, (data + LIBNET_ETH_H), LIBNET_IP_H);
  338. /* copy over the source mac */
  339. strncpy((char *)node->mac, (char *)eth_hdr->ether_shost, 6);
  340. /* copy over the source ip */
  341. node->ip = ip_hdr.ip_src.s_addr;
  342. /*
  343. * TCP
  344. */
  345. if (ip_hdr.ip_p == IPPROTO_TCP) {
  346. dbg(1, "%s uses TCP... ",
  347. libnet_addr2name4(ip_hdr.ip_src.s_addr, RESOLVE));
  348. /* memcpy it over to prevent alignment issues */
  349. memcpy(&tcp_hdr, (data + LIBNET_ETH_H + (ip_hdr.ip_hl * 4)),
  350. LIBNET_TCP_H);
  351. /* ftp-data is going to skew our results so we ignore it */
  352. if (tcp_hdr.th_sport == 20) {
  353. return (node);
  354. }
  355. /* set TREE->type based on TCP flags */
  356. if (tcp_hdr.th_flags == TH_SYN) {
  357. node->type = CLIENT;
  358. dbg(1, "is a client");
  359. }
  360. else if (tcp_hdr.th_flags == (TH_SYN | TH_ACK)) {
  361. node->type = SERVER;
  362. dbg(1, "is a server");
  363. }
  364. else {
  365. dbg(1, "is an unknown");
  366. }
  367. /*
  368. * UDP
  369. */
  370. }
  371. else if (ip_hdr.ip_p == IPPROTO_UDP) {
  372. /* memcpy over to prevent alignment issues */
  373. memcpy(&udp_hdr, (data + LIBNET_ETH_H + (ip_hdr.ip_hl * 4)),
  374. LIBNET_UDP_H);
  375. dbg(1, "%s uses UDP... ",
  376. libnet_addr2name4(ip_hdr.ip_src.s_addr, RESOLVE));
  377. switch (ntohs(udp_hdr.uh_dport)) {
  378. case 0x0035: /* dns */
  379. /* prevent memory alignment issues */
  380. memcpy(&dns_hdr,
  381. (data + LIBNET_ETH_H + (ip_hdr.ip_hl * 4) + LIBNET_UDP_H),
  382. LIBNET_DNS_H);
  383. if (dns_hdr.flags & DNS_QUERY_FLAG) {
  384. /* bit set, response */
  385. node->type = SERVER;
  386. dbg(1, "is a dns server");
  387. }
  388. else {
  389. /* bit not set, query */
  390. node->type = CLIENT;
  391. dbg(1, "is a dns client");
  392. }
  393. return (node);
  394. break;
  395. default:
  396. break;
  397. }
  398. switch (ntohs(udp_hdr.uh_sport)) {
  399. case 0x0035: /* dns */
  400. /* prevent memory alignment issues */
  401. memcpy(&dns_hdr,
  402. (data + LIBNET_ETH_H + (ip_hdr.ip_hl * 4) + LIBNET_UDP_H),
  403. LIBNET_DNS_H);
  404. if (dns_hdr.flags & DNS_QUERY_FLAG) {
  405. /* bit set, response */
  406. node->type = SERVER;
  407. dbg(1, "is a dns server");
  408. }
  409. else {
  410. /* bit not set, query */
  411. node->type = CLIENT;
  412. dbg(1, "is a dns client");
  413. }
  414. return (node);
  415. break;
  416. default:
  417. dbg(1, "unknown UDP protocol: %hu->%hu", udp_hdr.uh_sport,
  418. udp_hdr.uh_dport);
  419. break;
  420. }
  421. /*
  422. * ICMP
  423. */
  424. }
  425. else if (ip_hdr.ip_p == IPPROTO_ICMP) {
  426. /* prevent alignment issues */
  427. memcpy(&icmp_hdr, (data + LIBNET_ETH_H + (ip_hdr.ip_hl * 4)),
  428. LIBNET_ICMP_H);
  429. dbg(1, "%s uses ICMP... ",
  430. libnet_addr2name4(ip_hdr.ip_src.s_addr, RESOLVE));
  431. /*
  432. * if port unreachable, then source == server, dst == client
  433. */
  434. if ((icmp_hdr.icmp_type == ICMP_UNREACH) &&
  435. (icmp_hdr.icmp_code == ICMP_UNREACH_PORT)) {
  436. node->type = SERVER;
  437. dbg(1, "is a server with a closed port");
  438. }
  439. }
  440. return (node);
  441. }
  442. /*
  443. * prints out a node of the tree to stderr
  444. */
  445. static char *
  446. tree_printnode(const char *name, const tree_t *node)
  447. {
  448. memset(&tree_print_buff, '\0', TREEPRINTBUFFLEN);
  449. if (node == NULL) {
  450. snprintf(tree_print_buff, TREEPRINTBUFFLEN, "%s node is null\n", name);
  451. }
  452. else {
  453. snprintf(tree_print_buff, TREEPRINTBUFFLEN,
  454. "-- %s: %p\nIP: %s\nMask: %d\nSrvr: %d\nClnt: %d\n",
  455. name, (void *)node, libnet_addr2name4(node->ip, RESOLVE),
  456. node->masklen, node->server_cnt, node->client_cnt);
  457. if (node->type == SERVER) {
  458. strlcat(tree_print_buff, "Type: Server\n--\n", TREEPRINTBUFFLEN);
  459. }
  460. else {
  461. strlcat(tree_print_buff, "Type: Client\n--\n", TREEPRINTBUFFLEN);
  462. }
  463. }
  464. return (tree_print_buff);
  465. }
  466. /*
  467. * prints out the entire tree
  468. */
  469. static char *
  470. tree_print(data_tree_t *treeroot)
  471. {
  472. tree_t *node = NULL;
  473. memset(&tree_print_buff, '\0', TREEPRINTBUFFLEN);
  474. RB_FOREACH(node, data_tree_s, treeroot) {
  475. tree_printnode("my node", node);
  476. }
  477. return (tree_print_buff);
  478. }
  479. /*
  480. Local Variables:
  481. mode:c
  482. indent-tabs-mode:nil
  483. c-basic-offset:4
  484. End:
  485. */