bridges.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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-2016, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file bridges.c
  8. * \brief Code to manage bridges and bridge selection.
  9. *
  10. * Bridges are fixed entry nodes, used for censorship circumvention.
  11. **/
  12. #include "or.h"
  13. #include "bridges.h"
  14. #include "circuitbuild.h"
  15. #include "config.h"
  16. #include "connection.h"
  17. #include "directory.h"
  18. #include "entrynodes.h"
  19. #include "nodelist.h"
  20. #include "policies.h"
  21. #include "router.h"
  22. #include "routerlist.h"
  23. #include "routerset.h"
  24. #include "transports.h"
  25. /** Information about a configured bridge. Currently this just matches the
  26. * ones in the torrc file, but one day we may be able to learn about new
  27. * bridges on our own, and remember them in the state file. */
  28. struct bridge_info_t {
  29. /** Address and port of the bridge, as configured by the user.*/
  30. tor_addr_port_t addrport_configured;
  31. /** Address of the bridge. */
  32. tor_addr_t addr;
  33. /** TLS port for the bridge. */
  34. uint16_t port;
  35. /** Boolean: We are re-parsing our bridge list, and we are going to remove
  36. * this one if we don't find it in the list of configured bridges. */
  37. unsigned marked_for_removal : 1;
  38. /** Expected identity digest, or all zero bytes if we don't know what the
  39. * digest should be. */
  40. char identity[DIGEST_LEN];
  41. /** Name of pluggable transport protocol taken from its config line. */
  42. char *transport_name;
  43. /** When should we next try to fetch a descriptor for this bridge? */
  44. download_status_t fetch_status;
  45. /** A smartlist of k=v values to be passed to the SOCKS proxy, if
  46. transports are used for this bridge. */
  47. smartlist_t *socks_args;
  48. };
  49. static void bridge_free(bridge_info_t *bridge);
  50. /** A list of configured bridges. Whenever we actually get a descriptor
  51. * for one, we add it as an entry guard. Note that the order of bridges
  52. * in this list does not necessarily correspond to the order of bridges
  53. * in the torrc. */
  54. static smartlist_t *bridge_list = NULL;
  55. /** Mark every entry of the bridge list to be removed on our next call to
  56. * sweep_bridge_list unless it has first been un-marked. */
  57. void
  58. mark_bridge_list(void)
  59. {
  60. if (!bridge_list)
  61. bridge_list = smartlist_new();
  62. SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b,
  63. b->marked_for_removal = 1);
  64. }
  65. /** Remove every entry of the bridge list that was marked with
  66. * mark_bridge_list if it has not subsequently been un-marked. */
  67. void
  68. sweep_bridge_list(void)
  69. {
  70. if (!bridge_list)
  71. bridge_list = smartlist_new();
  72. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
  73. if (b->marked_for_removal) {
  74. SMARTLIST_DEL_CURRENT(bridge_list, b);
  75. bridge_free(b);
  76. }
  77. } SMARTLIST_FOREACH_END(b);
  78. }
  79. /** Initialize the bridge list to empty, creating it if needed. */
  80. static void
  81. clear_bridge_list(void)
  82. {
  83. if (!bridge_list)
  84. bridge_list = smartlist_new();
  85. SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b, bridge_free(b));
  86. smartlist_clear(bridge_list);
  87. }
  88. /** Free the bridge <b>bridge</b>. */
  89. static void
  90. bridge_free(bridge_info_t *bridge)
  91. {
  92. if (!bridge)
  93. return;
  94. tor_free(bridge->transport_name);
  95. if (bridge->socks_args) {
  96. SMARTLIST_FOREACH(bridge->socks_args, char*, s, tor_free(s));
  97. smartlist_free(bridge->socks_args);
  98. }
  99. tor_free(bridge);
  100. }
  101. /** Return a list of all the configured bridges, as bridge_info_t pointers. */
  102. const smartlist_t *
  103. bridge_list_get(void)
  104. {
  105. if (!bridge_list)
  106. bridge_list = smartlist_new();
  107. return bridge_list;
  108. }
  109. /**
  110. * Given a <b>bridge</b>, return a pointer to its RSA identity digest, or
  111. * NULL if we don't know one for it.
  112. */
  113. const uint8_t *
  114. bridge_get_rsa_id_digest(const bridge_info_t *bridge)
  115. {
  116. tor_assert(bridge);
  117. if (tor_digest_is_zero(bridge->identity))
  118. return NULL;
  119. else
  120. return (const uint8_t *) bridge->identity;
  121. }
  122. /**
  123. * Given a <b>bridge</b>, return a pointer to its configured addr:port
  124. * combination.
  125. */
  126. const tor_addr_port_t *
  127. bridge_get_addr_port(const bridge_info_t *bridge)
  128. {
  129. tor_assert(bridge);
  130. return &bridge->addrport_configured;
  131. }
  132. /** If we have a bridge configured whose digest matches <b>digest</b>, or a
  133. * bridge with no known digest whose address matches any of the
  134. * tor_addr_port_t's in <b>orports</b>, return that bridge. Else return
  135. * NULL. */
  136. static bridge_info_t *
  137. get_configured_bridge_by_orports_digest(const char *digest,
  138. const smartlist_t *orports)
  139. {
  140. if (!bridge_list)
  141. return NULL;
  142. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
  143. {
  144. if (tor_digest_is_zero(bridge->identity)) {
  145. SMARTLIST_FOREACH_BEGIN(orports, tor_addr_port_t *, ap)
  146. {
  147. if (tor_addr_compare(&bridge->addr, &ap->addr, CMP_EXACT) == 0 &&
  148. bridge->port == ap->port)
  149. return bridge;
  150. }
  151. SMARTLIST_FOREACH_END(ap);
  152. }
  153. if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
  154. return bridge;
  155. }
  156. SMARTLIST_FOREACH_END(bridge);
  157. return NULL;
  158. }
  159. /** If we have a bridge configured whose digest matches <b>digest</b>, or a
  160. * bridge with no known digest whose address matches <b>addr</b>:<b>port</b>,
  161. * return that bridge. Else return NULL. If <b>digest</b> is NULL, check for
  162. * address/port matches only. */
  163. static bridge_info_t *
  164. get_configured_bridge_by_addr_port_digest(const tor_addr_t *addr,
  165. uint16_t port,
  166. const char *digest)
  167. {
  168. if (!bridge_list)
  169. return NULL;
  170. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
  171. {
  172. if ((tor_digest_is_zero(bridge->identity) || digest == NULL) &&
  173. !tor_addr_compare(&bridge->addr, addr, CMP_EXACT) &&
  174. bridge->port == port)
  175. return bridge;
  176. if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
  177. return bridge;
  178. }
  179. SMARTLIST_FOREACH_END(bridge);
  180. return NULL;
  181. }
  182. /** If we have a bridge configured whose digest matches <b>digest</b>, or a
  183. * bridge with no known digest whose address matches <b>addr</b>:<b>port</b>,
  184. * return 1. Else return 0. If <b>digest</b> is NULL, check for
  185. * address/port matches only. */
  186. int
  187. addr_is_a_configured_bridge(const tor_addr_t *addr,
  188. uint16_t port,
  189. const char *digest)
  190. {
  191. tor_assert(addr);
  192. return get_configured_bridge_by_addr_port_digest(addr, port, digest) ? 1 : 0;
  193. }
  194. /** If we have a bridge configured whose digest matches
  195. * <b>ei->identity_digest</b>, or a bridge with no known digest whose address
  196. * matches <b>ei->addr</b>:<b>ei->port</b>, return 1. Else return 0.
  197. * If <b>ei->onion_key</b> is NULL, check for address/port matches only. */
  198. int
  199. extend_info_is_a_configured_bridge(const extend_info_t *ei)
  200. {
  201. const char *digest = ei->onion_key ? ei->identity_digest : NULL;
  202. return addr_is_a_configured_bridge(&ei->addr, ei->port, digest);
  203. }
  204. /** Wrapper around get_configured_bridge_by_addr_port_digest() to look
  205. * it up via router descriptor <b>ri</b>. */
  206. static bridge_info_t *
  207. get_configured_bridge_by_routerinfo(const routerinfo_t *ri)
  208. {
  209. bridge_info_t *bi = NULL;
  210. smartlist_t *orports = router_get_all_orports(ri);
  211. bi = get_configured_bridge_by_orports_digest(ri->cache_info.identity_digest,
  212. orports);
  213. SMARTLIST_FOREACH(orports, tor_addr_port_t *, p, tor_free(p));
  214. smartlist_free(orports);
  215. return bi;
  216. }
  217. /** Return 1 if <b>ri</b> is one of our known bridges, else 0. */
  218. int
  219. routerinfo_is_a_configured_bridge(const routerinfo_t *ri)
  220. {
  221. return get_configured_bridge_by_routerinfo(ri) ? 1 : 0;
  222. }
  223. /** Return 1 if <b>node</b> is one of our configured bridges, else 0. */
  224. int
  225. node_is_a_configured_bridge(const node_t *node)
  226. {
  227. int retval = 0;
  228. smartlist_t *orports = node_get_all_orports(node);
  229. retval = get_configured_bridge_by_orports_digest(node->identity,
  230. orports) != NULL;
  231. SMARTLIST_FOREACH(orports, tor_addr_port_t *, p, tor_free(p));
  232. smartlist_free(orports);
  233. return retval;
  234. }
  235. /** We made a connection to a router at <b>addr</b>:<b>port</b>
  236. * without knowing its digest. Its digest turned out to be <b>digest</b>.
  237. * If it was a bridge, and we still don't know its digest, record it.
  238. */
  239. void
  240. learned_router_identity(const tor_addr_t *addr, uint16_t port,
  241. const char *digest)
  242. {
  243. bridge_info_t *bridge =
  244. get_configured_bridge_by_addr_port_digest(addr, port, digest);
  245. if (bridge && tor_digest_is_zero(bridge->identity)) {
  246. char *transport_info = NULL;
  247. const char *transport_name =
  248. find_transport_name_by_bridge_addrport(addr, port);
  249. if (transport_name)
  250. tor_asprintf(&transport_info, " (with transport '%s')", transport_name);
  251. memcpy(bridge->identity, digest, DIGEST_LEN);
  252. log_notice(LD_DIR, "Learned fingerprint %s for bridge %s%s.",
  253. hex_str(digest, DIGEST_LEN), fmt_addrport(addr, port),
  254. transport_info ? transport_info : "");
  255. tor_free(transport_info);
  256. // XXXX prop271 here. we will need to update the guard info too.
  257. }
  258. }
  259. /** Return true if <b>bridge</b> has the same identity digest as
  260. * <b>digest</b>. If <b>digest</b> is NULL, it matches
  261. * bridges with unspecified identity digests. */
  262. static int
  263. bridge_has_digest(const bridge_info_t *bridge, const char *digest)
  264. {
  265. if (digest)
  266. return tor_memeq(digest, bridge->identity, DIGEST_LEN);
  267. else
  268. return tor_digest_is_zero(bridge->identity);
  269. }
  270. /** We are about to add a new bridge at <b>addr</b>:<b>port</b>, with optional
  271. * <b>digest</b> and <b>transport_name</b>. Mark for removal any previously
  272. * existing bridge with the same address and port, and warn the user as
  273. * appropriate.
  274. */
  275. static void
  276. bridge_resolve_conflicts(const tor_addr_t *addr, uint16_t port,
  277. const char *digest, const char *transport_name)
  278. {
  279. /* Iterate the already-registered bridge list:
  280. If you find a bridge with the same adress and port, mark it for
  281. removal. It doesn't make sense to have two active bridges with
  282. the same IP:PORT. If the bridge in question has a different
  283. digest or transport than <b>digest</b>/<b>transport_name</b>,
  284. it's probably a misconfiguration and we should warn the user.
  285. */
  286. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge) {
  287. if (bridge->marked_for_removal)
  288. continue;
  289. if (tor_addr_eq(&bridge->addr, addr) && (bridge->port == port)) {
  290. bridge->marked_for_removal = 1;
  291. if (!bridge_has_digest(bridge, digest) ||
  292. strcmp_opt(bridge->transport_name, transport_name)) {
  293. /* warn the user */
  294. char *bridge_description_new, *bridge_description_old;
  295. tor_asprintf(&bridge_description_new, "%s:%s:%s",
  296. fmt_addrport(addr, port),
  297. digest ? hex_str(digest, DIGEST_LEN) : "",
  298. transport_name ? transport_name : "");
  299. tor_asprintf(&bridge_description_old, "%s:%s:%s",
  300. fmt_addrport(&bridge->addr, bridge->port),
  301. tor_digest_is_zero(bridge->identity) ?
  302. "" : hex_str(bridge->identity,DIGEST_LEN),
  303. bridge->transport_name ? bridge->transport_name : "");
  304. log_warn(LD_GENERAL,"Tried to add bridge '%s', but we found a conflict"
  305. " with the already registered bridge '%s'. We will discard"
  306. " the old bridge and keep '%s'. If this is not what you"
  307. " wanted, please change your configuration file accordingly.",
  308. bridge_description_new, bridge_description_old,
  309. bridge_description_new);
  310. tor_free(bridge_description_new);
  311. tor_free(bridge_description_old);
  312. }
  313. }
  314. } SMARTLIST_FOREACH_END(bridge);
  315. }
  316. /** Return True if we have a bridge that uses a transport with name
  317. * <b>transport_name</b>. */
  318. MOCK_IMPL(int,
  319. transport_is_needed, (const char *transport_name))
  320. {
  321. if (!bridge_list)
  322. return 0;
  323. SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
  324. if (bridge->transport_name &&
  325. !strcmp(bridge->transport_name, transport_name))
  326. return 1;
  327. } SMARTLIST_FOREACH_END(bridge);
  328. return 0;
  329. }
  330. /** Register the bridge information in <b>bridge_line</b> to the
  331. * bridge subsystem. Steals reference of <b>bridge_line</b>. */
  332. void
  333. bridge_add_from_config(bridge_line_t *bridge_line)
  334. {
  335. bridge_info_t *b;
  336. { /* Log the bridge we are about to register: */
  337. log_debug(LD_GENERAL, "Registering bridge at %s (transport: %s) (%s)",
  338. fmt_addrport(&bridge_line->addr, bridge_line->port),
  339. bridge_line->transport_name ?
  340. bridge_line->transport_name : "no transport",
  341. tor_digest_is_zero(bridge_line->digest) ?
  342. "no key listed" : hex_str(bridge_line->digest, DIGEST_LEN));
  343. if (bridge_line->socks_args) { /* print socks arguments */
  344. int i = 0;
  345. tor_assert(smartlist_len(bridge_line->socks_args) > 0);
  346. log_debug(LD_GENERAL, "Bridge uses %d SOCKS arguments:",
  347. smartlist_len(bridge_line->socks_args));
  348. SMARTLIST_FOREACH(bridge_line->socks_args, const char *, arg,
  349. log_debug(LD_CONFIG, "%d: %s", ++i, arg));
  350. }
  351. }
  352. bridge_resolve_conflicts(&bridge_line->addr,
  353. bridge_line->port,
  354. bridge_line->digest,
  355. bridge_line->transport_name);
  356. b = tor_malloc_zero(sizeof(bridge_info_t));
  357. tor_addr_copy(&b->addrport_configured.addr, &bridge_line->addr);
  358. b->addrport_configured.port = bridge_line->port;
  359. tor_addr_copy(&b->addr, &bridge_line->addr);
  360. b->port = bridge_line->port;
  361. memcpy(b->identity, bridge_line->digest, DIGEST_LEN);
  362. if (bridge_line->transport_name)
  363. b->transport_name = bridge_line->transport_name;
  364. b->fetch_status.schedule = DL_SCHED_BRIDGE;
  365. b->fetch_status.backoff = DL_SCHED_RANDOM_EXPONENTIAL;
  366. b->socks_args = bridge_line->socks_args;
  367. if (!bridge_list)
  368. bridge_list = smartlist_new();
  369. tor_free(bridge_line); /* Deallocate bridge_line now. */
  370. smartlist_add(bridge_list, b);
  371. }
  372. /** Return true iff <b>routerset</b> contains the bridge <b>bridge</b>. */
  373. static int
  374. routerset_contains_bridge(const routerset_t *routerset,
  375. const bridge_info_t *bridge)
  376. {
  377. int result;
  378. extend_info_t *extinfo;
  379. tor_assert(bridge);
  380. if (!routerset)
  381. return 0;
  382. extinfo = extend_info_new(
  383. NULL, bridge->identity, NULL, NULL, &bridge->addr, bridge->port);
  384. result = routerset_contains_extendinfo(routerset, extinfo);
  385. extend_info_free(extinfo);
  386. return result;
  387. }
  388. /** If <b>digest</b> is one of our known bridges, return it. */
  389. static bridge_info_t *
  390. find_bridge_by_digest(const char *digest)
  391. {
  392. SMARTLIST_FOREACH(bridge_list, bridge_info_t *, bridge,
  393. {
  394. if (tor_memeq(bridge->identity, digest, DIGEST_LEN))
  395. return bridge;
  396. });
  397. return NULL;
  398. }
  399. /** Given the <b>addr</b> and <b>port</b> of a bridge, if that bridge
  400. * supports a pluggable transport, return its name. Otherwise, return
  401. * NULL. */
  402. const char *
  403. find_transport_name_by_bridge_addrport(const tor_addr_t *addr, uint16_t port)
  404. {
  405. if (!bridge_list)
  406. return NULL;
  407. SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
  408. if (tor_addr_eq(&bridge->addr, addr) &&
  409. (bridge->port == port))
  410. return bridge->transport_name;
  411. } SMARTLIST_FOREACH_END(bridge);
  412. return NULL;
  413. }
  414. /** If <b>addr</b> and <b>port</b> match the address and port of a
  415. * bridge of ours that uses pluggable transports, place its transport
  416. * in <b>transport</b>.
  417. *
  418. * Return 0 on success (found a transport, or found a bridge with no
  419. * transport, or found no bridge); return -1 if we should be using a
  420. * transport, but the transport could not be found.
  421. */
  422. int
  423. get_transport_by_bridge_addrport(const tor_addr_t *addr, uint16_t port,
  424. const transport_t **transport)
  425. {
  426. *transport = NULL;
  427. if (!bridge_list)
  428. return 0;
  429. SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
  430. if (tor_addr_eq(&bridge->addr, addr) &&
  431. (bridge->port == port)) { /* bridge matched */
  432. if (bridge->transport_name) { /* it also uses pluggable transports */
  433. *transport = transport_get_by_name(bridge->transport_name);
  434. if (*transport == NULL) { /* it uses pluggable transports, but
  435. the transport could not be found! */
  436. return -1;
  437. }
  438. return 0;
  439. } else { /* bridge matched, but it doesn't use transports. */
  440. break;
  441. }
  442. }
  443. } SMARTLIST_FOREACH_END(bridge);
  444. *transport = NULL;
  445. return 0;
  446. }
  447. /** Return a smartlist containing all the SOCKS arguments that we
  448. * should pass to the SOCKS proxy. */
  449. const smartlist_t *
  450. get_socks_args_by_bridge_addrport(const tor_addr_t *addr, uint16_t port)
  451. {
  452. bridge_info_t *bridge = get_configured_bridge_by_addr_port_digest(addr,
  453. port,
  454. NULL);
  455. return bridge ? bridge->socks_args : NULL;
  456. }
  457. /** We need to ask <b>bridge</b> for its server descriptor. */
  458. static void
  459. launch_direct_bridge_descriptor_fetch(bridge_info_t *bridge)
  460. {
  461. const or_options_t *options = get_options();
  462. if (connection_get_by_type_addr_port_purpose(
  463. CONN_TYPE_DIR, &bridge->addr, bridge->port,
  464. DIR_PURPOSE_FETCH_SERVERDESC))
  465. return; /* it's already on the way */
  466. if (routerset_contains_bridge(options->ExcludeNodes, bridge)) {
  467. download_status_mark_impossible(&bridge->fetch_status);
  468. log_warn(LD_APP, "Not using bridge at %s: it is in ExcludeNodes.",
  469. safe_str_client(fmt_and_decorate_addr(&bridge->addr)));
  470. return;
  471. }
  472. /* Until we get a descriptor for the bridge, we only know one address for
  473. * it. */
  474. if (!fascist_firewall_allows_address_addr(&bridge->addr, bridge->port,
  475. FIREWALL_OR_CONNECTION, 0, 0)) {
  476. log_notice(LD_CONFIG, "Tried to fetch a descriptor directly from a "
  477. "bridge, but that bridge is not reachable through our "
  478. "firewall.");
  479. return;
  480. }
  481. directory_initiate_command(&bridge->addr, bridge->port,
  482. NULL, 0, /*no dirport*/
  483. bridge->identity,
  484. DIR_PURPOSE_FETCH_SERVERDESC,
  485. ROUTER_PURPOSE_BRIDGE,
  486. DIRIND_ONEHOP, "authority.z", NULL, 0, 0);
  487. }
  488. /** Fetching the bridge descriptor from the bridge authority returned a
  489. * "not found". Fall back to trying a direct fetch. */
  490. void
  491. retry_bridge_descriptor_fetch_directly(const char *digest)
  492. {
  493. bridge_info_t *bridge = find_bridge_by_digest(digest);
  494. if (!bridge)
  495. return; /* not found? oh well. */
  496. launch_direct_bridge_descriptor_fetch(bridge);
  497. }
  498. /** For each bridge in our list for which we don't currently have a
  499. * descriptor, fetch a new copy of its descriptor -- either directly
  500. * from the bridge or via a bridge authority. */
  501. void
  502. fetch_bridge_descriptors(const or_options_t *options, time_t now)
  503. {
  504. int num_bridge_auths = get_n_authorities(BRIDGE_DIRINFO);
  505. int ask_bridge_directly;
  506. int can_use_bridge_authority;
  507. if (!bridge_list)
  508. return;
  509. /* If we still have unconfigured managed proxies, don't go and
  510. connect to a bridge. */
  511. if (pt_proxies_configuration_pending())
  512. return;
  513. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
  514. {
  515. if (!download_status_is_ready(&bridge->fetch_status, now,
  516. IMPOSSIBLE_TO_DOWNLOAD))
  517. continue; /* don't bother, no need to retry yet */
  518. if (routerset_contains_bridge(options->ExcludeNodes, bridge)) {
  519. download_status_mark_impossible(&bridge->fetch_status);
  520. log_warn(LD_APP, "Not using bridge at %s: it is in ExcludeNodes.",
  521. safe_str_client(fmt_and_decorate_addr(&bridge->addr)));
  522. continue;
  523. }
  524. /* schedule another fetch as if this one will fail, in case it does */
  525. download_status_failed(&bridge->fetch_status, 0);
  526. can_use_bridge_authority = !tor_digest_is_zero(bridge->identity) &&
  527. num_bridge_auths;
  528. ask_bridge_directly = !can_use_bridge_authority ||
  529. !options->UpdateBridgesFromAuthority;
  530. log_debug(LD_DIR, "ask_bridge_directly=%d (%d, %d, %d)",
  531. ask_bridge_directly, tor_digest_is_zero(bridge->identity),
  532. !options->UpdateBridgesFromAuthority, !num_bridge_auths);
  533. if (ask_bridge_directly &&
  534. !fascist_firewall_allows_address_addr(&bridge->addr, bridge->port,
  535. FIREWALL_OR_CONNECTION, 0,
  536. 0)) {
  537. log_notice(LD_DIR, "Bridge at '%s' isn't reachable by our "
  538. "firewall policy. %s.",
  539. fmt_addrport(&bridge->addr, bridge->port),
  540. can_use_bridge_authority ?
  541. "Asking bridge authority instead" : "Skipping");
  542. if (can_use_bridge_authority)
  543. ask_bridge_directly = 0;
  544. else
  545. continue;
  546. }
  547. if (ask_bridge_directly) {
  548. /* we need to ask the bridge itself for its descriptor. */
  549. launch_direct_bridge_descriptor_fetch(bridge);
  550. } else {
  551. /* We have a digest and we want to ask an authority. We could
  552. * combine all the requests into one, but that may give more
  553. * hints to the bridge authority than we want to give. */
  554. char resource[10 + HEX_DIGEST_LEN];
  555. memcpy(resource, "fp/", 3);
  556. base16_encode(resource+3, HEX_DIGEST_LEN+1,
  557. bridge->identity, DIGEST_LEN);
  558. memcpy(resource+3+HEX_DIGEST_LEN, ".z", 3);
  559. log_info(LD_DIR, "Fetching bridge info '%s' from bridge authority.",
  560. resource);
  561. directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
  562. ROUTER_PURPOSE_BRIDGE, resource, 0, DL_WANT_AUTHORITY);
  563. }
  564. }
  565. SMARTLIST_FOREACH_END(bridge);
  566. }
  567. /** If our <b>bridge</b> is configured to be a different address than
  568. * the bridge gives in <b>node</b>, rewrite the routerinfo
  569. * we received to use the address we meant to use. Now we handle
  570. * multihomed bridges better.
  571. */
  572. static void
  573. rewrite_node_address_for_bridge(const bridge_info_t *bridge, node_t *node)
  574. {
  575. /* XXXX move this function. */
  576. /* XXXX overridden addresses should really live in the node_t, so that the
  577. * routerinfo_t and the microdesc_t can be immutable. But we can only
  578. * do that safely if we know that no function that connects to an OR
  579. * does so through an address from any source other than node_get_addr().
  580. */
  581. tor_addr_t addr;
  582. const or_options_t *options = get_options();
  583. if (node->ri) {
  584. routerinfo_t *ri = node->ri;
  585. tor_addr_from_ipv4h(&addr, ri->addr);
  586. if ((!tor_addr_compare(&bridge->addr, &addr, CMP_EXACT) &&
  587. bridge->port == ri->or_port) ||
  588. (!tor_addr_compare(&bridge->addr, &ri->ipv6_addr, CMP_EXACT) &&
  589. bridge->port == ri->ipv6_orport)) {
  590. /* they match, so no need to do anything */
  591. } else {
  592. if (tor_addr_family(&bridge->addr) == AF_INET) {
  593. ri->addr = tor_addr_to_ipv4h(&bridge->addr);
  594. ri->or_port = bridge->port;
  595. log_info(LD_DIR,
  596. "Adjusted bridge routerinfo for '%s' to match configured "
  597. "address %s:%d.",
  598. ri->nickname, fmt_addr32(ri->addr), ri->or_port);
  599. } else if (tor_addr_family(&bridge->addr) == AF_INET6) {
  600. tor_addr_copy(&ri->ipv6_addr, &bridge->addr);
  601. ri->ipv6_orport = bridge->port;
  602. log_info(LD_DIR,
  603. "Adjusted bridge routerinfo for '%s' to match configured "
  604. "address %s.",
  605. ri->nickname, fmt_addrport(&ri->ipv6_addr, ri->ipv6_orport));
  606. } else {
  607. log_err(LD_BUG, "Address family not supported: %d.",
  608. tor_addr_family(&bridge->addr));
  609. return;
  610. }
  611. }
  612. if (options->ClientPreferIPv6ORPort == -1) {
  613. /* Mark which address to use based on which bridge_t we got. */
  614. node->ipv6_preferred = (tor_addr_family(&bridge->addr) == AF_INET6 &&
  615. !tor_addr_is_null(&node->ri->ipv6_addr));
  616. } else {
  617. /* Mark which address to use based on user preference */
  618. node->ipv6_preferred = (fascist_firewall_prefer_ipv6_orport(options) &&
  619. !tor_addr_is_null(&node->ri->ipv6_addr));
  620. }
  621. /* XXXipv6 we lack support for falling back to another address for
  622. the same relay, warn the user */
  623. if (!tor_addr_is_null(&ri->ipv6_addr)) {
  624. tor_addr_port_t ap;
  625. node_get_pref_orport(node, &ap);
  626. log_notice(LD_CONFIG,
  627. "Bridge '%s' has both an IPv4 and an IPv6 address. "
  628. "Will prefer using its %s address (%s) based on %s.",
  629. ri->nickname,
  630. node->ipv6_preferred ? "IPv6" : "IPv4",
  631. fmt_addrport(&ap.addr, ap.port),
  632. options->ClientPreferIPv6ORPort == -1 ?
  633. "the configured Bridge address" :
  634. "ClientPreferIPv6ORPort");
  635. }
  636. }
  637. if (node->rs) {
  638. routerstatus_t *rs = node->rs;
  639. tor_addr_from_ipv4h(&addr, rs->addr);
  640. if (!tor_addr_compare(&bridge->addr, &addr, CMP_EXACT) &&
  641. bridge->port == rs->or_port) {
  642. /* they match, so no need to do anything */
  643. } else {
  644. rs->addr = tor_addr_to_ipv4h(&bridge->addr);
  645. rs->or_port = bridge->port;
  646. log_info(LD_DIR,
  647. "Adjusted bridge routerstatus for '%s' to match "
  648. "configured address %s.",
  649. rs->nickname, fmt_addrport(&bridge->addr, rs->or_port));
  650. }
  651. }
  652. }
  653. /** We just learned a descriptor for a bridge. See if that
  654. * digest is in our entry guard list, and add it if not. */
  655. void
  656. learned_bridge_descriptor(routerinfo_t *ri, int from_cache)
  657. {
  658. tor_assert(ri);
  659. tor_assert(ri->purpose == ROUTER_PURPOSE_BRIDGE);
  660. if (get_options()->UseBridges) {
  661. int first = num_bridges_usable() <= 1;
  662. bridge_info_t *bridge = get_configured_bridge_by_routerinfo(ri);
  663. time_t now = time(NULL);
  664. router_set_status(ri->cache_info.identity_digest, 1);
  665. if (bridge) { /* if we actually want to use this one */
  666. node_t *node;
  667. /* it's here; schedule its re-fetch for a long time from now. */
  668. if (!from_cache)
  669. download_status_reset(&bridge->fetch_status);
  670. node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  671. tor_assert(node);
  672. rewrite_node_address_for_bridge(bridge, node);
  673. if (tor_digest_is_zero(bridge->identity)) {
  674. memcpy(bridge->identity,ri->cache_info.identity_digest, DIGEST_LEN);
  675. log_notice(LD_DIR, "Learned identity %s for bridge at %s:%d",
  676. hex_str(bridge->identity, DIGEST_LEN),
  677. fmt_and_decorate_addr(&bridge->addr),
  678. (int) bridge->port);
  679. }
  680. // XXXX prop271 here we will need to update the guard info too.
  681. add_bridge_as_entry_guard(get_guard_selection_info(), node);
  682. log_notice(LD_DIR, "new bridge descriptor '%s' (%s): %s", ri->nickname,
  683. from_cache ? "cached" : "fresh", router_describe(ri));
  684. /* set entry->made_contact so if it goes down we don't drop it from
  685. * our entry node list */
  686. // XXXX prop271 use new interface here when we hit bridges?
  687. entry_guard_register_connect_status(ri->cache_info.identity_digest,
  688. 1, 0, now);
  689. if (first) {
  690. routerlist_retry_directory_downloads(now);
  691. }
  692. }
  693. }
  694. }
  695. /** Return the number of bridges that have descriptors that
  696. * are marked with purpose 'bridge' and are running.
  697. *
  698. * We use this function to decide if we're ready to start building
  699. * circuits through our bridges, or if we need to wait until the
  700. * directory "server/authority" requests finish. */
  701. int
  702. any_bridge_descriptors_known(void)
  703. {
  704. tor_assert(get_options()->UseBridges);
  705. // XXXX prop271 this needs to get fixed. -- bridges
  706. return choose_random_entry(NULL) != NULL;
  707. }
  708. /** Return a smartlist containing all bridge identity digests */
  709. MOCK_IMPL(smartlist_t *,
  710. list_bridge_identities, (void))
  711. {
  712. smartlist_t *result = NULL;
  713. char *digest_tmp;
  714. if (get_options()->UseBridges && bridge_list) {
  715. result = smartlist_new();
  716. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
  717. digest_tmp = tor_malloc(DIGEST_LEN);
  718. memcpy(digest_tmp, b->identity, DIGEST_LEN);
  719. smartlist_add(result, digest_tmp);
  720. } SMARTLIST_FOREACH_END(b);
  721. }
  722. return result;
  723. }
  724. /** Get the download status for a bridge descriptor given its identity */
  725. MOCK_IMPL(download_status_t *,
  726. get_bridge_dl_status_by_id, (const char *digest))
  727. {
  728. download_status_t *dl = NULL;
  729. if (digest && get_options()->UseBridges && bridge_list) {
  730. SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
  731. if (tor_memeq(digest, b->identity, DIGEST_LEN)) {
  732. dl = &(b->fetch_status);
  733. break;
  734. }
  735. } SMARTLIST_FOREACH_END(b);
  736. }
  737. return dl;
  738. }
  739. /** Release all storage held in bridges.c */
  740. void
  741. bridges_free_all(void)
  742. {
  743. clear_bridge_list();
  744. smartlist_free(bridge_list);
  745. bridge_list = NULL;
  746. }