entrynodes.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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, timeouts;
  945. if (!node) {
  946. *msg = tor_strdup("Unable to parse entry nodes: "
  947. "EntryGuardPathBias without EntryGuard");
  948. break;
  949. }
  950. /* First try 3 params, then 2. */
  951. if (tor_sscanf(line->value, "%u %u %u", &success_cnt, &hop_cnt,
  952. &timeouts) != 3) {
  953. timeouts = 0;
  954. if (tor_sscanf(line->value, "%u %u", &success_cnt, &hop_cnt) != 2) {
  955. log_warn(LD_GENERAL, "Unable to parse guard path bias info: "
  956. "Misformated EntryGuardPathBias %s", escaped(line->value));
  957. continue;
  958. }
  959. }
  960. node->first_hops = hop_cnt;
  961. node->circuit_successes = success_cnt;
  962. node->timeouts = timeouts;
  963. log_info(LD_GENERAL, "Read %u/%u path bias for node %s",
  964. node->circuit_successes, node->first_hops, node->nickname);
  965. /* Note: We rely on the < comparison here to allow us to set a 0
  966. * rate and disable the feature entirely. If refactoring, don't
  967. * change to <= */
  968. if ((node->circuit_successes/((double)node->first_hops)
  969. < pathbias_get_crit_rate(options)) &&
  970. pathbias_get_dropguards(options)) {
  971. node->path_bias_disabled = 1;
  972. log_info(LD_GENERAL,
  973. "Path bias is too high (%u/%u); disabling node %s",
  974. node->circuit_successes, node->first_hops, node->nickname);
  975. }
  976. } else {
  977. log_warn(LD_BUG, "Unexpected key %s", line->key);
  978. }
  979. }
  980. SMARTLIST_FOREACH_BEGIN(new_entry_guards, entry_guard_t *, e) {
  981. char *sp;
  982. char *val = digestmap_get(added_by, e->identity);
  983. if (val && (sp = strchr(val, ' '))) {
  984. time_t when;
  985. *sp++ = '\0';
  986. if (parse_iso_time(sp, &when)<0) {
  987. log_warn(LD_BUG, "Can't read time %s in EntryGuardAddedBy", sp);
  988. } else {
  989. e->chosen_by_version = tor_strdup(val);
  990. e->chosen_on_date = when;
  991. }
  992. } else {
  993. if (state_version) {
  994. e->chosen_by_version = tor_strdup(state_version);
  995. e->chosen_on_date = time(NULL) - crypto_rand_int(3600*24*30);
  996. }
  997. }
  998. if (e->path_bias_disabled && !e->bad_since)
  999. e->bad_since = time(NULL);
  1000. }
  1001. SMARTLIST_FOREACH_END(e);
  1002. if (*msg || !set) {
  1003. SMARTLIST_FOREACH(new_entry_guards, entry_guard_t *, e,
  1004. entry_guard_free(e));
  1005. smartlist_free(new_entry_guards);
  1006. } else { /* !err && set */
  1007. if (entry_guards) {
  1008. SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
  1009. entry_guard_free(e));
  1010. smartlist_free(entry_guards);
  1011. }
  1012. entry_guards = new_entry_guards;
  1013. entry_guards_dirty = 0;
  1014. /* XXX024 hand new_entry_guards to this func, and move it up a
  1015. * few lines, so we don't have to re-dirty it */
  1016. if (remove_obsolete_entry_guards(now))
  1017. entry_guards_dirty = 1;
  1018. }
  1019. digestmap_free(added_by, tor_free_);
  1020. return *msg ? -1 : 0;
  1021. }
  1022. /** Our list of entry guards has changed, or some element of one
  1023. * of our entry guards has changed. Write the changes to disk within
  1024. * the next few minutes.
  1025. */
  1026. void
  1027. entry_guards_changed(void)
  1028. {
  1029. time_t when;
  1030. entry_guards_dirty = 1;
  1031. /* or_state_save() will call entry_guards_update_state(). */
  1032. when = get_options()->AvoidDiskWrites ? time(NULL) + 3600 : time(NULL)+600;
  1033. or_state_mark_dirty(get_or_state(), when);
  1034. }
  1035. /** If the entry guard info has not changed, do nothing and return.
  1036. * Otherwise, free the EntryGuards piece of <b>state</b> and create
  1037. * a new one out of the global entry_guards list, and then mark
  1038. * <b>state</b> dirty so it will get saved to disk.
  1039. */
  1040. void
  1041. entry_guards_update_state(or_state_t *state)
  1042. {
  1043. config_line_t **next, *line;
  1044. if (! entry_guards_dirty)
  1045. return;
  1046. config_free_lines(state->EntryGuards);
  1047. next = &state->EntryGuards;
  1048. *next = NULL;
  1049. if (!entry_guards)
  1050. entry_guards = smartlist_new();
  1051. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) {
  1052. char dbuf[HEX_DIGEST_LEN+1];
  1053. if (!e->made_contact)
  1054. continue; /* don't write this one to disk */
  1055. *next = line = tor_malloc_zero(sizeof(config_line_t));
  1056. line->key = tor_strdup("EntryGuard");
  1057. base16_encode(dbuf, sizeof(dbuf), e->identity, DIGEST_LEN);
  1058. tor_asprintf(&line->value, "%s %s", e->nickname, dbuf);
  1059. next = &(line->next);
  1060. if (e->unreachable_since) {
  1061. *next = line = tor_malloc_zero(sizeof(config_line_t));
  1062. line->key = tor_strdup("EntryGuardDownSince");
  1063. line->value = tor_malloc(ISO_TIME_LEN+1+ISO_TIME_LEN+1);
  1064. format_iso_time(line->value, e->unreachable_since);
  1065. if (e->last_attempted) {
  1066. line->value[ISO_TIME_LEN] = ' ';
  1067. format_iso_time(line->value+ISO_TIME_LEN+1, e->last_attempted);
  1068. }
  1069. next = &(line->next);
  1070. }
  1071. if (e->bad_since) {
  1072. *next = line = tor_malloc_zero(sizeof(config_line_t));
  1073. line->key = tor_strdup("EntryGuardUnlistedSince");
  1074. line->value = tor_malloc(ISO_TIME_LEN+1);
  1075. format_iso_time(line->value, e->bad_since);
  1076. next = &(line->next);
  1077. }
  1078. if (e->chosen_on_date && e->chosen_by_version &&
  1079. !strchr(e->chosen_by_version, ' ')) {
  1080. char d[HEX_DIGEST_LEN+1];
  1081. char t[ISO_TIME_LEN+1];
  1082. *next = line = tor_malloc_zero(sizeof(config_line_t));
  1083. line->key = tor_strdup("EntryGuardAddedBy");
  1084. base16_encode(d, sizeof(d), e->identity, DIGEST_LEN);
  1085. format_iso_time(t, e->chosen_on_date);
  1086. tor_asprintf(&line->value, "%s %s %s",
  1087. d, e->chosen_by_version, t);
  1088. next = &(line->next);
  1089. }
  1090. if (e->first_hops) {
  1091. *next = line = tor_malloc_zero(sizeof(config_line_t));
  1092. line->key = tor_strdup("EntryGuardPathBias");
  1093. tor_asprintf(&line->value, "%u %u %u",
  1094. e->circuit_successes, e->first_hops, e->timeouts);
  1095. next = &(line->next);
  1096. }
  1097. } SMARTLIST_FOREACH_END(e);
  1098. if (!get_options()->AvoidDiskWrites)
  1099. or_state_mark_dirty(get_or_state(), 0);
  1100. entry_guards_dirty = 0;
  1101. }
  1102. /** If <b>question</b> is the string "entry-guards", then dump
  1103. * to *<b>answer</b> a newly allocated string describing all of
  1104. * the nodes in the global entry_guards list. See control-spec.txt
  1105. * for details.
  1106. * For backward compatibility, we also handle the string "helper-nodes".
  1107. * */
  1108. int
  1109. getinfo_helper_entry_guards(control_connection_t *conn,
  1110. const char *question, char **answer,
  1111. const char **errmsg)
  1112. {
  1113. (void) conn;
  1114. (void) errmsg;
  1115. if (!strcmp(question,"entry-guards") ||
  1116. !strcmp(question,"helper-nodes")) {
  1117. smartlist_t *sl = smartlist_new();
  1118. char tbuf[ISO_TIME_LEN+1];
  1119. char nbuf[MAX_VERBOSE_NICKNAME_LEN+1];
  1120. if (!entry_guards)
  1121. entry_guards = smartlist_new();
  1122. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) {
  1123. const char *status = NULL;
  1124. time_t when = 0;
  1125. const node_t *node;
  1126. if (!e->made_contact) {
  1127. status = "never-connected";
  1128. } else if (e->bad_since) {
  1129. when = e->bad_since;
  1130. status = "unusable";
  1131. } else {
  1132. status = "up";
  1133. }
  1134. node = node_get_by_id(e->identity);
  1135. if (node) {
  1136. node_get_verbose_nickname(node, nbuf);
  1137. } else {
  1138. nbuf[0] = '$';
  1139. base16_encode(nbuf+1, sizeof(nbuf)-1, e->identity, DIGEST_LEN);
  1140. /* e->nickname field is not very reliable if we don't know about
  1141. * this router any longer; don't include it. */
  1142. }
  1143. if (when) {
  1144. format_iso_time(tbuf, when);
  1145. smartlist_add_asprintf(sl, "%s %s %s\n", nbuf, status, tbuf);
  1146. } else {
  1147. smartlist_add_asprintf(sl, "%s %s\n", nbuf, status);
  1148. }
  1149. } SMARTLIST_FOREACH_END(e);
  1150. *answer = smartlist_join_strings(sl, "", 0, NULL);
  1151. SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
  1152. smartlist_free(sl);
  1153. }
  1154. return 0;
  1155. }
  1156. /** A list of configured bridges. Whenever we actually get a descriptor
  1157. * for one, we add it as an entry guard. Note that the order of bridges
  1158. * in this list does not necessarily correspond to the order of bridges
  1159. * in the torrc. */
  1160. static smartlist_t *bridge_list = NULL;
  1161. /** Mark every entry of the bridge list to be removed on our next call to
  1162. * sweep_bridge_list unless it has first been un-marked. */
  1163. void
  1164. mark_bridge_list(void)
  1165. {
  1166. if (!bridge_list)
  1167. bridge_list = smartlist_new();
  1168. SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b,
  1169. b->marked_for_removal = 1);
  1170. }
  1171. /** Remove every entry of the bridge list that was marked with
  1172. * mark_bridge_list if it has not subsequently been un-marked. */
  1173. void
  1174. sweep_bridge_list(void)
  1175. {
  1176. if (!bridge_list)
  1177. bridge_list = smartlist_new();
  1178. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
  1179. if (b->marked_for_removal) {
  1180. SMARTLIST_DEL_CURRENT(bridge_list, b);
  1181. bridge_free(b);
  1182. }
  1183. } SMARTLIST_FOREACH_END(b);
  1184. }
  1185. /** Initialize the bridge list to empty, creating it if needed. */
  1186. static void
  1187. clear_bridge_list(void)
  1188. {
  1189. if (!bridge_list)
  1190. bridge_list = smartlist_new();
  1191. SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b, bridge_free(b));
  1192. smartlist_clear(bridge_list);
  1193. }
  1194. /** Free the bridge <b>bridge</b>. */
  1195. static void
  1196. bridge_free(bridge_info_t *bridge)
  1197. {
  1198. if (!bridge)
  1199. return;
  1200. tor_free(bridge->transport_name);
  1201. tor_free(bridge);
  1202. }
  1203. /** If we have a bridge configured whose digest matches <b>digest</b>, or a
  1204. * bridge with no known digest whose address matches any of the
  1205. * tor_addr_port_t's in <b>orports</b>, return that bridge. Else return
  1206. * NULL. */
  1207. static bridge_info_t *
  1208. get_configured_bridge_by_orports_digest(const char *digest,
  1209. const smartlist_t *orports)
  1210. {
  1211. if (!bridge_list)
  1212. return NULL;
  1213. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
  1214. {
  1215. if (tor_digest_is_zero(bridge->identity)) {
  1216. SMARTLIST_FOREACH_BEGIN(orports, tor_addr_port_t *, ap)
  1217. {
  1218. if (tor_addr_compare(&bridge->addr, &ap->addr, CMP_EXACT) == 0 &&
  1219. bridge->port == ap->port)
  1220. return bridge;
  1221. }
  1222. SMARTLIST_FOREACH_END(ap);
  1223. }
  1224. if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
  1225. return bridge;
  1226. }
  1227. SMARTLIST_FOREACH_END(bridge);
  1228. return NULL;
  1229. }
  1230. /** If we have a bridge configured whose digest matches <b>digest</b>, or a
  1231. * bridge with no known digest whose address matches <b>addr</b>:<b>/port</b>,
  1232. * return that bridge. Else return NULL. */
  1233. static bridge_info_t *
  1234. get_configured_bridge_by_addr_port_digest(const tor_addr_t *addr,
  1235. uint16_t port,
  1236. const char *digest)
  1237. {
  1238. if (!bridge_list)
  1239. return NULL;
  1240. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
  1241. {
  1242. if (tor_digest_is_zero(bridge->identity) &&
  1243. !tor_addr_compare(&bridge->addr, addr, CMP_EXACT) &&
  1244. bridge->port == port)
  1245. return bridge;
  1246. if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
  1247. return bridge;
  1248. }
  1249. SMARTLIST_FOREACH_END(bridge);
  1250. return NULL;
  1251. }
  1252. /** Wrapper around get_configured_bridge_by_addr_port_digest() to look
  1253. * it up via router descriptor <b>ri</b>. */
  1254. static bridge_info_t *
  1255. get_configured_bridge_by_routerinfo(const routerinfo_t *ri)
  1256. {
  1257. bridge_info_t *bi = NULL;
  1258. smartlist_t *orports = router_get_all_orports(ri);
  1259. bi = get_configured_bridge_by_orports_digest(ri->cache_info.identity_digest,
  1260. orports);
  1261. SMARTLIST_FOREACH(orports, tor_addr_port_t *, p, tor_free(p));
  1262. smartlist_free(orports);
  1263. return bi;
  1264. }
  1265. /** Return 1 if <b>ri</b> is one of our known bridges, else 0. */
  1266. int
  1267. routerinfo_is_a_configured_bridge(const routerinfo_t *ri)
  1268. {
  1269. return get_configured_bridge_by_routerinfo(ri) ? 1 : 0;
  1270. }
  1271. /** Return 1 if <b>node</b> is one of our configured bridges, else 0. */
  1272. int
  1273. node_is_a_configured_bridge(const node_t *node)
  1274. {
  1275. int retval = 0;
  1276. smartlist_t *orports = node_get_all_orports(node);
  1277. retval = get_configured_bridge_by_orports_digest(node->identity,
  1278. orports) != NULL;
  1279. SMARTLIST_FOREACH(orports, tor_addr_port_t *, p, tor_free(p));
  1280. smartlist_free(orports);
  1281. return retval;
  1282. }
  1283. /** We made a connection to a router at <b>addr</b>:<b>port</b>
  1284. * without knowing its digest. Its digest turned out to be <b>digest</b>.
  1285. * If it was a bridge, and we still don't know its digest, record it.
  1286. */
  1287. void
  1288. learned_router_identity(const tor_addr_t *addr, uint16_t port,
  1289. const char *digest)
  1290. {
  1291. bridge_info_t *bridge =
  1292. get_configured_bridge_by_addr_port_digest(addr, port, digest);
  1293. if (bridge && tor_digest_is_zero(bridge->identity)) {
  1294. memcpy(bridge->identity, digest, DIGEST_LEN);
  1295. log_notice(LD_DIR, "Learned fingerprint %s for bridge %s",
  1296. hex_str(digest, DIGEST_LEN), fmt_addrport(addr, port));
  1297. }
  1298. }
  1299. /** Return true if <b>bridge</b> has the same identity digest as
  1300. * <b>digest</b>. If <b>digest</b> is NULL, it matches
  1301. * bridges with unspecified identity digests. */
  1302. static int
  1303. bridge_has_digest(const bridge_info_t *bridge, const char *digest)
  1304. {
  1305. if (digest)
  1306. return tor_memeq(digest, bridge->identity, DIGEST_LEN);
  1307. else
  1308. return tor_digest_is_zero(bridge->identity);
  1309. }
  1310. /** We are about to add a new bridge at <b>addr</b>:<b>port</b>, with optional
  1311. * <b>digest</b> and <b>transport_name</b>. Mark for removal any previously
  1312. * existing bridge with the same address and port, and warn the user as
  1313. * appropriate.
  1314. */
  1315. static void
  1316. bridge_resolve_conflicts(const tor_addr_t *addr, uint16_t port,
  1317. const char *digest, const char *transport_name)
  1318. {
  1319. /* Iterate the already-registered bridge list:
  1320. If you find a bridge with the same adress and port, mark it for
  1321. removal. It doesn't make sense to have two active bridges with
  1322. the same IP:PORT. If the bridge in question has a different
  1323. digest or transport than <b>digest</b>/<b>transport_name</b>,
  1324. it's probably a misconfiguration and we should warn the user.
  1325. */
  1326. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge) {
  1327. if (bridge->marked_for_removal)
  1328. continue;
  1329. if (tor_addr_eq(&bridge->addr, addr) && (bridge->port == port)) {
  1330. bridge->marked_for_removal = 1;
  1331. if (!bridge_has_digest(bridge, digest) ||
  1332. strcmp_opt(bridge->transport_name, transport_name)) {
  1333. /* warn the user */
  1334. char *bridge_description_new, *bridge_description_old;
  1335. tor_asprintf(&bridge_description_new, "%s:%s:%s",
  1336. fmt_addrport(addr, port),
  1337. digest ? hex_str(digest, DIGEST_LEN) : "",
  1338. transport_name ? transport_name : "");
  1339. tor_asprintf(&bridge_description_old, "%s:%s:%s",
  1340. fmt_addrport(&bridge->addr, bridge->port),
  1341. tor_digest_is_zero(bridge->identity) ?
  1342. "" : hex_str(bridge->identity,DIGEST_LEN),
  1343. bridge->transport_name ? bridge->transport_name : "");
  1344. log_warn(LD_GENERAL,"Tried to add bridge '%s', but we found a conflict"
  1345. " with the already registered bridge '%s'. We will discard"
  1346. " the old bridge and keep '%s'. If this is not what you"
  1347. " wanted, please change your configuration file accordingly.",
  1348. bridge_description_new, bridge_description_old,
  1349. bridge_description_new);
  1350. tor_free(bridge_description_new);
  1351. tor_free(bridge_description_old);
  1352. }
  1353. }
  1354. } SMARTLIST_FOREACH_END(bridge);
  1355. }
  1356. /** Remember a new bridge at <b>addr</b>:<b>port</b>. If <b>digest</b>
  1357. * is set, it tells us the identity key too. If we already had the
  1358. * bridge in our list, unmark it, and don't actually add anything new.
  1359. * If <b>transport_name</b> is non-NULL - the bridge is associated with a
  1360. * pluggable transport - we assign the transport to the bridge. */
  1361. void
  1362. bridge_add_from_config(const tor_addr_t *addr, uint16_t port,
  1363. const char *digest, const char *transport_name)
  1364. {
  1365. bridge_info_t *b;
  1366. bridge_resolve_conflicts(addr, port, digest, transport_name);
  1367. b = tor_malloc_zero(sizeof(bridge_info_t));
  1368. tor_addr_copy(&b->addr, addr);
  1369. b->port = port;
  1370. if (digest)
  1371. memcpy(b->identity, digest, DIGEST_LEN);
  1372. if (transport_name)
  1373. b->transport_name = tor_strdup(transport_name);
  1374. b->fetch_status.schedule = DL_SCHED_BRIDGE;
  1375. if (!bridge_list)
  1376. bridge_list = smartlist_new();
  1377. smartlist_add(bridge_list, b);
  1378. }
  1379. /** Return true iff <b>routerset</b> contains the bridge <b>bridge</b>. */
  1380. static int
  1381. routerset_contains_bridge(const routerset_t *routerset,
  1382. const bridge_info_t *bridge)
  1383. {
  1384. int result;
  1385. extend_info_t *extinfo;
  1386. tor_assert(bridge);
  1387. if (!routerset)
  1388. return 0;
  1389. extinfo = extend_info_new(
  1390. NULL, bridge->identity, NULL, &bridge->addr, bridge->port);
  1391. result = routerset_contains_extendinfo(routerset, extinfo);
  1392. extend_info_free(extinfo);
  1393. return result;
  1394. }
  1395. /** If <b>digest</b> is one of our known bridges, return it. */
  1396. static bridge_info_t *
  1397. find_bridge_by_digest(const char *digest)
  1398. {
  1399. SMARTLIST_FOREACH(bridge_list, bridge_info_t *, bridge,
  1400. {
  1401. if (tor_memeq(bridge->identity, digest, DIGEST_LEN))
  1402. return bridge;
  1403. });
  1404. return NULL;
  1405. }
  1406. /* DOCDOC find_transport_name_by_bridge_addrport */
  1407. const char *
  1408. find_transport_name_by_bridge_addrport(const tor_addr_t *addr, uint16_t port)
  1409. {
  1410. if (!bridge_list)
  1411. return NULL;
  1412. SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
  1413. if (tor_addr_eq(&bridge->addr, addr) &&
  1414. (bridge->port == port))
  1415. return bridge->transport_name;
  1416. } SMARTLIST_FOREACH_END(bridge);
  1417. return NULL;
  1418. }
  1419. /** If <b>addr</b> and <b>port</b> match the address and port of a
  1420. * bridge of ours that uses pluggable transports, place its transport
  1421. * in <b>transport</b>.
  1422. *
  1423. * Return 0 on success (found a transport, or found a bridge with no
  1424. * transport, or found no bridge); return -1 if we should be using a
  1425. * transport, but the transport could not be found.
  1426. */
  1427. int
  1428. find_transport_by_bridge_addrport(const tor_addr_t *addr, uint16_t port,
  1429. const transport_t **transport)
  1430. {
  1431. *transport = NULL;
  1432. if (!bridge_list)
  1433. return 0;
  1434. SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
  1435. if (tor_addr_eq(&bridge->addr, addr) &&
  1436. (bridge->port == port)) { /* bridge matched */
  1437. if (bridge->transport_name) { /* it also uses pluggable transports */
  1438. *transport = transport_get_by_name(bridge->transport_name);
  1439. if (*transport == NULL) { /* it uses pluggable transports, but
  1440. the transport could not be found! */
  1441. return -1;
  1442. }
  1443. return 0;
  1444. } else { /* bridge matched, but it doesn't use transports. */
  1445. break;
  1446. }
  1447. }
  1448. } SMARTLIST_FOREACH_END(bridge);
  1449. *transport = NULL;
  1450. return 0;
  1451. }
  1452. /** We need to ask <b>bridge</b> for its server descriptor. */
  1453. static void
  1454. launch_direct_bridge_descriptor_fetch(bridge_info_t *bridge)
  1455. {
  1456. char *address;
  1457. const or_options_t *options = get_options();
  1458. if (connection_get_by_type_addr_port_purpose(
  1459. CONN_TYPE_DIR, &bridge->addr, bridge->port,
  1460. DIR_PURPOSE_FETCH_SERVERDESC))
  1461. return; /* it's already on the way */
  1462. if (routerset_contains_bridge(options->ExcludeNodes, bridge)) {
  1463. download_status_mark_impossible(&bridge->fetch_status);
  1464. log_warn(LD_APP, "Not using bridge at %s: it is in ExcludeNodes.",
  1465. safe_str_client(fmt_and_decorate_addr(&bridge->addr)));
  1466. return;
  1467. }
  1468. address = tor_dup_addr(&bridge->addr);
  1469. directory_initiate_command(address, &bridge->addr,
  1470. bridge->port, 0/*no dirport*/,
  1471. bridge->identity,
  1472. DIR_PURPOSE_FETCH_SERVERDESC,
  1473. ROUTER_PURPOSE_BRIDGE,
  1474. DIRIND_ONEHOP, "authority.z", NULL, 0, 0);
  1475. tor_free(address);
  1476. }
  1477. /** Fetching the bridge descriptor from the bridge authority returned a
  1478. * "not found". Fall back to trying a direct fetch. */
  1479. void
  1480. retry_bridge_descriptor_fetch_directly(const char *digest)
  1481. {
  1482. bridge_info_t *bridge = find_bridge_by_digest(digest);
  1483. if (!bridge)
  1484. return; /* not found? oh well. */
  1485. launch_direct_bridge_descriptor_fetch(bridge);
  1486. }
  1487. /** For each bridge in our list for which we don't currently have a
  1488. * descriptor, fetch a new copy of its descriptor -- either directly
  1489. * from the bridge or via a bridge authority. */
  1490. void
  1491. fetch_bridge_descriptors(const or_options_t *options, time_t now)
  1492. {
  1493. int num_bridge_auths = get_n_authorities(BRIDGE_DIRINFO);
  1494. int ask_bridge_directly;
  1495. int can_use_bridge_authority;
  1496. if (!bridge_list)
  1497. return;
  1498. /* If we still have unconfigured managed proxies, don't go and
  1499. connect to a bridge. */
  1500. if (pt_proxies_configuration_pending())
  1501. return;
  1502. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
  1503. {
  1504. if (!download_status_is_ready(&bridge->fetch_status, now,
  1505. IMPOSSIBLE_TO_DOWNLOAD))
  1506. continue; /* don't bother, no need to retry yet */
  1507. if (routerset_contains_bridge(options->ExcludeNodes, bridge)) {
  1508. download_status_mark_impossible(&bridge->fetch_status);
  1509. log_warn(LD_APP, "Not using bridge at %s: it is in ExcludeNodes.",
  1510. safe_str_client(fmt_and_decorate_addr(&bridge->addr)));
  1511. continue;
  1512. }
  1513. /* schedule another fetch as if this one will fail, in case it does */
  1514. download_status_failed(&bridge->fetch_status, 0);
  1515. can_use_bridge_authority = !tor_digest_is_zero(bridge->identity) &&
  1516. num_bridge_auths;
  1517. ask_bridge_directly = !can_use_bridge_authority ||
  1518. !options->UpdateBridgesFromAuthority;
  1519. log_debug(LD_DIR, "ask_bridge_directly=%d (%d, %d, %d)",
  1520. ask_bridge_directly, tor_digest_is_zero(bridge->identity),
  1521. !options->UpdateBridgesFromAuthority, !num_bridge_auths);
  1522. if (ask_bridge_directly &&
  1523. !fascist_firewall_allows_address_or(&bridge->addr, bridge->port)) {
  1524. log_notice(LD_DIR, "Bridge at '%s' isn't reachable by our "
  1525. "firewall policy. %s.",
  1526. fmt_addrport(&bridge->addr, bridge->port),
  1527. can_use_bridge_authority ?
  1528. "Asking bridge authority instead" : "Skipping");
  1529. if (can_use_bridge_authority)
  1530. ask_bridge_directly = 0;
  1531. else
  1532. continue;
  1533. }
  1534. if (ask_bridge_directly) {
  1535. /* we need to ask the bridge itself for its descriptor. */
  1536. launch_direct_bridge_descriptor_fetch(bridge);
  1537. } else {
  1538. /* We have a digest and we want to ask an authority. We could
  1539. * combine all the requests into one, but that may give more
  1540. * hints to the bridge authority than we want to give. */
  1541. char resource[10 + HEX_DIGEST_LEN];
  1542. memcpy(resource, "fp/", 3);
  1543. base16_encode(resource+3, HEX_DIGEST_LEN+1,
  1544. bridge->identity, DIGEST_LEN);
  1545. memcpy(resource+3+HEX_DIGEST_LEN, ".z", 3);
  1546. log_info(LD_DIR, "Fetching bridge info '%s' from bridge authority.",
  1547. resource);
  1548. directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
  1549. ROUTER_PURPOSE_BRIDGE, resource, 0);
  1550. }
  1551. }
  1552. SMARTLIST_FOREACH_END(bridge);
  1553. }
  1554. /** If our <b>bridge</b> is configured to be a different address than
  1555. * the bridge gives in <b>node</b>, rewrite the routerinfo
  1556. * we received to use the address we meant to use. Now we handle
  1557. * multihomed bridges better.
  1558. */
  1559. static void
  1560. rewrite_node_address_for_bridge(const bridge_info_t *bridge, node_t *node)
  1561. {
  1562. /* XXXX move this function. */
  1563. /* XXXX overridden addresses should really live in the node_t, so that the
  1564. * routerinfo_t and the microdesc_t can be immutable. But we can only
  1565. * do that safely if we know that no function that connects to an OR
  1566. * does so through an address from any source other than node_get_addr().
  1567. */
  1568. tor_addr_t addr;
  1569. if (node->ri) {
  1570. routerinfo_t *ri = node->ri;
  1571. tor_addr_from_ipv4h(&addr, ri->addr);
  1572. if ((!tor_addr_compare(&bridge->addr, &addr, CMP_EXACT) &&
  1573. bridge->port == ri->or_port) ||
  1574. (!tor_addr_compare(&bridge->addr, &ri->ipv6_addr, CMP_EXACT) &&
  1575. bridge->port == ri->ipv6_orport)) {
  1576. /* they match, so no need to do anything */
  1577. } else {
  1578. if (tor_addr_family(&bridge->addr) == AF_INET) {
  1579. ri->addr = tor_addr_to_ipv4h(&bridge->addr);
  1580. tor_free(ri->address);
  1581. ri->address = tor_dup_ip(ri->addr);
  1582. ri->or_port = bridge->port;
  1583. log_info(LD_DIR,
  1584. "Adjusted bridge routerinfo for '%s' to match configured "
  1585. "address %s:%d.",
  1586. ri->nickname, ri->address, ri->or_port);
  1587. } else if (tor_addr_family(&bridge->addr) == AF_INET6) {
  1588. tor_addr_copy(&ri->ipv6_addr, &bridge->addr);
  1589. ri->ipv6_orport = bridge->port;
  1590. log_info(LD_DIR,
  1591. "Adjusted bridge routerinfo for '%s' to match configured "
  1592. "address %s.",
  1593. ri->nickname, fmt_addrport(&ri->ipv6_addr, ri->ipv6_orport));
  1594. } else {
  1595. log_err(LD_BUG, "Address family not supported: %d.",
  1596. tor_addr_family(&bridge->addr));
  1597. return;
  1598. }
  1599. }
  1600. /* Mark which address to use based on which bridge_t we got. */
  1601. node->ipv6_preferred = (tor_addr_family(&bridge->addr) == AF_INET6 &&
  1602. !tor_addr_is_null(&node->ri->ipv6_addr));
  1603. /* XXXipv6 we lack support for falling back to another address for
  1604. the same relay, warn the user */
  1605. if (!tor_addr_is_null(&ri->ipv6_addr)) {
  1606. tor_addr_port_t ap;
  1607. node_get_pref_orport(node, &ap);
  1608. log_notice(LD_CONFIG,
  1609. "Bridge '%s' has both an IPv4 and an IPv6 address. "
  1610. "Will prefer using its %s address (%s).",
  1611. ri->nickname,
  1612. tor_addr_family(&ap.addr) == AF_INET6 ? "IPv6" : "IPv4",
  1613. fmt_addrport(&ap.addr, ap.port));
  1614. }
  1615. }
  1616. if (node->rs) {
  1617. routerstatus_t *rs = node->rs;
  1618. tor_addr_from_ipv4h(&addr, rs->addr);
  1619. if (!tor_addr_compare(&bridge->addr, &addr, CMP_EXACT) &&
  1620. bridge->port == rs->or_port) {
  1621. /* they match, so no need to do anything */
  1622. } else {
  1623. rs->addr = tor_addr_to_ipv4h(&bridge->addr);
  1624. rs->or_port = bridge->port;
  1625. log_info(LD_DIR,
  1626. "Adjusted bridge routerstatus for '%s' to match "
  1627. "configured address %s.",
  1628. rs->nickname, fmt_addrport(&bridge->addr, rs->or_port));
  1629. }
  1630. }
  1631. }
  1632. /** We just learned a descriptor for a bridge. See if that
  1633. * digest is in our entry guard list, and add it if not. */
  1634. void
  1635. learned_bridge_descriptor(routerinfo_t *ri, int from_cache)
  1636. {
  1637. tor_assert(ri);
  1638. tor_assert(ri->purpose == ROUTER_PURPOSE_BRIDGE);
  1639. if (get_options()->UseBridges) {
  1640. int first = !any_bridge_descriptors_known();
  1641. bridge_info_t *bridge = get_configured_bridge_by_routerinfo(ri);
  1642. time_t now = time(NULL);
  1643. router_set_status(ri->cache_info.identity_digest, 1);
  1644. if (bridge) { /* if we actually want to use this one */
  1645. node_t *node;
  1646. /* it's here; schedule its re-fetch for a long time from now. */
  1647. if (!from_cache)
  1648. download_status_reset(&bridge->fetch_status);
  1649. node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  1650. tor_assert(node);
  1651. rewrite_node_address_for_bridge(bridge, node);
  1652. add_an_entry_guard(node, 1, 1);
  1653. log_notice(LD_DIR, "new bridge descriptor '%s' (%s): %s", ri->nickname,
  1654. from_cache ? "cached" : "fresh", router_describe(ri));
  1655. /* set entry->made_contact so if it goes down we don't drop it from
  1656. * our entry node list */
  1657. entry_guard_register_connect_status(ri->cache_info.identity_digest,
  1658. 1, 0, now);
  1659. if (first)
  1660. routerlist_retry_directory_downloads(now);
  1661. }
  1662. }
  1663. }
  1664. /** Return 1 if any of our entry guards have descriptors that
  1665. * are marked with purpose 'bridge' and are running. Else return 0.
  1666. *
  1667. * We use this function to decide if we're ready to start building
  1668. * circuits through our bridges, or if we need to wait until the
  1669. * directory "server/authority" requests finish. */
  1670. int
  1671. any_bridge_descriptors_known(void)
  1672. {
  1673. tor_assert(get_options()->UseBridges);
  1674. return choose_random_entry(NULL)!=NULL ? 1 : 0;
  1675. }
  1676. /** Return 1 if there are any directory conns fetching bridge descriptors
  1677. * that aren't marked for close. We use this to guess if we should tell
  1678. * the controller that we have a problem. */
  1679. int
  1680. any_pending_bridge_descriptor_fetches(void)
  1681. {
  1682. smartlist_t *conns = get_connection_array();
  1683. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  1684. if (conn->type == CONN_TYPE_DIR &&
  1685. conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
  1686. TO_DIR_CONN(conn)->router_purpose == ROUTER_PURPOSE_BRIDGE &&
  1687. !conn->marked_for_close &&
  1688. conn->linked &&
  1689. conn->linked_conn && !conn->linked_conn->marked_for_close) {
  1690. log_debug(LD_DIR, "found one: %s", conn->address);
  1691. return 1;
  1692. }
  1693. } SMARTLIST_FOREACH_END(conn);
  1694. return 0;
  1695. }
  1696. /** Return 1 if we have at least one descriptor for an entry guard
  1697. * (bridge or member of EntryNodes) and all descriptors we know are
  1698. * down. Else return 0. If <b>act</b> is 1, then mark the down guards
  1699. * up; else just observe and report. */
  1700. static int
  1701. entries_retry_helper(const or_options_t *options, int act)
  1702. {
  1703. const node_t *node;
  1704. int any_known = 0;
  1705. int any_running = 0;
  1706. int need_bridges = options->UseBridges != 0;
  1707. if (!entry_guards)
  1708. entry_guards = smartlist_new();
  1709. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) {
  1710. node = node_get_by_id(e->identity);
  1711. if (node && node_has_descriptor(node) &&
  1712. node_is_bridge(node) == need_bridges) {
  1713. any_known = 1;
  1714. if (node->is_running)
  1715. any_running = 1; /* some entry is both known and running */
  1716. else if (act) {
  1717. /* Mark all current connections to this OR as unhealthy, since
  1718. * otherwise there could be one that started 30 seconds
  1719. * ago, and in 30 seconds it will time out, causing us to mark
  1720. * the node down and undermine the retry attempt. We mark even
  1721. * the established conns, since if the network just came back
  1722. * we'll want to attach circuits to fresh conns. */
  1723. connection_or_set_bad_connections(node->identity, 1);
  1724. /* mark this entry node for retry */
  1725. router_set_status(node->identity, 1);
  1726. e->can_retry = 1;
  1727. e->bad_since = 0;
  1728. }
  1729. }
  1730. } SMARTLIST_FOREACH_END(e);
  1731. log_debug(LD_DIR, "%d: any_known %d, any_running %d",
  1732. act, any_known, any_running);
  1733. return any_known && !any_running;
  1734. }
  1735. /** Do we know any descriptors for our bridges / entrynodes, and are
  1736. * all the ones we have descriptors for down? */
  1737. int
  1738. entries_known_but_down(const or_options_t *options)
  1739. {
  1740. tor_assert(entry_list_is_constrained(options));
  1741. return entries_retry_helper(options, 0);
  1742. }
  1743. /** Mark all down known bridges / entrynodes up. */
  1744. void
  1745. entries_retry_all(const or_options_t *options)
  1746. {
  1747. tor_assert(entry_list_is_constrained(options));
  1748. entries_retry_helper(options, 1);
  1749. }
  1750. /** Return true if we've ever had a bridge running a Tor version that can't
  1751. * provide microdescriptors to us. In that case fall back to asking for
  1752. * full descriptors. Eventually all bridges will support microdescriptors
  1753. * and we can take this check out; see bug 4013. */
  1754. int
  1755. any_bridges_dont_support_microdescriptors(void)
  1756. {
  1757. const node_t *node;
  1758. static int ever_answered_yes = 0;
  1759. if (!get_options()->UseBridges || !entry_guards)
  1760. return 0;
  1761. if (ever_answered_yes)
  1762. return 1; /* if we ever answer 'yes', always answer 'yes' */
  1763. SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) {
  1764. node = node_get_by_id(e->identity);
  1765. if (node && node->ri &&
  1766. node_is_bridge(node) && node_is_a_configured_bridge(node) &&
  1767. !tor_version_supports_microdescriptors(node->ri->platform)) {
  1768. /* This is one of our current bridges, and we know enough about
  1769. * it to know that it won't be able to answer our microdescriptor
  1770. * questions. */
  1771. ever_answered_yes = 1;
  1772. return 1;
  1773. }
  1774. } SMARTLIST_FOREACH_END(e);
  1775. return 0;
  1776. }
  1777. /** Release all storage held by the list of entry guards and related
  1778. * memory structs. */
  1779. void
  1780. entry_guards_free_all(void)
  1781. {
  1782. if (entry_guards) {
  1783. SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
  1784. entry_guard_free(e));
  1785. smartlist_free(entry_guards);
  1786. entry_guards = NULL;
  1787. }
  1788. clear_bridge_list();
  1789. smartlist_free(bridge_list);
  1790. bridge_list = NULL;
  1791. circuit_build_times_free_timeouts(&circ_times);
  1792. }