bridges.c 28 KB

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