tree.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include "defines.h"
  21. #include "common.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "tree.h"
  26. #include "tcpprep.h"
  27. #include "tcpprep_opts.h"
  28. #include "tcpprep_api.h"
  29. extern tcpr_data_tree_t treeroot;
  30. extern tcpprep_t *tcpprep;
  31. #ifdef DEBUG
  32. extern int debug;
  33. #endif
  34. /* static buffer used by tree_print*() functions */
  35. char tree_print_buff[TREEPRINTBUFFLEN];
  36. static tcpr_tree_t *new_tree();
  37. static tcpr_tree_t *packet2tree(const u_char *);
  38. #ifdef DEBUG /* prevent compile warnings */
  39. static char *tree_print(tcpr_data_tree_t *);
  40. static char *tree_printnode(const char *, const tcpr_tree_t *);
  41. #endif /* DEBUG */
  42. static void tree_buildcidr(tcpr_data_tree_t *, tcpr_buildcidr_t *);
  43. static int tree_checkincidr(tcpr_data_tree_t *, tcpr_buildcidr_t *);
  44. static int ipv6_cmp(const struct tcpr_in6_addr *a, const struct tcpr_in6_addr *b);
  45. RB_PROTOTYPE(tcpr_data_tree_s, tcpr_tree_s, node, tree_comp)
  46. RB_GENERATE(tcpr_data_tree_s, tcpr_tree_s, node, tree_comp)
  47. /**
  48. * used with rbwalk to walk a tree and generate cidr_t * cidrdata.
  49. * is smart enough to prevent dupes. void * arg is cast to bulidcidr_t
  50. */
  51. void
  52. tree_buildcidr(tcpr_data_tree_t *treeroot, tcpr_buildcidr_t * bcdata)
  53. {
  54. tcpr_tree_t *node = NULL;
  55. tcpr_cidr_t *newcidr = NULL;
  56. unsigned long network = 0;
  57. struct tcpr_in6_addr network6;
  58. unsigned long mask = ~0; /* turn on all bits */
  59. tcpprep_opt_t *options = tcpprep->options;
  60. int i, j, k;
  61. dbg(1, "Running: tree_buildcidr()");
  62. RB_FOREACH(node, tcpr_data_tree_s, treeroot) {
  63. /* we only check types that are valid */
  64. if (bcdata->type != DIR_ANY) /* don't check if we're adding ANY */
  65. if (bcdata->type != node->type) /* no match, exit early */
  66. return;
  67. /*
  68. * in cases of leaves and last visit add to cidrdata if
  69. * necessary. First check IPv4
  70. */
  71. dbgx(4, "Checking if %s exists in cidrdata...", get_addr2name4(node->u.ip, RESOLVE));
  72. if (node->family == AF_INET) {
  73. if (! check_ip_cidr(options->cidrdata, node->u.ip)) { /* if we exist, abort */
  74. dbgx(3, "Node %s doesn't exist... creating.",
  75. get_addr2name4(node->u.ip, RESOLVE));
  76. newcidr = new_cidr();
  77. newcidr->masklen = bcdata->masklen;
  78. network = node->u.ip & (mask << (32 - bcdata->masklen));
  79. dbgx(3, "Using network: %s", get_addr2name4(network, RESOLVE));
  80. newcidr->u.network = network;
  81. add_cidr(&options->cidrdata, &newcidr);
  82. }
  83. }
  84. /* Check IPv6 Address */
  85. else if (node->family == AF_INET6) {
  86. if (! check_ip6_cidr(options->cidrdata, &node->u.ip6)) { /* if we exist, abort */
  87. dbgx(3, "Node %s doesn't exist... creating.",
  88. get_addr2name6(&node->u.ip6, RESOLVE));
  89. newcidr = new_cidr();
  90. newcidr->masklen = bcdata->masklen;
  91. /* init each 4 quads to zero */
  92. for (i = 0; i < 4; i++)
  93. network6.tcpr_s6_addr32[i] = 0;
  94. /* Build our mask */
  95. j = bcdata->masklen / 8;
  96. for (i = 0; i < j; i++)
  97. network6.tcpr_s6_addr[i] = node->u.ip6.tcpr_s6_addr[i];
  98. if ((k = bcdata->masklen % 8) != 0) {
  99. k = ~0 << (8 - k);
  100. network6.tcpr_s6_addr[j] = node->u.ip6.tcpr_s6_addr[i] & k;
  101. }
  102. dbgx(3, "Using network: %s", get_addr2name6(&network6, RESOLVE));
  103. newcidr->u.network6 = network6;
  104. add_cidr(&options->cidrdata, &newcidr);
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * uses rbwalk to check to see if a given ip address of a given type in the
  111. * tree is inside any of the cidrdata
  112. */
  113. static int
  114. tree_checkincidr(tcpr_data_tree_t *treeroot, tcpr_buildcidr_t * bcdata)
  115. {
  116. tcpr_tree_t *node = NULL;
  117. tcpprep_opt_t *options = tcpprep->options;
  118. RB_FOREACH(node, tcpr_data_tree_s, treeroot) {
  119. /* we only check types that are valid */
  120. if (bcdata->type != DIR_ANY) /* don't check if we're adding ANY */
  121. if (bcdata->type != node->type) /* no match, exit early */
  122. return 0;
  123. /*
  124. * in cases of leaves and last visit add to cidrdata if
  125. * necessary
  126. */
  127. if (node->family == AF_INET && check_ip_cidr(options->cidrdata, node->u.ip)) /* if we exist, abort */
  128. return 1;
  129. if (node->family == AF_INET6 && check_ip6_cidr(options->cidrdata, &node->u.ip6))
  130. return 1;
  131. }
  132. return 0;
  133. }
  134. /**
  135. * processes the tree using rbwalk / tree2cidr to generate a CIDR
  136. * used for 2nd pass, router mode
  137. *
  138. * returns > 0 for success (the mask len), 0 for fail
  139. */
  140. int
  141. process_tree(void)
  142. {
  143. int mymask = 0;
  144. tcpr_buildcidr_t *bcdata;
  145. tcpprep_opt_t *options = tcpprep->options;
  146. dbg(1, "Running: process_tree()");
  147. bcdata = (tcpr_buildcidr_t *)safe_malloc(sizeof(tcpr_buildcidr_t));
  148. for (mymask = options->max_mask; mymask <= options->min_mask; mymask++) {
  149. dbgx(1, "Current mask: %u", mymask);
  150. /* set starting vals */
  151. bcdata->type = DIR_SERVER;
  152. bcdata->masklen = mymask;
  153. /* build cidrdata with servers */
  154. tree_buildcidr(&treeroot, bcdata);
  155. /* calculate types of all IP's */
  156. tree_calculate(&treeroot);
  157. /* try to find clients in cidrdata */
  158. bcdata->type = DIR_CLIENT;
  159. if (! tree_checkincidr(&treeroot, bcdata)) { /* didn't find any clients in cidrdata */
  160. safe_free(bcdata);
  161. return (mymask); /* success! */
  162. }
  163. else {
  164. destroy_cidr(options->cidrdata); /* clean up after our mess */
  165. options->cidrdata = NULL;
  166. }
  167. }
  168. safe_free(bcdata);
  169. /* we failed to find a valid cidr list */
  170. notice("Unable to determine any IP addresses as a clients.");
  171. notice("Perhaps you should change the --ratio, --minmask/maxmask settings, or try another mode?");
  172. return (0);
  173. }
  174. /*
  175. * processes rbdata to bulid cidrdata based upon the
  176. * given type (SERVER, CLIENT, UNKNOWN) using the given masklen
  177. *
  178. * is smart enough to prevent dupes
  179. void
  180. tcpr_tree_to_cidr(const int masklen, const int type)
  181. {
  182. }
  183. */
  184. /**
  185. * Checks to see if an IP is client or server by finding it in the tree
  186. * returns TCPR_DIR_C2S or TCPR_DIR_S2C or -1 on error
  187. * if mode = UNKNOWN, then abort on unknowns
  188. * if mode = CLIENT, then unknowns become clients
  189. * if mode = SERVER, then unknowns become servers
  190. */
  191. tcpr_dir_t
  192. check_ip_tree(const int mode, const unsigned long ip)
  193. {
  194. tcpr_tree_t *node = NULL, *finder = NULL;
  195. finder = new_tree();
  196. finder->family = AF_INET;
  197. finder->u.ip = ip;
  198. node = RB_FIND(tcpr_data_tree_s, &treeroot, finder);
  199. if (node == NULL && mode == DIR_UNKNOWN)
  200. errx(-1, "%s (%lu) is an unknown system... aborting.!\n"
  201. "Try a different auto mode (-n router|client|server)",
  202. get_addr2name4(ip, RESOLVE), ip);
  203. #ifdef DEBUG
  204. switch (node->type) {
  205. case DIR_SERVER:
  206. dbgx(1, "DIR_SERVER: %s", get_addr2name4(ip, RESOLVE));
  207. break;
  208. case DIR_CLIENT:
  209. dbgx(1, "DIR_CLIENT: %s", get_addr2name4(ip, RESOLVE));
  210. break;
  211. case DIR_UNKNOWN:
  212. dbgx(1, "DIR_UNKNOWN: %s", get_addr2name4(ip, RESOLVE));
  213. break;
  214. case DIR_ANY:
  215. dbgx(1, "DIR_ANY: %s", get_addr2name4(ip, RESOLVE));
  216. break;
  217. }
  218. #endif
  219. /* return node type if we found the node, else return the default (mode) */
  220. if (node != NULL) {
  221. switch (node->type) {
  222. case DIR_SERVER:
  223. return TCPR_DIR_S2C;
  224. break;
  225. case DIR_CLIENT:
  226. return TCPR_DIR_C2S;
  227. break;
  228. case DIR_UNKNOWN:
  229. case DIR_ANY:
  230. /* use our current mode to determine return code */
  231. goto return_unknown;
  232. default:
  233. errx(-1, "Node for %s has invalid type: %d", get_addr2name4(ip, RESOLVE), node->type);
  234. }
  235. }
  236. return_unknown:
  237. safe_free(finder);
  238. switch (mode) {
  239. case DIR_SERVER:
  240. return TCPR_DIR_S2C;
  241. break;
  242. case DIR_CLIENT:
  243. return TCPR_DIR_C2S;
  244. break;
  245. default:
  246. return -1;
  247. }
  248. }
  249. tcpr_dir_t
  250. check_ip6_tree(const int mode, const struct tcpr_in6_addr *addr)
  251. {
  252. tcpr_tree_t *node = NULL, *finder = NULL;
  253. finder = new_tree();
  254. finder->family = AF_INET6;
  255. finder->u.ip6 = *addr;
  256. node = RB_FIND(tcpr_data_tree_s, &treeroot, finder);
  257. if (node == NULL && mode == DIR_UNKNOWN)
  258. errx(-1, "%s is an unknown system... aborting.!\n"
  259. "Try a different auto mode (-n router|client|server)",
  260. get_addr2name6(addr, RESOLVE));
  261. #ifdef DEBUG
  262. switch (node->type) {
  263. case DIR_SERVER:
  264. dbgx(1, "DIR_SERVER: %s", get_addr2name6(addr, RESOLVE));
  265. break;
  266. case DIR_CLIENT:
  267. dbgx(1, "DIR_CLIENT: %s", get_addr2name6(addr, RESOLVE));
  268. break;
  269. case DIR_UNKNOWN:
  270. dbgx(1, "DIR_UNKNOWN: %s", get_addr2name6(addr, RESOLVE));
  271. break;
  272. case DIR_ANY:
  273. dbgx(1, "DIR_ANY: %s", get_addr2name6(addr, RESOLVE));
  274. break;
  275. }
  276. #endif
  277. /*
  278. * FIXME: Is this logic correct? I think this might be backwards :(
  279. */
  280. /* return node type if we found the node, else return the default (mode) */
  281. if (node != NULL) {
  282. switch (node->type) {
  283. case DIR_SERVER:
  284. return TCPR_DIR_C2S;
  285. break;
  286. case DIR_CLIENT:
  287. return TCPR_DIR_S2C;
  288. break;
  289. case DIR_UNKNOWN:
  290. case DIR_ANY:
  291. /* use our current mode to determine return code */
  292. goto return_unknown;
  293. default:
  294. errx(-1, "Node for %s has invalid type: %d", get_addr2name6(addr, RESOLVE), node->type);
  295. }
  296. }
  297. return_unknown:
  298. safe_free(finder);
  299. switch (mode) {
  300. case DIR_SERVER:
  301. return TCPR_DIR_C2S;
  302. break;
  303. case DIR_CLIENT:
  304. return TCPR_DIR_S2C;
  305. break;
  306. default:
  307. return -1;
  308. }
  309. }
  310. /**
  311. * Parses the IP header of the given packet (data) to get the SRC/DST IP
  312. * addresses. If the SRC IP doesn't exist in the TREE, we add it as a
  313. * client, if the DST IP doesn't exist in the TREE, we add it as a server
  314. */
  315. void
  316. add_tree_first_ipv4(const u_char *data)
  317. {
  318. tcpr_tree_t *newnode = NULL, *findnode;
  319. ipv4_hdr_t ip_hdr;
  320. assert(data);
  321. /*
  322. * first add/find the source IP/client
  323. */
  324. newnode = new_tree();
  325. /* prevent issues with byte alignment, must memcpy */
  326. memcpy(&ip_hdr, (data + TCPR_ETH_H), TCPR_IPV4_H);
  327. /* copy over the source ip, and values to gurantee this a client */
  328. newnode->family = AF_INET;
  329. newnode->u.ip = ip_hdr.ip_src.s_addr;
  330. newnode->type = DIR_CLIENT;
  331. newnode->client_cnt = 1000;
  332. findnode = RB_FIND(tcpr_data_tree_s, &treeroot, newnode);
  333. /* if we didn't find it, add it to the tree, else free it */
  334. if (findnode == NULL) {
  335. RB_INSERT(tcpr_data_tree_s, &treeroot, newnode);
  336. } else {
  337. safe_free(newnode);
  338. }
  339. /*
  340. * now add/find the destination IP/server
  341. */
  342. newnode = new_tree();
  343. memcpy(&ip_hdr, (data + TCPR_ETH_H), TCPR_IPV4_H);
  344. newnode->family = AF_INET;
  345. newnode->u.ip = ip_hdr.ip_dst.s_addr;
  346. newnode->type = DIR_SERVER;
  347. newnode->server_cnt = 1000;
  348. findnode = RB_FIND(tcpr_data_tree_s, &treeroot, newnode);
  349. if (findnode == NULL) {
  350. RB_INSERT(tcpr_data_tree_s, &treeroot, newnode);
  351. } else {
  352. safe_free(newnode);
  353. }
  354. }
  355. void
  356. add_tree_first_ipv6(const u_char *data)
  357. {
  358. tcpr_tree_t *newnode = NULL, *findnode;
  359. ipv6_hdr_t ip6_hdr;
  360. assert(data);
  361. /*
  362. * first add/find the source IP/client
  363. */
  364. newnode = new_tree();
  365. /* prevent issues with byte alignment, must memcpy */
  366. memcpy(&ip6_hdr, (data + TCPR_ETH_H), TCPR_IPV6_H);
  367. /* copy over the source ip, and values to gurantee this a client */
  368. newnode->family = AF_INET6;
  369. newnode->u.ip6 = ip6_hdr.ip_src;
  370. newnode->type = DIR_CLIENT;
  371. newnode->client_cnt = 1000;
  372. findnode = RB_FIND(tcpr_data_tree_s, &treeroot, newnode);
  373. /* if we didn't find it, add it to the tree, else free it */
  374. if (findnode == NULL) {
  375. RB_INSERT(tcpr_data_tree_s, &treeroot, newnode);
  376. } else {
  377. safe_free(newnode);
  378. }
  379. /*
  380. * now add/find the destination IP/server
  381. */
  382. newnode = new_tree();
  383. memcpy(&ip6_hdr, (data + TCPR_ETH_H), TCPR_IPV6_H);
  384. newnode->family = AF_INET6;
  385. newnode->u.ip6 = ip6_hdr.ip_dst;
  386. newnode->type = DIR_SERVER;
  387. newnode->server_cnt = 1000;
  388. findnode = RB_FIND(tcpr_data_tree_s, &treeroot, newnode);
  389. if (findnode == NULL) {
  390. RB_INSERT(tcpr_data_tree_s, &treeroot, newnode);
  391. } else {
  392. safe_free(newnode);
  393. }
  394. }
  395. static void
  396. add_tree_node(tcpr_tree_t *newnode)
  397. {
  398. tcpr_tree_t *node;
  399. /* try to find a simular entry in the tree */
  400. node = RB_FIND(tcpr_data_tree_s, &treeroot, newnode);
  401. dbgx(3, "%s", tree_printnode("add_tree", node));
  402. /* new entry required */
  403. if (node == NULL) {
  404. /* increment counters */
  405. if (newnode->type == DIR_SERVER) {
  406. newnode->server_cnt++;
  407. }
  408. else if (newnode->type == DIR_CLIENT) {
  409. newnode->client_cnt++;
  410. }
  411. /* insert it in */
  412. RB_INSERT(tcpr_data_tree_s, &treeroot, newnode);
  413. }
  414. else {
  415. /* we found something, so update it */
  416. dbgx(2, " node: %p\nnewnode: %p", node, newnode);
  417. dbgx(3, "%s", tree_printnode("update node", node));
  418. /* increment counter */
  419. if (newnode->type == DIR_SERVER) {
  420. node->server_cnt++;
  421. }
  422. else if (newnode->type == DIR_CLIENT) {
  423. /* temp debug code */
  424. node->client_cnt++;
  425. }
  426. /* didn't insert it, so free it */
  427. safe_free(newnode);
  428. }
  429. dbg(2, "------- START NEXT -------");
  430. dbgx(3, "%s", tree_print(&treeroot));
  431. }
  432. /**
  433. * adds an entry to the tree (phase 1 of auto mode). We add each host
  434. * to the tree if it doesn't yet exist. We go through and track:
  435. * - number of times each host acts as a client or server
  436. * - the way the host acted the first time we saw it (client or server)
  437. */
  438. void
  439. add_tree_ipv4(const unsigned long ip, const u_char * data)
  440. {
  441. tcpr_tree_t *newnode = NULL;
  442. assert(data);
  443. newnode = packet2tree(data);
  444. assert(ip == newnode->u.ip);
  445. if (newnode->type == DIR_UNKNOWN) {
  446. /* couldn't figure out if packet was client or server */
  447. dbgx(2, "%s (%lu) unknown client/server",
  448. get_addr2name4(newnode->u.ip, RESOLVE), newnode->u.ip);
  449. }
  450. add_tree_node(newnode);
  451. }
  452. void
  453. add_tree_ipv6(const struct tcpr_in6_addr * addr, const u_char * data)
  454. {
  455. tcpr_tree_t *newnode = NULL;
  456. assert(data);
  457. newnode = packet2tree(data);
  458. assert(ipv6_cmp(addr, &newnode->u.ip6) == 0);
  459. if (newnode->type == DIR_UNKNOWN) {
  460. /* couldn't figure out if packet was client or server */
  461. dbgx(2, "%s unknown client/server",
  462. get_addr2name6(&newnode->u.ip6, RESOLVE));
  463. }
  464. add_tree_node(newnode);
  465. }
  466. /**
  467. * calculates whether each node in the tree is a client, server, or unknown for each node in the tree
  468. */
  469. void
  470. tree_calculate(tcpr_data_tree_t *treeroot)
  471. {
  472. tcpr_tree_t *node;
  473. tcpprep_opt_t *options = tcpprep->options;
  474. dbg(1, "Running tree_calculate()");
  475. RB_FOREACH(node, tcpr_data_tree_s, treeroot) {
  476. dbgx(4, "Processing %s", get_addr2name4(node->u.ip, RESOLVE));
  477. if ((node->server_cnt > 0) || (node->client_cnt > 0)) {
  478. /* type based on: server >= (client*ratio) */
  479. if ((double)node->server_cnt >= (double)node->client_cnt * options->ratio) {
  480. node->type = DIR_SERVER;
  481. dbgx(3, "Setting %s to server",
  482. get_addr2name4(node->u.ip, RESOLVE));
  483. }
  484. else {
  485. node->type = DIR_CLIENT;
  486. dbgx(3, "Setting %s to client",
  487. get_addr2name4(node->u.ip, RESOLVE));
  488. }
  489. }
  490. else { /* IP had no client or server connections */
  491. node->type = DIR_UNKNOWN;
  492. dbgx(3, "Setting %s to unknown",
  493. get_addr2name4(node->u.ip, RESOLVE));
  494. }
  495. }
  496. }
  497. static int
  498. ipv6_cmp(const struct tcpr_in6_addr *a, const struct tcpr_in6_addr *b)
  499. {
  500. int i, k;
  501. for (i = 0; i < 4; i++) {
  502. if ((k = (a->tcpr_s6_addr32[i] - b->tcpr_s6_addr32[i]))) {
  503. return (k > 0) ? 1 : -1;
  504. }
  505. }
  506. return 0;
  507. }
  508. /**
  509. * tree_comp(), called by rbsearch compares two treees and returns:
  510. * 1 = first > second
  511. * -1 = first < second
  512. * 0 = first = second
  513. * based upon the ip address stored
  514. *
  515. */
  516. int
  517. tree_comp(tcpr_tree_t *t1, tcpr_tree_t *t2)
  518. {
  519. int ret;
  520. if (t1->family > t2->family) {
  521. dbgx(2, "family %d > %d", t1->family, t2->family);
  522. return 1;
  523. }
  524. if (t1->family < t2->family) {
  525. dbgx(2, "family %d < %d", t1->family, t2->family);
  526. return -1;
  527. }
  528. if (t1->family == AF_INET) {
  529. if (t1->u.ip > t2->u.ip) {
  530. dbgx(2, "%s > %s", get_addr2name4(t1->u.ip, RESOLVE),
  531. get_addr2name4(t2->u.ip, RESOLVE));
  532. return 1;
  533. }
  534. if (t1->u.ip < t2->u.ip) {
  535. dbgx(2, "%s < %s", get_addr2name4(t1->u.ip, RESOLVE),
  536. get_addr2name4(t2->u.ip, RESOLVE));
  537. return -1;
  538. }
  539. dbgx(2, "%s = %s", get_addr2name4(t1->u.ip, RESOLVE),
  540. get_addr2name4(t2->u.ip, RESOLVE));
  541. return 0;
  542. }
  543. if (t1->family == AF_INET6) {
  544. ret = ipv6_cmp(&t1->u.ip6, &t1->u.ip6);
  545. dbgx(2, "cmp(%s, %s) = %d", get_addr2name6(&t1->u.ip6, RESOLVE),
  546. get_addr2name6(&t2->u.ip6, RESOLVE), ret);
  547. return ret;
  548. }
  549. return 0;
  550. }
  551. /**
  552. * creates a new TREE * with reasonable defaults
  553. */
  554. static tcpr_tree_t *
  555. new_tree()
  556. {
  557. tcpr_tree_t *node;
  558. node = (tcpr_tree_t *)safe_malloc(sizeof(tcpr_tree_t));
  559. memset(node, '\0', sizeof(tcpr_tree_t));
  560. node->server_cnt = 0;
  561. node->client_cnt = 0;
  562. node->type = DIR_UNKNOWN;
  563. node->masklen = -1;
  564. node->u.ip = 0;
  565. return (node);
  566. }
  567. /**
  568. * returns a struct of TREE * from a packet header
  569. * and sets the type to be SERVER or CLIENT or UNKNOWN
  570. * if it's an undefined packet, we return -1 for the type
  571. * the u_char * data should be the data that is passed by pcap_dispatch()
  572. */
  573. tcpr_tree_t *
  574. packet2tree(const u_char * data)
  575. {
  576. tcpr_tree_t *node = NULL;
  577. eth_hdr_t *eth_hdr = NULL;
  578. ipv4_hdr_t ip_hdr;
  579. ipv6_hdr_t ip6_hdr;
  580. tcp_hdr_t tcp_hdr;
  581. udp_hdr_t udp_hdr;
  582. icmpv4_hdr_t icmp_hdr;
  583. dnsv4_hdr_t dnsv4_hdr;
  584. u_int16_t ether_type;
  585. u_char proto = 0;
  586. int hl = 0;
  587. #ifdef DEBUG
  588. char srcip[INET6_ADDRSTRLEN];
  589. #endif
  590. node = new_tree();
  591. eth_hdr = (eth_hdr_t *) (data);
  592. /* prevent issues with byte alignment, must memcpy */
  593. memcpy(&ether_type, (u_char*)eth_hdr + 12, 2);
  594. /* drop VLAN info if it exists before the IP info */
  595. if (ether_type == htons(ETHERTYPE_VLAN)) {
  596. dbg(4,"Processing as VLAN traffic...");
  597. /* prevent issues with byte alignment, must memcpy */
  598. memcpy(&ether_type, (u_char*)eth_hdr + 16, 2);
  599. hl += 4;
  600. }
  601. if (ether_type == htons(ETHERTYPE_IP)) {
  602. memcpy(&ip_hdr, (data + TCPR_ETH_H + hl), TCPR_IPV4_H);
  603. node->family = AF_INET;
  604. node->u.ip = ip_hdr.ip_src.s_addr;
  605. proto = ip_hdr.ip_p;
  606. hl += ip_hdr.ip_hl * 4;
  607. #ifdef DEBUG
  608. strlcpy(srcip, get_addr2name4(ip_hdr.ip_src.s_addr,
  609. RESOLVE), 16);
  610. #endif
  611. } else if (ether_type == htons(ETHERTYPE_IP6)) {
  612. memcpy(&ip6_hdr, (data + TCPR_ETH_H + hl), TCPR_IPV6_H);
  613. node->family = AF_INET6;
  614. node->u.ip6 = ip6_hdr.ip_src;
  615. proto = ip6_hdr.ip_nh;
  616. hl += TCPR_IPV6_H;
  617. #ifdef DEBUG
  618. strlcpy(srcip, get_addr2name6(&ip6_hdr.ip_src, RESOLVE), INET6_ADDRSTRLEN);
  619. #endif
  620. } else {
  621. dbgx(2,"Unrecognized ether_type (%x)", ether_type);
  622. }
  623. /* copy over the source mac */
  624. memset(&node->mac[0], 0, sizeof(node->mac));
  625. strncpy((char *)node->mac, (char *)eth_hdr->ether_shost,
  626. sizeof(node->mac) - 1);
  627. /*
  628. * TCP
  629. */
  630. if (proto == IPPROTO_TCP) {
  631. dbgx(3, "%s uses TCP... ", srcip);
  632. /* memcpy it over to prevent alignment issues */
  633. memcpy(&tcp_hdr, (data + TCPR_ETH_H + hl), TCPR_TCP_H);
  634. /* ftp-data is going to skew our results so we ignore it */
  635. if (tcp_hdr.th_sport == 20)
  636. return (node);
  637. /* set TREE->type based on TCP flags */
  638. if (tcp_hdr.th_flags == TH_SYN) {
  639. node->type = DIR_CLIENT;
  640. dbg(3, "is a client");
  641. }
  642. else if (tcp_hdr.th_flags == (TH_SYN | TH_ACK)) {
  643. node->type = DIR_SERVER;
  644. dbg(3, "is a server");
  645. }
  646. else {
  647. dbg(3, "is an unknown");
  648. }
  649. }
  650. /*
  651. * UDP
  652. */
  653. else if (proto == IPPROTO_UDP) {
  654. /* memcpy over to prevent alignment issues */
  655. memcpy(&udp_hdr, (data + TCPR_ETH_H + hl), TCPR_UDP_H);
  656. dbgx(3, "%s uses UDP... ", srcip);
  657. switch (ntohs(udp_hdr.uh_dport)) {
  658. case 0x0035: /* dns */
  659. /* prevent memory alignment issues */
  660. memcpy(&dnsv4_hdr,
  661. (data + TCPR_ETH_H + hl + TCPR_UDP_H), TCPR_DNS_H);
  662. if (dnsv4_hdr.flags & DNS_QUERY_FLAG) {
  663. /* bit set, response */
  664. node->type = DIR_SERVER;
  665. dbg(3, "is a dns server");
  666. }
  667. else {
  668. /* bit not set, query */
  669. node->type = DIR_CLIENT;
  670. dbg(3, "is a dns client");
  671. }
  672. return (node);
  673. break;
  674. default:
  675. break;
  676. }
  677. switch (ntohs(udp_hdr.uh_sport)) {
  678. case 0x0035: /* dns */
  679. /* prevent memory alignment issues */
  680. memcpy(&dnsv4_hdr,
  681. (data + TCPR_ETH_H + hl + TCPR_UDP_H),
  682. TCPR_DNS_H);
  683. if ((dnsv4_hdr.flags & 0x7FFFF) ^ DNS_QUERY_FLAG) {
  684. /* bit set, response */
  685. node->type = DIR_SERVER;
  686. dbg(3, "is a dns server");
  687. }
  688. else {
  689. /* bit not set, query */
  690. node->type = DIR_CLIENT;
  691. dbg(3, "is a dns client");
  692. }
  693. return (node);
  694. break;
  695. default:
  696. dbgx(3, "unknown UDP protocol: %hu->%hu", udp_hdr.uh_sport,
  697. udp_hdr.uh_dport);
  698. break;
  699. }
  700. }
  701. /*
  702. * ICMP
  703. */
  704. else if (proto == IPPROTO_ICMP) {
  705. /* prevent alignment issues */
  706. memcpy(&icmp_hdr, (data + TCPR_ETH_H + hl), TCPR_ICMPV4_H);
  707. dbgx(3, "%s uses ICMP... ", srcip);
  708. /*
  709. * if port unreachable, then source == server, dst == client
  710. */
  711. if ((icmp_hdr.icmp_type == ICMP_UNREACH) &&
  712. (icmp_hdr.icmp_code == ICMP_UNREACH_PORT)) {
  713. node->type = DIR_SERVER;
  714. dbg(3, "is a server with a closed port");
  715. }
  716. }
  717. return (node);
  718. }
  719. #ifdef DEBUG
  720. /**
  721. * prints out a node of the tree to stderr
  722. */
  723. static char *
  724. tree_printnode(const char *name, const tcpr_tree_t *node)
  725. {
  726. memset(&tree_print_buff, '\0', TREEPRINTBUFFLEN);
  727. if (node == NULL) {
  728. snprintf(tree_print_buff, TREEPRINTBUFFLEN, "%s node is null", name);
  729. }
  730. else {
  731. snprintf(tree_print_buff, TREEPRINTBUFFLEN,
  732. "-- %s: %p\nIP: %s\nMask: %d\nSrvr: %d\nClnt: %d\n",
  733. name, (void *)node, node->family == AF_INET ?
  734. get_addr2name4(node->u.ip, RESOLVE) :
  735. get_addr2name6(&node->u.ip6, RESOLVE),
  736. node->masklen, node->server_cnt, node->client_cnt);
  737. if (node->type == DIR_SERVER) {
  738. strlcat(tree_print_buff, "Type: Server\n--\n", TREEPRINTBUFFLEN);
  739. }
  740. else {
  741. strlcat(tree_print_buff, "Type: Client\n--", TREEPRINTBUFFLEN);
  742. }
  743. }
  744. return (tree_print_buff);
  745. }
  746. /**
  747. * prints out the entire tree
  748. */
  749. static char *
  750. tree_print(tcpr_data_tree_t *treeroot)
  751. {
  752. tcpr_tree_t *node = NULL;
  753. memset(&tree_print_buff, '\0', TREEPRINTBUFFLEN);
  754. RB_FOREACH(node, tcpr_data_tree_s, treeroot) {
  755. tree_printnode("my node", node);
  756. }
  757. return (tree_print_buff);
  758. }
  759. #endif /* DEBUG */