nodelist.c 78 KB

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