123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671 |
- /* $Id: tree.c 2074 2008-11-09 08:42:24Z aturner $ */
- /*
- * Copyright (c) 2001-2007 Aaron Turner.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the names of the copyright owners nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
- * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "config.h"
- #include "defines.h"
- #include "common.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "tree.h"
- #include "tcpprep.h"
- #include "tcpprep_opts.h"
- extern tcpr_data_tree_t treeroot;
- extern tcpprep_opt_t options;
- #ifdef DEBUG
- extern int debug;
- #endif
- /* static buffer used by tree_print*() functions */
- char tree_print_buff[TREEPRINTBUFFLEN];
- static tcpr_tree_t *new_tree();
- static tcpr_tree_t *packet2tree(const u_char *);
- static char *tree_print(tcpr_data_tree_t *);
- static char *tree_printnode(const char *, const tcpr_tree_t *);
- static void tree_buildcidr(tcpr_data_tree_t *, tcpr_buildcidr_t *);
- static int tree_checkincidr(tcpr_data_tree_t *, tcpr_buildcidr_t *);
- RB_PROTOTYPE(tcpr_data_tree_s, tcpr_tree_s, node, tree_comp)
- RB_GENERATE(tcpr_data_tree_s, tcpr_tree_s, node, tree_comp)
- /**
- * used with rbwalk to walk a tree and generate cidr_t * cidrdata.
- * is smart enough to prevent dupes. void * arg is cast to bulidcidr_t
- */
- void
- tree_buildcidr(tcpr_data_tree_t *treeroot, tcpr_buildcidr_t * bcdata)
- {
- tcpr_tree_t *node = NULL;
- tcpr_cidr_t *newcidr = NULL;
- unsigned long network = 0;
- unsigned long mask = ~0; /* turn on all bits */
- dbg(1, "Running: tree_buildcidr()");
- RB_FOREACH(node, tcpr_data_tree_s, treeroot) {
- /* we only check types that are vaild */
- if (bcdata->type != DIR_ANY) /* don't check if we're adding ANY */
- if (bcdata->type != node->type) /* no match, exit early */
- return;
- /*
- * in cases of leaves and last visit add to cidrdata if
- * necessary
- */
- dbgx(4, "Checking if %s exists in cidrdata...", get_addr2name4(node->ip, RESOLVE));
- if (!check_ip_cidr(options.cidrdata, node->ip)) { /* if we exist, abort */
- dbgx(3, "Node %s doesn't exist... creating.",
- get_addr2name4(node->ip, RESOLVE));
- newcidr = new_cidr();
- newcidr->masklen = bcdata->masklen;
- network = node->ip & (mask << (32 - bcdata->masklen));
- dbgx(3, "Using network: %s",
- get_addr2name4(network, RESOLVE));
- newcidr->network = network;
- add_cidr(&options.cidrdata, &newcidr);
- }
- }
- }
- /**
- * uses rbwalk to check to see if a given ip address of a given type in the
- * tree is inside any of the cidrdata
- */
- static int
- tree_checkincidr(tcpr_data_tree_t *treeroot, tcpr_buildcidr_t * bcdata)
- {
- tcpr_tree_t *node = NULL;
- RB_FOREACH(node, tcpr_data_tree_s, treeroot) {
- /* we only check types that are vaild */
- if (bcdata->type != DIR_ANY) /* don't check if we're adding ANY */
- if (bcdata->type != node->type) /* no match, exit early */
- return 0;
- /*
- * in cases of leaves and last visit add to cidrdata if
- * necessary
- */
- if (check_ip_cidr(options.cidrdata, node->ip)) /* if we exist, abort */
- return 1;
- }
- return 0;
- }
- /**
- * processes the tree using rbwalk / tree2cidr to generate a CIDR
- * used for 2nd pass, router mode
- *
- * returns > 0 for success (the mask len), 0 for fail
- */
- int
- process_tree(void)
- {
- int mymask = 0;
- tcpr_buildcidr_t *bcdata;
- dbg(1, "Running: process_tree()");
- bcdata = (tcpr_buildcidr_t *)safe_malloc(sizeof(tcpr_buildcidr_t));
- for (mymask = options.max_mask; mymask <= options.min_mask; mymask++) {
- dbgx(1, "Current mask: %u", mymask);
- /* set starting vals */
- bcdata->type = DIR_SERVER;
- bcdata->masklen = mymask;
- /* build cidrdata with servers */
- tree_buildcidr(&treeroot, bcdata);
- /* calculate types of all IP's */
- tree_calculate(&treeroot);
- /* try to find clients in cidrdata */
- bcdata->type = DIR_CLIENT;
- if (! tree_checkincidr(&treeroot, bcdata)) { /* didn't find any clients in cidrdata */
- safe_free(bcdata);
- return (mymask); /* success! */
- }
- else {
- destroy_cidr(options.cidrdata); /* clean up after our mess */
- options.cidrdata = NULL;
- }
- }
- safe_free(bcdata);
- /* we failed to find a vaild cidr list */
- notice("Unable to determine any IP addresses as a clients.");
- notice("Perhaps you should change the --ratio, --minmask/maxmask settings, or try another mode?");
- return (0);
- }
- /*
- * processes rbdata to bulid cidrdata based upon the
- * given type (SERVER, CLIENT, UNKNOWN) using the given masklen
- *
- * is smart enough to prevent dupes
- void
- tcpr_tree_to_cidr(const int masklen, const int type)
- {
- }
- */
- /**
- * Checks to see if an IP is client or server by finding it in the tree
- * returns TCPR_DIR_C2S or TCPR_DIR_S2C or -1 on error
- * if mode = UNKNOWN, then abort on unknowns
- * if mode = CLIENT, then unknowns become clients
- * if mode = SERVER, then unknowns become servers
- */
- tcpr_dir_t
- check_ip_tree(const int mode, const unsigned long ip)
- {
- tcpr_tree_t *node = NULL, *finder = NULL;
- finder = new_tree();
- finder->ip = ip;
- node = RB_FIND(tcpr_data_tree_s, &treeroot, finder);
- if (node == NULL && mode == DIR_UNKNOWN)
- errx(-1, "%s (%lu) is an unknown system... aborting.!\n"
- "Try a different auto mode (-n router|client|server)",
- get_addr2name4(ip, RESOLVE), ip);
- #ifdef DEBUG
- switch (node->type) {
- case DIR_SERVER:
- dbgx(1, "DIR_SERVER: %s", get_addr2name4(ip, RESOLVE));
- break;
- case DIR_CLIENT:
- dbgx(1, "DIR_CLIENT: %s", get_addr2name4(ip, RESOLVE));
- break;
- case DIR_UNKNOWN:
- dbgx(1, "DIR_UNKNOWN: %s", get_addr2name4(ip, RESOLVE));
- break;
- case DIR_ANY:
- dbgx(1, "DIR_ANY: %s", get_addr2name4(ip, RESOLVE));
- break;
- }
- #endif
- /*
- * FIXME: Is this logic correct? I think this might be backwards :(
- */
- /* return node type if we found the node, else return the default (mode) */
- if (node != NULL) {
- switch (node->type) {
- case DIR_SERVER:
- return TCPR_DIR_C2S;
- break;
- case DIR_CLIENT:
- return TCPR_DIR_S2C;
- break;
- case DIR_UNKNOWN:
- case DIR_ANY:
- /* use our current mode to determine return code */
- goto return_unknown;
- default:
- errx(-1, "Node for %s has invalid type: %d", get_addr2name4(ip, RESOLVE), node->type);
- }
- }
-
- return_unknown:
- switch (mode) {
- case DIR_SERVER:
- return TCPR_DIR_C2S;
- break;
- case DIR_CLIENT:
- return TCPR_DIR_S2C;
- break;
- default:
- return -1;
- }
- }
- /**
- * Parses the IP header of the given packet (data) to get the SRC/DST IP
- * addresses. If the SRC IP doesn't exist in the TREE, we add it as a
- * client, if the DST IP doesn't exist in the TREE, we add it as a server
- */
- void
- add_tree_first(const u_char *data)
- {
- tcpr_tree_t *newnode = NULL, *findnode;
- eth_hdr_t *eth_hdr = NULL;
- ipv4_hdr_t ip_hdr;
-
- assert(data);
- /*
- * first add/find the source IP/client
- */
- newnode = new_tree();
- eth_hdr = (eth_hdr_t *) (data);
- /* prevent issues with byte alignment, must memcpy */
- memcpy(&ip_hdr, (data + TCPR_ETH_H), TCPR_IPV4_H);
- /* copy over the source ip, and values to gurantee this a client */
- newnode->ip = ip_hdr.ip_src.s_addr;
- newnode->type = DIR_CLIENT;
- newnode->client_cnt = 1000;
- findnode = RB_FIND(tcpr_data_tree_s, &treeroot, newnode);
-
- /* if we didn't find it, add it to the tree, else free it */
- if (findnode == NULL) {
- RB_INSERT(tcpr_data_tree_s, &treeroot, newnode);
- } else {
- safe_free(newnode);
- }
-
- /*
- * now add/find the destination IP/server
- */
- newnode = new_tree();
- eth_hdr = (eth_hdr_t *) (data);
- memcpy(&ip_hdr, (data + TCPR_ETH_H), TCPR_IPV4_H);
- newnode->ip = ip_hdr.ip_dst.s_addr;
- newnode->type = DIR_SERVER;
- newnode->server_cnt = 1000;
- findnode = RB_FIND(tcpr_data_tree_s, &treeroot, newnode);
- if (findnode == NULL) {
- RB_INSERT(tcpr_data_tree_s, &treeroot, newnode);
- } else {
- safe_free(newnode);
- }
- }
- /**
- * adds an entry to the tree (phase 1 of auto mode). We add each host
- * to the tree if it doesn't yet exist. We go through and track:
- * - number of times each host acts as a client or server
- * - the way the host acted the first time we saw it (client or server)
- */
- void
- add_tree(const unsigned long ip, const u_char * data)
- {
- tcpr_tree_t *node = NULL, *newnode = NULL;
- assert(data);
-
- newnode = packet2tree(data);
- assert(ip == newnode->ip);
- if (newnode->type == DIR_UNKNOWN) {
- /* couldn't figure out if packet was client or server */
- dbgx(2, "%s (%lu) unknown client/server",
- get_addr2name4(newnode->ip, RESOLVE), newnode->ip);
- }
- /* try to find a simular entry in the tree */
- node = RB_FIND(tcpr_data_tree_s, &treeroot, newnode);
- dbgx(3, "%s", tree_printnode("add_tree", node));
- /* new entry required */
- if (node == NULL) {
- /* increment counters */
- if (newnode->type == DIR_SERVER) {
- newnode->server_cnt++;
- }
- else if (newnode->type == DIR_CLIENT) {
- newnode->client_cnt++;
- }
- /* insert it in */
- RB_INSERT(tcpr_data_tree_s, &treeroot, newnode);
- }
- else {
- /* we found something, so update it */
- dbgx(2, " node: %p\nnewnode: %p", node, newnode);
- dbgx(3, "%s", tree_printnode("update node", node));
- /* increment counter */
- if (newnode->type == DIR_SERVER) {
- node->server_cnt++;
- }
- else if (newnode->type == DIR_CLIENT) {
- /* temp debug code */
- node->client_cnt++;
- }
-
- /* didn't insert it, so free it */
- safe_free(newnode);
- }
- dbg(2, "------- START NEXT -------");
- dbgx(3, "%s", tree_print(&treeroot));
- }
- /**
- * calculates wether each node in the tree is a client, server, or unknown for each node in the tree
- */
- void
- tree_calculate(tcpr_data_tree_t *treeroot)
- {
- tcpr_tree_t *node;
- dbg(1, "Running tree_calculate()");
- RB_FOREACH(node, tcpr_data_tree_s, treeroot) {
- dbgx(4, "Processing %s", get_addr2name4(node->ip, RESOLVE));
- if ((node->server_cnt > 0) || (node->client_cnt > 0)) {
- /* type based on: server >= (client*ratio) */
- if ((double)node->server_cnt >= (double)node->client_cnt * options.ratio) {
- node->type = DIR_SERVER;
- dbgx(3, "Setting %s to server",
- get_addr2name4(node->ip, RESOLVE));
- }
- else {
- node->type = DIR_CLIENT;
- dbgx(3, "Setting %s to client",
- get_addr2name4(node->ip, RESOLVE));
- }
- }
- else { /* IP had no client or server connections */
- node->type = DIR_UNKNOWN;
- dbgx(3, "Setting %s to unknown",
- get_addr2name4(node->ip, RESOLVE));
- }
- }
- }
- /**
- * tree_comp(), called by rbsearch compares two treees and returns:
- * 1 = first > second
- * -1 = first < second
- * 0 = first = second
- * based upon the ip address stored
- *
- */
- int
- tree_comp(tcpr_tree_t *t1, tcpr_tree_t *t2)
- {
- if (t1->ip > t2->ip) {
- dbgx(2, "%s > %s", get_addr2name4(t1->ip, RESOLVE),
- get_addr2name4(t2->ip, RESOLVE));
- return 1;
- }
- if (t1->ip < t2->ip) {
- dbgx(2, "%s < %s", get_addr2name4(t1->ip, RESOLVE),
- get_addr2name4(t2->ip, RESOLVE));
- return -1;
- }
- dbgx(2, "%s = %s", get_addr2name4(t1->ip, RESOLVE),
- get_addr2name4(t2->ip, RESOLVE));
- return 0;
- }
- /**
- * creates a new TREE * with reasonable defaults
- */
- static tcpr_tree_t *
- new_tree()
- {
- tcpr_tree_t *node;
- node = (tcpr_tree_t *)safe_malloc(sizeof(tcpr_tree_t));
- memset(node, '\0', sizeof(tcpr_tree_t));
- node->server_cnt = 0;
- node->client_cnt = 0;
- node->type = DIR_UNKNOWN;
- node->masklen = -1;
- node->ip = 0;
- return (node);
- }
- /**
- * returns a struct of TREE * from a packet header
- * and sets the type to be SERVER or CLIENT or UNKNOWN
- * if it's an undefined packet, we return -1 for the type
- * the u_char * data should be the data that is passed by pcap_dispatch()
- */
- tcpr_tree_t *
- packet2tree(const u_char * data)
- {
- tcpr_tree_t *node = NULL;
- eth_hdr_t *eth_hdr = NULL;
- ipv4_hdr_t ip_hdr;
- tcp_hdr_t tcp_hdr;
- udp_hdr_t udp_hdr;
- icmpv4_hdr_t icmp_hdr;
- dnsv4_hdr_t dnsv4_hdr;
- node = new_tree();
- eth_hdr = (eth_hdr_t *) (data);
- /* prevent issues with byte alignment, must memcpy */
- memcpy(&ip_hdr, (data + TCPR_ETH_H), TCPR_IPV4_H);
- /* copy over the source mac */
- strncpy((char *)node->mac, (char *)eth_hdr->ether_shost, 6);
- /* copy over the source ip */
- node->ip = ip_hdr.ip_src.s_addr;
- /*
- * TCP
- */
- if (ip_hdr.ip_p == IPPROTO_TCP) {
- dbgx(3, "%s uses TCP... ",
- get_addr2name4(ip_hdr.ip_src.s_addr, RESOLVE));
- /* memcpy it over to prevent alignment issues */
- memcpy(&tcp_hdr, (data + TCPR_ETH_H + (ip_hdr.ip_hl * 4)),
- TCPR_TCP_H);
- /* ftp-data is going to skew our results so we ignore it */
- if (tcp_hdr.th_sport == 20) {
- return (node);
- }
- /* set TREE->type based on TCP flags */
- if (tcp_hdr.th_flags == TH_SYN) {
- node->type = DIR_CLIENT;
- dbg(3, "is a client");
- }
- else if (tcp_hdr.th_flags == (TH_SYN | TH_ACK)) {
- node->type = DIR_SERVER;
- dbg(3, "is a server");
- }
- else {
- dbg(3, "is an unknown");
- }
- /*
- * UDP
- */
- }
- else if (ip_hdr.ip_p == IPPROTO_UDP) {
- /* memcpy over to prevent alignment issues */
- memcpy(&udp_hdr, (data + TCPR_ETH_H + (ip_hdr.ip_hl * 4)),
- TCPR_UDP_H);
- dbgx(3, "%s uses UDP... ",
- get_addr2name4(ip_hdr.ip_src.s_addr, RESOLVE));
- switch (ntohs(udp_hdr.uh_dport)) {
- case 0x0035: /* dns */
- /* prevent memory alignment issues */
- memcpy(&dnsv4_hdr,
- (data + TCPR_ETH_H + (ip_hdr.ip_hl * 4) + TCPR_UDP_H),
- TCPR_DNS_H);
- if (dnsv4_hdr.flags & DNS_QUERY_FLAG) {
- /* bit set, response */
- node->type = DIR_SERVER;
- dbg(3, "is a dns server");
- }
- else {
- /* bit not set, query */
- node->type = DIR_CLIENT;
- dbg(3, "is a dns client");
- }
- return (node);
- break;
- default:
- break;
- }
- switch (ntohs(udp_hdr.uh_sport)) {
- case 0x0035: /* dns */
- /* prevent memory alignment issues */
- memcpy(&dnsv4_hdr,
- (data + TCPR_ETH_H + (ip_hdr.ip_hl * 4) + TCPR_UDP_H),
- TCPR_DNS_H);
- if ((dnsv4_hdr.flags & 0x7FFFF) ^ DNS_QUERY_FLAG) {
- /* bit set, response */
- node->type = DIR_SERVER;
- dbg(3, "is a dns server");
- }
- else {
- /* bit not set, query */
- node->type = DIR_CLIENT;
- dbg(3, "is a dns client");
- }
- return (node);
- break;
- default:
- dbgx(3, "unknown UDP protocol: %hu->%hu", udp_hdr.uh_sport,
- udp_hdr.uh_dport);
- break;
- }
- /*
- * ICMP
- */
- }
- else if (ip_hdr.ip_p == IPPROTO_ICMP) {
- /* prevent alignment issues */
- memcpy(&icmp_hdr, (data + TCPR_ETH_H + (ip_hdr.ip_hl * 4)),
- TCPR_ICMPV4_H);
- dbgx(3, "%s uses ICMP... ",
- get_addr2name4(ip_hdr.ip_src.s_addr, RESOLVE));
- /*
- * if port unreachable, then source == server, dst == client
- */
- if ((icmp_hdr.icmp_type == ICMP_UNREACH) &&
- (icmp_hdr.icmp_code == ICMP_UNREACH_PORT)) {
- node->type = DIR_SERVER;
- dbg(3, "is a server with a closed port");
- }
- }
- return (node);
- }
- /**
- * prints out a node of the tree to stderr
- */
- static char *
- tree_printnode(const char *name, const tcpr_tree_t *node)
- {
- memset(&tree_print_buff, '\0', TREEPRINTBUFFLEN);
- if (node == NULL) {
- snprintf(tree_print_buff, TREEPRINTBUFFLEN, "%s node is null", name);
- }
- else {
- snprintf(tree_print_buff, TREEPRINTBUFFLEN,
- "-- %s: %p\nIP: %s\nMask: %d\nSrvr: %d\nClnt: %d\n",
- name, (void *)node, get_addr2name4(node->ip, RESOLVE),
- node->masklen, node->server_cnt, node->client_cnt);
- if (node->type == DIR_SERVER) {
- strlcat(tree_print_buff, "Type: Server\n--\n", TREEPRINTBUFFLEN);
- }
- else {
- strlcat(tree_print_buff, "Type: Client\n--", TREEPRINTBUFFLEN);
- }
- }
- return (tree_print_buff);
- }
- /**
- * prints out the entire tree
- */
- static char *
- tree_print(tcpr_data_tree_t *treeroot)
- {
- tcpr_tree_t *node = NULL;
- memset(&tree_print_buff, '\0', TREEPRINTBUFFLEN);
- RB_FOREACH(node, tcpr_data_tree_s, treeroot) {
- tree_printnode("my node", node);
- }
- return (tree_print_buff);
- }
- /*
- Local Variables:
- mode:c
- indent-tabs-mode:nil
- c-basic-offset:4
- End:
- */
|