tree.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. /* return node type if we found the node, else return the default (mode) */
  204. if (node != NULL) {
  205. #ifdef DEBUG
  206. switch (node->type) {
  207. case DIR_SERVER:
  208. dbgx(1, "DIR_SERVER: %s", get_addr2name4(ip, RESOLVE));
  209. break;
  210. case DIR_CLIENT:
  211. dbgx(1, "DIR_CLIENT: %s", get_addr2name4(ip, RESOLVE));
  212. break;
  213. case DIR_UNKNOWN:
  214. dbgx(1, "DIR_UNKNOWN: %s", get_addr2name4(ip, RESOLVE));
  215. break;
  216. case DIR_ANY:
  217. dbgx(1, "DIR_ANY: %s", get_addr2name4(ip, RESOLVE));
  218. break;
  219. }
  220. #endif
  221. switch (node->type) {
  222. case DIR_SERVER:
  223. safe_free(finder);
  224. return TCPR_DIR_S2C;
  225. break;
  226. case DIR_CLIENT:
  227. safe_free(finder);
  228. return TCPR_DIR_C2S;
  229. break;
  230. case DIR_UNKNOWN:
  231. case DIR_ANY:
  232. /* use our current mode to determine return code */
  233. goto return_unknown;
  234. default:
  235. errx(-1, "Node for %s has invalid type: %d", get_addr2name4(ip, RESOLVE), node->type);
  236. }
  237. }
  238. return_unknown:
  239. safe_free(finder);
  240. switch (mode) {
  241. case DIR_SERVER:
  242. return TCPR_DIR_S2C;
  243. break;
  244. case DIR_CLIENT:
  245. return TCPR_DIR_C2S;
  246. break;
  247. default:
  248. return -1;
  249. }
  250. }
  251. tcpr_dir_t
  252. check_ip6_tree(const int mode, const struct tcpr_in6_addr *addr)
  253. {
  254. tcpr_tree_t *node = NULL, *finder = NULL;
  255. finder = new_tree();
  256. finder->family = AF_INET6;
  257. finder->u.ip6 = *addr;
  258. node = RB_FIND(tcpr_data_tree_s, &treeroot, finder);
  259. if (node == NULL && mode == DIR_UNKNOWN)
  260. errx(-1, "%s is an unknown system... aborting.!\n"
  261. "Try a different auto mode (-n router|client|server)",
  262. get_addr2name6(addr, RESOLVE));
  263. /*
  264. * FIXME: Is this logic correct? I think this might be backwards :(
  265. */
  266. /* return node type if we found the node, else return the default (mode) */
  267. if (node != NULL) {
  268. #ifdef DEBUG
  269. switch (node->type) {
  270. case DIR_SERVER:
  271. dbgx(1, "DIR_SERVER: %s", get_addr2name6(addr, RESOLVE));
  272. break;
  273. case DIR_CLIENT:
  274. dbgx(1, "DIR_CLIENT: %s", get_addr2name6(addr, RESOLVE));
  275. break;
  276. case DIR_UNKNOWN:
  277. dbgx(1, "DIR_UNKNOWN: %s", get_addr2name6(addr, RESOLVE));
  278. break;
  279. case DIR_ANY:
  280. dbgx(1, "DIR_ANY: %s", get_addr2name6(addr, RESOLVE));
  281. break;
  282. }
  283. #endif
  284. switch (node->type) {
  285. case DIR_SERVER:
  286. return TCPR_DIR_C2S;
  287. break;
  288. case DIR_CLIENT:
  289. return TCPR_DIR_S2C;
  290. break;
  291. case DIR_UNKNOWN:
  292. case DIR_ANY:
  293. /* use our current mode to determine return code */
  294. goto return_unknown;
  295. default:
  296. errx(-1, "Node for %s has invalid type: %d", get_addr2name6(addr, RESOLVE), node->type);
  297. }
  298. }
  299. return_unknown:
  300. safe_free(finder);
  301. switch (mode) {
  302. case DIR_SERVER:
  303. return TCPR_DIR_C2S;
  304. break;
  305. case DIR_CLIENT:
  306. return TCPR_DIR_S2C;
  307. break;
  308. default:
  309. return -1;
  310. }
  311. }
  312. /**
  313. * Parses the IP header of the given packet (data) to get the SRC/DST IP
  314. * addresses. If the SRC IP doesn't exist in the TREE, we add it as a
  315. * client, if the DST IP doesn't exist in the TREE, we add it as a server
  316. */
  317. void
  318. add_tree_first_ipv4(const u_char *data)
  319. {
  320. tcpr_tree_t *newnode = NULL, *findnode;
  321. ipv4_hdr_t ip_hdr;
  322. assert(data);
  323. /*
  324. * first add/find the source IP/client
  325. */
  326. newnode = new_tree();
  327. /* prevent issues with byte alignment, must memcpy */
  328. memcpy(&ip_hdr, (data + TCPR_ETH_H), TCPR_IPV4_H);
  329. /* copy over the source ip, and values to gurantee this a client */
  330. newnode->family = AF_INET;
  331. newnode->u.ip = ip_hdr.ip_src.s_addr;
  332. newnode->type = DIR_CLIENT;
  333. newnode->client_cnt = 1000;
  334. findnode = RB_FIND(tcpr_data_tree_s, &treeroot, newnode);
  335. /* if we didn't find it, add it to the tree, else free it */
  336. if (findnode == NULL) {
  337. RB_INSERT(tcpr_data_tree_s, &treeroot, newnode);
  338. } else {
  339. safe_free(newnode);
  340. }
  341. /*
  342. * now add/find the destination IP/server
  343. */
  344. newnode = new_tree();
  345. memcpy(&ip_hdr, (data + TCPR_ETH_H), TCPR_IPV4_H);
  346. newnode->family = AF_INET;
  347. newnode->u.ip = ip_hdr.ip_dst.s_addr;
  348. newnode->type = DIR_SERVER;
  349. newnode->server_cnt = 1000;
  350. findnode = RB_FIND(tcpr_data_tree_s, &treeroot, newnode);
  351. if (findnode == NULL) {
  352. RB_INSERT(tcpr_data_tree_s, &treeroot, newnode);
  353. } else {
  354. safe_free(newnode);
  355. }
  356. }
  357. void
  358. add_tree_first_ipv6(const u_char *data)
  359. {
  360. tcpr_tree_t *newnode = NULL, *findnode;
  361. ipv6_hdr_t ip6_hdr;
  362. assert(data);
  363. /*
  364. * first add/find the source IP/client
  365. */
  366. newnode = new_tree();
  367. /* prevent issues with byte alignment, must memcpy */
  368. memcpy(&ip6_hdr, (data + TCPR_ETH_H), TCPR_IPV6_H);
  369. /* copy over the source ip, and values to gurantee this a client */
  370. newnode->family = AF_INET6;
  371. newnode->u.ip6 = ip6_hdr.ip_src;
  372. newnode->type = DIR_CLIENT;
  373. newnode->client_cnt = 1000;
  374. findnode = RB_FIND(tcpr_data_tree_s, &treeroot, newnode);
  375. /* if we didn't find it, add it to the tree, else free it */
  376. if (findnode == NULL) {
  377. RB_INSERT(tcpr_data_tree_s, &treeroot, newnode);
  378. } else {
  379. safe_free(newnode);
  380. }
  381. /*
  382. * now add/find the destination IP/server
  383. */
  384. newnode = new_tree();
  385. memcpy(&ip6_hdr, (data + TCPR_ETH_H), TCPR_IPV6_H);
  386. newnode->family = AF_INET6;
  387. newnode->u.ip6 = ip6_hdr.ip_dst;
  388. newnode->type = DIR_SERVER;
  389. newnode->server_cnt = 1000;
  390. findnode = RB_FIND(tcpr_data_tree_s, &treeroot, newnode);
  391. if (findnode == NULL) {
  392. RB_INSERT(tcpr_data_tree_s, &treeroot, newnode);
  393. } else {
  394. safe_free(newnode);
  395. }
  396. }
  397. static void
  398. add_tree_node(tcpr_tree_t *newnode)
  399. {
  400. tcpr_tree_t *node;
  401. /* try to find a simular entry in the tree */
  402. node = RB_FIND(tcpr_data_tree_s, &treeroot, newnode);
  403. dbgx(3, "%s", tree_printnode("add_tree", node));
  404. /* new entry required */
  405. if (node == NULL) {
  406. /* increment counters */
  407. if (newnode->type == DIR_SERVER) {
  408. newnode->server_cnt++;
  409. }
  410. else if (newnode->type == DIR_CLIENT) {
  411. newnode->client_cnt++;
  412. }
  413. /* insert it in */
  414. RB_INSERT(tcpr_data_tree_s, &treeroot, newnode);
  415. }
  416. else {
  417. /* we found something, so update it */
  418. dbgx(2, " node: %p\nnewnode: %p", node, newnode);
  419. dbgx(3, "%s", tree_printnode("update node", node));
  420. /* increment counter */
  421. if (newnode->type == DIR_SERVER) {
  422. node->server_cnt++;
  423. }
  424. else if (newnode->type == DIR_CLIENT) {
  425. /* temp debug code */
  426. node->client_cnt++;
  427. }
  428. /* didn't insert it, so free it */
  429. safe_free(newnode);
  430. }
  431. dbg(2, "------- START NEXT -------");
  432. dbgx(3, "%s", tree_print(&treeroot));
  433. }
  434. /**
  435. * adds an entry to the tree (phase 1 of auto mode). We add each host
  436. * to the tree if it doesn't yet exist. We go through and track:
  437. * - number of times each host acts as a client or server
  438. * - the way the host acted the first time we saw it (client or server)
  439. */
  440. void
  441. add_tree_ipv4(const unsigned long ip, const u_char * data)
  442. {
  443. tcpr_tree_t *newnode = NULL;
  444. assert(data);
  445. newnode = packet2tree(data);
  446. assert(ip == newnode->u.ip);
  447. if (newnode->type == DIR_UNKNOWN) {
  448. /* couldn't figure out if packet was client or server */
  449. dbgx(2, "%s (%lu) unknown client/server",
  450. get_addr2name4(newnode->u.ip, RESOLVE), newnode->u.ip);
  451. }
  452. add_tree_node(newnode);
  453. }
  454. void
  455. add_tree_ipv6(const struct tcpr_in6_addr * addr, const u_char * data)
  456. {
  457. tcpr_tree_t *newnode = NULL;
  458. assert(data);
  459. newnode = packet2tree(data);
  460. assert(ipv6_cmp(addr, &newnode->u.ip6) == 0);
  461. if (newnode->type == DIR_UNKNOWN) {
  462. /* couldn't figure out if packet was client or server */
  463. dbgx(2, "%s unknown client/server",
  464. get_addr2name6(&newnode->u.ip6, RESOLVE));
  465. }
  466. add_tree_node(newnode);
  467. }
  468. /**
  469. * calculates whether each node in the tree is a client, server, or unknown for each node in the tree
  470. */
  471. void
  472. tree_calculate(tcpr_data_tree_t *treeroot)
  473. {
  474. tcpr_tree_t *node;
  475. tcpprep_opt_t *options = tcpprep->options;
  476. dbg(1, "Running tree_calculate()");
  477. RB_FOREACH(node, tcpr_data_tree_s, treeroot) {
  478. dbgx(4, "Processing %s", get_addr2name4(node->u.ip, RESOLVE));
  479. if ((node->server_cnt > 0) || (node->client_cnt > 0)) {
  480. /* type based on: server >= (client*ratio) */
  481. if ((double)node->server_cnt >= (double)node->client_cnt * options->ratio) {
  482. node->type = DIR_SERVER;
  483. dbgx(3, "Setting %s to server",
  484. get_addr2name4(node->u.ip, RESOLVE));
  485. }
  486. else {
  487. node->type = DIR_CLIENT;
  488. dbgx(3, "Setting %s to client",
  489. get_addr2name4(node->u.ip, RESOLVE));
  490. }
  491. }
  492. else { /* IP had no client or server connections */
  493. node->type = DIR_UNKNOWN;
  494. dbgx(3, "Setting %s to unknown",
  495. get_addr2name4(node->u.ip, RESOLVE));
  496. }
  497. }
  498. }
  499. static int
  500. ipv6_cmp(const struct tcpr_in6_addr *a, const struct tcpr_in6_addr *b)
  501. {
  502. int i, k;
  503. for (i = 0; i < 4; i++) {
  504. if ((k = (a->tcpr_s6_addr32[i] - b->tcpr_s6_addr32[i]))) {
  505. return (k > 0) ? 1 : -1;
  506. }
  507. }
  508. return 0;
  509. }
  510. /**
  511. * tree_comp(), called by rbsearch compares two treees and returns:
  512. * 1 = first > second
  513. * -1 = first < second
  514. * 0 = first = second
  515. * based upon the ip address stored
  516. *
  517. */
  518. int
  519. tree_comp(tcpr_tree_t *t1, tcpr_tree_t *t2)
  520. {
  521. int ret;
  522. if (t1->family > t2->family) {
  523. dbgx(2, "family %d > %d", t1->family, t2->family);
  524. return 1;
  525. }
  526. if (t1->family < t2->family) {
  527. dbgx(2, "family %d < %d", t1->family, t2->family);
  528. return -1;
  529. }
  530. if (t1->family == AF_INET) {
  531. if (t1->u.ip > t2->u.ip) {
  532. dbgx(2, "%s > %s", get_addr2name4(t1->u.ip, RESOLVE),
  533. get_addr2name4(t2->u.ip, RESOLVE));
  534. return 1;
  535. }
  536. if (t1->u.ip < t2->u.ip) {
  537. dbgx(2, "%s < %s", get_addr2name4(t1->u.ip, RESOLVE),
  538. get_addr2name4(t2->u.ip, RESOLVE));
  539. return -1;
  540. }
  541. dbgx(2, "%s = %s", get_addr2name4(t1->u.ip, RESOLVE),
  542. get_addr2name4(t2->u.ip, RESOLVE));
  543. return 0;
  544. }
  545. if (t1->family == AF_INET6) {
  546. ret = ipv6_cmp(&t1->u.ip6, &t1->u.ip6);
  547. dbgx(2, "cmp(%s, %s) = %d", get_addr2name6(&t1->u.ip6, RESOLVE),
  548. get_addr2name6(&t2->u.ip6, RESOLVE), ret);
  549. return ret;
  550. }
  551. return 0;
  552. }
  553. /**
  554. * creates a new TREE * with reasonable defaults
  555. */
  556. static tcpr_tree_t *
  557. new_tree()
  558. {
  559. tcpr_tree_t *node;
  560. node = (tcpr_tree_t *)safe_malloc(sizeof(tcpr_tree_t));
  561. memset(node, '\0', sizeof(tcpr_tree_t));
  562. node->server_cnt = 0;
  563. node->client_cnt = 0;
  564. node->type = DIR_UNKNOWN;
  565. node->masklen = -1;
  566. node->u.ip = 0;
  567. return (node);
  568. }
  569. /**
  570. * returns a struct of TREE * from a packet header
  571. * and sets the type to be SERVER or CLIENT or UNKNOWN
  572. * if it's an undefined packet, we return -1 for the type
  573. * the u_char * data should be the data that is passed by pcap_dispatch()
  574. */
  575. tcpr_tree_t *
  576. packet2tree(const u_char * data)
  577. {
  578. tcpr_tree_t *node = NULL;
  579. eth_hdr_t *eth_hdr = NULL;
  580. ipv4_hdr_t ip_hdr;
  581. ipv6_hdr_t ip6_hdr;
  582. tcp_hdr_t tcp_hdr;
  583. udp_hdr_t udp_hdr;
  584. icmpv4_hdr_t icmp_hdr;
  585. dnsv4_hdr_t dnsv4_hdr;
  586. u_int16_t ether_type;
  587. u_char proto = 0;
  588. int hl = 0;
  589. #ifdef DEBUG
  590. char srcip[INET6_ADDRSTRLEN];
  591. #endif
  592. node = new_tree();
  593. eth_hdr = (eth_hdr_t *) (data);
  594. /* prevent issues with byte alignment, must memcpy */
  595. memcpy(&ether_type, (u_char*)eth_hdr + 12, 2);
  596. /* drop VLAN info if it exists before the IP info */
  597. if (ether_type == htons(ETHERTYPE_VLAN)) {
  598. dbg(4,"Processing as VLAN traffic...");
  599. /* prevent issues with byte alignment, must memcpy */
  600. memcpy(&ether_type, (u_char*)eth_hdr + 16, 2);
  601. hl += 4;
  602. }
  603. if (ether_type == htons(ETHERTYPE_IP)) {
  604. memcpy(&ip_hdr, (data + TCPR_ETH_H + hl), TCPR_IPV4_H);
  605. node->family = AF_INET;
  606. node->u.ip = ip_hdr.ip_src.s_addr;
  607. proto = ip_hdr.ip_p;
  608. hl += ip_hdr.ip_hl * 4;
  609. #ifdef DEBUG
  610. strlcpy(srcip, get_addr2name4(ip_hdr.ip_src.s_addr,
  611. RESOLVE), 16);
  612. #endif
  613. } else if (ether_type == htons(ETHERTYPE_IP6)) {
  614. memcpy(&ip6_hdr, (data + TCPR_ETH_H + hl), TCPR_IPV6_H);
  615. node->family = AF_INET6;
  616. node->u.ip6 = ip6_hdr.ip_src;
  617. proto = ip6_hdr.ip_nh;
  618. hl += TCPR_IPV6_H;
  619. #ifdef DEBUG
  620. strlcpy(srcip, get_addr2name6(&ip6_hdr.ip_src, RESOLVE), INET6_ADDRSTRLEN);
  621. #endif
  622. } else {
  623. dbgx(2,"Unrecognized ether_type (%x)", ether_type);
  624. }
  625. /* copy over the source mac */
  626. memset(&node->mac[0], 0, sizeof(node->mac));
  627. strncpy((char *)node->mac, (char *)eth_hdr->ether_shost,
  628. sizeof(node->mac) - 1);
  629. /*
  630. * TCP
  631. */
  632. if (proto == IPPROTO_TCP) {
  633. dbgx(3, "%s uses TCP... ", srcip);
  634. /* memcpy it over to prevent alignment issues */
  635. memcpy(&tcp_hdr, (data + TCPR_ETH_H + hl), TCPR_TCP_H);
  636. /* ftp-data is going to skew our results so we ignore it */
  637. if (tcp_hdr.th_sport == 20)
  638. return (node);
  639. /* set TREE->type based on TCP flags */
  640. if (tcp_hdr.th_flags == TH_SYN) {
  641. node->type = DIR_CLIENT;
  642. dbg(3, "is a client");
  643. }
  644. else if (tcp_hdr.th_flags == (TH_SYN | TH_ACK)) {
  645. node->type = DIR_SERVER;
  646. dbg(3, "is a server");
  647. }
  648. else {
  649. dbg(3, "is an unknown");
  650. }
  651. }
  652. /*
  653. * UDP
  654. */
  655. else if (proto == IPPROTO_UDP) {
  656. /* memcpy over to prevent alignment issues */
  657. memcpy(&udp_hdr, (data + TCPR_ETH_H + hl), TCPR_UDP_H);
  658. dbgx(3, "%s uses UDP... ", srcip);
  659. switch (ntohs(udp_hdr.uh_dport)) {
  660. case 0x0035: /* dns */
  661. /* prevent memory alignment issues */
  662. memcpy(&dnsv4_hdr,
  663. (data + TCPR_ETH_H + hl + TCPR_UDP_H), TCPR_DNS_H);
  664. if (dnsv4_hdr.flags & DNS_QUERY_FLAG) {
  665. /* bit set, response */
  666. node->type = DIR_SERVER;
  667. dbg(3, "is a dns server");
  668. }
  669. else {
  670. /* bit not set, query */
  671. node->type = DIR_CLIENT;
  672. dbg(3, "is a dns client");
  673. }
  674. return (node);
  675. break;
  676. default:
  677. break;
  678. }
  679. switch (ntohs(udp_hdr.uh_sport)) {
  680. case 0x0035: /* dns */
  681. /* prevent memory alignment issues */
  682. memcpy(&dnsv4_hdr,
  683. (data + TCPR_ETH_H + hl + TCPR_UDP_H),
  684. TCPR_DNS_H);
  685. if ((dnsv4_hdr.flags & 0x7FFFF) ^ DNS_QUERY_FLAG) {
  686. /* bit set, response */
  687. node->type = DIR_SERVER;
  688. dbg(3, "is a dns server");
  689. }
  690. else {
  691. /* bit not set, query */
  692. node->type = DIR_CLIENT;
  693. dbg(3, "is a dns client");
  694. }
  695. return (node);
  696. break;
  697. default:
  698. dbgx(3, "unknown UDP protocol: %hu->%hu", udp_hdr.uh_sport,
  699. udp_hdr.uh_dport);
  700. break;
  701. }
  702. }
  703. /*
  704. * ICMP
  705. */
  706. else if (proto == IPPROTO_ICMP) {
  707. /* prevent alignment issues */
  708. memcpy(&icmp_hdr, (data + TCPR_ETH_H + hl), TCPR_ICMPV4_H);
  709. dbgx(3, "%s uses ICMP... ", srcip);
  710. /*
  711. * if port unreachable, then source == server, dst == client
  712. */
  713. if ((icmp_hdr.icmp_type == ICMP_UNREACH) &&
  714. (icmp_hdr.icmp_code == ICMP_UNREACH_PORT)) {
  715. node->type = DIR_SERVER;
  716. dbg(3, "is a server with a closed port");
  717. }
  718. }
  719. return (node);
  720. }
  721. #ifdef DEBUG
  722. /**
  723. * prints out a node of the tree to stderr
  724. */
  725. static char *
  726. tree_printnode(const char *name, const tcpr_tree_t *node)
  727. {
  728. memset(&tree_print_buff, '\0', TREEPRINTBUFFLEN);
  729. if (node == NULL) {
  730. snprintf(tree_print_buff, TREEPRINTBUFFLEN, "%s node is null", name);
  731. }
  732. else {
  733. snprintf(tree_print_buff, TREEPRINTBUFFLEN,
  734. "-- %s: %p\nIP: %s\nMask: %d\nSrvr: %d\nClnt: %d\n",
  735. name, (void *)node, node->family == AF_INET ?
  736. get_addr2name4(node->u.ip, RESOLVE) :
  737. get_addr2name6(&node->u.ip6, RESOLVE),
  738. node->masklen, node->server_cnt, node->client_cnt);
  739. if (node->type == DIR_SERVER) {
  740. strlcat(tree_print_buff, "Type: Server\n--\n", TREEPRINTBUFFLEN);
  741. }
  742. else {
  743. strlcat(tree_print_buff, "Type: Client\n--", TREEPRINTBUFFLEN);
  744. }
  745. }
  746. return (tree_print_buff);
  747. }
  748. /**
  749. * prints out the entire tree
  750. */
  751. static char *
  752. tree_print(tcpr_data_tree_t *treeroot)
  753. {
  754. tcpr_tree_t *node = NULL;
  755. memset(&tree_print_buff, '\0', TREEPRINTBUFFLEN);
  756. RB_FOREACH(node, tcpr_data_tree_s, treeroot) {
  757. tree_printnode("my node", node);
  758. }
  759. return (tree_print_buff);
  760. }
  761. #endif /* DEBUG */