entrynodes.c 67 KB

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