bridges.c 33 KB

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