nodelist.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file nodelist.c
  8. *
  9. * \brief Structures and functions for tracking what we know about the routers
  10. * on the Tor network, and correlating information from networkstatus,
  11. * routerinfo, and microdescs.
  12. *
  13. * The key structure here is node_t: that's the canonical way to refer
  14. * to a Tor relay that we might want to build a circuit through. Every
  15. * node_t has either a routerinfo_t, or a routerstatus_t from the current
  16. * networkstatus consensus. If it has a routerstatus_t, it will also
  17. * need to have a microdesc_t before you can use it for circuits.
  18. *
  19. * The nodelist_t is a global singleton that maps identities to node_t
  20. * objects. Access them with the node_get_*() functions. The nodelist_t
  21. * is maintained by calls throughout the codebase
  22. *
  23. * Generally, other code should not have to reach inside a node_t to
  24. * see what information it has. Instead, you should call one of the
  25. * many accessor functions that works on a generic node_t. If there
  26. * isn't one that does what you need, it's better to make such a function,
  27. * and then use it.
  28. *
  29. * For historical reasons, some of the functions that select a node_t
  30. * from the list of all usable node_t objects are in the routerlist.c
  31. * module, since they originally selected a routerinfo_t. (TODO: They
  32. * should move!)
  33. *
  34. * (TODO: Perhaps someday we should abstract the remaining ways of
  35. * talking about a relay to also be node_t instances. Those would be
  36. * routerstatus_t as used for directory requests, and dir_server_t as
  37. * used for authorities and fallback directories.)
  38. */
  39. #include "or.h"
  40. #include "address.h"
  41. #include "config.h"
  42. #include "control.h"
  43. #include "dirserv.h"
  44. #include "entrynodes.h"
  45. #include "geoip.h"
  46. #include "hs_common.h"
  47. #include "main.h"
  48. #include "microdesc.h"
  49. #include "networkstatus.h"
  50. #include "nodelist.h"
  51. #include "policies.h"
  52. #include "protover.h"
  53. #include "rendservice.h"
  54. #include "router.h"
  55. #include "routerlist.h"
  56. #include "routerparse.h"
  57. #include "routerset.h"
  58. #include "torcert.h"
  59. #include <string.h>
  60. static void nodelist_drop_node(node_t *node, int remove_from_ht);
  61. static void node_free(node_t *node);
  62. /** count_usable_descriptors counts descriptors with these flag(s)
  63. */
  64. typedef enum {
  65. /* All descriptors regardless of flags */
  66. USABLE_DESCRIPTOR_ALL = 0,
  67. /* Only descriptors with the Exit flag */
  68. USABLE_DESCRIPTOR_EXIT_ONLY = 1
  69. } usable_descriptor_t;
  70. static void count_usable_descriptors(int *num_present,
  71. int *num_usable,
  72. smartlist_t *descs_out,
  73. const networkstatus_t *consensus,
  74. time_t now,
  75. routerset_t *in_set,
  76. usable_descriptor_t exit_only);
  77. static void update_router_have_minimum_dir_info(void);
  78. static double get_frac_paths_needed_for_circs(const or_options_t *options,
  79. const networkstatus_t *ns);
  80. /** A nodelist_t holds a node_t object for every router we're "willing to use
  81. * for something". Specifically, it should hold a node_t for every node that
  82. * is currently in the routerlist, or currently in the consensus we're using.
  83. */
  84. typedef struct nodelist_t {
  85. /* A list of all the nodes. */
  86. smartlist_t *nodes;
  87. /* Hash table to map from node ID digest to node. */
  88. HT_HEAD(nodelist_map, node_t) nodes_by_id;
  89. } nodelist_t;
  90. static inline unsigned int
  91. node_id_hash(const node_t *node)
  92. {
  93. return (unsigned) siphash24g(node->identity, DIGEST_LEN);
  94. }
  95. static inline unsigned int
  96. node_id_eq(const node_t *node1, const node_t *node2)
  97. {
  98. return tor_memeq(node1->identity, node2->identity, DIGEST_LEN);
  99. }
  100. HT_PROTOTYPE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq)
  101. HT_GENERATE2(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq,
  102. 0.6, tor_reallocarray_, tor_free_)
  103. /** The global nodelist. */
  104. static nodelist_t *the_nodelist=NULL;
  105. /** Create an empty nodelist if we haven't done so already. */
  106. static void
  107. init_nodelist(void)
  108. {
  109. if (PREDICT_UNLIKELY(the_nodelist == NULL)) {
  110. the_nodelist = tor_malloc_zero(sizeof(nodelist_t));
  111. HT_INIT(nodelist_map, &the_nodelist->nodes_by_id);
  112. the_nodelist->nodes = smartlist_new();
  113. }
  114. }
  115. /** As node_get_by_id, but returns a non-const pointer */
  116. node_t *
  117. node_get_mutable_by_id(const char *identity_digest)
  118. {
  119. node_t search, *node;
  120. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  121. return NULL;
  122. memcpy(&search.identity, identity_digest, DIGEST_LEN);
  123. node = HT_FIND(nodelist_map, &the_nodelist->nodes_by_id, &search);
  124. return node;
  125. }
  126. /** Return the node_t whose identity is <b>identity_digest</b>, or NULL
  127. * if no such node exists. */
  128. MOCK_IMPL(const node_t *,
  129. node_get_by_id,(const char *identity_digest))
  130. {
  131. return node_get_mutable_by_id(identity_digest);
  132. }
  133. /** Internal: return the node_t whose identity_digest is
  134. * <b>identity_digest</b>. If none exists, create a new one, add it to the
  135. * nodelist, and return it.
  136. *
  137. * Requires that the nodelist be initialized.
  138. */
  139. static node_t *
  140. node_get_or_create(const char *identity_digest)
  141. {
  142. node_t *node;
  143. if ((node = node_get_mutable_by_id(identity_digest)))
  144. return node;
  145. node = tor_malloc_zero(sizeof(node_t));
  146. memcpy(node->identity, identity_digest, DIGEST_LEN);
  147. HT_INSERT(nodelist_map, &the_nodelist->nodes_by_id, node);
  148. smartlist_add(the_nodelist->nodes, node);
  149. node->nodelist_idx = smartlist_len(the_nodelist->nodes) - 1;
  150. node->hsdir_index = tor_malloc_zero(sizeof(hsdir_index_t));
  151. node->country = -1;
  152. return node;
  153. }
  154. /* For a given <b>node</b> for the consensus <b>ns</b>, set the hsdir index
  155. * for the node, both current and next if possible. This can only fails if the
  156. * node_t ed25519 identity key can't be found which would be a bug. */
  157. static void
  158. node_set_hsdir_index(node_t *node, const networkstatus_t *ns)
  159. {
  160. time_t now = time(NULL);
  161. const ed25519_public_key_t *node_identity_pk;
  162. uint8_t *next_hsdir_index_srv = NULL, *current_hsdir_index_srv = NULL;
  163. uint64_t next_time_period_num, current_time_period_num;
  164. tor_assert(node);
  165. tor_assert(ns);
  166. node_identity_pk = node_get_ed25519_id(node);
  167. if (node_identity_pk == NULL) {
  168. log_warn(LD_BUG, "ed25519 identity public key not found when "
  169. "trying to build the hsdir indexes for node %s",
  170. node_describe(node));
  171. goto done;
  172. }
  173. /* Get the current and next time period number, we might use them both. */
  174. current_time_period_num = hs_get_time_period_num(now);
  175. next_time_period_num = hs_get_next_time_period_num(now);
  176. /* If NOT in overlap mode, we only need to compute the current hsdir index
  177. * for the ongoing time period and thus the current SRV. If it can't be
  178. * found, the disaster one is returned. */
  179. current_hsdir_index_srv = hs_get_current_srv(current_time_period_num);
  180. if (hs_overlap_mode_is_active(ns, now)) {
  181. /* We are in overlap mode, this means that our consensus has just cycled
  182. * from current SRV to previous SRV so for the _next_ upcoming time
  183. * period, we have to use the current SRV and use the previous SRV for the
  184. * current time period. If the current or previous SRV can't be found, the
  185. * disaster one is returned. */
  186. next_hsdir_index_srv = hs_get_current_srv(next_time_period_num);
  187. /* The following can be confusing so again, in overlap mode, we use our
  188. * previous SRV for our _current_ hsdir index. */
  189. current_hsdir_index_srv = hs_get_previous_srv(current_time_period_num);
  190. }
  191. /* Build the current hsdir index. */
  192. hs_build_hsdir_index(node_identity_pk, current_hsdir_index_srv,
  193. current_time_period_num, node->hsdir_index->current);
  194. if (next_hsdir_index_srv) {
  195. /* Build the next hsdir index if we have a next SRV that we can use. */
  196. hs_build_hsdir_index(node_identity_pk, next_hsdir_index_srv,
  197. next_time_period_num, node->hsdir_index->next);
  198. }
  199. done:
  200. tor_free(current_hsdir_index_srv);
  201. tor_free(next_hsdir_index_srv);
  202. return;
  203. }
  204. /** Called when a node's address changes. */
  205. static void
  206. node_addrs_changed(node_t *node)
  207. {
  208. node->last_reachable = node->last_reachable6 = 0;
  209. node->country = -1;
  210. }
  211. /** Add <b>ri</b> to an appropriate node in the nodelist. If we replace an
  212. * old routerinfo, and <b>ri_old_out</b> is not NULL, set *<b>ri_old_out</b>
  213. * to the previous routerinfo.
  214. */
  215. node_t *
  216. nodelist_set_routerinfo(routerinfo_t *ri, routerinfo_t **ri_old_out)
  217. {
  218. node_t *node;
  219. const char *id_digest;
  220. int had_router = 0;
  221. tor_assert(ri);
  222. init_nodelist();
  223. id_digest = ri->cache_info.identity_digest;
  224. node = node_get_or_create(id_digest);
  225. if (node->ri) {
  226. if (!routers_have_same_or_addrs(node->ri, ri)) {
  227. node_addrs_changed(node);
  228. }
  229. had_router = 1;
  230. if (ri_old_out)
  231. *ri_old_out = node->ri;
  232. } else {
  233. if (ri_old_out)
  234. *ri_old_out = NULL;
  235. }
  236. node->ri = ri;
  237. if (node->country == -1)
  238. node_set_country(node);
  239. if (authdir_mode(get_options()) && !had_router) {
  240. const char *discard=NULL;
  241. uint32_t status = dirserv_router_get_status(ri, &discard, LOG_INFO);
  242. dirserv_set_node_flags_from_authoritative_status(node, status);
  243. }
  244. /* Setting the HSDir index requires the ed25519 identity key which can
  245. * only be found either in the ri or md. This is why this is called here.
  246. * Only nodes supporting HSDir=2 protocol version needs this index. */
  247. if (node->rs && node->rs->supports_v3_hsdir) {
  248. node_set_hsdir_index(node,
  249. networkstatus_get_latest_consensus());
  250. }
  251. return node;
  252. }
  253. /** Set the appropriate node_t to use <b>md</b> as its microdescriptor.
  254. *
  255. * Called when a new microdesc has arrived and the usable consensus flavor
  256. * is "microdesc".
  257. **/
  258. node_t *
  259. nodelist_add_microdesc(microdesc_t *md)
  260. {
  261. networkstatus_t *ns =
  262. networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC);
  263. const routerstatus_t *rs;
  264. node_t *node;
  265. if (ns == NULL)
  266. return NULL;
  267. init_nodelist();
  268. /* Microdescriptors don't carry an identity digest, so we need to figure
  269. * it out by looking up the routerstatus. */
  270. rs = router_get_consensus_status_by_descriptor_digest(ns, md->digest);
  271. if (rs == NULL)
  272. return NULL;
  273. node = node_get_mutable_by_id(rs->identity_digest);
  274. if (node) {
  275. if (node->md)
  276. node->md->held_by_nodes--;
  277. node->md = md;
  278. md->held_by_nodes++;
  279. /* Setting the HSDir index requires the ed25519 identity key which can
  280. * only be found either in the ri or md. This is why this is called here.
  281. * Only nodes supporting HSDir=2 protocol version needs this index. */
  282. if (rs->supports_v3_hsdir) {
  283. node_set_hsdir_index(node, ns);
  284. }
  285. }
  286. return node;
  287. }
  288. /** Tell the nodelist that the current usable consensus is <b>ns</b>.
  289. * This makes the nodelist change all of the routerstatus entries for
  290. * the nodes, drop nodes that no longer have enough info to get used,
  291. * and grab microdescriptors into nodes as appropriate.
  292. */
  293. void
  294. nodelist_set_consensus(networkstatus_t *ns)
  295. {
  296. const or_options_t *options = get_options();
  297. int authdir = authdir_mode_v3(options);
  298. init_nodelist();
  299. if (ns->flavor == FLAV_MICRODESC)
  300. (void) get_microdesc_cache(); /* Make sure it exists first. */
  301. SMARTLIST_FOREACH(the_nodelist->nodes, node_t *, node,
  302. node->rs = NULL);
  303. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  304. node_t *node = node_get_or_create(rs->identity_digest);
  305. node->rs = rs;
  306. if (ns->flavor == FLAV_MICRODESC) {
  307. if (node->md == NULL ||
  308. tor_memneq(node->md->digest,rs->descriptor_digest,DIGEST256_LEN)) {
  309. if (node->md)
  310. node->md->held_by_nodes--;
  311. node->md = microdesc_cache_lookup_by_digest256(NULL,
  312. rs->descriptor_digest);
  313. if (node->md)
  314. node->md->held_by_nodes++;
  315. }
  316. }
  317. if (rs->supports_v3_hsdir) {
  318. node_set_hsdir_index(node, ns);
  319. }
  320. node_set_country(node);
  321. /* If we're not an authdir, believe others. */
  322. if (!authdir) {
  323. node->is_valid = rs->is_valid;
  324. node->is_running = rs->is_flagged_running;
  325. node->is_fast = rs->is_fast;
  326. node->is_stable = rs->is_stable;
  327. node->is_possible_guard = rs->is_possible_guard;
  328. node->is_exit = rs->is_exit;
  329. node->is_bad_exit = rs->is_bad_exit;
  330. node->is_hs_dir = rs->is_hs_dir;
  331. node->ipv6_preferred = 0;
  332. if (fascist_firewall_prefer_ipv6_orport(options) &&
  333. (tor_addr_is_null(&rs->ipv6_addr) == 0 ||
  334. (node->md && tor_addr_is_null(&node->md->ipv6_addr) == 0)))
  335. node->ipv6_preferred = 1;
  336. }
  337. } SMARTLIST_FOREACH_END(rs);
  338. nodelist_purge();
  339. if (! authdir) {
  340. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  341. /* We have no routerstatus for this router. Clear flags so we can skip
  342. * it, maybe.*/
  343. if (!node->rs) {
  344. tor_assert(node->ri); /* if it had only an md, or nothing, purge
  345. * would have removed it. */
  346. if (node->ri->purpose == ROUTER_PURPOSE_GENERAL) {
  347. /* Clear all flags. */
  348. node->is_valid = node->is_running = node->is_hs_dir =
  349. node->is_fast = node->is_stable =
  350. node->is_possible_guard = node->is_exit =
  351. node->is_bad_exit = node->ipv6_preferred = 0;
  352. }
  353. }
  354. } SMARTLIST_FOREACH_END(node);
  355. }
  356. }
  357. /** Helper: return true iff a node has a usable amount of information*/
  358. static inline int
  359. node_is_usable(const node_t *node)
  360. {
  361. return (node->rs) || (node->ri);
  362. }
  363. /** Tell the nodelist that <b>md</b> is no longer a microdescriptor for the
  364. * node with <b>identity_digest</b>. */
  365. void
  366. nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md)
  367. {
  368. node_t *node = node_get_mutable_by_id(identity_digest);
  369. if (node && node->md == md) {
  370. node->md = NULL;
  371. md->held_by_nodes--;
  372. }
  373. }
  374. /** Tell the nodelist that <b>ri</b> is no longer in the routerlist. */
  375. void
  376. nodelist_remove_routerinfo(routerinfo_t *ri)
  377. {
  378. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  379. if (node && node->ri == ri) {
  380. node->ri = NULL;
  381. if (! node_is_usable(node)) {
  382. nodelist_drop_node(node, 1);
  383. node_free(node);
  384. }
  385. }
  386. }
  387. /** Remove <b>node</b> from the nodelist. (Asserts that it was there to begin
  388. * with.) */
  389. static void
  390. nodelist_drop_node(node_t *node, int remove_from_ht)
  391. {
  392. node_t *tmp;
  393. int idx;
  394. if (remove_from_ht) {
  395. tmp = HT_REMOVE(nodelist_map, &the_nodelist->nodes_by_id, node);
  396. tor_assert(tmp == node);
  397. }
  398. idx = node->nodelist_idx;
  399. tor_assert(idx >= 0);
  400. tor_assert(node == smartlist_get(the_nodelist->nodes, idx));
  401. smartlist_del(the_nodelist->nodes, idx);
  402. if (idx < smartlist_len(the_nodelist->nodes)) {
  403. tmp = smartlist_get(the_nodelist->nodes, idx);
  404. tmp->nodelist_idx = idx;
  405. }
  406. node->nodelist_idx = -1;
  407. }
  408. /** Return a newly allocated smartlist of the nodes that have <b>md</b> as
  409. * their microdescriptor. */
  410. smartlist_t *
  411. nodelist_find_nodes_with_microdesc(const microdesc_t *md)
  412. {
  413. smartlist_t *result = smartlist_new();
  414. if (the_nodelist == NULL)
  415. return result;
  416. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  417. if (node->md == md) {
  418. smartlist_add(result, node);
  419. }
  420. } SMARTLIST_FOREACH_END(node);
  421. return result;
  422. }
  423. /** Release storage held by <b>node</b> */
  424. static void
  425. node_free(node_t *node)
  426. {
  427. if (!node)
  428. return;
  429. if (node->md)
  430. node->md->held_by_nodes--;
  431. tor_assert(node->nodelist_idx == -1);
  432. tor_free(node->hsdir_index);
  433. tor_free(node);
  434. }
  435. /** Remove all entries from the nodelist that don't have enough info to be
  436. * usable for anything. */
  437. void
  438. nodelist_purge(void)
  439. {
  440. node_t **iter;
  441. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  442. return;
  443. /* Remove the non-usable nodes. */
  444. for (iter = HT_START(nodelist_map, &the_nodelist->nodes_by_id); iter; ) {
  445. node_t *node = *iter;
  446. if (node->md && !node->rs) {
  447. /* An md is only useful if there is an rs. */
  448. node->md->held_by_nodes--;
  449. node->md = NULL;
  450. }
  451. if (node_is_usable(node)) {
  452. iter = HT_NEXT(nodelist_map, &the_nodelist->nodes_by_id, iter);
  453. } else {
  454. iter = HT_NEXT_RMV(nodelist_map, &the_nodelist->nodes_by_id, iter);
  455. nodelist_drop_node(node, 0);
  456. node_free(node);
  457. }
  458. }
  459. nodelist_assert_ok();
  460. }
  461. /** Release all storage held by the nodelist. */
  462. void
  463. nodelist_free_all(void)
  464. {
  465. if (PREDICT_UNLIKELY(the_nodelist == NULL))
  466. return;
  467. HT_CLEAR(nodelist_map, &the_nodelist->nodes_by_id);
  468. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  469. node->nodelist_idx = -1;
  470. node_free(node);
  471. } SMARTLIST_FOREACH_END(node);
  472. smartlist_free(the_nodelist->nodes);
  473. tor_free(the_nodelist);
  474. }
  475. /** Check that the nodelist is internally consistent, and consistent with
  476. * the directory info it's derived from.
  477. */
  478. void
  479. nodelist_assert_ok(void)
  480. {
  481. routerlist_t *rl = router_get_routerlist();
  482. networkstatus_t *ns = networkstatus_get_latest_consensus();
  483. digestmap_t *dm;
  484. if (!the_nodelist)
  485. return;
  486. dm = digestmap_new();
  487. /* every routerinfo in rl->routers should be in the nodelist. */
  488. if (rl) {
  489. SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
  490. const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
  491. tor_assert(node && node->ri == ri);
  492. tor_assert(fast_memeq(ri->cache_info.identity_digest,
  493. node->identity, DIGEST_LEN));
  494. tor_assert(! digestmap_get(dm, node->identity));
  495. digestmap_set(dm, node->identity, (void*)node);
  496. } SMARTLIST_FOREACH_END(ri);
  497. }
  498. /* every routerstatus in ns should be in the nodelist */
  499. if (ns) {
  500. SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
  501. const node_t *node = node_get_by_id(rs->identity_digest);
  502. tor_assert(node && node->rs == rs);
  503. tor_assert(fast_memeq(rs->identity_digest, node->identity, DIGEST_LEN));
  504. digestmap_set(dm, node->identity, (void*)node);
  505. if (ns->flavor == FLAV_MICRODESC) {
  506. /* If it's a microdesc consensus, every entry that has a
  507. * microdescriptor should be in the nodelist.
  508. */
  509. microdesc_t *md =
  510. microdesc_cache_lookup_by_digest256(NULL, rs->descriptor_digest);
  511. tor_assert(md == node->md);
  512. if (md)
  513. tor_assert(md->held_by_nodes >= 1);
  514. }
  515. } SMARTLIST_FOREACH_END(rs);
  516. }
  517. /* The nodelist should have no other entries, and its entries should be
  518. * well-formed. */
  519. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  520. tor_assert(digestmap_get(dm, node->identity) != NULL);
  521. tor_assert(node_sl_idx == node->nodelist_idx);
  522. } SMARTLIST_FOREACH_END(node);
  523. tor_assert((long)smartlist_len(the_nodelist->nodes) ==
  524. (long)HT_SIZE(&the_nodelist->nodes_by_id));
  525. digestmap_free(dm, NULL);
  526. }
  527. /** Return a list of a node_t * for every node we know about. The caller
  528. * MUST NOT modify the list. (You can set and clear flags in the nodes if
  529. * you must, but you must not add or remove nodes.) */
  530. MOCK_IMPL(smartlist_t *,
  531. nodelist_get_list,(void))
  532. {
  533. init_nodelist();
  534. return the_nodelist->nodes;
  535. }
  536. /** Given a hex-encoded nickname of the format DIGEST, $DIGEST, $DIGEST=name,
  537. * or $DIGEST~name, return the node with the matching identity digest and
  538. * nickname (if any). Return NULL if no such node exists, or if <b>hex_id</b>
  539. * is not well-formed. */
  540. const node_t *
  541. node_get_by_hex_id(const char *hex_id)
  542. {
  543. char digest_buf[DIGEST_LEN];
  544. char nn_buf[MAX_NICKNAME_LEN+1];
  545. char nn_char='\0';
  546. if (hex_digest_nickname_decode(hex_id, digest_buf, &nn_char, nn_buf)==0) {
  547. const node_t *node = node_get_by_id(digest_buf);
  548. if (!node)
  549. return NULL;
  550. if (nn_char) {
  551. const char *real_name = node_get_nickname(node);
  552. if (!real_name || strcasecmp(real_name, nn_buf))
  553. return NULL;
  554. if (nn_char == '=') {
  555. const char *named_id =
  556. networkstatus_get_router_digest_by_nickname(nn_buf);
  557. if (!named_id || tor_memneq(named_id, digest_buf, DIGEST_LEN))
  558. return NULL;
  559. }
  560. }
  561. return node;
  562. }
  563. return NULL;
  564. }
  565. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  566. * the corresponding node_t, or NULL if none exists. Warn the user if
  567. * <b>warn_if_unnamed</b> is set, and they have specified a router by
  568. * nickname, but the Named flag isn't set for that router. */
  569. MOCK_IMPL(const node_t *,
  570. node_get_by_nickname,(const char *nickname, int warn_if_unnamed))
  571. {
  572. if (!the_nodelist)
  573. return NULL;
  574. /* Handle these cases: DIGEST, $DIGEST, $DIGEST=name, $DIGEST~name. */
  575. {
  576. const node_t *node;
  577. if ((node = node_get_by_hex_id(nickname)) != NULL)
  578. return node;
  579. }
  580. if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
  581. return NULL;
  582. /* Okay, so if we get here, the nickname is just a nickname. Is there
  583. * a binding for it in the consensus? */
  584. {
  585. const char *named_id =
  586. networkstatus_get_router_digest_by_nickname(nickname);
  587. if (named_id)
  588. return node_get_by_id(named_id);
  589. }
  590. /* Is it marked as owned-by-someone-else? */
  591. if (networkstatus_nickname_is_unnamed(nickname)) {
  592. log_info(LD_GENERAL, "The name %s is listed as Unnamed: there is some "
  593. "router that holds it, but not one listed in the current "
  594. "consensus.", escaped(nickname));
  595. return NULL;
  596. }
  597. /* Okay, so the name is not canonical for anybody. */
  598. {
  599. smartlist_t *matches = smartlist_new();
  600. const node_t *choice = NULL;
  601. SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
  602. if (!strcasecmp(node_get_nickname(node), nickname))
  603. smartlist_add(matches, node);
  604. } SMARTLIST_FOREACH_END(node);
  605. if (smartlist_len(matches)>1 && warn_if_unnamed) {
  606. int any_unwarned = 0;
  607. SMARTLIST_FOREACH_BEGIN(matches, node_t *, node) {
  608. if (!node->name_lookup_warned) {
  609. node->name_lookup_warned = 1;
  610. any_unwarned = 1;
  611. }
  612. } SMARTLIST_FOREACH_END(node);
  613. if (any_unwarned) {
  614. log_warn(LD_CONFIG, "There are multiple matches for the name %s, "
  615. "but none is listed as Named in the directory consensus. "
  616. "Choosing one arbitrarily.", nickname);
  617. }
  618. } else if (smartlist_len(matches)==1 && warn_if_unnamed) {
  619. char fp[HEX_DIGEST_LEN+1];
  620. node_t *node = smartlist_get(matches, 0);
  621. if (! node->name_lookup_warned) {
  622. base16_encode(fp, sizeof(fp), node->identity, DIGEST_LEN);
  623. log_warn(LD_CONFIG,
  624. "You specified a relay \"%s\" by name, but nicknames can be "
  625. "used by any relay, not just the one you meant. "
  626. "To make sure you get the same relay in the future, refer "
  627. "to it by key, as \"$%s\".", nickname, fp);
  628. node->name_lookup_warned = 1;
  629. }
  630. }
  631. if (smartlist_len(matches))
  632. choice = smartlist_get(matches, 0);
  633. smartlist_free(matches);
  634. return choice;
  635. }
  636. }
  637. /** Return the Ed25519 identity key for the provided node, or NULL if it
  638. * doesn't have one. */
  639. const ed25519_public_key_t *
  640. node_get_ed25519_id(const node_t *node)
  641. {
  642. if (node->ri) {
  643. if (node->ri->cache_info.signing_key_cert) {
  644. const ed25519_public_key_t *pk =
  645. &node->ri->cache_info.signing_key_cert->signing_key;
  646. if (BUG(ed25519_public_key_is_zero(pk)))
  647. goto try_the_md;
  648. return pk;
  649. }
  650. }
  651. try_the_md:
  652. if (node->md) {
  653. if (node->md->ed25519_identity_pkey) {
  654. return node->md->ed25519_identity_pkey;
  655. }
  656. }
  657. return NULL;
  658. }
  659. /** Return true iff this node's Ed25519 identity matches <b>id</b>.
  660. * (An absent Ed25519 identity matches NULL or zero.) */
  661. int
  662. node_ed25519_id_matches(const node_t *node, const ed25519_public_key_t *id)
  663. {
  664. const ed25519_public_key_t *node_id = node_get_ed25519_id(node);
  665. if (node_id == NULL || ed25519_public_key_is_zero(node_id)) {
  666. return id == NULL || ed25519_public_key_is_zero(id);
  667. } else {
  668. return id && ed25519_pubkey_eq(node_id, id);
  669. }
  670. }
  671. /** Return true iff <b>node</b> supports authenticating itself
  672. * by ed25519 ID during the link handshake in a way that we can understand
  673. * when we probe it. */
  674. int
  675. node_supports_ed25519_link_authentication(const node_t *node)
  676. {
  677. /* XXXX Oh hm. What if some day in the future there are link handshake
  678. * versions that aren't 3 but which are ed25519 */
  679. if (! node_get_ed25519_id(node))
  680. return 0;
  681. if (node->ri) {
  682. const char *protos = node->ri->protocol_list;
  683. if (protos == NULL)
  684. return 0;
  685. return protocol_list_supports_protocol(protos, PRT_LINKAUTH, 3);
  686. }
  687. if (node->rs) {
  688. return node->rs->supports_ed25519_link_handshake;
  689. }
  690. tor_assert_nonfatal_unreached_once();
  691. return 0;
  692. }
  693. /** Return true iff <b>node</b> supports the hidden service directory version
  694. * 3 protocol (proposal 224). */
  695. int
  696. node_supports_v3_hsdir(const node_t *node)
  697. {
  698. tor_assert(node);
  699. if (node->rs) {
  700. return node->rs->supports_v3_hsdir;
  701. }
  702. if (node->ri) {
  703. if (node->ri->protocol_list == NULL) {
  704. return 0;
  705. }
  706. if (node->ri->platform) {
  707. /* Bug #22447 forces us to filter on this version. */
  708. return tor_version_as_new_as(node->ri->platform, "0.3.0.8");
  709. }
  710. return protocol_list_supports_protocol(node->ri->protocol_list,
  711. PRT_HSDIR, PROTOVER_HSDIR_V3);
  712. }
  713. tor_assert_nonfatal_unreached_once();
  714. return 0;
  715. }
  716. /** Return true iff <b>node</b> supports ed25519 authentication as an hidden
  717. * service introduction point.*/
  718. int
  719. node_supports_ed25519_hs_intro(const node_t *node)
  720. {
  721. tor_assert(node);
  722. if (node->rs) {
  723. return node->rs->supports_ed25519_hs_intro;
  724. }
  725. if (node->ri) {
  726. if (node->ri->protocol_list == NULL) {
  727. return 0;
  728. }
  729. return protocol_list_supports_protocol(node->ri->protocol_list,
  730. PRT_HSINTRO, PROTOVER_HS_INTRO_V3);
  731. }
  732. tor_assert_nonfatal_unreached_once();
  733. return 0;
  734. }
  735. /** Return the RSA ID key's SHA1 digest for the provided node. */
  736. const uint8_t *
  737. node_get_rsa_id_digest(const node_t *node)
  738. {
  739. tor_assert(node);
  740. return (const uint8_t*)node->identity;
  741. }
  742. /** Return the nickname of <b>node</b>, or NULL if we can't find one. */
  743. const char *
  744. node_get_nickname(const node_t *node)
  745. {
  746. tor_assert(node);
  747. if (node->rs)
  748. return node->rs->nickname;
  749. else if (node->ri)
  750. return node->ri->nickname;
  751. else
  752. return NULL;
  753. }
  754. /** Return true iff the nickname of <b>node</b> is canonical, based on the
  755. * latest consensus. */
  756. int
  757. node_is_named(const node_t *node)
  758. {
  759. const char *named_id;
  760. const char *nickname = node_get_nickname(node);
  761. if (!nickname)
  762. return 0;
  763. named_id = networkstatus_get_router_digest_by_nickname(nickname);
  764. if (!named_id)
  765. return 0;
  766. return tor_memeq(named_id, node->identity, DIGEST_LEN);
  767. }
  768. /** Return true iff <b>node</b> appears to be a directory authority or
  769. * directory cache */
  770. int
  771. node_is_dir(const node_t *node)
  772. {
  773. if (node->rs) {
  774. routerstatus_t * rs = node->rs;
  775. /* This is true if supports_tunnelled_dir_requests is true which
  776. * indicates that we support directory request tunnelled or through the
  777. * DirPort. */
  778. return rs->is_v2_dir;
  779. } else if (node->ri) {
  780. routerinfo_t * ri = node->ri;
  781. /* Both tunnelled request is supported or DirPort is set. */
  782. return ri->supports_tunnelled_dir_requests;
  783. } else {
  784. return 0;
  785. }
  786. }
  787. /** Return true iff <b>node</b> has either kind of usable descriptor -- that
  788. * is, a routerdescriptor or a microdescriptor. */
  789. int
  790. node_has_descriptor(const node_t *node)
  791. {
  792. return (node->ri ||
  793. (node->rs && node->md));
  794. }
  795. /** Return the router_purpose of <b>node</b>. */
  796. int
  797. node_get_purpose(const node_t *node)
  798. {
  799. if (node->ri)
  800. return node->ri->purpose;
  801. else
  802. return ROUTER_PURPOSE_GENERAL;
  803. }
  804. /** Compute the verbose ("extended") nickname of <b>node</b> and store it
  805. * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
  806. * <b>verbose_name_out</b> */
  807. void
  808. node_get_verbose_nickname(const node_t *node,
  809. char *verbose_name_out)
  810. {
  811. const char *nickname = node_get_nickname(node);
  812. int is_named = node_is_named(node);
  813. verbose_name_out[0] = '$';
  814. base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, node->identity,
  815. DIGEST_LEN);
  816. if (!nickname)
  817. return;
  818. verbose_name_out[1+HEX_DIGEST_LEN] = is_named ? '=' : '~';
  819. strlcpy(verbose_name_out+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
  820. }
  821. /** Compute the verbose ("extended") nickname of node with
  822. * given <b>id_digest</b> and store it into the MAX_VERBOSE_NICKNAME_LEN+1
  823. * character buffer at <b>verbose_name_out</b>
  824. *
  825. * If node_get_by_id() returns NULL, base 16 encoding of
  826. * <b>id_digest</b> is returned instead. */
  827. void
  828. node_get_verbose_nickname_by_id(const char *id_digest,
  829. char *verbose_name_out)
  830. {
  831. const node_t *node = node_get_by_id(id_digest);
  832. if (!node) {
  833. verbose_name_out[0] = '$';
  834. base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN);
  835. } else {
  836. node_get_verbose_nickname(node, verbose_name_out);
  837. }
  838. }
  839. /** Return true iff it seems that <b>node</b> allows circuits to exit
  840. * through it directlry from the client. */
  841. int
  842. node_allows_single_hop_exits(const node_t *node)
  843. {
  844. if (node && node->ri)
  845. return node->ri->allow_single_hop_exits;
  846. else
  847. return 0;
  848. }
  849. /** Return true iff it seems that <b>node</b> has an exit policy that doesn't
  850. * actually permit anything to exit, or we don't know its exit policy */
  851. int
  852. node_exit_policy_rejects_all(const node_t *node)
  853. {
  854. if (node->rejects_all)
  855. return 1;
  856. if (node->ri)
  857. return node->ri->policy_is_reject_star;
  858. else if (node->md)
  859. return node->md->exit_policy == NULL ||
  860. short_policy_is_reject_star(node->md->exit_policy);
  861. else
  862. return 1;
  863. }
  864. /** Return true iff the exit policy for <b>node</b> is such that we can treat
  865. * rejecting an address of type <b>family</b> unexpectedly as a sign of that
  866. * node's failure. */
  867. int
  868. node_exit_policy_is_exact(const node_t *node, sa_family_t family)
  869. {
  870. if (family == AF_UNSPEC) {
  871. return 1; /* Rejecting an address but not telling us what address
  872. * is a bad sign. */
  873. } else if (family == AF_INET) {
  874. return node->ri != NULL;
  875. } else if (family == AF_INET6) {
  876. return 0;
  877. }
  878. tor_fragile_assert();
  879. return 1;
  880. }
  881. /* Check if the "addr" and port_field fields from r are a valid non-listening
  882. * address/port. If so, set valid to true and add a newly allocated
  883. * tor_addr_port_t containing "addr" and port_field to sl.
  884. * "addr" is an IPv4 host-order address and port_field is a uint16_t.
  885. * r is typically a routerinfo_t or routerstatus_t.
  886. */
  887. #define SL_ADD_NEW_IPV4_AP(r, port_field, sl, valid) \
  888. STMT_BEGIN \
  889. if (tor_addr_port_is_valid_ipv4h((r)->addr, (r)->port_field, 0)) { \
  890. valid = 1; \
  891. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t)); \
  892. tor_addr_from_ipv4h(&ap->addr, (r)->addr); \
  893. ap->port = (r)->port_field; \
  894. smartlist_add((sl), ap); \
  895. } \
  896. STMT_END
  897. /* Check if the "addr" and port_field fields from r are a valid non-listening
  898. * address/port. If so, set valid to true and add a newly allocated
  899. * tor_addr_port_t containing "addr" and port_field to sl.
  900. * "addr" is a tor_addr_t and port_field is a uint16_t.
  901. * r is typically a routerinfo_t or routerstatus_t.
  902. */
  903. #define SL_ADD_NEW_IPV6_AP(r, port_field, sl, valid) \
  904. STMT_BEGIN \
  905. if (tor_addr_port_is_valid(&(r)->ipv6_addr, (r)->port_field, 0)) { \
  906. valid = 1; \
  907. tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t)); \
  908. tor_addr_copy(&ap->addr, &(r)->ipv6_addr); \
  909. ap->port = (r)->port_field; \
  910. smartlist_add((sl), ap); \
  911. } \
  912. STMT_END
  913. /** Return list of tor_addr_port_t with all OR ports (in the sense IP
  914. * addr + TCP port) for <b>node</b>. Caller must free all elements
  915. * using tor_free() and free the list using smartlist_free().
  916. *
  917. * XXX this is potentially a memory fragmentation hog -- if on
  918. * critical path consider the option of having the caller allocate the
  919. * memory
  920. */
  921. smartlist_t *
  922. node_get_all_orports(const node_t *node)
  923. {
  924. smartlist_t *sl = smartlist_new();
  925. int valid = 0;
  926. /* Find a valid IPv4 address and port */
  927. if (node->ri != NULL) {
  928. SL_ADD_NEW_IPV4_AP(node->ri, or_port, sl, valid);
  929. }
  930. /* If we didn't find a valid address/port in the ri, try the rs */
  931. if (!valid && node->rs != NULL) {
  932. SL_ADD_NEW_IPV4_AP(node->rs, or_port, sl, valid);
  933. }
  934. /* Find a valid IPv6 address and port */
  935. valid = 0;
  936. if (node->ri != NULL) {
  937. SL_ADD_NEW_IPV6_AP(node->ri, ipv6_orport, sl, valid);
  938. }
  939. if (!valid && node->rs != NULL) {
  940. SL_ADD_NEW_IPV6_AP(node->rs, ipv6_orport, sl, valid);
  941. }
  942. if (!valid && node->md != NULL) {
  943. SL_ADD_NEW_IPV6_AP(node->md, ipv6_orport, sl, valid);
  944. }
  945. return sl;
  946. }
  947. #undef SL_ADD_NEW_IPV4_AP
  948. #undef SL_ADD_NEW_IPV6_AP
  949. /** Wrapper around node_get_prim_orport for backward
  950. compatibility. */
  951. void
  952. node_get_addr(const node_t *node, tor_addr_t *addr_out)
  953. {
  954. tor_addr_port_t ap;
  955. node_get_prim_orport(node, &ap);
  956. tor_addr_copy(addr_out, &ap.addr);
  957. }
  958. /** Return the host-order IPv4 address for <b>node</b>, or 0 if it doesn't
  959. * seem to have one. */
  960. uint32_t
  961. node_get_prim_addr_ipv4h(const node_t *node)
  962. {
  963. /* Don't check the ORPort or DirPort, as this function isn't port-specific,
  964. * and the node might have a valid IPv4 address, yet have a zero
  965. * ORPort or DirPort.
  966. */
  967. if (node->ri && tor_addr_is_valid_ipv4h(node->ri->addr, 0)) {
  968. return node->ri->addr;
  969. } else if (node->rs && tor_addr_is_valid_ipv4h(node->rs->addr, 0)) {
  970. return node->rs->addr;
  971. }
  972. return 0;
  973. }
  974. /** Copy a string representation of an IP address for <b>node</b> into
  975. * the <b>len</b>-byte buffer at <b>buf</b>. */
  976. void
  977. node_get_address_string(const node_t *node, char *buf, size_t len)
  978. {
  979. uint32_t ipv4_addr = node_get_prim_addr_ipv4h(node);
  980. if (tor_addr_is_valid_ipv4h(ipv4_addr, 0)) {
  981. tor_addr_t addr;
  982. tor_addr_from_ipv4h(&addr, ipv4_addr);
  983. tor_addr_to_str(buf, &addr, len, 0);
  984. } else if (len > 0) {
  985. buf[0] = '\0';
  986. }
  987. }
  988. /** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
  989. * one. */
  990. long
  991. node_get_declared_uptime(const node_t *node)
  992. {
  993. if (node->ri)
  994. return node->ri->uptime;
  995. else
  996. return -1;
  997. }
  998. /** Return <b>node</b>'s platform string, or NULL if we don't know it. */
  999. const char *
  1000. node_get_platform(const node_t *node)
  1001. {
  1002. /* If we wanted, we could record the version in the routerstatus_t, since
  1003. * the consensus lists it. We don't, though, so this function just won't
  1004. * work with microdescriptors. */
  1005. if (node->ri)
  1006. return node->ri->platform;
  1007. else
  1008. return NULL;
  1009. }
  1010. /** Return true iff <b>node</b> is one representing this router. */
  1011. int
  1012. node_is_me(const node_t *node)
  1013. {
  1014. return router_digest_is_me(node->identity);
  1015. }
  1016. /** Return <b>node</b> declared family (as a list of names), or NULL if
  1017. * the node didn't declare a family. */
  1018. const smartlist_t *
  1019. node_get_declared_family(const node_t *node)
  1020. {
  1021. if (node->ri && node->ri->declared_family)
  1022. return node->ri->declared_family;
  1023. else if (node->md && node->md->family)
  1024. return node->md->family;
  1025. else
  1026. return NULL;
  1027. }
  1028. /* Does this node have a valid IPv6 address?
  1029. * Prefer node_has_ipv6_orport() or node_has_ipv6_dirport() for
  1030. * checking specific ports. */
  1031. int
  1032. node_has_ipv6_addr(const node_t *node)
  1033. {
  1034. /* Don't check the ORPort or DirPort, as this function isn't port-specific,
  1035. * and the node might have a valid IPv6 address, yet have a zero
  1036. * ORPort or DirPort.
  1037. */
  1038. if (node->ri && tor_addr_is_valid(&node->ri->ipv6_addr, 0))
  1039. return 1;
  1040. if (node->rs && tor_addr_is_valid(&node->rs->ipv6_addr, 0))
  1041. return 1;
  1042. if (node->md && tor_addr_is_valid(&node->md->ipv6_addr, 0))
  1043. return 1;
  1044. return 0;
  1045. }
  1046. /* Does this node have a valid IPv6 ORPort? */
  1047. int
  1048. node_has_ipv6_orport(const node_t *node)
  1049. {
  1050. tor_addr_port_t ipv6_orport;
  1051. node_get_pref_ipv6_orport(node, &ipv6_orport);
  1052. return tor_addr_port_is_valid_ap(&ipv6_orport, 0);
  1053. }
  1054. /* Does this node have a valid IPv6 DirPort? */
  1055. int
  1056. node_has_ipv6_dirport(const node_t *node)
  1057. {
  1058. tor_addr_port_t ipv6_dirport;
  1059. node_get_pref_ipv6_dirport(node, &ipv6_dirport);
  1060. return tor_addr_port_is_valid_ap(&ipv6_dirport, 0);
  1061. }
  1062. /** Return 1 if we prefer the IPv6 address and OR TCP port of
  1063. * <b>node</b>, else 0.
  1064. *
  1065. * We prefer the IPv6 address if the router has an IPv6 address,
  1066. * and we can use IPv6 addresses, and:
  1067. * i) the node_t says that it prefers IPv6
  1068. * or
  1069. * ii) the router has no IPv4 OR address.
  1070. *
  1071. * If you don't have a node, consider looking it up.
  1072. * If there is no node, use fascist_firewall_prefer_ipv6_orport().
  1073. */
  1074. int
  1075. node_ipv6_or_preferred(const node_t *node)
  1076. {
  1077. const or_options_t *options = get_options();
  1078. tor_addr_port_t ipv4_addr;
  1079. node_assert_ok(node);
  1080. /* XX/teor - node->ipv6_preferred is set from
  1081. * fascist_firewall_prefer_ipv6_orport() each time the consensus is loaded.
  1082. */
  1083. if (!fascist_firewall_use_ipv6(options)) {
  1084. return 0;
  1085. } else if (node->ipv6_preferred || node_get_prim_orport(node, &ipv4_addr)) {
  1086. return node_has_ipv6_orport(node);
  1087. }
  1088. return 0;
  1089. }
  1090. #define RETURN_IPV4_AP(r, port_field, ap_out) \
  1091. STMT_BEGIN \
  1092. if (r && tor_addr_port_is_valid_ipv4h((r)->addr, (r)->port_field, 0)) { \
  1093. tor_addr_from_ipv4h(&(ap_out)->addr, (r)->addr); \
  1094. (ap_out)->port = (r)->port_field; \
  1095. return 0; \
  1096. } \
  1097. STMT_END
  1098. /** Copy the primary (IPv4) OR port (IP address and TCP port) for
  1099. * <b>node</b> into *<b>ap_out</b>. Return 0 if a valid address and
  1100. * port was copied, else return non-zero.*/
  1101. int
  1102. node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
  1103. {
  1104. node_assert_ok(node);
  1105. tor_assert(ap_out);
  1106. /* Check ri first, because rewrite_node_address_for_bridge() updates
  1107. * node->ri with the configured bridge address. */
  1108. RETURN_IPV4_AP(node->ri, or_port, ap_out);
  1109. RETURN_IPV4_AP(node->rs, or_port, ap_out);
  1110. /* Microdescriptors only have an IPv6 address */
  1111. return -1;
  1112. }
  1113. /** Copy the preferred OR port (IP address and TCP port) for
  1114. * <b>node</b> into *<b>ap_out</b>. */
  1115. void
  1116. node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out)
  1117. {
  1118. tor_assert(ap_out);
  1119. if (node_ipv6_or_preferred(node)) {
  1120. node_get_pref_ipv6_orport(node, ap_out);
  1121. } else {
  1122. /* the primary ORPort is always on IPv4 */
  1123. node_get_prim_orport(node, ap_out);
  1124. }
  1125. }
  1126. /** Copy the preferred IPv6 OR port (IP address and TCP port) for
  1127. * <b>node</b> into *<b>ap_out</b>. */
  1128. void
  1129. node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
  1130. {
  1131. node_assert_ok(node);
  1132. tor_assert(ap_out);
  1133. /* Check ri first, because rewrite_node_address_for_bridge() updates
  1134. * node->ri with the configured bridge address.
  1135. * Prefer rs over md for consistency with the fascist_firewall_* functions.
  1136. * Check if the address or port are valid, and try another alternative
  1137. * if they are not. */
  1138. if (node->ri && tor_addr_port_is_valid(&node->ri->ipv6_addr,
  1139. node->ri->ipv6_orport, 0)) {
  1140. tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
  1141. ap_out->port = node->ri->ipv6_orport;
  1142. } else if (node->rs && tor_addr_port_is_valid(&node->rs->ipv6_addr,
  1143. node->rs->ipv6_orport, 0)) {
  1144. tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
  1145. ap_out->port = node->rs->ipv6_orport;
  1146. } else if (node->md && tor_addr_port_is_valid(&node->md->ipv6_addr,
  1147. node->md->ipv6_orport, 0)) {
  1148. tor_addr_copy(&ap_out->addr, &node->md->ipv6_addr);
  1149. ap_out->port = node->md->ipv6_orport;
  1150. } else {
  1151. tor_addr_make_null(&ap_out->addr, AF_INET6);
  1152. ap_out->port = 0;
  1153. }
  1154. }
  1155. /** Return 1 if we prefer the IPv6 address and Dir TCP port of
  1156. * <b>node</b>, else 0.
  1157. *
  1158. * We prefer the IPv6 address if the router has an IPv6 address,
  1159. * and we can use IPv6 addresses, and:
  1160. * i) the router has no IPv4 Dir address.
  1161. * or
  1162. * ii) our preference is for IPv6 Dir addresses.
  1163. *
  1164. * If there is no node, use fascist_firewall_prefer_ipv6_dirport().
  1165. */
  1166. int
  1167. node_ipv6_dir_preferred(const node_t *node)
  1168. {
  1169. const or_options_t *options = get_options();
  1170. tor_addr_port_t ipv4_addr;
  1171. node_assert_ok(node);
  1172. /* node->ipv6_preferred is set from fascist_firewall_prefer_ipv6_orport(),
  1173. * so we can't use it to determine DirPort IPv6 preference.
  1174. * This means that bridge clients will use IPv4 DirPorts by default.
  1175. */
  1176. if (!fascist_firewall_use_ipv6(options)) {
  1177. return 0;
  1178. } else if (node_get_prim_dirport(node, &ipv4_addr)
  1179. || fascist_firewall_prefer_ipv6_dirport(get_options())) {
  1180. return node_has_ipv6_dirport(node);
  1181. }
  1182. return 0;
  1183. }
  1184. /** Copy the primary (IPv4) Dir port (IP address and TCP port) for
  1185. * <b>node</b> into *<b>ap_out</b>. Return 0 if a valid address and
  1186. * port was copied, else return non-zero.*/
  1187. int
  1188. node_get_prim_dirport(const node_t *node, tor_addr_port_t *ap_out)
  1189. {
  1190. node_assert_ok(node);
  1191. tor_assert(ap_out);
  1192. /* Check ri first, because rewrite_node_address_for_bridge() updates
  1193. * node->ri with the configured bridge address. */
  1194. RETURN_IPV4_AP(node->ri, dir_port, ap_out);
  1195. RETURN_IPV4_AP(node->rs, dir_port, ap_out);
  1196. /* Microdescriptors only have an IPv6 address */
  1197. return -1;
  1198. }
  1199. #undef RETURN_IPV4_AP
  1200. /** Copy the preferred Dir port (IP address and TCP port) for
  1201. * <b>node</b> into *<b>ap_out</b>. */
  1202. void
  1203. node_get_pref_dirport(const node_t *node, tor_addr_port_t *ap_out)
  1204. {
  1205. tor_assert(ap_out);
  1206. if (node_ipv6_dir_preferred(node)) {
  1207. node_get_pref_ipv6_dirport(node, ap_out);
  1208. } else {
  1209. /* the primary DirPort is always on IPv4 */
  1210. node_get_prim_dirport(node, ap_out);
  1211. }
  1212. }
  1213. /** Copy the preferred IPv6 Dir port (IP address and TCP port) for
  1214. * <b>node</b> into *<b>ap_out</b>. */
  1215. void
  1216. node_get_pref_ipv6_dirport(const node_t *node, tor_addr_port_t *ap_out)
  1217. {
  1218. node_assert_ok(node);
  1219. tor_assert(ap_out);
  1220. /* Check ri first, because rewrite_node_address_for_bridge() updates
  1221. * node->ri with the configured bridge address.
  1222. * Prefer rs over md for consistency with the fascist_firewall_* functions.
  1223. * Check if the address or port are valid, and try another alternative
  1224. * if they are not. */
  1225. /* Assume IPv4 and IPv6 dirports are the same */
  1226. if (node->ri && tor_addr_port_is_valid(&node->ri->ipv6_addr,
  1227. node->ri->dir_port, 0)) {
  1228. tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
  1229. ap_out->port = node->ri->dir_port;
  1230. } else if (node->rs && tor_addr_port_is_valid(&node->rs->ipv6_addr,
  1231. node->rs->dir_port, 0)) {
  1232. tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
  1233. ap_out->port = node->rs->dir_port;
  1234. } else {
  1235. tor_addr_make_null(&ap_out->addr, AF_INET6);
  1236. ap_out->port = 0;
  1237. }
  1238. }
  1239. /** Return true iff <b>md</b> has a curve25519 onion key.
  1240. * Use node_has_curve25519_onion_key() instead of calling this directly. */
  1241. static int
  1242. microdesc_has_curve25519_onion_key(const microdesc_t *md)
  1243. {
  1244. if (!md) {
  1245. return 0;
  1246. }
  1247. if (!md->onion_curve25519_pkey) {
  1248. return 0;
  1249. }
  1250. if (tor_mem_is_zero((const char*)md->onion_curve25519_pkey->public_key,
  1251. CURVE25519_PUBKEY_LEN)) {
  1252. return 0;
  1253. }
  1254. return 1;
  1255. }
  1256. /** Return true iff <b>node</b> has a curve25519 onion key. */
  1257. int
  1258. node_has_curve25519_onion_key(const node_t *node)
  1259. {
  1260. if (!node)
  1261. return 0;
  1262. if (node->ri)
  1263. return routerinfo_has_curve25519_onion_key(node->ri);
  1264. else if (node->md)
  1265. return microdesc_has_curve25519_onion_key(node->md);
  1266. else
  1267. return 0;
  1268. }
  1269. /** Refresh the country code of <b>ri</b>. This function MUST be called on
  1270. * each router when the GeoIP database is reloaded, and on all new routers. */
  1271. void
  1272. node_set_country(node_t *node)
  1273. {
  1274. tor_addr_t addr = TOR_ADDR_NULL;
  1275. /* XXXXipv6 */
  1276. if (node->rs)
  1277. tor_addr_from_ipv4h(&addr, node->rs->addr);
  1278. else if (node->ri)
  1279. tor_addr_from_ipv4h(&addr, node->ri->addr);
  1280. node->country = geoip_get_country_by_addr(&addr);
  1281. }
  1282. /** Set the country code of all routers in the routerlist. */
  1283. void
  1284. nodelist_refresh_countries(void)
  1285. {
  1286. smartlist_t *nodes = nodelist_get_list();
  1287. SMARTLIST_FOREACH(nodes, node_t *, node,
  1288. node_set_country(node));
  1289. }
  1290. /** Return true iff router1 and router2 have similar enough network addresses
  1291. * that we should treat them as being in the same family */
  1292. int
  1293. addrs_in_same_network_family(const tor_addr_t *a1,
  1294. const tor_addr_t *a2)
  1295. {
  1296. return 0 == tor_addr_compare_masked(a1, a2, 16, CMP_SEMANTIC);
  1297. }
  1298. /** Return true if <b>node</b>'s nickname matches <b>nickname</b>
  1299. * (case-insensitive), or if <b>node's</b> identity key digest
  1300. * matches a hexadecimal value stored in <b>nickname</b>. Return
  1301. * false otherwise. */
  1302. static int
  1303. node_nickname_matches(const node_t *node, const char *nickname)
  1304. {
  1305. const char *n = node_get_nickname(node);
  1306. if (n && nickname[0]!='$' && !strcasecmp(n, nickname))
  1307. return 1;
  1308. return hex_digest_nickname_matches(nickname,
  1309. node->identity,
  1310. n,
  1311. node_is_named(node));
  1312. }
  1313. /** Return true iff <b>node</b> is named by some nickname in <b>lst</b>. */
  1314. static inline int
  1315. node_in_nickname_smartlist(const smartlist_t *lst, const node_t *node)
  1316. {
  1317. if (!lst) return 0;
  1318. SMARTLIST_FOREACH(lst, const char *, name, {
  1319. if (node_nickname_matches(node, name))
  1320. return 1;
  1321. });
  1322. return 0;
  1323. }
  1324. /** Return true iff r1 and r2 are in the same family, but not the same
  1325. * router. */
  1326. int
  1327. nodes_in_same_family(const node_t *node1, const node_t *node2)
  1328. {
  1329. const or_options_t *options = get_options();
  1330. /* Are they in the same family because of their addresses? */
  1331. if (options->EnforceDistinctSubnets) {
  1332. tor_addr_t a1, a2;
  1333. node_get_addr(node1, &a1);
  1334. node_get_addr(node2, &a2);
  1335. if (addrs_in_same_network_family(&a1, &a2))
  1336. return 1;
  1337. }
  1338. /* Are they in the same family because the agree they are? */
  1339. {
  1340. const smartlist_t *f1, *f2;
  1341. f1 = node_get_declared_family(node1);
  1342. f2 = node_get_declared_family(node2);
  1343. if (f1 && f2 &&
  1344. node_in_nickname_smartlist(f1, node2) &&
  1345. node_in_nickname_smartlist(f2, node1))
  1346. return 1;
  1347. }
  1348. /* Are they in the same option because the user says they are? */
  1349. if (options->NodeFamilySets) {
  1350. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  1351. if (routerset_contains_node(rs, node1) &&
  1352. routerset_contains_node(rs, node2))
  1353. return 1;
  1354. });
  1355. }
  1356. return 0;
  1357. }
  1358. /**
  1359. * Add all the family of <b>node</b>, including <b>node</b> itself, to
  1360. * the smartlist <b>sl</b>.
  1361. *
  1362. * This is used to make sure we don't pick siblings in a single path, or
  1363. * pick more than one relay from a family for our entry guard list.
  1364. * Note that a node may be added to <b>sl</b> more than once if it is
  1365. * part of <b>node</b>'s family for more than one reason.
  1366. */
  1367. void
  1368. nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
  1369. {
  1370. const smartlist_t *all_nodes = nodelist_get_list();
  1371. const smartlist_t *declared_family;
  1372. const or_options_t *options = get_options();
  1373. tor_assert(node);
  1374. declared_family = node_get_declared_family(node);
  1375. /* Let's make sure that we have the node itself, if it's a real node. */
  1376. {
  1377. const node_t *real_node = node_get_by_id(node->identity);
  1378. if (real_node)
  1379. smartlist_add(sl, (node_t*)real_node);
  1380. }
  1381. /* First, add any nodes with similar network addresses. */
  1382. if (options->EnforceDistinctSubnets) {
  1383. tor_addr_t node_addr;
  1384. node_get_addr(node, &node_addr);
  1385. SMARTLIST_FOREACH_BEGIN(all_nodes, const node_t *, node2) {
  1386. tor_addr_t a;
  1387. node_get_addr(node2, &a);
  1388. if (addrs_in_same_network_family(&a, &node_addr))
  1389. smartlist_add(sl, (void*)node2);
  1390. } SMARTLIST_FOREACH_END(node2);
  1391. }
  1392. /* Now, add all nodes in the declared_family of this node, if they
  1393. * also declare this node to be in their family. */
  1394. if (declared_family) {
  1395. /* Add every r such that router declares familyness with node, and node
  1396. * declares familyhood with router. */
  1397. SMARTLIST_FOREACH_BEGIN(declared_family, const char *, name) {
  1398. const node_t *node2;
  1399. const smartlist_t *family2;
  1400. if (!(node2 = node_get_by_nickname(name, 0)))
  1401. continue;
  1402. if (!(family2 = node_get_declared_family(node2)))
  1403. continue;
  1404. SMARTLIST_FOREACH_BEGIN(family2, const char *, name2) {
  1405. if (node_nickname_matches(node, name2)) {
  1406. smartlist_add(sl, (void*)node2);
  1407. break;
  1408. }
  1409. } SMARTLIST_FOREACH_END(name2);
  1410. } SMARTLIST_FOREACH_END(name);
  1411. }
  1412. /* If the user declared any families locally, honor those too. */
  1413. if (options->NodeFamilySets) {
  1414. SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
  1415. if (routerset_contains_node(rs, node)) {
  1416. routerset_get_all_nodes(sl, rs, NULL, 0);
  1417. }
  1418. });
  1419. }
  1420. }
  1421. /** Find a router that's up, that has this IP address, and
  1422. * that allows exit to this address:port, or return NULL if there
  1423. * isn't a good one.
  1424. * Don't exit enclave to excluded relays -- it wouldn't actually
  1425. * hurt anything, but this way there are fewer confused users.
  1426. */
  1427. const node_t *
  1428. router_find_exact_exit_enclave(const char *address, uint16_t port)
  1429. {/*XXXX MOVE*/
  1430. uint32_t addr;
  1431. struct in_addr in;
  1432. tor_addr_t a;
  1433. const or_options_t *options = get_options();
  1434. if (!tor_inet_aton(address, &in))
  1435. return NULL; /* it's not an IP already */
  1436. addr = ntohl(in.s_addr);
  1437. tor_addr_from_ipv4h(&a, addr);
  1438. SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
  1439. if (node_get_addr_ipv4h(node) == addr &&
  1440. node->is_running &&
  1441. compare_tor_addr_to_node_policy(&a, port, node) ==
  1442. ADDR_POLICY_ACCEPTED &&
  1443. !routerset_contains_node(options->ExcludeExitNodesUnion_, node))
  1444. return node;
  1445. });
  1446. return NULL;
  1447. }
  1448. /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
  1449. * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
  1450. * If <b>need_capacity</b> is non-zero, we require a minimum advertised
  1451. * bandwidth.
  1452. * If <b>need_guard</b>, we require that the router is a possible entry guard.
  1453. */
  1454. int
  1455. node_is_unreliable(const node_t *node, int need_uptime,
  1456. int need_capacity, int need_guard)
  1457. {
  1458. if (need_uptime && !node->is_stable)
  1459. return 1;
  1460. if (need_capacity && !node->is_fast)
  1461. return 1;
  1462. if (need_guard && !node->is_possible_guard)
  1463. return 1;
  1464. return 0;
  1465. }
  1466. /** Return 1 if all running sufficiently-stable routers we can use will reject
  1467. * addr:port. Return 0 if any might accept it. */
  1468. int
  1469. router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port,
  1470. int need_uptime)
  1471. {
  1472. addr_policy_result_t r;
  1473. SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
  1474. if (node->is_running &&
  1475. !node_is_unreliable(node, need_uptime, 0, 0)) {
  1476. r = compare_tor_addr_to_node_policy(addr, port, node);
  1477. if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
  1478. return 0; /* this one could be ok. good enough. */
  1479. }
  1480. } SMARTLIST_FOREACH_END(node);
  1481. return 1; /* all will reject. */
  1482. }
  1483. /** Mark the router with ID <b>digest</b> as running or non-running
  1484. * in our routerlist. */
  1485. void
  1486. router_set_status(const char *digest, int up)
  1487. {
  1488. node_t *node;
  1489. tor_assert(digest);
  1490. SMARTLIST_FOREACH(router_get_fallback_dir_servers(),
  1491. dir_server_t *, d,
  1492. if (tor_memeq(d->digest, digest, DIGEST_LEN))
  1493. d->is_running = up);
  1494. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1495. dir_server_t *, d,
  1496. if (tor_memeq(d->digest, digest, DIGEST_LEN))
  1497. d->is_running = up);
  1498. node = node_get_mutable_by_id(digest);
  1499. if (node) {
  1500. #if 0
  1501. log_debug(LD_DIR,"Marking router %s as %s.",
  1502. node_describe(node), up ? "up" : "down");
  1503. #endif
  1504. if (!up && node_is_me(node) && !net_is_disabled())
  1505. log_warn(LD_NET, "We just marked ourself as down. Are your external "
  1506. "addresses reachable?");
  1507. if (bool_neq(node->is_running, up))
  1508. router_dir_info_changed();
  1509. node->is_running = up;
  1510. }
  1511. }
  1512. /** True iff, the last time we checked whether we had enough directory info
  1513. * to build circuits, the answer was "yes". If there are no exits in the
  1514. * consensus, we act as if we have 100% of the exit directory info. */
  1515. static int have_min_dir_info = 0;
  1516. /** Does the consensus contain nodes that can exit? */
  1517. static consensus_path_type_t have_consensus_path = CONSENSUS_PATH_UNKNOWN;
  1518. /** True iff enough has changed since the last time we checked whether we had
  1519. * enough directory info to build circuits that our old answer can no longer
  1520. * be trusted. */
  1521. static int need_to_update_have_min_dir_info = 1;
  1522. /** String describing what we're missing before we have enough directory
  1523. * info. */
  1524. static char dir_info_status[512] = "";
  1525. /** Return true iff we have enough consensus information to
  1526. * start building circuits. Right now, this means "a consensus that's
  1527. * less than a day old, and at least 60% of router descriptors (configurable),
  1528. * weighted by bandwidth. Treat the exit fraction as 100% if there are
  1529. * no exits in the consensus."
  1530. * To obtain the final weighted bandwidth, we multiply the
  1531. * weighted bandwidth fraction for each position (guard, middle, exit). */
  1532. int
  1533. router_have_minimum_dir_info(void)
  1534. {
  1535. static int logged_delay=0;
  1536. const char *delay_fetches_msg = NULL;
  1537. if (should_delay_dir_fetches(get_options(), &delay_fetches_msg)) {
  1538. if (!logged_delay)
  1539. log_notice(LD_DIR, "Delaying directory fetches: %s", delay_fetches_msg);
  1540. logged_delay=1;
  1541. strlcpy(dir_info_status, delay_fetches_msg, sizeof(dir_info_status));
  1542. return 0;
  1543. }
  1544. logged_delay = 0; /* reset it if we get this far */
  1545. if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
  1546. update_router_have_minimum_dir_info();
  1547. }
  1548. return have_min_dir_info;
  1549. }
  1550. /** Set to CONSENSUS_PATH_EXIT if there is at least one exit node
  1551. * in the consensus. We update this flag in compute_frac_paths_available if
  1552. * there is at least one relay that has an Exit flag in the consensus.
  1553. * Used to avoid building exit circuits when they will almost certainly fail.
  1554. * Set to CONSENSUS_PATH_INTERNAL if there are no exits in the consensus.
  1555. * (This situation typically occurs during bootstrap of a test network.)
  1556. * Set to CONSENSUS_PATH_UNKNOWN if we have never checked, or have
  1557. * reason to believe our last known value was invalid or has expired.
  1558. * If we're in a network with TestingDirAuthVoteExit set,
  1559. * this can cause router_have_consensus_path() to be set to
  1560. * CONSENSUS_PATH_EXIT, even if there are no nodes with accept exit policies.
  1561. */
  1562. MOCK_IMPL(consensus_path_type_t,
  1563. router_have_consensus_path, (void))
  1564. {
  1565. return have_consensus_path;
  1566. }
  1567. /** Called when our internal view of the directory has changed. This can be
  1568. * when the authorities change, networkstatuses change, the list of routerdescs
  1569. * changes, or number of running routers changes.
  1570. */
  1571. void
  1572. router_dir_info_changed(void)
  1573. {
  1574. need_to_update_have_min_dir_info = 1;
  1575. rend_hsdir_routers_changed();
  1576. }
  1577. /** Return a string describing what we're missing before we have enough
  1578. * directory info. */
  1579. const char *
  1580. get_dir_info_status_string(void)
  1581. {
  1582. return dir_info_status;
  1583. }
  1584. /** Iterate over the servers listed in <b>consensus</b>, and count how many of
  1585. * them seem like ones we'd use (store this in *<b>num_usable</b>), and how
  1586. * many of <em>those</em> we have descriptors for (store this in
  1587. * *<b>num_present</b>).
  1588. *
  1589. * If <b>in_set</b> is non-NULL, only consider those routers in <b>in_set</b>.
  1590. * If <b>exit_only</b> is USABLE_DESCRIPTOR_EXIT_ONLY, only consider nodes
  1591. * with the Exit flag.
  1592. * If *<b>descs_out</b> is present, add a node_t for each usable descriptor
  1593. * to it.
  1594. */
  1595. static void
  1596. count_usable_descriptors(int *num_present, int *num_usable,
  1597. smartlist_t *descs_out,
  1598. const networkstatus_t *consensus,
  1599. time_t now,
  1600. routerset_t *in_set,
  1601. usable_descriptor_t exit_only)
  1602. {
  1603. const int md = (consensus->flavor == FLAV_MICRODESC);
  1604. *num_present = 0, *num_usable = 0;
  1605. SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, routerstatus_t *, rs)
  1606. {
  1607. const node_t *node = node_get_by_id(rs->identity_digest);
  1608. if (!node)
  1609. continue; /* This would be a bug: every entry in the consensus is
  1610. * supposed to have a node. */
  1611. if (exit_only == USABLE_DESCRIPTOR_EXIT_ONLY && ! rs->is_exit)
  1612. continue;
  1613. if (in_set && ! routerset_contains_routerstatus(in_set, rs, -1))
  1614. continue;
  1615. if (client_would_use_router(rs, now)) {
  1616. const char * const digest = rs->descriptor_digest;
  1617. int present;
  1618. ++*num_usable; /* the consensus says we want it. */
  1619. if (md)
  1620. present = NULL != microdesc_cache_lookup_by_digest256(NULL, digest);
  1621. else
  1622. present = NULL != router_get_by_descriptor_digest(digest);
  1623. if (present) {
  1624. /* we have the descriptor listed in the consensus. */
  1625. ++*num_present;
  1626. }
  1627. if (descs_out)
  1628. smartlist_add(descs_out, (node_t*)node);
  1629. }
  1630. }
  1631. SMARTLIST_FOREACH_END(rs);
  1632. log_debug(LD_DIR, "%d usable, %d present (%s%s).",
  1633. *num_usable, *num_present,
  1634. md ? "microdesc" : "desc",
  1635. exit_only == USABLE_DESCRIPTOR_EXIT_ONLY ? " exits" : "s");
  1636. }
  1637. /** Return an estimate of which fraction of usable paths through the Tor
  1638. * network we have available for use. Count how many routers seem like ones
  1639. * we'd use (store this in *<b>num_usable_out</b>), and how many of
  1640. * <em>those</em> we have descriptors for (store this in
  1641. * *<b>num_present_out</b>.)
  1642. *
  1643. * If **<b>status_out</b> is present, allocate a new string and print the
  1644. * available percentages of guard, middle, and exit nodes to it, noting
  1645. * whether there are exits in the consensus.
  1646. * If there are no exits in the consensus, we treat the exit fraction as 100%,
  1647. * but set router_have_consensus_path() so that we can only build internal
  1648. * paths. */
  1649. static double
  1650. compute_frac_paths_available(const networkstatus_t *consensus,
  1651. const or_options_t *options, time_t now,
  1652. int *num_present_out, int *num_usable_out,
  1653. char **status_out)
  1654. {
  1655. smartlist_t *guards = smartlist_new();
  1656. smartlist_t *mid = smartlist_new();
  1657. smartlist_t *exits = smartlist_new();
  1658. double f_guard, f_mid, f_exit;
  1659. double f_path = 0.0;
  1660. /* Used to determine whether there are any exits in the consensus */
  1661. int np = 0;
  1662. /* Used to determine whether there are any exits with descriptors */
  1663. int nu = 0;
  1664. const int authdir = authdir_mode_v3(options);
  1665. count_usable_descriptors(num_present_out, num_usable_out,
  1666. mid, consensus, now, NULL,
  1667. USABLE_DESCRIPTOR_ALL);
  1668. if (options->EntryNodes) {
  1669. count_usable_descriptors(&np, &nu, guards, consensus, now,
  1670. options->EntryNodes, USABLE_DESCRIPTOR_ALL);
  1671. } else {
  1672. SMARTLIST_FOREACH(mid, const node_t *, node, {
  1673. if (authdir) {
  1674. if (node->rs && node->rs->is_possible_guard)
  1675. smartlist_add(guards, (node_t*)node);
  1676. } else {
  1677. if (node->is_possible_guard)
  1678. smartlist_add(guards, (node_t*)node);
  1679. }
  1680. });
  1681. }
  1682. /* All nodes with exit flag
  1683. * If we're in a network with TestingDirAuthVoteExit set,
  1684. * this can cause false positives on have_consensus_path,
  1685. * incorrectly setting it to CONSENSUS_PATH_EXIT. This is
  1686. * an unavoidable feature of forcing authorities to declare
  1687. * certain nodes as exits.
  1688. */
  1689. count_usable_descriptors(&np, &nu, exits, consensus, now,
  1690. NULL, USABLE_DESCRIPTOR_EXIT_ONLY);
  1691. log_debug(LD_NET,
  1692. "%s: %d present, %d usable",
  1693. "exits",
  1694. np,
  1695. nu);
  1696. /* We need at least 1 exit present in the consensus to consider
  1697. * building exit paths */
  1698. /* Update our understanding of whether the consensus has exits */
  1699. consensus_path_type_t old_have_consensus_path = have_consensus_path;
  1700. have_consensus_path = ((nu > 0) ?
  1701. CONSENSUS_PATH_EXIT :
  1702. CONSENSUS_PATH_INTERNAL);
  1703. if (have_consensus_path == CONSENSUS_PATH_INTERNAL
  1704. && old_have_consensus_path != have_consensus_path) {
  1705. log_notice(LD_NET,
  1706. "The current consensus has no exit nodes. "
  1707. "Tor can only build internal paths, "
  1708. "such as paths to hidden services.");
  1709. /* However, exit nodes can reachability self-test using this consensus,
  1710. * join the network, and appear in a later consensus. This will allow
  1711. * the network to build exit paths, such as paths for world wide web
  1712. * browsing (as distinct from hidden service web browsing). */
  1713. }
  1714. f_guard = frac_nodes_with_descriptors(guards, WEIGHT_FOR_GUARD);
  1715. f_mid = frac_nodes_with_descriptors(mid, WEIGHT_FOR_MID);
  1716. f_exit = frac_nodes_with_descriptors(exits, WEIGHT_FOR_EXIT);
  1717. log_debug(LD_NET,
  1718. "f_guard: %.2f, f_mid: %.2f, f_exit: %.2f",
  1719. f_guard,
  1720. f_mid,
  1721. f_exit);
  1722. smartlist_free(guards);
  1723. smartlist_free(mid);
  1724. smartlist_free(exits);
  1725. if (options->ExitNodes) {
  1726. double f_myexit, f_myexit_unflagged;
  1727. smartlist_t *myexits= smartlist_new();
  1728. smartlist_t *myexits_unflagged = smartlist_new();
  1729. /* All nodes with exit flag in ExitNodes option */
  1730. count_usable_descriptors(&np, &nu, myexits, consensus, now,
  1731. options->ExitNodes, USABLE_DESCRIPTOR_EXIT_ONLY);
  1732. log_debug(LD_NET,
  1733. "%s: %d present, %d usable",
  1734. "myexits",
  1735. np,
  1736. nu);
  1737. /* Now compute the nodes in the ExitNodes option where which we don't know
  1738. * what their exit policy is, or we know it permits something. */
  1739. count_usable_descriptors(&np, &nu, myexits_unflagged,
  1740. consensus, now,
  1741. options->ExitNodes, USABLE_DESCRIPTOR_ALL);
  1742. log_debug(LD_NET,
  1743. "%s: %d present, %d usable",
  1744. "myexits_unflagged (initial)",
  1745. np,
  1746. nu);
  1747. SMARTLIST_FOREACH_BEGIN(myexits_unflagged, const node_t *, node) {
  1748. if (node_has_descriptor(node) && node_exit_policy_rejects_all(node)) {
  1749. SMARTLIST_DEL_CURRENT(myexits_unflagged, node);
  1750. /* this node is not actually an exit */
  1751. np--;
  1752. /* this node is unusable as an exit */
  1753. nu--;
  1754. }
  1755. } SMARTLIST_FOREACH_END(node);
  1756. log_debug(LD_NET,
  1757. "%s: %d present, %d usable",
  1758. "myexits_unflagged (final)",
  1759. np,
  1760. nu);
  1761. f_myexit= frac_nodes_with_descriptors(myexits,WEIGHT_FOR_EXIT);
  1762. f_myexit_unflagged=
  1763. frac_nodes_with_descriptors(myexits_unflagged,WEIGHT_FOR_EXIT);
  1764. log_debug(LD_NET,
  1765. "f_exit: %.2f, f_myexit: %.2f, f_myexit_unflagged: %.2f",
  1766. f_exit,
  1767. f_myexit,
  1768. f_myexit_unflagged);
  1769. /* If our ExitNodes list has eliminated every possible Exit node, and there
  1770. * were some possible Exit nodes, then instead consider nodes that permit
  1771. * exiting to some ports. */
  1772. if (smartlist_len(myexits) == 0 &&
  1773. smartlist_len(myexits_unflagged)) {
  1774. f_myexit = f_myexit_unflagged;
  1775. }
  1776. smartlist_free(myexits);
  1777. smartlist_free(myexits_unflagged);
  1778. /* This is a tricky point here: we don't want to make it easy for a
  1779. * directory to trickle exits to us until it learns which exits we have
  1780. * configured, so require that we have a threshold both of total exits
  1781. * and usable exits. */
  1782. if (f_myexit < f_exit)
  1783. f_exit = f_myexit;
  1784. }
  1785. /* if the consensus has no exits, treat the exit fraction as 100% */
  1786. if (router_have_consensus_path() != CONSENSUS_PATH_EXIT) {
  1787. f_exit = 1.0;
  1788. }
  1789. f_path = f_guard * f_mid * f_exit;
  1790. if (status_out)
  1791. tor_asprintf(status_out,
  1792. "%d%% of guards bw, "
  1793. "%d%% of midpoint bw, and "
  1794. "%d%% of exit bw%s = "
  1795. "%d%% of path bw",
  1796. (int)(f_guard*100),
  1797. (int)(f_mid*100),
  1798. (int)(f_exit*100),
  1799. (router_have_consensus_path() == CONSENSUS_PATH_EXIT ?
  1800. "" :
  1801. " (no exits in consensus)"),
  1802. (int)(f_path*100));
  1803. return f_path;
  1804. }
  1805. /** We just fetched a new set of descriptors. Compute how far through
  1806. * the "loading descriptors" bootstrapping phase we are, so we can inform
  1807. * the controller of our progress. */
  1808. int
  1809. count_loading_descriptors_progress(void)
  1810. {
  1811. int num_present = 0, num_usable=0;
  1812. time_t now = time(NULL);
  1813. const or_options_t *options = get_options();
  1814. const networkstatus_t *consensus =
  1815. networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
  1816. double paths, fraction;
  1817. if (!consensus)
  1818. return 0; /* can't count descriptors if we have no list of them */
  1819. paths = compute_frac_paths_available(consensus, options, now,
  1820. &num_present, &num_usable,
  1821. NULL);
  1822. fraction = paths / get_frac_paths_needed_for_circs(options,consensus);
  1823. if (fraction > 1.0)
  1824. return 0; /* it's not the number of descriptors holding us back */
  1825. return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
  1826. (fraction*(BOOTSTRAP_STATUS_CONN_OR-1 -
  1827. BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
  1828. }
  1829. /** Return the fraction of paths needed before we're willing to build
  1830. * circuits, as configured in <b>options</b>, or in the consensus <b>ns</b>. */
  1831. static double
  1832. get_frac_paths_needed_for_circs(const or_options_t *options,
  1833. const networkstatus_t *ns)
  1834. {
  1835. #define DFLT_PCT_USABLE_NEEDED 60
  1836. if (options->PathsNeededToBuildCircuits >= 0.0) {
  1837. return options->PathsNeededToBuildCircuits;
  1838. } else {
  1839. return networkstatus_get_param(ns, "min_paths_for_circs_pct",
  1840. DFLT_PCT_USABLE_NEEDED,
  1841. 25, 95)/100.0;
  1842. }
  1843. }
  1844. /** Change the value of have_min_dir_info, setting it true iff we have enough
  1845. * network and router information to build circuits. Clear the value of
  1846. * need_to_update_have_min_dir_info. */
  1847. static void
  1848. update_router_have_minimum_dir_info(void)
  1849. {
  1850. time_t now = time(NULL);
  1851. int res;
  1852. const or_options_t *options = get_options();
  1853. const networkstatus_t *consensus =
  1854. networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
  1855. int using_md;
  1856. if (!consensus) {
  1857. if (!networkstatus_get_latest_consensus())
  1858. strlcpy(dir_info_status, "We have no usable consensus.",
  1859. sizeof(dir_info_status));
  1860. else
  1861. strlcpy(dir_info_status, "We have no recent usable consensus.",
  1862. sizeof(dir_info_status));
  1863. res = 0;
  1864. goto done;
  1865. }
  1866. using_md = consensus->flavor == FLAV_MICRODESC;
  1867. if (! entry_guards_have_enough_dir_info_to_build_circuits()) {
  1868. strlcpy(dir_info_status, "We're missing descriptors for some of our "
  1869. "primary entry guards", sizeof(dir_info_status));
  1870. res = 0;
  1871. goto done;
  1872. }
  1873. /* Check fraction of available paths */
  1874. {
  1875. char *status = NULL;
  1876. int num_present=0, num_usable=0;
  1877. double paths = compute_frac_paths_available(consensus, options, now,
  1878. &num_present, &num_usable,
  1879. &status);
  1880. if (paths < get_frac_paths_needed_for_circs(options,consensus)) {
  1881. tor_snprintf(dir_info_status, sizeof(dir_info_status),
  1882. "We need more %sdescriptors: we have %d/%d, and "
  1883. "can only build %d%% of likely paths. (We have %s.)",
  1884. using_md?"micro":"", num_present, num_usable,
  1885. (int)(paths*100), status);
  1886. tor_free(status);
  1887. res = 0;
  1888. control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
  1889. goto done;
  1890. }
  1891. tor_free(status);
  1892. res = 1;
  1893. }
  1894. done:
  1895. /* If paths have just become available in this update. */
  1896. if (res && !have_min_dir_info) {
  1897. control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
  1898. if (control_event_bootstrap(BOOTSTRAP_STATUS_CONN_OR, 0) == 0) {
  1899. log_notice(LD_DIR,
  1900. "We now have enough directory information to build circuits.");
  1901. }
  1902. }
  1903. /* If paths have just become unavailable in this update. */
  1904. if (!res && have_min_dir_info) {
  1905. int quiet = directory_too_idle_to_fetch_descriptors(options, now);
  1906. tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
  1907. "Our directory information is no longer up-to-date "
  1908. "enough to build circuits: %s", dir_info_status);
  1909. /* a) make us log when we next complete a circuit, so we know when Tor
  1910. * is back up and usable, and b) disable some activities that Tor
  1911. * should only do while circuits are working, like reachability tests
  1912. * and fetching bridge descriptors only over circuits. */
  1913. note_that_we_maybe_cant_complete_circuits();
  1914. have_consensus_path = CONSENSUS_PATH_UNKNOWN;
  1915. control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
  1916. }
  1917. have_min_dir_info = res;
  1918. need_to_update_have_min_dir_info = 0;
  1919. }