tree.c 27 KB

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