bridges.c 28 KB

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