entrynodes.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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-2012, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file entrynodes.c
  8. * \brief Code to manage our fixed first nodes for various functions.
  9. *
  10. * Entry nodes can be guards (for general use) or bridges (for censorship
  11. * circumvention).
  12. **/
  13. #include "or.h"
  14. #include "circuitbuild.h"
  15. #include "circuitstats.h"
  16. #include "config.h"
  17. #include "confparse.h"
  18. #include "connection.h"
  19. #include "connection_or.h"
  20. #include "control.h"
  21. #include "directory.h"
  22. #include "entrynodes.h"
  23. #include "main.h"
  24. #include "nodelist.h"
  25. #include "policies.h"
  26. #include "router.h"
  27. #include "routerlist.h"
  28. #include "routerparse.h"
  29. #include "routerset.h"
  30. #include "transports.h"
  31. #include "statefile.h"
  32. /** Information about a configured bridge. Currently this just matches the
  33. * ones in the torrc file, but one day we may be able to learn about new
  34. * bridges on our own, and remember them in the state file. */
  35. typedef struct {
  36. /** Address of the bridge. */
  37. tor_addr_t addr;
  38. /** TLS port for the bridge. */
  39. uint16_t port;
  40. /** Boolean: We are re-parsing our bridge list, and we are going to remove
  41. * this one if we don't find it in the list of configured bridges. */
  42. unsigned marked_for_removal : 1;
  43. /** Expected identity digest, or all zero bytes if we don't know what the
  44. * digest should be. */
  45. char identity[DIGEST_LEN];
  46. /** Name of pluggable transport protocol taken from its config line. */
  47. char *transport_name;
  48. /** When should we next try to fetch a descriptor for this bridge? */
  49. download_status_t fetch_status;
  50. } bridge_info_t;
  51. /** A list of our chosen entry guards. */
  52. static smartlist_t *entry_guards = NULL;
  53. /** A value of 1 means that the entry_guards list has changed
  54. * and those changes need to be flushed to disk. */
  55. static int entry_guards_dirty = 0;
  56. static void bridge_free(bridge_info_t *bridge);
  57. /** Return the list of entry guards, creating it if necessary. */
  58. const smartlist_t *
  59. get_entry_guards(void)
  60. {
  61. if (! entry_guards)
  62. entry_guards = smartlist_new();
  63. return entry_guards;
  64. }
  65. /** Check whether the entry guard <b>e</b> is usable, given the directory
  66. * authorities' opinion about the router (stored in <b>ri</b>) and the user's
  67. * configuration (in <b>options</b>). Set <b>e</b>-&gt;bad_since
  68. * accordingly. Return true iff the entry guard's status changes.
  69. *
  70. * If it's not usable, set *<b>reason</b> to a static string explaining why.
  71. */
  72. static int
  73. entry_guard_set_status(entry_guard_t *e, const node_t *node,
  74. time_t now, const or_options_t *options,
  75. const char **reason)
  76. {
  77. char buf[HEX_DIGEST_LEN+1];
  78. int changed = 0;
  79. *reason = NULL;
  80. /* Do we want to mark this guard as bad? */
  81. if (!node)
  82. *reason = "unlisted";
  83. else if (!node->is_running)
  84. *reason = "down";
  85. else if (options->UseBridges && (!node->ri ||
  86. node->ri->purpose != ROUTER_PURPOSE_BRIDGE))
  87. *reason = "not a bridge";
  88. else if (options->UseBridges && !node_is_a_configured_bridge(node))
  89. *reason = "not a configured bridge";
  90. else if (!options->UseBridges && !node->is_possible_guard &&
  91. !routerset_contains_node(options->EntryNodes,node))
  92. *reason = "not recommended as a guard";
  93. else if (routerset_contains_node(options->ExcludeNodes, node))
  94. *reason = "excluded";
  95. else if (e->path_bias_disabled)
  96. *reason = "path-biased";
  97. if (*reason && ! e->bad_since) {
  98. /* Router is newly bad. */
  99. base16_encode(buf, sizeof(buf), e->identity, DIGEST_LEN);
  100. log_info(LD_CIRC, "Entry guard %s (%s) is %s: marking as unusable.",
  101. e->nickname, buf, *reason);
  102. e->bad_since = now;
  103. control_event_guard(e->nickname, e->identity, "BAD");
  104. changed = 1;
  105. } else if (!*reason && e->bad_since) {
  106. /* There's nothing wrong with the router any more. */
  107. base16_encode(buf, sizeof(buf), e->identity, DIGEST_LEN);
  108. log_info(LD_CIRC, "Entry guard %s (%s) is no longer unusable: "
  109. "marking as ok.", e->nickname, buf);
  110. e->bad_since = 0;
  111. control_event_guard(e->nickname, e->identity, "GOOD");
  112. changed = 1;
  113. }
  114. return changed;
  115. }
  116. /** Return true iff enough time has passed since we last tried to connect
  117. * to the unreachable guard <b>e</b> that we're willing to try again. */
  118. static int
  119. entry_is_time_to_retry(entry_guard_t *e, time_t now)
  120. {
  121. long diff;
  122. if (e->last_attempted < e->unreachable_since)
  123. return 1;
  124. diff = now - e->unreachable_since;
  125. if (diff < 6*60*60)
  126. return now > (e->last_attempted + 60*60);
  127. else if (diff < 3*24*60*60)
  128. return now > (e->last_attempted + 4*60*60);
  129. else if (diff < 7*24*60*60)
  130. return now > (e->last_attempted + 18*60*60);
  131. else
  132. return now > (e->last_attempted + 36*60*60);
  133. }
  134. /** Return the node corresponding to <b>e</b>, if <b>e</b> is
  135. * working well enough that we are willing to use it as an entry
  136. * right now. (Else return NULL.) In particular, it must be
  137. * - Listed as either up or never yet contacted;
  138. * - Present in the routerlist;
  139. * - Listed as 'stable' or 'fast' by the current dirserver consensus,
  140. * if demanded by <b>need_uptime</b> or <b>need_capacity</b>
  141. * (unless it's a configured EntryNode);
  142. * - Allowed by our current ReachableORAddresses config option; and
  143. * - Currently thought to be reachable by us (unless <b>assume_reachable</b>
  144. * is true).
  145. *
  146. * If the answer is no, set *<b>msg</b> to an explanation of why.
  147. */
  148. static INLINE const node_t *
  149. entry_is_live(entry_guard_t *e, int need_uptime, int need_capacity,
  150. int assume_reachable, const char **msg)
  151. {
  152. const node_t *node;
  153. const or_options_t *options = get_options();
  154. tor_assert(msg);
  155. if (e->path_bias_disabled) {
  156. *msg = "path-biased";
  157. return NULL;
  158. }
  159. if (e->bad_since) {
  160. *msg = "bad";
  161. return NULL;
  162. }
  163. /* no good if it's unreachable, unless assume_unreachable or can_retry. */
  164. if (!assume_reachable && !e->can_retry &&
  165. e->unreachable_since && !entry_is_time_to_retry(e, time(NULL))) {
  166. *msg = "unreachable";
  167. return NULL;
  168. }
  169. node = node_get_by_id(e->identity);
  170. if (!node || !node_has_descriptor(node)) {
  171. *msg = "no descriptor";
  172. return NULL;
  173. }
  174. if (get_options()->UseBridges) {
  175. if (node_get_purpose(node) != ROUTER_PURPOSE_BRIDGE) {
  176. *msg = "not a bridge";
  177. return NULL;
  178. }
  179. if (!node_is_a_configured_bridge(node)) {
  180. *msg = "not a configured bridge";
  181. return NULL;
  182. }
  183. } else { /* !get_options()->UseBridges */
  184. if (node_get_purpose(node) != ROUTER_PURPOSE_GENERAL) {
  185. *msg = "not general-purpose";
  186. return NULL;
  187. }
  188. }
  189. if (routerset_contains_node(options->EntryNodes, node)) {
  190. /* they asked for it, they get it */
  191. need_uptime = need_capacity = 0;
  192. }
  193. if (node_is_unreliable(node, need_uptime, need_capacity, 0)) {
  194. *msg = "not fast/stable";
  195. return NULL;
  196. }
  197. if (!fascist_firewall_allows_node(node)) {
  198. *msg = "unreachable by config";
  199. return NULL;
  200. }
  201. return node;
  202. }
  203. /** Return the number of entry guards that we think are usable. */
  204. int
  205. num_live_entry_guards(void)
  206. {
  207. int n = 0;
  208. const char *msg;
  209. if (! entry_guards)
  210. return 0;
  211. SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
  212. {
  213. if (entry_is_live(entry, 0, 1, 0, &msg))
  214. ++n;
  215. });
  216. return n;
  217. }
  218. /** If <b>digest</b> matches the identity of any node in the
  219. * entry_guards list, return that node. Else return NULL. */
  220. entry_guard_t *
  221. entry_guard_get_by_id_digest(const char *digest)
  222. {
  223. SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
  224. if (tor_memeq(digest, entry->identity, DIGEST_LEN))
  225. return entry;
  226. );
  227. return NULL;
  228. }
  229. /** Dump a description of our list of entry guards to the log at level
  230. * <b>severity</b>. */
  231. static void
  232. log_entry_guards(int severity)
  233. {
  234. smartlist_t *elements = smartlist_new();
  235. char *s;
  236. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e)
  237. {
  238. const char *msg = NULL;
  239. if (entry_is_live(e, 0, 1, 0, &msg))
  240. smartlist_add_asprintf(elements, "%s [%s] (up %s)",
  241. e->nickname,
  242. hex_str(e->identity, DIGEST_LEN),
  243. e->made_contact ? "made-contact" : "never-contacted");
  244. else
  245. smartlist_add_asprintf(elements, "%s [%s] (%s, %s)",
  246. e->nickname,
  247. hex_str(e->identity, DIGEST_LEN),
  248. msg,
  249. e->made_contact ? "made-contact" : "never-contacted");
  250. }
  251. SMARTLIST_FOREACH_END(e);
  252. s = smartlist_join_strings(elements, ",", 0, NULL);
  253. SMARTLIST_FOREACH(elements, char*, cp, tor_free(cp));
  254. smartlist_free(elements);
  255. log_fn(severity,LD_CIRC,"%s",s);
  256. tor_free(s);
  257. }
  258. /** Called when one or more guards that we would previously have used for some
  259. * purpose are no longer in use because a higher-priority guard has become
  260. * usable again. */
  261. static void
  262. control_event_guard_deferred(void)
  263. {
  264. /* XXXX We don't actually have a good way to figure out _how many_ entries
  265. * are live for some purpose. We need an entry_is_even_slightly_live()
  266. * function for this to work right. NumEntryGuards isn't reliable: if we
  267. * need guards with weird properties, we can have more than that number
  268. * live.
  269. **/
  270. #if 0
  271. int n = 0;
  272. const char *msg;
  273. const or_options_t *options = get_options();
  274. if (!entry_guards)
  275. return;
  276. SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
  277. {
  278. if (entry_is_live(entry, 0, 1, 0, &msg)) {
  279. if (n++ == options->NumEntryGuards) {
  280. control_event_guard(entry->nickname, entry->identity, "DEFERRED");
  281. return;
  282. }
  283. }
  284. });
  285. #endif
  286. }
  287. /** Add a new (preferably stable and fast) router to our
  288. * entry_guards list. Return a pointer to the router if we succeed,
  289. * or NULL if we can't find any more suitable entries.
  290. *
  291. * If <b>chosen</b> is defined, use that one, and if it's not
  292. * already in our entry_guards list, put it at the *beginning*.
  293. * Else, put the one we pick at the end of the list. */
  294. static const node_t *
  295. add_an_entry_guard(const node_t *chosen, int reset_status, int prepend)
  296. {
  297. const node_t *node;
  298. entry_guard_t *entry;
  299. if (chosen) {
  300. node = chosen;
  301. entry = entry_guard_get_by_id_digest(node->identity);
  302. if (entry) {
  303. if (reset_status) {
  304. entry->bad_since = 0;
  305. entry->can_retry = 1;
  306. }
  307. return NULL;
  308. }
  309. } else {
  310. node = choose_good_entry_server(CIRCUIT_PURPOSE_C_GENERAL, NULL);
  311. if (!node)
  312. return NULL;
  313. }
  314. entry = tor_malloc_zero(sizeof(entry_guard_t));
  315. log_info(LD_CIRC, "Chose %s as new entry guard.",
  316. node_describe(node));
  317. strlcpy(entry->nickname, node_get_nickname(node), sizeof(entry->nickname));
  318. memcpy(entry->identity, node->identity, DIGEST_LEN);
  319. /* Choose expiry time smudged over the past month. The goal here
  320. * is to a) spread out when Tor clients rotate their guards, so they
  321. * don't all select them on the same day, and b) avoid leaving a
  322. * precise timestamp in the state file about when we first picked
  323. * this guard. For details, see the Jan 2010 or-dev thread. */
  324. entry->chosen_on_date = time(NULL) - crypto_rand_int(3600*24*30);
  325. entry->chosen_by_version = tor_strdup(VERSION);
  326. if (prepend)
  327. smartlist_insert(entry_guards, 0, entry);
  328. else
  329. smartlist_add(entry_guards, entry);
  330. control_event_guard(entry->nickname, entry->identity, "NEW");
  331. control_event_guard_deferred();
  332. log_entry_guards(LOG_INFO);
  333. return node;
  334. }
  335. /** If the use of entry guards is configured, choose more entry guards
  336. * until we have enough in the list. */
  337. static void
  338. pick_entry_guards(const or_options_t *options)
  339. {
  340. int changed = 0;
  341. tor_assert(entry_guards);
  342. while (num_live_entry_guards() < options->NumEntryGuards) {
  343. if (!add_an_entry_guard(NULL, 0, 0))
  344. break;
  345. changed = 1;
  346. }
  347. if (changed)
  348. entry_guards_changed();
  349. }
  350. /** How long (in seconds) do we allow an entry guard to be nonfunctional,
  351. * unlisted, excluded, or otherwise nonusable before we give up on it? */
  352. #define ENTRY_GUARD_REMOVE_AFTER (30*24*60*60)
  353. /** Release all storage held by <b>e</b>. */
  354. static void
  355. entry_guard_free(entry_guard_t *e)
  356. {
  357. if (!e)
  358. return;
  359. tor_free(e->chosen_by_version);
  360. tor_free(e);
  361. }
  362. /** Remove any entry guard which was selected by an unknown version of Tor,
  363. * or which was selected by a version of Tor that's known to select
  364. * entry guards badly, or which was selected more 2 months ago. */
  365. /* XXXX The "obsolete guards" and "chosen long ago guards" things should
  366. * probably be different functions. */
  367. static int
  368. remove_obsolete_entry_guards(time_t now)
  369. {
  370. int changed = 0, i;
  371. for (i = 0; i < smartlist_len(entry_guards); ++i) {
  372. entry_guard_t *entry = smartlist_get(entry_guards, i);
  373. const char *ver = entry->chosen_by_version;
  374. const char *msg = NULL;
  375. tor_version_t v;
  376. int version_is_bad = 0, date_is_bad = 0;
  377. if (!ver) {
  378. msg = "does not say what version of Tor it was selected by";
  379. version_is_bad = 1;
  380. } else if (tor_version_parse(ver, &v)) {
  381. msg = "does not seem to be from any recognized version of Tor";
  382. version_is_bad = 1;
  383. } else {
  384. char *tor_ver = NULL;
  385. tor_asprintf(&tor_ver, "Tor %s", ver);
  386. if ((tor_version_as_new_as(tor_ver, "0.1.0.10-alpha") &&
  387. !tor_version_as_new_as(tor_ver, "0.1.2.16-dev")) ||
  388. (tor_version_as_new_as(tor_ver, "0.2.0.0-alpha") &&
  389. !tor_version_as_new_as(tor_ver, "0.2.0.6-alpha")) ||
  390. /* above are bug 440; below are bug 1217 */
  391. (tor_version_as_new_as(tor_ver, "0.2.1.3-alpha") &&
  392. !tor_version_as_new_as(tor_ver, "0.2.1.23")) ||
  393. (tor_version_as_new_as(tor_ver, "0.2.2.0-alpha") &&
  394. !tor_version_as_new_as(tor_ver, "0.2.2.7-alpha"))) {
  395. msg = "was selected without regard for guard bandwidth";
  396. version_is_bad = 1;
  397. }
  398. tor_free(tor_ver);
  399. }
  400. if (!version_is_bad && entry->chosen_on_date + 3600*24*60 < now) {
  401. /* It's been 2 months since the date listed in our state file. */
  402. msg = "was selected several months ago";
  403. date_is_bad = 1;
  404. }
  405. if (version_is_bad || date_is_bad) { /* we need to drop it */
  406. char dbuf[HEX_DIGEST_LEN+1];
  407. tor_assert(msg);
  408. base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
  409. log_fn(version_is_bad ? LOG_NOTICE : LOG_INFO, LD_CIRC,
  410. "Entry guard '%s' (%s) %s. (Version=%s.) Replacing it.",
  411. entry->nickname, dbuf, msg, ver?escaped(ver):"none");
  412. control_event_guard(entry->nickname, entry->identity, "DROPPED");
  413. entry_guard_free(entry);
  414. smartlist_del_keeporder(entry_guards, i--);
  415. log_entry_guards(LOG_INFO);
  416. changed = 1;
  417. }
  418. }
  419. return changed ? 1 : 0;
  420. }
  421. /** Remove all entry guards that have been down or unlisted for so
  422. * long that we don't think they'll come up again. Return 1 if we
  423. * removed any, or 0 if we did nothing. */
  424. static int
  425. remove_dead_entry_guards(time_t now)
  426. {
  427. char dbuf[HEX_DIGEST_LEN+1];
  428. char tbuf[ISO_TIME_LEN+1];
  429. int i;
  430. int changed = 0;
  431. for (i = 0; i < smartlist_len(entry_guards); ) {
  432. entry_guard_t *entry = smartlist_get(entry_guards, i);
  433. if (entry->bad_since &&
  434. ! entry->path_bias_disabled &&
  435. entry->bad_since + ENTRY_GUARD_REMOVE_AFTER < now) {
  436. base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
  437. format_local_iso_time(tbuf, entry->bad_since);
  438. log_info(LD_CIRC, "Entry guard '%s' (%s) has been down or unlisted "
  439. "since %s local time; removing.",
  440. entry->nickname, dbuf, tbuf);
  441. control_event_guard(entry->nickname, entry->identity, "DROPPED");
  442. entry_guard_free(entry);
  443. smartlist_del_keeporder(entry_guards, i);
  444. log_entry_guards(LOG_INFO);
  445. changed = 1;
  446. } else
  447. ++i;
  448. }
  449. return changed ? 1 : 0;
  450. }
  451. /** A new directory or router-status has arrived; update the down/listed
  452. * status of the entry guards.
  453. *
  454. * An entry is 'down' if the directory lists it as nonrunning.
  455. * An entry is 'unlisted' if the directory doesn't include it.
  456. *
  457. * Don't call this on startup; only on a fresh download. Otherwise we'll
  458. * think that things are unlisted.
  459. */
  460. void
  461. entry_guards_compute_status(const or_options_t *options, time_t now)
  462. {
  463. int changed = 0;
  464. digestmap_t *reasons;
  465. if (! entry_guards)
  466. return;
  467. if (options->EntryNodes) /* reshuffle the entry guard list if needed */
  468. entry_nodes_should_be_added();
  469. reasons = digestmap_new();
  470. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, entry)
  471. {
  472. const node_t *r = node_get_by_id(entry->identity);
  473. const char *reason = NULL;
  474. if (entry_guard_set_status(entry, r, now, options, &reason))
  475. changed = 1;
  476. if (entry->bad_since)
  477. tor_assert(reason);
  478. if (reason)
  479. digestmap_set(reasons, entry->identity, (char*)reason);
  480. }
  481. SMARTLIST_FOREACH_END(entry);
  482. if (remove_dead_entry_guards(now))
  483. changed = 1;
  484. if (remove_obsolete_entry_guards(now))
  485. changed = 1;
  486. if (changed) {
  487. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, entry) {
  488. const char *reason = digestmap_get(reasons, entry->identity);
  489. const char *live_msg = "";
  490. const node_t *r = entry_is_live(entry, 0, 1, 0, &live_msg);
  491. log_info(LD_CIRC, "Summary: Entry %s [%s] is %s, %s%s%s, and %s%s.",
  492. entry->nickname,
  493. hex_str(entry->identity, DIGEST_LEN),
  494. entry->unreachable_since ? "unreachable" : "reachable",
  495. entry->bad_since ? "unusable" : "usable",
  496. reason ? ", ": "",
  497. reason ? reason : "",
  498. r ? "live" : "not live / ",
  499. r ? "" : live_msg);
  500. } SMARTLIST_FOREACH_END(entry);
  501. log_info(LD_CIRC, " (%d/%d entry guards are usable/new)",
  502. num_live_entry_guards(), smartlist_len(entry_guards));
  503. log_entry_guards(LOG_INFO);
  504. entry_guards_changed();
  505. }
  506. digestmap_free(reasons, NULL);
  507. }
  508. /** Called when a connection to an OR with the identity digest <b>digest</b>
  509. * is established (<b>succeeded</b>==1) or has failed (<b>succeeded</b>==0).
  510. * If the OR is an entry, change that entry's up/down status.
  511. * Return 0 normally, or -1 if we want to tear down the new connection.
  512. *
  513. * If <b>mark_relay_status</b>, also call router_set_status() on this
  514. * relay.
  515. *
  516. * XXX024 change succeeded and mark_relay_status into 'int flags'.
  517. */
  518. int
  519. entry_guard_register_connect_status(const char *digest, int succeeded,
  520. int mark_relay_status, time_t now)
  521. {
  522. int changed = 0;
  523. int refuse_conn = 0;
  524. int first_contact = 0;
  525. entry_guard_t *entry = NULL;
  526. int idx = -1;
  527. char buf[HEX_DIGEST_LEN+1];
  528. if (! entry_guards)
  529. return 0;
  530. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) {
  531. tor_assert(e);
  532. if (tor_memeq(e->identity, digest, DIGEST_LEN)) {
  533. entry = e;
  534. idx = e_sl_idx;
  535. break;
  536. }
  537. } SMARTLIST_FOREACH_END(e);
  538. if (!entry)
  539. return 0;
  540. base16_encode(buf, sizeof(buf), entry->identity, DIGEST_LEN);
  541. if (succeeded) {
  542. if (entry->unreachable_since) {
  543. log_info(LD_CIRC, "Entry guard '%s' (%s) is now reachable again. Good.",
  544. entry->nickname, buf);
  545. entry->can_retry = 0;
  546. entry->unreachable_since = 0;
  547. entry->last_attempted = now;
  548. control_event_guard(entry->nickname, entry->identity, "UP");
  549. changed = 1;
  550. }
  551. if (!entry->made_contact) {
  552. entry->made_contact = 1;
  553. first_contact = changed = 1;
  554. }
  555. } else { /* ! succeeded */
  556. if (!entry->made_contact) {
  557. /* We've never connected to this one. */
  558. log_info(LD_CIRC,
  559. "Connection to never-contacted entry guard '%s' (%s) failed. "
  560. "Removing from the list. %d/%d entry guards usable/new.",
  561. entry->nickname, buf,
  562. num_live_entry_guards()-1, smartlist_len(entry_guards)-1);
  563. control_event_guard(entry->nickname, entry->identity, "DROPPED");
  564. entry_guard_free(entry);
  565. smartlist_del_keeporder(entry_guards, idx);
  566. log_entry_guards(LOG_INFO);
  567. changed = 1;
  568. } else if (!entry->unreachable_since) {
  569. log_info(LD_CIRC, "Unable to connect to entry guard '%s' (%s). "
  570. "Marking as unreachable.", entry->nickname, buf);
  571. entry->unreachable_since = entry->last_attempted = now;
  572. control_event_guard(entry->nickname, entry->identity, "DOWN");
  573. changed = 1;
  574. entry->can_retry = 0; /* We gave it an early chance; no good. */
  575. } else {
  576. char tbuf[ISO_TIME_LEN+1];
  577. format_iso_time(tbuf, entry->unreachable_since);
  578. log_debug(LD_CIRC, "Failed to connect to unreachable entry guard "
  579. "'%s' (%s). It has been unreachable since %s.",
  580. entry->nickname, buf, tbuf);
  581. entry->last_attempted = now;
  582. entry->can_retry = 0; /* We gave it an early chance; no good. */
  583. }
  584. }
  585. /* if the caller asked us to, also update the is_running flags for this
  586. * relay */
  587. if (mark_relay_status)
  588. router_set_status(digest, succeeded);
  589. if (first_contact) {
  590. /* We've just added a new long-term entry guard. Perhaps the network just
  591. * came back? We should give our earlier entries another try too,
  592. * and close this connection so we don't use it before we've given
  593. * the others a shot. */
  594. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) {
  595. if (e == entry)
  596. break;
  597. if (e->made_contact) {
  598. const char *msg;
  599. const node_t *r = entry_is_live(e, 0, 1, 1, &msg);
  600. if (r && e->unreachable_since) {
  601. refuse_conn = 1;
  602. e->can_retry = 1;
  603. }
  604. }
  605. } SMARTLIST_FOREACH_END(e);
  606. if (refuse_conn) {
  607. log_info(LD_CIRC,
  608. "Connected to new entry guard '%s' (%s). Marking earlier "
  609. "entry guards up. %d/%d entry guards usable/new.",
  610. entry->nickname, buf,
  611. num_live_entry_guards(), smartlist_len(entry_guards));
  612. log_entry_guards(LOG_INFO);
  613. changed = 1;
  614. }
  615. }
  616. if (changed)
  617. entry_guards_changed();
  618. return refuse_conn ? -1 : 0;
  619. }
  620. /** When we try to choose an entry guard, should we parse and add
  621. * config's EntryNodes first? */
  622. static int should_add_entry_nodes = 0;
  623. /** Called when the value of EntryNodes changes in our configuration. */
  624. void
  625. entry_nodes_should_be_added(void)
  626. {
  627. log_info(LD_CIRC, "EntryNodes config option set. Putting configured "
  628. "relays at the front of the entry guard list.");
  629. should_add_entry_nodes = 1;
  630. }
  631. /** Adjust the entry guards list so that it only contains entries from
  632. * EntryNodes, adding new entries from EntryNodes to the list as needed. */
  633. static void
  634. entry_guards_set_from_config(const or_options_t *options)
  635. {
  636. smartlist_t *entry_nodes, *worse_entry_nodes, *entry_fps;
  637. smartlist_t *old_entry_guards_on_list, *old_entry_guards_not_on_list;
  638. tor_assert(entry_guards);
  639. should_add_entry_nodes = 0;
  640. if (!options->EntryNodes) {
  641. /* It's possible that a controller set EntryNodes, thus making
  642. * should_add_entry_nodes set, then cleared it again, all before the
  643. * call to choose_random_entry() that triggered us. If so, just return.
  644. */
  645. return;
  646. }
  647. {
  648. char *string = routerset_to_string(options->EntryNodes);
  649. log_info(LD_CIRC,"Adding configured EntryNodes '%s'.", string);
  650. tor_free(string);
  651. }
  652. entry_nodes = smartlist_new();
  653. worse_entry_nodes = smartlist_new();
  654. entry_fps = smartlist_new();
  655. old_entry_guards_on_list = smartlist_new();
  656. old_entry_guards_not_on_list = smartlist_new();
  657. /* Split entry guards into those on the list and those not. */
  658. routerset_get_all_nodes(entry_nodes, options->EntryNodes,
  659. options->ExcludeNodes, 0);
  660. SMARTLIST_FOREACH(entry_nodes, const node_t *,node,
  661. smartlist_add(entry_fps, (void*)node->identity));
  662. SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e, {
  663. if (smartlist_digest_isin(entry_fps, e->identity))
  664. smartlist_add(old_entry_guards_on_list, e);
  665. else
  666. smartlist_add(old_entry_guards_not_on_list, e);
  667. });
  668. /* Remove all currently configured guard nodes, excluded nodes, unreachable
  669. * nodes, or non-Guard nodes from entry_nodes. */
  670. SMARTLIST_FOREACH_BEGIN(entry_nodes, const node_t *, node) {
  671. if (entry_guard_get_by_id_digest(node->identity)) {
  672. SMARTLIST_DEL_CURRENT(entry_nodes, node);
  673. continue;
  674. } else if (routerset_contains_node(options->ExcludeNodes, node)) {
  675. SMARTLIST_DEL_CURRENT(entry_nodes, node);
  676. continue;
  677. } else if (!fascist_firewall_allows_node(node)) {
  678. SMARTLIST_DEL_CURRENT(entry_nodes, node);
  679. continue;
  680. } else if (! node->is_possible_guard) {
  681. smartlist_add(worse_entry_nodes, (node_t*)node);
  682. SMARTLIST_DEL_CURRENT(entry_nodes, node);
  683. }
  684. } SMARTLIST_FOREACH_END(node);
  685. /* Now build the new entry_guards list. */
  686. smartlist_clear(entry_guards);
  687. /* First, the previously configured guards that are in EntryNodes. */
  688. smartlist_add_all(entry_guards, old_entry_guards_on_list);
  689. /* Next, scramble the rest of EntryNodes, putting the guards first. */
  690. smartlist_shuffle(entry_nodes);
  691. smartlist_shuffle(worse_entry_nodes);
  692. smartlist_add_all(entry_nodes, worse_entry_nodes);
  693. /* Next, the rest of EntryNodes */
  694. SMARTLIST_FOREACH_BEGIN(entry_nodes, const node_t *, node) {
  695. add_an_entry_guard(node, 0, 0);
  696. if (smartlist_len(entry_guards) > options->NumEntryGuards * 10)
  697. break;
  698. } SMARTLIST_FOREACH_END(node);
  699. log_notice(LD_GENERAL, "%d entries in guards", smartlist_len(entry_guards));
  700. /* Finally, free the remaining previously configured guards that are not in
  701. * EntryNodes. */
  702. SMARTLIST_FOREACH(old_entry_guards_not_on_list, entry_guard_t *, e,
  703. entry_guard_free(e));
  704. smartlist_free(entry_nodes);
  705. smartlist_free(worse_entry_nodes);
  706. smartlist_free(entry_fps);
  707. smartlist_free(old_entry_guards_on_list);
  708. smartlist_free(old_entry_guards_not_on_list);
  709. entry_guards_changed();
  710. }
  711. /** Return 0 if we're fine adding arbitrary routers out of the
  712. * directory to our entry guard list, or return 1 if we have a
  713. * list already and we must stick to it.
  714. */
  715. int
  716. entry_list_is_constrained(const or_options_t *options)
  717. {
  718. if (options->EntryNodes)
  719. return 1;
  720. if (options->UseBridges)
  721. return 1;
  722. return 0;
  723. }
  724. /** Pick a live (up and listed) entry guard from entry_guards. If
  725. * <b>state</b> is non-NULL, this is for a specific circuit --
  726. * make sure not to pick this circuit's exit or any node in the
  727. * exit's family. If <b>state</b> is NULL, we're looking for a random
  728. * guard (likely a bridge). */
  729. const node_t *
  730. choose_random_entry(cpath_build_state_t *state)
  731. {
  732. const or_options_t *options = get_options();
  733. smartlist_t *live_entry_guards = smartlist_new();
  734. smartlist_t *exit_family = smartlist_new();
  735. const node_t *chosen_exit =
  736. state?build_state_get_exit_node(state) : NULL;
  737. const node_t *node = NULL;
  738. int need_uptime = state ? state->need_uptime : 0;
  739. int need_capacity = state ? state->need_capacity : 0;
  740. int preferred_min, consider_exit_family = 0;
  741. if (chosen_exit) {
  742. nodelist_add_node_and_family(exit_family, chosen_exit);
  743. consider_exit_family = 1;
  744. }
  745. if (!entry_guards)
  746. entry_guards = smartlist_new();
  747. if (should_add_entry_nodes)
  748. entry_guards_set_from_config(options);
  749. if (!entry_list_is_constrained(options) &&
  750. smartlist_len(entry_guards) < options->NumEntryGuards)
  751. pick_entry_guards(options);
  752. retry:
  753. smartlist_clear(live_entry_guards);
  754. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, entry) {
  755. const char *msg;
  756. node = entry_is_live(entry, need_uptime, need_capacity, 0, &msg);
  757. if (!node)
  758. continue; /* down, no point */
  759. if (node == chosen_exit)
  760. continue; /* don't pick the same node for entry and exit */
  761. if (consider_exit_family && smartlist_isin(exit_family, node))
  762. continue; /* avoid relays that are family members of our exit */
  763. #if 0 /* since EntryNodes is always strict now, this clause is moot */
  764. if (options->EntryNodes &&
  765. !routerset_contains_node(options->EntryNodes, node)) {
  766. /* We've come to the end of our preferred entry nodes. */
  767. if (smartlist_len(live_entry_guards))
  768. goto choose_and_finish; /* only choose from the ones we like */
  769. if (options->StrictNodes) {
  770. /* in theory this case should never happen, since
  771. * entry_guards_set_from_config() drops unwanted relays */
  772. tor_fragile_assert();
  773. } else {
  774. log_info(LD_CIRC,
  775. "No relays from EntryNodes available. Using others.");
  776. }
  777. }
  778. #endif
  779. smartlist_add(live_entry_guards, (void*)node);
  780. if (!entry->made_contact) {
  781. /* Always start with the first not-yet-contacted entry
  782. * guard. Otherwise we might add several new ones, pick
  783. * the second new one, and now we've expanded our entry
  784. * guard list without needing to. */
  785. goto choose_and_finish;
  786. }
  787. if (smartlist_len(live_entry_guards) >= options->NumEntryGuards)
  788. goto choose_and_finish; /* we have enough */
  789. } SMARTLIST_FOREACH_END(entry);
  790. if (entry_list_is_constrained(options)) {
  791. /* If we prefer the entry nodes we've got, and we have at least
  792. * one choice, that's great. Use it. */
  793. preferred_min = 1;
  794. } else {
  795. /* Try to have at least 2 choices available. This way we don't
  796. * get stuck with a single live-but-crummy entry and just keep
  797. * using him.
  798. * (We might get 2 live-but-crummy entry guards, but so be it.) */
  799. preferred_min = 2;
  800. }
  801. if (smartlist_len(live_entry_guards) < preferred_min) {
  802. if (!entry_list_is_constrained(options)) {
  803. /* still no? try adding a new entry then */
  804. /* XXX if guard doesn't imply fast and stable, then we need
  805. * to tell add_an_entry_guard below what we want, or it might
  806. * be a long time til we get it. -RD */
  807. node = add_an_entry_guard(NULL, 0, 0);
  808. if (node) {
  809. entry_guards_changed();
  810. /* XXX we start over here in case the new node we added shares
  811. * a family with our exit node. There's a chance that we'll just
  812. * load up on entry guards here, if the network we're using is
  813. * one big family. Perhaps we should teach add_an_entry_guard()
  814. * to understand nodes-to-avoid-if-possible? -RD */
  815. goto retry;
  816. }
  817. }
  818. if (!node && need_uptime) {
  819. need_uptime = 0; /* try without that requirement */
  820. goto retry;
  821. }
  822. if (!node && need_capacity) {
  823. /* still no? last attempt, try without requiring capacity */
  824. need_capacity = 0;
  825. goto retry;
  826. }
  827. #if 0
  828. /* Removing this retry logic: if we only allow one exit, and it is in the
  829. same family as all our entries, then we are just plain not going to win
  830. here. */
  831. if (!node && entry_list_is_constrained(options) && consider_exit_family) {
  832. /* still no? if we're using bridges or have strictentrynodes
  833. * set, and our chosen exit is in the same family as all our
  834. * bridges/entry guards, then be flexible about families. */
  835. consider_exit_family = 0;
  836. goto retry;
  837. }
  838. #endif
  839. /* live_entry_guards may be empty below. Oh well, we tried. */
  840. }
  841. choose_and_finish:
  842. if (entry_list_is_constrained(options)) {
  843. /* We need to weight by bandwidth, because our bridges or entryguards
  844. * were not already selected proportional to their bandwidth. */
  845. node = node_sl_choose_by_bandwidth(live_entry_guards, WEIGHT_FOR_GUARD);
  846. } else {
  847. /* We choose uniformly at random here, because choose_good_entry_server()
  848. * already weights its choices by bandwidth, so we don't want to
  849. * *double*-weight our guard selection. */
  850. node = smartlist_choose(live_entry_guards);
  851. }
  852. smartlist_free(live_entry_guards);
  853. smartlist_free(exit_family);
  854. return node;
  855. }
  856. /** Parse <b>state</b> and learn about the entry guards it describes.
  857. * If <b>set</b> is true, and there are no errors, replace the global
  858. * entry_list with what we find.
  859. * On success, return 0. On failure, alloc into *<b>msg</b> a string
  860. * describing the error, and return -1.
  861. */
  862. int
  863. entry_guards_parse_state(or_state_t *state, int set, char **msg)
  864. {
  865. entry_guard_t *node = NULL;
  866. smartlist_t *new_entry_guards = smartlist_new();
  867. config_line_t *line;
  868. time_t now = time(NULL);
  869. const char *state_version = state->TorVersion;
  870. digestmap_t *added_by = digestmap_new();
  871. *msg = NULL;
  872. for (line = state->EntryGuards; line; line = line->next) {
  873. if (!strcasecmp(line->key, "EntryGuard")) {
  874. smartlist_t *args = smartlist_new();
  875. node = tor_malloc_zero(sizeof(entry_guard_t));
  876. /* all entry guards on disk have been contacted */
  877. node->made_contact = 1;
  878. smartlist_add(new_entry_guards, node);
  879. smartlist_split_string(args, line->value, " ",
  880. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  881. if (smartlist_len(args)<2) {
  882. *msg = tor_strdup("Unable to parse entry nodes: "
  883. "Too few arguments to EntryGuard");
  884. } else if (!is_legal_nickname(smartlist_get(args,0))) {
  885. *msg = tor_strdup("Unable to parse entry nodes: "
  886. "Bad nickname for EntryGuard");
  887. } else {
  888. strlcpy(node->nickname, smartlist_get(args,0), MAX_NICKNAME_LEN+1);
  889. if (base16_decode(node->identity, DIGEST_LEN, smartlist_get(args,1),
  890. strlen(smartlist_get(args,1)))<0) {
  891. *msg = tor_strdup("Unable to parse entry nodes: "
  892. "Bad hex digest for EntryGuard");
  893. }
  894. }
  895. SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
  896. smartlist_free(args);
  897. if (*msg)
  898. break;
  899. } else if (!strcasecmp(line->key, "EntryGuardDownSince") ||
  900. !strcasecmp(line->key, "EntryGuardUnlistedSince")) {
  901. time_t when;
  902. time_t last_try = 0;
  903. if (!node) {
  904. *msg = tor_strdup("Unable to parse entry nodes: "
  905. "EntryGuardDownSince/UnlistedSince without EntryGuard");
  906. break;
  907. }
  908. if (parse_iso_time(line->value, &when)<0) {
  909. *msg = tor_strdup("Unable to parse entry nodes: "
  910. "Bad time in EntryGuardDownSince/UnlistedSince");
  911. break;
  912. }
  913. if (when > now) {
  914. /* It's a bad idea to believe info in the future: you can wind
  915. * up with timeouts that aren't allowed to happen for years. */
  916. continue;
  917. }
  918. if (strlen(line->value) >= ISO_TIME_LEN+ISO_TIME_LEN+1) {
  919. /* ignore failure */
  920. (void) parse_iso_time(line->value+ISO_TIME_LEN+1, &last_try);
  921. }
  922. if (!strcasecmp(line->key, "EntryGuardDownSince")) {
  923. node->unreachable_since = when;
  924. node->last_attempted = last_try;
  925. } else {
  926. node->bad_since = when;
  927. }
  928. } else if (!strcasecmp(line->key, "EntryGuardAddedBy")) {
  929. char d[DIGEST_LEN];
  930. /* format is digest version date */
  931. if (strlen(line->value) < HEX_DIGEST_LEN+1+1+1+ISO_TIME_LEN) {
  932. log_warn(LD_BUG, "EntryGuardAddedBy line is not long enough.");
  933. continue;
  934. }
  935. if (base16_decode(d, sizeof(d), line->value, HEX_DIGEST_LEN)<0 ||
  936. line->value[HEX_DIGEST_LEN] != ' ') {
  937. log_warn(LD_BUG, "EntryGuardAddedBy line %s does not begin with "
  938. "hex digest", escaped(line->value));
  939. continue;
  940. }
  941. digestmap_set(added_by, d, tor_strdup(line->value+HEX_DIGEST_LEN+1));
  942. } else if (!strcasecmp(line->key, "EntryGuardPathBias")) {
  943. const or_options_t *options = get_options();
  944. unsigned hop_cnt, success_cnt;
  945. if (!node) {
  946. *msg = tor_strdup("Unable to parse entry nodes: "
  947. "EntryGuardPathBias without EntryGuard");
  948. break;
  949. }
  950. if (tor_sscanf(line->value, "%u %u", &success_cnt, &hop_cnt) != 2) {
  951. log_warn(LD_GENERAL, "Unable to parse guard path bias info: "
  952. "Misformated EntryGuardPathBias %s", escaped(line->value));
  953. continue;
  954. }
  955. node->first_hops = hop_cnt;
  956. node->circuit_successes = success_cnt;
  957. log_info(LD_GENERAL, "Read %u/%u path bias for node %s",
  958. node->circuit_successes, node->first_hops, node->nickname);
  959. /* Note: We rely on the < comparison here to allow us to set a 0
  960. * rate and disable the feature entirely. If refactoring, don't
  961. * change to <= */
  962. if (node->circuit_successes/((double)node->first_hops)
  963. < pathbias_get_disable_rate(options)) {
  964. node->path_bias_disabled = 1;
  965. log_info(LD_GENERAL,
  966. "Path bias is too high (%u/%u); disabling node %s",
  967. node->circuit_successes, node->first_hops, node->nickname);
  968. }
  969. } else {
  970. log_warn(LD_BUG, "Unexpected key %s", line->key);
  971. }
  972. }
  973. SMARTLIST_FOREACH_BEGIN(new_entry_guards, entry_guard_t *, e) {
  974. char *sp;
  975. char *val = digestmap_get(added_by, e->identity);
  976. if (val && (sp = strchr(val, ' '))) {
  977. time_t when;
  978. *sp++ = '\0';
  979. if (parse_iso_time(sp, &when)<0) {
  980. log_warn(LD_BUG, "Can't read time %s in EntryGuardAddedBy", sp);
  981. } else {
  982. e->chosen_by_version = tor_strdup(val);
  983. e->chosen_on_date = when;
  984. }
  985. } else {
  986. if (state_version) {
  987. e->chosen_by_version = tor_strdup(state_version);
  988. e->chosen_on_date = time(NULL) - crypto_rand_int(3600*24*30);
  989. }
  990. }
  991. if (e->path_bias_disabled && !e->bad_since)
  992. e->bad_since = time(NULL);
  993. }
  994. SMARTLIST_FOREACH_END(e);
  995. if (*msg || !set) {
  996. SMARTLIST_FOREACH(new_entry_guards, entry_guard_t *, e,
  997. entry_guard_free(e));
  998. smartlist_free(new_entry_guards);
  999. } else { /* !err && set */
  1000. if (entry_guards) {
  1001. SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
  1002. entry_guard_free(e));
  1003. smartlist_free(entry_guards);
  1004. }
  1005. entry_guards = new_entry_guards;
  1006. entry_guards_dirty = 0;
  1007. /* XXX024 hand new_entry_guards to this func, and move it up a
  1008. * few lines, so we don't have to re-dirty it */
  1009. if (remove_obsolete_entry_guards(now))
  1010. entry_guards_dirty = 1;
  1011. }
  1012. digestmap_free(added_by, tor_free_);
  1013. return *msg ? -1 : 0;
  1014. }
  1015. /** Our list of entry guards has changed, or some element of one
  1016. * of our entry guards has changed. Write the changes to disk within
  1017. * the next few minutes.
  1018. */
  1019. void
  1020. entry_guards_changed(void)
  1021. {
  1022. time_t when;
  1023. entry_guards_dirty = 1;
  1024. /* or_state_save() will call entry_guards_update_state(). */
  1025. when = get_options()->AvoidDiskWrites ? time(NULL) + 3600 : time(NULL)+600;
  1026. or_state_mark_dirty(get_or_state(), when);
  1027. }
  1028. /** If the entry guard info has not changed, do nothing and return.
  1029. * Otherwise, free the EntryGuards piece of <b>state</b> and create
  1030. * a new one out of the global entry_guards list, and then mark
  1031. * <b>state</b> dirty so it will get saved to disk.
  1032. */
  1033. void
  1034. entry_guards_update_state(or_state_t *state)
  1035. {
  1036. config_line_t **next, *line;
  1037. if (! entry_guards_dirty)
  1038. return;
  1039. config_free_lines(state->EntryGuards);
  1040. next = &state->EntryGuards;
  1041. *next = NULL;
  1042. if (!entry_guards)
  1043. entry_guards = smartlist_new();
  1044. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) {
  1045. char dbuf[HEX_DIGEST_LEN+1];
  1046. if (!e->made_contact)
  1047. continue; /* don't write this one to disk */
  1048. *next = line = tor_malloc_zero(sizeof(config_line_t));
  1049. line->key = tor_strdup("EntryGuard");
  1050. base16_encode(dbuf, sizeof(dbuf), e->identity, DIGEST_LEN);
  1051. tor_asprintf(&line->value, "%s %s", e->nickname, dbuf);
  1052. next = &(line->next);
  1053. if (e->unreachable_since) {
  1054. *next = line = tor_malloc_zero(sizeof(config_line_t));
  1055. line->key = tor_strdup("EntryGuardDownSince");
  1056. line->value = tor_malloc(ISO_TIME_LEN+1+ISO_TIME_LEN+1);
  1057. format_iso_time(line->value, e->unreachable_since);
  1058. if (e->last_attempted) {
  1059. line->value[ISO_TIME_LEN] = ' ';
  1060. format_iso_time(line->value+ISO_TIME_LEN+1, e->last_attempted);
  1061. }
  1062. next = &(line->next);
  1063. }
  1064. if (e->bad_since) {
  1065. *next = line = tor_malloc_zero(sizeof(config_line_t));
  1066. line->key = tor_strdup("EntryGuardUnlistedSince");
  1067. line->value = tor_malloc(ISO_TIME_LEN+1);
  1068. format_iso_time(line->value, e->bad_since);
  1069. next = &(line->next);
  1070. }
  1071. if (e->chosen_on_date && e->chosen_by_version &&
  1072. !strchr(e->chosen_by_version, ' ')) {
  1073. char d[HEX_DIGEST_LEN+1];
  1074. char t[ISO_TIME_LEN+1];
  1075. *next = line = tor_malloc_zero(sizeof(config_line_t));
  1076. line->key = tor_strdup("EntryGuardAddedBy");
  1077. base16_encode(d, sizeof(d), e->identity, DIGEST_LEN);
  1078. format_iso_time(t, e->chosen_on_date);
  1079. tor_asprintf(&line->value, "%s %s %s",
  1080. d, e->chosen_by_version, t);
  1081. next = &(line->next);
  1082. }
  1083. if (e->first_hops) {
  1084. *next = line = tor_malloc_zero(sizeof(config_line_t));
  1085. line->key = tor_strdup("EntryGuardPathBias");
  1086. tor_asprintf(&line->value, "%u %u",
  1087. e->circuit_successes, e->first_hops);
  1088. next = &(line->next);
  1089. }
  1090. } SMARTLIST_FOREACH_END(e);
  1091. if (!get_options()->AvoidDiskWrites)
  1092. or_state_mark_dirty(get_or_state(), 0);
  1093. entry_guards_dirty = 0;
  1094. }
  1095. /** If <b>question</b> is the string "entry-guards", then dump
  1096. * to *<b>answer</b> a newly allocated string describing all of
  1097. * the nodes in the global entry_guards list. See control-spec.txt
  1098. * for details.
  1099. * For backward compatibility, we also handle the string "helper-nodes".
  1100. * */
  1101. int
  1102. getinfo_helper_entry_guards(control_connection_t *conn,
  1103. const char *question, char **answer,
  1104. const char **errmsg)
  1105. {
  1106. (void) conn;
  1107. (void) errmsg;
  1108. if (!strcmp(question,"entry-guards") ||
  1109. !strcmp(question,"helper-nodes")) {
  1110. smartlist_t *sl = smartlist_new();
  1111. char tbuf[ISO_TIME_LEN+1];
  1112. char nbuf[MAX_VERBOSE_NICKNAME_LEN+1];
  1113. if (!entry_guards)
  1114. entry_guards = smartlist_new();
  1115. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) {
  1116. const char *status = NULL;
  1117. time_t when = 0;
  1118. const node_t *node;
  1119. if (!e->made_contact) {
  1120. status = "never-connected";
  1121. } else if (e->bad_since) {
  1122. when = e->bad_since;
  1123. status = "unusable";
  1124. } else {
  1125. status = "up";
  1126. }
  1127. node = node_get_by_id(e->identity);
  1128. if (node) {
  1129. node_get_verbose_nickname(node, nbuf);
  1130. } else {
  1131. nbuf[0] = '$';
  1132. base16_encode(nbuf+1, sizeof(nbuf)-1, e->identity, DIGEST_LEN);
  1133. /* e->nickname field is not very reliable if we don't know about
  1134. * this router any longer; don't include it. */
  1135. }
  1136. if (when) {
  1137. format_iso_time(tbuf, when);
  1138. smartlist_add_asprintf(sl, "%s %s %s\n", nbuf, status, tbuf);
  1139. } else {
  1140. smartlist_add_asprintf(sl, "%s %s\n", nbuf, status);
  1141. }
  1142. } SMARTLIST_FOREACH_END(e);
  1143. *answer = smartlist_join_strings(sl, "", 0, NULL);
  1144. SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
  1145. smartlist_free(sl);
  1146. }
  1147. return 0;
  1148. }
  1149. /** A list of configured bridges. Whenever we actually get a descriptor
  1150. * for one, we add it as an entry guard. Note that the order of bridges
  1151. * in this list does not necessarily correspond to the order of bridges
  1152. * in the torrc. */
  1153. static smartlist_t *bridge_list = NULL;
  1154. /** Mark every entry of the bridge list to be removed on our next call to
  1155. * sweep_bridge_list unless it has first been un-marked. */
  1156. void
  1157. mark_bridge_list(void)
  1158. {
  1159. if (!bridge_list)
  1160. bridge_list = smartlist_new();
  1161. SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b,
  1162. b->marked_for_removal = 1);
  1163. }
  1164. /** Remove every entry of the bridge list that was marked with
  1165. * mark_bridge_list if it has not subsequently been un-marked. */
  1166. void
  1167. sweep_bridge_list(void)
  1168. {
  1169. if (!bridge_list)
  1170. bridge_list = smartlist_new();
  1171. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
  1172. if (b->marked_for_removal) {
  1173. SMARTLIST_DEL_CURRENT(bridge_list, b);
  1174. bridge_free(b);
  1175. }
  1176. } SMARTLIST_FOREACH_END(b);
  1177. }
  1178. /** Initialize the bridge list to empty, creating it if needed. */
  1179. static void
  1180. clear_bridge_list(void)
  1181. {
  1182. if (!bridge_list)
  1183. bridge_list = smartlist_new();
  1184. SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b, bridge_free(b));
  1185. smartlist_clear(bridge_list);
  1186. }
  1187. /** Free the bridge <b>bridge</b>. */
  1188. static void
  1189. bridge_free(bridge_info_t *bridge)
  1190. {
  1191. if (!bridge)
  1192. return;
  1193. tor_free(bridge->transport_name);
  1194. tor_free(bridge);
  1195. }
  1196. /** If we have a bridge configured whose digest matches <b>digest</b>, or a
  1197. * bridge with no known digest whose address matches any of the
  1198. * tor_addr_port_t's in <b>orports</b>, return that bridge. Else return
  1199. * NULL. */
  1200. static bridge_info_t *
  1201. get_configured_bridge_by_orports_digest(const char *digest,
  1202. const smartlist_t *orports)
  1203. {
  1204. if (!bridge_list)
  1205. return NULL;
  1206. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
  1207. {
  1208. if (tor_digest_is_zero(bridge->identity)) {
  1209. SMARTLIST_FOREACH_BEGIN(orports, tor_addr_port_t *, ap)
  1210. {
  1211. if (tor_addr_compare(&bridge->addr, &ap->addr, CMP_EXACT) == 0 &&
  1212. bridge->port == ap->port)
  1213. return bridge;
  1214. }
  1215. SMARTLIST_FOREACH_END(ap);
  1216. }
  1217. if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
  1218. return bridge;
  1219. }
  1220. SMARTLIST_FOREACH_END(bridge);
  1221. return NULL;
  1222. }
  1223. /** If we have a bridge configured whose digest matches <b>digest</b>, or a
  1224. * bridge with no known digest whose address matches <b>addr</b>:<b>/port</b>,
  1225. * return that bridge. Else return NULL. */
  1226. static bridge_info_t *
  1227. get_configured_bridge_by_addr_port_digest(const tor_addr_t *addr,
  1228. uint16_t port,
  1229. const char *digest)
  1230. {
  1231. if (!bridge_list)
  1232. return NULL;
  1233. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
  1234. {
  1235. if (tor_digest_is_zero(bridge->identity) &&
  1236. !tor_addr_compare(&bridge->addr, addr, CMP_EXACT) &&
  1237. bridge->port == port)
  1238. return bridge;
  1239. if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
  1240. return bridge;
  1241. }
  1242. SMARTLIST_FOREACH_END(bridge);
  1243. return NULL;
  1244. }
  1245. /** Wrapper around get_configured_bridge_by_addr_port_digest() to look
  1246. * it up via router descriptor <b>ri</b>. */
  1247. static bridge_info_t *
  1248. get_configured_bridge_by_routerinfo(const routerinfo_t *ri)
  1249. {
  1250. bridge_info_t *bi = NULL;
  1251. smartlist_t *orports = router_get_all_orports(ri);
  1252. bi = get_configured_bridge_by_orports_digest(ri->cache_info.identity_digest,
  1253. orports);
  1254. SMARTLIST_FOREACH(orports, tor_addr_port_t *, p, tor_free(p));
  1255. smartlist_free(orports);
  1256. return bi;
  1257. }
  1258. /** Return 1 if <b>ri</b> is one of our known bridges, else 0. */
  1259. int
  1260. routerinfo_is_a_configured_bridge(const routerinfo_t *ri)
  1261. {
  1262. return get_configured_bridge_by_routerinfo(ri) ? 1 : 0;
  1263. }
  1264. /** Return 1 if <b>node</b> is one of our configured bridges, else 0. */
  1265. int
  1266. node_is_a_configured_bridge(const node_t *node)
  1267. {
  1268. int retval = 0;
  1269. smartlist_t *orports = node_get_all_orports(node);
  1270. retval = get_configured_bridge_by_orports_digest(node->identity,
  1271. orports) != NULL;
  1272. SMARTLIST_FOREACH(orports, tor_addr_port_t *, p, tor_free(p));
  1273. smartlist_free(orports);
  1274. return retval;
  1275. }
  1276. /** We made a connection to a router at <b>addr</b>:<b>port</b>
  1277. * without knowing its digest. Its digest turned out to be <b>digest</b>.
  1278. * If it was a bridge, and we still don't know its digest, record it.
  1279. */
  1280. void
  1281. learned_router_identity(const tor_addr_t *addr, uint16_t port,
  1282. const char *digest)
  1283. {
  1284. bridge_info_t *bridge =
  1285. get_configured_bridge_by_addr_port_digest(addr, port, digest);
  1286. if (bridge && tor_digest_is_zero(bridge->identity)) {
  1287. memcpy(bridge->identity, digest, DIGEST_LEN);
  1288. log_notice(LD_DIR, "Learned fingerprint %s for bridge %s",
  1289. hex_str(digest, DIGEST_LEN), fmt_addrport(addr, port));
  1290. }
  1291. }
  1292. /** Return true if <b>bridge</b> has the same identity digest as
  1293. * <b>digest</b>. If <b>digest</b> is NULL, it matches
  1294. * bridges with unspecified identity digests. */
  1295. static int
  1296. bridge_has_digest(const bridge_info_t *bridge, const char *digest)
  1297. {
  1298. if (digest)
  1299. return tor_memeq(digest, bridge->identity, DIGEST_LEN);
  1300. else
  1301. return tor_digest_is_zero(bridge->identity);
  1302. }
  1303. /** We are about to add a new bridge at <b>addr</b>:<b>port</b>, with optional
  1304. * <b>digest</b> and <b>transport_name</b>. Mark for removal any previously
  1305. * existing bridge with the same address and port, and warn the user as
  1306. * appropriate.
  1307. */
  1308. static void
  1309. bridge_resolve_conflicts(const tor_addr_t *addr, uint16_t port,
  1310. const char *digest, const char *transport_name)
  1311. {
  1312. /* Iterate the already-registered bridge list:
  1313. If you find a bridge with the same adress and port, mark it for
  1314. removal. It doesn't make sense to have two active bridges with
  1315. the same IP:PORT. If the bridge in question has a different
  1316. digest or transport than <b>digest</b>/<b>transport_name</b>,
  1317. it's probably a misconfiguration and we should warn the user.
  1318. */
  1319. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge) {
  1320. if (bridge->marked_for_removal)
  1321. continue;
  1322. if (tor_addr_eq(&bridge->addr, addr) && (bridge->port == port)) {
  1323. bridge->marked_for_removal = 1;
  1324. if (!bridge_has_digest(bridge, digest) ||
  1325. strcmp_opt(bridge->transport_name, transport_name)) {
  1326. /* warn the user */
  1327. char *bridge_description_new, *bridge_description_old;
  1328. tor_asprintf(&bridge_description_new, "%s:%s:%s",
  1329. fmt_addrport(addr, port),
  1330. digest ? hex_str(digest, DIGEST_LEN) : "",
  1331. transport_name ? transport_name : "");
  1332. tor_asprintf(&bridge_description_old, "%s:%s:%s",
  1333. fmt_addrport(&bridge->addr, bridge->port),
  1334. tor_digest_is_zero(bridge->identity) ?
  1335. "" : hex_str(bridge->identity,DIGEST_LEN),
  1336. bridge->transport_name ? bridge->transport_name : "");
  1337. log_warn(LD_GENERAL,"Tried to add bridge '%s', but we found a conflict"
  1338. " with the already registered bridge '%s'. We will discard"
  1339. " the old bridge and keep '%s'. If this is not what you"
  1340. " wanted, please change your configuration file accordingly.",
  1341. bridge_description_new, bridge_description_old,
  1342. bridge_description_new);
  1343. tor_free(bridge_description_new);
  1344. tor_free(bridge_description_old);
  1345. }
  1346. }
  1347. } SMARTLIST_FOREACH_END(bridge);
  1348. }
  1349. /** Remember a new bridge at <b>addr</b>:<b>port</b>. If <b>digest</b>
  1350. * is set, it tells us the identity key too. If we already had the
  1351. * bridge in our list, unmark it, and don't actually add anything new.
  1352. * If <b>transport_name</b> is non-NULL - the bridge is associated with a
  1353. * pluggable transport - we assign the transport to the bridge. */
  1354. void
  1355. bridge_add_from_config(const tor_addr_t *addr, uint16_t port,
  1356. const char *digest, const char *transport_name)
  1357. {
  1358. bridge_info_t *b;
  1359. bridge_resolve_conflicts(addr, port, digest, transport_name);
  1360. b = tor_malloc_zero(sizeof(bridge_info_t));
  1361. tor_addr_copy(&b->addr, addr);
  1362. b->port = port;
  1363. if (digest)
  1364. memcpy(b->identity, digest, DIGEST_LEN);
  1365. if (transport_name)
  1366. b->transport_name = tor_strdup(transport_name);
  1367. b->fetch_status.schedule = DL_SCHED_BRIDGE;
  1368. if (!bridge_list)
  1369. bridge_list = smartlist_new();
  1370. smartlist_add(bridge_list, b);
  1371. }
  1372. /** Return true iff <b>routerset</b> contains the bridge <b>bridge</b>. */
  1373. static int
  1374. routerset_contains_bridge(const routerset_t *routerset,
  1375. const bridge_info_t *bridge)
  1376. {
  1377. int result;
  1378. extend_info_t *extinfo;
  1379. tor_assert(bridge);
  1380. if (!routerset)
  1381. return 0;
  1382. extinfo = extend_info_new(
  1383. NULL, bridge->identity, NULL, &bridge->addr, bridge->port);
  1384. result = routerset_contains_extendinfo(routerset, extinfo);
  1385. extend_info_free(extinfo);
  1386. return result;
  1387. }
  1388. /** If <b>digest</b> is one of our known bridges, return it. */
  1389. static bridge_info_t *
  1390. find_bridge_by_digest(const char *digest)
  1391. {
  1392. SMARTLIST_FOREACH(bridge_list, bridge_info_t *, bridge,
  1393. {
  1394. if (tor_memeq(bridge->identity, digest, DIGEST_LEN))
  1395. return bridge;
  1396. });
  1397. return NULL;
  1398. }
  1399. /* DOCDOC find_transport_name_by_bridge_addrport */
  1400. const char *
  1401. find_transport_name_by_bridge_addrport(const tor_addr_t *addr, uint16_t port)
  1402. {
  1403. if (!bridge_list)
  1404. return NULL;
  1405. SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
  1406. if (tor_addr_eq(&bridge->addr, addr) &&
  1407. (bridge->port == port))
  1408. return bridge->transport_name;
  1409. } SMARTLIST_FOREACH_END(bridge);
  1410. return NULL;
  1411. }
  1412. /** If <b>addr</b> and <b>port</b> match the address and port of a
  1413. * bridge of ours that uses pluggable transports, place its transport
  1414. * in <b>transport</b>.
  1415. *
  1416. * Return 0 on success (found a transport, or found a bridge with no
  1417. * transport, or found no bridge); return -1 if we should be using a
  1418. * transport, but the transport could not be found.
  1419. */
  1420. int
  1421. find_transport_by_bridge_addrport(const tor_addr_t *addr, uint16_t port,
  1422. const transport_t **transport)
  1423. {
  1424. *transport = NULL;
  1425. if (!bridge_list)
  1426. return 0;
  1427. SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
  1428. if (tor_addr_eq(&bridge->addr, addr) &&
  1429. (bridge->port == port)) { /* bridge matched */
  1430. if (bridge->transport_name) { /* it also uses pluggable transports */
  1431. *transport = transport_get_by_name(bridge->transport_name);
  1432. if (*transport == NULL) { /* it uses pluggable transports, but
  1433. the transport could not be found! */
  1434. return -1;
  1435. }
  1436. return 0;
  1437. } else { /* bridge matched, but it doesn't use transports. */
  1438. break;
  1439. }
  1440. }
  1441. } SMARTLIST_FOREACH_END(bridge);
  1442. *transport = NULL;
  1443. return 0;
  1444. }
  1445. /** We need to ask <b>bridge</b> for its server descriptor. */
  1446. static void
  1447. launch_direct_bridge_descriptor_fetch(bridge_info_t *bridge)
  1448. {
  1449. char *address;
  1450. const or_options_t *options = get_options();
  1451. if (connection_get_by_type_addr_port_purpose(
  1452. CONN_TYPE_DIR, &bridge->addr, bridge->port,
  1453. DIR_PURPOSE_FETCH_SERVERDESC))
  1454. return; /* it's already on the way */
  1455. if (routerset_contains_bridge(options->ExcludeNodes, bridge)) {
  1456. download_status_mark_impossible(&bridge->fetch_status);
  1457. log_warn(LD_APP, "Not using bridge at %s: it is in ExcludeNodes.",
  1458. safe_str_client(fmt_and_decorate_addr(&bridge->addr)));
  1459. return;
  1460. }
  1461. address = tor_dup_addr(&bridge->addr);
  1462. directory_initiate_command(address, &bridge->addr,
  1463. bridge->port, 0/*no dirport*/,
  1464. bridge->identity,
  1465. DIR_PURPOSE_FETCH_SERVERDESC,
  1466. ROUTER_PURPOSE_BRIDGE,
  1467. DIRIND_ONEHOP, "authority.z", NULL, 0, 0);
  1468. tor_free(address);
  1469. }
  1470. /** Fetching the bridge descriptor from the bridge authority returned a
  1471. * "not found". Fall back to trying a direct fetch. */
  1472. void
  1473. retry_bridge_descriptor_fetch_directly(const char *digest)
  1474. {
  1475. bridge_info_t *bridge = find_bridge_by_digest(digest);
  1476. if (!bridge)
  1477. return; /* not found? oh well. */
  1478. launch_direct_bridge_descriptor_fetch(bridge);
  1479. }
  1480. /** For each bridge in our list for which we don't currently have a
  1481. * descriptor, fetch a new copy of its descriptor -- either directly
  1482. * from the bridge or via a bridge authority. */
  1483. void
  1484. fetch_bridge_descriptors(const or_options_t *options, time_t now)
  1485. {
  1486. int num_bridge_auths = get_n_authorities(BRIDGE_DIRINFO);
  1487. int ask_bridge_directly;
  1488. int can_use_bridge_authority;
  1489. if (!bridge_list)
  1490. return;
  1491. /* If we still have unconfigured managed proxies, don't go and
  1492. connect to a bridge. */
  1493. if (pt_proxies_configuration_pending())
  1494. return;
  1495. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
  1496. {
  1497. if (!download_status_is_ready(&bridge->fetch_status, now,
  1498. IMPOSSIBLE_TO_DOWNLOAD))
  1499. continue; /* don't bother, no need to retry yet */
  1500. if (routerset_contains_bridge(options->ExcludeNodes, bridge)) {
  1501. download_status_mark_impossible(&bridge->fetch_status);
  1502. log_warn(LD_APP, "Not using bridge at %s: it is in ExcludeNodes.",
  1503. safe_str_client(fmt_and_decorate_addr(&bridge->addr)));
  1504. continue;
  1505. }
  1506. /* schedule another fetch as if this one will fail, in case it does */
  1507. download_status_failed(&bridge->fetch_status, 0);
  1508. can_use_bridge_authority = !tor_digest_is_zero(bridge->identity) &&
  1509. num_bridge_auths;
  1510. ask_bridge_directly = !can_use_bridge_authority ||
  1511. !options->UpdateBridgesFromAuthority;
  1512. log_debug(LD_DIR, "ask_bridge_directly=%d (%d, %d, %d)",
  1513. ask_bridge_directly, tor_digest_is_zero(bridge->identity),
  1514. !options->UpdateBridgesFromAuthority, !num_bridge_auths);
  1515. if (ask_bridge_directly &&
  1516. !fascist_firewall_allows_address_or(&bridge->addr, bridge->port)) {
  1517. log_notice(LD_DIR, "Bridge at '%s' isn't reachable by our "
  1518. "firewall policy. %s.",
  1519. fmt_addrport(&bridge->addr, bridge->port),
  1520. can_use_bridge_authority ?
  1521. "Asking bridge authority instead" : "Skipping");
  1522. if (can_use_bridge_authority)
  1523. ask_bridge_directly = 0;
  1524. else
  1525. continue;
  1526. }
  1527. if (ask_bridge_directly) {
  1528. /* we need to ask the bridge itself for its descriptor. */
  1529. launch_direct_bridge_descriptor_fetch(bridge);
  1530. } else {
  1531. /* We have a digest and we want to ask an authority. We could
  1532. * combine all the requests into one, but that may give more
  1533. * hints to the bridge authority than we want to give. */
  1534. char resource[10 + HEX_DIGEST_LEN];
  1535. memcpy(resource, "fp/", 3);
  1536. base16_encode(resource+3, HEX_DIGEST_LEN+1,
  1537. bridge->identity, DIGEST_LEN);
  1538. memcpy(resource+3+HEX_DIGEST_LEN, ".z", 3);
  1539. log_info(LD_DIR, "Fetching bridge info '%s' from bridge authority.",
  1540. resource);
  1541. directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
  1542. ROUTER_PURPOSE_BRIDGE, resource, 0);
  1543. }
  1544. }
  1545. SMARTLIST_FOREACH_END(bridge);
  1546. }
  1547. /** If our <b>bridge</b> is configured to be a different address than
  1548. * the bridge gives in <b>node</b>, rewrite the routerinfo
  1549. * we received to use the address we meant to use. Now we handle
  1550. * multihomed bridges better.
  1551. */
  1552. static void
  1553. rewrite_node_address_for_bridge(const bridge_info_t *bridge, node_t *node)
  1554. {
  1555. /* XXXX move this function. */
  1556. /* XXXX overridden addresses should really live in the node_t, so that the
  1557. * routerinfo_t and the microdesc_t can be immutable. But we can only
  1558. * do that safely if we know that no function that connects to an OR
  1559. * does so through an address from any source other than node_get_addr().
  1560. */
  1561. tor_addr_t addr;
  1562. if (node->ri) {
  1563. routerinfo_t *ri = node->ri;
  1564. tor_addr_from_ipv4h(&addr, ri->addr);
  1565. if ((!tor_addr_compare(&bridge->addr, &addr, CMP_EXACT) &&
  1566. bridge->port == ri->or_port) ||
  1567. (!tor_addr_compare(&bridge->addr, &ri->ipv6_addr, CMP_EXACT) &&
  1568. bridge->port == ri->ipv6_orport)) {
  1569. /* they match, so no need to do anything */
  1570. } else {
  1571. if (tor_addr_family(&bridge->addr) == AF_INET) {
  1572. ri->addr = tor_addr_to_ipv4h(&bridge->addr);
  1573. tor_free(ri->address);
  1574. ri->address = tor_dup_ip(ri->addr);
  1575. ri->or_port = bridge->port;
  1576. log_info(LD_DIR,
  1577. "Adjusted bridge routerinfo for '%s' to match configured "
  1578. "address %s:%d.",
  1579. ri->nickname, ri->address, ri->or_port);
  1580. } else if (tor_addr_family(&bridge->addr) == AF_INET6) {
  1581. tor_addr_copy(&ri->ipv6_addr, &bridge->addr);
  1582. ri->ipv6_orport = bridge->port;
  1583. log_info(LD_DIR,
  1584. "Adjusted bridge routerinfo for '%s' to match configured "
  1585. "address %s.",
  1586. ri->nickname, fmt_addrport(&ri->ipv6_addr, ri->ipv6_orport));
  1587. } else {
  1588. log_err(LD_BUG, "Address family not supported: %d.",
  1589. tor_addr_family(&bridge->addr));
  1590. return;
  1591. }
  1592. }
  1593. /* Mark which address to use based on which bridge_t we got. */
  1594. node->ipv6_preferred = (tor_addr_family(&bridge->addr) == AF_INET6 &&
  1595. !tor_addr_is_null(&node->ri->ipv6_addr));
  1596. /* XXXipv6 we lack support for falling back to another address for
  1597. the same relay, warn the user */
  1598. if (!tor_addr_is_null(&ri->ipv6_addr)) {
  1599. tor_addr_port_t ap;
  1600. node_get_pref_orport(node, &ap);
  1601. log_notice(LD_CONFIG,
  1602. "Bridge '%s' has both an IPv4 and an IPv6 address. "
  1603. "Will prefer using its %s address (%s).",
  1604. ri->nickname,
  1605. tor_addr_family(&ap.addr) == AF_INET6 ? "IPv6" : "IPv4",
  1606. fmt_addrport(&ap.addr, ap.port));
  1607. }
  1608. }
  1609. if (node->rs) {
  1610. routerstatus_t *rs = node->rs;
  1611. tor_addr_from_ipv4h(&addr, rs->addr);
  1612. if (!tor_addr_compare(&bridge->addr, &addr, CMP_EXACT) &&
  1613. bridge->port == rs->or_port) {
  1614. /* they match, so no need to do anything */
  1615. } else {
  1616. rs->addr = tor_addr_to_ipv4h(&bridge->addr);
  1617. rs->or_port = bridge->port;
  1618. log_info(LD_DIR,
  1619. "Adjusted bridge routerstatus for '%s' to match "
  1620. "configured address %s.",
  1621. rs->nickname, fmt_addrport(&bridge->addr, rs->or_port));
  1622. }
  1623. }
  1624. }
  1625. /** We just learned a descriptor for a bridge. See if that
  1626. * digest is in our entry guard list, and add it if not. */
  1627. void
  1628. learned_bridge_descriptor(routerinfo_t *ri, int from_cache)
  1629. {
  1630. tor_assert(ri);
  1631. tor_assert(ri->purpose == ROUTER_PURPOSE_BRIDGE);
  1632. if (get_options()->UseBridges) {
  1633. int first = !any_bridge_descriptors_known();
  1634. bridge_info_t *bridge = get_configured_bridge_by_routerinfo(ri);
  1635. time_t now = time(NULL);
  1636. router_set_status(ri->cache_info.identity_digest, 1);
  1637. if (bridge) { /* if we actually want to use this one */
  1638. node_t *node;
  1639. /* it's here; schedule its re-fetch for a long time from now. */
  1640. if (!from_cache)
  1641. download_status_reset(&bridge->fetch_status);
  1642. node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  1643. tor_assert(node);
  1644. rewrite_node_address_for_bridge(bridge, node);
  1645. add_an_entry_guard(node, 1, 1);
  1646. log_notice(LD_DIR, "new bridge descriptor '%s' (%s): %s", ri->nickname,
  1647. from_cache ? "cached" : "fresh", router_describe(ri));
  1648. /* set entry->made_contact so if it goes down we don't drop it from
  1649. * our entry node list */
  1650. entry_guard_register_connect_status(ri->cache_info.identity_digest,
  1651. 1, 0, now);
  1652. if (first)
  1653. routerlist_retry_directory_downloads(now);
  1654. }
  1655. }
  1656. }
  1657. /** Return 1 if any of our entry guards have descriptors that
  1658. * are marked with purpose 'bridge' and are running. Else return 0.
  1659. *
  1660. * We use this function to decide if we're ready to start building
  1661. * circuits through our bridges, or if we need to wait until the
  1662. * directory "server/authority" requests finish. */
  1663. int
  1664. any_bridge_descriptors_known(void)
  1665. {
  1666. tor_assert(get_options()->UseBridges);
  1667. return choose_random_entry(NULL)!=NULL ? 1 : 0;
  1668. }
  1669. /** Return 1 if there are any directory conns fetching bridge descriptors
  1670. * that aren't marked for close. We use this to guess if we should tell
  1671. * the controller that we have a problem. */
  1672. int
  1673. any_pending_bridge_descriptor_fetches(void)
  1674. {
  1675. smartlist_t *conns = get_connection_array();
  1676. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  1677. if (conn->type == CONN_TYPE_DIR &&
  1678. conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
  1679. TO_DIR_CONN(conn)->router_purpose == ROUTER_PURPOSE_BRIDGE &&
  1680. !conn->marked_for_close &&
  1681. conn->linked &&
  1682. conn->linked_conn && !conn->linked_conn->marked_for_close) {
  1683. log_debug(LD_DIR, "found one: %s", conn->address);
  1684. return 1;
  1685. }
  1686. } SMARTLIST_FOREACH_END(conn);
  1687. return 0;
  1688. }
  1689. /** Return 1 if we have at least one descriptor for an entry guard
  1690. * (bridge or member of EntryNodes) and all descriptors we know are
  1691. * down. Else return 0. If <b>act</b> is 1, then mark the down guards
  1692. * up; else just observe and report. */
  1693. static int
  1694. entries_retry_helper(const or_options_t *options, int act)
  1695. {
  1696. const node_t *node;
  1697. int any_known = 0;
  1698. int any_running = 0;
  1699. int need_bridges = options->UseBridges != 0;
  1700. if (!entry_guards)
  1701. entry_guards = smartlist_new();
  1702. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) {
  1703. node = node_get_by_id(e->identity);
  1704. if (node && node_has_descriptor(node) &&
  1705. node_is_bridge(node) == need_bridges) {
  1706. any_known = 1;
  1707. if (node->is_running)
  1708. any_running = 1; /* some entry is both known and running */
  1709. else if (act) {
  1710. /* Mark all current connections to this OR as unhealthy, since
  1711. * otherwise there could be one that started 30 seconds
  1712. * ago, and in 30 seconds it will time out, causing us to mark
  1713. * the node down and undermine the retry attempt. We mark even
  1714. * the established conns, since if the network just came back
  1715. * we'll want to attach circuits to fresh conns. */
  1716. connection_or_set_bad_connections(node->identity, 1);
  1717. /* mark this entry node for retry */
  1718. router_set_status(node->identity, 1);
  1719. e->can_retry = 1;
  1720. e->bad_since = 0;
  1721. }
  1722. }
  1723. } SMARTLIST_FOREACH_END(e);
  1724. log_debug(LD_DIR, "%d: any_known %d, any_running %d",
  1725. act, any_known, any_running);
  1726. return any_known && !any_running;
  1727. }
  1728. /** Do we know any descriptors for our bridges / entrynodes, and are
  1729. * all the ones we have descriptors for down? */
  1730. int
  1731. entries_known_but_down(const or_options_t *options)
  1732. {
  1733. tor_assert(entry_list_is_constrained(options));
  1734. return entries_retry_helper(options, 0);
  1735. }
  1736. /** Mark all down known bridges / entrynodes up. */
  1737. void
  1738. entries_retry_all(const or_options_t *options)
  1739. {
  1740. tor_assert(entry_list_is_constrained(options));
  1741. entries_retry_helper(options, 1);
  1742. }
  1743. /** Return true if we've ever had a bridge running a Tor version that can't
  1744. * provide microdescriptors to us. In that case fall back to asking for
  1745. * full descriptors. Eventually all bridges will support microdescriptors
  1746. * and we can take this check out; see bug 4013. */
  1747. int
  1748. any_bridges_dont_support_microdescriptors(void)
  1749. {
  1750. const node_t *node;
  1751. static int ever_answered_yes = 0;
  1752. if (!get_options()->UseBridges || !entry_guards)
  1753. return 0;
  1754. if (ever_answered_yes)
  1755. return 1; /* if we ever answer 'yes', always answer 'yes' */
  1756. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) {
  1757. node = node_get_by_id(e->identity);
  1758. if (node && node->ri &&
  1759. node_is_bridge(node) && node_is_a_configured_bridge(node) &&
  1760. !tor_version_supports_microdescriptors(node->ri->platform)) {
  1761. /* This is one of our current bridges, and we know enough about
  1762. * it to know that it won't be able to answer our microdescriptor
  1763. * questions. */
  1764. ever_answered_yes = 1;
  1765. return 1;
  1766. }
  1767. } SMARTLIST_FOREACH_END(e);
  1768. return 0;
  1769. }
  1770. /** Release all storage held by the list of entry guards and related
  1771. * memory structs. */
  1772. void
  1773. entry_guards_free_all(void)
  1774. {
  1775. if (entry_guards) {
  1776. SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
  1777. entry_guard_free(e));
  1778. smartlist_free(entry_guards);
  1779. entry_guards = NULL;
  1780. }
  1781. clear_bridge_list();
  1782. smartlist_free(bridge_list);
  1783. bridge_list = NULL;
  1784. circuit_build_times_free_timeouts(&circ_times);
  1785. }