nodelist.c 84 KB

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