nodelist.c 81 KB

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