entrynodes.c 69 KB

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