networkstatus.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  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-2015, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file networkstatus.c
  8. * \brief Functions and structures for handling network status documents as a
  9. * client or cache.
  10. */
  11. #define NETWORKSTATUS_PRIVATE
  12. #include "or.h"
  13. #include "channel.h"
  14. #include "circuitmux.h"
  15. #include "circuitmux_ewma.h"
  16. #include "circuitstats.h"
  17. #include "config.h"
  18. #include "connection.h"
  19. #include "connection_or.h"
  20. #include "control.h"
  21. #include "directory.h"
  22. #include "dirserv.h"
  23. #include "dirvote.h"
  24. #include "entrynodes.h"
  25. #include "main.h"
  26. #include "microdesc.h"
  27. #include "networkstatus.h"
  28. #include "nodelist.h"
  29. #include "relay.h"
  30. #include "router.h"
  31. #include "routerlist.h"
  32. #include "routerparse.h"
  33. #include "transports.h"
  34. /** Map from lowercase nickname to identity digest of named server, if any. */
  35. static strmap_t *named_server_map = NULL;
  36. /** Map from lowercase nickname to (void*)1 for all names that are listed
  37. * as unnamed for some server in the consensus. */
  38. static strmap_t *unnamed_server_map = NULL;
  39. /** Most recently received and validated v3 consensus network status,
  40. * of whichever type we are using for our own circuits. This will be the same
  41. * as one of current_ns_consensus or current_md_consensus.
  42. */
  43. #define current_consensus \
  44. (we_use_microdescriptors_for_circuits(get_options()) ? \
  45. current_md_consensus : current_ns_consensus)
  46. /** Most recently received and validated v3 "ns"-flavored consensus network
  47. * status. */
  48. static networkstatus_t *current_ns_consensus = NULL;
  49. /** Most recently received and validated v3 "microdec"-flavored consensus
  50. * network status. */
  51. static networkstatus_t *current_md_consensus = NULL;
  52. /** A v3 consensus networkstatus that we've received, but which we don't
  53. * have enough certificates to be happy about. */
  54. typedef struct consensus_waiting_for_certs_t {
  55. /** The consensus itself. */
  56. networkstatus_t *consensus;
  57. /** The encoded version of the consensus, nul-terminated. */
  58. char *body;
  59. /** When did we set the current value of consensus_waiting_for_certs? If
  60. * this is too recent, we shouldn't try to fetch a new consensus for a
  61. * little while, to give ourselves time to get certificates for this one. */
  62. time_t set_at;
  63. /** Set to 1 if we've been holding on to it for so long we should maybe
  64. * treat it as being bad. */
  65. int dl_failed;
  66. } consensus_waiting_for_certs_t;
  67. /** An array, for each flavor of consensus we might want, of consensuses that
  68. * we have downloaded, but which we cannot verify due to having insufficient
  69. * authority certificates. */
  70. static consensus_waiting_for_certs_t
  71. consensus_waiting_for_certs[N_CONSENSUS_FLAVORS];
  72. /** A time before which we shouldn't try to replace the current consensus:
  73. * this will be at some point after the next consensus becomes valid, but
  74. * before the current consensus becomes invalid. */
  75. static time_t time_to_download_next_consensus[N_CONSENSUS_FLAVORS];
  76. /** Download status for the current consensus networkstatus. */
  77. static download_status_t consensus_dl_status[N_CONSENSUS_FLAVORS] =
  78. {
  79. { 0, 0, DL_SCHED_CONSENSUS },
  80. { 0, 0, DL_SCHED_CONSENSUS },
  81. };
  82. /** True iff we have logged a warning about this OR's version being older than
  83. * listed by the authorities. */
  84. static int have_warned_about_old_version = 0;
  85. /** True iff we have logged a warning about this OR's version being newer than
  86. * listed by the authorities. */
  87. static int have_warned_about_new_version = 0;
  88. static void routerstatus_list_update_named_server_map(void);
  89. /** Forget that we've warned about anything networkstatus-related, so we will
  90. * give fresh warnings if the same behavior happens again. */
  91. void
  92. networkstatus_reset_warnings(void)
  93. {
  94. if (current_consensus) {
  95. SMARTLIST_FOREACH(nodelist_get_list(), node_t *, node,
  96. node->name_lookup_warned = 0);
  97. }
  98. have_warned_about_old_version = 0;
  99. have_warned_about_new_version = 0;
  100. }
  101. /** Reset the descriptor download failure count on all networkstatus docs, so
  102. * that we can retry any long-failed documents immediately.
  103. */
  104. void
  105. networkstatus_reset_download_failures(void)
  106. {
  107. int i;
  108. for (i=0; i < N_CONSENSUS_FLAVORS; ++i)
  109. download_status_reset(&consensus_dl_status[i]);
  110. }
  111. /** Read every cached v3 consensus networkstatus from the disk. */
  112. int
  113. router_reload_consensus_networkstatus(void)
  114. {
  115. char *filename;
  116. char *s;
  117. const unsigned int flags = NSSET_FROM_CACHE | NSSET_DONT_DOWNLOAD_CERTS;
  118. int flav;
  119. /* FFFF Suppress warnings if cached consensus is bad? */
  120. for (flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  121. char buf[128];
  122. const char *flavor = networkstatus_get_flavor_name(flav);
  123. if (flav == FLAV_NS) {
  124. filename = get_datadir_fname("cached-consensus");
  125. } else {
  126. tor_snprintf(buf, sizeof(buf), "cached-%s-consensus", flavor);
  127. filename = get_datadir_fname(buf);
  128. }
  129. s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  130. if (s) {
  131. if (networkstatus_set_current_consensus(s, flavor, flags) < -1) {
  132. log_warn(LD_FS, "Couldn't load consensus %s networkstatus from \"%s\"",
  133. flavor, filename);
  134. }
  135. tor_free(s);
  136. }
  137. tor_free(filename);
  138. if (flav == FLAV_NS) {
  139. filename = get_datadir_fname("unverified-consensus");
  140. } else {
  141. tor_snprintf(buf, sizeof(buf), "unverified-%s-consensus", flavor);
  142. filename = get_datadir_fname(buf);
  143. }
  144. s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  145. if (s) {
  146. if (networkstatus_set_current_consensus(s, flavor,
  147. flags|NSSET_WAS_WAITING_FOR_CERTS)) {
  148. log_info(LD_FS, "Couldn't load consensus %s networkstatus from \"%s\"",
  149. flavor, filename);
  150. }
  151. tor_free(s);
  152. }
  153. tor_free(filename);
  154. }
  155. if (!current_consensus) {
  156. if (!named_server_map)
  157. named_server_map = strmap_new();
  158. if (!unnamed_server_map)
  159. unnamed_server_map = strmap_new();
  160. }
  161. update_certificate_downloads(time(NULL));
  162. routers_update_all_from_networkstatus(time(NULL), 3);
  163. update_microdescs_from_networkstatus(time(NULL));
  164. return 0;
  165. }
  166. /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
  167. STATIC void
  168. vote_routerstatus_free(vote_routerstatus_t *rs)
  169. {
  170. vote_microdesc_hash_t *h, *next;
  171. if (!rs)
  172. return;
  173. tor_free(rs->version);
  174. tor_free(rs->status.exitsummary);
  175. for (h = rs->microdesc; h; h = next) {
  176. tor_free(h->microdesc_hash_line);
  177. next = h->next;
  178. tor_free(h);
  179. }
  180. tor_free(rs);
  181. }
  182. /** Free all storage held by the routerstatus object <b>rs</b>. */
  183. void
  184. routerstatus_free(routerstatus_t *rs)
  185. {
  186. if (!rs)
  187. return;
  188. tor_free(rs->exitsummary);
  189. tor_free(rs);
  190. }
  191. /** Free all storage held in <b>sig</b> */
  192. void
  193. document_signature_free(document_signature_t *sig)
  194. {
  195. tor_free(sig->signature);
  196. tor_free(sig);
  197. }
  198. /** Return a newly allocated copy of <b>sig</b> */
  199. document_signature_t *
  200. document_signature_dup(const document_signature_t *sig)
  201. {
  202. document_signature_t *r = tor_memdup(sig, sizeof(document_signature_t));
  203. if (r->signature)
  204. r->signature = tor_memdup(sig->signature, sig->signature_len);
  205. return r;
  206. }
  207. /** Free all storage held in <b>ns</b>. */
  208. void
  209. networkstatus_vote_free(networkstatus_t *ns)
  210. {
  211. if (!ns)
  212. return;
  213. tor_free(ns->client_versions);
  214. tor_free(ns->server_versions);
  215. if (ns->known_flags) {
  216. SMARTLIST_FOREACH(ns->known_flags, char *, c, tor_free(c));
  217. smartlist_free(ns->known_flags);
  218. }
  219. if (ns->weight_params) {
  220. SMARTLIST_FOREACH(ns->weight_params, char *, c, tor_free(c));
  221. smartlist_free(ns->weight_params);
  222. }
  223. if (ns->net_params) {
  224. SMARTLIST_FOREACH(ns->net_params, char *, c, tor_free(c));
  225. smartlist_free(ns->net_params);
  226. }
  227. if (ns->supported_methods) {
  228. SMARTLIST_FOREACH(ns->supported_methods, char *, c, tor_free(c));
  229. smartlist_free(ns->supported_methods);
  230. }
  231. if (ns->voters) {
  232. SMARTLIST_FOREACH_BEGIN(ns->voters, networkstatus_voter_info_t *, voter) {
  233. tor_free(voter->nickname);
  234. tor_free(voter->address);
  235. tor_free(voter->contact);
  236. if (voter->sigs) {
  237. SMARTLIST_FOREACH(voter->sigs, document_signature_t *, sig,
  238. document_signature_free(sig));
  239. smartlist_free(voter->sigs);
  240. }
  241. tor_free(voter);
  242. } SMARTLIST_FOREACH_END(voter);
  243. smartlist_free(ns->voters);
  244. }
  245. authority_cert_free(ns->cert);
  246. if (ns->routerstatus_list) {
  247. if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_OPINION) {
  248. SMARTLIST_FOREACH(ns->routerstatus_list, vote_routerstatus_t *, rs,
  249. vote_routerstatus_free(rs));
  250. } else {
  251. SMARTLIST_FOREACH(ns->routerstatus_list, routerstatus_t *, rs,
  252. routerstatus_free(rs));
  253. }
  254. smartlist_free(ns->routerstatus_list);
  255. }
  256. digestmap_free(ns->desc_digest_map, NULL);
  257. memwipe(ns, 11, sizeof(*ns));
  258. tor_free(ns);
  259. }
  260. /** Return the voter info from <b>vote</b> for the voter whose identity digest
  261. * is <b>identity</b>, or NULL if no such voter is associated with
  262. * <b>vote</b>. */
  263. networkstatus_voter_info_t *
  264. networkstatus_get_voter_by_id(networkstatus_t *vote,
  265. const char *identity)
  266. {
  267. if (!vote || !vote->voters)
  268. return NULL;
  269. SMARTLIST_FOREACH(vote->voters, networkstatus_voter_info_t *, voter,
  270. if (fast_memeq(voter->identity_digest, identity, DIGEST_LEN))
  271. return voter);
  272. return NULL;
  273. }
  274. /** Check whether the signature <b>sig</b> is correctly signed with the
  275. * signing key in <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
  276. * signing key; otherwise set the good_signature or bad_signature flag on
  277. * <b>voter</b>, and return 0. */
  278. int
  279. networkstatus_check_document_signature(const networkstatus_t *consensus,
  280. document_signature_t *sig,
  281. const authority_cert_t *cert)
  282. {
  283. char key_digest[DIGEST_LEN];
  284. const int dlen = sig->alg == DIGEST_SHA1 ? DIGEST_LEN : DIGEST256_LEN;
  285. char *signed_digest;
  286. size_t signed_digest_len;
  287. if (crypto_pk_get_digest(cert->signing_key, key_digest)<0)
  288. return -1;
  289. if (tor_memneq(sig->signing_key_digest, key_digest, DIGEST_LEN) ||
  290. tor_memneq(sig->identity_digest, cert->cache_info.identity_digest,
  291. DIGEST_LEN))
  292. return -1;
  293. if (authority_cert_is_blacklisted(cert)) {
  294. /* We implement blacklisting for authority signing keys by treating
  295. * all their signatures as always bad. That way we don't get into
  296. * crazy loops of dropping and re-fetching signatures. */
  297. log_warn(LD_DIR, "Ignoring a consensus signature made with deprecated"
  298. " signing key %s",
  299. hex_str(cert->signing_key_digest, DIGEST_LEN));
  300. sig->bad_signature = 1;
  301. return 0;
  302. }
  303. signed_digest_len = crypto_pk_keysize(cert->signing_key);
  304. signed_digest = tor_malloc(signed_digest_len);
  305. if (crypto_pk_public_checksig(cert->signing_key,
  306. signed_digest,
  307. signed_digest_len,
  308. sig->signature,
  309. sig->signature_len) < dlen ||
  310. tor_memneq(signed_digest, consensus->digests.d[sig->alg], dlen)) {
  311. log_warn(LD_DIR, "Got a bad signature on a networkstatus vote");
  312. sig->bad_signature = 1;
  313. } else {
  314. sig->good_signature = 1;
  315. }
  316. tor_free(signed_digest);
  317. return 0;
  318. }
  319. /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
  320. * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
  321. * signature from every recognized authority on it, 0 if there are
  322. * enough good signatures from recognized authorities on it, -1 if we might
  323. * get enough good signatures by fetching missing certificates, and -2
  324. * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
  325. * about every problem; if warn is at least 1, warn only if we can't get
  326. * enough signatures; if warn is negative, log nothing at all. */
  327. int
  328. networkstatus_check_consensus_signature(networkstatus_t *consensus,
  329. int warn)
  330. {
  331. int n_good = 0;
  332. int n_missing_key = 0, n_dl_failed_key = 0;
  333. int n_bad = 0;
  334. int n_unknown = 0;
  335. int n_no_signature = 0;
  336. int n_v3_authorities = get_n_authorities(V3_DIRINFO);
  337. int n_required = n_v3_authorities/2 + 1;
  338. smartlist_t *list_good = smartlist_new();
  339. smartlist_t *list_no_signature = smartlist_new();
  340. smartlist_t *need_certs_from = smartlist_new();
  341. smartlist_t *unrecognized = smartlist_new();
  342. smartlist_t *missing_authorities = smartlist_new();
  343. int severity;
  344. time_t now = time(NULL);
  345. tor_assert(consensus->type == NS_TYPE_CONSENSUS);
  346. SMARTLIST_FOREACH_BEGIN(consensus->voters, networkstatus_voter_info_t *,
  347. voter) {
  348. int good_here = 0;
  349. int bad_here = 0;
  350. int unknown_here = 0;
  351. int missing_key_here = 0, dl_failed_key_here = 0;
  352. SMARTLIST_FOREACH_BEGIN(voter->sigs, document_signature_t *, sig) {
  353. if (!sig->good_signature && !sig->bad_signature &&
  354. sig->signature) {
  355. /* we can try to check the signature. */
  356. int is_v3_auth = trusteddirserver_get_by_v3_auth_digest(
  357. sig->identity_digest) != NULL;
  358. authority_cert_t *cert =
  359. authority_cert_get_by_digests(sig->identity_digest,
  360. sig->signing_key_digest);
  361. tor_assert(tor_memeq(sig->identity_digest, voter->identity_digest,
  362. DIGEST_LEN));
  363. if (!is_v3_auth) {
  364. smartlist_add(unrecognized, voter);
  365. ++unknown_here;
  366. continue;
  367. } else if (!cert || cert->expires < now) {
  368. smartlist_add(need_certs_from, voter);
  369. ++missing_key_here;
  370. if (authority_cert_dl_looks_uncertain(sig->identity_digest))
  371. ++dl_failed_key_here;
  372. continue;
  373. }
  374. if (networkstatus_check_document_signature(consensus, sig, cert) < 0) {
  375. smartlist_add(need_certs_from, voter);
  376. ++missing_key_here;
  377. if (authority_cert_dl_looks_uncertain(sig->identity_digest))
  378. ++dl_failed_key_here;
  379. continue;
  380. }
  381. }
  382. if (sig->good_signature)
  383. ++good_here;
  384. else if (sig->bad_signature)
  385. ++bad_here;
  386. } SMARTLIST_FOREACH_END(sig);
  387. if (good_here) {
  388. ++n_good;
  389. smartlist_add(list_good, voter->nickname);
  390. } else if (bad_here) {
  391. ++n_bad;
  392. } else if (missing_key_here) {
  393. ++n_missing_key;
  394. if (dl_failed_key_here)
  395. ++n_dl_failed_key;
  396. } else if (unknown_here) {
  397. ++n_unknown;
  398. } else {
  399. ++n_no_signature;
  400. smartlist_add(list_no_signature, voter->nickname);
  401. }
  402. } SMARTLIST_FOREACH_END(voter);
  403. /* Now see whether we're missing any voters entirely. */
  404. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  405. dir_server_t *, ds,
  406. {
  407. if ((ds->type & V3_DIRINFO) &&
  408. !networkstatus_get_voter_by_id(consensus, ds->v3_identity_digest))
  409. smartlist_add(missing_authorities, ds);
  410. });
  411. if (warn > 1 || (warn >= 0 &&
  412. (n_good + n_missing_key - n_dl_failed_key < n_required))) {
  413. severity = LOG_WARN;
  414. } else {
  415. severity = LOG_INFO;
  416. }
  417. if (warn >= 0) {
  418. SMARTLIST_FOREACH(unrecognized, networkstatus_voter_info_t *, voter,
  419. {
  420. tor_log(severity, LD_DIR, "Consensus includes unrecognized authority "
  421. "'%s' at %s:%d (contact %s; identity %s)",
  422. voter->nickname, voter->address, (int)voter->dir_port,
  423. voter->contact?voter->contact:"n/a",
  424. hex_str(voter->identity_digest, DIGEST_LEN));
  425. });
  426. SMARTLIST_FOREACH(need_certs_from, networkstatus_voter_info_t *, voter,
  427. {
  428. tor_log(severity, LD_DIR, "Looks like we need to download a new "
  429. "certificate from authority '%s' at %s:%d (contact %s; "
  430. "identity %s)",
  431. voter->nickname, voter->address, (int)voter->dir_port,
  432. voter->contact?voter->contact:"n/a",
  433. hex_str(voter->identity_digest, DIGEST_LEN));
  434. });
  435. SMARTLIST_FOREACH(missing_authorities, dir_server_t *, ds,
  436. {
  437. tor_log(severity, LD_DIR, "Consensus does not include configured "
  438. "authority '%s' at %s:%d (identity %s)",
  439. ds->nickname, ds->address, (int)ds->dir_port,
  440. hex_str(ds->v3_identity_digest, DIGEST_LEN));
  441. });
  442. {
  443. char *joined;
  444. smartlist_t *sl = smartlist_new();
  445. char *tmp = smartlist_join_strings(list_good, " ", 0, NULL);
  446. smartlist_add_asprintf(sl,
  447. "A consensus needs %d good signatures from recognized "
  448. "authorities for us to accept it. This one has %d (%s).",
  449. n_required, n_good, tmp);
  450. tor_free(tmp);
  451. if (n_no_signature) {
  452. tmp = smartlist_join_strings(list_no_signature, " ", 0, NULL);
  453. smartlist_add_asprintf(sl,
  454. "%d (%s) of the authorities we know didn't sign it.",
  455. n_no_signature, tmp);
  456. tor_free(tmp);
  457. }
  458. if (n_unknown) {
  459. smartlist_add_asprintf(sl,
  460. "It has %d signatures from authorities we don't "
  461. "recognize.", n_unknown);
  462. }
  463. if (n_bad) {
  464. smartlist_add_asprintf(sl, "%d of the signatures on it didn't verify "
  465. "correctly.", n_bad);
  466. }
  467. if (n_missing_key) {
  468. smartlist_add_asprintf(sl,
  469. "We were unable to check %d of the signatures, "
  470. "because we were missing the keys.", n_missing_key);
  471. }
  472. joined = smartlist_join_strings(sl, " ", 0, NULL);
  473. tor_log(severity, LD_DIR, "%s", joined);
  474. tor_free(joined);
  475. SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
  476. smartlist_free(sl);
  477. }
  478. }
  479. smartlist_free(list_good);
  480. smartlist_free(list_no_signature);
  481. smartlist_free(unrecognized);
  482. smartlist_free(need_certs_from);
  483. smartlist_free(missing_authorities);
  484. if (n_good == n_v3_authorities)
  485. return 1;
  486. else if (n_good >= n_required)
  487. return 0;
  488. else if (n_good + n_missing_key >= n_required)
  489. return -1;
  490. else
  491. return -2;
  492. }
  493. /** How far in the future do we allow a network-status to get before removing
  494. * it? (seconds) */
  495. #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
  496. /** Helper for bsearching a list of routerstatus_t pointers: compare a
  497. * digest in the key to the identity digest of a routerstatus_t. */
  498. int
  499. compare_digest_to_routerstatus_entry(const void *_key, const void **_member)
  500. {
  501. const char *key = _key;
  502. const routerstatus_t *rs = *_member;
  503. return tor_memcmp(key, rs->identity_digest, DIGEST_LEN);
  504. }
  505. /** Helper for bsearching a list of routerstatus_t pointers: compare a
  506. * digest in the key to the identity digest of a routerstatus_t. */
  507. int
  508. compare_digest_to_vote_routerstatus_entry(const void *_key,
  509. const void **_member)
  510. {
  511. const char *key = _key;
  512. const vote_routerstatus_t *vrs = *_member;
  513. return tor_memcmp(key, vrs->status.identity_digest, DIGEST_LEN);
  514. }
  515. /** As networkstatus_find_entry, but do not return a const pointer */
  516. routerstatus_t *
  517. networkstatus_vote_find_mutable_entry(networkstatus_t *ns, const char *digest)
  518. {
  519. return smartlist_bsearch(ns->routerstatus_list, digest,
  520. compare_digest_to_routerstatus_entry);
  521. }
  522. /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
  523. * NULL if none was found. */
  524. const routerstatus_t *
  525. networkstatus_vote_find_entry(networkstatus_t *ns, const char *digest)
  526. {
  527. return networkstatus_vote_find_mutable_entry(ns, digest);
  528. }
  529. /*XXXX MOVE make this static once functions are moved into this file. */
  530. /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
  531. * <b>digest</b>. Return value and set *<b>found_out</b> as for
  532. * smartlist_bsearch_idx(). */
  533. int
  534. networkstatus_vote_find_entry_idx(networkstatus_t *ns,
  535. const char *digest, int *found_out)
  536. {
  537. return smartlist_bsearch_idx(ns->routerstatus_list, digest,
  538. compare_digest_to_routerstatus_entry,
  539. found_out);
  540. }
  541. /** As router_get_consensus_status_by_descriptor_digest, but does not return
  542. * a const pointer. */
  543. MOCK_IMPL(routerstatus_t *,
  544. router_get_mutable_consensus_status_by_descriptor_digest,(
  545. networkstatus_t *consensus,
  546. const char *digest))
  547. {
  548. if (!consensus)
  549. consensus = current_consensus;
  550. if (!consensus)
  551. return NULL;
  552. if (!consensus->desc_digest_map) {
  553. digestmap_t *m = consensus->desc_digest_map = digestmap_new();
  554. SMARTLIST_FOREACH(consensus->routerstatus_list,
  555. routerstatus_t *, rs,
  556. {
  557. digestmap_set(m, rs->descriptor_digest, rs);
  558. });
  559. }
  560. return digestmap_get(consensus->desc_digest_map, digest);
  561. }
  562. /** Return the consensus view of the status of the router whose current
  563. * <i>descriptor</i> digest in <b>consensus</b> is <b>digest</b>, or NULL if
  564. * no such router is known. */
  565. const routerstatus_t *
  566. router_get_consensus_status_by_descriptor_digest(networkstatus_t *consensus,
  567. const char *digest)
  568. {
  569. return router_get_mutable_consensus_status_by_descriptor_digest(
  570. consensus, digest);
  571. }
  572. /** Given the digest of a router descriptor, return its current download
  573. * status, or NULL if the digest is unrecognized. */
  574. MOCK_IMPL(download_status_t *,
  575. router_get_dl_status_by_descriptor_digest,(const char *d))
  576. {
  577. routerstatus_t *rs;
  578. if (!current_ns_consensus)
  579. return NULL;
  580. if ((rs = router_get_mutable_consensus_status_by_descriptor_digest(
  581. current_ns_consensus, d)))
  582. return &rs->dl_status;
  583. return NULL;
  584. }
  585. /** As router_get_consensus_status_by_id, but do not return a const pointer */
  586. routerstatus_t *
  587. router_get_mutable_consensus_status_by_id(const char *digest)
  588. {
  589. if (!current_consensus)
  590. return NULL;
  591. return smartlist_bsearch(current_consensus->routerstatus_list, digest,
  592. compare_digest_to_routerstatus_entry);
  593. }
  594. /** Return the consensus view of the status of the router whose identity
  595. * digest is <b>digest</b>, or NULL if we don't know about any such router. */
  596. const routerstatus_t *
  597. router_get_consensus_status_by_id(const char *digest)
  598. {
  599. return router_get_mutable_consensus_status_by_id(digest);
  600. }
  601. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  602. * the corresponding routerstatus_t, or NULL if none exists. Warn the
  603. * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
  604. * nickname, but the Named flag isn't set for that router. */
  605. const routerstatus_t *
  606. router_get_consensus_status_by_nickname(const char *nickname,
  607. int warn_if_unnamed)
  608. {
  609. const node_t *node = node_get_by_nickname(nickname, warn_if_unnamed);
  610. if (node)
  611. return node->rs;
  612. else
  613. return NULL;
  614. }
  615. /** Return the identity digest that's mapped to officially by
  616. * <b>nickname</b>. */
  617. const char *
  618. networkstatus_get_router_digest_by_nickname(const char *nickname)
  619. {
  620. if (!named_server_map)
  621. return NULL;
  622. return strmap_get_lc(named_server_map, nickname);
  623. }
  624. /** Return true iff <b>nickname</b> is disallowed from being the nickname
  625. * of any server. */
  626. int
  627. networkstatus_nickname_is_unnamed(const char *nickname)
  628. {
  629. if (!unnamed_server_map)
  630. return 0;
  631. return strmap_get_lc(unnamed_server_map, nickname) != NULL;
  632. }
  633. /** How frequently do directory authorities re-download fresh networkstatus
  634. * documents? */
  635. #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
  636. /** How frequently do non-authority directory caches re-download fresh
  637. * networkstatus documents? */
  638. #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
  639. /** Return true iff, given the options listed in <b>options</b>, <b>flavor</b>
  640. * is the flavor of a consensus networkstatus that we would like to fetch. */
  641. static int
  642. we_want_to_fetch_flavor(const or_options_t *options, int flavor)
  643. {
  644. if (flavor < 0 || flavor > N_CONSENSUS_FLAVORS) {
  645. /* This flavor is crazy; we don't want it */
  646. /*XXXX handle unrecognized flavors later */
  647. return 0;
  648. }
  649. if (authdir_mode_v3(options) || directory_caches_dir_info(options)) {
  650. /* We want to serve all flavors to others, regardless if we would use
  651. * it ourselves. */
  652. return 1;
  653. }
  654. if (options->FetchUselessDescriptors) {
  655. /* In order to get all descriptors, we need to fetch all consensuses. */
  656. return 1;
  657. }
  658. /* Otherwise, we want the flavor only if we want to use it to build
  659. * circuits. */
  660. return flavor == usable_consensus_flavor();
  661. }
  662. /** How long will we hang onto a possibly live consensus for which we're
  663. * fetching certs before we check whether there is a better one? */
  664. #define DELAY_WHILE_FETCHING_CERTS (20*60)
  665. /** If we want to download a fresh consensus, launch a new download as
  666. * appropriate. */
  667. static void
  668. update_consensus_networkstatus_downloads(time_t now)
  669. {
  670. int i;
  671. const or_options_t *options = get_options();
  672. for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
  673. /* XXXX need some way to download unknown flavors if we are caching. */
  674. const char *resource;
  675. consensus_waiting_for_certs_t *waiting;
  676. networkstatus_t *c;
  677. if (! we_want_to_fetch_flavor(options, i))
  678. continue;
  679. c = networkstatus_get_latest_consensus_by_flavor(i);
  680. if (! (c && c->valid_after <= now && now <= c->valid_until)) {
  681. /* No live consensus? Get one now!*/
  682. time_to_download_next_consensus[i] = now;
  683. }
  684. if (time_to_download_next_consensus[i] > now)
  685. continue; /* Wait until the current consensus is older. */
  686. resource = networkstatus_get_flavor_name(i);
  687. /* Let's make sure we remembered to update consensus_dl_status */
  688. tor_assert(consensus_dl_status[i].schedule == DL_SCHED_CONSENSUS);
  689. if (!download_status_is_ready(&consensus_dl_status[i], now,
  690. options->TestingConsensusMaxDownloadTries))
  691. continue; /* We failed downloading a consensus too recently. */
  692. if (connection_dir_get_by_purpose_and_resource(
  693. DIR_PURPOSE_FETCH_CONSENSUS, resource))
  694. continue; /* There's an in-progress download.*/
  695. waiting = &consensus_waiting_for_certs[i];
  696. if (waiting->consensus) {
  697. /* XXXX make sure this doesn't delay sane downloads. */
  698. if (waiting->set_at + DELAY_WHILE_FETCHING_CERTS > now) {
  699. continue; /* We're still getting certs for this one. */
  700. } else {
  701. if (!waiting->dl_failed) {
  702. download_status_failed(&consensus_dl_status[i], 0);
  703. waiting->dl_failed=1;
  704. }
  705. }
  706. }
  707. log_info(LD_DIR, "Launching %s networkstatus consensus download.",
  708. networkstatus_get_flavor_name(i));
  709. directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS,
  710. ROUTER_PURPOSE_GENERAL, resource,
  711. PDS_RETRY_IF_NO_SERVERS);
  712. }
  713. }
  714. /** Called when an attempt to download a consensus fails: note that the
  715. * failure occurred, and possibly retry. */
  716. void
  717. networkstatus_consensus_download_failed(int status_code, const char *flavname)
  718. {
  719. int flav = networkstatus_parse_flavor_name(flavname);
  720. if (flav >= 0) {
  721. tor_assert(flav < N_CONSENSUS_FLAVORS);
  722. /* XXXX handle unrecognized flavors */
  723. download_status_failed(&consensus_dl_status[flav], status_code);
  724. /* Retry immediately, if appropriate. */
  725. update_consensus_networkstatus_downloads(time(NULL));
  726. }
  727. }
  728. /** How long do we (as a cache) wait after a consensus becomes non-fresh
  729. * before trying to fetch another? */
  730. #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
  731. /** Update the time at which we'll consider replacing the current
  732. * consensus of flavor <b>flav</b> */
  733. static void
  734. update_consensus_networkstatus_fetch_time_impl(time_t now, int flav)
  735. {
  736. const or_options_t *options = get_options();
  737. networkstatus_t *c = networkstatus_get_latest_consensus_by_flavor(flav);
  738. const char *flavor = networkstatus_get_flavor_name(flav);
  739. if (! we_want_to_fetch_flavor(get_options(), flav))
  740. return;
  741. if (c && c->valid_after <= now && now <= c->valid_until) {
  742. long dl_interval;
  743. long interval = c->fresh_until - c->valid_after;
  744. long min_sec_before_caching = CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
  745. time_t start;
  746. if (min_sec_before_caching > interval/16) {
  747. /* Usually we allow 2-minutes slop factor in case clocks get
  748. desynchronized a little. If we're on a private network with
  749. a crazy-fast voting interval, though, 2 minutes may be too
  750. much. */
  751. min_sec_before_caching = interval/16;
  752. /* make sure we always delay by at least a second before caching */
  753. if (min_sec_before_caching == 0) {
  754. min_sec_before_caching = 1;
  755. }
  756. }
  757. if (directory_fetches_dir_info_early(options)) {
  758. /* We want to cache the next one at some point after this one
  759. * is no longer fresh... */
  760. start = (time_t)(c->fresh_until + min_sec_before_caching);
  761. /* Some clients may need the consensus sooner than others. */
  762. if (options->FetchDirInfoExtraEarly || authdir_mode_v3(options)) {
  763. dl_interval = 60;
  764. if (min_sec_before_caching + dl_interval > interval)
  765. dl_interval = interval/2;
  766. } else {
  767. /* But only in the first half-interval after that. */
  768. dl_interval = interval/2;
  769. }
  770. } else {
  771. /* We're an ordinary client or a bridge. Give all the caches enough
  772. * time to download the consensus. */
  773. start = (time_t)(c->fresh_until + (interval*3)/4);
  774. /* But download the next one well before this one is expired. */
  775. dl_interval = ((c->valid_until - start) * 7 )/ 8;
  776. /* If we're a bridge user, make use of the numbers we just computed
  777. * to choose the rest of the interval *after* them. */
  778. if (directory_fetches_dir_info_later(options)) {
  779. /* Give all the *clients* enough time to download the consensus. */
  780. start = (time_t)(start + dl_interval + min_sec_before_caching);
  781. /* But try to get it before ours actually expires. */
  782. dl_interval = (c->valid_until - start) - min_sec_before_caching;
  783. }
  784. }
  785. /* catch low dl_interval in crazy-fast networks */
  786. if (dl_interval < 1)
  787. dl_interval = 1;
  788. /* catch late start in crazy-fast networks */
  789. if (start+dl_interval >= c->valid_until)
  790. start = c->valid_until - dl_interval - 1;
  791. log_debug(LD_DIR,
  792. "fresh_until: %ld start: %ld "
  793. "dl_interval: %ld valid_until: %ld ",
  794. (long)c->fresh_until, (long)start, dl_interval,
  795. (long)c->valid_until);
  796. /* We must not try to replace c while it's still fresh: */
  797. tor_assert(c->fresh_until < start);
  798. /* We must download the next one before c is invalid: */
  799. tor_assert(start+dl_interval < c->valid_until);
  800. time_to_download_next_consensus[flav] =
  801. start + crypto_rand_int((int)dl_interval);
  802. {
  803. char tbuf1[ISO_TIME_LEN+1];
  804. char tbuf2[ISO_TIME_LEN+1];
  805. char tbuf3[ISO_TIME_LEN+1];
  806. format_local_iso_time(tbuf1, c->fresh_until);
  807. format_local_iso_time(tbuf2, c->valid_until);
  808. format_local_iso_time(tbuf3, time_to_download_next_consensus[flav]);
  809. log_info(LD_DIR, "Live %s consensus %s the most recent until %s and "
  810. "will expire at %s; fetching the next one at %s.",
  811. flavor, (c->fresh_until > now) ? "will be" : "was",
  812. tbuf1, tbuf2, tbuf3);
  813. }
  814. } else {
  815. time_to_download_next_consensus[flav] = now;
  816. log_info(LD_DIR, "No live %s consensus; we should fetch one immediately.",
  817. flavor);
  818. }
  819. }
  820. /** Update the time at which we'll consider replacing the current
  821. * consensus of flavor 'flavor' */
  822. void
  823. update_consensus_networkstatus_fetch_time(time_t now)
  824. {
  825. int i;
  826. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  827. if (we_want_to_fetch_flavor(get_options(), i))
  828. update_consensus_networkstatus_fetch_time_impl(now, i);
  829. }
  830. }
  831. /** Return 1 if there's a reason we shouldn't try any directory
  832. * fetches yet (e.g. we demand bridges and none are yet known).
  833. * Else return 0.
  834. * If we return 1 and <b>msg_out</b> is provided, set <b>msg_out</b>
  835. * to an explanation of why directory fetches are delayed. (If we
  836. * return 0, we set msg_out to NULL.)
  837. */
  838. int
  839. should_delay_dir_fetches(const or_options_t *options, const char **msg_out)
  840. {
  841. if (msg_out) {
  842. *msg_out = NULL;
  843. }
  844. if (options->DisableNetwork) {
  845. if (msg_out) {
  846. *msg_out = "DisableNetwork is set.";
  847. }
  848. log_info(LD_DIR, "Delaying dir fetches (DisableNetwork is set)");
  849. return 1;
  850. }
  851. if (options->UseBridges) {
  852. if (!any_bridge_descriptors_known()) {
  853. if (msg_out) {
  854. *msg_out = "No running bridges";
  855. }
  856. log_info(LD_DIR, "Delaying dir fetches (no running bridges known)");
  857. return 1;
  858. }
  859. if (pt_proxies_configuration_pending()) {
  860. if (msg_out) {
  861. *msg_out = "Pluggable transport proxies still configuring";
  862. }
  863. log_info(LD_DIR, "Delaying dir fetches (pt proxies still configuring)");
  864. return 1;
  865. }
  866. }
  867. return 0;
  868. }
  869. /** Launch requests for networkstatus documents and authority certificates as
  870. * appropriate. */
  871. void
  872. update_networkstatus_downloads(time_t now)
  873. {
  874. const or_options_t *options = get_options();
  875. if (should_delay_dir_fetches(options, NULL))
  876. return;
  877. update_consensus_networkstatus_downloads(now);
  878. update_certificate_downloads(now);
  879. }
  880. /** Launch requests as appropriate for missing directory authority
  881. * certificates. */
  882. void
  883. update_certificate_downloads(time_t now)
  884. {
  885. int i;
  886. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  887. if (consensus_waiting_for_certs[i].consensus)
  888. authority_certs_fetch_missing(consensus_waiting_for_certs[i].consensus,
  889. now);
  890. }
  891. if (current_ns_consensus)
  892. authority_certs_fetch_missing(current_ns_consensus, now);
  893. if (current_md_consensus)
  894. authority_certs_fetch_missing(current_md_consensus, now);
  895. }
  896. /** Return 1 if we have a consensus but we don't have enough certificates
  897. * to start using it yet. */
  898. int
  899. consensus_is_waiting_for_certs(void)
  900. {
  901. return consensus_waiting_for_certs[usable_consensus_flavor()].consensus
  902. ? 1 : 0;
  903. }
  904. /** Return the most recent consensus that we have downloaded, or NULL if we
  905. * don't have one. */
  906. networkstatus_t *
  907. networkstatus_get_latest_consensus(void)
  908. {
  909. return current_consensus;
  910. }
  911. /** Return the latest consensus we have whose flavor matches <b>f</b>, or NULL
  912. * if we don't have one. */
  913. MOCK_IMPL(networkstatus_t *,
  914. networkstatus_get_latest_consensus_by_flavor,(consensus_flavor_t f))
  915. {
  916. if (f == FLAV_NS)
  917. return current_ns_consensus;
  918. else if (f == FLAV_MICRODESC)
  919. return current_md_consensus;
  920. else {
  921. tor_assert(0);
  922. return NULL;
  923. }
  924. }
  925. /** Return the most recent consensus that we have downloaded, or NULL if it is
  926. * no longer live. */
  927. networkstatus_t *
  928. networkstatus_get_live_consensus(time_t now)
  929. {
  930. if (current_consensus &&
  931. current_consensus->valid_after <= now &&
  932. now <= current_consensus->valid_until)
  933. return current_consensus;
  934. else
  935. return NULL;
  936. }
  937. /* XXXX remove this in favor of get_live_consensus. But actually,
  938. * leave something like it for bridge users, who need to not totally
  939. * lose if they spend a while fetching a new consensus. */
  940. /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
  941. * consensuses. */
  942. networkstatus_t *
  943. networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
  944. {
  945. #define REASONABLY_LIVE_TIME (24*60*60)
  946. networkstatus_t *consensus =
  947. networkstatus_get_latest_consensus_by_flavor(flavor);
  948. if (consensus &&
  949. consensus->valid_after <= now &&
  950. now <= consensus->valid_until+REASONABLY_LIVE_TIME)
  951. return consensus;
  952. else
  953. return NULL;
  954. }
  955. /** Given two router status entries for the same router identity, return 1 if
  956. * if the contents have changed between them. Otherwise, return 0. */
  957. static int
  958. routerstatus_has_changed(const routerstatus_t *a, const routerstatus_t *b)
  959. {
  960. tor_assert(tor_memeq(a->identity_digest, b->identity_digest, DIGEST_LEN));
  961. return strcmp(a->nickname, b->nickname) ||
  962. fast_memneq(a->descriptor_digest, b->descriptor_digest, DIGEST_LEN) ||
  963. a->addr != b->addr ||
  964. a->or_port != b->or_port ||
  965. a->dir_port != b->dir_port ||
  966. a->is_authority != b->is_authority ||
  967. a->is_exit != b->is_exit ||
  968. a->is_stable != b->is_stable ||
  969. a->is_fast != b->is_fast ||
  970. a->is_flagged_running != b->is_flagged_running ||
  971. a->is_named != b->is_named ||
  972. a->is_unnamed != b->is_unnamed ||
  973. a->is_valid != b->is_valid ||
  974. a->is_possible_guard != b->is_possible_guard ||
  975. a->is_bad_exit != b->is_bad_exit ||
  976. a->is_hs_dir != b->is_hs_dir ||
  977. a->version_known != b->version_known;
  978. }
  979. /** Notify controllers of any router status entries that changed between
  980. * <b>old_c</b> and <b>new_c</b>. */
  981. static void
  982. notify_control_networkstatus_changed(const networkstatus_t *old_c,
  983. const networkstatus_t *new_c)
  984. {
  985. smartlist_t *changed;
  986. if (old_c == new_c)
  987. return;
  988. /* tell the controller exactly which relays are still listed, as well
  989. * as what they're listed as */
  990. control_event_newconsensus(new_c);
  991. if (!control_event_is_interesting(EVENT_NS))
  992. return;
  993. if (!old_c) {
  994. control_event_networkstatus_changed(new_c->routerstatus_list);
  995. return;
  996. }
  997. changed = smartlist_new();
  998. SMARTLIST_FOREACH_JOIN(
  999. old_c->routerstatus_list, const routerstatus_t *, rs_old,
  1000. new_c->routerstatus_list, const routerstatus_t *, rs_new,
  1001. tor_memcmp(rs_old->identity_digest,
  1002. rs_new->identity_digest, DIGEST_LEN),
  1003. smartlist_add(changed, (void*) rs_new)) {
  1004. if (routerstatus_has_changed(rs_old, rs_new))
  1005. smartlist_add(changed, (void*)rs_new);
  1006. } SMARTLIST_FOREACH_JOIN_END(rs_old, rs_new);
  1007. control_event_networkstatus_changed(changed);
  1008. smartlist_free(changed);
  1009. }
  1010. /** Copy all the ancillary information (like router download status and so on)
  1011. * from <b>old_c</b> to <b>new_c</b>. */
  1012. static void
  1013. networkstatus_copy_old_consensus_info(networkstatus_t *new_c,
  1014. const networkstatus_t *old_c)
  1015. {
  1016. if (old_c == new_c)
  1017. return;
  1018. if (!old_c || !smartlist_len(old_c->routerstatus_list))
  1019. return;
  1020. SMARTLIST_FOREACH_JOIN(old_c->routerstatus_list, routerstatus_t *, rs_old,
  1021. new_c->routerstatus_list, routerstatus_t *, rs_new,
  1022. tor_memcmp(rs_old->identity_digest,
  1023. rs_new->identity_digest, DIGEST_LEN),
  1024. STMT_NIL) {
  1025. /* Okay, so we're looking at the same identity. */
  1026. rs_new->last_dir_503_at = rs_old->last_dir_503_at;
  1027. if (tor_memeq(rs_old->descriptor_digest, rs_new->descriptor_digest,
  1028. DIGEST256_LEN)) {
  1029. /* And the same descriptor too! */
  1030. memcpy(&rs_new->dl_status, &rs_old->dl_status,sizeof(download_status_t));
  1031. }
  1032. } SMARTLIST_FOREACH_JOIN_END(rs_old, rs_new);
  1033. }
  1034. /** Try to replace the current cached v3 networkstatus with the one in
  1035. * <b>consensus</b>. If we don't have enough certificates to validate it,
  1036. * store it in consensus_waiting_for_certs and launch a certificate fetch.
  1037. *
  1038. * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
  1039. * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
  1040. * already received, but we were waiting for certificates on it. If flags &
  1041. * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
  1042. * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
  1043. * consensus, even if it comes from many days in the past.
  1044. *
  1045. * Return 0 on success, <0 on failure. On failure, caller should increment
  1046. * the failure count as appropriate.
  1047. *
  1048. * We return -1 for mild failures that don't need to be reported to the
  1049. * user, and -2 for more serious problems.
  1050. */
  1051. int
  1052. networkstatus_set_current_consensus(const char *consensus,
  1053. const char *flavor,
  1054. unsigned flags)
  1055. {
  1056. networkstatus_t *c=NULL;
  1057. int r, result = -1;
  1058. time_t now = time(NULL);
  1059. const or_options_t *options = get_options();
  1060. char *unverified_fname = NULL, *consensus_fname = NULL;
  1061. int flav = networkstatus_parse_flavor_name(flavor);
  1062. const unsigned from_cache = flags & NSSET_FROM_CACHE;
  1063. const unsigned was_waiting_for_certs = flags & NSSET_WAS_WAITING_FOR_CERTS;
  1064. const unsigned dl_certs = !(flags & NSSET_DONT_DOWNLOAD_CERTS);
  1065. const unsigned accept_obsolete = flags & NSSET_ACCEPT_OBSOLETE;
  1066. const unsigned require_flavor = flags & NSSET_REQUIRE_FLAVOR;
  1067. const digests_t *current_digests = NULL;
  1068. consensus_waiting_for_certs_t *waiting = NULL;
  1069. time_t current_valid_after = 0;
  1070. int free_consensus = 1; /* Free 'c' at the end of the function */
  1071. int old_ewma_enabled;
  1072. if (flav < 0) {
  1073. /* XXXX we don't handle unrecognized flavors yet. */
  1074. log_warn(LD_BUG, "Unrecognized consensus flavor %s", flavor);
  1075. return -2;
  1076. }
  1077. /* Make sure it's parseable. */
  1078. c = networkstatus_parse_vote_from_string(consensus, NULL, NS_TYPE_CONSENSUS);
  1079. if (!c) {
  1080. log_warn(LD_DIR, "Unable to parse networkstatus consensus");
  1081. result = -2;
  1082. goto done;
  1083. }
  1084. if ((int)c->flavor != flav) {
  1085. /* This wasn't the flavor we thought we were getting. */
  1086. if (require_flavor) {
  1087. log_warn(LD_DIR, "Got consensus with unexpected flavor %s (wanted %s)",
  1088. networkstatus_get_flavor_name(c->flavor), flavor);
  1089. goto done;
  1090. }
  1091. flav = c->flavor;
  1092. flavor = networkstatus_get_flavor_name(flav);
  1093. }
  1094. if (flav != usable_consensus_flavor() &&
  1095. !directory_caches_dir_info(options)) {
  1096. /* This consensus is totally boring to us: we won't use it, and we won't
  1097. * serve it. Drop it. */
  1098. goto done;
  1099. }
  1100. if (from_cache && !accept_obsolete &&
  1101. c->valid_until < now-OLD_ROUTER_DESC_MAX_AGE) {
  1102. log_info(LD_DIR, "Loaded an expired consensus. Discarding.");
  1103. goto done;
  1104. }
  1105. if (!strcmp(flavor, "ns")) {
  1106. consensus_fname = get_datadir_fname("cached-consensus");
  1107. unverified_fname = get_datadir_fname("unverified-consensus");
  1108. if (current_ns_consensus) {
  1109. current_digests = &current_ns_consensus->digests;
  1110. current_valid_after = current_ns_consensus->valid_after;
  1111. }
  1112. } else if (!strcmp(flavor, "microdesc")) {
  1113. consensus_fname = get_datadir_fname("cached-microdesc-consensus");
  1114. unverified_fname = get_datadir_fname("unverified-microdesc-consensus");
  1115. if (current_md_consensus) {
  1116. current_digests = &current_md_consensus->digests;
  1117. current_valid_after = current_md_consensus->valid_after;
  1118. }
  1119. } else {
  1120. cached_dir_t *cur;
  1121. char buf[128];
  1122. tor_snprintf(buf, sizeof(buf), "cached-%s-consensus", flavor);
  1123. consensus_fname = get_datadir_fname(buf);
  1124. tor_snprintf(buf, sizeof(buf), "unverified-%s-consensus", flavor);
  1125. unverified_fname = get_datadir_fname(buf);
  1126. cur = dirserv_get_consensus(flavor);
  1127. if (cur) {
  1128. current_digests = &cur->digests;
  1129. current_valid_after = cur->published;
  1130. }
  1131. }
  1132. if (current_digests &&
  1133. tor_memeq(&c->digests, current_digests, sizeof(c->digests))) {
  1134. /* We already have this one. That's a failure. */
  1135. log_info(LD_DIR, "Got a %s consensus we already have", flavor);
  1136. goto done;
  1137. }
  1138. if (current_valid_after && c->valid_after <= current_valid_after) {
  1139. /* We have a newer one. There's no point in accepting this one,
  1140. * even if it's great. */
  1141. log_info(LD_DIR, "Got a %s consensus at least as old as the one we have",
  1142. flavor);
  1143. goto done;
  1144. }
  1145. /* Make sure it's signed enough. */
  1146. if ((r=networkstatus_check_consensus_signature(c, 1))<0) {
  1147. if (r == -1) {
  1148. /* Okay, so it _might_ be signed enough if we get more certificates. */
  1149. if (!was_waiting_for_certs) {
  1150. log_info(LD_DIR,
  1151. "Not enough certificates to check networkstatus consensus");
  1152. }
  1153. if (!current_valid_after ||
  1154. c->valid_after > current_valid_after) {
  1155. waiting = &consensus_waiting_for_certs[flav];
  1156. networkstatus_vote_free(waiting->consensus);
  1157. tor_free(waiting->body);
  1158. waiting->consensus = c;
  1159. free_consensus = 0;
  1160. waiting->body = tor_strdup(consensus);
  1161. waiting->set_at = now;
  1162. waiting->dl_failed = 0;
  1163. if (!from_cache) {
  1164. write_str_to_file(unverified_fname, consensus, 0);
  1165. }
  1166. if (dl_certs)
  1167. authority_certs_fetch_missing(c, now);
  1168. /* This case is not a success or a failure until we get the certs
  1169. * or fail to get the certs. */
  1170. result = 0;
  1171. } else {
  1172. /* Even if we had enough signatures, we'd never use this as the
  1173. * latest consensus. */
  1174. if (was_waiting_for_certs && from_cache)
  1175. if (unlink(unverified_fname) != 0) {
  1176. log_warn(LD_FS,
  1177. "Failed to unlink %s: %s",
  1178. unverified_fname, strerror(errno));
  1179. }
  1180. }
  1181. goto done;
  1182. } else {
  1183. /* This can never be signed enough: Kill it. */
  1184. if (!was_waiting_for_certs) {
  1185. log_warn(LD_DIR, "Not enough good signatures on networkstatus "
  1186. "consensus");
  1187. result = -2;
  1188. }
  1189. if (was_waiting_for_certs && (r < -1) && from_cache) {
  1190. if (unlink(unverified_fname) != 0) {
  1191. log_warn(LD_FS,
  1192. "Failed to unlink %s: %s",
  1193. unverified_fname, strerror(errno));
  1194. }
  1195. }
  1196. goto done;
  1197. }
  1198. }
  1199. if (!from_cache && flav == usable_consensus_flavor())
  1200. control_event_client_status(LOG_NOTICE, "CONSENSUS_ARRIVED");
  1201. /* Are we missing any certificates at all? */
  1202. if (r != 1 && dl_certs)
  1203. authority_certs_fetch_missing(c, now);
  1204. if (flav == usable_consensus_flavor()) {
  1205. notify_control_networkstatus_changed(current_consensus, c);
  1206. }
  1207. if (flav == FLAV_NS) {
  1208. if (current_ns_consensus) {
  1209. networkstatus_copy_old_consensus_info(c, current_ns_consensus);
  1210. networkstatus_vote_free(current_ns_consensus);
  1211. /* Defensive programming : we should set current_consensus very soon,
  1212. * but we're about to call some stuff in the meantime, and leaving this
  1213. * dangling pointer around has proven to be trouble. */
  1214. current_ns_consensus = NULL;
  1215. }
  1216. current_ns_consensus = c;
  1217. free_consensus = 0; /* avoid free */
  1218. } else if (flav == FLAV_MICRODESC) {
  1219. if (current_md_consensus) {
  1220. networkstatus_copy_old_consensus_info(c, current_md_consensus);
  1221. networkstatus_vote_free(current_md_consensus);
  1222. /* more defensive programming */
  1223. current_md_consensus = NULL;
  1224. }
  1225. current_md_consensus = c;
  1226. free_consensus = 0; /* avoid free */
  1227. }
  1228. waiting = &consensus_waiting_for_certs[flav];
  1229. if (waiting->consensus &&
  1230. waiting->consensus->valid_after <= c->valid_after) {
  1231. networkstatus_vote_free(waiting->consensus);
  1232. waiting->consensus = NULL;
  1233. if (consensus != waiting->body)
  1234. tor_free(waiting->body);
  1235. else
  1236. waiting->body = NULL;
  1237. waiting->set_at = 0;
  1238. waiting->dl_failed = 0;
  1239. if (unlink(unverified_fname) != 0) {
  1240. log_warn(LD_FS,
  1241. "Failed to unlink %s: %s",
  1242. unverified_fname, strerror(errno));
  1243. }
  1244. }
  1245. /* Reset the failure count only if this consensus is actually valid. */
  1246. if (c->valid_after <= now && now <= c->valid_until) {
  1247. download_status_reset(&consensus_dl_status[flav]);
  1248. } else {
  1249. if (!from_cache)
  1250. download_status_failed(&consensus_dl_status[flav], 0);
  1251. }
  1252. if (flav == usable_consensus_flavor()) {
  1253. /* XXXXNM Microdescs: needs a non-ns variant. ???? NM*/
  1254. update_consensus_networkstatus_fetch_time(now);
  1255. nodelist_set_consensus(current_consensus);
  1256. dirvote_recalculate_timing(options, now);
  1257. routerstatus_list_update_named_server_map();
  1258. /* Update ewma and adjust policy if needed; first cache the old value */
  1259. old_ewma_enabled = cell_ewma_enabled();
  1260. /* Change the cell EWMA settings */
  1261. cell_ewma_set_scale_factor(options, networkstatus_get_latest_consensus());
  1262. /* If we just enabled ewma, set the cmux policy on all active channels */
  1263. if (cell_ewma_enabled() && !old_ewma_enabled) {
  1264. channel_set_cmux_policy_everywhere(&ewma_policy);
  1265. } else if (!cell_ewma_enabled() && old_ewma_enabled) {
  1266. /* Turn it off everywhere */
  1267. channel_set_cmux_policy_everywhere(NULL);
  1268. }
  1269. /* XXXX024 this call might be unnecessary here: can changing the
  1270. * current consensus really alter our view of any OR's rate limits? */
  1271. connection_or_update_token_buckets(get_connection_array(), options);
  1272. circuit_build_times_new_consensus_params(get_circuit_build_times_mutable(),
  1273. current_consensus);
  1274. }
  1275. if (directory_caches_dir_info(options)) {
  1276. dirserv_set_cached_consensus_networkstatus(consensus,
  1277. flavor,
  1278. &c->digests,
  1279. c->valid_after);
  1280. }
  1281. if (!from_cache) {
  1282. write_str_to_file(consensus_fname, consensus, 0);
  1283. }
  1284. /** If a consensus appears more than this many seconds before its declared
  1285. * valid-after time, declare that our clock is skewed. */
  1286. #define EARLY_CONSENSUS_NOTICE_SKEW 60
  1287. if (now < c->valid_after - EARLY_CONSENSUS_NOTICE_SKEW) {
  1288. char tbuf[ISO_TIME_LEN+1];
  1289. char dbuf[64];
  1290. long delta = now - c->valid_after;
  1291. format_iso_time(tbuf, c->valid_after);
  1292. format_time_interval(dbuf, sizeof(dbuf), delta);
  1293. log_warn(LD_GENERAL, "Our clock is %s behind the time published in the "
  1294. "consensus network status document (%s UTC). Tor needs an "
  1295. "accurate clock to work correctly. Please check your time and "
  1296. "date settings!", dbuf, tbuf);
  1297. control_event_general_status(LOG_WARN,
  1298. "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta);
  1299. }
  1300. router_dir_info_changed();
  1301. result = 0;
  1302. done:
  1303. if (free_consensus)
  1304. networkstatus_vote_free(c);
  1305. tor_free(consensus_fname);
  1306. tor_free(unverified_fname);
  1307. return result;
  1308. }
  1309. /** Called when we have gotten more certificates: see whether we can
  1310. * now verify a pending consensus. */
  1311. void
  1312. networkstatus_note_certs_arrived(void)
  1313. {
  1314. int i;
  1315. for (i=0; i<N_CONSENSUS_FLAVORS; ++i) {
  1316. consensus_waiting_for_certs_t *waiting = &consensus_waiting_for_certs[i];
  1317. if (!waiting->consensus)
  1318. continue;
  1319. if (networkstatus_check_consensus_signature(waiting->consensus, 0)>=0) {
  1320. char *waiting_body = waiting->body;
  1321. if (!networkstatus_set_current_consensus(
  1322. waiting_body,
  1323. networkstatus_get_flavor_name(i),
  1324. NSSET_WAS_WAITING_FOR_CERTS)) {
  1325. tor_free(waiting_body);
  1326. }
  1327. }
  1328. }
  1329. }
  1330. /** If the network-status list has changed since the last time we called this
  1331. * function, update the status of every routerinfo from the network-status
  1332. * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
  1333. * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
  1334. */
  1335. void
  1336. routers_update_all_from_networkstatus(time_t now, int dir_version)
  1337. {
  1338. routerlist_t *rl = router_get_routerlist();
  1339. networkstatus_t *consensus = networkstatus_get_reasonably_live_consensus(now,
  1340. FLAV_NS);
  1341. if (!consensus || dir_version < 3) /* nothing more we should do */
  1342. return;
  1343. /* calls router_dir_info_changed() when it's done -- more routers
  1344. * might be up or down now, which might affect whether there's enough
  1345. * directory info. */
  1346. routers_update_status_from_consensus_networkstatus(rl->routers, 0);
  1347. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
  1348. ri->cache_info.routerlist_index = ri_sl_idx);
  1349. if (rl->old_routers)
  1350. signed_descs_update_status_from_consensus_networkstatus(rl->old_routers);
  1351. if (!have_warned_about_old_version) {
  1352. int is_server = server_mode(get_options());
  1353. version_status_t status;
  1354. const char *recommended = is_server ?
  1355. consensus->server_versions : consensus->client_versions;
  1356. status = tor_version_is_obsolete(VERSION, recommended);
  1357. if (status == VS_RECOMMENDED) {
  1358. log_info(LD_GENERAL, "The directory authorities say my version is ok.");
  1359. } else if (status == VS_EMPTY) {
  1360. log_info(LD_GENERAL,
  1361. "The directory authorities don't recommend any versions.");
  1362. } else if (status == VS_NEW || status == VS_NEW_IN_SERIES) {
  1363. if (!have_warned_about_new_version) {
  1364. log_notice(LD_GENERAL, "This version of Tor (%s) is newer than any "
  1365. "recommended version%s, according to the directory "
  1366. "authorities. Recommended versions are: %s",
  1367. VERSION,
  1368. status == VS_NEW_IN_SERIES ? " in its series" : "",
  1369. recommended);
  1370. have_warned_about_new_version = 1;
  1371. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  1372. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  1373. VERSION, "NEW", recommended);
  1374. }
  1375. } else {
  1376. log_warn(LD_GENERAL, "Please upgrade! "
  1377. "This version of Tor (%s) is %s, according to the directory "
  1378. "authorities. Recommended versions are: %s",
  1379. VERSION,
  1380. status == VS_OLD ? "obsolete" : "not recommended",
  1381. recommended);
  1382. have_warned_about_old_version = 1;
  1383. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  1384. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  1385. VERSION, status == VS_OLD ? "OBSOLETE" : "UNRECOMMENDED",
  1386. recommended);
  1387. }
  1388. }
  1389. }
  1390. /** Update our view of the list of named servers from the most recently
  1391. * retrieved networkstatus consensus. */
  1392. static void
  1393. routerstatus_list_update_named_server_map(void)
  1394. {
  1395. if (!current_consensus)
  1396. return;
  1397. strmap_free(named_server_map, tor_free_);
  1398. named_server_map = strmap_new();
  1399. strmap_free(unnamed_server_map, NULL);
  1400. unnamed_server_map = strmap_new();
  1401. SMARTLIST_FOREACH_BEGIN(current_consensus->routerstatus_list,
  1402. const routerstatus_t *, rs) {
  1403. if (rs->is_named) {
  1404. strmap_set_lc(named_server_map, rs->nickname,
  1405. tor_memdup(rs->identity_digest, DIGEST_LEN));
  1406. }
  1407. if (rs->is_unnamed) {
  1408. strmap_set_lc(unnamed_server_map, rs->nickname, (void*)1);
  1409. }
  1410. } SMARTLIST_FOREACH_END(rs);
  1411. }
  1412. /** Given a list <b>routers</b> of routerinfo_t *, update each status field
  1413. * according to our current consensus networkstatus. May re-order
  1414. * <b>routers</b>. */
  1415. void
  1416. routers_update_status_from_consensus_networkstatus(smartlist_t *routers,
  1417. int reset_failures)
  1418. {
  1419. const or_options_t *options = get_options();
  1420. int authdir = authdir_mode_v3(options);
  1421. networkstatus_t *ns = current_consensus;
  1422. if (!ns || !smartlist_len(ns->routerstatus_list))
  1423. return;
  1424. routers_sort_by_identity(routers);
  1425. SMARTLIST_FOREACH_JOIN(ns->routerstatus_list, routerstatus_t *, rs,
  1426. routers, routerinfo_t *, router,
  1427. tor_memcmp(rs->identity_digest,
  1428. router->cache_info.identity_digest, DIGEST_LEN),
  1429. {
  1430. }) {
  1431. /* Is it the same descriptor, or only the same identity? */
  1432. if (tor_memeq(router->cache_info.signed_descriptor_digest,
  1433. rs->descriptor_digest, DIGEST_LEN)) {
  1434. if (ns->valid_until > router->cache_info.last_listed_as_valid_until)
  1435. router->cache_info.last_listed_as_valid_until = ns->valid_until;
  1436. }
  1437. if (authdir) {
  1438. /* If we _are_ an authority, we should check whether this router
  1439. * is one that will cause us to need a reachability test. */
  1440. routerinfo_t *old_router =
  1441. router_get_mutable_by_digest(router->cache_info.identity_digest);
  1442. if (old_router != router) {
  1443. router->needs_retest_if_added =
  1444. dirserv_should_launch_reachability_test(router, old_router);
  1445. }
  1446. }
  1447. if (reset_failures) {
  1448. download_status_reset(&rs->dl_status);
  1449. }
  1450. } SMARTLIST_FOREACH_JOIN_END(rs, router);
  1451. router_dir_info_changed();
  1452. }
  1453. /** Given a list of signed_descriptor_t, update their fields (mainly, when
  1454. * they were last listed) from the most recent consensus. */
  1455. void
  1456. signed_descs_update_status_from_consensus_networkstatus(smartlist_t *descs)
  1457. {
  1458. networkstatus_t *ns = current_ns_consensus;
  1459. if (!ns)
  1460. return;
  1461. if (!ns->desc_digest_map) {
  1462. char dummy[DIGEST_LEN];
  1463. /* instantiates the digest map. */
  1464. memset(dummy, 0, sizeof(dummy));
  1465. router_get_consensus_status_by_descriptor_digest(ns, dummy);
  1466. }
  1467. SMARTLIST_FOREACH(descs, signed_descriptor_t *, d,
  1468. {
  1469. const routerstatus_t *rs = digestmap_get(ns->desc_digest_map,
  1470. d->signed_descriptor_digest);
  1471. if (rs) {
  1472. if (ns->valid_until > d->last_listed_as_valid_until)
  1473. d->last_listed_as_valid_until = ns->valid_until;
  1474. }
  1475. });
  1476. }
  1477. /** Generate networkstatus lines for a single routerstatus_t object, and
  1478. * return the result in a newly allocated string. Used only by controller
  1479. * interface (for now.) */
  1480. char *
  1481. networkstatus_getinfo_helper_single(const routerstatus_t *rs)
  1482. {
  1483. return routerstatus_format_entry(rs, NULL, NS_CONTROL_PORT, NULL);
  1484. }
  1485. /** Alloc and return a string describing routerstatuses for the most
  1486. * recent info of each router we know about that is of purpose
  1487. * <b>purpose_string</b>. Return NULL if unrecognized purpose.
  1488. *
  1489. * Right now this function is oriented toward listing bridges (you
  1490. * shouldn't use this for general-purpose routers, since those
  1491. * should be listed from the consensus, not from the routers list). */
  1492. char *
  1493. networkstatus_getinfo_by_purpose(const char *purpose_string, time_t now)
  1494. {
  1495. time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
  1496. char *answer;
  1497. routerlist_t *rl = router_get_routerlist();
  1498. smartlist_t *statuses;
  1499. uint8_t purpose = router_purpose_from_string(purpose_string);
  1500. routerstatus_t rs;
  1501. int bridge_auth = authdir_mode_bridge(get_options());
  1502. if (purpose == ROUTER_PURPOSE_UNKNOWN) {
  1503. log_info(LD_DIR, "Unrecognized purpose '%s' when listing router statuses.",
  1504. purpose_string);
  1505. return NULL;
  1506. }
  1507. statuses = smartlist_new();
  1508. SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
  1509. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  1510. if (!node)
  1511. continue;
  1512. if (ri->cache_info.published_on < cutoff)
  1513. continue;
  1514. if (ri->purpose != purpose)
  1515. continue;
  1516. if (bridge_auth && ri->purpose == ROUTER_PURPOSE_BRIDGE)
  1517. dirserv_set_router_is_running(ri, now);
  1518. /* then generate and write out status lines for each of them */
  1519. set_routerstatus_from_routerinfo(&rs, node, ri, now, 0, 0);
  1520. smartlist_add(statuses, networkstatus_getinfo_helper_single(&rs));
  1521. } SMARTLIST_FOREACH_END(ri);
  1522. answer = smartlist_join_strings(statuses, "", 0, NULL);
  1523. SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
  1524. smartlist_free(statuses);
  1525. return answer;
  1526. }
  1527. /** Write out router status entries for all our bridge descriptors. */
  1528. void
  1529. networkstatus_dump_bridge_status_to_file(time_t now)
  1530. {
  1531. char *status = networkstatus_getinfo_by_purpose("bridge", now);
  1532. const or_options_t *options = get_options();
  1533. char *fname = NULL;
  1534. char *thresholds = NULL;
  1535. char *published_thresholds_and_status = NULL;
  1536. routerlist_t *rl = router_get_routerlist();
  1537. char published[ISO_TIME_LEN+1];
  1538. format_iso_time(published, now);
  1539. dirserv_compute_bridge_flag_thresholds(rl);
  1540. thresholds = dirserv_get_flag_thresholds_line();
  1541. tor_asprintf(&published_thresholds_and_status,
  1542. "published %s\nflag-thresholds %s\n%s",
  1543. published, thresholds, status);
  1544. tor_asprintf(&fname, "%s"PATH_SEPARATOR"networkstatus-bridges",
  1545. options->DataDirectory);
  1546. write_str_to_file(fname,published_thresholds_and_status,0);
  1547. tor_free(thresholds);
  1548. tor_free(published_thresholds_and_status);
  1549. tor_free(fname);
  1550. tor_free(status);
  1551. }
  1552. /* DOCDOC get_net_param_from_list */
  1553. static int32_t
  1554. get_net_param_from_list(smartlist_t *net_params, const char *param_name,
  1555. int32_t default_val, int32_t min_val, int32_t max_val)
  1556. {
  1557. int32_t res = default_val;
  1558. size_t name_len = strlen(param_name);
  1559. tor_assert(max_val > min_val);
  1560. tor_assert(min_val <= default_val);
  1561. tor_assert(max_val >= default_val);
  1562. SMARTLIST_FOREACH_BEGIN(net_params, const char *, p) {
  1563. if (!strcmpstart(p, param_name) && p[name_len] == '=') {
  1564. int ok=0;
  1565. long v = tor_parse_long(p+name_len+1, 10, INT32_MIN,
  1566. INT32_MAX, &ok, NULL);
  1567. if (ok) {
  1568. res = (int32_t) v;
  1569. break;
  1570. }
  1571. }
  1572. } SMARTLIST_FOREACH_END(p);
  1573. if (res < min_val) {
  1574. log_warn(LD_DIR, "Consensus parameter %s is too small. Got %d, raising to "
  1575. "%d.", param_name, res, min_val);
  1576. res = min_val;
  1577. } else if (res > max_val) {
  1578. log_warn(LD_DIR, "Consensus parameter %s is too large. Got %d, capping to "
  1579. "%d.", param_name, res, max_val);
  1580. res = max_val;
  1581. }
  1582. return res;
  1583. }
  1584. /** Return the value of a integer parameter from the networkstatus <b>ns</b>
  1585. * whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
  1586. * latest consensus ourselves. Return <b>default_val</b> if no latest
  1587. * consensus, or if it has no parameter called <b>param_name</b>.
  1588. * Make sure the value parsed from the consensus is at least
  1589. * <b>min_val</b> and at most <b>max_val</b> and raise/cap the parsed value
  1590. * if necessary. */
  1591. int32_t
  1592. networkstatus_get_param(const networkstatus_t *ns, const char *param_name,
  1593. int32_t default_val, int32_t min_val, int32_t max_val)
  1594. {
  1595. if (!ns) /* if they pass in null, go find it ourselves */
  1596. ns = networkstatus_get_latest_consensus();
  1597. if (!ns || !ns->net_params)
  1598. return default_val;
  1599. return get_net_param_from_list(ns->net_params, param_name,
  1600. default_val, min_val, max_val);
  1601. }
  1602. /**
  1603. * Retrieve the consensus parameter that governs the
  1604. * fixed-point precision of our network balancing 'bandwidth-weights'
  1605. * (which are themselves integer consensus values). We divide them
  1606. * by this value and ensure they never exceed this value.
  1607. */
  1608. int
  1609. networkstatus_get_weight_scale_param(networkstatus_t *ns)
  1610. {
  1611. return networkstatus_get_param(ns, "bwweightscale",
  1612. BW_WEIGHT_SCALE,
  1613. BW_MIN_WEIGHT_SCALE,
  1614. BW_MAX_WEIGHT_SCALE);
  1615. }
  1616. /** Return the value of a integer bw weight parameter from the networkstatus
  1617. * <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
  1618. * loading the latest consensus ourselves. Return <b>default_val</b> if no
  1619. * latest consensus, or if it has no parameter called <b>weight_name</b>. */
  1620. int32_t
  1621. networkstatus_get_bw_weight(networkstatus_t *ns, const char *weight_name,
  1622. int32_t default_val)
  1623. {
  1624. int32_t param;
  1625. int max;
  1626. if (!ns) /* if they pass in null, go find it ourselves */
  1627. ns = networkstatus_get_latest_consensus();
  1628. if (!ns || !ns->weight_params)
  1629. return default_val;
  1630. max = networkstatus_get_weight_scale_param(ns);
  1631. param = get_net_param_from_list(ns->weight_params, weight_name,
  1632. default_val, -1,
  1633. BW_MAX_WEIGHT_SCALE);
  1634. if (param > max) {
  1635. log_warn(LD_DIR, "Value of consensus weight %s was too large, capping "
  1636. "to %d", weight_name, max);
  1637. param = max;
  1638. }
  1639. return param;
  1640. }
  1641. /** Return the name of the consensus flavor <b>flav</b> as used to identify
  1642. * the flavor in directory documents. */
  1643. const char *
  1644. networkstatus_get_flavor_name(consensus_flavor_t flav)
  1645. {
  1646. switch (flav) {
  1647. case FLAV_NS:
  1648. return "ns";
  1649. case FLAV_MICRODESC:
  1650. return "microdesc";
  1651. default:
  1652. tor_fragile_assert();
  1653. return "??";
  1654. }
  1655. }
  1656. /** Return the consensus_flavor_t value for the flavor called <b>flavname</b>,
  1657. * or -1 if the flavor is not recognized. */
  1658. int
  1659. networkstatus_parse_flavor_name(const char *flavname)
  1660. {
  1661. if (!strcmp(flavname, "ns"))
  1662. return FLAV_NS;
  1663. else if (!strcmp(flavname, "microdesc"))
  1664. return FLAV_MICRODESC;
  1665. else
  1666. return -1;
  1667. }
  1668. /** Return 0 if this routerstatus is obsolete, too new, isn't
  1669. * running, or otherwise not a descriptor that we would make any
  1670. * use of even if we had it. Else return 1. */
  1671. int
  1672. client_would_use_router(const routerstatus_t *rs, time_t now,
  1673. const or_options_t *options)
  1674. {
  1675. if (!rs->is_flagged_running && !options->FetchUselessDescriptors) {
  1676. /* If we had this router descriptor, we wouldn't even bother using it.
  1677. * But, if we want to have a complete list, fetch it anyway. */
  1678. return 0;
  1679. }
  1680. if (rs->published_on + options->TestingEstimatedDescriptorPropagationTime
  1681. > now) {
  1682. /* Most caches probably don't have this descriptor yet. */
  1683. return 0;
  1684. }
  1685. if (rs->published_on + OLD_ROUTER_DESC_MAX_AGE < now) {
  1686. /* We'd drop it immediately for being too old. */
  1687. return 0;
  1688. }
  1689. return 1;
  1690. }
  1691. /** If <b>question</b> is a string beginning with "ns/" in a format the
  1692. * control interface expects for a GETINFO question, set *<b>answer</b> to a
  1693. * newly-allocated string containing networkstatus lines for the appropriate
  1694. * ORs. Return 0 on success, -1 on unrecognized question format. */
  1695. int
  1696. getinfo_helper_networkstatus(control_connection_t *conn,
  1697. const char *question, char **answer,
  1698. const char **errmsg)
  1699. {
  1700. const routerstatus_t *status;
  1701. (void) conn;
  1702. if (!current_consensus) {
  1703. *answer = tor_strdup("");
  1704. return 0;
  1705. }
  1706. if (!strcmp(question, "ns/all")) {
  1707. smartlist_t *statuses = smartlist_new();
  1708. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  1709. const routerstatus_t *, rs,
  1710. {
  1711. smartlist_add(statuses, networkstatus_getinfo_helper_single(rs));
  1712. });
  1713. *answer = smartlist_join_strings(statuses, "", 0, NULL);
  1714. SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
  1715. smartlist_free(statuses);
  1716. return 0;
  1717. } else if (!strcmpstart(question, "ns/id/")) {
  1718. char d[DIGEST_LEN];
  1719. const char *q = question + 6;
  1720. if (*q == '$')
  1721. ++q;
  1722. if (base16_decode(d, DIGEST_LEN, q, strlen(q))) {
  1723. *errmsg = "Data not decodeable as hex";
  1724. return -1;
  1725. }
  1726. status = router_get_consensus_status_by_id(d);
  1727. } else if (!strcmpstart(question, "ns/name/")) {
  1728. status = router_get_consensus_status_by_nickname(question+8, 0);
  1729. } else if (!strcmpstart(question, "ns/purpose/")) {
  1730. *answer = networkstatus_getinfo_by_purpose(question+11, time(NULL));
  1731. return *answer ? 0 : -1;
  1732. } else {
  1733. return 0;
  1734. }
  1735. if (status)
  1736. *answer = networkstatus_getinfo_helper_single(status);
  1737. return 0;
  1738. }
  1739. /** Free all storage held locally in this module. */
  1740. void
  1741. networkstatus_free_all(void)
  1742. {
  1743. int i;
  1744. networkstatus_vote_free(current_ns_consensus);
  1745. networkstatus_vote_free(current_md_consensus);
  1746. current_md_consensus = current_ns_consensus = NULL;
  1747. for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
  1748. consensus_waiting_for_certs_t *waiting = &consensus_waiting_for_certs[i];
  1749. if (waiting->consensus) {
  1750. networkstatus_vote_free(waiting->consensus);
  1751. waiting->consensus = NULL;
  1752. }
  1753. tor_free(waiting->body);
  1754. }
  1755. strmap_free(named_server_map, tor_free_);
  1756. strmap_free(unnamed_server_map, NULL);
  1757. }