voteflags.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file voteflags.c
  7. * \brief Authority code for deciding the performance thresholds for flags,
  8. * and assigning flags to routers.
  9. **/
  10. #define VOTEFLAGS_PRIVATE
  11. #include "core/or/or.h"
  12. #include "feature/dirauth/voteflags.h"
  13. #include "app/config/config.h"
  14. #include "core/mainloop/mainloop.h"
  15. #include "core/or/policies.h"
  16. #include "feature/dirauth/bwauth.h"
  17. #include "feature/dirauth/reachability.h"
  18. #include "feature/hibernate/hibernate.h"
  19. #include "feature/nodelist/dirlist.h"
  20. #include "feature/nodelist/networkstatus.h"
  21. #include "feature/nodelist/nodelist.h"
  22. #include "feature/nodelist/routerlist.h"
  23. #include "feature/nodelist/routerset.h"
  24. #include "feature/relay/router.h"
  25. #include "feature/stats/rephist.h"
  26. #include "feature/nodelist/node_st.h"
  27. #include "feature/nodelist/routerinfo_st.h"
  28. #include "feature/nodelist/vote_routerstatus_st.h"
  29. #include "lib/container/order.h"
  30. /** If a router's uptime is at least this value, then it is always
  31. * considered stable, regardless of the rest of the network. This
  32. * way we resist attacks where an attacker doubles the size of the
  33. * network using allegedly high-uptime nodes, displacing all the
  34. * current guards. */
  35. #define UPTIME_TO_GUARANTEE_STABLE (3600*24*30)
  36. /** If a router's MTBF is at least this value, then it is always stable.
  37. * See above. (Corresponds to about 7 days for current decay rates.) */
  38. #define MTBF_TO_GUARANTEE_STABLE (60*60*24*5)
  39. /** Similarly, every node with at least this much weighted time known can be
  40. * considered familiar enough to be a guard. Corresponds to about 20 days for
  41. * current decay rates.
  42. */
  43. #define TIME_KNOWN_TO_GUARANTEE_FAMILIAR (8*24*60*60)
  44. /** Similarly, every node with sufficient WFU is around enough to be a guard.
  45. */
  46. #define WFU_TO_GUARANTEE_GUARD (0.98)
  47. /* Thresholds for server performance: set by
  48. * dirserv_compute_performance_thresholds, and used by
  49. * generate_v2_networkstatus */
  50. /** Any router with an uptime of at least this value is stable. */
  51. static uint32_t stable_uptime = 0; /* start at a safe value */
  52. /** Any router with an mtbf of at least this value is stable. */
  53. static double stable_mtbf = 0.0;
  54. /** If true, we have measured enough mtbf info to look at stable_mtbf rather
  55. * than stable_uptime. */
  56. static int enough_mtbf_info = 0;
  57. /** Any router with a weighted fractional uptime of at least this much might
  58. * be good as a guard. */
  59. static double guard_wfu = 0.0;
  60. /** Don't call a router a guard unless we've known about it for at least this
  61. * many seconds. */
  62. static long guard_tk = 0;
  63. /** Any router with a bandwidth at least this high is "Fast" */
  64. static uint32_t fast_bandwidth_kb = 0;
  65. /** If exits can be guards, then all guards must have a bandwidth this
  66. * high. */
  67. static uint32_t guard_bandwidth_including_exits_kb = 0;
  68. /** If exits can't be guards, then all guards must have a bandwidth this
  69. * high. */
  70. static uint32_t guard_bandwidth_excluding_exits_kb = 0;
  71. /** Helper: estimate the uptime of a router given its stated uptime and the
  72. * amount of time since it last stated its stated uptime. */
  73. static inline long
  74. real_uptime(const routerinfo_t *router, time_t now)
  75. {
  76. if (now < router->cache_info.published_on)
  77. return router->uptime;
  78. else
  79. return router->uptime + (now - router->cache_info.published_on);
  80. }
  81. /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
  82. * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
  83. * If <b>need_capacity</b> is non-zero, we require a minimum advertised
  84. * bandwidth.
  85. */
  86. static int
  87. dirserv_thinks_router_is_unreliable(time_t now,
  88. routerinfo_t *router,
  89. int need_uptime, int need_capacity)
  90. {
  91. if (need_uptime) {
  92. if (!enough_mtbf_info) {
  93. /* XXXX We should change the rule from
  94. * "use uptime if we don't have mtbf data" to "don't advertise Stable on
  95. * v3 if we don't have enough mtbf data." Or maybe not, since if we ever
  96. * hit a point where we need to reset a lot of authorities at once,
  97. * none of them would be in a position to declare Stable.
  98. */
  99. long uptime = real_uptime(router, now);
  100. if ((unsigned)uptime < stable_uptime &&
  101. (unsigned)uptime < UPTIME_TO_GUARANTEE_STABLE)
  102. return 1;
  103. } else {
  104. double mtbf =
  105. rep_hist_get_stability(router->cache_info.identity_digest, now);
  106. if (mtbf < stable_mtbf &&
  107. mtbf < MTBF_TO_GUARANTEE_STABLE)
  108. return 1;
  109. }
  110. }
  111. if (need_capacity) {
  112. uint32_t bw_kb = dirserv_get_credible_bandwidth_kb(router);
  113. if (bw_kb < fast_bandwidth_kb)
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. /** Return 1 if <b>ri</b>'s descriptor is "active" -- running, valid,
  119. * not hibernating, having observed bw greater 0, and not too old. Else
  120. * return 0.
  121. */
  122. static int
  123. router_is_active(const routerinfo_t *ri, const node_t *node, time_t now)
  124. {
  125. time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
  126. if (ri->cache_info.published_on < cutoff) {
  127. return 0;
  128. }
  129. if (!node->is_running || !node->is_valid || ri->is_hibernating) {
  130. return 0;
  131. }
  132. /* Only require bandwidth capacity in non-test networks, or
  133. * if TestingTorNetwork, and TestingMinExitFlagThreshold is non-zero */
  134. if (!ri->bandwidthcapacity) {
  135. if (get_options()->TestingTorNetwork) {
  136. if (get_options()->TestingMinExitFlagThreshold > 0) {
  137. /* If we're in a TestingTorNetwork, and TestingMinExitFlagThreshold is,
  138. * then require bandwidthcapacity */
  139. return 0;
  140. }
  141. } else {
  142. /* If we're not in a TestingTorNetwork, then require bandwidthcapacity */
  143. return 0;
  144. }
  145. }
  146. return 1;
  147. }
  148. /** Return true iff <b>router</b> should be assigned the "HSDir" flag.
  149. *
  150. * Right now this means it advertises support for it, it has a high uptime,
  151. * it's a directory cache, it has the Stable and Fast flags, and it's currently
  152. * considered Running.
  153. *
  154. * This function needs to be called after router-\>is_running has
  155. * been set.
  156. */
  157. static int
  158. dirserv_thinks_router_is_hs_dir(const routerinfo_t *router,
  159. const node_t *node, time_t now)
  160. {
  161. long uptime;
  162. /* If we haven't been running for at least
  163. * get_options()->MinUptimeHidServDirectoryV2 seconds, we can't
  164. * have accurate data telling us a relay has been up for at least
  165. * that long. We also want to allow a bit of slack: Reachability
  166. * tests aren't instant. If we haven't been running long enough,
  167. * trust the relay. */
  168. if (get_uptime() >
  169. get_options()->MinUptimeHidServDirectoryV2 * 1.1)
  170. uptime = MIN(rep_hist_get_uptime(router->cache_info.identity_digest, now),
  171. real_uptime(router, now));
  172. else
  173. uptime = real_uptime(router, now);
  174. return (router->wants_to_be_hs_dir &&
  175. router->supports_tunnelled_dir_requests &&
  176. node->is_stable && node->is_fast &&
  177. uptime >= get_options()->MinUptimeHidServDirectoryV2 &&
  178. router_is_active(router, node, now));
  179. }
  180. /** Don't consider routers with less bandwidth than this when computing
  181. * thresholds. */
  182. #define ABSOLUTE_MIN_BW_VALUE_TO_CONSIDER_KB 4
  183. /** Helper for dirserv_compute_performance_thresholds(): Decide whether to
  184. * include a router in our calculations, and return true iff we should; the
  185. * require_mbw parameter is passed in by
  186. * dirserv_compute_performance_thresholds() and controls whether we ever
  187. * count routers with only advertised bandwidths */
  188. static int
  189. router_counts_toward_thresholds(const node_t *node, time_t now,
  190. const digestmap_t *omit_as_sybil,
  191. int require_mbw)
  192. {
  193. /* Have measured bw? */
  194. int have_mbw =
  195. dirserv_has_measured_bw(node->identity);
  196. uint64_t min_bw_kb = ABSOLUTE_MIN_BW_VALUE_TO_CONSIDER_KB;
  197. const or_options_t *options = get_options();
  198. if (options->TestingTorNetwork) {
  199. min_bw_kb = (int64_t)options->TestingMinExitFlagThreshold / 1000;
  200. }
  201. return node->ri && router_is_active(node->ri, node, now) &&
  202. !digestmap_get(omit_as_sybil, node->identity) &&
  203. (dirserv_get_credible_bandwidth_kb(node->ri) >= min_bw_kb) &&
  204. (have_mbw || !require_mbw);
  205. }
  206. /** Look through the routerlist, the Mean Time Between Failure history, and
  207. * the Weighted Fractional Uptime history, and use them to set thresholds for
  208. * the Stable, Fast, and Guard flags. Update the fields stable_uptime,
  209. * stable_mtbf, enough_mtbf_info, guard_wfu, guard_tk, fast_bandwidth,
  210. * guard_bandwidth_including_exits, and guard_bandwidth_excluding_exits.
  211. *
  212. * Also, set the is_exit flag of each router appropriately. */
  213. void
  214. dirserv_compute_performance_thresholds(digestmap_t *omit_as_sybil)
  215. {
  216. int n_active, n_active_nonexit, n_familiar;
  217. uint32_t *uptimes, *bandwidths_kb, *bandwidths_excluding_exits_kb;
  218. long *tks;
  219. double *mtbfs, *wfus;
  220. smartlist_t *nodelist;
  221. time_t now = time(NULL);
  222. const or_options_t *options = get_options();
  223. /* Require mbw? */
  224. int require_mbw =
  225. (dirserv_get_last_n_measured_bws() >
  226. options->MinMeasuredBWsForAuthToIgnoreAdvertised) ? 1 : 0;
  227. /* initialize these all here, in case there are no routers */
  228. stable_uptime = 0;
  229. stable_mtbf = 0;
  230. fast_bandwidth_kb = 0;
  231. guard_bandwidth_including_exits_kb = 0;
  232. guard_bandwidth_excluding_exits_kb = 0;
  233. guard_tk = 0;
  234. guard_wfu = 0;
  235. nodelist_assert_ok();
  236. nodelist = nodelist_get_list();
  237. /* Initialize arrays that will hold values for each router. We'll
  238. * sort them and use that to compute thresholds. */
  239. n_active = n_active_nonexit = 0;
  240. /* Uptime for every active router. */
  241. uptimes = tor_calloc(smartlist_len(nodelist), sizeof(uint32_t));
  242. /* Bandwidth for every active router. */
  243. bandwidths_kb = tor_calloc(smartlist_len(nodelist), sizeof(uint32_t));
  244. /* Bandwidth for every active non-exit router. */
  245. bandwidths_excluding_exits_kb =
  246. tor_calloc(smartlist_len(nodelist), sizeof(uint32_t));
  247. /* Weighted mean time between failure for each active router. */
  248. mtbfs = tor_calloc(smartlist_len(nodelist), sizeof(double));
  249. /* Time-known for each active router. */
  250. tks = tor_calloc(smartlist_len(nodelist), sizeof(long));
  251. /* Weighted fractional uptime for each active router. */
  252. wfus = tor_calloc(smartlist_len(nodelist), sizeof(double));
  253. /* Now, fill in the arrays. */
  254. SMARTLIST_FOREACH_BEGIN(nodelist, node_t *, node) {
  255. if (options->BridgeAuthoritativeDir &&
  256. node->ri &&
  257. node->ri->purpose != ROUTER_PURPOSE_BRIDGE)
  258. continue;
  259. routerinfo_t *ri = node->ri;
  260. if (ri) {
  261. node->is_exit = (!router_exit_policy_rejects_all(ri) &&
  262. exit_policy_is_general_exit(ri->exit_policy));
  263. }
  264. if (router_counts_toward_thresholds(node, now, omit_as_sybil,
  265. require_mbw)) {
  266. const char *id = node->identity;
  267. uint32_t bw_kb;
  268. /* resolve spurious clang shallow analysis null pointer errors */
  269. tor_assert(ri);
  270. uptimes[n_active] = (uint32_t)real_uptime(ri, now);
  271. mtbfs[n_active] = rep_hist_get_stability(id, now);
  272. tks [n_active] = rep_hist_get_weighted_time_known(id, now);
  273. bandwidths_kb[n_active] = bw_kb = dirserv_get_credible_bandwidth_kb(ri);
  274. if (!node->is_exit || node->is_bad_exit) {
  275. bandwidths_excluding_exits_kb[n_active_nonexit] = bw_kb;
  276. ++n_active_nonexit;
  277. }
  278. ++n_active;
  279. }
  280. } SMARTLIST_FOREACH_END(node);
  281. /* Now, compute thresholds. */
  282. if (n_active) {
  283. /* The median uptime is stable. */
  284. stable_uptime = median_uint32(uptimes, n_active);
  285. /* The median mtbf is stable, if we have enough mtbf info */
  286. stable_mtbf = median_double(mtbfs, n_active);
  287. /* The 12.5th percentile bandwidth is fast. */
  288. fast_bandwidth_kb = find_nth_uint32(bandwidths_kb, n_active, n_active/8);
  289. /* (Now bandwidths is sorted.) */
  290. if (fast_bandwidth_kb < RELAY_REQUIRED_MIN_BANDWIDTH/(2 * 1000))
  291. fast_bandwidth_kb = bandwidths_kb[n_active/4];
  292. guard_bandwidth_including_exits_kb =
  293. third_quartile_uint32(bandwidths_kb, n_active);
  294. guard_tk = find_nth_long(tks, n_active, n_active/8);
  295. }
  296. if (guard_tk > TIME_KNOWN_TO_GUARANTEE_FAMILIAR)
  297. guard_tk = TIME_KNOWN_TO_GUARANTEE_FAMILIAR;
  298. {
  299. /* We can vote on a parameter for the minimum and maximum. */
  300. #define ABSOLUTE_MIN_VALUE_FOR_FAST_FLAG 4
  301. int32_t min_fast_kb, max_fast_kb, min_fast, max_fast;
  302. min_fast = networkstatus_get_param(NULL, "FastFlagMinThreshold",
  303. ABSOLUTE_MIN_VALUE_FOR_FAST_FLAG,
  304. ABSOLUTE_MIN_VALUE_FOR_FAST_FLAG,
  305. INT32_MAX);
  306. if (options->TestingTorNetwork) {
  307. min_fast = (int32_t)options->TestingMinFastFlagThreshold;
  308. }
  309. max_fast = networkstatus_get_param(NULL, "FastFlagMaxThreshold",
  310. INT32_MAX, min_fast, INT32_MAX);
  311. min_fast_kb = min_fast / 1000;
  312. max_fast_kb = max_fast / 1000;
  313. if (fast_bandwidth_kb < (uint32_t)min_fast_kb)
  314. fast_bandwidth_kb = min_fast_kb;
  315. if (fast_bandwidth_kb > (uint32_t)max_fast_kb)
  316. fast_bandwidth_kb = max_fast_kb;
  317. }
  318. /* Protect sufficiently fast nodes from being pushed out of the set
  319. * of Fast nodes. */
  320. if (options->AuthDirFastGuarantee &&
  321. fast_bandwidth_kb > options->AuthDirFastGuarantee/1000)
  322. fast_bandwidth_kb = (uint32_t)options->AuthDirFastGuarantee/1000;
  323. /* Now that we have a time-known that 7/8 routers are known longer than,
  324. * fill wfus with the wfu of every such "familiar" router. */
  325. n_familiar = 0;
  326. SMARTLIST_FOREACH_BEGIN(nodelist, node_t *, node) {
  327. if (router_counts_toward_thresholds(node, now,
  328. omit_as_sybil, require_mbw)) {
  329. routerinfo_t *ri = node->ri;
  330. const char *id = ri->cache_info.identity_digest;
  331. long tk = rep_hist_get_weighted_time_known(id, now);
  332. if (tk < guard_tk)
  333. continue;
  334. wfus[n_familiar++] = rep_hist_get_weighted_fractional_uptime(id, now);
  335. }
  336. } SMARTLIST_FOREACH_END(node);
  337. if (n_familiar)
  338. guard_wfu = median_double(wfus, n_familiar);
  339. if (guard_wfu > WFU_TO_GUARANTEE_GUARD)
  340. guard_wfu = WFU_TO_GUARANTEE_GUARD;
  341. enough_mtbf_info = rep_hist_have_measured_enough_stability();
  342. if (n_active_nonexit) {
  343. guard_bandwidth_excluding_exits_kb =
  344. find_nth_uint32(bandwidths_excluding_exits_kb,
  345. n_active_nonexit, n_active_nonexit*3/4);
  346. }
  347. log_info(LD_DIRSERV,
  348. "Cutoffs: For Stable, %lu sec uptime, %lu sec MTBF. "
  349. "For Fast: %lu kilobytes/sec. "
  350. "For Guard: WFU %.03f%%, time-known %lu sec, "
  351. "and bandwidth %lu or %lu kilobytes/sec. "
  352. "We%s have enough stability data.",
  353. (unsigned long)stable_uptime,
  354. (unsigned long)stable_mtbf,
  355. (unsigned long)fast_bandwidth_kb,
  356. guard_wfu*100,
  357. (unsigned long)guard_tk,
  358. (unsigned long)guard_bandwidth_including_exits_kb,
  359. (unsigned long)guard_bandwidth_excluding_exits_kb,
  360. enough_mtbf_info ? "" : " don't");
  361. tor_free(uptimes);
  362. tor_free(mtbfs);
  363. tor_free(bandwidths_kb);
  364. tor_free(bandwidths_excluding_exits_kb);
  365. tor_free(tks);
  366. tor_free(wfus);
  367. }
  368. /* Use dirserv_compute_performance_thresholds() to compute the thresholds
  369. * for the status flags, specifically for bridges.
  370. *
  371. * This is only called by a Bridge Authority from
  372. * networkstatus_getinfo_by_purpose().
  373. */
  374. void
  375. dirserv_compute_bridge_flag_thresholds(void)
  376. {
  377. digestmap_t *omit_as_sybil = digestmap_new();
  378. dirserv_compute_performance_thresholds(omit_as_sybil);
  379. digestmap_free(omit_as_sybil, NULL);
  380. }
  381. /** Give a statement of our current performance thresholds for inclusion
  382. * in a vote document. */
  383. char *
  384. dirserv_get_flag_thresholds_line(void)
  385. {
  386. char *result=NULL;
  387. const int measured_threshold =
  388. get_options()->MinMeasuredBWsForAuthToIgnoreAdvertised;
  389. const int enough_measured_bw =
  390. dirserv_get_last_n_measured_bws() > measured_threshold;
  391. tor_asprintf(&result,
  392. "stable-uptime=%lu stable-mtbf=%lu "
  393. "fast-speed=%lu "
  394. "guard-wfu=%.03f%% guard-tk=%lu "
  395. "guard-bw-inc-exits=%lu guard-bw-exc-exits=%lu "
  396. "enough-mtbf=%d ignoring-advertised-bws=%d",
  397. (unsigned long)stable_uptime,
  398. (unsigned long)stable_mtbf,
  399. (unsigned long)fast_bandwidth_kb*1000,
  400. guard_wfu*100,
  401. (unsigned long)guard_tk,
  402. (unsigned long)guard_bandwidth_including_exits_kb*1000,
  403. (unsigned long)guard_bandwidth_excluding_exits_kb*1000,
  404. enough_mtbf_info ? 1 : 0,
  405. enough_measured_bw ? 1 : 0);
  406. return result;
  407. }
  408. /* DOCDOC running_long_enough_to_decide_unreachable */
  409. int
  410. running_long_enough_to_decide_unreachable(void)
  411. {
  412. return time_of_process_start
  413. + get_options()->TestingAuthDirTimeToLearnReachability < approx_time();
  414. }
  415. /** Each server needs to have passed a reachability test no more
  416. * than this number of seconds ago, or it is listed as down in
  417. * the directory. */
  418. #define REACHABLE_TIMEOUT (45*60)
  419. /** If we tested a router and found it reachable _at least this long_ after it
  420. * declared itself hibernating, it is probably done hibernating and we just
  421. * missed a descriptor from it. */
  422. #define HIBERNATION_PUBLICATION_SKEW (60*60)
  423. /** Treat a router as alive if
  424. * - It's me, and I'm not hibernating.
  425. * or - We've found it reachable recently. */
  426. void
  427. dirserv_set_router_is_running(routerinfo_t *router, time_t now)
  428. {
  429. /*XXXX This function is a mess. Separate out the part that calculates
  430. whether it's reachable and the part that tells rephist that the router was
  431. unreachable.
  432. */
  433. int answer;
  434. const or_options_t *options = get_options();
  435. node_t *node = node_get_mutable_by_id(router->cache_info.identity_digest);
  436. tor_assert(node);
  437. if (router_is_me(router)) {
  438. /* We always know if we are shutting down or hibernating ourselves. */
  439. answer = ! we_are_hibernating();
  440. } else if (router->is_hibernating &&
  441. (router->cache_info.published_on +
  442. HIBERNATION_PUBLICATION_SKEW) > node->last_reachable) {
  443. /* A hibernating router is down unless we (somehow) had contact with it
  444. * since it declared itself to be hibernating. */
  445. answer = 0;
  446. } else if (options->AssumeReachable) {
  447. /* If AssumeReachable, everybody is up unless they say they are down! */
  448. answer = 1;
  449. } else {
  450. /* Otherwise, a router counts as up if we found all announced OR
  451. ports reachable in the last REACHABLE_TIMEOUT seconds.
  452. XXX prop186 For now there's always one IPv4 and at most one
  453. IPv6 OR port.
  454. If we're not on IPv6, don't consider reachability of potential
  455. IPv6 OR port since that'd kill all dual stack relays until a
  456. majority of the dir auths have IPv6 connectivity. */
  457. answer = (now < node->last_reachable + REACHABLE_TIMEOUT &&
  458. (options->AuthDirHasIPv6Connectivity != 1 ||
  459. tor_addr_is_null(&router->ipv6_addr) ||
  460. now < node->last_reachable6 + REACHABLE_TIMEOUT));
  461. }
  462. if (!answer && running_long_enough_to_decide_unreachable()) {
  463. /* Not considered reachable. tell rephist about that.
  464. Because we launch a reachability test for each router every
  465. REACHABILITY_TEST_CYCLE_PERIOD seconds, then the router has probably
  466. been down since at least that time after we last successfully reached
  467. it.
  468. XXX ipv6
  469. */
  470. time_t when = now;
  471. if (node->last_reachable &&
  472. node->last_reachable + REACHABILITY_TEST_CYCLE_PERIOD < now)
  473. when = node->last_reachable + REACHABILITY_TEST_CYCLE_PERIOD;
  474. rep_hist_note_router_unreachable(router->cache_info.identity_digest, when);
  475. }
  476. node->is_running = answer;
  477. }
  478. /** Extract status information from <b>ri</b> and from other authority
  479. * functions and store it in <b>rs</b>. <b>rs</b> is zeroed out before it is
  480. * set.
  481. *
  482. * We assume that ri-\>is_running has already been set, e.g. by
  483. * dirserv_set_router_is_running(ri, now);
  484. */
  485. void
  486. set_routerstatus_from_routerinfo(routerstatus_t *rs,
  487. node_t *node,
  488. routerinfo_t *ri,
  489. time_t now,
  490. int listbadexits)
  491. {
  492. const or_options_t *options = get_options();
  493. uint32_t routerbw_kb = dirserv_get_credible_bandwidth_kb(ri);
  494. memset(rs, 0, sizeof(routerstatus_t));
  495. rs->is_authority =
  496. router_digest_is_trusted_dir(ri->cache_info.identity_digest);
  497. /* Already set by compute_performance_thresholds. */
  498. rs->is_exit = node->is_exit;
  499. rs->is_stable = node->is_stable =
  500. !dirserv_thinks_router_is_unreliable(now, ri, 1, 0);
  501. rs->is_fast = node->is_fast =
  502. !dirserv_thinks_router_is_unreliable(now, ri, 0, 1);
  503. rs->is_flagged_running = node->is_running; /* computed above */
  504. rs->is_valid = node->is_valid;
  505. if (node->is_fast && node->is_stable &&
  506. ri->supports_tunnelled_dir_requests &&
  507. ((options->AuthDirGuardBWGuarantee &&
  508. routerbw_kb >= options->AuthDirGuardBWGuarantee/1000) ||
  509. routerbw_kb >= MIN(guard_bandwidth_including_exits_kb,
  510. guard_bandwidth_excluding_exits_kb))) {
  511. long tk = rep_hist_get_weighted_time_known(
  512. node->identity, now);
  513. double wfu = rep_hist_get_weighted_fractional_uptime(
  514. node->identity, now);
  515. rs->is_possible_guard = (wfu >= guard_wfu && tk >= guard_tk) ? 1 : 0;
  516. } else {
  517. rs->is_possible_guard = 0;
  518. }
  519. rs->is_bad_exit = listbadexits && node->is_bad_exit;
  520. rs->is_hs_dir = node->is_hs_dir =
  521. dirserv_thinks_router_is_hs_dir(ri, node, now);
  522. rs->is_named = rs->is_unnamed = 0;
  523. rs->published_on = ri->cache_info.published_on;
  524. memcpy(rs->identity_digest, node->identity, DIGEST_LEN);
  525. memcpy(rs->descriptor_digest, ri->cache_info.signed_descriptor_digest,
  526. DIGEST_LEN);
  527. rs->addr = ri->addr;
  528. strlcpy(rs->nickname, ri->nickname, sizeof(rs->nickname));
  529. rs->or_port = ri->or_port;
  530. rs->dir_port = ri->dir_port;
  531. rs->is_v2_dir = ri->supports_tunnelled_dir_requests;
  532. if (options->AuthDirHasIPv6Connectivity == 1 &&
  533. !tor_addr_is_null(&ri->ipv6_addr) &&
  534. node->last_reachable6 >= now - REACHABLE_TIMEOUT) {
  535. /* We're configured as having IPv6 connectivity. There's an IPv6
  536. OR port and it's reachable so copy it to the routerstatus. */
  537. tor_addr_copy(&rs->ipv6_addr, &ri->ipv6_addr);
  538. rs->ipv6_orport = ri->ipv6_orport;
  539. } else {
  540. tor_addr_make_null(&rs->ipv6_addr, AF_INET6);
  541. rs->ipv6_orport = 0;
  542. }
  543. if (options->TestingTorNetwork) {
  544. dirserv_set_routerstatus_testing(rs);
  545. }
  546. }
  547. /** Use TestingDirAuthVoteExit, TestingDirAuthVoteGuard, and
  548. * TestingDirAuthVoteHSDir to give out the Exit, Guard, and HSDir flags,
  549. * respectively. But don't set the corresponding node flags.
  550. * Should only be called if TestingTorNetwork is set. */
  551. STATIC void
  552. dirserv_set_routerstatus_testing(routerstatus_t *rs)
  553. {
  554. const or_options_t *options = get_options();
  555. tor_assert(options->TestingTorNetwork);
  556. if (routerset_contains_routerstatus(options->TestingDirAuthVoteExit,
  557. rs, 0)) {
  558. rs->is_exit = 1;
  559. } else if (options->TestingDirAuthVoteExitIsStrict) {
  560. rs->is_exit = 0;
  561. }
  562. if (routerset_contains_routerstatus(options->TestingDirAuthVoteGuard,
  563. rs, 0)) {
  564. rs->is_possible_guard = 1;
  565. } else if (options->TestingDirAuthVoteGuardIsStrict) {
  566. rs->is_possible_guard = 0;
  567. }
  568. if (routerset_contains_routerstatus(options->TestingDirAuthVoteHSDir,
  569. rs, 0)) {
  570. rs->is_hs_dir = 1;
  571. } else if (options->TestingDirAuthVoteHSDirIsStrict) {
  572. rs->is_hs_dir = 0;
  573. }
  574. }