bridges.c 33 KB

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