voteflags.c 24 KB

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