networkstatus.c 64 KB

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