bridges.c 33 KB

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