bridges.c 33 KB

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