tree.c 16 KB

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