nodelist.c 78 KB

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